1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2012-2021 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 class KernelFunctions {
33 private:
34 /// Is the metric matrix diagonal
35   enum {diagonal,multi,vonmises} dtype;
36 /// What type of kernel are we using
37   enum {gaussian,truncatedgaussian,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 /// Used to set all the data in the kernel during construction - avoids double coding as this has two constructors
45   void setData( const std::vector<double>& at, const std::vector<double>& sig, const std::string& type, const std::string& mtype, const double& w );
46 /// Convert the width into matrix form
47   Matrix<double> getMatrix() const;
48 public:
49   explicit KernelFunctions( const std::string& input );
50   KernelFunctions( const std::vector<double>& at, const std::vector<double>& sig, const std::string& type, const std::string& mtype, const double& w );
51   explicit KernelFunctions( const KernelFunctions* in );
52 /// Normalise the function and scale the height accordingly
53   void normalize( const std::vector<Value*>& myvals );
54 /// Get the dimensionality of the kernel
55   unsigned ndim() const;
56 /// Get the cutoff for a kernel
57   double getCutoff( const double& width ) const ;
58 /// Get the position of the center
59   std::vector<double> getCenter() const;
60 /// Get the support
61   std::vector<unsigned> getSupport( const std::vector<double>& dx ) const;
62 /// get it in continuous form
63   std::vector<double> getContinuousSupport( ) const;
64 /// Evaluate the kernel function with constant intervals
65   double evaluate( const std::vector<Value*>& pos, std::vector<double>& derivatives, bool usederiv=true, bool doInt=false, double lowI_=-1, double uppI_=-1 ) const;
66 /// Read a kernel function from a file
67   static std::unique_ptr<KernelFunctions> read( IFile* ifile, const bool& cholesky, const std::vector<std::string>& valnames );
68 };
69 
70 inline
getMatrix()71 Matrix<double> KernelFunctions::getMatrix() const {
72   unsigned k=0, ncv=ndim(); Matrix<double> mymatrix(ncv,ncv);
73   for(unsigned i=0; i<ncv; i++) {
74     for(unsigned j=i; j<ncv; j++) {
75       mymatrix(i,j)=mymatrix(j,i)=width[k]; // recompose the full inverse matrix
76       k++;
77     }
78   }
79   return mymatrix;
80 }
81 
82 inline
ndim()83 unsigned KernelFunctions::ndim() const {
84   return center.size();
85 }
86 
87 inline
getCenter()88 std::vector<double> KernelFunctions::getCenter() const {
89   return center;
90 }
91 
92 }
93 #endif
94