-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Allow specifying attribute name when reading meshtag #3257
base: main
Are you sure you want to change the base?
Changes from 18 commits
9c22fac
4efecce
361a202
983af85
9c31b90
6950dd0
b7fa6c4
cf379a4
ca55e25
fc18895
6075ef4
fa3a840
2501a9e
dbf155a
26aed10
7719809
b4a5187
47e9fdc
60829f0
af42617
9df7574
90d5b48
b4ed95a
3ed4bf7
edd09a1
5b13c3b
c4f87c6
80fe197
ae2d766
4457147
3bbeb7e
7ee33ec
48e0835
9c4c8cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -322,8 +322,9 @@ template void XDMFFile::write_meshtags(const mesh::MeshTags<std::int32_t>&, | |
/// @endcond | ||
//----------------------------------------------------------------------------- | ||
mesh::MeshTags<std::int32_t> | ||
XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name, | ||
std::string xpath) | ||
XDMFFile::read_meshtags_by_label(const mesh::Mesh<double>& mesh, | ||
std::string name, std::string attribute_label, | ||
std::string xpath) | ||
{ | ||
spdlog::info("XDMF read meshtags ({})", name); | ||
pugi::xml_node node = _xml_doc->select_node(xpath.c_str()).node(); | ||
|
@@ -336,8 +337,28 @@ XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name, | |
|
||
const auto [entities, eshape] = read_topology_data(name, xpath); | ||
|
||
pugi::xml_node values_data_node | ||
= grid_node.child("Attribute").child("DataItem"); | ||
pugi::xml_node attribute_node = grid_node.child("Attribute"); | ||
pugi::xml_node values_data_node = attribute_node.child("DataItem"); | ||
if (!attribute_label.empty()) | ||
{ | ||
bool found = false; | ||
while ((attribute_node = attribute_node.next_sibling("Attribute"))) | ||
{ | ||
pugi::xml_attribute hint; | ||
pugi::xml_attribute name = attribute_node.attribute("Name", hint); | ||
if (std::string(name.value()) == attribute_label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating the string is a bit clumsy, maybe use https://en.cppreference.com/w/cpp/string/char_traits/compare. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the function you suggested requires providing a number of characters to compare, so it would have to be something like if (auto len = std::max(std::strlen(name.value()), attribute_label.size());
std::char_traits<char>::compare(name.value(), attribute_label.c_str(),
len) == 0) right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
{ | ||
values_data_node = attribute_node.child("DataItem"); | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) | ||
{ | ||
throw std::runtime_error("Attribute with name '" + attribute_label | ||
+ "' not found."); | ||
} | ||
} | ||
const std::vector values = xdmf_utils::get_dataset<std::int32_t>( | ||
_comm.comm(), values_data_node, _h5_id); | ||
|
||
|
@@ -376,6 +397,13 @@ XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name, | |
return meshtags; | ||
} | ||
//----------------------------------------------------------------------------- | ||
mesh::MeshTags<std::int32_t> | ||
XDMFFile::read_meshtags(const mesh::Mesh<double>& mesh, std::string name, | ||
std::string xpath) | ||
{ | ||
return read_meshtags_by_label(mesh, name, std::string(), xpath); | ||
} | ||
//----------------------------------------------------------------------------- | ||
std::pair<mesh::CellType, int> XDMFFile::read_cell_type(std::string grid_name, | ||
std::string xpath) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,9 +164,21 @@ class XDMFFile | |
const mesh::Geometry<T>& x, std::string geometry_xpath, | ||
std::string xpath = "/Xdmf/Domain"); | ||
|
||
/// Read MeshTags by name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit confusing - the docstring says "Read MeshTags by name" but the function name is "read_meshtags_by_label". Is it by name or by label? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue comes from the following fact: mesh tags are identified by "a word", so it is natural that this word be called the "name" of the mesh tag. The problem is that there is already another There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, re-reading this conversation I noticed that in the first review, four months ago, you asked me to change the method name to avoid the word "name" in favour of "label". I had forgotten about that and, based on your comment, I reverted the change. Which one should I keep? To a user, a mesh tag's "label" means presumably very little... |
||
/// @param[in] mesh The Mesh that the data is defined on | ||
/// @param[in] name Name of the grid node in the xml file. E.g. "Material" in | ||
/// Grid Name="Material" GridType="Uniform" | ||
/// @param[in] attribute_label The name of the attribute to read | ||
/// @param[in] xpath XPath where MeshTags Grid is stored in file | ||
mesh::MeshTags<std::int32_t> | ||
read_meshtags_by_label(const mesh::Mesh<double>& mesh, std::string name, | ||
std::string attribute_label, | ||
std::string xpath = "/Xdmf/Domain"); | ||
|
||
/// Read MeshTags | ||
/// @param[in] mesh The Mesh that the data is defined on | ||
/// @param[in] name | ||
/// @param[in] name Name of the grid node in the xml file. E.g. "Material" in | ||
/// Grid Name="Material" GridType="Uniform" | ||
/// @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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Xdmf Version="3.0"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid adding data files? Can it be created by the test rather than added to the repo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can convert the file to ascii, add it to the source code and have the code write it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second though, that's really just the same thing as adding a file with the ascii content, which is what I do now XD There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, never mind... I turned on my brain and found a solution. |
||
<Domain> | ||
<Grid Name="Grid"> | ||
<Geometry GeometryType="XYZ"> | ||
<DataItem DataType="Float" Dimensions="262 3" Format="HDF" Precision="8"> | ||
Domain.h5:/data0</DataItem> | ||
</Geometry> | ||
<Topology TopologyType="Tetrahedron" NumberOfElements="813" NodesPerElement="4"> | ||
<DataItem DataType="Int" Dimensions="813 4" Format="HDF" Precision="8"> | ||
Domain.h5:/data1</DataItem> | ||
</Topology> | ||
<Attribute Name="point_tags" AttributeType="Scalar" Center="Node"> | ||
<DataItem DataType="Int" Dimensions="262" Format="HDF" Precision="8"> | ||
Domain.h5:/data2</DataItem> | ||
</Attribute> | ||
<Attribute Name="domain" AttributeType="Scalar" Center="Cell"> | ||
<DataItem DataType="Int" Dimensions="813" Format="HDF" Precision="8"> | ||
Domain.h5:/data3</DataItem> | ||
</Attribute> | ||
<Attribute Name="material" AttributeType="Scalar" Center="Cell"> | ||
<DataItem DataType="Int" Dimensions="813" Format="HDF" Precision="8"> | ||
Domain.h5:/data4</DataItem> | ||
</Attribute> | ||
</Grid> | ||
</Domain> | ||
</Xdmf> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2024 Massimiliano Leoni | ||
// | ||
// This file is part of DOLFINx (https://www.fenicsproject.org) | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
// Unit tests for reading meshtags by name | ||
|
||
#include <catch2/catch_template_test_macros.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <dolfinx/common/IndexMap.h> | ||
#include <dolfinx/common/MPI.h> | ||
#include <dolfinx/io/XDMFFile.h> | ||
#include <dolfinx/la/Vector.h> | ||
#include <dolfinx/mesh/MeshTags.h> | ||
#include <dolfinx/mesh/utils.h> | ||
|
||
#include <string> | ||
|
||
using namespace dolfinx; | ||
|
||
namespace | ||
{ | ||
|
||
void test_read_named_meshtags() | ||
{ | ||
const std::string mesh_file = "Domain.xdmf"; | ||
|
||
io::XDMFFile meshFile(MPI_COMM_WORLD, mesh_file, "r"); | ||
auto mesh = std::make_shared<mesh::Mesh<double>>(meshFile.read_mesh( | ||
fem::CoordinateElement<double>(mesh::CellType::tetrahedron, 1), | ||
mesh::GhostMode::none, "Grid")); | ||
|
||
const auto mt_domain = meshFile.read_meshtags_by_label( | ||
*mesh, "Grid", "domain", "/Xdmf/Domain"); | ||
|
||
CHECK(mt_domain.values().front() == 0); | ||
|
||
const auto mt_material = meshFile.read_meshtags_by_label( | ||
*mesh, "Grid", "material", "/Xdmf/Domain"); | ||
|
||
CHECK(mt_material.values().front() == 1); | ||
|
||
CHECK_THROWS(meshFile.read_meshtags_by_label(*mesh, "Grid", "missing")); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST_CASE("Read meshtag by name", "[read_meshtag_by_name]") | ||
{ | ||
CHECK_NOTHROW(test_read_named_meshtags()); | ||
} |
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 is this for?
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.
It's something that
pugixml
requires, like a hint to try to speed-up the search of a node.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.
The hint is not set or re-used - does the code compile without it?
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 don't think it can because
hint
is also an output parameter of the function.