Simpatico  v1.10
SpeciesEnsemble.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 "SpeciesEnsemble.h"
9 #ifdef UTIL_MPI
10 #include <util/mpi/MpiStructBuilder.h>
11 #endif
12 
13 namespace Simp
14 {
15 
16  using namespace Util;
17 
18  /*
19  * Constructor.
20  */
22  : mu_(1.0),
23  type_(type)
24  {}
25 
26  /*
27  * Destructor.
28  */
30  {}
31 
32  /*
33  * Set the chemical potential mu.
34  */
36  {
37  if (!isGrand()) {
38  UTIL_THROW("Must be a grand-canonical ensemble");
39  }
40  mu_ = mu;
41  }
42 
43  /*
44  * Read the type and (if necessary) chemical potential mu from file.
45  */
46  void SpeciesEnsemble::readParam(std::istream& in)
47  {
48  readBegin(in, "SpeciesEnsemble");
49  read<Type>(in, "type", type_);
50  if (isGrand()) {
51  read<double>(in, "mu", mu_);
52  }
53  readEnd(in);
54  }
55 
56  /*
57  * Extract an SpeciesEnsemble::Type from an istream as a string.
58  */
59  std::istream& operator>>(std::istream& in, SpeciesEnsemble::Type &type)
60  {
61  std::string buffer;
62  in >> buffer;
63  if (buffer == "CLOSED" || buffer == "closed") {
64  type = SpeciesEnsemble::CLOSED;
65  } else
66  if (buffer == "GRAND" || buffer == "grand") {
67  type = SpeciesEnsemble::GRAND;
68  } else {
69  UTIL_THROW("Invalid SpeciesEnsemble::Type value input");
70  }
71  return in;
72  }
73 
74  /*
75  * Insert a SpeciesEnsemble::Type to an ostream as a string.
76  */
77  std::ostream& operator<<(std::ostream& out, const SpeciesEnsemble::Type &type)
78  {
79  if (type == SpeciesEnsemble::CLOSED) {
80  out << "closed";
81  } else
82  if (type == SpeciesEnsemble::GRAND) {
83  out << "grand";
84  }
85  return out;
86  }
87 
88  #ifdef UTIL_MPI
89  /*
90  * Commit MPI Datatype.
91  */
93  {
94  MpiStructBuilder builder;
95  SpeciesEnsemble object;
96 
97  builder.setBase(&object);
98  builder.addMember(&object.mu_, MPI::DOUBLE);
99  builder.addMember(&object.type_, MPI::INT);
102  }
103  #endif
104 
105 }
106 
107 #ifdef UTIL_MPI
108 namespace Util
109 {
110 
111  /*
112  * Initialize MPI Datatype.
113  */
114  MPI::Datatype MpiTraits<Simp::SpeciesEnsemble>::type = MPI::BYTE;
116 
117  /*
118  * Initialize MPI Datatype.
119  */
120  MPI::Datatype MpiTraits<Simp::SpeciesEnsemble::Type>::type = MPI::INT;
122 
123 }
124 #endif
125 
void addMember(void *memberAddress, MPI::Datatype type, int count=1)
Add a new member variable to the type map.
An ensemble for the number of molecules of one Species.
double mu() const
Return the chemical potential mu.
bool isGrand() const
Is this a Grand ensemble?
End & readEnd(std::istream &in)
Add and read the closing bracket.
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.
void commit(MPI::Datatype &newType)
Build and commit a user-defined MPI Struct datatype.
SpeciesEnsemble(Type type=UNKNOWN)
Constructor.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void setMu(double mu)
Set the chemical potential mu.
static void commitMpiType()
Commit associated MPI DataType.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readParam(std::istream &in)
Read the type and (if appropriate) mu from file.
Default MpiTraits class.
Definition: MpiTraits.h:39
std::ostream & operator<<(std::ostream &out, const MonoclinicBoundary &boundary)
ostream inserter for an MonoclinicBoundary.
Type
Enumeration of the allowed types of SpeciesEnsemble.
~SpeciesEnsemble()
Destructor.
A MpiStructBuilder objects is used to create an MPI Struct datatype.
Begin & readBegin(std::istream &in, const char *label, bool isRequired=true)
Add and read a class label and opening bracket.