Skip to content

Commit

Permalink
Add optional function name to read
Browse files Browse the repository at this point in the history
  • Loading branch information
mleoni-pf committed Nov 26, 2024
1 parent c300e3e commit 3284905
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cpp/dolfinx/io/XDMFFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ XDMFFile::write_function(const fem::Function<std::complex<double>, double>&,
//-----------------------------------------------------------------------------
void XDMFFile::read_function(const mesh::Mesh<double>& mesh, std::string name,
fem::Function<double, double>& u,
std::optional<std::string> function_name,
std::string xpath)
{
/*
Expand Down Expand Up @@ -343,6 +344,20 @@ void XDMFFile::read_function(const mesh::Mesh<double>& mesh, std::string name,

pugi::xml_node values_data_node
= grid_node.child("Attribute").child("DataItem");
if (function_name)
{
// Search for a child that contains an attribute with the requested name
pugi::xml_node function_node = grid_node.find_child(
[fun_name = *function_name](auto n)
{ return n.attribute("Name").value() == fun_name; });
if (!function_node)
{
throw std::runtime_error("Function with name '" + *function_name
+ "' not found.");
}
else
values_data_node = function_node.child("DataItem");
}
const std::vector values
= xdmf_utils::get_dataset<double>(_comm.comm(), values_data_node, _h5_id);

Expand Down
3 changes: 2 additions & 1 deletion cpp/dolfinx/io/XDMFFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ class XDMFFile
/// @param[in] mesh The Mesh that the data is defined on
/// @param[in] name
/// @param[out] u The function into which to read data
/// @param[in] function_name The (optinal) name of the function to read from file
/// @param[in] xpath XPath where MeshFunction Grid is stored in file
void read_function(const mesh::Mesh<double>& mesh, std::string name,
fem::Function<double, double>& u,
fem::Function<double, double>& u, std::optional<std::string> function_name,
std::string xpath = "/Xdmf/Domain");

/// Write MeshTags
Expand Down

0 comments on commit 3284905

Please sign in to comment.