All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PlumedMain.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_PlumedMain_h
23 #define __PLUMED_core_PlumedMain_h
24 
25 #include "WithCmd.h"
26 #include <cstdio>
27 #include <string>
28 #include <vector>
29 #include <set>
30 
31 
32 // !!!!!!!!!!!!!!!!!!!!!! DANGER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
33 // THE FOLLOWING ARE DEFINITIONS WHICH ARE NECESSARY FOR DYNAMIC LOADING OF THE PLUMED KERNEL:
34 // This section should be consistent with the Plumed.h file.
35 // Since the Plumed.h file may be included in host MD codes, **NEVER** MODIFY THE CODE DOWN HERE
36 
37 /* Generic function pointer */
38 typedef void (*plumed_function_pointer)(void);
39 
40 /* Holder for function pointer */
41 typedef struct {
44 
45 // END OF DANGER
46 ////////////////////////////////////////////////////////////
47 
48 namespace PLMD {
49 
50 
51 
52 class ActionAtomistic;
53 class ActionPilot;
54 class Log;
55 class Atoms;
56 class ActionSet;
57 class DLLoader;
58 class Communicator;
59 class Stopwatch;
60 class Citations;
61 class ExchangePatterns;
62 class FileBase;
63 
64 /**
65 Main plumed object.
66 In MD engines this object is not manipulated directly but it is wrapped in
67 plumed or PLMD::Plumed objects. Its main method is cmd(),
68 which defines completely the external plumed interface.
69 It does not contain any static data.
70 */
71 class PlumedMain:
72  public WithCmd
73 {
74 public:
75 /// Communicator for plumed.
76 /// Includes all the processors used by plumed.
79 
80 private:
82 
86 /// Flag to avoid double initialization
88 /// Name of MD engine
89  std::string MDEngine;
90 /// Log stream
91  Log& log;
92 /// tools/Citations.holder
94 
95 /// Present step number.
96  long int step;
97 
98 /// Condition for plumed to be active.
99 /// At every step, PlumedMain is checking if there are Action's requiring some work.
100 /// If at least one Action requires some work, this variable is set to true.
101  bool active;
102 
103 /// Name of the input file
104  std::string plumedDat;
105 
106 /// Object containing information about atoms (such as positions,...).
107  Atoms& atoms; // atomic coordinates
108 
109 /// Set of actions found in plumed.dat file
111 
112 /// Set of Pilot actions.
113 /// These are the action the, if they are Pilot::onStep(), can trigger execution
114  std::vector<ActionPilot*> pilots;
115 
116 /// Suffix string for file opening, useful for multiple simulations in the same directory
117  std::string suffix;
118 
119 /// The total bias (=total energy of the restraints)
120  double bias;
121 
122 /// Class of possible exchange patterns, used for BIASEXCHANGE but also for future parallel tempering
124 
125 /// Set to true if on an exchange step
127 
128 /// Flag for restart
129  bool restart;
130 
131  std::set<FileBase*> files;
132  typedef std::set<FileBase*>::iterator files_iterator;
133 
134 /// Stuff to make plumed stop the MD code cleanly
135  int* stopFlag;
136  bool stopNow;
137 
138 public:
139 /// Flag to switch off virial calculation (for debug and MD codes with no barostat)
140  bool novirial;
141 
142 /// Flag to switch on detailed timers
144 
145 /// Add a citation, returning a string containing the reference number, something like "[10]"
146  std::string cite(const std::string&);
147 
148 public:
149  PlumedMain();
150 // this is to access to WithCmd versions of cmd (allowing overloading of a virtual method)
151  using WithCmd::cmd;
152 /**
153  cmd method, accessible with standard Plumed.h interface.
154  \param key The name of the command to be executed.
155  \param val The argument of the command to be executed.
156  It is called as plumed_cmd() or as PLMD::Plumed::cmd()
157  It is the interpreter for plumed commands. It basically contains the definition of the plumed interface.
158  If you want to add a new functionality to the interface between plumed
159  and an MD engine, this is the right place
160  Notice that this interface should always keep retro-compatibility
161 */
162  void cmd(const std::string&key,void*val=NULL);
163  ~PlumedMain();
164 /**
165  Read an input file.
166  \param str name of the file
167 */
168  void readInputFile(std::string str);
169 /**
170  Read an input string.
171  \param str name of the string
172 */
173  void readInputWords(const std::vector<std::string> & str);
174 
175 /**
176  Initialize the object.
177  Should be called once.
178 */
179  void init();
180 /**
181  Prepare the calculation.
182  Here it is checked which are the active Actions and communication of the relevant atoms is initiated.
183  Shortcut for prepareDependencies() + shareData()
184 */
185  void prepareCalc();
186 /**
187  Prepare the list of active Actions and needed atoms.
188  Scan the Actions to see which are active and which are not, so as to prepare a list of
189  the atoms needed at this step.
190 */
191  void prepareDependencies();
192 /**
193  Share the needed atoms.
194  In asynchronous implementations, this method sends the required atoms to all the plumed processes,
195  without waiting for the communication to complete.
196 */
197  void shareData();
198 /**
199  Perform the calculation.
200  Shortcut for waitData() + justCalculate() + justApply()
201 */
202  void performCalc();
203 /**
204  Complete PLUMED calculation.
205  Shortcut for prepareCalc() + performCalc()
206 */
207  void calc();
208 /**
209  Scatters the needed atoms.
210  In asynchronous implementations, this method waits for the communications started in shareData()
211  to be completed. Otherwise, just send around needed atoms.
212 */
213  void waitData();
214 /**
215  Perform the forward loop on active actions.
216 */
217  void justCalculate();
218 /**
219  Perform the backward loop on active actions.
220  Needed to apply the forces back.
221 */
222  void justApply();
223 /**
224  If there are calculations that need to be done at the very end of the calculations this
225  makes sures they are done
226 */
228 /// Reference to atoms object
229  Atoms& getAtoms();
230 /// Reference to the list of Action's
231  const ActionSet & getActionSet()const;
232 /// Referenge to the log stream
233  Log & getLog();
234 /// Return the number of the step
235  long int getStep()const{return step;}
236 /// Stop the run
237  void exit(int c=0);
238 /// Load a shared library
239  void load(const std::string&);
240 /// Get the suffix string
241  const std::string & getSuffix()const;
242 /// Set the suffix string
243  void setSuffix(const std::string&);
244 /// get the value of the bias
245  double getBias()const;
246 /// Opens a file.
247 /// Similar to plain fopen, but, if it finds an error in opening the file, it also tries with
248 /// path+suffix. This trick is useful for multiple replica simulations.
249  FILE* fopen(const char *path, const char *mode);
250 /// Closes a file opened with PlumedMain::fopen()
251  int fclose(FILE*fp);
252 /// Insert a file
253  void insertFile(FileBase&);
254 /// Erase a file
255  void eraseFile(FileBase&);
256 /// Flush all files
257  void fflush();
258 /// Check if restarting
259  bool getRestart()const;
260 /// Set restart flag
261  void setRestart(bool f){restart=f;}
262 /// Set exchangeStep flag
263  void setExchangeStep(bool f);
264 /// Get exchangeStep flag
265  bool getExchangeStep()const;
266 /// Stop the calculation cleanly (both the MD code and plumed)
267  void stop();
268 /// Enforce active flag.
269 /// This is a (bit dirty) hack to solve a bug. When there is no active ActionPilot,
270 /// several shortcuts are used. However, these shortcuts can block GREX module.
271 /// This function allows to enforce active plumed when doing exchanges,
272 /// thus fixing the bug.
273  void resetActive(bool active);
274 
275 /// Access to exchange patterns
277 };
278 
279 /////
280 // FAST INLINE METHODS:
281 
282 inline
284  return actionSet;
285 }
286 
287 inline
289  return atoms;
290 }
291 
292 inline
293 const std::string & PlumedMain::getSuffix()const{
294  return suffix;
295 }
296 
297 inline
298 void PlumedMain::setSuffix(const std::string&s){
299  suffix=s;
300 }
301 
302 inline
304  return restart;
305 }
306 
307 inline
309  exchangeStep=s;
310 }
311 
312 inline
314  return exchangeStep;
315 }
316 
317 inline
318 void PlumedMain::resetActive(bool active){
319  this->active=active;
320 }
321 
322 }
323 
324 #endif
325 
long int step
Present step number.
Definition: PlumedMain.h:96
WithCmd * cltool
Definition: PlumedMain.h:83
void init()
Initialize the object.
Definition: PlumedMain.cpp:349
std::string plumedDat
Name of the input file.
Definition: PlumedMain.h:104
Class taking care of dynamic loading.
Definition: DLLoader.h:40
void setExchangeStep(bool f)
Set exchangeStep flag.
Definition: PlumedMain.h:308
std::vector containing the sequence of Action to be done.
Definition: ActionSet.h:38
bool novirial
Flag to switch off virial calculation (for debug and MD codes with no barostat)
Definition: PlumedMain.h:140
Log & log
Log stream.
Definition: PlumedMain.h:91
std::vector< ActionPilot * > pilots
Set of Pilot actions.
Definition: PlumedMain.h:114
void setRestart(bool f)
Set restart flag.
Definition: PlumedMain.h:261
bool getRestart() const
Check if restarting.
Definition: PlumedMain.h:303
void waitData()
Scatters the needed atoms.
Definition: PlumedMain.cpp:500
Communicator & multi_sim_comm
Definition: PlumedMain.h:78
void load(const std::string &)
Load a shared library.
Definition: PlumedMain.cpp:601
Class containing wrappers to MPI.
Definition: Communicator.h:44
void stop()
Stop the calculation cleanly (both the MD code and plumed)
Definition: PlumedMain.cpp:670
Class containing atom related quantities from the MD code.
Definition: Atoms.h:45
double bias
The total bias (=total energy of the restraints)
Definition: PlumedMain.h:120
bool getExchangeStep() const
Get exchangeStep flag.
Definition: PlumedMain.h:313
Atoms & getAtoms()
Reference to atoms object.
Definition: PlumedMain.h:288
int * stopFlag
Stuff to make plumed stop the MD code cleanly.
Definition: PlumedMain.h:135
Class containing the log stream.
Definition: Log.h:35
void justCalculate()
Perform the forward loop on active actions.
Definition: PlumedMain.cpp:508
void justApply()
Perform the backward loop on active actions.
Definition: PlumedMain.cpp:548
Holder for function pointer.
Definition: PlumedMain.h:41
void runJobsAtEndOfCalculation()
If there are calculations that need to be done at the very end of the calculations this makes sures t...
Definition: PlumedMain.cpp:674
DLLoader & dlloader
Definition: PlumedMain.h:81
void eraseFile(FileBase &)
Erase a file.
Definition: PlumedMain.cpp:666
void prepareDependencies()
Prepare the list of active Actions and needed atoms.
Definition: PlumedMain.cpp:449
WithCmd * grex
Definition: PlumedMain.h:85
void readInputWords(const std::vector< std::string > &str)
Read an input string.
Definition: PlumedMain.cpp:393
void exit(int c=0)
Stop the run.
Definition: PlumedMain.cpp:422
Base for classes with cmd() method.
Definition: WithCmd.h:34
void fflush()
Flush all files.
Definition: PlumedMain.cpp:656
bool initialized
Flag to avoid double initialization.
Definition: PlumedMain.h:87
void cmd(const std::string &key, const void *val)
Const val version, which indeed just overrides the const and call the virtual method.
Definition: WithCmd.h:44
void setSuffix(const std::string &)
Set the suffix string.
Definition: PlumedMain.h:298
Citations & citations
tools/Citations.holder
Definition: PlumedMain.h:93
Atoms & atoms
Object containing information about atoms (such as positions,...).
Definition: PlumedMain.h:107
bool restart
Flag for restart.
Definition: PlumedMain.h:129
void insertFile(FileBase &)
Insert a file.
Definition: PlumedMain.cpp:662
Class taking care of bibliography.
Definition: Citations.h:54
std::set< FileBase * >::iterator files_iterator
Definition: PlumedMain.h:132
void calc()
Complete PLUMED calculation.
Definition: PlumedMain.cpp:434
bool active
Condition for plumed to be active.
Definition: PlumedMain.h:101
void prepareCalc()
Prepare the calculation.
Definition: PlumedMain.cpp:439
Communicator & comm
Communicator for plumed.
Definition: PlumedMain.h:77
void readInputFile(std::string str)
Read an input file.
Definition: PlumedMain.cpp:378
FILE * fopen(const char *path, const char *mode)
Opens a file.
Definition: PlumedMain.cpp:637
ActionSet & actionSet
Set of actions found in plumed.dat file.
Definition: PlumedMain.h:110
const ActionSet & getActionSet() const
Reference to the list of Action's.
Definition: PlumedMain.h:283
double getBias() const
get the value of the bias
Definition: PlumedMain.cpp:633
std::set< FileBase * > files
Definition: PlumedMain.h:131
plumed_function_pointer p
Definition: PlumedMain.h:42
long int getStep() const
Return the number of the step.
Definition: PlumedMain.h:235
const std::string & getSuffix() const
Get the suffix string.
Definition: PlumedMain.h:293
void cmd(const std::string &key, void *val=NULL)
cmd method, accessible with standard Plumed.h interface.
Definition: PlumedMain.cpp:104
int fclose(FILE *fp)
Closes a file opened with PlumedMain::fopen()
Definition: PlumedMain.cpp:648
bool detailedTimers
Flag to switch on detailed timers.
Definition: PlumedMain.h:143
Main plumed object.
Definition: PlumedMain.h:71
std::string suffix
Suffix string for file opening, useful for multiple simulations in the same directory.
Definition: PlumedMain.h:117
Class implementing stopwatch to time execution.
Definition: Stopwatch.h:98
void shareData()
Share the needed atoms.
Definition: PlumedMain.cpp:486
void performCalc()
Perform the calculation.
Definition: PlumedMain.cpp:494
void resetActive(bool active)
Enforce active flag.
Definition: PlumedMain.h:318
Base class for dealing with files.
Definition: FileBase.h:39
std::string cite(const std::string &)
Add a citation, returning a string containing the reference number, something like "[10]"...
Definition: PlumedMain.cpp:652
ExchangePatterns & getExchangePatterns()
Access to exchange patterns.
Definition: PlumedMain.h:276
Stopwatch & stopwatch
Definition: PlumedMain.h:84
ExchangePatterns & exchangePatterns
Class of possible exchange patterns, used for BIASEXCHANGE but also for future parallel tempering...
Definition: PlumedMain.h:123
Log & getLog()
Referenge to the log stream.
Definition: PlumedMain.cpp:426
std::string MDEngine
Name of MD engine.
Definition: PlumedMain.h:89
void(* plumed_function_pointer)(void)
Definition: PlumedMain.h:38
bool exchangeStep
Set to true if on an exchange step.
Definition: PlumedMain.h:126