Simpatico  v1.10
AutoCorrAnalyzer.tpp
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 "AutoCorrAnalyzer.h"
9 #include <ddMd/simulation/Simulation.h>
10 #include <util/accumulators/AutoCorrelation.tpp>
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 DdMd
18 {
19 
20  using namespace Util;
21 
22  /*
23  * Constructor.
24  */
25  template <typename Data, typename Product>
27  : Analyzer(simulation),
28  accumulatorPtr_(0),
29  bufferCapacity_(-1),
30  maxStageId_(10),
31  isInitialized_(false)
32  { setClassName("AutoCorrAnalyzer"); }
33 
34  /*
35  * Read interval, outputFileName and bufferCapacity_.
36  */
37  template <typename Data, typename Product>
39  {
40  readInterval(in);
42  read<int>(in,"bufferCapacity", bufferCapacity_);
43  if (simulation().domain().isMaster()) {
44  accumulatorPtr_ = new AutoCorrelation<Tensor, double>;
45  accumulatorPtr_->setParam(bufferCapacity_, maxStageId_);
46  }
47  isInitialized_ = true;
48  }
49 
50 
51  /*
52  * Load internal state from an archive.
53  */
54  template <typename Data, typename Product>
56  {
57  loadInterval(ar);
59  loadParameter(ar, "bufferCapacity", bufferCapacity_);
60 
61  if (simulation().domain().isMaster()) {
62  accumulatorPtr_ = new AutoCorrelation<Tensor, double>;
63  ar >> *accumulatorPtr_;
64  }
65 
66  isInitialized_ = true;
67  }
68 
69  /*
70  * Save internal state to an archive.
71  */
72  template <typename Data, typename Product>
74  {
75  saveInterval(ar);
77  ar & bufferCapacity_;
78  if (simulation().domain().isMaster()) {
79  if (!accumulatorPtr_) {
80  UTIL_THROW("Null accumulatorPtr_ on master");
81  }
82  ar << *accumulatorPtr_;
83  }
84  }
85 
86  /*
87  * Clear accumulator.
88  */
89  template <typename Data, typename Product>
91  {
92  if (!isInitialized_) {
93  UTIL_THROW("Error: Object not initialized");
94  }
95  if (simulation().domain().isMaster()) {
96  if (!accumulatorPtr_) {
97  UTIL_THROW("Null accumulatorPtr_ on master");
98  }
99  accumulatorPtr_->clear();
100  }
101  }
102 
103  /*
104  * Setup before simulation.
105  */
106  template <typename Data, typename Product>
108  {
109  if (!isInitialized_) {
110  UTIL_THROW("Object not initialized.");
111  }
112  // clear();
113  }
114 
115  /*
116  * Sample one Data value.
117  */
118  template <typename Data, typename Product>
120  {
121  if (!isAtInterval(iStep)) {
122  UTIL_THROW("Time step index is not a multiple of interval");
123  }
124  computeData();
125  if (simulation().domain().isMaster()) {
126  if (!accumulatorPtr_) {
127  UTIL_THROW("Null accumulatorPtr_ on master");
128  }
129  Data value = data();
130  accumulatorPtr_->sample(value);
131  }
132  }
133 
134  /*
135  * Output autocorrelation function to file.
136  */
137  template <typename Data, typename Product>
139  {
140  if (simulation().domain().isMaster()) {
141  if (!accumulatorPtr_) {
142  UTIL_THROW("Null accumulatorPtr_ on master");
143  }
144 
145  simulation().fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
146  writeParam(outputFile_);
147  outputFile_ << std::endl;
148  outputFile_ << "bufferCapacity " << accumulatorPtr_->bufferCapacity() << std::endl;
149  outputFile_ << "nSample " << accumulatorPtr_->nSample() << std::endl;
150  outputFile_ << std::endl;
151  outputFile_ << "Format of *.dat file" << std::endl;
152  outputFile_ << "[int time delay (samples)] [double autocorrelation function]"
153  << std::endl;
154  outputFile_ << std::endl;
155  outputFile_.close();
156 
157  // Write xy autocorrelation function to data file
158  simulation().fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
159  accumulatorPtr_->output(outputFile_);
160  outputFile_.close();
161  }
162  }
163 
164 }
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.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
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
void readInterval(std::istream &in)
Read parameter interval from file.
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
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.
virtual void sample(long iStep)
Compute new Data value and update accumulator.
#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 input archive.
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
virtual void save(Serializable::OArchive &ar)
Save internal state to an output archive.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
const std::string & outputFileName() const
Return outputFileName string.
virtual Data data()=0
Get current Data value, call only on master.
bool isMaster() const
Is this the master processor (gridRank == 0) ?
Definition: Domain.h:313
virtual void computeData()
Compute Data value, call on all processors.
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.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
virtual void clear()
Clear nSample counter.
Domain & domain()
Get the Domain by reference.
virtual void setup()
Setup accumulator.
virtual void readParameters(std::istream &in)
Read interval, outputFileName and bufferCapacity from parameter file.
AutoCorrAnalyzer(Simulation &simulation)
Constructor.