Skip to content

Commit

Permalink
Fix reference counting for KSP from NewtonSolver in Python (#3190)
Browse files Browse the repository at this point in the history
* Fix reference counting for KSP from NewtonSolver in Python

* Test reference count
  • Loading branch information
garth-wells authored May 2, 2024
1 parent 314db79 commit 4e5af76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 1 addition & 2 deletions python/dolfinx/wrappers/petsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ void petsc_nls_module(nb::module_& m)
{
KSP ksp = self.get_krylov_solver().ksp();
PyObject* obj = PyPetscKSP_New(ksp);
PetscObjectDereference((PetscObject)ksp);
return nb::borrow(obj);
return nb::steal(obj);
})
.def("setF", &dolfinx::nls::petsc::NewtonSolver::setF, nb::arg("F"),
nb::arg("b"))
Expand Down
8 changes: 7 additions & 1 deletion python/test/unit/nls/test_newton.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def J(self, snes, x, J, P):
@pytest.mark.petsc4py
class TestNLS:
def test_linear_pde(self):
"""Test Newton solver for a linear PDE"""
"""Test Newton solver for a linear PDE."""
from petsc4py import PETSc

# Create mesh and function space
Expand Down Expand Up @@ -144,6 +144,12 @@ def update(solver, dx, x):
assert converged
assert n == 1

# Check reference counting
ksp = solver.krylov_solver
assert ksp.refcount == 2
del solver
assert ksp.refcount == 1

def test_nonlinear_pde(self):
"""Test Newton solver for a simple nonlinear PDE"""
from petsc4py import PETSc
Expand Down

0 comments on commit 4e5af76

Please sign in to comment.