ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/EAM.cpp
Revision: 1629
Committed: Wed Sep 14 21:15:17 2011 UTC (13 years, 7 months ago) by gezelter
File size: 15826 byte(s)
Log Message:
Merging changes from old branch into development branch

File Contents

# User Rev Content
1 gezelter 1478 /*
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 gezelter 1479 #include "types/NonBondedInteractionType.hpp"
49 gezelter 1478
50    
51     namespace OpenMD {
52    
53 gezelter 1502 EAM::EAM() : name_("EAM"), initialized_(false), forceField_(NULL),
54 gezelter 1586 mixMeth_(eamJohnson), eamRcut_(0.0), haveCutoffRadius_(false) {}
55 gezelter 1478
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 gezelter 1482 for (int i = 0; i < nr; i++) rvals.push_back(RealType(i) * dr);
100 gezelter 1478
101     CubicSpline* cs = new CubicSpline();
102     cs->addPoints(rvals, eamParam.Z);
103     return cs;
104     }
105    
106 gezelter 1479 RealType EAM::getRcut(AtomType* atomType) {
107     EAMParam eamParam = getEAMParam(atomType);
108     return eamParam.rcut;
109     }
110    
111 gezelter 1478 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 gezelter 1482 for (int i = 0; i < nr; i++) rvals.push_back(RealType(i) * dr);
118 gezelter 1478
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 gezelter 1482 rhovals.push_back(RealType(i) * drho);
133 gezelter 1478 scaledF.push_back( eamParam.F[i] * 23.06054 );
134     }
135    
136     CubicSpline* cs = new CubicSpline();
137 gezelter 1482 cs->addPoints(rhovals, scaledF);
138 gezelter 1478 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 gezelter 1481 // 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 gezelter 1478
156 gezelter 1481 rmax = max(rmax, eamParam2.rcut);
157     rmax = max(rmax, eamParam2.nr * eamParam2.dr);
158    
159 gezelter 1478 // use the smallest dr (finest grid) to build our grid:
160    
161 gezelter 1481 RealType dr = min(eamParam1.dr, eamParam2.dr);
162    
163     int nr = int(rmax/dr + 0.5);
164    
165 gezelter 1478 vector<RealType> rvals;
166 gezelter 1481 for (int i = 0; i < nr; i++) rvals.push_back(RealType(i*dr));
167 gezelter 1478
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 gezelter 1502 // 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 gezelter 1481
185     zi = r <= eamParam1.rcut ? z1->getValueAt(r) : 0.0;
186     zj = r <= eamParam2.rcut ? z2->getValueAt(r) : 0.0;
187    
188 gezelter 1478 phi = 331.999296 * (zi * zj) / r;
189 gezelter 1481
190 gezelter 1478 phivals.push_back(phi);
191     }
192    
193     CubicSpline* cs = new CubicSpline();
194     cs->addPoints(rvals, phivals);
195     return cs;
196     }
197    
198 gezelter 1586 void EAM::setCutoffRadius( RealType rCut ) {
199     eamRcut_ = rCut;
200     haveCutoffRadius_ = true;
201     }
202    
203 gezelter 1478 void EAM::initialize() {
204    
205     // set up the mixing method:
206 gezelter 1479 ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
207 gezelter 1481 string EAMMixMeth = fopts.getEAMMixingMethod();
208 gezelter 1480 toUpper(EAMMixMeth);
209    
210 gezelter 1478 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 gezelter 1481 pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes();
240 gezelter 1478
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 gezelter 1479 EAMMixingParam eamParam = eamData->getData();
265 gezelter 1478
266 gezelter 1479 vector<RealType> phiAB = eamParam.phi;
267 gezelter 1478 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 gezelter 1479
282 gezelter 1478 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 gezelter 1481 pair<map<int,AtomType*>::iterator,bool> ret;
291     ret = EAMlist.insert( pair<int, AtomType*>(atp.ident, atomType) );
292 gezelter 1478 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 gezelter 1481 map<AtomType*, EAMAtomData>::iterator it;
306 gezelter 1478 for( it = EAMMap.begin(); it != EAMMap.end(); ++it) {
307    
308 gezelter 1479 AtomType* atype2 = (*it).first;
309 gezelter 1478
310     EAMInteractionData mixer;
311     mixer.phi = getPhi(atomType, atype2);
312     mixer.explicitlySet = false;
313    
314 gezelter 1481 pair<AtomType*, AtomType*> key1, key2;
315     key1 = make_pair(atomType, atype2);
316     key2 = make_pair(atype2, atomType);
317 gezelter 1478
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 gezelter 1479 vector<RealType> rVals;
337 gezelter 1478
338 gezelter 1479 for (int i = 0; i < nr; i++) rVals.push_back(i * dr);
339 gezelter 1478
340     cs->addPoints(rVals, phiVals);
341     mixer.phi = cs;
342     mixer.explicitlySet = true;
343    
344 gezelter 1481 pair<AtomType*, AtomType*> key1, key2;
345     key1 = make_pair(atype1, atype2);
346     key2 = make_pair(atype2, atype1);
347 gezelter 1478
348     MixingMap[key1] = mixer;
349     if (key2 != key1) {
350     MixingMap[key2] = mixer;
351     }
352     return;
353     }
354    
355 gezelter 1545 void EAM::calcDensity(InteractionData &idat) {
356 gezelter 1479
357 gezelter 1478 if (!initialized_) initialize();
358 gezelter 1479
359 gezelter 1571 EAMAtomData data1 = EAMMap[idat.atypes.first];
360     EAMAtomData data2 = EAMMap[idat.atypes.second];
361 gezelter 1586
362     if (haveCutoffRadius_)
363     if ( *(idat.rij) > eamRcut_) return;
364    
365 gezelter 1629 if ( *(idat.rij) < data1.rcut)
366 gezelter 1575 *(idat.rho1) += data1.rho->getValueAt( *(idat.rij));
367 gezelter 1629
368 gezelter 1586
369 gezelter 1629 if ( *(idat.rij) < data2.rcut)
370     *(idat.rho2) += data2.rho->getValueAt( *(idat.rij));
371    
372     return;
373 gezelter 1478 }
374 gezelter 1586
375 gezelter 1545 void EAM::calcFunctional(SelfData &sdat) {
376 gezelter 1586
377 gezelter 1478 if (!initialized_) initialize();
378    
379 gezelter 1554 EAMAtomData data1 = EAMMap[ sdat.atype ];
380 gezelter 1478
381 gezelter 1554 pair<RealType, RealType> result = data1.F->getValueAndDerivativeAt( *(sdat.rho) );
382 gezelter 1478
383 gezelter 1554 *(sdat.frho) = result.first;
384     *(sdat.dfrhodrho) = result.second;
385 gezelter 1575
386 gezelter 1586 (*(sdat.pot))[METALLIC_FAMILY] += result.first;
387 gezelter 1575 *(sdat.particlePot) += result.first;
388    
389 gezelter 1478 return;
390     }
391    
392    
393 gezelter 1536 void EAM::calcForce(InteractionData &idat) {
394 gezelter 1478
395     if (!initialized_) initialize();
396 gezelter 1481
397 gezelter 1586 if (haveCutoffRadius_)
398     if ( *(idat.rij) > eamRcut_) return;
399    
400 gezelter 1478 pair<RealType, RealType> res;
401    
402 gezelter 1586 EAMAtomData data1 = EAMMap[idat.atypes.first];
403     EAMAtomData data2 = EAMMap[idat.atypes.second];
404    
405     // get type-specific cutoff radii
406    
407     RealType rci = data1.rcut;
408     RealType rcj = data2.rcut;
409    
410     RealType rha(0.0), drha(0.0), rhb(0.0), drhb(0.0);
411     RealType pha(0.0), dpha(0.0), phb(0.0), dphb(0.0);
412     RealType phab(0.0), dvpdr(0.0);
413     RealType drhoidr, drhojdr, dudr;
414    
415     if ( *(idat.rij) < rci) {
416     res = data1.rho->getValueAndDerivativeAt( *(idat.rij));
417     rha = res.first;
418     drha = res.second;
419    
420     res = MixingMap[make_pair(idat.atypes.first, idat.atypes.first)].phi->getValueAndDerivativeAt( *(idat.rij) );
421     pha = res.first;
422     dpha = res.second;
423     }
424    
425     if ( *(idat.rij) < rcj) {
426     res = data2.rho->getValueAndDerivativeAt( *(idat.rij) );
427     rhb = res.first;
428     drhb = res.second;
429    
430     res = MixingMap[make_pair(idat.atypes.second, idat.atypes.second)].phi->getValueAndDerivativeAt( *(idat.rij) );
431     phb = res.first;
432     dphb = res.second;
433     }
434 gezelter 1478
435 gezelter 1586 switch(mixMeth_) {
436     case eamJohnson:
437 gezelter 1478
438 gezelter 1554 if ( *(idat.rij) < rci) {
439 gezelter 1586 phab = phab + 0.5 * (rhb / rha) * pha;
440     dvpdr = dvpdr + 0.5*((rhb/rha)*dpha +
441     pha*((drhb/rha) - (rhb*drha/rha/rha)));
442 gezelter 1478 }
443 gezelter 1586
444    
445    
446 gezelter 1554 if ( *(idat.rij) < rcj) {
447 gezelter 1586 phab = phab + 0.5 * (rha / rhb) * phb;
448     dvpdr = dvpdr + 0.5 * ((rha/rhb)*dphb +
449     phb*((drha/rhb) - (rha*drhb/rhb/rhb)));
450 gezelter 1478 }
451    
452 gezelter 1586 break;
453    
454     case eamDaw:
455     res = MixingMap[idat.atypes].phi->getValueAndDerivativeAt( *(idat.rij));
456     phab = res.first;
457     dvpdr = res.second;
458    
459     break;
460     case eamUnknown:
461     default:
462    
463     sprintf(painCave.errMsg,
464     "EAM::calcForce hit a mixing method it doesn't know about!\n"
465     );
466     painCave.severity = OPENMD_ERROR;
467     painCave.isFatal = 1;
468     simError();
469    
470     }
471    
472     drhoidr = drha;
473     drhojdr = drhb;
474    
475     dudr = drhojdr* *(idat.dfrho1) + drhoidr* *(idat.dfrho2) + dvpdr;
476    
477     *(idat.f1) += *(idat.d) * dudr / *(idat.rij);
478 gezelter 1478
479 gezelter 1586 // particlePot is the difference between the full potential and
480     // the full potential without the presence of a particular
481     // particle (atom1).
482     //
483     // This reduces the density at other particle locations, so we
484     // need to recompute the density at atom2 assuming atom1 didn't
485     // contribute. This then requires recomputing the density
486     // functional for atom2 as well.
487    
488     *(idat.particlePot1) += data2.F->getValueAt( *(idat.rho2) - rha )
489     - *(idat.frho2);
490    
491     *(idat.particlePot2) += data1.F->getValueAt( *(idat.rho1) - rhb)
492     - *(idat.frho1);
493    
494     (*(idat.pot))[METALLIC_FAMILY] += phab;
495    
496     *(idat.vpair) += phab;
497    
498 gezelter 1478 return;
499    
500     }
501 gezelter 1505
502 gezelter 1545 RealType EAM::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) {
503 gezelter 1505 if (!initialized_) initialize();
504    
505     RealType cut = 0.0;
506    
507     map<AtomType*, EAMAtomData>::iterator it;
508    
509 gezelter 1545 it = EAMMap.find(atypes.first);
510 gezelter 1505 if (it != EAMMap.end()) {
511     EAMAtomData data1 = (*it).second;
512     cut = data1.rcut;
513     }
514    
515 gezelter 1545 it = EAMMap.find(atypes.second);
516 gezelter 1505 if (it != EAMMap.end()) {
517     EAMAtomData data2 = (*it).second;
518     if (data2.rcut > cut)
519     cut = data2.rcut;
520     }
521    
522     return cut;
523     }
524 gezelter 1478 }
525    

Properties

Name Value
svn:eol-style native