Skip to content

Comments

Add mesh movement function that simplifies movement for any ufl expression#185

Merged
jorgensd merged 6 commits intomainfrom
dokken/move-mesh
Feb 18, 2026
Merged

Add mesh movement function that simplifies movement for any ufl expression#185
jorgensd merged 6 commits intomainfrom
dokken/move-mesh

Conversation

@jorgensd
Copy link
Member

@jorgensd jorgensd commented Feb 17, 2026

Talking with @andre-massing, I realized that we could use the functionality from IO4dolfinx to simplify mesh movement (https://github.com/scientificcomputing/io4dolfinx/blob/main/src/io4dolfinx/readers.py#L314-L353).

Additional API fixes for: FEniCS/dolfinx#4049

@jorgensd jorgensd requested a review from finsberg February 17, 2026 14:12
Copy link
Member

@finsberg finsberg left a comment

Choose a reason for hiding this comment

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

I think the move function would be very useful in many cases, and this is something I think a lot of people coming from legacy FEniCS is missing in FEniCSx.

Comment on lines +459 to +466
def move(
mesh: dolfinx.mesh.Mesh,
u: dolfinx.fem.Function
| ufl.core.expr.Expr
| typing.Callable[[npt.NDArray[np.floating]], npt.NDArray[np.inexact]],
):
"""
Move the geometry nodes of a mesh given by the movement of a function u.
Copy link
Member

@finsberg finsberg Feb 17, 2026

Choose a reason for hiding this comment

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

Should perhaps specify that this function modifies the input-mesh in-place. I generally don't like functions that mutate the object in-place, but I do see the benefit in this case. My preference would however be that the default behaviour would be to create a new mesh and modify it instead, and have an argument , inplace which defaults to False.

Copy link
Member

Choose a reason for hiding this comment

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

This would be similar to the API in pandas for instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t think this is useful for the problems we are considering, as then all forms, function spaces, matrises etc has to be regenerated.

additionally, by not returning the mesh, it should be quite clear from the user that the geometry is modified.

in most cases with mesh movement one moves the mesh more than once, and it would cause a memory cleanup nightmare.

Copy link
Member

Choose a reason for hiding this comment

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

OK, making it mutate the mesh in place is also consistent with the legacy way of doing things, so I guess it is fine. We could perhaps consider making a new function that simply copies the mesh and suggest users that want this behaviour to chain the two functions together.

Comment on lines +404 to +409
def create_geometry_function_space(
mesh: dolfinx.mesh.Mesh, N: int | None = None
) -> dolfinx.fem.FunctionSpace:
"""
Reconstruct a vector space with N components using
the geometry dofmap to ensure a 1-1 mapping between mesh nodes and DOFs.
Copy link
Member

Choose a reason for hiding this comment

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

How about we add io4dolfinx as a depencendy instead? So that we don't need to maintain two versions of the same function?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would rather avoid that, for now maintaining it in two places is the best option. Maybe in the future io4dolfinx would depend on scifem.

I do like some separation of concerns

Copy link
Member

@finsberg finsberg Feb 18, 2026

Choose a reason for hiding this comment

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

I think io4dolfinx is a lighter dependency (we don't need to build from source and link with c++ layer in dolfinx like we do in scifem) so making scifem depend on io4dolfinx might be easier, but we can discuss this at a later point. As long as it is only a single function it should be fine to maintain it in two places.

@finsberg finsberg self-requested a review February 18, 2026 06:42
@jorgensd jorgensd merged commit 4e41e0f into main Feb 18, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants