OGS
Phase.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Phase.h"
5
6#include "BaseLib/Algorithm.h"
7#include "BaseLib/Error.h"
8#include "Component.h"
10
11namespace MaterialPropertyLib
12{
13std::string_view toString(PhaseName phase_name)
14{
15 switch (phase_name)
16 {
18 return "Solid";
20 return "AqueousLiquid";
21 case PhaseName::Gas:
22 return "Gas";
24 return "FrozenLiquid";
25 }
26 OGS_FATAL("Unknown phase name");
27}
28
29PhaseName fromString(std::string const& phase_name)
30{
31 if (phase_name == "Solid")
32 {
33 return PhaseName::Solid;
34 }
35 if (phase_name == "AqueousLiquid")
36 {
38 }
39 if (phase_name == "Gas")
40 {
41 return PhaseName::Gas;
42 }
43 if (phase_name == "FrozenLiquid")
44 {
46 }
47 OGS_FATAL("Unknown phase name '{}'", phase_name);
48}
49
51 std::vector<std::unique_ptr<Component>>&& components,
52 std::unique_ptr<PropertyArray>&& properties)
53 : phaseName(phase_name), components_(std::move(components))
54{
55 if (properties)
56 {
57 overwriteExistingProperties(properties_, *properties, this);
58 }
59}
60
61Component const& Phase::component(const std::size_t& index) const
62{
63 return *components_[index];
64}
65
66bool Phase::hasComponent(std::size_t const& index) const
67{
68 return components_[index] != nullptr;
69}
70
71Component const& Phase::component(std::string const& name) const
72{
75 [&name](
76 std::unique_ptr<MaterialPropertyLib::Component> const& component)
77 { return component->name == name; },
78 [&]() { OGS_FATAL("Could not find component named '{:s}'.", name); });
79}
80
82{
83 Property const* const property = properties_[p].get();
84 if (property == nullptr)
85 {
86 OGS_FATAL("Trying to access undefined property '{:s}' of {:s}",
88 }
89 return *properties_[p];
90}
91
93{
94 return property(p);
95}
96
97bool Phase::hasProperty(PropertyType const& p) const
98{
99 return properties_[p] != nullptr;
100}
101
102std::size_t Phase::numberOfComponents() const
103{
104 return components_.size();
105}
106
107std::string Phase::description() const
108{
109 return "phase '" + std::string(toString(phaseName)) + "'";
110}
111
113 Phase const& phase, std::span<PropertyType const> const required_properties)
114{
115 for (auto const& p : required_properties)
116 {
117 if (!phase.hasProperty(p))
118 {
119 OGS_FATAL("The property '{:s}' is missing in the {:s} phase.",
121 }
122 }
123}
124
125} // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition Error.h:19
This class defines components (substances).
Definition Component.h:18
Property const & property(PropertyType const &p) const
Definition Phase.cpp:81
PropertyArray properties_
Definition Phase.h:77
std::string description() const
Short description of the phase with its name.
Definition Phase.cpp:107
std::size_t numberOfComponents() const
A get-function for retrieving the number of components in this phase.
Definition Phase.cpp:102
std::vector< std::unique_ptr< Component > > const components_
Definition Phase.h:70
Property const & operator[](PropertyType const &p) const
Definition Phase.cpp:92
bool hasProperty(PropertyType const &p) const
Definition Phase.cpp:97
bool hasComponent(std::size_t const &index) const
Definition Phase.cpp:66
PhaseName const phaseName
Definition Phase.h:67
Component const & component(std::size_t const &index) const
Definition Phase.cpp:61
Phase(PhaseName phase_name, std::vector< std::unique_ptr< Component > > &&components, std::unique_ptr< PropertyArray > &&properties)
The Phase constructor is called with the phase type enum.
Definition Phase.cpp:50
ranges::range_reference_t< Range > findElementOrError(Range &range, std::predicate< ranges::range_reference_t< Range > > auto &&predicate, std::invocable auto error_callback)
Definition Algorithm.h:74
void overwriteExistingProperties(PropertyArray &properties, PropertyArray &new_properties, std::variant< Medium *, Phase *, Component * > scale_pointer)
PhaseName fromString(std::string const &phase_name)
Convert string to phase enum. Throws if invalid phase name.
Definition Phase.cpp:29
void checkRequiredProperties(Component const &c, std::span< PropertyType const > const required_properties)
Definition Component.cpp:51
PhaseName
Enumeration of phase types.
Definition Phase.h:19
static const std::array< std::string, PropertyType::number_of_properties > property_enum_to_string
std::string_view toString(PhaseName phase_name)
Convert phase enum to its string representation.
Definition Phase.cpp:13