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] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
*/ |
42 |
|
43 |
#include <stdio.h> |
44 |
#include <string.h> |
45 |
|
46 |
#include <cmath> |
47 |
#include "nonbonded/SC.hpp" |
48 |
#include "utils/simError.h" |
49 |
#include "types/NonBondedInteractionType.hpp" |
50 |
|
51 |
namespace OpenMD { |
52 |
|
53 |
|
54 |
SC::SC() : name_("SC"), initialized_(false), forceField_(NULL), |
55 |
scRcut_(0.0), np_(3000) {} |
56 |
|
57 |
RealType SC::getM(AtomType* atomType1, AtomType* atomType2) { |
58 |
SuttonChenAdapter sca1 = SuttonChenAdapter(atomType1); |
59 |
SuttonChenAdapter sca2 = SuttonChenAdapter(atomType2); |
60 |
RealType m1 = sca1.getM(); |
61 |
RealType m2 = sca2.getM(); |
62 |
return 0.5 * (m1 + m2); |
63 |
} |
64 |
|
65 |
RealType SC::getN(AtomType* atomType1, AtomType* atomType2) { |
66 |
SuttonChenAdapter sca1 = SuttonChenAdapter(atomType1); |
67 |
SuttonChenAdapter sca2 = SuttonChenAdapter(atomType2); |
68 |
RealType n1 = sca1.getN(); |
69 |
RealType n2 = sca2.getN(); |
70 |
return 0.5 * (n1 + n2); |
71 |
} |
72 |
|
73 |
RealType SC::getAlpha(AtomType* atomType1, AtomType* atomType2) { |
74 |
SuttonChenAdapter sca1 = SuttonChenAdapter(atomType1); |
75 |
SuttonChenAdapter sca2 = SuttonChenAdapter(atomType2); |
76 |
RealType alpha1 = sca1.getAlpha(); |
77 |
RealType alpha2 = sca2.getAlpha(); |
78 |
|
79 |
ForceFieldOptions& fopts = forceField_->getForceFieldOptions(); |
80 |
std::string DistanceMix = fopts.getDistanceMixingRule(); |
81 |
toUpper(DistanceMix); |
82 |
|
83 |
if (DistanceMix == "GEOMETRIC") |
84 |
return sqrt(alpha1 * alpha2); |
85 |
else |
86 |
return 0.5 * (alpha1 + alpha2); |
87 |
} |
88 |
|
89 |
RealType SC::getEpsilon(AtomType* atomType1, AtomType* atomType2) { |
90 |
SuttonChenAdapter sca1 = SuttonChenAdapter(atomType1); |
91 |
SuttonChenAdapter sca2 = SuttonChenAdapter(atomType2); |
92 |
RealType epsilon1 = sca1.getEpsilon(); |
93 |
RealType epsilon2 = sca2.getEpsilon(); |
94 |
return sqrt(epsilon1 * epsilon2); |
95 |
} |
96 |
|
97 |
void SC::initialize() { |
98 |
// find all of the SC atom Types: |
99 |
ForceField::AtomTypeContainer* atomTypes = forceField_->getAtomTypes(); |
100 |
ForceField::AtomTypeContainer::MapTypeIterator i; |
101 |
AtomType* at; |
102 |
|
103 |
for (at = atomTypes->beginType(i); at != NULL; |
104 |
at = atomTypes->nextType(i)) { |
105 |
SuttonChenAdapter sca = SuttonChenAdapter(at); |
106 |
if (sca.isSuttonChen()) |
107 |
addType(at); |
108 |
} |
109 |
initialized_ = true; |
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
void SC::addType(AtomType* atomType){ |
115 |
|
116 |
SuttonChenAdapter sca = SuttonChenAdapter(atomType); |
117 |
SCAtomData scAtomData; |
118 |
|
119 |
scAtomData.c = sca.getC(); |
120 |
scAtomData.m = sca.getM(); |
121 |
scAtomData.n = sca.getN(); |
122 |
scAtomData.alpha = sca.getAlpha(); |
123 |
scAtomData.epsilon = sca.getEpsilon(); |
124 |
scAtomData.rCut = 2.0 * scAtomData.alpha; |
125 |
|
126 |
// add it to the map: |
127 |
|
128 |
pair<map<int,AtomType*>::iterator,bool> ret; |
129 |
ret = SClist.insert( pair<int, AtomType*>(atomType->getIdent(), atomType) ); |
130 |
if (ret.second == false) { |
131 |
sprintf( painCave.errMsg, |
132 |
"SC already had a previous entry with ident %d\n", |
133 |
atomType->getIdent() ); |
134 |
painCave.severity = OPENMD_INFO; |
135 |
painCave.isFatal = 0; |
136 |
simError(); |
137 |
} |
138 |
|
139 |
SCMap[atomType] = scAtomData; |
140 |
|
141 |
// Now, iterate over all known types and add to the mixing map: |
142 |
|
143 |
map<AtomType*, SCAtomData>::iterator it; |
144 |
for( it = SCMap.begin(); it != SCMap.end(); ++it) { |
145 |
|
146 |
AtomType* atype2 = (*it).first; |
147 |
|
148 |
SCInteractionData mixer; |
149 |
|
150 |
mixer.alpha = getAlpha(atomType, atype2); |
151 |
mixer.rCut = 2.0 * mixer.alpha; |
152 |
mixer.epsilon = getEpsilon(atomType, atype2); |
153 |
mixer.m = getM(atomType, atype2); |
154 |
mixer.n = getN(atomType, atype2); |
155 |
|
156 |
RealType dr = mixer.rCut / (np_ - 1); |
157 |
vector<RealType> rvals; |
158 |
vector<RealType> vvals; |
159 |
vector<RealType> phivals; |
160 |
|
161 |
rvals.push_back(0.0); |
162 |
vvals.push_back(0.0); |
163 |
phivals.push_back(0.0); |
164 |
|
165 |
for (int k = 1; k < np_; k++) { |
166 |
RealType r = dr * k; |
167 |
rvals.push_back(r); |
168 |
vvals.push_back( mixer.epsilon * pow(mixer.alpha/r, mixer.n) ); |
169 |
phivals.push_back( pow(mixer.alpha/r, mixer.m) ); |
170 |
} |
171 |
|
172 |
mixer.vCut = mixer.epsilon * pow(mixer.alpha/mixer.rCut, mixer.n); |
173 |
|
174 |
CubicSpline* V = new CubicSpline(); |
175 |
V->addPoints(rvals, vvals); |
176 |
|
177 |
CubicSpline* phi = new CubicSpline(); |
178 |
phi->addPoints(rvals, phivals); |
179 |
|
180 |
mixer.V = V; |
181 |
mixer.phi = phi; |
182 |
|
183 |
mixer.explicitlySet = false; |
184 |
|
185 |
pair<AtomType*, AtomType*> key1, key2; |
186 |
key1 = make_pair(atomType, atype2); |
187 |
key2 = make_pair(atype2, atomType); |
188 |
|
189 |
MixingMap[key1] = mixer; |
190 |
if (key2 != key1) { |
191 |
MixingMap[key2] = mixer; |
192 |
} |
193 |
} |
194 |
return; |
195 |
} |
196 |
|
197 |
void SC::addExplicitInteraction(AtomType* atype1, AtomType* atype2, |
198 |
RealType epsilon, RealType m, RealType n, |
199 |
RealType alpha) { |
200 |
|
201 |
// in case these weren't already in the map |
202 |
addType(atype1); |
203 |
addType(atype2); |
204 |
|
205 |
SCInteractionData mixer; |
206 |
|
207 |
mixer.epsilon = epsilon; |
208 |
mixer.m = m; |
209 |
mixer.n = n; |
210 |
mixer.alpha = alpha; |
211 |
mixer.rCut = 2.0 * mixer.alpha; |
212 |
|
213 |
RealType dr = mixer.rCut / (np_ - 1); |
214 |
vector<RealType> rvals; |
215 |
vector<RealType> vvals; |
216 |
vector<RealType> phivals; |
217 |
|
218 |
rvals.push_back(0.0); |
219 |
vvals.push_back(0.0); |
220 |
phivals.push_back(0.0); |
221 |
|
222 |
for (int k = 1; k < np_; k++) { |
223 |
RealType r = dr * k; |
224 |
rvals.push_back(r); |
225 |
vvals.push_back( mixer.epsilon * pow(mixer.alpha/r, mixer.n) ); |
226 |
phivals.push_back( pow(mixer.alpha/r, mixer.m) ); |
227 |
} |
228 |
|
229 |
mixer.vCut = mixer.epsilon * pow(mixer.alpha/mixer.rCut, mixer.n); |
230 |
|
231 |
CubicSpline* V = new CubicSpline(); |
232 |
V->addPoints(rvals, vvals); |
233 |
|
234 |
CubicSpline* phi = new CubicSpline(); |
235 |
phi->addPoints(rvals, phivals); |
236 |
|
237 |
mixer.V = V; |
238 |
mixer.phi = phi; |
239 |
|
240 |
mixer.explicitlySet = true; |
241 |
|
242 |
pair<AtomType*, AtomType*> key1, key2; |
243 |
key1 = make_pair(atype1, atype2); |
244 |
key2 = make_pair(atype2, atype1); |
245 |
|
246 |
MixingMap[key1] = mixer; |
247 |
if (key2 != key1) { |
248 |
MixingMap[key2] = mixer; |
249 |
} |
250 |
return; |
251 |
} |
252 |
|
253 |
void SC::calcDensity(InteractionData &idat) { |
254 |
|
255 |
if (!initialized_) initialize(); |
256 |
|
257 |
SCInteractionData mixer = MixingMap[ idat.atypes ]; |
258 |
|
259 |
RealType rcij = mixer.rCut; |
260 |
|
261 |
if ( *(idat.rij) < rcij) { |
262 |
RealType rho = mixer.phi->getValueAt( *(idat.rij) ); |
263 |
*(idat.rho1) += rho; |
264 |
*(idat.rho2) += rho; |
265 |
} |
266 |
|
267 |
return; |
268 |
} |
269 |
|
270 |
void SC::calcFunctional(SelfData &sdat) { |
271 |
|
272 |
if (!initialized_) initialize(); |
273 |
|
274 |
SCAtomData data1 = SCMap[sdat.atype]; |
275 |
|
276 |
RealType u = - data1.c * data1.epsilon * sqrt( *(sdat.rho) ); |
277 |
*(sdat.frho) = u; |
278 |
*(sdat.dfrhodrho) = 0.5 * *(sdat.frho) / *(sdat.rho); |
279 |
|
280 |
(*(sdat.pot))[METALLIC_FAMILY] += u; |
281 |
if (sdat.doParticlePot) { |
282 |
*(sdat.particlePot) += u; |
283 |
} |
284 |
|
285 |
return; |
286 |
} |
287 |
|
288 |
|
289 |
void SC::calcForce(InteractionData &idat) { |
290 |
|
291 |
if (!initialized_) initialize(); |
292 |
|
293 |
SCAtomData data1 = SCMap[idat.atypes.first]; |
294 |
SCAtomData data2 = SCMap[idat.atypes.second]; |
295 |
|
296 |
SCInteractionData mixer = MixingMap[idat.atypes]; |
297 |
|
298 |
RealType rcij = mixer.rCut; |
299 |
|
300 |
if ( *(idat.rij) < rcij) { |
301 |
RealType vcij = mixer.vCut; |
302 |
|
303 |
pair<RealType, RealType> res; |
304 |
|
305 |
res = mixer.phi->getValueAndDerivativeAt( *(idat.rij) ); |
306 |
RealType rhtmp = res.first; |
307 |
RealType drhodr = res.second; |
308 |
|
309 |
res = mixer.V->getValueAndDerivativeAt( *(idat.rij) ); |
310 |
RealType vptmp = res.first; |
311 |
RealType dvpdr = res.second; |
312 |
|
313 |
RealType pot_temp = vptmp - vcij; |
314 |
*(idat.vpair) += pot_temp; |
315 |
|
316 |
RealType dudr = drhodr * ( *(idat.dfrho1) + *(idat.dfrho2) ) + dvpdr; |
317 |
|
318 |
*(idat.f1) += *(idat.d) * dudr / *(idat.rij) ; |
319 |
|
320 |
if (idat.doParticlePot) { |
321 |
// particlePot is the difference between the full potential and |
322 |
// the full potential without the presence of a particular |
323 |
// particle (atom1). |
324 |
// |
325 |
// This reduces the density at other particle locations, so we |
326 |
// need to recompute the density at atom2 assuming atom1 didn't |
327 |
// contribute. This then requires recomputing the density |
328 |
// functional for atom2 as well. |
329 |
|
330 |
*(idat.particlePot1) -= data2.c * data2.epsilon * |
331 |
sqrt( *(idat.rho2) - rhtmp) + *(idat.frho2); |
332 |
|
333 |
*(idat.particlePot2) -= data1.c * data1.epsilon * |
334 |
sqrt( *(idat.rho1) - rhtmp) + *(idat.frho1); |
335 |
} |
336 |
|
337 |
(*(idat.pot))[METALLIC_FAMILY] += pot_temp; |
338 |
} |
339 |
|
340 |
return; |
341 |
} |
342 |
|
343 |
RealType SC::getSuggestedCutoffRadius(pair<AtomType*, AtomType*> atypes) { |
344 |
if (!initialized_) initialize(); |
345 |
|
346 |
map<pair<AtomType*, AtomType*>, SCInteractionData>::iterator it; |
347 |
it = MixingMap.find(atypes); |
348 |
if (it == MixingMap.end()) |
349 |
return 0.0; |
350 |
else { |
351 |
SCInteractionData mixer = (*it).second; |
352 |
return mixer.rCut; |
353 |
} |
354 |
} |
355 |
} |