Simpatico  v1.10
MdKineticEnergyAverage.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 "MdKineticEnergyAverage.h" // class header
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("MdKineticEnergyAverage"); }
28 
29  /*
30  * Read parameters and initialize.
31  */
33  {
34  readInterval(in);
36  read<int>(in, "nSamplePerBlock", nSamplePerBlock_);
37 
38  accumulator_.setNSamplePerBlock(nSamplePerBlock_);
39 
40  // If nSamplePerBlock != 0, open an output file for block averages.
41  if (accumulator_.nSamplePerBlock()) {
42  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
43  }
44  isInitialized_ = true;
45  }
46 
47  /*
48  * Load internal state from archive.
49  */
51  {
52  loadInterval(ar);
54  loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_);
55  ar & accumulator_;
56 
57  // If nSamplePerBlock != 0, open an output file for block averages.
58  if (accumulator_.nSamplePerBlock()) {
59  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
60  }
61  isInitialized_ = true;
62  }
63 
64  /*
65  * Save internal state to archive.
66  */
68  { ar & *this; }
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().kineticEnergy(), 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
virtual void sample(long iStep)
Calculate, analyze and/or output a physical quantity.
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.
virtual void output()
Output results at end of simulation.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Saving / output archive for binary ostream.
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
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
Definition: Average.cpp:178
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Definition: Average.cpp:63
virtual void readParameters(std::istream &in)
Read parameters and initialize.
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).
MdKineticEnergyAverage(MdSystem &system)
Constructor.
int nSamplePerBlock() const
Get number of samples per block average.
Definition: Average.h:220
virtual void setup()
Clear accumulator.
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.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.