Simpatico  v1.10
MdEnergyOutput.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 "MdEnergyOutput.h"
10 #include <util/misc/FileMaster.h>
11 #include <util/format/Dbl.h>
12 #include <util/format/Int.h>
13 
14 namespace McMd
15 {
16 
17  using namespace Util;
18 
19  /*
20  * Constructor.
21  */
22  MdEnergyOutput::MdEnergyOutput(MdSystem& system) :
23  SystemAnalyzer<MdSystem>(system)
24  { setClassName("MdEnergyOutput"); }
25 
26  /*
27  * Read interval and output file name.
28  */
29  void MdEnergyOutput::readParameters(std::istream& in)
30  {
31  readInterval(in);
33  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
34  }
35 
36  /*
37  * Load internal state from archive.
38  */
40  {
41  loadInterval(ar);
43  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
44  }
45 
46  /*
47  * Save internal state to archive.
48  */
50  { ar & *this; }
51 
52  /*
53  * Evaluate energy and print.
54  */
55  void MdEnergyOutput::sample(long iStep)
56  {
57  if (isAtInterval(iStep)) {
58  double potential = 0.0;
59  #ifndef SIMP_NOPAIR
60  double pair = system().pairPotential().energy();
61  potential += pair;
62  outputFile_ << Dbl(pair);
63  #endif
64  #ifdef SIMP_BOND
65  double bond = system().bondPotential().energy();
66  potential += bond;
67  outputFile_ << Dbl(bond);
68  #endif
69  #ifdef SIMP_ANGLE
70  if (system().hasAnglePotential()) {
71  double angle = system().anglePotential().energy();
72  potential += angle;
73  outputFile_ << Dbl(angle);
74  }
75  #endif
76  #ifdef SIMP_DIHEDRAL
77  if (system().hasDihedralPotential()) {
78  double dihedral = system().dihedralPotential().energy();
79  potential += dihedral;
80  outputFile_ << Dbl(dihedral);
81  }
82  #endif
83  #ifdef SIMP_COULOMB
84  if (system().hasCoulombPotential()) {
85  double coulombk = system().coulombPotential().energy();
86  potential += coulombk;
87  outputFile_ << Dbl(coulombk);
88  }
89  #endif
90  #ifdef SIMP_EXTERNAL
91  if (system().hasExternalPotential()) {
92  double external = system().externalPotential().energy();
93  potential += external;
94  outputFile_ << Dbl(external);
95  }
96  #endif
97  #ifdef SIMP_SPECIAL
98  if (system().hasSpecialPotential()) {
99  double special = system().specialPotential().energy();
100  potential += special;
101  outputFile_ << Dbl(special);
102  }
103  #endif
104  #ifdef MCMD_LINK
105  if (system().hasLinkPotential()) {
106  double link = system().linkPotential().energy();
107  potential += link;
108  outputFile_ << Dbl(link);
109  }
110  #endif
111  #ifdef SIMP_TETHER
112  if (system().hasTetherPotential()) {
113  double tether = system().tetherPotential().energy();
114  potential += tether;
115  outputFile_ << Dbl(tether);
116  }
117  #endif
118  outputFile_ << Dbl(potential);
119  double kinetic = system().kineticEnergy();
120  outputFile_ << Dbl(kinetic);
121  double total = potential + kinetic;
122  outputFile_ << Dbl(total) << std::endl;
123  }
124  }
125 
126  /*
127  * Summary
128  */
130  {
131  // Close *.dat file
132  outputFile_.close();
133 
134  // Open and write summary file
135  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
136  writeParam(outputFile_);
137  outputFile_ << std::endl;
138 
139  outputFile_ << "File format:" << std::endl;
140  #ifndef SIMP_NOPAIR
141  outputFile_ << " ";
142  outputFile_ << "[pair] ";
143  #endif
144  #ifdef SIMP_BOND
145  outputFile_ << "[bond] ";
146  #endif
147  #ifdef SIMP_ANGLE
148  if (system().hasAnglePotential()) {
149  outputFile_ << "[angle] ";
150  }
151  #endif
152  #ifdef SIMP_DIHEDRAL
153  if (system().hasDihedralPotential()) {
154  outputFile_ << "[dihedral] ";
155  }
156  #endif
157  #ifdef SIMP_COULOMB
158  if (system().hasCoulombPotential()) {
159  outputFile_ << "[coulomb] ";
160  }
161  #endif
162  #ifdef SIMP_EXTERNAL
163  if (system().hasExternalPotential()) {
164  outputFile_ << "[external] ";
165  }
166  #endif
167  #ifdef SIMP_SPECIAL
168  if (system().hasSpecialPotential()) {
169  outputFile_ << "[special] ";
170  }
171  #endif
172  #ifdef MCMD_LINK
173  if (system().hasLinkPotential()) {
174  outputFile_ << "[link] ";
175  }
176  #endif
177  #ifdef SIMP_TETHER
178  if (system().hasTetherPotential()) {
179  outputFile_ << "[tether] ";
180  }
181  #endif
182  outputFile_ << "[potential] ";
183  outputFile_ << "[kinetic] ";
184  outputFile_ << "[total] ";
185  outputFile_ << std::endl;
186 
187  outputFile_.close();
188  }
189 }
Include this file to include all MD potentials at once.
DihedralPotential & dihedralPotential() const
Return DihedralPotential by reference.
Definition: MdSystem.h:571
virtual double energy(double rsq, int iAtomType, int jAtomType) const =0
Return pair energy for a single pair.
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 loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
MdSystem & system()
Return reference to parent system.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
ExternalPotential & externalPotential() const
Return ExternalPotential by reference.
Definition: MdSystem.h:605
virtual void output()
Output final summary and file format.
Saving / output archive for binary ostream.
virtual double energy(const Vector &R1, const Vector &R2, const Vector &R3, int type) const =0
Returns potential energy for one dihedral.
MdPairPotential & pairPotential() const
Return MdPairPotential by reference.
Definition: MdSystem.h:520
virtual void readParameters(std::istream &in)
Read output file and nStepPerSample.
virtual double energy(const Vector &position, int i) const =0
Returns external potential energy of a single particle.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
BondPotential & linkPotential() const
Return link potential by reference.
Definition: MdSystem.h:639
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
AnglePotential & anglePotential() const
Return AnglePotential by reference.
Definition: MdSystem.h:554
double kineticEnergy() const
Compute and return total kinetic energy.
Definition: MdSystem.cpp:1003
virtual void sample(long iStep)
Evaluate energy and print.
virtual double energy(double cosTheta, int type) const =0
Returns potential energy for one angle.
virtual double energy(double rSq, int type) const =0
Returns potential energy for one bond.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
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.
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.
double energy()
Get total Coulomb energy.
BondPotential & bondPotential() const
Return BondPotential by reference.
Definition: MdSystem.h:537
MdCoulombPotential & coulombPotential() const
Return CoulombPotential by reference.
Definition: MdSystem.h:588