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

Let dlx.la.Vector manage the scope of the petsc4py wrapper (#1) #3092

Merged
merged 12 commits into from
Apr 12, 2024

Conversation

uvilla
Copy link
Contributor

@uvilla uvilla commented Mar 7, 2024

This PR allows for a dlx.la.Vector (rather than a dlx.fem.Function) to manage the memory and the scope of the petsc4py.PETSc.vec object that wraps the underlying data.

The implementation of the property dlx.fem.Function.vector was also modified to delegate the creation of the wrapper to the underlying dlx.la.Vector.

All changes are backward compatible.

See also Issue #3061
@jorgensd
@garth-wells
@jhale

This PR allows for a `dlx.la.Vector` (rather than a `dlx.fem.Function`) to manage the memory and the scope of the `petsc4py.PETSc.vec` object that wraps the underlying data.

The implementation of the property `dlx.fem.Function.vector` was also modified to delegate the creation of the wrapper to the underlying `dlx.la.Vector`.

All changes are backward compatible.
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.

Looks good - a few small comments.

python/dolfinx/fem/function.py Outdated Show resolved Hide resolved
python/dolfinx/fem/function.py Outdated Show resolved Hide resolved
@@ -485,7 +480,7 @@ def copy(self) -> Function:
@property
def x(self) -> la.Vector:
"""Vector holding the degrees-of-freedom."""
return la.Vector(self._cpp_object.x) # type: ignore
return self._x # type: ignore

@property
def vector(self):
Copy link
Member

Choose a reason for hiding this comment

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

We should probably deprecate this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can deprecate the method. However, I would like to get consensus on this, since it will require to update all the python examples using Function.vector to use Function.x.petsc_vec

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I think we should put in a deprecation-warning, and remove it with the release of 0.9.x

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I'll do that and replace all occurrences of u.vector to u.x.petsc_vec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@garth-wells @jorgensd : This has been addressed. All occurrences of u.vector were replaced with u.x.petsc_vec and a deprecation warning was added to dlx.fem.Function.vector.

@@ -245,6 +250,18 @@ def array(self) -> np.ndarray:
"""Local representation of the vector."""
return self._cpp_object.array

@property
def vector(self):
Copy link
Member

Choose a reason for hiding this comment

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

I think we need a more descriptive name. Maybe petsc_vec?

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 also prefer petsc_vec. I originally used vector since this was the method used in the fem.Function class.
I will make the update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@garth-wells : This has been renamed as recommended.

Co-authored-by: Garth N. Wells <gnw20@cam.ac.uk>
@garth-wells garth-wells added the enhancement New feature or request label Mar 8, 2024
@uvilla
Copy link
Contributor Author

uvilla commented Apr 11, 2024

@garth-wells @jhale @jorgensd : Requested changes have been addressed.
I know this is the 11-th hour, but it would be much appreciated if this PR can make the 0.8 release.

We are about to opensource hippylibx---the porting of hippylib to dolfinx---and this new wrapper would be extremely handy.

Thank you

@jhale jhale enabled auto-merge April 12, 2024 19:16
@jhale jhale added this pull request to the merge queue Apr 12, 2024
Merged via the queue into FEniCS:main with commit bf3968a Apr 12, 2024
12 checks passed
jorgensd added a commit to jorgensd/dolfinx_mpc that referenced this pull request May 28, 2024
- Also add some additional `create_connectivity(tdim, tdim)` calls prior to midpoint computations (consequence of FEniCS/dolfinx#3209)
- Switch to spdlog and move some logging to debug mode (FEniCS/dolfinx#3216)
- Support nanobind 2.0.0 (FEniCS/dolfinx#3105)
- Replace `dolfinx.Function.vector` with `dolfinx.Function.x.petsc_vec` (FEniCS/dolfinx#3092)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants