Skip to content

Commit

Permalink
instrument DiffusionModel, #231
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed May 4, 2024
1 parent 0428dfa commit 32bb7a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion js/common/model/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type SelfOptions = {
};

export type BaseModelOptions = SelfOptions &
PickOptional<PhetioObjectOptions, 'phetioState'> & // because subclass DiffusionModel has state
PickOptional<PhetioObjectOptions, 'phetioState' | 'phetioType'> & // because subclass DiffusionModel has state
PickRequired<PhetioObjectOptions, 'tandem'>;

export default class BaseModel extends PhetioObject implements TModel {
Expand Down
56 changes: 52 additions & 4 deletions js/diffusion/model/DiffusionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ import gasProperties from '../../gasProperties.js';
import DiffusionCollisionDetector from './DiffusionCollisionDetector.js';
import DiffusionContainer from './DiffusionContainer.js';
import DiffusionData from './DiffusionData.js';
import DiffusionParticle1 from './DiffusionParticle1.js';
import DiffusionParticle2 from './DiffusionParticle2.js';
import DiffusionParticle1, { DiffusionParticle1StateObject } from './DiffusionParticle1.js';
import DiffusionParticle2, { DiffusionParticle2StateObject } from './DiffusionParticle2.js';
import DiffusionSettings from './DiffusionSettings.js';
import ParticleFlowRateModel from './ParticleFlowRateModel.js';
import Particle, { ParticleOptions } from '../../common/model/Particle.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import { combineOptions } from '../../../../phet-core/js/optionize.js';
import isSettingPhetioStateProperty from '../../../../tandem/js/isSettingPhetioStateProperty.js';
import DiffusionParticle from './DiffusionParticle.js';
import ArrayIO from '../../../../tandem/js/types/ArrayIO.js';
import IOType from '../../../../tandem/js/types/IOType.js';

// constants
const CENTER_OF_MASS_PROPERTY_OPTIONS = {
Expand All @@ -44,12 +46,21 @@ const CENTER_OF_MASS_PROPERTY_OPTIONS = {
// Options to createParticle functions
type CreateParticleOptions = PickRequired<ParticleOptions, 'mass' | 'radius'>;

type DiffusionModelStateObject = {
particles1: DiffusionParticle1StateObject[];
particles2: DiffusionParticle2StateObject[];
};

const DIFFUSION_MODEL_STATE_SCHEMA = {
particles1: ArrayIO( DiffusionParticle1.DiffusionParticle1IO ),
particles2: ArrayIO( DiffusionParticle2.DiffusionParticle2IO )
};

export default class DiffusionModel extends BaseModel {

public readonly container: DiffusionContainer;

// particles of each species, together these make up the 'particle system'
//TODO https://github.com/phetsims/gas-properties/issues/77 PhET-iO instrumentation?
public readonly particles1: DiffusionParticle1[];
public readonly particles2: DiffusionParticle2[];

Expand Down Expand Up @@ -84,7 +95,9 @@ export default class DiffusionModel extends BaseModel {
modelOriginOffset: new Vector2( 670, 520 ),
stopwatchPosition: new Vector2( 60, 50 ),
hasTimeSpeedFeature: true,
tandem: tandem
tandem: tandem,
phetioType: DiffusionModel.DiffusionModelIO,
phetioState: true // Override phetioState: false in superclass BaseModel.
} );

this.container = new DiffusionContainer( tandem.createTandem( 'container' ) );
Expand Down Expand Up @@ -299,6 +312,41 @@ export default class DiffusionModel extends BaseModel {
this.leftData.update( this.particles1, this.particles2 );
this.rightData.update( this.particles1, this.particles2 );
}

/**
* Serializes an instance of DiffusionModel.
*/
private toStateObject(): DiffusionModelStateObject {
return {
particles1: this.particles1.map( particle => DiffusionParticle1.DiffusionParticle1IO.toStateObject( particle ) ),
particles2: this.particles2.map( particle => DiffusionParticle2.DiffusionParticle2IO.toStateObject( particle ) )
};
}

/**
* Deserializes an instance of DiffusionModel.
*/
private static applyState( diffusionModel: DiffusionModel, stateObject: DiffusionModelStateObject ): void {

diffusionModel.particles1.length = 0;
diffusionModel.particles2.length = 0;

stateObject.particles1.forEach( ( stateObject: DiffusionParticle1StateObject ) => {
diffusionModel.particles1.push( DiffusionParticle1.DiffusionParticle1IO.fromStateObject( stateObject ) );
} );

stateObject.particles2.forEach( ( stateObject: DiffusionParticle2StateObject ) => {
diffusionModel.particles2.push( DiffusionParticle2.DiffusionParticle2IO.fromStateObject( stateObject ) );
} );
}

public static readonly DiffusionModelIO = new IOType<DiffusionModel, DiffusionModelStateObject>( 'DiffusionModelIO', {
valueType: DiffusionModel,
defaultDeserializationMethod: 'applyState',
stateSchema: DIFFUSION_MODEL_STATE_SCHEMA,
toStateObject: diffusionModel => diffusionModel.toStateObject(),
applyState: DiffusionModel.applyState
} );
}

/**
Expand Down

0 comments on commit 32bb7a2

Please sign in to comment.