From 364a1b97468ae2fe093540e095fc44a9bf191eaf Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 31 Oct 2024 12:19:37 -0400 Subject: [PATCH] petsc_options as args --- src/festim/hydrogen_transport_problem.py | 2 ++ src/festim/problem.py | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/festim/hydrogen_transport_problem.py b/src/festim/hydrogen_transport_problem.py index ddefd98f1..6ff1cd98f 100644 --- a/src/festim/hydrogen_transport_problem.py +++ b/src/festim/hydrogen_transport_problem.py @@ -149,6 +149,7 @@ def __init__( settings=None, exports=None, traps=None, + petsc_options=None, ): super().__init__( mesh=mesh, @@ -157,6 +158,7 @@ def __init__( subdomains=subdomains, boundary_conditions=boundary_conditions, settings=settings, + petsc_options=petsc_options, ) self.species = species or [] diff --git a/src/festim/problem.py b/src/festim/problem.py index 223f79ad1..76ecaf326 100644 --- a/src/festim/problem.py +++ b/src/festim/problem.py @@ -7,6 +7,7 @@ import ufl from dolfinx import fem from dolfinx.nls.petsc import NewtonSolver +from petsc4py import PETSc import festim as F from festim.mesh.mesh import Mesh as _Mesh @@ -41,7 +42,7 @@ def __init__( subdomains=None, boundary_conditions=None, settings=None, - petcs_options=None, + petsc_options=None, ) -> None: self.mesh = mesh # for arguments to initialise as empty list @@ -60,7 +61,7 @@ def __init__( self.formulation = None self.bc_forms = [] self.show_progress_bar = True - self.petcs_options = petcs_options + self.petsc_options = petsc_options @property def volume_subdomains(self): @@ -123,12 +124,21 @@ def create_solver(self): self.solver.rtol = self.settings.rtol self.solver.max_it = self.settings.max_iterations - if self.petcs_options is None: - ksp = self.solver.krylov_solver + ksp = self.solver.krylov_solver + + if self.petsc_options is None: ksp.setType("preonly") ksp.getPC().setType("lu") ksp.getPC().setFactorSolverType("mumps") ksp.setErrorIfNotConverged(True) + else: + # Set PETSc options + opts = PETSc.Options() + for k, v in self.petsc_options.items(): + opts[k] = v + ksp.setFromOptions() + + print(opts) def run(self): """Runs the model"""