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_COSINE 32 : /* 33 : Fourier cosine basis functions. 34 : 35 : Use as basis functions Fourier cosine series defined on a periodic interval. 36 : You need to provide the periodic interval $[a,b]$ 37 : on which the basis functions are to be used, and the order of the 38 : expansion $N$ (i.e. the highest Fourier cosine mode used). 39 : The total number of basis functions is $N+1$ as 40 : the constant $f_{0}(x)=1$ is also included. 41 : These basis functions should only be used for periodic CVs. 42 : They can be useful if the periodic function being expanded is an 43 : even function, i.e. $F(-x)=F(x)$. 44 : 45 : The Fourier cosine basis functions are given by 46 : 47 : $$ 48 : \begin{aligned} 49 : f_{0}(x) &= 1 \\ 50 : f_{1}(x) &= cos(\frac{2\pi }{P} x) \\ 51 : f_{2}(x) &= cos(2 \cdot \frac{2\pi}{P} x) \\ 52 : f_{3}(x) &= cos(3 \cdot \frac{2\pi}{P} x) \\ 53 : & \vdots \\ 54 : f_{n}(x) &= cos(n \cdot \frac{2\pi}{P} x) \\ 55 : & \vdots \\ 56 : f_{N}(x) &= cos(N \cdot \frac{2\pi}{P} x) \\ 57 : \end{aligned} 58 : $$ 59 : 60 : where $P=(b-a)$ is the periodicity of the interval. 61 : They are orthogonal over the interval $[a,b]$ 62 : 63 : $$ 64 : \int_{a}^{b} dx \, f_{n}(x)\, f_{m}(x) = 65 : \begin{cases} 66 : 0 & n \neq m \\ 67 : (b-a) & n = m = 0 \\ 68 : (b-a)/2 & n = m \neq 0 69 : \end{cases}. 70 : $$ 71 : 72 : ## Examples 73 : 74 : Here we employ a Fourier cosine expansion of order 10 over the periodic interval 75 : $-\pi$ to $+\pi$. 76 : This results in a total number of 11 basis functions. 77 : The label used to identify the basis function action can then be 78 : referenced later on in the input file. 79 : 80 : ```plumed 81 : BF_COSINE MINIMUM=-pi MAXIMUM=+pi ORDER=10 LABEL=bf1 82 : ``` 83 : 84 : 85 : 86 : */ 87 : //+ENDPLUMEDOC 88 : 89 : 90 : class BF_Cosine : public BasisFunctions { 91 : void setupLabels() override; 92 : void setupUniformIntegrals() override; 93 : public: 94 : static void registerKeywords(Keywords&); 95 : explicit BF_Cosine(const ActionOptions&); 96 : void getAllValues(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const override; 97 : }; 98 : 99 : 100 : PLUMED_REGISTER_ACTION(BF_Cosine,"BF_COSINE") 101 : 102 : 103 6 : void BF_Cosine::registerKeywords(Keywords& keys) { 104 6 : BasisFunctions::registerKeywords(keys); 105 6 : } 106 : 107 : 108 4 : BF_Cosine::BF_Cosine(const ActionOptions&ao): 109 4 : PLUMED_VES_BASISFUNCTIONS_INIT(ao) { 110 4 : setNumberOfBasisFunctions(getOrder()+1); 111 8 : setIntrinsicInterval("-pi","+pi"); 112 : setPeriodic(); 113 : setIntervalBounded(); 114 4 : setType("trigonometric_cos"); 115 4 : setDescription("Cosine"); 116 4 : setupBF(); 117 4 : checkRead(); 118 4 : } 119 : 120 : 121 9229 : void BF_Cosine::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const { 122 : // plumed_assert(values.size()==numberOfBasisFunctions()); 123 : // plumed_assert(derivs.size()==numberOfBasisFunctions()); 124 9229 : inside_range=true; 125 9229 : argT=translateArgument(arg, inside_range); 126 9229 : values[0]=1.0; 127 9229 : derivs[0]=0.0; 128 101519 : for(unsigned int i=1; i < getOrder()+1; i++) { 129 92290 : double io = i; 130 92290 : double cos_tmp = cos(io*argT); 131 92290 : double sin_tmp = sin(io*argT); 132 92290 : values[i] = cos_tmp; 133 92290 : derivs[i] = -io*sin_tmp*intervalDerivf(); 134 : } 135 9229 : if(!inside_range) { 136 960 : for(unsigned int i=0; i<derivs.size(); i++) { 137 880 : derivs[i]=0.0; 138 : } 139 : } 140 9229 : } 141 : 142 : 143 4 : void BF_Cosine::setupLabels() { 144 4 : setLabel(0,"1"); 145 44 : for(unsigned int i=1; i < getOrder()+1; i++) { 146 : std::string is; 147 40 : Tools::convert(i,is); 148 80 : setLabel(i,"cos("+is+"*s)"); 149 : } 150 4 : } 151 : 152 : 153 3 : void BF_Cosine::setupUniformIntegrals() { 154 3 : setAllUniformIntegralsToZero(); 155 : setUniformIntegral(0,1.0); 156 3 : } 157 : 158 : 159 : } 160 : }