Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make read_meshtags a template function #3533

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
25 changes: 19 additions & 6 deletions cpp/dolfinx/io/XDMFFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ template void XDMFFile::write_meshtags(const mesh::MeshTags<std::int32_t>&,
std::string, std::string);
/// @endcond
//-----------------------------------------------------------------------------
mesh::MeshTags<std::int32_t>
template <typename T>
mesh::MeshTags<T>
XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
std::optional<std::string> attribute_name,
std::string xpath)
Expand Down Expand Up @@ -365,8 +366,8 @@ XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
else
values_data_node = attribute_node.child("DataItem");
}
const std::vector values = xdmf_utils::get_dataset<std::int32_t>(
_comm.comm(), values_data_node, _h5_id);
const std::vector values
= xdmf_utils::get_dataset<T>(_comm.comm(), values_data_node, _h5_id);

const std::pair<std::string, int> cell_type_str
= xdmf_utils::get_cell_type(grid_node.child("Topology"));
Expand All @@ -380,8 +381,8 @@ XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
const std::int64_t,
MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 2>>
entities_span(entities1.data(), eshape);
std::pair<std::vector<std::int32_t>, std::vector<std::int32_t>>
entities_values = xdmf_utils::distribute_entity_data<std::int32_t>(
std::pair<std::vector<std::int32_t>, std::vector<T>> entities_values
= xdmf_utils::distribute_entity_data<T>(
*mesh.topology(), mesh.geometry().input_global_indices(),
mesh.geometry().index_map()->size_global(),
mesh.geometry().cmap().create_dof_layout(), mesh.geometry().dofmap(),
Expand All @@ -397,12 +398,24 @@ XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
num_vertices_per_entity);
mesh::MeshTags meshtags = mesh::create_meshtags(
mesh.topology(), mesh::cell_dim(cell_type), entities_adj,
std::span<const std::int32_t>(entities_values.second));
std::span<const T>(entities_values.second));
meshtags.name = name;

return meshtags;
}
//-----------------------------------------------------------------------------
// Instantiation for different types
/// @cond
template mesh::MeshTags<std::int32_t>
XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
std::optional<std::string> attribute_name,
std::string xpath);
template mesh::MeshTags<double>
XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
std::optional<std::string> attribute_name,
std::string xpath);
/// @endcond
//-----------------------------------------------------------------------------
std::pair<mesh::CellType, int> XDMFFile::read_cell_type(std::string grid_name,
std::string xpath)
{
Expand Down
9 changes: 5 additions & 4 deletions cpp/dolfinx/io/XDMFFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ class XDMFFile
/// GridType="Uniform"
/// @param[in] attribute_name Name of the attribute to read
/// @param[in] xpath XPath where MeshTags Grid is stored in file
mesh::MeshTags<std::int32_t>
read_meshtags(const mesh::Mesh<double>& mesh, std::string name,
std::optional<std::string> attribute_name,
std::string xpath = "/Xdmf/Domain");
template <typename T = std::int32_t>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What types are supported? If the type in the file can't be checked it can lead to all sorts of crashes and mis-behaviours.

What is needed for this to be robust is a function for querying the data type in the file. I started this ages ago for HDF5 files, but it wasn't trivial to make it precise, but getting the type size was easy and may be adequate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We hit the same issue with the recent checkpointing project. We should have a chat in #developmemt about the best ways to tackle this.

mesh::MeshTags<T> read_meshtags(const mesh::Mesh<double>& mesh,
std::string name,
std::optional<std::string> attribute_name,
std::string xpath = "/Xdmf/Domain");

/// Write Information
/// @param[in] name
Expand Down
2 changes: 1 addition & 1 deletion python/dolfinx/wrappers/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void io(nb::module_& m)
nb::arg("name") = "mesh", nb::arg("xpath") = "/Xdmf/Domain")
.def("read_cell_type", &dolfinx::io::XDMFFile::read_cell_type,
nb::arg("name") = "mesh", nb::arg("xpath") = "/Xdmf/Domain")
.def("read_meshtags", &dolfinx::io::XDMFFile::read_meshtags,
.def("read_meshtags", &dolfinx::io::XDMFFile::read_meshtags<std::int32_t>,
nb::arg("mesh"), nb::arg("name"), nb::arg("attribute_name").none(),
nb::arg("xpath"))
.def("write_information", &dolfinx::io::XDMFFile::write_information,
Expand Down
Loading