Skip to content

Commit 6151fb1

Browse files
committed
Use match_section if possible
1 parent 118e694 commit 6151fb1

File tree

4 files changed

+63
-59
lines changed

4 files changed

+63
-59
lines changed

src/core/fem/src/condition/4C_fem_condition_definition.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,27 @@ void Core::Conditions::ConditionDefinition::read(Core::IO::InputFile& input,
6464
all_of(specs_),
6565
});
6666

67-
for (const auto& fragment : input.in_section(section_name()))
67+
Core::IO::InputParameterContainer container;
68+
try
6869
{
69-
std::optional<Core::IO::InputParameterContainer> data;
70-
try
71-
{
72-
data = fragment.match(condition_spec);
73-
}
74-
catch (const Core::Exception& e)
75-
{
76-
FOUR_C_THROW("Failed to match condition specification in section '{}'. The error was:\n{}.",
77-
section_name().c_str(), e.what());
78-
}
79-
if (!data)
80-
{
81-
FOUR_C_THROW(
82-
"Failed to match condition specification in section '{}'.", sectionname_.c_str());
83-
}
70+
input.match_section(section_name(), container);
71+
}
72+
catch (const Core::Exception& e)
73+
{
74+
FOUR_C_THROW("Failed to match condition specification in section '{}'. The error was:\n{}.",
75+
section_name(), e.what());
76+
}
8477

78+
79+
for (const auto& condition_data :
80+
container.get_or<std::vector<Core::IO::InputParameterContainer>>(section_name(), {}))
81+
{
8582
// Read a one-based condition number but convert it to zero-based for internal use.
86-
const int dobjid = data->get<int>("E") - 1;
83+
const int dobjid = condition_data.get<int>("E") - 1;
8784

8885
std::shared_ptr<Core::Conditions::Condition> condition =
8986
std::make_shared<Core::Conditions::Condition>(dobjid, condtype_, buildgeometry_, gtype_);
90-
condition->parameters() = *std::move(data);
87+
condition->parameters() = condition_data;
9188

9289
//------------------------------- put condition in map of conditions
9390
cmap.insert(std::pair<int, std::shared_ptr<Core::Conditions::Condition>>(dobjid, condition));

src/core/io/src/4C_io_input_file.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ namespace Core::IO
309309
std::all_of(section_name.begin() + 5, section_name.end(),
310310
[](const char c) { return std::isdigit(c); });
311311
}
312+
313+
bool is_legacy_section(const std::string& section_name) const
314+
{
315+
return std::ranges::any_of(
316+
legacy_section_names_, [&](const auto& name) { return name == section_name; });
317+
}
312318
};
313319

314320
} // namespace Internal
@@ -878,6 +884,11 @@ namespace Core::IO
878884
InputFile::FragmentIteratorRange InputFile::in_section_rank_0_only(
879885
const std::string& section_name) const
880886
{
887+
FOUR_C_ASSERT_ALWAYS(pimpl_->is_legacy_section(section_name),
888+
"You tried to process section '{}' on rank 0 only, but this feature is meant for special "
889+
"legacy sections. Please use match_section() instead.",
890+
section_name);
891+
881892
if (Core::Communication::my_mpi_rank(pimpl_->comm_) == 0 &&
882893
pimpl_->content_by_section_.contains(section_name))
883894
{

src/core/io/src/4C_io_input_parameter_container.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <map>
2222
#include <optional>
2323
#include <ostream>
24+
#include <ranges>
2425
#include <string>
2526
#include <typeindex>
2627
#include <vector>
@@ -50,6 +51,8 @@ namespace Core::IO
5051
*/
5152
using List = std::vector<InputParameterContainer>;
5253

54+
using GroupStorage = std::map<std::string, InputParameterContainer>;
55+
5356
/**
5457
* \brief Add @data to the container at the given key @name.
5558
*
@@ -68,6 +71,11 @@ namespace Core::IO
6871
*/
6972
[[nodiscard]] const InputParameterContainer& group(const std::string& name) const;
7073

74+
/**
75+
* Get a range view of all groups in the container.
76+
*/
77+
[[nodiscard]] auto groups() const;
78+
7179
/**
7280
* Check if a group with the name @p name exists.
7381
*/
@@ -266,6 +274,11 @@ namespace Core::IO::Internal::InputParameterContainerImplementation
266274
} // namespace Core::IO::Internal::InputParameterContainerImplementation
267275

268276

277+
inline auto Core::IO::InputParameterContainer::groups() const
278+
{
279+
return std::ranges::subrange(groups_);
280+
}
281+
269282

270283
template <typename T>
271284
const T& Core::IO::InputParameterContainer::get(const std::string& name) const

src/global_data/4C_global_data_read.cpp

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ namespace
6262

6363
{
6464
std::vector<Core::IO::InputSpec> possible_materials;
65+
std::vector<Core::Materials::MaterialType> material_type;
6566
{
6667
auto materials = global_legacy_module_callbacks().materials();
6768
for (auto&& [type, spec] : materials)
6869
{
6970
possible_materials.emplace_back(std::move(spec));
71+
material_type.push_back(type);
7072
}
7173
}
7274

7375
auto all_materials = all_of({
7476
parameter<int>("MAT"),
75-
one_of(possible_materials),
77+
one_of(possible_materials,
78+
store_index_as<Core::Materials::MaterialType>("_material_type", material_type)),
7679
});
7780
section_specs["MATERIALS"] = all_materials;
7881
}
@@ -1933,57 +1936,37 @@ void Global::read_parameter(Global::Problem& problem, Core::IO::InputFile& input
19331936
/*----------------------------------------------------------------------*/
19341937
void Global::read_materials(Global::Problem& problem, Core::IO::InputFile& input)
19351938
{
1936-
std::vector<Core::IO::InputSpec> all_specs;
1937-
std::vector<Core::Materials::MaterialType> all_types;
1939+
Core::IO::InputParameterContainer container;
1940+
try
19381941
{
1939-
auto materials = global_legacy_module_callbacks().materials();
1940-
for (auto&& [type, spec] : materials)
1941-
{
1942-
all_specs.emplace_back(std::move(spec));
1943-
all_types.push_back(type);
1944-
}
1942+
input.match_section("MATERIALS", container);
19451943
}
1946-
1947-
// Whenever one of the materials is read, the lambda function will update this index to the
1948-
// current material index. This lets us access the correct subcontainer for the current material
1949-
// without searching through all of them.
1950-
std::size_t current_index = 0;
1951-
1952-
using namespace Core::IO::InputSpecBuilders;
1953-
1954-
auto all_materials = all_of({
1955-
parameter<int>(
1956-
"MAT", {.description = "Material ID that may be used to refer to this material."}),
1957-
one_of(all_specs, [&current_index](Core::IO::InputParameterContainer& container,
1958-
std::size_t index) { current_index = index; }),
1959-
});
1960-
1961-
for (const auto& fragment : input.in_section("MATERIALS"))
1944+
catch (const Core::Exception& e)
19621945
{
1963-
auto container = fragment.match(all_materials);
1964-
1965-
if (!container.has_value())
1966-
{
1967-
std::string l(fragment.get_as_dat_style_string());
1968-
FOUR_C_THROW("Invalid material specification. Could not parse line:\n {}", l.c_str());
1969-
}
1946+
FOUR_C_THROW(
1947+
"Failed to match specification in section 'MATERIALS'. The error was:\n{}.", e.what());
1948+
}
19701949

1971-
const int mat_id = container->get<int>("MAT");
1950+
for (const auto& material_entry :
1951+
container.get_or<std::vector<Core::IO::InputParameterContainer>>("MATERIALS", {}))
1952+
{
1953+
const int mat_id = material_entry.get<int>("MAT");
19721954

19731955
FOUR_C_ASSERT_ALWAYS(mat_id >= 0, "Material ID must be non-negative. Found: {}", mat_id);
19741956

19751957
if (problem.materials()->id_exists(mat_id))
19761958
FOUR_C_THROW("More than one material with 'MAT {}'", mat_id);
19771959

1978-
const std::string material_name = all_specs[current_index].impl().name();
1979-
FOUR_C_ASSERT_ALWAYS(container->has_group(material_name),
1980-
"Material type '{}' does not have a corresponding group in the input file.",
1981-
material_name.c_str());
1960+
const auto mat_type = material_entry.get<Core::Materials::MaterialType>("_material_type");
1961+
1962+
const auto& group = material_entry.groups();
1963+
FOUR_C_ASSERT_ALWAYS(
1964+
group.size() == 1, "Internal error: material must have exactly one group.");
1965+
const auto& [material_name, material_container] = group.front();
19821966

19831967
problem.materials()->insert(
19841968
mat_id, Core::Utils::LazyPtr<Core::Mat::PAR::Parameter>(
1985-
[mat_id, mat_type = all_types[current_index],
1986-
container = container->group(material_name)]()
1969+
[mat_id, mat_type, container = material_entry.group(material_name)]()
19871970
{ return Mat::make_parameter(mat_id, mat_type, container); }));
19881971
}
19891972

@@ -1994,8 +1977,8 @@ void Global::read_materials(Global::Problem& problem, Core::IO::InputFile& input
19941977
// such operations happen in the code base, thus we construct the materials here.
19951978
for (const auto& [id, mat] : problem.materials()->map())
19961979
{
1997-
// This is the point where the material is actually constructed via the side effect that we try
1998-
// to access the material.
1980+
// This is the point where the material is actually constructed via the side effect that we
1981+
// try to access the material.
19991982
(void)mat.get();
20001983
}
20011984
}

0 commit comments

Comments
 (0)