All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
External.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 #include "tools/Grid.h"
25 #include "tools/Exception.h"
26 #include "tools/File.h"
27 
28 
29 using namespace std;
30 
31 
32 namespace PLMD{
33 namespace bias{
34 
35 //+PLUMEDOC BIAS EXTERNAL
36 /*
37 Calculate a restraint that is defined on a grid that is read during start up
38 
39 \par Examples
40 The following is an input for a calculation with an external potential that is
41 defined in the file bias.dat and that acts on the distance between atoms 3 and 5.
42 \verbatim
43 DISTANCE ATOMS=3,5 LABEL=d1
44 EXTERNAL ARG=d1 FILENAME=bias.dat LABEL=external
45 \endverbatim
46 (See also \ref DISTANCE \ref PRINT).
47 
48 The header in the file bias.dat should read:
49 \verbatim
50 #! FIELDS d1 external.bias der_d1
51 #! SET min_d1 0.0
52 #! SET max_d1 1.0
53 #! SET nbins_d1 100
54 #! SET periodic_d1 false
55 \endverbatim
56 
57 This should then be followed by the value of the potential and its derivative
58 at 100 equally spaced points along the distance between 0 and 1. If you run
59 with NOSPLINE you do not need to provide derivative information.
60 
61 You can also include grids that are a function of more than one collective
62 variable. For instance the following would be the input for an external
63 potential acting on two torsional angles:
64 \verbatim
65 TORSION ATOMS=4,5,6,7 LABEL=t1
66 TORSION ATOMS=6,7,8,9 LABEL=t2
67 EXTERNAL ARG=t1,t2 FILENAME=bias.dat LABEL=ext
68 \endverbatim
69 
70 The header in the file bias.dat for this calculation would read:
71 \verbatim
72 #! FIELDS t1 t2 ext.bias der_t1 der_t2
73 #! SET min_t1 -pi
74 #! SET max_t1 +pi
75 #! SET nbins_t1 100
76 #! SET periodic_t1 true
77 #! SET min_t2 -pi
78 #! SET max_t2 +pi
79 #! SET nbins_t2 100
80 #! SET periodic_t2 true
81 \endverbatim
82 
83 This would be then followed by 100 blocks of data. In the first block of data the
84 value of t1 (the value in the first column) is kept fixed and the value of
85 the function is given at 100 equally spaced values for t2 between \f$-pi\f$ and \f$+pi\f$. In the
86 second block of data t1 is fixed at \f$-pi + \frac{2pi}{100}\f$ and the value of the function is
87 given at 100 equally spaced values for t2 between \f$-pi\f$ and \f$+pi\f$. In the third block of
88 data the same is done but t1 is fixed at \f$-pi + \frac{4pi}{100}\f$ and so on untill you get to
89 the 100th block of data where t1 is fixed at \f$+pi\f$.
90 
91 Please note the order that the order of arguments in the plumed.dat file must be the same as
92 the order of arguments in the header of the grid file.
93 */
94 //+ENDPLUMEDOC
95 
96 class External : public Bias{
97 
98 private:
100 
101 public:
102  External(const ActionOptions&);
103  ~External();
104  void calculate();
105  static void registerKeywords(Keywords& keys);
106 };
107 
108 PLUMED_REGISTER_ACTION(External,"EXTERNAL")
109 
110 void External::registerKeywords(Keywords& keys){
111  Bias::registerKeywords(keys);
112  keys.use("ARG");
113  keys.add("compulsory","FILE","the name of the file containing the external potential.");
114  keys.addFlag("NOSPLINE",false,"specifies that no spline interpolation is to be used when calculating the energy and forces due to the external potential");
115  keys.addFlag("SPARSE",false,"specifies that the external potential uses a sparse grid");
116  componentsAreNotOptional(keys);
117  keys.addOutputComponent("bias","default","the instantaneous value of the bias potential");
118 }
119 
120 External::~External(){
121  delete BiasGrid_;
122 }
123 
124 External::External(const ActionOptions& ao):
125 PLUMED_BIAS_INIT(ao),
126 BiasGrid_(NULL)
127 {
128  string filename;
129  parse("FILE",filename);
130  if( filename.length()==0 ) error("No external potential file was specified");
131  bool sparsegrid=false;
132  parseFlag("SPARSE",sparsegrid);
133  bool nospline=false;
134  parseFlag("NOSPLINE",nospline);
135  bool spline=!nospline;
136 
137  checkRead();
138 
139  log.printf(" External potential from file %s\n",filename.c_str());
140  if(spline){log.printf(" External potential uses spline interpolation\n");}
141  if(sparsegrid){log.printf(" External potential uses sparse grid\n");}
142 
143  addComponent("bias"); componentIsNotPeriodic("bias");
144 
145 // read grid
146  IFile gridfile; gridfile.open(filename);
147  std::string funcl=getLabel() + ".bias";
148  BiasGrid_=Grid::create(funcl,getArguments(),gridfile,sparsegrid,spline,true);
149  gridfile.close();
150  if(BiasGrid_->getDimension()!=getNumberOfArguments()) error("mismatch between dimensionality of input grid and number of arguments");
151  for(unsigned i=0;i<getNumberOfArguments();++i){
152  if( getPntrToArgument(i)->isPeriodic()!=BiasGrid_->getIsPeriodic()[i] ) error("periodicity mismatch between arguments and input bias");
153  }
154 }
155 
157 {
158  unsigned ncv=getNumberOfArguments();
159  vector<double> cv(ncv), der(ncv);
160 
161  for(unsigned i=0;i<ncv;++i){cv[i]=getArgument(i);}
162 
163  double ene=BiasGrid_->getValueAndDerivatives(cv,der);
164 
165  getPntrToComponent("bias")->set(ene);
166 
167 // set Forces
168  for(unsigned i=0;i<ncv;++i){
169  const double f=-der[i];
170  setOutputForce(i,f);
171  }
172 }
173 
174 }
175 }
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
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.
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
virtual double getValueAndDerivatives(unsigned index, std::vector< double > &der) const
get grid value and derivatives
Definition: Grid.cpp:395
unsigned getDimension() const
get grid dimension
Definition: Grid.cpp:163
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
Value * getPntrToArgument(const unsigned n)
Return a pointer to specific argument.
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
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
void calculate()
Calculate an Action.
Definition: External.cpp:156
IFile & open(const std::string &name)
Opens the file.
Definition: IFile.cpp:90
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
std::vector< Value * > & getArguments()
Returns an array of pointers to the arguments.
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
void close()
Closes the file Should be used only for explicitely opened files.
Definition: FileBase.cpp:140
Class for input files.
Definition: IFile.h:40
void setOutputForce(int i, double g)
Definition: Bias.h:56
double getArgument(const unsigned n) const
Returns the value of an argument.
This is the abstract base class to use for implementing new simulation biases, within it there is inf...
Definition: Bias.h:40
bool isPeriodic() const
Check if the value is periodic.
Definition: Value.cpp:75
#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.
static Grid * create(const std::string &, std::vector< Value * >, IFile &, bool, bool, bool)
read grid from file
Definition: Grid.cpp:573
Provides the keyword EXTERNAL
Definition: External.cpp:96
std::vector< bool > getIsPeriodic() const
get if periodic
Definition: Grid.cpp:146