-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
base: main
Are you sure you want to change the base?
Make read_meshtags a template function #3533
Conversation
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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented something like this [untested]:
// Define a type-trait-kinda thing
template <typename T>
struct xdmf_data
{
};
template <>
struct xdmf_data<std::int32_t>
{
static const std::string data_type;
static const std::size_t precision = 4;
};
const std::string xdmf_data<std::int32_t>::data_type = "Int";
template <>
struct xdmf_data<std::int64_t>
{
static const std::string data_type;
static const std::size_t precision = 8;
};
const std::string xdmf_data<std::int64_t>::data_type = "Int";
template <>
struct xdmf_data<float>
{
static const std::string data_type;
static const std::size_t precision = 4;
};
const std::string xdmf_data<float>::data_type = "Float";
template <>
struct xdmf_data<double>
{
static const std::string data_type;
static const std::size_t precision = 8;
};
const std::string xdmf_data<double>::data_type = "Float";
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)
{
// omitted stuff...
values_data_node = attribute_node.child("DataItem");
const std::string data_type = values_data_node.attribute("DataType").value();
const auto precision
= std::stol(values_data_node.attribute("Precision").value());
if (xdmf_data<T>::data_type != data_type
|| xdmf_data<T>::precision != precision)
{
throw std::runtime_error(
"The data in the XDMF file does not match the required data type.");
}
// continue...
What do you think of this approach? Needs polishing obviously... @garth-wells @jhale
This PR leverages functionalities introduced some time ago by @jorgensd that makes
xdmf_utils::distribute_entity_data
process data of a generic type, not onlystd::int32_t
. In this PR I makeXDMFFile::read_meshtags
a template so that we can read mesh tags of any type [esp.double
] from file. This will be useful in an upcoming PR to read functions from file.