Simpatico  v1.10
HarmonicAngle.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 "HarmonicAngle.h"
9 #include <util/math/Constants.h>
10 #include <util/random/Random.h>
11 
12 
13 namespace Simp
14 {
15 
16  using namespace Util;
17 
18  /*
19  * Constructor.
20  */
22  : nAngleType_(0)
23  {
24  setClassName("HarmonicAngle");
25  for (int i = 0; i < MaxNAngleType; ++i) {
26  kappa_[i] = 0.0;
27  theta0_[i] = 0.0;
28  }
29  }
30 
31  /*
32  * Copy constructor.
33  */
35  : nAngleType_(other.nAngleType_)
36  {
37  for (int i = 0; i < MaxNAngleType; ++i) {
38  kappa_[i] = other.kappa_[i];
39  theta0_[i] = other.theta0_[i];
40  }
41  }
42 
43  /*
44  * Assignment.
45  */
47  {
48  nAngleType_ = other.nAngleType_;
49  for (int i = 0; i < MaxNAngleType; ++i) {
50  kappa_[i] = other.kappa_[i];
51  theta0_[i] = other.theta0_[i];
52  }
53  return *this;
54  }
55 
56  /*
57  * Set the nAngleType_ member.
58  */
59  void HarmonicAngle::setNAngleType(int nAngleType)
60  {
61  if (nAngleType > MaxNAngleType) {
62  UTIL_THROW("nAngleType > HarmonicAngle::MaxNAngleType");
63  }
64  nAngleType_ = nAngleType;
65  }
66 
67  /*
68  * Read bend interaction parameters kappa from file.
69  */
70  void HarmonicAngle::readParameters(std::istream &in)
71  {
72  // Precondition
73  UTIL_CHECK(nAngleType_ > 0);
74 
75  // Read parameters
76  readCArray<double>(in, "kappa", kappa_, nAngleType_);
77  readCArray<double>(in, "theta0", theta0_, nAngleType_);
78 
79  // Convert from degrees to radians.
80  for (int i = 0; i < nAngleType_; ++i) {
81  theta0_[i] = theta0_[i] * Constants::Pi / 180.0;
82  }
83  }
84 
85  /*
86  * Load internal state from an archive.
87  */
89  {
90  UTIL_CHECK(nAngleType_ > 0);
91  loadCArray<double> (ar, "kappa", kappa_, nAngleType_);
92  loadCArray<double>(ar, "theta0", theta0_, nAngleType_);
93  }
94 
95  /*
96  * Save internal state to an archive.
97  */
99  {
100  UTIL_CHECK(nAngleType_ > 0);
101  ar.pack(kappa_, nAngleType_);
102  ar.pack(theta0_, nAngleType_);
103  }
104 
105  /*
106  * Generate a random bond angle chosen from an equilibrium distribution for
107  * randomly oriented bonds.
108  *
109  * Algorithm: Generate random bond vector and take the angle.
110  */
112  Random *random, double beta, int type) const
113  {
114  double Theta, Theta_min;
115  double U, U_min;
116  bool chosen = false;
117 
118  Theta_min = theta0_[type];
119  U_min = energy(cos(Theta_min), type);
120 
121  while (chosen == false) {
122  Theta = random->uniform(0, Constants::Pi);
123  U = energy(cos(Theta), type);
124  if (random->uniform() < exp(-beta*(U-U_min))*cos(Theta)/cos(Theta_min))
125  chosen = true;
126  }
127 
128  return Theta;
129  }
130 
131  /*
132  * Generate a random bond angle cosine chosen from an equilibrium distribution
133  * for randomly oriented bonds.
134  *
135  * Algorithm: Generate random bond vector and take the angle.
136  */
138  Random *random, double beta, int type) const
139  {
140  double CosineTheta, CosineTheta_min;
141  double U, U_min;
142  bool chosen = false;
143 
144  CosineTheta_min = cos(theta0_[type]);
145  U_min = energy(CosineTheta_min, type);
146 
147  while (chosen == false) {
148  CosineTheta = random->uniform(-1, 1);
149  U = energy(CosineTheta, type);
150  if (random->uniform() < exp(-beta*(U-U_min)))
151  chosen = true;
152  }
153 
154  return CosineTheta;
155  }
156 
157  /*
158  * Modify a parameter, identified by a string.
159  */
160  void HarmonicAngle::set(std::string name, int type, double value)
161  {
162  if (name == "kappa") {
163  kappa_[type] = value;
164  } else
165  if (name == "theta0") {
166  theta0_[type] = value;
167  } else {
168  UTIL_THROW("Unrecognized parameter name");
169  }
170  }
171 
172  /*
173  * Get a parameter value, identified by a string.
174  */
175  double HarmonicAngle::get(std::string name, int type) const
176  {
177  double value = 0.0;
178  if (name == "kappa") {
179  value = kappa_[type];
180  } else
181  if (name == "theta0") {
182  value = theta0_[type];
183  } else {
184  UTIL_THROW("Unrecognized parameter name");
185  }
186  return value;
187  }
188 
189  /*
190  * Return name string "HarmonicAngle" for this evaluator class.
191  */
192  std::string HarmonicAngle::className() const
193  { return std::string("HarmonicAngle"); }
194 
195 
196 }
double get(std::string name, int type) const
Get a parameter value, identified by a string.
void readParameters(std::istream &in)
Read angle interaction parameters from input stream.
HarmonicAngle & operator=(const HarmonicAngle &other)
Assignment.
double randomCosineAngle(Random *random, double beta, int type) const
Return bond angle cosine chosen from equilibrium distribution.
Classes used by all simpatico molecular simulations.
A angle potential that is harmonic in the angle.
Definition: HarmonicAngle.h:34
double uniform()
Return a random floating point number x, uniformly distributed in the range 0 <= x < 1...
Definition: Random.h:203
HarmonicAngle()
Default constructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void pack(const T &data)
Pack one object of type T.
double energy(double cosTheta, int type) const
Returns potential energy for one angle.
Utility classes for scientific computation.
Definition: accumulators.mod:1
double randomAngle(Random *random, double beta, int type) const
Return bond angle chosen from equilibrium distribution.
Saving archive for binary istream.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
static const double Pi
Trigonometric constant Pi.
Definition: Constants.h:35
std::string className() const
Return name string "HarmonicAngle" for this evaluator class.
#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.
void setNAngleType(int nAngleType)
Set the number of angle types.
Random number generator.
Definition: Random.h:46
void set(std::string name, int type, double value)
Modify a parameter, identified by a string.