-
Notifications
You must be signed in to change notification settings - Fork 6
/
DiffusionParticleSystem.ts
367 lines (310 loc) · 16.1 KB
/
DiffusionParticleSystem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2018-2024, University of Colorado Boulder
/**
* DiffusionParticleSystem is the particle system for the 'Diffusion' screen.
*
* @author Chris Malley (PixelZoom, Inc.)
*/
import gasProperties from '../../gasProperties.js';
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
import DiffusionParticle1, { DiffusionParticle1StateObject } from './DiffusionParticle1.js';
import DiffusionParticle2, { DiffusionParticle2StateObject } from './DiffusionParticle2.js';
import DiffusionSettings from './DiffusionSettings.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import isSettingPhetioStateProperty from '../../../../tandem/js/isSettingPhetioStateProperty.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import ParticleUtils from '../../common/model/ParticleUtils.js';
import Multilink from '../../../../axon/js/Multilink.js';
import GasPropertiesConstants from '../../common/GasPropertiesConstants.js';
import DiffusionParticle from './DiffusionParticle.js';
import Bounds2 from '../../../../dot/js/Bounds2.js';
import DiffusionContainer from './DiffusionContainer.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import Particle, { ParticleOptions } from '../../common/model/Particle.js';
import dotRandom from '../../../../dot/js/dotRandom.js';
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 ParticleFlowRateModel from './ParticleFlowRateModel.js';
import ReferenceArrayIO from '../../../../tandem/js/types/ReferenceArrayIO.js';
import { Units } from '../../../../axon/js/units.js';
const CENTER_OF_MASS_PROPERTY_OPTIONS = {
units: 'pm' as Units,
valueType: [ 'number', null ],
phetioValueType: NullableIO( NumberIO ),
phetioReadOnly: true // derived from the state of the particle system
};
// This should match STATE_SCHEMA, but with JavaScript types.
type DiffusionParticleSystemStateObject = {
particles1: DiffusionParticle1StateObject[];
particles2: DiffusionParticle2StateObject[];
};
// This should match DiffusionParticleSystemStateObject, but with IOTypes.
const STATE_SCHEMA = {
particles1: ReferenceArrayIO( DiffusionParticle1.DiffusionParticle1IO ),
particles2: ReferenceArrayIO( DiffusionParticle2.DiffusionParticle2IO )
};
// This is the documentation that appears for DiffusionParticleSystemIO in Studio.
// Each field in STATE_SCHEMA should be described, in the same order as STATE_SCHEMA.
const IO_TYPE_DOCUMENTATION =
'PhET-iO Type for the particle system. Fields include:<br>' +
'<ul>' +
'<li>particles1: particles of type 1 that are inside the container<br>' +
'<li>particles2: particles of type 2 that are inside the container<br>' +
'</ul>';
// Options to createParticle functions
type CreateParticleOptions = PickRequired<ParticleOptions, 'mass' | 'radius'>;
export default class DiffusionParticleSystem extends PhetioObject {
private readonly container: DiffusionContainer;
private readonly isPlayingProperty: TReadOnlyProperty<boolean>;
// Particles of each species, together these make up the 'particle system'.
public readonly particles1: DiffusionParticle1[];
public readonly particles2: DiffusionParticle2[];
// Settings for the particle types 1 and 2, before the divider is removed.
public readonly particle1Settings: DiffusionSettings;
public readonly particle2Settings: DiffusionSettings;
// N, the total number of particles in the container
public readonly numberOfParticlesProperty: TReadOnlyProperty<number>;
// Center of mass for each particle species, in pm, relative to the center (divider) of the container.
// null when there are no particles in the container. This is actually an x offset, not a 'center' Vector2.
// But we decided to keep these Property names so that they match the associated checkbox labels.
// See https://github.com/phetsims/gas-properties/issues/228
public readonly centerOfMass1Property: Property<number | null>;
public readonly centerOfMass2Property: Property<number | null>;
// Flow rate model for each particle species.
public readonly particle1FlowRateModel: ParticleFlowRateModel;
public readonly particle2FlowRateModel: ParticleFlowRateModel;
public constructor( container: DiffusionContainer, isPlayingProperty: TReadOnlyProperty<boolean>, tandem: Tandem ) {
super( {
isDisposable: true,
tandem: tandem,
phetioType: DiffusionParticleSystem.DiffusionParticleSystemIO
} );
this.container = container;
this.isPlayingProperty = isPlayingProperty;
this.particles1 = [];
this.particles2 = [];
this.particle1Settings = new DiffusionSettings( tandem.createTandem( 'particle1Settings' ) );
this.particle2Settings = new DiffusionSettings( tandem.createTandem( 'particle2Settings' ) );
// Synchronize particle counts and arrays.
this.particle1Settings.numberOfParticlesProperty.link( numberOfParticles => {
if ( !isSettingPhetioStateProperty.value ) {
this.updateNumberOfParticles( numberOfParticles, this.particles1, this.particle1Settings, container.leftBounds,
( options: CreateParticleOptions ) => new DiffusionParticle1( options ) );
}
} );
this.particle2Settings.numberOfParticlesProperty.link( numberOfParticles => {
if ( !isSettingPhetioStateProperty.value ) {
this.updateNumberOfParticles( numberOfParticles, this.particles2, this.particle2Settings, container.rightBounds,
( options: CreateParticleOptions ) => new DiffusionParticle2( options ) );
}
} );
this.numberOfParticlesProperty = new DerivedProperty(
[ this.particle1Settings.numberOfParticlesProperty, this.particle2Settings.numberOfParticlesProperty ],
( numberOfParticles1, numberOfParticles2 ) => numberOfParticles1 + numberOfParticles2, {
isValidValue: value => ( Number.isInteger( value ) && value >= 0 ),
phetioValueType: NumberIO,
tandem: tandem.createTandem( 'numberOfParticlesProperty' ),
phetioFeatured: true,
phetioDocumentation: 'Total number of particles in the container.'
} );
// After PhET-iO state has been restored, verify the sanity of the particle system.
if ( assert && Tandem.PHET_IO_ENABLED ) {
phet.phetio.phetioEngine.phetioStateEngine.stateSetEmitter.addListener( () => {
assert && assert( this.particles1.length === this.particle1Settings.numberOfParticlesProperty.value, 'incorrect number of particles1' );
assert && assert( this.particles2.length === this.particle2Settings.numberOfParticlesProperty.value, 'incorrect number of particles2' );
} );
}
this.centerOfMass1Property = new Property<number | null>( null,
combineOptions<PropertyOptions<number | null>>( {}, CENTER_OF_MASS_PROPERTY_OPTIONS, {
tandem: tandem.createTandem( 'centerOfMass1Property' ),
phetioReadOnly: true,
phetioFeatured: true,
phetioDocumentation: 'Center of mass for particles of type 1. This is the x offset from the center of the container.'
} ) );
this.centerOfMass2Property = new Property<number | null>( null,
combineOptions<PropertyOptions<number | null>>( {}, CENTER_OF_MASS_PROPERTY_OPTIONS, {
tandem: tandem.createTandem( 'centerOfMass2Property' ),
phetioReadOnly: true,
phetioFeatured: true,
phetioDocumentation: 'Center of mass for particles of type 2. This is the x offset from the center of the container.'
} ) );
this.particle1FlowRateModel = new ParticleFlowRateModel( this.container.dividerX, this.particles1, tandem.createTandem( 'particle1FlowRateModel' ) );
this.particle2FlowRateModel = new ParticleFlowRateModel( this.container.dividerX, this.particles2, tandem.createTandem( 'particle2FlowRateModel' ) );
// Update mass and temperature of existing particles. This adjusts speed of the particles.
Multilink.multilink(
[ this.particle1Settings.massProperty, this.particle1Settings.initialTemperatureProperty ],
( mass, initialTemperature ) => {
if ( !isSettingPhetioStateProperty.value ) {
updateMassAndSpeed( mass, initialTemperature, this.particles1 );
}
} );
Multilink.multilink(
[ this.particle2Settings.massProperty, this.particle2Settings.initialTemperatureProperty ],
( mass, initialTemperature ) => {
if ( !isSettingPhetioStateProperty.value ) {
updateMassAndSpeed( mass, initialTemperature, this.particles2 );
}
} );
// Update radii of existing particles.
this.particle1Settings.radiusProperty.link( radius => {
if ( !isSettingPhetioStateProperty.value ) {
updateRadius( radius, this.particles1, container.leftBounds, isPlayingProperty.value );
}
} );
this.particle2Settings.radiusProperty.link( radius => {
if ( !isSettingPhetioStateProperty.value ) {
updateRadius( radius, this.particles2, container.rightBounds, isPlayingProperty.value );
}
} );
}
public reset(): void {
this.particle1Settings.reset();
this.particle2Settings.reset();
assert && assert( this.particles1.length === 0, 'there should be no DiffusionParticle1 particles' );
assert && assert( this.particles2.length === 0, 'there should be no DiffusionParticle2 particles' );
this.centerOfMass1Property.reset();
this.centerOfMass2Property.reset();
this.particle1FlowRateModel.reset();
this.particle2FlowRateModel.reset();
}
public restart(): void {
this.particle1Settings.restart();
this.particle2Settings.restart();
this.particle1FlowRateModel.reset();
this.particle2FlowRateModel.reset();
}
public step( dt: number ): void {
// Step particles
ParticleUtils.stepParticles( this.particles1, dt );
ParticleUtils.stepParticles( this.particles2, dt );
// Particle Flow Rate model
if ( !this.container.isDividedProperty.value ) {
this.particle1FlowRateModel.step( dt );
this.particle2FlowRateModel.step( dt );
}
}
/**
* Adjusts an array of particles to have the desired number of elements.
* @param numberOfParticles - desired number of particles
* @param particles - array of particles that corresponds to newValue and oldValue
* @param settings
* @param positionBounds - initial position will be inside this bounds
* @param createParticle - creates a Particle instance
*/
private updateNumberOfParticles( numberOfParticles: number,
particles: Particle[],
settings: DiffusionSettings,
positionBounds: Bounds2,
createParticle: ( options: CreateParticleOptions ) => Particle ): void {
assert && assert( !isSettingPhetioStateProperty.value, 'updateNumberOfParticles should not be called while setting state.' );
const delta = numberOfParticles - particles.length;
if ( delta !== 0 ) {
if ( delta > 0 ) {
addParticles( delta, particles, settings, positionBounds, createParticle );
}
else {
ParticleUtils.removeLastParticles( -delta, particles );
}
// If paused, update things that would normally be handled by step.
if ( !this.isPlayingProperty.value ) {
this.updateCenterOfMass();
}
}
}
/**
* Updates the center of mass, as shown by the center-of-mass indicators.
*/
public updateCenterOfMass(): void {
this.centerOfMass1Property.value = ParticleUtils.getCenterXOfMass( this.particles1, this.container.width / 2 );
this.centerOfMass2Property.value = ParticleUtils.getCenterXOfMass( this.particles2, this.container.width / 2 );
}
/**
* 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: STATE_SCHEMA,
documentation: IO_TYPE_DOCUMENTATION
// toStateObject: Use the default, which is derived from stateSchema.
// applyState: Use the default, which is derived from stateSchema.
} );
}
/**
* Adds n particles to the end of the specified array. Particles must be inside positionBounds.
*/
function addParticles( n: number,
particles: Particle[],
settings: DiffusionSettings,
positionBounds: Bounds2,
createParticle: ( options: CreateParticleOptions ) => Particle ): void {
assert && assert( n > 0 && Number.isInteger( n ), `invalid n: ${n}` );
assert && assert( !isSettingPhetioStateProperty.value, 'addParticles should not be called while setting state.' );
// Create n particles
for ( let i = 0; i < n; i++ ) {
const particle = createParticle( {
mass: settings.massProperty.value,
radius: settings.radiusProperty.value
} );
// Position the particle at a random position within positionBounds, accounting for particle radius.
const x = dotRandom.nextDoubleBetween( positionBounds.minX + particle.radius, positionBounds.maxX - particle.radius );
const y = dotRandom.nextDoubleBetween( positionBounds.minY + particle.radius, positionBounds.maxY - particle.radius );
particle.setXY( x, y );
assert && assert( positionBounds.containsCoordinates( particle.x, particle.y ), 'particle is outside of positionBounds' );
// Set the initial velocity, based on initial temperature and mass.
particle.setVelocityPolar(
// |v| = sqrt( 3kT / m )
Math.sqrt( 3 * GasPropertiesConstants.BOLTZMANN * settings.initialTemperatureProperty.value / particle.mass ),
// Random angle
dotRandom.nextDouble() * 2 * Math.PI
);
particles.push( particle );
}
}
/**
* Update the mass and speed for a set of particles. Speed is based on temperature and mass.
*/
function updateMassAndSpeed( mass: number, temperature: number, particles: DiffusionParticle[] ): void {
assert && assert( mass > 0, `invalid mass: ${mass}` );
assert && assert( temperature >= 0, `invalid temperature: ${temperature}` );
assert && assert( Array.isArray( particles ), `invalid particles: ${particles}` );
assert && assert( !isSettingPhetioStateProperty.value, 'updateMassAndSpeed should not be called while setting state.' );
for ( let i = particles.length - 1; i >= 0; i-- ) {
particles[ i ].setMass( mass );
// |v| = sqrt( 3kT / m )
particles[ i ].setSpeed( Math.sqrt( 3 * GasPropertiesConstants.BOLTZMANN * temperature / mass ) );
}
}
/**
* Updates the radius for a set of particles. Particles will be inside the specified bounds.
*/
function updateRadius( radius: number, particles: DiffusionParticle[], bounds: Bounds2, isPlaying: boolean ): void {
assert && assert( radius > 0, `invalid radius: ${radius}` );
assert && assert( !isSettingPhetioStateProperty.value, 'updateRadius should not be called while setting state.' );
for ( let i = particles.length - 1; i >= 0; i-- ) {
const particle = particles[ i ];
particle.setRadius( radius );
// If the sim is paused, then adjust the position of any particles are not fully inside the bounds.
// While the sim is playing, this adjustment will be handled by collision detection.
if ( !isPlaying ) {
// constrain horizontally
if ( particle.left < bounds.minX ) {
particle.left = bounds.minX;
}
else if ( particle.right > bounds.maxX ) {
particle.right = bounds.maxX;
}
// constrain vertically
if ( particle.bottom < bounds.minY ) {
particle.bottom = bounds.minY;
}
else if ( particle.top > bounds.maxY ) {
particle.top = bounds.maxY;
}
}
}
}
gasProperties.register( 'DiffusionParticleSystem', DiffusionParticleSystem );