All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Action.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 "Action.h"
23 #include "ActionWithValue.h"
24 #include "PlumedMain.h"
25 #include "tools/Log.h"
26 #include "tools/Exception.h"
27 #include "Atoms.h"
28 #include "ActionSet.h"
29 #include <iostream>
30 
31 namespace PLMD{
32 
34 
35 ActionOptions::ActionOptions(PlumedMain&p,const std::vector<std::string>&l):
36 plumed(p),
37 line(l),
38 keys(emptyKeys)
39 {
40 }
41 
43 plumed(ao.plumed),
44 line(ao.line),
45 keys(keys)
46 {
47 }
48 
50  plumed_assert( keys.size()==0 );
51  keys.add( "hidden", "LABEL", "a label for the action so that its output can be referenced in the input to other actions. Actions with scalar output are referenced using their label only. Actions with vector output must have a separate label for every component. Individual componets are then refered to using label.component" );
52 }
53 
55  name(ao.line[0]),
56  line(ao.line),
57  active(false),
58  plumed(ao.plumed),
59  log(plumed.getLog()),
60  comm(plumed.comm),
61  multi_sim_comm(plumed.multi_sim_comm),
62  keywords(ao.keys)
63 {
64  line.erase(line.begin());
65  log.printf("Action %s\n",name.c_str());
66 
67  if ( keywords.exists("LABEL") ){ parse("LABEL",label); }
68 
69  if(label.length()==0){
70  std::string s; Tools::convert(plumed.getActionSet().size(),s);
71  label="@"+s;
72  }
73  if( plumed.getActionSet().selectWithLabel<Action*>(label) ) error("label " + label + " has been already used");
74  log.printf(" with label %s\n",label.c_str());
75 }
76 
78  if(files.size()!=0){
79  std::cerr<<"WARNING: some files open in action "+getLabel()+" where not properly closed. This could lead to data loss!!\n";
80  }
81 }
82 
83 FILE* Action::fopen(const char *path, const char *mode){
84  bool write(false);
85  for(const char*p=mode;*p;p++) if(*p=='w' || *p=='a' || *p=='+') write=true;
86  FILE* fp;
87  if(write && comm.Get_rank()!=0) fp=plumed.fopen("/dev/null",mode);
88  else fp=plumed.fopen(path,mode);
89  files.insert(fp);
90  return fp;
91 }
92 
93 int Action::fclose(FILE*fp){
94  files.erase(fp);
95  return plumed.fclose(fp);
96 }
97 
99  for(files_iterator p=files.begin();p!=files.end();++p){
100  std::fflush((*p));
101  }
102 }
103 
104 void Action::parseFlag(const std::string&key,bool & t){
105  // Check keyword has been registered
106  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
107  // Check keyword is a flag
108  if(!keywords.style(key,"nohtml")){
109  plumed_massert(keywords.style(key,"flag"), "keyword " + key + " is not a flag");
110  }
111 
112  // Read in the flag otherwise get the default value from the keywords object
113  if(!Tools::parseFlag(line,key,t)){
114  if( keywords.style(key,"nohtml") ){
115  t=false;
116  } else if ( !keywords.getLogicalDefault(key,t) ){
117  log.printf("ERROR in action %s with label %s : flag %s has no default",name.c_str(),label.c_str(),key.c_str() );
118  plumed_error();
119  }
120  }
121 }
122 
124  after.insert(action);
125 }
126 
128 // preparation step is called only the first time an Action is activated.
129 // since it could change its dependences (e.g. in an ActionAtomistic which is
130 // accessing to a virtual atom), this is done just before dependencies are
131 // activated
132  if(!active){
133  this->unlockRequests();
134  prepare();
135  this->lockRequests();
136  }
137  for(Dependencies::iterator p=after.begin();p!=after.end();++p) (*p)->activate();
138  active=true;
139 }
140 
141 void Action::setOption(const std::string &s){
142 // This overloads the action and activate some options
143  options.insert(s);
144  for(Dependencies::iterator p=after.begin();p!=after.end();++p) (*p)->setOption(s);
145 }
146 
148 // This overloads the action and activate some options
149  options.clear();
150 }
151 
152 
154  after.clear();
155 }
156 
157 std::string Action::getDocumentation()const{
158  return std::string("UNDOCUMENTED ACTION");
159 }
160 
162  if(!line.empty()){
163  std::string msg="cannot understand the following words from the input line : ";
164  for(unsigned i=0;i<line.size();i++) msg = msg + line[i] + ", ";
165  error(msg);
166  }
167 }
168 
169 long int Action::getStep()const{
170  return plumed.getStep();
171 }
172 
173 double Action::getTime()const{
174  return plumed.getAtoms().getTimeStep()*getStep();
175 }
176 
177 double Action::getTimeStep()const{
178  return plumed.getAtoms().getTimeStep();
179 }
180 
181 
182 
183 void Action::exit(int c){
184  plumed.exit(c);
185 }
186 
188  plumed_merror("if you get here it means that you are trying to use numerical derivatives for a class that does not implement them");
189 }
190 
192  return;
193 }
194 
195 void Action::error( const std::string & msg ) const {
196  log.printf("ERROR in input to action %s with label %s : %s \n \n", name.c_str(), label.c_str(), msg.c_str() );
197  if( !line.empty() ) keywords.print( log );
198  plumed_merror("ERROR in input to action " + name + " with label " + label + " : " + msg );
199 }
200 
201 void Action::warning( const std::string & msg ){
202  log.printf("WARNING for action %s with label %s : %s \n", name.c_str(), label.c_str(), msg.c_str() );
203 }
204 
205 void Action::calculateFromPDB( const PDB& pdb ){
206  activate();
207  for(Dependencies::iterator p=after.begin();p!=after.end();++p){
208  ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p);
209  if(av){ av->clearInputForces(); av->clearDerivatives(); }
210  (*p)->readAtomsFromPDB( pdb );
211  (*p)->calculate();
212  }
213  readAtomsFromPDB( pdb );
214  calculate();
215 }
216 
218  return plumed.getExchangeStep();
219 }
220 
221 std::string Action::cite(const std::string&s){
222  return plumed.cite(s);
223 }
224 
225 
226 }
227 
static Keywords emptyKeys
Definition: Action.h:50
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
void warning(const std::string &msg)
Issue a warning.
Definition: Action.cpp:201
static bool parseFlag(std::vector< std::string > &line, const std::string &key, bool &val)
Find a keyword without arguments on the input line.
Definition: Tools.h:157
void exit(int c=0)
Exit with error code c.
Definition: Action.cpp:183
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
virtual void lockRequests()
Definition: Action.h:172
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
void add(const std::string &t, const std::string &k, const std::string &d)
Add a new keyword of type t with name k and description d.
Definition: Keywords.cpp:167
Communicator & comm
Definition: Action.h:158
virtual void calculate()=0
Calculate an Action.
std::vector< std::string > line
Directive line.
Definition: Action.h:72
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 ...
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
FILE * fopen(const char *path, const char *mode)
Opens a file.
Definition: Action.cpp:83
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
int Get_rank() const
Obtain the rank of the present process.
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void clearInputForces()
Clear the forces on the values.
void print(Log &log) const
Print the documentation to the log file (used by PLMD::Action::error)
Definition: Keywords.cpp:461
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
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
virtual ~Action()
Destructor.
Definition: Action.cpp:77
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
Minimalistic pdb parser.
Definition: PDB.h:38
int fclose(FILE *fp)
Closes a file opened with Action::fclose().
Definition: Action.cpp:93
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
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
virtual void clearDerivatives()
Clear the derivatives of values wrt parameters.
Main plumed object.
Definition: PlumedMain.h:71
Main plumed object.
Definition: Plumed.h:201
void clearDependencies()
Clear the dependence list for this Action.
Definition: Action.cpp:153
bool getLogicalDefault(std::string key, bool &def) const
find out whether flag key is on or off by default.
Definition: Keywords.cpp:534
void const char const char int double * a
Definition: Matrix.h:42
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
std::set< std::string > options
Option that you might have enabled.
Definition: Action.h:85
unsigned size() const
Return the number of defined keywords.
Definition: Keywords.cpp:230
std::set< FILE * > files
Definition: Action.h:139