VERB4D
MonotCubicInterpolator.hpp
Go to the documentation of this file.
1 /* -*-C++-*- */
2 
3 #ifndef _MONOTCUBICINTERPOLATOR_H
4 #define _MONOTCUBICINTERPOLATOR_H
5 
6 #include <vector>
7 #include <map>
8 #include <string>
9 
10 /*
11  MonotCubicInterpolator
12  Copyright (C) 2006 Statoil ASA
13 
14  This program is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License
16  as published by the Free Software Foundation; either version 2
17  of the License, or (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 */
28 
29 
34 namespace Opm
35 {
36 
70  public:
71 
81  MonotCubicInterpolator(const std::string & datafilename) throw (const char*)
82  {
83  if (!read(datafilename)) {
84  throw("Unable to constuct MonotCubicInterpolator from file.") ;
85  } ;
86  }
87 
88 
101  MonotCubicInterpolator(const char* datafilename) throw (const char*)
102  {
103  if (!read(std::string(datafilename))) {
104  throw("Unable to constuct MonotCubicInterpolator from file.") ;
105  } ;
106  }
107 
108 
117  MonotCubicInterpolator(const char* datafilename, int xColumn, int fColumn) throw (const char*)
118  {
119  if (!read(std::string(datafilename),xColumn,fColumn)) {
120  throw("Unable to constuct MonotCubicInterpolator from file.") ;
121  } ;
122  }
123 
132  MonotCubicInterpolator(const std::string & datafilename, int xColumn, int fColumn) throw (const char*)
133  {
134  if (!read(datafilename,xColumn,fColumn)) {
135  throw("Unable to constuct MonotCubicInterpolator from file.") ;
136  } ;
137  }
138 
147  MonotCubicInterpolator(const std::vector<double> & x ,
148  const std::vector<double> & f);
149 
157 
158 
159 
173  bool read(const std::string & datafilename) {
174  return read(datafilename,1,2) ;
175  }
176 
185  bool read(const std::string & datafilename, int xColumn, int fColumn) ;
186 
187 
188 
200  double operator () (double x) const { return evaluate(x) ; }
201 
213  double evaluate(double x) const throw(const char*);
214 
234  double evaluate(double x, double & errorestimate_output ) const ;
235 
242  std::pair<double,double> getMinimumX() const {
243  // Easy since the data is sorted on x:
244  return *data.begin();
245  }
246 
253  std::pair<double,double> getMaximumX() const {
254  // Easy since the data is sorted on x:
255  return *data.rbegin();
256  }
257 
264  std::pair<double,double> getMaximumF() const throw(const char*) ;
265 
272  std::pair<double,double> getMinimumF() const throw(const char*) ;
273 
274 
282  std::vector<double> get_xVector() const ;
283 
292  std::vector<double> get_fVector() const ;
293 
299  void scaleData(double factor);
300 
309 
310  /* Use cached value if it can be trusted */
311  if (strictlyMonotoneCached) {
312  return strictlyMonotone;
313  }
314  else {
315  computeInternalFunctionData();
316  return strictlyMonotone;
317  }
318  }
319 
325  bool isMonotone() const {
326  if (monotoneCached) {
327  return monotone;
328  }
329  else {
330  computeInternalFunctionData();
331  return monotone;
332  }
333  }
342 
343  /* Use cached value if it can be trusted */
344  if (strictlyMonotoneCached) {
345  return (strictlyMonotone && strictlyIncreasing);
346  }
347  else {
348  computeInternalFunctionData();
349  return (strictlyMonotone && strictlyIncreasing);
350  }
351  }
352 
358  bool isMonotoneIncreasing() const {
359  if (monotoneCached) {
360  return (monotone && increasing);
361  }
362  else {
363  computeInternalFunctionData();
364  return (monotone && increasing);
365  }
366  }
375 
376  /* Use cached value if it can be trusted */
377  if (strictlyMonotoneCached) {
378  return (strictlyMonotone && strictlyDecreasing);
379  }
380  else {
381  computeInternalFunctionData();
382  return (strictlyMonotone && strictlyDecreasing);
383  }
384  }
385 
391  bool isMonotoneDecreasing() const {
392  if (monotoneCached) {
393  return (monotone && decreasing);
394  }
395  else {
396  computeInternalFunctionData();
397  return (monotone && decreasing);
398  }
399  }
400 
401 
402 
416  void addPair(double newx, double newf) throw(const char*);
417 
430  std::pair<double,double> getMissingX() const throw(const char*) ;
431 
437  std::string toString() const;
438 
442  int getSize() const {
443  return data.size();
444  }
445 
465  void chopFlatEndpoints(const double);
466 
472  chopFlatEndpoints(1e-14);
473  }
474 
489  void shrinkFlatAreas(const double);
490 
496  shrinkFlatAreas(1e-14);
497  }
498 
499 
500 
501 private:
502 
503 
504 
506  std::map<double, double> data;
507 
509  mutable std::map<double, double> ddata;
510 
511 
512  // Storage containers for precomputed interpolation data
513  // std::vector<double> dvalues; // derivatives in Hermite interpolation.
514 
515  mutable bool strictlyMonotoneCached;
516  mutable bool monotoneCached;
517 
518  mutable bool strictlyMonotone;
519  mutable bool monotone;
520 
521  // if strictlyMonotone is true (and can be trusted), the two next are meaningful
522 
523  mutable bool strictlyDecreasing;
524  mutable bool strictlyIncreasing;
525  mutable bool decreasing;
526  mutable bool increasing;
527 
528 
529  /* Hermite basis functions, t \in [0,1] ,
530  notation from:
531  http://en.wikipedia.org/w/index.php?title=Cubic_Hermite_spline&oldid=84495502
532  */
533 
534 
536  double H00(double t) const {
537  return 2*t*t*t - 3*t*t + 1;
538  }
540  double H10(double t) const {
541  return t*t*t - 2*t*t + t;
542  }
544  double H01(double t) const {
545  return -2*t*t*t + 3*t*t;
546  }
548  double H11(double t) const {
549  return t*t*t - t*t;
550  }
551 
552 
563  void computeInternalFunctionData() const ;
564 
572  void computeSimpleDerivatives() const ;
573 
574 
581  void adjustDerivativesForMonotoneness() const ;
582 
592  bool isMonotoneCoeff(double alpha, double beta) const {
593  if ((alpha*alpha + beta*beta) <= 9) {
594  return true;
595  } else {
596  return false;
597  }
598  }
599 
600 };
601 
602 
603 } // namespace Opm
604 
605 #endif
std::pair< double, double > getMinimumX() const
Definition: MonotCubicInterpolator.hpp:242
void addPair(double newx, double newf)
Definition: MonotCubicInterpolator.cpp:174
double operator()(double x) const
Definition: MonotCubicInterpolator.hpp:200
double evaluate(double x) const
Definition: MonotCubicInterpolator.cpp:189
void shrinkFlatAreas()
Definition: MonotCubicInterpolator.hpp:495
bool isMonotone() const
Definition: MonotCubicInterpolator.hpp:325
bool isStrictlyIncreasing()
Definition: MonotCubicInterpolator.hpp:341
std::vector< double > get_xVector() const
Definition: MonotCubicInterpolator.cpp:251
Namespace included for using the monotone cubic interpolator.
Definition: MonotCubicInterpolator.cpp:101
MonotCubicInterpolator(const char *datafilename, int xColumn, int fColumn)
Definition: MonotCubicInterpolator.hpp:117
bool read(const std::string &datafilename)
Definition: MonotCubicInterpolator.hpp:173
bool isMonotoneIncreasing() const
Definition: MonotCubicInterpolator.hpp:358
void scaleData(double factor)
Definition: MonotCubicInterpolator.cpp:723
MonotCubicInterpolator(const std::string &datafilename)
Definition: MonotCubicInterpolator.hpp:81
int getSize() const
Definition: MonotCubicInterpolator.hpp:442
bool isMonotoneDecreasing() const
Definition: MonotCubicInterpolator.hpp:391
std::pair< double, double > getMaximumX() const
Definition: MonotCubicInterpolator.hpp:253
bool isStrictlyDecreasing()
Definition: MonotCubicInterpolator.hpp:374
MonotCubicInterpolator()
Definition: MonotCubicInterpolator.hpp:156
std::vector< double > get_fVector() const
Definition: MonotCubicInterpolator.cpp:266
Represents one dimensional function f with single valued argument x that can be interpolated using mo...
Definition: MonotCubicInterpolator.hpp:69
std::pair< double, double > getMinimumF() const
Definition: MonotCubicInterpolator.cpp:368
MonotCubicInterpolator(const char *datafilename)
Definition: MonotCubicInterpolator.hpp:101
MonotCubicInterpolator(const std::string &datafilename, int xColumn, int fColumn)
Definition: MonotCubicInterpolator.hpp:132
std::pair< double, double > getMaximumF() const
Definition: MonotCubicInterpolator.cpp:345
void chopFlatEndpoints()
Definition: MonotCubicInterpolator.hpp:471
bool isStrictlyMonotone()
Definition: MonotCubicInterpolator.hpp:308
std::string toString() const
Definition: MonotCubicInterpolator.cpp:283
std::pair< double, double > getMissingX() const
Definition: MonotCubicInterpolator.cpp:311