Skip to content

Commit

Permalink
added common function for detecting energy packets crossing an altitu…
Browse files Browse the repository at this point in the history
…de, see #337
  • Loading branch information
jbphet committed Jul 21, 2023
1 parent 81c469d commit ae16f62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 2 additions & 3 deletions js/common/model/EnergyAbsorbingEmittingLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 );
}

/**
Expand Down
12 changes: 2 additions & 10 deletions js/common/model/FluxSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
Expand Down
17 changes: 17 additions & 0 deletions js/common/model/energyPacketCrossedAltitude.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ae16f62

Please sign in to comment.