ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/SC.cpp
Revision: 1868
Committed: Tue Apr 30 15:56:54 2013 UTC (12 years ago) by gezelter
File size: 11339 byte(s)
Log Message:
CLearing out some memory leaks

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

Properties

Name Value
svn:eol-style native