Skip to content

Commit

Permalink
Merge pull request #467 from sblauth/dev/petsc_default_options
Browse files Browse the repository at this point in the history
Make SNES accept the options provided by the petsc_options
  • Loading branch information
sblauth authored Jul 11, 2024
2 parents f6ba790 + 148240e commit 6fb903a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
5 changes: 1 addition & 4 deletions cashocs/_utils/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,7 @@ def solve(
setup_fieldsplit_preconditioner(fun, ksp, options)
setup_petsc_options([ksp], [options])

if rtol is not None:
ksp.rtol = rtol
if atol is not None:
ksp.atol = atol
ksp.setTolerances(rtol=rtol, atol=atol)

ksp.solve(b, x)

Expand Down
26 changes: 15 additions & 11 deletions cashocs/nonlinear_solvers/snes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

default_snes_options: _typing.KspOption = {
"snes_type": "newtonls",
"snes_atol": 1e-10,
"snes_rtol": 1e-10,
"snes_max_it": 50,
}


Expand All @@ -53,9 +56,9 @@ def __init__(
derivative: Optional[ufl.Form] = None,
petsc_options: Optional[_typing.KspOption] = None,
shift: Optional[ufl.Form] = None,
rtol: float = 1e-10,
atol: float = 1e-10,
max_iter: int = 50,
rtol: Optional[float] = None,
atol: Optional[float] = None,
max_iter: Optional[int] = None,
A_tensor: Optional[fenics.PETScMatrix] = None, # pylint: disable=invalid-name
b_tensor: Optional[fenics.PETScVector] = None,
preconditioner_form: Optional[ufl.Form] = None,
Expand All @@ -75,10 +78,12 @@ def __init__(
petsc_options: The options for PETSc SNES object.
shift: A shift term, if the right-hand side of the nonlinear problem is not
zero, but shift.
rtol: Relative tolerance of the solver (default is ``rtol = 1e-10``).
atol: Absolute tolerance of the solver (default is ``atol = 1e-10``).
max_iter: Maximum number of iterations carried out by the method (default is
``max_iter = 50``).
rtol: Relative tolerance of the solver. Overrides the specification in the
petsc_options.
atol: Absolute tolerance of the solver. Overrides the specification in the
petsc_options.
max_iter: Maximum number of iterations carried out by the method. Overrides
the specification in the petsc_options.
A_tensor: A fenics.PETScMatrix for storing the left-hand side of the linear
sub-problem.
b_tensor: A fenics.PETScVector for storing the right-hand side of the linear
Expand Down Expand Up @@ -197,7 +202,6 @@ def assemble_jacobian(
"""
if not self.is_preassembled:
print("Assembling the Jacobian for the SNES solver.")
self.u.vector().vec().setArray(x)
self.u.vector().apply("")

Expand Down Expand Up @@ -251,9 +255,9 @@ def snes_solve(
derivative: Optional[ufl.Form] = None,
petsc_options: Optional[_typing.KspOption] = None,
shift: Optional[ufl.Form] = None,
rtol: float = 1e-10,
atol: float = 1e-10,
max_iter: int = 50,
rtol: Optional[float] = None,
atol: Optional[float] = None,
max_iter: Optional[int] = None,
A_tensor: Optional[fenics.PETScMatrix] = None, # pylint: disable=invalid-name
b_tensor: Optional[fenics.PETScVector] = None,
preconditioner_form: Optional[ufl.Form] = None,
Expand Down

0 comments on commit 6fb903a

Please sign in to comment.