Simpatico  v1.10
SymmTensorAverageAnalyzer.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 <util/global.h>
9 #include "SymmTensorAverageAnalyzer.h"
10 #include <ddMd/simulation/Simulation.h>
11 #include <util/accumulators/SymmTensorAverage.h>
12 #include <util/space/Tensor.h>
13 #include <util/format/Int.h>
14 #include <util/format/Dbl.h>
15 #include <util/misc/ioUtil.h>
16 
17 #include <sstream>
18 
19 namespace DdMd
20 {
21 
22  using namespace Util;
23 
24  /*
25  * Constructor.
26  */
28  : Analyzer(simulation),
29  outputFile_(),
30  accumulatorPtr_(0),
31  nSamplePerBlock_(0),
32  isInitialized_(false)
33  { setClassName("SymmTensorAverageAnalyzer"); }
34 
35  /*
36  * Destructor.
37  */
39  {
40  if (accumulatorPtr_) {
41  delete accumulatorPtr_;
42  }
43  }
44 
45  /*
46  * Read interval and outputFileName.
47  */
49  {
50  readInterval(in);
52  nSamplePerBlock_ = 0;
53  readOptional<int>(in,"nSamplePerBlock", nSamplePerBlock_);
54  if (simulation().domain().isMaster()) {
55  accumulatorPtr_ = new SymmTensorAverage;
56  accumulatorPtr_->setNSamplePerBlock(nSamplePerBlock_);
57  }
58  isInitialized_ = true;
59  }
60 
61  /*
62  * Load internal state from an archive.
63  */
65  {
66  loadInterval(ar);
68  nSamplePerBlock_ = 0;
69  bool isRequired = false;
70  loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_, isRequired);
71  if (simulation().domain().isMaster()) {
72  accumulatorPtr_ = new SymmTensorAverage;
73  ar >> *accumulatorPtr_;
74  if (nSamplePerBlock_ != accumulatorPtr_->nSamplePerBlock()) {
75  UTIL_THROW("Inconsistent values of nSamplePerBlock");
76  }
77  } else {
78  accumulatorPtr_ = 0;
79  }
80  isInitialized_ = true;
81  }
82 
83  /*
84  * Save internal state to an archive.
85  */
87  {
88  assert(simulation().domain().isMaster());
89  assert(accumulatorPtr_);
90 
91  saveInterval(ar);
93  bool isActive = (bool)nSamplePerBlock_;
94  Parameter::saveOptional(ar, nSamplePerBlock_, isActive);
95  ar << *accumulatorPtr_;
96  }
97 
98  /*
99  * Clear accumulator (do nothing on slave processors).
100  */
102  {
103  if (simulation().domain().isMaster()){
104  accumulatorPtr_->clear();
105  }
106  }
107 
108  /*
109  * Open outputfile
110  */
112  {
113  if (simulation().domain().isMaster()) {
114  if (nSamplePerBlock_) {
115  std::string filename = outputFileName(".dat");
116  simulation().fileMaster().openOutputFile(filename, outputFile_);
117  }
118  }
119  }
120 
121  /*
122  * Compute value and add to sequence.
123  */
125  {
126  if (!isAtInterval(iStep)) {
127  UTIL_THROW("Time step index is not a multiple of interval");
128  }
129  compute();
130  if (simulation().domain().isMaster()) {
131  Tensor data = value();
132  accumulatorPtr_->sample(data);
133  if (nSamplePerBlock_ > 0 && accumulatorPtr_->isBlockComplete()) {
134  int beginStep = iStep - (nSamplePerBlock_ - 1)*interval();
135  outputFile_ << Int(beginStep) << " ";
136  double ave;
137  int i, j;
138  for (i = 0; i < Dimension; ++i) {
139  for (j = 0; j <= i; ++j) {
140  ave = (*accumulatorPtr_)(i, j).blockAverage();
141  outputFile_ << Dbl(ave) << " ";
142  }
143  }
144  outputFile_ << "\n";
145  }
146  }
147  }
148 
149  /*
150  * Output results to file after simulation is completed.
151  */
153  {
154  if (simulation().domain().isMaster()) {
155 
156  // Close data (*.dat) file, if any
157  if (outputFile_.is_open()) {
158  outputFile_.close();
159  }
160 
161  // Write parameter (*.prm) file
162  FileMaster& fileMaster = simulation().fileMaster();
163  fileMaster.openOutputFile(outputFileName(".prm"), outputFile_);
164  ParamComposite::writeParam(outputFile_);
165  outputFile_.close();
166 
167  // Write average (*.ave) file with averages for all elements
168  fileMaster.openOutputFile(outputFileName(".ave"), outputFile_);
169  double ave, err;
170  int i, j;
171  for (i = 0; i < Dimension; ++i) {
172  for (j = 0; j <= i ; ++j) {
173  ave = (*accumulatorPtr_)(i, j).average();
174  err = (*accumulatorPtr_)(i, j).blockingError();
175  outputFile_ << "Average(" << i << ", " << j << ") = ";
176  outputFile_ << Dbl(ave) << " +- " << Dbl(err, 9, 2) << "\n";
177  }
178  }
179  outputFile_.close();
180 
181  // Write average error analysis (*.aer) file
182  fileMaster.openOutputFile(outputFileName(".aer"), outputFile_);
183  for (i = 0; i < Dimension; ++i) {
184  for (j = 0; j <= i ; ++j) {
185  outputFile_ << "Element(" << i << ", " << j << "): \n\n";
186  (*accumulatorPtr_)(i, j).output(outputFile_);
187  outputFile_ <<
188  "----------------------------------------------------------------------------\n";
189  }
190  }
191  outputFile_.close();
192 
193  }
194  }
195 
196 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
void clear()
Clear all accumulators, set to empty initial state.
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
virtual void compute()=0
Compute value of sampled quantity.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
bool isRequired() const
Is this ParamComposite required in the input file?
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an input archive.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
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 sample(long iStep)
Compute a sampled value and add it to the accumulator.
bool isBlockComplete() const
Is the current block average complete?
void readInterval(std::istream &in)
Read parameter interval from file.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
File containing preprocessor macros for error handling.
void sample(const Tensor &value)
Add a sampled value to the ensemble.
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
bool isActive() const
Is this parameter active?
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
virtual Tensor value()=0
Get current value, set by compute function.
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
Calculates averages of all components of a Tensor-valued variable.
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
SymmTensorAverageAnalyzer(Simulation &simulation)
Constructor.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
const std::string & outputFileName() const
Return outputFileName string.
bool isMaster() const
Is this the master processor (gridRank == 0) ?
Definition: Domain.h:313
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:142
virtual void setup()
Setup before loop.
Saving archive for binary istream.
virtual void save(Serializable::OArchive &ar)
Save internal state to an output archive.
virtual void readParameters(std::istream &in)
Read interval, outputFileName and (optionally) nSamplePerBlock.
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
int interval() const
Get interval value.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
virtual void output()
Write final results to a file.
Domain & domain()
Get the Domain by reference.
static void saveOptional(Serializable::OArchive &ar, Type &value, bool isActive)
Save an optional parameter value to an output archive.
Definition: Parameter.h:224
virtual void clear()
Clear accumulator on master, do nothing on other processors.