Skip to content

Commit 9cb8a2a

Browse files
further refactoring
1 parent 491a400 commit 9cb8a2a

2 files changed

Lines changed: 41 additions & 53 deletions

File tree

src/festim/hydrogen_transport_problem.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,32 +1674,7 @@ def create_solver(self):
16741674
elif Version(dolfinx.__version__) > Version("0.9.0"):
16751675
from dolfinx.fem.petsc import NonlinearProblem
16761676

1677-
petsc_options = festim.problem.get_default_petsc_options()
1678-
1679-
# Update default PETSc options with user-provided options, if any
1680-
if self.petsc_options:
1681-
petsc_options.update(self.petsc_options)
1682-
1683-
if (
1684-
"snes_atol" in self.petsc_options
1685-
or "snes_rtol" in self.petsc_options
1686-
or "snes_max_it" in self.petsc_options
1687-
):
1688-
warnings.warn(
1689-
"You have set one of the following PETSc options: snes_atol, "
1690-
"snes_rtol or snes_max_it. These options will be overwritten by "
1691-
"the values in festim.Settings (atol, rtol and max_iterations) to "
1692-
"ensure consistency between different versions of dolfinx. If you "
1693-
"want to set these options manually, please set them in "
1694-
"festim.Settings and not in the petsc_options dictionary."
1695-
)
1696-
petsc_options.update(
1697-
{
1698-
"snes_atol": self.settings.atol,
1699-
"snes_rtol": self.settings.rtol,
1700-
"snes_max_it": self.settings.max_iterations,
1701-
}
1702-
)
1677+
petsc_options = self.get_petsc_options()
17031678

17041679
self.solver = NonlinearProblem(
17051680
self.forms,

src/festim/problem.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,45 @@ def define_boundary_conditions(self):
119119
form = self.create_dirichletbc_form(bc)
120120
self.bc_forms.append(form)
121121

122+
def get_petsc_options(self) -> dict[str, Any]:
123+
"""
124+
Gets the PETSc options to pass to the NewtonProblem solver. Default
125+
options are updated with user-provided options, if any.
126+
127+
Returns:
128+
the petsc options to pass to the NewtonProblem solver.
129+
"""
130+
petsc_options = get_default_petsc_options()
131+
132+
# Update default PETSc options with user-provided options, if any
133+
if self.petsc_options:
134+
petsc_options.update(self.petsc_options)
135+
136+
if self.petsc_options:
137+
if (
138+
"snes_atol" in self.petsc_options
139+
or "snes_rtol" in self.petsc_options
140+
or "snes_max_it" in self.petsc_options
141+
):
142+
warnings.warn(
143+
"You have set one of the following PETSc options: snes_atol, "
144+
"snes_rtol or snes_max_it. These options will be overwritten by "
145+
"the values in festim.Settings (atol, rtol and max_iterations) to "
146+
"ensure consistency between different versions of dolfinx. If you "
147+
"want to set these options manually, please set them in "
148+
"festim.Settings and not in the petsc_options dictionary."
149+
)
150+
151+
petsc_options.update(
152+
{
153+
"snes_atol": self.settings.atol,
154+
"snes_rtol": self.settings.rtol,
155+
"snes_max_it": self.settings.max_iterations,
156+
}
157+
)
158+
159+
return petsc_options
160+
122161
def create_solver(self):
123162
"""Creates the solver of the model"""
124163

@@ -160,33 +199,7 @@ def create_solver(self):
160199
elif Version(dolfinx.__version__) > Version("0.9.0"):
161200
from dolfinx.fem.petsc import NonlinearProblem
162201

163-
petsc_options = get_default_petsc_options()
164-
165-
# Update default PETSc options with user-provided options, if any
166-
if self.petsc_options:
167-
petsc_options.update(self.petsc_options)
168-
169-
if (
170-
"snes_atol" in self.petsc_options
171-
or "snes_rtol" in self.petsc_options
172-
or "snes_max_it" in self.petsc_options
173-
):
174-
warnings.warn(
175-
"You have set one of the following PETSc options: snes_atol, "
176-
"snes_rtol or snes_max_it. These options will be overwritten by "
177-
"the values in festim.Settings (atol, rtol and max_iterations) to "
178-
"ensure consistency between different versions of dolfinx. If you "
179-
"want to set these options manually, please set them in "
180-
"festim.Settings and not in the petsc_options dictionary."
181-
)
182-
petsc_options.update(
183-
{
184-
"snes_atol": self.settings.atol,
185-
"snes_rtol": self.settings.rtol,
186-
"snes_max_it": self.settings.max_iterations,
187-
}
188-
)
189-
202+
petsc_options = self.get_petsc_options()
190203
self.solver = NonlinearProblem(
191204
self.formulation,
192205
self.u,

0 commit comments

Comments
 (0)