ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/SC.cpp
Revision: 1869
Committed: Tue Apr 30 17:03:03 2013 UTC (12 years ago) by gezelter
File size: 11107 byte(s)
Log Message:
Fixed a de-allocation problem in Sutton Chen

File Contents

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

Properties

Name Value
svn:eol-style native