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 63 : void BF_Legendre::registerKeywords(Keywords& keys) { 118 63 : BasisFunctions::registerKeywords(keys); 119 63 : keys.addFlag("SCALED",false,"Scale the polynomials such that they are orthonormal to 1."); 120 63 : } 121 : 122 61 : BF_Legendre::BF_Legendre(const ActionOptions&ao): 123 : PLUMED_VES_BASISFUNCTIONS_INIT(ao), 124 61 : scaled_(false) { 125 61 : parseFlag("SCALED",scaled_); 126 122 : addKeywordToList("SCALED",scaled_); 127 61 : setNumberOfBasisFunctions(getOrder()+1); 128 122 : setIntrinsicInterval("-1.0","+1.0"); 129 : setNonPeriodic(); 130 : setIntervalBounded(); 131 61 : setType("Legendre"); 132 61 : setDescription("Legendre polynomials"); 133 61 : setLabelPrefix("L"); 134 61 : setupBF(); 135 61 : checkRead(); 136 61 : } 137 : 138 : 139 1625057 : void BF_Legendre::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const { 140 : // plumed_assert(values.size()==numberOfBasisFunctions()); 141 : // plumed_assert(derivs.size()==numberOfBasisFunctions()); 142 1625057 : inside_range=true; 143 1625057 : argT=translateArgument(arg, inside_range); 144 1625057 : std::vector<double> derivsT(derivs.size()); 145 : // 146 1625057 : values[0]=1.0; 147 1625057 : derivsT[0]=0.0; 148 1625057 : derivs[0]=0.0; 149 1625057 : values[1]=argT; 150 1625057 : derivsT[1]=1.0; 151 1625057 : derivs[1]=intervalDerivf(); 152 15470320 : for(unsigned int i=1; i < getOrder(); i++) { 153 13845263 : double io = static_cast<double>(i); 154 13845263 : values[i+1] = ((2.0*io+1.0)/(io+1.0))*argT*values[i] - (io/(io+1.0))*values[i-1]; 155 13845263 : derivsT[i+1] = ((2.0*io+1.0)/(io+1.0))*(values[i]+argT*derivsT[i])-(io/(io+1.0))*derivsT[i-1]; 156 13845263 : derivs[i+1] = intervalDerivf()*derivsT[i+1]; 157 : } 158 1625057 : if(scaled_) { 159 : // L0 is also scaled! 160 5088852 : for(unsigned int i=0; i<values.size(); i++) { 161 4632062 : double io = static_cast<double>(i); 162 4632062 : double sf = sqrt(io+0.5); 163 4632062 : values[i] *= sf; 164 4632062 : derivs[i] *= sf; 165 : } 166 : } 167 1625057 : if(!inside_range) { 168 131484 : for(unsigned int i=0; i<derivs.size(); i++) { 169 119950 : derivs[i]=0.0; 170 : } 171 : } 172 1625057 : } 173 : 174 : 175 59 : void BF_Legendre::setupUniformIntegrals() { 176 59 : setAllUniformIntegralsToZero(); 177 : double L0_int = 1.0; 178 59 : if(scaled_) { 179 : L0_int = sqrt(0.5); 180 : } 181 : setUniformIntegral(0,L0_int); 182 59 : } 183 : 184 : 185 : } 186 : }