Simpatico  v1.10
MdPotentialEnergyAverage.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 "MdPotentialEnergyAverage.h"
9 #include <util/misc/FileMaster.h>
10 #include <util/archives/Serializable_includes.h>
11 
12 #include <cstdio>
13 
14 namespace McMd
15 {
16 
17  using namespace Util;
18 
19  /*
20  * Constructor.
21  */
23  : SystemAnalyzer<MdSystem>(system),
24  outputFile_(),
25  accumulator_(),
26  nSamplePerBlock_(1)
27  { setClassName("MdPotentialEnergyAverage"); }
28 
29  /*
30  * Read parameters and initialize.
31  */
33  {
34  readInterval(in);
36  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
37  accumulator_.setNSamplePerBlock(nSamplePerBlock_);
38 
39  // If nSamplePerBlock != 0, open an output file for block averages.
40  if (accumulator_.nSamplePerBlock()) {
41  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
42  }
43  isInitialized_ = true;
44  }
45 
46  /*
47  * Load state from an archive.
48  */
50  {
51  loadInterval(ar);
53  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
54  ar & accumulator_;
55 
56  // If nSamplePerBlock != 0, open an output file for block averages.
57  if (accumulator_.nSamplePerBlock()) {
58  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
59  }
60  isInitialized_ = true;
61  }
62 
63  /*
64  * Save state to archive.
65  */
67  { ar & *this; }
68 
69 
70  /*
71  * Clear accumulator.
72  */
74  { accumulator_.clear(); }
75 
76  /*
77  * Evaluate energy, and add to accumulator.
78  */
80  {
81  if (isAtInterval(iStep)) {
82  accumulator_.sample(system().potentialEnergy(), outputFile_);
83  }
84  }
85 
86  /*
87  * Output results to file after simulation is completed.
88  */
90  {
91  // If outputFile_ was used to write block averages, close it.
92  if (accumulator_.nSamplePerBlock()) {
93  outputFile_.close();
94  }
95 
96  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
97  writeParam(outputFile_);
98  outputFile_.close();
99 
100  fileMaster().openOutputFile(outputFileName(".ave"), outputFile_);
101  accumulator_.output(outputFile_);
102  outputFile_.close();
103 
104  }
105 
106 }
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
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
MdSystem & system()
Return reference to parent system.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
Saving / output archive for binary ostream.
virtual void readParameters(std::istream &in)
Read parameters and initialize.
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
virtual void output()
Output results at end of simulation.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
virtual void setup()
Clear accumulator.
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
Definition: Average.cpp:178
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
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
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSamplePerBlock() const
Get number of samples per block average.
Definition: Average.h:220
void setClassName(const char *className)
Set class name string.
FileMaster & fileMaster()
Get the FileMaster by reference.
void loadInterval(Serializable::IArchive &ar)
Load interval from archive, with error checking.
A System for Molecular Dynamics simulation.
Definition: MdSystem.h:68
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name from archive.
MdPotentialEnergyAverage(MdSystem &system)
Constructor.
virtual void sample(long iStep)
Calculate, analyze and/or output a physical quantity.