Simpatico  v1.10
TensorAverageAnalyzer.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 "TensorAverageAnalyzer.h"
9 #include <ddMd/simulation/Simulation.h>
10 #include <util/accumulators/TensorAverage.h>
11 #include <util/space/Tensor.h>
12 #include <util/format/Int.h>
13 #include <util/format/Dbl.h>
14 #include <util/mpi/MpiLoader.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("TensorAverageAnalyzer"); }
34 
35  /*
36  * Destructor.
37  */
39  {
40  if (accumulatorPtr_) {
41  delete accumulatorPtr_;
42  }
43  }
44 
45  /*
46  * Read interval and outputFileName.
47  */
48  void TensorAverageAnalyzer::readParameters(std::istream& in)
49  {
50  readInterval(in);
52  readOptional<int>(in,"nSamplePerBlock", nSamplePerBlock_);
53 
54  if (simulation().domain().isMaster()) {
55  accumulatorPtr_ = new TensorAverage;
56  accumulatorPtr_->setNSamplePerBlock(nSamplePerBlock_);
57  }
58 
59  isInitialized_ = true;
60  }
61 
62  /*
63  * Load internal state from an archive.
64  */
66  {
67  loadInterval(ar);
69  nSamplePerBlock_ = 0;
70  bool isRequired = false;
71  loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_,
72  isRequired);
73  if (simulation().domain().isMaster()) {
74  accumulatorPtr_ = new TensorAverage;
75  ar >> *accumulatorPtr_;
76  if (nSamplePerBlock_ != accumulatorPtr_->nSamplePerBlock()) {
77  UTIL_THROW("Inconsistent values of nSamplePerBlock");
78  }
79  } else {
80  accumulatorPtr_ = 0;
81  }
82  isInitialized_ = true;
83  }
84 
85  /*
86  * Save internal state to an archive.
87  */
89  {
90  saveInterval(ar);
92  bool isActive = (bool)nSamplePerBlock_;
93  Parameter::saveOptional(ar, nSamplePerBlock_, isActive);
94  if (simulation().domain().isMaster()) {
95  ar << *accumulatorPtr_;
96  }
97  }
98 
99  /*
100  * Clear accumulator (do nothing on slave processors).
101  */
103  {
104  if (simulation().domain().isMaster()){
105  accumulatorPtr_->clear();
106  }
107  }
108 
109  /*
110  * Open outputfile
111  */
113  {
114  if (simulation().domain().isMaster()) {
115  if (nSamplePerBlock_) {
116  std::string filename = outputFileName(".dat");
117  simulation().fileMaster().openOutputFile(filename, outputFile_);
118  }
119  }
120  }
121 
122  /*
123  * Compute value.
124  */
126  {
127  if (!isAtInterval(iStep)) {
128  UTIL_THROW("Time step index is not a multiple of interval");
129  }
130  compute();
131  if (simulation().domain().isMaster()) {
132  Tensor data = value();
133  accumulatorPtr_->sample(data);
134  if (nSamplePerBlock_ > 0 && accumulatorPtr_->isBlockComplete()) {
135  int beginStep = iStep - (nSamplePerBlock_ - 1)*interval();
136  outputFile_ << Int(beginStep);
137  double ave;
138  int i, j;
139  for (i = 0; i < Dimension; ++i) {
140  for (j = 0; j < Dimension; ++j) {
141  ave = (*accumulatorPtr_)(i, j).blockAverage();
142  outputFile_ << " " << Dbl(ave);
143  }
144  }
145  outputFile_ << "\n";
146  }
147  }
148  }
149 
150  /*
151  * Output results to file after simulation is completed.
152  */
154  {
155  if (simulation().domain().isMaster()) {
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  #if 0
168  // Write average (*.ave) file
169  fileMaster.openOutputFile(outputFileName(".ave"), outputFile_);
170  int i, j;
171  for (i = 0; i < Dimension; ++i) {
172  for (j = 0; j < Dimension; ++j) {
173  (*accumulatorPtr_)(i, j).output(outputFile_);
174  outputFile_ << "\n";
175  }
176  }
177  outputFile_.close();
178  #endif
179 
180  // Write average (*.ave) file with averages for all elements
181  fileMaster.openOutputFile(outputFileName(".ave"), outputFile_);
182  double ave, err;
183  int i, j;
184  for (i = 0; i < Dimension; ++i) {
185  for (j = 0; j < Dimension ; ++j) {
186  ave = (*accumulatorPtr_)(i, j).average();
187  err = (*accumulatorPtr_)(i, j).blockingError();
188  outputFile_ << "Average(" << i << ", " << j << ") = ";
189  outputFile_ << Dbl(ave) << " +- " << Dbl(err, 9, 2) << "\n";
190  }
191  }
192  outputFile_.close();
193 
194  // Write average error analysis (*.aer) file
195  fileMaster.openOutputFile(outputFileName(".aer"), outputFile_);
196  for (i = 0; i < Dimension; ++i) {
197  for (j = 0; j < Dimension ; ++j) {
198  outputFile_ << "Element(" << i << ", " << j << "): \n\n";
199  (*accumulatorPtr_)(i, j).output(outputFile_);
200  outputFile_ <<
201  "----------------------------------------------------------------------------\n";
202  }
203  }
204  outputFile_.close();
205 
206  }
207  }
208 
209 }
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
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.
bool isBlockComplete() const
Is the current block average complete?
virtual void sample(long iStep)
Compute a sampled value and add update accumulator.
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
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.
virtual ~TensorAverageAnalyzer()
Destructor.
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?
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
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
TensorAverageAnalyzer(Simulation &simulation)
Constructor.
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.
void clear()
Clear all accumulators, set to empty initial state.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void compute()=0
Compute value of sampled quantity.
Calculates averages of all components of a Tensor-valued variable.
Definition: TensorAverage.h:32
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
void sample(const Tensor &value)
Add a sampled value to the ensemble.
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
Saving archive for binary istream.
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
virtual void setup()
Setup before loop - open output file.
int interval() const
Get interval value.
virtual Tensor value()=0
Get current value, set by compute function.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
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 readParameters(std::istream &in)
Read interval, outputFileName and (optionally) nSamplePerBlock.
virtual void output()
Write final average and error analysis to file.
virtual void clear()
Clear accumulator on master, do nothing on other processors.