Skip to content

Commit

Permalink
fix serialization of Diffusion particles, #284
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jul 17, 2024
1 parent 2d705ed commit cab2dc5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
4 changes: 2 additions & 2 deletions js/common/view/GasPropertiesIconFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,14 @@ const GasPropertiesIconFactory = {
* Creates an icon for particle type 1 in the Diffusion screen.
*/
createDiffusionParticle1Icon( modelViewTransform: ModelViewTransform2, options?: ParticleNodeOptions ): Node {
return createParticleIcon( new DiffusionParticle1(), modelViewTransform, options );
return createParticleIcon( DiffusionParticle1.withDefaults(), modelViewTransform, options );
},

/**
* Creates an icon for particle type 2 in the Diffusion screen.
*/
createDiffusionParticle2Icon( modelViewTransform: ModelViewTransform2, options?: ParticleNodeOptions ): Node {
return createParticleIcon( new DiffusionParticle2(), modelViewTransform, options );
return createParticleIcon( DiffusionParticle2.withDefaults(), modelViewTransform, options );
},

/**
Expand Down
1 change: 1 addition & 0 deletions js/diffusion/model/DiffusionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class DiffusionData {
* Updates Properties based on the contents of the particle arrays.
*/
public update(): void {
assert && this.particleSystem.verify();

let numberOfParticles1 = 0;
let numberOfParticles2 = 0;
Expand Down
15 changes: 5 additions & 10 deletions js/diffusion/model/DiffusionParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@
* @author Chris Malley (PixelZoom, Inc.)
*/

import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import Particle, { ParticleOptions } from '../../common/model/Particle.js';
import GasPropertiesConstants from '../../common/GasPropertiesConstants.js';
import gasProperties from '../../gasProperties.js';
import WithOptional from '../../../../phet-core/js/types/WithOptional.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';

type SelfOptions = EmptySelfOptions;

export type DiffusionParticleOptions = SelfOptions & WithOptional<ParticleOptions, 'mass' | 'radius'>;
export type DiffusionParticleOptions = SelfOptions &
WithRequired<ParticleOptions, 'mass' | 'radius' | 'colorProperty' | 'highlightColorProperty'>;

export default class DiffusionParticle extends Particle {

protected constructor( providedOptions: DiffusionParticleOptions ) {
super( optionize<DiffusionParticleOptions, SelfOptions, ParticleOptions>()( {

// ParticleOptions
mass: GasPropertiesConstants.DIFFUSION_MASS_RANGE.defaultValue,
radius: GasPropertiesConstants.DIFFUSION_RADIUS_RANGE.defaultValue
}, providedOptions ) );
super( providedOptions );
}

/**
Expand Down
17 changes: 15 additions & 2 deletions js/diffusion/model/DiffusionParticle1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import gasProperties from '../../gasProperties.js';
import DiffusionParticle, { DiffusionParticleOptions } from './DiffusionParticle.js';
import { ParticleStateObject } from '../../common/model/Particle.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import GasPropertiesConstants from '../../common/GasPropertiesConstants.js';

type SelfOptions = EmptySelfOptions;

Expand All @@ -25,7 +26,7 @@ export type DiffusionParticle1StateObject = ParticleStateObject;

export default class DiffusionParticle1 extends DiffusionParticle {

public constructor( providedOptions?: DiffusionParticle1Options ) {
public constructor( providedOptions: DiffusionParticle1Options ) {
super( optionize<DiffusionParticle1Options, SelfOptions, DiffusionParticleOptions>()( {

// DiffusionParticleOptions
Expand All @@ -34,6 +35,16 @@ export default class DiffusionParticle1 extends DiffusionParticle {
}, providedOptions ) );
}

/**
* Creates a DiffusionParticle1 with default configuration. This is used for creating icons.
*/
public static withDefaults(): DiffusionParticle1 {
return new DiffusionParticle1( {
mass: GasPropertiesConstants.DIFFUSION_MASS_RANGE.defaultValue,
radius: GasPropertiesConstants.DIFFUSION_RADIUS_RANGE.defaultValue
} );
}

/**
* Deserializes an instance of DiffusionParticle1.
*/
Expand All @@ -44,7 +55,9 @@ export default class DiffusionParticle1 extends DiffusionParticle {
previousX: stateObject._previousX,
previousY: stateObject._previousY,
vx: stateObject.vx,
vy: stateObject.vy
vy: stateObject.vy,
mass: stateObject.mass,
radius: stateObject.radius
} );
}

Expand Down
17 changes: 15 additions & 2 deletions js/diffusion/model/DiffusionParticle2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import gasProperties from '../../gasProperties.js';
import DiffusionParticle, { DiffusionParticleOptions } from './DiffusionParticle.js';
import { ParticleStateObject } from '../../common/model/Particle.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import GasPropertiesConstants from '../../common/GasPropertiesConstants.js';

type SelfOptions = EmptySelfOptions;

Expand All @@ -25,7 +26,7 @@ export type DiffusionParticle2StateObject = ParticleStateObject;

export default class DiffusionParticle2 extends DiffusionParticle {

public constructor( providedOptions?: DiffusionParticle2Options ) {
public constructor( providedOptions: DiffusionParticle2Options ) {
super( optionize<DiffusionParticle2Options, SelfOptions, DiffusionParticleOptions>()( {

// ParticleOptions
Expand All @@ -34,6 +35,16 @@ export default class DiffusionParticle2 extends DiffusionParticle {
}, providedOptions ) );
}

/**
* Creates a DiffusionParticle2 with default configuration. This is used for creating icons.
*/
public static withDefaults(): DiffusionParticle2 {
return new DiffusionParticle2( {
mass: GasPropertiesConstants.DIFFUSION_MASS_RANGE.defaultValue,
radius: GasPropertiesConstants.DIFFUSION_RADIUS_RANGE.defaultValue
} );
}

/**
* Deserializes an instance of DiffusionParticle1.
*/
Expand All @@ -44,7 +55,9 @@ export default class DiffusionParticle2 extends DiffusionParticle {
previousX: stateObject._previousX,
previousY: stateObject._previousY,
vx: stateObject.vx,
vy: stateObject.vy
vy: stateObject.vy,
mass: stateObject.mass,
radius: stateObject.radius
} );
}

Expand Down
20 changes: 20 additions & 0 deletions js/diffusion/model/DiffusionParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ export default class DiffusionParticleSystem extends PhetioObject {
this.centerOfMass2Property.value = ParticleUtils.getCenterXOfMass( this.particles2, this.container.width / 2 );
}

/**
* Verifies that the particles system is in a consistent state.
*/
public verify(): void {
this.verifyParticles( this.particles1, this.particle1Settings );
this.verifyParticles( this.particles2, this.particle2Settings );
}

/**
* Verifies that all particles of the same type have the same mass and radius.
*/
private verifyParticles( particles: DiffusionParticle[], settings: DiffusionSettings ): void {
if ( particles.length > 0 ) {
assert && assert( _.every( particles, particle => ( particle.mass === settings.massProperty.value ) ),
'Particles do not have consistent mass.' );
assert && assert( _.every( particles, particle => ( particle.radius === settings.radiusProperty.value ) ),
'Particles do not have consistent radius.' );
}
}

/**
* DiffusionParticleSystemIO handles serialization of the particle arrays. It implements reference-type serialization,
* as described in https://github.com/phetsims/phet-io/blob/main/doc/phet-io-instrumentation-technical-guide.md#serialization.
Expand Down
4 changes: 2 additions & 2 deletions js/diffusion/view/DiffusionParticleSystemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export default class DiffusionParticleSystemNode extends ParticlesNode {

// generated canvas for DiffusionParticle1 species
const particle1CanvasProperty = new DiffusionParticleCanvasProperty(
new DiffusionParticle1(),
DiffusionParticle1.withDefaults(),
model.modelViewTransform,
model.particleSystem.particle1Settings.radiusProperty
);

// generated canvas for DiffusionParticle2 species
const particle2CanvasProperty = new DiffusionParticleCanvasProperty(
new DiffusionParticle2(),
DiffusionParticle2.withDefaults(),
model.modelViewTransform,
model.particleSystem.particle2Settings.radiusProperty
);
Expand Down

0 comments on commit cab2dc5

Please sign in to comment.