From 8526548679ef1c93c724c5fb6f04d2601632d0a8 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <dmitri.naumov@ufz.de>
Date: Thu, 4 Apr 2024 18:36:14 +0200
Subject: [PATCH] [PL/TH2M] Replace xmWG with 1 - xmCG

Reducing storage a little more.
---
 .../NoPhaseTransition.cpp                     |  3 +-
 .../ConstitutiveRelations/PhaseTransition.cpp | 17 +++++-----
 .../PhaseTransitionData.h                     |  3 --
 ProcessLib/TH2M/TH2MFEM-impl.h                | 11 ++++---
 .../TH2M/TestTH2MNoPhaseTransition.cpp        |  3 +-
 .../TH2M/TestTH2MPhaseTransition.cpp          | 32 +++++++++----------
 6 files changed, 32 insertions(+), 37 deletions(-)

diff --git a/ProcessLib/TH2M/ConstitutiveRelations/NoPhaseTransition.cpp b/ProcessLib/TH2M/ConstitutiveRelations/NoPhaseTransition.cpp
index 3a98aeab783..43f2212b006 100644
--- a/ProcessLib/TH2M/ConstitutiveRelations/NoPhaseTransition.cpp
+++ b/ProcessLib/TH2M/ConstitutiveRelations/NoPhaseTransition.cpp
@@ -66,12 +66,11 @@ void NoPhaseTransition::eval(SpaceTimeData const& x_t,
     vapour_pressure_data.pWGR = 0;
 
     // C-component is only component in the gas phase
-    cv.xmWG = 0.;
     cv.dxmWG_dpGR = 0.;
     cv.dxmWG_dpCap = 0.;
     cv.dxmWG_dT = 0.;
     mass_mole_fractions_data.xnCG = 1.;
-    mass_mole_fractions_data.xmCG = 1. - cv.xmWG;
+    mass_mole_fractions_data.xmCG = 1.;
 
     auto const M =
         gas_phase.property(MaterialPropertyLib::PropertyType::molar_mass)
diff --git a/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransition.cpp b/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransition.cpp
index 59901703477..748fd9cca98 100644
--- a/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransition.cpp
+++ b/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransition.cpp
@@ -247,8 +247,8 @@ void PhaseTransition::eval(SpaceTimeData const& x_t,
     variables.molar_mass = MG;
 
     // gas phase mass fractions
-    cv.xmWG = xnWG * M_W / MG;
-    mass_mole_fractions_data.xmCG = 1. - cv.xmWG;
+    double const xmWG = xnWG * M_W / MG;
+    mass_mole_fractions_data.xmCG = 1. - xmWG;
 
     auto const dxn_dxm_conversion = M_W * M_C / MG / MG;
     // gas phase mass fraction derivatives
@@ -302,7 +302,7 @@ void PhaseTransition::eval(SpaceTimeData const& x_t,
     // model is still consistent.
     constituent_density_data.rho_C_GR =
         mass_mole_fractions_data.xmCG * fluid_density_data.rho_GR;
-    constituent_density_data.rho_W_GR = cv.xmWG * fluid_density_data.rho_GR;
+    constituent_density_data.rho_W_GR = xmWG * fluid_density_data.rho_GR;
 
     // 'Air'-component partial density derivatives
     cv.drho_C_GR_dp_GR = mass_mole_fractions_data.xmCG * cv.drho_GR_dp_GR -
@@ -314,11 +314,11 @@ void PhaseTransition::eval(SpaceTimeData const& x_t,
 
     // Vapour-component partial density derivatives
     cv.drho_W_GR_dp_GR =
-        cv.xmWG * cv.drho_GR_dp_GR + cv.dxmWG_dpGR * fluid_density_data.rho_GR;
-    cv.drho_W_GR_dp_cap = cv.xmWG * cv.drho_GR_dp_cap +
-                          cv.dxmWG_dpCap * fluid_density_data.rho_GR;
+        xmWG * cv.drho_GR_dp_GR + cv.dxmWG_dpGR * fluid_density_data.rho_GR;
+    cv.drho_W_GR_dp_cap =
+        xmWG * cv.drho_GR_dp_cap + cv.dxmWG_dpCap * fluid_density_data.rho_GR;
     cv.drho_W_GR_dT =
-        cv.xmWG * cv.drho_GR_dT + cv.dxmWG_dT * fluid_density_data.rho_GR;
+        xmWG * cv.drho_GR_dT + cv.dxmWG_dT * fluid_density_data.rho_GR;
 
     // specific heat capacities of dry air and vapour
     auto const cpCG =
@@ -335,8 +335,7 @@ void PhaseTransition::eval(SpaceTimeData const& x_t,
     cv.hWG = cpWG * T + dh_evap;
 
     // specific enthalpy of gas phase
-    enthalpy_data.h_G =
-        mass_mole_fractions_data.xmCG * cv.hCG + cv.xmWG * cv.hWG;
+    enthalpy_data.h_G = mass_mole_fractions_data.xmCG * cv.hCG + xmWG * cv.hWG;
     cv.dh_G_dT = 0;
 
     // specific inner energies of gas phase
diff --git a/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransitionData.h b/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransitionData.h
index 98cd682b4ee..a0042c6afbb 100644
--- a/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransitionData.h
+++ b/ProcessLib/TH2M/ConstitutiveRelations/PhaseTransitionData.h
@@ -43,9 +43,6 @@ struct PhaseTransitionData
     double drho_W_LR_dT = nan;
     double drho_W_LR_dp_LR = nan;
 
-    // constituent mass and molar fractions
-    double xmWG = nan;
-
     // mass fraction derivatives
     double dxmWG_dpGR = nan;
     double dxmWG_dpCap = nan;
diff --git a/ProcessLib/TH2M/TH2MFEM-impl.h b/ProcessLib/TH2M/TH2MFEM-impl.h
index e7e6336c4d7..edad8484191 100644
--- a/ProcessLib/TH2M/TH2MFEM-impl.h
+++ b/ProcessLib/TH2M/TH2MFEM-impl.h
@@ -295,11 +295,12 @@ TH2MLocalAssembler<ShapeFunctionDisplacement, ShapeFunctionPressure,
                            : -phi_G / ip_out.mass_mole_fractions_data.xmCG *
                                  c.diffusion_coefficient_vapour * gradxmCG;
 
-        ip_data.d_WG =
-            c.xmWG == 0.
-                ? 0. * gradxmWG  // Keep d_WG's dimension and prevent
-                                 // division by zero
-                : -phi_G / c.xmWG * c.diffusion_coefficient_vapour * gradxmWG;
+        ip_data.d_WG = ip_out.mass_mole_fractions_data.xmCG == 1.
+                           ? 0. * gradxmWG  // Keep d_WG's dimension and prevent
+                                            // division by zero
+                           : -phi_G /
+                                 (1 - ip_out.mass_mole_fractions_data.xmCG) *
+                                 c.diffusion_coefficient_vapour * gradxmWG;
 
         ip_data.d_CL =
             xmCL == 0.
diff --git a/Tests/ProcessLib/TH2M/TestTH2MNoPhaseTransition.cpp b/Tests/ProcessLib/TH2M/TestTH2MNoPhaseTransition.cpp
index 52b49cd7a02..a11601304c2 100644
--- a/Tests/ProcessLib/TH2M/TestTH2MNoPhaseTransition.cpp
+++ b/Tests/ProcessLib/TH2M/TestTH2MNoPhaseTransition.cpp
@@ -149,9 +149,8 @@ TEST(ProcessLib, TH2MNoPhaseTransition)
     ASSERT_NEAR(rhoCGR, constituent_density.rho_C_GR, 1.0e-10);
     ASSERT_NEAR(rhoWGR, constituent_density.rho_W_GR, 1.0e-10);
     ASSERT_NEAR(rhoCLR, constituent_density.rho_C_LR, 1.0e-10);
-    ASSERT_NEAR(xmCG, 1. - cv.xmWG, 1.e-10);
     ASSERT_NEAR(xmCG, mass_mole_fractions.xmCG, 1.e-10);
-    ASSERT_NEAR(xmWG, cv.xmWG, 1.e-10);
+    ASSERT_NEAR(xmWG, 1 - mass_mole_fractions.xmCG, 1.e-10);
     ASSERT_NEAR(dxmWG_dpGR, cv.dxmWG_dpGR, 1.0e-10);
     ASSERT_NEAR(dxmCG_dpGR, -cv.dxmWG_dpGR, 1.0e-10);
     ASSERT_NEAR(dxmWG_dT, cv.dxmWG_dT, 1.0e-10);
diff --git a/Tests/ProcessLib/TH2M/TestTH2MPhaseTransition.cpp b/Tests/ProcessLib/TH2M/TestTH2MPhaseTransition.cpp
index e4a4ed4d45e..3bdf25f935f 100644
--- a/Tests/ProcessLib/TH2M/TestTH2MPhaseTransition.cpp
+++ b/Tests/ProcessLib/TH2M/TestTH2MPhaseTransition.cpp
@@ -250,7 +250,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        auto xmWG_plus = cv.xmWG;
+        auto xmWG_plus = 1 - mass_mole_fractions.xmCG;
         auto rhoGR_plus = fluid_density.rho_GR;
         auto rhoCGR_plus = constituent_density.rho_C_GR;
         auto rhoWGR_plus = constituent_density.rho_W_GR;
@@ -262,7 +262,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        auto xmWG_minus = cv.xmWG;
+        auto xmWG_minus = 1 - mass_mole_fractions.xmCG;
         auto rhoGR_minus = fluid_density.rho_GR;
         auto rhoCGR_minus = constituent_density.rho_C_GR;
         auto rhoWGR_minus = constituent_density.rho_W_GR;
@@ -300,7 +300,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   viscosity, enthalpy, mass_mole_fractions, fluid_density,
                   vapour_pressure, constituent_density, cv);
 
-        xmWG_plus = cv.xmWG;
+        xmWG_plus = 1 - mass_mole_fractions.xmCG;
         rhoGR_plus = fluid_density.rho_GR;
         rhoCGR_plus = constituent_density.rho_C_GR;
         rhoWGR_plus = constituent_density.rho_W_GR;
@@ -313,7 +313,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   viscosity, enthalpy, mass_mole_fractions, fluid_density,
                   vapour_pressure, constituent_density, cv);
 
-        xmWG_minus = cv.xmWG;
+        xmWG_minus = 1 - mass_mole_fractions.xmCG;
         rhoGR_minus = fluid_density.rho_GR;
         rhoCGR_minus = constituent_density.rho_C_GR;
         rhoWGR_minus = constituent_density.rho_W_GR;
@@ -352,7 +352,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   enthalpy, mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        xmWG_plus = cv.xmWG;
+        xmWG_plus = 1 - mass_mole_fractions.xmCG;
         rhoGR_plus = fluid_density.rho_GR;
         rhoCGR_plus = constituent_density.rho_C_GR;
         rhoWGR_plus = constituent_density.rho_W_GR;
@@ -364,7 +364,7 @@ TEST(ProcessLib, TH2MPhaseTransition)
                   enthalpy, mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        xmWG_minus = cv.xmWG;
+        xmWG_minus = 1 - mass_mole_fractions.xmCG;
         rhoGR_minus = fluid_density.rho_GR;
         rhoCGR_minus = constituent_density.rho_C_GR;
         rhoWGR_minus = constituent_density.rho_W_GR;
@@ -404,12 +404,12 @@ TEST(ProcessLib, TH2MPhaseTransition)
         // must be equal to the mass fraction of those constituents in both
         // phases.
         ASSERT_NEAR(constituent_density.rho_W_GR / fluid_density.rho_GR,
-                    cv.xmWG, 1.e-10);
+                    1 - mass_mole_fractions.xmCG, 1.e-10);
         ASSERT_NEAR(rhoWLR() / fluid_density.rho_LR, mass_mole_fractions.xmWL,
                     1.e-10);
 
         ASSERT_NEAR(constituent_density.rho_C_GR / fluid_density.rho_GR,
-                    1. - cv.xmWG, 1.e-10);
+                    mass_mole_fractions.xmCG, 1.e-10);
         ASSERT_NEAR(constituent_density.rho_C_LR / fluid_density.rho_LR,
                     1. - mass_mole_fractions.xmWL, 1.e-10);
 
@@ -518,7 +518,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        auto xmWG_plus = cv.xmWG;
+        auto xmWG_plus = 1 - mass_mole_fractions.xmCG;
         auto rhoCGR_plus = constituent_density.rho_C_GR;
         auto rhoWGR_plus = constituent_density.rho_W_GR;
 
@@ -529,7 +529,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        auto xmWG_minus = cv.xmWG;
+        auto xmWG_minus = 1 - mass_mole_fractions.xmCG;
         auto rhoCGR_minus = constituent_density.rho_C_GR;
         auto rhoWGR_minus = constituent_density.rho_W_GR;
 
@@ -565,7 +565,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   viscosity, enthalpy, mass_mole_fractions, fluid_density,
                   vapour_pressure, constituent_density, cv);
 
-        xmWG_plus = cv.xmWG;
+        xmWG_plus = 1 - mass_mole_fractions.xmCG;
         rhoCGR_plus = constituent_density.rho_C_GR;
         rhoWGR_plus = constituent_density.rho_W_GR;
 
@@ -577,7 +577,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   viscosity, enthalpy, mass_mole_fractions, fluid_density,
                   vapour_pressure, constituent_density, cv);
 
-        xmWG_minus = cv.xmWG;
+        xmWG_minus = 1 - mass_mole_fractions.xmCG;
         rhoCGR_minus = constituent_density.rho_C_GR;
         rhoWGR_minus = constituent_density.rho_W_GR;
 
@@ -614,7 +614,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   enthalpy, mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        xmWG_plus = cv.xmWG;
+        xmWG_plus = 1 - mass_mole_fractions.xmCG;
         rhoCGR_plus = constituent_density.rho_C_GR;
         rhoWGR_plus = constituent_density.rho_W_GR;
 
@@ -625,7 +625,7 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
                   enthalpy, mass_mole_fractions, fluid_density, vapour_pressure,
                   constituent_density, cv);
 
-        xmWG_minus = cv.xmWG;
+        xmWG_minus = 1 - mass_mole_fractions.xmCG;
         rhoCGR_minus = constituent_density.rho_C_GR;
         rhoWGR_minus = constituent_density.rho_W_GR;
 
@@ -663,12 +663,12 @@ TEST(ProcessLib, TH2MPhaseTransitionConstRho)
         // must be equal to the mass fraction of those constituents in both
         // phases.
         ASSERT_NEAR(constituent_density.rho_W_GR / fluid_density.rho_GR,
-                    cv.xmWG, 1.e-10);
+                    1 - mass_mole_fractions.xmCG, 1.e-10);
         ASSERT_NEAR(rhoWLR() / fluid_density.rho_LR, mass_mole_fractions.xmWL,
                     1.e-10);
 
         ASSERT_NEAR(constituent_density.rho_C_GR / fluid_density.rho_GR,
-                    1. - cv.xmWG, 1.e-10);
+                    mass_mole_fractions.xmCG, 1.e-10);
         ASSERT_NEAR(constituent_density.rho_C_LR / fluid_density.rho_LR,
                     1. - mass_mole_fractions.xmWL, 1.e-10);
 
-- 
GitLab