From ae16f62a313f4558e5fdae86f118e06cb7f0cc14 Mon Sep 17 00:00:00 2001 From: jbphet Date: Fri, 21 Jul 2023 14:13:40 -0600 Subject: [PATCH] added common function for detecting energy packets crossing an altitude, see https://github.com/phetsims/greenhouse-effect/issues/337 --- js/common/model/EnergyAbsorbingEmittingLayer.ts | 5 ++--- js/common/model/FluxSensor.ts | 12 ++---------- js/common/model/energyPacketCrossedAltitude.ts | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 js/common/model/energyPacketCrossedAltitude.ts diff --git a/js/common/model/EnergyAbsorbingEmittingLayer.ts b/js/common/model/EnergyAbsorbingEmittingLayer.ts index 38be442e..598f48cc 100644 --- a/js/common/model/EnergyAbsorbingEmittingLayer.ts +++ b/js/common/model/EnergyAbsorbingEmittingLayer.ts @@ -18,6 +18,7 @@ import GreenhouseEffectConstants from '../GreenhouseEffectConstants.js'; import EMEnergyPacket from './EMEnergyPacket.js'; import EnergyDirection from './EnergyDirection.js'; import WithRequired from '../../../../phet-core/js/types/WithRequired.js'; +import energyPacketCrossedAltitude from './energyPacketCrossedAltitude.js'; // constants @@ -173,9 +174,7 @@ class EnergyAbsorbingEmittingLayer extends PhetioObject { * Returns true if the provided energy packet crossed over this layer during its latest step. */ protected energyPacketCrossedThisLayer( energyPacket: EMEnergyPacket ): boolean { - const altitude = this.altitude; - return ( energyPacket.previousAltitude > altitude && energyPacket.altitude <= altitude ) || - ( energyPacket.previousAltitude < altitude && energyPacket.altitude >= altitude ); + return energyPacketCrossedAltitude( energyPacket, this.altitude ); } /** diff --git a/js/common/model/FluxSensor.ts b/js/common/model/FluxSensor.ts index f6ebbd69..4ec115a8 100644 --- a/js/common/model/FluxSensor.ts +++ b/js/common/model/FluxSensor.ts @@ -19,6 +19,7 @@ import optionize from '../../../../phet-core/js/optionize.js'; import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js'; import greenhouseEffect from '../../greenhouseEffect.js'; import EMEnergyPacket from './EMEnergyPacket.js'; +import energyPacketCrossedAltitude from './energyPacketCrossedAltitude.js'; import EnergyDirection from './EnergyDirection.js'; import EnergyRateTracker from './EnergyRateTracker.js'; import LayersModel from './LayersModel.js'; @@ -129,7 +130,7 @@ class FluxSensor extends PhetioObject { // Go through each energy packet and determine if it has moved through the sensor and, if so, measure it. energyPackets.forEach( energyPacket => { - if ( this.energyPacketCrossedAltitude( energyPacket ) ) { + if ( energyPacketCrossedAltitude( energyPacket, this.altitudeProperty.value ) ) { if ( energyPacket.direction === EnergyDirection.DOWN ) { assert && assert( energyPacket.isVisible || energyPacket.isInfrared, 'energy packet must be visible or IR' ); if ( energyPacket.isVisible ) { @@ -187,15 +188,6 @@ class FluxSensor extends PhetioObject { this.clearEnergyTrackers(); this.altitudeProperty.reset(); } - - /** - * Returns true if the provided energy packet passed through the altitude at which this sensor resides. - */ - private energyPacketCrossedAltitude( energyPacket: EMEnergyPacket ): boolean { - const altitude = this.altitudeProperty.value; - return ( energyPacket.previousAltitude > altitude && energyPacket.altitude <= altitude ) || - ( energyPacket.previousAltitude < altitude && energyPacket.altitude >= altitude ); - } } greenhouseEffect.register( 'FluxSensor', FluxSensor ); diff --git a/js/common/model/energyPacketCrossedAltitude.ts b/js/common/model/energyPacketCrossedAltitude.ts new file mode 100644 index 00000000..8713887e --- /dev/null +++ b/js/common/model/energyPacketCrossedAltitude.ts @@ -0,0 +1,17 @@ +// Copyright 2023, University of Colorado Boulder + +import EMEnergyPacket from './EMEnergyPacket.js'; + +/** + * A utility function for testing whether an energy packet has crossed through the provided altitude in either the + * upward or downward moving direction. + * + * @author John Blanco (PhET Interactive Simulations) + */ + +const energyPacketCrossedAltitude = ( energyPacket: EMEnergyPacket, altitude: number ): boolean => { + return ( energyPacket.previousAltitude > altitude && energyPacket.altitude <= altitude ) || + ( energyPacket.previousAltitude < altitude && energyPacket.altitude >= altitude ); +}; + +export default energyPacketCrossedAltitude; \ No newline at end of file