48#include "nonbonded/LJ.hpp"
54#include "types/LennardJonesAdapter.hpp"
55#include "types/LennardJonesInteractionType.hpp"
56#include "utils/simError.h"
60 LJ::LJ() : initialized_(false), forceField_(NULL), name_(
"LJ") {}
62 RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
63 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
64 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
65 RealType sigma1 = lja1.getSigma();
66 RealType sigma2 = lja2.getSigma();
68 ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
69 string DistanceMix = fopts.getDistanceMixingRule();
72 if (DistanceMix ==
"GEOMETRIC")
73 return sqrt(sigma1 * sigma2);
75 return 0.5 * (sigma1 + sigma2);
78 RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {
79 LennardJonesAdapter lja1 = LennardJonesAdapter(atomType1);
80 LennardJonesAdapter lja2 = LennardJonesAdapter(atomType2);
82 RealType epsilon1 = lja1.getEpsilon();
83 RealType epsilon2 = lja2.getEpsilon();
84 return sqrt(epsilon1 * epsilon2);
87 void LJ::initialize() {
93 LJtids.resize(forceField_->getNAtomType(), -1);
95 AtomTypeSet::iterator at;
96 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
97 if ((*at)->isLennardJones()) nLJ_++;
100 MixingMap.resize(nLJ_);
102 for (at = simTypes_.begin(); at != simTypes_.end(); ++at) {
103 if ((*at)->isLennardJones()) addType(*at);
106 ForceField::NonBondedInteractionTypeContainer* nbiTypes =
107 forceField_->getNonBondedInteractionTypes();
108 ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
109 NonBondedInteractionType* nbt;
110 ForceField::NonBondedInteractionTypeContainer::KeyType keys;
112 for (nbt = nbiTypes->beginType(j); nbt != NULL;
113 nbt = nbiTypes->nextType(j)) {
114 if (nbt->isLennardJones()) {
115 keys = nbiTypes->getKeys(j);
116 AtomType* at1 = forceField_->getAtomType(keys[0]);
118 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
119 "LennardJones::initialize could not find AtomType %s\n"
120 "\tto for for %s - %s interaction.\n",
121 keys[0].c_str(), keys[0].c_str(), keys[1].c_str());
122 painCave.severity = OPENMD_ERROR;
123 painCave.isFatal = 1;
127 AtomType* at2 = forceField_->getAtomType(keys[1]);
129 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
130 "LennardJones::initialize could not find AtomType %s\n"
131 "\tfor %s - %s nonbonded interaction.\n",
132 keys[1].c_str(), keys[0].c_str(), keys[1].c_str());
133 painCave.severity = OPENMD_ERROR;
134 painCave.isFatal = 1;
138 LennardJonesInteractionType* ljit =
139 dynamic_cast<LennardJonesInteractionType*
>(nbt);
143 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
144 "LJ::initialize could not convert NonBondedInteractionType\n"
145 "\tto LennardJonesInteractionType for %s - %s interaction.\n",
146 at1->getName().c_str(), at2->getName().c_str());
147 painCave.severity = OPENMD_ERROR;
148 painCave.isFatal = 1;
152 RealType sigma = ljit->getSigma();
153 RealType epsilon = ljit->getEpsilon();
154 addExplicitInteraction(at1, at2, sigma, epsilon);
160 void LJ::addType(AtomType* atomType) {
162 int atid = atomType->getIdent();
163 int ljtid = LJtypes.size();
165 pair<set<int>::iterator,
bool> ret;
166 ret = LJtypes.insert(atid);
167 if (ret.second ==
false) {
168 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
169 "LJ already had a previous entry with ident %d\n", atid);
170 painCave.severity = OPENMD_INFO;
171 painCave.isFatal = 0;
176 RealType s = getSigma(atomType, atomType);
177 if (fabs(s) < std::numeric_limits<RealType>::epsilon()) {
178 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
179 "Lennard-Jones atom %s was defined with a sigma value (%f)\n"
180 "\tthat was too close to zero.",
181 atomType->getName().c_str(), s);
182 painCave.severity = OPENMD_ERROR;
183 painCave.isFatal = 1;
187 LJtids[atid] = ljtid;
188 MixingMap[ljtid].resize(nLJ_);
192 std::set<int>::iterator it;
193 for (it = LJtypes.begin(); it != LJtypes.end(); ++it) {
194 int ljtid2 = LJtids[(*it)];
195 AtomType* atype2 = forceField_->getAtomType((*it));
197 LJInteractionData mixer;
198 mixer.sigma = getSigma(atomType, atype2);
199 mixer.epsilon = getEpsilon(atomType, atype2);
200 mixer.sigmai = 1.0 / mixer.sigma;
202 mixer.explicitlySet =
false;
203 MixingMap[ljtid2].resize(nLJ_);
205 MixingMap[ljtid][ljtid2] = mixer;
206 if (ljtid2 != ljtid) { MixingMap[ljtid2][ljtid] = mixer; }
210 void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2,
211 RealType sigma, RealType epsilon) {
212 LJInteractionData mixer;
214 mixer.epsilon = epsilon;
215 mixer.sigmai = 1.0 / mixer.sigma;
216 mixer.explicitlySet =
true;
218 int atid1 = atype1->getIdent();
219 int atid2 = atype2->getIdent();
223 pair<set<int>::iterator,
bool> ret;
224 ret = LJtypes.insert(atid1);
225 if (ret.second ==
false) {
227 ljtid1 = LJtids[atid1];
231 LJtids[atid1] = nLJ_;
235 ret = LJtypes.insert(atid2);
236 if (ret.second ==
false) {
238 ljtid2 = LJtids[atid2];
242 LJtids[atid2] = nLJ_;
246 MixingMap.resize(nLJ_);
247 MixingMap[ljtid1].resize(nLJ_);
249 MixingMap[ljtid1][ljtid2] = mixer;
250 if (ljtid2 != ljtid1) {
251 MixingMap[ljtid2].resize(nLJ_);
252 MixingMap[ljtid2][ljtid1] = mixer;
256 void LJ::calcForce(InteractionData& idat) {
257 if (!initialized_) initialize();
259 LJInteractionData& mixer =
260 MixingMap[LJtids[idat.atid1]][LJtids[idat.atid2]];
262 RealType sigmai = mixer.sigmai;
263 RealType epsilon = mixer.epsilon;
271 RealType myPot = 0.0;
272 RealType myPotC = 0.0;
273 RealType myDeriv = 0.0;
274 RealType myDerivC = 0.0;
276 ros = idat.rij * sigmai;
278 getLJfunc(ros, myPot, myDeriv);
280 if (idat.shiftedPot) {
281 rcos = idat.rcut * sigmai;
282 getLJfunc(rcos, myPotC, myDerivC);
284 }
else if (idat.shiftedForce) {
285 rcos = idat.rcut * sigmai;
286 getLJfunc(rcos, myPotC, myDerivC);
287 myPotC = myPotC + myDerivC * (idat.rij - idat.rcut) * sigmai;
293 RealType pot_temp = idat.vdwMult * epsilon * (myPot - myPotC);
294 idat.vpair += pot_temp;
297 idat.sw * idat.vdwMult * epsilon * (myDeriv - myDerivC) * sigmai;
302 idat.f1 += idat.d * dudr / idat.rij;
307 void LJ::getLJfunc(RealType r, RealType& pot, RealType& deriv) {
308 RealType ri = 1.0 / r;
309 RealType ri2 = ri * ri;
310 RealType ri6 = ri2 * ri2 * ri2;
311 RealType ri7 = ri6 * ri;
312 RealType ri12 = ri6 * ri6;
313 RealType ri13 = ri12 * ri;
315 pot = 4.0 * (ri12 - ri6);
316 deriv = 24.0 * (ri7 - 2.0 * ri13);
321 RealType LJ::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
322 if (!initialized_) initialize();
324 int atid1 = atypes.first->getIdent();
325 int atid2 = atypes.second->getIdent();
326 int ljtid1 = LJtids[atid1];
327 int ljtid2 = LJtids[atid2];
329 if (ljtid1 == -1 || ljtid2 == -1)
332 LJInteractionData mixer = MixingMap[ljtid1][ljtid2];
333 return 2.5 * mixer.sigma;
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
@ VANDERWAALS_FAMILY
Long-range dispersion and short-range pauli repulsion.