Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-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_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: 42 6 : 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> 49 2187 : 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 2187 : Brent1DRootSearch<F1dim<FCLASS> > bb(f1dim); 52 : 53 : // This does the actual search for the root 54 2187 : double ax=0.0, xx=1.0; 55 2187 : bb.bracket( ax, xx, &F1dim<FCLASS>::getEng ); 56 2187 : double xmin=bb.search( &F1dim<FCLASS>::getEng ); 57 8748 : for(unsigned i=0; i<p.size(); ++i) p[i] += xmin*dir[i]; 58 2187 : } 59 : 60 : template <class FCLASS> 61 1896 : 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 1896 : F1dim<FCLASS> f1dim( p, dir, myclass_func, myfunc, NULL ); 64 : // Actually do the search 65 1896 : doSearch( dir, p, f1dim ); 66 1896 : } 67 : 68 : template <class FCLASS> 69 291 : 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 291 : F1dim<FCLASS> f1dim( p, dir, myclass_func, NULL, myfunc ); 72 : // Actually do the search 73 291 : doSearch( dir, p, f1dim ); 74 291 : } 75 : 76 : } 77 : #endif