diff --git a/js/common/model/Particle.ts b/js/common/model/Particle.ts index 5dfa2d4e..0c2779f8 100644 --- a/js/common/model/Particle.ts +++ b/js/common/model/Particle.ts @@ -167,18 +167,12 @@ export default class Particle { public get vy(): number { return this._vy; } - /** - * Gets the particle's speed, the velocity magnitude, in pm/ps. - */ - public get speed(): number { - return Math.sqrt( this._vx * this._vx + this._vy * this._vy ); - } - /** * Gets the kinetic energy of this particle, in AMU * pm^2 / ps^2. */ public getKineticEnergy(): number { - return 0.5 * this._mass * this.speed * this.speed; // KE = (1/2) * m * |v|^2 + const speed = this.getSpeed(); + return 0.5 * this._mass * speed * speed; // KE = (1/2) * m * |v|^2 } /** @@ -228,11 +222,18 @@ export default class Particle { } /** - * Sets this particle's speed (velocity magnitude). + * Gets the particle's speed (velocity magnitude) in pm/ps. + */ + public getSpeed(): number { + return Math.sqrt( this._vx * this._vx + this._vy * this._vy ); + } + + /** + * Sets this particle's speed (velocity magnitude) in pm/ps. */ public setSpeed( speed: number ): void { assert && assert( speed >= 0, `invalid magnitude: ${speed}` ); - this.scaleVelocity( speed / this.speed ); + this.scaleVelocity( speed / this.getSpeed() ); } /** diff --git a/js/energy/model/AverageSpeedModel.ts b/js/energy/model/AverageSpeedModel.ts index 7de3fc1f..c88f7497 100644 --- a/js/energy/model/AverageSpeedModel.ts +++ b/js/energy/model/AverageSpeedModel.ts @@ -184,7 +184,7 @@ function getAverageSpeed( particles: Particle[] ): number { if ( particles.length > 0 ) { let totalSpeed = 0; for ( let i = particles.length - 1; i >= 0; i-- ) { - totalSpeed += particles[ i ].speed; + totalSpeed += particles[ i ].getSpeed(); } averageSpeed = totalSpeed / particles.length; } diff --git a/js/energy/model/HistogramsModel.ts b/js/energy/model/HistogramsModel.ts index f7dd9e5b..9743fc35 100644 --- a/js/energy/model/HistogramsModel.ts +++ b/js/energy/model/HistogramsModel.ts @@ -306,7 +306,7 @@ export default class HistogramsModel { function getSpeedValues( particles: Particle[] ): number[] { const values = []; for ( let i = particles.length - 1; i >= 0; i-- ) { - values.push( particles[ i ].speed ); + values.push( particles[ i ].getSpeed() ); } return values; }