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

Update computation of number of components for arbitrary value shape in xdmf_function #2848

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions cpp/dolfinx/io/xdmf_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ using namespace dolfinx::io;

namespace
{
/// Convert a value_rank to the XDMF string description (Scalar, Vector,
/// Convert a shape to the XDMF string description (Scalar, Vector,
/// Tensor).
std::string rank_to_string(int value_rank)
std::string shape_to_string(std::span<const std::size_t> shape)
{
switch (value_rank)
{
case 0:
if (shape.empty())
return "Scalar";
else if (shape.size() == 1 and shape[0] == 1)
return "Scalar";
case 1:
else if (shape.size() == 1)
return "Vector";
case 2:
else if (shape.size() == 2)
return "Tensor";
default:
throw std::runtime_error("Range Error");
}
else
throw std::runtime_error("Unsupported value shape");
}
} // namespace

Expand All @@ -53,7 +52,8 @@ void xdmf_function::add_function(MPI_Comm comm, const fem::Function<T, U>& u,
assert(u.function_space());
auto mesh = u.function_space()->mesh();
assert(mesh);
auto element = u.function_space()->element();
std::shared_ptr<const fem::FiniteElement<U>> element
= u.function_space()->element();
assert(element);

// FIXME: is the below check adequate for detecting a Lagrange
Expand All @@ -75,8 +75,9 @@ void xdmf_function::add_function(MPI_Comm comm, const fem::Function<T, U>& u,
assert(dofmap);
const int bs = dofmap->bs();

int rank = element->value_shape().size();
int num_components = std::pow(3, rank);
std::span<const std::size_t> value_shape = element->value_shape();
int num_components = std::reduce(value_shape.begin(), value_shape.end(), 1,
std::multiplies{});

// Get fem::Function data values and shape
std::vector<T> data_values;
Expand Down Expand Up @@ -174,7 +175,8 @@ void xdmf_function::add_function(MPI_Comm comm, const fem::Function<T, U>& u,
pugi::xml_node attr_node = xml_node.append_child("Attribute");
assert(attr_node);
attr_node.append_attribute("Name") = attr_name.c_str();
attr_node.append_attribute("AttributeType") = rank_to_string(rank).c_str();
attr_node.append_attribute("AttributeType")
= shape_to_string(value_shape).c_str();
attr_node.append_attribute("Center") = cell_centred ? "Cell" : "Node";

std::span<const scalar_value_type_t<T>> u;
Expand Down