Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2019 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.org for more information.
6 :
7 : This file is part of plumed, version 2.
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 : #include <stack>
31 : #include <map>
32 :
33 : // !!!!!!!!!!!!!!!!!!!!!! DANGER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
34 : // THE FOLLOWING ARE DEFINITIONS WHICH ARE NECESSARY FOR DYNAMIC LOADING OF THE PLUMED KERNEL:
35 : // This section should be consistent with the Plumed.h file.
36 : // Since the Plumed.h file may be included in host MD codes, **NEVER** MODIFY THE CODE DOWN HERE
37 :
38 : /* Generic function pointer */
39 : typedef void (*plumed_function_pointer)(void);
40 :
41 : /* Holder for function pointer */
42 : typedef struct {
43 : plumed_function_pointer p;
44 : } plumed_function_holder;
45 :
46 : // END OF DANGER
47 : ////////////////////////////////////////////////////////////
48 :
49 : namespace PLMD {
50 :
51 :
52 :
53 : class ActionAtomistic;
54 : class ActionPilot;
55 : class Log;
56 : class Atoms;
57 : class ActionSet;
58 : class DLLoader;
59 : class Communicator;
60 : class Stopwatch;
61 : class Citations;
62 : class ExchangePatterns;
63 : class FileBase;
64 :
65 : /**
66 : Main plumed object.
67 : In MD engines this object is not manipulated directly but it is wrapped in
68 : plumed or PLMD::Plumed objects. Its main method is cmd(),
69 : which defines completely the external plumed interface.
70 : It does not contain any static data.
71 : */
72 : class PlumedMain:
73 : public WithCmd
74 : {
75 : public:
76 : /// Communicator for plumed.
77 : /// Includes all the processors used by plumed.
78 : Communicator&comm;
79 : Communicator&multi_sim_comm;
80 :
81 : private:
82 : DLLoader& dlloader;
83 :
84 : WithCmd* cltool;
85 : Stopwatch& stopwatch;
86 : WithCmd* grex;
87 : /// Flag to avoid double initialization
88 : bool initialized;
89 : /// Name of MD engine
90 : std::string MDEngine;
91 : /// Log stream
92 : Log& log;
93 : /// tools/Citations.holder
94 : Citations& citations;
95 :
96 : /// Present step number.
97 : long int step;
98 :
99 : /// Condition for plumed to be active.
100 : /// At every step, PlumedMain is checking if there are Action's requiring some work.
101 : /// If at least one Action requires some work, this variable is set to true.
102 : bool active;
103 :
104 : /// Name of the input file
105 : std::string plumedDat;
106 :
107 : /// End of input file.
108 : /// Set to true to terminate reading
109 : bool endPlumed;
110 :
111 : /// Object containing information about atoms (such as positions,...).
112 : Atoms& atoms; // atomic coordinates
113 :
114 : /// Set of actions found in plumed.dat file
115 : ActionSet& actionSet;
116 :
117 : /// Set of Pilot actions.
118 : /// These are the action the, if they are Pilot::onStep(), can trigger execution
119 : std::vector<ActionPilot*> pilots;
120 :
121 : /// Suffix string for file opening, useful for multiple simulations in the same directory
122 : std::string suffix;
123 :
124 : /// The total bias (=total energy of the restraints)
125 : double bias;
126 :
127 : /// The total work.
128 : /// This computed by accumulating the change in external potentials.
129 : double work;
130 :
131 : /// Class of possible exchange patterns, used for BIASEXCHANGE but also for future parallel tempering
132 : ExchangePatterns& exchangePatterns;
133 :
134 : /// Set to true if on an exchange step
135 : bool exchangeStep;
136 :
137 : /// Flag for restart
138 : bool restart;
139 :
140 : /// Flag for checkpointig
141 : bool doCheckPoint;
142 :
143 : std::set<FileBase*> files;
144 :
145 : /// Stuff to make plumed stop the MD code cleanly
146 : int* stopFlag;
147 : bool stopNow;
148 :
149 : /// Stack for update flags.
150 : /// Store information used in class \ref generic::UpdateIf
151 : std::stack<bool> updateFlags;
152 :
153 : public:
154 : /// Flag to switch off virial calculation (for debug and MD codes with no barostat)
155 : bool novirial;
156 :
157 : /// Flag to switch on detailed timers
158 : bool detailedTimers;
159 :
160 : /// Generic map string -> double
161 : /// intended to pass information across Actions
162 : std::map<std::string,double> passMap;
163 :
164 : /// Add a citation, returning a string containing the reference number, something like "[10]"
165 : std::string cite(const std::string&);
166 :
167 : /// Get number of threads that can be used by openmp
168 : unsigned getNumThreads()const;
169 :
170 : /// Get a reasonable number of threads so as to access to an array of size s located at x
171 : template<typename T>
172 : unsigned getGoodNumThreads(const T*x,unsigned s)const;
173 :
174 : /// Get a reasonable number of threads so as to access to vector v;
175 : template<typename T>
176 : unsigned getGoodNumThreads(const std::vector<T> & v)const;
177 :
178 : public:
179 : PlumedMain();
180 : // this is to access to WithCmd versions of cmd (allowing overloading of a virtual method)
181 : using WithCmd::cmd;
182 : /**
183 : cmd method, accessible with standard Plumed.h interface.
184 : \param key The name of the command to be executed.
185 : \param val The argument of the command to be executed.
186 : It is called as plumed_cmd() or as PLMD::Plumed::cmd()
187 : It is the interpreter for plumed commands. It basically contains the definition of the plumed interface.
188 : If you want to add a new functionality to the interface between plumed
189 : and an MD engine, this is the right place
190 : Notice that this interface should always keep retro-compatibility
191 : */
192 : void cmd(const std::string&key,void*val=NULL);
193 : ~PlumedMain();
194 : /**
195 : Read an input file.
196 : \param str name of the file
197 : */
198 : void readInputFile(std::string str);
199 : /**
200 : Read an input string.
201 : \param str name of the string
202 : */
203 : void readInputWords(const std::vector<std::string> & str);
204 :
205 : /**
206 : Read an input string.
207 : \param str name of the string
208 : At variance with readInputWords(), this is splitting the string into words
209 : */
210 : void readInputLine(const std::string & str);
211 :
212 : /**
213 : Initialize the object.
214 : Should be called once.
215 : */
216 : void init();
217 : /**
218 : Prepare the calculation.
219 : Here it is checked which are the active Actions and communication of the relevant atoms is initiated.
220 : Shortcut for prepareDependencies() + shareData()
221 : */
222 : void prepareCalc();
223 : /**
224 : Prepare the list of active Actions and needed atoms.
225 : Scan the Actions to see which are active and which are not, so as to prepare a list of
226 : the atoms needed at this step.
227 : */
228 : void prepareDependencies();
229 : /**
230 : Share the needed atoms.
231 : In asynchronous implementations, this method sends the required atoms to all the plumed processes,
232 : without waiting for the communication to complete.
233 : */
234 : void shareData();
235 : /**
236 : Perform the calculation.
237 : Shortcut for waitData() + justCalculate() + justApply().
238 : Equivalently: waitData() + justCalculate() + backwardPropagate() + update().
239 : */
240 : void performCalc();
241 : /**
242 : Perform the calculation without update()
243 : Shortcut for: waitData() + justCalculate() + backwardPropagate()
244 : */
245 : void performCalcNoUpdate();
246 : /**
247 : Complete PLUMED calculation.
248 : Shortcut for prepareCalc() + performCalc()
249 : */
250 : void calc();
251 : /**
252 : Scatters the needed atoms.
253 : In asynchronous implementations, this method waits for the communications started in shareData()
254 : to be completed. Otherwise, just send around needed atoms.
255 : */
256 : void waitData();
257 : /**
258 : Perform the forward loop on active actions.
259 : */
260 : void justCalculate();
261 : /**
262 : Backward propagate and update.
263 : Shortcut for backwardPropagate() + update()
264 : I leave it here for backward compatibility
265 : */
266 : void justApply();
267 : /**
268 : Perform the backward loop on active actions.
269 : Needed to apply the forces back.
270 : */
271 : void backwardPropagate();
272 : /**
273 : Call the update() method.
274 : */
275 : void update();
276 : /**
277 : If there are calculations that need to be done at the very end of the calculations this
278 : makes sures they are done
279 : */
280 : void runJobsAtEndOfCalculation();
281 : /// Reference to atoms object
282 : Atoms& getAtoms();
283 : /// Reference to the list of Action's
284 : const ActionSet & getActionSet()const;
285 : /// Referenge to the log stream
286 : Log & getLog();
287 : /// Return the number of the step
288 1146902 : long int getStep()const {return step;}
289 : /// Stop the run
290 : void exit(int c=0);
291 : /// Load a shared library
292 : void load(const std::string&);
293 : /// Get the suffix string
294 : const std::string & getSuffix()const;
295 : /// Set the suffix string
296 : void setSuffix(const std::string&);
297 : /// get the value of the bias
298 : double getBias()const;
299 : /// get the value of the work
300 : double getWork()const;
301 : /// Opens a file.
302 : /// Similar to plain fopen, but, if it finds an error in opening the file, it also tries with
303 : /// path+suffix. This trick is useful for multiple replica simulations.
304 : FILE* fopen(const char *path, const char *mode);
305 : /// Closes a file opened with PlumedMain::fopen()
306 : int fclose(FILE*fp);
307 : /// Insert a file
308 : void insertFile(FileBase&);
309 : /// Erase a file
310 : void eraseFile(FileBase&);
311 : /// Flush all files
312 : void fflush();
313 : /// Check if restarting
314 : bool getRestart()const;
315 : /// Set restart flag
316 38 : void setRestart(bool f) {restart=f;}
317 : /// Check if checkpointing
318 : bool getCPT()const;
319 : /// Set exchangeStep flag
320 : void setExchangeStep(bool f);
321 : /// Get exchangeStep flag
322 : bool getExchangeStep()const;
323 : /// Stop the calculation cleanly (both the MD code and plumed)
324 : void stop();
325 : /// Enforce active flag.
326 : /// This is a (bit dirty) hack to solve a bug. When there is no active ActionPilot,
327 : /// several shortcuts are used. However, these shortcuts can block GREX module.
328 : /// This function allows to enforce active plumed when doing exchanges,
329 : /// thus fixing the bug.
330 : void resetActive(bool active);
331 :
332 : /// Access to exchange patterns
333 0 : ExchangePatterns& getExchangePatterns() {return exchangePatterns;}
334 :
335 : /// Push a state to update flags
336 : void updateFlagsPush(bool);
337 : /// Pop a state from update flags
338 : void updateFlagsPop();
339 : /// Get top of update flags
340 : bool updateFlagsTop();
341 : /// Set end of input file
342 : void setEndPlumed();
343 : };
344 :
345 : /////
346 : // FAST INLINE METHODS:
347 :
348 : inline
349 : const ActionSet & PlumedMain::getActionSet()const {
350 37469 : return actionSet;
351 : }
352 :
353 : inline
354 : Atoms& PlumedMain::getAtoms() {
355 951401 : return atoms;
356 : }
357 :
358 : inline
359 : const std::string & PlumedMain::getSuffix()const {
360 : return suffix;
361 : }
362 :
363 : inline
364 : void PlumedMain::setSuffix(const std::string&s) {
365 309 : suffix=s;
366 : }
367 :
368 : inline
369 : bool PlumedMain::getRestart()const {
370 5316 : return restart;
371 : }
372 :
373 : inline
374 : bool PlumedMain::getCPT()const {
375 6050 : return doCheckPoint;
376 : }
377 :
378 : inline
379 : void PlumedMain::setExchangeStep(bool s) {
380 168 : exchangeStep=s;
381 : }
382 :
383 : inline
384 : bool PlumedMain::getExchangeStep()const {
385 13718 : return exchangeStep;
386 : }
387 :
388 : inline
389 : void PlumedMain::resetActive(bool active) {
390 84 : this->active=active;
391 : }
392 :
393 : inline
394 : void PlumedMain::updateFlagsPush(bool on) {
395 : updateFlags.push(on);
396 : }
397 :
398 : inline
399 : void PlumedMain::updateFlagsPop() {
400 : updateFlags.pop();
401 : }
402 :
403 : inline
404 : bool PlumedMain::updateFlagsTop() {
405 186629 : return updateFlags.top();
406 : }
407 :
408 : inline
409 : void PlumedMain::setEndPlumed() {
410 190 : endPlumed=true;
411 : }
412 :
413 : }
414 :
415 : #endif
416 :
|