8 #include "Simulation.h" 9 #include <ddMd/storage/AtomIterator.h> 10 #include <ddMd/storage/GhostIterator.h> 11 #include <ddMd/storage/GroupStorage.tpp> 12 #include <ddMd/integrators/Integrator.h> 13 #include <ddMd/integrators/IntegratorFactory.h> 14 #include <ddMd/configIos/ConfigIo.h> 15 #include <ddMd/configIos/ConfigIoFactory.h> 16 #include <ddMd/configIos/DdMdConfigIo.h> 17 #include <ddMd/configIos/SerializeConfigIo.h> 18 #include <ddMd/analyzers/AnalyzerManager.h> 20 #include <ddMd/modifiers/ModifierManager.h> 23 #include <ddMd/potentials/pair/PairPotential.h> 24 #include <ddMd/potentials/pair/PairFactory.h> 26 #include <ddMd/potentials/bond/BondPotential.h> 27 #include <ddMd/potentials/bond/BondFactory.h> 30 #include <ddMd/potentials/angle/AnglePotential.h> 31 #include <ddMd/potentials/angle/AngleFactory.h> 34 #include <ddMd/potentials/dihedral/DihedralPotential.h> 35 #include <ddMd/potentials/dihedral/DihedralFactory.h> 38 #include <ddMd/potentials/external/ExternalPotential.h> 39 #include <ddMd/potentials/external/ExternalPotentialImpl.h> 40 #include <ddMd/potentials/external/ExternalFactory.h> 44 #include <simp/ensembles/EnergyEnsemble.h> 45 #include <simp/ensembles/BoundaryEnsemble.h> 48 #include <util/misc/FileMaster.h> 49 #include <util/space/Vector.h> 50 #include <util/space/IntVector.h> 51 #include <util/space/Tensor.h> 52 #include <util/param/Factory.h> 53 #include <util/misc/Log.h> 54 #include <util/misc/Memory.h> 56 #include <util/misc/Timer.h> 57 #include <util/misc/Bit.h> 58 #include <util/misc/initStatic.h> 59 #include <util/format/Int.h> 60 #include <util/format/Dbl.h> 61 #include <util/format/Str.h> 62 #include <util/misc/ioUtil.h> 101 pairPotentialPtr_(0),
103 bondPotentialPtr_(0),
106 anglePotentialPtr_(0),
109 dihedralPotentialPtr_(0),
112 externalPotentialPtr_(0),
115 energyEnsemblePtr_(0),
116 boundaryEnsemblePtr_(0),
119 serializeConfigIoPtr_(0),
120 #ifdef DDMD_MODIFIERS 121 modifierManagerPtr_(0),
123 analyzerManagerPtr_(0),
132 dihedralFactoryPtr_(0),
135 externalFactoryPtr_(0),
137 integratorFactoryPtr_(0),
138 configIoFactoryPtr_(0),
165 hasAtomContext_(
false),
166 maskedPairPolicy_(MaskBonded),
167 reverseUpdateFlag_(
false),
169 communicator_(communicator),
171 isInitialized_(
false),
175 setClassName(
"Simulation");
178 if (!MPI::Is_initialized()) {
185 setIoCommunicator(communicator);
186 domain_.setGridCommunicator(communicator);
192 domain_.setBoundary(boundary_);
193 exchanger_.associate(domain_, boundary_, atomStorage_, buffer_);
194 atomStorage_.associate(domain_, boundary_, buffer_);
196 bondStorage_.associate(domain_, atomStorage_, buffer_);
199 angleStorage_.associate(domain_, atomStorage_, buffer_);
202 dihedralStorage_.associate(domain_, atomStorage_, buffer_);
208 #ifdef DDMD_MODIFIERS 219 if (pairFactoryPtr_) {
220 delete pairFactoryPtr_;
222 if (pairPotentialPtr_) {
223 delete pairPotentialPtr_;
226 if (bondFactoryPtr_) {
227 delete bondFactoryPtr_;
229 if (bondPotentialPtr_) {
230 delete bondPotentialPtr_;
234 if (angleFactoryPtr_) {
235 delete angleFactoryPtr_;
237 if (anglePotentialPtr_) {
238 delete anglePotentialPtr_;
242 if (dihedralFactoryPtr_) {
243 delete dihedralFactoryPtr_;
245 if (dihedralPotentialPtr_) {
246 delete dihedralPotentialPtr_;
250 if (externalFactoryPtr_) {
251 delete externalFactoryPtr_;
253 if (externalPotentialPtr_) {
254 delete externalPotentialPtr_;
257 if (configIoFactoryPtr_) {
258 delete configIoFactoryPtr_;
263 if (serializeConfigIoPtr_) {
264 delete serializeConfigIoPtr_;
266 if (integratorFactoryPtr_) {
267 delete integratorFactoryPtr_;
269 if (integratorPtr_) {
270 delete integratorPtr_;
272 if (analyzerManagerPtr_) {
273 delete analyzerManagerPtr_;
275 #ifdef DDMD_MODIFIERS 276 if (modifierManagerPtr_) {
277 delete modifierManagerPtr_;
280 if (fileMasterPtr_) {
281 delete fileMasterPtr_;
283 if (energyEnsemblePtr_) {
284 delete energyEnsemblePtr_;
286 if (boundaryEnsemblePtr_) {
287 delete boundaryEnsemblePtr_;
291 if (logFile_.is_open()) {
321 while ((c = getopt(argc, argv,
"qes:p:r:c:i:o:")) != -1) {
332 nSystem = atoi(sArg);
355 char optChar = optopt;
356 Log::file() <<
"Unknown option -" << optChar << std::endl;
366 int worldRank = communicator_.Get_rank();
367 int worldSize = communicator_.Get_size();
368 if (worldSize % nSystem != 0) {
369 UTIL_THROW(
"World communicator size not a multiple of nSystem");
373 int systemSize = worldSize/nSystem;
374 int systemId = worldRank/systemSize;
375 communicator_ = communicator_.Split(systemId, worldRank);
378 setIoCommunicator(communicator_);
379 domain_.setGridCommunicator(communicator_);
380 fileMaster().setDirectoryId(systemId);
384 fileMaster().openOutputFile(
"log", logFile_);
390 if (communicator_.Get_rank() == 0) {
403 UTIL_THROW(
"Cannot have both parameter and restart files");
405 fileMaster().setParamFileName(std::string(pArg));
410 if (isIoProcessor()) {
411 Log::file() <<
"Begin reading restart, base file name " 412 << std::string(rArg) << std::endl;
414 load(std::string(rArg));
415 if (isIoProcessor()) {
416 Log::file() <<
"Done reading restart file" << std::endl;
426 fileMaster().setCommandFileName(std::string(cArg));
431 fileMaster().setInputPrefix(std::string(iArg));
436 fileMaster().setOutputPrefix(std::string(oArg));
446 if (!isRestarting_) {
447 readBegin(in, className().c_str());
470 if (!isRestarting_) {
471 readParam(fileMaster().paramFile());
481 if (isInitialized_) {
482 UTIL_THROW(
"Error: Called Simulation::readParameters when isInitialized");
486 readParamComposite(in, domain_);
490 read<int>(in,
"nAtomType", nAtomType_);
493 readOptional<int>(in,
"nBondType", nBondType_);
495 exchanger_.addGroupExchanger(bondStorage_);
500 readOptional<int>(in,
"nAngleType", nAngleType_);
502 exchanger_.addGroupExchanger(angleStorage_);
507 readOptional<int>(in,
"nDihedralType", nDihedralType_);
508 if (nDihedralType_) {
509 exchanger_.addGroupExchanger(dihedralStorage_);
513 hasExternal_ =
false;
514 readOptional<bool>(in,
"hasExternal", hasExternal_);
517 hasAtomContext_ =
false;
518 readOptional<bool>(in,
"hasAtomContext", hasAtomContext_);
522 atomTypes_.allocate(nAtomType_);
523 for (
int i = 0; i < nAtomType_; ++i) {
524 atomTypes_[i].setId(i);
526 readDArray<AtomType>(in,
"atomTypes", atomTypes_, nAtomType_);
529 readParamComposite(in, atomStorage_);
532 readParamComposite(in, bondStorage_);
537 readParamComposite(in, angleStorage_);
541 if (nDihedralType_) {
542 readParamComposite(in, dihedralStorage_);
546 readParamComposite(in, buffer_);
547 readPotentialStyles(in);
552 assert(pairPotentialPtr_ == 0);
553 pairPotentialPtr_ = pairFactory().factory(pairStyle());
554 if (!pairPotentialPtr_) {
557 pairPotential().setReverseUpdateFlag(reverseUpdateFlag_);
558 readParamComposite(in, *pairPotentialPtr_);
562 assert(bondPotentialPtr_ == 0);
564 bondPotentialPtr_ = bondFactory().factory(bondStyle());
565 if (!bondPotentialPtr_) {
568 readParamComposite(in, *bondPotentialPtr_);
574 assert(anglePotentialPtr_ == 0);
576 anglePotentialPtr_ = angleFactory().factory(angleStyle());
577 if (!anglePotentialPtr_) {
580 readParamComposite(in, *anglePotentialPtr_);
586 assert(dihedralPotentialPtr_ == 0);
587 if (nDihedralType_) {
588 dihedralPotentialPtr_ = dihedralFactory().factory(dihedralStyle());
589 if (!dihedralPotentialPtr_) {
592 readParamComposite(in, *dihedralPotentialPtr_);
599 assert(externalPotentialPtr_ == 0);
600 externalPotentialPtr_ = externalFactory().factory(externalStyle());
601 if (!externalPotentialPtr_) {
604 readParamComposite(in, *externalPotentialPtr_);
611 std::string className;
613 assert(integratorPtr_ == 0);
615 integratorFactory().readObject(in, *
this, className, isEnd);
616 if (!integratorPtr_) {
617 std::string msg(
"Unknown Integrator subclass name: ");
621 #ifdef DDMD_MODIFIERS 622 readParamCompositeOptional(in, *modifierManagerPtr_);
624 readParamComposite(in, random_);
625 readParamComposite(in, *analyzerManagerPtr_);
629 exchanger_.setPairCutoff(pairPotential().cutoff());
630 exchanger_.allocate();
646 exchangeSignal().addObserver(bondStorage_, memberPtr);
652 exchangeSignal().addObserver(angleStorage_, memberPtr);
656 if (nDihedralType_) {
658 exchangeSignal().addObserver(dihedralStorage_, memberPtr);
662 isInitialized_ =
true;
670 if (isInitialized_) {
671 UTIL_THROW(
"Error: Called loadParameters when already initialized");
673 if (!hasIoCommunicator()) {
674 UTIL_THROW(
"Error: No IoCommunicator is set");
676 isRestarting_ =
true;
678 loadParamComposite(ar, domain_);
682 loadParameter<int>(ar,
"nAtomType", nAtomType_);
685 loadParameter<int>(ar,
"nBondType", nBondType_,
false);
687 exchanger_.addGroupExchanger(bondStorage_);
692 loadParameter<int>(ar,
"nAngleType", nAngleType_,
false);
694 exchanger_.addGroupExchanger(angleStorage_);
699 loadParameter<int>(ar,
"nDihedralType", nDihedralType_,
false);
700 if (nDihedralType_) {
701 exchanger_.addGroupExchanger(dihedralStorage_);
705 hasExternal_ =
false;
706 loadParameter<bool>(ar,
"hasExternal", hasExternal_,
false);
709 hasAtomContext_ =
false;
710 loadParameter<bool>(ar,
"hasAtomContext", hasAtomContext_,
false);
713 atomTypes_.allocate(nAtomType_);
714 for (
int i = 0; i < nAtomType_; ++i) {
715 atomTypes_[i].setId(i);
717 loadDArray<AtomType>(ar,
"atomTypes", atomTypes_, nAtomType_);
720 loadParamComposite(ar, atomStorage_);
723 loadParamComposite(ar, bondStorage_);
728 loadParamComposite(ar, angleStorage_);
732 if (nDihedralType_) {
733 loadParamComposite(ar, dihedralStorage_);
736 loadParamComposite(ar, buffer_);
739 loadPotentialStyles(ar);
742 assert(pairPotentialPtr_ == 0);
743 pairPotentialPtr_ = pairFactory().factory(pairStyle());
744 if (!pairPotentialPtr_) {
747 loadParamComposite(ar, *pairPotentialPtr_);
748 pairPotential().setReverseUpdateFlag(reverseUpdateFlag_);
752 assert(bondPotentialPtr_ == 0);
754 bondPotentialPtr_ = bondFactory().factory(bondStyle());
755 if (!bondPotentialPtr_) {
758 loadParamComposite(ar, *bondPotentialPtr_);
764 assert(anglePotentialPtr_ == 0);
766 anglePotentialPtr_ = angleFactory().factory(angleStyle());
767 if (!anglePotentialPtr_) {
770 loadParamComposite(ar, *anglePotentialPtr_);
776 assert(dihedralPotentialPtr_ == 0);
777 if (nDihedralType_) {
778 dihedralPotentialPtr_ = dihedralFactory().factory(dihedralStyle());
779 if (!dihedralPotentialPtr_) {
782 loadParamComposite(ar, *dihedralPotentialPtr_);
788 assert(externalPotentialPtr_ == 0);
790 externalPotentialPtr_ = externalFactory().factory(externalStyle());
791 if (!externalPotentialPtr_) {
794 loadParamComposite(ar, *externalPotentialPtr_);
801 assert(integratorPtr_ == 0);
802 std::string className;
804 integratorFactory().loadObject(ar, *
this, className);
805 if (!integratorPtr_) {
806 std::string msg(
"Unknown Integrator subclass name: ");
810 #ifdef DDMD_MODIFIERS 811 loadParamCompositeOptional(ar, *modifierManagerPtr_);
813 loadParamComposite(ar, random_);
814 loadParamComposite(ar, *analyzerManagerPtr_);
818 exchanger_.setPairCutoff(pairPotential().cutoff());
819 exchanger_.allocate();
835 exchangeSignal().addObserver(bondStorage_, memberPtr);
841 exchangeSignal().addObserver(angleStorage_, memberPtr);
845 if (nDihedralType_) {
847 exchangeSignal().addObserver(dihedralStorage_, memberPtr);
851 isInitialized_ =
true;
854 serializeConfigIo().loadConfig(ar, maskedPairPolicy_);
857 exchanger_.exchange();
868 if (isInitialized_) {
869 UTIL_THROW(
"Error: Called load when already initialized");
871 if (!hasIoCommunicator()) {
872 UTIL_THROW(
"Error: No IoCommunicator is set");
876 if (isIoProcessor()) {
877 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary;
878 fileMaster().openRestartIFile(filename, ar.
file(), mode);
882 if (isIoProcessor()) {
916 atomStorage_.save(ar);
919 bondStorage_.save(ar);
924 angleStorage_.save(ar);
928 if (nDihedralType_) {
929 dihedralStorage_.save(ar);
936 savePotentialStyles(ar);
937 pairPotential().save(ar);
940 bondPotential().save(ar);
945 anglePotential().save(ar);
949 if (nDihedralType_) {
950 dihedralPotential().save(ar);
955 externalPotential().save(ar);
961 std::string name = integrator().className();
963 integrator().save(ar);
964 #ifdef DDMD_MODIFIERS 965 modifierManager().saveOptional(ar);
968 analyzerManager().save(ar);
977 atomStorage_.computeStatistics(domain_.communicator());
980 bondStorage_.computeStatistics(domain_.communicator());
985 angleStorage_.computeStatistics(domain_.communicator());
989 if (nDihedralType_) {
990 dihedralStorage_.computeStatistics(domain_.communicator());
993 buffer_.computeStatistics(domain_.communicator());
994 if (integratorPtr_) {
995 integrator().computeStatistics();
1000 if (isIoProcessor()) {
1001 std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary;
1002 fileMaster().openRestartOFile(filename, ar.
file(), mode);
1007 serializeConfigIo().saveConfig(ar);
1009 if (isIoProcessor()) {
1021 assert(fileMasterPtr_);
1022 readParamComposite(in, *fileMasterPtr_);
1023 assert(fileMasterPtr_->hasIoCommunicator());
1031 assert(fileMasterPtr_);
1032 loadParamComposite(ar, *fileMasterPtr_);
1033 assert(fileMasterPtr_->hasIoCommunicator());
1040 { fileMasterPtr_->save(ar); }
1047 read<std::string>(in,
"pairStyle", pairStyle_);
1050 read<std::string>(in,
"bondStyle", bondStyle_);
1055 read<std::string>(in,
"angleStyle", angleStyle_);
1058 #ifdef SIMP_DIHEDRAL 1059 if (nDihedralType_) {
1060 read<std::string>(in,
"dihedralStyle", dihedralStyle_);
1063 #ifdef SIMP_EXTERNAL 1065 read<std::string>(in,
"externalStyle", externalStyle_);
1071 read<MaskPolicy>(in,
"maskedPairPolicy", maskedPairPolicy_);
1074 read<bool>(in,
"reverseUpdateFlag", reverseUpdateFlag_);
1083 loadParameter<std::string>(ar,
"pairStyle", pairStyle_);
1086 loadParameter<std::string>(ar,
"bondStyle", bondStyle_);
1091 loadParameter<std::string>(ar,
"angleStyle", angleStyle_);
1094 #ifdef SIMP_DIHEDRAL 1095 if (nDihedralType_) {
1096 loadParameter<std::string>(ar,
"dihedralStyle", dihedralStyle_);
1099 #ifdef SIMP_EXTERNAL 1101 loadParameter<std::string>(ar,
"externalStyle", externalStyle_);
1104 loadParameter<MaskPolicy>(ar,
"maskedPairPolicy", maskedPairPolicy_);
1105 loadParameter<bool>(ar,
"reverseUpdateFlag", reverseUpdateFlag_);
1107 isInitialized_ =
true;
1126 #ifdef SIMP_DIHEDRAL 1127 if (nDihedralType_) {
1128 ar << dihedralStyle_;
1131 #ifdef SIMP_EXTERNAL 1133 ar << externalStyle_;
1136 ar << maskedPairPolicy_;
1137 ar << reverseUpdateFlag_;
1145 readParamComposite(in, *energyEnsemblePtr_);
1146 readParamComposite(in, *boundaryEnsemblePtr_);
1154 loadParamComposite(ar, *energyEnsemblePtr_);
1155 loadParamComposite(ar, *boundaryEnsemblePtr_);
1163 energyEnsemblePtr_->save(ar);
1164 boundaryEnsemblePtr_->save(ar);
1174 std::string command;
1175 std::string filename;
1176 std::ifstream inputFile;
1177 std::ofstream outputFile;
1180 std::stringstream inBuffer;
1183 std::istream& inBuffer = in;
1186 bool readNext =
true;
1190 if (atomStorage().isCartesian()) {
1191 UTIL_THROW(
"Error: Storage set for Cartesian atom coordinates");
1196 if (!hasIoCommunicator() || isIoProcessor()) {
1200 if (hasIoCommunicator()) {
1201 bcast<std::string>(domain_.communicator(), line, 0);
1203 #else // ifndef UTIL_MPI 1208 for (
unsigned i=0; i < line.size(); ++i) {
1209 inBuffer.put(line[i]);
1212 inBuffer >> command;
1213 if (isRestarting_) {
1215 if (command ==
"RESTART") {
1217 inBuffer >> endStep;
1218 int iStep = integrator().iStep();
1219 int nStep = endStep - iStep;
1220 if (isIoProcessor()) {
1221 Log::file() <<
"Running from iStep =" << iStep <<
" to " 1222 << endStep << std::endl;
1224 integrator().run(nStep);
1225 isRestarting_ =
false;
1227 UTIL_THROW(
"Missing RESTART command when restarting");
1232 if (command ==
"READ_CONFIG") {
1235 inBuffer >> filename;
1236 readConfig(filename);
1238 if (command ==
"THERMALIZE") {
1240 inBuffer >> temperature;
1241 setBoltzmannVelocities(temperature);
1242 removeDriftVelocity();
1244 if (command ==
"SIMULATE") {
1247 if (domain_.isMaster()) {
1250 integrator().run(nStep);
1252 if (command ==
"OUTPUT_ANALYZERS") {
1253 analyzerManager().output();
1255 if (command ==
"OUTPUT_INTEGRATOR_STATS") {
1257 integrator().computeStatistics();
1258 if (domain_.isMaster()) {
1259 integrator().outputStatistics(
Log::file());
1262 if (command ==
"OUTPUT_EXCHANGER_STATS") {
1264 integrator().computeStatistics();
1266 exchanger().timer().reduce(domain().communicator());
1268 if (domain_.isMaster()) {
1269 int iStep = integrator().iStep();
1270 double time = integrator().time();
1271 exchanger_.outputStatistics(
Log::file(), time, iStep);
1274 if (command ==
"OUTPUT_MEMORY_STATS") {
1277 atomStorage().computeStatistics(domain_.communicator());
1280 bondStorage().computeStatistics(domain_.communicator());
1285 angleStorage().computeStatistics(domain_.communicator());
1288 #ifdef SIMP_DIHEDRAL 1289 if (nDihedralType_) {
1290 dihedralStorage().computeStatistics(domain_.communicator());
1293 pairPotential().pairList()
1294 .computeStatistics(domain_.communicator());
1295 buffer().computeStatistics(domain_.communicator());
1296 int maxMemory =
Memory::max(domain_.communicator());
1297 if (domain_.isMaster()) {
1298 atomStorage().outputStatistics(
Log::file());
1301 bondStorage().outputStatistics(
Log::file());
1306 angleStorage().outputStatistics(
Log::file());
1309 #ifdef SIMP_DIHEDRAL 1310 if (nDihedralType_) {
1311 dihedralStorage().outputStatistics(
Log::file());
1315 pairPotential().pairList().outputStatistics(
Log::file());
1317 Log::file() <<
"Memory: maximum allocated for arrays = " 1318 << maxMemory <<
" bytes" << std::endl;
1322 atomStorage().clearStatistics();
1325 bondStorage().clearStatistics();
1330 angleStorage().clearStatistics();
1333 #ifdef SIMP_DIHEDRAL 1334 if (nDihedralType_) {
1335 dihedralStorage().clearStatistics();
1338 buffer().clearStatistics();
1339 pairPotential().pairList().clearStatistics();
1342 if (command ==
"CLEAR_INTEGRATOR") {
1345 integrator().clear();
1347 if (command ==
"WRITE_CONFIG") {
1351 inBuffer >> filename;
1352 writeConfig(filename);
1354 if (command ==
"WRITE_PARAM") {
1356 inBuffer >> filename;
1357 if (isIoProcessor()) {
1358 fileMaster().openOutputFile(filename, outputFile);
1359 writeParam(outputFile);
1363 if (command ==
"SET_CONFIG_IO") {
1366 std::string classname;
1367 inBuffer >> classname;
1368 setConfigIo(classname);
1370 if (command ==
"SET_INPUT_PREFIX") {
1375 fileMaster().setInputPrefix(prefix);
1377 if (command ==
"SET_OUTPUT_PREFIX") {
1382 fileMaster().setOutputPrefix(prefix);
1384 if (command ==
"SET_PAIR") {
1386 std::string paramName;
1387 int typeId1, typeId2;
1389 inBuffer >> paramName >> typeId1 >> typeId2 >> value;
1390 pairPotential().set(paramName, typeId1, typeId2, value);
1393 if (command ==
"SET_BOND") {
1394 if (nBondType_ == 0) {
1395 UTIL_THROW(
"SET_BOND command with nBondType = 0");
1398 std::string paramName;
1401 inBuffer >> paramName >> typeId >> value;
1402 bondPotential().set(paramName, typeId, value);
1406 if (command ==
"SET_ANGLE") {
1407 if (nAngleType_ == 0) {
1408 UTIL_THROW(
"SET_ANGLE command with nAngleType = 0");
1411 std::string paramName;
1414 inBuffer >> paramName >> typeId >> value;
1415 anglePotential().set(paramName, typeId, value);
1418 #ifdef SIMP_DIHEDRAL 1419 if (command ==
"SET_DIHEDRAL") {
1420 if (nDihedralType_ == 0) {
1421 UTIL_THROW(
"SET_DIHEDRAL command with nDihedralType = 0");
1424 std::string paramName;
1427 inBuffer >> paramName >> typeId >> value;
1428 dihedralPotential().set(paramName, typeId, value);
1431 if (command ==
"SET_GROUP") {
1434 if (command ==
"FINISH") {
1438 Log::file() <<
"Error: Unknown command " << std::endl;
1451 if (fileMaster().commandFileName().empty()) {
1454 readCommands(fileMaster().commandFile());
1462 reverseUpdateFlag_ = reverseUpdateFlag;
1463 if (pairPotentialPtr_) {
1464 pairPotential().setReverseUpdateFlag(reverseUpdateFlag);
1477 atomStorage_.begin(atomIter);
1478 for( ; atomIter.
notEnd(); ++atomIter){
1479 mass = atomType(atomIter->typeId()).mass();
1481 scale = sqrt(temperature/mass);
1483 atomIter->velocity()[i] = scale*random_.gaussian();
1488 velocitySignal().notify();
1497 Vector momentumLocal(0.0);
1498 Vector momentumTotal(0.0);
1500 double massLocal = 0.0;
1501 double massTotal = 0.0;
1506 atomStorage_.begin(atomIter);
1507 for( ; atomIter.
notEnd(); ++atomIter){
1508 mass = atomType(atomIter->typeId()).mass();
1509 massLocal = massLocal + mass;
1511 momentum[j] = atomIter->velocity()[j];
1512 momentum[j] *= mass;
1514 momentumLocal += momentum;
1518 domain_.communicator().Allreduce(&massLocal, &massTotal, 1,
1519 MPI::DOUBLE, MPI::SUM);
1520 domain_.communicator().Allreduce(&momentumLocal[0],
1522 MPI::DOUBLE, MPI::SUM);
1525 Vector drift = momentumTotal;
1527 atomStorage_.begin(atomIter);
1528 for( ; atomIter.
notEnd(); ++atomIter) {
1529 atomIter->velocity() -= drift;
1533 velocitySignal().notify();
1545 { atomStorage_.zeroForces(reverseUpdateFlag_); }
1553 pairPotential().computeForces();
1556 bondPotential().computeForces();
1561 anglePotential().computeForces();
1564 #ifdef SIMP_DIHEDRAL 1565 if (nDihedralType_) {
1566 dihedralPotential().computeForces();
1569 #ifdef SIMP_EXTERNAL 1571 externalPotential().computeForces();
1576 if (reverseUpdateFlag_) {
1577 exchanger_.reverseUpdate();
1587 pairPotential().computeForcesAndStress(domain_.communicator());
1590 bondPotential().computeForcesAndStress(domain_.communicator());
1595 anglePotential().computeForcesAndStress(domain_.communicator());
1598 #ifdef SIMP_DIHEDRAL 1599 if (nDihedralType_) {
1600 dihedralPotential().computeForcesAndStress(domain_.communicator());
1603 #ifdef SIMP_EXTERNAL 1605 externalPotential().computeForces();
1610 if (reverseUpdateFlag_) {
1611 exchanger_.reverseUpdate();
1623 if (kineticEnergy_.isSet())
return;
1625 double localEnergy = 0.0;
1631 atomStorage_.begin(atomIter);
1632 for( ; atomIter.
notEnd(); ++atomIter){
1633 typeId = atomIter->typeId();
1634 mass = atomTypes_[typeId].mass();
1635 localEnergy += mass*(atomIter->velocity().square());
1637 localEnergy = 0.5*localEnergy;
1641 double totalEnergy = 0.0;
1642 domain_.communicator().Reduce(&localEnergy, &totalEnergy, 1,
1643 MPI::DOUBLE, MPI::SUM, 0);
1644 if (domain_.communicator().Get_rank() != 0) {
1647 kineticEnergy_.set(totalEnergy);
1649 kineticEnergy_.set(localEnergy);
1659 {
return kineticEnergy_.value(); }
1665 { kineticEnergy_.unset(); }
1678 if (kineticStress_.isSet())
return;
1689 atomStorage_.begin(atomIter);
1690 for( ; atomIter.
notEnd(); ++atomIter){
1691 typeId = atomIter->typeId();
1692 velocity = atomIter->velocity();
1693 mass = atomTypes_[typeId].mass();
1696 localStress(i, j) += mass*velocity[i]*velocity[j];
1702 localStress /= boundary().volume();
1707 domain_.communicator().Reduce(&localStress(0, 0), &totalStress(0, 0),
1709 if (domain_.communicator().Get_rank() != 0) {
1712 kineticStress_.set(totalStress);
1714 kineticStress_.set(localStress);
1722 {
return kineticStress_.value(); }
1729 double pressure = 0.0;
1731 pressure += kineticStress_.value()(i, i);
1741 { kineticStress_.unset(); }
1751 pairPotential().computeEnergy(domain_.communicator());
1754 bondPotential().computeEnergy(domain_.communicator());
1759 anglePotential().computeEnergy(domain_.communicator());
1762 #ifdef SIMP_DIHEDRAL 1763 if (nDihedralType_) {
1764 dihedralPotential().computeEnergy(domain_.communicator());
1767 #ifdef SIMP_EXTERNAL 1769 externalPotential().computeEnergy(domain_.communicator());
1780 pairPotential().computeEnergy();
1783 bondPotential().computeEnergy();
1788 anglePotential().computeEnergy();
1791 #ifdef SIMP_DIHEDRAL 1792 if (nDihedralType_) {
1793 dihedralPotential().computeEnergy();
1796 #ifdef SIMP_EXTERNAL 1798 externalPotential().computeEnergy();
1819 double energy = 0.0;
1820 energy += pairPotential().energy();
1823 energy += bondPotential().energy();
1828 energy += anglePotential().energy();
1831 #ifdef SIMP_DIHEDRAL 1832 if (nDihedralType_) {
1833 energy += dihedralPotential().energy();
1836 #ifdef SIMP_EXTERNAL 1838 energy += externalPotential().energy();
1849 pairPotential().unsetEnergy();
1852 bondPotential().unsetEnergy();
1857 anglePotential().unsetEnergy();
1860 #ifdef SIMP_DIHEDRAL 1861 if (nDihedralType_) {
1862 dihedralPotential().unsetEnergy();
1865 #ifdef SIMP_EXTERNAL 1867 externalPotential().unsetEnergy();
1880 pairPotential().computeStress(domain_.communicator());
1883 bondPotential().computeStress(domain_.communicator());
1888 anglePotential().computeStress(domain_.communicator());
1891 #ifdef SIMP_DIHEDRAL 1892 if (nDihedralType_) {
1893 dihedralPotential().computeStress(domain_.communicator());
1903 pairPotential().computeStress();
1906 bondPotential().computeStress();
1911 anglePotential().computeStress();
1914 #ifdef SIMP_DIHEDRAL 1915 if (nDihedralType_) {
1916 dihedralPotential().computeStress();
1929 stress += pairPotential().stress();
1932 stress += bondPotential().stress();
1937 stress += anglePotential().stress();
1940 #ifdef SIMP_DIHEDRAL 1941 if (nDihedralType_) {
1942 stress += dihedralPotential().stress();
1955 pressure += pairPotential().pressure();
1958 pressure += bondPotential().pressure();
1963 pressure += anglePotential().pressure();
1966 #ifdef SIMP_DIHEDRAL 1967 if (nDihedralType_) {
1968 pressure += dihedralPotential().pressure();
1979 { pairPotential().computePairEnergies(domain_.communicator()); }
1985 { pairPotential().computePairEnergies(); }
1994 pairEnergies.
allocate(nAtomType_, nAtomType_);
1995 pairEnergies = pairPotential().pairEnergies();
1996 return pairEnergies;
2004 pairPotential().unsetStress();
2007 bondPotential().unsetStress();
2012 anglePotential().unsetStress();
2015 #ifdef SIMP_DIHEDRAL 2016 if (nDihedralType_) {
2017 dihedralPotential().unsetStress();
2029 if (configIoPtr_ == 0) {
2038 return *configIoPtr_;
2046 if (serializeConfigIoPtr_ == 0) {
2049 return *serializeConfigIoPtr_;
2059 std::ifstream inputFile;
2060 if (domain_.isMaster()) {
2061 fileMaster().openInputFile(filename, inputFile);
2063 configIo().readConfig(inputFile, maskedPairPolicy_);
2064 exchanger_.exchange();
2065 if (domain_.isMaster()) {
2075 std::ofstream outputFile;
2076 if (domain_.isMaster()) {
2077 fileMaster().openOutputFile(filename, outputFile);
2079 configIo().writeConfig(outputFile);
2080 if (domain_.isMaster()) {
2092 if (!pairFactoryPtr_) {
2095 assert(pairFactoryPtr_);
2096 return *pairFactoryPtr_;
2103 {
return pairStyle_; }
2111 if (!bondFactoryPtr_) {
2114 assert(bondFactoryPtr_);
2115 return *bondFactoryPtr_;
2122 {
return bondStyle_; }
2131 if (angleFactoryPtr_ == 0) {
2134 assert(angleFactoryPtr_);
2135 return *angleFactoryPtr_;
2142 {
return angleStyle_; }
2145 #ifdef SIMP_DIHEDRAL 2151 if (dihedralFactoryPtr_ == 0) {
2154 assert(dihedralFactoryPtr_);
2155 return *dihedralFactoryPtr_;
2162 {
return dihedralStyle_; }
2165 #ifdef SIMP_EXTERNAL 2171 if (externalFactoryPtr_ == 0) {
2174 assert(externalFactoryPtr_);
2175 return *externalFactoryPtr_;
2182 {
return externalStyle_; }
2192 if (integratorFactoryPtr_ == 0) {
2195 assert(integratorFactoryPtr_);
2196 return *integratorFactoryPtr_;
2204 if (configIoFactoryPtr_ == 0) {
2207 assert(configIoFactoryPtr_);
2208 return *configIoFactoryPtr_;
2216 ConfigIo* ptr = configIoFactory().factory(classname);
2218 UTIL_THROW(
"Unrecognized ConfigIo subclass name");
2221 delete configIoPtr_;
2231 void Simulation::setGroup(std::stringstream& inBuffer)
2235 unsigned int groupId;
2236 inBuffer >> groupId;
2240 std::string groupStyle;
2241 inBuffer >> groupStyle;
2244 if (groupStyle ==
"delete") {
2245 for (atomStorage_.begin(iter) ; iter.
notEnd(); ++iter) {
2246 bit.
clear(iter->groups());
2249 if (groupStyle ==
"all") {
2250 for (atomStorage_.begin(iter) ; iter.
notEnd(); ++iter) {
2251 bit.
set(iter->groups());
2254 if (groupStyle ==
"atomType") {
2257 for (atomStorage_.begin(iter) ; iter.
notEnd(); ++iter) {
2258 if (iter->typeId() == typeId) {
2259 bit.
set(iter->groups());
2263 if (groupStyle ==
"species") {
2268 inBuffer >> speciesId;
2269 for (atomStorage_.begin(iter) ; iter.
notEnd(); ++iter) {
2270 if (iter->context().speciesId == speciesId) {
2271 bit.
set(iter->groups());
2275 std::string msg =
"SET_GROUP command with unknown group style ";
2289 out <<
"-g Debugging ON " << std::endl;
2291 out <<
"-g Debugging OFF" << std::endl;
2294 out <<
"-m MPI ON " << std::endl;
2296 out <<
"-m MPI OFF" << std::endl;
2299 out <<
"-c Coulomb ON " << std::endl;
2301 out <<
"-c Coulomb OFF" << std::endl;
2304 out <<
"-np Pairs ON " << std::endl;
2306 out <<
"-np Pairs OFF" << std::endl;
2309 out <<
"-b Bonds ON " << std::endl;
2311 out <<
"-b Bonds OFF" << std::endl;
2314 out <<
"-a Angles ON " << std::endl;
2316 out <<
"-a Angles OFF" << std::endl;
2318 #ifdef SIMP_DIHEDRAL 2319 out <<
"-d Dihedrals ON " << std::endl;
2321 out <<
"-d Dihedrals OFF" << std::endl;
2323 #ifdef SIMP_EXTERNAL 2324 out <<
"-e External ON " << std::endl;
2326 out <<
"-e External OFF" << std::endl;
2329 out <<
"-s Special ON " << std::endl;
2331 out <<
"-s Special OFF" << std::endl;
2341 atomStorage_.isValid(domain_.communicator());
2343 atomStorage_.isValid();
2347 int nGhost = atomStorage_.nGhost();
2350 domain_.communicator().Reduce(&nGhost, &nGhostAll, 1,
2351 MPI::INT, MPI::SUM, 0);
2352 bcast(domain_.communicator(), nGhostAll, 0);
2356 bool hasGhosts = bool(nGhostAll);
2362 bondStorage_.isValid(atomStorage_, domain_.communicator(), hasGhosts);
2367 angleStorage_.isValid(atomStorage_, domain_.communicator(), hasGhosts);
2370 #ifdef SIMP_DIHEDRAL 2371 if (nDihedralType_) {
2372 dihedralStorage_.isValid(atomStorage_, domain_.communicator(),
2376 #else // ifdef UTIL_MPI 2379 bondStorage_.isValid(atomStorage_, hasGhosts);
2384 angleStorage_.isValid(atomStorage_, hasGhosts);
2387 #ifdef SIMP_DIHEDRAL 2388 if (nDihedralType_) {
2389 dihedralStorage_.isValid(atomStorage_, hasGhosts);
2392 #endif // ifdef UTIL_MPI 2396 pairPotential().isValid(domain_.communicator());
2399 bondPotential().isValid(domain_.communicator());
2404 anglePotential().isValid(domain_.communicator());
2407 #ifdef SIMP_DIHEDRAL 2408 if (nDihedralType_) {
2409 dihedralPotential().isValid(domain_.communicator());
2412 #ifdef SIMP_EXTERNAL 2414 externalPotential().isValid(domain_.communicator());
2417 #endif // ifdef UTIL_MPI void writeConfig(const std::string &filename)
Write configuration file.
void computeForcesAndVirial()
Compute forces for all local atoms and virial stress.
void readPotentialStyles(std::istream &in)
Read potential styles and maskedPairPolicy.
void loadEnsembles(Serializable::IArchive &ar)
Load energy and boundary ensembles from an input archive.
void set(unsigned int &flags) const
Set this bit in the flags parameter.
const int Dimension
Dimensionality of space.
static void commitMpiType()
Commit MPI datatype MpiTraits<Vector>::type.
A Vector is a Cartesian vector.
static void commitMpiType()
Commit MPI datatype MpiTraits<IntVector>::type.
virtual void loadParameters(Serializable::IArchive &ar)
Load parameters from a restart archive.
void setConfigIo(std::string &classname)
Create a new configuration file reader/writer.
void setBoltzmannVelocities(double temperature)
Set random velocities chosen from a Boltzmann distribution.
Factory< ConfigIo > & configIoFactory()
Get the configuration file reader/writer factory by reference.
virtual void readParameters(std::istream &in)
Read body of the parameter block, allocate memory and initialize.
void computeForces()
Compute forces for all local atoms.
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
void readConfig(const std::string &filename)
Read configuration file on master and distribute atoms.
double kineticEnergy()
Return precomputed total kinetic energy.
Factory< DihedralPotential > & dihedralFactory()
Get the associated Dihedral Factory by reference.
Default Factory for subclasses of ConfigIo.
virtual void readParam()
Read parameters from default parameter file.
static void initStatic()
Call this to guarantee initialization of static data.
Manager for a list of Analyzer objects.
double kineticPressure() const
Return total kinetic pressure.
Factory for PairPotential objects.
DMatrix< double > pairEnergies() const
Return precomputed pair energies.
Statistical ensemble for changes in the periodic unit cell size.
static void setEcho(bool echo=true)
Enable or disable echoing for all subclasses of ParamComponent.
void unsetPotentialEnergies()
Mark all potential energies as unknown.
Tensor & zero()
Set all elements of this tensor to zero.
void zeroForces()
Set forces for all atoms to zero.
Parallel domain decomposition (DD) MD simulation.
Classes used by all simpatico molecular simulations.
void initStatic()
Guarantee initialization of all static class members in Util namespace.
void readCommands()
Read and execute commands from the default command file.
Factory< BondPotential > & bondFactory()
Get the Factory<BondPotential> by reference.
double virialPressure() const
Return total virial pressure.
void outputOptions(std::ostream &out) const
Output a list of options enabled & disabled during compilation.
Native / default format for configuration files.
A Tensor represents a Cartesian tensor.
void clear(unsigned int &flags) const
Clear this bit in the flags parameter.
void unsetVirialStress()
Mark all virial stress contributions as unknown.
void load(const std::string &filename)
Load internal state from a restart file.
void saveEnsembles(Serializable::OArchive &ar)
Save energy and boundary ensembles to archive.
void unsetNTotal()
Mark nTotal as unknown.
static void setFile(std::ofstream &file)
Set the log ostream to a file.
Saving / output archive for binary ostream.
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
Factory< ExternalPotential > & externalFactory()
Get the associated External Factory by reference.
A statistical ensemble for energy.
void unsetKineticStress()
Mark kinetic stress as unknown.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
void unsetKineticEnergy()
Mark kinetic energy as unknown.
Factory for BondPotential objects.
void bcast(MPI::Intracomm &comm, T &data, int root)
Broadcast a single T value.
std::string bondStyle() const
Return covalent bond style string.
Factory for subclasses of Integrator (i.e., MD integrators).
void computePotentialEnergies()
Calculate and store total potential energy on all processors.
Factory< Integrator > & integratorFactory()
Get the Integrator factory by reference.
void saveFileMaster(Serializable::OArchive &ar)
Save FileMaster to archive.
Factory for ExternalPotential objects.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
void computePairEnergies()
Compute pair energies for each pair of atom types.
static bool hasAtomContext()
Is AtomContext data enabled?
void loadPotentialStyles(Serializable::IArchive &ar)
Load potential styles.
Tensor kineticStress() const
Return total kinetic stress.
Factory for DihedralPotential objects.
void readEnsembles(std::istream &in)
Read energy and boundary ensembles.
void readFileMaster(std::istream &in)
Read the FileMaster.
void setReverseUpdateFlag(bool reverseUpdateFlag)
Set flag to identify if reverse communication is enabled.
void setOptions(int argc, char *const *argv)
Process command line options.
void computeVirialStress()
Calculate and store all virial stress contributions.
Factory< AnglePotential > & angleFactory()
Get the AngleFactory by reference.
static std::ostream & file()
Get log ostream by reference.
Factory< PairPotential > & pairFactory()
Get the Factory<PairPotential> by reference.
A FileMaster manages input and output files for a simulation.
double potentialEnergy() const
Return sum of precomputed total potential energies.
Manager for a set of Modifier objects.
Saving archive for binary istream.
Abstract reader/writer for configuration files.
static void setHasAtomContext(bool hasAtomContext)
Enable (true) or disable (false) use of AtomContext data.
std::ifstream & file()
Get the underlying ifstream by reference.
Container for Group<3> (angle) objects.
Save / load configuration from / to an archive.
static int max()
Return the maximum amount of allocated heap memory thus far.
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
Vector removeDriftVelocity()
Subtract system center of mass velocity from atom velocities.
Factory for AnglePotential objects.
std::string pairStyle() const
Return nonbonded pair style string.
Represents a specific bit location within an unsigned int.
Container for Group<4> (dihedral) objects.
std::string dihedralStyle() const
Return dihedral potential style string.
bool isValid()
Return true if this Simulation is valid, or throw an Exception.
void computeKineticEnergy()
Compute total kinetic energy.
void savePotentialStyles(Serializable::OArchive &ar)
Save potential styles to archive.
Tensor virialStress() const
Return total virial stress.
Iterator for all atoms owned by an AtomStorage.
void save(const std::string &filename)
Save state to a restart file.
std::ofstream & file()
Get the underlying ifstream by reference.
static void saveOptional(Serializable::OArchive &ar, Type &value, bool isActive)
Save an optional parameter value to an output archive.
void loadFileMaster(Serializable::IArchive &ar)
Load FileMaster from an archive.
void computeKineticStress()
Calculate and store kinetic stress.
std::string externalStyle() const
Return external potential style string.
Simulation(MPI::Intracomm &communicator=MPI::COMM_WORLD)
Constructor.
Container for for Group<2> (bond) objects.
std::string angleStyle() const
Return angle potential style string.