Loading...
Searching...
No Matches
MonotCubicInterpolator.hpp
/* -*-C++-*- */
#ifndef _MONOTCUBICINTERPOLATOR_H
#define _MONOTCUBICINTERPOLATOR_H
#include <vector>
#include <map>
#include <string>
/*
MonotCubicInterpolator
Copyright (C) 2006 Statoil ASA
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace Opm
{
class MonotCubicInterpolator {
public:
explicit MonotCubicInterpolator(const std::string & datafilename)
{
if (!read(datafilename)) {
throw("Unable to constuct MonotCubicInterpolator from file.") ;
} ;
}
explicit MonotCubicInterpolator(const char* datafilename)
{
if (!read(std::string(datafilename))) {
throw("Unable to constuct MonotCubicInterpolator from file.") ;
} ;
}
MonotCubicInterpolator(const char* datafilename, int xColumn, int fColumn)
{
if (!read(std::string(datafilename),xColumn,fColumn)) {
throw("Unable to constuct MonotCubicInterpolator from file.") ;
} ;
}
MonotCubicInterpolator(const std::string & datafilename, int xColumn, int fColumn)
{
if (!read(datafilename,xColumn,fColumn)) {
throw("Unable to constuct MonotCubicInterpolator from file.") ;
} ;
}
MonotCubicInterpolator(const std::vector<double> & x ,
const std::vector<double> & f);
MonotCubicInterpolator() { }
bool read(const std::string & datafilename) {
return read(datafilename,1,2) ;
}
bool read(const std::string & datafilename, int xColumn, int fColumn) ;
double operator () (double x) const { return evaluate(x) ; }
double evaluate(double x) const;
double evaluate(double x, double & errorestimate_output ) const ;
std::pair<double,double> getMinimumX() const {
// Easy since the data is sorted on x:
return *data.begin();
}
std::pair<double,double> getMaximumX() const {
// Easy since the data is sorted on x:
return *data.rbegin();
}
std::pair<double,double> getMaximumF() const ;
std::pair<double,double> getMinimumF() const ;
std::vector<double> get_xVector() const ;
std::vector<double> get_fVector() const ;
void scaleData(double factor);
bool isStrictlyMonotone() {
/* Use cached value if it can be trusted */
if (strictlyMonotoneCached) {
return strictlyMonotone;
}
else {
computeInternalFunctionData();
return strictlyMonotone;
}
}
bool isMonotone() const {
if (monotoneCached) {
return monotone;
}
else {
computeInternalFunctionData();
return monotone;
}
}
bool isStrictlyIncreasing() {
/* Use cached value if it can be trusted */
if (strictlyMonotoneCached) {
return (strictlyMonotone && strictlyIncreasing);
}
else {
computeInternalFunctionData();
return (strictlyMonotone && strictlyIncreasing);
}
}
bool isMonotoneIncreasing() const {
if (monotoneCached) {
return (monotone && increasing);
}
else {
computeInternalFunctionData();
return (monotone && increasing);
}
}
bool isStrictlyDecreasing() {
/* Use cached value if it can be trusted */
if (strictlyMonotoneCached) {
return (strictlyMonotone && strictlyDecreasing);
}
else {
computeInternalFunctionData();
return (strictlyMonotone && strictlyDecreasing);
}
}
bool isMonotoneDecreasing() const {
if (monotoneCached) {
return (monotone && decreasing);
}
else {
computeInternalFunctionData();
return (monotone && decreasing);
}
}
void addPair(double newx, double newf);
std::pair<double,double> getMissingX() const;
std::string toString() const;
int getSize() const {
return data.size();
}
void chopFlatEndpoints(const double);
void chopFlatEndpoints() {
chopFlatEndpoints(1e-14);
}
void shrinkFlatAreas(const double);
void shrinkFlatAreas() {
shrinkFlatAreas(1e-14);
}
private:
// Data structure to store x- and f-values
std::map<double, double> data{};
// Data structure to store x- and d-values
mutable std::map<double, double> ddata{};
// Storage containers for precomputed interpolation data
// std::vector<double> dvalues; // derivatives in Hermite interpolation.
// Flag to determine whether the boolean strictlyMonotone can be
// trusted.
mutable bool strictlyMonotoneCached{false};
mutable bool monotoneCached{false}; /* only monotone, not stricly montone */
mutable bool strictlyMonotone{false};
mutable bool monotone{false};
// if strictlyMonotone is true (and can be trusted), the two next are meaningful
mutable bool strictlyDecreasing{false};
mutable bool strictlyIncreasing{false};
mutable bool decreasing{false};
mutable bool increasing{false};
/* Hermite basis functions, t \in [0,1] ,
notation from:
http://en.wikipedia.org/w/index.php?title=Cubic_Hermite_spline&oldid=84495502
*/
double H00(double t) const {
return 2*t*t*t - 3*t*t + 1;
}
double H10(double t) const {
return t*t*t - 2*t*t + t;
}
double H01(double t) const {
return -2*t*t*t + 3*t*t;
}
double H11(double t) const {
return t*t*t - t*t;
}
void computeInternalFunctionData() const ;
void computeSimpleDerivatives() const ;
void adjustDerivativesForMonotoneness() const ;
bool isMonotoneCoeff(double alpha, double beta) const {
if ((alpha*alpha + beta*beta) <= 9) {
return true;
} else {
return false;
}
}
};
} // namespace Opm
#endif
Opm::MonotCubicInterpolator::evaluate
double evaluate(double x, double &errorestimate_output) const
Opm::MonotCubicInterpolator::isStrictlyDecreasing
bool isStrictlyDecreasing()
Determines if the current function-value-data is strictly decreasing.
Definition MonotCubicInterpolator.hpp:368
Opm::MonotCubicInterpolator::read
bool read(const std::string &datafilename)
Definition MonotCubicInterpolator.hpp:167
Opm::MonotCubicInterpolator::toString
std::string toString() const
Constructs a string containing the data in a table.
Definition MonotCubicInterpolator.cpp:271
Opm::MonotCubicInterpolator::get_fVector
std::vector< double > get_fVector() const
Provide a copy of tghe function data as a vector.
Definition MonotCubicInterpolator.cpp:258
Opm::MonotCubicInterpolator::getMinimumX
std::pair< double, double > getMinimumX() const
Minimum x-value, returns both x and f in a pair.
Definition MonotCubicInterpolator.hpp:236
Opm::MonotCubicInterpolator::getMaximumX
std::pair< double, double > getMaximumX() const
Maximum x-value, returns both x and f in a pair.
Definition MonotCubicInterpolator.hpp:247
Opm::MonotCubicInterpolator::isStrictlyMonotone
bool isStrictlyMonotone()
Determines if the current function-value-data is strictly monotone.
Definition MonotCubicInterpolator.hpp:302
Opm::MonotCubicInterpolator::get_xVector
std::vector< double > get_xVector() const
Provide a copy of the x-data as a vector.
Definition MonotCubicInterpolator.cpp:246
Opm::MonotCubicInterpolator::getSize
int getSize() const
Definition MonotCubicInterpolator.hpp:436
Opm::MonotCubicInterpolator::addPair
void addPair(double newx, double newf)
Definition MonotCubicInterpolator.cpp:169
Opm::MonotCubicInterpolator::getMissingX
std::pair< double, double > getMissingX() const
Returns an x-value that is believed to yield the best improvement in global accuracy for the interpol...
Definition MonotCubicInterpolator.cpp:298
Opm::MonotCubicInterpolator::MonotCubicInterpolator
MonotCubicInterpolator(const std::string &datafilename)
Definition MonotCubicInterpolator.hpp:74
Opm::MonotCubicInterpolator::isMonotoneDecreasing
bool isMonotoneDecreasing() const
Determines if the current function-value-data is monotone and decreasing.
Definition MonotCubicInterpolator.hpp:385
Opm::MonotCubicInterpolator::getMaximumF
std::pair< double, double > getMaximumF() const
Maximum f-value, returns both x and f in a pair.
Definition MonotCubicInterpolator.cpp:332
Opm::MonotCubicInterpolator::MonotCubicInterpolator
MonotCubicInterpolator(const char *datafilename, int xColumn, int fColumn)
Definition MonotCubicInterpolator.hpp:111
Opm::MonotCubicInterpolator::MonotCubicInterpolator
MonotCubicInterpolator()
No input, an empty function object is created.
Definition MonotCubicInterpolator.hpp:150
Opm::MonotCubicInterpolator::MonotCubicInterpolator
MonotCubicInterpolator(const char *datafilename)
Definition MonotCubicInterpolator.hpp:95
Opm::MonotCubicInterpolator::shrinkFlatAreas
void shrinkFlatAreas()
Wrapper function for shrinkFlatAreas(const double) providing a default epsilon parameter.
Definition MonotCubicInterpolator.hpp:489
Opm::MonotCubicInterpolator::isMonotoneIncreasing
bool isMonotoneIncreasing() const
Determines if the current function-value-data is monotone and increasing.
Definition MonotCubicInterpolator.hpp:352
Opm::MonotCubicInterpolator::scaleData
void scaleData(double factor)
Definition MonotCubicInterpolator.cpp:709
Opm::MonotCubicInterpolator::isStrictlyIncreasing
bool isStrictlyIncreasing()
Determines if the current function-value-data is strictly increasing.
Definition MonotCubicInterpolator.hpp:335
Opm::MonotCubicInterpolator::operator()
double operator()(double x) const
Definition MonotCubicInterpolator.hpp:194
Opm::MonotCubicInterpolator::evaluate
double evaluate(double x) const
Definition MonotCubicInterpolator.cpp:184
Opm::MonotCubicInterpolator::getMinimumF
std::pair< double, double > getMinimumF() const
Minimum f-value, returns both x and f in a pair.
Definition MonotCubicInterpolator.cpp:355
Opm::MonotCubicInterpolator::MonotCubicInterpolator
MonotCubicInterpolator(const std::string &datafilename, int xColumn, int fColumn)
Definition MonotCubicInterpolator.hpp:126
Opm::MonotCubicInterpolator::isMonotone
bool isMonotone() const
Determines if the current function-value-data is monotone.
Definition MonotCubicInterpolator.hpp:319
Opm::MonotCubicInterpolator::chopFlatEndpoints
void chopFlatEndpoints()
Wrapper function for chopFlatEndpoints(const double) providing a default epsilon parameter.
Definition MonotCubicInterpolator.hpp:465
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30