MODFLOW 6  version 6.5.0.dev2
MODFLOW 6 Code Documentation
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_petsc_pc (this)
 Set up a standard PETSc preconditioner from. 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 296 of file PetscSolver.F90.

298  class(PetscSolverType) :: this !< This solver instance
299  type(ConvergenceSummaryType), pointer :: convergence_summary
300  ! local
301  petscerrorcode :: ierr
302 
303  call this%petsc_ctx%create(this%mat_petsc, this%linear_settings, &
304  convergence_summary)
305 
306  call kspsetconvergencetest(this%ksp_petsc, petsc_check_convergence, &
307  this%petsc_ctx, petsc_null_function, ierr)
308  chkerrq(ierr)
309 
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 199 of file PetscSolver.F90.

200  class(PetscSolverType) :: this !< This solver instance
201  ! local
202  petscerrorcode :: ierr
203 
204  call kspcreate(petsc_comm_world, this%ksp_petsc, ierr)
205  chkerrq(ierr)
206 
207  call kspsetoperators(this%ksp_petsc, this%mat_petsc, this%mat_petsc, ierr)
208  chkerrq(ierr)
209 
210  call kspsetinitialguessnonzero(this%ksp_petsc, .true., ierr)
211  chkerrq(ierr)
212 
213  call kspsettype(this%ksp_petsc, this%ksp_type, ierr)
214  chkerrq(ierr)
215 
216  if (this%use_ims_pc) then
217  call this%set_ims_pc()
218  else
219  call this%set_petsc_pc()
220  end if
221 
222  call kspseterrorifnotconverged(this%ksp_petsc, .false., ierr)
223  chkerrq(ierr)
224 
225  ! finally override these options from the
226  ! optional .petscrc file
227  call kspsetfromoptions(this%ksp_petsc, ierr)
228  chkerrq(ierr)
229 

◆ 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 
70  solver => petsc_solver
71  solver%name = sln_name
72 
73  if (simulation_mode /= 'PARALLEL') then
74  write (errmsg, '(a,a)') 'PETSc solver not supported for run mode: ', &
75  trim(simulation_mode)
76  call store_error(errmsg, terminate=.true.)
77  end if
78 
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 185 of file PetscSolver.F90.

186  class(PetscSolverType) :: this
187  ! local
188  petscerrorcode :: ierr
189  logical(LGP) :: found
190 
191  call petscoptionsgetbool(petsc_null_options, petsc_null_character, &
192  '-ims_pc', this%use_ims_pc, found, ierr)
193  chkerrq(ierr)
194 

◆ petsc_check_settings()

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

Definition at line 126 of file PetscSolver.F90.

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

437  class(PetscSolverType) :: this
438  class(MatrixBaseType), pointer :: matrix
439  ! local
440  class(PetscMatrixType), pointer :: petsc_matrix
441 
442  allocate (petsc_matrix)
443  matrix => petsc_matrix
444 

◆ petsc_destroy()

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

Definition at line 419 of file PetscSolver.F90.

420  class(PetscSolverType) :: this
421  ! local
422  petscerrorcode :: ierr
423 
424  call kspdestroy(this%ksp_petsc, ierr)
425  chkerrq(ierr)
426 
427  ! delete context
428  call this%petsc_ctx%destroy()
429  deallocate (this%petsc_ctx)
430 
431  call this%pc_context%destroy()
432  deallocate (this%pc_context)
433 

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

84  class(PetscSolverType) :: this !< This solver instance
85  class(MatrixBaseType), pointer :: matrix !< The solution matrix as KSP operator
86  type(ImsLinearSettingsType), pointer :: linear_settings !< the settings for the linear solver from the .ims file
87  type(ConvergenceSummaryType), pointer :: convergence_summary !< a convergence record for diagnostics
88 
89  this%linear_settings => linear_settings
90 
91  call this%print_petsc_version()
92 
93  this%mat_petsc => null()
94  select type (pm => matrix)
95  class is (petscmatrixtype)
96  this%matrix => pm
97  this%mat_petsc => pm%mat
98  end select
99 
100  call this%petsc_check_settings(linear_settings)
101 
102  this%use_ims_pc = .true. ! default is true, 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  this%pc_type = pcbjacobi
113  this%sub_pc_type = pcilu
114 
115  ! get MODFLOW options from PETSc database file
116  call this%get_options_mf6()
117 
118  ! create the solver object
119  call this%create_ksp()
120 
121  ! Create custom convergence check
122  call this%create_convergence_check(convergence_summary)
123 

◆ petsc_print_summary()

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

Definition at line 372 of file PetscSolver.F90.

373  class(PetscSolverType) :: this
374  ! local
375  character(len=128) :: ksp_str, pc_str, subpc_str, &
376  dvclose_str, rclose_str, relax_str, dtol_str
377  integer :: ierr
378  pc :: pc
379 
380  call kspgettype(this%ksp_petsc, ksp_str, ierr)
381  chkerrq(ierr)
382  call kspgetpc(this%ksp_petsc, pc, ierr)
383  chkerrq(ierr)
384  call pcgettype(pc, pc_str, ierr)
385  chkerrq(ierr)
386  if (this%use_ims_pc) then
387  subpc_str = this%pc_context%ims_pc_type
388  else
389  subpc_str = this%sub_pc_type
390  end if
391 
392  write (dvclose_str, '(e15.5)') this%linear_settings%dvclose
393  write (rclose_str, '(e15.5)') this%linear_settings%rclose
394  write (relax_str, '(e15.5)') this%linear_settings%relax
395  write (dtol_str, '(e15.5)') this%linear_settings%droptol
396 
397  write (iout, '(/,7x,a)') "PETSc linear solver settings: "
398  write (iout, '(1x,a)') repeat('-', 66)
399  write (iout, '(1x,a,a)') "Linear acceleration method: ", trim(ksp_str)
400  write (iout, '(1x,a,a)') "Preconditioner type: ", trim(pc_str)
401  write (iout, '(1x,a,a)') "Sub-preconditioner type: ", trim(subpc_str)
402  write (iout, '(1x,a,i0)') "Maximum nr. of iterations: ", &
403  this%linear_settings%iter1
404  write (iout, '(1x,a,a)') &
405  "Dep. var. closure criterion: ", trim(adjustl(dvclose_str))
406  write (iout, '(1x,a,a)') &
407  "Residual closure criterion: ", trim(adjustl(rclose_str))
408  write (iout, '(1x,a,i0)') &
409  "Residual convergence option: ", this%linear_settings%icnvgopt
410  write (iout, '(1x,a,a)') &
411  "Relaxation factor MILU(T): ", trim(adjustl(relax_str))
412  write (iout, '(1x,a,i0)') &
413  "Fill level in factorization: ", this%linear_settings%level
414  write (iout, '(1x,a,a,/)') &
415  "Drop tolerance level fill: ", trim(adjustl(dtol_str))
416 

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

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

◆ print_petsc_version()

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

Definition at line 161 of file PetscSolver.F90.

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

◆ print_vec()

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

Definition at line 447 of file PetscSolver.F90.

448  use tdismodule, only: nper, kstp
449  class(PetscSolverType) :: this
450  class(PetscVectorType) :: vec
451  character(len=*) :: vec_name
452  integer(I4B) :: kiter
453  ! local
454  petscviewer :: viewer
455  character(len=24) :: filename
456  petscerrorcode :: ierr
457 
458  write (filename, '(2a,i0,a,i0,a,i0,a)') vec_name, '_', nper, &
459  '_', kstp, '_', kiter, '.txt'
460  call petscviewerasciiopen(petsc_comm_world, filename, viewer, ierr)
461  chkerrq(ierr)
462  call vecview(vec%vec_impl, viewer, ierr)
463  chkerrq(ierr)
464  call petscviewerdestroy(viewer, ierr)
465  chkerrq(ierr)
466 
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 263 of file PetscSolver.F90.

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

◆ set_petsc_pc()

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

Definition at line 234 of file PetscSolver.F90.

235  class(PetscSolverType) :: this !< This solver instance
236  ! local
237  pc :: pc, sub_pc
238  ksp, dimension(1) :: sub_ksp
239  petscint :: n_local, n_first
240  petscerrorcode :: ierr
241 
242  call kspgetpc(this%ksp_petsc, pc, ierr)
243  chkerrq(ierr)
244  call pcsettype(pc, this%pc_type, ierr)
245  chkerrq(ierr)
246  call pcsetup(pc, ierr)
247  chkerrq(ierr)
248  call pcbjacobigetsubksp(pc, n_local, n_first, sub_ksp, ierr)
249  chkerrq(ierr)
250  call kspgetpc(sub_ksp(1), sub_pc, ierr)
251  chkerrq(ierr)
252  call pcsettype(sub_pc, this%sub_pc_type, ierr)
253  chkerrq(ierr)
254  call pcsetfromoptions(sub_pc, ierr)
255  chkerrq(ierr)
256  call pcfactorsetlevels(sub_pc, this%linear_settings%level, ierr)
257  chkerrq(ierr)
258