www.mooseframework.org
SinglePhaseFluidProperties.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 DualReal want##_from_##prop1##_##prop2(const DualReal & p1, const DualReal & 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  DualReal result = x; \
32  result.derivatives() = p1.derivatives() * dxd1 + p2.derivatives() * dxd2; \
33  return result; \
34  } \
35  \
36  virtual void want##_from_##prop1##_##prop2(const DualReal & prop1, \
37  const DualReal & prop2, \
38  DualReal & val, \
39  DualReal & d##want##d1, \
40  DualReal & d##want##d2) const \
41  { \
42  fluidPropError(name(), ": ", __PRETTY_FUNCTION__, " derivative derivatives not implemented."); \
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(name(), ": ", __PRETTY_FUNCTION__, " not implemented."); \
58  } \
59  \
60  virtual void want##_from_##prop1##_##prop2( \
61  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const \
62  { \
63  fluidPropError(name(), ": ", __PRETTY_FUNCTION__, " derivatives not implemented."); \
64  d##want##d1 = 0; \
65  d##want##d2 = 0; \
66  val = want##_from_##prop1##_##prop2(prop1, prop2); \
67  } \
68  \
69  propfuncAD(want, prop1, prop2)
70 
76 #define propfuncWithDefault(want, prop1, prop2) \
77  virtual Real want##_from_##prop1##_##prop2(Real, Real) const; \
78  virtual void want##_from_##prop1##_##prop2( \
79  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const; \
80  \
81  propfuncAD(want, prop1, prop2)
82 
86 #define propfuncWithDefinitionOverride(want, prop1, prop2) \
87  Real want##_from_##prop1##_##prop2(Real, Real) const override; \
88  void want##_from_##prop1##_##prop2( \
89  Real prop1, Real prop2, Real & val, Real & d##want##d1, Real & d##want##d2) const override; \
90  ADReal want##_from_##prop1##_##prop2(const ADReal &, const ADReal &) const override; \
91  void want##_from_##prop1##_##prop2(const ADReal & prop1, \
92  const ADReal & prop2, \
93  ADReal & val, \
94  ADReal & d##want##d1, \
95  ADReal & d##want##d2) const override; \
96  template <typename CppType> \
97  CppType want##_from_##prop1##_##prop2##_template(const CppType & prop1, const CppType & prop2) \
98  const; \
99  template <typename CppType> \
100  void want##_from_##prop1##_##prop2##_template(const CppType & prop1, \
101  const CppType & prop2, \
102  CppType & val, \
103  CppType & d##want##d1, \
104  CppType & d##want##d2) const
105 
110 {
111 public:
112  static InputParameters validParams();
113 
115  virtual ~SinglePhaseFluidProperties();
116 
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
119  // clang-format off
120 
175  propfunc(p, v, e)
177  propfunc(T, v, e)
178  propfunc(c, v, e)
179  propfunc(cp, v, e)
180  propfunc(cv, v, e)
181  propfunc(mu, v, e)
182  propfunc(k, v, e)
184  propfunc(s, h, p)
186  propfunc(e, v, h)
188  propfunc(pp_sat, p, T)
190  propfunc(k, rho, T)
196  propfunc(rho, p, T)
198  propfunc(e, T, v)
200  propfunc(h, T, v)
201  propfunc(s, T, v)
202  propfunc(cv, T, v)
204  propfunc(g, v, e)
206  propfunc(T, h, p) // temporary, until uniformization
208  propfuncWithDefault(beta, p, T)
210  propfuncWithDefault(e, p, T)
214 
215  // clang-format on
216 
217 #undef propfunc
218 #undef propfuncWithDefault
219 #undef propfuncAD
220 
225  virtual std::string fluidName() const;
226 
231  virtual Real molarMass() const;
232 
237  virtual Real criticalPressure() const;
238 
243  virtual Real criticalTemperature() const;
244 
249  virtual Real criticalDensity() const;
250 
255  virtual Real criticalInternalEnergy() const;
256 
261  virtual Real triplePointPressure() const;
262 
267  virtual Real triplePointTemperature() const;
268 
275  virtual Real e_spndl_from_v(Real v) const;
276 
283  virtual void v_e_spndl_from_T(Real T, Real & v, Real & e) const;
284 
294  virtual Real vaporPressure(Real T) const;
295  virtual void vaporPressure(Real T, Real & psat, Real & dpsat_dT) const;
296  DualReal vaporPressure(const DualReal & T) const;
297 
307  virtual Real vaporTemperature(Real p) const;
308  virtual void vaporTemperature(Real p, Real & Tsat, Real & dTsat_dp) const;
309  DualReal vaporTemperature(const DualReal & p) const;
310 
315  virtual std::vector<Real> henryCoefficients() const;
316 
317  template <typename CppType>
318  void v_e_from_p_T(const CppType & p, const CppType & T, CppType & v, CppType & e) const;
319  template <typename CppType>
320  void v_e_from_p_T(const CppType & p,
321  const CppType & T,
322  CppType & v,
323  CppType & dv_dp,
324  CppType & dv_dT,
325  CppType & e,
326  CppType & de_dp,
327  CppType & de_dT) const;
328 
337  virtual void rho_mu_from_p_T(Real p, Real T, Real & rho, Real & mu) const;
338  virtual void rho_mu_from_p_T(Real p,
339  Real T,
340  Real & rho,
341  Real & drho_dp,
342  Real & drho_dT,
343  Real & mu,
344  Real & dmu_dp,
345  Real & dmu_dT) const;
346  virtual void
347  rho_mu_from_p_T(const DualReal & p, const DualReal & T, DualReal & rho, DualReal & mu) const;
348 
349  virtual void rho_e_from_p_T(Real p,
350  Real T,
351  Real & rho,
352  Real & drho_dp,
353  Real & drho_dT,
354  Real & e,
355  Real & de_dp,
356  Real & de_dT) const;
357 
369  template <typename CppType>
370  void p_T_from_v_e(const CppType & v,
371  const CppType & e,
372  Real p0,
373  Real T0,
374  CppType & p,
375  CppType & T,
376  bool & conversion_succeeded) const;
377 
389  template <typename T>
390  void p_T_from_v_h(const T & v,
391  const T & h,
392  Real p0,
393  Real T0,
394  T & pressure,
395  T & temperature,
396  bool & conversion_succeeded) const;
408  template <typename T>
409  void p_T_from_h_s(const T & h,
410  const T & s,
411  Real p0,
412  Real T0,
413  T & pressure,
414  T & temperature,
415  bool & conversion_succeeded) const;
416 
417 protected:
423  template <typename T, typename Functor>
424  static void
425  xyDerivatives(const T x, const T & y, T & z, T & dz_dx, T & dz_dy, const Functor & z_from_x_y);
426 
431  template <typename T>
432  static std::pair<T, T> makeZeroAndOne(const T &);
433 
442 
443 private:
444  template <typename... Args>
445  void fluidPropError(Args... args) const
446  {
448  mooseDoOnce(mooseWarning(std::forward<Args>(args)...));
449  else
450  mooseError(std::forward<Args>(args)...);
451  }
452 };
453 
454 #pragma GCC diagnostic pop
455 
456 template <typename T>
457 std::pair<T, T>
459 {
460  return {T{0, 0}, T{1, 0}};
461 }
462 
463 template <>
464 inline std::pair<Real, Real>
466 {
467  return {Real{0}, Real{1}};
468 }
469 
470 template <typename T, typename Functor>
471 void
473  const T x, const T & y, T & z, T & dz_dx, T & dz_dy, const Functor & z_from_x_y)
474 {
476  const auto [zero, one] = makeZeroAndOne(x);
477 
478  CompoundType x_c(x, zero);
479  auto & x_cd = x_c.derivatives();
480  x_cd[0] = one;
481  CompoundType y_c(y, zero);
482  auto & y_cd = y_c.derivatives();
483  y_cd[1] = one;
484 
485  const auto z_c = z_from_x_y(x_c, y_c);
486  z = z_c.value();
487  dz_dx = z_c.derivatives()[0];
488  dz_dy = z_c.derivatives()[1];
489 }
490 
491 template <typename CppType>
492 void
493 SinglePhaseFluidProperties::p_T_from_v_e(const CppType & v, // v value
494  const CppType & e, // e value
495  const Real p0, // initial guess
496  const Real T0, // initial guess
497  CppType & p, // returned pressure
498  CppType & T, // returned temperature
499  bool & conversion_succeeded) const
500 {
501  auto v_lambda = [&](const CppType & pressure,
502  const CppType & temperature,
503  CppType & new_v,
504  CppType & dv_dp,
505  CppType & dv_dT) { v_from_p_T(pressure, temperature, new_v, dv_dp, dv_dT); };
506  auto e_lambda = [&](const CppType & pressure,
507  const CppType & temperature,
508  CppType & new_e,
509  CppType & de_dp,
510  CppType & de_dT) { e_from_p_T(pressure, temperature, new_e, de_dp, de_dT); };
511  try
512  {
514  v, e, p0, T0, p, T, _tolerance, _tolerance, v_lambda, e_lambda);
515  conversion_succeeded = true;
516  }
517  catch (MooseException &)
518  {
519  conversion_succeeded = false;
520  }
521 
522  if (!conversion_succeeded)
523  mooseDoOnce(mooseWarning("Conversion from (v, e)=(", v, ", ", e, ") to (p, T) failed"));
524 }
525 
526 template <typename T>
527 void
529  const T & h, // e value
530  const Real p0, // initial guess
531  const Real T0, // initial guess
532  T & pressure, // returned pressure
533  T & temperature, // returned temperature
534  bool & conversion_succeeded) const
535 {
536  auto v_lambda = [&](const T & pressure, const T & temperature, T & new_v, T & dv_dp, T & dv_dT)
537  { v_from_p_T(pressure, temperature, new_v, dv_dp, dv_dT); };
538  auto h_lambda = [&](const T & pressure, const T & temperature, T & new_h, T & dh_dp, T & dh_dT)
539  { h_from_p_T(pressure, temperature, new_h, dh_dp, dh_dT); };
540  try
541  {
543  v, h, p0, T0, pressure, temperature, _tolerance, _tolerance, v_lambda, h_lambda);
544  conversion_succeeded = true;
545  }
546  catch (MooseException &)
547  {
548  conversion_succeeded = false;
549  }
550 
551  if (!conversion_succeeded)
552  mooseDoOnce(mooseWarning("Conversion from (v, h)=(", v, ", ", h, ") to (p, T) failed"));
553 }
554 
555 template <typename T>
556 void
558  const T & s, // s value
559  const Real p0, // initial guess
560  const Real T0, // initial guess
561  T & pressure, // returned pressure
562  T & temperature, // returned temperature
563  bool & conversion_succeeded) const
564 {
565  auto h_lambda = [&](const T & pressure, const T & temperature, T & new_h, T & dh_dp, T & dh_dT)
566  { h_from_p_T(pressure, temperature, new_h, dh_dp, dh_dT); };
567  auto s_lambda = [&](const T & pressure, const T & temperature, T & new_s, T & ds_dp, T & ds_dT)
568  { s_from_p_T(pressure, temperature, new_s, ds_dp, ds_dT); };
569  try
570  {
572  h, s, p0, T0, pressure, temperature, _tolerance, _tolerance, h_lambda, s_lambda);
573  conversion_succeeded = true;
574  }
575  catch (MooseException &)
576  {
577  conversion_succeeded = false;
578  }
579 
580  if (!conversion_succeeded)
581  mooseDoOnce(mooseWarning("Conversion from (h, s)=(", h, ", ", s, ") to (p, T) failed"));
582 }
583 
584 template <typename CppType>
585 void
587  const CppType & T,
588  CppType & v,
589  CppType & e) const
590 {
591  const CppType rho = rho_from_p_T(p, T);
592  v = 1.0 / rho;
593  try
594  {
595  // more likely to not involve a Newton search
596  e = e_from_p_T(p, T);
597  }
598  catch (...)
599  {
600  e = e_from_p_rho(p, rho);
601  }
602 }
603 
604 template <typename CppType>
605 void
607  const CppType & T,
608  CppType & v,
609  CppType & dv_dp,
610  CppType & dv_dT,
611  CppType & e,
612  CppType & de_dp,
613  CppType & de_dT) const
614 {
615  CppType rho, drho_dp, drho_dT;
616  rho_from_p_T(p, T, rho, drho_dp, drho_dT);
617 
618  v = 1.0 / rho;
619  const CppType dv_drho = -1.0 / (rho * rho);
620  dv_dp = dv_drho * drho_dp;
621  dv_dT = dv_drho * drho_dT;
622 
623  CppType de_dp_partial, de_drho;
624  e_from_p_rho(p, rho, e, de_dp_partial, de_drho);
625  de_dp = de_dp_partial + de_drho * drho_dp;
626  de_dT = de_drho * drho_dT;
627 }
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:121
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()
DualNumber< Real, DNDerivativeType, true > DualReal
static std::pair< T, T > makeZeroAndOne(const T &)
Given a type example, this method returns zero and unity reperesentations of that type (first and sec...
virtual virtual std Real molarMass() const
Fluid name.
e e e e s T T T T T rho v v T s h T e propfuncWithDefault(gamma, p, T)
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:57
const Number zero
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:120
const Real _tolerance
Newton&#39;s method may be used to convert between variable sets _tolerance, _T_initial_guess, and _p_initial_guess are the parameters for these iterative solves.
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:122
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.
e e e e s T T T T T rho v v T h
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:82
static const std::string pressure
Definition: NS.h:56
void mooseError(Args &&... args) const
virtual Real criticalPressure() const
Critical pressure.
const InputParameters & parameters() const
void fluidPropError(Args... args) const
virtual Real vaporPressure(Real T) const
Vapor pressure.
static const std::string k
Definition: NS.h:124