Simpatico  v1.10
mcMd/analyzers/system/StressAutoCorr.h
1 #ifndef MCMD_STRESS_AUTO_CORR_H
2 #define MCMD_STRESS_AUTO_CORR_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <mcMd/analyzers/SystemAnalyzer.h>
12 #include <util/space/Tensor.h>
13 #include <util/accumulators/AutoCorrelation.tpp> // member template
14 
15 namespace McMd
16 {
17 
18  using namespace Util;
19 
27  template <class SystemType>
28  class StressAutoCorr : public SystemAnalyzer<SystemType>
29  {
30 
31  public:
32 
38  StressAutoCorr(SystemType& system);
39 
43  virtual ~StressAutoCorr();
44 
50  virtual void readParameters(std::istream& in);
51 
57  virtual void loadParameters(Serializable::IArchive &ar);
58 
64  virtual void save(Serializable::OArchive &ar);
65 
72  template <class Archive>
73  void serialize(Archive& ar, const unsigned int version);
74 
78  virtual void setup();
79 
85  virtual void sample(long iStep);
86 
90  virtual void output();
91 
92  protected:
93 
99  virtual void computeStress(Tensor& stress) = 0;
100 
111 
112  private:
113 
115  std::ofstream outputFile_;
116 
118  AutoCorrelation<Tensor, double> accumulator_;
119 
121  int capacity_;
122 
124  int maxStageId_;
125 
127  int blockFactor_;
128 
130  long isInitialized_;
131 
132  };
133 
134  /*
135  * Constructor.
136  */
137  template <class SystemType>
139  : SystemAnalyzer<SystemType>(system),
140  outputFile_(),
141  accumulator_(),
142  capacity_(64),
143  maxStageId_(10),
144  blockFactor_(2),
145  isInitialized_(false)
146  {}
147 
148  /*
149  * Destructor.
150  */
151  template <class SystemType>
153  {}
154 
155  /*
156  * Read parameters and initialize.
157  */
158  template <class SystemType>
160  {
161  readInterval(in);
162  readOutputFileName(in);
163  read(in, "capacity", capacity_);
164  readOptional(in, "maxStageId", maxStageId_);
165 
166  accumulator_.setParam(capacity_, maxStageId_, blockFactor_);
167  accumulator_.clear();
168 
169  isInitialized_ = true;
170  }
171 
172  /*
173  * Load state from an archive.
174  */
175  template <class SystemType>
177  {
179 
180  loadParameter(ar, "capacity", capacity_);
181  ar & accumulator_;
182 
183  if (accumulator_.bufferCapacity() != capacity_) {
184  UTIL_THROW("Inconsistent values of capacity");
185  }
186 
187  isInitialized_ = true;
188  }
189 
190  /*
191  * Save state to archive.
192  */
193  template <class SystemType>
195  { ar & *this; }
196 
197 
198  /*
199  * Serialize to/from an archive.
200  */
201  template <class SystemType>
202  template <class Archive>
203  void
205  const unsigned int version)
206  {
207  Analyzer::serialize(ar, version);
208  ar & capacity_;
209  ar & accumulator_;
210  }
211 
212  /*
213  * Set up immediately before simulation.
214  */
215  template <class SystemType>
217  {
218  if (!isInitialized_) {
219  UTIL_THROW("Object not initialized");
220  }
221  accumulator_.clear();
222  }
223 
224  /*
225  * Evaluate pressure, and add to accumulator.
226  */
227  template <class SystemType>
229  {
230  if (isAtInterval(iStep)){
231 
232  #if 0
233  // Compute stress
234  Tensor total;
235  Tensor virial;
236  Tensor kinetic;
237  system().computeVirialStress(virial);
238  system().computeKineticStress(kinetic);
239  total.add(virial, kinetic);
240  #endif
241 
242  Tensor stress;
243  computeStress(stress);
244 
245  // Remove trace
246  double pressure = 0.0;
247  int i, j;
248  for (i = 0; i < Dimension; ++i) {
249  pressure += stress(i,i);
250  }
251  pressure = pressure/double(Dimension);
252  for (i = 0; i < Dimension; ++i) {
253  stress(i,i) -= pressure;
254  }
255 
256  double factor = 1.0/sqrt(10.0);
257  for (i = 0; i < Dimension; ++i) {
258  for (j = 0; j < Dimension; ++j) {
259  stress(i,j) *= factor;
260  }
261  }
262 
263  accumulator_.sample(stress);
264  }
265  }
266 
267  /*
268  * Output results to file after simulation is completed.
269  */
270  template <class SystemType>
272  {
273  // Write parameters to *.prm file
274  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
275  writeParam(outputFile_);
276  outputFile_ << std::endl;
277  outputFile_ << "bufferCapacity "
278  << accumulator_.bufferCapacity() << std::endl;
279  outputFile_ << "nSample "
280  << accumulator_.nSample() << std::endl;
281  outputFile_ << std::endl;
282  outputFile_.close();
283 
284  // Write autocorrelation function to data file
285  fileMaster().openOutputFile(outputFileName(".corr"), outputFile_);
286  accumulator_.output(outputFile_);
287  outputFile_.close();
288  }
289 
290 }
291 #endif
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Compute stress autocorrelation for an isotropic system.
virtual void computeStress(Tensor &stress)=0
Compute total stress tensor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
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 loadParameters(Serializable::IArchive &ar)
Load parameters from archive.
SystemType & system()
Return reference to parent system.
virtual void readParameters(std::istream &in)
Read parameters from file and initialize.
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
void clear()
Clear accumulators and destroy descendants.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void serialize(Archive &ar, PairSelector &selector, const unsigned int version)
Serialize a PairSelector.
Definition: PairSelector.h:167
virtual void setup()
Setup before beginning of loop.
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.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
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 setParam(int bufferCapacity=64, int maxStageId=0, int blockFactor=2)
Set all parameters and allocate to initialize state.
StressAutoCorr(SystemType &system)
Constructor.
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.
Template for Analyzer associated with one System.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
virtual void sample(long iStep)
Sample stress tensor and add to accumulator.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int bufferCapacity() const
Return capacity of history buffer.
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.
virtual void sample(Data value)
Sample a value.
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
virtual void output()
Output results to file (parameters and autocorrelation function).
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.