From 6287120431f275affc8bfcf4df5e2c948b43b748 Mon Sep 17 00:00:00 2001 From: Sebastian Blauth Date: Tue, 4 Jul 2023 08:49:06 +0200 Subject: [PATCH 1/2] PETSc Patch for 3.18 --- cashocs/_utils/linalg.py | 52 +++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/cashocs/_utils/linalg.py b/cashocs/_utils/linalg.py index 98d25269..e9dc487a 100644 --- a/cashocs/_utils/linalg.py +++ b/cashocs/_utils/linalg.py @@ -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, @@ -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] @@ -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("") From 9bed654c5eaaab5f4566d547e27bb091d5dea828 Mon Sep 17 00:00:00 2001 From: Sebastian Blauth Date: Tue, 4 Jul 2023 08:50:17 +0200 Subject: [PATCH 2/2] version bump --- .zenodo.json | 4 ++-- cashocs/__init__.py | 2 +- docs/source/_static/version_switcher.json | 4 ++-- docs/source/conf.py | 2 +- pyproject.toml | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.zenodo.json b/.zenodo.json index d21e47ce..b215ad54 100755 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,8 +1,8 @@ { "description": "

cashocs is a computational, adjoint-based shape optimization and optimal control software.

", "license": "GPL-3.0+", - "title": "cashocs v2.0.5", - "version": "v2.0.5", + "title": "cashocs v2.0.7", + "version": "v2.0.7", "upload_type": "software", "creators": [ { diff --git a/cashocs/__init__.py b/cashocs/__init__.py index 5b06a477..86d925ce 100755 --- a/cashocs/__init__.py +++ b/cashocs/__init__.py @@ -60,7 +60,7 @@ from cashocs.nonlinear_solvers import newton_solve from cashocs.nonlinear_solvers import picard_iteration -__version__ = "2.0.5" +__version__ = "2.0.7" __citation__ = """ @Article{Blauth2021cashocs, diff --git a/docs/source/_static/version_switcher.json b/docs/source/_static/version_switcher.json index 02a9d5c1..dc106f60 100755 --- a/docs/source/_static/version_switcher.json +++ b/docs/source/_static/version_switcher.json @@ -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", diff --git a/docs/source/conf.py b/docs/source/conf.py index 86073198..8d4df84f 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -28,7 +28,7 @@ author = "Sebastian Blauth" # The full version, including alpha/beta/rc tags -release = "2.0.5" +release = "2.0.7" # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index 4a189ac6..a58621c0 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta" [project] name = "cashocs" -version = "2.0.5" +version = "2.0.7" description = "Computational Adjoint-Based Shape Optimization and Optimal Control Software" readme = "README.rst" requires-python = ">=3.8"