Skip to content

Commit e745904

Browse files
get_default_petsc_options
1 parent d7d8372 commit e745904

2 files changed

Lines changed: 42 additions & 52 deletions

File tree

src/festim/hydrogen_transport_problem.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,36 +1675,17 @@ def create_solver(self):
16751675
from dolfinx.fem.petsc import NonlinearProblem
16761676

16771677
if self.petsc_options is None:
1678-
# taken from https://github.com/FEniCS/dolfinx/blob/5fcb988c5b0f46b8f9183bc844d8f533a2130d6a/python/demo/demo_cahn-hilliard.py#L279C1-L286C28
1679-
use_superlu = (
1680-
PETSc.IntType == np.int64
1681-
) # or PETSc.ScalarType == np.complex64
1682-
sys = PETSc.Sys() # type: ignore
1683-
if sys.hasExternalPackage("mumps") and not use_superlu:
1684-
linear_solver = "mumps"
1685-
elif sys.hasExternalPackage("superlu_dist"):
1686-
linear_solver = "superlu_dist"
1687-
else:
1688-
linear_solver = "petsc"
1689-
1690-
petsc_options = {
1691-
"snes_type": "newtonls",
1692-
"snes_linesearch_type": "none",
1693-
"snes_stol": np.sqrt(np.finfo(dolfinx.default_real_type).eps)
1694-
* 1e-2,
1695-
# TODO : make atol and rtol callable
1678+
petsc_options = festim.problem.get_default_petsc_options()
1679+
else:
1680+
petsc_options = self.petsc_options
1681+
1682+
petsc_options.update(
1683+
{
16961684
"snes_atol": self.settings.atol,
16971685
"snes_rtol": self.settings.rtol,
16981686
"snes_max_it": self.settings.max_iterations,
1699-
"snes_divergence_tolerance": "PETSC_UNLIMITED",
1700-
"ksp_type": "preonly",
1701-
"pc_type": "lu",
1702-
"pc_factor_mat_solver_type": linear_solver,
1703-
"snes_error_if_not_converged": True,
1704-
"ksp_error_if_not_converged": True,
17051687
}
1706-
else:
1707-
petsc_options = self.petsc_options
1688+
)
17081689

17091690
self.solver = NonlinearProblem(
17101691
self.forms,

src/festim/problem.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,36 +160,17 @@ def create_solver(self):
160160
from dolfinx.fem.petsc import NonlinearProblem
161161

162162
if self.petsc_options is None:
163-
# taken from https://github.com/FEniCS/dolfinx/blob/5fcb988c5b0f46b8f9183bc844d8f533a2130d6a/python/demo/demo_cahn-hilliard.py#L279C1-L286C28
164-
use_superlu = (
165-
PETSc.IntType == np.int64
166-
) # or PETSc.ScalarType == np.complex64
167-
sys = PETSc.Sys() # type: ignore
168-
if sys.hasExternalPackage("mumps") and not use_superlu:
169-
linear_solver = "mumps"
170-
elif sys.hasExternalPackage("superlu_dist"):
171-
linear_solver = "superlu_dist"
172-
else:
173-
linear_solver = "petsc"
174-
175-
petsc_options = {
176-
"snes_type": "newtonls",
177-
"snes_linesearch_type": "none",
178-
"snes_stol": np.sqrt(np.finfo(dolfinx.default_real_type).eps)
179-
* 1e-2,
180-
# TODO : make atol and rtol callable
163+
petsc_options = get_default_petsc_options()
164+
else:
165+
petsc_options = self.petsc_options
166+
167+
petsc_options.update(
168+
{
181169
"snes_atol": self.settings.atol,
182170
"snes_rtol": self.settings.rtol,
183171
"snes_max_it": self.settings.max_iterations,
184-
"snes_divergence_tolerance": "PETSC_UNLIMITED",
185-
"ksp_type": "preonly",
186-
"pc_type": "lu",
187-
"pc_factor_mat_solver_type": linear_solver,
188-
"snes_error_if_not_converged": True,
189-
"ksp_error_if_not_converged": True,
190172
}
191-
else:
192-
petsc_options = self.petsc_options
173+
)
193174

194175
self.solver = NonlinearProblem(
195176
self.formulation,
@@ -285,3 +266,31 @@ def update_time_dependent_values(self):
285266
for source in self.sources:
286267
if source.value.explicit_time_dependent:
287268
source.value.update(t=t)
269+
270+
271+
# DEFAULT PETSC OPTIONS
272+
273+
# taken from https://github.com/FEniCS/dolfinx/blob/5fcb988c5b0f46b8f9183bc844d8f533a2130d6a/python/demo/demo_cahn-hilliard.py#L279C1-L286C28
274+
use_superlu = PETSc.IntType == np.int64 # or PETSc.ScalarType == np.complex64
275+
sys = PETSc.Sys() # type: ignore
276+
if sys.hasExternalPackage("mumps") and not use_superlu:
277+
linear_solver = "mumps"
278+
elif sys.hasExternalPackage("superlu_dist"):
279+
linear_solver = "superlu_dist"
280+
else:
281+
linear_solver = "petsc"
282+
_DEFAULT_PETSC_OPTS = {
283+
"snes_type": "newtonls",
284+
"snes_linesearch_type": "none",
285+
"snes_stol": np.sqrt(np.finfo(dolfinx.default_real_type).eps) * 1e-2,
286+
"snes_divergence_tolerance": "PETSC_UNLIMITED",
287+
"ksp_type": "preonly",
288+
"pc_type": "lu",
289+
"pc_factor_mat_solver_type": linear_solver,
290+
"snes_error_if_not_converged": True,
291+
"ksp_error_if_not_converged": True,
292+
}
293+
294+
295+
def get_default_petsc_options():
296+
return _DEFAULT_PETSC_OPTS.copy()

0 commit comments

Comments
 (0)