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

Add functions to copy data between Function(s) and PETSc Vecs #3652

Closed
wants to merge 29 commits into from

Conversation

jhale
Copy link
Member

@jhale jhale commented Feb 27, 2025

This PR adds functions that can copy data between function(s) and standard/blocked/nested PETSc Vecs. Dispatch to correct function is performed through weak typing, always duck-typing through to standard routine.

This work was based on #3648 by @jorgensd

@jhale jhale changed the title Function to PETSc Vec copy Add functions to copy data between Function(s) and PETSc Vecs Feb 27, 2025
@jhale
Copy link
Member Author

jhale commented Feb 27, 2025

Some outstanding points:

  • Use localForm in _copy_vec_to_function
  • Should we assume x and vector underlying u enters in a consistent state?
  • Are all of the ghost updates necessary?

@jorgensd
Copy link
Member

Some outstanding points:

* Use localForm in `_copy_vec_to_function`

* Should we assume `x` and vector underlying `u` enters in a consistent state?

* Are all of the ghost updates necessary?

They do not do not always come in as a consistent state (observed in the SNESSolver).

@garth-wells garth-wells marked this pull request as draft March 1, 2025 20:51
@garth-wells
Copy link
Member

garth-wells commented Mar 2, 2025

I've reduced the number of functions and made them much simpler, and demonstrated usage in the SNES tests.

The functions should not have ghost updates. Copying the ghost values eliminates any need for a 'post' update. Any 'pre' ghost update should be performed by the caller. Throwing in mis-guided ghost updates is something we have eliminated in DOLFINx.

If we go with this, the 'assign' functions that are 'linear algebra' only should probably move to a dolfinx.la.petsc module, or be made private.

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.

Only minor things to consider.

Should we move it into dolfinx.la.petsc right away?


@functools.singledispatch
def assign(
x0: typing.Union[npt.NDArray[np.floating], list[npt.NDArray[np.floating]]], x1: PETSc.Vec
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the typing also include np.complexfloating for complex values?

Copy link
Member Author

Choose a reason for hiding this comment

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

From my reading of the numpy documentation this concept is expressed as np.inexact. https://numpy.org/doc/stable/reference/arrays.scalars.html#inexact-types

I suspect we're making a number of issues overtyping/undertyping/incorrectly typing throughout the codebase. This could be looked at in another PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Clearly we should get this one agreed on so we can look elsewhere!


Assigns values in ``x`` to the degrees-of-freedom of ``u``, which is
possibly a collection of ``Functions``s. When ``u`` is a collecion
of ``Functions``s, values in ``x`` are assigned block-wise ro the
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
of ``Functions``s, values in ``x`` are assigned block-wise ro the
of ``Functions``s, values in ``x`` are assigned block-wise to the


@assign_function.register(PETSc.Vec)
def _(x: PETSc.Vec, u: typing.Union[_Function, list[_Function]]):
"""Assign vector entries to :class:`.Function` degrees-of-freedom.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Assign vector entries to :class:`.Function` degrees-of-freedom.
"""Assign vector entries to :class:`Function` degrees-of-freedom.

Should there really be a . here?

@garth-wells
Copy link
Member

Should we move it into dolfinx.la.petsc right away?

We don't have a dolfinx.ls.petsc.py file at the moment, so rather than creating one it might be better to make the linear algebra functions private.

@jorgensd
Copy link
Member

jorgensd commented Mar 4, 2025

Should we move it into dolfinx.la.petsc right away?

We don't have a dolfinx.ls.petsc.py file at the moment, so rather than creating one it might be better to make the linear algebra functions private.

The assign vec<->function are very useful, even for experienced programmers. This was pointed out by @jhale in the snes-problem PR. I don’t understand the reason for making them if you want them to be private.

@jhale
Copy link
Member Author

jhale commented Mar 5, 2025

Should we move it into dolfinx.la.petsc right away?

We don't have a dolfinx.ls.petsc.py file at the moment, so rather than creating one it might be better to make the linear algebra functions private.

The assign vec<->function are very useful, even for experienced programmers. This was pointed out by @jhale in the snes-problem PR. I don’t understand the reason for making them if you want them to be private.

My interpretation of @garth-wells comment was that the lower-level routine should be made private, rather than putting it in a new location la.petsc (which would be the strictly correct placement). The routines that take Functions belong in fem.petsc.

@garth-wells
Copy link
Member

My interpretation of @garth-wells comment was that the lower-level routine should be made private, rather than putting it in a new location la.petsc (which would be the strictly correct placement). The routines that take Functions belong in fem.petsc.

Correct.

jhale and others added 4 commits March 5, 2025 10:44
Co-authored-by: Jørgen Schartum Dokken <[email protected]>
Co-authored-by: Jørgen Schartum Dokken <[email protected]>
Co-authored-by: Jørgen Schartum Dokken <[email protected]>
Co-authored-by: Jørgen Schartum Dokken <[email protected]>
@jhale jhale marked this pull request as ready for review March 5, 2025 09:50
jhale added 2 commits March 5, 2025 13:18
A Collection has no ordering - Sequence is correct.
Use np.inexact instead of np.floating
@jhale
Copy link
Member Author

jhale commented Mar 5, 2025

I've done the legwork to split la into a native and PETSc part in a manner consistent with the rest of our interface. This creates an API change dolfinx.la.create_petsc_vector -> dolfinx.la.petsc.create_petsc_vector. The second petsc now seems redundant. Should it be renamed e.g. dolfinx.la.petsc.create_vector and dolfinx.la.petsc.create_vector_wrap?

https://github.com/FEniCS/dolfinx/tree/jhale/refactor-la-petsc

@jhale
Copy link
Member Author

jhale commented Mar 6, 2025

Closed in favour of #3659

@jhale jhale closed this Mar 6, 2025
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.

3 participants