Simpatico  v1.10
LinearRouseAutoCorr.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 "LinearRouseAutoCorr.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 
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  projector_(),
33  speciesId_(-1),
34  nMolecule_(-1),
35  nAtom_(-1),
36  p_(-1),
37  capacity_(-1),
38  isInitialized_(false)
39  { setClassName("LinearRouseAutoCorr"); }
40 
41  /*
42  * Destructor.
43  */
45  {}
46 
47  /*
48  * Read parameters from file.
49  */
50  void LinearRouseAutoCorr::readParameters(std::istream& in)
51  {
52 
53  // Read interval and parameters for AutoCorrArray
54  readInterval(in);
56 
57  read<int>(in, "speciesId", speciesId_);
58  read<int>(in, "p", p_);
59  read<int>(in, "capacity", capacity_);
60 
61  // Validate input
62  if (speciesId_ < 0)
63  UTIL_THROW("Negative speciesId");
64  if (p_ < 0)
65  UTIL_THROW("Negative mode index");
66  if (capacity_ <= 0)
67  UTIL_THROW("Negative capacity");
68  if (speciesId_ < 0)
69  UTIL_THROW("speciesId < 0");
70  if (speciesId_ >= system().simulation().nSpecies())
71  UTIL_THROW("speciesId >= nSpecies");
72 
73  speciesPtr_ = &system().simulation().species(speciesId_);
74  nAtom_ = speciesPtr_->nAtom();
75 
76  // Allocate arrays
77  int speciesCapacity = speciesPtr_->capacity();
78  accumulator_.setParam(speciesCapacity, capacity_);
79  data_.allocate(speciesCapacity);
80  projector_.allocate(nAtom_);
81 
82  isInitialized_ = true;
83  }
84 
85 
86  /*
87  * Load internal state from an archive.
88  */
90  {
92  loadParameter<int>(ar, "speciesId", speciesId_);
93  loadParameter<int>(ar, "p", p_);
94  loadParameter<int>(ar, "capacity", capacity_);
95  ar & nAtom_;
96  ar & nMolecule_;
97  ar & accumulator_;
98  ar & projector_;
99 
100  speciesPtr_ = &system().simulation().species(speciesId_);
101  int speciesCapacity = speciesPtr_->capacity();
102  data_.allocate(speciesCapacity);
103 
104  // Validate input
105  if (speciesId_ < 0) {
106  UTIL_THROW("Negative speciesId");
107  }
108  if (p_ < 0) {
109  UTIL_THROW("Negative mode index");
110  }
111  if (capacity_ <= 0) {
112  UTIL_THROW("Negative capacity");
113  }
114  if (speciesId_ < 0) {
115  UTIL_THROW("speciesId < 0");
116  }
117  if (speciesId_ >= system().simulation().nSpecies()) {
118  UTIL_THROW("speciesId >= nSpecies");
119  }
120  if (nAtom_ != speciesPtr_->nAtom()) {
121  UTIL_THROW("Inconsistent values of nAtom");
122  }
123  if (projector_.capacity() != nAtom_) {
124  UTIL_THROW("Inconsistent projector capacity");
125  }
126  if (accumulator_.bufferCapacity() != capacity_) {
127  UTIL_THROW("Inconsistent accumulator buffer capacity");
128  }
129 
130  isInitialized_ = true;
131  }
132 
133  /*
134  * Save internal state to an archive.
135  */
137  { ar << *this; }
138 
139  /*
140  * Evaluate end-to-end vectors of all chains, add to ensemble.
141  */
143  {
144 
145  if (!isInitialized_) {
146  UTIL_THROW("Object is not intitialized");
147  }
148 
149  // Get number of molecules and number of atoms per molecule.
150  nMolecule_ = system().nMolecule(speciesId_);
151  if (nAtom_ <= 1) UTIL_THROW("Number of atoms per molecule < 2.");
152 
153  // Initialize mode projection coefficients.
154  double qMode = acos(-1.0) * double(p_) / double(nAtom_ - 1);
155  if (p_ == 0) {
156  for (int j = 0; j < nAtom_; ++j)
157  projector_[j] = 1.0 / double(nAtom_);
158  } else {
159  for (int j = 0; j < nAtom_; ++j)
160  projector_[j] = 1.0 / double(nAtom_) * cos(qMode*double(j));
161  }
162  projector_[0] -= 0.5/double(nAtom_);
163  projector_[nAtom_ - 1] -= 0.5 / double(nAtom_);
164 
165  // Initialize the AutoCorrArray object
166  accumulator_.setNEnsemble(nMolecule_);
167 
168  accumulator_.clear();
169  }
170 
171  /*
172  * Evaluate end-to-end vectors of all chains, add to ensemble.
173  */
174  void LinearRouseAutoCorr::sample(long iStep)
175  {
176  if (isAtInterval(iStep)) {
177 
178  Molecule* moleculePtr;
179  Vector r1, r2, dR, atomPos;
180  int i, j;
181 
182  // Confirm that nMolecule has remained constant
183  if (nMolecule_ != system().nMolecule(speciesId_)) {
184  UTIL_THROW("Number of molecules has changed.");
185  }
186 
187  // Loop over molecules
188  for (i = 0; i < nMolecule_; ++i) {
189  moleculePtr = &(system().molecule(speciesId_, i));
190 
191  // Retrace non-periodic shape and calculate coefficients
192  atomPos = moleculePtr->atom(0).position();
193  dR.multiply(atomPos, projector_[0]);
194  data_[i] = dR;
195  for (j = 1; j < nAtom_; j++) {
196  r1 = moleculePtr->atom(j-1).position();
197  r2 = moleculePtr->atom(j).position();
198  system().boundary().distanceSq(r2, r1, dR);
199  atomPos += dR;
200  dR.multiply(atomPos, projector_[j]);
201  data_[i] += dR;
202  }
203  }
204 
205  accumulator_.sample(data_);
206 
207  } // if isAtInterval
208 
209  }
210 
213  {
214 
215  // Echo parameter to analyzer log file
216  fileMaster().openOutputFile(outputFileName(), outputFile_);
217  writeParam(outputFile_);
218  outputFile_.close();
219 
220  // Output statistical analysis to separate data file
221  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
222  accumulator_.output(outputFile_);
223  outputFile_.close();
224 
225  }
226 
227 
228 }
virtual void output()
Output results after simulation is completed.
A Vector is a Cartesian vector.
Definition: Vector.h:75
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.
Vector & multiply(const Vector &v, double s)
Multiply a vector v by a scalar s.
Definition: Vector.h:686
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 setup()
Set number of molecules, initialize eigenvector, and clear accumulator.
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.
virtual void sample(long iStep)
Evaluate end-to-end vectors of all chains, add to ensemble.
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
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
#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.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual ~LinearRouseAutoCorr()
Destructor.
Template for Analyzer associated with one System.
LinearRouseAutoCorr(System &system)
Constructor.
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 readParameters(std::istream &in)
Read parameters from file.
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.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
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.