CoolProp
IncompressibleFluid.h
1 /*
2  * CoolPropFluid.h
3  *
4  * Created on: 20 Dec 2013
5  * Author: jowr
6  */
7 
8 #ifndef INCOMPRESSIBLEFLUID_H_
9 #define INCOMPRESSIBLEFLUID_H_
10 
11 #include "DataStructures.h"
12 #include "Helmholtz.h"
13 #include "Solvers.h"
14 
15 #include <numeric>
16 #include <string>
17 #include <vector>
18 #include <map>
19 #include <cassert>
20 #include <iterator>
21 
22 #include <Eigen/Core>
23 #include "PolyMath.h"
24 #include "MatrixMath.h"
25 
26 namespace CoolProp {
27 
29 {
30  enum IncompressibleTypeEnum
31  {
32  INCOMPRESSIBLE_NOT_SET,
33  INCOMPRESSIBLE_POLYNOMIAL,
34  INCOMPRESSIBLE_EXPPOLYNOMIAL,
35  INCOMPRESSIBLE_EXPONENTIAL,
36  INCOMPRESSIBLE_LOGEXPONENTIAL,
37  INCOMPRESSIBLE_POLYOFFSET
38  };
39  IncompressibleTypeEnum type;
40  Eigen::MatrixXd coeffs; //TODO: Can we store the Eigen::Matrix objects more efficiently?
41  //std::vector<std::vector<double> > coeffs;
43  type = INCOMPRESSIBLE_NOT_SET;
44  };
45 };
46 
47 #if !defined(NO_FMTLIB) && FMT_VERSION >= 90000
48 inline int format_as(IncompressibleData::IncompressibleTypeEnum type) {
49  return fmt::underlying(type);
50 }
51 #endif
52 
54 
58 {
59 
60  protected:
61  bool strict;
62 
63  std::string name;
64  std::string description;
65  std::string reference;
66 
67  double Tmin, Tmax;
68  double xmin, xmax;
70 
71  double TminPsat;
72  double xbase, Tbase;
73 
75 
84 
90 
97 
102 
107 
112 
116 
118 
123 
128 
132 
133  Polynomial2DFrac poly;
134 
135  // Forward declaration of the some internal functions
136  //double h_u(double T, double p, double x);
137  //double u_h(double T, double p, double x);
138 
139  public:
140  IncompressibleFluid() : Tmin(_HUGE), Tmax(_HUGE), xmin(_HUGE), xmax(_HUGE), TminPsat(_HUGE), xbase(_HUGE), Tbase(_HUGE) {
141  strict = true;
142  xid = IFRAC_UNDEFINED;
143  };
144  virtual ~IncompressibleFluid(){};
145 
146  std::string getName() const {
147  return name;
148  }
149  std::string get_name() const {
150  return getName();
151  } // For backwards-compatibility.
152  std::string getDescription() const {
153  return description;
154  }
155  std::string getReference() const {
156  return reference;
157  }
158 
159  double getTmax() const {
160  return Tmax;
161  }
162  double getTmin() const {
163  return Tmin;
164  }
165  double getxmax() const {
166  return xmax;
167  }
168  double getxmin() const {
169  return xmin;
170  }
171  composition_types getxid() const {
172  return xid;
173  }
174  double getTminPsat() const {
175  return TminPsat;
176  }
177  double getTbase() const {
178  return Tbase;
179  }
180  double getxbase() const {
181  return xbase;
182  }
183 
184  void setName(const std::string& name) {
185  this->name = name;
186  }
187  void setDescription(const std::string& description) {
188  this->description = description;
189  }
190  void setReference(const std::string& reference) {
191  this->reference = reference;
192  }
193  void setTmax(double Tmax) {
194  this->Tmax = Tmax;
195  }
196  void setTmin(double Tmin) {
197  this->Tmin = Tmin;
198  }
199  void setxmax(double xmax) {
200  this->xmax = xmax;
201  }
202  void setxmin(double xmin) {
203  this->xmin = xmin;
204  }
205  void setxid(composition_types xid) {
206  this->xid = xid;
207  }
208  void setTminPsat(double TminPsat) {
209  this->TminPsat = TminPsat;
210  }
211  void setTbase(double Tbase) {
212  this->Tbase = Tbase;
213  }
214  void setxbase(double xbase) {
215  this->xbase = xbase;
216  }
217 
220  this->density = density;
221  }
222  void setSpecificHeat(IncompressibleData specific_heat) {
223  this->specific_heat = specific_heat;
224  }
225  void setViscosity(IncompressibleData viscosity) {
226  this->viscosity = viscosity;
227  }
228  void setConductivity(IncompressibleData conductivity) {
229  this->conductivity = conductivity;
230  }
231  void setPsat(IncompressibleData p_sat) {
232  this->p_sat = p_sat;
233  }
234  void setTfreeze(IncompressibleData T_freeze) {
235  this->T_freeze = T_freeze;
236  }
237 
240  this->mass2input = mass2input;
241  }
242  void setVolume2input(IncompressibleData volume2input) {
243  this->volume2input = volume2input;
244  }
245  void setMole2input(IncompressibleData mole2input) {
246  this->mole2input = mole2input;
247  }
248 
250  void validate();
252  bool is_pure();
253 
254  protected:
256  double baseExponential(IncompressibleData data, double y, double ybase);
257  double baseLogexponential(IncompressibleData data, double y, double ybase);
258  double baseExponentialOffset(IncompressibleData data, double y);
259  double basePolyOffset(IncompressibleData data, double y, double z = 0.0);
260 
261  public:
262  /* All functions need T and p as input. Might not
263  * be necessary, but gives a clearer structure.
264  */
266  double rho(double T, double p, double x);
268  double c(double T, double p, double x);
269  double cp(double T, double p, double x) {
270  throw ValueError(format("%s (%d): Please use the c-function instead.", __FILE__, __LINE__));
271  }
272  double cv(double T, double p, double x) {
273  throw ValueError(format("%s (%d): Please use the c-function instead.", __FILE__, __LINE__));
274  }
276  double s(double T, double p, double x) {
277  throw ValueError(format("%s (%d): The internal calculations have changed, use the backend to calculate entropy from the partial derivatives.",
278  __FILE__, __LINE__));
279  }
281  double u(double T, double p, double x) {
282  throw ValueError(
283  format("%s (%d): The internal calculations have changed, use the backend to calculate internal energy from enthalpy.", __FILE__, __LINE__));
284  }
286  double h(double T, double p, double x) {
287  throw ValueError(
288  format("%s (%d): The internal calculations have changed, use the backend to calculate enthalpy from the partial derivatives.", __FILE__,
289  __LINE__));
290  }
292  double visc(double T, double p, double x);
294  double cond(double T, double p, double x);
296  double psat(double T, double x);
298  double Tfreeze(double p, double x);
299 
300  /* Below are direct calculations of the derivatives. Nothing
301  * special is going on, we simply use the polynomial class to
302  * derive the different functions with respect to temperature.
303  */
305  // with respect to temperature at constant pressure and composition
306  double drhodTatPx(double T, double p, double x);
309  //double dsdTatPx (double T, double p, double x){return c(T,p,x)/T;};
312  //double dhdTatPx (double T, double p, double x){return c(T,p,x);};
314  // with respect to temperature at constant pressure and composition
315  // integrated in temperature
316  double dsdTatPxdT(double T, double p, double x);
318  // with respect to temperature at constant pressure and composition
319  // integrated in temperature
320  double dhdTatPxdT(double T, double p, double x);
321 
323 
325  double inputFromMass(double T, double x);
327 
329  double inputFromVolume(double T, double x);
331 
333  double inputFromMole(double T, double x);
334 
335  /* Some functions can be inverted directly, those are listed
336  * here. It is also possible to solve for other quantities, but
337  * that involves some more sophisticated processing and is not
338  * done here, but in the backend, T(h,p) for example.
339  */
341  double T_rho(double Dmass, double p, double x);
343  double T_c(double Cmass, double p, double x);
345  double T_s(double Smass, double p, double x) {
346  throw NotImplementedError(format("%s (%d): T from entropy is not implemented in the fluid, use the backend.", __FILE__, __LINE__));
347  }
349  double T_u(double Umass, double p, double x) {
350  throw NotImplementedError(format("%s (%d): T from internal energy is not implemented in the fluid, use the backend.", __FILE__, __LINE__));
351  }
353  double T_h(double Hmass, double p, double x) {
354  throw NotImplementedError(format("%s (%d): T from enthalpy is not implemented in the fluid, use the backend.", __FILE__, __LINE__));
355  }
357  double T_visc(double visc, double p, double x) {
358  throw NotImplementedError(format("%s (%d): T from viscosity is not implemented.", __FILE__, __LINE__));
359  }
361  double T_cond(double cond, double p, double x) {
362  throw NotImplementedError(format("%s (%d): T from conductivity is not implemented.", __FILE__, __LINE__));
363  }
365  double T_psat(double psat, double x) {
366  throw NotImplementedError(format("%s (%d): T from psat is not implemented.", __FILE__, __LINE__));
367  }
369  double x_Tfreeze(double Tfreeze, double p) {
370  throw NotImplementedError(format("%s (%d): x from T_freeze is not implemented.", __FILE__, __LINE__));
371  }
372 
373  protected:
374  /* Define internal energy and enthalpy as functions of the
375  * other properties to provide data in case there are no
376  * coefficients.
377  */
379 
382  double h_u(double T, double p, double x) {
383  return u(T, p, x) + p / rho(T, p, x);
384  };
385 
387 
390  double u_h(double T, double p, double x) {
391  return h(T, p, x) - p / rho(T, p, x);
392  };
393 
394  /*
395  * Some more functions to provide a single implementation
396  * of important routines.
397  * We start with the check functions that can validate input
398  * in terms of pressure p, temperature T and composition x.
399  */
401 
404  bool checkT(double T, double p, double x);
405 
407 
413  bool checkP(double T, double p, double x);
414 
415  public:
417 
420  bool checkX(double x);
421 
423  bool checkTPX(double T, double p, double x) {
424  return (checkT(T, p, x) && checkP(T, p, x) && checkX(x));
425  };
426 };
427 
428 } /* namespace CoolProp */
429 #endif /* INCOMPRESSIBLEFLUID_H_ */
IncompressibleData p_sat
Saturation pressure coefficients.
Definition: IncompressibleFluid.h:110
double s(double T, double p, double x)
Entropy as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:276
double T_cond(double cond, double p, double x)
Thermal conductivity as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:361
IncompressibleData conductivity
Conductivity coefficients.
Definition: IncompressibleFluid.h:105
double T_u(double Umass, double p, double x)
Temperature as a function of internal energy as a function of temperature, pressure and composition...
Definition: IncompressibleFluid.h:349
void setDensity(IncompressibleData density)
Setters for the coefficients.
Definition: IncompressibleFluid.h:219
double T_psat(double psat, double x)
Saturation pressure as a function of temperature and composition.
Definition: IncompressibleFluid.h:365
void setMass2input(IncompressibleData mass2input)
Setters for the concentration conversion coefficients.
Definition: IncompressibleFluid.h:239
A property provider for incompressible solutions and pure fluids.
Definition: IncompressibleFluid.h:57
double x_Tfreeze(double Tfreeze, double p)
Composition as a function of freezing temperature and pressure.
Definition: IncompressibleFluid.h:369
A class for polynomials starting at an arbitrary degree.
Definition: PolyMath.h:215
double T_visc(double visc, double p, double x)
Viscosity as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:357
Definition: IncompressibleFluid.h:28
composition_types
These are constants for the compositions.
Definition: DataStructures.h:252
bool checkTPX(double T, double p, double x)
Check validity of temperature, pressure and composition input.
Definition: IncompressibleFluid.h:423
double T_h(double Hmass, double p, double x)
Temperature as a function of enthalpy, pressure and composition.
Definition: IncompressibleFluid.h:353
double u_h(double T, double p, double x)
Internal energy from h, p and rho.
Definition: IncompressibleFluid.h:390
Definition: Exceptions.h:45
IncompressibleData mole2input
Mole fraction conversion coefficients.
Definition: IncompressibleFluid.h:131
double T_s(double Smass, double p, double x)
Temperature as a function of entropy as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:345
double h_u(double T, double p, double x)
Enthalpy from u, p and rho.
Definition: IncompressibleFluid.h:382
IncompressibleData viscosity
Viscosity coefficients.
Definition: IncompressibleFluid.h:100
double h(double T, double p, double x)
Enthalpy as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:286
IncompressibleData specific_heat
Specific heat coefficients.
Definition: IncompressibleFluid.h:95
IncompressibleData volume2input
Volume fraction conversion coefficients.
Definition: IncompressibleFluid.h:126
This file contains flash routines in which the state is unknown, and a solver of some kind must be us...
Definition: AbstractState.h:19
double u(double T, double p, double x)
Internal energy as a function of temperature, pressure and composition.
Definition: IncompressibleFluid.h:281
IncompressibleData T_freeze
Freezing temperature coefficients.
Definition: IncompressibleFluid.h:115
IncompressibleData density
These are the objects that hold the coefficients.
Definition: IncompressibleFluid.h:88
IncompressibleData mass2input
Mass fraction conversion coefficients.
Definition: IncompressibleFluid.h:121