Skip to content

Commit

Permalink
Merge pull request #909 from festim-dev/fix-solver-options
Browse files Browse the repository at this point in the history
Fix solver options
  • Loading branch information
RemDelaporteMathurin authored Nov 1, 2024
2 parents 013937a + dcb89e9 commit ef8d875
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/festim/hydrogen_transport_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def __init__(
settings=None,
exports=None,
traps=None,
petsc_options=None,
):
super().__init__(
mesh=mesh,
Expand All @@ -157,6 +158,7 @@ def __init__(
subdomains=subdomains,
boundary_conditions=boundary_conditions,
settings=settings,
petsc_options=petsc_options,
)

self.species = species or []
Expand Down
17 changes: 13 additions & 4 deletions src/festim/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -123,12 +124,20 @@ 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()
option_prefix = ksp.getOptionsPrefix()
for k, v in self.petsc_options.items():
opts[f"{option_prefix}{k}"] = v
ksp.setFromOptions()

def run(self):
"""Runs the model"""
Expand Down
39 changes: 39 additions & 0 deletions test/system_tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit ef8d875

Please sign in to comment.