All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DumpDerivatives.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 "core/ActionPilot.h"
23 #include "core/ActionWithArguments.h"
24 #include "core/ActionRegister.h"
25 #include "tools/File.h"
26 
27 using namespace std;
28 
29 namespace PLMD{
30 namespace generic{
31 
32 //+PLUMEDOC ANALYSIS DUMPDERIVATIVES
33 /*
34 Dump the derivatives with respect to the input parameters for one or more objects (generally CVs, functions or biases).
35 
36 For a CV this line in input instructs plumed to print the derivative of the CV with respect to the atom positions
37 and the cell vectors (virial-like form). In contrast, for a function or bias the derivative with respect to the input "CVs"
38 will be output. This command is most often used to test whether or not analytic derivatives have been implemented correctly. This
39 can be done by outputting the derivatives calculated analytically and numerically. You can control the buffering of output using the \ref FLUSH keyword.
40 
41 \par Examples
42 The following input instructs plumed to write a file called deriv that contains both the
43 analytical and numerical derivatives of the distance between atoms 1 and 2.
44 \verbatim
45 DISTANCE ATOM=1,2 LABEL=distance
46 DISTANCE ATOM=1,2 LABEL=distanceN NUMERICAL_DERIVATIVES
47 DUMPDERIVATIVES ARG=distance,distanceN STRIDE=1 FILE=deriv
48 \endverbatim
49 
50 (See also \ref DISTANCE)
51 
52 */
53 //+ENDPLUMEDOC
54 
56 public ActionPilot,
58 {
59  string file;
60  string fmt;
62 public:
63  void calculate(){}
65  static void registerKeywords(Keywords& keys);
66  void apply(){}
67  void update();
68  ~DumpDerivatives();
69 };
70 
71 PLUMED_REGISTER_ACTION(DumpDerivatives,"DUMPDERIVATIVES")
72 
73 void DumpDerivatives::registerKeywords(Keywords& keys){
74  Action::registerKeywords(keys);
75  ActionPilot::registerKeywords(keys);
76  ActionWithArguments::registerKeywords(keys);
77  keys.use("ARG");
78  keys.add("compulsory","STRIDE","1","the frequency with which the derivatives should be output");
79  keys.add("compulsory","FILE","the name of the file on which to output the derivatives");
80  keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
81 }
82 
83 DumpDerivatives::DumpDerivatives(const ActionOptions&ao):
84 Action(ao),
85 ActionPilot(ao),
87 fmt("%15.10f")
88 {
89  parse("FILE",file);
90  if( file.length()==0 ) error("name of output file was not specified");
91  parse("FMT",fmt);
92  fmt=" "+fmt;
93  of.link(*this);
94  of.open(file);
95  log.printf(" on file %s\n",file.c_str());
96  log.printf(" with format %s\n",fmt.c_str());
97  unsigned nargs=getNumberOfArguments();
98  if( nargs==0 ) error("no arguments specified");
99  unsigned npar=getPntrToArgument(0)->getNumberOfDerivatives();
100  if( npar==0 ) error("one or more arguments has no derivatives");
101  for(unsigned i=1;i<nargs;i++){
102  if( npar!=getPntrToArgument(i)->getNumberOfDerivatives() ) error("the number of derivatives must be the same in all values being dumped");
103  }
104  checkRead();
105 }
106 
107 
109  unsigned npar=getPntrToArgument(0)->getNumberOfDerivatives();
110  for(unsigned ipar=0;ipar<npar;ipar++){
111  of.fmtField(" %f");
112  of.printField("time",getTime());
113  of.printField("parameter",(int)ipar);
114  for(unsigned i=0;i<getNumberOfArguments();i++){
115  of.fmtField(fmt);
116  of.printField(getPntrToArgument(i)->getName(),getPntrToArgument(i)->getDerivative(ipar) );
117  }
118  of.printField();
119  }
120 }
121 
123 }
124 
125 }
126 
127 
128 }
Log & log
Reference to the log stream.
Definition: Action.h:93
This is used to create PLMD::Action objects that are run with some set frequency. ...
Definition: ActionPilot.h:39
void apply()
Apply an Action.
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
unsigned getNumberOfDerivatives() const
Get the number of derivatives that this particular value has.
Definition: Value.h:201
OFile & fmtField(const std::string &)
Set the format for writing double precision fields.
Definition: OFile.cpp:135
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.
This is used to create PLMD::Action objects that take the output from some other Action as input...
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
Provides the keyword DUMPDERIVATIVES
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
OFile & link(OFile &)
Allows linking this OFile to another one.
Definition: OFile.cpp:71
double getTime() const
Return the present time.
Definition: Action.cpp:173
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
void calculate()
Calculate an Action.
const std::string & getName() const
Returns the name.
Definition: Action.h:268
Base class for all the input Actions.
Definition: Action.h:60
Class for output files.
Definition: OFile.h:84
OFile & printField(const std::string &, double)
Set the value of a double precision field.
Definition: OFile.cpp:145
unsigned getNumberOfArguments() const
Returns the number of arguments.
OFile & open(const std::string &name)
Opens the file using automatic append/backup.
Definition: OFile.cpp:264