forked from CoolProp/CoolProp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolvers.h
109 lines (95 loc) · 4.09 KB
/
Solvers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef SOLVERS_H
#define SOLVERS_H
#include <vector>
#include <string>
#include "Exceptions.h"
#include "CoolPropTools.h"
namespace CoolProp {
// *****************************************************************************
// *****************************************************************************
// SOLVER WRAPPER CLASSES
// *****************************************************************************
// *****************************************************************************
class FuncWrapper1D
{
public:
int errcode;
std::string errstring;
Dictionary options;
int iter;
FuncWrapper1D() : errcode(0), errstring(""){};
virtual ~FuncWrapper1D(){};
virtual double call(double) = 0;
/**
* /brief A function for checking whether the input is in range;
*
* Meant to be implemented by derived classes; return true if input is out of range
*/
virtual bool input_not_in_range(double x) {
return false;
};
};
class FuncWrapper1DWithDeriv : public FuncWrapper1D
{
public:
virtual double deriv(double) = 0;
};
class FuncWrapper1DWithTwoDerivs : public FuncWrapper1DWithDeriv
{
public:
virtual double second_deriv(double) = 0;
};
class FuncWrapper1DWithThreeDerivs : public FuncWrapper1DWithTwoDerivs
{
public:
virtual double third_deriv(double) = 0;
};
class FuncWrapperND
{
public:
int errcode;
std::string errstring;
FuncWrapperND() : errcode(0), errstring(""){};
virtual ~FuncWrapperND(){};
virtual std::vector<double> call(const std::vector<double>&) = 0; // must be provided
virtual std::vector<std::vector<double>> Jacobian(const std::vector<double>&);
};
// *****************************************************************************
// *****************************************************************************
// SOLVER ROUTINES
// *****************************************************************************
// *****************************************************************************
// Single-Dimensional solvers, pointer versions
double Brent(FuncWrapper1D* f, double a, double b, double macheps, double t, int maxiter);
double Secant(FuncWrapper1D* f, double x0, double dx, double ftol, int maxiter);
double BoundedSecant(FuncWrapper1D* f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter);
double ExtrapolatingSecant(FuncWrapper1D* f, double x0, double dx, double ftol, int maxiter);
double Newton(FuncWrapper1DWithDeriv* f, double x0, double ftol, int maxiter);
double Halley(FuncWrapper1DWithTwoDerivs* f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12);
double Householder4(FuncWrapper1DWithThreeDerivs* f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12);
// Single-Dimensional solvers, refere
inline double Brent(FuncWrapper1D& f, double a, double b, double macheps, double t, int maxiter) {
return Brent(&f, a, b, macheps, t, maxiter);
}
inline double Secant(FuncWrapper1D& f, double x0, double dx, double ftol, int maxiter) {
return Secant(&f, x0, dx, ftol, maxiter);
}
inline double ExtrapolatingSecant(FuncWrapper1D& f, double x0, double dx, double ftol, int maxiter){
return ExtrapolatingSecant(&f, x0, dx, ftol, maxiter);
}
inline double BoundedSecant(FuncWrapper1D& f, double x0, double xmin, double xmax, double dx, double ftol, int maxiter){
return BoundedSecant(&f, x0, xmin, xmax, dx, ftol, maxiter);
}
inline double Newton(FuncWrapper1DWithDeriv& f, double x0, double ftol, int maxiter) {
return Newton(&f, x0, ftol, maxiter);
}
inline double Halley(FuncWrapper1DWithTwoDerivs& f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12) {
return Halley(&f, x0, ftol, maxiter, xtol_rel);
}
inline double Householder4(FuncWrapper1DWithThreeDerivs& f, double x0, double ftol, int maxiter, double xtol_rel = 1e-12) {
return Householder4(&f, x0, ftol, maxiter, xtol_rel);
}
// Multi-Dimensional solvers
std::vector<double> NDNewtonRaphson_Jacobian(FuncWrapperND* f, const std::vector<double>& x0, double tol, int maxiter, double w = 1.0);
}; /*namespace CoolProp*/
#endif