Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-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_KernelFunctions_h 23 : #define __PLUMED_tools_KernelFunctions_h 24 : 25 : #include "Matrix.h" 26 : #include "core/Value.h" 27 : #include <vector> 28 : #include <memory> 29 : 30 : namespace PLMD { 31 : 32 6143 : class KernelFunctions { 33 : private: 34 : /// Is the metric matrix diagonal 35 : enum class matrixType {diagonal,multi,vonmises} dtype; 36 : /// What type of kernel are we using 37 : enum class kernelType {gaussian,truncatedgaussian,stretchedgaussian,uniform,triangular} ktype; 38 : /// The center of the kernel function 39 : std::vector<double> center; 40 : /// The width of the kernel 41 : std::vector<double> width; 42 : /// The height of the kernel 43 : double height; 44 : 45 : double stretchA=1.0; 46 : double stretchB=0.0; 47 : 48 : /// Used to set all the data in the kernel during construction - avoids double coding as this has two constructors 49 : void setData( const std::vector<double>& at, 50 : const std::vector<double>& sig, 51 : const std::string& type, 52 : const std::string& mtype, 53 : const double& w ); 54 : /// Convert the width into matrix form 55 : Matrix<double> getMatrix() const; 56 : public: 57 : explicit KernelFunctions( const std::string& input ); 58 : KernelFunctions( const std::vector<double>& at, 59 : const std::vector<double>& sig, 60 : const std::string& type, 61 : const std::string& mtype, 62 : const double& w ); 63 : explicit KernelFunctions( const KernelFunctions* in ); 64 : /// Normalise the function and scale the height accordingly 65 : void normalize( const std::vector<Value*>& myvals ); 66 : /// Get the dimensionality of the kernel 67 : unsigned ndim() const; 68 : /// Get the cutoff for a kernel 69 : double getCutoff(double width) const ; 70 : /// Get the position of the center 71 : std::vector<double> getCenter() const; 72 : /// Get the support 73 : std::vector<unsigned> getSupport( const std::vector<double>& dx ) const; 74 : /// get it in continuous form 75 : std::vector<double> getContinuousSupport( ) const; 76 : /// Evaluate the kernel function with constant intervals 77 : double evaluate( const std::vector<Value*>& pos, 78 : std::vector<double>& derivatives, 79 : bool usederiv=true, 80 : bool doInt=false, 81 : double lowI_=-1, 82 : double uppI_=-1 ) const; 83 : /// If this is a Gaussian kernel with a diagonal metric, get the parameters to evaluate it 84 : /// as a product over the dimensions: with t_i=(x_i-c_i)/w_i and r2=sum_i t_i^2, the kernel is 85 : /// height*(a*prod_i exp(-t_i^2/2)+b) for r2<r2cut and zero otherwise. Returns false otherwise. 86 : bool getSeparableGaussian(double & h, double & a, double & b, double & r2cut, std::vector<double> & w) const; 87 : /// Read a kernel function from a file 88 : static std::unique_ptr<KernelFunctions> read( IFile* ifile, 89 : const bool& cholesky, 90 : const std::vector<std::string>& valnames ); 91 : }; 92 : 93 : inline 94 974872 : Matrix<double> KernelFunctions::getMatrix() const { 95 : unsigned k=0, ncv=ndim(); 96 974872 : Matrix<double> mymatrix(ncv,ncv); 97 2924616 : for(unsigned i=0; i<ncv; i++) { 98 4874360 : for(unsigned j=i; j<ncv; j++) { 99 2924616 : mymatrix(i,j)=mymatrix(j,i)=width[k]; // recompose the full inverse matrix 100 2924616 : k++; 101 : } 102 : } 103 974872 : return mymatrix; 104 : } 105 : 106 : inline 107 : unsigned KernelFunctions::ndim() const { 108 3891993 : return center.size(); 109 : } 110 : 111 : inline 112 : std::vector<double> KernelFunctions::getCenter() const { 113 6155 : return center; 114 : } 115 : 116 : } 117 : #endif