OGS
Medium.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 "Medium.h"
5
6#include "BaseLib/Algorithm.h"
7#include "BaseLib/Error.h"
9
10namespace MaterialPropertyLib
11{
12Medium::Medium(int const material_id,
13 std::vector<std::unique_ptr<Phase>>&& phases,
14 std::unique_ptr<PropertyArray>&& properties)
15 : phases_(std::move(phases)), material_id_(material_id)
16{
17 if (properties)
18 {
19 overwriteExistingProperties(properties_, *properties, this);
20 }
22}
23
24Phase const& Medium::phase(std::size_t const index) const
25{
26 return *phases_[index];
27}
28
29Phase const& Medium::phase(PhaseName phase_name) const
30{
32 phases_,
33 [phase_name](std::unique_ptr<MaterialPropertyLib::Phase> const& phase)
34 { return phase->phaseName == phase_name; },
35 [phase_name]()
36 {
37 OGS_FATAL("Could not find phase named '{:s}.'",
38 toString(phase_name));
39 });
40}
41
42bool Medium::hasPhase(PhaseName phase_name) const
43{
44 return std::any_of(begin(phases_), end(phases_),
45 [phase_name](auto const& phase)
46 { return phase->phaseName == phase_name; });
47}
48
50{
51 Property const* const property = properties_[p].get();
52 if (property == nullptr)
53 {
54 OGS_FATAL("Trying to access undefined property '{:s}' of {:s}",
56 }
57 return *properties_[p];
58}
59
61{
62 return property(p);
63}
64
66{
67 return properties_[p] != nullptr;
68}
69
70std::size_t Medium::numberOfPhases() const
71{
72 return phases_.size();
73}
74
75std::string Medium::description() const
76{
77 return "medium " + std::to_string(material_id_);
78}
79
81 Medium const& medium,
82 std::span<PropertyType const> const required_properties)
83{
84 for (auto const& p : required_properties)
85 {
86 if (!medium.hasProperty(p))
87 {
89 "The property '{:s}' is missing in the medium definition.",
91 }
92 }
93}
94
95Phase const& fluidPhase(Medium const& medium)
96{
97 if (medium.hasPhase(PhaseName::Gas))
98 {
99 return medium.phase(PhaseName::Gas);
100 }
102 {
103 return medium.phase(PhaseName::AqueousLiquid);
104 }
105 OGS_FATAL(
106 "Neither Gas nor AqueousLiquid phase is available for the medium, but "
107 "a fluid phase was requested.");
108}
109
110Phase const* getOptionalPhase(Medium const& medium, PhaseName phase_name)
111{
112 return medium.hasPhase(phase_name) ? &medium.phase(phase_name) : nullptr;
113}
114} // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition Error.h:19
std::size_t numberOfPhases() const
Definition Medium.cpp:70
Phase const & phase(std::size_t index) const
Definition Medium.cpp:24
bool hasPhase(PhaseName phase_name) const
A query for a particular phase type.
Definition Medium.cpp:42
Medium(int const material_id, std::vector< std::unique_ptr< Phase > > &&phases, std::unique_ptr< PropertyArray > &&properties)
Definition Medium.cpp:12
std::vector< std::unique_ptr< Phase > > const phases_
The vector that holds the phases.
Definition Medium.h:86
PropertyArray properties_
Definition Medium.h:93
Property const & property(PropertyType const &p) const
Definition Medium.cpp:49
bool hasProperty(PropertyType const &p) const
Definition Medium.cpp:65
Property const & operator[](PropertyType const &p) const
Definition Medium.cpp:60
std::string description() const
Short description of the medium.
Definition Medium.cpp:75
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)
void checkRequiredProperties(Component const &c, std::span< PropertyType const > const required_properties)
Definition Component.cpp:51
void updatePropertiesForAllPhases(PropertyArray &properties, std::vector< std::unique_ptr< Phase > > const &phases)
PhaseName
Enumeration of phase types.
Definition Phase.h:19
static const std::array< std::string, PropertyType::number_of_properties > property_enum_to_string
Phase const * getOptionalPhase(Medium const &medium, PhaseName phase_name)
Definition Medium.cpp:110
Phase const & fluidPhase(Medium const &medium)
Returns a gas or aqueous liquid phase of the given medium.
Definition Medium.cpp:95
std::string_view toString(PhaseName phase_name)
Convert phase enum to its string representation.
Definition Phase.cpp:13