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

Allow specifying attribute name when reading meshtag #3257

Merged
merged 42 commits into from
Nov 22, 2024

Conversation

mleoni-pf
Copy link
Contributor

If no attribute_name is provided, read the first attribute. Otherwise, read the attribute with the provided name or throw an error if it couldn't be found.

@mleoni-pf mleoni-pf changed the title Allow to specify attribute name when reading meshtag Allow specifying attribute name when reading meshtag Jun 6, 2024
return MeshTags(mt)

def read_meshtags(self, mesh, name, xpath="/Xdmf/Domain"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -276,10 +276,13 @@ def read_mesh(
)
return Mesh(msh, domain)

def read_meshtags(self, mesh, name, attribute_name="", xpath="/Xdmf/Domain"):
mt = super().read_meshtags(mesh._cpp_object, name, attribute_name, xpath)
def read_meshtags_by_name(self, mesh, name, attribute_name, xpath="/Xdmf/Domain"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better as one function and using default arg attribute_name=None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const mesh::Mesh<double>& mesh, const std::string name,
const std::string attribute_name, const std::string xpath)
mesh::MeshTags<std::int32_t>
XDMFFile::read_meshtags_by_name(const mesh::Mesh<double>& mesh,
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jorgensd jorgensd added the io label Jun 7, 2024
Copy link
Member

@garth-wells garth-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is XDMFFile.cpp looks to me more complicated than it needs to be. I've left some suggestions as a first pass. I can look again once it's been simplified some more.

cpp/dolfinx/io/XDMFFile.h Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
python/dolfinx/wrappers/io.cpp Outdated Show resolved Hide resolved
python/dolfinx/wrappers/io.cpp Outdated Show resolved Hide resolved
@mscroggs mscroggs added this to the 0.9.0 milestone Sep 3, 2024
@@ -164,9 +164,21 @@ class XDMFFile
const mesh::Geometry<T>& x, std::string geometry_xpath,
std::string xpath = "/Xdmf/Domain");

/// Read MeshTags by name
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 name parameter which refers to the name of the mesh node in the same file. I picked the word "label" to distinguish the two internally, in the code implementation, but a user should not be concerned with this implementation detail: a user saved a mesh tag giving it a "name" and wants to read it back by the same "name".
The fact remains that the user still needs to call a function whose name includes the word "label", not "name", which is indeed a bit confusing. I will change everything to be called more consistently, the code shouldn't become too unintelligible on the development side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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...

cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
@garth-wells garth-wells modified the milestones: 0.9.0, 0.10.0 Oct 4, 2024
cpp/dolfinx/io/XDMFFile.h Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.h Outdated Show resolved Hide resolved
@jorgensd
Copy link
Member

jorgensd commented Nov 8, 2024

@garth-wells
@mleoni-pf and I sat down and looked through the pugixml docs today.
The new solution is much cleaner and much closer to what we use in general in DOLFINx (i.e. lambda function for search predicate).
This is a very neat function to have, as it will allow storing multiple data arrays within the same grid (for instance material parameters.

@@ -334,8 +334,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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want a separate function for this in C++? Couldn't we just put the attribute name as a fourth argument (optional) argument.

I don't see the added value of a constructor for handling one optional value to have a backwards compatible C++ interface. @garth-wells

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, one function would be better. Would is make sense to use std::optional for attribute_name?

Related to this, we should not pass empty strings as 'null' - use std::optional.

@@ -334,8 +334,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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, one function would be better. Would is make sense to use std::optional for attribute_name?

Related to this, we should not pass empty strings as 'null' - use std::optional.

cpp/dolfinx/io/XDMFFile.cpp Outdated Show resolved Hide resolved
cpp/dolfinx/io/XDMFFile.cpp Show resolved Hide resolved
std::iota(std::begin(indices), std::end(indices), 0);

std::vector<std::int32_t> domain_values(n_cells);
std::ranges::fill(domain_values, domain_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use std::vector<std::int32_t> domain_values(n_cells, domain_value);

std::vector<std::int32_t> domain_values(n_cells);
std::ranges::fill(domain_values, domain_value);

std::vector<std::int32_t> material_values(n_cells);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment.

@@ -276,8 +276,15 @@ 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(self, mesh, name, attribute_label=None, xpath="/Xdmf/Domain"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the Google docs style with Arguments, etc. There are quite a few examples in the code.

@garth-wells
Copy link
Member

@garth-wells @mleoni-pf and I sat down and looked through the pugixml docs today. The new solution is much cleaner and much closer to what we use in general in DOLFINx (i.e. lambda function for search predicate). This is a very neat function to have, as it will allow storing multiple data arrays within the same grid (for instance material parameters.

Nice - looking good.

Copy link
Member

@jorgensd jorgensd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@garth-wells garth-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed some fixes to the code. Last things that is missing is a Python test. @massimiliano-leoni can you add a Python test?

@mleoni-pf
Copy link
Contributor Author

@garth-wells Python test added!

@garth-wells garth-wells added this pull request to the merge queue Nov 22, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Nov 22, 2024
@jorgensd jorgensd added this pull request to the merge queue Nov 22, 2024
Merged via the queue into FEniCS:main with commit 13bc37f Nov 22, 2024
15 checks passed
@mleoni-pf mleoni-pf deleted the mleoni/readNamedMeshtags branch November 25, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants