@@ -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