1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2016-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_RootFindingBase_h
23 #define __PLUMED_tools_RootFindingBase_h
24 
25 #include "MinimiseBase.h"
26 #include "Brent1DRootSearch.h"
27 
28 namespace PLMD {
29 
30 template <class FCLASS>
31 class RootFindingBase {
32 private:
33 /// This is the pointer to the member function in the energy
34 /// calculating class that calculates the energy
35   typedef double(FCLASS::*engf_pointer)( const std::vector<double>& p, std::vector<double>& der ) const ;
36   typedef double(FCLASS::*engfnc_pointer)( const std::vector<double>& p, std::vector<double>& der ) ;
37 /// The class that calculates the energy given a position
38   FCLASS* myclass_func;
39 /// This actually does the search for a root
40   void doSearch( const std::vector<double>& dir, std::vector<double>& p, F1dim<FCLASS>& f1dim  ) const ;
41 public:
RootFindingBase(FCLASS * funcc)42   explicit RootFindingBase( FCLASS* funcc ) : myclass_func(funcc) {}
43 /// This is the line minimiser
44   void linesearch( const std::vector<double>& dir, std::vector<double>& p, engf_pointer myfunc ) const ;
45   void lsearch( const std::vector<double>& dir, std::vector<double>& p, engfnc_pointer myfunc ) const ;
46 };
47 
48 template <class FCLASS>
doSearch(const std::vector<double> & dir,std::vector<double> & p,F1dim<FCLASS> & f1dim)49 void RootFindingBase<FCLASS>::doSearch( const std::vector<double>& dir, std::vector<double>& p, F1dim<FCLASS>& f1dim ) const {
50   // Construct an object that will do the line search for the minimum
51   Brent1DRootSearch<F1dim<FCLASS> > bb(f1dim);
52 
53   // This does the actual search for the root
54   double ax=0.0, xx=1.0;
55   bb.bracket( ax, xx, &F1dim<FCLASS>::getEng );
56   double xmin=bb.search( &F1dim<FCLASS>::getEng );
57   for(unsigned i=0; i<p.size(); ++i) p[i] += xmin*dir[i];
58 }
59 
60 template <class FCLASS>
linesearch(const std::vector<double> & dir,std::vector<double> & p,engf_pointer myfunc)61 void RootFindingBase<FCLASS>::linesearch( const std::vector<double>& dir, std::vector<double>& p, engf_pointer myfunc ) const {
62   // Construct the object that turns points on a line into vectors
63   F1dim<FCLASS> f1dim( p, dir, myclass_func, myfunc, NULL );
64   // Actually do the search
65   doSearch( dir, p, f1dim );
66 }
67 
68 template <class FCLASS>
lsearch(const std::vector<double> & dir,std::vector<double> & p,engfnc_pointer myfunc)69 void RootFindingBase<FCLASS>::lsearch( const std::vector<double>& dir, std::vector<double>& p, engfnc_pointer myfunc ) const {
70   // Construct the object that turns points on a line into vectors
71   F1dim<FCLASS> f1dim( p, dir, myclass_func, NULL, myfunc );
72   // Actually do the search
73   doSearch( dir, p, f1dim );
74 }
75 
76 }
77 #endif
78