Skip to content

Commit

Permalink
Add query parameters for the nucleon counts. See #57.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisav1 committed Oct 18, 2022
1 parent 9c20583 commit 49904f8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
8 changes: 1 addition & 7 deletions js/chart-intro/view/ChartIntroScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ import PeriodicTableAndIsotopeSymbol from './PeriodicTableAndIsotopeSymbol.js';
// constants
const LABEL_FONT = new PhetFont( BANConstants.REGULAR_FONT_SIZE );
const NUCLEON_CAPTURE_RADIUS = 100;
const NUMBER_OF_NUCLEON_LAYERS = 22; // This is based on max number of particles, may need adjustment if that changes.

// types
export type NuclideChartIntroScreenViewOptions = BANScreenViewOptions;

class ChartIntroScreenView extends BANScreenView<ChartIntroModel> {

public static NUMBER_OF_NUCLEON_LAYERS: number;

private readonly atomNode: Node;

// layers where nucleons exist
Expand Down Expand Up @@ -159,7 +156,7 @@ class ChartIntroScreenView extends BANScreenView<ChartIntroModel> {

// Add the nucleonLayers
this.nucleonLayers = [];
_.times( NUMBER_OF_NUCLEON_LAYERS, () => {
_.times( BANScreenView.NUMBER_OF_NUCLEON_LAYERS, () => {
const nucleonLayer = new Node();
this.nucleonLayers.push( nucleonLayer );
this.particleViewLayerNode.addChild( nucleonLayer );
Expand Down Expand Up @@ -285,8 +282,5 @@ class ChartIntroScreenView extends BANScreenView<ChartIntroModel> {
}
}

// export for usage when creating shred Particles
ChartIntroScreenView.NUMBER_OF_NUCLEON_LAYERS = NUMBER_OF_NUCLEON_LAYERS;

buildANucleus.register( 'ChartIntroScreenView', ChartIntroScreenView );
export default ChartIntroScreenView;
35 changes: 35 additions & 0 deletions js/common/BANQueryParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021-2022, University of Colorado Boulder

/**
* BANQueryParameters defines query parameters that are specific to this simulation.
*
* @author Luisa Vargas
* @author Chris Klusendorf (PhET Interactive Simulations)
*/


import buildANucleus from '../buildANucleus.js';
import BANConstants from './BANConstants.js';

const BANQueryParameters = QueryStringMachine.getAll( {

// the number of neutrons in the atom that the sim starts up with
neutrons: {
public: true,
type: 'number',
defaultValue: 0,
isValidValue: ( number: number ) => Number.isInteger( number ) && number >= 0 && number <= BANConstants.MAX_NUMBER_OF_NEUTRONS
},

// the number of protons in the atom that the sim starts up with
protons: {
public: true,
type: 'number',
defaultValue: 0,
isValidValue: ( number: number ) => Number.isInteger( number ) && number >= 0 && number <= BANConstants.MAX_NUMBER_OF_PROTONS
}

} );

buildANucleus.register( 'BANQueryParameters', BANQueryParameters );
export default BANQueryParameters;
20 changes: 20 additions & 0 deletions js/common/view/BANScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import arrayRemove from '../../../../phet-core/js/arrayRemove.js';
const MIN_ELECTRON_CLOUD_RADIUS = 42.5;

const TOUCH_AREA_Y_DILATION = 3;
const NUMBER_OF_NUCLEON_LAYERS = 22; // This is based on max number of particles, may need adjustment if that changes.

// types
export type BANScreenViewOptions = ScreenViewOptions;
Expand All @@ -45,6 +46,8 @@ const HORIZONTAL_DISTANCE_BETWEEN_ARROW_BUTTONS = 160;

abstract class BANScreenView<M extends BANModel> extends ScreenView {

public static NUMBER_OF_NUCLEON_LAYERS: number;

protected model: M;
private timeSinceCountdownStarted: number;
private previousProtonCount: number;
Expand Down Expand Up @@ -414,6 +417,20 @@ abstract class BANScreenView<M extends BANModel> extends ScreenView {
this.protonArrowButtons = protonArrowButtons;
}

/**
* Create and add a nucleon of particleType immediately to the particleAtom.
*/
public addNucleonImmediatelyToAtom( particleType: ParticleType ): void {
const particle = new Particle( particleType.name.toLowerCase(), {
maxZLayer: BANScreenView.NUMBER_OF_NUCLEON_LAYERS - 1
} );

// place the particle the center of the particleAtom and add it to the model and particleAtom
particle.setPositionAndDestination( this.model.particleAtom.positionProperty.value );
this.model.addParticle( particle );
this.model.particleAtom.addParticle( particle );
}

/**
* Set the input enabled and visibility of a creator node.
*/
Expand Down Expand Up @@ -611,5 +628,8 @@ abstract class BANScreenView<M extends BANModel> extends ScreenView {
}
}

// export for usage when creating shred Particles
BANScreenView.NUMBER_OF_NUCLEON_LAYERS = NUMBER_OF_NUCLEON_LAYERS;

buildANucleus.register( 'BANScreenView', BANScreenView );
export default BANScreenView;
36 changes: 16 additions & 20 deletions js/decay/view/DecayScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import StringUtils from '../../../../phetcommon/js/util/StringUtils.js';
import Multilink from '../../../../axon/js/Multilink.js';
import ReturnButton from '../../../../scenery-phet/js/buttons/ReturnButton.js';
import StringProperty from '../../../../axon/js/StringProperty.js';
import BANQueryParameters from '../../common/BANQueryParameters.js';

// constants
const LABEL_FONT = new PhetFont( BANConstants.REGULAR_FONT_SIZE );
const NUCLEON_CAPTURE_RADIUS = 100;
const NUMBER_OF_NUCLEON_LAYERS = 22; // This is based on max number of particles, may need adjustment if that changes.
const NUMBER_OF_PROTONS_IN_ALPHA_PARTICLE = 2;
const NUMBER_OF_NEUTRONS_IN_ALPHA_PARTICLE = 2;

Expand All @@ -53,8 +53,6 @@ export type DecayScreenViewOptions = BANScreenViewOptions;

class DecayScreenView extends BANScreenView<DecayModel> {

public static NUMBER_OF_NUCLEON_LAYERS: number;

private readonly stabilityIndicator: Text;
private readonly atomNode: Node;

Expand Down Expand Up @@ -151,7 +149,7 @@ class DecayScreenView extends BANScreenView<DecayModel> {

for ( let i = 0; i < Math.abs( nucleonCountDifference ); i++ ) {
if ( nucleonCountDifference > 0 ) {
addNucleonImmediatelyToAtom( particleType );
this.addNucleonImmediatelyToAtom( particleType );
}
else if ( nucleonCountDifference < 0 ) {
removeNucleonImmediatelyFromAtom( particleType );
Expand All @@ -165,18 +163,6 @@ class DecayScreenView extends BANScreenView<DecayModel> {
this.animateAndRemoveParticle( particleToRemove );
};

// create and add a nucleon of particleType immediately to the particleAtom
const addNucleonImmediatelyToAtom = ( particleType: ParticleType ) => {
const particle = new Particle( particleType.name.toLowerCase(), {
maxZLayer: DecayScreenView.NUMBER_OF_NUCLEON_LAYERS - 1
} );

// place the particle the center of the particleAtom and add it to the model and particleAtom
particle.setPositionAndDestination( this.model.particleAtom.positionProperty.value );
this.model.addParticle( particle );
this.model.particleAtom.addParticle( particle );
};

// show the undoDecayButton
const showAndRepositionUndoDecayButton = ( decayType: string ) => {
repositionUndoDecayButton( decayType );
Expand Down Expand Up @@ -293,6 +279,7 @@ class DecayScreenView extends BANScreenView<DecayModel> {
// update the cloud size as the massNumber changes
model.particleAtom.protonCountProperty.link( updateCloudSize );

// TODO: should be moved to BANScreenView bc repeated in Chart screen view
// Maps a number of electrons to a diameter in screen coordinates for the electron shell. This mapping function is
// based on the real size relationships between the various atoms, but has some tweakable parameters to reduce the
// range and scale to provide values that are usable for our needs on the canvas.
Expand Down Expand Up @@ -372,7 +359,7 @@ class DecayScreenView extends BANScreenView<DecayModel> {

// Add the nucleonLayers
this.nucleonLayers = [];
_.times( NUMBER_OF_NUCLEON_LAYERS, () => {
_.times( BANScreenView.NUMBER_OF_NUCLEON_LAYERS, () => {
const nucleonLayer = new Node();
this.nucleonLayers.push( nucleonLayer );
this.particleViewLayerNode.addChild( nucleonLayer );
Expand Down Expand Up @@ -472,6 +459,18 @@ class DecayScreenView extends BANScreenView<DecayModel> {
this.nucleonLayers[ zLayer ].addChild( particleView! );
}
} );

// TODO: should be moved to BANScreenView
// add initial neutrons and protons specified by the query parameters to the atom
_.times( Math.max( BANQueryParameters.neutrons, BANQueryParameters.protons ), () => {
if ( this.model.particleAtom.neutronCountProperty.value < BANQueryParameters.neutrons ) {
this.addNucleonImmediatelyToAtom( ParticleType.NEUTRON );
}
if ( this.model.particleAtom.protonCountProperty.value < BANQueryParameters.protons ) {
this.addNucleonImmediatelyToAtom( ParticleType.PROTON );
}
// TODO: need to detect if this forms a nuclide that shouldn't exist and then call QueryStringMachine.addWarning here and model.reset()
} );
}

/**
Expand Down Expand Up @@ -670,8 +669,5 @@ class DecayScreenView extends BANScreenView<DecayModel> {
}
}

// export for usage when creating shred Particles
DecayScreenView.NUMBER_OF_NUCLEON_LAYERS = NUMBER_OF_NUCLEON_LAYERS;

buildANucleus.register( 'DecayScreenView', DecayScreenView );
export default DecayScreenView;

0 comments on commit 49904f8

Please sign in to comment.