OGS
MaterialLib Namespace Reference

Namespaces

namespace  Fluid
namespace  Fracture
namespace  PhysicalConstant
namespace  Solids

Functions

void checkForWhitespaces (std::string_view const part)
auto expandRange (int const start, int const end)
std::vector< int > splitMaterialIdString (std::string const &material_id_string, char const separator)
std::vector< int > parseMaterialIdString (std::string const &material_id_string, MeshLib::PropertyVector< int > const *const material_ids, char const separator, bool const validate)
template<typename T, typename CreateMedium>
requires std::convertible_to< decltype(std::declval<CreateMedium>()(std::declval<int>())), std::shared_ptr<T>>
void createMediumForId (int const id, std::map< int, std::shared_ptr< T > > &media, std::vector< int > const &material_ids_of_this_medium, CreateMedium &&create_medium)

Function Documentation

◆ checkForWhitespaces()

void MaterialLib::checkForWhitespaces ( std::string_view const part)

Checks that a string part contains no whitespace. Throws OGS_FATAL if any whitespace is found.

Definition at line 22 of file MediaCreation.cpp.

23{
24 if (std::ranges::any_of(
25 part,
26 [](char c) { return std::isspace(static_cast<unsigned char>(c)); }))
27 {
29 "Whitespace is not allowed in ranges. Use 'start:end' without "
30 "spaces around the colon.");
31 }
32}
#define OGS_FATAL(...)
Definition Error.h:10

References OGS_FATAL.

Referenced by splitMaterialIdString().

◆ createMediumForId()

template<typename T, typename CreateMedium>
requires std::convertible_to< decltype(std::declval<CreateMedium>()(std::declval<int>())), std::shared_ptr<T>>
void MaterialLib::createMediumForId ( int const id,
std::map< int, std::shared_ptr< T > > & media,
std::vector< int > const & material_ids_of_this_medium,
CreateMedium && create_medium )

Creates a new entry for the material id in the media map by either calling the create_medium function and creating a new shared pointer, or by reusing the existing shared pointer.

Definition at line 51 of file MediaCreation.h.

55{
56 if (media.find(id) != end(media))
57 {
59 "Multiple media were specified for the same material id '{:d}'. "
60 "Keep in mind, that if no material id is specified, it is assumed "
61 "to be 0 by default.",
62 id);
63 }
64
65 if (id == material_ids_of_this_medium[0])
66 {
67 media[id] = create_medium(id);
68 }
69 else
70 {
71 media[id] = media[material_ids_of_this_medium[0]];
72 }
73}

References OGS_FATAL.

Referenced by MaterialLib::Solids::createConstitutiveRelationsGeneric(), and ProjectData::parseMedia().

◆ expandRange()

auto MaterialLib::expandRange ( int const start,
int const end )

Creates a range of integers from start to end (inclusive). Throws OGS_FATAL if end < start.

Definition at line 36 of file MediaCreation.cpp.

37{
38 if (end < start)
39 {
41 "Invalid range '{}:{}'. The end must be greater than or equal "
42 "to the start.",
43 start, end);
44 }
45 return ranges::views::iota(start, end + 1);
46}

References OGS_FATAL.

Referenced by splitMaterialIdString().

◆ parseMaterialIdString()

std::vector< int > MaterialLib::parseMaterialIdString ( std::string const & material_id_string,
MeshLib::PropertyVector< int > const *const material_ids,
char const separator = ',',
bool const validate = false )

Parses a comma separated list of integers or "*" string. Such lists occur in the medium definition in the OGS prj file. Range syntax is supported with colon separator, e.g., "1:5" expands to "1,2,3,4,5". For the "*" input a vector of all (unique) material ids is returned. Error messages in this function refer to this specific purpose. If validate is true and material_ids is not null, every parsed id must be present in material_ids, otherwise the function fails. Validation is opt-in so that existing callers (media, constitutive relations) keep tolerating ids that are absent from the current mesh, e.g. on a PETSc partition or a project file shared across several meshes.

Definition at line 110 of file MediaCreation.cpp.

114{
115 if (material_id_string == "*")
116 {
117 if (material_ids == nullptr)
118 {
119 OGS_FATAL(
120 "MaterialIDs property is not defined in the mesh but it is "
121 "required to parse '*' definition.");
122 }
123
124 std::vector<int> const material_ids_of_this_medium =
125 *material_ids | ranges::views::unique | ranges::to_vector |
126 ranges::actions::sort | ranges::actions::unique |
127 ranges::to<std::vector>;
128 DBUG("Catch all medium definition for material ids {}.",
129 fmt::join(material_ids_of_this_medium, ", "));
130 return material_ids_of_this_medium;
131 }
132
133 // Usual case of ids or ranges separated by comma.
134 auto const parsed_ids =
135 splitMaterialIdString(material_id_string, separator);
136
137 if (material_ids == nullptr || !validate)
138 {
139 return parsed_ids;
140 }
141
142 // Create a set of valid material IDs for efficient lookup
143 std::set<int> const valid_material_ids(material_ids->begin(),
144 material_ids->end());
145
146 for (int id : parsed_ids)
147 {
148 if (valid_material_ids.find(id) == valid_material_ids.end())
149 {
150 OGS_FATAL(
151 "Material ID {} specified in material_id_string '{}' does "
152 "not exist in the mesh. Available material IDs are: {}.",
153 id, material_id_string, fmt::join(valid_material_ids, ", "));
154 }
155 }
156 return parsed_ids;
157}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
constexpr PROP_VAL_TYPE * begin()
constexpr PROP_VAL_TYPE * end()
std::vector< int > splitMaterialIdString(std::string const &material_id_string, char const separator)

References MeshLib::PropertyVector< PROP_VAL_TYPE >::begin(), DBUG(), MeshLib::PropertyVector< PROP_VAL_TYPE >::end(), OGS_FATAL, and splitMaterialIdString().

Referenced by MaterialLib::Solids::createConstitutiveRelationsGeneric(), ProjectData::parseMedia(), and parseOverwriteMeshData().

◆ splitMaterialIdString()

std::vector< int > MaterialLib::splitMaterialIdString ( std::string const & material_id_string,
char const separator = ',' )

Parses a comma separated list of integers and/or ranges. Such lists occur in the medium definition in the OGS prj file. Range syntax is supported with colon separator, e.g., "1:5" expands to "1,2,3,4,5". Multiple entries are separated by comma, e.g., "-1:2,5,7:9" expands to "-1,0,1,2,5,7,8,9". Error messages in this function refer to this specific purpose.

Definition at line 48 of file MediaCreation.cpp.

50{
51 if (separator == ':')
52 {
54 "Invalid separator ':'. Character ':' is reserved for range "
55 "specification.");
56 }
57 auto const material_ids_strings =
58 BaseLib::splitString(material_id_string, separator);
59
60 // Pre-allocate with estimated capacity (simplified heuristic)
61 std::vector<int> material_ids;
62 material_ids.reserve(material_ids_strings.size());
63
64 for (std::string mid_str : material_ids_strings)
65 {
66 // Trim leading and trailing whitespace
67 BaseLib::trim(mid_str);
68
69 auto const parts =
70 BaseLib::splitString(mid_str, ':') | ranges::to_vector;
71 if (parts.size() == 2)
72 {
73 checkForWhitespaces(parts[0]);
74 auto const start_id = BaseLib::parseInteger(parts[0]);
75 if (!start_id)
76 {
77 OGS_FATAL("Could not parse material ID: {}", start_id.error());
78 }
79 checkForWhitespaces(parts[1]);
80 auto const end_id = BaseLib::parseInteger(parts[1]);
81 if (!end_id)
82 {
83 OGS_FATAL("Could not parse material ID: {}", end_id.error());
84 }
85 ranges::copy(expandRange(*start_id, *end_id),
86 std::back_inserter(material_ids));
87 }
88 else if (parts.size() == 1)
89 {
90 auto const material_id = BaseLib::parseInteger(mid_str);
91 if (!material_id)
92 {
93 OGS_FATAL("Could not parse material ID: {}",
94 material_id.error());
95 }
96 material_ids.push_back(*material_id);
97 }
98 else
99 {
100 OGS_FATAL(
101 "Could not parse material ID from '{}'. Invalid range format. "
102 "Use 'start:end' for ranges or a single integer.",
103 mid_str);
104 }
105 }
106
107 return material_ids;
108}
void trim(std::string &str, char ch)
std::expected< int, std::string > parseInteger(std::string_view const str)
std::vector< std::string > splitString(std::string const &str)
auto expandRange(int const start, int const end)
void checkForWhitespaces(std::string_view const part)

References checkForWhitespaces(), expandRange(), OGS_FATAL, BaseLib::parseInteger(), BaseLib::splitString(), and BaseLib::trim().

Referenced by ProcessLib::HeatTransportBHE::createHeatTransportBHEProcess(), parseMaterialIdString(), and parseOutputMeshConfig().