Simpatico  v1.10
mcMd/analyzers/util/AverageAnalyzer.h
1 #ifndef MCMD_AVERAGE_ANALYZER_H
2 #define MCMD_AVERAGE_ANALYZER_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <mcMd/analyzers/SystemAnalyzer.h> // base class template
12 #include <util/accumulators/Average.h> // member
13 #include <util/misc/FileMaster.h>
14 
15 namespace McMd
16 {
17 
18  using namespace Util;
19 
25  template <class SystemType>
26  class AverageAnalyzer : public SystemAnalyzer<SystemType>
27  {
28 
29  public:
30 
34  AverageAnalyzer(SystemType& system);
35 
39  ~AverageAnalyzer();
40 
44  virtual void readParameters(std::istream& in);
45 
51  virtual void loadParameters(Serializable::IArchive& ar);
52 
58  virtual void save(Serializable::OArchive& ar);
59 
66  template <class Archive>
67  void serialize(Archive& ar, const unsigned int version);
68 
72  virtual void setup();
73 
77  virtual void output();
78 
79  protected:
80 
82  std::ofstream outputFile_;
83 
86 
89 
90  };
91 
92  /*
93  * Constructor.
94  */
95  template <class SystemType>
97  : SystemAnalyzer<SystemType>(system),
98  outputFile_(),
99  accumulator_(),
100  nSamplePerBlock_(1)
101  {}
102 
103  /*
104  * Constructor.
105  */
106  template <class SystemType>
108  {}
109 
110  /*
111  * Read parameters and initialize.
112  */
113  template <class SystemType>
115  {
116 
119  ParamComposite::read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
120 
122 
123  // If nSamplePerBlock != 0, open an output file for block averages.
127  }
128 
129  }
130 
131  /*
132  * Load state from an archive.
133  */
134  template <class SystemType>
137  {
139  ParamComposite::loadParameter<int>(ar, "nSamplePerBlock",
141  ar & accumulator_;
142 
143  if (nSamplePerBlock_ != accumulator_.nSamplePerBlock()) {
144  UTIL_THROW("Inconsistent values of nSamplePerBlock_");
145  }
146 
147  // If nSamplePerBlock != 0, open an output file for block averages.
148  if (accumulator_.nSamplePerBlock()) {
151  }
152 
153  }
154 
155  /*
156  * Save state to an archive.
157  */
158  template <class SystemType>
160  { ar & *this; }
161 
162 
163  /*
164  * Serialize to/from an archive.
165  */
166  template <class SystemType>
167  template <class Archive>
169  const unsigned int version)
170  {
171  Analyzer::serialize(ar, version);
172  ar & nSamplePerBlock_;
173  ar & accumulator_;
174  }
175 
176  /*
177  * Clear accumulators.
178  */
179  template <class SystemType>
181  { accumulator_.clear(); }
182 
183  /*
184  * Output results to file after simulation is completed.
185  */
186  template <class SystemType>
188  {
189  // If outputFile_ was used to write block averages, close it.
191  outputFile_.close();
192  }
193 
194  // Write parameter block to *.prm file
196  outputFile_);
198  outputFile_.close();
199 
200  // Write average value to *.avefile
202  outputFile_);
204  outputFile_.close();
205 
206  }
207 
208 }
209 #endif
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
AverageAnalyzer(SystemType &system)
Constructor.
std::ofstream outputFile_
Output file stream.
Calculates the average and variance of a sampled property.
Definition: Average.h:43
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 output()
Output results at end of simulation.
virtual void loadParameters(Serializable::IArchive &ar)
Load parameters from archive.
int nSamplePerBlock_
Number of samples per block average output.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void serialize(Archive &ar, PairSelector &selector, const unsigned int version)
Serialize a PairSelector.
Definition: PairSelector.h:167
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
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Average accumulator_
Average object - statistical accumulator.
AverageAnalyzer averages of total potential energy.
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
Definition: Average.cpp:178
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Definition: Average.cpp:63
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
int nSamplePerBlock() const
Get number of samples per block average.
Definition: Average.h:220
virtual void readParameters(std::istream &in)
Read parameters and initialize.
FileMaster & fileMaster()
Get the FileMaster by reference.
const std::string & outputFileName() const
Return outputFileName string.
virtual void setup()
Clear accumulators.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.