CoolProp  6.6.0
An open-source fluid property and humid air property database
Solvers.h
Go to the documentation of this file.
1 #ifndef SOLVERS_H
2 #define SOLVERS_H
3 
4 #include <vector>
5 #include <string>
6 #include "Exceptions.h"
7 #include "CoolPropTools.h"
8 
9 namespace CoolProp {
10 
11 // *****************************************************************************
12 // *****************************************************************************
13 // SOLVER WRAPPER CLASSES
14 // *****************************************************************************
15 // *****************************************************************************
16 
18 {
19  public:
20  int errcode;
21  std::string errstring;
23  int iter;
25  virtual ~FuncWrapper1D(){};
26  virtual double call(double) = 0;
32  virtual bool input_not_in_range(double x) {
33  return false;
34  };
35 };
36 
38 {
39  public:
40  virtual double deriv(double) = 0;
41 };
42 
44 {
45  public:
46  virtual double second_deriv(double) = 0;
47 };
48 
50 {
51  public:
52  virtual double third_deriv(double) = 0;
53 };
54 
56 {
57  public:
58  int errcode;
59  std::string errstring;
61  virtual ~FuncWrapperND(){};
62  virtual std::vector<double> call(const std::vector<double>&) = 0; // must be provided
63  virtual std::vector<std::vector<double>> Jacobian(const std::vector<double>&);
64 };
65 
66 // *****************************************************************************
67 // *****************************************************************************
68 // SOLVER ROUTINES
69 // *****************************************************************************
70 // *****************************************************************************
71 
72 // Single-Dimensional solvers, pointer versions
73 double Brent(FuncWrapper1D* f, double a, double b, double macheps, double t, int maxiter);
74 double Secant(FuncWrapper1D* f, double x0, double dx, double ftol, int maxiter);
75 double BoundedSecant(FuncWrapper1D* f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter);
76 double ExtrapolatingSecant(FuncWrapper1D* f, double x0, double dx, double ftol, int maxiter);
77 double Newton(FuncWrapper1DWithDeriv* f, double x0, double ftol, int maxiter);
78 double Halley(FuncWrapper1DWithTwoDerivs* f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12);
79 double Householder4(FuncWrapper1DWithThreeDerivs* f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12);
80 
81 // Single-Dimensional solvers, refere
82 inline double Brent(FuncWrapper1D& f, double a, double b, double macheps, double t, int maxiter) {
83  return Brent(&f, a, b, macheps, t, maxiter);
84 }
85 inline double Secant(FuncWrapper1D& f, double x0, double dx, double ftol, int maxiter) {
86  return Secant(&f, x0, dx, ftol, maxiter);
87 }
88 
89 inline double ExtrapolatingSecant(FuncWrapper1D& f, double x0, double dx, double ftol, int maxiter){
90  return ExtrapolatingSecant(&f, x0, dx, ftol, maxiter);
91 }
92 inline double BoundedSecant(FuncWrapper1D& f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter){
93  return BoundedSecant(&f, x0, xmin, xmax, dx, ftol, maxiter);
94 }
95 inline double Newton(FuncWrapper1DWithDeriv& f, double x0, double ftol, int maxiter) {
96  return Newton(&f, x0, ftol, maxiter);
97 }
98 inline double Halley(FuncWrapper1DWithTwoDerivs& f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12) {
99  return Halley(&f, x0, ftol, maxiter, xtol_rel);
100 }
101 inline double Householder4(FuncWrapper1DWithThreeDerivs& f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12) {
102  return Householder4(&f, x0, ftol, maxiter, xtol_rel);
103 }
104 
105 // Multi-Dimensional solvers
106 std::vector<double> NDNewtonRaphson_Jacobian(FuncWrapperND* f, const std::vector<double>& x0, double tol, int maxiter, double w = 1.0);
107 
108 }; /*namespace CoolProp*/
109 #endif