Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2018 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 :
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 : /// Object containing information about atoms (such as positions,...).
108 : Atoms& atoms; // atomic coordinates
109 :
110 : /// Set of actions found in plumed.dat file
111 : ActionSet& actionSet;
112 :
113 : /// Set of Pilot actions.
114 : /// These are the action the, if they are Pilot::onStep(), can trigger execution
115 : std::vector<ActionPilot*> pilots;
116 :
117 : /// Suffix string for file opening, useful for multiple simulations in the same directory
118 : std::string suffix;
119 :
120 : /// The total bias (=total energy of the restraints)
121 : double bias;
122 :
123 : /// The total work.
124 : /// This computed by accumulating the change in external potentials.
125 : double work;
126 :
127 : /// Class of possible exchange patterns, used for BIASEXCHANGE but also for future parallel tempering
128 : ExchangePatterns& exchangePatterns;
129 :
130 : /// Set to true if on an exchange step
131 : bool exchangeStep;
132 :
133 : /// Flag for restart
134 : bool restart;
135 :
136 : /// Flag for checkpointig
137 : bool doCheckPoint;
138 :
139 : std::set<FileBase*> files;
140 : typedef std::set<FileBase*>::iterator files_iterator;
141 :
142 : /// Stuff to make plumed stop the MD code cleanly
143 : int* stopFlag;
144 : bool stopNow;
145 :
146 : /// Stack for update flags.
147 : /// Store information used in class \ref generic::UpdateIf
148 : std::stack<bool> updateFlags;
149 :
150 : public:
151 : /// Flag to switch off virial calculation (for debug and MD codes with no barostat)
152 : bool novirial;
153 :
154 : /// Flag to switch on detailed timers
155 : bool detailedTimers;
156 :
157 : /// Add a citation, returning a string containing the reference number, something like "[10]"
158 : std::string cite(const std::string&);
159 :
160 : /// Get number of threads that can be used by openmp
161 : unsigned getNumThreads()const;
162 :
163 : /// Get a reasonable number of threads so as to access to an array of size s located at x
164 : template<typename T>
165 : unsigned getGoodNumThreads(const T*x,unsigned s)const;
166 :
167 : /// Get a reasonable number of threads so as to access to vector v;
168 : template<typename T>
169 : unsigned getGoodNumThreads(const std::vector<T> & v)const;
170 :
171 : public:
172 : PlumedMain();
173 : // this is to access to WithCmd versions of cmd (allowing overloading of a virtual method)
174 : using WithCmd::cmd;
175 : /**
176 : cmd method, accessible with standard Plumed.h interface.
177 : \param key The name of the command to be executed.
178 : \param val The argument of the command to be executed.
179 : It is called as plumed_cmd() or as PLMD::Plumed::cmd()
180 : It is the interpreter for plumed commands. It basically contains the definition of the plumed interface.
181 : If you want to add a new functionality to the interface between plumed
182 : and an MD engine, this is the right place
183 : Notice that this interface should always keep retro-compatibility
184 : */
185 : void cmd(const std::string&key,void*val=NULL);
186 : ~PlumedMain();
187 : /**
188 : Read an input file.
189 : \param str name of the file
190 : */
191 : void readInputFile(std::string str);
192 : /**
193 : Read an input string.
194 : \param str name of the string
195 : */
196 : void readInputWords(const std::vector<std::string> & str);
197 :
198 : /**
199 : Read an input string.
200 : \param str name of the string
201 : At variance with readInputWords(), this is splitting the string into words
202 : */
203 : void readInputLine(const std::string & str);
204 :
205 : /**
206 : Initialize the object.
207 : Should be called once.
208 : */
209 : void init();
210 : /**
211 : Prepare the calculation.
212 : Here it is checked which are the active Actions and communication of the relevant atoms is initiated.
213 : Shortcut for prepareDependencies() + shareData()
214 : */
215 : void prepareCalc();
216 : /**
217 : Prepare the list of active Actions and needed atoms.
218 : Scan the Actions to see which are active and which are not, so as to prepare a list of
219 : the atoms needed at this step.
220 : */
221 : void prepareDependencies();
222 : /**
223 : Share the needed atoms.
224 : In asynchronous implementations, this method sends the required atoms to all the plumed processes,
225 : without waiting for the communication to complete.
226 : */
227 : void shareData();
228 : /**
229 : Perform the calculation.
230 : Shortcut for waitData() + justCalculate() + justApply().
231 : Equivalently: waitData() + justCalculate() + backwardPropagate() + update().
232 : */
233 : void performCalc();
234 : /**
235 : Perform the calculation without update()
236 : Shortcut for: waitData() + justCalculate() + backwardPropagate()
237 : */
238 : void performCalcNoUpdate();
239 : /**
240 : Complete PLUMED calculation.
241 : Shortcut for prepareCalc() + performCalc()
242 : */
243 : void calc();
244 : /**
245 : Scatters the needed atoms.
246 : In asynchronous implementations, this method waits for the communications started in shareData()
247 : to be completed. Otherwise, just send around needed atoms.
248 : */
249 : void waitData();
250 : /**
251 : Perform the forward loop on active actions.
252 : */
253 : void justCalculate();
254 : /**
255 : Backward propagate and update.
256 : Shortcut for backwardPropagate() + update()
257 : I leave it here for backward compatibility
258 : */
259 : void justApply();
260 : /**
261 : Perform the backward loop on active actions.
262 : Needed to apply the forces back.
263 : */
264 : void backwardPropagate();
265 : /**
266 : Call the update() method.
267 : */
268 : void update();
269 : /**
270 : If there are calculations that need to be done at the very end of the calculations this
271 : makes sures they are done
272 : */
273 : void runJobsAtEndOfCalculation();
274 : /// Reference to atoms object
275 : Atoms& getAtoms();
276 : /// Reference to the list of Action's
277 : const ActionSet & getActionSet()const;
278 : /// Referenge to the log stream
279 : Log & getLog();
280 : /// Return the number of the step
281 785207 : long int getStep()const {return step;}
282 : /// Stop the run
283 : void exit(int c=0);
284 : /// Load a shared library
285 : void load(const std::string&);
286 : /// Get the suffix string
287 : const std::string & getSuffix()const;
288 : /// Set the suffix string
289 : void setSuffix(const std::string&);
290 : /// get the value of the bias
291 : double getBias()const;
292 : /// get the value of the work
293 : double getWork()const;
294 : /// Opens a file.
295 : /// Similar to plain fopen, but, if it finds an error in opening the file, it also tries with
296 : /// path+suffix. This trick is useful for multiple replica simulations.
297 : FILE* fopen(const char *path, const char *mode);
298 : /// Closes a file opened with PlumedMain::fopen()
299 : int fclose(FILE*fp);
300 : /// Insert a file
301 : void insertFile(FileBase&);
302 : /// Erase a file
303 : void eraseFile(FileBase&);
304 : /// Flush all files
305 : void fflush();
306 : /// Check if restarting
307 : bool getRestart()const;
308 : /// Set restart flag
309 13 : void setRestart(bool f) {restart=f;}
310 : /// Check if checkpointing
311 : bool getCPT()const;
312 : /// Set exchangeStep flag
313 : void setExchangeStep(bool f);
314 : /// Get exchangeStep flag
315 : bool getExchangeStep()const;
316 : /// Stop the calculation cleanly (both the MD code and plumed)
317 : void stop();
318 : /// Enforce active flag.
319 : /// This is a (bit dirty) hack to solve a bug. When there is no active ActionPilot,
320 : /// several shortcuts are used. However, these shortcuts can block GREX module.
321 : /// This function allows to enforce active plumed when doing exchanges,
322 : /// thus fixing the bug.
323 : void resetActive(bool active);
324 :
325 : /// Access to exchange patterns
326 0 : ExchangePatterns& getExchangePatterns() {return exchangePatterns;}
327 :
328 : /// Push a state to update flags
329 : void updateFlagsPush(bool);
330 : /// Pop a state from update flags
331 : void updateFlagsPop();
332 : /// Get top of update flags
333 : bool updateFlagsTop();
334 : };
335 :
336 : /////
337 : // FAST INLINE METHODS:
338 :
339 : inline
340 25313 : const ActionSet & PlumedMain::getActionSet()const {
341 25313 : return actionSet;
342 : }
343 :
344 : inline
345 685174 : Atoms& PlumedMain::getAtoms() {
346 685174 : return atoms;
347 : }
348 :
349 : inline
350 1616 : const std::string & PlumedMain::getSuffix()const {
351 1616 : return suffix;
352 : }
353 :
354 : inline
355 129 : void PlumedMain::setSuffix(const std::string&s) {
356 129 : suffix=s;
357 129 : }
358 :
359 : inline
360 2360 : bool PlumedMain::getRestart()const {
361 2360 : return restart;
362 : }
363 :
364 : inline
365 2427 : bool PlumedMain::getCPT()const {
366 2427 : return doCheckPoint;
367 : }
368 :
369 : inline
370 144 : void PlumedMain::setExchangeStep(bool s) {
371 144 : exchangeStep=s;
372 144 : }
373 :
374 : inline
375 7892 : bool PlumedMain::getExchangeStep()const {
376 7892 : return exchangeStep;
377 : }
378 :
379 : inline
380 72 : void PlumedMain::resetActive(bool active) {
381 72 : this->active=active;
382 72 : }
383 :
384 : inline
385 16 : void PlumedMain::updateFlagsPush(bool on) {
386 16 : updateFlags.push(on);
387 16 : }
388 :
389 : inline
390 12 : void PlumedMain::updateFlagsPop() {
391 12 : updateFlags.pop();
392 12 : }
393 :
394 : inline
395 106999 : bool PlumedMain::updateFlagsTop() {
396 106999 : return updateFlags.top();
397 : }
398 :
399 : }
400 :
401 : #endif
402 :
|