Simpatico  v1.10
G1MSD.cpp
1 #ifndef G1MSD_CPP
2 #define G1MSD_CPP
3 
4 /*
5 * MolMcD - Monte Carlo and Molecular Dynamics Simulator for Molecular Liquids
6 *
7 * Copyright 2010 - 2014, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "G1MSD.h"
12 #include <mcMd/simulation/Simulation.h>
13 #include <mcMd/chemistry/Molecule.h>
14 #include <mcMd/chemistry/Atom.h>
15 #include <simp/species/Species.h>
16 #include <simp/boundary/Boundary.h>
17 #include <util/space/Dimension.h>
18 
19 namespace McMd
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
25  /*
26  * Constructor.
27  */
28  G1MSD::G1MSD(System& system) :
29  SystemAnalyzer<System>(system),
30  outputFile_(),
31  accumulator_(),
32  truePositions_(),
33  oldPositions_(),
34  shifts_(),
35  speciesPtr_(0),
36  speciesId_(-1),
37  nMolecule_(-1),
38  capacity_(-1),
39  nAtom_(0)
40  { setClassName("G1MSD"); }
41 
42  /*
43  * Read parameters from file, and allocate data array.
44  */
45  void G1MSD::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, "capacity", capacity_);
53 
54  // Validate parameters
55  if (speciesId_ < 0) UTIL_THROW("Negative speciesId");
56  if (speciesId_ >= system().simulation().nSpecies())
57  UTIL_THROW("speciesId > nSpecies");
58  if (capacity_ <= 0) UTIL_THROW("Negative capacity");
59 
60  speciesPtr_ = &system().simulation().species(speciesId_);
61  nAtom_ = speciesPtr_->nAtom();
62  }
63 
64  /*
65  * Allocate arrays.
66  */
67  void G1MSD::setup()
68  {
69 
70  // Get number of molecules of this species in the System.
71  nMolecule_ = system().nMolecule(speciesId_);
72 
73  int nratoms;
74  nratoms = nMolecule_*nAtom_;
75  // Allocate arrays of position Vectors and shifts
76  truePositions_.allocate(nratoms);
77  oldPositions_.allocate(nratoms);
78  shifts_.allocate(nratoms);
79 
80  // Initialize the AutoCorrArray object
81  accumulator_.setParam(nratoms, capacity_);
82 
83  // Store initial positions, and set initial shift vectors.
84  Vector r;
85  IntVector zero(0);
86  Molecule* moleculePtr;
87  int iatom = 0;
88  for (int i = 0; i < nMolecule_; ++i) {
89  moleculePtr = &system().molecule(speciesId_, i);
90  for (int j = 0 ; j < nAtom_; j++) {
91  r = moleculePtr->atom(j).position();
92  system().boundary().shift(r);
93  oldPositions_[iatom] = r;
94  shifts_[iatom] = zero;
95  iatom++;
96  }
97  }
98 
99  }
100 
101  /*
102  * Evaluate end-to-end vectors of all chains, add to ensemble.
103  */
104  void G1MSD::sample(long iStep)
105  {
106  if (!isAtInterval(iStep)) return;
107 
108  // Confirm that nMolecule has remained constant, and nMolecule > 0.
109  if (nMolecule_ <= 0) {
110  UTIL_THROW("nMolecule <= 0");
111  }
112  if (nMolecule_ != system().nMolecule(speciesId_)) {
113  UTIL_THROW("Number of molecules has changed.");
114  }
115 
116  Vector r;
117  IntVector shift;
118  Molecule* moleculePtr;
119  Vector lengths = system().boundary().lengths();
120  int i, j, k;
121  int iatom = 0;
122  for (i = 0; i < nMolecule_; ++i) {
123  moleculePtr = &system().molecule(speciesId_, i);
124  for (j = 0 ; j < nAtom_; j++) {
125  r = moleculePtr->atom(j).position();
126  system().boundary().shift(r);
127 
128  // Compare current r to previous position, oldPositions_[iatom]
129  system().boundary().distanceSq(r, oldPositions_[iatom], shift);
130 
131  // If this atom crossed a boundary, increment its shift vector
132  shifts_[iatom] += shift;
133 
134  // Reconstruct true position
135  for (k = 0; k < Dimension; ++k) {
136  truePositions_[iatom][k] = r[k] + shifts_[iatom][k]*lengths[k];
137  }
138 
139  // Store current position in box for comparison to next one
140  oldPositions_[iatom] = r;
141 
142  iatom++;
143  }
144  }
145  accumulator_.sample(truePositions_);
146 
147  }
148 
151  {
152 
153  // Echo parameter to analyzer log file
154  fileMaster().openOutputFile(outputFileName(), outputFile_);
155  writeParam(outputFile_);
156  outputFile_ << std::endl;
157  outputFile_ << "nrAtoms " << accumulator_.nEnsemble() << std::endl;
158  outputFile_ << "buffercapacity " << accumulator_.bufferCapacity() << std::endl;
159  outputFile_ << "nSample " << accumulator_.nSample() << std::endl;
160  outputFile_ << std::endl;
161  outputFile_ << "Format of *.dat file:" << std::endl;
162  outputFile_ << "[time in samples] [Mean Sq Displacement]"
163  << std::endl;
164  outputFile_.close();
165 
166  // Output statistical analysis to separate data file
167  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
168  accumulator_.output(outputFile_);
169  outputFile_.close();
170 
171  }
172 
173 }
174 
175 #endif
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void sample(long iStep)
Evaluate end-to-end vectors of all chains, add to ensemble.
Definition: G1MSD.cpp:104
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 Vector & lengths() const
Get Vector of unit cell lengths by const reference.
virtual void readParameters(std::istream &in)
Read parameters from file.
Definition: G1MSD.cpp:45
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.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
G1MSD(System &system)
Constructor.
Definition: G1MSD.cpp:28
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 output()
Output results to file after simulation is completed.
Definition: G1MSD.cpp:150
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
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
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void setup()
Determine number of molecules and allocate memory.
Definition: G1MSD.cpp:67
void setClassName(const char *className)
Set class name string.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
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.