LCOV - code coverage report
Current view: top level - gridtools - GridSearch.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 52 54 96.3 %
Date: 2026-03-30 13:16:06 Functions: 2 2 100.0 %

          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_gridtools_GridSearch_h
      23             : #define __PLUMED_gridtools_GridSearch_h
      24             : 
      25             : #include "tools/MinimiseBase.h"
      26             : #include "GridVessel.h"
      27             : #include <iostream>
      28             : #include <memory>
      29             : 
      30             : namespace PLMD {
      31             : namespace gridtools {
      32             : 
      33             : template <class FCLASS>
      34           1 : class GridSearch {
      35             : private:
      36             : /// This is the pointer to the member function in the energy
      37             : /// calculating class that calculates the energy
      38             :   typedef double(FCLASS::*engf_pointer)( const std::vector<double>& p, std::vector<double>& der );
      39             :   FCLASS* myclass_func;
      40             :   std::unique_ptr<GridVessel> mygrid;
      41             :   std::unique_ptr<GridVessel> myfgrid;
      42             : public:
      43           1 :   GridSearch( const std::vector<double>& mmin, const std::vector<double>& mmax, const std::vector<unsigned>& ng, const std::vector<unsigned>& nfg, FCLASS* funcc ) :
      44           1 :     myclass_func( funcc ) {
      45             :     // Create the grid objects
      46           1 :     std::string nstr, vstring="COMPONENTS=func COORDINATES=x1";
      47           2 :     for(unsigned i=1; i<mmin.size(); ++i) {
      48           1 :       Tools::convert(i+1,nstr);
      49           2 :       vstring += ",x" + nstr;
      50             :     }
      51             :     vstring += " PBC=F";
      52           2 :     for(unsigned i=1; i<mmin.size(); ++i) {
      53             :       vstring += ",F";
      54             :     }
      55           2 :     vesselbase::VesselOptions da("mygrid","",-1,vstring,NULL);
      56           1 :     Keywords keys;
      57           1 :     gridtools::GridVessel::registerKeywords( keys );
      58           1 :     vesselbase::VesselOptions dar( da, keys );
      59           2 :     mygrid=Tools::make_unique<GridVessel>(dar);
      60           1 :     if( nfg[0]>0 ) {
      61           2 :       myfgrid=Tools::make_unique<GridVessel>(dar);
      62             :     }
      63             : 
      64             :     // Now setup the min and max values for the grid
      65           1 :     std::vector<std::string> gmin( nfg.size() ), gmax( nfg.size() );
      66             :     std::vector<double> dummy_spacing;
      67           3 :     for(unsigned i=0; i<nfg.size(); ++i) {
      68           2 :       Tools::convert(mmin[i],gmin[i]);
      69           2 :       Tools::convert(mmax[i],gmax[i]);
      70             :     }
      71           1 :     mygrid->setBounds( gmin, gmax, ng, dummy_spacing );
      72           1 :     mygrid->resize();
      73           1 :     if( myfgrid ) {
      74           1 :       myfgrid->setBounds( gmin, gmax, nfg, dummy_spacing );
      75             :     }
      76           2 :   }
      77             :   bool minimise( std::vector<double>& p, engf_pointer myfunc );
      78             : };
      79             : 
      80             : template <class FCLASS>
      81         200 : bool GridSearch<FCLASS>::minimise( std::vector<double>& p, engf_pointer myfunc ) {
      82         200 :   std::vector<double> der( p.size() );
      83         200 :   std::vector<double> coords( p.size() );
      84         200 :   double initial_eng = (myclass_func->*myfunc)( p, der );
      85         200 :   mygrid->getGridPointCoordinates( 0, coords );
      86         200 :   double emin=(myclass_func->*myfunc)( coords, der );
      87         200 :   mygrid->setValueAndDerivatives( 0, 0, emin, der );
      88         200 :   unsigned pmin=0;
      89       24200 :   for(unsigned i=1; i<mygrid->getNumberOfPoints(); ++i) {
      90       24000 :     mygrid->getGridPointCoordinates( i, coords );
      91       24000 :     double eng = (myclass_func->*myfunc)( coords, der );
      92       24000 :     mygrid->setValueAndDerivatives( i, 0, eng, der );
      93       24000 :     if( eng<emin ) {
      94        2344 :       emin=eng;
      95        2344 :       pmin=i;
      96             :     }
      97             :   }
      98             :   // This prevents division by zero
      99             :   mygrid->setNorm( 1.0 );
     100             : 
     101         200 :   if( myfgrid ) {
     102         200 :     myfgrid->getGridPointCoordinates( 0, coords );
     103         200 :     pmin=0;
     104         200 :     double emin=mygrid->getValueAndDerivatives( coords, 0, der );
     105      520200 :     for(unsigned i=1; i<myfgrid->getNumberOfPoints(); ++i) {
     106      520000 :       myfgrid->getGridPointCoordinates( i, coords );
     107      520000 :       double eng = mygrid->getValueAndDerivatives( coords, 0, der );
     108      520000 :       if( eng<emin ) {
     109             :         emin=eng;
     110       16431 :         pmin=i;
     111             :       }
     112             :     }
     113         200 :     myfgrid->getGridPointCoordinates( pmin, coords );
     114         200 :     double checkEng = (myclass_func->*myfunc)( coords, der );
     115         200 :     if( checkEng<initial_eng ) {
     116          22 :       myfgrid->getGridPointCoordinates( pmin, p );
     117             :       return true;
     118             :     } else {
     119             :       return false;
     120             :     }
     121             :   }
     122             : 
     123           0 :   if( emin<initial_eng ) {
     124           0 :     mygrid->getGridPointCoordinates( pmin, p );
     125             :     return true;
     126             :   } else {
     127             :     return false;
     128             :   }
     129             : }
     130             : 
     131             : }
     132             : }
     133             : #endif
     134             : 

Generated by: LCOV version 1.16