MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
ImsLinearBase.f90
Go to the documentation of this file.
1
2!> @brief This module contains the IMS linear accelerator subroutines
3!!
4!! This module contains the IMS linear accelerator subroutines used by a
5!! MODFLOW 6 solution.
6!<
8 ! -- modules
9 use kindmodule, only: dp, i4b
10 use constantsmodule, only: linelength, izero, &
12 use mathutilmodule, only: is_close
17
18 IMPLICIT NONE
19
20 type(blockparsertype), private :: parser
21
22contains
23
24 !> @brief Preconditioned Conjugate Gradient linear accelerator
25 !!
26 !! Apply the Preconditioned Conjugate Gradient linear accelerator to
27 !! the current coefficient matrix, right-hand side, using the current
28 !! dependent-variable.
29 !!
30 !<
31 SUBROUTINE ims_base_cg(ICNVG, ITMAX, INNERIT, &
32 NEQ, NJA, NIAPC, NJAPC, &
33 IPC, ICNVGOPT, NORTH, ISCL, RSCALE, HSCALE, &
34 DVCLOSE, RCLOSE, L2NORM0, EPFACT, &
35 IA0, JA0, A0, IAPC, JAPC, APC, &
36 X, B, D, P, Q, Z, &
37 NJLU, IW, JLU, &
38 NCONV, CONVNMOD, CONVMODSTART, &
39 CACCEL, summary)
40 ! -- dummy variables
41 integer(I4B), INTENT(INOUT) :: ICNVG !< convergence flag (1) non-convergence (0)
42 integer(I4B), INTENT(IN) :: ITMAX !< maximum number of inner iterations
43 integer(I4B), INTENT(INOUT) :: INNERIT !< inner iteration count
44 integer(I4B), INTENT(IN) :: NEQ !< number of equations
45 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
46 integer(I4B), INTENT(IN) :: NIAPC !< preconditioner number of rows
47 integer(I4B), INTENT(IN) :: NJAPC !< preconditioner number of non-zero entries
48 integer(I4B), INTENT(IN) :: IPC !< preconditioner option
49 integer(I4B), INTENT(IN) :: ICNVGOPT !< flow convergence criteria option
50 integer(I4B), INTENT(IN) :: NORTH !< orthogonalization frequency
51 integer(I4B), INTENT(IN) :: ISCL !< scaling option
52 real(DP), DIMENSION(*), INTENT(IN) :: RSCALE !< factor returning the residual to model units
53 real(DP), DIMENSION(*), INTENT(IN) :: HSCALE !< factor returning the head change to model units
54 real(DP), INTENT(IN) :: DVCLOSE !< dependent-variable closure criteria
55 real(DP), INTENT(IN) :: RCLOSE !< flow closure criteria
56 real(DP), INTENT(IN) :: L2NORM0 !< initial L-2 norm for system of equations
57 real(DP), INTENT(IN) :: EPFACT !< factor for decreasing flow convergence criteria for subsequent Picard iterations
58 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA0 !< CRS row pointers
59 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA0 !< CRS column pointers
60 real(DP), DIMENSION(NJA), INTENT(IN) :: A0 !< coefficient matrix
61 integer(I4B), DIMENSION(NIAPC + 1), INTENT(IN) :: IAPC !< preconditioner CRS row pointers
62 integer(I4B), DIMENSION(NJAPC), INTENT(IN) :: JAPC !< preconditioner CRS column pointers
63 real(DP), DIMENSION(NJAPC), INTENT(IN) :: APC !< preconditioner matrix
64 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: X !< dependent-variable vector
65 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: B !< right-hand side vector
66 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: D !< working vector
67 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: P !< working vector
68 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: Q !< working vector
69 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: Z !< working vector
70 ! -- ILUT dummy variables
71 integer(I4B), INTENT(IN) :: NJLU !< preconditioner length of JLU vector
72 integer(I4B), DIMENSION(NIAPC), INTENT(IN) :: IW !< preconditioner integer working vector
73 integer(I4B), DIMENSION(NJLU), INTENT(IN) :: JLU !< preconditioner JLU working vector
74 ! -- convergence information dummy variables dummy variables
75 integer(I4B), INTENT(IN) :: NCONV !< maximum number of inner iterations in a time step (maxiter * maxinner)
76 integer(I4B), INTENT(IN) :: CONVNMOD !< number of models in the solution
77 integer(I4B), DIMENSION(CONVNMOD + 1), INTENT(INOUT) :: CONVMODSTART !< pointer to the start of each model in the convmod* arrays
78 character(len=31), DIMENSION(NCONV), INTENT(INOUT) :: CACCEL !< convergence string
79 type(convergencesummarytype), pointer, intent(in) :: summary !< Convergence summary report
80 ! -- local variables
81 LOGICAL :: lorth
82 logical :: lsame
83 character(len=31) :: cval
84 integer(I4B) :: n
85 integer(I4B) :: iiter
86 integer(I4B) :: xloc, rloc
87 integer(I4B) :: im, im0, im1
88 real(DP) :: ddot
89 real(DP) :: tv
90 real(DP) :: deltax
91 real(DP) :: rmax
92 real(DP) :: l2norm
93 real(DP) :: rcnvg
94 real(DP) :: denominator
95 real(DP) :: alpha, beta
96 real(DP) :: rho, rho0
97 !
98 ! -- initialize local variables
99 rho0 = dzero
100 rho = dzero
101 innerit = 0
102 !
103 ! -- INNER ITERATION
104 inner: DO iiter = 1, itmax
105 innerit = innerit + 1
106 summary%iter_cnt = summary%iter_cnt + 1
107 !
108 ! -- APPLY PRECONDITIONER
109 SELECT CASE (ipc)
110 !
111 ! -- ILU0 AND MILU0
112 CASE (ipc_ilu0, ipc_milu0)
113 CALL ims_base_ilu0a(nja, neq, apc, iapc, japc, d, z)
114 !
115 ! -- ILUT AND MILUT
116 CASE (ipc_ilut, ipc_milut)
117 CALL lusol(neq, d, z, apc, jlu, iw)
118 END SELECT
119 rho = ddot(neq, d, 1, z, 1)
120 !
121 ! -- COMPUTE DIRECTIONAL VECTORS
122 IF (iiter == 1) THEN
123 DO n = 1, neq
124 p(n) = z(n)
125 END DO
126 ELSE
127 beta = rho / rho0
128 DO n = 1, neq
129 p(n) = z(n) + beta * p(n)
130 END DO
131 END IF
132 !
133 ! -- COMPUTE ITERATES
134 !
135 ! -- UPDATE Q
136 call amux(neq, p, q, a0, ja0, ia0)
137 denominator = ddot(neq, p, 1, q, 1)
138 denominator = denominator + sign(dprec, denominator)
139 alpha = rho / denominator
140 !
141 ! -- UPDATE X AND RESIDUAL
142 deltax = dzero
143 rmax = dzero
144 l2norm = dzero
145 DO im = 1, convnmod
146 summary%locdv(im) = 0
147 summary%dvmax(im) = dzero
148 summary%locr(im) = 0
149 summary%rmax(im) = dzero
150 END DO
151 im = 1
152 im0 = convmodstart(1)
153 im1 = convmodstart(2)
154 DO n = 1, neq
155 !
156 ! -- determine current model index
157 if (n == im1) then
158 im = im + 1
159 im0 = convmodstart(im)
160 im1 = convmodstart(im + 1)
161 end if
162 !
163 ! -- identify deltax and rmax
164 tv = alpha * p(n)
165 x(n) = x(n) + tv
166 IF (iscl /= 0) tv = tv * hscale(n)
167 IF (abs(tv) > abs(deltax)) THEN
168 deltax = tv
169 xloc = n
170 END IF
171 IF (abs(tv) > abs(summary%dvmax(im))) THEN
172 summary%dvmax(im) = tv
173 summary%locdv(im) = n
174 END IF
175 tv = d(n)
176 tv = tv - alpha * q(n)
177 d(n) = tv
178 IF (iscl /= 0) tv = tv * rscale(n)
179 IF (abs(tv) > abs(rmax)) THEN
180 rmax = tv
181 rloc = n
182 END IF
183 IF (abs(tv) > abs(summary%rmax(im))) THEN
184 summary%rmax(im) = tv
185 summary%locr(im) = n
186 END IF
187 l2norm = l2norm + tv * tv
188 END DO
189 l2norm = sqrt(l2norm)
190 !
191 ! -- SAVE SOLVER convergence information dummy variables
192 IF (nconv > 1) THEN !<
193 n = summary%iter_cnt
194 WRITE (cval, '(g15.7)') alpha
195 caccel(n) = cval
196 summary%itinner(n) = iiter
197 DO im = 1, convnmod
198 summary%convlocdv(im, n) = summary%locdv(im)
199 summary%convlocr(im, n) = summary%locr(im)
200 summary%convdvmax(im, n) = summary%dvmax(im)
201 summary%convrmax(im, n) = summary%rmax(im)
202 END DO
203 END IF
204 !
205 ! -- TEST FOR SOLVER CONVERGENCE
206 IF (icnvgopt == 2 .OR. icnvgopt == 3 .OR. icnvgopt == 4) THEN
207 rcnvg = l2norm
208 ELSE
209 rcnvg = rmax
210 END IF
211 CALL ims_base_testcnvg(icnvgopt, icnvg, innerit, &
212 deltax, rcnvg, &
213 l2norm0, epfact, dvclose, rclose)
214 !
215 ! -- CHECK FOR EXACT SOLUTION
216 IF (rcnvg == dzero) icnvg = 1
217 !
218 ! -- CHECK FOR STANDARD CONVERGENCE
219 IF (icnvg .NE. 0) EXIT inner
220 !
221 ! -- CHECK THAT CURRENT AND PREVIOUS rho ARE DIFFERENT
222 lsame = is_close(rho, rho0)
223 IF (lsame) THEN
224 EXIT inner
225 END IF
226 !
227 ! -- RECALCULATE THE RESIDUAL
228 IF (north > 0) THEN
229 lorth = mod(iiter + 1, north) == 0
230 IF (lorth) THEN
231 call ims_base_residual(neq, nja, x, b, d, a0, ia0, ja0)
232 END IF
233 END IF
234 !
235 ! -- exit inner if rho is zero
236 if (rho == dzero) then
237 exit inner
238 end if
239 !
240 ! -- SAVE CURRENT INNER ITERATES
241 rho0 = rho
242 END DO inner
243 !
244 ! -- RESET ICNVG
245 IF (icnvg < 0) icnvg = 0
246 end SUBROUTINE ims_base_cg
247
248 !> @brief Preconditioned BiConjugate Gradient Stabilized linear accelerator
249 !!
250 !! Apply the Preconditioned BiConjugate Gradient Stabilized linear
251 !! accelerator to the current coefficient matrix, right-hand side, using
252 !! the currentdependent-variable.
253 !!
254 !<
255 SUBROUTINE ims_base_bcgs(ICNVG, ITMAX, INNERIT, &
256 NEQ, NJA, NIAPC, NJAPC, &
257 IPC, ICNVGOPT, NORTH, ISCL, RSCALE, HSCALE, &
258 DVCLOSE, RCLOSE, L2NORM0, EPFACT, &
259 IA0, JA0, A0, IAPC, JAPC, APC, &
260 X, B, D, P, Q, &
261 T, V, DHAT, PHAT, QHAT, &
262 NJLU, IW, JLU, &
263 NCONV, CONVNMOD, CONVMODSTART, &
264 CACCEL, summary)
265 ! -- dummy variables
266 integer(I4B), INTENT(INOUT) :: ICNVG !< convergence flag (1) non-convergence (0)
267 integer(I4B), INTENT(IN) :: ITMAX !< maximum number of inner iterations
268 integer(I4B), INTENT(INOUT) :: INNERIT !< inner iteration count
269 integer(I4B), INTENT(IN) :: NEQ !< number of equations
270 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
271 integer(I4B), INTENT(IN) :: NIAPC !< preconditioner number of rows
272 integer(I4B), INTENT(IN) :: NJAPC !< preconditioner number of non-zero entries
273 integer(I4B), INTENT(IN) :: IPC !< preconditioner option
274 integer(I4B), INTENT(IN) :: ICNVGOPT !< flow convergence criteria option
275 integer(I4B), INTENT(IN) :: NORTH !< orthogonalization frequency
276 integer(I4B), INTENT(IN) :: ISCL !< scaling option
277 real(DP), DIMENSION(*), INTENT(IN) :: RSCALE !< factor returning the residual to model units
278 real(DP), DIMENSION(*), INTENT(IN) :: HSCALE !< factor returning the head change to model units
279 real(DP), INTENT(IN) :: DVCLOSE !< dependent-variable closure criteria
280 real(DP), INTENT(IN) :: RCLOSE !< flow closure criteria
281 real(DP), INTENT(IN) :: L2NORM0 !< initial L-2 norm for system of equations
282 real(DP), INTENT(IN) :: EPFACT !< factor for decreasing flow convergence criteria for subsequent Picard iterations
283 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA0 !< CRS row pointers
284 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA0 !< CRS column pointers
285 real(DP), DIMENSION(NJA), INTENT(IN) :: A0 !< coefficient matrix
286 integer(I4B), DIMENSION(NIAPC + 1), INTENT(IN) :: IAPC !< preconditioner CRS row pointers
287 integer(I4B), DIMENSION(NJAPC), INTENT(IN) :: JAPC !< preconditioner CRS column pointers
288 real(DP), DIMENSION(NJAPC), INTENT(IN) :: APC !< preconditioner matrix
289 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: X !< dependent-variable vector
290 real(DP), DIMENSION(NEQ), INTENT(IN) :: B !< right-hand side vector
291 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: D !< preconditioner working vector
292 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: P !< preconditioner working vector
293 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: Q !< preconditioner working vector
294 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: T !< preconditioner working vector
295 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: V !< preconditioner working vector
296 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: DHAT !< BCGS preconditioner working vector
297 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: PHAT !< BCGS preconditioner working vector
298 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: QHAT !< BCGS preconditioner working vector
299 ! -- ILUT dummy variables
300 integer(I4B), INTENT(IN) :: NJLU !< preconditioner length of JLU vector
301 integer(I4B), DIMENSION(NIAPC), INTENT(IN) :: IW !< preconditioner integer working vector
302 integer(I4B), DIMENSION(NJLU), INTENT(IN) :: JLU !< preconditioner JLU working vector
303 ! -- convergence information dummy variables
304 integer(I4B), INTENT(IN) :: NCONV !< maximum number of inner iterations in a time step (maxiter * maxinner)
305 integer(I4B), INTENT(IN) :: CONVNMOD !< number of models in the solution
306 integer(I4B), DIMENSION(CONVNMOD + 1), INTENT(INOUT) :: CONVMODSTART !< pointer to the start of each model in the convmod* arrays
307 character(len=31), DIMENSION(NCONV), INTENT(INOUT) :: CACCEL !< convergence string
308 type(convergencesummarytype), pointer, intent(in) :: summary !< Convergence summary report
309 ! -- local variables
310 LOGICAL :: LORTH
311 logical :: lsame
312 character(len=15) :: cval1, cval2
313 integer(I4B) :: n
314 integer(I4B) :: iiter
315 integer(I4B) :: xloc, rloc
316 integer(I4B) :: im, im0, im1
317 real(DP) :: ddot
318 real(DP) :: tv
319 real(DP) :: deltax
320 real(DP) :: rmax
321 real(DP) :: l2norm
322 real(DP) :: rcnvg
323 real(DP) :: alpha, alpha0
324 real(DP) :: beta
325 real(DP) :: rho, rho0
326 real(DP) :: omega, omega0
327 real(DP) :: numerator, denominator
328 !
329 ! -- initialize local variables
330 innerit = 0
331 alpha = dzero
332 alpha0 = dzero
333 beta = dzero
334 rho = dzero
335 rho0 = dzero
336 omega = dzero
337 omega0 = dzero
338 !
339 ! -- SAVE INITIAL RESIDUAL
340 DO n = 1, neq
341 dhat(n) = d(n)
342 END DO
343 !
344 ! -- INNER ITERATION
345 inner: DO iiter = 1, itmax
346 innerit = innerit + 1
347 summary%iter_cnt = summary%iter_cnt + 1
348 !
349 ! -- CALCULATE rho
350 rho = ddot(neq, dhat, 1, d, 1)
351 !
352 ! -- COMPUTE DIRECTIONAL VECTORS
353 IF (iiter == 1) THEN
354 DO n = 1, neq
355 p(n) = d(n)
356 END DO
357 ELSE
358 beta = (rho / rho0) * (alpha0 / omega0)
359 DO n = 1, neq
360 p(n) = d(n) + beta * (p(n) - omega0 * v(n))
361 END DO
362 END IF
363 !
364 ! -- APPLY PRECONDITIONER TO UPDATE PHAT
365 SELECT CASE (ipc)
366 !
367 ! -- ILU0 AND MILU0
368 CASE (ipc_ilu0, ipc_milu0)
369 CALL ims_base_ilu0a(nja, neq, apc, iapc, japc, p, phat)
370 !
371 ! -- ILUT AND MILUT
372 CASE (ipc_ilut, ipc_milut)
373 CALL lusol(neq, p, phat, apc, jlu, iw)
374 END SELECT
375 !
376 ! -- COMPUTE ITERATES
377 !
378 ! -- UPDATE V WITH A AND PHAT
379 call amux(neq, phat, v, a0, ja0, ia0)
380 !
381 ! -- UPDATE alpha WITH DHAT AND V
382 denominator = ddot(neq, dhat, 1, v, 1)
383 denominator = denominator + sign(dprec, denominator)
384 alpha = rho / denominator
385 !
386 ! -- UPDATE Q
387 DO n = 1, neq
388 q(n) = d(n) - alpha * v(n)
389 END DO
390 !
391 ! ! -- CALCULATE INFINITY NORM OF Q - TEST FOR TERMINATION
392 ! ! TERMINATE IF rmax IS LESS THAN MACHINE PRECISION (DPREC)
393 ! rmax = DZERO
394 ! DO n = 1, NEQ
395 ! tv = Q(n)
396 ! IF (ISCL.NE.0 ) tv = tv * RSCALE(n)
397 ! IF (ABS(tv) > ABS(rmax) ) rmax = tv
398 ! END DO
399 ! IF (ABS(rmax).LE.DPREC) THEN
400 ! deltax = DZERO
401 ! DO n = 1, NEQ
402 ! tv = alpha * PHAT(n)
403 ! IF (ISCL.NE.0) THEN
404 ! tv = tv * HSCALE(n)
405 ! END IF
406 ! X(n) = X(n) + tv
407 ! IF (ABS(tv) > ABS(deltax) ) deltax = tv
408 ! END DO
409 ! CALL IMSLINEARSUB_TESTCNVG(ICNVGOPT, ICNVG, INNERIT, &
410 ! deltax, rmax, &
411 ! rmax, EPFACT, DVCLOSE, RCLOSE )
412 ! IF (ICNVG.NE.0 ) EXIT INNER
413 ! END IF
414 !
415 ! -- APPLY PRECONDITIONER TO UPDATE QHAT
416 SELECT CASE (ipc)
417 !
418 ! -- ILU0 AND MILU0
419 CASE (ipc_ilu0, ipc_milu0)
420 CALL ims_base_ilu0a(nja, neq, apc, iapc, japc, q, qhat)
421 !
422 ! -- ILUT AND MILUT
423 CASE (ipc_ilut, ipc_milut)
424 CALL lusol(neq, q, qhat, apc, jlu, iw)
425 END SELECT
426 !
427 ! -- UPDATE T WITH A AND QHAT
428 call amux(neq, qhat, t, a0, ja0, ia0)
429 !
430 ! -- UPDATE omega
431 numerator = ddot(neq, t, 1, q, 1)
432 denominator = ddot(neq, t, 1, t, 1)
433 denominator = denominator + sign(dprec, denominator)
434 omega = numerator / denominator
435 !
436 ! -- UPDATE X AND RESIDUAL
437 deltax = dzero
438 rmax = dzero
439 l2norm = dzero
440 DO im = 1, convnmod
441 summary%dvmax(im) = dzero
442 summary%rmax(im) = dzero
443 END DO
444 im = 1
445 im0 = convmodstart(1)
446 im1 = convmodstart(2)
447 DO n = 1, neq
448 !
449 ! -- determine current model index
450 if (n == im1) then
451 im = im + 1
452 im0 = convmodstart(im)
453 im1 = convmodstart(im + 1)
454 end if
455 !
456 ! -- X AND DX
457 tv = alpha * phat(n) + omega * qhat(n)
458 x(n) = x(n) + tv
459 IF (iscl .NE. 0) THEN
460 tv = tv * hscale(n)
461 END IF
462 IF (abs(tv) > abs(deltax)) THEN
463 deltax = tv
464 xloc = n
465 END IF
466 IF (abs(tv) > abs(summary%dvmax(im))) THEN
467 summary%dvmax(im) = tv
468 summary%locdv(im) = n
469 END IF
470 !
471 ! -- RESIDUAL
472 tv = q(n) - omega * t(n)
473 d(n) = tv
474 IF (iscl .NE. 0) THEN
475 tv = tv * rscale(n)
476 END IF
477 IF (abs(tv) > abs(rmax)) THEN
478 rmax = tv
479 rloc = n
480 END IF
481 IF (abs(tv) > abs(summary%rmax(im))) THEN
482 summary%rmax(im) = tv
483 summary%locr(im) = n
484 END IF
485 l2norm = l2norm + tv * tv
486 END DO
487 l2norm = sqrt(l2norm)
488 !
489 ! -- SAVE SOLVER convergence information dummy variables
490 IF (nconv > 1) THEN !<
491 n = summary%iter_cnt
492 WRITE (cval1, '(g15.7)') alpha
493 WRITE (cval2, '(g15.7)') omega
494 caccel(n) = trim(adjustl(cval1))//','//trim(adjustl(cval2))
495 summary%itinner(n) = iiter
496 DO im = 1, convnmod
497 summary%convdvmax(im, n) = summary%dvmax(im)
498 summary%convlocdv(im, n) = summary%locdv(im)
499 summary%convrmax(im, n) = summary%rmax(im)
500 summary%convlocr(im, n) = summary%locr(im)
501 END DO
502 END IF
503 !
504 ! -- TEST FOR SOLVER CONVERGENCE
505 IF (icnvgopt == 2 .OR. icnvgopt == 3 .OR. icnvgopt == 4) THEN
506 rcnvg = l2norm
507 ELSE
508 rcnvg = rmax
509 END IF
510 CALL ims_base_testcnvg(icnvgopt, icnvg, innerit, &
511 deltax, rcnvg, &
512 l2norm0, epfact, dvclose, rclose)
513 !
514 ! -- CHECK FOR EXACT SOLUTION
515 IF (rcnvg == dzero) icnvg = 1
516 !
517 ! -- CHECK FOR STANDARD CONVERGENCE
518 IF (icnvg .NE. 0) EXIT inner
519 !
520 ! -- CHECK THAT CURRENT AND PREVIOUS rho, alpha, AND omega ARE
521 ! DIFFERENT
522 lsame = is_close(rho, rho0)
523 IF (lsame) THEN
524 EXIT inner
525 END IF
526 lsame = is_close(alpha, alpha0)
527 IF (lsame) THEN
528 EXIT inner
529 END IF
530 lsame = is_close(omega, omega0)
531 IF (lsame) THEN
532 EXIT inner
533 END IF
534 !
535 ! -- RECALCULATE THE RESIDUAL
536 IF (north > 0) THEN
537 lorth = mod(iiter + 1, north) == 0
538 IF (lorth) THEN
539 call ims_base_residual(neq, nja, x, b, d, a0, ia0, ja0)
540 END IF
541 END IF
542 !
543 ! -- exit inner if rho or omega are zero
544 if (rho * omega == dzero) then
545 exit inner
546 end if
547 !
548 ! -- SAVE CURRENT INNER ITERATES
549 rho0 = rho
550 alpha0 = alpha
551 omega0 = omega
552 END DO inner
553 !
554 ! -- RESET ICNVG
555 IF (icnvg < 0) icnvg = 0
556 end SUBROUTINE ims_base_bcgs
557
558 !> @brief Calculate LORDER AND IORDER
559 !!
560 !! Calculate LORDER and IORDER for reordering.
561 !!
562 !<
563 SUBROUTINE ims_base_calc_order(IORD, NEQ, NJA, IA, JA, LORDER, IORDER)
564 ! -- modules
566 ! -- dummy variables
567 integer(I4B), INTENT(IN) :: IORD !< reordering option
568 integer(I4B), INTENT(IN) :: NEQ !< number of rows
569 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
570 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< row pointer
571 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< column pointer
572 integer(I4B), DIMENSION(NEQ), INTENT(INOUT) :: LORDER !< reorder vector
573 integer(I4B), DIMENSION(NEQ), INTENT(INOUT) :: IORDER !< inverse of reorder vector
574 ! -- local variables
575 character(len=LINELENGTH) :: errmsg
576 integer(I4B) :: n
577 integer(I4B) :: nsp
578 integer(I4B), DIMENSION(:), ALLOCATABLE :: iwork0
579 integer(I4B), DIMENSION(:), ALLOCATABLE :: iwork1
580 integer(I4B) :: iflag
581 !
582 ! -- initialize lorder and iorder
583 DO n = 1, neq
584 lorder(n) = izero
585 iorder(n) = izero
586 END DO
587 ! ALLOCATE (iwork0(NEQ))
588 SELECT CASE (iord)
589 CASE (1)
590 CALL genrcm(neq, nja, ia, ja, lorder)
591 CASE (2)
592 nsp = 3 * neq + 4 * nja
593 allocate (iwork0(neq))
594 allocate (iwork1(nsp))
595 CALL ims_odrv(neq, nja, nsp, ia, ja, lorder, iwork0, &
596 iwork1, iflag)
597 IF (iflag .NE. 0) THEN
598 write (errmsg, '(A,1X,A)') &
599 'IMSLINEARSUB_CALC_ORDER error creating minimum degree ', &
600 'order permutation '
601 call store_error(errmsg)
602 END IF
603 !
604 ! -- DEALLOCATE TEMPORARY STORAGE
605 deallocate (iwork0, iwork1)
606 END SELECT
607 !
608 ! -- GENERATE INVERSE OF LORDER
609 DO n = 1, neq
610 iorder(lorder(n)) = n
611 END DO
612 !
613 ! -- terminate if errors occurred
614 if (count_errors() > 0) then
615 call parser%StoreErrorUnit()
616 end if
617 end SUBROUTINE ims_base_calc_order
618
619 !
620 !> @brief Scale the coefficient matrix
621 !!
622 !! Scale the coefficient matrix (AMAT), the right-hand side (B),
623 !! and the estimate of the dependent variable (X).
624 !!
625 !<
626 SUBROUTINE ims_base_scale(IOPT, ISCL, NEQ, NJA, IA, JA, AMAT, X, B, &
627 DSCALE, DSCALE2)
628 ! -- dummy variables
629 integer(I4B), INTENT(IN) :: IOPT !< flag to scale (0) or unscale the system of equations
630 integer(I4B), INTENT(IN) :: ISCL !< scaling option (1) symmetric (2) L-2 norm
631 integer(I4B), INTENT(IN) :: NEQ !< number of equations
632 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
633 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< CRS row pointer
634 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< CRS column pointer
635 real(DP), DIMENSION(NJA), INTENT(INOUT) :: AMAT !< coefficient matrix
636 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: X !< dependent variable
637 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: B !< right-hand side
638 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: DSCALE !< first scaling vector
639 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: DSCALE2 !< second scaling vector
640 ! -- local variables
641 integer(I4B) :: i, n
642 integer(I4B) :: id, jc
643 integer(I4B) :: i0, i1
644 real(DP) :: v, c1, c2
645 !
646 ! -- SCALE SCALE AMAT, X, AND B
647 IF (iopt == 0) THEN
648 !
649 ! -- SYMMETRIC SCALING
650 SELECT CASE (iscl)
651 CASE (1)
652 DO n = 1, neq
653 id = ia(n)
654 v = amat(id)
655 c1 = done / sqrt(abs(v))
656 dscale(n) = c1
657 dscale2(n) = c1
658 END DO
659 !
660 ! -- SCALE AMAT -- AMAT = DSCALE(row) * AMAT(i) * DSCALE2(col)
661 DO n = 1, neq
662 c1 = dscale(n)
663 i0 = ia(n)
664 i1 = ia(n + 1) - 1
665 DO i = i0, i1
666 jc = ja(i)
667 c2 = dscale2(jc)
668 amat(i) = c1 * amat(i) * c2
669 END DO
670 END DO
671 !
672 ! -- L-2 NORM SCALING
673 CASE (2)
674 !
675 ! -- SCALE EACH ROW SO THAT THE L-2 NORM IS 1
676 DO n = 1, neq
677 c1 = dzero
678 i0 = ia(n)
679 i1 = ia(n + 1) - 1
680 DO i = i0, i1
681 c1 = c1 + amat(i) * amat(i)
682 END DO
683 c1 = sqrt(c1)
684 IF (c1 == dzero) THEN
685 c1 = done
686 ELSE
687 c1 = done / c1
688 END IF
689 dscale(n) = c1
690 !
691 ! -- INITIAL SCALING OF AMAT -- AMAT = DSCALE(row) * AMAT(i)
692 DO i = i0, i1
693 amat(i) = c1 * amat(i)
694 END DO
695 END DO
696 !
697 ! -- SCALE EACH COLUMN SO THAT THE L-2 NORM IS 1
698 DO n = 1, neq
699 dscale2(n) = dzero
700 END DO
701 c2 = dzero
702 DO n = 1, neq
703 i0 = ia(n)
704 i1 = ia(n + 1) - 1
705 DO i = i0, i1
706 jc = ja(i)
707 c2 = amat(i)
708 dscale2(jc) = dscale2(jc) + c2 * c2
709 END DO
710 END DO
711 DO n = 1, neq
712 c2 = dscale2(n)
713 IF (c2 == dzero) THEN
714 c2 = done
715 ELSE
716 c2 = done / sqrt(c2)
717 END IF
718 dscale2(n) = c2
719 END DO
720 !
721 ! -- FINAL SCALING OF AMAT -- AMAT = DSCALE2(col) * AMAT(i)
722 DO n = 1, neq
723 i0 = ia(n)
724 i1 = ia(n + 1) - 1
725 DO i = i0, i1
726 jc = ja(i)
727 c2 = dscale2(jc)
728 amat(i) = c2 * amat(i)
729 END DO
730 END DO
731 END SELECT
732 !
733 ! -- SCALE X AND B
734 DO n = 1, neq
735 c1 = dscale(n)
736 c2 = dscale2(n)
737 x(n) = x(n) / c2
738 b(n) = b(n) * c1
739 END DO
740 !
741 ! -- UNSCALE SCALE AMAT, X, AND B
742 ELSE
743 DO n = 1, neq
744 c1 = dscale(n)
745 i0 = ia(n)
746 i1 = ia(n + 1) - 1
747 !
748 ! -- UNSCALE AMAT
749 DO i = i0, i1
750 jc = ja(i)
751 c2 = dscale2(jc)
752 amat(i) = (done / c1) * amat(i) * (done / c2)
753 END DO
754 !
755 ! -- UNSCALE X AND B
756 c2 = dscale2(n)
757 x(n) = x(n) * c2
758 b(n) = b(n) / c1
759 END DO
760 END IF
761 end SUBROUTINE ims_base_scale
762
763 !> @brief Set the factors that return the residual and head change to
764 !! model units, in the order of the reordered system
765 !<
766 SUBROUTINE ims_base_cnvg_scale(NEQ, IORD, DSCALE, DSCALE2, LORDER, &
767 RSCALE, HSCALE)
768 ! -- dummy variables
769 integer(I4B), INTENT(IN) :: NEQ !< number of equations
770 integer(I4B), INTENT(IN) :: IORD !< reordering option
771 real(DP), DIMENSION(NEQ), INTENT(IN) :: DSCALE !< first scaling vector
772 real(DP), DIMENSION(NEQ), INTENT(IN) :: DSCALE2 !< second scaling vector
773 integer(I4B), DIMENSION(*), INTENT(INOUT) :: LORDER !< reordering mapping
774 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: RSCALE !< residual factor
775 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: HSCALE !< head change factor
776 ! -- local variables
777 integer(I4B) :: n
778 !
779 DO n = 1, neq
780 rscale(n) = done / dscale(n)
781 hscale(n) = dscale2(n)
782 END DO
783 IF (iord /= 0) THEN
784 CALL dvperm(neq, rscale, lorder)
785 CALL dvperm(neq, hscale, lorder)
786 END IF
787 end SUBROUTINE ims_base_cnvg_scale
788
789 !> @brief Update the preconditioner
790 !!
791 !! Update the preconditioner using the current coefficient matrix.
792 !!
793 !<
794 SUBROUTINE ims_base_pcu(IOUT, NJA, NEQ, NIAPC, NJAPC, IPC, RELAX, &
795 AMAT, IA, JA, APC, IAPC, JAPC, IW, W, &
796 LEVEL, DROPTOL, NJLU, NJW, NWLU, JLU, JW, WLU)
797 ! -- modules
799 ! -- dummy variables
800 integer(I4B), INTENT(IN) :: IOUT !< simulation listing file unit
801 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
802 integer(I4B), INTENT(IN) :: NEQ !< number of equations
803 integer(I4B), INTENT(IN) :: NIAPC !< preconditioner number of rows
804 integer(I4B), INTENT(IN) :: NJAPC !< preconditioner number of non-zero entries
805 integer(I4B), INTENT(IN) :: IPC !< preconditioner (1) ILU0 (2) MILU0 (3) ILUT (4) MILUT
806 real(DP), INTENT(IN) :: RELAX !< preconditioner relaxation factor for MILU0 and MILUT
807 real(DP), DIMENSION(NJA), INTENT(IN) :: AMAT !< coefficient matrix
808 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< CRS row pointers
809 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< CRS column pointers
810 real(DP), DIMENSION(NJAPC), INTENT(INOUT) :: APC !< preconditioner matrix
811 integer(I4B), DIMENSION(NIAPC + 1), INTENT(INOUT) :: IAPC !< preconditioner CRS row pointers
812 integer(I4B), DIMENSION(NJAPC), INTENT(INOUT) :: JAPC !< preconditioner CRS column pointers
813 integer(I4B), DIMENSION(NIAPC), INTENT(INOUT) :: IW !< preconditioner integer work vector
814 real(DP), DIMENSION(NIAPC), INTENT(INOUT) :: W !< preconditioner work vector
815 ! -- ILUT dummy variables
816 integer(I4B), INTENT(IN) :: LEVEL !< number of levels of fill for ILUT and MILUT
817 real(DP), INTENT(IN) :: DROPTOL !< drop tolerance
818 integer(I4B), INTENT(IN) :: NJLU !< length of JLU working vector
819 integer(I4B), INTENT(IN) :: NJW !< length of JW working vector
820 integer(I4B), INTENT(IN) :: NWLU !< length of WLU working vector
821 integer(I4B), DIMENSION(NJLU), INTENT(INOUT) :: JLU !< ILUT/MILUT JLU working vector
822 integer(I4B), DIMENSION(NJW), INTENT(INOUT) :: JW !< ILUT/MILUT JW working vector
823 real(DP), DIMENSION(NWLU), INTENT(INOUT) :: WLU !< ILUT/MILUT WLU working vector
824 ! -- local variables
825 character(len=LINELENGTH) :: errmsg
826 character(len=100), dimension(5), parameter :: cerr = &
827 ["Elimination process has generated a row in L or U whose length is > n.", &
828 &"The matrix L overflows the array al. ", &
829 &"The matrix U overflows the array alu. ", &
830 &"Illegal value for lfil. ", &
831 &"Zero row encountered. "]
832 integer(i4b) :: ipcflag
833 integer(I4B) :: icount
834 integer(I4B) :: ierr
835 real(DP) :: delta
836 real(DP) :: relax_used
837 ! -- formats
8382000 FORMAT(/, ' MATRIX IS SEVERELY NON-DIAGONALLY DOMINANT.', &
839 /, ' ADDED SMALL VALUE TO PIVOT ', i0, ' TIMES IN', &
840 ' IMSLINEARSUB_PCU.')
841 !
842 ! -- initialize local variables
843 ipcflag = 0
844 icount = 0
845 delta = dzero
846 relax_used = relax
847 pcscale: DO
848 SELECT CASE (ipc)
849 !
850 ! -- ILU0 AND MILU0
851 CASE (ipc_ilu0, ipc_milu0)
852 CALL ims_base_pcilu0(nja, neq, amat, ia, ja, &
853 apc, iapc, japc, iw, w, &
854 relax_used, ipcflag, delta)
855 !
856 ! -- ILUT AND MILUT
857 CASE (ipc_ilut, ipc_milut)
858 ierr = 0
859 CALL ilut(neq, amat, ja, ia, level, droptol, &
860 apc, jlu, iw, njapc, wlu, jw, ierr, &
861 relax_used, ipcflag, delta)
862 if (ierr /= 0) then
863 if (ierr > 0) then
864 write (errmsg, '(a,1x,i0,1x,a)') &
865 'ILUT: zero pivot encountered at step number', ierr, '.'
866 else
867 write (errmsg, '(a,1x,a)') 'ILUT:', cerr(-ierr)
868 end if
869 call store_error(errmsg)
870 call parser%StoreErrorUnit()
871 end if
872 !
873 ! -- ADDITIONAL PRECONDITIONERS
874 CASE DEFAULT
875 ipcflag = 0
876 END SELECT
877 IF (ipcflag < 1) THEN
878 EXIT pcscale
879 END IF
880 !
881 ! -- a modified factorization can break down where the unmodified
882 ! one does not: first retry without relaxation
883 if (relax_used > dzero) then
884 relax_used = dzero
885 ipcflag = 0
886 cycle pcscale
887 end if
888 delta = 1.5d0 * delta + dem3
889 ipcflag = 0
890 IF (delta > dhalf) THEN
891 delta = dhalf
892 ipcflag = 2
893 END IF
894 icount = icount + 1
895 !
896 ! -- replace bad pivots on the last attempt so it completes
897 if (icount == 10) then
898 ipcflag = 2
899 end if
900 !
901 ! -- terminate pcscale loop if not making progress
902 if (icount > 10) then
903 exit pcscale
904 end if
905
906 END DO pcscale
907 !
908 ! -- write error message if small value added to pivot
909 if (icount > 0) then
910 write (iout, 2000) icount
911 end if
912 end SUBROUTINE ims_base_pcu
913
914 !> @brief Jacobi preconditioner
915 !!
916 !! Calculate the Jacobi preconditioner (inverse of the diagonal) using
917 !! the current coefficient matrix.
918 !!
919 !<
920 SUBROUTINE ims_base_pcjac(NJA, NEQ, AMAT, APC, IA, JA)
921 ! -- dummy variables
922 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
923 integer(I4B), INTENT(IN) :: NEQ !< number of equations
924 real(DP), DIMENSION(NJA), INTENT(IN) :: AMAT !< coefficient matrix
925 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: APC !< preconditioner matrix
926 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< CRS row pointers
927 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< CRS column pointers
928 ! -- local variables
929 integer(I4B) :: i, n
930 integer(I4B) :: ic0, ic1
931 integer(I4B) :: id
932 real(DP) :: tv
933 ! -- code
934 DO n = 1, neq
935 ic0 = ia(n)
936 ic1 = ia(n + 1) - 1
937 id = ia(n)
938 DO i = ic0, ic1
939 IF (ja(i) == n) THEN
940 id = i
941 EXIT
942 END IF
943 END DO
944 tv = amat(id)
945 IF (abs(tv) > dzero) tv = done / tv
946 apc(n) = tv
947 END DO
948 end SUBROUTINE ims_base_pcjac
949
950 !> @brief Apply the Jacobi preconditioner
951 !!
952 !! Apply the Jacobi preconditioner and return the resultant vector.
953 !!
954 !<
955 SUBROUTINE ims_base_jaca(NEQ, A, D1, D2)
956 ! -- dummy variables
957 integer(I4B), INTENT(IN) :: NEQ !< number of equations
958 real(DP), DIMENSION(NEQ), INTENT(IN) :: A !< Jacobi preconditioner
959 real(DP), DIMENSION(NEQ), INTENT(IN) :: D1 !< input vector
960 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: D2 !< resultant vector
961 ! -- local variables
962 integer(I4B) :: n
963 real(DP) :: tv
964 ! -- code
965 DO n = 1, neq
966 tv = a(n) * d1(n)
967 d2(n) = tv
968 END DO
969 end SUBROUTINE ims_base_jaca
970
971 !> @brief Update the ILU0 preconditioner
972 !!
973 !! Update the ILU0 preconditioner using the current coefficient matrix.
974 !!
975 !<
976 SUBROUTINE ims_base_pcilu0(NJA, NEQ, AMAT, IA, JA, &
977 APC, IAPC, JAPC, IW, W, &
978 RELAX, IPCFLAG, DELTA)
979 ! -- dummy variables
980 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
981 integer(I4B), INTENT(IN) :: NEQ !< number of equations
982 real(DP), DIMENSION(NJA), INTENT(IN) :: AMAT !< coefficient matrix
983 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< CRS row pointers
984 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< CRS column pointers
985 real(DP), DIMENSION(NJA), INTENT(INOUT) :: APC !< preconditioned matrix
986 integer(I4B), DIMENSION(NEQ + 1), INTENT(INOUT) :: IAPC !< preconditioner CRS row pointers
987 integer(I4B), DIMENSION(NJA), INTENT(INOUT) :: JAPC !< preconditioner CRS column pointers
988 integer(I4B), DIMENSION(NEQ), INTENT(INOUT) :: IW !< preconditioner integer work vector
989 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: W !< preconditioner work vector
990 real(DP), INTENT(IN) :: RELAX !< MILU0 preconditioner relaxation factor
991 integer(I4B), INTENT(INOUT) :: IPCFLAG !< preconditioner error flag
992 real(DP), INTENT(IN) :: DELTA !< factor used to correct non-diagonally dominant matrices
993 ! -- local variables
994 integer(I4B) :: ic0, ic1
995 integer(I4B) :: iic0, iic1
996 integer(I4B) :: iu, iiu
997 integer(I4B) :: j, n
998 integer(I4B) :: jj
999 integer(I4B) :: jcol, jw
1000 integer(I4B) :: jjcol
1001 real(DP) :: drelax
1002 real(DP) :: sd1
1003 real(DP) :: tl
1004 real(DP) :: rs
1005 real(DP) :: d
1006 !
1007 ! -- initialize local variables
1008 drelax = relax
1009 DO n = 1, neq
1010 iw(n) = 0
1011 w(n) = dzero
1012 END DO
1013 main: DO n = 1, neq
1014 ic0 = ia(n)
1015 ic1 = ia(n + 1) - 1
1016 DO j = ic0, ic1
1017 jcol = ja(j)
1018 iw(jcol) = 1
1019 w(jcol) = w(jcol) + amat(j)
1020 END DO
1021 ic0 = iapc(n)
1022 ic1 = iapc(n + 1) - 1
1023 iu = japc(n)
1024 rs = dzero
1025 lower: DO j = ic0, iu - 1
1026 jcol = japc(j)
1027 iic0 = iapc(jcol)
1028 iic1 = iapc(jcol + 1) - 1
1029 iiu = japc(jcol)
1030 tl = w(jcol) * apc(jcol)
1031 w(jcol) = tl
1032 DO jj = iiu, iic1
1033 jjcol = japc(jj)
1034 jw = iw(jjcol)
1035 IF (jw .NE. 0) THEN
1036 w(jjcol) = w(jjcol) - tl * apc(jj)
1037 ELSE
1038 rs = rs + tl * apc(jj)
1039 END IF
1040 END DO
1041 END DO lower
1042 !
1043 ! -- DIAGONAL - CALCULATE INVERSE OF DIAGONAL FOR SOLUTION
1044 d = w(n)
1045 tl = (done + delta) * d - (drelax * rs)
1046 !
1047 ! -- ENSURE THAT THE SIGN OF THE DIAGONAL HAS NOT CHANGED AND IS
1048 sd1 = sign(d, tl)
1049 IF (sd1 .NE. d) THEN
1050 !
1051 ! -- USE SMALL VALUE IF DIAGONAL SCALING IS NOT EFFECTIVE FOR
1052 ! PIVOTS THAT CHANGE THE SIGN OF THE DIAGONAL
1053 IF (ipcflag > 1) THEN
1054 tl = sign(dem6, d)
1055 !
1056 ! -- DIAGONAL SCALING CONTINUES TO BE EFFECTIVE
1057 ELSE
1058 ipcflag = 1
1059 EXIT main
1060 END IF
1061 END IF
1062 IF (abs(tl) == dzero) THEN
1063 !
1064 ! -- USE SMALL VALUE IF DIAGONAL SCALING IS NOT EFFECTIVE FOR
1065 ! ZERO PIVOTS
1066 IF (ipcflag > 1) THEN
1067 tl = sign(dem6, d)
1068 !
1069 ! -- DIAGONAL SCALING CONTINUES TO BE EFFECTIVE FOR ELIMINATING
1070 ELSE
1071 ipcflag = 1
1072 EXIT main
1073 END IF
1074 END IF
1075 apc(n) = done / tl
1076 !
1077 ! -- RESET POINTER FOR IW TO ZERO
1078 iw(n) = 0
1079 w(n) = dzero
1080 DO j = ic0, ic1
1081 jcol = japc(j)
1082 apc(j) = w(jcol)
1083 iw(jcol) = 0
1084 w(jcol) = dzero
1085 END DO
1086 END DO main
1087 !
1088 ! -- AFTER SUCCESSFUL COMPLETION OF MAIN, WHICH LEAVES n = NEQ + 1, SCALE
1089 ! THE UPPER ENTRIES BY THE INVERSE OF THE DIAGONAL AND RESET IPCFLAG;
1090 ! AFTER EXIT MAIN IPCFLAG STAYS 1 SO THE CALLER RETRIES
1091 IF (n > neq) THEN
1092 DO n = 1, neq
1093 DO j = japc(n), iapc(n + 1) - 1
1094 apc(j) = apc(j) * apc(n)
1095 END DO
1096 END DO
1097 ipcflag = 0
1098 END IF
1099 end SUBROUTINE ims_base_pcilu0
1100
1101 !> @brief Apply the ILU0 and MILU0 preconditioners
1102 !!
1103 !! Apply the ILU0 and MILU0 preconditioners to the passed vector (R).
1104 !!
1105 !<
1106 SUBROUTINE ims_base_ilu0a(NJA, NEQ, APC, IAPC, JAPC, R, D)
1107 ! -- dummy variables
1108 integer(I4B), INTENT(IN) :: NJA !< number of non-zero entries
1109 integer(I4B), INTENT(IN) :: NEQ !< number of equations
1110 real(DP), DIMENSION(NJA), INTENT(IN) :: APC !< ILU0/MILU0 preconditioner matrix
1111 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IAPC !< ILU0/MILU0 preconditioner CRS row pointers
1112 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JAPC !< ILU0/MILU0 preconditioner CRS column pointers
1113 real(DP), DIMENSION(NEQ), INTENT(IN) :: R !< input vector
1114 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: D !< output vector after applying APC to R
1115 ! -- local variables
1116 integer(I4B) :: ic0, ic1
1117 integer(I4B) :: iu
1118 integer(I4B) :: jcol
1119 integer(I4B) :: j, n
1120 real(DP) :: tv
1121 !
1122 ! -- FORWARD SOLVE - APC * D = R
1123 forward: DO n = 1, neq
1124 tv = r(n)
1125 ic0 = iapc(n)
1126 ic1 = iapc(n + 1) - 1
1127 iu = japc(n) - 1
1128 lower: DO j = ic0, iu
1129 jcol = japc(j)
1130 tv = tv - apc(j) * d(jcol)
1131 END DO lower
1132 d(n) = tv
1133 END DO forward
1134 !
1135 ! -- BACKWARD SOLVE - D = D / U, WITH THE NEAREST COLUMN SUBTRACTED
1136 ! LAST TO SHORTEN THE DEPENDENCY CHAIN
1137 backward: DO n = neq, 1, -1
1138 ic1 = iapc(n + 1) - 1
1139 iu = japc(n)
1140 tv = d(n) * apc(n)
1141 upper: DO j = ic1, iu, -1
1142 jcol = japc(j)
1143 tv = tv - apc(j) * d(jcol)
1144 END DO upper
1145 d(n) = tv
1146 END DO backward
1147 end SUBROUTINE ims_base_ilu0a
1148
1149 !> @brief Test for solver convergence
1150 !!
1151 !! General routine for testing for solver convergence based on the
1152 !! user-specified convergence option (Icnvgopt).
1153 !<
1154 !
1155 ! -- TEST FOR SOLVER CONVERGENCE
1156 SUBROUTINE ims_base_testcnvg(Icnvgopt, Icnvg, Iiter, &
1157 Dvmax, Rmax, &
1158 Rmax0, Epfact, Dvclose, Rclose)
1159 ! -- dummy variables
1160 integer(I4B), INTENT(IN) :: Icnvgopt !< convergence option - see documentation for option
1161 integer(I4B), INTENT(INOUT) :: Icnvg !< flag indicating if convergence achieved (1) or not (0)
1162 integer(I4B), INTENT(IN) :: Iiter !< inner iteration number (used for strict convergence option)
1163 real(DP), INTENT(IN) :: Dvmax !< maximum dependent-variable change
1164 real(DP), INTENT(IN) :: Rmax !< maximum flow change
1165 real(DP), INTENT(IN) :: Rmax0 !< initial flow change (initial L2-norm)
1166 real(DP), INTENT(IN) :: Epfact !< factor for reducing convergence criteria in subsequent Picard iterations
1167 real(DP), INTENT(IN) :: Dvclose !< Maximum depenendent-variable change allowed
1168 real(DP), INTENT(IN) :: Rclose !< Maximum flow change allowed
1169 ! -- code
1170 IF (icnvgopt == 0) THEN
1171 IF (abs(dvmax) <= dvclose .AND. abs(rmax) <= rclose) THEN
1172 icnvg = 1
1173 END IF
1174 ELSE IF (icnvgopt == 1) THEN
1175 IF (abs(dvmax) <= dvclose .AND. abs(rmax) <= rclose) THEN
1176 IF (iiter == 1) THEN
1177 icnvg = 1
1178 ELSE
1179 icnvg = -1
1180 END IF
1181 END IF
1182 ELSE IF (icnvgopt == 2) THEN
1183 IF (abs(dvmax) <= dvclose .OR. rmax <= rclose) THEN
1184 icnvg = 1
1185 ELSE IF (rmax <= rmax0 * epfact) THEN
1186 icnvg = -1
1187 END IF
1188 ELSE IF (icnvgopt == 3) THEN
1189 IF (abs(dvmax) <= dvclose) THEN
1190 icnvg = 1
1191 ELSE IF (rmax <= rmax0 * rclose) THEN
1192 icnvg = -1
1193 END IF
1194 ELSE IF (icnvgopt == 4) THEN
1195 IF (abs(dvmax) <= dvclose .AND. rmax <= rclose) THEN
1196 icnvg = 1
1197 ELSE IF (rmax <= rmax0 * epfact) THEN
1198 icnvg = -1
1199 END IF
1200 END IF
1201 end SUBROUTINE ims_base_testcnvg
1202
1203 subroutine ims_calc_pcdims(neq, nja, ia, level, ipc, &
1204 niapc, njapc, njlu, njw, nwlu)
1205 integer(I4B), intent(in) :: neq !< nr. of rows A
1206 integer(I4B), intent(in) :: nja !< nr. of nonzeros A
1207 integer(I4B), dimension(:), intent(in) :: ia !< CSR row pointers A
1208 integer(I4B), intent(in) :: level !< fill level ILU
1209 integer(I4B), intent(in) :: ipc !< IMS preconditioner type
1210 integer(I4B), intent(inout) :: niapc !< work array size
1211 integer(I4B), intent(inout) :: njapc !< work array size
1212 integer(I4B), intent(inout) :: njlu !< work array size
1213 integer(I4B), intent(inout) :: njw !< work array size
1214 integer(I4B), intent(inout) :: nwlu !< work array size
1215 ! local
1216 integer(I4B) :: n, i
1217 integer(I4B) :: ijlu, ijw, iwlu, iwk
1218
1219 ijlu = 1
1220 ijw = 1
1221 iwlu = 1
1222
1223 ! ILU0 and MILU0
1224 niapc = neq
1225 njapc = nja
1226
1227 ! ILUT and MILUT
1228 if (ipc == 3 .or. ipc == 4) then
1229 niapc = neq
1230 if (level > 0) then
1231 iwk = neq * (level * 2 + 1)
1232 else
1233 iwk = 0
1234 do n = 1, neq
1235 i = ia(n + 1) - ia(n)
1236 if (i > iwk) then
1237 iwk = i
1238 end if
1239 end do
1240 iwk = neq * iwk
1241 end if
1242 njapc = iwk
1243 ijlu = iwk
1244 ijw = 2 * neq
1245 iwlu = neq + 1
1246 end if
1247
1248 njlu = ijlu
1249 njw = ijw
1250 nwlu = iwlu
1251
1252 end subroutine ims_calc_pcdims
1253
1254 !> @brief Generate CRS pointers for the preconditioner
1255 !!
1256 !! Generate the CRS row and column pointers for the preconditioner.
1257 !! JAPC(1:NEQ) hHas the position of the upper entry for a row,
1258 !! JAPC(NEQ+1:NJA) is the column position for entry,
1259 !! APC(1:NEQ) is the preconditioned inverse of the diagonal, and
1260 !! APC(NEQ+1:NJA) are the preconditioned entries for off diagonals.
1261 !<
1262 SUBROUTINE ims_base_pccrs(NEQ, NJA, IA, JA, &
1263 IAPC, JAPC)
1264 ! -- dummy variables
1265 integer(I4B), INTENT(IN) :: NEQ !<
1266 integer(I4B), INTENT(IN) :: NJA !<
1267 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !<
1268 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !<
1269 integer(I4B), DIMENSION(NEQ + 1), INTENT(INOUT) :: IAPC !<
1270 integer(I4B), DIMENSION(NJA), INTENT(INOUT) :: JAPC !<
1271 ! -- local variables
1272 integer(I4B) :: n, j
1273 integer(I4B) :: i0, i1
1274 integer(I4B) :: nlen
1275 integer(I4B) :: ic, ip
1276 integer(I4B) :: jcol
1277 integer(I4B), DIMENSION(:), ALLOCATABLE :: iarr
1278 ! -- code
1279 ip = neq + 1
1280 DO n = 1, neq
1281 i0 = ia(n)
1282 i1 = ia(n + 1) - 1
1283 nlen = i1 - i0
1284 ALLOCATE (iarr(nlen))
1285 ic = 0
1286 DO j = i0, i1
1287 jcol = ja(j)
1288 IF (jcol == n) cycle
1289 ic = ic + 1
1290 iarr(ic) = jcol
1291 END DO
1292 CALL ims_base_isort(nlen, iarr)
1293 iapc(n) = ip
1294 DO j = 1, nlen
1295 jcol = iarr(j)
1296 japc(ip) = jcol
1297 ip = ip + 1
1298 END DO
1299 DEALLOCATE (iarr)
1300 END DO
1301 iapc(neq + 1) = nja + 1
1302 !
1303 ! -- POSITION OF THE FIRST UPPER ENTRY FOR ROW
1304 DO n = 1, neq
1305 i0 = iapc(n)
1306 i1 = iapc(n + 1) - 1
1307 japc(n) = iapc(n + 1)
1308 DO j = i0, i1
1309 jcol = japc(j)
1310 IF (jcol > n) THEN
1311 japc(n) = j
1312 EXIT
1313 END IF
1314 END DO
1315 END DO
1316 end SUBROUTINE ims_base_pccrs
1317
1318 !> @brief In-place sorting for an integer array
1319 !!
1320 !! Subroutine sort an integer array in-place.
1321 !!
1322 !<
1323 SUBROUTINE ims_base_isort(NVAL, IARRAY)
1324 ! -- dummy variables
1325 integer(I4B), INTENT(IN) :: NVAL !< length of the integer array
1326 integer(I4B), DIMENSION(NVAL), INTENT(INOUT) :: IARRAY !< integer array to be sorted
1327 ! -- local variables
1328 integer(I4B) :: i, j, itemp
1329 ! -- code
1330 DO i = 1, nval - 1
1331 DO j = i + 1, nval
1332 if (iarray(i) > iarray(j)) then
1333 itemp = iarray(j)
1334 iarray(j) = iarray(i)
1335 iarray(i) = itemp
1336 END IF
1337 END DO
1338 END DO
1339 end SUBROUTINE ims_base_isort
1340
1341 !> @brief Calculate residual
1342 !!
1343 !! Subroutine to calculate the residual.
1344 !!
1345 !<
1346 SUBROUTINE ims_base_residual(NEQ, NJA, X, B, D, A, IA, JA)
1347 ! -- dummy variables
1348 integer(I4B), INTENT(IN) :: NEQ !< length of vectors
1349 integer(I4B), INTENT(IN) :: NJA !< length of coefficient matrix
1350 real(DP), DIMENSION(NEQ), INTENT(IN) :: X !< dependent variable
1351 real(DP), DIMENSION(NEQ), INTENT(IN) :: B !< right-hand side
1352 real(DP), DIMENSION(NEQ), INTENT(INOUT) :: D !< residual
1353 real(DP), DIMENSION(NJA), INTENT(IN) :: A !< coefficient matrix
1354 integer(I4B), DIMENSION(NEQ + 1), INTENT(IN) :: IA !< CRS row pointers
1355 integer(I4B), DIMENSION(NJA), INTENT(IN) :: JA !< CRS column pointers
1356 ! -- local variables
1357 integer(I4B) :: n
1358 ! -- code
1359 !
1360 ! -- calculate matrix-vector product
1361 call amux(neq, x, d, a, ja, ia)
1362 !
1363 ! -- subtract matrix-vector product from right-hand side
1364 DO n = 1, neq
1365 d(n) = b(n) - d(n)
1366 END DO
1367 end SUBROUTINE ims_base_residual
1368
1369 !> @brief Function returning EPFACT
1370 !<
1371 function ims_base_epfact(icnvgopt, kstp) result(epfact)
1372 integer(I4B) :: icnvgopt !< IMS convergence option
1373 integer(I4B) :: kstp !< time step number
1374 real(dp) :: epfact !< factor for decreasing convergence criteria in subsequent Picard iterations
1375
1376 if (icnvgopt == 2) then
1377 if (kstp == 1) then
1378 epfact = 0.01
1379 else
1380 epfact = 0.10
1381 end if
1382 else if (icnvgopt == 4) then
1383 epfact = dem4
1384 else
1385 epfact = done
1386 end if
1387
1388 end function ims_base_epfact
1389
1390END MODULE imslinearbasemodule
subroutine ilut(n, a, ja, ia, lfil, droptol, alu, jlu, ju, iwk, w, jw, ierr, relax, izero, delta)
Definition ilut.f90:51
subroutine lusol(n, y, x, alu, jlu, ju)
Definition ilut.f90:466
This module contains block parser methods.
This module contains simulation constants.
Definition Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition Constants.f90:45
real(dp), parameter dhalf
real constant 1/2
Definition Constants.f90:68
real(dp), parameter dem3
real constant 1e-3
integer(i4b), parameter izero
integer constant zero
Definition Constants.f90:51
real(dp), parameter dem4
real constant 1e-4
real(dp), parameter dem6
real constant 1e-6
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
real(dp), parameter dprec
real constant machine precision
real(dp), parameter done
real constant 1
Definition Constants.f90:76
This module contains the IMS linear accelerator subroutines.
subroutine ims_base_pcjac(nja, neq, amat, apc, ia, ja)
Jacobi preconditioner.
subroutine ims_base_pcu(iout, nja, neq, niapc, njapc, ipc, relax, amat, ia, ja, apc, iapc, japc, iw, w, level, droptol, njlu, njw, nwlu, jlu, jw, wlu)
Update the preconditioner.
subroutine ims_calc_pcdims(neq, nja, ia, level, ipc, niapc, njapc, njlu, njw, nwlu)
subroutine ims_base_testcnvg(icnvgopt, icnvg, iiter, dvmax, rmax, rmax0, epfact, dvclose, rclose)
Test for solver convergence.
subroutine ims_base_isort(nval, iarray)
In-place sorting for an integer array.
subroutine ims_base_pcilu0(nja, neq, amat, ia, ja, apc, iapc, japc, iw, w, relax, ipcflag, delta)
Update the ILU0 preconditioner.
real(dp) function ims_base_epfact(icnvgopt, kstp)
Function returning EPFACT.
subroutine ims_base_jaca(neq, a, d1, d2)
Apply the Jacobi preconditioner.
subroutine ims_base_residual(neq, nja, x, b, d, a, ia, ja)
Calculate residual.
subroutine ims_base_ilu0a(nja, neq, apc, iapc, japc, r, d)
Apply the ILU0 and MILU0 preconditioners.
subroutine ims_base_cnvg_scale(neq, iord, dscale, dscale2, lorder, rscale, hscale)
Set the factors that return the residual and head change to model units, in the order of the reordere...
subroutine ims_base_cg(icnvg, itmax, innerit, neq, nja, niapc, njapc, ipc, icnvgopt, north, iscl, rscale, hscale, dvclose, rclose, l2norm0, epfact, ia0, ja0, a0, iapc, japc, apc, x, b, d, p, q, z, njlu, iw, jlu, nconv, convnmod, convmodstart, caccel, summary)
Preconditioned Conjugate Gradient linear accelerator.
type(blockparsertype), private parser
subroutine ims_base_calc_order(iord, neq, nja, ia, ja, lorder, iorder)
Calculate LORDER AND IORDER.
subroutine ims_base_bcgs(icnvg, itmax, innerit, neq, nja, niapc, njapc, ipc, icnvgopt, north, iscl, rscale, hscale, dvclose, rclose, l2norm0, epfact, ia0, ja0, a0, iapc, japc, apc, x, b, d, p, q, t, v, dhat, phat, qhat, njlu, iw, jlu, nconv, convnmod, convmodstart, caccel, summary)
Preconditioned BiConjugate Gradient Stabilized linear accelerator.
subroutine ims_base_scale(iopt, iscl, neq, nja, ia, ja, amat, x, b, dscale, dscale2)
Scale the coefficient matrix.
subroutine ims_base_pccrs(neq, nja, ia, ja, iapc, japc)
Generate CRS pointers for the preconditioner.
@ ipc_ilut
ILUT (incomplete LU with threshold).
@ ipc_ilu0
ILU0 (incomplete LU, zero fill).
subroutine, public ims_odrv(n, nja, nsp, ia, ja, p, ip, isp, flag)
This module defines variable data types.
Definition kind.f90:8
pure logical function, public is_close(a, b, rtol, atol, symmetric)
Check if a real value is approximately equal to another.
Definition MathUtil.f90:46
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
integer(i4b) function, public count_errors()
Return number of errors.
Definition Sim.f90:60
subroutine genrcm(node_num, adj_num, adj_row, adj, perm)
Definition rcm.f90:1004
subroutine amux(n, x, y, a, ja, ia)
Definition sparsekit.f90:2
subroutine dvperm(n, x, perm)
Definition sparsekit.f90:62
This structure stores the generic convergence info for a solution.