Skip to content

Commit

Permalink
added code to initialize the water liquid state from previously saved…
Browse files Browse the repository at this point in the history
… data, consolidated code for loading state, see #212
  • Loading branch information
jbphet committed Jan 24, 2018
1 parent c9a1840 commit 8638953
Show file tree
Hide file tree
Showing 5 changed files with 832 additions and 128 deletions.
6 changes: 0 additions & 6 deletions js/common/model/MultipleParticleModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,6 @@ define( function( require ) {
stepInternal: function( dt ) {

this.particleInjectedThisStep = false;
// debugger;
if ( window.printTheStuff ){
console.log( JSON.stringify( this.moleculeDataSet, function( key, val ) {
return typeof val === 'number' ? Number( val.toFixed( 3 ) ) : val;
} ) );
}

if ( !this.isExplodedProperty.get() ) {

Expand Down
83 changes: 31 additions & 52 deletions js/common/model/engine/AbstractPhaseStateChanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,62 +271,41 @@ define( function( require ) {
},

/**
* Set the phase state to liquid, works for all multi-atom molecules (so far).
* Load previously saved position and motion state, does NOT load forces state
* @protected
*/
setPhaseLiquidMultiAtom: function( minInitialDistance, spacingFactor ) {

// Set the model temperature for this phase.
this.multipleParticleModel.setTemperature( StatesOfMatterConstants.LIQUID_TEMPERATURE );

// Get references to the various elements of the data set.
var moleculeDataSet = this.multipleParticleModel.moleculeDataSet;
var moleculeCenterOfMassPositions = moleculeDataSet.moleculeCenterOfMassPositions;
var moleculeRotationAngles = moleculeDataSet.moleculeRotationAngles;

// Initialize the velocities of the molecules.
this.initializeVelocities();

// Assign each molecule to a position.
var moleculesPlaced = 0;
var centerPointX = this.multipleParticleModel.normalizedContainerWidth / 2;
var centerPointY = this.multipleParticleModel.normalizedContainerHeight / 4;
var currentLayer = 0;
var particlesOnCurrentLayer = 0;
var particlesThatWillFitOnCurrentLayer = 1;
for ( var i = 0; i < moleculeDataSet.getNumberOfMolecules(); i++ ) {
for ( var j = 0; j < this.MAX_PLACEMENT_ATTEMPTS; j++ ) {
var distanceFromCenter = currentLayer * minInitialDistance * spacingFactor;
var angle = (particlesOnCurrentLayer / particlesThatWillFitOnCurrentLayer * 2 * Math.PI) +
(particlesThatWillFitOnCurrentLayer / (4 * Math.PI));
var xPos = centerPointX + (distanceFromCenter * Math.cos( angle ));
var yPos = centerPointY + (distanceFromCenter * Math.sin( angle ));

// Consider this spot used even if we don't actually put the particle there.
particlesOnCurrentLayer++;
if ( particlesOnCurrentLayer >= particlesThatWillFitOnCurrentLayer ) {

// This layer is full - move to the next one.
currentLayer++;
particlesThatWillFitOnCurrentLayer = Math.floor( currentLayer * 2 * Math.PI /
( minInitialDistance * spacingFactor ) );
particlesOnCurrentLayer = 0;
}
loadSavedState: function( savedState ) {

assert && assert(
this.multipleParticleModel.moleculeDataSet.numberOfMolecules === savedState.numberOfMolecules,
'unexpected number of particles in saved data set'
);

// Set the initial velocity for each of the atoms based on the new temperature.
var numberOfMolecules = this.multipleParticleModel.moleculeDataSet.numberOfMolecules;
var moleculeCenterOfMassPositions = this.multipleParticleModel.moleculeDataSet.moleculeCenterOfMassPositions;
var moleculeVelocities = this.multipleParticleModel.moleculeDataSet.moleculeVelocities;
var moleculeRotationAngles = this.multipleParticleModel.moleculeDataSet.moleculeRotationAngles;
var moleculeRotationRates = this.multipleParticleModel.moleculeDataSet.moleculeRotationRates;
var moleculesInsideContainer = this.multipleParticleModel.moleculeDataSet.insideContainer;

// Check if the position is too close to the wall. Note that we don't check inter-particle distances here -
// we rely on the placement algorithm to make sure that this is not a problem.
if ( ( xPos > this.MIN_INITIAL_PARTICLE_TO_WALL_DISTANCE ) &&
( xPos < this.multipleParticleModel.normalizedContainerWidth - this.MIN_INITIAL_PARTICLE_TO_WALL_DISTANCE ) &&
( yPos > this.MIN_INITIAL_PARTICLE_TO_WALL_DISTANCE ) &&
( xPos < this.multipleParticleModel.normalizedContainerHeight - this.MIN_INITIAL_PARTICLE_TO_WALL_DISTANCE ) ) {

// This is an acceptable position.
moleculeCenterOfMassPositions[ moleculesPlaced ].setXY( xPos, yPos );
moleculeRotationAngles[ moleculesPlaced ] = angle + Math.PI / 2;
moleculesPlaced++;
break;
}
// for ( var i = 0; i < numberOfMolecules; i++ ) {
for ( var i = 0; i < numberOfMolecules; i++ ) {
moleculeCenterOfMassPositions[ i ].setXY(
savedState.moleculeCenterOfMassPositions[ i ].x,
savedState.moleculeCenterOfMassPositions[ i ].y
);
moleculeVelocities[ i ].setXY(
savedState.moleculeVelocities[ i ].x,
savedState.moleculeVelocities[ i ].y
);
if ( savedState.moleculeRotationAngles ) {
moleculeRotationAngles[ i ] = savedState.moleculeRotationAngles[ i ];
}
if ( savedState.moleculeRotationAngles ) {
moleculeRotationRates[ i ] = savedState.moleculeRotationRates[ i ];
}
moleculesInsideContainer[ i ] = true;
}
},

Expand Down
35 changes: 5 additions & 30 deletions js/common/model/engine/DiatomicPhaseStateChanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,42 +622,17 @@ define( function( require ) {

var dataSetToLoad;

// find the data for this substance
if ( this.multipleParticleModel.substanceProperty.get() === SubstanceType.DIATOMIC_OXYGEN ) {
dataSetToLoad = OXYGEN_LIQUID_INITIAL_STATE;
}
else {
assert && assert( false, 'unhandled substance: ' + this.multipleParticleModel.substanceProperty.get() );
}
assert && assert( dataSetToLoad, 'unhandled substance: ' + this.multipleParticleModel.substanceProperty.get() );

assert && assert(
this.multipleParticleModel.moleculeDataSet.numberOfMolecules === dataSetToLoad.numberOfMolecules,
'unexpected number of particles in data set'
);
// load the previously saved state
this.loadSavedState( dataSetToLoad );

// set the temperature
this.multipleParticleModel.setTemperature( StatesOfMatterConstants.LIQUID_TEMPERATURE );

// Set the initial velocity for each of the atoms based on the new temperature.
var numberOfMolecules = this.multipleParticleModel.moleculeDataSet.numberOfMolecules;
var moleculeCenterOfMassPositions = this.multipleParticleModel.moleculeDataSet.moleculeCenterOfMassPositions;
var moleculeVelocities = this.multipleParticleModel.moleculeDataSet.moleculeVelocities;
var moleculeRotationAngles = this.multipleParticleModel.moleculeDataSet.moleculeRotationAngles;
var moleculeRotationRates = this.multipleParticleModel.moleculeDataSet.moleculeRotationRates;
var moleculesInsideContainer = this.multipleParticleModel.moleculeDataSet.insideContainer;

// for ( var i = 0; i < numberOfMolecules; i++ ) {
for ( var i = 0; i < numberOfMolecules; i++ ) {
moleculeCenterOfMassPositions[ i ].setXY(
dataSetToLoad.moleculeCenterOfMassPositions[ i ].x,
dataSetToLoad.moleculeCenterOfMassPositions[ i ].y
);
moleculeVelocities[ i ].setXY(
dataSetToLoad.moleculeVelocities[ i ].x,
dataSetToLoad.moleculeVelocities[ i ].y
);
moleculeRotationAngles[ i ] = dataSetToLoad.moleculeRotationAngles[ i ];
moleculeRotationRates[ i ] = dataSetToLoad.moleculeRotationRates[ i ];
moleculesInsideContainer[ i ] = true;
}
}
} );
} );
34 changes: 2 additions & 32 deletions js/common/model/engine/MonatomicPhaseStateChanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1421,46 +1421,16 @@ define( function( require ) {
* @protected
*/
setPhaseLiquid: function() {

var dataSetToLoad;

if ( this.multipleParticleModel.substanceProperty.get() === SubstanceType.NEON ) {
dataSetToLoad = NEON_LIQUID_INITIAL_STATE;
}
else if ( this.multipleParticleModel.substanceProperty.get() === SubstanceType.ARGON ) {
dataSetToLoad = ARGON_LIQUID_INITIAL_STATE;
}
else {
assert && assert( false, 'unhandled substance: ' + this.multipleParticleModel.substanceProperty.get() );
}

assert && assert(
this.multipleParticleModel.moleculeDataSet.numberOfMolecules = dataSetToLoad.numberOfMolecules,
'unexpected number of particles in data set'
);

assert && assert( dataSetToLoad, 'unhandled substance: ' + this.multipleParticleModel.substanceProperty.get() );
this.loadSavedState( dataSetToLoad );
this.multipleParticleModel.setTemperature( StatesOfMatterConstants.LIQUID_TEMPERATURE );

// Set the initial velocity for each of the atoms based on the new temperature.
var numberOfAtoms = this.multipleParticleModel.moleculeDataSet.numberOfAtoms;
var moleculeCenterOfMassPositions = this.multipleParticleModel.moleculeDataSet.moleculeCenterOfMassPositions;
var moleculeVelocities = this.multipleParticleModel.moleculeDataSet.moleculeVelocities;
var moleculesInsideContainer = this.multipleParticleModel.moleculeDataSet.insideContainer;

for ( var i = 0; i < numberOfAtoms; i++ ) {

moleculeCenterOfMassPositions[ i ].setXY(
dataSetToLoad.moleculeCenterOfMassPositions[ i ].x,
dataSetToLoad.moleculeCenterOfMassPositions[ i ].y
);

moleculeVelocities[ i ].setXY(
dataSetToLoad.moleculeVelocities[ i ].x,
dataSetToLoad.moleculeVelocities[ i ].y
);

moleculesInsideContainer[ i ] = true;
}
}
} );
} );
Loading

0 comments on commit 8638953

Please sign in to comment.