MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
petscsolvermodule Module Reference

Data Types

type  petscsolvertype

Functions/Subroutines

class(linearsolverbasetype) function, pointer, public create_petsc_solver (sln_name)
 Create a PETSc solver object.
subroutine petsc_initialize (this, matrix, linear_settings, convergence_summary)
 Initialize PETSc KSP solver with.
subroutine petsc_check_settings (this, linear_settings)
subroutine print_petsc_version (this)
 Print PETSc version string from shared lib.
subroutine get_options_mf6 (this)
 Get the MODFLOW specific options from the PETSc database.
subroutine create_ksp (this)
 Create the PETSc KSP object.
subroutine set_ims_pc (this)
 Set up a custom preconditioner following the ones.
subroutine create_convergence_check (this, convergence_summary)
 Create and assign a custom convergence.
subroutine petsc_solve (this, kiter, rhs, x, cnvg_summary)
subroutine petsc_print_summary (this)
subroutine petsc_destroy (this)
class(matrixbasetype) function, pointer petsc_create_matrix (this)
subroutine print_vec (this, vec, vec_name, kiter)

Function/Subroutine Documentation

◆ create_convergence_check()

subroutine petscsolvermodule::create_convergence_check ( class(petscsolvertype) this,
type(convergencesummarytype), pointer convergence_summary )
private
Parameters
thisThis solver instance

Definition at line 291 of file PetscSolver.F90.

293 class(PetscSolverType) :: this !< This solver instance
294 type(ConvergenceSummaryType), pointer :: convergence_summary
295 ! local
296 petscerrorcode :: ierr
297
298 call this%petsc_ctx%create(this%mat_petsc, this%linear_settings, &
299 convergence_summary)
300
301 if (.not. this%use_ims_cnvgopt) then
302 ! use PETSc residual L2 norm for convergence
303 call developmode('Using PETSc convergence is under development, install &
304 &the nightly build or compile from source with IDEVELOPMODE = 1.')
305 call kspsetconvergencetest(this%ksp_petsc, petsc_cnvg_check_internal, &
306 this%petsc_ctx, petsc_null_function, ierr)
307 else
308 ! IMS convergence check
309 call kspsetconvergencetest(this%ksp_petsc, petsc_cnvg_check, &
310 this%petsc_ctx, petsc_null_function, ierr)
311 end if
312 chkerrq(ierr)
313
This module contains the IMS linear accelerator subroutines.
real(dp) function ims_base_epfact(icnvgopt, kstp)
Function returning EPFACT.
Here is the call graph for this function:

◆ create_ksp()

subroutine petscsolvermodule::create_ksp ( class(petscsolvertype) this)
private
Parameters
thisThis solver instance

Definition at line 210 of file PetscSolver.F90.

211 class(PetscSolverType) :: this !< This solver instance
212 ! local
213 petscerrorcode :: ierr
214 pc :: pc
215
216 call kspcreate(petsc_comm_world, this%ksp_petsc, ierr)
217 chkerrq(ierr)
218
219 ! set prefix for options database
220 call kspsetoptionsprefix(this%ksp_petsc, trim(this%option_prefix), ierr)
221 chkerrq(ierr)
222
223 call kspsetoperators(this%ksp_petsc, this%mat_petsc, this%mat_petsc, ierr)
224 chkerrq(ierr)
225
226 call kspsetinitialguessnonzero(this%ksp_petsc, .true., ierr)
227 chkerrq(ierr)
228
229 call kspsettype(this%ksp_petsc, this%ksp_type, ierr)
230 chkerrq(ierr)
231
232 if (this%use_ims_pc) then
233 call this%set_ims_pc()
234 else
235 call developmode('PETSc preconditioning is under development, install the &
236 &nightly build or compile from source with IDEVELOPMODE = 1.')
237 ! The PC options will be set from the .petscrc
238 ! file in the call to KSPSetFromOptions below
239 call kspgetpc(this%ksp_petsc, pc, ierr)
240 chkerrq(ierr)
241 call pcsetfromoptions(pc, ierr)
242 chkerrq(ierr)
243 end if
244
245 call kspseterrorifnotconverged(this%ksp_petsc, .false., ierr)
246 chkerrq(ierr)
247
248 ! finally override these options from the
249 ! optional .petscrc file
250 call kspsetfromoptions(this%ksp_petsc, ierr)
251 chkerrq(ierr)
252
253 call kspsetup(this%ksp_petsc, ierr)
254 chkerrq(ierr)
255
Here is the call graph for this function:

◆ create_petsc_solver()

class(linearsolverbasetype) function, pointer, public petscsolvermodule::create_petsc_solver ( character(len=lensolutionname) sln_name)
Returns
Uninitialized instance of the PETSc solver
Parameters
sln_namethe solution name

Definition at line 60 of file PetscSolver.F90.

61 class(LinearSolverBaseType), pointer :: solver !< Uninitialized instance of the PETSc solver
62 character(len=LENSOLUTIONNAME) :: sln_name !< the solution name
63 ! local
64 class(PetscSolverType), pointer :: petsc_solver
65 character(len=LINELENGTH) :: errmsg
66
67 allocate (petsc_solver)
68 allocate (petsc_solver%petsc_ctx)
69 petsc_solver%option_prefix = ""
70 if (basesolutionlist%Count() > 1) then
71 petsc_solver%option_prefix = trim(sln_name)//"_"
72 end if
73
74 solver => petsc_solver
75 solver%name = sln_name
76
77 if (simulation_mode /= 'PARALLEL') then
78 write (errmsg, '(a,a)') 'PETSc solver not supported for run mode: ', &
79 trim(simulation_mode)
80 call store_error(errmsg, terminate=.true.)
81 end if
82
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_options_mf6()

subroutine petscsolvermodule::get_options_mf6 ( class(petscsolvertype) this)
private

Definition at line 187 of file PetscSolver.F90.

188 class(PetscSolverType) :: this
189 ! local
190 petscerrorcode :: ierr
191 logical(LGP) :: found
192 logical(LGP) :: use_petsc_pc, use_petsc_cnvg
193
194 use_petsc_pc = .false.
195 call petscoptionsgetbool(petsc_null_options, trim(this%option_prefix), &
196 '-use_petsc_pc', use_petsc_pc, found, ierr)
197 chkerrq(ierr)
198 this%use_ims_pc = .not. use_petsc_pc
199
200 use_petsc_cnvg = .false.
201 call petscoptionsgetbool(petsc_null_options, trim(this%option_prefix), &
202 '-use_petsc_cnvg', use_petsc_cnvg, found, ierr)
203 chkerrq(ierr)
204 this%use_ims_cnvgopt = .not. use_petsc_cnvg
205

◆ petsc_check_settings()

subroutine petscsolvermodule::petsc_check_settings ( class(petscsolvertype) this,
type(imslinearsettingstype), pointer linear_settings )
private

Definition at line 128 of file PetscSolver.F90.

129 class(PetscSolverType) :: this
130 type(ImsLinearSettingsType), pointer :: linear_settings
131 ! local
132 character(len=LINELENGTH) :: warnmsg, errmsg
133
134 ! errors
135 if (linear_settings%ilinmeth /= cg_method .and. &
136 linear_settings%ilinmeth /= bcgs_method) then
137 write (errmsg, '(a,a)') 'PETSc: unknown linear solver method in ', &
138 this%name
139 call store_error(errmsg, terminate=.true.)
140 end if
141
142 ! warnings
143 if (linear_settings%iord > 0) then
144 linear_settings%iord = 0
145 write (warnmsg, '(a)') 'PETSc: IMS reordering not supported'
146 call store_warning(warnmsg)
147 end if
148 if (linear_settings%iscl > 0) then
149 linear_settings%iscl = 0
150 write (warnmsg, '(a)') 'PETSc: IMS matrix scaling not supported'
151 call store_warning(warnmsg)
152 end if
153 if (linear_settings%north > 0) then
154 linear_settings%north = 0
155 write (warnmsg, '(a)') 'PETSc: IMS orthogonalization not supported'
156 call store_warning(warnmsg)
157 end if
158
Here is the call graph for this function:

◆ petsc_create_matrix()

class(matrixbasetype) function, pointer petscsolvermodule::petsc_create_matrix ( class(petscsolvertype) this)
private

Definition at line 468 of file PetscSolver.F90.

469 class(PetscSolverType) :: this
470 class(MatrixBaseType), pointer :: matrix
471 ! local
472 class(PetscMatrixType), pointer :: petsc_matrix
473
474 allocate (petsc_matrix)
475 matrix => petsc_matrix
476

◆ petsc_destroy()

subroutine petscsolvermodule::petsc_destroy ( class(petscsolvertype) this)
private

Definition at line 451 of file PetscSolver.F90.

452 class(PetscSolverType) :: this
453 ! local
454 petscerrorcode :: ierr
455
456 call kspdestroy(this%ksp_petsc, ierr)
457 chkerrq(ierr)
458
459 ! delete context
460 call this%petsc_ctx%destroy()
461 deallocate (this%petsc_ctx)
462
463 call this%pc_context%destroy()
464 deallocate (this%pc_context)
465

◆ petsc_initialize()

subroutine petscsolvermodule::petsc_initialize ( class(petscsolvertype) this,
class(matrixbasetype), pointer matrix,
type(imslinearsettingstype), pointer linear_settings,
type(convergencesummarytype), pointer convergence_summary )
private
Parameters
thisThis solver instance
matrixThe solution matrix as KSP operator
linear_settingsthe settings for the linear solver from the .ims file
convergence_summarya convergence record for diagnostics

Definition at line 87 of file PetscSolver.F90.

88 class(PetscSolverType) :: this !< This solver instance
89 class(MatrixBaseType), pointer :: matrix !< The solution matrix as KSP operator
90 type(ImsLinearSettingsType), pointer :: linear_settings !< the settings for the linear solver from the .ims file
91 type(ConvergenceSummaryType), pointer :: convergence_summary !< a convergence record for diagnostics
92
93 this%linear_settings => linear_settings
94
95 call this%print_petsc_version()
96
97 this%mat_petsc => null()
98 select type (pm => matrix)
99 class is (petscmatrixtype)
100 this%matrix => pm
101 this%mat_petsc => pm%mat
102 end select
103
104 call this%petsc_check_settings(linear_settings)
105
106 this%use_ims_cnvgopt = .true. ! use IMS convergence check, override with .petscrc
107 this%use_ims_pc = .true. ! use IMS preconditioning, override with .petscrc
108 allocate (this%pc_context)
109 call this%pc_context%create(this%matrix, linear_settings)
110
111 if (linear_settings%ilinmeth == cg_method) then
112 this%ksp_type = kspcg
113 else
114 this%ksp_type = kspbcgs
115 end if
116
117 ! get MODFLOW options from PETSc database file
118 call this%get_options_mf6()
119
120 ! create the solver object
121 call this%create_ksp()
122
123 ! Create custom convergence check
124 call this%create_convergence_check(convergence_summary)
125

◆ petsc_print_summary()

subroutine petscsolvermodule::petsc_print_summary ( class(petscsolvertype) this)
private

Definition at line 384 of file PetscSolver.F90.

385 class(PetscSolverType) :: this
386 ! local
387 character(len=128) :: ksp_str, pc_str, subpc_str, &
388 dvclose_str, rclose_str, relax_str, dtol_str
389 character(len=128) :: ksp_logfile
390 integer :: ierr
391 pc :: pc
392 petscviewer :: ksp_viewer
393
394 if (this%use_ims_pc) then
395 call kspgettype(this%ksp_petsc, ksp_str, ierr)
396 chkerrq(ierr)
397 call kspgetpc(this%ksp_petsc, pc, ierr)
398 chkerrq(ierr)
399 call pcgettype(pc, pc_str, ierr)
400 chkerrq(ierr)
401 subpc_str = this%pc_context%ims_pc_type
402
403 write (dvclose_str, '(e15.5)') this%linear_settings%dvclose
404 write (rclose_str, '(e15.5)') this%linear_settings%rclose
405 write (relax_str, '(e15.5)') this%linear_settings%relax
406 write (dtol_str, '(e15.5)') this%linear_settings%droptol
407
408 write (iout, '(/,1x,a)') "PETSc linear solver settings: "
409 write (iout, '(1x,a)') repeat('-', 66)
410 write (iout, '(1x,a,a)') "Linear acceleration method: ", trim(ksp_str)
411 write (iout, '(1x,a,a)') "Preconditioner type: ", trim(pc_str)
412 write (iout, '(1x,a,a)') "Sub-preconditioner type: ", trim(subpc_str)
413 write (iout, '(1x,a,i0)') "Maximum nr. of iterations: ", &
414 this%linear_settings%iter1
415 write (iout, '(1x,a,a)') &
416 "Dep. var. closure criterion: ", trim(adjustl(dvclose_str))
417 write (iout, '(1x,a,a)') &
418 "Residual closure criterion: ", trim(adjustl(rclose_str))
419 if (this%use_ims_cnvgopt) then
420 write (iout, '(1x,a,i0)') &
421 "Residual convergence option: ", this%linear_settings%icnvgopt
422 else
423 write (iout, '(1x,a)') &
424 "Residual convergence option: PETSc L2 norm"
425 end if
426 write (iout, '(1x,a,a)') &
427 "Relaxation factor MILU(T): ", trim(adjustl(relax_str))
428 write (iout, '(1x,a,i0)') &
429 "Fill level in factorization: ", this%linear_settings%level
430 write (iout, '(1x,a,a,/)') &
431 "Drop tolerance level fill: ", trim(adjustl(dtol_str))
432 else
433 ksp_logfile = trim(this%option_prefix)//"ksp_logview.txt"
434 write (iout, '(/,1x,a)') "PETSc linear solver settings from .petscrc: "
435 write (iout, '(1x,a)') repeat('-', 66)
436 write (iout, '(1x,2a)') "see ", trim(ksp_logfile)
437
438 ! collective write
439 call petscviewerasciiopen(petsc_comm_world, ksp_logfile, ksp_viewer, ierr);
440 chkerrq(ierr)
441 call petscviewerpushformat(ksp_viewer, petsc_viewer_ascii_info_detail, ierr)
442 chkerrq(ierr)
443 call kspview(this%ksp_petsc, ksp_viewer, ierr)
444 chkerrq(ierr)
445 call petscviewerdestroy(ksp_viewer, ierr)
446 chkerrq(ierr)
447 end if
448

◆ petsc_solve()

subroutine petscsolvermodule::petsc_solve ( class(petscsolvertype) this,
integer(i4b) kiter,
class(vectorbasetype), pointer rhs,
class(vectorbasetype), pointer x,
type(convergencesummarytype) cnvg_summary )
private

Definition at line 316 of file PetscSolver.F90.

317 class(PetscSolverType) :: this
318 integer(I4B) :: kiter
319 class(VectorBaseType), pointer :: rhs
320 class(VectorBaseType), pointer :: x
321 type(ConvergenceSummaryType) :: cnvg_summary
322 ! local
323 petscerrorcode :: ierr
324 class(PetscVectorType), pointer :: rhs_petsc, x_petsc
325 kspconvergedreason :: cnvg_reason
326 integer :: it_number
327 character(len=LINELENGTH) :: errmsg
328
329 rhs_petsc => null()
330 select type (rhs)
331 class is (petscvectortype)
332 rhs_petsc => rhs
333 end select
334
335 x_petsc => null()
336 select type (x)
337 class is (petscvectortype)
338 x_petsc => x
339 end select
340
341 this%iteration_number = 0
342 this%is_converged = 0
343 if (kiter == 1) then
344 this%petsc_ctx%cnvg_summary%iter_cnt = 0
345 end if
346
347 ! update matrix coefficients
348 call this%matrix%update()
349 call kspsolve(this%ksp_petsc, rhs_petsc%vec_impl, x_petsc%vec_impl, ierr)
350 chkerrq(ierr)
351
352 call kspgetiterationnumber(this%ksp_petsc, it_number, ierr)
353 chkerrq(ierr)
354 this%iteration_number = it_number
355
356 call kspgetconvergedreason(this%ksp_petsc, cnvg_reason, ierr)
357 chkerrq(ierr)
358 if (cnvg_reason > 0) then
359 if (this%petsc_ctx%icnvg_ims == -1) then
360 ! move to next Picard iteration (e.g. with 'STRICT' option)
361 this%is_converged = 0
362 else
363 ! linear convergence reached
364 this%is_converged = 1
365 end if
366 end if
367
368 if (cnvg_reason < 0) then
369 if (cnvg_reason == ksp_diverged_breakdown .or. &
370 cnvg_reason == ksp_diverged_its) then
371 ! out of iterations, or rho/eta became zero,
372 ! move to next Picard iteration and try again
373 this%is_converged = 0
374 else
375 ! this is an solver error, terminate
376 write (errmsg, '(1x,3a,i0)') "PETSc convergence failure in ", &
377 trim(this%name), ": ", cnvg_reason
378 call store_error(errmsg, terminate=.true.)
379 end if
380 end if
381
Here is the call graph for this function:

◆ print_petsc_version()

subroutine petscsolvermodule::print_petsc_version ( class(petscsolvertype) this)
private

Definition at line 163 of file PetscSolver.F90.

164 class(PetscSolverType) :: this
165 ! local
166 petscerrorcode :: ierr
167 petscint :: major, minor, subminor, release
168 character(len=128) :: petsc_version, release_str
169
170 call petscgetversionnumber(major, minor, subminor, release, ierr)
171 chkerrq(ierr)
172
173 if (release == 1) then
174 release_str = "(release)"
175 else
176 release_str = "(unofficial)"
177 end if
178 write (petsc_version, '(i0,a,i0,a,i0,a,a)') &
179 major, ".", minor, ".", subminor, " ", trim(release_str)
180 write (iout, '(/,1x,4a,/)') "PETSc Linear Solver will be used for ", &
181 trim(this%name), ": version ", petsc_version
182

◆ print_vec()

subroutine petscsolvermodule::print_vec ( class(petscsolvertype) this,
class(petscvectortype) vec,
character(len=*) vec_name,
integer(i4b) kiter )
private

Definition at line 479 of file PetscSolver.F90.

480 use tdismodule, only: nper, kstp
481 class(PetscSolverType) :: this
482 class(PetscVectorType) :: vec
483 character(len=*) :: vec_name
484 integer(I4B) :: kiter
485 ! local
486 petscviewer :: viewer
487 character(len=24) :: filename
488 petscerrorcode :: ierr
489
490 write (filename, '(2a,i0,a,i0,a,i0,a)') vec_name, '_', nper, &
491 '_', kstp, '_', kiter, '.txt'
492 call petscviewerasciiopen(petsc_comm_world, filename, viewer, ierr)
493 chkerrq(ierr)
494 call vecview(vec%vec_impl, viewer, ierr)
495 chkerrq(ierr)
496 call petscviewerdestroy(viewer, ierr)
497 chkerrq(ierr)
498
integer(i4b), pointer, public kstp
current time step number
Definition tdis.f90:27
integer(i4b), pointer, public nper
number of stress period
Definition tdis.f90:24

◆ set_ims_pc()

subroutine petscsolvermodule::set_ims_pc ( class(petscsolvertype) this)
private
Parameters
thisThis solver instance

Definition at line 260 of file PetscSolver.F90.

261 class(PetscSolverType) :: this !< This solver instance
262 ! local
263 pc :: pc, sub_pc
264 ksp, dimension(1) :: sub_ksp
265 petscint :: n_local, n_first
266 petscerrorcode :: ierr
267
268 call kspgetpc(this%ksp_petsc, pc, ierr)
269 chkerrq(ierr)
270 call pcsettype(pc, pcbjacobi, ierr)
271 chkerrq(ierr)
272 call pcsetup(pc, ierr)
273 chkerrq(ierr)
274 call pcbjacobigetsubksp(pc, n_local, n_first, sub_ksp, ierr)
275 chkerrq(ierr)
276 call kspgetpc(sub_ksp(1), sub_pc, ierr)
277 chkerrq(ierr)
278 call pcsettype(sub_pc, pcshell, ierr)
279 chkerrq(ierr)
280 call pcshellsetapply(sub_pc, pcshell_apply, ierr)
281 chkerrq(ierr)
282 call pcshellsetsetup(sub_pc, pcshell_setup, ierr)
283 chkerrq(ierr)
284 call pcshellsetcontext(sub_pc, this%pc_context, ierr)
285 chkerrq(ierr)
286
Here is the call graph for this function: