Skip to content

Commit

Permalink
use ReferenceArrayIO and default applyState, #77
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed May 13, 2024
1 parent af317b4 commit 17c9699
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 109 deletions.
36 changes: 7 additions & 29 deletions js/common/model/IdealGasLawParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import optionize from '../../../../phet-core/js/optionize.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import ArrayIO from '../../../../tandem/js/types/ArrayIO.js';
import isSettingPhetioStateProperty from '../../../../tandem/js/isSettingPhetioStateProperty.js';
import ReferenceArrayIO from '../../../../tandem/js/types/ReferenceArrayIO.js';

// used to compute the initial velocity angle for particles, in radians
const PARTICLE_DISPERSION_ANGLE = Math.PI / 2;
Expand All @@ -55,10 +55,10 @@ type IdealGasLawParticleSystemStateObject = {

// This should match IdealGasLawParticleSystemStateObject, but with IOTypes.
const IDEAL_GAS_LAW_PARTICLE_SYSTEM_STATE_SCHEMA = {
heavyParticles: ArrayIO( HeavyParticle.HeavyParticleIO ),
lightParticles: ArrayIO( LightParticle.LightParticleIO ),
heavyParticlesOutside: ArrayIO( HeavyParticle.HeavyParticleIO ),
lightParticlesOutside: ArrayIO( LightParticle.LightParticleIO )
heavyParticles: ReferenceArrayIO( HeavyParticle.HeavyParticleIO ),
lightParticles: ReferenceArrayIO( LightParticle.LightParticleIO ),
heavyParticlesOutside: ReferenceArrayIO( HeavyParticle.HeavyParticleIO ),
lightParticlesOutside: ReferenceArrayIO( LightParticle.LightParticleIO )
};

export default class IdealGasLawParticleSystem extends PhetioObject {
Expand Down Expand Up @@ -384,37 +384,15 @@ export default class IdealGasLawParticleSystem extends PhetioObject {
ParticleUtils.getTotalKineticEnergy( this.lightParticles );
}

/**
* Deserializes an instance of IdealGasLawParticleSystem.
*/
private static applyState( particleSystem: IdealGasLawParticleSystem, stateObject: IdealGasLawParticleSystemStateObject ): void {

particleSystem.heavyParticles.length = 0;
stateObject.heavyParticles.forEach( ( stateObject: HeavyParticleStateObject ) =>
particleSystem.heavyParticles.push( HeavyParticle.HeavyParticleIO.fromStateObject( stateObject ) ) );

particleSystem.lightParticles.length = 0;
stateObject.lightParticles.forEach( ( stateObject: LightParticleStateObject ) =>
particleSystem.lightParticles.push( LightParticle.LightParticleIO.fromStateObject( stateObject ) ) );

particleSystem.heavyParticlesOutside.length = 0;
stateObject.heavyParticlesOutside.forEach( ( stateObject: HeavyParticleStateObject ) =>
particleSystem.heavyParticlesOutside.push( HeavyParticle.HeavyParticleIO.fromStateObject( stateObject ) ) );

particleSystem.lightParticlesOutside.length = 0;
stateObject.lightParticlesOutside.forEach( ( stateObject: LightParticleStateObject ) =>
particleSystem.lightParticlesOutside.push( LightParticle.LightParticleIO.fromStateObject( stateObject ) ) );
}

/**
* IdealGasLawParticleSystemIO 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.
*/
private static readonly IdealGasLawParticleSystemIO = new IOType<IdealGasLawParticleSystem, IdealGasLawParticleSystemStateObject>( 'IdealGasLawParticleSystemIO', {
valueType: IdealGasLawParticleSystem,
stateSchema: IDEAL_GAS_LAW_PARTICLE_SYSTEM_STATE_SCHEMA,
stateSchema: IDEAL_GAS_LAW_PARTICLE_SYSTEM_STATE_SCHEMA
// toStateObject: Use the default, which is derived from stateSchema.
applyState: IdealGasLawParticleSystem.applyState
// applyState: Use the default, which is derived from stateSchema.
} );
}

Expand Down
24 changes: 5 additions & 19 deletions js/diffusion/model/DiffusionParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import Property, { PropertyOptions } from '../../../../axon/js/Property.js';
import { combineOptions } from '../../../../phet-core/js/optionize.js';
import NullableIO from '../../../../tandem/js/types/NullableIO.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import ArrayIO from '../../../../tandem/js/types/ArrayIO.js';
import ParticleFlowRateModel from './ParticleFlowRateModel.js';
import ReferenceArrayIO from '../../../../tandem/js/types/ReferenceArrayIO.js';

const CENTER_OF_MASS_PROPERTY_OPTIONS = {
units: 'pm',
Expand All @@ -47,8 +47,8 @@ type DiffusionParticleSystemStateObject = {

// This should match DiffusionParticleSystemStateObject, but with IOTypes.
const DIFFUSION_PARTICLE_SYSTEM_SCHEMA = {
particles1: ArrayIO( DiffusionParticle1.DiffusionParticle1IO ),
particles2: ArrayIO( DiffusionParticle2.DiffusionParticle2IO )
particles1: ReferenceArrayIO( DiffusionParticle1.DiffusionParticle1IO ),
particles2: ReferenceArrayIO( DiffusionParticle2.DiffusionParticle2IO )
};

// Options to createParticle functions
Expand Down Expand Up @@ -251,29 +251,15 @@ export default class DiffusionParticleSystem extends PhetioObject {
this.centerOfMass2Property.value = ParticleUtils.getCenterXOfMass( this.particles2, this.container.width / 2 );
}

/**
* Deserializes an instance of DiffusionParticleSystem.
*/
private static applyState( particleSystem: DiffusionParticleSystem, stateObject: DiffusionParticleSystemStateObject ): void {

particleSystem.particles1.length = 0;
stateObject.particles1.forEach( ( stateObject: DiffusionParticle1StateObject ) =>
particleSystem.particles1.push( DiffusionParticle1.DiffusionParticle1IO.fromStateObject( stateObject ) ) );

particleSystem.particles2.length = 0;
stateObject.particles2.forEach( ( stateObject: DiffusionParticle2StateObject ) =>
particleSystem.particles2.push( DiffusionParticle2.DiffusionParticle2IO.fromStateObject( stateObject ) ) );
}

/**
* 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.
*/
private static readonly DiffusionParticleSystemIO = new IOType<DiffusionParticleSystem, DiffusionParticleSystemStateObject>( 'DiffusionParticleSystemIO', {
valueType: DiffusionParticleSystem,
stateSchema: DIFFUSION_PARTICLE_SYSTEM_SCHEMA,
stateSchema: DIFFUSION_PARTICLE_SYSTEM_SCHEMA
// toStateObject: Use the default, which is derived from stateSchema.
applyState: DiffusionParticleSystem.applyState
// applyState: Use the default, which is derived from stateSchema.
} );
}

Expand Down
27 changes: 6 additions & 21 deletions js/diffusion/model/ParticleFlowRateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import Tandem from '../../../../tandem/js/Tandem.js';
import Particle from '../../common/model/Particle.js';
import gasProperties from '../../gasProperties.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import ArrayIO from '../../../../tandem/js/types/ArrayIO.js';
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import ReferenceArrayIO from '../../../../tandem/js/types/ReferenceArrayIO.js';

const FLOW_RATE_PROPERTY_OPTIONS: PropertyOptions<number> = {
isValidValue: value => ( value >= 0 ),
Expand All @@ -37,9 +37,9 @@ type ParticleFlowRateModelStateObject = {

// This should match ParticleFlowRateModelStateObject, but with IOTypes.
const PARTICLE_FLOW_RATE_MODEL_STATE_SCHEMA = {
dts: ArrayIO( NumberIO ),
leftCounts: ArrayIO( NumberIO ),
rightCounts: ArrayIO( NumberIO )
dts: ReferenceArrayIO( NumberIO ),
leftCounts: ReferenceArrayIO( NumberIO ),
rightCounts: ReferenceArrayIO( NumberIO )
};

export default class ParticleFlowRateModel extends PhetioObject {
Expand Down Expand Up @@ -155,30 +155,15 @@ export default class ParticleFlowRateModel extends PhetioObject {
this.rightFlowRateProperty.value = rightAverage / dtAverage;
}

/**
* Deserializes an instance of ParticleFlowRateModel.
*/
private static applyState( particleFlowRateModel: ParticleFlowRateModel, stateObject: ParticleFlowRateModelStateObject ): void {

particleFlowRateModel.dts.length = 0;
particleFlowRateModel.dts.push( ...stateObject.dts );

particleFlowRateModel.leftCounts.length = 0;
particleFlowRateModel.leftCounts.push( ...stateObject.leftCounts );

particleFlowRateModel.rightCounts.length = 0;
particleFlowRateModel.rightCounts.push( ...stateObject.rightCounts );
}

/**
* ParticleFlowRateModelIO handles serialization of the particle flow rate model. It implements reference-type serialization,
* as described in https://github.com/phetsims/phet-io/blob/main/doc/phet-io-instrumentation-technical-guide.md#serialization.
*/
private static readonly ParticleFlowRateModelIO = new IOType<ParticleFlowRateModel, ParticleFlowRateModelStateObject>( 'ParticleFlowRateModelIO', {
valueType: ParticleFlowRateModel,
stateSchema: PARTICLE_FLOW_RATE_MODEL_STATE_SCHEMA,
stateSchema: PARTICLE_FLOW_RATE_MODEL_STATE_SCHEMA
// toStateObject: Use the default, which is derived from stateSchema.
applyState: ParticleFlowRateModel.applyState
// applyState: Use the default, which is derived from stateSchema.
} );
}

Expand Down
14 changes: 2 additions & 12 deletions js/energy/model/AverageSpeedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,15 @@ export default class AverageSpeedModel extends PhetioObject {
this.clearSamples();
}

/**
* Deserializes an instance of AverageSpeedModel.
*/
private static applyState( averageSpeedModel: AverageSpeedModel, stateObject: AverageSpeedModelStateObject ): void {
averageSpeedModel.dtAccumulator = stateObject.dtAccumulator;
averageSpeedModel.numberOfSamples = stateObject.numberOfSamples;
averageSpeedModel.heavyAverageSpeedSum = stateObject.heavyAverageSpeedSum;
averageSpeedModel.lightAverageSpeedSum = stateObject.lightAverageSpeedSum;
}

/**
* AverageSpeedModelIO handles serialization of the average speed model. It implements reference-type serialization,
* as described in https://github.com/phetsims/phet-io/blob/main/doc/phet-io-instrumentation-technical-guide.md#serialization.
*/
private static readonly AverageSpeedModelIO = new IOType<AverageSpeedModel, AverageSpeedModelStateObject>( 'AverageSpeedModelIO', {
valueType: AverageSpeedModel,
stateSchema: AVERAGE_SPEED_MODEL_STATE_SCHEMA,
stateSchema: AVERAGE_SPEED_MODEL_STATE_SCHEMA
// toStateObject: Use the default, which is derived from stateSchema.
applyState: AverageSpeedModel.applyState
// applyState: Use the default, which is derived from stateSchema.
} );
}

Expand Down
35 changes: 7 additions & 28 deletions js/energy/model/HistogramsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import gasProperties from '../../gasProperties.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import isSettingPhetioStateProperty from '../../../../tandem/js/isSettingPhetioStateProperty.js';
import ReferenceArrayIO from '../../../../tandem/js/types/ReferenceArrayIO.js';

// Describes the properties of the histograms at a specific zoom level.
type ZoomLevel = {
Expand All @@ -48,10 +49,10 @@ type HistogramsModelStateObject = {
const HISTOGRAMS_MODEL_STATE_SCHEMA = {
dtAccumulator: NumberIO,
numberOfSamples: NumberIO,
heavySpeedSamples: ArrayIO( ArrayIO( NumberIO ) ),
lightSpeedSamples: ArrayIO( ArrayIO( NumberIO ) ),
heavyKineticEnergySamples: ArrayIO( ArrayIO( NumberIO ) ),
lightKineticEnergySamples: ArrayIO( ArrayIO( NumberIO ) )
heavySpeedSamples: ReferenceArrayIO( ArrayIO( NumberIO ) ),
lightSpeedSamples: ReferenceArrayIO( ArrayIO( NumberIO ) ),
heavyKineticEnergySamples: ReferenceArrayIO( ArrayIO( NumberIO ) ),
lightKineticEnergySamples: ReferenceArrayIO( ArrayIO( NumberIO ) )
};

export default class HistogramsModel extends PhetioObject {
Expand Down Expand Up @@ -333,37 +334,15 @@ export default class HistogramsModel extends PhetioObject {
this.clearSamples();
}

/**
* Deserializes an instance of HistogramsModel.
*/
private static applyState( histogramsModel: HistogramsModel, stateObject: HistogramsModelStateObject ): void {

histogramsModel.dtAccumulator = stateObject.dtAccumulator;
histogramsModel.numberOfSamples = stateObject.numberOfSamples;

//TODO https://github.com/phetsims/gas-properties/issues/77 Does this work correctly for number[][] ?
histogramsModel.heavySpeedSamples.length = 0;
histogramsModel.heavySpeedSamples.push( ...stateObject.heavySpeedSamples );

histogramsModel.lightSpeedSamples.length = 0;
histogramsModel.lightSpeedSamples.push( ...stateObject.lightSpeedSamples );

histogramsModel.heavyKineticEnergySamples.length = 0;
histogramsModel.heavyKineticEnergySamples.push( ...stateObject.heavyKineticEnergySamples );

histogramsModel.lightKineticEnergySamples.length = 0;
histogramsModel.lightKineticEnergySamples.push( ...stateObject.lightKineticEnergySamples );
}

/**
* HistogramModelIO handles serialization of the histograms model. It implements reference-type serialization,
* as described in https://github.com/phetsims/phet-io/blob/main/doc/phet-io-instrumentation-technical-guide.md#serialization.
*/
private static readonly HistogramsModelIO = new IOType<HistogramsModel, HistogramsModelStateObject>( 'HistogramsModelIO', {
valueType: HistogramsModel,
stateSchema: HISTOGRAMS_MODEL_STATE_SCHEMA,
stateSchema: HISTOGRAMS_MODEL_STATE_SCHEMA
// toStateObject: Use the default, which is derived from stateSchema.
applyState: HistogramsModel.applyState
// applyState: Use the default, which is derived from stateSchema.
} );
}

Expand Down

0 comments on commit 17c9699

Please sign in to comment.