https://mooseframework.inl.gov
SinglePhaseFluidProperties.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "FluidProperties.h"
13 #include "NewtonInversion.h"
14 #include "metaphysicl/dualnumberarray.h"
15 
21 #define propfuncAD(want, prop1, prop2) \
22  virtual ADReal want##_from_##prop1##_##prop2(const ADReal & p1, const ADReal & p2) const \
23  { \
24  Real x = 0; \
25  Real raw1 = p1.value(); \
26  Real raw2 = p2.value(); \
27  Real dxd1 = 0; \
28  Real dxd2 = 0; \
29  want##_from_##prop1##_##prop2(raw1, raw2, x, dxd1, dxd2); \
30  \
31  ADReal result = x; \
32  result.derivatives() = p1.derivatives() * dxd1 + p2.derivatives() * dxd2; \
33  return result; \
34  } \
35  \
36  virtual void want##_from_##prop1##_##prop2(const ADReal & prop1, \
37  const ADReal & prop2, \
38  ADReal & val, \
39  ADReal & d##want##d1, \
40  ADReal & d##want##d2) const \
41  { \
42  unimplementedDerivativeMethod(__PRETTY_FUNCTION__); \
43  Real dummy, tmp1, tmp2; \
44  val = want##_from_##prop1##_##prop2(prop1, prop2); \
45  want##_from_##prop1##_##prop2(prop1.value(), prop2.value(), dummy, tmp1, tmp2); \
46  d##want##d1 = tmp1; \
47  d##want##d2 = tmp2; \
48  }
49 
54 #define propfunc(want, prop1, prop2) \
55  virtual Real want##_from_##prop1##_##prop2(Real, Real) const \
56  { \
57  mooseError( \
58  "The fluid properties class '", \
59  type(), \
60  "' has not implemented the method below. If your application requires this method, you " \
61  "must either implement it or use a different fluid properties class.\n\n", \
62  __PRETTY_FUNCTION__); \
63  } \
64  \
65  virtual void want##_from_##prop1##_##prop2( \
66  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const \
67  { \
68  unimplementedDerivativeMethod(__PRETTY_FUNCTION__); \
69  d##want##d1 = 0; \
70  d##want##d2 = 0; \
71  val = want##_from_##prop1##_##prop2(prop1, prop2); \
72  } \
73  \
74  propfuncAD(want, prop1, prop2)
75 
81 #define propfuncWithDefault(want, prop1, prop2) \
82  virtual Real want##_from_##prop1##_##prop2(Real, Real) const; \
83  virtual void want##_from_##prop1##_##prop2( \
84  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const; \
85  \
86  propfuncAD(want, prop1, prop2)
87 
91 #define propfuncWithDefinitionOverride(want, prop1, prop2) \
92  Real want##_from_##prop1##_##prop2(Real, Real) const override; \
93  void want##_from_##prop1##_##prop2( \
94  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const override; \
95  ADReal want##_from_##prop1##_##prop2(const ADReal &, const ADReal &) const override; \
96  void want##_from_##prop1##_##prop2(const ADReal & prop1, \
97  const ADReal & prop2, \
98  ADReal & val, \
99  ADReal & d##want##d1, \
100  ADReal & d##want##d2) const override; \
101  template <typename CppType> \
102  CppType want##_from_##prop1##_##prop2##_template(const CppType & prop1, const CppType & prop2) \
103  const; \
104  template <typename CppType> \
105  void want##_from_##prop1##_##prop2##_template(const CppType & prop1, \
106  const CppType & prop2, \
107  CppType & val, \
108  CppType & d##want##d1, \
109  CppType & d##want##d2) const
110 
115 {
116 public:
117  static InputParameters validParams();
118 
120  virtual ~SinglePhaseFluidProperties();
121 
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
124  // clang-format off
125 
180  propfunc(p, v, e)
182  propfunc(T, v, e)
183  propfunc(c, v, e)
184  propfunc(cp, v, e)
185  propfunc(cv, v, e)
186  propfunc(mu, v, e)
187  propfunc(k, v, e)
189  propfunc(s, h, p)
191  propfunc(e, v, h)
193  propfunc(pp_sat, p, T)
195  propfunc(k, rho, T)
201  propfunc(rho, p, T)
203  propfunc(e, T, v)
205  propfunc(h, T, v)
206  propfunc(s, T, v)
207  propfunc(cv, T, v)
209  propfuncWithDefault(h, v, e)
210  propfunc(g, v, e)
211  propfuncWithDefault(p, h, s)
212  propfunc(T, h, p) // temporary, until uniformization
217  propfuncWithDefault(gamma, v, e)
220 
221  // clang-format on
222 
223 #undef propfunc
224 #undef propfuncWithDefault
225 #undef propfuncAD
226 
231  virtual std::string fluidName() const;
232 
237  virtual Real molarMass() const;
238 
243  virtual Real criticalPressure() const;
244 
249  virtual Real criticalTemperature() const;
250 
255  virtual Real criticalDensity() const;
256 
261  virtual Real criticalInternalEnergy() const;
262 
267  virtual Real triplePointPressure() const;
268 
273  virtual Real triplePointTemperature() const;
274 
281  virtual Real e_spndl_from_v(Real v) const;
282 
289  virtual void v_e_spndl_from_T(Real T, Real & v, Real & e) const;
290 
300  virtual Real vaporPressure(Real T) const;
301  virtual void vaporPressure(Real T, Real & psat, Real & dpsat_dT) const;
302  virtual ADReal vaporPressure(const ADReal & T) const;
303 
313  virtual Real vaporTemperature(Real p) const;
314  virtual void vaporTemperature(Real p, Real & Tsat, Real & dTsat_dp) const;
315  virtual ADReal vaporTemperature(const ADReal & p) const;
316 
321  virtual std::vector<Real> henryCoefficients() const;
322 
323  template <typename CppType>
324  void v_e_from_p_T(const CppType & p, const CppType & T, CppType & v, CppType & e) const;
325  template <typename CppType>
326  void v_e_from_p_T(const CppType & p,
327  const CppType & T,
328  CppType & v,
329  CppType & dv_dp,
330  CppType & dv_dT,
331  CppType & e,
332  CppType & de_dp,
333  CppType & de_dT) const;
334 
343  virtual void rho_mu_from_p_T(Real p, Real T, Real & rho, Real & mu) const;
344  virtual void rho_mu_from_p_T(Real p,
345  Real T,
346  Real & rho,
347  Real & drho_dp,
348  Real & drho_dT,
349  Real & mu,
350  Real & dmu_dp,
351  Real & dmu_dT) const;
352  virtual void rho_mu_from_p_T(const ADReal & p, const ADReal & T, ADReal & rho, ADReal & mu) const;
353 
354  virtual void rho_e_from_p_T(Real p,
355  Real T,
356  Real & rho,
357  Real & drho_dp,
358  Real & drho_dT,
359  Real & e,
360  Real & de_dp,
361  Real & de_dT) const;
362 
374  template <typename CppType>
375  void p_T_from_v_e(const CppType & v,
376  const CppType & e,
377  Real p0,
378  Real T0,
379  CppType & p,
380  CppType & T,
381  bool & conversion_succeeded) const;
382 
394  template <typename T>
395  void p_T_from_v_h(const T & v,
396  const T & h,
397  Real p0,
398  Real T0,
399  T & pressure,
400  T & temperature,
401  bool & conversion_succeeded) const;
413  template <typename T>
414  void p_T_from_h_s(const T & h,
415  const T & s,
416  Real p0,
417  Real T0,
418  T & pressure,
419  T & temperature,
420  bool & conversion_succeeded) const;
421 
422 protected:
428  template <typename T, typename Functor>
429  static void
430  xyDerivatives(const T x, const T & y, T & z, T & dz_dx, T & dz_dy, const Functor & z_from_x_y);
431 
436  template <typename T>
437  static std::pair<T, T> makeZeroAndOne(const T &);
438 
442  const Real _tolerance;
449  const unsigned int _max_newton_its;
450 
451 private:
452  void unimplementedDerivativeMethod(const std::string & property_function_name) const
453  {
454  const std::string message =
455  "The fluid properties class '" + type() +
456  "' has not implemented the method below, which computes derivatives of fluid properties "
457  "with regards to the flow variables. If your application requires this "
458  "method, you must either implement it or use a different fluid properties "
459  " class.\n\n" +
460  property_function_name;
461 
463  mooseDoOnce(mooseWarning(message + "\nThe unimplemented derivatives for this fluid property "
464  "are currently neglected, set to 0."));
465  else
466  mooseError(message + "\n\nYou can avoid this error by neglecting the "
467  "unimplemented derivatives of fluid properties by setting the "
468  "'allow_imperfect_jacobians' parameter");
469  }
470 };
471 
472 #pragma GCC diagnostic pop
473 
474 template <typename T>
475 std::pair<T, T>
477 {
478  return {T{0, 0}, T{1, 0}};
479 }
480 
481 template <>
482 inline std::pair<Real, Real>
484 {
485  return {Real{0}, Real{1}};
486 }
487 
488 template <typename T, typename Functor>
489 void
491  const T x, const T & y, T & z, T & dz_dx, T & dz_dy, const Functor & z_from_x_y)
492 {
494  const auto [zero, one] = makeZeroAndOne(x);
495 
496  CompoundType x_c(x, zero);
497  auto & x_cd = x_c.derivatives();
498  x_cd[0] = one;
499  CompoundType y_c(y, zero);
500  auto & y_cd = y_c.derivatives();
501  y_cd[1] = one;
502 
503  const auto z_c = z_from_x_y(x_c, y_c);
504  z = z_c.value();
505  dz_dx = z_c.derivatives()[0];
506  dz_dy = z_c.derivatives()[1];
507 }
508 
509 template <typename CppType>
510 void
511 SinglePhaseFluidProperties::p_T_from_v_e(const CppType & v, // v value
512  const CppType & e, // e value
513  const Real p0, // initial guess
514  const Real T0, // initial guess
515  CppType & p, // returned pressure
516  CppType & T, // returned temperature
517  bool & conversion_succeeded) const
518 {
519  auto v_lambda = [&](const CppType & pressure,
520  const CppType & temperature,
521  CppType & new_v,
522  CppType & dv_dp,
523  CppType & dv_dT) { v_from_p_T(pressure, temperature, new_v, dv_dp, dv_dT); };
524  auto e_lambda = [&](const CppType & pressure,
525  const CppType & temperature,
526  CppType & new_e,
527  CppType & de_dp,
528  CppType & de_dT) { e_from_p_T(pressure, temperature, new_e, de_dp, de_dT); };
529  try
530  {
532  v, e, p0, T0, p, T, _tolerance, _tolerance, v_lambda, e_lambda);
533  conversion_succeeded = true;
534  }
535  catch (MooseException &)
536  {
537  conversion_succeeded = false;
538  }
539 
540  if (!conversion_succeeded)
541  mooseDoOnce(mooseWarning("Conversion from (v, e)=(", v, ", ", e, ") to (p, T) failed"));
542 }
543 
544 template <typename T>
545 void
547  const T & h, // e value
548  const Real p0, // initial guess
549  const Real T0, // initial guess
550  T & pressure, // returned pressure
551  T & temperature, // returned temperature
552  bool & conversion_succeeded) const
553 {
554  auto v_lambda = [&](const T & pressure, const T & temperature, T & new_v, T & dv_dp, T & dv_dT)
555  { v_from_p_T(pressure, temperature, new_v, dv_dp, dv_dT); };
556  auto h_lambda = [&](const T & pressure, const T & temperature, T & new_h, T & dh_dp, T & dh_dT)
557  { h_from_p_T(pressure, temperature, new_h, dh_dp, dh_dT); };
558  try
559  {
561  v, h, p0, T0, pressure, temperature, _tolerance, _tolerance, v_lambda, h_lambda);
562  conversion_succeeded = true;
563  }
564  catch (MooseException &)
565  {
566  conversion_succeeded = false;
567  }
568 
569  if (!conversion_succeeded)
570  mooseDoOnce(mooseWarning("Conversion from (v, h)=(", v, ", ", h, ") to (p, T) failed"));
571 }
572 
573 template <typename T>
574 void
576  const T & s, // s value
577  const Real p0, // initial guess
578  const Real T0, // initial guess
579  T & pressure, // returned pressure
580  T & temperature, // returned temperature
581  bool & conversion_succeeded) const
582 {
583  auto h_lambda = [&](const T & pressure, const T & temperature, T & new_h, T & dh_dp, T & dh_dT)
584  { h_from_p_T(pressure, temperature, new_h, dh_dp, dh_dT); };
585  auto s_lambda = [&](const T & pressure, const T & temperature, T & new_s, T & ds_dp, T & ds_dT)
586  { s_from_p_T(pressure, temperature, new_s, ds_dp, ds_dT); };
587  try
588  {
590  h, s, p0, T0, pressure, temperature, _tolerance, _tolerance, h_lambda, s_lambda);
591  conversion_succeeded = true;
592  }
593  catch (MooseException &)
594  {
595  conversion_succeeded = false;
596  }
597 
598  if (!conversion_succeeded)
599  mooseDoOnce(mooseWarning("Conversion from (h, s)=(", h, ", ", s, ") to (p, T) failed"));
600 }
601 
602 template <typename CppType>
603 void
605  const CppType & T,
606  CppType & v,
607  CppType & e) const
608 {
609  const CppType rho = rho_from_p_T(p, T);
610  v = 1.0 / rho;
611  try
612  {
613  // more likely to not involve a Newton search
614  e = e_from_p_T(p, T);
615  }
616  catch (...)
617  {
618  e = e_from_p_rho(p, rho);
619  }
620 }
621 
622 template <typename CppType>
623 void
625  const CppType & T,
626  CppType & v,
627  CppType & dv_dp,
628  CppType & dv_dT,
629  CppType & e,
630  CppType & de_dp,
631  CppType & de_dT) const
632 {
633  CppType rho, drho_dp, drho_dT;
634  rho_from_p_T(p, T, rho, drho_dp, drho_dT);
635 
636  v = 1.0 / rho;
637  const CppType dv_drho = -1.0 / (rho * rho);
638  dv_dp = dv_drho * drho_dp;
639  dv_dT = dv_drho * drho_dT;
640 
641  CppType de_dp_partial, de_drho;
642  e_from_p_rho(p, rho, e, de_dp_partial, de_drho);
643  de_dp = de_dp_partial + de_drho * drho_dp;
644  de_dT = de_drho * drho_dT;
645 }
propfunc(p, v, e) propfunc(T
Compute a fluid property given for the state defined by two given properties.
static const std::string cv
Definition: NS.h:122
e e e e s T T T propfuncWithDefault(cp, p, T) propfuncWithDefault(cv
const bool _allow_imperfect_jacobians
Flag to set unimplemented Jacobian entries to zero.
virtual Real triplePointTemperature() const
Triple point temperature.
void p_T_from_v_h(const T &v, const T &h, Real p0, Real T0, T &pressure, T &temperature, bool &conversion_succeeded) const
Determines (p,T) from (v,h) using Newton Solve in 2D Useful for conversion between different sets of ...
static InputParameters validParams()
static std::pair< T, T > makeZeroAndOne(const T &)
Given a type example, this method returns zero and unity representations of that type (first and seco...
virtual Real molarMass() const
Molar mass [kg/mol].
virtual std::vector< Real > henryCoefficients() const
Henry&#39;s law coefficients for dissolution in water.
SinglePhaseFluidProperties(const InputParameters &parameters)
const std::vector< double > y
static const std::string temperature
Definition: NS.h:59
const Number zero
DualNumber< Real, DNDerivativeType, true > ADReal
void v_e_from_p_T(const CppType &p, const CppType &T, CppType &v, CppType &e) const
void mooseWarning(Args &&... args) const
void p_T_from_v_e(const CppType &v, const CppType &e, Real p0, Real T0, CppType &p, CppType &T, bool &conversion_succeeded) const
Determines (p,T) from (v,e) using Newton Solve in 2D Useful for conversion between different sets of ...
virtual Real criticalInternalEnergy() const
Critical specific internal energy.
static void xyDerivatives(const T x, const T &y, T &z, T &dz_dx, T &dz_dy, const Functor &z_from_x_y)
Computes the dependent variable z and its derivatives with respect to the independent variables x and...
static const std::string cp
Definition: NS.h:121
const Real _tolerance
Newton&#39;s method may be used to convert between variable sets.
e e e e s T T T T T rho v v T e h
virtual Real vaporTemperature(Real p) const
Vapor temperature.
void NewtonSolve2D(const T &f, const T &g, const Real x0, const Real y0, T &x_final, T &y_final, const Real f_tol, const Real g_tol, const Functor1 &func1, const Functor2 &func2, const unsigned int max_its=100)
NewtonSolve2D does a 2D Newton Solve to solve for the x and y such that: f = func1(x, y) and g = func2(x, y).
FunctorEnvelope< T > Functor
virtual Real criticalTemperature() const
Critical temperature.
const std::vector< double > x
static const std::string mu
Definition: NS.h:123
const std::string & type() const
Common class for single phase fluid properties.
virtual void v_e_spndl_from_T(Real T, Real &v, Real &e) const
Specific internal energy from temperature and specific volume.
virtual void rho_e_from_p_T(Real p, Real T, Real &rho, Real &drho_dp, Real &drho_dT, Real &e, Real &de_dp, Real &de_dT) const
virtual Real triplePointPressure() const
Triple point pressure.
virtual Real e_spndl_from_v(Real v) const
Specific internal energy from temperature and specific volume.
void p_T_from_h_s(const T &h, const T &s, Real p0, Real T0, T &pressure, T &temperature, bool &conversion_succeeded) const
Determines (p,T) from (h,s) using Newton Solve in 2D Useful for conversion between different sets of ...
virtual void rho_mu_from_p_T(Real p, Real T, Real &rho, Real &mu) const
Combined methods.
virtual Real criticalDensity() const
Critical density.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const std::string v
Definition: NS.h:84
static const std::string message
const unsigned int _max_newton_its
Maximum number of iterations for the variable conversion newton solves.
static const std::string pressure
Definition: NS.h:56
const Real _p_initial_guess
Initial guess for pressure (or pressure used to compute the initial guess)
void mooseError(Args &&... args) const
virtual Real criticalPressure() const
Critical pressure.
e e e e s T T T T T rho v v T e p T T virtual T std::string fluidName() const
Fluid name.
const InputParameters & parameters() const
virtual Real vaporPressure(Real T) const
Vapor pressure.
const Real _T_initial_guess
Initial guess for temperature (or temperature used to compute the initial guess)
void unimplementedDerivativeMethod(const std::string &property_function_name) const
static const std::string k
Definition: NS.h:130