--- trunk/src/constraints/ZconstraintForceManager.cpp 2005/10/13 22:26:47 665 +++ trunk/src/constraints/ZconstraintForceManager.cpp 2015/03/05 15:35:37 2067 @@ -6,19 +6,10 @@ * redistribute this software in source and binary code form, provided * that the following conditions are met: * - * 1. Acknowledgement of the program authors must be made in any - * publication of scientific results based in part on use of the - * program. An acceptable form of acknowledgement is citation of - * the article in which the program was described (Matthew - * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher - * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented - * Parallel Simulation Engine for Molecular Dynamics," - * J. Comput. Chem. 26, pp. 252-271 (2005)) - * - * 2. Redistributions of source code must retain the above copyright + * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - * 3. Redistributions in binary form must reproduce the above copyright + * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. @@ -37,15 +28,29 @@ * arising out of the use of or inability to use software, even if the * University of Notre Dame has been advised of the possibility of * such damages. + * + * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your + * research, please cite the appropriate papers when you publish your + * work. Good starting points are: + * + * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). + * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). + * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). + * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ +#ifdef IS_MPI +#include +#endif #include #include "constraints/ZconstraintForceManager.hpp" #include "integrators/Integrator.hpp" #include "utils/simError.h" -#include "utils/OOPSEConstant.hpp" +#include "utils/PhysicalConstants.hpp" #include "utils/StringUtils.hpp" -namespace oopse { + +namespace OpenMD { ZconstraintForceManager::ZconstraintForceManager(SimInfo* info): ForceManager(info), infiniteTime(1e31) { currSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); Globals* simParam = info_->getSimParams(); @@ -77,7 +82,7 @@ namespace oopse { zconsTol_ = 0.01; sprintf(painCave.errMsg, "ZConstraint Warning: Tolerance for z-constraint method is not specified.\n" - "\tOOPSE will use a default value of %f.\n" + "\tOpenMD will use a default value of %f.\n" "\tTo set the tolerance, use the zconsTol variable.\n", zconsTol_); painCave.isFatal = 0; @@ -111,17 +116,17 @@ namespace oopse { //estimate the force constant of harmonical potential Mat3x3d hmat = currSnapshot_->getHmat(); - double halfOfLargestBox = std::max(hmat(0, 0), std::max(hmat(1, 1), hmat(2, 2))) /2; - double targetTemp; + RealType halfOfLargestBox = std::max(hmat(0, 0), std::max(hmat(1, 1), hmat(2, 2))) /2; + RealType targetTemp; if (simParam->haveTargetTemp()) { targetTemp = simParam->getTargetTemp(); } else { targetTemp = 298.0; } - double zforceConstant = OOPSEConstant::kb * targetTemp / (halfOfLargestBox * halfOfLargestBox); + RealType zforceConstant = PhysicalConstants::kb * targetTemp / (halfOfLargestBox * halfOfLargestBox); - int nZconstraints = simParam->getNZconstraints(); - ZconStamp** stamp = simParam->getZconStamp(); + int nZconstraints = simParam->getNZconsStamps(); + std::vector stamp = simParam->getZconsStamps(); // for (int i = 0; i < nZconstraints; i++){ @@ -148,7 +153,7 @@ namespace oopse { update(); //calculate masss of unconstraint molecules in the whole system (never change during the simulation) - double totMassUnconsMols_local = 0.0; + RealType totMassUnconsMols_local = 0.0; std::vector::iterator j; for ( j = unzconsMols_.begin(); j != unzconsMols_.end(); ++j) { totMassUnconsMols_local += (*j)->getMass(); @@ -156,8 +161,8 @@ namespace oopse { #ifndef IS_MPI totMassUnconsMols_ = totMassUnconsMols_local; #else - MPI_Allreduce(&totMassUnconsMols_local, &totMassUnconsMols_, 1, MPI_DOUBLE, - MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&totMassUnconsMols_local, &totMassUnconsMols_, 1, + MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); #endif // creat zconsWriter @@ -194,7 +199,7 @@ namespace oopse { zmol.param = i->second; zmol.cantPos = zmol.param.zTargetPos; /**@todo fixed me when zmol migrate, it is incorrect*/ Vector3d com = zmol.mol->getCom(); - double diff = fabs(zmol.param.zTargetPos - com[whichDirection]); + RealType diff = fabs(zmol.param.zTargetPos - com[whichDirection]); if (diff < zconsTol_) { fixedZMols_.push_back(zmol); } else { @@ -240,8 +245,8 @@ namespace oopse { currZconsTime_ = currSnapshot_->getTime(); } - void ZconstraintForceManager::calcForces(bool needPotential, bool needStress){ - ForceManager::calcForces(needPotential, needStress); + void ZconstraintForceManager::calcForces(){ + ForceManager::calcForces(); if (usingZconsGap_){ updateZPos(); @@ -282,25 +287,28 @@ namespace oopse { Vector3d vel; std::list::iterator i; Molecule* mol; - StuntDouble* integrableObject; + StuntDouble* sd; Molecule::IntegrableObjectIterator ii; //zero out the velocities of center of mass of fixed z-constrained molecules for(i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { + mol = i->mol; comVel = mol->getComVel(); - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { - vel = integrableObject->getVel(); + + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + + vel = sd->getVel(); vel[whichDirection] -= comVel[whichDirection]; - integrableObject->setVel(vel); + sd->setVel(vel); } } // calculate the vz of center of mass of moving molecules(include unconstrained molecules // and moving z-constrained molecules) - double pzMovingMols_local = 0.0; - double pzMovingMols; + RealType pzMovingMols_local = 0.0; + RealType pzMovingMols; for ( i = movingZMols_.begin(); i != movingZMols_.end(); ++i) { mol = i->mol; @@ -318,33 +326,37 @@ namespace oopse { #ifndef IS_MPI pzMovingMols = pzMovingMols_local; #else - MPI_Allreduce(&pzMovingMols_local, &pzMovingMols, 1, MPI_DOUBLE, - MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&pzMovingMols_local, &pzMovingMols, 1, + MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); #endif - double vzMovingMols = pzMovingMols / (totMassMovingZMols_ + totMassUnconsMols_); + RealType vzMovingMols = pzMovingMols / (totMassMovingZMols_ + totMassUnconsMols_); //modify the velocities of moving z-constrained molecuels for ( i = movingZMols_.begin(); i != movingZMols_.end(); ++i) { + mol = i->mol; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { - vel = integrableObject->getVel(); + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + + vel = sd->getVel(); vel[whichDirection] -= vzMovingMols; - integrableObject->setVel(vel); + sd->setVel(vel); } } //modify the velocites of unconstrained molecules for ( j = unzconsMols_.begin(); j != unzconsMols_.end(); ++j) { + mol =*j; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { - vel = integrableObject->getVel(); + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + + vel = sd->getVel(); vel[whichDirection] -= vzMovingMols; - integrableObject->setVel(vel); + sd->setVel(vel); } } @@ -352,8 +364,8 @@ namespace oopse { void ZconstraintForceManager::doZconstraintForce(){ - double totalFZ; - double totalFZ_local; + RealType totalFZ; + RealType totalFZ_local; Vector3d com; Vector3d force(0.0); @@ -366,16 +378,18 @@ namespace oopse { //calculate the total z-contrained force of fixed z-contrained molecules std::list::iterator i; Molecule* mol; - StuntDouble* integrableObject; + StuntDouble* sd; Molecule::IntegrableObjectIterator ii; for ( i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { + mol = i->mol; i->fz = 0.0; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { - force = integrableObject->getFrc(); + for( sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + + force = sd->getFrc(); i->fz += force[whichDirection]; } totalFZ_local += i->fz; @@ -383,7 +397,8 @@ namespace oopse { //calculate total z-constraint force #ifdef IS_MPI - MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&totalFZ_local, &totalFZ, 1, + MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); #else totalFZ = totalFZ_local; #endif @@ -391,35 +406,41 @@ namespace oopse { // apply negative to fixed z-constrained molecues; for ( i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { + mol = i->mol; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { - force[whichDirection] = -getZFOfFixedZMols(mol, integrableObject, i->fz); - integrableObject->addFrc(force); + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + + force[whichDirection] = -getZFOfFixedZMols(mol, sd, i->fz); + sd->addFrc(force); } } //modify the forces of moving z-constrained molecules for ( i = movingZMols_.begin(); i != movingZMols_.end(); ++i) { + mol = i->mol; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + force[whichDirection] = -getZFOfMovingMols(mol,totalFZ); - integrableObject->addFrc(force); + sd->addFrc(force); } } //modify the forces of unconstrained molecules std::vector::iterator j; for ( j = unzconsMols_.begin(); j != unzconsMols_.end(); ++j) { + mol =*j; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + force[whichDirection] = -getZFOfMovingMols(mol, totalFZ); - integrableObject->addFrc(force); + sd->addFrc(force); } } @@ -427,48 +448,54 @@ namespace oopse { void ZconstraintForceManager::doHarmonic(){ - double totalFZ; + RealType totalFZ; Vector3d force(0.0); Vector3d com; - double totalFZ_local = 0; + RealType totalFZ_local = 0; + RealType lrPot; std::list::iterator i; - StuntDouble* integrableObject; + StuntDouble* sd; Molecule::IntegrableObjectIterator ii; Molecule* mol; for ( i = movingZMols_.begin(); i != movingZMols_.end(); ++i) { mol = i->mol; com = mol->getCom(); - double resPos = usingSMD_? i->cantPos : i->param.zTargetPos; - double diff = com[whichDirection] - resPos; - double harmonicU = 0.5 * i->param.kz * diff * diff; - currSnapshot_->statData[Stats::LONG_RANGE_POTENTIAL] += harmonicU; - double harmonicF = -i->param.kz * diff; + RealType resPos = usingSMD_? i->cantPos : i->param.zTargetPos; + RealType diff = com[whichDirection] - resPos; + RealType harmonicU = 0.5 * i->param.kz * diff * diff; + lrPot = currSnapshot_->getLongRangePotential(); + lrPot += harmonicU; + currSnapshot_->setLongRangePotential(lrPot); + RealType harmonicF = -i->param.kz * diff; totalFZ_local += harmonicF; //adjust force - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { - force[whichDirection] = getHFOfFixedZMols(mol, integrableObject, harmonicF); - integrableObject->addFrc(force); + force[whichDirection] = getHFOfFixedZMols(mol, sd, harmonicF); + sd->addFrc(force); } } #ifndef IS_MPI totalFZ = totalFZ_local; #else - MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_REALTYPE, + MPI_SUM, MPI_COMM_WORLD); #endif //modify the forces of unconstrained molecules std::vector::iterator j; for ( j = unzconsMols_.begin(); j != unzconsMols_.end(); ++j) { + mol = *j; - for(integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; - integrableObject = mol->nextIntegrableObject(ii)) { + for(sd = mol->beginIntegrableObject(ii); sd != NULL; + sd = mol->nextIntegrableObject(ii)) { + force[whichDirection] = getHFOfUnconsMols(mol, totalFZ); - integrableObject->addFrc(force); + sd->addFrc(force); } } @@ -476,7 +503,7 @@ namespace oopse { bool ZconstraintForceManager::checkZConsState(){ Vector3d com; - double diff; + RealType diff; int changed_local = 0; std::list::iterator i; @@ -540,8 +567,8 @@ namespace oopse { #ifndef IS_MPI havingFixed = havingFixed_local; #else - MPI_Allreduce(&havingFixed_local, &havingFixed, 1, MPI_INT, MPI_SUM, - MPI_COMM_WORLD); + MPI_Allreduce(&havingFixed_local, &havingFixed, 1, + MPI_INT, MPI_SUM, MPI_COMM_WORLD); #endif return havingFixed > 0; @@ -557,8 +584,8 @@ namespace oopse { #ifndef IS_MPI havingMoving = havingMoving_local; #else - MPI_Allreduce(&havingMoving_local, &havingMoving, 1, MPI_INT, MPI_SUM, - MPI_COMM_WORLD); + MPI_Allreduce(&havingMoving_local, &havingMoving, 1, + MPI_INT, MPI_SUM, MPI_COMM_WORLD); #endif return havingMoving > 0; @@ -566,39 +593,38 @@ namespace oopse { void ZconstraintForceManager::calcTotalMassMovingZMols(){ - double totMassMovingZMols_local = 0.0; + RealType totMassMovingZMols_local = 0.0; std::list::iterator i; for ( i = movingZMols_.begin(); i != movingZMols_.end(); ++i) { totMassMovingZMols_local += i->mol->getMass(); } #ifdef IS_MPI - MPI_Allreduce(&totMassMovingZMols_local, &totMassMovingZMols_, 1, MPI_DOUBLE, - MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&totMassMovingZMols_local, &totMassMovingZMols_, + 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); #else totMassMovingZMols_ = totMassMovingZMols_local; #endif } - double ZconstraintForceManager::getZFOfFixedZMols(Molecule* mol, StuntDouble* sd, double totalForce){ + RealType ZconstraintForceManager::getZFOfFixedZMols(Molecule* mol, StuntDouble* sd, RealType totalForce){ return totalForce * sd->getMass() / mol->getMass(); } - double ZconstraintForceManager::getZFOfMovingMols(Molecule* mol, double totalForce){ + RealType ZconstraintForceManager::getZFOfMovingMols(Molecule* mol, RealType totalForce){ return totalForce * mol->getMass() / (totMassUnconsMols_ + totMassMovingZMols_); } - double ZconstraintForceManager::getHFOfFixedZMols(Molecule* mol, StuntDouble*sd, double totalForce){ + RealType ZconstraintForceManager::getHFOfFixedZMols(Molecule* mol, StuntDouble*sd, RealType totalForce){ return totalForce * sd->getMass() / mol->getMass(); } - double ZconstraintForceManager::getHFOfUnconsMols(Molecule* mol, double totalForce){ + RealType ZconstraintForceManager::getHFOfUnconsMols(Molecule* mol, RealType totalForce){ return totalForce * mol->getMass() / totMassUnconsMols_; } void ZconstraintForceManager::updateZPos(){ - double curTime = currSnapshot_->getTime(); std::list::iterator i; for ( i = fixedZMols_.begin(); i != fixedZMols_.end(); ++i) { i->param.zTargetPos += zconsGap_; @@ -612,8 +638,8 @@ namespace oopse { } } - double ZconstraintForceManager::getZTargetPos(int index){ - double zTargetPos; + RealType ZconstraintForceManager::getZTargetPos(int index){ + RealType zTargetPos; #ifndef IS_MPI Molecule* mol = info_->getMoleculeByGlobalIndex(index); assert(mol); @@ -621,7 +647,7 @@ namespace oopse { zTargetPos = com[whichDirection]; #else int whicProc = info_->getMolToProc(index); - MPI_Bcast(&zTargetPos, 1, MPI_DOUBLE, whicProc, MPI_COMM_WORLD); + MPI_Bcast(&zTargetPos, 1, MPI_REALTYPE, whicProc, MPI_COMM_WORLD); #endif return zTargetPos; }