Simpatico  v1.10
mcMd/analyzers/system/AtomMSD.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 "AtomMSD.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <simp/species/Species.h>
11 #include <mcMd/chemistry/Molecule.h>
12 #include <mcMd/chemistry/Atom.h>
13 #include <simp/boundary/Boundary.h>
14 #include <util/space/Dimension.h>
15 #include <util/archives/Serializable_includes.h>
16 
17 #include <util/global.h>
18 
19 namespace McMd
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
25  /*
26  * Constructor.
27  */
29  : SystemAnalyzer<System>(system),
30  outputFile_(),
31  accumulator_(),
32  truePositions_(),
33  oldPositions_(),
34  shifts_(),
35  speciesId_(-1),
36  atomId_(-1),
37  nMolecule_(-1),
38  capacity_(-1),
39  isInitialized_(false)
40  { setClassName("AtomMSD"); }
41 
42  /*
43  * Read parameters from file, and allocate data array.
44  */
45  void AtomMSD::readParameters(std::istream& in)
46  {
47 
48  // Read interval and parameters for AutoCorrArray
49  readInterval(in);
51  read<int>(in, "speciesId", speciesId_);
52  read<int>(in, "atomId", atomId_);
53  read<int>(in, "capacity", capacity_);
54 
55  // Validate parameters
56  if (speciesId_ < 0)
57  UTIL_THROW("Negative speciesId");
58  if (speciesId_ >= system().simulation().nSpecies())
59  UTIL_THROW("speciesId > nSpecies");
60  if (atomId_ < 0)
61  UTIL_THROW("Negative atomId");
62  if (capacity_ <= 0)
63  UTIL_THROW("Negative capacity");
64 
65  Species* speciesPtr = &system().simulation().species(speciesId_);
66 
67  if (atomId_ >= speciesPtr->nAtom())
68  UTIL_THROW("atomId >= nAtom");
69 
70  // Maximum possible number of molecules of this species
71  int speciesCapacity = speciesPtr->capacity();
72 
73  // Allocate local arrays
74  truePositions_.allocate(speciesCapacity);
75  oldPositions_.allocate(speciesCapacity);
76  shifts_.allocate(speciesCapacity);
77 
78  // Allocate memory for the AutoCorrArray accumulator
79  accumulator_.setParam(speciesCapacity, capacity_);
80 
81  isInitialized_ = true;
82  }
83 
84  /*
85  * Load state from an archive.
86  */
88  {
89  loadInterval(ar);
91  loadParameter<int>(ar, "speciesId", speciesId_);
92  loadParameter<int>(ar, "atomId", atomId_);
93  loadParameter<int>(ar, "capacity", capacity_);
94 
95  // Validate parameters
96  if (speciesId_ < 0)
97  UTIL_THROW("Negative speciesId");
98  if (speciesId_ >= system().simulation().nSpecies()) {
99  UTIL_THROW("speciesId > nSpecies");
100  }
101  if (atomId_ < 0) UTIL_THROW("Negative atomId");
102  if (capacity_ <= 0) UTIL_THROW("Negative capacity");
103 
104  Species* speciesPtr = &system().simulation().species(speciesId_);
105 
106  if (atomId_ >= speciesPtr->nAtom()) {
107  UTIL_THROW("atomId >= nAtom");
108  }
109 
110  // Maximum possible number of molecules of this species
111  int speciesCapacity = speciesPtr->capacity();
112 
113  // Allocate local arrays
114  truePositions_.allocate(speciesCapacity);
115  oldPositions_.allocate(speciesCapacity);
116  shifts_.allocate(speciesCapacity);
117 
118  // Allocate memory for the AutoCorrArray accumulator
119  accumulator_.setParam(speciesCapacity, capacity_);
120 
121  ar & accumulator_;
122  ar & truePositions_;
123  ar & oldPositions_;
124  ar & shifts_;
125  ar & nMolecule_;
126 
127  isInitialized_ = true;
128  }
129 
130  /*
131  * Save state to archive.
132  */
134  { ar & (*this); }
135 
136  /*
137  * Initialize at beginning of simulation.
138  */
140  {
141  if (!isInitialized_) {
142  UTIL_THROW("Error: object is not initialized");
143  }
144 
145  // Set number of molecules of this species in the System.
146  nMolecule_ = system().nMolecule(speciesId_);
147  accumulator_.setNEnsemble(nMolecule_);
148  accumulator_.clear();
149 
150  // Store initial positions, and set initial shift vectors.
151  Vector r;
152  IntVector zero(0);
153  for (int i = 0; i < nMolecule_; ++i) {
154  r = system().molecule(speciesId_, i).atom(atomId_).position();
155  system().boundary().shift(r);
156  oldPositions_[i] = r;
157  shifts_[i] = zero;
158  }
159  }
160 
161  /*
162  * Evaluate end-to-end vectors of all chains, add to ensemble.
163  */
164  void AtomMSD::sample(long iStep)
165  {
166  if (!isAtInterval(iStep)) return;
167 
168  // Confirm that nMolecule has remained constant, and nMolecule > 0.
169  if (nMolecule_ <= 0) {
170  UTIL_THROW("nMolecule <= 0");
171  }
172  if (nMolecule_ != system().nMolecule(speciesId_)) {
173  UTIL_THROW("Number of molecules has changed.");
174  }
175 
176  Vector r;
177  IntVector shift;
178  Vector lengths = system().boundary().lengths();
179  int i, j;
180  for (i = 0; i < nMolecule_; ++i) {
181 
182  r = system().molecule(speciesId_, i).atom(atomId_).position();
183  system().boundary().shift(r);
184 
185  // Compare current r to previous position, oldPositions_[i]
186  system().boundary().distanceSq(r, oldPositions_[i], shift);
187 
188  // If this atom crossed a boundary, increment its shift vector
189  shifts_[i] += shift;
190 
191  // Reconstruct true position
192  for (j = 0; j < Dimension; ++j) {
193  truePositions_[i][j] = r[j] + shifts_[i][j]*lengths[j];
194  }
195 
196  // Store current position in box for comparison to next one
197  oldPositions_[i] = r;
198 
199  }
200  accumulator_.sample(truePositions_);
201 
202  }
203 
206  {
207 
208  // Output parameters
209  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
210  writeParam(outputFile_);
211  outputFile_ << std::endl;
212  outputFile_ << std::endl;
213  outputFile_ << "nMolecule " << accumulator_.nEnsemble()
214  << std::endl;
215  outputFile_ << "buffercapacity " << accumulator_.bufferCapacity()
216  << std::endl;
217  outputFile_ << "nSample " << accumulator_.nSample()
218  << std::endl;
219  outputFile_ << std::endl;
220  //outputFile_ << "Format of *.dat file:" << std::endl;
221  //outputFile_ << "[i] [MSD]" << std::endl;
222  outputFile_.close();
223 
224  // Output statistical analysis to separate data file
225  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
226  accumulator_.output(outputFile_);
227  outputFile_.close();
228 
229  }
230 
231 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void setup()
Determine number of molecules and allocate memory.
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 to file after simulation is completed.
virtual void save(Serializable::OArchive &ar)
Save state to archive.
const Vector & lengths() const
Get Vector of unit cell lengths by const reference.
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
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 loadParameters(Serializable::IArchive &ar)
Load state from an archive.
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.
virtual void sample(long iStep)
Evaluate end-to-end vectors of all chains, add to ensemble.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
Template for Analyzer associated with one System.
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).
void setClassName(const char *className)
Set class name string.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
virtual void readParameters(std::istream &in)
Read parameters from file.
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.
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 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.
AtomMSD(System &system)
Constructor.