Simpatico  v1.10
tools/analyzers/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 <tools/chemistry/Molecule.h>
10 #include <tools/chemistry/Atom.h>
11 #include <tools/chemistry/Species.h>
12 #include <tools/storage/Configuration.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 Tools
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
25  /*
26  * Constructor.
27  */
29  : Analyzer(processor),
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  * Constructor.
44  */
46  : Analyzer(configuration, fileMaster),
47  outputFile_(),
48  accumulator_(),
49  truePositions_(),
50  oldPositions_(),
51  shifts_(),
52  speciesId_(-1),
53  atomId_(-1),
54  nMolecule_(-1),
55  capacity_(-1),
56  isInitialized_(false)
57  { setClassName("AtomMSD"); }
58 
59  /*
60  * Read parameters from file, and allocate data array.
61  */
62  void AtomMSD::readParameters(std::istream& in)
63  {
64 
65  // Read interval and parameters for AutoCorrArray
66  readInterval(in);
68  read<int>(in, "speciesId", speciesId_);
69  read<int>(in, "atomId", atomId_);
70  read<int>(in, "capacity", capacity_);
71 
72  // Validate parameters
73  if (speciesId_ < 0) {
74  UTIL_THROW("Negative speciesId");
75  }
76  if (speciesId_ >= configuration().nSpecies()) {
77  UTIL_THROW("speciesId > nSpecies");
78  }
79  Species& species = configuration().species(speciesId_);
80  if (atomId_ < 0) {
81  UTIL_THROW("Negative atomId");
82  }
83  if (atomId_ >= species.nAtom()) {
84  UTIL_THROW("atomId >= nAtom");
85  }
86  if (capacity_ <= 0) {
87  UTIL_THROW("Negative capacity");
88  }
89 
90  // Maximum possible number of molecules of this species
91  int speciesCapacity = species.capacity();
92 
93  // Allocate local arrays
94  truePositions_.allocate(speciesCapacity);
95  oldPositions_.allocate(speciesCapacity);
96  shifts_.allocate(speciesCapacity);
97 
98  // Allocate memory for the AutoCorrArray accumulator
99  accumulator_.setParam(speciesCapacity, capacity_);
100 
101  isInitialized_ = true;
102  }
103 
104  /*
105  * Initialize at beginning of simulation.
106  */
108  {
109  if (!isInitialized_) {
110  UTIL_THROW("Error: object is not initialized");
111  }
112  Species& species = configuration().species(speciesId_);
113  Boundary& boundary = configuration().boundary();
114 
115  // Set number of molecules of this species in the Configuration.
116  nMolecule_ = species.size();
117  accumulator_.setNEnsemble(nMolecule_);
118  accumulator_.clear();
119 
120  // Store initial positions, and set initial shift vectors.
121  Vector r;
122  IntVector zero(0);
123  Species::Iterator iter;
124  int i = 0;
125 
126  species.begin(iter);
127  for ( ; iter.notEnd(); ++iter) {
128  r = iter->atom(atomId_).position;
129  boundary.shift(r);
130  oldPositions_[i] = r;
131  shifts_[i] = zero;
132  ++i;
133  }
134  }
135 
136  /*
137  * Evaluate end-to-end vectors of all chains, add to ensemble.
138  */
139  void AtomMSD::sample(long iStep)
140  {
141  if (isAtInterval(iStep)) {
142  Species& species = configuration().species(speciesId_);
143  Boundary& boundary = configuration().boundary();
144 
145  // Confirm that nMolecule is positive, and unchanged.
146  if (nMolecule_ <= 0) {
147  UTIL_THROW("nMolecule <= 0");
148  }
149  if (nMolecule_ != species.size()) {
150  UTIL_THROW("Number of molecules has changed.");
151  }
152 
153  Vector r;
154  IntVector shift;
155  Vector lengths = boundary.lengths();
156  int i, j;
157  Species::Iterator iter;
158 
159  i = 0;
160  for (species.begin(iter); iter.notEnd(); ++iter) {
161 
162  r = iter->atom(atomId_).position;
163  boundary.shift(r);
164 
165  // Compare current r to previous position, oldPositions_[i]
166  boundary.distanceSq(r, oldPositions_[i], shift);
167 
168  // If a boundary was crossed, increment shift vector
169  shifts_[i] += shift;
170 
171  // Reconstruct true position
172  for (j = 0; j < Dimension; ++j) {
173  truePositions_[i][j] = r[j] + shifts_[i][j]*lengths[j];
174  }
175 
176  // Store current position in box for comparison to next one
177  oldPositions_[i] = r;
178 
179  ++i;
180  }
181  accumulator_.sample(truePositions_);
182  }
183 
184  }
185 
188  {
189 
190  // Output parameters
192  outputFile_);
193 
194  writeParam(outputFile_);
195  outputFile_ << std::endl;
196  outputFile_ << std::endl;
197  outputFile_ << "nMolecule " << accumulator_.nEnsemble()
198  << std::endl;
199  outputFile_ << "buffercapacity " << accumulator_.bufferCapacity()
200  << std::endl;
201  outputFile_ << "nSample " << accumulator_.nSample()
202  << std::endl;
203  outputFile_ << std::endl;
204  //outputFile_ << "Format of *.dat file:" << std::endl;
205  //outputFile_ << "[i] [MSD]" << std::endl;
206  outputFile_.close();
207 
208  // Output statistical analysis to separate data file
210  outputFile_);
211  accumulator_.output(outputFile_);
212  outputFile_.close();
213 
214  }
215 
216 }
void readOutputFileName(std::istream &in)
Read outputFileName from file.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
A post-processor for analyzing outputs of MD simulations.
Definition: Processor.h:30
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.
AtomMSD(Processor &processor)
Constructor.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
const Vector & lengths() const
Get Vector of unit cell lengths by const reference.
An orthorhombic periodic unit cell.
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
File containing preprocessor macros for error handling.
Abstract base for periodic output and/or analysis actions.
Classes used by all simpatico molecular simulations.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
Configuration & configuration()
Get the parent Configuration by reference.
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
Single-processor classes for pre- and post-processing MD trajectories.
void readInterval(std::istream &in)
Read parameter interval from file.
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:142
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
virtual void setup()
Determine number of molecules and allocate memory.
Boundary & boundary()
Get the Boundary by non-const reference.
virtual void output()
Output results to file after simulation is completed.
void setClassName(const char *className)
Set class name string.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
int capacity() const
Maximum allowed number of molecules for this Species.
virtual void sample(long iStep)
Evaluate end-to-end vectors of all chains, add to ensemble.
FileMaster & fileMaster()
Get an associated FileMaster by reference.
Species & species(int i)
Get a particular species identified by index.
A Species represents a set of chemically similar molecules.
virtual void readParameters(std::istream &in)
Read parameters from file.
const std::string & outputFileName() const
Return outputFileName string.