-
-
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 4 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_name(const mesh::Mesh<double>& mesh, | ||
std::string name, std::string attribute_name, | ||
std::string xpath) | ||
{ | ||
spdlog::info("XDMF read meshtags ({})", name); | ||
pugi::xml_node node = _xml_doc->select_node(xpath.c_str()).node(); | ||
|
@@ -336,8 +337,29 @@ 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 (not attribute_name.empty()) | ||
{ | ||
bool found = false; | ||
for (; attribute_node; | ||
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 looks weird and is hard to read. Should it be 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 |
||
attribute_node = attribute_node.next_sibling("Attribute")) | ||
{ | ||
pugi::xml_attribute hint; | ||
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. What is this for? 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. It's something that 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 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 commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it can because |
||
pugi::xml_attribute name = attribute_node.attribute("Name", hint); | ||
if (std::string(name.value()) == attribute_name) | ||
{ | ||
values_data_node = attribute_node.child("DataItem"); | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (not found) | ||
mleoni-pf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw std::runtime_error("Attribute with name '" + attribute_name | ||
+ "' not found."); | ||
} | ||
} | ||
const std::vector values = xdmf_utils::get_dataset<std::int32_t>( | ||
_comm.comm(), values_data_node, _h5_id); | ||
|
||
|
@@ -376,6 +398,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_name(mesh, name, "", 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,6 +164,16 @@ 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 | ||
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. What is 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, but for the record this was already undocumented. |
||
/// @param[in] attribute_name 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_name(const mesh::Mesh<double>& mesh, std::string name, | ||
std::string attribute_name, | ||
std::string xpath = "/Xdmf/Domain"); | ||
|
||
/// Read MeshTags | ||
/// @param[in] mesh The Mesh that the data is defined on | ||
/// @param[in] name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,10 +276,13 @@ def read_mesh( | |
) | ||
return Mesh(msh, domain) | ||
|
||
def read_meshtags(self, mesh, name, xpath="/Xdmf/Domain"): | ||
mt = super().read_meshtags(mesh._cpp_object, name, xpath) | ||
def read_meshtags_by_name(self, mesh, name, attribute_name, xpath="/Xdmf/Domain"): | ||
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. Would it be better as one function and using default arg 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 remember that you don't like having two default arguments in one function but I'm guessing that's only for C++, 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. Not good for C++, fine with Python if it's simple (which this is) and doesn't introduce complicated conditional handling. 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 |
||
mt = super().read_meshtags_by_name(mesh._cpp_object, name, attribute_name, xpath) | ||
return MeshTags(mt) | ||
|
||
def read_meshtags(self, mesh, name, xpath="/Xdmf/Domain"): | ||
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. Needs docs. 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 |
||
return self.read_meshtags_by_name(mesh, name, "", xpath) | ||
|
||
|
||
def distribute_entity_data( | ||
mesh: Mesh, entity_dim: int, entities: npt.NDArray[np.int64], values: np.ndarray | ||
|
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.
Let's change
name
->label
in the function name. 'name' is ambiguous.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.
Done