9 #include "MdPairEnergyCoefficients.h" 11 #include <mcMd/simulation/Simulation.h> 12 #include <mcMd/potentials/pair/MdPairPotential.h> 13 #include <mcMd/neighbor/PairList.h> 14 #include <mcMd/neighbor/PairIterator.h> 15 #include <simp/species/Species.h> 16 #include <util/format/Dbl.h> 17 #include <util/format/Int.h> 18 #include <util/archives/Serializable_includes.h> 19 #include <util/misc/FileMaster.h> 31 nAtomType_(system.simulation().nAtomType()),
32 nSpecies_(system.simulation().nSpecies()),
33 pairListPtr_(&system.pairPotential().pairList()),
34 pairPotentialPtr_(&system.pairPotential()),
35 boundaryPtr_(&system.boundary()),
36 maxMoleculeNeighbors_(0)
48 void MdPairEnergyCoefficients::clear()
50 int iSpecies1, iMolecule1;
52 for (iSpecies1 = 0; iSpecies1 < nSpecies_; ++iSpecies1) {
56 for (iMolecule1 = 0; iMolecule1 < species1Ptr->
capacity();
58 moleculeNeighbors_[iSpecies1][iMolecule1].clear();
70 read<PairSelector>(in,
"selector", selector_);
71 read<int>(in,
"maxMoleculeNeighbors", maxMoleculeNeighbors_);
75 moleculeNeighbors_.allocate(nSpecies_);
76 twoMoleculePairEnergy_.allocate(nSpecies_);
77 for (iSpecies = 0; iSpecies < nSpecies_; ++iSpecies) {
85 moleculeNeighbors_[iSpecies].allocate(nMolecule);
86 twoMoleculePairEnergy_[iSpecies].allocate(nMolecule);
87 for (iMolecule = 0; iMolecule < nMolecule; ++iMolecule) {
88 moleculeNeighbors_[iSpecies][iMolecule].
89 allocate(maxMoleculeNeighbors_);
93 isInitialized_ =
true;
105 loadParameter<PairSelector>(ar,
"selector", selector_);
106 loadParameter<int>(ar,
"maxMoleculeNeighbors", maxMoleculeNeighbors_);
108 int nAtomType, nSpecies;
112 if (nAtomType != nAtomType_) {
113 UTIL_THROW(
"Inconsistent values of nAtomType");
115 if (nSpecies != nSpecies_) {
116 UTIL_THROW(
"Inconsistent values of nSpecies");
121 moleculeNeighbors_.allocate(nSpecies_);
122 twoMoleculePairEnergy_.allocate(nSpecies_);
124 for (iSpecies = 0; iSpecies < nSpecies_; ++iSpecies) {
132 moleculeNeighbors_[iSpecies].allocate(nMolecule);
133 twoMoleculePairEnergy_[iSpecies].allocate(nMolecule);
134 for (iMolecule = 0; iMolecule < nMolecule; ++iMolecule) {
135 moleculeNeighbors_[iSpecies][iMolecule].
136 allocate(maxMoleculeNeighbors_);
140 ar & pairEnergyAccumulator_;
141 ar & moleculePESqAccumulator_;
142 ar & twoMoleculePESqAccumulator_;
143 ar & pESqAccumulator_;
145 isInitialized_ =
true;
173 for (pairListPtr_->
begin(iter); iter.
notEnd(); ++iter) {
174 iter.
getPair(atom0Ptr, atom1Ptr);
175 if (selector_.
match(*atom0Ptr, *atom1Ptr)) {
179 int speciesId, moleculeId;
181 atomPair[0] = atom0Ptr;
182 atomPair[1] = atom1Ptr;
187 moleculePtr = & atom0Ptr->
molecule();
188 speciesPtr = & moleculePtr->
species();
189 speciesId = speciesPtr->
id();
190 moleculeId = moleculePtr->
id();
192 moleculeNeighbors_[speciesId][moleculeId].
195 moleculePtr = & atom1Ptr->
molecule();
196 speciesPtr = & moleculePtr->
species();
197 speciesId = speciesPtr->
id();
198 moleculeId = moleculePtr->
id();
201 atomPair[0] = atom1Ptr;
202 atomPair[1] = atom0Ptr;
204 moleculeNeighbors_[speciesId][moleculeId].
210 double moleculePESq = 0.0;
211 double pairEnergy=0.0;
212 double twoMoleculePESq=0.0;
215 int iSpecies1, iMolecule1;
217 for (iSpecies1 = 0; iSpecies1 < nSpecies_; ++iSpecies1) {
221 for (iMolecule1 = 0; iMolecule1 < species1Ptr->
capacity();
223 int iSpecies2, iMolecule2;
225 double moleculePairEnergy=0.0;
228 for (iSpecies2 = 0; iSpecies2 < nSpecies_; ++iSpecies2) {
232 for (iMolecule2 = 0; iMolecule2 < species2Ptr->
capacity();
234 twoMoleculePairEnergy_[iSpecies2][iMolecule2]=0.0;
242 neighborsPtr=&moleculeNeighbors_[iSpecies1][iMolecule1];
244 Atom *atom0Ptr, *atom1Ptr;
248 if (selector_.
match(*atom0Ptr,*atom1Ptr)) {
252 int species2Id,molecule2Id;
255 molecule2Ptr = &atom1Ptr->
molecule();
257 species2Id = species2Ptr->
id();
258 molecule2Id = molecule2Ptr->
id();
262 energy = pairPotentialPtr_->
energy(rsq,
266 twoMoleculePairEnergy_[species2Id][molecule2Id] +=
272 for (iSpecies2 = 0; iSpecies2 < nSpecies_; ++iSpecies2) {
276 for (iMolecule2 = 0; iMolecule2 < species2Ptr->
capacity();
279 twoMoleculePairEnergy_[iSpecies2][iMolecule2];
280 moleculePairEnergy += energy;
281 twoMoleculePESq += energy*energy;
287 pairEnergy += moleculePairEnergy;
288 moleculePESq += moleculePairEnergy*moleculePairEnergy;
295 pairEnergyAccumulator_.
sample(pairEnergy);
298 moleculePESqAccumulator_.
sample(moleculePESq);
301 twoMoleculePESqAccumulator_.
sample(twoMoleculePESq);
304 pESqAccumulator_.
sample(pairEnergy*pairEnergy);
306 outputFile_ <<
Dbl(pairEnergy);
307 outputFile_ << Dbl(moleculePESq);
308 outputFile_ << Dbl(twoMoleculePESq);
309 outputFile_ << Dbl(pairEnergy*pairEnergy);
311 outputFile_ << std::endl;
326 outputFile_ << std::endl;
328 outputFile_ <<
"File format:" << std::endl;
330 outputFile_ <<
"[pairE] ";
331 outputFile_ <<
"[moPairESq] ";
332 outputFile_ <<
"[twoMolPairESq]";
333 outputFile_ <<
"[pairESq] ";
334 outputFile_ << std::endl;
341 pairEnergyAccumulator_.
output(outputFile_);
346 moleculePESqAccumulator_.
output(outputFile_);
351 twoMoleculePESqAccumulator_.
output(outputFile_);
356 pESqAccumulator_.
output(outputFile_);
Molecule & molecule() const
Get the parent Molecule by reference.
virtual double energy(double rsq, int iAtomType, int jAtomType) const =0
Return pair energy for a single pair.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
~MdPairEnergyCoefficients()
Destructor.
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
virtual void readParameters(std::istream &in)
Read parameters and initialize.
Species & species() const
Get the molecular Species by reference.
MdSystem & system()
Return reference to parent system.
Wrapper for a double precision number, for formatted ostream output.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
int id() const
Get the index for this Molecule (unique in species).
void getPair(Atom *&atom1Ptr, Atom *&atom2Ptr) const
Get pointers for current pair of Atoms.
An array of exactly 2 objects.
Forward const iterator for an Array or a C array.
Saving / output archive for binary ostream.
bool match(const Atom &atom1, const Atom &atom2) const
Return true if pair of atoms matches the selector policy.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Simulation & simulation() const
Get the parent Simulation by reference.
int typeId() const
Get type index for this Atom.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
void readInterval(std::istream &in)
Read interval from file, with error checking.
A point particle within a Molecule.
virtual void output()
Output final summary and file format.
Utility classes for scientific computation.
Iterator for pairs in a PairList.
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
virtual void save(Serializable::OArchive &ar)
Save state to archive.
void begin(PairIterator &iterator) const
Initialize a PairIterator.
int id() const
Get integer id of this Species.
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 sample(long iStep)
Evaluate energy and print.
void begin(ArrayIterator< Data > &iterator)
Set an ArrayIterator to the beginning of this Array.
void setClassName(const char *className)
Set class name string.
int capacity() const
Maximum allowed number of molecules for this Species.
bool notEnd() const
Return true if not at end of PairList.
FileMaster & fileMaster()
Get the FileMaster by reference.
void loadInterval(Serializable::IArchive &ar)
Load interval from archive, with error checking.
A System for Molecular Dynamics simulation.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Dynamically allocated array with variable logical size.
const std::string & outputFileName() const
Return outputFileName string.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name from archive.
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
bool notEnd() const
Is this not the end of the array?
A Species represents a set of chemically similar molecules.
MdPairEnergyCoefficients(MdSystem &system)
Constructor.
Species & species(int i)
Get a specific Species by reference.