All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Restraint.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 "ActionRegister.h"
24 
25 
26 using namespace std;
27 
28 
29 namespace PLMD{
30 namespace bias{
31 
32 //+PLUMEDOC BIAS RESTRAINT
33 /*
34 Adds harmonic and/or linear restraints on one or more variables.
35 
36 Either or both
37 of SLOPE and KAPPA must be present to specify the linear and harmonic force constants
38 respectively. The resulting potential is given by:
39 \f[
40  \sum_i \frac{k_i}{2} (x_i-a_i)^2 + m_i*(x_i-a_i)
41 \f].
42 
43 The number of components for any vector of force constants must be equal to the number
44 of arguments to the action.
45 
46 \par Examples
47 The following input tells plumed to restrain the distance between atoms 3 and 5
48 and the distance between atoms 2 and 4, at different equilibrium
49 values, and to print the energy of the restraint
50 \verbatim
51 DISTANCE ATOMS=3,5 LABEL=d1
52 DISTANCE ATOMS=2,4 LABEL=d2
53 RESTRAINT ARG=d1,d2 AT=1.0,1.5 KAPPA=150.0,150.0 LABEL=restraint
54 PRINT ARG=restraint.bias
55 \endverbatim
56 (See also \ref DISTANCE and \ref PRINT).
57 
58 */
59 //+ENDPLUMEDOC
60 
61 class Restraint : public Bias{
62  std::vector<double> at;
63  std::vector<double> kappa;
64  std::vector<double> slope;
67 public:
68  Restraint(const ActionOptions&);
69  void calculate();
70  static void registerKeywords(Keywords& keys);
71 };
72 
73 PLUMED_REGISTER_ACTION(Restraint,"RESTRAINT")
74 
75 void Restraint::registerKeywords(Keywords& keys){
76  Bias::registerKeywords(keys);
77  keys.use("ARG");
78  keys.add("compulsory","SLOPE","0.0","specifies that the restraint is linear and what the values of the force constants on each of the variables are");
79  keys.add("compulsory","KAPPA","0.0","specifies that the restraint is harmonic and what the values of the force constants on each of the variables are");
80  keys.add("compulsory","AT","the position of the restraint");
81  componentsAreNotOptional(keys);
82  keys.addOutputComponent("bias","default","the instantaneous value of the bias potential");
83  keys.addOutputComponent("force2","default","the instantaneous value of the squared force due to this bias potential");
84 }
85 
86 Restraint::Restraint(const ActionOptions&ao):
88 at(getNumberOfArguments()),
89 kappa(getNumberOfArguments(),0.0),
90 slope(getNumberOfArguments(),0.0)
91 {
92  parseVector("SLOPE",slope);
93  parseVector("KAPPA",kappa);
94  parseVector("AT",at);
95  checkRead();
96 
97  log.printf(" at");
98  for(unsigned i=0;i<at.size();i++) log.printf(" %f",at[i]);
99  log.printf("\n");
100  log.printf(" with harmonic force constant");
101  for(unsigned i=0;i<kappa.size();i++) log.printf(" %f",kappa[i]);
102  log.printf("\n");
103  log.printf(" and linear force constant");
104  for(unsigned i=0;i<slope.size();i++) log.printf(" %f",slope[i]);
105  log.printf("\n");
106 
107  addComponent("bias"); componentIsNotPeriodic("bias");
108  addComponent("force2"); componentIsNotPeriodic("force2");
111 }
112 
113 
115  double ene=0.0;
116  double totf2=0.0;
117  for(unsigned i=0;i<getNumberOfArguments();++i){
118  const double cv=difference(i,at[i],getArgument(i));
119  const double k=kappa[i];
120  const double m=slope[i];
121  const double f=-(k*cv+m);
122  ene+=0.5*k*cv*cv+m*cv;
123  setOutputForce(i,f);
124  totf2+=f*f;
125  };
126  valueBias->set(ene);
127  valueForce2->set(totf2);
128 }
129 
130 }
131 
132 
133 }
Log & log
Reference to the log stream.
Definition: Action.h:93
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
double difference(int, double, double) const
Takes the difference taking into account pbc for arg i.
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.
std::vector< double > kappa
Definition: Restraint.cpp:63
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
Provides the keyword RESTRAINT
Definition: Restraint.cpp:61
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 calculate()
Calculate an Action.
Definition: Restraint.cpp:114
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
void setOutputForce(int i, double g)
Definition: Bias.h:56
double getArgument(const unsigned n) const
Returns the value of an argument.
std::vector< double > slope
Definition: Restraint.cpp:64
void const char const char int double int double double int int double int * m
Definition: Matrix.h:42
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.
std::vector< double > at
Definition: Restraint.cpp:62