8 #include "McNVTChemicalPotential.h" 9 #include <util/misc/FileMaster.h> 10 #include <util/archives/Serializable_includes.h> 12 #include <mcMd/mcSimulation/McSystem.h> 13 #include <mcMd/mcMoves/SystemMove.h> 16 #include <simp/boundary/Boundary.h> 17 #include <simp/species/Species.h> 19 #include <mcMd/chemistry/Molecule.h> 20 #include <mcMd/chemistry/Bond.h> 21 #include <mcMd/chemistry/Atom.h> 22 #include <util/space/Vector.h> 23 #include <util/space/Tensor.h> 41 simulationPtr_(&system.simulation()),
42 boundaryPtr_(&system.boundary()),
43 energyEnsemblePtr_(&system.energyEnsemble()),
44 randomPtr_(&system.simulation().random()),
62 read<int>(in,
"nSamplePerBlock", nSamplePerBlock_);
63 read<int>(in,
"nTrial", nTrial_);
64 read<int>(in,
"nMoleculeTrial", nMoleculeTrial_);
65 read<int>(in,
"speciesId", speciesId_);
68 read<double>(in,
"Emin", Emin_);
69 read<double>(in,
"Emax", Emax_);
70 read<double>(in,
"EnBin", EnBin_);
71 Eaccumulator_.
setParam(Emin_, Emax_, EnBin_);
72 Eaccumulator_.
clear();
73 read<double>(in,
"Ecmin", Ecmin_);
74 read<double>(in,
"Ecmax", Ecmax_);
75 read<double>(in,
"EcnBin", EcnBin_);
76 Ecaccumulator_.
setParam(Ecmin_, Ecmax_, EcnBin_);
77 Ecaccumulator_.
clear();
78 read<double>(in,
"Emmin", Emmin_);
79 read<double>(in,
"Emmax", Emmax_);
80 read<double>(in,
"EmnBin", EmnBin_);
81 Emaccumulator_.
setParam(Emmin_, Emmax_, EmnBin_);
82 Emaccumulator_.
clear();
83 read<double>(in,
"BRmin", BRmin_);
84 read<double>(in,
"BRmax", BRmax_);
85 read<double>(in,
"BRnBin", BRnBin_);
86 BRaccumulator_.
setParam(BRmin_, BRmax_, BRnBin_);
87 BRaccumulator_.
clear();
94 if (nTrial_ <=0 || nTrial_ > MaxTrial_) {
98 if (nMoleculeTrial_ <=0) {
99 UTIL_THROW(
"Invalid value input for nMoleculeTrial");
102 if (speciesId_ <0 || speciesId_ >= system().simulation().nSpecies()) {
103 UTIL_THROW(
"Invalid value input for speciesId");
106 isInitialized_ =
true;
114 { accumulator_.
clear(); }
124 Atom* endPtr, *pvt1Ptr;
125 Vector trialPos[MaxTrial_], bondVec, pvt1Pos;
133 double w = 0, de = 0;
135 double rosenbluth = 1, energy = 0;
136 double trialProb[MaxTrial_], trialEnergy[MaxTrial_];
139 beta = energyEnsemble().
beta();
144 for (
int i = 0; i < nMoleculeTrial_; i++) {
147 endPtr = &molPtr->
atom(0);
148 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
151 boundary().
shift(trialPos[iTrial]);
154 trialEnergy[iTrial] =
157 trialEnergy[iTrial] = 0.0;
161 trialEnergy[iTrial] +=
165 trialProb[iTrial] = boltzmann(trialEnergy[iTrial]);
166 w += trialProb[iTrial];
170 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
171 trialProb[iTrial] = trialProb[iTrial]/rosenbluth;
175 iTrial = random().
drawFrom(trialProb, nTrial_);
178 de = trialEnergy[iTrial];
181 endPtr->
position() = trialPos[iTrial];
199 pvt1Ptr = &molPtr->
atom(0);
200 endPtr = &molPtr->
atom(1);
204 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
209 trialPos[iTrial].
add(pvt1Pos, bondVec);
210 boundary().
shift(trialPos[iTrial]);
211 endPtr->
position() = trialPos[iTrial];
214 trialEnergy[iTrial] =
217 trialEnergy[iTrial] = 0.0;
221 trialEnergy[iTrial] +=
225 trialProb[iTrial] = boltzmann(trialEnergy[iTrial]);
226 w += trialProb[iTrial];
230 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
231 trialProb[iTrial] = trialProb[iTrial]/rosenbluth;
235 iTrial = random().
drawFrom(trialProb, nTrial_);
238 de = trialEnergy[iTrial];
244 endPtr->
position() = trialPos[iTrial];
264 pvt2Ptr = &molPtr->
atom(0);
272 pvt1Ptr = &molPtr->
atom(1);
274 endPtr = &molPtr->
atom(2);
276 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
279 if (system().hasAnglePotential()) {
280 uniformCone(length, angle, n, bondVec);
291 trialPos[iTrial].
add(pvt1Pos, bondVec);
292 boundary().
shift(trialPos[iTrial]);
293 endPtr->
position() = trialPos[iTrial];
296 trialEnergy[iTrial] =
299 trialEnergy[iTrial] = 0.0;
303 trialEnergy[iTrial] +=
307 trialProb[iTrial] = boltzmann(trialEnergy[iTrial]);
308 w += trialProb[iTrial];
311 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
312 trialProb[iTrial] = trialProb[iTrial]/rosenbluth;
316 iTrial = random().
drawFrom(trialProb, nTrial_);
319 de = trialEnergy[iTrial];
328 endPtr->
position() = trialPos[iTrial];
337 for (
int atomId = 3; atomId < molPtr->
nAtom(); ++atomId) {
338 addEndAtom(molPtr, atomId, w, de);
346 rosenbluth = rosenbluth / pow(nTrial_,molPtr->
nAtom());
347 accumulator_.
sample(rosenbluth, outputFile_);
350 for (
int atomId = 0; atomId < molPtr->
nAtom(); ++atomId) {
377 accumulator_.
output(outputFile_);
381 Eaccumulator_.
output(outputFile_);
385 Ecaccumulator_.
output(outputFile_);
389 Emaccumulator_.
output(outputFile_);
393 BRaccumulator_.
output(outputFile_);
414 McNVTChemicalPotential::addEndAtom(
Molecule* molPtr,
415 int atomId,
double &rosenbluth,
418 Atom* endPtr = &molPtr->
atom(atomId);
419 Vector trialPos[MaxTrial_];
431 double trialProb[MaxTrial_], trialEnergy[MaxTrial_];
433 int bondTypeId = molPtr->
bond(atomId - 1).
typeId();
437 int angleTypeId = molPtr->
angle(atomId - 2).
typeId();
444 beta = energyEnsemble().
beta();
464 for (iTrial=0; iTrial < nTrial_; ++iTrial) {
467 if (system().hasAnglePotential()) {
468 uniformCone(length, angle, n, bondVec);
478 trialPos[iTrial].
add(pvt1Pos, bondVec);
479 boundary().
shift(trialPos[iTrial]);
480 endPtr->
position() = trialPos[iTrial];
485 trialEnergy[iTrial] = 0.0;
489 if (system().hasDihedralPotential()) {
490 trialEnergy[iTrial] +=
497 trialEnergy[iTrial] +=
502 if (eMin > trialEnergy[iTrial])
503 eMin = trialEnergy[iTrial];
504 Eaccumulator_.
sample(trialEnergy[iTrial]);
506 trialProb[iTrial] = boltzmann(trialEnergy[iTrial]);
507 rosenbluth += trialProb[iTrial];
510 Emaccumulator_.
sample(eMin);
511 BRaccumulator_.
sample(rosenbluth);
513 for (iTrial = 0; iTrial < nTrial_; ++iTrial) {
514 trialProb[iTrial] = trialProb[iTrial]/rosenbluth;
518 iTrial = random().
drawFrom(trialProb, nTrial_);
526 energy += trialEnergy[iTrial];
527 Ecaccumulator_.
sample(energy);
530 endPtr->
position() = trialPos[iTrial];
536 void McNVTChemicalPotential::uniformCone(
const double length,
547 }
while (p.
abs() > 1.0E-8);
550 v1 *= length*cos(angle);
552 p *= length*sin(angle);
virtual void readParameters(std::istream &in)
Read parameters and initialize.
Bond & bond(int localId)
Get a specific Bond in this Molecule by non-const reference.
void clear()
Clear all accumulators, set to empty initial state.
A System for use in a Markov chain Monte Carlo simulation.
A Vector is a Cartesian vector.
virtual double atomEnergy(const Atom &atom) const =0
Calculate the external energy for one Atom.
Vector & divide(const Vector &v, double s)
Divide vector v by scalar s.
Vector & add(const Vector &v1, const Vector &v2)
Add vectors v1 and v2.
Include this file to include all MC potential energy classes at once.
BondPotential & bondPotential() const
Return the BondPotential by reference.
Dihedral & dihedral(int localId)
Get a specific Dihedral in this Molecule by reference.
void randomPosition(Random &random, Vector &r) const
Generate random position within the primary unit cell.
Vector & multiply(const Vector &v, double s)
Multiply a vector v by a scalar s.
void addAtom(Atom &atom)
Add an Atom to the CellList.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
int typeId() const
Get the typeId for this covalent group.
virtual void save(Serializable::OArchive &ar)
Save state to binary file archive.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
long drawFrom(double probability[], long size)
Choose one of several outcomes with a specified set of probabilities.
Molecule & getMolecule(int speciesId)
Get a new molecule from a reservoir of unused Molecule objects.
virtual void output()
Output results at end of simulation.
void returnMolecule(Molecule &molecule)
Return a molecule to a reservoir of unused molecules.
Saving / output archive for binary ostream.
virtual double energy(const Vector &R1, const Vector &R2, const Vector &R3, int type) const =0
Returns potential energy for one dihedral.
double beta() const
Return the inverse temperature.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
virtual double randomAngle(Util::Random *random, double beta, int type) const =0
Return bond angle chosen from equilibrium distribution.
virtual void load(Serializable::IArchive &ar)
Load state from a binary file archive.
void addMolecule(Molecule &molecule)
Add a Molecule to this System.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
ExternalPotential & externalPotential() const
Return ExternalPotential by reference.
void readInterval(std::istream &in)
Read interval from file, with error checking.
A point particle within a Molecule.
Utility classes for scientific computation.
McNVTChemicalPotential(McSystem &system)
Constructor.
void sample(double value)
Sample a value.
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
void output(std::ostream &out)
Output the distribution to file.
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
virtual double energy(double cosTheta, int type) const =0
Returns potential energy for one angle.
virtual double energy(double rSq, int type) const =0
Returns potential energy for one bond.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
Saving archive for binary istream.
void sample(double value)
Add a sampled value to the ensemble.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void setup()
Clear accumulator.
double abs() const
Return absolute magnitude of this vector.
int nSamplePerBlock() const
Get number of samples per block average.
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
FileMaster & fileMaster()
Get the FileMaster by reference.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
AnglePotential & anglePotential() const
Return AnglePotential by reference.
const std::string & outputFileName() const
Return outputFileName string.
double projection(const Vector &p) const
Return projection of this vector along vector p.
void setParam(double min, double max, int nBin)
Set parameters and initialize.
A physical molecule (a set of covalently bonded Atoms).
void removeMolecule(Molecule &molecule)
Remove a specific molecule from this System.
const Vector & position() const
Get the position Vector by const reference.
virtual double randomBondLength(Util::Random *random, double beta, int type) const =0
Return bond length chosen from equilibrium distribution.
Angle & angle(int localId)
Get a specific Angle in this Molecule by non-const reference.
void deleteAtom(Atom &atom)
Remove an Atom from the CellList.
virtual void sample(long iStep)
Evaluate energy per particle, and add to ensemble.
int nAtom() const
Get the number of Atoms in this Molecule.
virtual double atomEnergy(const Atom &atom) const =0
Calculate the nonbonded pair energy for one Atom.
void unitVector(Vector &v)
Generate unit vector with uniform probability over the unit sphere.
DihedralPotential & dihedralPotential() const
Return the DihedralPotential by reference.