Skip to content

Commit

Permalink
Merge pull request #261 from sblauth/hotfix/2.0.7
Browse files Browse the repository at this point in the history
Hotfix/2.0.7
  • Loading branch information
sblauth authored Jul 4, 2023
2 parents 70b9ada + 68b91ae commit 1afffcd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
52 changes: 38 additions & 14 deletions cashocs/_utils/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,40 @@ def define_ksp_options(
return options


def setup_matrix_and_preconditioner(
ksp: PETSc.KSP,
A: Optional[PETSc.Mat] = None, # pylint: disable=invalid-name
P: Optional[PETSc.Mat] = None, # pylint: disable=invalid-name
) -> PETSc.Mat:
"""Set up the system matrix and preconditioner for a linear solve.
Args:
ksp: The KSP object used to solve the problem.
A: The system matrix or `None`.
P: The preconditioner matrix or `None`.
Returns:
The system matrix.
"""
if A is not None:
if P is None:
ksp.setOperators(A)
else:
ksp.setOperators(A, P)
else:
A = ksp.getOperators()[0]
if A.size[0] == -1 and A.size[1] == -1:
raise _exceptions.InputError(
"cashocs._utils.solve_linear_problem",
"ksp",
"The KSP object has to be initialized with some Matrix in case A is "
"None.",
)

return A


def solve_linear_problem(
A: Optional[PETSc.Mat] = None, # pylint: disable=invalid-name
b: Optional[PETSc.Vec] = None,
Expand Down Expand Up @@ -318,20 +352,7 @@ def solve_linear_problem(
comm = _initialize_comm(comm)
ksp = PETSc.KSP().create(comm=comm)

if A is not None:
if P is None:
ksp.setOperators(A)
else:
ksp.setOperators(A, P)
else:
A = ksp.getOperators()[0]
if A.size[0] == -1 and A.size[1] == -1:
raise _exceptions.InputError(
"cashocs._utils.solve_linear_problem",
"ksp",
"The KSP object has to be initialized with some Matrix in case A is "
"None.",
)
A = setup_matrix_and_preconditioner(ksp, A, P)

if b is None:
return A.getVecs()[0]
Expand All @@ -355,6 +376,9 @@ def solve_linear_problem(
if ksp.getConvergedReason() < 0:
raise _exceptions.PETScKSPError(ksp.getConvergedReason())

if hasattr(PETSc, "garbage_cleanup"):
PETSc.garbage_cleanup(comm=comm)

if fun is not None:
fun.vector().apply("")

Expand Down
4 changes: 2 additions & 2 deletions docs/source/_static/version_switcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
},
{
"name": "2.0 (stable)",
"version": "2.0.5",
"url": "https://cashocs.readthedocs.io/en/v2.0.5/"
"version": "2.0.7",
"url": "https://cashocs.readthedocs.io/en/v2.0.7/"
},
{
"name": "1.8",
Expand Down

0 comments on commit 1afffcd

Please sign in to comment.