Simpatico  v1.10
GeneralPeriodicExternal.h
1 #ifndef SIMP_GENERAL_PERIODIC_EXTERNAL_H
2 #define SIMP_GENERAL_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 = 3;
146 
148  DArray<double> prefactor_;
149 
151  double externalParameter_;
152 
154  int nWaveVectors_;
155 
157  DArray<Vector> waveVectors_;
158 
160  DArray<double> phases_;
161 
163  DArray<double> shifts_;
164 
166  int periodicity_;
167 
169  double interfaceWidth_;
170 
172  Boundary *boundaryPtr_;
173 
175  int nAtomType_;
176 
178  bool isInitialized_;
179 
180  };
181 
182  // inline methods
183 
184  /*
185  * Calculate external potential energy for a single atom.
186  */
187  inline double GeneralPeriodicExternal::energy(const Vector& position, int type) const
188  {
189  const Vector cellLengths = boundaryPtr_->lengths();
190  double clipParameter = 1.0/(2.0*M_PI*periodicity_*interfaceWidth_);
191 
192  Vector r;
193  Vector a;
194  r.zero();
195  a.zero();
196  double cosine = 0.0;
197  for (int i = 0; i < nWaveVectors_; ++i) {
198  r = position;
199  for (int j = 0; j < Dimension; j++) {
200  a[j] = shifts_[i*Dimension + j];
201  }
202  r -= a;
203  Vector q;
204  q[0] = 2.0*M_PI*periodicity_*waveVectors_[i][0]/cellLengths[0];
205  q[1] = 2.0*M_PI*periodicity_*waveVectors_[i][1]/cellLengths[1];
206  q[2] = 2.0*M_PI*periodicity_*waveVectors_[i][2]/cellLengths[2];
207  double arg = q.dot(r)+phases_[i];
208  cosine += prefactor_[i*nAtomType_ + type]*cos(arg);
209  }
210  cosine *= clipParameter;
211  return externalParameter_*tanh(cosine);
212  }
213 
214  /*
215  * Calculate external force for a single atom.
216  */
217  inline
218  void GeneralPeriodicExternal::getForce(const Vector& position, int type,
219  Vector& force) const
220  {
221  const Vector cellLengths = boundaryPtr_->lengths();
222  double clipParameter = 1.0/(2.0*M_PI*periodicity_*interfaceWidth_);
223 
224  Vector r;
225  Vector a;
226  r.zero();
227  a.zero();
228  double cosine = 0.0;
229  Vector deriv;
230  deriv.zero();
231  for (int i = 0; i < nWaveVectors_; ++i) {
232  r = position;
233  for (int j = 0; j < Dimension; j++) {
234  a[j] = shifts_[i*Dimension + j];
235  }
236  r -= a;
237  Vector q;
238  q[0] = 2.0*M_PI*periodicity_*waveVectors_[i][0]/cellLengths[0];
239  q[1] = 2.0*M_PI*periodicity_*waveVectors_[i][1]/cellLengths[1];
240  q[2] = 2.0*M_PI*periodicity_*waveVectors_[i][2]/cellLengths[2];
241  double arg = q.dot(r)+phases_[i];
242  cosine += prefactor_[i*nAtomType_ + type]*cos(arg);
243  double sine = -1.0*sin(arg);
244  q *= prefactor_[i*nAtomType_ + type]*sine;
245  deriv += q;
246  }
247  cosine *= clipParameter;
248  deriv *= clipParameter;
249  double tanH = tanh(cosine);
250  double sechSq = (1.0 - tanH*tanH);
251  double f = externalParameter_*sechSq;
252  deriv *= -1.0*f;
253  force = deriv;
254  }
255 
256 }
257 #endif
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Vector & zero()
Set all elements of a 3D vector to zero.
Definition: Vector.h:514
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
double dot(const Vector &v) const
Return dot product of this vector and vector v.
Definition: Vector.h:632
void readParameters(std::istream &in)
Read potential parameters, and initialize other variables.
An orthorhombic periodic unit cell.
void setNAtomType(int nAtomType)
Set nAtomType value.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
double externalParameter() const
Returns external parameter.
Saving / output archive for binary ostream.
Utility classes for scientific computation.
Definition: accumulators.mod:1
std::string className() const
Return name string "GeneralPeriodicExternal".
void getForce(const Vector &position, int type, Vector &force) const
Returns force caused by the external potential.
GeneralPeriodicExternal()
Default constructor.
double energy(const Vector &position, int i) const
Returns external potential energy of a single particle.
Saving archive for binary istream.
A clipped cosine potential that induces ordering along directions specified by waveIntVectors, w_i.
An object that can read multiple parameters from file.
void setExternalParameter(double externalParameter)
Sets external parameter.
GeneralPeriodicExternal & operator=(const GeneralPeriodicExternal &other)
Assignment.
void setBoundary(Boundary &boundary)
Set pointer to Boundary.