All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Grid.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 class BiasWeight:public WeightBase{
43  public:
44  double beta,invbeta;
45  BiasWeight(double v){beta=v;invbeta=1./beta;}
46  double projectInnerLoop(double &input, double &v){return input+exp(beta*v);}
47  double projectOuterLoop(double &v){return -invbeta*std::log(v);}
48 };
49 
50 class ProbWeight:public WeightBase{
51  public:
52  double beta,invbeta;
53  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 
68 /// \ingroup TOOLBOX
69 class Grid
70 {
71  std::vector<double> grid_;
72  std::vector< std::vector<double> > der_;
73 protected:
74  std::string funcname;
75  std::vector<std::string> argnames;
76  std::vector<std::string> str_min_, str_max_;
77  std::vector<double> min_,max_,dx_;
78  std::vector<unsigned> nbin_;
79  std::vector<bool> pbc_;
80  unsigned maxsize_, dimension_;
82  std::string fmt_; // format for output
83  /// get "neighbors" for spline
84  std::vector<unsigned> getSplineNeighbors(const std::vector<unsigned> & indices)const;
85 
86 
87 public:
88  /// clear grid
89  virtual void clear();
90  /// this constructor here is Value-aware
91  Grid(const std::string& funcl, std::vector<Value*> args, const std::vector<std::string> & gmin,
92  const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline,
93  bool usederiv, bool doclear=true);
94  /// this constructor here is not Value-aware
95  Grid(const std::string& funcl, const std::vector<std::string> &names, const std::vector<std::string> & gmin,
96  const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline,
97  bool usederiv, bool doclear, const std::vector<bool> &isperiodic, const std::vector<std::string> &pmin,
98  const std::vector<std::string> &pmax );
99  /// this is the real initializator
100  void Init(const std::string & funcl, const std::vector<std::string> &names, const std::vector<std::string> & gmin,
101  const std::vector<std::string> & gmax, const std::vector<unsigned> & nbin, bool dospline, bool usederiv,
102  bool doclear, const std::vector<bool> &isperiodic, const std::vector<std::string> &pmin, const std::vector<std::string> &pmax);
103 /// get lower boundary
104  std::vector<std::string> getMin() const;
105 /// get upper boundary
106  std::vector<std::string> getMax() const;
107 /// get bin size
108  std::vector<double> getDx() const;
109 /// get bin volume
110  double getBinVolume() const;
111 /// get number of bins
112  std::vector<unsigned> getNbin() const;
113 /// get if periodic
114  std::vector<bool> getIsPeriodic() const;
115 /// get grid dimension
116  unsigned getDimension() const;
117 /// get argument names of this grid
118  std::vector<std::string> getArgNames() const;
119 
120 /// methods to handle grid indices
121  std::vector<unsigned> getIndices(unsigned index) const;
122  std::vector<unsigned> getIndices(const std::vector<double> & x) const;
123  unsigned getIndex(const std::vector<unsigned> & indices) const;
124  unsigned getIndex(const std::vector<double> & x) const;
125  std::vector<double> getPoint(unsigned index) const;
126  std::vector<double> getPoint(const std::vector<unsigned> & indices) const;
127  std::vector<double> getPoint(const std::vector<double> & x) const;
128 /// faster versions relying on preallocated vectors
129  void getPoint(unsigned index,std::vector<double> & point) const;
130  void getPoint(const std::vector<unsigned> & indices,std::vector<double> & point) const;
131  void getPoint(const std::vector<double> & x,std::vector<double> & point) const;
132 
133 /// get neighbors
134  std::vector<unsigned> getNeighbors(unsigned index,const std::vector<unsigned> & neigh) const;
135  std::vector<unsigned> getNeighbors(const std::vector<unsigned> & indices,const std::vector<unsigned> & neigh) const;
136  std::vector<unsigned> getNeighbors(const std::vector<double> & x,const std::vector<unsigned> & neigh) const;
137 
138 /// write header for grid file
139  void writeHeader(OFile& file);
140 
141 /// read grid from file
142  static Grid* create(const std::string&,std::vector<Value*>,IFile&,bool,bool,bool);
143 /// read grid from file and check boundaries are what is expected from input
144  static Grid* create(const std::string&, std::vector<Value*>, IFile&,
145  const std::vector<std::string>&,const std::vector<std::string>&,
146  const std::vector<unsigned>&,bool,bool,bool);
147 /// get grid size
148  virtual unsigned getSize() const;
149 /// get grid value
150  virtual double getValue(unsigned index) const;
151  virtual double getValue(const std::vector<unsigned> & indices) const;
152  virtual double getValue(const std::vector<double> & x) const;
153 /// get minimum value
154  virtual double getMinValue() const;
155 /// get maximum value
156  virtual double getMaxValue() const;
157 /// get grid value and derivatives
158  virtual double getValueAndDerivatives(unsigned index, std::vector<double>& der) const ;
159  virtual double getValueAndDerivatives(const std::vector<unsigned> & indices, std::vector<double>& der) const;
160  virtual double getValueAndDerivatives(const std::vector<double> & x, std::vector<double>& der) const;
161 
162 /// set grid value
163  virtual void setValue(unsigned index, double value);
164  virtual void setValue(const std::vector<unsigned> & indices, double value);
165 /// set grid value and derivatives
166  virtual void setValueAndDerivatives(unsigned index, double value, std::vector<double>& der);
167  virtual void setValueAndDerivatives(const std::vector<unsigned> & indices, double value, std::vector<double>& der);
168 /// add to grid value
169  virtual void addValue(unsigned index, double value);
170  virtual void addValue(const std::vector<unsigned> & indices, double value);
171 /// add to grid value and derivatives
172  virtual void addValueAndDerivatives(unsigned index, double value, std::vector<double>& der);
173  virtual void addValueAndDerivatives(const std::vector<unsigned> & indices, double value, std::vector<double>& der);
174 /// Scale all grid values and derivatives by a constant factor
175  virtual void scaleAllValuesAndDerivatives( const double& scalef );
176 /// apply function: takes pointer to function that accepts a double and apply
177  virtual void applyFunctionAllValuesAndDerivatives( double (*func)(double val), double (*funcder)(double valder) );
178 /// add a kernel function to the grid
179  void addKernel( const KernelFunctions& kernel );
180 
181 /// dump grid on file
182  virtual void writeToFile(OFile&);
183 
184  virtual ~Grid(){}
185 
186 /// project a high dimensional grid onto a low dimensional one: this should be changed at some time
187 /// to enable many types of weighting
188  Grid project( const std::vector<std::string> & proj , WeightBase *ptr2obj );
189  void projectOnLowDimension(double &val , std::vector<int> &varHigh, WeightBase* ptr2obj );
190 /// set output format
191  void setOutputFmt(std::string ss){fmt_=ss;}
192 };
193 
194 
195 class SparseGrid : public Grid
196 {
197 
198  std::map<unsigned,double> map_;
199  typedef std::map<unsigned,double>::const_iterator iterator;
200  std::map< unsigned,std::vector<double> > der_;
201  typedef std::map<unsigned,std::vector<double> >::const_iterator iterator_der;
202 
203  protected:
204  void clear();
205 
206  public:
207  SparseGrid(const std::string& funcl, std::vector<Value*> args, const std::vector<std::string> & gmin,
208  const std::vector<std::string> & gmax,
209  const std::vector<unsigned> & nbin, bool dospline, bool usederiv):
210  Grid(funcl,args,gmin,gmax,nbin,dospline,usederiv,false){}
211 
212  unsigned getSize() const;
213  unsigned getMaxSize() const;
214 
215 /// this is to access to Grid:: version of these methods (allowing overloading of virtual methods)
216  using Grid::getValue;
218  using Grid::setValue;
220  using Grid::addValue;
222 
223  /// get grid value
224  double getValue(unsigned index) const;
225 /// get grid value and derivatives
226  double getValueAndDerivatives(unsigned index, std::vector<double>& der) const;
227 
228 /// set grid value
229  void setValue(unsigned index, double value);
230 /// set grid value and derivatives
231  void setValueAndDerivatives(unsigned index, double value, std::vector<double>& der);
232 /// add to grid value
233  void addValue(unsigned index, double value);
234 /// add to grid value and derivatives
235  void addValueAndDerivatives(unsigned index, double value, std::vector<double>& der);
236 
237 /// dump grid on file
238  void writeToFile(OFile&);
239 
240  virtual ~SparseGrid(){}
241 };
242 }
243 
244 #endif
virtual double projectInnerLoop(double &input, double &v)=0
virtual ~WeightBase()
Definition: Grid.h:39
double getValueAndDerivatives(unsigned index, std::vector< double > &der) const
get grid value and derivatives
Definition: Grid.cpp:654
unsigned maxsize_
Definition: Grid.h:80
Grid project(const std::vector< std::string > &proj, WeightBase *ptr2obj)
project a high dimensional grid onto a low dimensional one: this should be changed at some time to en...
Definition: Grid.cpp:751
unsigned getMaxSize() const
Definition: Grid.cpp:641
std::map< unsigned, std::vector< double > > der_
Definition: Grid.h:200
void writeToFile(OFile &)
dump grid on file
Definition: Grid.cpp:690
void addValue(unsigned index, double value)
add to grid value
Definition: Grid.cpp:677
std::vector< double > dx_
Definition: Grid.h:77
double projectOuterLoop(double &v)
Definition: Grid.h:47
double getValue(unsigned index) const
get grid value
Definition: Grid.cpp:645
virtual void clear()
clear grid
Definition: Grid.cpp:116
Grid(const std::string &funcl, std::vector< Value * > args, const std::vector< std::string > &gmin, const std::vector< std::string > &gmax, const std::vector< unsigned > &nbin, bool dospline, bool usederiv, bool doclear=true)
this constructor here is Value-aware
Definition: Grid.cpp:39
virtual double getValueAndDerivatives(unsigned index, std::vector< double > &der) const
get grid value and derivatives
Definition: Grid.cpp:395
unsigned getDimension() const
get grid dimension
Definition: Grid.cpp:163
std::vector< unsigned > nbin_
Definition: Grid.h:78
double projectOuterLoop(double &v)
Definition: Grid.h:55
virtual void addValue(unsigned index, double value)
add to grid value
Definition: Grid.cpp:479
std::map< unsigned, std::vector< double > >::const_iterator iterator_der
Definition: Grid.h:201
std::vector< bool > pbc_
Definition: Grid.h:79
double invbeta
Definition: Grid.h:52
double invbeta
Definition: Grid.h:44
void addValueAndDerivatives(unsigned index, double value, std::vector< double > &der)
add to grid value and derivatives
Definition: Grid.cpp:683
std::vector< unsigned > getIndices(unsigned index) const
methods to handle grid indices
Definition: Grid.cpp:190
virtual void setValue(unsigned index, double value)
set grid value
Definition: Grid.cpp:458
BiasWeight(double v)
Definition: Grid.h:45
std::string fmt_
Definition: Grid.h:82
std::string funcname
Definition: Grid.h:74
std::map< unsigned, double >::const_iterator iterator
Definition: Grid.h:199
std::vector< double > grid_
Definition: Grid.h:71
std::vector< double > getDx() const
get bin size
Definition: Grid.cpp:136
virtual double getValue(unsigned index) const
get grid value
Definition: Grid.cpp:355
ProbWeight(double v)
Definition: Grid.h:53
virtual void addValueAndDerivatives(unsigned index, double value, std::vector< double > &der)
add to grid value and derivatives
Definition: Grid.cpp:489
virtual void applyFunctionAllValuesAndDerivatives(double(*func)(double val), double(*funcder)(double valder))
apply function: takes pointer to function that accepts a double and apply
Definition: Grid.cpp:511
void addKernel(const KernelFunctions &kernel)
add a kernel function to the grid
Definition: Grid.cpp:325
std::vector< unsigned > getSplineNeighbors(const std::vector< unsigned > &indices) const
get "neighbors" for spline
Definition: Grid.cpp:305
std::vector< std::string > str_min_
Definition: Grid.h:76
double beta
Definition: Grid.h:44
unsigned dimension_
Definition: Grid.h:80
virtual double getMinValue() const
get minimum value
Definition: Grid.cpp:360
std::vector< std::string > getArgNames() const
get argument names of this grid
Definition: Grid.cpp:154
virtual unsigned getSize() const
get grid size
Definition: Grid.cpp:159
Class for input files.
Definition: IFile.h:40
std::vector< std::vector< double > > der_
Definition: Grid.h:72
void projectOnLowDimension(double &val, std::vector< int > &varHigh, WeightBase *ptr2obj)
Definition: Grid.cpp:717
double projectInnerLoop(double &input, double &v)
Definition: Grid.h:46
virtual void writeToFile(OFile &)
dump grid on file
Definition: Grid.cpp:531
virtual ~SparseGrid()
Definition: Grid.h:240
void setValueAndDerivatives(unsigned index, double value, std::vector< double > &der)
set grid value and derivatives
Definition: Grid.cpp:671
double getBinVolume() const
get bin volume
Definition: Grid.cpp:140
Class for output files.
Definition: OFile.h:84
virtual void scaleAllValuesAndDerivatives(const double &scalef)
Scale all grid values and derivatives by a constant factor.
Definition: Grid.cpp:500
std::vector< std::string > getMax() const
get upper boundary
Definition: Grid.cpp:132
virtual void setValueAndDerivatives(unsigned index, double value, std::vector< double > &der)
set grid value and derivatives
Definition: Grid.cpp:468
bool usederiv_
Definition: Grid.h:81
double beta
Definition: Grid.h:52
unsigned getSize() const
get grid size
Definition: Grid.cpp:637
virtual ~Grid()
Definition: Grid.h:184
unsigned getIndex(const std::vector< unsigned > &indices) const
Definition: Grid.cpp:168
virtual double projectOuterLoop(double &v)=0
std::vector< std::string > str_max_
Definition: Grid.h:76
std::vector< std::string > getMin() const
get lower boundary
Definition: Grid.cpp:128
std::vector< unsigned > getNbin() const
get number of bins
Definition: Grid.cpp:150
void Init(const std::string &funcl, const std::vector< std::string > &names, const std::vector< std::string > &gmin, const std::vector< std::string > &gmax, const std::vector< unsigned > &nbin, bool dospline, bool usederiv, bool doclear, const std::vector< bool > &isperiodic, const std::vector< std::string > &pmin, const std::vector< std::string > &pmax)
this is the real initializator
Definition: Grid.cpp:74
SparseGrid(const std::string &funcl, std::vector< Value * > args, const std::vector< std::string > &gmin, const std::vector< std::string > &gmax, const std::vector< unsigned > &nbin, bool dospline, bool usederiv)
Definition: Grid.h:207
std::vector< double > getPoint(unsigned index) const
Definition: Grid.cpp:222
virtual double getMaxValue() const
get maximum value
Definition: Grid.cpp:369
std::vector< double > max_
Definition: Grid.h:77
void setOutputFmt(std::string ss)
set output format
Definition: Grid.h:191
bool dospline_
Definition: Grid.h:81
void clear()
clear grid
Definition: Grid.cpp:633
void writeHeader(OFile &file)
write header for grid file
Definition: Grid.cpp:522
void setValue(unsigned index, double value)
set grid value
Definition: Grid.cpp:665
std::vector< unsigned > getNeighbors(unsigned index, const std::vector< unsigned > &neigh) const
get neighbors
Definition: Grid.cpp:300
std::vector< std::string > argnames
Definition: Grid.h:75
std::map< unsigned, double > map_
Definition: Grid.h:198
double projectInnerLoop(double &input, double &v)
Definition: Grid.h:54
static Grid * create(const std::string &, std::vector< Value * >, IFile &, bool, bool, bool)
read grid from file
Definition: Grid.cpp:573
std::vector< double > min_
Definition: Grid.h:77
std::vector< bool > getIsPeriodic() const
get if periodic
Definition: Grid.cpp:146