From 17c96990b03c90f9460bdbfb3b592480d146cb85 Mon Sep 17 00:00:00 2001 From: pixelzoom Date: Mon, 13 May 2024 15:39:02 -0600 Subject: [PATCH] use ReferenceArrayIO and default applyState, https://github.com/phetsims/gas-properties/issues/77 --- js/common/model/IdealGasLawParticleSystem.ts | 36 ++++--------------- js/diffusion/model/DiffusionParticleSystem.ts | 24 +++---------- js/diffusion/model/ParticleFlowRateModel.ts | 27 ++++---------- js/energy/model/AverageSpeedModel.ts | 14 ++------ js/energy/model/HistogramsModel.ts | 35 ++++-------------- 5 files changed, 27 insertions(+), 109 deletions(-) diff --git a/js/common/model/IdealGasLawParticleSystem.ts b/js/common/model/IdealGasLawParticleSystem.ts index 976d874f..488d376b 100644 --- a/js/common/model/IdealGasLawParticleSystem.ts +++ b/js/common/model/IdealGasLawParticleSystem.ts @@ -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; @@ -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 { @@ -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( '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. } ); } diff --git a/js/diffusion/model/DiffusionParticleSystem.ts b/js/diffusion/model/DiffusionParticleSystem.ts index d5c488a4..31dfb4b6 100644 --- a/js/diffusion/model/DiffusionParticleSystem.ts +++ b/js/diffusion/model/DiffusionParticleSystem.ts @@ -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', @@ -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 @@ -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( '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. } ); } diff --git a/js/diffusion/model/ParticleFlowRateModel.ts b/js/diffusion/model/ParticleFlowRateModel.ts index a81ccd6d..a562e63e 100644 --- a/js/diffusion/model/ParticleFlowRateModel.ts +++ b/js/diffusion/model/ParticleFlowRateModel.ts @@ -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 = { isValidValue: value => ( value >= 0 ), @@ -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 { @@ -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( '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. } ); } diff --git a/js/energy/model/AverageSpeedModel.ts b/js/energy/model/AverageSpeedModel.ts index 20f32532..95bee415 100644 --- a/js/energy/model/AverageSpeedModel.ts +++ b/js/energy/model/AverageSpeedModel.ts @@ -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( '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. } ); } diff --git a/js/energy/model/HistogramsModel.ts b/js/energy/model/HistogramsModel.ts index 800a4bd0..bc9ffc41 100644 --- a/js/energy/model/HistogramsModel.ts +++ b/js/energy/model/HistogramsModel.ts @@ -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 = { @@ -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 { @@ -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( '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. } ); }