Simpatico  v1.10
StressAutoCorr.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 "StressAutoCorr.h"
9 #include <util/accumulators/AutoCorrelation.tpp>
10 #include <util/misc/ioUtil.h>
11 #include <util/format/Int.h>
12 #include <util/format/Dbl.h>
13 
14 #include <sstream>
15 
16 namespace DdMd
17 {
18 
19  using namespace Util;
20 
21  /*
22  * Constructor.
23  */
25  : Analyzer(simulation),
26  accumulatorPtr_(0),
27  bufferCapacity_(-1),
28  maxStageId_(10),
29  isInitialized_(false)
30  { setClassName("StressAutoCorr"); }
31 
32  /*
33  * Read interval and outputFileName.
34  */
35  void StressAutoCorr::readParameters(std::istream& in)
36  {
37  readInterval(in);
39  read<int>(in,"bufferCapacity", bufferCapacity_);
40  if (simulation().domain().isMaster()) {
41  accumulatorPtr_ = new AutoCorrelation<Tensor, double>;
42  accumulatorPtr_->setParam(bufferCapacity_, maxStageId_);
43  }
44  isInitialized_ = true;
45  }
46 
47 
48  /*
49  * Load internal state from an archive.
50  */
52  {
53  loadInterval(ar);
55  loadParameter(ar, "bufferCapacity", bufferCapacity_);
56 
57  if (simulation().domain().isMaster()) {
58  accumulatorPtr_ = new AutoCorrelation<Tensor, double>;
59  ar >> *accumulatorPtr_;
60  }
61 
62  isInitialized_ = true;
63  }
64 
65  /*
66  * Save internal state to an archive.
67  */
69  {
70  saveInterval(ar);
72  ar & bufferCapacity_;
73  if (simulation().domain().isMaster()) {
74  if (!accumulatorPtr_) {
75  UTIL_THROW("Null accumulatorPtr_ on master");
76  }
77  ar << *accumulatorPtr_;
78  }
79  }
80 
81  /*
82  * Clear accumulator.
83  */
85  {
86  if (!isInitialized_) {
87  UTIL_THROW("Error: Object not initialized");
88  }
89  if (simulation().domain().isMaster()) {
90  if (!accumulatorPtr_) {
91  UTIL_THROW("Null accumulatorPtr_ on master");
92  }
93  accumulatorPtr_->clear();
94  }
95  }
96 
97 
98  /*
99  * * Set actual number of molecules and clear accumulator.
100  * */
102  {
103  if (!isInitialized_) {
104  UTIL_THROW("Object not initialized.");
105  }
106  clear();
107  }
108 
109  /*
110  * Sample the stress tensor.
111  */
112  void StressAutoCorr::sample(long iStep)
113  {
114  if (isAtInterval(iStep)) {
115  Simulation& sim = simulation();
116  sim.computeVirialStress();
117  sim.computeKineticStress();
118 
119  if (sim.domain().isMaster()) {
120  if (!accumulatorPtr_) {
121  UTIL_THROW("Null accumulatorPtr_ on master");
122  }
123 
124  Tensor virial = sim.virialStress();
125  Tensor kinetic = sim.kineticStress();
126  Tensor total;
127  total.add(virial, kinetic);
128 
129  // Remove trace
130  double pressure = 0.0;
131  int i, j;
132  for (i = 0; i < Dimension; ++i) {
133  pressure += total(i,i);
134  }
135  pressure = pressure/double(Dimension);
136  for (i = 0; i < Dimension; ++i) {
137  total(i,i) -= pressure;
138  }
139 
140  double factor = sqrt(sim.boundary().volume()/10.0);
141  for (i = 0; i < Dimension; ++i) {
142  for (j = 0; j < Dimension; ++j) {
143  total(i,j) *= factor;
144  }
145  }
146 
147  accumulatorPtr_->sample(total);
148  }
149  }
150  }
151 
152  /*
153  * Dump StressTensor Measurment results.
154  */
156  {
157  if (simulation().domain().isMaster()) {
158  if (!accumulatorPtr_) {
159  UTIL_THROW("Null accumulatorPtr_ on master");
160  }
161 
162  simulation().fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
163  writeParam(outputFile_);
164  outputFile_ << std::endl;
165  outputFile_ << "bufferCapacity " << accumulatorPtr_->bufferCapacity() << std::endl;
166  outputFile_ << "nSample " << accumulatorPtr_->nSample() << std::endl;
167  outputFile_ << std::endl;
168  outputFile_ << "Format of *.dat file" << std::endl;
169  outputFile_ << "[int time delay (samples)] [double autocorrelation function]"
170  << std::endl;
171  outputFile_ << std::endl;
172  outputFile_.close();
173 
174  // Write xy autocorrelation function to data file
175  simulation().fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
176  accumulatorPtr_->output(outputFile_);
177  outputFile_.close();
178  }
179  }
180 
181 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
virtual void sample(long iStep)
Sample virial stress to accumulators.
double volume() const
Return unit cell volume.
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
void readInterval(std::istream &in)
Read parameter interval from file.
void clear()
Clear accumulators and destroy descendants.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
Tensor & add(const Tensor &t1, const Tensor &t2)
Add tensors t1 and t2.
Definition: Tensor.h:567
void output(std::ostream &out)
Output the autocorrelation function, assuming zero mean.
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
virtual void clear()
Clear nSample counter.
FileMaster & fileMaster()
Get the associated FileMaster by reference.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual void setup()
Setup accumulator!
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void setParam(int bufferCapacity=64, int maxStageId=0, int blockFactor=2)
Set all parameters and allocate to initialize state.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Tensor kineticStress() const
Return total kinetic stress.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
long nSample() const
Return the number of sampled values.
const std::string & outputFileName() const
Return outputFileName string.
bool isMaster() const
Is this the master processor (gridRank == 0) ?
Definition: Domain.h:313
void computeVirialStress()
Calculate and store all virial stress contributions.
virtual void output()
Dump configuration to file.
Saving archive for binary istream.
int bufferCapacity() const
Return capacity of history buffer.
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
StressAutoCorr(Simulation &simulation)
Constructor.
Boundary & boundary()
Get the Boundary by reference.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
virtual void sample(Data value)
Sample a value.
Domain & domain()
Get the Domain by reference.
Tensor virialStress() const
Return total virial stress.
void computeKineticStress()
Calculate and store kinetic stress.
virtual void readParameters(std::istream &in)
Read dumpPrefix and interval.