Widom’s Method. Java Code
public class MeterWidomInsertion extends Meter
* Performs insertion average used in chemical-potential calculation
public double currentValue() {
double sum = 0.0; //zero sum for insertion average
phase.addMolecule(molecule,speciesAgent); //place molecule in phase
for(int i=nInsert; iɬ i--) { //perform nInsert insertions
molecule.translateTo(phase.randomPosition()); //select random position
double u = phase.potentialEnergy.currentValue(molecule); //compute energy
if(u < Double.MAX_VALUE) //add to test-particle average
sum += Math.exp(-u/(phase.integrator().temperature()));
phase.deleteMolecule(molecule); //remove molecule from phase
if(!residual) sum *= speciesAgent.moleculeCount/phase.volume(); //multiply by Ni/V
return sum/(double)nInsert; //return average
//Method to identify species for chemical-potential calculation by this meter
//Normally called only once in a simulation
public void setSpecies(Species s) {
molecule = species.getMolecule();
if(phase != null) speciesAgent = species.getAgent(phase);