Skip to content

Commit 2180aae

Browse files
authored
Merge pull request #660 from sebproell/mesh-cleanup-2
Unify input of discretizations
2 parents f2a02af + 7848337 commit 2180aae

File tree

4 files changed

+276
-576
lines changed

4 files changed

+276
-576
lines changed

src/core/fem/src/general/utils/4C_fem_general_utils_createdis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ void Core::FE::DiscretizationCreatorBase::initial_checks(
2020
const Core::FE::Discretization& sourcedis, const Core::FE::Discretization& targetdis) const
2121
{
2222
// are the source and target discretizations ready?
23-
if (!sourcedis.filled()) FOUR_C_THROW("The source discretization is not filled!");
24-
if (!targetdis.filled()) FOUR_C_THROW("The target discretization is not filled!");
23+
if (!sourcedis.filled())
24+
FOUR_C_THROW("The source discretization '{}' is not filled!", sourcedis.name());
25+
if (!targetdis.filled())
26+
FOUR_C_THROW("The target discretization '{}' is not filled!", targetdis.name());
2527

2628
// is the target discretization really empty?
2729
if (targetdis.num_global_elements() or targetdis.num_global_nodes())

src/core/io/src/4C_io_meshreader.cpp

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,47 +186,80 @@ Core::IO::MeshReader::MeshReader(
186186

187187
/*----------------------------------------------------------------------*/
188188
/*----------------------------------------------------------------------*/
189-
void Core::IO::MeshReader::add_advanced_reader(std::shared_ptr<Core::FE::Discretization> dis,
190-
Core::IO::InputFile& input, const std::string& sectionname,
191-
const Core::IO::GeometryType geometrysource)
189+
void Core::IO::MeshReader::add_advanced_reader(
190+
std::shared_ptr<Core::FE::Discretization> dis, const std::string& sectionname)
192191
{
193-
switch (geometrysource)
192+
target_discretizations_.emplace_back(sectionname, dis);
193+
}
194+
195+
/*----------------------------------------------------------------------*/
196+
/*----------------------------------------------------------------------*/
197+
void Core::IO::MeshReader::read_and_partition()
198+
{
199+
// We need to track the max global node ID to offset node numbering and for sanity checks
200+
int max_node_id = 0;
201+
202+
for (const auto& [section_name, dis] : target_discretizations_)
194203
{
195-
case Core::IO::geometry_full:
204+
// Find out which section we have available for input. We can only do this on rank zero due
205+
// to large legacy sections that are not available everywhere. Communicate the result to all
206+
// ranks.
207+
std::map<std::string, bool> available_section;
208+
const int my_rank = Core::Communication::my_mpi_rank(comm_);
209+
if (my_rank == 0)
210+
{
211+
available_section[section_name + " ELEMENTS"] =
212+
input_.has_section(section_name + " ELEMENTS");
213+
available_section[section_name + " DOMAIN"] = input_.has_section(section_name + " DOMAIN");
214+
available_section[section_name + " GEOMETRY"] =
215+
input_.has_section(section_name + " GEOMETRY");
216+
Core::Communication::broadcast(available_section, 0, comm_);
217+
}
218+
else
219+
{
220+
Core::Communication::broadcast(available_section, 0, comm_);
221+
}
222+
223+
const int num_sections_in_file =
224+
std::ranges::count_if(available_section, [](const auto& pair) { return pair.second; });
225+
if (num_sections_in_file > 1)
196226
{
197-
std::string fullsectionname(sectionname + " ELEMENTS");
198-
ElementReader er = ElementReader(dis, input, fullsectionname);
199-
element_readers_.emplace_back(er);
200-
break;
227+
std::string found_sections;
228+
for (const auto& [section, exists] : available_section)
229+
{
230+
if (exists) found_sections += "'" + section + "' ";
231+
}
232+
FOUR_C_THROW(
233+
"Multiple options to read mesh for discretization '{}'. Only one is allowed.\n Found "
234+
"sections: {}",
235+
dis->name(), found_sections);
236+
}
237+
238+
if (num_sections_in_file == 0)
239+
{
240+
// This used to be the default, so we use it for backwards compatibility.
241+
element_readers_.emplace_back(
242+
Core::IO::ElementReader(dis, input_, section_name + " ELEMENTS"));
243+
continue;
244+
}
245+
246+
if (available_section[section_name + " ELEMENTS"])
247+
{
248+
element_readers_.emplace_back(
249+
Core::IO::ElementReader(dis, input_, section_name + " ELEMENTS"));
201250
}
202-
case Core::IO::geometry_box:
251+
if (available_section[section_name + " DOMAIN"])
203252
{
204-
std::string fullsectionname(sectionname + " DOMAIN");
205-
DomainReader dr = DomainReader(dis, input, fullsectionname);
206-
domain_readers_.emplace_back(dr);
207-
break;
253+
domain_readers_.emplace_back(Core::IO::DomainReader(dis, input_, section_name + " DOMAIN"));
208254
}
209-
case Core::IO::geometry_file:
255+
if (available_section[section_name + " GEOMETRY"])
210256
{
211-
std::string fullsectionname(sectionname + " GEOMETRY");
212257
exodus_readers_.emplace_back(Internal::ExodusReader{
213258
.target_discretization = *dis,
214-
.section_name = fullsectionname,
259+
.section_name = section_name + " GEOMETRY",
215260
});
216-
break;
217261
}
218-
default:
219-
FOUR_C_THROW("Unknown geometry source");
220-
break;
221262
}
222-
}
223-
224-
/*----------------------------------------------------------------------*/
225-
/*----------------------------------------------------------------------*/
226-
void Core::IO::MeshReader::read_and_partition()
227-
{
228-
// We need to track the max global node ID to offset node numbering and for sanity checks
229-
int max_node_id = 0;
230263

231264
graph_.resize(element_readers_.size());
232265

src/core/io/src/4C_io_meshreader.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,10 @@ namespace Core::IO
103103
*
104104
* \param dis [in] This discretization will be passed on
105105
* \param input [in] The input file.
106-
* \param sectionname [in] This will be passed on element/domain readers only (not used for
107-
* file reader)
108-
* \param geometrysource [in] selects which reader will be created
106+
* \param sectionname [in] The section name in the input file.
109107
*/
110-
void add_advanced_reader(std::shared_ptr<Core::FE::Discretization> dis,
111-
Core::IO::InputFile& input, const std::string& sectionname,
112-
const Core::IO::GeometryType geometrysource);
108+
void add_advanced_reader(
109+
std::shared_ptr<Core::FE::Discretization> dis, const std::string& sectionname);
113110

114111
/// do the actual reading
115112
/*!
@@ -194,6 +191,11 @@ namespace Core::IO
194191

195192
/// Additional parameters for reading meshes.
196193
MeshReaderParameters parameters_;
194+
195+
/// The discretizations to be filled. The key is an identifier for the sections in the input.
196+
/// Multiple discretizations might be filled from the same section.
197+
std::vector<std::pair<std::string, std::shared_ptr<Core::FE::Discretization>>>
198+
target_discretizations_;
197199
};
198200
} // namespace Core::IO
199201

0 commit comments

Comments
 (0)