Simpatico  v1.10
HarmonicL0Bond.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 <util/global.h>
9 #include "HarmonicL0Bond.h"
10 #include <util/random/Random.h>
11 
12 namespace Simp
13 {
14 
15  using namespace Util;
16 
17  /*
18  * Constructor.
19  */
21  : nBondType_(0)
22  {
23  setClassName("HarmonicL0Bond");
24  for (int i = 0; i < MaxNBondType; ++i) {
25  kappa_[i] = 0.0;
26  }
27  }
28 
29  /*
30  * Copy constructor.
31  */
33  : nBondType_(other.nBondType_)
34  {
35  for (int i = 0; i < nBondType_; ++i) {
36  kappa_[i] = other.kappa_[i];
37  }
38  }
39 
40  /*
41  * Assignment.
42  */
44  {
45  nBondType_ = other.nBondType_;
46  for (int i = 0; i < nBondType_; ++i) {
47  kappa_[i] = other.kappa_[i];
48  }
49  return *this;
50  }
51 
52  /*
53  * Set the nBondType_ member
54  */
55  void HarmonicL0Bond::setNBondType(int nBondType)
56  {
57  if (nBondType > MaxNBondType) {
58  UTIL_THROW("nBondType > HarmonicL0Bond::MaxNBondType");
59  }
60  nBondType_ = nBondType;
61  }
62 
63  /*
64  * Read bond interaction parameters kappa and length from file
65  */
66  void HarmonicL0Bond::readParameters(std::istream &in)
67  {
68  UTIL_CHECK(nBondType_ > 0);
69  readCArray<double>(in, "kappa", kappa_, nBondType_);
70  }
71 
72  /*
73  * Load internal state from an archive.
74  */
76  {
77  UTIL_CHECK(nBondType_ > 0);
78  loadCArray<double> (ar, "kappa", kappa_, nBondType_);
79  }
80 
81  /*
82  * Save internal state to an archive.
83  */
85  {
86  UTIL_CHECK(nBondType_ > 0);
87  ar.pack(kappa_, nBondType_);
88  }
89 
90  /*
91  * Generate a random bond length chosen from an equilibrium distribution for
92  * randomly oriented bonds.
93  *
94  * Algorithm: Generate random bond vector and take the sqrt.
95  */
97  Random *random, double beta, int type) const
98  {
99  double x, y, z;
100  x = random->gaussian();
101  y = random->gaussian();
102  z = random->gaussian();
103  return sqrt(x*x + y*y + z*z)/sqrt(beta*kappa_[type]);
104  }
105 
106  /*
107  * Modify a parameter, identified by a string.
108  */
109  void HarmonicL0Bond::set(std::string name, int type, double value)
110  {
111  if (name == "kappa") {
112  kappa_[type] = value;
113  } else {
114  UTIL_THROW("Unrecognized parameter name");
115  }
116  }
117 
118  /*
119  * Get a parameter value, identified by a string.
120  */
121  double HarmonicL0Bond::get(std::string name, int type) const
122  {
123  double value = 0.0;
124  if (name == "kappa") {
125  value = kappa_[type];
126  } else {
127  UTIL_THROW("Unrecognized parameter name");
128  }
129  return value;
130  }
131 
132 }
void set(std::string name, int type, double value)
Modify a parameter, identified by a string.
double get(std::string name, int type) const
Get a parameter value, identified by a string.
void setNBondType(int nBondType)
Set the number of bond types.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
HarmonicL0Bond & operator=(const HarmonicL0Bond &other)
Assignment.
Saving / output archive for binary ostream.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
A harmonic bond potential with zero rest length.
void pack(const T &data)
Pack one object of type T.
Utility classes for scientific computation.
Definition: accumulators.mod:1
double gaussian(void)
Return a Gaussian random number with zero average and unit variance.
Definition: Random.cpp:92
HarmonicL0Bond()
Constructor.
double randomBondLength(Random *random, double beta, int type) const
Return bond length chosen from equilibrium distribution.
Saving archive for binary istream.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
void setClassName(const char *className)
Set class name string.
Random number generator.
Definition: Random.h:46
void readParameters(std::istream &in)
Read bond interaction parameters from input stream.