ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/nonbonded/SwitchingFunction.cpp
Revision: 1530
Committed: Tue Dec 28 21:47:55 2010 UTC (14 years, 4 months ago) by gezelter
File size: 5953 byte(s)
Log Message:
Moved switching functions and force options over to the C++ side, and
removed them from Fortran.

File Contents

# User Rev Content
1 gezelter 1530 /*
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 <cmath>
43    
44     #include "nonbonded/SwitchingFunction.hpp"
45     #include "utils/simError.h"
46    
47     using namespace std;
48     namespace OpenMD {
49    
50     SwitchingFunction::SwitchingFunction() : np_(150), haveSpline_(false),
51     isCubic_(true), functionType_(cubic) {}
52    
53     void SwitchingFunction::setSwitchType(SwitchingFunctionType sft) {
54     if ((sft == fifth_order_poly) || (sft == cubic)) {
55     if (haveSpline_) {
56     delete switchSpline_;
57     setSwitch(rin_, rout_);
58     }
59     } else {
60     sprintf( painCave.errMsg,
61     "SwitchingFunction::setSwitchType was given unknown function type\n");
62     painCave.severity = OPENMD_ERROR;
63     painCave.isFatal = 1;
64     simError();
65     }
66     }
67    
68     void SwitchingFunction::setSwitch(RealType rinner, RealType router) {
69     if (router < rinner) {
70     sprintf( painCave.errMsg,
71     "SwitchingFunction::setSwitch was given rinner (%lf) which was\n"
72     "\tlarger than router (%lf).\n", rinner, router);
73     painCave.severity = OPENMD_ERROR;
74     painCave.isFatal = 1;
75     simError();
76     }
77     if (router < 0.0) {
78     sprintf( painCave.errMsg,
79     "SwitchingFunction::setSwitch was given router (%lf) which was\n"
80     "\tless than zero.\n", router);
81     painCave.severity = OPENMD_ERROR;
82     painCave.isFatal = 1;
83     simError();
84     }
85     if (rinner < 0.0) {
86     sprintf( painCave.errMsg,
87     "SwitchingFunction::setSwitch was given rinner (%lf) which was\n"
88     "\tless than zero.\n", router);
89     painCave.severity = OPENMD_ERROR;
90     painCave.isFatal = 1;
91     simError();
92     }
93    
94     rin_ = rinner;
95     rout_ = router;
96     rin2_ = rin_ * rin_;
97     rout2_ = rout_ * rout_;
98    
99     if ((router - rinner) < 1.0e-8) {
100     // no reason to set up spline if the switching region is tiny
101     return;
102     }
103    
104     // setup r -> sw lookup spline
105     CubicSpline* switchSpline_ = new CubicSpline();
106     if (functionType_ == fifth_order_poly) {
107     isCubic_ = false;
108     RealType c0 = 1.0;
109     RealType c3 = -10.0;
110     RealType c4 = 15.0;
111     RealType c5 = -6.0;
112    
113     RealType dx, r, yval, rval, rval2, rval3, rval4, rval5;
114     RealType rvaldi, rvaldi2, rvaldi3, rvaldi4, rvaldi5;
115    
116     dx = (rout_ - rin_) / RealType(np_-1);
117    
118     for (int i = 0; i < np_; i++) {
119     r = rin_ + RealType(i)*dx;
120     rval = ( r - rin_ );
121     rval2 = rval*rval;
122     rval3 = rval2*rval;
123     rval4 = rval2*rval2;
124     rval5 = rval3*rval2;
125     rvaldi = 1.0 / ( rout_ - rin_ );
126     rvaldi2 = rvaldi*rvaldi;
127     rvaldi3 = rvaldi2*rvaldi;
128     rvaldi4 = rvaldi2*rvaldi2;
129     rvaldi5 = rvaldi3*rvaldi2;
130     yval= c0 + c3*rval3*rvaldi3 + c4*rval4*rvaldi4 + c5*rval5*rvaldi5;
131     switchSpline_->addPoint(r, yval);
132     }
133     } else {
134     // cubic splines only need 2 points to do a cubic switching function...
135     isCubic_ = true;
136     switchSpline_->addPoint(rin_, 1.0);
137     switchSpline_->addPoint(rout_, 0.0);
138     }
139     haveSpline_ = true;
140     return;
141     }
142    
143     bool SwitchingFunction::getSwitch(RealType r2, RealType sw, RealType dswdr,
144     RealType r) {
145     sw = 1.0;
146     dswdr = 0.0;
147     bool in_switching_region = false;
148    
149     if (r2 > rin2_) {
150     if (r2 > rout2_) {
151     sw = 0.0;
152     dswdr = 0.0;
153     return in_switching_region;
154     } else {
155     in_switching_region = true;
156     r = sqrt(r2);
157     pair<RealType, RealType> result = switchSpline_->getValueAndDerivativeAt(r);
158     sw = result.first;
159     dswdr = result.second;
160     return in_switching_region;
161     }
162     } else {
163     return in_switching_region;
164     }
165     }
166     }

Properties

Name Value
svn:eol-style native