8 #include "McMuExchange.h" 10 #include <mcMd/mcSimulation/McSystem.h> 11 #include <mcMd/potentials/pair/McPairPotential.h> 12 #include <mcMd/neighbor/CellList.h> 13 #include <mcMd/chemistry/Molecule.h> 14 #include <mcMd/chemistry/Atom.h> 16 #include <simp/species/Species.h> 17 #include <simp/ensembles/EnergyEnsemble.h> 18 #include <simp/boundary/Boundary.h> 20 #include <util/archives/Serializable_includes.h> 21 #include <util/space/Vector.h> 22 #include <util/misc/FileMaster.h> 38 simulationPtr_(&system.simulation()),
39 boundaryPtr_(&system.boundary()),
60 read<int>(in,
"speciesId", speciesId_);
64 if (speciesId_ >=
system().simulation().nSpecies()) {
69 nAtom_ = speciesPtr->
nAtom();
71 readDArray<int>(in,
"newTypeIds", newTypeIds_, nAtom_);
75 for (
int i = 0; i < nAtom_; ++i) {
76 if (newTypeIds_[i] != speciesPtr->
atomTypeId(i)) {
78 isAtomFlipped_[i] = 1;
80 isAtomFlipped_[i] = 0;
83 accumulators_.allocate(speciesPtr->
capacity());
85 isInitialized_ =
true;
94 if (nMolecule_ > accumulators_.capacity()) {
97 if (nMolecule_ <= 0) {
100 for (
int iMol = 0; iMol < nMolecule_; ++iMol) {
101 accumulators_[iMol].clear();
113 if (!
system().energyEnsemble().isIsothermal()) {
114 UTIL_THROW(
"EnergyEnsemble is not isothermal");
116 if (nMolecule_ !=
system().nMolecule(speciesId_)) {
117 UTIL_THROW(
"nMolecule has changed since setup");
127 double rsq, dE, beta, boltzmann;
128 int j, k, nNeighbor, nFlip;
129 int i1, i2, id1, id2, t1, t2, t1New, iMol;
131 nFlip = flipAtomIds_.
size();
136 for ( ; molIter.
notEnd(); ++molIter) {
138 ptr0 = &molIter->atom(0);
141 for (j = 0; j < nFlip; ++j) {
142 i1 = flipAtomIds_[j];
143 t1New = newTypeIds_[i1];
144 ptr1 = &molIter->atom(i1);
147 maskPtr = &(ptr1->
mask());
151 nNeighbor = neighbors_.
size();
152 for (k = 0; k < nNeighbor; ++k) {
153 ptr2 = neighbors_[k];
167 dE -= potential.
energy(rsq, t1, t2);
168 dE += potential.
energy(rsq, t1New, t2);
172 dE -= potential.
energy(rsq, t1, t2);
173 i2 = (int)(ptr2 - ptr0);
174 t2 = newTypeIds_[i2];
175 dE += potential.
energy(rsq, t1New, t2);
177 i2 = (int)(ptr2 - ptr0);
178 if (!isAtomFlipped_[i2]) {
179 dE -= potential.
energy(rsq, t1, t2);
180 t2 = newTypeIds_[i2];
181 dE += potential.
energy(rsq, t1New, t2);
191 boltzmann = exp(-beta*dE);
192 accumulators_[iMol].sample(boltzmann);
210 double sumAveSq = 0.0;
211 double sumErrSq = 0.0;
212 for (
int iMol=0; iMol < nMolecule_; ++iMol) {
213 ave = accumulators_[iMol].average();
214 err = accumulators_[iMol].blockingError();
219 double rMol = double(nMolecule_);
221 err = sqrt(sumErrSq/rMol);
222 double dev = sqrt((sumAveSq/rMol) - ave*ave);
225 outputFile_ <<
"Average = " << ave <<
" +- " 226 << dev/sqrt(rMol) << std::endl;
227 outputFile_ <<
"Error via molecule variance = " 228 << dev/sqrt(rMol) << std::endl;
229 outputFile_ <<
"Error via time series analysis = " 230 << err/sqrt(rMol) << std::endl;
231 outputFile_ << std::endl;
243 if (!isInitialized_) {
244 UTIL_THROW(
"McMuExchange object not initialized");
252 for (
int i = 0; i < nMolecule_; ++i) {
253 ar << accumulators_[i];
268 if (nAtom_ != speciesPtr->
nAtom()) {
269 UTIL_THROW(
"Inconsistent values of nAtom on loading");
272 loadDArray(ar,
"newTypeIds", newTypeIds_, nAtom_);
276 for (
int i = 0; i < nAtom_; ++i) {
277 if (newTypeIds_[i] != speciesPtr->
atomTypeId(i)) {
279 isAtomFlipped_[i] = 1;
281 isAtomFlipped_[i] = 0;
284 accumulators_.allocate(speciesPtr->
capacity());
287 for (
int i = 0; i < nMolecule_; ++i) {
288 ar >> accumulators_[i];
290 isInitialized_ =
true;
int size() const
Return logical size of this array (i.e., number of elements).
Molecule & molecule() const
Get the parent Molecule by reference.
A System for use in a Markov chain Monte Carlo simulation.
virtual void readParameters(std::istream &in)
Read parameters and initialize.
virtual double energy(double rsq, int iAtomType, int jAtomType) const =0
Return pair energy for a single pair.
void getNeighbors(const Vector &pos, NeighborArray &neighbors) const
Fill a NeighborArray with pointers to atoms near a specified position.
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
int nAtom() const
Get number of atoms per molecule for this Species.
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
const CellList & cellList() const
Get the cellList by const reference.
virtual void save(Serializable::OArchive &ar)
Save state to binary file archive.
McMuExchange(McSystem &system)
Constructor.
void append(const Data &data)
Append data to the end of the array.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from a binary file archive.
Set of Atoms for which pair interactions with a target Atom are "masked".
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
A PairPotential for MC simulations (abstract).
EnergyEnsemble & energyEnsemble() const
Get the EnergyEnsemble by reference.
Mask & mask()
Get the associated Mask by reference.
McSystem & system()
Return reference to parent system.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
Saving / output archive for binary ostream.
long interval_
Number of simulation steps between subsequent actions.
bool isMasked(const Atom &atom) const
True if the atom is in the masked set for the target Atom.
int nMolecule(int speciesId) const
Get the number of molecules of one Species in this System.
double beta() const
Return the inverse temperature.
DArrayParam< Type > & loadDArray(Serializable::IArchive &ar, const char *label, DArray< Type > &array, int n, bool isRequired)
Add an load a DArray < Type > array parameter.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
void allocate(int capacity)
Allocates the underlying C array.
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.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
virtual void sample(long iStep)
Evaluate energy per particle, and add to ensemble.
std::string outputFileName_
Base name of output file(s).
int id() const
Get global index for this Atom within the Simulation.
int atomTypeId(int iAtom) const
Get atom type index for a specific atom, by local atom index.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
Forward iterator for a PArray.
virtual void setup()
Clear accumulators.
Template for Analyzer associated with one System.
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
A cell list for Atom objects in a periodic system boundary.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setClassName(const char *className)
Set class name string.
int capacity() const
Maximum allowed number of molecules for this Species.
FileMaster & fileMaster()
Get the FileMaster by reference.
void loadInterval(Serializable::IArchive &ar)
Load interval from archive, with error checking.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
void allocate(int capacity)
Allocate the underlying C array.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name from archive.
const Vector & position() const
Get the position Vector by const reference.
A Species represents a set of chemically similar molecules.
Species & species(int i)
Get a specific Species by reference.
int size() const
Return logical size of this array (i.e., number of elements).
virtual void output()
Output results at end of simulation.