Simpatico  v1.10
OutputEnergy.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 "OutputEnergy.h"
9 #include <ddMd/potentials/pair/PairPotential.h>
10 #ifdef SIMP_BOND
11 #include <ddMd/potentials/bond/BondPotential.h>
12 #endif
13 #ifdef SIMP_ANGLE
14 #include <ddMd/potentials/angle/AnglePotential.h>
15 #endif
16 #ifdef SIMP_DIHEDRAL
17 #include <ddMd/potentials/dihedral/DihedralPotential.h>
18 #endif
19 #ifdef SIMP_EXTERNAL
20 #include <ddMd/potentials/external/ExternalPotential.h>
21 #endif
22 #include <util/format/Int.h>
23 #include <util/format/Dbl.h>
24 #include <util/mpi/MpiLoader.h>
25 #include <util/misc/ioUtil.h>
26 
27 #include <sstream>
28 
29 namespace DdMd
30 {
31 
32  using namespace Util;
33 
34  /*
35  * Constructor.
36  */
38  : Analyzer(simulation),
39  nSample_(0),
40  isInitialized_(false)
41  { setClassName("OutputEnergy"); }
42 
43  /*
44  * Read interval and outputFileName.
45  */
46  void OutputEnergy::readParameters(std::istream& in)
47  {
48  readInterval(in);
50  #if 0
51  std::string filename = outputFileName();
52  simulation().fileMaster().openOutputFile(filename, outputFile_);
53  #endif
54  isInitialized_ = true;
55  }
56 
57  /*
58  * Load internal state from an archive.
59  */
61  {
62  loadInterval(ar);
64  MpiLoader<Serializable::IArchive> loader(*this, ar);
65  loader.load(nSample_);
66  #if 0
67  std::string filename = outputFileName();
68  simulation().fileMaster().openOutputFile(filename, outputFile_);
69  #endif
70  isInitialized_ = true;
71  }
72 
73  /*
74  * Save internal state to an archive.
75  */
77  {
78  saveInterval(ar);
80  ar << nSample_;
81  }
82 
83  /*
84  * Reset nSample.
85  */
87  { nSample_ = 0; }
88 
89  /*
90  * Open outputfile
91  */
93  {
94  if (simulation().domain().isMaster()) {
95  std::string filename;
96  filename = outputFileName();
97  simulation().fileMaster().openOutputFile(filename, outputFile_);
98  }
99  }
100 
101  /*
102  * Output energy to file
103  */
104  void OutputEnergy::sample(long iStep)
105  {
106  if (isAtInterval(iStep)) {
107  Simulation& sim = simulation();
108  sim.computeKineticEnergy();
110  if (sim.domain().isMaster()) {
111  double kinetic = sim.kineticEnergy();
112  outputFile_ << Int(iStep, 10)
113  << Dbl(kinetic, 15);
114  double potential = 0.0;
115  double pair = sim.pairPotential().energy();
116  potential += pair;
117  outputFile_ << Dbl(pair, 15);
118  #ifdef SIMP_BOND
119  if (sim.nBondType()) {
120  double bond = sim.bondPotential().energy();
121  potential += bond;
122  outputFile_ << Dbl(bond, 15);
123  }
124  #endif
125  #ifdef SIMP_ANGLE
126  if (sim.nAngleType()) {
127  double angle = sim.anglePotential().energy();
128  potential += angle;
129  outputFile_ << Dbl(angle, 15);
130  }
131  #endif
132  #ifdef SIMP_DIHEDRAL
133  if (sim.nDihedralType()) {
134  double dihedral = sim.dihedralPotential().energy();
135  potential += dihedral;
136  outputFile_ << Dbl(dihedral, 15);
137  }
138  #endif
139  #ifdef SIMP_EXTERNAL
140  if (sim.hasExternal()) {
141  double external = sim.externalPotential().energy();
142  potential += external;
143  outputFile_ << Dbl(external, 15);
144  }
145  #endif
146  outputFile_ << Dbl(kinetic + potential, 20)
147  << std::endl;
148  }
149  ++nSample_;
150  }
151  }
152 
153 }
virtual void readParameters(std::istream &in)
Read dumpPrefix and interval.
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
virtual void sample(long iStep)
Write energy to file.
const BondPotential & bondPotential() const
Get the PairPotential by const reference.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
double kineticEnergy()
Return precomputed total kinetic energy.
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.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
OutputEnergy(Simulation &simulation)
Constructor.
int nBondType()
Get maximum number of bond types.
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
const ExternalPotential & externalPotential() const
Get the ExternalPotential by reference.
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
int nDihedralType()
Get maximum number of dihedral types.
FileMaster & fileMaster()
Get the associated FileMaster by reference.
const DihedralPotential & dihedralPotential() const
Get the DihedralPotential by const reference.
void computePotentialEnergies()
Calculate and store total potential energy on all processors.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void load(Data &value)
Load and broadcast a single Data value.
Definition: MpiLoader.h:137
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
virtual void clear()
Clear nSample counter.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
const std::string & outputFileName() const
Return outputFileName string.
virtual void setup()
Setup - open output file.
bool isMaster() const
Is this the master processor (gridRank == 0) ?
Definition: Domain.h:313
const PairPotential & pairPotential() const
Get the PairPotential by const reference.
double energy() const
Return the total potential, from all processors.
Definition: Potential.cpp:39
Saving archive for binary istream.
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
const AnglePotential & anglePotential() const
Get the AnglePotential by const reference.
int nAngleType()
Get maximum number of angle types.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
void computeKineticEnergy()
Compute total kinetic energy.
Domain & domain()
Get the Domain by reference.
bool hasExternal()
Does this simulation have an external potential?