All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ABMD.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 "Bias.h"
23 #include "tools/Random.h"
24 #include "ActionRegister.h"
25 #include <ctime>
26 
27 using namespace std;
28 
29 namespace PLMD{
30 namespace bias{
31 
32 //+PLUMEDOC BIAS ABMD
33 /*
34 Adds a ratchet-and-pawl like restraint on one or more variables.
35 
36 This action can be used to evolve a system towards a target value in
37 CV space using an harmonic potential moving with the thermal fluctuations of the CV
38 \cite ballone \cite provasi10abmd \cite camilloni11abmd. The biasing potential in this
39 method is as follows:
40 
41 \f$
42 V(\rho(t)) = \left \{ \begin{array}{ll} \frac{K}{2}\left(\rho(t)-\rho_m(t)\right)^2, &\rho(t)>\rho_m(t)\\
43  0, & \rho(t)\le\rho_m(t), \end{array} \right .
44 \f$
45 
46 
47 where
48 
49 
50 \f$
51 \rho(t)=\left(CV(t)-TO\right)^2
52 \f$
53 
54 
55 and
56 
57 
58 \f$
59 \rho_m(t)=\min_{0\le\tau\le t}\rho(\tau)+\eta(t)
60 \f$.
61 
62 The method is based on the introduction of a biasing potential which is zero when
63 the system is moving towards the desired arrival point and which damps the
64 fluctuations when the system attempts to move in the opposite direction. As in the
65 case of the ratchet and pawl system, propelled by thermal motion of the solvent
66 molecules, the biasing potential does not exert work on the system. \f$\eta(t)\f$ is
67 an additional white noise acting on the minimum position of the bias.
68 
69 \par Examples
70 The following input sets up two biases, one on the distance between atoms 3 and 5
71 and another on the distance between atoms 2 and 4. The two target values are defined
72 using TO and the two strength using KAPPA. The total energy of the bias is printed.
73 \verbatim
74 DISTANCE ATOMS=3,5 LABEL=d1
75 DISTANCE ATOMS=2,4 LABEL=d2
76 ABMD ARG=d1,d2 TO=1.0,1.5 KAPPA=5.0,5.0 LABEL=abmd
77 PRINT ARG=abmd.bias,abmd.min_1,abmd.min_2
78 \endverbatim
79 (See also \ref DISTANCE and \ref PRINT).
80 
81 */
82 //+ENDPLUMEDOC
83 
84 class ABMD : public Bias{
85  std::vector<double> to;
86  std::vector<double> min;
87  std::vector<double> kappa;
88  std::vector<double> temp;
89  std::vector<int> seed;
90  vector<Random> random;
91 public:
92  ABMD(const ActionOptions&);
93  void calculate();
94  static void registerKeywords(Keywords& keys);
95 };
96 
97 PLUMED_REGISTER_ACTION(ABMD,"ABMD")
98 
99 void ABMD::registerKeywords(Keywords& keys){
100  Bias::registerKeywords(keys);
101  keys.use("ARG");
102  keys.add("compulsory","TO","The array of target values");
103  keys.add("compulsory","KAPPA","The array of force constants.");
104  keys.add("optional","MIN","Array of starting values for the bias (set rho_m(t), otherwise it is set using the current value of ARG)");
105  keys.add("optional","NOISE","Array of white noise intensities (add a temperature to the ABMD)");
106  keys.add("optional","SEED","Array of seeds for the white noise (add a temperature to the ABMD)");
107  componentsAreNotOptional(keys);
108  keys.addOutputComponent("bias","default","the instantaneous value of the bias potential");
109  keys.addOutputComponent("force2","default","the instantaneous value of the squared force due to this bias potential");
110 // keys.addOutputComponent("_min","default","");
111  keys.addOutputComponent("min_","default","one or multiple instances of this quantity will be refereceable elsewhere in the input file. "
112  "These quantities will be named as min_# and the number of the argument to which they refer."
113  "These quantities tell the user the minimum value assumed by rho_m(t).");
114 }
115 
116 ABMD::ABMD(const ActionOptions&ao):
117 PLUMED_BIAS_INIT(ao),
118 to(getNumberOfArguments(),0.0),
119 min(getNumberOfArguments(),-1.0),
120 kappa(getNumberOfArguments(),0.0),
121 temp(getNumberOfArguments(),0.0),
122 seed(getNumberOfArguments(),time(0)),
123 random(getNumberOfArguments())
124 {
125  // Note : parseVector will check that number of arguments is correct
126  parseVector("KAPPA",kappa);
127  parseVector("MIN",min);
128  if(min.size()==0) min.assign(getNumberOfArguments(),-1.0);
129  if(min.size()!=getNumberOfArguments()) error("MIN array should have the same size as ARG array");
130  parseVector("NOISE",temp);
131  parseVector("SEED",seed);
132  parseVector("TO",to);
133  checkRead();
134 
135  log.printf(" min");
136  for(unsigned i=0;i<min.size();i++) log.printf(" %f",min[i]);
137  log.printf("\n");
138  log.printf(" to");
139  for(unsigned i=0;i<to.size();i++) log.printf(" %f",to[i]);
140  log.printf("\n");
141  log.printf(" with force constant");
142  for(unsigned i=0;i<kappa.size();i++) log.printf(" %f",kappa[i]);
143  log.printf("\n");
144 
145  for(unsigned i=0;i<getNumberOfArguments();i++) {
146  char str_min[6];
147  sprintf(str_min,"min_%u",i+1);
148 // std::string str_min=getPntrToArgument(i)->getName()+"_min";
149  addComponent(str_min);
150  componentIsNotPeriodic(str_min);
151  }
152  for(unsigned i=0;i<getNumberOfArguments();i++) {random[i].setSeed(-seed[i]);}
153  addComponent("bias"); componentIsNotPeriodic("bias");
154  addComponent("force2"); componentIsNotPeriodic("force2");
155 }
156 
157 
159  double ene=0.0;
160  double totf2=0.0;
161  for(unsigned i=0;i<getNumberOfArguments();++i){
162  const double cv=difference(i,to[i],getArgument(i));
163  const double cv2=cv*cv;
164  const double k=kappa[i];
165  double diff=temp[i];
166  if(cv2<=diff) { diff=0.; temp[i]=0.; }
167  double noise = 2.*random[i].Gaussian()*diff;
168 
169  // min < 0 means that the variable has not been used in the input file, so the current position of the CV is used
170  // cv2 < min means that the collective variable is nearer to the target value than at any other previous time so
171  // min is set to the CV value
172  if(min[i]<0.||cv2<min[i]) min[i] = cv2;
173  else {
174  // otherwise a noise is added to the minimum value
175  min[i] += noise;
176  const double f = -2.*k*(cv2-min[i])*cv;
177  setOutputForce(i,f);
178  ene += 0.5*k*(cv2-min[i])*(cv2-min[i]);
179  totf2+=f*f;
180  }
181  char str_min[6];
182  sprintf(str_min,"min_%u",i+1);
183 // std::string str_min=getPntrToArgument(i)->getName()+"_min";
184  getPntrToComponent(str_min)->set(min[i]);
185  };
186  getPntrToComponent("bias")->set(ene);
187  getPntrToComponent("force2")->set(totf2);
188 }
189 
190 }
191 }
192 
193 
vector< Random > random
Definition: ABMD.cpp:90
Log & log
Reference to the log stream.
Definition: Action.h:93
std::vector< int > seed
Definition: ABMD.cpp:89
std::vector< double > to
Definition: ABMD.cpp:85
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
double difference(int, double, double) const
Takes the difference taking into account pbc for arg i.
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
void addComponent(const std::string &name)
Add a value with a name like label.name.
void set(double)
Set the value of the function.
Definition: Value.h:174
This class holds the keywords and their documentation.
Definition: Keywords.h:36
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
std::vector< double > min
Definition: ABMD.cpp:86
void setOutputForce(int i, double g)
Definition: Bias.h:56
std::vector< double > temp
Definition: ABMD.cpp:88
void calculate()
Calculate an Action.
Definition: ABMD.cpp:158
double getArgument(const unsigned n) const
Returns the value of an argument.
std::vector< double > kappa
Definition: ABMD.cpp:87
Provides the keyword ABMD
Definition: ABMD.cpp:84
This is the abstract base class to use for implementing new simulation biases, within it there is inf...
Definition: Bias.h:40
#define PLUMED_BIAS_INIT(ao)
Definition: Bias.h:29
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
unsigned getNumberOfArguments() const
Returns the number of arguments.