Simpatico  v1.10
MdVirialStressTensorAverage.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 "MdVirialStressTensorAverage.h"
9 //#include <util/misc/FileMaster.h>
10 #include <mcMd/simulation/Simulation.h>
11 #include <util/misc/ioUtil.h>
12 #include <util/format/Int.h>
13 #include <util/format/Dbl.h>
14 
15 #include <sstream>
16 
17 namespace McMd
18 {
19 
20  using namespace Util;
21 
22  /*
23  * Constructor.
24  */
26  : SystemAnalyzer<MdSystem>(system),
27  sxxAccumulator_(),
28  sxyAccumulator_(),
29  sxzAccumulator_(),
30  syxAccumulator_(),
31  syyAccumulator_(),
32  syzAccumulator_(),
33  szxAccumulator_(),
34  szyAccumulator_(),
35  szzAccumulator_(),
36  nSamplePerBlock_(1),
37  nSample_(0),
38  isInitialized_(false)
39  { setClassName("MdVirialStressTensorAverage"); }
40 
41  /*
42  * Read interval and outputFileName.
43  */
45  {
46  readInterval(in);
48  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
49 
50  sxxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
51  sxyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
52  sxzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
53  syxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
54  syyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
55  syzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
56  szxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
57  szyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
58  szzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
59 
60  std::string filename;
61  filename = outputFileName();
62  system().fileMaster().openOutputFile(outputFileName(), outputFile_);
63 
64  isInitialized_ = true;
65  }
66 
67 
68  /*
69  * Load internal state from an archive.
70  */
72  {
74  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
75 
76  sxxAccumulator_.loadParameters(ar);
77  sxyAccumulator_.loadParameters(ar);
78  sxzAccumulator_.loadParameters(ar);
79  syxAccumulator_.loadParameters(ar);
80  syyAccumulator_.loadParameters(ar);
81  syzAccumulator_.loadParameters(ar);
82  szxAccumulator_.loadParameters(ar);
83  szyAccumulator_.loadParameters(ar);
84  szzAccumulator_.loadParameters(ar);
85 
86  if (nSamplePerBlock_) {
87  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
88  }
89  isInitialized_ = true;
90  }
91 
92  /*
93  * Save internal state to an archive.
94  */
96  { ar & *this; }
97 
98 
99  /*
100  * Read interval and outputFileName.
101  */
103  {
104  if (!isInitialized_) {
105  UTIL_THROW("Error: Object not initialized");
106  }
107 
108  nSample_ = 0;
109 
110  sxxAccumulator_.clear();
111  sxyAccumulator_.clear();
112  sxzAccumulator_.clear();
113  syxAccumulator_.clear();
114  syyAccumulator_.clear();
115  syzAccumulator_.clear();
116  szxAccumulator_.clear();
117  szyAccumulator_.clear();
118  szzAccumulator_.clear();
119  }
120 
121  /*
122  * Sample the stress tensor.
123  */
125  {
126  if (isAtInterval(iStep)) {
127 
128  Tensor virial;
129  system().computeVirialStress<Tensor>(virial);
130 
131  sxxAccumulator_.sample(virial(0,0));
132  sxyAccumulator_.sample(virial(0,1));
133  sxzAccumulator_.sample(virial(0,2));
134  syxAccumulator_.sample(virial(1,0));
135  syyAccumulator_.sample(virial(1,1));
136  syzAccumulator_.sample(virial(1,2));
137  szxAccumulator_.sample(virial(2,0));
138  szyAccumulator_.sample(virial(2,1));
139  szzAccumulator_.sample(virial(2,2));
140 
141  ++nSample_;
142  }
143  }
144 
145  /*
146  * Dump StressTensor Measurment results.
147  */
149  {
150 
151  outputFile_ <<
152  "Sxx=" << Dbl(sxxAccumulator_.average(), 17)<< " +- " << Dbl(sxxAccumulator_.error(), 9, 8) << "\n" <<
153  "Sxy=" << Dbl(sxyAccumulator_.average(), 17)<< " +- " << Dbl(sxyAccumulator_.error(), 9, 8) << "\n" <<
154  "Sxz=" << Dbl(sxzAccumulator_.average(), 17)<< " +- " << Dbl(sxzAccumulator_.error(), 9, 8) << "\n" <<
155  "Syx=" << Dbl(syxAccumulator_.average(), 17)<< " +- " << Dbl(syxAccumulator_.error(), 9, 8) << "\n" <<
156  "Syy=" << Dbl(syyAccumulator_.average(), 17)<< " +- " << Dbl(syyAccumulator_.error(), 9, 8) << "\n" <<
157  "Syz=" << Dbl(syzAccumulator_.average(), 17)<< " +- " << Dbl(syzAccumulator_.error(), 9, 8) << "\n" <<
158  "Szx=" << Dbl(szxAccumulator_.average(), 17)<< " +- " << Dbl(szxAccumulator_.error(), 9, 8) << "\n" <<
159  "Szy=" << Dbl(szyAccumulator_.average(), 17)<< " +- " << Dbl(szyAccumulator_.error(), 9, 8) << "\n" <<
160  "Szz=" << Dbl(szzAccumulator_.average(), 17)<< " +- " << Dbl(szzAccumulator_.error(), 9, 8) << "\n" <<
161  std::endl;
162  }
163 
164 }
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
MdVirialStressTensorAverage(MdSystem &system)
Constructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: Average.cpp:74
FileMaster & fileMaster() const
Get the associated FileMaster by reference.
Definition: System.h:1113
double average() const
Return the average of all sampled values.
virtual void output()
Dump configuration to file.
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.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
MdSystem & 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.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
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
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void computeVirialStress(T &stress) const
Compute total virial stress, from all forces.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void sample(long iStep)
Sample virial stress to accumulators.
Template for Analyzer associated with one System.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Definition: Average.cpp:63
Saving archive for binary istream.
virtual void clear()
Clear nSample counter.
void sample(double value)
Add a sampled value to the ensemble.
Definition: Average.cpp:94
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setClassName(const char *className)
Set class name string.
FileMaster & fileMaster()
Get the FileMaster by reference.
A System for Molecular Dynamics simulation.
Definition: MdSystem.h:68
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 readParameters(std::istream &in)
Read dumpPrefix and interval.
double error() const
Return a naive estimate for the std deviation of the average.