Skip to content

Commit

Permalink
tighten up the API for DiffusionData, #284
Browse files Browse the repository at this point in the history
(cherry picked from commit 023cf87)
  • Loading branch information
jbphet authored and pixelzoom committed Jul 15, 2024
1 parent 1cc8831 commit 09833ea
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions js/diffusion/model/DiffusionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import Disposable from '../../../../axon/js/Disposable.js';
import NumberProperty from '../../../../axon/js/NumberProperty.js';
import Property from '../../../../axon/js/Property.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import Bounds2 from '../../../../dot/js/Bounds2.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import NullableIO from '../../../../tandem/js/types/NullableIO.js';
Expand All @@ -26,40 +27,45 @@ export default class DiffusionData {
private readonly particleSystem: DiffusionParticleSystem;

// number of DiffusionParticle1 in this half of the container
public readonly numberOfParticles1Property: Property<number>;
public readonly numberOfParticles1Property: TReadOnlyProperty<number>;
private readonly _numberOfParticles1Property: Property<number>;

// number of DiffusionParticle2 in this half of the container
public readonly numberOfParticles2Property: Property<number>;
public readonly numberOfParticles2Property: TReadOnlyProperty<number>;
private readonly _numberOfParticles2Property: Property<number>;

// average temperature in this half of the container, in K
// null when there are no particles in this half of the container.
public readonly averageTemperatureProperty: Property<number | null>;
public readonly averageTemperatureProperty: TReadOnlyProperty<number | null>;
private readonly _averageTemperatureProperty: Property<number | null>;

public constructor( bounds: Bounds2, particleSystem: DiffusionParticleSystem,
leftOrRightString: 'left' | 'right', tandem: Tandem ) {

this.bounds = bounds;
this.particleSystem = particleSystem;

this.numberOfParticles1Property = new NumberProperty( 0, {
this._numberOfParticles1Property = new NumberProperty( 0, {
numberType: 'Integer',
isValidValue: value => ( value >= 0 ),
tandem: tandem.createTandem( 'numberOfParticles1Property' ),
phetioReadOnly: true, // derived from the state of the particle system
phetioFeatured: true,
phetioDocumentation: `Number of particles of type 1 that are in the ${leftOrRightString} half of the container.`
} );
this.numberOfParticles1Property = this._numberOfParticles1Property;

this.numberOfParticles2Property = new NumberProperty( 0, {
this._numberOfParticles2Property = new NumberProperty( 0, {
numberType: 'Integer',
isValidValue: value => ( value >= 0 ),
tandem: tandem.createTandem( 'numberOfParticles2Property' ),
phetioReadOnly: true, // derived from the state of the particle system
phetioFeatured: true,
phetioDocumentation: `Number of particles of type 2 that are in the ${leftOrRightString} half of the container.`
} );
this.numberOfParticles2Property = this._numberOfParticles2Property;

this.averageTemperatureProperty = new Property<number | null>( null, {
this._averageTemperatureProperty = new Property<number | null>( null, {
units: 'K',
isValidValue: value => ( value === null || value > 0 ),
phetioValueType: NullableIO( NumberIO ),
Expand All @@ -68,6 +74,7 @@ export default class DiffusionData {
phetioFeatured: true,
phetioDocumentation: `Average temperature in the ${leftOrRightString} half of the container.`
} );
this.averageTemperatureProperty = this._averageTemperatureProperty;

this.update();
}
Expand Down Expand Up @@ -105,20 +112,20 @@ export default class DiffusionData {
}

// Update number of particles
this.numberOfParticles1Property.value = numberOfParticles1;
this.numberOfParticles2Property.value = numberOfParticles2;
this._numberOfParticles1Property.value = numberOfParticles1;
this._numberOfParticles2Property.value = numberOfParticles2;

// Update average temperature
const totalNumberOfParticles = numberOfParticles1 + numberOfParticles2;
if ( totalNumberOfParticles === 0 ) {
this.averageTemperatureProperty.value = null;
this._averageTemperatureProperty.value = null;
}
else {
assert && assert( totalKE !== 0, 'totalKE should not be zero when the container is not empty' );

// T = (2/3)KE/k
const averageKE = totalKE / totalNumberOfParticles;
this.averageTemperatureProperty.value = ( 2 / 3 ) * averageKE / GasPropertiesConstants.BOLTZMANN; // K
this._averageTemperatureProperty.value = ( 2 / 3 ) * averageKE / GasPropertiesConstants.BOLTZMANN; // K
}
}
}
Expand Down

0 comments on commit 09833ea

Please sign in to comment.