MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
petscsolvermodule Module Reference

Data Types

type  petscsolvertype
 

Functions/Subroutines

class(linearsolverbasetype) function, pointer, public create_petsc_solver (sln_name)
 Create a PETSc solver object. More...
 
subroutine petsc_initialize (this, matrix, linear_settings, convergence_summary)
 Initialize PETSc KSP solver with. More...
 
subroutine petsc_check_settings (this, linear_settings)
 
subroutine print_petsc_version (this)
 Print PETSc version string from shared lib. More...
 
subroutine get_options_mf6 (this)
 Get the MODFLOW specific options from the PETSc database. More...
 
subroutine create_ksp (this)
 Create the PETSc KSP object. More...
 
subroutine set_ims_pc (this)
 Set up a custom preconditioner following the ones. More...
 
subroutine create_convergence_check (this, convergence_summary)
 Create and assign a custom convergence. More...
 
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 282 of file PetscSolver.F90.

284  class(PetscSolverType) :: this !< This solver instance
285  type(ConvergenceSummaryType), pointer :: convergence_summary
286  ! local
287  petscerrorcode :: ierr
288 
289  call this%petsc_ctx%create(this%mat_petsc, this%linear_settings, &
290  convergence_summary)
291  if (.not. this%use_ims_cnvgopt) then
292  ! use PETSc residual L2 norm for convergence
293  call dev_feature('Using PETSc convergence is under development, install &
294  &the nightly build or compile from source with IDEVELOPMODE = 1.')
295  this%petsc_ctx%icnvgopt = 100
296  end if
297 
298  call kspsetconvergencetest(this%ksp_petsc, petsc_cnvg_check, &
299  this%petsc_ctx, petsc_null_function, ierr)
300  chkerrq(ierr)
301 
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 205 of file PetscSolver.F90.

206  class(PetscSolverType) :: this !< This solver instance
207  ! local
208  petscerrorcode :: ierr
209  pc :: pc
210 
211  call kspcreate(petsc_comm_world, this%ksp_petsc, ierr)
212  chkerrq(ierr)
213 
214  call kspsetoperators(this%ksp_petsc, this%mat_petsc, this%mat_petsc, ierr)
215  chkerrq(ierr)
216 
217  call kspsetinitialguessnonzero(this%ksp_petsc, .true., ierr)
218  chkerrq(ierr)
219 
220  call kspsettype(this%ksp_petsc, this%ksp_type, ierr)
221  chkerrq(ierr)
222 
223  if (this%use_ims_pc) then
224  call this%set_ims_pc()
225  else
226  call dev_feature('PETSc preconditioning is under development, install the &
227  &nightly build or compile from source with IDEVELOPMODE = 1.')
228  ! The PC options will be set from the .petscrc
229  ! file in the call to KSPSetFromOptions below
230  call kspgetpc(this%ksp_petsc, pc, ierr)
231  chkerrq(ierr)
232  call pcsetfromoptions(pc, ierr)
233  chkerrq(ierr)
234  end if
235 
236  call kspseterrorifnotconverged(this%ksp_petsc, .false., ierr)
237  chkerrq(ierr)
238 
239  ! finally override these options from the
240  ! optional .petscrc file
241  call kspsetfromoptions(this%ksp_petsc, ierr)
242  chkerrq(ierr)
243 
244  call kspsetup(this%ksp_petsc, ierr)
245  chkerrq(ierr)
246 
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 59 of file PetscSolver.F90.

60  class(LinearSolverBaseType), pointer :: solver !< Uninitialized instance of the PETSc solver
61  character(len=LENSOLUTIONNAME) :: sln_name !< the solution name
62  ! local
63  class(PetscSolverType), pointer :: petsc_solver
64  character(len=LINELENGTH) :: errmsg
65 
66  allocate (petsc_solver)
67  allocate (petsc_solver%petsc_ctx)
68 
69  solver => petsc_solver
70  solver%name = sln_name
71 
72  if (simulation_mode /= 'PARALLEL') then
73  write (errmsg, '(a,a)') 'PETSc solver not supported for run mode: ', &
74  trim(simulation_mode)
75  call store_error(errmsg, terminate=.true.)
76  end if
77 
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 182 of file PetscSolver.F90.

183  class(PetscSolverType) :: this
184  ! local
185  petscerrorcode :: ierr
186  logical(LGP) :: found
187  logical(LGP) :: use_petsc_pc, use_petsc_cnvg
188 
189  use_petsc_pc = .false.
190  call petscoptionsgetbool(petsc_null_options, petsc_null_character, &
191  '-use_petsc_pc', use_petsc_pc, found, ierr)
192  chkerrq(ierr)
193  this%use_ims_pc = .not. use_petsc_pc
194 
195  use_petsc_cnvg = .false.
196  call petscoptionsgetbool(petsc_null_options, petsc_null_character, &
197  '-use_petsc_cnvg', use_petsc_cnvg, found, ierr)
198  chkerrq(ierr)
199  this%use_ims_cnvgopt = .not. use_petsc_cnvg
200 

◆ petsc_check_settings()

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

Definition at line 123 of file PetscSolver.F90.

124  class(PetscSolverType) :: this
125  type(ImsLinearSettingsType), pointer :: linear_settings
126  ! local
127  character(len=LINELENGTH) :: warnmsg, errmsg
128 
129  ! errors
130  if (linear_settings%ilinmeth /= cg_method .and. &
131  linear_settings%ilinmeth /= bcgs_method) then
132  write (errmsg, '(a,a)') 'PETSc: unknown linear solver method in ', &
133  this%name
134  call store_error(errmsg, terminate=.true.)
135  end if
136 
137  ! warnings
138  if (linear_settings%iord > 0) then
139  linear_settings%iord = 0
140  write (warnmsg, '(a)') 'PETSc: IMS reordering not supported'
141  call store_warning(warnmsg)
142  end if
143  if (linear_settings%iscl > 0) then
144  linear_settings%iscl = 0
145  write (warnmsg, '(a)') 'PETSc: IMS matrix scaling not supported'
146  call store_warning(warnmsg)
147  end if
148  if (linear_settings%north > 0) then
149  linear_settings%north = 0
150  write (warnmsg, '(a)') 'PETSc: IMS orthogonalization not supported'
151  call store_warning(warnmsg)
152  end if
153 
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 448 of file PetscSolver.F90.

449  class(PetscSolverType) :: this
450  class(MatrixBaseType), pointer :: matrix
451  ! local
452  class(PetscMatrixType), pointer :: petsc_matrix
453 
454  allocate (petsc_matrix)
455  matrix => petsc_matrix
456 

◆ petsc_destroy()

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

Definition at line 431 of file PetscSolver.F90.

432  class(PetscSolverType) :: this
433  ! local
434  petscerrorcode :: ierr
435 
436  call kspdestroy(this%ksp_petsc, ierr)
437  chkerrq(ierr)
438 
439  ! delete context
440  call this%petsc_ctx%destroy()
441  deallocate (this%petsc_ctx)
442 
443  call this%pc_context%destroy()
444  deallocate (this%pc_context)
445 

◆ 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 82 of file PetscSolver.F90.

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

◆ petsc_print_summary()

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

Definition at line 364 of file PetscSolver.F90.

365  class(PetscSolverType) :: this
366  ! local
367  character(len=128) :: ksp_str, pc_str, subpc_str, &
368  dvclose_str, rclose_str, relax_str, dtol_str
369  character(len=128) :: ksp_logfile
370  integer :: ierr
371  pc :: pc
372  petscviewer :: ksp_viewer
373 
374  if (this%use_ims_pc) then
375  call kspgettype(this%ksp_petsc, ksp_str, ierr)
376  chkerrq(ierr)
377  call kspgetpc(this%ksp_petsc, pc, ierr)
378  chkerrq(ierr)
379  call pcgettype(pc, pc_str, ierr)
380  chkerrq(ierr)
381  subpc_str = this%pc_context%ims_pc_type
382 
383  write (dvclose_str, '(e15.5)') this%linear_settings%dvclose
384  write (rclose_str, '(e15.5)') this%linear_settings%rclose
385  write (relax_str, '(e15.5)') this%linear_settings%relax
386  write (dtol_str, '(e15.5)') this%linear_settings%droptol
387 
388  write (iout, '(/,1x,a)') "PETSc linear solver settings: "
389  write (iout, '(1x,a)') repeat('-', 66)
390  write (iout, '(1x,a,a)') "Linear acceleration method: ", trim(ksp_str)
391  write (iout, '(1x,a,a)') "Preconditioner type: ", trim(pc_str)
392  write (iout, '(1x,a,a)') "Sub-preconditioner type: ", trim(subpc_str)
393  write (iout, '(1x,a,i0)') "Maximum nr. of iterations: ", &
394  this%linear_settings%iter1
395  write (iout, '(1x,a,a)') &
396  "Dep. var. closure criterion: ", trim(adjustl(dvclose_str))
397  write (iout, '(1x,a,a)') &
398  "Residual closure criterion: ", trim(adjustl(rclose_str))
399  if (this%use_ims_cnvgopt) then
400  write (iout, '(1x,a,i0)') &
401  "Residual convergence option: ", this%linear_settings%icnvgopt
402  else
403  write (iout, '(1x,a)') &
404  "Residual convergence option: PETSc L2 norm"
405  end if
406  write (iout, '(1x,a,a)') &
407  "Relaxation factor MILU(T): ", trim(adjustl(relax_str))
408  write (iout, '(1x,a,i0)') &
409  "Fill level in factorization: ", this%linear_settings%level
410  write (iout, '(1x,a,a,/)') &
411  "Drop tolerance level fill: ", trim(adjustl(dtol_str))
412  else
413  ksp_logfile = "ksp_logview.txt"
414  write (iout, '(/,1x,a)') "PETSc linear solver settings from .petscrc: "
415  write (iout, '(1x,a)') repeat('-', 66)
416  write (iout, '(1x,2a)') "see ", trim(ksp_logfile)
417 
418  ! collective write
419  call petscviewerasciiopen(petsc_comm_world, ksp_logfile, ksp_viewer, ierr);
420  chkerrq(ierr)
421  call petscviewerpushformat(ksp_viewer, petsc_viewer_ascii_info_detail, ierr)
422  chkerrq(ierr)
423  call kspview(this%ksp_petsc, ksp_viewer, ierr)
424  chkerrq(ierr)
425  call petscviewerdestroy(ksp_viewer, ierr)
426  chkerrq(ierr)
427  end if
428 

◆ petsc_solve()

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

Definition at line 304 of file PetscSolver.F90.

305  class(PetscSolverType) :: this
306  integer(I4B) :: kiter
307  class(VectorBaseType), pointer :: rhs
308  class(VectorBaseType), pointer :: x
309  type(ConvergenceSummaryType) :: cnvg_summary
310  ! local
311  petscerrorcode :: ierr
312  class(PetscVectorType), pointer :: rhs_petsc, x_petsc
313  kspconvergedreason :: cnvg_reason
314  integer :: it_number
315  character(len=LINELENGTH) :: errmsg
316 
317  rhs_petsc => null()
318  select type (rhs)
319  class is (petscvectortype)
320  rhs_petsc => rhs
321  end select
322 
323  x_petsc => null()
324  select type (x)
325  class is (petscvectortype)
326  x_petsc => x
327  end select
328 
329  this%iteration_number = 0
330  this%is_converged = 0
331  if (kiter == 1) then
332  this%petsc_ctx%cnvg_summary%iter_cnt = 0
333  end if
334 
335  ! update matrix coefficients
336  call this%matrix%update()
337  call kspsolve(this%ksp_petsc, rhs_petsc%vec_impl, x_petsc%vec_impl, ierr)
338  chkerrq(ierr)
339 
340  call kspgetiterationnumber(this%ksp_petsc, it_number, ierr)
341  chkerrq(ierr)
342  this%iteration_number = it_number
343 
344  call kspgetconvergedreason(this%ksp_petsc, cnvg_reason, ierr)
345  chkerrq(ierr)
346  if (cnvg_reason > 0) then
347  if (this%petsc_ctx%icnvg_ims == -1) then
348  ! move to next Picard iteration (e.g. with 'STRICT' option)
349  this%is_converged = 0
350  else
351  ! linear convergence reached
352  this%is_converged = 1
353  end if
354  end if
355 
356  if (cnvg_reason < 0 .and. cnvg_reason /= ksp_diverged_its) then
357  write (errmsg, '(1x,3a,i0)') "PETSc convergence failure in ", &
358  trim(this%name), ": ", cnvg_reason
359  call store_error(errmsg, terminate=.true.)
360  end if
361 
Here is the call graph for this function:

◆ print_petsc_version()

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

Definition at line 158 of file PetscSolver.F90.

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

◆ print_vec()

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

Definition at line 459 of file PetscSolver.F90.

460  use tdismodule, only: nper, kstp
461  class(PetscSolverType) :: this
462  class(PetscVectorType) :: vec
463  character(len=*) :: vec_name
464  integer(I4B) :: kiter
465  ! local
466  petscviewer :: viewer
467  character(len=24) :: filename
468  petscerrorcode :: ierr
469 
470  write (filename, '(2a,i0,a,i0,a,i0,a)') vec_name, '_', nper, &
471  '_', kstp, '_', kiter, '.txt'
472  call petscviewerasciiopen(petsc_comm_world, filename, viewer, ierr)
473  chkerrq(ierr)
474  call vecview(vec%vec_impl, viewer, ierr)
475  chkerrq(ierr)
476  call petscviewerdestroy(viewer, ierr)
477  chkerrq(ierr)
478 
integer(i4b), pointer, public kstp
current time step number
Definition: tdis.f90:24
integer(i4b), pointer, public nper
number of stress period
Definition: tdis.f90:21

◆ set_ims_pc()

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

Definition at line 251 of file PetscSolver.F90.

252  class(PetscSolverType) :: this !< This solver instance
253  ! local
254  pc :: pc, sub_pc
255  ksp, dimension(1) :: sub_ksp
256  petscint :: n_local, n_first
257  petscerrorcode :: ierr
258 
259  call kspgetpc(this%ksp_petsc, pc, ierr)
260  chkerrq(ierr)
261  call pcsettype(pc, pcbjacobi, ierr)
262  chkerrq(ierr)
263  call pcsetup(pc, ierr)
264  chkerrq(ierr)
265  call pcbjacobigetsubksp(pc, n_local, n_first, sub_ksp, ierr)
266  chkerrq(ierr)
267  call kspgetpc(sub_ksp(1), sub_pc, ierr)
268  chkerrq(ierr)
269  call pcsettype(sub_pc, pcshell, ierr)
270  chkerrq(ierr)
271  call pcshellsetapply(sub_pc, pcshell_apply, ierr)
272  chkerrq(ierr)
273  call pcshellsetsetup(sub_pc, pcshell_setup, ierr)
274  chkerrq(ierr)
275  call pcshellsetcontext(sub_pc, this%pc_context, ierr)
276  chkerrq(ierr)
277 
Here is the call graph for this function: