Skip to content

Commit

Permalink
factor out DiffusionParticleSystem with IOType, #231
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed May 7, 2024
1 parent 679de44 commit 342d23d
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 398 deletions.
9 changes: 3 additions & 6 deletions js/common/model/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import gasProperties from '../../gasProperties.js';
import GasPropertiesConstants from '../GasPropertiesConstants.js';
import TimeTransform from './TimeTransform.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import PickOptional from '../../../../phet-core/js/types/PickOptional.js';

Expand All @@ -50,7 +50,7 @@ export type BaseModelOptions = SelfOptions &
PickOptional<PhetioObjectOptions, 'phetioState' | 'phetioType'> & // because subclass DiffusionModel has state
PickRequired<PhetioObjectOptions, 'tandem'>;

export default class BaseModel extends PhetioObject implements TModel {
export default class BaseModel implements TModel {

// transform between model and view coordinate frames
public readonly modelViewTransform: ModelViewTransform2;
Expand All @@ -77,12 +77,9 @@ export default class BaseModel extends PhetioObject implements TModel {
hasTimeSpeedFeature: false,

// PhetioObjectOptions
isDisposable: false,
phetioState: false
isDisposable: false
}, providedOptions );

super( options );

this.modelViewTransform = ModelViewTransform2.createOffsetXYScaleMapping(
options.modelOriginOffset,
MODEL_VIEW_SCALE,
Expand Down
9 changes: 3 additions & 6 deletions js/diffusion/model/DiffusionCollisionDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import Vector2 from '../../../../dot/js/Vector2.js';
import CollisionDetector from '../../common/model/CollisionDetector.js';
import gasProperties from '../../gasProperties.js';
import DiffusionContainer from './DiffusionContainer.js';
import DiffusionParticle1 from './DiffusionParticle1.js';
import DiffusionParticle2 from './DiffusionParticle2.js';
import DiffusionParticleSystem from './DiffusionParticleSystem.js';

export default class DiffusionCollisionDetector extends CollisionDetector {

private readonly diffusionContainer: DiffusionContainer;

public constructor( diffusionContainer: DiffusionContainer,
particles1: DiffusionParticle1[],
particles2: DiffusionParticle2[] ) {
super( diffusionContainer, [ particles1, particles2 ], new BooleanProperty( true ) );
public constructor( diffusionContainer: DiffusionContainer, particleSystem: DiffusionParticleSystem ) {
super( diffusionContainer, [ particleSystem.particles1, particleSystem.particles2 ], new BooleanProperty( true ) );
this.diffusionContainer = diffusionContainer;
}

Expand Down
17 changes: 8 additions & 9 deletions js/diffusion/model/DiffusionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import NullableIO from '../../../../tandem/js/types/NullableIO.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import GasPropertiesConstants from '../../common/GasPropertiesConstants.js';
import gasProperties from '../../gasProperties.js';
import DiffusionParticle1 from './DiffusionParticle1.js';
import DiffusionParticle2 from './DiffusionParticle2.js';
import DiffusionParticleSystem from './DiffusionParticleSystem.js';

// constants
const NUMBER_OF_PARTICLES_PROPERTY_OPTIONS: NumberPropertyOptions = {
Expand All @@ -42,7 +41,7 @@ export default class DiffusionData {
// null when there are no particles in this half of the container.
public readonly averageTemperatureProperty: Property<number | null>;

public constructor( bounds: Bounds2, particles1: DiffusionParticle1[], particles2: DiffusionParticle2[],
public constructor( bounds: Bounds2, particleSystem: DiffusionParticleSystem,
leftOrRightString: 'left' | 'right', tandem: Tandem ) {

this.bounds = bounds;
Expand Down Expand Up @@ -73,7 +72,7 @@ export default class DiffusionData {
phetioDocumentation: `Average temperature in the ${leftOrRightString} half of the container.`
} );

this.update( particles1, particles2 );
this.update( particleSystem );
}

public dispose(): void {
Expand All @@ -83,15 +82,15 @@ export default class DiffusionData {
/**
* Updates Properties based on the contents of the particle arrays.
*/
public update( particles1: DiffusionParticle1[], particles2: DiffusionParticle2[] ): void {
public update( particleSystem: DiffusionParticleSystem ): void {

let numberOfParticles1 = 0;
let numberOfParticles2 = 0;
let totalKE = 0;

// Contribution by DiffusionParticle1 species
for ( let i = particles1.length - 1; i >= 0; i-- ) {
const particle = particles1[ i ];
for ( let i = particleSystem.particles1.length - 1; i >= 0; i-- ) {
const particle = particleSystem.particles1[ i ];
if ( this.bounds.containsCoordinates( particle.x, particle.y ) ) {
numberOfParticles1++;
totalKE += particle.getKineticEnergy();
Expand All @@ -100,8 +99,8 @@ export default class DiffusionData {

// Contribution by DiffusionParticle2 species.
// Note that there's a wee bit of code duplication here, but it gains us some iteration efficiency.
for ( let i = particles2.length - 1; i >= 0; i-- ) {
const particle = particles2[ i ];
for ( let i = particleSystem.particles2.length - 1; i >= 0; i-- ) {
const particle = particleSystem.particles2[ i ];
if ( this.bounds.containsCoordinates( particle.x, particle.y ) ) {
numberOfParticles2++;
totalKE += particle.getKineticEnergy();
Expand Down
Loading

0 comments on commit 342d23d

Please sign in to comment.