Simpatico  v1.10
EnergyEnsemble.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
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 "EnergyEnsemble.h"
9 #ifdef UTIL_MPI
10 #include <util/mpi/MpiStructBuilder.h>
11 #endif
12 
13 #include <iostream>
14 
15 namespace Simp{
16 
17  using namespace Util;
18 
19  /*
20  * Constructor.
21  */
23  : temperature_(1.0),
24  beta_(1.0),
25  type_(type)
26  { setClassName("EnergyEnsemble"); }
27 
28  /*
29  * Destructor.
30  */
32  {}
33 
34  /*
35  * Set the temperature.
36  */
38  {
39  if (!isIsothermal()) {
40  UTIL_THROW("Must be an isothermal ensemble");
41  }
42  temperature_ = temperature;
43  beta_ = 1.0/temperature;
44  }
45 
46  /*
47  * Read the type and (if necessary) temperature from file.
48  */
49  void EnergyEnsemble::readParameters(std::istream& in)
50  {
51  read<Type>(in, "type", type_);
52  if (isIsothermal()) {
53  read<double>(in, "temperature", temperature_);
54  beta_ = 1.0/temperature_;
55  }
56  }
57 
58  /*
59  * Load internal state from an archive.
60  */
62  {
63  loadParameter<Type>(ar, "type", type_);
64  if (isIsothermal()) {
65  loadParameter<double>(ar, "temperature", temperature_);
66  ar >> beta_;
67  }
68  }
69 
70  /*
71  * Save internal state to an archive.
72  */
74  {
75  ar << type_;
76  if (isIsothermal()) {
77  ar << temperature_;
78  ar << beta_;
79  }
80  }
81 
82  /*
83  * Extract an EnergyEnsemble::Type from an istream as a string.
84  */
85  std::istream& operator >> (std::istream& in, EnergyEnsemble::Type &type)
86  {
87  std::string buffer;
88  in >> buffer;
89  if (buffer == "ADIABATIC" || buffer == "adiabatic") {
90  type = EnergyEnsemble::ADIABATIC;
91  } else
92  if (buffer == "ISOTHERMAL" || buffer == "isothermal") {
93  type = EnergyEnsemble::ISOTHERMAL;
94  } else {
95  UTIL_THROW("Invalid EnergyEnsemble::Type value input");
96  }
97  return in;
98  }
99 
100  /*
101  * Insert a EnergyEnsemble::Type to an ostream as a string.
102  */
103  std::ostream& operator<<(std::ostream& out, const EnergyEnsemble::Type &type)
104  {
105  if (type == EnergyEnsemble::ADIABATIC) {
106  out << "adiabatic";
107  } else
108  if (type == EnergyEnsemble::ISOTHERMAL) {
109  out << "isothermal";
110  }
111  return out;
112  }
113 
114 
115  #ifdef UTIL_MPI
116  /*
117  * Commit MPI Datatype.
118  */
120  {
121  MpiStructBuilder builder;
122  EnergyEnsemble object;
123 
124  builder.setBase(&object);
125  builder.addMember(&object.temperature_, MPI::DOUBLE);
126  builder.addMember(&object.beta_, MPI::DOUBLE);
127  builder.addMember(&object.type_, MPI::INT);
130  }
131  #endif
132 
133 }
134 #ifdef UTIL_MPI
135 namespace Util
136 {
137 
138  /*
139  * Initialize Simp::EnergyEnsemble MPI Datatype.
140  */
141  MPI::Datatype MpiTraits<Simp::EnergyEnsemble>::type = MPI::BYTE;
143 
144  /*
145  * Initialize Simp::EnergyEnsemble::Type MPI Datatype.
146  */
147  MPI::Datatype MpiTraits<Simp::EnergyEnsemble::Type>::type = MPI::INT;
149 
150 }
151 #endif
void addMember(void *memberAddress, MPI::Datatype type, int count=1)
Add a new member variable to the type map.
Classes used by all simpatico molecular simulations.
std::istream & operator>>(std::istream &in, MonoclinicBoundary &boundary)
istream extractor for a MonoclinicBoundary.
void setBase(void *objectAddress)
Set address of an class instance.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void commit(MPI::Datatype &newType)
Build and commit a user-defined MPI Struct datatype.
Saving / output archive for binary ostream.
A statistical ensemble for energy.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
bool isIsothermal() const
Is this an Isothermal ensemble?
Utility classes for scientific computation.
Definition: accumulators.mod:1
~EnergyEnsemble()
Destructor.
Default MpiTraits class.
Definition: MpiTraits.h:39
std::ostream & operator<<(std::ostream &out, const MonoclinicBoundary &boundary)
ostream inserter for an MonoclinicBoundary.
void setTemperature(double temperature)
Set the temperature.
A MpiStructBuilder objects is used to create an MPI Struct datatype.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Type
Enumeration of the allowed types of EnergyEnsemble.
EnergyEnsemble(Type type=UNKNOWN)
Constructor.
Saving archive for binary istream.
void setClassName(const char *className)
Set class name string.
virtual void readParameters(std::istream &in)
Read the type and (if necessary) temperature from file.
double temperature() const
Return the temperature.
static void commitMpiType()
Commit MPI data type for an EnergyEnsemble.