Simpatico  v1.10
mcMd/chemistry/AtomType.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 "AtomType.h"
9 #include <util/param/Parameter.h>
10 
11 namespace McMd
12 {
13 
14  using namespace Util;
15 
16  // Constructor
18  : mass_(1.0),
19  #ifdef SIMP_COULOMB
20  charge_(0.0),
21  #endif
22  name_(),
23  id_(-1)
24  #ifdef SIMP_COULOMB
25  , hasCharge_(0)
26  #endif
27  {}
28 
29  // Destructor
31  {}
32 
33  // Set the index.
34  void AtomType::setId(int id)
35  { id_ = id; }
36 
37  /*
38  * Set the name string.
39  */
40  void AtomType::setName(std::string name)
41  { name_ = name; }
42 
43  /*
44  * Set the mass.
45  */
46  void AtomType::setMass(double mass)
47  { mass_ = mass; }
48 
49  #ifdef SIMP_COULOMB
50  /*
51  * Set the hasCharge property.
52  */
54  { hasCharge_ = hasCharge; }
55 
56  /*
57  * Set the electrical charge.
58  */
60  {
61  UTIL_CHECK(hasCharge_);
62  charge_ = charge;
63  }
64  #endif
65 
66  /*
67  * Input a AtomType from an istream, without line breaks.
68  */
69  std::istream& operator>>(std::istream& in, AtomType &atomType)
70  {
71  in >> atomType.name_;
72  in >> atomType.mass_;
73  #ifdef SIMP_COULOMB
74  if (atomType.hasCharge_) {
75  in >> atomType.charge_;
76  }
77  #endif
78  return in;
79  }
80 
81  /*
82  * Output a AtomType to an ostream, without line breaks.
83  */
84  std::ostream& operator<<(std::ostream& out, const AtomType &atomType)
85  {
86  out.width(Parameter::Width);
87  out << atomType.name_;
88  out.setf(std::ios::scientific);
89  out.width(Parameter::Width);
90  out.precision(Parameter::Precision);
91  out << atomType.mass_;
92  #ifdef SIMP_COULOMB
93  if (atomType.hasCharge_) {
94  out.width(Parameter::Width);
95  out << atomType.charge_;
96  }
97  #endif
98  return out;
99  }
100 
101 }
102 
103 #ifdef UTIL_MPI
104 namespace Util
105 {
106 
107  template <>
108  void send<McMd::AtomType>(MPI::Comm& comm, McMd::AtomType& data, int dest, int tag)
109  {
110  std::string name = data.name();
111  send<std::string>(comm, name, dest, tag);
112 
113  double mass = data.mass();
114  send<double>(comm, mass, dest, tag);
115 
116  #ifdef SIMP_COULOMB
117  bool hasCharge = data.hasCharge();
118  send<bool>(comm, hasCharge, dest, tag);
119  if (hasCharge) {
120  double charge = data.charge();
121  send<double>(comm, charge, dest, tag);
122  }
123  #endif
124  }
125 
126  template <>
127  void recv<McMd::AtomType>(MPI::Comm& comm, McMd::AtomType& data, int source, int tag)
128  {
129  std::string name;
130  recv<std::string>(comm, name, source, tag);
131  data.setName(name);
132 
133  double mass;
134  recv<double>(comm, mass, source, tag);
135  data.setMass(mass);
136 
137  #ifdef SIMP_COULOMB
138  bool hasCharge;
139  recv<bool>(comm, hasCharge, source, tag);
140  data.setHasCharge(hasCharge);
141  if (hasCharge) {
142  double charge;
143  recv<double>(comm, charge, source, tag);
144  data.setCharge(charge);
145  }
146  #endif
147  }
148 
149  template <>
150  void bcast<McMd::AtomType>(MPI::Intracomm& comm, McMd::AtomType& data, int root)
151  {
152  std::string name;
153  double mass;
154  #ifdef SIMP_COULOMB
155  double charge;
156  bool hasCharge;
157  #endif
158  int rank = comm.Get_rank();
159  if (rank == root) {
160  name = data.name();
161  mass = data.mass();
162  #ifdef SIMP_COULOMB
163  hasCharge = data.hasCharge();
164  if (hasCharge) {
165  charge = data.charge();
166  }
167  #endif
168  }
169  bcast<std::string>(comm, name, root);
170  bcast<double>(comm, mass, root);
171  #ifdef SIMP_COULOMB
172  bcast<bool>(comm, hasCharge, root);
173  if (hasCharge) {
174  bcast<double>(comm, charge, root);
175  }
176  #endif
177  if (rank != root) {
178  data.setName(name);
179  data.setMass(mass);
180  #ifdef SIMP_COULOMB
181  data.setHasCharge(hasCharge);
182  if (hasCharge) {
183  data.setCharge(charge);
184  }
185  #endif
186  }
187  }
188 
192  MPI::Datatype MpiTraits<McMd::AtomType>::type = MPI::BYTE;
194 
195 }
196 #endif // ifdef UTIL_MPI
void setId(int Id)
Set the type index.
void setMass(double mass)
Set the mass.
void setHasCharge(bool hasCharge)
Set the boolean "hasCharge" property.
virtual ~AtomType()
Destructor.
void setName(std::string name)
Set the name string.
friend std::ostream & operator<<(std::ostream &out, const AtomType &atomType)
ostream inserter (<<) for an AtomType.
Descriptor for a type of Atom.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Default MpiTraits class.
Definition: MpiTraits.h:39
int id() const
Get the index.
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
double mass() const
Get the mass.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
double charge() const
Get the electrical charge value.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
const std::string & name() const
Get the name string.
bool hasCharge() const
Does this type have a charge value?
friend std::istream & operator>>(std::istream &in, AtomType &atomType)
istream extractor (>>) for an AtomType.
void setCharge(double charge)
Set the charge value.