--- branches/development/src/nonbonded/LJ.cpp 2012/02/29 20:33:01 1683 +++ trunk/src/nonbonded/LJ.cpp 2014/09/02 18:31:44 2017 @@ -35,7 +35,7 @@ * * [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, 24107 (2008). + * [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). */ @@ -46,56 +46,19 @@ #include #include "nonbonded/LJ.hpp" #include "utils/simError.h" +#include "types/LennardJonesAdapter.hpp" #include "types/LennardJonesInteractionType.hpp" namespace OpenMD { LJ::LJ() : name_("LJ"), initialized_(false), forceField_(NULL) {} - LJParam LJ::getLJParam(AtomType* atomType) { - - // Do sanity checking on the AtomType we were passed before - // building any data structures: - if (!atomType->isLennardJones()) { - sprintf( painCave.errMsg, - "LJ::getLJParam was passed an atomType (%s) that does not\n" - "\tappear to be a Lennard-Jones atom.\n", - atomType->getName().c_str()); - painCave.severity = OPENMD_ERROR; - painCave.isFatal = 1; - simError(); - } - - GenericData* data = atomType->getPropertyByName("LennardJones"); - if (data == NULL) { - sprintf( painCave.errMsg, "LJ::getLJParam could not find Lennard-Jones\n" - "\tparameters for atomType %s.\n", atomType->getName().c_str()); - painCave.severity = OPENMD_ERROR; - painCave.isFatal = 1; - simError(); - } - - LJParamGenericData* ljData = dynamic_cast(data); - if (ljData == NULL) { - sprintf( painCave.errMsg, - "LJ::getLJParam could not convert GenericData to LJParam for\n" - "\tatom type %s\n", atomType->getName().c_str()); - painCave.severity = OPENMD_ERROR; - painCave.isFatal = 1; - simError(); - } - - return ljData->getData(); - } + RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) { - RealType LJ::getSigma(AtomType* atomType) { - LJParam ljParam = getLJParam(atomType); - return ljParam.sigma; - } - - RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) { - RealType sigma1 = getSigma(atomType1); - RealType sigma2 = getSigma(atomType2); + LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1); + LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2); + RealType sigma1 = lja1.getSigma(); + RealType sigma2 = lja2.getSigma(); ForceFieldOptions& fopts = forceField_->getForceFieldOptions(); string DistanceMix = fopts.getDistanceMixingRule(); @@ -107,34 +70,40 @@ namespace OpenMD { return 0.5 * (sigma1 + sigma2); } - RealType LJ::getEpsilon(AtomType* atomType) { - LJParam ljParam = getLJParam(atomType); - return ljParam.epsilon; - } - - RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) { - RealType epsilon1 = getEpsilon(atomType1); - RealType epsilon2 = getEpsilon(atomType2); + RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) { + LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1); + LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2); + + RealType epsilon1 = lja1.getEpsilon(); + RealType epsilon2 = lja2.getEpsilon(); return sqrt(epsilon1 * epsilon2); } void LJ::initialize() { - ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes(); - ForceField::AtomTypeContainer::MapTypeIterator i; - AtomType* at; - for (at = atomTypes->beginType(i); at != NULL; - at = atomTypes->nextType(i)) { - if (at->isLennardJones()){ - addType(at); - } + LJtypes.clear(); + LJtids.clear(); + MixingMap.clear(); + nLJ_ = 0; + + LJtids.resize( forceField_->getNAtomType(), -1); + + set::iterator at; + for (at = simTypes_.begin(); at != simTypes_.end(); ++at) { + if ((*at)->isLennardJones()) nLJ_++; } + + MixingMap.resize(nLJ_); + + for (at = simTypes_.begin(); at != simTypes_.end(); ++at) { + if ((*at)->isLennardJones()) addType(*at); + } + ForceField::NonBondedInteractionTypeContainer* nbiTypes = forceField_->getNonBondedInteractionTypes(); ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j; NonBondedInteractionType* nbt; ForceField::NonBondedInteractionTypeContainer::KeyType keys; - for (nbt = nbiTypes->beginType(j); nbt != NULL; nbt = nbiTypes->nextType(j)) { @@ -163,47 +132,50 @@ namespace OpenMD { } initialized_ = true; } - void LJ::addType(AtomType* atomType){ - RealType sigma1 = getSigma(atomType); - RealType epsilon1 = getEpsilon(atomType); + LennardJonesAdapter lja1 = LennardJonesAdapter(atomType); + + RealType sigma1 = lja1.getSigma(); + RealType epsilon1 = lja1.getEpsilon(); // add it to the map: - AtomTypeProperties atp = atomType->getATP(); - - pair::iterator,bool> ret; - ret = LJMap.insert( pair(atp.ident, atomType) ); + int atid = atomType->getIdent(); + int ljtid = LJtypes.size(); + + pair::iterator,bool> ret; + ret = LJtypes.insert( atid ); if (ret.second == false) { sprintf( painCave.errMsg, "LJ already had a previous entry with ident %d\n", - atp.ident); + atid) ; painCave.severity = OPENMD_INFO; painCave.isFatal = 0; simError(); } - + + LJtids[atid] = ljtid; + MixingMap[ljtid].resize( nLJ_ ); + // Now, iterate over all known types and add to the mixing map: - std::map::iterator it; - for( it = LJMap.begin(); it != LJMap.end(); ++it) { - - AtomType* atype2 = (*it).second; + std::set::iterator it; + for( it = LJtypes.begin(); it != LJtypes.end(); ++it) { + int ljtid2 = LJtids[ (*it) ]; + AtomType* atype2 = forceField_->getAtomType( (*it) ); + LJInteractionData mixer; mixer.sigma = getSigma(atomType, atype2); mixer.epsilon = getEpsilon(atomType, atype2); mixer.sigmai = 1.0 / mixer.sigma; mixer.explicitlySet = false; + MixingMap[ljtid2].resize( nLJ_ ); - std::pair key1, key2; - key1 = std::make_pair(atomType, atype2); - key2 = std::make_pair(atype2, atomType); - - MixingMap[key1] = mixer; - if (key2 != key1) { - MixingMap[key2] = mixer; + MixingMap[ljtid][ljtid2] = mixer; + if (ljtid2 != ljtid) { + MixingMap[ljtid2][ljtid] = mixer; } } } @@ -216,61 +188,84 @@ namespace OpenMD { mixer.sigmai = 1.0 / mixer.sigma; mixer.explicitlySet = true; - std::pair key1, key2; - key1 = std::make_pair(atype1, atype2); - key2 = std::make_pair(atype2, atype1); + int atid1 = atype1->getIdent(); + int atid2 = atype2->getIdent(); + + int ljtid1, ljtid2; + + pair::iterator,bool> ret; + ret = LJtypes.insert( atid1 ); + if (ret.second == false) { + // already had this type in the LJMap, just get the ljtid: + ljtid1 = LJtids[ atid1 ]; + } else { + // didn't already have it, so make a new one and assign it: + ljtid1 = nLJ_; + LJtids[atid1] = nLJ_; + nLJ_++; + } + + ret = LJtypes.insert( atid2 ); + if (ret.second == false) { + // already had this type in the LJMap, just get the ljtid: + ljtid2 = LJtids[ atid2 ]; + } else { + // didn't already have it, so make a new one and assign it: + ljtid2 = nLJ_; + LJtids[atid2] = nLJ_; + nLJ_++; + } - MixingMap[key1] = mixer; - if (key2 != key1) { - MixingMap[key2] = mixer; + MixingMap.resize(nLJ_); + MixingMap[ljtid1].resize(nLJ_); + + MixingMap[ljtid1][ljtid2] = mixer; + if (ljtid2 != ljtid1) { + MixingMap[ljtid2].resize(nLJ_); + MixingMap[ljtid2][ljtid1] = mixer; } } void LJ::calcForce(InteractionData &idat) { if (!initialized_) initialize(); - map, LJInteractionData>::iterator it; - it = MixingMap.find( idat.atypes ); - if (it != MixingMap.end()) { - - LJInteractionData mixer = (*it).second; - - RealType sigmai = mixer.sigmai; - RealType epsilon = mixer.epsilon; - - RealType ros; - RealType rcos; - RealType myPot = 0.0; - RealType myPotC = 0.0; - RealType myDeriv = 0.0; - RealType myDerivC = 0.0; - - ros = *(idat.rij) * sigmai; - - getLJfunc(ros, myPot, myDeriv); - - if (idat.shiftedPot) { - rcos = *(idat.rcut) * sigmai; - getLJfunc(rcos, myPotC, myDerivC); - myDerivC = 0.0; - } else if (idat.shiftedForce) { - rcos = *(idat.rcut) * sigmai; - getLJfunc(rcos, myPotC, myDerivC); - myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai; - } else { - myPotC = 0.0; - myDerivC = 0.0; - } + LJInteractionData &mixer = MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]]; - RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC); - *(idat.vpair) += pot_temp; - - RealType dudr = *(idat.sw) * *(idat.vdwMult) * epsilon * (myDeriv - - myDerivC)*sigmai; - - (*(idat.pot))[VANDERWAALS_FAMILY] += *(idat.sw) * pot_temp; - *(idat.f1) += *(idat.d) * dudr / *(idat.rij); + RealType sigmai = mixer.sigmai; + RealType epsilon = mixer.epsilon; + + RealType ros; + RealType rcos; + RealType myPot = 0.0; + RealType myPotC = 0.0; + RealType myDeriv = 0.0; + RealType myDerivC = 0.0; + + ros = *(idat.rij) * sigmai; + + getLJfunc(ros, myPot, myDeriv); + + if (idat.shiftedPot) { + rcos = *(idat.rcut) * sigmai; + getLJfunc(rcos, myPotC, myDerivC); + myDerivC = 0.0; + } else if (idat.shiftedForce) { + rcos = *(idat.rcut) * sigmai; + getLJfunc(rcos, myPotC, myDerivC); + myPotC = myPotC + myDerivC * (*(idat.rij) - *(idat.rcut)) * sigmai; + } else { + myPotC = 0.0; + myDerivC = 0.0; } + + RealType pot_temp = *(idat.vdwMult) * epsilon * (myPot - myPotC); + *(idat.vpair) += pot_temp; + + RealType dudr = *(idat.sw) * *(idat.vdwMult) * epsilon * (myDeriv - + myDerivC)*sigmai; + (*(idat.pot))[VANDERWAALS_FAMILY] += *(idat.sw) * pot_temp; + *(idat.f1) += *(idat.d) * dudr / *(idat.rij); + return; } @@ -291,14 +286,17 @@ namespace OpenMD { RealType LJ::getSuggestedCutoffRadius(pair atypes) { if (!initialized_) initialize(); - map, LJInteractionData>::iterator it; - it = MixingMap.find(atypes); - if (it == MixingMap.end()) - return 0.0; - else { - LJInteractionData mixer = (*it).second; + + int atid1 = atypes.first->getIdent(); + int atid2 = atypes.second->getIdent(); + int ljtid1 = LJtids[atid1]; + int ljtid2 = LJtids[atid2]; + + if (ljtid1 == -1 || ljtid2 == -1) return 0.0; + else { + LJInteractionData mixer = MixingMap[ljtid1][ljtid2]; return 2.5 * mixer.sigma; } } - + }