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: 2020-11-18 11:20:57 Functions: 7 9 77.8 %

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

Generated by: LCOV version 1.13