Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2023 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 "tools/ForwardDecl.h"
27 : #include <cstdio>
28 : #include <string>
29 : #include <vector>
30 : #include <set>
31 : #include <stack>
32 : #include <memory>
33 : #include <map>
34 : #include <atomic>
35 :
36 : // !!!!!!!!!!!!!!!!!!!!!! DANGER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
37 : // THE FOLLOWING ARE DEFINITIONS WHICH ARE NECESSARY FOR DYNAMIC LOADING OF THE PLUMED KERNEL:
38 : // This section should be consistent with the Plumed.h file.
39 : // Since the Plumed.h file may be included in host MD codes, **NEVER** MODIFY THE CODE DOWN HERE
40 :
41 : /* Generic function pointer */
42 : typedef void (*plumed_function_pointer)(void);
43 :
44 : /* Holder for function pointer */
45 : typedef struct {
46 : plumed_function_pointer p;
47 : } plumed_function_holder;
48 :
49 : // END OF DANGER
50 : ////////////////////////////////////////////////////////////
51 :
52 : namespace PLMD {
53 :
54 :
55 :
56 : class ActionAtomistic;
57 : class ActionPilot;
58 : class ActionForInterface;
59 : class Log;
60 : class Atoms;
61 : class ActionSet;
62 : class DLLoader;
63 : class Communicator;
64 : class Stopwatch;
65 : class Citations;
66 : class ExchangePatterns;
67 : class FileBase;
68 : class TypesafePtr;
69 : class IFile;
70 : class Units;
71 : class Keywords;
72 : class DataPassingTools;
73 :
74 : /**
75 : Main plumed object.
76 : In MD engines this object is not manipulated directly but it is wrapped in
77 : plumed or PLMD::Plumed objects. Its main method is cmd(),
78 : which defines completely the external plumed interface.
79 : It does not contain any static data.
80 : */
81 : class PlumedMain:
82 : public WithCmd
83 : {
84 : /// Pointers to files opened in actions associated to this object.
85 : /// Notice that with the current implementation this should be at the top of this
86 : /// structure. Indeed, this should be destroyed *after* all the actions allocated
87 : /// in this PlumedMain object have been destroyed.
88 : std::set<FileBase*> files;
89 : /// Forward declaration.
90 : ForwardDecl<Communicator> comm_fwd;
91 : public:
92 : /// Communicator for plumed.
93 : /// Includes all the processors used by plumed.
94 : Communicator&comm=*comm_fwd;
95 :
96 : private:
97 : class DeprecatedAtoms {
98 : private:
99 : PlumedMain & plumed;
100 : public:
101 806698 : explicit DeprecatedAtoms(PlumedMain& p): plumed(p) {}
102 : [[deprecated("Use Action::getKBoltzmann().")]]
103 : double getKBoltzmann() const ;
104 : [[deprecated("Use Action::getkBT() N.B. this function also reads the TEMP keyword from the input for you.")]]
105 : double getKbT() const ;
106 : [[deprecated]]
107 : int getNatoms() const ;
108 : [[deprecated("Use Action::usingNaturalUnits().")]]
109 : bool usingNaturalUnits() const ;
110 : [[deprecated]]
111 : void setCollectEnergy(bool b) const;
112 : [[deprecated]]
113 : double getEnergy() const ;
114 : };
115 : /// Forward declaration.
116 : ForwardDecl<DeprecatedAtoms> datoms_fwd;
117 : /// Object containing old bits of atoms that are used by many folks
118 : DeprecatedAtoms& datoms=*datoms_fwd;
119 : /// Forward declaration.
120 : ForwardDecl<Communicator> multi_sim_comm_fwd;
121 : public:
122 : DeprecatedAtoms& getAtoms();
123 : Communicator&multi_sim_comm=*multi_sim_comm_fwd;
124 :
125 : private:
126 : /// Error handler.
127 : /// Pointer to a function that is called an exception thrown within
128 : /// the library is about to leave the library.
129 : /// Can be used to remap exceptions in case the plumed wrapper was compiled
130 : /// with a different version of the C++ standard library.
131 : /// Should only be called from \ref plumed_plumedmain_cmd().
132 : typedef struct {
133 : void* ptr;
134 : void(*handler)(void* ptr,int code,const char*);
135 : } plumed_error_handler;
136 :
137 : plumed_error_handler error_handler= {NULL,NULL};
138 :
139 : bool nestedExceptions=false;
140 :
141 : /// Forward declaration.
142 : ForwardDecl<DLLoader> dlloader_fwd;
143 : DLLoader& dlloader=*dlloader_fwd;
144 :
145 : std::unique_ptr<WithCmd> cltool;
146 :
147 : std::unique_ptr<WithCmd> grex;
148 : /// Flag to avoid double initialization
149 : bool initialized=false;
150 : /// Name of MD engine
151 : std::string MDEngine{"mdcode"};
152 :
153 : /// Forward declaration.
154 : ForwardDecl<Log> log_fwd;
155 : /// Log stream
156 : Log& log=*log_fwd;
157 :
158 : /// Forward declaration.
159 : /// Should be placed after log since its constructor takes a log reference as an argument.
160 : ForwardDecl<Stopwatch> stopwatch_fwd;
161 : Stopwatch& stopwatch=*stopwatch_fwd;
162 :
163 : /// Forward declaration.
164 : ForwardDecl<Citations> citations_fwd;
165 : /// tools/Citations.holder
166 : Citations& citations=*citations_fwd;
167 :
168 : /// Present step number.
169 : long long int step=0;
170 :
171 : /// Condition for plumed to be active.
172 : /// At every step, PlumedMain is checking if there are Action's requiring some work.
173 : /// If at least one Action requires some work, this variable is set to true.
174 : bool active=false;
175 :
176 : /// Name of the input file
177 : std::string plumedDat;
178 :
179 : /// End of input file.
180 : /// Set to true to terminate reading
181 : bool endPlumed=false;
182 :
183 : /// Forward declaration.
184 : ForwardDecl<ActionSet> actionSet_fwd;
185 : /// Set of actions found in plumed.dat file
186 : ActionSet& actionSet=*actionSet_fwd;
187 :
188 : /// These are tools to pass data to PLUMED
189 : std::unique_ptr<DataPassingTools> passtools;
190 :
191 : /// Vector of actions that are passed data from the MD code
192 : std::vector<ActionForInterface*> inputs;
193 :
194 : /// Set of Pilot actions.
195 : /// These are the action the, if they are Pilot::onStep(), can trigger execution
196 : std::vector<ActionPilot*> pilots;
197 :
198 : /// Suffix string for file opening, useful for multiple simulations in the same directory
199 : std::string suffix;
200 :
201 : /// The total bias (=total energy of the restraints)
202 : double bias=0.0;
203 :
204 : /// The total work.
205 : /// This computed by accumulating the change in external potentials.
206 : double work=0.0;
207 :
208 : /// Forward declaration.
209 : ForwardDecl<ExchangePatterns> exchangePatterns_fwd;
210 : /// Class of possible exchange patterns, used for BIASEXCHANGE but also for future parallel tempering
211 : ExchangePatterns& exchangePatterns=*exchangePatterns_fwd;
212 :
213 : /// Set to true if on an exchange step
214 : bool exchangeStep=false;
215 :
216 : /// Flag for restart
217 : bool restart=false;
218 :
219 : /// Flag for checkpointig
220 : bool doCheckPoint=false;
221 :
222 : /// A string that holds the name of the action that gets the energy from the MD
223 : /// code. Set empty if energy is not used.
224 : std::string name_of_energy{""};
225 :
226 : /// This sets up the values that are set from the MD code
227 : void startStep();
228 :
229 : /// This sets up the vector that contains the interface to the MD code
230 : void setupInterfaceActions();
231 :
232 : /// Flag for parse only mode -- basically just forces restart to turn off
233 : bool doParseOnly=false;
234 :
235 : private:
236 : /// Forward declaration.
237 : ForwardDecl<TypesafePtr> stopFlag_fwd;
238 : public:
239 : /// Stuff to make plumed stop the MD code cleanly
240 : TypesafePtr& stopFlag=*stopFlag_fwd;
241 : bool stopNow=false;
242 :
243 : /// Stack for update flags.
244 : /// Store information used in class \ref generic::UpdateIf
245 : std::stack<bool> updateFlags;
246 :
247 : public:
248 : /// This determines if the user has created a value to hold the quantity that is being passed
249 : bool valueExists( const std::string& name ) const ;
250 :
251 : /// This sets the the value with a particular name to the pointer to the data in the MD code
252 : void setInputValue( const std::string& name, const unsigned& start, const unsigned& stride, const TypesafePtr & val );
253 :
254 : /// This sets the the forces with a particular name to the pointer to the data in the MD code
255 : void setInputForce( const std::string& name, const TypesafePtr & val );
256 :
257 : /// This updates the units of the input quantities
258 : void setUnits( const bool& natural, const Units& u );
259 :
260 : /// Flag to switch off virial calculation (for debug and MD codes with no barostat)
261 : bool novirial=false;
262 :
263 : /// Flag to switch on detailed timers
264 : bool detailedTimers=false;
265 :
266 : /// GpuDevice Identifier
267 : int gpuDeviceId=-1;
268 :
269 : /// Generic map string -> double
270 : /// intended to pass information across Actions
271 : std::map<std::string,double> passMap;
272 :
273 : /// Add a citation, returning a string containing the reference number, something like "[10]"
274 : std::string cite(const std::string&);
275 :
276 : /// Get number of threads that can be used by openmp
277 : unsigned getNumThreads()const;
278 :
279 : /// Get a reasonable number of threads so as to access to an array of size s located at x
280 : template<typename T>
281 : unsigned getGoodNumThreads(const T*x,unsigned s)const;
282 :
283 : /// Get a reasonable number of threads so as to access to vector v;
284 : template<typename T>
285 : unsigned getGoodNumThreads(const std::vector<T> & v)const;
286 :
287 : public:
288 : PlumedMain();
289 : // this is to access to WithCmd versions of cmd (allowing overloading of a virtual method)
290 : using WithCmd::cmd;
291 : /**
292 : cmd method, accessible with standard Plumed.h interface.
293 : \param key The name of the command to be executed.
294 : \param val The argument of the command to be executed.
295 : It is called as plumed_cmd() or as PLMD::Plumed::cmd()
296 : It is the interpreter for plumed commands. It basically contains the definition of the plumed interface.
297 : If you want to add a new functionality to the interface between plumed
298 : and an MD engine, this is the right place
299 : Notice that this interface should always keep retro-compatibility
300 : */
301 : void cmd(std::string_view key,const TypesafePtr & val) override;
302 : ~PlumedMain();
303 : /**
304 : Turn on parse only mode to deactivate restart in all actions.
305 : This is only used by plumed driver --parse-only
306 : */
307 : void activateParseOnlyMode();
308 : /**
309 : This checks if parse only mode is active and turns off any restart.
310 : */
311 : bool parseOnlyMode() const ;
312 : /**
313 : Read an input file.
314 : \param str name of the file
315 : */
316 : void readInputFile(const std::string & str);
317 : /**
318 : Read an input file.
319 : \param ifile
320 : */
321 : void readInputFile(IFile & ifile);
322 : /**
323 : Read an input string.
324 : \param str name of the string
325 : */
326 : void readInputWords(const std::vector<std::string> & str, const bool& before_init);
327 :
328 : /**
329 : Read an input string.
330 : \param str name of the string
331 : At variance with readInputWords(), this is splitting the string into words
332 : */
333 : void readInputLine(const std::string & str, const bool& before_init=false);
334 :
335 : /**
336 : Read an input buffer.
337 : \param str name of the string
338 : Same as readInputFile, but first write str on a temporary file and then read
339 : that files. At variance with readInputLine, it can take care of comments and
340 : continuation lines.
341 : */
342 : void readInputLines(const std::string & str);
343 :
344 : /**
345 : Initialize the object.
346 : Should be called once.
347 : */
348 : void init();
349 : /**
350 : Prepare the calculation.
351 : Here it is checked which are the active Actions and communication of the relevant atoms is initiated.
352 : Shortcut for prepareDependencies() + shareData()
353 : */
354 : void prepareCalc();
355 : /**
356 : Prepare the list of active Actions and needed atoms.
357 : Scan the Actions to see which are active and which are not, so as to prepare a list of
358 : the atoms needed at this step.
359 : */
360 : void prepareDependencies();
361 : /**
362 : Ensure that all the atoms are shared.
363 : This is used in GREX to ensure that we transfer all the positions from the MD code to PLUMED.
364 : */
365 : void shareAll();
366 : /**
367 : Share the needed atoms.
368 : In asynchronous implementations, this method sends the required atoms to all the plumed processes,
369 : without waiting for the communication to complete.
370 : */
371 : void shareData();
372 : /**
373 : Perform the calculation.
374 : Shortcut for waitData() + justCalculate() + justApply().
375 : Equivalently: waitData() + justCalculate() + backwardPropagate() + update().
376 : */
377 : void performCalc();
378 : /**
379 : Perform the calculation without update()
380 : Shortcut for: waitData() + justCalculate() + backwardPropagate()
381 : */
382 : void performCalcNoUpdate();
383 : /**
384 : Perform the calculation without backpropagation nor update()
385 : Shortcut for: waitData() + justCalculate()
386 : */
387 : void performCalcNoForces();
388 : /**
389 : Complete PLUMED calculation.
390 : Shortcut for prepareCalc() + performCalc()
391 : */
392 : void calc();
393 : /**
394 : Scatters the needed atoms.
395 : In asynchronous implementations, this method waits for the communications started in shareData()
396 : to be completed. Otherwise, just send around needed atoms.
397 : */
398 : void waitData();
399 : /**
400 : Perform the forward loop on active actions.
401 : */
402 : void justCalculate();
403 : /**
404 : Backward propagate and update.
405 : Shortcut for backwardPropagate() + update()
406 : I leave it here for backward compatibility
407 : */
408 : void justApply();
409 : /**
410 : Perform the backward loop on active actions.
411 : Needed to apply the forces back.
412 : */
413 : void backwardPropagate();
414 : /**
415 : Call the update() method.
416 : */
417 : void update();
418 : /**
419 : If there are calculations that need to be done at the very end of the calculations this
420 : makes sures they are done
421 : */
422 : /**
423 : This function does clearInputForces for the list of atoms that have a force on them. This
424 : is an optimisation to prevent calling std::fill over a large array
425 : */
426 : void resetInputs();
427 : void runJobsAtEndOfCalculation();
428 : /// Reference to the list of Action's
429 : const ActionSet & getActionSet()const;
430 : /// Referenge to the log stream
431 : Log & getLog();
432 : /// Return the number of the step
433 5403876 : long long int getStep()const {return step;}
434 : /// Stop the run
435 : void exit(int c=0);
436 : /// Load a shared library
437 : void load(const std::string&);
438 : /// Get the suffix string
439 : const std::string & getSuffix()const;
440 : /// Set the suffix string
441 : void setSuffix(const std::string&);
442 : /// get the value of the bias
443 : double getBias()const;
444 : /// get the value of the work
445 : double getWork()const;
446 : /// Opens a file.
447 : /// Similar to plain fopen, but, if it finds an error in opening the file, it also tries with
448 : /// path+suffix. This trick is useful for multiple replica simulations.
449 : FILE* fopen(const char *path, const char *mode);
450 : /// Closes a file opened with PlumedMain::fopen()
451 : int fclose(FILE*fp);
452 : /// Insert a file
453 : void insertFile(FileBase&);
454 : /// Erase a file
455 : void eraseFile(FileBase&);
456 : /// Flush all files
457 : void fflush();
458 : /// Check if restarting
459 : bool getRestart()const;
460 : /// Set restart flag
461 56 : void setRestart(bool f) {if(!doParseOnly) restart=f;}
462 : /// Check if checkpointing
463 : bool getCPT()const;
464 : /// Set exchangeStep flag
465 : void setExchangeStep(bool f);
466 : /// Get exchangeStep flag
467 : bool getExchangeStep()const;
468 : /// Stop the calculation cleanly (both the MD code and plumed)
469 : void stop();
470 : /// Enforce active flag.
471 : /// This is a (bit dirty) hack to solve a bug. When there is no active ActionPilot,
472 : /// several shortcuts are used. However, these shortcuts can block GREX module.
473 : /// This function allows to enforce active plumed when doing exchanges,
474 : /// thus fixing the bug.
475 : void resetActive(bool active);
476 :
477 : /// Access to exchange patterns
478 0 : ExchangePatterns& getExchangePatterns() {return exchangePatterns;}
479 :
480 : /// Push a state to update flags
481 : void updateFlagsPush(bool);
482 : /// Pop a state from update flags
483 : void updateFlagsPop();
484 : /// Get top of update flags
485 : bool updateFlagsTop();
486 : /// Set end of input file
487 : void setEndPlumed();
488 : /// Get the value of the end plumed flag
489 : bool getEndPlumed() const ;
490 : /// Get the value of the gpuDeviceId
491 : int getGpuDeviceId() const ;
492 : /// Call error handler.
493 : /// Should only be called from \ref plumed_plumedmain_cmd().
494 : /// If the error handler was not set, returns false.
495 : bool callErrorHandler(int code,const char* msg)const;
496 : private:
497 : std::atomic<unsigned> referenceCounter{};
498 : public:
499 : /// Atomically increase reference counter and return the new value
500 : unsigned increaseReferenceCounter() noexcept;
501 : /// Atomically decrease reference counter and return the new value
502 : unsigned decreaseReferenceCounter() noexcept;
503 : /// Report the reference counter
504 : unsigned useCountReferenceCounter() const noexcept;
505 : void enableNestedExceptions();
506 : bool getNestedExceptions()const {
507 166 : return nestedExceptions;
508 : }
509 : /// Check if there is active input in the action set
510 : bool inputsAreActive() const ;
511 : /// Transfer information from input MD code
512 : void writeBinary(std::ostream&)const;
513 : void readBinary(std::istream&);
514 : /// Used to set the name of the action that holds the energy
515 : void setEnergyValue( const std::string& name );
516 : /// Get the real preicision
517 : int getRealPrecision() const;
518 : /// Are we using natural units
519 : bool usingNaturalUnits() const ;
520 : /// Get the units that are being used
521 : const Units& getUnits();
522 : /// Take an energy that is calculated by PLUMED and pass it to a typesafe pointer
523 : /// that the MD code can access.
524 : void plumedQuantityToMD( const std::string& unit, const double& eng, const TypesafePtr & m) const ;
525 : /// Take a typesafe pointer from the MD code and convert it to a double
526 : double MDQuantityToPLUMED( const std::string& unit, const TypesafePtr & m) const ;
527 : /// Get the keywords for a particular action
528 : void getKeywordsForAction( const std::string& action, Keywords& keys ) const ;
529 : };
530 :
531 : /////
532 : // FAST INLINE METHODS:
533 :
534 : inline
535 : const ActionSet & PlumedMain::getActionSet()const {
536 1580475 : return actionSet;
537 : }
538 :
539 : inline
540 : const std::string & PlumedMain::getSuffix()const {
541 5315 : return suffix;
542 : }
543 :
544 : inline
545 : void PlumedMain::setSuffix(const std::string&s) {
546 411 : suffix=s;
547 411 : }
548 :
549 : inline
550 : bool PlumedMain::getRestart()const {
551 51260 : return restart;
552 : }
553 :
554 : inline
555 : bool PlumedMain::getCPT()const {
556 51204 : return doCheckPoint;
557 : }
558 :
559 : inline
560 : void PlumedMain::setExchangeStep(bool s) {
561 228 : exchangeStep=s;
562 : }
563 :
564 : inline
565 : bool PlumedMain::getExchangeStep()const {
566 28109 : return exchangeStep;
567 : }
568 :
569 : inline
570 : void PlumedMain::resetActive(bool active) {
571 114 : this->active=active;
572 : }
573 :
574 : inline
575 : void PlumedMain::updateFlagsPush(bool on) {
576 : updateFlags.push(on);
577 : }
578 :
579 : inline
580 : void PlumedMain::updateFlagsPop() {
581 : updateFlags.pop();
582 12 : }
583 :
584 : inline
585 : bool PlumedMain::updateFlagsTop() {
586 2524117 : return updateFlags.top();
587 : }
588 :
589 : inline
590 : void PlumedMain::setEndPlumed() {
591 281 : endPlumed=true;
592 : }
593 :
594 : inline
595 : bool PlumedMain::getEndPlumed() const {
596 24 : return endPlumed;
597 : }
598 :
599 : inline
600 : int PlumedMain::getGpuDeviceId() const {
601 : return gpuDeviceId;
602 : }
603 :
604 : inline
605 : bool PlumedMain::callErrorHandler(int code,const char* msg)const {
606 : if(error_handler.handler) {
607 : error_handler.handler(error_handler.ptr,code,msg);
608 : return true;
609 : } else return false;
610 : }
611 :
612 :
613 : }
614 :
615 : #endif
616 :
|