1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
*/ |
41 |
|
42 |
#include <stdio.h> |
43 |
#include <string.h> |
44 |
|
45 |
#include <cmath> |
46 |
#include "nonbonded/EAM.hpp" |
47 |
#include "utils/simError.h" |
48 |
#include "types/NonBondedInteractionType.hpp" |
49 |
|
50 |
|
51 |
namespace OpenMD { |
52 |
|
53 |
EAM::EAM() : name_("EAM"), initialized_(false), forceField_(NULL), |
54 |
mixMeth_(eamJohnson), eamRcut_(0.0), haveCutoffRadius_(false) {} |
55 |
|
56 |
EAMParam EAM::getEAMParam(AtomType* atomType) { |
57 |
|
58 |
// Do sanity checking on the AtomType we were passed before |
59 |
// building any data structures: |
60 |
if (!atomType->isEAM()) { |
61 |
sprintf( painCave.errMsg, |
62 |
"EAM::getEAMParam was passed an atomType (%s) that does not\n" |
63 |
"\tappear to be an embedded atom method (EAM) atom.\n", |
64 |
atomType->getName().c_str()); |
65 |
painCave.severity = OPENMD_ERROR; |
66 |
painCave.isFatal = 1; |
67 |
simError(); |
68 |
} |
69 |
|
70 |
GenericData* data = atomType->getPropertyByName("EAM"); |
71 |
if (data == NULL) { |
72 |
sprintf( painCave.errMsg, "EAM::getEAMParam could not find EAM\n" |
73 |
"\tparameters for atomType %s.\n", |
74 |
atomType->getName().c_str()); |
75 |
painCave.severity = OPENMD_ERROR; |
76 |
painCave.isFatal = 1; |
77 |
simError(); |
78 |
} |
79 |
|
80 |
EAMParamGenericData* eamData = dynamic_cast<EAMParamGenericData*>(data); |
81 |
if (eamData == NULL) { |
82 |
sprintf( painCave.errMsg, |
83 |
"EAM::getEAMParam could not convert GenericData to EAMParam for\n" |
84 |
"\tatom type %s\n", atomType->getName().c_str()); |
85 |
painCave.severity = OPENMD_ERROR; |
86 |
painCave.isFatal = 1; |
87 |
simError(); |
88 |
} |
89 |
|
90 |
return eamData->getData(); |
91 |
} |
92 |
|
93 |
CubicSpline* EAM::getZ(AtomType* atomType) { |
94 |
EAMParam eamParam = getEAMParam(atomType); |
95 |
int nr = eamParam.nr; |
96 |
RealType dr = eamParam.dr; |
97 |
vector<RealType> rvals; |
98 |
|
99 |
for (int i = 0; i < nr; i++) rvals.push_back(RealType(i) * dr); |
100 |
|
101 |
CubicSpline* cs = new CubicSpline(); |
102 |
cs->addPoints(rvals, eamParam.Z); |
103 |
return cs; |
104 |
} |
105 |
|
106 |
RealType EAM::getRcut(AtomType* atomType) { |
107 |
EAMParam eamParam = getEAMParam(atomType); |
108 |
return eamParam.rcut; |
109 |
} |
110 |
|
111 |
CubicSpline* EAM::getRho(AtomType* atomType) { |
112 |
EAMParam eamParam = getEAMParam(atomType); |
113 |
int nr = eamParam.nr; |
114 |
RealType dr = eamParam.dr; |
115 |
vector<RealType> rvals; |
116 |
|
117 |
for (int i = 0; i < nr; i++) rvals.push_back(RealType(i) * dr); |
118 |
|
119 |
CubicSpline* cs = new CubicSpline(); |
120 |
cs->addPoints(rvals, eamParam.rho); |
121 |
return cs; |
122 |
} |
123 |
|
124 |
CubicSpline* EAM::getF(AtomType* atomType) { |
125 |
EAMParam eamParam = getEAMParam(atomType); |
126 |
int nrho = eamParam.nrho; |
127 |
RealType drho = eamParam.drho; |
128 |
vector<RealType> rhovals; |
129 |
vector<RealType> scaledF; |
130 |
|
131 |
for (int i = 0; i < nrho; i++) { |
132 |
rhovals.push_back(RealType(i) * drho); |
133 |
scaledF.push_back( eamParam.F[i] * 23.06054 ); |
134 |
} |
135 |
|
136 |
CubicSpline* cs = new CubicSpline(); |
137 |
cs->addPoints(rhovals, scaledF); |
138 |
return cs; |
139 |
} |
140 |
|
141 |
CubicSpline* EAM::getPhi(AtomType* atomType1, AtomType* atomType2) { |
142 |
EAMParam eamParam1 = getEAMParam(atomType1); |
143 |
EAMParam eamParam2 = getEAMParam(atomType2); |
144 |
CubicSpline* z1 = getZ(atomType1); |
145 |
CubicSpline* z2 = getZ(atomType2); |
146 |
|
147 |
// make the r grid: |
148 |
|
149 |
|
150 |
// we need phi out to the largest value we'll encounter in the radial space; |
151 |
|
152 |
RealType rmax = 0.0; |
153 |
rmax = max(rmax, eamParam1.rcut); |
154 |
rmax = max(rmax, eamParam1.nr * eamParam1.dr); |
155 |
|
156 |
rmax = max(rmax, eamParam2.rcut); |
157 |
rmax = max(rmax, eamParam2.nr * eamParam2.dr); |
158 |
|
159 |
// use the smallest dr (finest grid) to build our grid: |
160 |
|
161 |
RealType dr = min(eamParam1.dr, eamParam2.dr); |
162 |
|
163 |
int nr = int(rmax/dr + 0.5); |
164 |
|
165 |
vector<RealType> rvals; |
166 |
for (int i = 0; i < nr; i++) rvals.push_back(RealType(i*dr)); |
167 |
|
168 |
// construct the pair potential: |
169 |
|
170 |
vector<RealType> phivals; |
171 |
RealType phi; |
172 |
RealType r; |
173 |
RealType zi, zj; |
174 |
|
175 |
phivals.push_back(0.0); |
176 |
|
177 |
for (int i = 1; i < rvals.size(); i++ ) { |
178 |
r = rvals[i]; |
179 |
|
180 |
// only use z(r) if we're inside this atom's cutoff radius, |
181 |
// otherwise, we'll use zero for the charge. This effectively |
182 |
// means that our phi grid goes out beyond the cutoff of the |
183 |
// pair potential |
184 |
|
185 |
zi = r <= eamParam1.rcut ? z1->getValueAt(r) : 0.0; |
186 |
zj = r <= eamParam2.rcut ? z2->getValueAt(r) : 0.0; |
187 |
|
188 |
phi = 331.999296 * (zi * zj) / r; |
189 |
|
190 |
phivals.push_back(phi); |
191 |
} |
192 |
|
193 |
CubicSpline* cs = new CubicSpline(); |
194 |
cs->addPoints(rvals, phivals); |
195 |
return cs; |
196 |
} |
197 |
|
198 |
void EAM::setCutoffRadius( RealType rCut ) { |
199 |
eamRcut_ = rCut; |
200 |
haveCutoffRadius_ = true; |
201 |
} |
202 |
|
203 |
void EAM::initialize() { |
204 |
|
205 |
// set up the mixing method: |
206 |
ForceFieldOptions& fopts = forceField_->getForceFieldOptions(); |
207 |
string EAMMixMeth = fopts.getEAMMixingMethod(); |
208 |
toUpper(EAMMixMeth); |
209 |
|
210 |
if (EAMMixMeth == "JOHNSON") |
211 |
mixMeth_ = eamJohnson; |
212 |
else if (EAMMixMeth == "DAW") |
213 |
mixMeth_ = eamDaw; |
214 |
else |
215 |
mixMeth_ = eamUnknown; |
216 |
|
217 |
// find all of the EAM atom Types: |
218 |
ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes(); |
219 |
ForceField::AtomTypeContainer::MapTypeIterator i; |
220 |
AtomType* at; |
221 |
|
222 |
for (at = atomTypes->beginType(i); at != NULL; |
223 |
at = atomTypes->nextType(i)) { |
224 |
|
225 |
if (at->isEAM()) |
226 |
addType(at); |
227 |
} |
228 |
|
229 |
// find all of the explicit EAM interactions (setfl): |
230 |
ForceField::NonBondedInteractionTypeContainer* nbiTypes = forceField_->getNonBondedInteractionTypes(); |
231 |
ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j; |
232 |
NonBondedInteractionType* nbt; |
233 |
|
234 |
for (nbt = nbiTypes->beginType(j); nbt != NULL; |
235 |
nbt = nbiTypes->nextType(j)) { |
236 |
|
237 |
if (nbt->isEAM()) { |
238 |
|
239 |
pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes(); |
240 |
|
241 |
GenericData* data = nbt->getPropertyByName("EAM"); |
242 |
if (data == NULL) { |
243 |
sprintf( painCave.errMsg, "EAM::rebuildMixingMap could not find\n" |
244 |
"\tEAM parameters for %s - %s interaction.\n", |
245 |
atypes.first->getName().c_str(), |
246 |
atypes.second->getName().c_str()); |
247 |
painCave.severity = OPENMD_ERROR; |
248 |
painCave.isFatal = 1; |
249 |
simError(); |
250 |
} |
251 |
|
252 |
EAMMixingData* eamData = dynamic_cast<EAMMixingData*>(data); |
253 |
if (eamData == NULL) { |
254 |
sprintf( painCave.errMsg, |
255 |
"EAM::rebuildMixingMap could not convert GenericData to\n" |
256 |
"\tEAMMixingData for %s - %s interaction.\n", |
257 |
atypes.first->getName().c_str(), |
258 |
atypes.second->getName().c_str()); |
259 |
painCave.severity = OPENMD_ERROR; |
260 |
painCave.isFatal = 1; |
261 |
simError(); |
262 |
} |
263 |
|
264 |
EAMMixingParam eamParam = eamData->getData(); |
265 |
|
266 |
vector<RealType> phiAB = eamParam.phi; |
267 |
RealType dr = eamParam.dr; |
268 |
int nr = eamParam.nr; |
269 |
|
270 |
addExplicitInteraction(atypes.first, atypes.second, dr, nr, phiAB); |
271 |
} |
272 |
} |
273 |
initialized_ = true; |
274 |
} |
275 |
|
276 |
|
277 |
|
278 |
void EAM::addType(AtomType* atomType){ |
279 |
|
280 |
EAMAtomData eamAtomData; |
281 |
|
282 |
eamAtomData.rho = getRho(atomType); |
283 |
eamAtomData.F = getF(atomType); |
284 |
eamAtomData.Z = getZ(atomType); |
285 |
eamAtomData.rcut = getRcut(atomType); |
286 |
|
287 |
// add it to the map: |
288 |
AtomTypeProperties atp = atomType->getATP(); |
289 |
|
290 |
pair<map<int,AtomType*>::iterator,bool> ret; |
291 |
ret = EAMlist.insert( pair<int, AtomType*>(atp.ident, atomType) ); |
292 |
if (ret.second == false) { |
293 |
sprintf( painCave.errMsg, |
294 |
"EAM already had a previous entry with ident %d\n", |
295 |
atp.ident); |
296 |
painCave.severity = OPENMD_INFO; |
297 |
painCave.isFatal = 0; |
298 |
simError(); |
299 |
} |
300 |
|
301 |
EAMMap[atomType] = eamAtomData; |
302 |
|
303 |
// Now, iterate over all known types and add to the mixing map: |
304 |
|
305 |
map<AtomType*, EAMAtomData>::iterator it; |
306 |
for( it = EAMMap.begin(); it != EAMMap.end(); ++it) { |
307 |
|
308 |
AtomType* atype2 = (*it).first; |
309 |
|
310 |
EAMInteractionData mixer; |
311 |
mixer.phi = getPhi(atomType, atype2); |
312 |
mixer.explicitlySet = false; |
313 |
|
314 |
pair<AtomType*, AtomType*> key1, key2; |
315 |
key1 = make_pair(atomType, atype2); |
316 |
key2 = make_pair(atype2, atomType); |
317 |
|
318 |
MixingMap[key1] = mixer; |
319 |
if (key2 != key1) { |
320 |
MixingMap[key2] = mixer; |
321 |
} |
322 |
} |
323 |
return; |
324 |
} |
325 |
|
326 |
void EAM::addExplicitInteraction(AtomType* atype1, AtomType* atype2, |
327 |
RealType dr, int nr, |
328 |
vector<RealType> phiVals) { |
329 |
|
330 |
// in case these weren't already in the map |
331 |
addType(atype1); |
332 |
addType(atype2); |
333 |
|
334 |
EAMInteractionData mixer; |
335 |
CubicSpline* cs = new CubicSpline(); |
336 |
vector<RealType> rVals; |
337 |
|
338 |
for (int i = 0; i < nr; i++) rVals.push_back(i * dr); |
339 |
|
340 |
cs->addPoints(rVals, phiVals); |
341 |
mixer.phi = cs; |
342 |
mixer.explicitlySet = true; |
343 |
|
344 |
pair<AtomType*, AtomType*> key1, key2; |
345 |
key1 = make_pair(atype1, atype2); |
346 |
key2 = make_pair(atype2, atype1); |
347 |
|
348 |
MixingMap[key1] = mixer; |
349 |
if (key2 != key1) { |
350 |
MixingMap[key2] = mixer; |
351 |
} |
352 |
return; |
353 |
} |
354 |
|
355 |
void EAM::calcDensity(InteractionData &idat) { |
356 |
|
357 |
if (!initialized_) initialize(); |
358 |
|
359 |
EAMAtomData data1 = EAMMap[idat.atypes.first]; |
360 |
EAMAtomData data2 = EAMMap[idat.atypes.second]; |
361 |
|
362 |
if (haveCutoffRadius_) |
363 |
if ( *(idat.rij) > eamRcut_) return; |
364 |
|
365 |
if ( *(idat.rij) < data1.rcut) { |
366 |
*(idat.rho1) += data1.rho->getValueAt( *(idat.rij)); |
367 |
|
368 |
|
369 |
if ( *(idat.rij) < data2.rcut) |
370 |
*(idat.rho2) += data2.rho->getValueAt( *(idat.rij)); |
371 |
|
372 |
return; |
373 |
} |
374 |
} |
375 |
|
376 |
void EAM::calcFunctional(SelfData &sdat) { |
377 |
|
378 |
if (!initialized_) initialize(); |
379 |
|
380 |
EAMAtomData data1 = EAMMap[ sdat.atype ]; |
381 |
|
382 |
pair<RealType, RealType> result = data1.F->getValueAndDerivativeAt( *(sdat.rho) ); |
383 |
|
384 |
*(sdat.frho) = result.first; |
385 |
*(sdat.dfrhodrho) = result.second; |
386 |
|
387 |
(*(sdat.pot))[METALLIC_FAMILY] += result.first; |
388 |
*(sdat.particlePot) += result.first; |
389 |
|
390 |
return; |
391 |
} |
392 |
|
393 |
|
394 |
void EAM::calcForce(InteractionData &idat) { |
395 |
|
396 |
if (!initialized_) initialize(); |
397 |
|
398 |
|
399 |
|
400 |
if (haveCutoffRadius_) |
401 |
if ( *(idat.rij) > eamRcut_) return; |
402 |
|
403 |
pair<RealType, RealType> res; |
404 |
|
405 |
|
406 |
EAMAtomData data1 = EAMMap[idat.atypes.first]; |
407 |
EAMAtomData data2 = EAMMap[idat.atypes.second]; |
408 |
|
409 |
// get type-specific cutoff radii |
410 |
|
411 |
RealType rci = data1.rcut; |
412 |
RealType rcj = data2.rcut; |
413 |
|
414 |
RealType rha(0.0), drha(0.0), rhb(0.0), drhb(0.0); |
415 |
RealType pha(0.0), dpha(0.0), phb(0.0), dphb(0.0); |
416 |
RealType phab(0.0), dvpdr(0.0); |
417 |
RealType drhoidr, drhojdr, dudr; |
418 |
|
419 |
if ( *(idat.rij) < rci) { |
420 |
res = data1.rho->getValueAndDerivativeAt( *(idat.rij)); |
421 |
rha = res.first; |
422 |
drha = res.second; |
423 |
|
424 |
res = MixingMap[make_pair(idat.atypes.first, idat.atypes.first)].phi->getValueAndDerivativeAt( *(idat.rij) ); |
425 |
pha = res.first; |
426 |
dpha = res.second; |
427 |
} |
428 |
|
429 |
if ( *(idat.rij) < rcj) { |
430 |
res = data2.rho->getValueAndDerivativeAt( *(idat.rij) ); |
431 |
rhb = res.first; |
432 |
drhb = res.second; |
433 |
|
434 |
res = MixingMap[make_pair(idat.atypes.second, idat.atypes.second)].phi->getValueAndDerivativeAt( *(idat.rij) ); |
435 |
phb = res.first; |
436 |
dphb = res.second; |
437 |
} |
438 |
|
439 |
switch(mixMeth_) { |
440 |
case eamJohnson: |
441 |
|
442 |
if ( *(idat.rij) < rci) { |
443 |
phab = phab + 0.5 * (rhb / rha) * pha; |
444 |
dvpdr = dvpdr + 0.5*((rhb/rha)*dpha + |
445 |
pha*((drhb/rha) - (rhb*drha/rha/rha))); |
446 |
} |
447 |
|
448 |
|
449 |
|
450 |
if ( *(idat.rij) < rcj) { |
451 |
phab = phab + 0.5 * (rha / rhb) * phb; |
452 |
dvpdr = dvpdr + 0.5 * ((rha/rhb)*dphb + |
453 |
phb*((drha/rhb) - (rha*drhb/rhb/rhb))); |
454 |
} |
455 |
|
456 |
break; |
457 |
|
458 |
case eamDaw: |
459 |
res = MixingMap[idat.atypes].phi->getValueAndDerivativeAt( *(idat.rij)); |
460 |
phab = res.first; |
461 |
dvpdr = res.second; |
462 |
|
463 |
break; |
464 |
case eamUnknown: |
465 |
default: |
466 |
|
467 |
sprintf(painCave.errMsg, |
468 |
"EAM::calcForce hit a mixing method it doesn't know about!\n" |
469 |
); |
470 |
painCave.severity = OPENMD_ERROR; |
471 |
painCave.isFatal = 1; |
472 |
simError(); |
473 |
|
474 |
} |
475 |
|
476 |
drhoidr = drha; |
477 |
drhojdr = drhb; |
478 |
|
479 |
dudr = drhojdr* *(idat.dfrho1) + drhoidr* *(idat.dfrho2) + dvpdr; |
480 |
|
481 |
*(idat.f1) += *(idat.d) * dudr / *(idat.rij); |
482 |
|
483 |
// particlePot is the difference between the full potential and |
484 |
// the full potential without the presence of a particular |
485 |
// particle (atom1). |
486 |
// |
487 |
// This reduces the density at other particle locations, so we |
488 |
// need to recompute the density at atom2 assuming atom1 didn't |
489 |
// contribute. This then requires recomputing the density |
490 |
// functional for atom2 as well. |
491 |
|
492 |
*(idat.particlePot1) += data2.F->getValueAt( *(idat.rho2) - rha ) |
493 |
- *(idat.frho2); |
494 |
|
495 |
*(idat.particlePot2) += data1.F->getValueAt( *(idat.rho1) - rhb) |
496 |
- *(idat.frho1); |
497 |
|
498 |
(*(idat.pot))[METALLIC_FAMILY] += phab; |
499 |
|
500 |
*(idat.vpair) += phab; |
501 |
|
502 |
return; |
503 |
|
504 |
} |
505 |
|
506 |
RealType EAM::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) { |
507 |
if (!initialized_) initialize(); |
508 |
|
509 |
RealType cut = 0.0; |
510 |
|
511 |
map<AtomType*, EAMAtomData>::iterator it; |
512 |
|
513 |
it = EAMMap.find(atypes.first); |
514 |
if (it != EAMMap.end()) { |
515 |
EAMAtomData data1 = (*it).second; |
516 |
cut = data1.rcut; |
517 |
} |
518 |
|
519 |
it = EAMMap.find(atypes.second); |
520 |
if (it != EAMMap.end()) { |
521 |
EAMAtomData data2 = (*it).second; |
522 |
if (data2.rcut > cut) |
523 |
cut = data2.rcut; |
524 |
} |
525 |
|
526 |
return cut; |
527 |
} |
528 |
} |
529 |
|