Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2021 The VES code team 3 : (see the PEOPLE-VES file at the root of this folder for a list of names) 4 : 5 : See http://www.ves-code.org for more information. 6 : 7 : This file is part of VES code module. 8 : 9 : The VES code module 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 : The VES code module 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 the VES code module. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : 23 : #include "BasisFunctions.h" 24 : 25 : #include "core/ActionRegister.h" 26 : 27 : 28 : namespace PLMD { 29 : namespace ves { 30 : 31 : //+PLUMEDOC VES_BASISF BF_LEGENDRE 32 : /* 33 : Legendre polynomials basis functions. 34 : 35 : Use as basis functions [Legendre polynomials](https://en.wikipedia.org/wiki/Legendre_polynomials) 36 : $P_{n}(x)$ defined on a bounded interval. 37 : You need to provide the interval $[a,b]$ 38 : on which the basis functions are to be used, and the order of the 39 : expansion $N$ (i.e. the highest order polynomial used). 40 : The total number of basis functions is $N+1$ as the constant $P_{0}(x)=1$ 41 : is also included. 42 : These basis functions should not be used for periodic CVs. 43 : 44 : Intrinsically the Legendre polynomials are defined on the interval $[-1,1]$. 45 : A variable $t$ in the interval $[a,b]$ is transformed to a variable $x$ 46 : in the intrinsic interval $[-1,1]$ by using the transform function 47 : 48 : $$ 49 : x(t) = \frac{t-(a+b)/2} 50 : {(b-a)/2} 51 : $$ 52 : 53 : The Legendre polynomials are given by the recurrence relation 54 : 55 : $$ 56 : \begin{aligned} 57 : P_{0}(x) &= 1 \\ 58 : P_{1}(x) &= x \\ 59 : P_{n+1}(x) &= \frac{2n+1}{n+1} \, x \, P_{n}(x) - \frac{n}{n+1} \, P_{n-1}(x) 60 : \end{aligned} 61 : $$ 62 : 63 : The first 6 polynomials are shown below 64 : 65 :  66 : 67 : The Legendre polynomial are orthogonal over the interval $[-1,1]$ 68 : 69 : $$ 70 : \int_{-1}^{1} dx \, P_{n}(x)\, P_{m}(x) = \frac{2}{2n+1} \delta_{n,m} 71 : $$ 72 : 73 : By using the SCALED keyword the polynomials are scaled by a factor of 74 : $\sqrt{\frac{2n+1}{2}}$ such that they are orthonormal to 1. 75 : 76 : 77 : From the above equation it follows that integral of the basis functions 78 : over the uniform target distribution $p_{\mathrm{u}}(x)$ are given by 79 : 80 : $$ 81 : \int_{-1}^{1} dx \, P_{n}(x) p_{\mathrm{u}}(x) = \delta_{n,0}, 82 : $$ 83 : 84 : and thus always zero except for the constant $P_{0}(x)=1$. 85 : 86 : 87 : For further mathematical properties of the Legendre polynomials see for example 88 : the [Wikipedia page](https://en.wikipedia.org/wiki/Legendre_polynomials). 89 : 90 : ## Examples 91 : 92 : Here we employ a Legendre expansion of order 20 over the interval -4.0 to 8.0. 93 : This results in a total number of 21 basis functions. 94 : The label used to identify the basis function action can then be 95 : referenced later on in the input file. 96 : 97 : ```plumed 98 : bf_leg: BF_LEGENDRE MINIMUM=-4.0 MAXIMUM=8.0 ORDER=20 99 : ``` 100 : 101 : */ 102 : //+ENDPLUMEDOC 103 : 104 : class BF_Legendre : public BasisFunctions { 105 : bool scaled_; 106 : void setupUniformIntegrals() override; 107 : public: 108 : static void registerKeywords(Keywords&); 109 : explicit BF_Legendre(const ActionOptions&); 110 : void getAllValues(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const override; 111 : }; 112 : 113 : 114 : PLUMED_REGISTER_ACTION(BF_Legendre,"BF_LEGENDRE") 115 : 116 : 117 66 : void BF_Legendre::registerKeywords(Keywords& keys) { 118 66 : BasisFunctions::registerKeywords(keys); 119 66 : keys.addFlag("SCALED",false,"Scale the polynomials such that they are orthonormal to 1."); 120 66 : } 121 : 122 62 : BF_Legendre::BF_Legendre(const ActionOptions&ao): 123 : PLUMED_VES_BASISFUNCTIONS_INIT(ao), 124 62 : scaled_(false) { 125 62 : parseFlag("SCALED",scaled_); 126 124 : addKeywordToList("SCALED",scaled_); 127 62 : setNumberOfBasisFunctions(getOrder()+1); 128 124 : setIntrinsicInterval("-1.0","+1.0"); 129 : setNonPeriodic(); 130 : setIntervalBounded(); 131 62 : setType("Legendre"); 132 62 : setDescription("Legendre polynomials"); 133 62 : setLabelPrefix("L"); 134 62 : setupBF(); 135 62 : checkRead(); 136 62 : } 137 : 138 : 139 1625168 : void BF_Legendre::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const { 140 1625168 : inside_range=true; 141 1625168 : argT=translateArgument(arg, inside_range); 142 1625168 : std::vector<double> derivsT(derivs.size()); 143 : // 144 1625168 : values[0]=1.0; 145 1625168 : derivsT[0]=0.0; 146 1625168 : derivs[0]=0.0; 147 1625168 : values[1]=argT; 148 1625168 : derivsT[1]=1.0; 149 1625168 : derivs[1]=intervalDerivf(); 150 15470653 : for(unsigned int i=1; i < getOrder(); i++) { 151 13845485 : double io = static_cast<double>(i); 152 13845485 : values[i+1] = ((2.0*io+1.0)/(io+1.0))*argT*values[i] - (io/(io+1.0))*values[i-1]; 153 13845485 : derivsT[i+1] = ((2.0*io+1.0)/(io+1.0))*(values[i]+argT*derivsT[i])-(io/(io+1.0))*derivsT[i-1]; 154 13845485 : derivs[i+1] = intervalDerivf()*derivsT[i+1]; 155 : } 156 1625168 : if(scaled_) { 157 : // L0 is also scaled! 158 5088852 : for(unsigned int i=0; i<values.size(); i++) { 159 4632062 : double io = static_cast<double>(i); 160 4632062 : double sf = sqrt(io+0.5); 161 4632062 : values[i] *= sf; 162 4632062 : derivs[i] *= sf; 163 : } 164 : } 165 1625168 : if(!inside_range) { 166 131604 : for(unsigned int i=0; i<derivs.size(); i++) { 167 120046 : derivs[i]=0.0; 168 : } 169 : } 170 1625168 : } 171 : 172 : 173 60 : void BF_Legendre::setupUniformIntegrals() { 174 60 : setAllUniformIntegralsToZero(); 175 : double L0_int = 1.0; 176 60 : if(scaled_) { 177 : L0_int = sqrt(0.5); 178 : } 179 : setUniformIntegral(0,L0_int); 180 60 : } 181 : 182 : 183 : } 184 : }