CoolProp  4.2.5
An open-source fluid property and humid air property database
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Spline.cpp
Go to the documentation of this file.
1 
2 #include "Spline.h"
3 #include "MatrixMath.h"
4 #include "CPExceptions.h"
5 #include "CoolPropTools.h"
6 
8 {
9  Nconstraints = 0;
10  A.resize(4, std::vector<double>(4, 0));
11  B.resize(4,0);
12 }
14 {
15  if (Nconstraints == 4)
16  {
17  std::vector<double> abcd = linsolve(A,B);
18  a = abcd[0];
19  b = abcd[1];
20  c = abcd[2];
21  d = abcd[3];
22  return true;
23  }
24  else
25  {
26  throw ValueError(format("Number of constraints[%d] is not equal to 4",Nconstraints));
27  }
28 }
29 bool SplineClass::add_value_constraint(double x, double y)
30 {
31  int i = Nconstraints;
32  if (i == 4)
33  return false;
34  A[i][0] = x*x*x;
35  A[i][1] = x*x;
36  A[i][2] = x;
37  A[i][3] = 1;
38  B[i] = y;
39  Nconstraints++;
40  return true;
41 }
42 void SplineClass::add_4value_constraints(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4)
43 {
44  add_value_constraint(x1, y1);
45  add_value_constraint(x2, y2);
46  add_value_constraint(x3, y3);
47  add_value_constraint(x4, y4);
48 }
49 bool SplineClass::add_derivative_constraint(double x, double dydx)
50 {
51  int i = Nconstraints;
52  if (i == 4)
53  return false;
54  A[i][0] = 3*x*x;
55  A[i][1] = 2*x;
56  A[i][2] = 1;
57  A[i][3] = 0;
58  B[i] = dydx;
59  Nconstraints++;
60  return true;
61 }
62 double SplineClass::evaluate(double x)
63 {
64  return a*x*x*x+b*x*x+c*x+d;
65 }
SplineClass()
Definition: Spline.cpp:7
double a
Definition: Spline.h:16
std::vector< double > B
Definition: Spline.h:14
double d
Definition: Spline.h:16
std::string format(const char *fmt,...)
std::vector< double > x(ncmax, 0)
std::vector< std::vector< double > > linsolve(std::vector< std::vector< double > > const &A, std::vector< std::vector< double > > const &B)
Definition: MatrixMath.cpp:185
double evaluate(double x)
Definition: Spline.cpp:62
bool add_value_constraint(double x, double y)
Definition: Spline.cpp:29
std::vector< std::vector< double > > A
Definition: Spline.h:13
void add_4value_constraints(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4)
Definition: Spline.cpp:42
double b
Definition: Spline.h:16
int Nconstraints
Definition: Spline.h:12
bool add_derivative_constraint(double x, double dydx)
Definition: Spline.cpp:49
double c
Definition: Spline.h:16
bool build(void)
Definition: Spline.cpp:13