ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/LJ.cpp
Revision: 1471
Committed: Mon Jul 19 18:59:59 2010 UTC (14 years, 9 months ago) by gezelter
File size: 13013 byte(s)
Log Message:
Added nonbonded directory.  Compiles, links, runs, but gives incorrect
answer.

File Contents

# User Rev Content
1 gezelter 1471 /*
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/LJ.hpp"
47     #include "utils/simError.h"
48    
49    
50     namespace OpenMD {
51    
52     bool LJ::initialized_ = false;
53     bool LJ::shiftedPot_ = false;
54     bool LJ::shiftedFrc_ = false;
55     ForceField* LJ::forceField_ = NULL;
56     std::map<int, AtomType*> LJ::LJMap;
57     std::map<std::pair<AtomType*, AtomType*>, LJInteractionData> LJ::MixingMap;
58    
59     LJ* LJ::_instance = NULL;
60    
61     LJ* LJ::Instance() {
62     if (!_instance) {
63     _instance = new LJ();
64     }
65     return _instance;
66     }
67    
68     LJParam LJ::getLJParam(AtomType* atomType) {
69    
70     // Do sanity checking on the AtomType we were passed before
71     // building any data structures:
72     if (!atomType->isLennardJones()) {
73     sprintf( painCave.errMsg,
74     "LJ::getLJParam was passed an atomType (%s) that does not\n"
75     "\tappear to be a Lennard-Jones atom.\n",
76     atomType->getName().c_str());
77     painCave.severity = OPENMD_ERROR;
78     painCave.isFatal = 1;
79     simError();
80     }
81    
82     GenericData* data = atomType->getPropertyByName("LennardJones");
83     if (data == NULL) {
84     sprintf( painCave.errMsg, "LJ::getLJParam could not find Lennard-Jones\n"
85     "\tparameters for atomType %s.\n", atomType->getName().c_str());
86     painCave.severity = OPENMD_ERROR;
87     painCave.isFatal = 1;
88     simError();
89     }
90    
91     LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
92     if (ljData == NULL) {
93     sprintf( painCave.errMsg,
94     "LJ::getLJParam could not convert GenericData to LJParam for\n"
95     "\tatom type %s\n", atomType->getName().c_str());
96     painCave.severity = OPENMD_ERROR;
97     painCave.isFatal = 1;
98     simError();
99     }
100    
101     return ljData->getData();
102     }
103    
104     RealType LJ::getSigma(AtomType* atomType) {
105     LJParam ljParam = getLJParam(atomType);
106     return ljParam.sigma;
107     }
108    
109     RealType LJ::getSigma(int atid) {
110     std::map<int, AtomType*> :: const_iterator it;
111     it = LJMap.find(atid);
112     if (it == LJMap.end()) {
113     sprintf( painCave.errMsg,
114     "LJ::getSigma could not find atid %d in LJMap\n",
115     (atid));
116     painCave.severity = OPENMD_ERROR;
117     painCave.isFatal = 1;
118     simError();
119     }
120     AtomType* atype = it->second;
121    
122     return getSigma(atype);
123     }
124    
125     RealType LJ::getSigma(AtomType* atomType1, AtomType* atomType2) {
126     RealType sigma1 = getSigma(atomType1);
127     RealType sigma2 = getSigma(atomType2);
128    
129     ForceFieldOptions& fopts = forceField_->getForceFieldOptions();
130     std::string DistanceMix = fopts.getDistanceMixingRule();
131     toUpper(DistanceMix);
132    
133     if (DistanceMix == "GEOMETRIC")
134     return sqrt(sigma1 * sigma2);
135     else
136     return 0.5 * (sigma1 + sigma2);
137     }
138    
139     RealType LJ::getEpsilon(AtomType* atomType) {
140     LJParam ljParam = getLJParam(atomType);
141     return ljParam.epsilon;
142     }
143    
144     RealType LJ::getEpsilon(int atid) {
145     std::map<int, AtomType*> :: const_iterator it;
146     it = LJMap.find(atid);
147     if (it == LJMap.end()) {
148     sprintf( painCave.errMsg,
149     "LJ::getEpsilon could not find atid %d in LJMap\n",
150     (atid));
151     painCave.severity = OPENMD_ERROR;
152     painCave.isFatal = 1;
153     simError();
154     }
155     AtomType* atype = it->second;
156    
157     return getEpsilon(atype);
158     }
159    
160     RealType LJ::getEpsilon(AtomType* atomType1, AtomType* atomType2) {
161     RealType epsilon1 = getEpsilon(atomType1);
162     RealType epsilon2 = getEpsilon(atomType2);
163     return sqrt(epsilon1 * epsilon2);
164     }
165    
166     void LJ::initialize() {
167     ForceField::AtomTypeContainer atomTypes = forceField_->getAtomTypes();
168     ForceField::AtomTypeContainer::MapTypeIterator i;
169     AtomType* at;
170    
171     for (at = atomTypes.beginType(i); at != NULL;
172     at = atomTypes.nextType(i)) {
173    
174     if (at->isLennardJones())
175     addType(at);
176     }
177    
178     ForceField::NonBondedInteractionTypeContainer nbiTypes = forceField_->getNonBondedInteractionTypes();
179     ForceField::NonBondedInteractionTypeContainer::MapTypeIterator j;
180     NonBondedInteractionType* nbt;
181    
182     for (nbt = nbiTypes.beginType(j); nbt != NULL;
183     nbt = nbiTypes.nextType(j)) {
184    
185     if (nbt->isLennardJones()) {
186    
187     std::pair<AtomType*, AtomType*> atypes = nbt->getAtomTypes();
188    
189     GenericData* data = nbt->getPropertyByName("LennardJones");
190     if (data == NULL) {
191     sprintf( painCave.errMsg, "LJ::rebuildMixingMap could not find\n"
192     "\tLennard-Jones parameters for %s - %s interaction.\n",
193     atypes.first->getName().c_str(),
194     atypes.second->getName().c_str());
195     painCave.severity = OPENMD_ERROR;
196     painCave.isFatal = 1;
197     simError();
198     }
199    
200     LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data);
201     if (ljData == NULL) {
202     sprintf( painCave.errMsg,
203     "LJ::rebuildMixingMap could not convert GenericData to\n"
204     "\tLJParam for %s - %s interaction.\n",
205     atypes.first->getName().c_str(),
206     atypes.second->getName().c_str());
207     painCave.severity = OPENMD_ERROR;
208     painCave.isFatal = 1;
209     simError();
210     }
211    
212     LJParam ljParam = ljData->getData();
213    
214     RealType sigma = ljParam.sigma;
215     RealType epsilon = ljParam.epsilon;
216    
217     addExplicitInteraction(atypes.first, atypes.second, sigma, epsilon);
218     }
219     }
220     initialized_ = true;
221     }
222    
223    
224    
225     void LJ::addType(AtomType* atomType){
226     RealType sigma1 = getSigma(atomType);
227     RealType epsilon1 = getEpsilon(atomType);
228    
229     // add it to the map:
230     AtomTypeProperties atp = atomType->getATP();
231     std::pair<std::map<int,AtomType*>::iterator,bool> ret;
232     ret = LJMap.insert( std::pair<int, AtomType*>(atp.ident, atomType) );
233     if (ret.second == false) {
234     sprintf( painCave.errMsg,
235     "LJ already had a previous entry with ident %d\n",
236     atp.ident);
237     painCave.severity = OPENMD_INFO;
238     painCave.isFatal = 0;
239     simError();
240     }
241    
242     // Now, iterate over all known types and add to the mixing map:
243    
244     std::map<int, AtomType*>::iterator it;
245     for( it = LJMap.begin(); it != LJMap.end(); ++it) {
246    
247     AtomType* atype2 = (*it).second;
248    
249     LJInteractionData mixer;
250     mixer.sigma = getSigma(atomType, atype2);
251     mixer.epsilon = getEpsilon(atomType, atype2);
252     mixer.sigmai = 1.0 / mixer.sigma;
253     mixer.explicitlySet = false;
254    
255     std::pair<AtomType*, AtomType*> key1, key2;
256     key1 = std::make_pair(atomType, atype2);
257     key2 = std::make_pair(atype2, atomType);
258    
259     MixingMap[key1] = mixer;
260     if (key2 != key1) {
261     MixingMap[key2] = mixer;
262     }
263     }
264     }
265    
266     void LJ::addExplicitInteraction(AtomType* atype1, AtomType* atype2, RealType sigma, RealType epsilon){
267    
268     // in case these weren't already in the map
269     addType(atype1);
270     addType(atype2);
271    
272     LJInteractionData mixer;
273     mixer.sigma = sigma;
274     mixer.epsilon = epsilon;
275     mixer.sigmai = 1.0 / mixer.sigma;
276     mixer.explicitlySet = true;
277    
278     std::pair<AtomType*, AtomType*> key1, key2;
279     key1 = std::make_pair(atype1, atype2);
280     key2 = std::make_pair(atype2, atype1);
281    
282     MixingMap[key1] = mixer;
283     if (key2 != key1) {
284     MixingMap[key2] = mixer;
285     }
286     }
287    
288     void LJ::calcForce(AtomType* at1, AtomType* at2, Vector3d d, RealType rij,
289     RealType r2, RealType rcut, RealType sw, RealType vdwMult,
290     RealType vpair, RealType pot, Vector3d f1) {
291    
292     if (!initialized_) initialize();
293    
294     RealType ros;
295     RealType rcos;
296     RealType myPot = 0.0;
297     RealType myPotC = 0.0;
298     RealType myDeriv = 0.0;
299     RealType myDerivC = 0.0;
300    
301     std::pair<AtomType*, AtomType*> key = std::make_pair(at1, at2);
302     LJInteractionData mixer = MixingMap[key];
303    
304     RealType sigmai = mixer.sigmai;
305     RealType epsilon = mixer.epsilon;
306    
307     ros = rij * sigmai;
308    
309     getLJfunc(ros, myPot, myDeriv);
310    
311     if (shiftedPot_) {
312     rcos = rcut * sigmai;
313     getLJfunc(rcos, myPotC, myDerivC);
314     myDerivC = 0.0;
315     } else if (LJ::shiftedFrc_) {
316     rcos = rcut * sigmai;
317     getLJfunc(rcos, myPotC, myDerivC);
318     myPotC = myPotC + myDerivC * (rij - rcut) * sigmai;
319     } else {
320     myPotC = 0.0;
321     myDerivC = 0.0;
322     }
323    
324     RealType pot_temp = vdwMult * epsilon * (myPot - myPotC);
325     vpair += pot_temp;
326    
327     RealType dudr = sw * vdwMult * epsilon * (myDeriv - myDerivC)*sigmai;
328    
329     pot += sw * pot_temp;
330     f1 = d * dudr / rij;
331    
332     return;
333     }
334    
335     void LJ::do_lj_pair(int *atid1, int *atid2, RealType *d, RealType *rij,
336     RealType *r2, RealType *rcut, RealType *sw,
337     RealType *vdwMult,
338     RealType *vpair, RealType *pot, RealType *f1) {
339    
340     if (!initialized_) initialize();
341    
342     AtomType* atype1 = LJMap[*atid1];
343     AtomType* atype2 = LJMap[*atid2];
344    
345     Vector3d disp(d[0], d[1], d[2]);
346     Vector3d frc(f1[0], f1[1], f1[2]);
347    
348     calcForce(atype1, atype2, disp, *rij, *r2, *rcut, *sw, *vdwMult, *vpair,
349     *pot, frc);
350     return;
351     }
352    
353     void LJ::getLJfunc(const RealType r, RealType pot, RealType deriv) {
354     RealType ri = 1.0 / r;
355     RealType ri2 = ri * ri;
356     RealType ri6 = ri2 * ri2 * ri2;
357     RealType ri7 = ri6 * ri;
358     RealType ri12 = ri6 * ri6;
359     RealType ri13 = ri12 * ri;
360    
361     pot = 4.0 * (ri12 - ri6);
362     deriv = 24.0 * (ri7 - 2.0 * ri13);
363     return;
364     }
365    
366    
367     void LJ::setLJDefaultCutoff(RealType *thisRcut, int *sP, int *sF) {
368     shiftedPot_ = (bool)(*sP);
369     shiftedFrc_ = (bool)(*sF);
370     }
371     }
372    
373     extern "C" {
374    
375     #define fortranGetSigma FC_FUNC(getsigma, GETSIGMA)
376     #define fortranGetEpsilon FC_FUNC(getepsilon, GETEPSILON)
377     #define fortranSetLJCutoff FC_FUNC(setljdefaultcutoff, SETLJDEFAULTCUTOFF)
378     #define fortranDoLJPair FC_FUNC(do_lj_pair, DO_LJ_PAIR)
379    
380     RealType fortranGetSigma(int* atid) {
381     return OpenMD::LJ::Instance()->getSigma(*atid);
382     }
383     RealType fortranGetEpsilon(int* atid) {
384     return OpenMD::LJ::Instance()->getEpsilon(*atid);
385     }
386     void fortranSetLJCutoff(RealType *rcut, int *shiftedPot, int *shiftedFrc) {
387     return OpenMD::LJ::Instance()->setLJDefaultCutoff(rcut, shiftedPot,
388     shiftedFrc);
389     }
390     void fortranDoLJPair(int *atid1, int *atid2, RealType *d, RealType *rij,
391     RealType *r2, RealType *rcut, RealType *sw,
392     RealType *vdwMult, RealType* vpair, RealType* pot,
393     RealType *f1){
394    
395     return OpenMD::LJ::Instance()->do_lj_pair(atid1, atid2, d, rij, r2, rcut,
396     sw, vdwMult, vpair, pot, f1);
397     }
398     }

Properties

Name Value
svn:eol-style native