MODFLOW 6  version 6.8.0.dev0
USGS Modular Hydrologic Model
ImsLinearSettings.f90
Go to the documentation of this file.
2  use kindmodule
7  implicit none
8  private
9 
10  integer(I4B), public, parameter :: cg_method = 1
11  integer(I4B), public, parameter :: bcgs_method = 2
12 
13  !> @brief IMS linear preconditioner types
14  !<
15  enum, bind(C)
16  enumerator :: ipc_unknown = 0 !< preconditioner type not set
17  enumerator :: ipc_ilu0 = 1 !< ILU0 (incomplete LU, zero fill)
18  enumerator :: ipc_milu0 = 2 !< modified ILU0
19  enumerator :: ipc_ilut = 3 !< ILUT (incomplete LU with threshold)
20  enumerator :: ipc_milut = 4 !< modified ILUT
21  end enum
23  public :: resolve_ipc
24 
25  type, public :: imslinearsettingstype
26  character(len=LENMEMPATH) :: memory_path
27  real(dp), pointer :: dvclose => null() !< dependent variable closure criterion
28  real(dp), pointer :: rclose => null() !< residual closure criterion
29  integer(I4B), pointer :: icnvgopt => null() !< convergence option
30  integer(I4B), pointer :: iter1 => null() !< max. iterations
31  integer(I4B), pointer :: ilinmeth => null() !< linear solver method
32  integer(I4B), pointer :: iscl => null() !< scaling method
33  integer(I4B), pointer :: iord => null() !< reordering method
34  integer(I4B), pointer :: north => null() !< number of orthogonalizations
35  real(dp), pointer :: relax => null() !< relaxation factor
36  integer(I4B), pointer :: level => null() !< nr. of preconditioner levels
37  real(dp), pointer :: droptol => null() !< drop tolerance for preconditioner
38  integer(I4B), pointer :: ifdparam => null() !< complexity option
39  contains
40  procedure :: init
41  procedure :: preset_config
42  procedure :: read_from_file
43  procedure :: check_settings
44  procedure :: destroy
45  end type
46 
47 contains
48 
49  subroutine init(this, mem_path)
51  class(imslinearsettingstype) :: this !< linear settings
52  character(len=LENMEMPATH) :: mem_path !< solution memory path
53 
54  this%memory_path = create_mem_path(mem_path, 'IMSLINEAR')
55 
56  call mem_allocate(this%dvclose, 'DVCLOSE', this%memory_path)
57  call mem_allocate(this%rclose, 'RCLOSE', this%memory_path)
58  call mem_allocate(this%icnvgopt, 'ICNVGOPT', this%memory_path)
59  call mem_allocate(this%iter1, 'ITER1', this%memory_path)
60  call mem_allocate(this%ilinmeth, 'ILINMETH', this%memory_path)
61  call mem_allocate(this%iscl, 'ISCL', this%memory_path)
62  call mem_allocate(this%iord, 'IORD', this%memory_path)
63  call mem_allocate(this%north, 'NORTH', this%memory_path)
64  call mem_allocate(this%relax, 'RELAX', this%memory_path)
65  call mem_allocate(this%level, 'LEVEL', this%memory_path)
66  call mem_allocate(this%droptol, 'DROPTOL', this%memory_path)
67  call mem_allocate(this%ifdparam, 'IDFPARAM', this%memory_path)
68 
69  ! defaults
70  this%dvclose = dzero
71  this%rclose = dzero
72  this%icnvgopt = 0
73  this%iter1 = 0
74  this%ilinmeth = 0
75  this%iscl = 0
76  this%iord = 0
77  this%north = 0
78  this%relax = dzero
79  this%level = 0
80  this%droptol = dzero
81  this%ifdparam = 0
82 
83  end subroutine init
84 
85  !> @brief Set solver pre-configured settings based on complexity option
86  !<
87  subroutine preset_config(this, idfparam)
88  class(imslinearsettingstype) :: this !< linear settings
89  integer(I4B) :: idfparam !< complexity option
90 
91  this%ifdparam = idfparam
92 
93  select case (idfparam)
94  case (1) ! Simple option
95  this%iter1 = 50
96  this%ilinmeth = 1
97  this%iscl = 0
98  this%iord = 0
99  this%dvclose = dem3
100  this%rclose = dem1
101  this%relax = dzero
102  this%level = 0
103  this%droptol = dzero
104  this%north = 0
105  case (2) ! Moderate
106  this%iter1 = 100
107  this%ilinmeth = 2
108  this%iscl = 0
109  this%iord = 0
110  this%dvclose = dem2
111  this%rclose = dem1
112  this%relax = 0.97d0
113  this%level = 0
114  this%droptol = dzero
115  this%north = 0
116  case (3) ! Complex
117  this%iter1 = 500
118  this%ilinmeth = 2
119  this%iscl = 0
120  this%iord = 0
121  this%dvclose = dem1
122  this%rclose = dem1
123  this%relax = dzero
124  this%level = 5
125  this%droptol = dem4
126  this%north = 2
127  end select
128 
129  end subroutine preset_config
130 
131  !> @brief Read the settings for the linear solver from the .ims file,
132  !< overriding a possible pre-set configuration with set_complexity
133  subroutine read_from_file(this, parser, iout)
134  class(imslinearsettingstype) :: this !< linear settings
135  type(blockparsertype) :: parser !< block parser
136  integer(I4B) :: iout !< listing file
137  ! local
138  logical(LGP) :: block_found, end_of_block
139  integer(I4B) :: ierr
140  character(len=LINELENGTH) :: errmsg
141  character(len=LINELENGTH) :: warnmsg
142  character(len=LINELENGTH) :: keyword
143  integer(I4B) :: iscaling, iordering
144 
145  call parser%GetBlock('LINEAR', block_found, ierr, supportopenclose=.true., &
146  blockrequired=.false.)
147 
148  if (block_found) then
149  write (iout, '(/1x,a)') 'PROCESSING LINEAR DATA'
150  do
151  call parser%GetNextLine(end_of_block)
152  if (end_of_block) exit
153  call parser%GetStringCaps(keyword)
154  ! -- parse keyword
155  select case (keyword)
156  case ('INNER_DVCLOSE')
157  this%dvclose = parser%GetDouble()
158  case ('INNER_RCLOSE')
159  this%rclose = parser%GetDouble()
160  ! -- look for additional key words
161  call parser%GetStringCaps(keyword)
162  if (keyword == 'STRICT') then
163  this%icnvgopt = 1
164  else if (keyword == 'L2NORM_RCLOSE') then
165  this%icnvgopt = 2
166  else if (keyword == 'RELATIVE_RCLOSE') then
167  this%icnvgopt = 3
168  else if (keyword == 'L2NORM_RELATIVE_RCLOSE') then
169  this%icnvgopt = 4
170  end if
171  case ('INNER_MAXIMUM')
172  this%iter1 = parser%GetInteger()
173  case ('LINEAR_ACCELERATION')
174  call parser%GetStringCaps(keyword)
175  if (keyword .eq. 'CG') then
176  this%ilinmeth = 1
177  else if (keyword .eq. 'BICGSTAB') then
178  this%ilinmeth = 2
179  else
180  this%ilinmeth = 0
181  write (errmsg, '(3a)') &
182  'Unknown IMSLINEAR LINEAR_ACCELERATION method (', &
183  trim(keyword), ').'
184  call store_error(errmsg)
185  end if
186  case ('SCALING_METHOD')
187  call parser%GetStringCaps(keyword)
188  iscaling = 0
189  if (keyword .eq. 'NONE') then
190  iscaling = 0
191  else if (keyword .eq. 'DIAGONAL') then
192  iscaling = 1
193  else if (keyword .eq. 'L2NORM') then
194  iscaling = 2
195  else
196  write (errmsg, '(3a)') &
197  'Unknown IMSLINEAR SCALING_METHOD (', trim(keyword), ').'
198  call store_error(errmsg)
199  end if
200  this%iscl = iscaling
201  case ('RED_BLACK_ORDERING')
202  iordering = 0
203  case ('REORDERING_METHOD')
204  call parser%GetStringCaps(keyword)
205  iordering = 0
206  if (keyword == 'NONE') then
207  iordering = 0
208  else if (keyword == 'RCM') then
209  iordering = 1
210  else if (keyword == 'MD') then
211  iordering = 2
212  else
213  write (errmsg, '(3a)') &
214  'Unknown IMSLINEAR REORDERING_METHOD (', trim(keyword), ').'
215  call store_error(errmsg)
216  end if
217  this%iord = iordering
218  case ('NUMBER_ORTHOGONALIZATIONS')
219  this%north = parser%GetInteger()
220  case ('RELAXATION_FACTOR')
221  this%relax = parser%GetDouble()
222  case ('PRECONDITIONER_LEVELS')
223  this%level = parser%GetInteger()
224  if (this%level < 0) then
225  write (errmsg, '(a,1x,a)') &
226  'IMSLINEAR PRECONDITIONER_LEVELS must be greater than', &
227  'or equal to zero'
228  call store_error(errmsg)
229  end if
230  case ('PRECONDITIONER_DROP_TOLERANCE')
231  this%droptol = parser%GetDouble()
232  if (this%droptol < dzero) then
233  write (errmsg, '(a,1x,a)') &
234  'IMSLINEAR PRECONDITIONER_DROP_TOLERANCE', &
235  'must be greater than or equal to zero'
236  call store_error(errmsg)
237  end if
238  !
239  ! -- deprecated variables
240  case ('INNER_HCLOSE')
241  this%dvclose = parser%GetDouble()
242  !
243  ! -- create warning message
244  write (warnmsg, '(a)') &
245  'SETTING INNER_DVCLOSE TO INNER_HCLOSE VALUE'
246  !
247  ! -- create deprecation warning
248  call deprecation_warning('LINEAR', 'INNER_HCLOSE', '6.1.1', &
249  warnmsg, parser%GetUnit())
250  !
251  ! -- default
252  case default
253  write (errmsg, '(3a)') &
254  'Unknown IMSLINEAR keyword (', trim(keyword), ').'
255  call store_error(errmsg)
256  end select
257  end do
258  write (iout, '(1x,a)') 'END OF LINEAR DATA'
259  else
260  if (this%ifdparam == 0) THEN
261  write (errmsg, '(a)') 'NO LINEAR block detected.'
262  call store_error(errmsg)
263  end if
264  end if
265 
266  end subroutine read_from_file
267 
268  !> @brief Check the settings after reading the configuration from file
269  !<
270  subroutine check_settings(this)
271  class(imslinearsettingstype) :: this !< linear settings
272  ! local
273  character(len=LINELENGTH) :: warnmsg
274 
275  if (this%level == 0 .and. this%droptol > 0.0_dp) then
276  write (warnmsg, '(a)') "PRECONDITIONER_DROP_TOLERANCE is ignored because &
277  &PRECONDITIONER_LEVELS equals zero."
278  call store_warning(warnmsg)
279  end if
280 
281  end subroutine check_settings
282 
283  subroutine destroy(this)
284  class(imslinearsettingstype) :: this !< linear settings
285 
286  call mem_deallocate(this%dvclose)
287  call mem_deallocate(this%rclose)
288  call mem_deallocate(this%icnvgopt)
289  call mem_deallocate(this%iter1)
290  call mem_deallocate(this%ilinmeth)
291  call mem_deallocate(this%iscl)
292  call mem_deallocate(this%iord)
293  call mem_deallocate(this%north)
294  call mem_deallocate(this%relax)
295  call mem_deallocate(this%level)
296  call mem_deallocate(this%droptol)
297  call mem_deallocate(this%ifdparam)
298 
299  end subroutine destroy
300 
301  !> @brief Resolve the preconditioner enum from the ILU controls
302  !!
303  !! Maps the ILU fill level and relaxation factor to a specific IPC_*
304  !! preconditioner:
305  !! level > 0 -> IPC_ILUT (-> IPC_MILUT when relax > 0)
306  !! level == 0 -> IPC_ILU0 (-> IPC_MILU0 when relax > 0)
307  !!
308  !! Extracted into a pure function so the same resolution can be reused when
309  !! preconditioner settings change at runtime (e.g. by stress period).
310  !<
311  pure function resolve_ipc(level, relax) result(ipc)
312  integer(I4B), intent(in) :: level !< ILU fill level
313  real(dp), intent(in) :: relax !< relaxation factor (> 0 selects the modified ILU)
314  integer(I4B) :: ipc !< resolved preconditioner enum (IPC_*)
315  !
316  ! -- map explicitly to the target enum (do not rely on IPC_* being
317  ! consecutive); relax > 0 selects the modified variant
318  if (level > 0) then
319  if (relax > dzero) then
320  ipc = ipc_milut
321  else
322  ipc = ipc_ilut
323  end if
324  else
325  if (relax > dzero) then
326  ipc = ipc_milu0
327  else
328  ipc = ipc_ilu0
329  end if
330  end if
331  end function resolve_ipc
332 
333 end module
subroutine init()
Definition: GridSorting.f90:25
This module contains block parser methods.
Definition: BlockParser.f90:7
This module contains simulation constants.
Definition: Constants.f90:9
real(dp), parameter dem1
real constant 1e-1
Definition: Constants.f90:103
real(dp), parameter dem3
real constant 1e-3
Definition: Constants.f90:106
real(dp), parameter dem4
real constant 1e-4
Definition: Constants.f90:107
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
real(dp), parameter dem2
real constant 1e-2
Definition: Constants.f90:105
integer(i4b), parameter, public cg_method
subroutine read_from_file(this, parser, iout)
Read the settings for the linear solver from the .ims file,.
subroutine preset_config(this, idfparam)
Set solver pre-configured settings based on complexity option.
@, public ipc_ilu0
ILU0 (incomplete LU, zero fill)
@, public ipc_milut
modified ILUT
subroutine check_settings(this)
Check the settings after reading the configuration from file.
@, public ipc_unknown
preconditioner type not set
integer(i4b), parameter, public bcgs_method
@, public ipc_milu0
modified ILU0
pure integer(i4b) function, public resolve_ipc(level, relax)
Resolve the preconditioner enum from the ILU controls.
@, public ipc_ilut
ILUT (incomplete LU with threshold)
This module defines variable data types.
Definition: kind.f90:8
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_warning(msg, substring)
Store warning message.
Definition: Sim.f90:236
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public deprecation_warning(cblock, cvar, cver, endmsg, iunit)
Store deprecation warning message.
Definition: Sim.f90:256