All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Action.h
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 #ifndef __PLUMED_core_Action_h
23 #define __PLUMED_core_Action_h
24 #include <vector>
25 #include <string>
26 #include <set>
27 #include "tools/Keywords.h"
28 #include "Value.h"
29 #include "tools/Tools.h"
30 #include "tools/Log.h"
31 
32 namespace PLMD{
33 
34 class PDB;
35 class PlumedMain;
36 class Communicator;
37 
38 /// This class is used to bring the relevant information to the Action constructor.
39 /// Only Action and ActionRegister class can access to its content, which is
40 /// kept private to other classes, and may change in the future.
42  friend class Action;
43  friend class ActionRegister;
44 /// Reference to main PlumedMain object
46 /// Input line which sets up the action
47  std::vector<std::string> line;
48 /// The documentation for this action
49  const Keywords& keys;
51 public:
52 /// Constructor
53  ActionOptions(PlumedMain&p,const std::vector<std::string>&);
54  ActionOptions(const ActionOptions&,const Keywords& keys);
55 };
56 
57 /// Base class for all the input Actions.
58 /// The input Actions are more or less corresponding to the directives
59 /// in the plumed.dat file and are applied in order at each time-step.
60 class Action
61 {
62 
63 /// Name of the directive in the plumed.dat file.
64  const std::string name;
65 
66 /// Label of the Action, as set with LABEL= in the plumed.dat file.
67  std::string label;
68 
69 /// Directive line.
70 /// This line is progressively erased during Action construction
71 /// so as to check if all the present keywords are correct.
72  std::vector<std::string> line;
73 
74 public:
75  typedef std::set<Action*> Dependencies;
76 
77 private:
78 /// Actions on which this Action depends.
79  Dependencies after;
80 
81 /// Switch to activate Action on this step.
82  bool active;
83 
84 /// Option that you might have enabled
85  std::set<std::string> options;
86 
87 public:
88 
89 /// Reference to main plumed object
91 
92 /// Reference to the log stream
93  Log& log;
94 
95 /// Specify that this Action depends on another one
96  void addDependency(Action*);
97 
98 /// Clear the dependence list for this Action
99  void clearDependencies();
100 
101 /// Return the present timestep
102  long int getStep()const;
103 
104 /// Return the present time
105  double getTime()const;
106 
107 /// Return the timestep
108  double getTimeStep()const;
109 
110 /// Parse one keyword as generic type
111  template<class T>
112  void parse(const std::string&key,T&t);
113 
114 /// Parse one numbered keyword as generic type
115  template<class T>
116  bool parseNumbered(const std::string&key, const int no, T&t);
117 
118 /// Parse one keyword as std::vector
119  template<class T>
120  void parseVector(const std::string&key,std::vector<T>&t);
121 
122 /// Parse a vector with a number
123  template<class T>
124  bool parseNumberedVector(const std::string& key, const int no, std::vector<T>&t);
125 
126 /// Parse one keyword as boolean flag
127  void parseFlag(const std::string&key,bool&t);
128 
129 /// Crash calculation and print documentation
130  void error( const std::string & msg ) const;
131 
132 /// Issue a warning
133  void warning( const std::string & msg );
134 
135 /// Exit with error code c
136  void exit(int c=0);
137 
138 ///
139  std::set<FILE*> files;
140  typedef std::set<FILE*>::iterator files_iterator;
141 
142 public:
143 /// Standard constructor from ActionOptions
144  Action(const ActionOptions&);
145 /// Destructor
146  virtual ~Action();
147 private:
148 /// Copy constructor is disabled (private and unimplemented)
149  Action(const Action&a);
150 /// Assignment operator is disabled (private and unimplemented)
151  Action& operator=(const Action&a);
152 public:
153 /// Check if Action was properly read.
154 /// This checks if Action::line is empty. It must be called after
155 /// a final Action has been initialized
156  void checkRead();
157 
160 
162 /// Prepare an Action for calculation
163 /// This can be used by Action if they need some special preparation
164 /// before calculation. Typical case is for collective variables
165 /// which would like to change their list of requested atoms.
166 /// By default (if not overridden) does nothing.
167  virtual void prepare();
168 
169 /// Register all the relevant keywords for the action
170  static void registerKeywords( Keywords& keys );
171 
172  virtual void lockRequests(){}
173  virtual void unlockRequests(){}
174 
175 /// Calculate an Action.
176 /// This method is called one or more times per step.
177 /// The set of all Actions is calculated in forward order.
178  virtual void calculate()=0;
179 
180 /// Apply an Action.
181 /// This method is called one time per step.
182 /// The set of all Actions is applied in backward order.
183  virtual void apply()=0;
184 
185 /// Update.
186 /// This method is called one time per step.
187 /// The set of all Actions is updated in forward order.
188  virtual void update(){}
189 
190 /// RunFinalJobs
191 /// This method is called once at the very end of the calculation.
192 /// The set of all Actions in run for the final time in forward order.
193  virtual void runFinalJobs(){}
194 
195 /// Tell to the Action to flush open files
196  void fflush();
197 
198  virtual std::string getDocumentation()const;
199 
200 /// Returns the label
201  const std::string & getLabel()const;
202 
203 /// Returns the name
204  const std::string & getName()const;
205 
206 /// Set action to active
207  virtual void activate();
208 
209 ///
210  virtual void setOption(const std::string &s);
211 
212  virtual void clearOptions();
213 
214 /// Set action to inactive
215  virtual void deactivate();
216 
217 /// Check if action is active
218  bool isActive()const;
219 
220 /// Check if an option is on
221  bool isOptionOn(const std::string &s)const;
222 
223 /// Return dependencies
224  const Dependencies & getDependencies()const{return after;}
225 
226 /// Check if numerical derivatives should be performed
227  virtual bool checkNumericalDerivatives()const{return false;}
228 
229 /// Check if the action needs gradient
230  virtual bool checkNeedsGradients()const{return false;}
231 
232 /// Perform calculation using numerical derivatives
233 /// N.B. only pass an ActionWithValue to this routine if you know exactly what you
234 /// are doing.
235  virtual void calculateNumericalDerivatives( ActionWithValue* a=NULL );
236 
237 /// Opens a file.
238 /// This is similar to plain fopen, but with some extra functionality.
239 /// * When opened for writing, processors other than the one with rank 0 just open /dev/null
240 /// * PlumedMain::fopen is used, so that other tricks may appear (see \ref PlumedMain::fopen)
241  FILE *fopen(const char *path, const char *mode);
242 /// Closes a file opened with Action::fclose().
243  int fclose(FILE*fp);
244 
245 /// Calculate the action given a pdb file as input. This is used to initialize
246 /// things like distance from a point in CV map space given a pdb as an input file
247  void calculateFromPDB( const PDB& );
248 /// This is overwritten in ActionAtomistic so that we can read
249 /// the atoms from the pdb input file rather than taking them from the
250 /// MD code
251  virtual void readAtomsFromPDB( const PDB& ){}
252 /// Check if we are on an exchange step
253  bool getExchangeStep()const;
254 
255 /// Cite a paper see PlumedMain::cite
256  std::string cite(const std::string&s);
257 };
258 
259 /////////////////////
260 // FAST INLINE METHODS
261 
262 inline
263 const std::string & Action::getLabel()const{
264  return label;
265 }
266 
267 inline
268 const std::string & Action::getName()const{
269  return name;
270 }
271 
272 template<class T>
273 void Action::parse(const std::string&key,T&t){
274 // if(!Tools::parse(line,key,t)){
275 // log.printf("ERROR parsing keyword %s\n",key.c_str());
276 // log.printf("%s\n",getDocumentation().c_str());
277 // this->exit(1);
278 // }
279  // Check keyword has been registered
280  plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
281 
282  // Now try to read the keyword
283  bool found; std::string def;
284  found=Tools::parse(line,key,t);
285 
286  // If it isn't read and it is compulsory see if a default value was specified
287  if ( !found && (keywords.style(key,"compulsory") || keywords.style(key,"hidden")) ){
288  if( keywords.getDefaultValue(key,def) ){
289  if( def.length()==0 || !Tools::convert(def,t) ){
290  log.printf("ERROR in action %s with label %s : keyword %s has weird default value",name.c_str(),label.c_str(),key.c_str() );
291  this->exit(1);
292  }
293  } else if( keywords.style(key,"compulsory") ){
294  error("keyword " + key + " is compulsory for this action");
295  }
296  }
297 }
298 
299 template<class T>
300 bool Action::parseNumbered(const std::string&key, const int no, T&t){
301  // Check keyword has been registered
302  plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
303  if( !keywords.numbered(key) ) error("numbered keywords are not allowed for " + key );
304 
305  // Now try to read the keyword
306  std::string num; Tools::convert(no,num);
307  return Tools::parse(line,key+num,t);
308 }
309 
310 template<class T>
311 void Action::parseVector(const std::string&key,std::vector<T>&t){
312 // if(!Tools::parseVector(line,key,t)){
313 // log.printf("ERROR parsing keyword %s\n",key.c_str());
314 // log.printf("%s\n",getDocumentation().c_str());
315 // this->exit(1);
316 // }
317 
318  // Check keyword has been registered
319  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
320  unsigned size=t.size(); bool skipcheck=false;
321  if(size==0) skipcheck=true;
322 
323  // Now try to read the keyword
324  bool found; std::string def; T val;
325  found=Tools::parseVector(line,key,t);
326 
327  // Check vectors size is correct (not if this is atoms or ARG)
328  if( !keywords.style(key,"atoms") && found ){
329 // bool skipcheck=false;
330 // if( keywords.style(key,"compulsory") ){ keywords.getDefaultValue(key,def); skipcheck=(def=="nosize"); }
331  if( !skipcheck && t.size()!=size ) error("vector read in for keyword " + key + " has the wrong size");
332  }
333 
334  // If it isn't read and it is compulsory see if a default value was specified
335  if ( !found && (keywords.style(key,"compulsory") || keywords.style(key,"hidden")) ){
336  if( keywords.getDefaultValue(key,def) ){
337  if( def.length()==0 || !Tools::convert(def,val) ){
338  log.printf("ERROR in action %s with label %s : keyword %s has weird default value",name.c_str(),label.c_str(),key.c_str() );
339  this->exit(1);
340  } else {
341  for(unsigned i=0;i<t.size();++i) t[i]=val;
342  }
343  } else if( keywords.style(key,"compulsory") ){
344  error("keyword " + key + " is compulsory for this action");
345  }
346  } else if ( !found ){
347  t.resize(0);
348  }
349 }
350 
351 template<class T>
352 bool Action::parseNumberedVector(const std::string&key, const int no, std::vector<T>&t){
353  plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
354  if( !keywords.numbered(key) ) error("numbered keywords are not allowed for " + key );
355 
356  unsigned size=t.size(); bool skipcheck=false;
357  if(size==0) skipcheck=true;
358  std::string num; Tools::convert(no,num);
359  bool found=Tools::parseVector(line,key+num,t);
360  if( keywords.style(key,"compulsory") ){
361  if (!skipcheck && found && t.size()!=size ) error("vector read in for keyword " + key + num + " has the wrong size");
362  } else if ( !found ){
363  t.resize(0);
364  }
365  return found;
366 }
367 
368 inline
370  options.clear();
371  active=false;
372 }
373 
374 inline
375 bool Action::isActive()const{
376  return active;
377 }
378 
379 inline
380 bool Action::isOptionOn(const std::string &s)const{
381  return options.count(s);
382 }
383 
384 }
385 #endif
386 
bool parseNumbered(const std::string &key, const int no, T &t)
Parse one numbered keyword as generic type.
Definition: Action.h:300
static Keywords emptyKeys
Definition: Action.h:50
static bool parseVector(std::vector< std::string > &line, const std::string &key, std::vector< T > &val)
Find a keyword on the input line, eventually deleting it, and saving its value to a vector...
Definition: Tools.h:135
bool isActive() const
Check if action is active.
Definition: Action.h:375
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
Action(const ActionOptions &)
Standard constructor from ActionOptions.
Definition: Action.cpp:54
Log & log
Reference to the log stream.
Definition: Action.h:93
bool active
Switch to activate Action on this step.
Definition: Action.h:82
ActionOptions(PlumedMain &p, const std::vector< std::string > &)
Constructor.
Definition: Action.cpp:35
const Dependencies & getDependencies() const
Return dependencies.
Definition: Action.h:224
void warning(const std::string &msg)
Issue a warning.
Definition: Action.cpp:201
void exit(int c=0)
Exit with error code c.
Definition: Action.cpp:183
virtual bool checkNumericalDerivatives() const
Check if numerical derivatives should be performed.
Definition: Action.h:227
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
void calculateFromPDB(const PDB &)
Calculate the action given a pdb file as input.
Definition: Action.cpp:205
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
virtual void activate()
Set action to active.
Definition: Action.cpp:127
Class containing wrappers to MPI.
Definition: Communicator.h:44
virtual void lockRequests()
Definition: Action.h:172
static bool parse(std::vector< std::string > &line, const std::string &key, T &val)
Find a keyword on the input line, eventually deleting it, and saving its value to val...
Definition: Tools.h:127
double getTimeStep() const
Return the timestep.
Definition: Action.cpp:177
virtual void readAtomsFromPDB(const PDB &)
This is overwritten in ActionAtomistic so that we can read the atoms from the pdb input file rather t...
Definition: Action.h:251
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
virtual void clearOptions()
Definition: Action.cpp:147
virtual void unlockRequests()
Definition: Action.h:173
Communicator & comm
Definition: Action.h:158
virtual void calculate()=0
Calculate an Action.
const Keywords & keys
The documentation for this action.
Definition: Action.h:49
Class containing the log stream.
Definition: Log.h:35
std::vector< std::string > line
Directive line.
Definition: Action.h:72
bool getDefaultValue(std::string key, std::string &def) const
Get the value of the default for the keyword named key.
Definition: Keywords.cpp:543
void addDependency(Action *)
Specify that this Action depends on another one.
Definition: Action.cpp:123
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
virtual void update()
Update.
Definition: Action.h:188
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
Communicator & multi_sim_comm
Definition: Action.h:159
FILE * fopen(const char *path, const char *mode)
Opens a file.
Definition: Action.cpp:83
virtual bool checkNeedsGradients() const
Check if the action needs gradient.
Definition: Action.h:230
This class holds the keywords and their documentation.
Definition: Keywords.h:36
const std::string name
Name of the directive in the plumed.dat file.
Definition: Action.h:64
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
virtual void deactivate()
Set action to inactive.
Definition: Action.h:369
bool numbered(const std::string &k) const
Check if numbered keywords are allowed for this action.
Definition: Keywords.cpp:217
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
const std::string & getName() const
Returns the name.
Definition: Action.h:268
Base class for all the input Actions.
Definition: Action.h:60
std::string label
Label of the Action, as set with LABEL= in the plumed.dat file.
Definition: Action.h:67
virtual void calculateNumericalDerivatives(ActionWithValue *a=NULL)
Perform calculation using numerical derivatives N.B.
Definition: Action.cpp:187
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
Definition: Action.cpp:49
bool getExchangeStep() const
Check if we are on an exchange step.
Definition: Action.cpp:217
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
virtual ~Action()
Destructor.
Definition: Action.cpp:77
bool isOptionOn(const std::string &s) const
Check if an option is on.
Definition: Action.h:380
virtual std::string getDocumentation() const
Definition: Action.cpp:157
virtual void prepare()
Prepare an Action for calculation This can be used by Action if they need some special preparation be...
Definition: Action.cpp:191
virtual void setOption(const std::string &s)
Definition: Action.cpp:141
bool style(const std::string &k, const std::string &t) const
Check if the keyword with name k has style t.
Definition: Keywords.cpp:223
PlumedMain & plumed
Reference to main plumed object.
Definition: Action.h:90
virtual void apply()=0
Apply an Action.
Minimalistic pdb parser.
Definition: PDB.h:38
int fclose(FILE *fp)
Closes a file opened with Action::fclose().
Definition: Action.cpp:93
Action & operator=(const Action &a)
Assignment operator is disabled (private and unimplemented)
Dependencies after
Actions on which this Action depends.
Definition: Action.h:79
void fflush()
Tell to the Action to flush open files.
Definition: Action.cpp:98
std::set< Action * > Dependencies
Definition: Action.h:75
std::vector< std::string > line
Input line which sets up the action.
Definition: Action.h:47
long int getStep() const
Return the present timestep.
Definition: Action.cpp:169
bool exists(const std::string &k) const
Check if there is a keyword with name k.
Definition: Keywords.cpp:239
bool parseNumberedVector(const std::string &key, const int no, std::vector< T > &t)
Parse a vector with a number.
Definition: Action.h:352
virtual void runFinalJobs()
RunFinalJobs This method is called once at the very end of the calculation.
Definition: Action.h:193
Main plumed object.
Definition: PlumedMain.h:71
void clearDependencies()
Clear the dependence list for this Action.
Definition: Action.cpp:153
void const char const char int double * a
Definition: Matrix.h:42
Register holding all the allowed keywords.
const Keywords & keywords
Definition: Action.h:161
std::set< FILE * >::iterator files_iterator
Definition: Action.h:140
std::string cite(const std::string &s)
Cite a paper see PlumedMain::cite.
Definition: Action.cpp:221
PlumedMain & plumed
Reference to main PlumedMain object.
Definition: Action.h:45
std::set< std::string > options
Option that you might have enabled.
Definition: Action.h:85
std::set< FILE * > files
Definition: Action.h:139