OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SwitchingFunction.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "nonbonded/SwitchingFunction.hpp"
49
50#include <cmath>
51#include <cstdio>
52#include <iostream>
53
54#include "utils/simError.h"
55
56using namespace std;
57namespace OpenMD {
58
59 SwitchingFunction::SwitchingFunction() :
60 functionType_(cubic), haveSpline_(false), isCubic_(true), np_(150) {
61 switchSpline_ = std::make_shared<CubicSpline>();
62 }
63
64 void SwitchingFunction::setSwitchType(SwitchingFunctionType sft) {
65 if ((sft == fifth_order_poly) || (sft == cubic)) {
66 if (haveSpline_) {
67 switchSpline_ = std::make_shared<CubicSpline>();
68 setSwitch(rin_, rout_);
69 }
70 } else {
71 snprintf(
72 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
73 "SwitchingFunction::setSwitchType was given unknown function type\n");
74 painCave.severity = OPENMD_ERROR;
75 painCave.isFatal = 1;
76 simError();
77 }
78 }
79
80 void SwitchingFunction::setSwitch(RealType rinner, RealType router) {
81 if (router < rinner) {
82 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
83 "SwitchingFunction::setSwitch was given rinner (%lf) which was\n"
84 "\tlarger than router (%lf).\n",
85 rinner, router);
86 painCave.severity = OPENMD_ERROR;
87 painCave.isFatal = 1;
88 simError();
89 }
90 if (router < 0.0) {
91 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
92 "SwitchingFunction::setSwitch was given router (%lf) which was\n"
93 "\tless than zero.\n",
94 router);
95 painCave.severity = OPENMD_ERROR;
96 painCave.isFatal = 1;
97 simError();
98 }
99 if (rinner < 0.0) {
100 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
101 "SwitchingFunction::setSwitch was given rinner (%lf) which was\n"
102 "\tless than zero.\n",
103 router);
104 painCave.severity = OPENMD_ERROR;
105 painCave.isFatal = 1;
106 simError();
107 }
108
109 rin_ = rinner;
110 rout_ = router;
111 rin2_ = rin_ * rin_;
112 rout2_ = rout_ * rout_;
113
114 if ((router - rinner) < 1.0e-8) {
115 // no reason to set up spline if the switching region is tiny
116 return;
117 }
118
119 // setup r -> sw lookup spline
120 if (functionType_ == fifth_order_poly) {
121 isCubic_ = false;
122 RealType c0 = 1.0;
123 RealType c3 = -10.0;
124 RealType c4 = 15.0;
125 RealType c5 = -6.0;
126
127 RealType dx, r, yval, rval, rval2, rval3, rval4, rval5;
128 RealType rvaldi, rvaldi2, rvaldi3, rvaldi4, rvaldi5;
129
130 dx = (rout_ - rin_) / RealType(np_ - 1);
131
132 for (int i = 0; i < np_; i++) {
133 r = rin_ + RealType(i) * dx;
134 rval = (r - rin_);
135 rval2 = rval * rval;
136 rval3 = rval2 * rval;
137 rval4 = rval2 * rval2;
138 rval5 = rval3 * rval2;
139 rvaldi = 1.0 / (rout_ - rin_);
140 rvaldi2 = rvaldi * rvaldi;
141 rvaldi3 = rvaldi2 * rvaldi;
142 rvaldi4 = rvaldi2 * rvaldi2;
143 rvaldi5 = rvaldi3 * rvaldi2;
144 yval = c0 + c3 * rval3 * rvaldi3 + c4 * rval4 * rvaldi4 +
145 c5 * rval5 * rvaldi5;
146 switchSpline_->addPoint(r, yval);
147 }
148 } else {
149 // cubic splines only need 2 points to do a cubic switching function...
150 isCubic_ = true;
151 switchSpline_->addPoint(rin_, 1.0);
152 switchSpline_->addPoint(rout_, 0.0);
153 }
154 haveSpline_ = true;
155 return;
156 }
157
158 bool SwitchingFunction::getSwitch(const RealType& r2, RealType& sw,
159 RealType& dswdr, RealType& r) {
160 sw = 1.0;
161 dswdr = 0.0;
162
163 bool in_switching_region = false;
164
165 if (r2 > rin2_) {
166 if (r2 > rout2_) {
167 sw = 0.0;
168 } else {
169 in_switching_region = true;
170 r = sqrt(r2);
171 switchSpline_->getValueAndDerivativeAt(r, sw, dswdr);
172 }
173 }
174 return in_switching_region;
175 }
176} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.