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