Simpatico  v1.10
SphericalTabulatedExternal.h
1 #ifndef SIMP_SPHERICAL_TABULATED_EXTERNAL_H
2 #define SIMP_SPHERICAL_TABULATED_EXTERNAL_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <simp/boundary/Boundary.h>
12 #include <util/space/Dimension.h>
13 #include <util/param/ParamComposite.h>
14 #include <util/global.h>
15 
16 #include <cmath>
17 #include <string>
18 
19 namespace Simp
20 {
21 
22  using namespace Util;
23 
30  {
31 
32  public:
33 
38 
43 
49 
51 
52 
58  void setNAtomType(int nAtomType);
59 
65  void setBoundary(Boundary &boundary);
66 
75  void readParameters(std::istream &in);
76 
82  virtual void loadParameters(Serializable::IArchive &ar);
83 
89  virtual void save(Serializable::OArchive &ar);
90 
91  /*
92  * Set a potential energy parameter, identified by a string.
93  */
94  void set(std::string name, double value);
95 
96  /*
97  * Get a parameter value, identified by a string.
98  */
99  double get(std::string name) const;
100 
102 
104 
108  std::string className() const;
109 
117  double energy(const Vector& position, int i) const;
118 
126  void getForce(const Vector& position, int type, Vector& force) const;
127 
129 
130  private:
131 
133  static const int MaxAtomType = 6;
134 
136  std::string filename_;
137 
139  double rMax_;
140 
142  double rMaxSq_;
143 
145  double dr_;
146 
148  Boundary *boundaryPtr_;
149 
151  int nr_;
152 
154  int nAtomType_;
155 
157  DArray< DArray<double> > potentials_;
158 
160  bool isInitialized_;
161 
162  };
163 
164  // inline methods
165 
166  /*
167  * Calculate external potential energy for a single atom.
168  */
169  inline
170  double SphericalTabulatedExternal::energy(const Vector& position, int type) const
171  {
172  Vector origin = boundaryPtr_->lengths();
173  origin *= 0.5;
174  double rSq = boundaryPtr_->distanceSq(position, origin);
175  if (rSq < rMaxSq_) {
176  double r = sqrt(rSq);
177  int k = (int) (r/dr_);
178  double fp = r - k*dr_;
179  UTIL_CHECK(k >= 0);
180  UTIL_CHECK(k < nr_ -1);
181  UTIL_CHECK(fp >= 0.0);
182  UTIL_CHECK(fp <= 1.0);
183  double fm = 1.0 - fp;
184  return fm*potentials_[type][k] + fp*potentials_[type][k+1];
185  } else {
186  return 0.0;
187  }
188  }
189 
190  /*
191  * Calculate external force for a single atom.
192  */
193  inline void
194  SphericalTabulatedExternal::getForce(const Vector& position, int type,
195  Vector& force)
196  const
197  {
198  Vector origin = boundaryPtr_->lengths();
199  origin *= 0.5;
200  double rSq = boundaryPtr_->distanceSq(position, origin, force);
201  if (rSq < rMaxSq_) {
202  double r = sqrt(rSq);
203  int k = (int) (r/dr_);
204  UTIL_CHECK(k >= 0);
205  UTIL_CHECK(k < nr_ -1);
206  double fs = potentials_[type][k] - potentials_[type][k+1];
207  fs /= dr_;
208  force *= fs/r;
209  } else {
210  for (int i = 0; i < Dimension; ++i) {
211  force[i] = 0.0;
212  }
213  }
214  }
215 
216 }
217 #endif
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
SphericalTabulatedExternal & operator=(const SphericalTabulatedExternal &other)
Assignment.
An orthorhombic periodic unit cell.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
std::string className() const
Return name string for this interaction class.
Saving / output archive for binary ostream.
double energy(const Vector &position, int i) const
Returns external potential energy of a single particle.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void readParameters(std::istream &in)
Read potential parameters, and initialize other variables.
Dynamically allocatable contiguous array template.
Definition: DArray.h:31
Saving archive for binary istream.
The potential is independent on the type of atoms.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
void setBoundary(Boundary &boundary)
Set pointer to Boundary.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void setNAtomType(int nAtomType)
Set nAtomType value.
An object that can read multiple parameters from file.
void getForce(const Vector &position, int type, Vector &force) const
Returns force caused by the external potential.