Newer
Older
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(see the PEOPLE file at the root of the distribution for a list of names)
plumed is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
plumed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "ActionAtomistic.h"
#include "ActionRegister.h"
#include "ActionSet.h"
#include "CLToolMain.h"
#include "ExchangePatterns.h"
#include "config/Config.h"
#include "tools/Citations.h"
#include "tools/Communicator.h"
#include "tools/DLLoader.h"
#include "tools/IFile.h"
#include "tools/OpenMP.h"
#include "tools/Tools.h"
#include <cstdlib>
#include <cstring>
#include <set>
#include <new>
#include <typeinfo>
#ifdef __PLUMED_LIBCXX11
#include <system_error>
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/// Small utility just used in this file to throw arbitrary exceptions
static void testThrow(const char* what) {
auto words=Tools::getWords(what);
plumed_assert(words.size()>0);
#define __PLUMED_THROW_NOMSG(type) if(words[0]==#type) throw type()
#define __PLUMED_THROW_MSG(type) if(words[0]==#type) throw type(what)
__PLUMED_THROW_MSG(PLMD::ExceptionError);
__PLUMED_THROW_MSG(PLMD::ExceptionDebug);
__PLUMED_THROW_MSG(PLMD::Exception);
__PLUMED_THROW_MSG(PLMD::lepton::Exception);
__PLUMED_THROW_NOMSG(std::bad_exception);
#ifdef __PLUMED_LIBCXX11
__PLUMED_THROW_NOMSG(std::bad_array_new_length);
#endif
__PLUMED_THROW_NOMSG(std::bad_alloc);
#ifdef __PLUMED_LIBCXX11
__PLUMED_THROW_NOMSG(std::bad_function_call);
__PLUMED_THROW_NOMSG(std::bad_weak_ptr);
#endif
__PLUMED_THROW_NOMSG(std::bad_cast);
__PLUMED_THROW_NOMSG(std::bad_typeid);
__PLUMED_THROW_MSG(std::underflow_error);
__PLUMED_THROW_MSG(std::overflow_error);
__PLUMED_THROW_MSG(std::range_error);
__PLUMED_THROW_MSG(std::runtime_error);
__PLUMED_THROW_MSG(std::out_of_range);
__PLUMED_THROW_MSG(std::length_error);
__PLUMED_THROW_MSG(std::domain_error);
__PLUMED_THROW_MSG(std::invalid_argument);
__PLUMED_THROW_MSG(std::logic_error);
#ifdef __PLUMED_LIBCXX11
if(words[0]=="std::system_error") {
plumed_assert(words.size()>2);
int error_code;
Tools::convert(words[2],error_code);
if(words[1]=="std::generic_category") throw std::system_error(error_code,std::generic_category(),what);
if(words[1]=="std::system_category") throw std::system_error(error_code,std::system_category(),what);
if(words[1]=="std::iostream_category") throw std::system_error(error_code,std::iostream_category(),what);
if(words[1]=="std::future_category") throw std::system_error(error_code,std::future_category(),what);
}
#endif
if(words[0]=="std::ios_base::failure") {
#ifdef __PLUMED_LIBCXX11
int error_code=0;
if(words.size()>2) Tools::convert(words[2],error_code);
if(words.size()>1 && words[1]=="std::generic_category") throw std::ios_base::failure(what,std::error_code(error_code,std::generic_category()));
if(words.size()>1 && words[1]=="std::system_category") throw std::ios_base::failure(what,std::error_code(error_code,std::system_category()));
if(words.size()>1 && words[1]=="std::iostream_category") throw std::ios_base::failure(what,std::error_code(error_code,std::iostream_category()));
if(words.size()>1 && words[1]=="std::future_category") throw std::ios_base::failure(what,std::error_code(error_code,std::future_category()));
#endif
throw std::ios_base::failure(what);
}
plumed_error() << "unknown exception " << what;
// automatically write on log in destructor
stopwatch_fwd(log),
mydatafetcher(DataFetchingObject::create(sizeof(double),*this)),
atoms_fwd(*this),
actionSet_fwd(*this),
restart(false),
doCheckPoint(false),
stopFlag(NULL),
novirial(false),
detailedTimers(false)
log.link(comm);
log.setLinePrefix("PLUMED: ");
// destructor needed to delete forward declarated objects
}
/////////////////////////////////////////////////////////////
// MAIN INTERPRETER
#define CHECK_INIT(ini,word) plumed_massert(ini,"cmd(\"" + word +"\") should be only used after plumed initialization")
#define CHECK_NOTINIT(ini,word) plumed_massert(!(ini),"cmd(\"" + word +"\") should be only used before plumed initialization")
#define CHECK_NOTNULL(val,word) plumed_massert(val,"NULL pointer received in cmd(\"" + word + "\")");
void PlumedMain::cmd(const std::string & word,void*val) {
// Enumerate all possible commands:
enum {
#include "PlumedMainEnum.inc"
};
// Static object (initialized once) containing the map of commands:
const static std::unordered_map<std::string, int> word_map = {
#include "PlumedMainMap.inc"
};
std::vector<std::string> words=Tools::getWords(word);
unsigned nw=words.size();
if(nw==0) {
// do nothing
} else {
int iword=-1;
double d;
const auto it=word_map.find(words[0]);
if(it!=word_map.end()) iword=it->second;
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
switch(iword) {
case cmd_setBox:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setBox(val);
break;
case cmd_setPositions:
CHECK_INIT(initialized,word);
atoms.setPositions(val);
break;
case cmd_setMasses:
CHECK_INIT(initialized,word);
atoms.setMasses(val);
break;
case cmd_setCharges:
CHECK_INIT(initialized,word);
atoms.setCharges(val);
break;
case cmd_setPositionsX:
CHECK_INIT(initialized,word);
atoms.setPositions(val,0);
break;
case cmd_setPositionsY:
CHECK_INIT(initialized,word);
atoms.setPositions(val,1);
break;
case cmd_setPositionsZ:
CHECK_INIT(initialized,word);
atoms.setPositions(val,2);
break;
case cmd_setVirial:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setVirial(val);
break;
case cmd_setEnergy:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setEnergy(val);
break;
case cmd_setForces:
CHECK_INIT(initialized,word);
atoms.setForces(val);
break;
case cmd_setForcesX:
CHECK_INIT(initialized,word);
atoms.setForces(val,0);
break;
case cmd_setForcesY:
CHECK_INIT(initialized,word);
atoms.setForces(val,1);
break;
case cmd_setForcesZ:
CHECK_INIT(initialized,word);
atoms.setForces(val,2);
break;
case cmd_calc:
CHECK_INIT(initialized,word);
calc();
break;
case cmd_prepareDependencies:
CHECK_INIT(initialized,word);
prepareDependencies();
break;
case cmd_shareData:
CHECK_INIT(initialized,word);
shareData();
break;
case cmd_prepareCalc:
CHECK_INIT(initialized,word);
prepareCalc();
break;
case cmd_performCalc:
CHECK_INIT(initialized,word);
performCalc();
break;
case cmd_performCalcNoUpdate:
CHECK_INIT(initialized,word);
performCalcNoUpdate();
break;
case cmd_update:
CHECK_INIT(initialized,word);
update();
break;
case cmd_setStep:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
step=(*static_cast<int*>(val));
atoms.startStep();
break;
case cmd_setStepLong:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
step=(*static_cast<long int*>(val));
atoms.startStep();
break;
// words used less frequently:
case cmd_setAtomsNlocal:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setAtomsNlocal(*static_cast<int*>(val));
break;
case cmd_setAtomsGatindex:
CHECK_INIT(initialized,word);
atoms.setAtomsGatindex(static_cast<int*>(val),false);
break;
case cmd_setAtomsFGatindex:
CHECK_INIT(initialized,word);
atoms.setAtomsGatindex(static_cast<int*>(val),true);
break;
case cmd_setAtomsContiguous:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setAtomsContiguous(*static_cast<int*>(val));
break;
case cmd_createFullList:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.createFullList(static_cast<int*>(val));
break;
case cmd_getFullList:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.getFullList(static_cast<int**>(val));
break;
case cmd_clearFullList:
CHECK_INIT(initialized,word);
atoms.clearFullList();
break;
/* ADDED WITH API==6 */
case cmd_getDataRank:
CHECK_INIT(initialized,words[0]); plumed_assert(nw==2 || nw==3);
if( nw==2 ) DataFetchingObject::get_rank( actionSet, words[1], "", static_cast<long*>(val) );
else DataFetchingObject::get_rank( actionSet, words[1], words[2], static_cast<long*>(val) );
break;
/* ADDED WITH API==6 */
case cmd_getDataShape:
CHECK_INIT(initialized,words[0]); plumed_assert(nw==2 || nw==3);
if( nw==2 ) DataFetchingObject::get_shape( actionSet, words[1], "", static_cast<long*>(val) );
else DataFetchingObject::get_shape( actionSet, words[1], words[2], static_cast<long*>(val) );
break;
/* ADDED WITH API==6 */
case cmd_setMemoryForData:
CHECK_INIT(initialized,words[0]); plumed_assert(nw==2 || nw==3);
if( nw==2 ) mydatafetcher->setData( words[1], "", val );
else mydatafetcher->setData( words[1], words[2], val );
break;
/* ADDED WITH API==6 */
case cmd_setErrorHandler:
{
if(val) error_handler=*static_cast<plumed_error_handler*>(val);
else error_handler.handler=NULL;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
case cmd_read:
CHECK_INIT(initialized,word);
if(val)readInputFile(static_cast<char*>(val));
else readInputFile("plumed.dat");
break;
case cmd_readInputLine:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
readInputLine(static_cast<char*>(val));
break;
case cmd_clear:
CHECK_INIT(initialized,word);
actionSet.clearDelete();
break;
case cmd_getApiVersion:
CHECK_NOTNULL(val,word);
*(static_cast<int*>(val))=6;
break;
// commands which can be used only before initialization:
case cmd_init:
CHECK_NOTINIT(initialized,word);
init();
break;
case cmd_setRealPrecision:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setRealPrecision(*static_cast<int*>(val));
mydatafetcher=DataFetchingObject::create(*static_cast<int*>(val),*this);
break;
case cmd_setMDLengthUnits:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.MD2double(val,d);
atoms.setMDLengthUnits(d);
break;
case cmd_setMDChargeUnits:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.MD2double(val,d);
atoms.setMDChargeUnits(d);
break;
case cmd_setMDMassUnits:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.MD2double(val,d);
atoms.setMDMassUnits(d);
break;
case cmd_setMDEnergyUnits:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.MD2double(val,d);
atoms.setMDEnergyUnits(d);
break;
case cmd_setMDTimeUnits:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.MD2double(val,d);
atoms.setMDTimeUnits(d);
break;
case cmd_setNaturalUnits:
// set the boltzman constant for MD in natural units (kb=1)
// only needed in LJ codes if the MD is passing temperatures to plumed (so, not yet...)
// use as cmd("setNaturalUnits")
CHECK_NOTINIT(initialized,word);
atoms.setMDNaturalUnits(true);
break;
case cmd_setNoVirial:
CHECK_NOTINIT(initialized,word);
novirial=true;
break;
case cmd_setPlumedDat:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
plumedDat=static_cast<char*>(val);
break;
case cmd_setMPIComm:
CHECK_NOTINIT(initialized,word);
comm.Set_comm(val);
atoms.setDomainDecomposition(comm);
break;
case cmd_setMPIFComm:
CHECK_NOTINIT(initialized,word);
comm.Set_fcomm(val);
atoms.setDomainDecomposition(comm);
break;
case cmd_setMPImultiSimComm:
CHECK_NOTINIT(initialized,word);
multi_sim_comm.Set_comm(val);
break;
case cmd_setNatoms:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setNatoms(*static_cast<int*>(val));
break;
case cmd_setTimestep:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setTimeStep(val);
break;
/* ADDED WITH API==2 */
case cmd_setKbT:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.setKbT(val);
break;
/* ADDED WITH API==3 */
case cmd_setRestart:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
if(*static_cast<int*>(val)!=0) restart=true;
break;
/* ADDED WITH API==4 */
case cmd_doCheckPoint:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
doCheckPoint = false;
if(*static_cast<int*>(val)!=0) doCheckPoint = true;
break;
/* ADDED WITH API==6 */
case cmd_setNumOMPthreads:
CHECK_NOTNULL(val,word);
OpenMP::setNumThreads(*static_cast<int*>(val)>0?*static_cast<int*>(val):1);
/* ADDED WITH API==6 */
/* only used for testing */
case cmd_throw:
CHECK_NOTNULL(val,word);
testThrow((const char*) val);
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* STOP API */
case cmd_setMDEngine:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
MDEngine=static_cast<char*>(val);
break;
case cmd_setLog:
CHECK_NOTINIT(initialized,word);
log.link(static_cast<FILE*>(val));
break;
case cmd_setLogFile:
CHECK_NOTINIT(initialized,word);
CHECK_NOTNULL(val,word);
log.open(static_cast<char*>(val));
break;
// other commands that should be used after initialization:
case cmd_setStopFlag:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
stopFlag=static_cast<int*>(val);
break;
case cmd_getExchangesFlag:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
exchangePatterns.getFlag((*static_cast<int*>(val)));
break;
case cmd_setExchangesSeed:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
exchangePatterns.setSeed((*static_cast<int*>(val)));
break;
case cmd_setNumberOfReplicas:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
exchangePatterns.setNofR((*static_cast<int*>(val)));
break;
case cmd_getExchangesList:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
exchangePatterns.getList((static_cast<int*>(val)));
break;
case cmd_runFinalJobs:
CHECK_INIT(initialized,word);
runJobsAtEndOfCalculation();
break;
case cmd_isEnergyNeeded:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
if(atoms.isEnergyNeeded()) *(static_cast<int*>(val))=1;
else *(static_cast<int*>(val))=0;
break;
case cmd_getBias:
CHECK_INIT(initialized,word);
CHECK_NOTNULL(val,word);
atoms.double2MD(getBias()/(atoms.getMDUnits().getEnergy()/atoms.getUnits().getEnergy()),val);
break;
case cmd_checkAction:
CHECK_NOTNULL(val,word);
plumed_assert(nw==2);
*(static_cast<int*>(val))=(actionRegister().check(words[1]) ? 1:0);
break;
case cmd_setExtraCV:
CHECK_NOTNULL(val,word);
plumed_assert(nw==2);
atoms.setExtraCV(words[1],val);
break;
case cmd_setExtraCVForce:
CHECK_NOTNULL(val,word);
plumed_assert(nw==2);
atoms.setExtraCVForce(words[1],val);
break;
case cmd_GREX:
if(!grex) grex.reset(new GREX(*this));
plumed_massert(grex,"error allocating grex");
{
std::string kk=words[1];
for(unsigned i=2; i<words.size(); i++) kk+=" "+words[i];
grex->cmd(kk.c_str(),val);
}
break;
case cmd_CLTool:
CHECK_NOTINIT(initialized,word);
if(!cltool) cltool.reset(new CLToolMain);
{
std::string kk=words[1];
for(unsigned i=2; i<words.size(); i++) kk+=" "+words[i];
cltool->cmd(kk.c_str(),val);
}
break;
default:
plumed_merror("cannot interpret cmd(\"" + word + "\"). check plumed developers manual to see the available commands.");
break;
if(log.isOpen()) {
log<<"\n\n################################################################################\n\n";
log<<e.what();
log<<"\n\n################################################################################\n\n";
log.flush();
}
throw;
}
////////////////////////////////////////////////////////////////////////
// check that initialization just happens once
initialized=true;
atoms.init();
log<<"Version: "<<config::getVersionLong()<<" (git: "<<config::getVersionGit()<<") "
<<"compiled on " <<config::getCompilationDate() << " at " << config::getCompilationTime() << "\n";
log<<"Please cite these papers when using PLUMED ";
log<<cite("The PLUMED consortium, Nat. Methods 16, 670 (2019)");
log<<cite("Tribello, Bonomi, Branduardi, Camilloni, and Bussi, Comput. Phys. Commun. 185, 604 (2014)");
log<<"For further information see the PLUMED web page at http://www.plumed.org\n";
log<<"Root: "<<config::getPlumedRoot()<<"\n";
log<<"For installed feature, see "<<config::getPlumedRoot() + "/src/config/config.txt\n";
log.printf("Molecular dynamics engine: %s\n",MDEngine.c_str());
log.printf("Precision of reals: %d\n",atoms.getRealPrecision());
log.printf("Running over %d %s\n",comm.Get_size(),(comm.Get_size()>1?"nodes":"node"));
log<<"Number of threads: "<<OpenMP::getNumThreads()<<"\n";
log<<"Cache line size: "<<OpenMP::getCachelineSize()<<"\n";
if(grex) log.printf("GROMACS-like replica exchange is on\n");
log.printf("File suffix: %s\n",getSuffix().c_str());
readInputFile(plumedDat);
plumedDat="";
}
atoms.updateUnits();
log.printf("Timestep: %f\n",atoms.getTimeStep());
if(atoms.getKbT()>0.0)
log.printf("KbT: %f\n",atoms.getKbT());
else {
log.printf("KbT has not been set by the MD engine\n");
log.printf("It should be set by hand where needed\n");
}
log<<"Relevant bibliography:\n";
log<<citations;
log<<"Please read and cite where appropriate!\n";
void PlumedMain::readInputFile(std::string str) {
plumed_assert(initialized);
while(Tools::getParsedLine(ifile,words) && !endPlumed) readInputWords(words);
endPlumed=false;
void PlumedMain::readInputLine(const std::string & str) {
plumed_assert(initialized);
if(str.empty()) return;
std::vector<std::string> words=Tools::getWords(str);
log<<"Relevant bibliography:\n";
log<<citations;
log<<"Please read and cite where appropriate!\n";
}
void PlumedMain::readInputWords(const std::vector<std::string> & words) {
plumed_assert(initialized);
if(words.empty())return;
plumed_assert(words.size()==2);
setSuffix(words[1]);
} else {
std::vector<std::string> interpreted(words);
Tools::interpretLabel(interpreted);
auto action=actionRegister().create(ActionOptions(*this,interpreted));
std::string msg;
msg ="ERROR\nI cannot understand line:";
for(unsigned i=0; i<interpreted.size(); ++i) msg+=" "+interpreted[i];
msg+="\nMaybe a missing space or a typo?";
log << msg;
};
action->checkRead();
};
pilots=actionSet.select<ActionPilot*>();
}
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// here we have the main steps in "calc()"
// they can be called individually, but the standard thing is to
// traverse them in this order:
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("1 Prepare dependencies");
// activate all the actions which are on step
// activation is recursive and enables also the dependencies
// before doing that, the prepare() method is called to see if there is some
// new/changed dependency (up to now, only useful for dependences on virtual atoms,
// which can be dynamically changed).
// First switch off all actions
for(const auto & p : actionSet) {
p->deactivate();
// for optimization, an "active" flag remains false if no action at all is active
for(unsigned i=0; i<pilots.size(); ++i) {
if(pilots[i]->onStep()) {
// also, if one of them is the total energy, tell to atoms that energy should be collected
for(const auto & p : actionSet) {
if(p->isActive()) {
if(p->checkNeedsGradients()) p->setOption("GRADIENTS");
// atom positions are shared (but only if there is something to do)
if(!active)return;
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("2 Sharing data");
if(atoms.getNatoms()>0) atoms.share();
waitData();
justCalculate();
backwardPropagate();
}
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("3 Waiting for data");
if(atoms.getNatoms()>0) atoms.wait();
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("4 Calculating (forward loop)");
// calculate the active actions in order (assuming *backward* dependence)
for(const auto & pp : actionSet) {
Action* p(pp.get());
// Stopwatch is stopped when sw goes out of scope.
// We explicitly declare a Stopwatch::Handler here to allow for conditional initialization.
Stopwatch::Handler sw;
Carlo Camilloni
committed
Tools::convert(iaction,actionNumberLabel);
const unsigned m=actionSet.size();
unsigned k=0; unsigned n=1; while(n<m) { n*=10; k++; }
const int pad=k-actionNumberLabel.length();
for(int i=0; i<pad; i++) actionNumberLabel=" "+actionNumberLabel;
sw=stopwatch.startStop("4A "+actionNumberLabel+" "+p->getLabel());
Carlo Camilloni
committed
}
ActionWithValue*av=dynamic_cast<ActionWithValue*>(p);
ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(p);
Carlo Camilloni
committed
{
if(av) av->clearInputForces();
if(av) av->clearDerivatives();
}
{
if(aa) aa->clearOutputForces();
if(aa) if(aa->isActive()) aa->retrieveAtoms();
}
if(p->checkNumericalDerivatives()) p->calculateNumericalDerivatives();
else p->calculate();
if(av) bias+=av->getOutputQuantity("bias");
if(av) work+=av->getOutputQuantity("work");
ActionWithVirtualAtom*avv=dynamic_cast<ActionWithVirtualAtom*>(p);
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("5 Applying (backward loop)");
for(auto pp=actionSet.rbegin(); pp!=actionSet.rend(); ++pp) {
// Stopwatch is stopped when sw goes out of scope.
// We explicitly declare a Stopwatch::Handler here to allow for conditional initialization.
Stopwatch::Handler sw;
Tools::convert(iaction,actionNumberLabel);
const unsigned m=actionSet.size();
unsigned k=0; unsigned n=1; while(n<m) { n*=10; k++; }
const int pad=k-actionNumberLabel.length();
for(int i=0; i<pad; i++) actionNumberLabel=" "+actionNumberLabel;
sw=stopwatch.startStop("5A "+actionNumberLabel+" "+p->getLabel());
p->apply();
ActionAtomistic*a=dynamic_cast<ActionAtomistic*>(p);
// still ActionAtomistic has a special treatment, since they may need to add forces on atoms
if(a) a->applyForces();
}
iaction++;
// Stopwatch is stopped when sw goes out of scope.
// We explicitly declare a Stopwatch::Handler here to allow for conditional initialization.
Stopwatch::Handler sw1;
if(detailedTimers) sw1=stopwatch.startStop("5B Update forces");
if(atoms.getNatoms()>0) atoms.updateForces();
// Stopwatch is stopped when sw goes out of scope
auto sw=stopwatch.startStop("6 Update");
// update step (for statistics, etc)
p->beforeUpdate();
if(p->isActive() && p->checkUpdate() && updateFlagsTop()) p->update();
while(!updateFlags.empty()) updateFlags.pop();
if(!updateFlags.empty()) plumed_merror("non matching changes in the update flags");
// Check that no action has told the calculation to stop
if(stopNow) {
if(stopFlag) (*stopFlag)=1;
else plumed_merror("your md code cannot handle plumed stop events - add a call to plumed.comm(stopFlag,stopCondition)");
}
// flush by default every 10000 steps
// hopefully will not affect performance
// also if receive checkpointing signal
for(const auto & p : actionSet) p->fflush();
void PlumedMain::load(const std::string& ss) {
if(DLLoader::installed()) {
if(n!=std::string::npos && n<s.length()-1) extension=s.substr(n+1);
if(n!=std::string::npos && n<s.length()) base=s.substr(0,n);
if(extension=="cpp") {
// full path command, including environment setup
// this will work even if plumed is not in the execution path or if it has been
// installed with a name different from "plumed"
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+s;
log<<"Executing: "<<cmd;
if(comm.Get_size()>0) log<<" (only on master node)";
log<<"\n";
if(comm.Get_rank()==0) system(cmd.c_str());
comm.Barrier();
base="./"+base;
}
s=base+"."+config::getSoExt();
void *p=dlloader.load(s);
if(!p) {
const std::string error_msg="I cannot load library " + ss + " " + dlloader.error();
log<<"ERROR\n";
log<<error_msg<<"\n";
plumed_merror(error_msg);
}
log<<"Loading shared library "<<s.c_str()<<"\n";
log<<"Here is the new list of available actions\n";
log<<actionRegister();
} else plumed_merror("loading not enabled, please recompile with -D__PLUMED_HAS_DLOPEN");
FILE* PlumedMain::fopen(const char *path, const char *mode) {
std::string mmode(mode);
std::string ppath(path);
std::string suffix(getSuffix());
std::string ppathsuf=ppath+suffix;
FILE*fp=std::fopen(const_cast<char*>(ppathsuf.c_str()),const_cast<char*>(mmode.c_str()));
if(!fp) fp=std::fopen(const_cast<char*>(ppath.c_str()),const_cast<char*>(mmode.c_str()));
return std::fclose(fp);
}
std::string PlumedMain::cite(const std::string&item) {
return citations.cite(item);
}
void PlumedMain::fflush() {
for(const auto & p : files) {
stopNow=true;
}
void PlumedMain::runJobsAtEndOfCalculation() {
for(const auto & p : actionSet) {
p->runFinalJobs();
}
#ifdef __PLUMED_HAS_PYTHON
// This is here to stop cppcheck throwing an error
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////