Simpatico  v1.10
VirialStressTensorAverage.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 "VirialStressTensorAverage.h"
9 //#include <util/misc/FileMaster.h>
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  sxxAccumulator_(),
27  sxyAccumulator_(),
28  sxzAccumulator_(),
29  syxAccumulator_(),
30  syyAccumulator_(),
31  syzAccumulator_(),
32  szxAccumulator_(),
33  szyAccumulator_(),
34  szzAccumulator_(),
35  nSamplePerBlock_(1),
36  isInitialized_(false)
37  { setClassName("VirialStressTensorAverage"); }
38 
39  /*
40  * Read interval and outputFileName.
41  */
43  {
44  readInterval(in);
46  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
47  sxxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
48  sxyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
49  sxzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
50  syxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
51  syyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
52  syzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
53  szxAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
54  szyAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
55  szzAccumulator_.setNSamplePerBlock(nSamplePerBlock_);
57  isInitialized_ = true;
58  }
59 
60 
61  /*
62  * Load internal state from an archive.
63  */
65  {
66  loadInterval(ar);
68  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
69  if (simulation().domain().isMaster()) {
70  #if 0
71  sxxAccumulator_.loadParameters(ar);
72  sxyAccumulator_.loadParameters(ar);
73  sxzAccumulator_.loadParameters(ar);
74  syxAccumulator_.loadParameters(ar);
75  syyAccumulator_.loadParameters(ar);
76  syzAccumulator_.loadParameters(ar);
77  szxAccumulator_.loadParameters(ar);
78  szyAccumulator_.loadParameters(ar);
79  szzAccumulator_.loadParameters(ar);
80  #endif
81  ar >> sxxAccumulator_;
82  ar >> sxyAccumulator_;
83  ar >> sxzAccumulator_;
84  ar >> syxAccumulator_;
85  ar >> syyAccumulator_;
86  ar >> syzAccumulator_;
87  ar >> szxAccumulator_;
88  ar >> szyAccumulator_;
89  ar >> szzAccumulator_;
91  }
92  isInitialized_ = true;
93  }
94 
95  /*
96  * Save internal state to an archive.
97  */
99  {
100  assert(simulation().domain().isMaster());
101  saveInterval(ar);
102  saveOutputFileName(ar);
103  ar << nSamplePerBlock_;
104  ar << sxxAccumulator_;
105  ar << sxyAccumulator_;
106  ar << sxzAccumulator_;
107  ar << syxAccumulator_;
108  ar << syyAccumulator_;
109  ar << syzAccumulator_;
110  ar << szxAccumulator_;
111  ar << szyAccumulator_;
112  ar << szzAccumulator_;
113  }
114 
115 
116  /*
117  * Read interval and outputFileName.
118  */
120  {
121  if (!isInitialized_) {
122  UTIL_THROW("Error: Object not initialized");
123  }
124  nSample_ = 0;
125  if (simulation().domain().isMaster()) {
126  sxxAccumulator_.clear();
127  sxyAccumulator_.clear();
128  sxzAccumulator_.clear();
129  syxAccumulator_.clear();
130  syyAccumulator_.clear();
131  syzAccumulator_.clear();
132  szxAccumulator_.clear();
133  szyAccumulator_.clear();
134  szzAccumulator_.clear();
135  }
136  }
137 
138  /*
139  * Sample the stress tensor.
140  */
142  {
143  if (isAtInterval(iStep)) {
144  Simulation& sys = simulation();
145  sys.computeVirialStress();
146  sys.computeKineticStress();
147  if (sys.domain().isMaster()) {
148  Tensor virial = sys.virialStress();
149  Tensor kinetic = sys.kineticStress();
150  Tensor total;
151  total.add(virial, kinetic);
152  sxxAccumulator_.sample(virial(0,0));
153  sxyAccumulator_.sample(virial(0,1));
154  sxzAccumulator_.sample(virial(0,2));
155  syxAccumulator_.sample(virial(1,0));
156  syyAccumulator_.sample(virial(1,1));
157  syzAccumulator_.sample(virial(1,2));
158  szxAccumulator_.sample(virial(2,0));
159  szyAccumulator_.sample(virial(2,1));
160  szzAccumulator_.sample(virial(2,2));
161  }
162  ++nSample_;
163  }
164  }
165 
166  /*
167  * Dump StressTensor Measurment results.
168  */
170  {
171 
172  if (simulation().domain().isMaster()) {
173 
174  outputFile_ <<
175  "Sxx=" << Dbl(sxxAccumulator_.average(), 17)<< " +- " << Dbl(sxxAccumulator_.error(), 9, 8) << "\n" <<
176  "Sxy=" << Dbl(sxyAccumulator_.average(), 17)<< " +- " << Dbl(sxyAccumulator_.error(), 9, 8) << "\n" <<
177  "Sxz=" << Dbl(sxzAccumulator_.average(), 17)<< " +- " << Dbl(sxzAccumulator_.error(), 9, 8) << "\n" <<
178  "Syx=" << Dbl(syxAccumulator_.average(), 17)<< " +- " << Dbl(syxAccumulator_.error(), 9, 8) << "\n" <<
179  "Syy=" << Dbl(syyAccumulator_.average(), 17)<< " +- " << Dbl(syyAccumulator_.error(), 9, 8) << "\n" <<
180  "Syz=" << Dbl(syzAccumulator_.average(), 17)<< " +- " << Dbl(syzAccumulator_.error(), 9, 8) << "\n" <<
181  "Szx=" << Dbl(szxAccumulator_.average(), 17)<< " +- " << Dbl(szxAccumulator_.error(), 9, 8) << "\n" <<
182  "Szy=" << Dbl(szyAccumulator_.average(), 17)<< " +- " << Dbl(szyAccumulator_.error(), 9, 8) << "\n" <<
183  "Szz=" << Dbl(szzAccumulator_.average(), 17)<< " +- " << Dbl(szzAccumulator_.error(), 9, 8) << "\n" <<
184  std::endl;
185  }
186  }
187 
188 }
virtual void clear()
Clear nSample counter.
Abstract base for periodic output and/or analysis actions.
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
Simulation & simulation()
Get the parent Simulation by reference.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: Average.cpp:74
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
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
VirialStressTensorAverage(Simulation &simulation)
Constructor.
void readInterval(std::istream &in)
Read parameter interval from file.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
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
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
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 output()
Dump configuration to file.
virtual void readParameters(std::istream &in)
Read dumpPrefix and interval.
virtual void sample(long iStep)
Sample virial stress to accumulators.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Tensor kineticStress() const
Return total kinetic stress.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
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.
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
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
Domain & domain()
Get the Domain by reference.
Tensor virialStress() const
Return total virial stress.
void computeKineticStress()
Calculate and store kinetic stress.
double error() const
Return a naive estimate for the std deviation of the average.