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