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
CoolPropTools.cpp
Go to the documentation of this file.
1 #define _CRT_SECURE_NO_WARNINGS
2 #include <string>
3 #include <vector>
4 #include <cstdio>
5 #include <cstdarg>
6 #include <stdlib.h>
7 #include "math.h"
8 #include "stdio.h"
9 #include "float.h"
10 #include <string.h>
11 #include "CoolPropTools.h"
12 
13 double root_sum_square(std::vector<double> x)
14 {
15  double sum = 0;
16  for (unsigned int i=0; i<x.size(); i++)
17  {
18  sum += pow(x[i],2);
19  }
20  return sqrt(sum);
21 }
22 std::string format(const char* fmt, ...)
23 {
24  int size = 512;
25  char* buffer = 0;
26  buffer = new char[size];
27  va_list vl;
28  va_start(vl,fmt);
29  int nsize = vsnprintf(buffer,size,fmt,vl);
30  if(size<=nsize){//fail delete buffer and try again
31  delete buffer; buffer = 0;
32  buffer = new char[nsize+1];//+1 for /0
33  nsize = vsnprintf(buffer,size,fmt,vl);
34  }
35  std::string ret(buffer);
36  va_end(vl);
37  delete[] buffer;
38  return ret;
39 }
40 
41 std::vector<std::string> strsplit(std::string s, char del)
42 {
43  int iL = 0, iR = 0, N;
44  N = s.size();
45  std::vector<std::string> v;
46  // Find the first instance of the delimiter
47  iR = s.find_first_of(del);
48  // Delimiter not found, return the same string again
49  if (iR < 0){
50  v.push_back(s);
51  return v;
52  }
53  while (iR != N-1)
54  {
55  v.push_back(s.substr(iL,iR-iL));
56  iL = iR;
57  iR = s.find_first_of(del,iR+1);
58  // Move the iL to the right to avoid the delimiter
59  iL += 1;
60  if (iR == (int)std::string::npos)
61  {
62  v.push_back(s.substr(iL,N-iL));
63  break;
64  }
65  }
66  return v;
67 }
68 
69 double interp1d(std::vector<double> *x, std::vector<double> *y, double x0)
70 {
71  unsigned int i,L,R,M;
72  L=0;
73  R=(*x).size()-1;
74  M=(L+R)/2;
75  // Use interval halving to find the indices which bracket the density of interest
76  while (R-L>1)
77  {
78  if (x0 >= (*x)[M])
79  { L=M; M=(L+R)/2; continue;}
80  if (x0 < (*x)[M])
81  { R=M; M=(L+R)/2; continue;}
82  }
83  i=L;
84  if (i<(*x).size()-2)
85  {
86  // Go "forwards" with the interpolation range
87  return QuadInterp((*x)[i],(*x)[i+1],(*x)[i+2],(*y)[i],(*y)[i+1],(*y)[i+2],x0);
88  }
89  else
90  {
91  // Go "backwards" with the interpolation range
92  return QuadInterp((*x)[i],(*x)[i-1],(*x)[i-2],(*y)[i],(*y)[i-1],(*y)[i-2],x0);
93  }
94 }
95 double powInt(double x, int y)
96 {
97  // Raise a double to an integer power
98  // Overload not provided in math.h
99  int i;
100  double product=1.0;
101  double x_in;
102  int y_in;
103 
104  if (y==0)
105  {
106  return 1.0;
107  }
108 
109  if (y<0)
110  {
111  x_in=1/x;
112  y_in=-y;
113  }
114  else
115  {
116  x_in=x;
117  y_in=y;
118  }
119 
120  if (y_in==1)
121  {
122  return x_in;
123  }
124 
125  product=x_in;
126  for (i=1;i<y_in;i++)
127  {
128  product=product*x_in;
129  }
130  return product;
131 }
132 
133 double QuadInterp(double x0, double x1, double x2, double f0, double f1, double f2, double x)
134 {
135  /* Quadratic interpolation.
136  Based on method from Kreyszig,
137  Advanced Engineering Mathematics, 9th Edition
138  */
139  double L0, L1, L2;
140  L0=((x-x1)*(x-x2))/((x0-x1)*(x0-x2));
141  L1=((x-x0)*(x-x2))/((x1-x0)*(x1-x2));
142  L2=((x-x0)*(x-x1))/((x2-x0)*(x2-x1));
143  return L0*f0+L1*f1+L2*f2;
144 }
145 double CubicInterp( double x0, double x1, double x2, double x3, double f0, double f1, double f2, double f3, double x)
146 {
147  /*
148  Lagrange cubic interpolation as from
149  http://nd.edu/~jjwteach/441/PdfNotes/lecture6.pdf
150  */
151  double L0,L1,L2,L3;
152  L0=((x-x1)*(x-x2)*(x-x3))/((x0-x1)*(x0-x2)*(x0-x3));
153  L1=((x-x0)*(x-x2)*(x-x3))/((x1-x0)*(x1-x2)*(x1-x3));
154  L2=((x-x0)*(x-x1)*(x-x3))/((x2-x0)*(x2-x1)*(x2-x3));
155  L3=((x-x0)*(x-x1)*(x-x2))/((x3-x0)*(x3-x1)*(x3-x2));
156  return L0*f0+L1*f1+L2*f2+L3*f3;
157 }
158 
159 void MatInv_2(double A[2][2] , double B[2][2])
160 {
161  double Det;
162  //Using Cramer's Rule to solve
163 
164  Det=A[0][0]*A[1][1]-A[1][0]*A[0][1];
165  B[0][0]=1.0/Det*A[1][1];
166  B[1][1]=1.0/Det*A[0][0];
167  B[1][0]=-1.0/Det*A[1][0];
168  B[0][1]=-1.0/Det*A[0][1];
169 }
170 
171 
172 std::string get_file_contents(const char *filename)
173 {
174  std::ifstream in(filename, std::ios::in | std::ios::binary);
175  if (in)
176  {
177  std::string contents;
178  in.seekg(0, std::ios::end);
179  contents.resize((unsigned int) in.tellg());
180  in.seekg(0, std::ios::beg);
181  in.read(&contents[0], contents.size());
182  in.close();
183  return(contents);
184  }
185  throw(errno);
186 }
187 
188 void solve_cubic(double a, double b, double c, double d, double *x0, double *x1, double *x2)
189 {
190  // 0 = ax^3 + b*x^2 + c*x + d
191 
192  // Discriminant
193  double DELTA = 18*a*b*c*d-4*b*b*b*d+b*b*c*c-4*a*c*c*c-27*a*a*d*d;
194 
195  // Coefficients for the depressed cubic t^3+p*t+q = 0
196  double p = (3*a*c-b*b)/(3*a*a);
197  double q = (2*b*b*b-9*a*b*c+27*a*a*d)/(27*a*a*a);
198 
199  if (DELTA<0)
200  {
201  // One real root
202  double t0;
203  if (4*p*p*p+27*q*q>0 && p<0)
204  {
205  t0 = -2.0*fabs(q)/q*sqrt(-p/3.0)*cosh(1.0/3.0*acosh(-3.0*fabs(q)/(2.0*p)*sqrt(-3.0/p)));
206  }
207  else
208  {
209  t0 = -2.0*sqrt(p/3.0)*sinh(1.0/3.0*asinh(3.0*q/(2.0*p)*sqrt(3.0/p)));
210  }
211  *x0 = t0-b/(3*a);
212  *x1 = t0-b/(3*a);
213  *x2 = t0-b/(3*a);
214  }
215  else //(DELTA>0)
216  {
217  // Three real roots
218  double t0 = 2.0*sqrt(-p/3.0)*cos(1.0/3.0*acos(3.0*q/(2.0*p)*sqrt(-3.0/p))-0*2.0*M_PI/3.0);
219  double t1 = 2.0*sqrt(-p/3.0)*cos(1.0/3.0*acos(3.0*q/(2.0*p)*sqrt(-3.0/p))-1*2.0*M_PI/3.0);
220  double t2 = 2.0*sqrt(-p/3.0)*cos(1.0/3.0*acos(3.0*q/(2.0*p)*sqrt(-3.0/p))-2*2.0*M_PI/3.0);
221 
222  *x0 = t0-b/(3*a);
223  *x1 = t1-b/(3*a);
224  *x2 = t2-b/(3*a);
225  }
226 }
227 
228 std::string strjoin(std::vector<std::string> strings, std::string delim)
229 {
230  // Empty input vector
231  if (strings.empty()){return "";}
232 
233  std::string output = strings[0];
234  for (unsigned int i = 1; i < strings.size(); i++)
235  {
236  output += format("%s%s",delim.c_str(),strings[i].c_str());
237  }
238  return output;
239 }
void MatInv_2(double A[2][2], double B[2][2])
double root_sum_square(std::vector< double > x)
#define M_PI
Definition: CoolPropTools.h:58
std::string strjoin(std::vector< std::string > strings, std::string delim)
std::vector< std::string > strsplit(std::string s, char del)
double powInt(double x, int y)
std::string format(const char *fmt,...)
double QuadInterp(double x0, double x1, double x2, double f0, double f1, double f2, double x)
std::vector< double > x(ncmax, 0)
void solve_cubic(double a, double b, double c, double d, double *x0, double *x1, double *x2)
double CubicInterp(double x0, double x1, double x2, double x3, double f0, double f1, double f2, double f3, double x)
std::string get_file_contents(const char *filename)
double interp1d(std::vector< double > *x, std::vector< double > *y, double x0)