Displacement Trial Move4a. Examination of Java Code
public void thisTrial(Phase phase) {
if(phase.atomCount==0) {return;} //no atoms to move
int i = (int)(rand.nextDouble()*phase.atomCount); //pick a random number from 1 to N
Atom a = phase.firstAtom();
for(int j=i; --j>=0; ) {a = a.nextAtom();} //get ith atom in list
uOld = phase.potentialEnergy.currentValue(a); //calculate its contribution to the energy
a.displaceWithin(stepSize); //move it within a local volume
phase.boundary().centralImage(a.coordinate.position()); //apply PBC
uNew = phase.potentialEnergy.currentValue(a); //calculate its new contribution to energy
if(uNew < uOld) { //accept if energy decreased
if(uNew >= Double.MAX_VALUE || //reject if energy is huge or doesn’t pass test
Math.exp(-(uNew-uOld)/parentIntegrator.temperature) < rand.nextDouble()) {
a.replace(); //...put it back in its original position
nAccept++; //if reached here, move is accepted