LCOV - code coverage report
Current view: top level - tools - Grid.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 10 11 90.9 %
Date: 2021-11-18 15:22:58 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2020 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_Grid_h
      23             : #define __PLUMED_tools_Grid_h
      24             : 
      25             : #include <vector>
      26             : #include <string>
      27             : #include <map>
      28             : #include <cmath>
      29             : #include <memory>
      30             : 
      31             : namespace PLMD {
      32             : 
      33             : 
      34             : // simple function to enable various weighting
      35             : 
      36             : class WeightBase {
      37             : public:
      38             :   virtual double projectInnerLoop(double &input, double &v)=0;
      39             :   virtual double projectOuterLoop(double &v)=0;
      40             :   virtual ~WeightBase() {}
      41             : };
      42             : 
      43           0 : class BiasWeight:public WeightBase {
      44             : public:
      45             :   double beta,invbeta;
      46           2 :   explicit BiasWeight(double v) {beta=v; invbeta=1./beta;}
      47       13120 :   double projectInnerLoop(double &input, double &v) {return  input+exp(beta*v);}
      48         164 :   double projectOuterLoop(double &v) {return -invbeta*std::log(v);}
      49             : };
      50             : 
      51             : class ProbWeight:public WeightBase {
      52             : public:
      53             :   double beta,invbeta;
      54             :   explicit ProbWeight(double v) {beta=v; invbeta=1./beta;}
      55             :   double projectInnerLoop(double &input, double &v) {return  input+v;}
      56             :   double projectOuterLoop(double &v) {return -invbeta*std::log(v);}
      57             : };
      58             : 
      59             : 
      60             : 
      61             : 
      62             : 
      63             : 
      64             : class Value;
      65             : class IFile;
      66             : class OFile;
      67             : class KernelFunctions;
      68             : class Communicator;
      69             : 
      70             : /// \ingroup TOOLBOX
      71         138 : class Grid
      72             : {
      73             : public:
      74             : // we use a size_t here
      75             : // should be 8 bytes on all 64-bit machines
      76             : // and more portable than "unsigned long long"
      77             :   typedef size_t index_t;
      78             : // to restore old implementation (unsigned) use the following instead:
      79             : // typedef unsigned index_t;
      80             : /// Maximum dimension (exaggerated value).
      81             : /// Can be used to replace local std::vectors with std::arrays (allocated on stack).
      82             :   static constexpr size_t maxdim=64;
      83             : private:
      84             :   double contour_location;
      85             :   std::vector<double> grid_;
      86             :   std::vector< std::vector<double> > der_;
      87             : protected:
      88             :   std::string funcname;
      89             :   std::vector<std::string> argnames;
      90             :   std::vector<std::string> str_min_, str_max_;
      91             :   std::vector<double> min_,max_,dx_;
      92             :   std::vector<unsigned> nbin_;
      93             :   std::vector<bool> pbc_;
      94             :   index_t maxsize_;
      95             :   unsigned dimension_;
      96             :   bool dospline_, usederiv_;
      97             :   std::string fmt_; // format for output
      98             : /// get "neighbors" for spline
      99             :   void getSplineNeighbors(const std::vector<unsigned> & indices, std::vector<index_t>& neigh, unsigned& nneigh )const;
     100             : // std::vector<index_t> getSplineNeighbors(const std::vector<unsigned> & indices)const;
     101             : 
     102             : 
     103             : public:
     104             : /// clear grid
     105             :   virtual void clear();
     106             : /// this constructor here is Value-aware
     107             :   Grid(const std::string& funcl, const std::vector<Value*> & args, const std::vector<std::string> & gmin,
     108             :        const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline,
     109             :        bool usederiv, bool doclear=true);
     110             : /// this constructor here is not Value-aware
     111             :   Grid(const std::string& funcl, const std::vector<std::string> &names, const std::vector<std::string> & gmin,
     112             :        const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline,
     113             :        bool usederiv, bool doclear, const std::vector<bool> &isperiodic, const std::vector<std::string> &pmin,
     114             :        const std::vector<std::string> &pmax );
     115             : /// this is the real initializator
     116             :   void Init(const std::string & funcl, const std::vector<std::string> &names, const std::vector<std::string> & gmin,
     117             :             const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline, bool usederiv,
     118             :             bool doclear, const std::vector<bool> &isperiodic, const std::vector<std::string> &pmin, const std::vector<std::string> &pmax);
     119             : /// get lower boundary
     120             :   std::vector<std::string> getMin() const;
     121             : /// get upper boundary
     122             :   std::vector<std::string> getMax() const;
     123             : /// get bin size
     124             :   std::vector<double> getDx() const;
     125             :   double getDx(index_t j) const ;
     126             : /// get bin volume
     127             :   double getBinVolume() const;
     128             : /// get number of bins
     129             :   std::vector<unsigned> getNbin() const;
     130             : /// get if periodic
     131             :   std::vector<bool> getIsPeriodic() const;
     132             : /// get grid dimension
     133             :   unsigned getDimension() const;
     134             : /// get argument names  of this grid
     135             :   std::vector<std::string> getArgNames() const;
     136             : /// get if the grid has derivatives
     137     1898210 :   bool hasDerivatives() const {return usederiv_;}
     138             : 
     139             : /// methods to handle grid indices
     140             :   void getIndices(index_t index, std::vector<unsigned>& rindex) const;
     141             :   void getIndices(const std::vector<double> & x, std::vector<unsigned>& rindex) const;
     142             :   std::vector<unsigned> getIndices(index_t index) const;
     143             :   std::vector<unsigned> getIndices(const std::vector<double> & x) const;
     144             :   index_t getIndex(const std::vector<unsigned> & indices) const;
     145             :   index_t getIndex(const std::vector<double> & x) const;
     146             :   std::vector<double> getPoint(index_t index) const;
     147             :   std::vector<double> getPoint(const std::vector<unsigned> & indices) const;
     148             :   std::vector<double> getPoint(const std::vector<double> & x) const;
     149             : /// faster versions relying on preallocated vectors
     150             :   void getPoint(index_t index,std::vector<double> & point) const;
     151             :   void getPoint(const std::vector<unsigned> & indices,std::vector<double> & point) const;
     152             :   void getPoint(const std::vector<double> & x,std::vector<double> & point) const;
     153             : 
     154             : /// get neighbors
     155             :   std::vector<index_t> getNeighbors(index_t index,const std::vector<unsigned> & neigh) const;
     156             :   std::vector<index_t> getNeighbors(const std::vector<unsigned> & indices,const std::vector<unsigned> & neigh) const;
     157             :   std::vector<index_t> getNeighbors(const std::vector<double> & x,const std::vector<unsigned> & neigh) const;
     158             : /// get nearest neighbors (those separated by exactly one lattice unit)
     159             :   std::vector<index_t> getNearestNeighbors(const index_t index) const;
     160             :   std::vector<index_t> getNearestNeighbors(const std::vector<unsigned> &indices) const;
     161             : 
     162             : /// write header for grid file
     163             :   void writeHeader(OFile& file);
     164             : 
     165             : /// read grid from file
     166             :   static std::unique_ptr<Grid> create(const std::string&,const std::vector<Value*>&,IFile&,bool,bool,bool);
     167             : /// read grid from file and check boundaries are what is expected from input
     168             :   static std::unique_ptr<Grid> create(const std::string&,const std::vector<Value*>&, IFile&,
     169             :                                       const std::vector<std::string>&,const std::vector<std::string>&,
     170             :                                       const std::vector<unsigned>&,bool,bool,bool);
     171             : /// get grid size
     172             :   virtual index_t getSize() const;
     173             : /// get grid value
     174             :   virtual double getValue(index_t index) const;
     175             :   virtual double getValue(const std::vector<unsigned> & indices) const;
     176             :   virtual double getValue(const std::vector<double> & x) const;
     177             : /// get minimum value
     178             :   virtual double getMinValue() const;
     179             : /// get maximum value
     180             :   virtual double getMaxValue() const;
     181             : /// get grid value and derivatives
     182             :   virtual double getValueAndDerivatives(index_t index, std::vector<double>& der) const ;
     183             :   virtual double getValueAndDerivatives(const std::vector<unsigned> & indices, std::vector<double>& der) const;
     184             :   virtual double getValueAndDerivatives(const std::vector<double> & x, std::vector<double>& der) const;
     185             : /// Get the difference from the contour
     186             :   double getDifferenceFromContour(const std::vector<double> & x, std::vector<double>& der) const ;
     187             : /// Find a set of points on a contour in the function
     188             :   void findSetOfPointsOnContour(const double& target, const std::vector<bool>& nosearch, unsigned& npoints, std::vector<std::vector<double> >& points );
     189             : 
     190             : /// set grid value
     191             :   virtual void setValue(index_t index, double value);
     192             :   virtual void setValue(const std::vector<unsigned> & indices, double value);
     193             : /// set grid value and derivatives
     194             :   virtual void setValueAndDerivatives(index_t index, double value, std::vector<double>& der);
     195             :   virtual void setValueAndDerivatives(const std::vector<unsigned> & indices, double value, std::vector<double>& der);
     196             : /// add to grid value
     197             :   virtual void addValue(index_t index, double value);
     198             :   virtual void addValue(const std::vector<unsigned> & indices, double value);
     199             : /// add to grid value and derivatives
     200             :   virtual void addValueAndDerivatives(index_t index, double value, std::vector<double>& der);
     201             :   virtual void addValueAndDerivatives(const std::vector<unsigned> & indices, double value, std::vector<double>& der);
     202             : /// Scale all grid values and derivatives by a constant factor
     203             :   virtual void scaleAllValuesAndDerivatives( const double& scalef );
     204             : /// Takes the scalef times the logarithm of all grid values and derivatives
     205             :   virtual void logAllValuesAndDerivatives( const double& scalef );
     206             : /// Set the minimum value of the grid to zero and translates accordingly
     207             :   virtual void setMinToZero();
     208             : /// apply function: takes  pointer to  function that accepts a double and apply
     209             :   virtual void applyFunctionAllValuesAndDerivatives( double (*func)(double val), double (*funcder)(double valder) );
     210             : /// add a kernel function to the grid
     211             :   void addKernel( const KernelFunctions& kernel );
     212             : 
     213             : /// dump grid on file
     214             :   virtual void writeToFile(OFile&);
     215             : /// dump grid to gaussian cube file
     216             :   void writeCubeFile(OFile&, const double& lunit);
     217             : 
     218        5962 :   virtual ~Grid() {}
     219             : 
     220             : /// project a high dimensional grid onto a low dimensional one: this should be changed at some time
     221             : /// to enable many types of weighting
     222             :   Grid project( const std::vector<std::string> & proj, WeightBase *ptr2obj  );
     223             :   void projectOnLowDimension(double &val, std::vector<int> &varHigh, WeightBase* ptr2obj );
     224             : /// set output format
     225         143 :   void setOutputFmt(const std::string & ss) {fmt_=ss;}
     226             : /// reset output format to the default %14.9f format
     227             :   void resetToDefaultOutputFmt() {fmt_="%14.9f";}
     228             : /// Integrate the function calculated on the grid
     229             :   double integrate( std::vector<unsigned>& npoints );
     230             : ///
     231             :   void mpiSumValuesAndDerivatives( Communicator& comm );
     232             : /// Find the maximum over paths of the minimum value of the gridded function along the paths
     233             : /// for all paths of neighboring grid lattice points from a source point to a sink point.
     234             :   virtual double findMaximalPathMinimum(const std::vector<double> &source, const std::vector<double> &sink);
     235             : };
     236             : 
     237             : 
     238             : class SparseGrid : public Grid
     239             : {
     240             : 
     241             :   std::map<index_t,double> map_;
     242             :   std::map< index_t,std::vector<double> > der_;
     243             : 
     244             : protected:
     245             :   void clear();
     246             : 
     247             : public:
     248             :   SparseGrid(const std::string& funcl, const std::vector<Value*> & args, const std::vector<std::string> & gmin,
     249             :              const std::vector<std::string> & gmax,
     250          12 :              const std::vector<unsigned> & nbin, bool dospline, bool usederiv):
     251          12 :     Grid(funcl,args,gmin,gmax,nbin,dospline,usederiv,false) {}
     252             : 
     253             :   index_t getSize() const;
     254             :   index_t getMaxSize() const;
     255             : 
     256             : /// this is to access to Grid:: version of these methods (allowing overloading of virtual methods)
     257             :   using Grid::getValue;
     258             :   using Grid::getValueAndDerivatives;
     259             :   using Grid::setValue;
     260             :   using Grid::setValueAndDerivatives;
     261             :   using Grid::addValue;
     262             :   using Grid::addValueAndDerivatives;
     263             : 
     264             : /// get grid value
     265             :   double getValue(index_t index) const;
     266             : /// get grid value and derivatives
     267             :   double getValueAndDerivatives(index_t index, std::vector<double>& der) const;
     268             : 
     269             : /// set grid value
     270             :   void setValue(index_t index, double value);
     271             : /// set grid value and derivatives
     272             :   void setValueAndDerivatives(index_t index, double value, std::vector<double>& der);
     273             : /// add to grid value
     274             :   void addValue(index_t index, double value);
     275             : /// add to grid value and derivatives
     276             :   void addValueAndDerivatives(index_t index, double value, std::vector<double>& der);
     277             : 
     278             : /// dump grid on file
     279             :   void writeToFile(OFile&);
     280             : 
     281          36 :   virtual ~SparseGrid() {}
     282             : 
     283             :   virtual double getMaxValue() const;
     284             :   virtual double getMinValue() const;
     285             : 
     286             : };
     287             : }
     288             : 
     289             : #endif

Generated by: LCOV version 1.14