Simpatico  v1.10
SimplePeriodicExternal.h
1 #ifndef SIMP_SIMPLE_PERIODIC_EXTERNAL_H
2 #define SIMP_SIMPLE_PERIODIC_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/space/Vector.h>
14 #include <util/param/ParamComposite.h>
15 #include <util/global.h>
16 #include <cmath>
17 
18 namespace Simp
19 {
20 
21  using namespace Util;
22 
38  {
39 
40  public:
41 
46 
51 
56 
62  void setNAtomType(int nAtomType);
63 
70 
76  void setBoundary(Boundary &boundary);
77 
86  void readParameters(std::istream &in);
87 
93  virtual void loadParameters(Serializable::IArchive &ar);
94 
100  virtual void save(Serializable::OArchive &ar);
101 
102  /*
103  * Set a potential energy parameter, identified by a string.
104  */
105  void set(std::string name, double value);
106 
107  /*
108  * Get a parameter value, identified by a string.
109  */
110  double get(std::string name) const;
111 
117  double externalParameter() const;
118 
126  double energy(const Vector& position, int i) const;
127 
135  void getForce(const Vector& position, int type, Vector& force) const;
136 
140  std::string className() const;
141 
142  private:
143 
145  static const int MaxAtomType = 2;
146 
148  DArray<double> prefactor_;
149 
151  double externalParameter_;
152 
154  int nWaveVectors_;
155 
157  DArray<IntVector> waveIntVectors_;
158 
160  int periodicity_;
161 
163  double interfaceWidth_;
164 
166  Boundary *boundaryPtr_;
167 
169  int nAtomType_;
170 
172  bool isInitialized_;
173 
174  };
175 
176  // inline methods
177 
178  /*
179  * Calculate external potential energy for a single atom.
180  */
181  inline double SimplePeriodicExternal::energy(const Vector& position, int type) const
182  {
183  const Vector cellLengths = boundaryPtr_->lengths();
184  double clipParameter = 1.0/(2.0*M_PI*periodicity_*interfaceWidth_);
185 
186  double cosine = 0.0;
187  for (int i = 0; i < nWaveVectors_; ++i) {
188  Vector q;
189  q[0] = 2.0*M_PI*periodicity_*waveIntVectors_[i][0]/cellLengths[0];
190  q[1] = 2.0*M_PI*periodicity_*waveIntVectors_[i][1]/cellLengths[1];
191  q[2] = 2.0*M_PI*periodicity_*waveIntVectors_[i][2]/cellLengths[2];
192  double arg = q.dot(position);
193  cosine += cos(arg);
194  }
195  cosine *= clipParameter;
196  return prefactor_[type]*externalParameter_*tanh(cosine);
197  }
198 
199  /*
200  * Calculate external force for a single atom.
201  */
202  inline
203  void SimplePeriodicExternal::getForce(const Vector& position, int type,
204  Vector& force) const
205  {
206  const Vector cellLengths = boundaryPtr_->lengths();
207  double clipParameter = 1.0/(2.0*M_PI*periodicity_*interfaceWidth_);
208 
209  double cosine = 0.0;
210  Vector deriv;
211  deriv.zero();
212  for (int i = 0; i < nWaveVectors_; ++i) {
213  Vector q;
214  q[0] = 2.0*M_PI*periodicity_*waveIntVectors_[i][0]/cellLengths[0];
215  q[1] = 2.0*M_PI*periodicity_*waveIntVectors_[i][1]/cellLengths[1];
216  q[2] = 2.0*M_PI*periodicity_*waveIntVectors_[i][2]/cellLengths[2];
217  double arg = q.dot(position);
218  cosine += cos(arg);
219  double sine = -1.0*sin(arg);
220  q *= sine;
221  deriv += q;
222  }
223  cosine *= clipParameter;
224  deriv *= clipParameter;
225  double tanH = tanh(cosine);
226  double sechSq = (1.0 - tanH*tanH);
227  double f = prefactor_[type]*externalParameter_*sechSq;
228  deriv *= -1.0*f;
229  force = deriv;
230  }
231 
232 }
233 #endif
Vector & zero()
Set all elements of a 3D vector to zero.
Definition: Vector.h:514
A Vector is a Cartesian vector.
Definition: Vector.h:75
double energy(const Vector &position, int i) const
Returns external potential energy of a single particle.
double dot(const Vector &v) const
Return dot product of this vector and vector v.
Definition: Vector.h:632
An orthorhombic periodic unit cell.
A clipped cosine potential that induces ordering along directions specified by waveIntVectors, w_i.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
std::string className() const
Return name string "SimplePeriodicExternal".
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
Saving / output archive for binary ostream.
Utility classes for scientific computation.
Definition: accumulators.mod:1
double externalParameter() const
Returns external parameter.
void setNAtomType(int nAtomType)
Set nAtomType value.
void setBoundary(Boundary &boundary)
Set pointer to Boundary.
void getForce(const Vector &position, int type, Vector &force) const
Returns force caused by the external potential.
Saving archive for binary istream.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
SimplePeriodicExternal()
Default constructor.
An object that can read multiple parameters from file.
SimplePeriodicExternal & operator=(const SimplePeriodicExternal &other)
Assignment.
void setExternalParameter(double externalParameter)
Sets external parameter.
void readParameters(std::istream &in)
Read potential parameters, and initialize other variables.