All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Sphere.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "ManyRestraintsBase.h"
23 #include "core/ActionRegister.h"
24 
25 namespace PLMD {
26 namespace manyrestraints {
27 
28 class Sphere : public ManyRestraintsBase {
29 private:
30  double at;
31  double kappa;
32  double exp;
33  double eps;
34  double offset;
35  bool nopbc;
37  std::vector<double> com_deriv;
38 public:
39  static void registerKeywords( Keywords& keys );
40  Sphere( const ActionOptions& );
41  bool isPeriodic(){ return false; }
42  void performTask( const unsigned& j );
43  void calculate();
44 };
45 
46 PLUMED_REGISTER_ACTION(Sphere,"SPHERICAL_RESTRAINT")
47 
48 void Sphere::registerKeywords( Keywords& keys ){
50  keys.add("atoms","ATOMS","the atoms that are being confined to the sphere");
51  keys.add("compulsory","RADIUS","the radius of the sphere");
52  keys.add("compulsory","KAPPA","the force constant for the wall. The k_i in the expression for a wall.");
53  keys.add("compulsory","OFFSET","0.0","the offset for the start of the wall. The o_i in the expression for a wall.");
54  keys.add("compulsory","EXP","2.0","the powers for the walls. The e_i in the expression for a wall.");
55  keys.add("compulsory","EPS","1.0","the values for s_i in the expression for a wall");
56  keys.addFlag("NOPBC",false,"turn off periodic boundary conditions");
57 }
58 
60 Action(ao),
62 {
63  std::vector<AtomNumber> atoms;
64  parseAtomList("ATOMS",atoms);
65  com_deriv.resize( atoms.size() );
66 
67  parse("RADIUS",at);
68  parse("OFFSET",offset);
69  parse("EPS",eps);
70  parse("EXP",exp);
71  parse("KAPPA",kappa);
72  parseFlag("NOPBC",nopbc);
73  checkRead();
74 
75  requestAtoms( atoms );
76  createRestraints( atoms.size() );
77 }
78 
80  // Calculate position of the center of mass
81  double mass=0; com.zero();
82  for(unsigned i=0;i<getNumberOfAtoms();i++) mass+=getMass(i);
83 
84  for(unsigned i=0;i<getNumberOfAtoms();i++){
85  com+=(getMass(i)/mass)*getPosition(i);
86  com_deriv[i]=(getMass(i)/mass);
87  }
88 
89  // Now run the full set of tasks
90  runAllTasks();
91 }
92 
93 void Sphere::performTask( const unsigned& j ){
94  Vector distance;
95 
96  if(!nopbc){
98  } else {
99  distance=delta(com,getPosition(current));
100  }
101 
102  double value=distance.modulo();
103  double uscale = (value - at + offset)/eps;
104  if( uscale > 0. ){
105  double invvalue= 1.0 / value ;
106  double power = pow( uscale, exp );
107  double f = invvalue * ( kappa / eps ) * exp * power / uscale;
108 
109  setElementValue( 0, kappa*power ); setElementValue( 1, 1.0 );
110  // Add derivatives for com
111  for(unsigned i=0;i<getNumberOfAtoms();++i) addAtomsDerivatives( i, -com_deriv[i]*f*distance );
112 
113  // Add derivatives for other atom
114  addAtomsDerivatives( current, f*distance );
115 
116  // Add derivatives for virial
117  addBoxDerivatives( -f*Tensor(distance,distance) );
118 
119  // We need to accumulate derivatives
120  return;
121  }
122 
123  // We need do nothing more if this is not true
124  setElementValue( 1, 0.0 );
125  return;
126 }
127 
128 }
129 }
130 
void calculate()
Calculate an Action.
Definition: Sphere.cpp:79
const Vector & getPosition(int) const
Get position of i-th atom.
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
unsigned current
The numerical index of the task we are curently performing.
double modulo() const
Compute the modulo.
Definition: Vector.h:325
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
void addAtomsDerivatives(const int &, const Vector &)
Add some derivatives to a particular atom.
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
void parseAtomList(const std::string &key, std::vector< AtomNumber > &t)
Parse a list of atoms without a numbered keyword.
Provides the keyword SPHERICAL_RESTRAINT
Definition: Sphere.cpp:28
static void registerKeywords(Keywords &keys)
Definition: Sphere.cpp:48
void runAllTasks()
Calculate the values of all the vessels.
Sphere(const ActionOptions &)
Definition: Sphere.cpp:59
void zero()
set it to zero
Definition: Vector.h:173
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
This class holds the keywords and their documentation.
Definition: Keywords.h:36
std::vector< double > com_deriv
Definition: Sphere.cpp:37
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void setElementValue(const unsigned &, const double &)
Set the value of the element.
Base class for all the input Actions.
Definition: Action.h:60
VectorGeneric< n > delta(const VectorGeneric< n > &v1, const VectorGeneric< n > &v2)
Definition: Vector.h:262
void performTask(const unsigned &j)
Calculate one of the functions in the distribution.
Definition: Sphere.cpp:93
Vector pbcDistance(const Vector &, const Vector &) const
Compute the pbc distance between two positions.
void requestAtoms(const std::vector< AtomNumber > &a)
Request an array of atoms.
void addBoxDerivatives(const Tensor &)
Add some derivatives to the virial.
Tensor3d Tensor
Definition: Tensor.h:425
unsigned getNumberOfAtoms() const
Get number of available atoms.
void createRestraints(const unsigned &nrestraints)
double getMass(int i) const
Get mass of i-th atom.
static void registerKeywords(Keywords &keys)
bool isPeriodic()
Are the base quantities periodic.
Definition: Sphere.cpp:41