Simpatico  v1.10
IntraPairAutoCorr.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "IntraPairAutoCorr.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/chemistry/Molecule.h>
11 #include <mcMd/chemistry/Atom.h>
12 #include <simp/species/Species.h>
13 #include <simp/boundary/Boundary.h>
14 #include <util/misc/FileMaster.h>
15 #include <util/archives/Serializable_includes.h>
16 #include <util/global.h>
17 
18 namespace McMd
19 {
20 
21  using namespace Util;
22  using namespace Simp;
23 
24  /*
25  * Constructor.
26  */
28  : SystemAnalyzer<System>(system),
29  outputFile_(),
30  accumulator_(),
31  data_(),
32  speciesId_(-1),
33  nMolecule_(-1),
34  atom1Id_(-1),
35  atom2Id_(-1),
36  capacity_(-1),
37  isInitialized_(false)
38  { setClassName("IntraPairAutoCorr"); }
39 
40  /*
41  * Read parameters from file, and allocate data array.
42  */
43  void IntraPairAutoCorr::readParameters(std::istream& in)
44  {
45  readInterval(in);
47  read<int>(in, "speciesId", speciesId_);
48  read<int>(in, "atom1Id", atom1Id_);
49  read<int>(in, "atom2Id", atom2Id_);
50  read<int>(in, "capacity", capacity_);
51 
52  // Validate input
53  if (speciesId_ < 0)
54  UTIL_THROW("Negative speciesId");
55  if (atom1Id_ < 0)
56  UTIL_THROW("Negative atom1Id");
57  if (atom2Id_ < 0)
58  UTIL_THROW("Negative atom2Id");
59  if (atom2Id_ <= atom1Id_)
60  UTIL_THROW("atom2Id <= atom1Id");
61  if (capacity_ <= 0)
62  UTIL_THROW("Negative capacity");
63  if (speciesId_ >= system().simulation().nSpecies())
64  UTIL_THROW("speciesId > nSpecies");
65 
66  speciesPtr_ = &system().simulation().species(speciesId_);
67 
68  int nAtom = speciesPtr_->nAtom();
69  if (atom1Id_ >= nAtom)
70  UTIL_THROW("atom1Id >= nAtom");
71  if (atom2Id_ >= nAtom)
72  UTIL_THROW("atom2Id >= nAtom");
73 
74  // Maximum possible number of molecules of this species
75  int speciesCapacity = speciesPtr_->capacity();
76 
77  // Allocate an array of separation Vectors
78  data_.allocate(speciesCapacity);
79 
80  // Allocate memory for the AutoCorrArray accumulator object
81  accumulator_.setParam(speciesCapacity, capacity_);
82 
83  isInitialized_ = true;
84  }
85 
86  /*
87  * Load state from an archive.
88  */
90  {
92  loadParameter<int>(ar, "speciesId", speciesId_);
93  loadParameter<int>(ar, "atom1Id", atom1Id_);
94  loadParameter<int>(ar, "atom2Id", atom2Id_);
95  loadParameter<int>(ar, "capacity", capacity_);
96  ar & nMolecule_;
97  ar & accumulator_;
98 
99  // Validate
100  if (speciesId_ < 0) UTIL_THROW("Negative speciesId");
101  if (atom1Id_ < 0) UTIL_THROW("Negative atom1Id");
102  if (atom2Id_ < 0) UTIL_THROW("Negative atom2Id");
103  if (atom2Id_ <= atom1Id_)UTIL_THROW("atom2Id <= atom1Id");
104  if (capacity_ <= 0) UTIL_THROW("Negative capacity");
105  if (speciesId_ >= system().simulation().nSpecies()) {
106  UTIL_THROW("speciesId > nSpecies");
107  }
108 
109  speciesPtr_ = &system().simulation().species(speciesId_);
110  int nAtom = speciesPtr_->nAtom();
111  if (atom1Id_ >= nAtom) UTIL_THROW("atom1Id >= nAtom");
112  if (atom2Id_ >= nAtom) UTIL_THROW("atom2Id >= nAtom");
113 
114  // Allocate an array of separation Vectors
115  int speciesCapacity = speciesPtr_->capacity();
116  data_.allocate(speciesCapacity);
117 
118  isInitialized_ = true;
119  }
120 
121  /*
122  * Save state to archive.
123  */
125  { ar & *this; }
126 
127  /*
128  * Set actual number of molecules and clear accumulator.
129  */
131  {
132  if (!isInitialized_) {
133  UTIL_THROW("Object not initialized.");
134  }
135 
136  // Set number of molecules and clear accumulator
137  nMolecule_ = system().nMolecule(speciesId_);
138  accumulator_.setNEnsemble(nMolecule_);
139  accumulator_.clear();
140  }
141 
142  /*
143  * Evaluate end-to-end vectors of all chains, add to ensemble.
144  */
145  void IntraPairAutoCorr::sample(long iStep)
146  {
147  if (isAtInterval(iStep)) {
148  Molecule* moleculePtr;
149  Vector r1, r2, dR;
150  int i, j;
151 
152  // Validate nMolecules_
153  if (nMolecule_ <= 0) {
154  UTIL_THROW("nMolecule <= 0");
155  }
156  if (nMolecule_ != system().nMolecule(speciesId_)) {
157  UTIL_THROW("Number of molecules has changed");
158  }
159 
160  // Loop over molecules in species
161  for (i = 0; i < nMolecule_; ++i) {
162  moleculePtr = &system().molecule(speciesId_, i);
163 
164  // Build separation between atoms atom1Id and atom2Id
165  // by adding separations between consecutive atoms.
166  data_[i].zero();
167  for (j = atom1Id_ + 1; j <= atom2Id_; ++j) {
168  r1 = moleculePtr->atom(j-1).position();
169  r2 = moleculePtr->atom(j).position();
170  system().boundary().distanceSq(r1, r2, dR);
171  data_[i] += dR;
172  }
173 
174  }
175  accumulator_.sample(data_);
176 
177  } // if isAtInterval
178  }
179 
180  /*
181  * Output results to file after simulation is completed.
182  */
184  {
185  // Echo parameters to log file
186  fileMaster().openOutputFile(outputFileName(), outputFile_);
187  writeParam(outputFile_);
188  outputFile_ << std::endl;
189  outputFile_ << "nMolecule " << accumulator_.nEnsemble()
190  << std::endl;
191  outputFile_ << "bufferCapacity " << accumulator_.bufferCapacity()
192  << std::endl;
193  outputFile_ << "nSample " << accumulator_.nSample()
194  << std::endl;
195  outputFile_ << std::endl;
196  outputFile_ << "average " << accumulator_.average()
197  << std::endl;
198  outputFile_ << std::endl;
199  outputFile_ << "Format of *.dat file" << std::endl;
200  outputFile_ << "[int time in samples] [double autocorrelation function]"
201  << std::endl;
202  outputFile_ << std::endl;
203  outputFile_.close();
204 
205  // Write autocorrelation function to data file
206  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
207  accumulator_.output(outputFile_);
208  outputFile_.close();
209  }
210 
211 }
virtual void setup()
Set number of molecules and clear accumulator.
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void sample(long iStep)
Evaluate separation vector for all chains, add to ensemble.
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.
virtual void output()
Output results after simulation is completed.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
Definition: FileMaster.cpp:290
virtual void loadParameters(Serializable::IArchive &ar)
Load parameters from archive.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
System & 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.
Molecule & molecule(int speciesId, int moleculeId)
Get a specific Molecule in this System, by integer index.
Definition: System.h:1137
int nMolecule(int speciesId) const
Get the number of molecules of one Species in this System.
Definition: System.h:1128
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
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.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Template for Analyzer associated with one System.
virtual void save(Serializable::OArchive &ar)
Save state to archive.
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
virtual void readParameters(std::istream &in)
Read parameters from file.
IntraPairAutoCorr(System &system)
Constructor.
void setClassName(const char *className)
Set class name string.
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
int capacity() const
Maximum allowed number of molecules for this Species.
FileMaster & fileMaster()
Get the FileMaster by reference.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
Species & species(int i)
Get a specific Species by reference.