From 54ec5d3c65160fc36e68f4d6abe219ed131e535c Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Wed, 24 Mar 2021 19:43:24 +0100
Subject: [PATCH] [MPL] Add hasPhase to medium and fluidPhase().

For some processes, like HM, there is no difference between
the gas and liquid phases, a fluid phase is sufficient.
---
 MaterialLib/MPL/Medium.cpp | 21 +++++++++++++++++++++
 MaterialLib/MPL/Medium.h   |  7 +++++++
 2 files changed, 28 insertions(+)

diff --git a/MaterialLib/MPL/Medium.cpp b/MaterialLib/MPL/Medium.cpp
index 94669996766..d0dd4b59e8a 100644
--- a/MaterialLib/MPL/Medium.cpp
+++ b/MaterialLib/MPL/Medium.cpp
@@ -40,6 +40,12 @@ Phase const& Medium::phase(std::string const& name) const
             return phase->name == name;
         },
         "Could not find phase name '" + name + "'.");
+
+bool Medium::hasPhase(std::string const& phase_name) const
+{
+    return std::any_of(
+        begin(phases_), end(phases_),
+        [&phase_name](auto const& phase) { return phase->name == phase_name; });
 }
 
 Property const& Medium::property(PropertyType const& p) const
@@ -67,4 +73,19 @@ std::string Medium::description()
 {
     return "medium";
 }
+
+Phase const& fluidPhase(Medium const& medium)
+{
+    if (medium.hasPhase("Gas"))
+    {
+        return medium.phase("Gas");
+    }
+    if (medium.hasPhase("AqueousLiquid"))
+    {
+        return medium.phase("AqueousLiquid");
+    }
+    OGS_FATAL(
+        "Neither Gas nor AqueousLiquid phase is available for the medium, but "
+        "a fluid phase was requested.");
+}
 }  // namespace MaterialPropertyLib
diff --git a/MaterialLib/MPL/Medium.h b/MaterialLib/MPL/Medium.h
index da4a99cc6cc..bfc0d290b85 100644
--- a/MaterialLib/MPL/Medium.h
+++ b/MaterialLib/MPL/Medium.h
@@ -38,6 +38,10 @@ public:
     Phase const& phase(std::size_t index) const;
     /// A get-function for a particular phase by phase name.
     Phase const& phase(std::string const& phase_name) const;
+
+    /// A query for a named phase.
+    bool hasPhase(std::string const& phase_name) const;
+
     /// A get-function for a property. The argument refers to the name of the
     /// property.
     Property const& property(PropertyType const& p) const;
@@ -107,4 +111,7 @@ void checkRequiredProperties(Medium const& medium,
     }
 }
 
+/// Returns a gas or aqueous liquid phase of the given medium.
+Phase const& fluidPhase(Medium const& medium);
+
 }  // namespace MaterialPropertyLib
-- 
GitLab