Skip to content

Allow specifying attribute name when reading meshtag - #3257

Merged
jorgensd merged 42 commits into
FEniCS:mainfrom
mleoni-pf:mleoni/readNamedMeshtags
Nov 22, 2024
Merged

Allow specifying attribute name when reading meshtag#3257
jorgensd merged 42 commits into
FEniCS:mainfrom
mleoni-pf:mleoni/readNamedMeshtags

Conversation

@mleoni-pf

Copy link
Copy Markdown
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
Comment thread python/dolfinx/io/utils.py Outdated
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"):

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread python/dolfinx/io/utils.py Outdated

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
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
Copy Markdown
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
Copy Markdown
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

@garth-wells garth-wells left a comment

Copy link
Copy Markdown
Member

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.

Comment thread cpp/dolfinx/io/XDMFFile.h Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread python/dolfinx/wrappers/io.cpp Outdated
Comment thread python/dolfinx/wrappers/io.cpp Outdated
@mleoni-pf
mleoni-pf requested a review from garth-wells June 21, 2024 08:03
Comment thread cpp/test/mesh/Domain.xdmf Outdated
@mleoni-pf
mleoni-pf requested a review from garth-wells July 31, 2024 16:17
@mscroggs mscroggs added this to the 0.9.0 milestone Sep 3, 2024
Comment thread cpp/dolfinx/io/XDMFFile.h Outdated
const mesh::Geometry<T>& x, std::string geometry_xpath,
std::string xpath = "/Xdmf/Domain");

/// Read MeshTags by name

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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...

Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
@garth-wells garth-wells modified the milestones: 0.9.0, 0.10.0 Oct 4, 2024
Comment thread cpp/dolfinx/io/XDMFFile.h Outdated
Comment thread cpp/dolfinx/io/XDMFFile.h Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp
@jorgensd

jorgensd commented Nov 8, 2024

Copy link
Copy Markdown
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.

Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
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
Copy Markdown
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
Copy Markdown
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.

Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
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
Copy Markdown
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.

Comment thread cpp/dolfinx/io/XDMFFile.cpp Outdated
Comment thread cpp/dolfinx/io/XDMFFile.cpp
Comment thread cpp/test/mesh/read_named_meshtags.cpp Outdated
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
Copy Markdown
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);

Comment thread cpp/test/mesh/read_named_meshtags.cpp Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

See above comment.

Comment thread python/dolfinx/io/utils.py Outdated

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
Copy Markdown
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
Copy Markdown
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.

Comment thread python/dolfinx/io/utils.py Outdated

@jorgensd jorgensd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@mleoni-pf
mleoni-pf force-pushed the mleoni/readNamedMeshtags branch from 64ad359 to d348efa Compare November 21, 2024 16:43

@garth-wells garth-wells left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
@mleoni-pf
mleoni-pf deleted the mleoni/readNamedMeshtags branch November 25, 2024 08:42
schnellerhase pushed a commit to schnellerhase/fenics-dolfinx that referenced this pull request Dec 28, 2024
* Safe default value for unbound variables in dolfinx.conf

* Allow to specify attribute name when reading meshtag

* Add python interface

* Disambiguate functions

* Fix Python wrapper

* Applying review changes

* Linting

* Only one function in the python wrapper and interface

* Applied code review changes

* Added unit test

* Fix comments

* Removed unused variables

* Using ASCII file to circumvent CI issue

* Specify ASCII encoding

* Fix a bug and create mesh within test

* Dodge issue 3316

* Applied review suggestions

* Fix missing renaming

* Fix python wrapper

* Simplify code

* Update cpp/dolfinx/io/XDMFFile.h

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Update cpp/dolfinx/io/XDMFFile.h

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Sprinkling some const

* Apply revision

* Fix doc

* Fix doc

* Ruff format

* Const and ref fixes

* Ruff fixes

* Update python/dolfinx/io/utils.py

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Add type hints

* Fix dispatch

* Fixes

* Add missing include

* Tidy cmake order

* Minor fixes

* Added test

* Ruff fixes

* Ruff fixes

---------

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>
Co-authored-by: Garth N. Wells <gnw20@cam.ac.uk>
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