Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_tools_SwitchingFunction_h 23 : #define __PLUMED_tools_SwitchingFunction_h 24 : 25 : #include <string> 26 : #include <vector> 27 : #include <memory> 28 : #include "lepton/Lepton.h" 29 : 30 : namespace PLMD { 31 : 32 : class Log; 33 : class Keywords; 34 : namespace switchContainers { 35 : /// container for the actual switching function used by PLMD::SwitchingFunction 36 : class baseSwitch { 37 : protected: 38 : /// Minimum distance (before this, function is one) 39 : double d0=0.0; 40 : /// Maximum distance (after this, function is zero) 41 : double dmax=0.0; 42 : /// Square of dmax, useful in calculateSqr() 43 : double dmax_2=0.0; 44 : /// We store the inverse to avoid a division 45 : double invr0=0.0; 46 : /// Square of invr0, useful in calculateSqr() 47 : double invr0_2=0.0; 48 : /// Parameters for stretching the function to zero at d_max 49 : double stretch=1.0; 50 : double shift=0.0; 51 : std::string mytype; 52 : virtual std::string specificDescription() const; 53 : // 54 : virtual double function(double rdist, double& dfunc) const=0; 55 : public: 56 : baseSwitch(double D0,double DMAX, double R0, std::string_view name); 57 : virtual ~baseSwitch(); 58 : ///the driver for the function (prepares rdist or returns 1 or 0 automatically) 59 : virtual double calculate(double distance, double& dfunc) const; 60 : virtual double calculateSqr(double distance2, double& dfunc) const; 61 : void setupStretch(); 62 : void removeStretch(); 63 : std::string description() const; 64 : double get_d0() const; 65 : double get_r0() const; 66 : double get_dmax() const; 67 : double get_dmax2() const; 68 : }; 69 : } // namespace switchContainers 70 : 71 : /// \ingroup TOOLBOX 72 : /// Small class to compute switching functions. 73 : /// Switching functions are created using set() and 74 : /// then can be used with function calculate() or calculateSqr(). 75 : /// Since this is typically computed on a distance vector, 76 : /// the second all (calculateSqr()) allows to skip the calculation 77 : /// of a square root in some case, thus potentially increasing 78 : /// performances. 79 2687 : class SwitchingFunction { 80 : /// This is to check that switching function has been initialized 81 : bool init=false; 82 : std::unique_ptr<switchContainers::baseSwitch> function; 83 : public: 84 : static void registerKeywords( Keywords& keys ); 85 : /// Set a "rational" switching function. 86 : /// Notice that a d_max is set automatically to a value such that 87 : /// f(d_max)=0.00001. 88 : void set(int nn,int mm,double r_0,double d_0); 89 : /// Set an arbitrary switching function. 90 : /// Parse the string in definition and possibly returns errors 91 : /// in the errormsg string 92 : void set(const std::string& definition, std::string& errormsg); 93 : /// Returns a string with a description of the switching function 94 : std::string description() const ; 95 : /// Compute the switching function. 96 : /// Returns s(x). df will be set to the value of the derivative 97 : /// of the switching function _divided_by_x 98 : double calculate(double x,double&df)const; 99 : /// Compute the switching function. 100 : /// Returns \f$ s(\sqrt{x})\f$ . 101 : /// df will be set to the \f$ \frac{1}{\sqrt{x}}\frac{ds}{d\sqrt{x}}= 2 \frac{ds}{dx}\f$ 102 : /// (same as calculate()). 103 : /// The advantage is that in some case the expensive square root can be avoided 104 : /// (namely for rational functions, if nn and mm are even and d0 is zero) 105 : double calculateSqr(double distance2,double&dfunc)const; 106 : /// Returns d0 107 : double get_d0() const; 108 : /// Returns r0 109 : double get_r0() const; 110 : /// Return dmax 111 : double get_dmax() const; 112 : /// Return dmax squared 113 : double get_dmax2() const; 114 : }; 115 : 116 : } //namespace PLMD 117 : 118 : #endif