Simpatico  v1.10
StressTensorAverage.h
1 #ifndef MCMD_STRESS_TENSOR_AVERAGE_H
2 #define MCMD_STRESS_TENSOR_AVERAGE_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/Average.h> // member template
14 
15 namespace McMd
16 {
17 
18  using namespace Util;
19 
25  template <class SystemType>
26  class StressTensorAverage : public SystemAnalyzer<SystemType>
27  {
28 
29  public:
30 
36  StressTensorAverage(SystemType& system);
37 
42  {}
43 
49  virtual void readParameters(std::istream& in);
50 
56  virtual void loadParameters(Serializable::IArchive &ar);
57 
63  virtual void save(Serializable::OArchive &ar);
64 
71  template <class Archive>
72  void serialize(Archive& ar, const unsigned int version);
73 
77  virtual void clear();
78 
82  virtual void setup();
83 
89  virtual void sample(long iStep);
90 
94  virtual void output();
95 
96  private:
97 
99  std::ofstream outputFile_;
100 
102  Average sxxAccumulator_;
103 
105  Average sxyAccumulator_;
106 
108  Average sxzAccumulator_;
109 
111  Average syxAccumulator_;
112 
114  Average syyAccumulator_;
115 
117  Average syzAccumulator_;
118 
120  Average szxAccumulator_;
121 
123  Average szyAccumulator_;
124 
126  Average szzAccumulator_;
127 
129  int nSamplePerBlock_;
130 
132  long isInitialized_;
133 
143 
144  };
145 
146  /*
147  * Constructor.
148  */
149  template <class SystemType>
151  : SystemAnalyzer<SystemType>(system),
152  outputFile_(),
153  sxxAccumulator_(),
154  sxyAccumulator_(),
155  sxzAccumulator_(),
156  syxAccumulator_(),
157  syyAccumulator_(),
158  syzAccumulator_(),
159  szxAccumulator_(),
160  szyAccumulator_(),
161  szzAccumulator_(),
162  nSamplePerBlock_(1),
163  isInitialized_(false)
164  {}
165 
166  /*
167  * Read parameters and initialize.
168  */
169  template <class SystemType>
171  {
172  readInterval(in);
173  readOutputFileName(in);
174  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
175 
176  sxxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
177  sxyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
178  sxzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
179  syxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
180  syyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
181  syzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
182  szxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
183  szyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
184  szzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
185 
186  std::string filename;
187  filename = outputFileName();
188  system().fileMaster().openOutputFile(outputFileName(), outputFile_);
189 
190  isInitialized_ = true;
191  }
192 
193  /*
194  * Load state from an archive.
195  */
196  template <class SystemType>
198  {
200  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
201 
202  sxxAccumulator_.loadParameters(ar);
203  sxyAccumulator_.loadParameters(ar);
204  sxzAccumulator_.loadParameters(ar);
205  syxAccumulator_.loadParameters(ar);
206  syyAccumulator_.loadParameters(ar);
207  syzAccumulator_.loadParameters(ar);
208  szxAccumulator_.loadParameters(ar);
209  szyAccumulator_.loadParameters(ar);
210  szzAccumulator_.loadParameters(ar);
211 
212  if (nSamplePerBlock_) {
213  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
214  }
215  isInitialized_ = true;
216  }
217 
218  /*
219  * Save state to archive.
220  */
221  template <class SystemType>
223  { ar & *this; }
224 
225 
226  /*
227  * Serialize to/from an archive.
228  */
229  template <class SystemType>
231  {
232  if (!isInitialized_) {
233  UTIL_THROW("Error: Object not initialized");
234  }
235 
236  sxxAccumulator_.clear();
237  sxyAccumulator_.clear();
238  sxzAccumulator_.clear();
239  syxAccumulator_.clear();
240  syyAccumulator_.clear();
241  syzAccumulator_.clear();
242  szxAccumulator_.clear();
243  szyAccumulator_.clear();
244  szzAccumulator_.clear();
245  }
246 
247  /*
248  * Evaluate pressure, and add to accumulator.
249  */
250  template <class SystemType>
252  {
253  if (isAtInterval(iStep)){
254 
255  SystemType& sys=system();
256 
257  Tensor total;
258  Tensor virial;
259  Tensor kinetic;
260 
261  sys.computeVirialStress(total);
262  sys.computeKineticStress(kinetic);
263  total.add(virial, kinetic);
264 
265  sxxAccumulator_.sample(total(0,0));
266  sxyAccumulator_.sample(total(0,1));
267  sxzAccumulator_.sample(total(0,2));
268  syxAccumulator_.sample(total(1,0));
269  syyAccumulator_.sample(total(1,1));
270  syzAccumulator_.sample(total(1,2));
271  szxAccumulator_.sample(total(2,0));
272  szyAccumulator_.sample(total(2,1));
273  szzAccumulator_.sample(total(2,2));
274  }
275  }
276 
277  /*
278  * Output results to file after simulation is completed.
279  */
280  template <class SystemType>
282  {
283  outputFile_ <<
284  "Sxx=" << Dbl(sxxAccumulator_.average(), 17)<< " +- " << Dbl(sxxAccumulator_.error(), 9, 8) << "\n" <<
285  "Sxy=" << Dbl(sxyAccumulator_.average(), 17)<< " +- " << Dbl(sxyAccumulator_.error(), 9, 8) << "\n" <<
286  "Sxz=" << Dbl(sxzAccumulator_.average(), 17)<< " +- " << Dbl(sxzAccumulator_.error(), 9, 8) << "\n" <<
287  "Syx=" << Dbl(syxAccumulator_.average(), 17)<< " +- " << Dbl(syxAccumulator_.error(), 9, 8) << "\n" <<
288  "Syy=" << Dbl(syyAccumulator_.average(), 17)<< " +- " << Dbl(syyAccumulator_.error(), 9, 8) << "\n" <<
289  "Syz=" << Dbl(syzAccumulator_.average(), 17)<< " +- " << Dbl(syzAccumulator_.error(), 9, 8) << "\n" <<
290  "Szx=" << Dbl(szxAccumulator_.average(), 17)<< " +- " << Dbl(szxAccumulator_.error(), 9, 8) << "\n" <<
291  "Szy=" << Dbl(szyAccumulator_.average(), 17)<< " +- " << Dbl(szyAccumulator_.error(), 9, 8) << "\n" <<
292  "Szz=" << Dbl(szzAccumulator_.average(), 17)<< " +- " << Dbl(szzAccumulator_.error(), 9, 8) << "\n" <<
293  std::endl;
294 
295  outputFile_.close();
296  }
297 
298 }
299 #endif
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
StressTensorAverage(SystemType &system)
Constructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: Average.cpp:74
Calculates the average and variance of a sampled property.
Definition: Average.h:43
double average() const
Return the average of all sampled values.
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.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
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
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
Saving / output archive for binary ostream.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void sample(long iStep)
Sample virial stress components to accumulators.
Periodically write (tensor) StressTensor to file.
virtual void output()
Output final results to file.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void clear()
Clear nSample counter.
Template for Analyzer associated with one System.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Definition: Average.cpp:63
Saving archive for binary istream.
void sample(double value)
Add a sampled value to the ensemble.
Definition: Average.cpp:94
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void readParameters(std::istream &in)
Read dumpPrefix and interval.
FileMaster & fileMaster()
Get the FileMaster by reference.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
const std::string & outputFileName() const
Return outputFileName string.
virtual ~StressTensorAverage()
Destructor.
double error() const
Return a naive estimate for the std deviation of the average.