@@ -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/* ----------------------------------------------------------------------*/
19341937void 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, [¤t_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