From 364a1b97468ae2fe093540e095fc44a9bf191eaf Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 31 Oct 2024 12:19:37 -0400 Subject: [PATCH 1/3] 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""" From 3f9767fbd2b52b3a9a230f62d8225470518e2ae3 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 31 Oct 2024 12:56:45 -0400 Subject: [PATCH 2/3] added prefix --- src/festim/problem.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/festim/problem.py b/src/festim/problem.py index 76ecaf326..ff4b36c26 100644 --- a/src/festim/problem.py +++ b/src/festim/problem.py @@ -134,12 +134,11 @@ def create_solver(self): else: # Set PETSc options opts = PETSc.Options() + option_prefix = ksp.getOptionsPrefix() for k, v in self.petsc_options.items(): - opts[k] = v + opts[f"{option_prefix}{k}"] = v ksp.setFromOptions() - print(opts) - def run(self): """Runs the model""" From dcb89e917fee13de189456dd25b163de013262db Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 31 Oct 2024 15:37:06 -0400 Subject: [PATCH 3/3] added a test that adds a unused variable to the petsc options --- test/system_tests/test_misc.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/system_tests/test_misc.py diff --git a/test/system_tests/test_misc.py b/test/system_tests/test_misc.py new file mode 100644 index 000000000..ba622cb1b --- /dev/null +++ b/test/system_tests/test_misc.py @@ -0,0 +1,39 @@ +import dolfinx +import festim as F +import numpy as np + + +def test_petsc_options(): + my_model = F.HydrogenTransportProblem( + petsc_options={ + "ksp_type": "preonly", + "pc_type": "lu", + "pc_factor_mat_solver_type": "mumps", + "coucoucou": 3, + } + ) + + tungsten = F.Material(D_0=1, E_D=0, K_S_0=1, E_K_S=0) + + my_model.mesh = F.Mesh1D(vertices=np.linspace(0, 1, num=100)) + vol1 = F.VolumeSubdomain1D(id=1, material=tungsten, borders=[0, 1]) + surface1 = F.SurfaceSubdomain1D(id=4, x=0) + surface2 = F.SurfaceSubdomain1D(id=5, x=1) + + my_model.subdomains = [vol1, surface1, surface2] + + mobile = F.Species(name="H", mobile=True) + + my_model.species = [mobile] + + my_model.temperature = 600 + + my_model.boundary_conditions = [ + F.FixedConcentrationBC(subdomain=surface1, species=mobile, value=1), + F.FixedConcentrationBC(subdomain=surface2, species=mobile, value=0), + ] + + my_model.settings = F.Settings(atol=1e-6, rtol=1e-6, transient=False) + + my_model.initialise() + my_model.run()