OGS
MaterialLib Namespace Reference

Namespaces

namespace  Fluid
namespace  Fracture
namespace  PhysicalConstant
namespace  Solids

Functions

void checkForWhitespaces (std::string_view part)
auto expandRange (int start, int end)
std::vector< int > splitMaterialIdString (std::string const &material_id_string)
std::vector< int > parseMaterialIdString (std::string const &material_id_string, MeshLib::PropertyVector< int > const *const material_ids)
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 part)

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

Definition at line 21 of file MediaCreation.cpp.

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

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 44 of file MediaCreation.h.

48{
49 if (media.find(id) != end(media))
50 {
52 "Multiple media were specified for the same material id '{:d}'. "
53 "Keep in mind, that if no material id is specified, it is assumed "
54 "to be 0 by default.",
55 id);
56 }
57
58 if (id == material_ids_of_this_medium[0])
59 {
60 media[id] = create_medium(id);
61 }
62 else
63 {
64 media[id] = media[material_ids_of_this_medium[0]];
65 }
66}

References OGS_FATAL.

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

◆ expandRange()

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

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

Definition at line 35 of file MediaCreation.cpp.

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

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 )

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.

Definition at line 102 of file MediaCreation.cpp.

105{
106 if (material_id_string == "*")
107 {
108 if (material_ids == nullptr)
109 {
110 OGS_FATAL(
111 "MaterialIDs property is not defined in the mesh but it is "
112 "required to parse '*' definition.");
113 }
114
115 std::vector<int> const material_ids_of_this_medium =
116 *material_ids | ranges::views::unique | ranges::to_vector |
117 ranges::actions::sort | ranges::actions::unique |
118 ranges::to<std::vector>;
119 DBUG("Catch all medium definition for material ids {}.",
120 fmt::join(material_ids_of_this_medium, ", "));
121 return material_ids_of_this_medium;
122 }
123
124 // Usual case of ids or ranges separated by comma.
125 return splitMaterialIdString(material_id_string);
126}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
std::vector< int > splitMaterialIdString(std::string const &material_id_string)

References DBUG(), OGS_FATAL, and splitMaterialIdString().

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

◆ splitMaterialIdString()

std::vector< int > MaterialLib::splitMaterialIdString ( std::string const & material_id_string)

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 47 of file MediaCreation.cpp.

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

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

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