isdgmr.f 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. *DECK ISDGMR
  2. INTEGER FUNCTION ISDGMR (N, B, X, XL, NELT, IA, JA, A, ISYM,
  3. + MSOLVE, NMSL, ITOL, TOL, ITMAX, ITER, ERR, IUNIT, R, Z, DZ,
  4. + RWORK, IWORK, RNRM, BNRM, SB, SX, JSCAL, KMP, LGMR, MAXL,
  5. + MAXLP1, V, Q, SNORMW, PROD, R0NRM, HES, JPRE)
  6. C***BEGIN PROLOGUE ISDGMR
  7. C***SUBSIDIARY
  8. C***PURPOSE Generalized Minimum Residual Stop Test.
  9. C This routine calculates the stop test for the Generalized
  10. C Minimum RESidual (GMRES) iteration scheme. It returns a
  11. C non-zero if the error estimate (the type of which is
  12. C determined by ITOL) is less than the user specified
  13. C tolerance TOL.
  14. C***LIBRARY SLATEC (SLAP)
  15. C***CATEGORY D2A4, D2B4
  16. C***TYPE DOUBLE PRECISION (ISSGMR-S, ISDGMR-D)
  17. C***KEYWORDS GMRES, LINEAR SYSTEM, SLAP, SPARSE, STOP TEST
  18. C***AUTHOR Brown, Peter, (LLNL), pnbrown@llnl.gov
  19. C Hindmarsh, Alan, (LLNL), alanh@llnl.gov
  20. C Seager, Mark K., (LLNL), seager@llnl.gov
  21. C Lawrence Livermore National Laboratory
  22. C PO Box 808, L-60
  23. C Livermore, CA 94550 (510) 423-3141
  24. C***DESCRIPTION
  25. C
  26. C *Usage:
  27. C INTEGER N, NELT, IA(NELT), JA(NELT), ISYM, NMSL, ITOL
  28. C INTEGER ITMAX, ITER, IUNIT, IWORK(USER DEFINED), JSCAL
  29. C INTEGER KMP, LGMR, MAXL, MAXLP1, JPRE
  30. C DOUBLE PRECISION B(N), X(N), XL(MAXL), A(NELT), TOL, ERR,
  31. C $ R(N), Z(N), DZ(N), RWORK(USER DEFINED),
  32. C $ RNRM, BNRM, SB(N), SX(N), V(N,MAXLP1),
  33. C $ Q(2*MAXL), SNORMW, PROD, R0NRM,
  34. C $ HES(MAXLP1,MAXL)
  35. C EXTERNAL MSOLVE
  36. C
  37. C IF (ISDGMR(N, B, X, XL, NELT, IA, JA, A, ISYM, MSOLVE,
  38. C $ NMSL, ITOL, TOL, ITMAX, ITER, ERR, IUNIT, R, Z, DZ,
  39. C $ RWORK, IWORK, RNRM, BNRM, SB, SX, JSCAL,
  40. C $ KMP, LGMR, MAXL, MAXLP1, V, Q, SNORMW, PROD, R0NRM,
  41. C $ HES, JPRE) .NE. 0) THEN ITERATION DONE
  42. C
  43. C *Arguments:
  44. C N :IN Integer.
  45. C Order of the Matrix.
  46. C B :IN Double Precision B(N).
  47. C Right-hand-side vector.
  48. C X :IN Double Precision X(N).
  49. C Approximate solution vector as of the last restart.
  50. C XL :OUT Double Precision XL(N)
  51. C An array of length N used to hold the approximate
  52. C solution as of the current iteration. Only computed by
  53. C this routine when ITOL=11.
  54. C NELT :IN Integer.
  55. C Number of Non-Zeros stored in A.
  56. C IA :IN Integer IA(NELT).
  57. C JA :IN Integer JA(NELT).
  58. C A :IN Double Precision A(NELT).
  59. C These arrays contain the matrix data structure for A.
  60. C It could take any form. See "Description", in the DGMRES,
  61. C DSLUGM and DSDGMR routines for more details.
  62. C ISYM :IN Integer.
  63. C Flag to indicate symmetric storage format.
  64. C If ISYM=0, all non-zero entries of the matrix are stored.
  65. C If ISYM=1, the matrix is symmetric, and only the upper
  66. C or lower triangle of the matrix is stored.
  67. C MSOLVE :EXT External.
  68. C Name of a routine which solves a linear system Mz = r for z
  69. C given r with the preconditioning matrix M (M is supplied via
  70. C RWORK and IWORK arrays. The name of the MSOLVE routine must
  71. C be declared external in the calling program. The calling
  72. C sequence to MSOLVE is:
  73. C CALL MSOLVE(N, R, Z, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  74. C Where N is the number of unknowns, R is the right-hand side
  75. C vector and Z is the solution upon return. NELT, IA, JA, A and
  76. C ISYM are defined as above. RWORK is a double precision array
  77. C that can be used to pass necessary preconditioning information
  78. C and/or workspace to MSOLVE. IWORK is an integer work array
  79. C for the same purpose as RWORK.
  80. C NMSL :INOUT Integer.
  81. C A counter for the number of calls to MSOLVE.
  82. C ITOL :IN Integer.
  83. C Flag to indicate the type of convergence criterion used.
  84. C ITOL=0 Means the iteration stops when the test described
  85. C below on the residual RL is satisfied. This is
  86. C the "Natural Stopping Criteria" for this routine.
  87. C Other values of ITOL cause extra, otherwise
  88. C unnecessary, computation per iteration and are
  89. C therefore much less efficient.
  90. C ITOL=1 Means the iteration stops when the first test
  91. C described below on the residual RL is satisfied,
  92. C and there is either right or no preconditioning
  93. C being used.
  94. C ITOL=2 Implies that the user is using left
  95. C preconditioning, and the second stopping criterion
  96. C below is used.
  97. C ITOL=3 Means the iteration stops when the third test
  98. C described below on Minv*Residual is satisfied, and
  99. C there is either left or no preconditioning begin
  100. C used.
  101. C ITOL=11 is often useful for checking and comparing
  102. C different routines. For this case, the user must
  103. C supply the "exact" solution or a very accurate
  104. C approximation (one with an error much less than
  105. C TOL) through a common block,
  106. C COMMON /DSLBLK/ SOLN( )
  107. C If ITOL=11, iteration stops when the 2-norm of the
  108. C difference between the iterative approximation and
  109. C the user-supplied solution divided by the 2-norm
  110. C of the user-supplied solution is less than TOL.
  111. C Note that this requires the user to set up the
  112. C "COMMON /DSLBLK/ SOLN(LENGTH)" in the calling
  113. C routine. The routine with this declaration should
  114. C be loaded before the stop test so that the correct
  115. C length is used by the loader. This procedure is
  116. C not standard Fortran and may not work correctly on
  117. C your system (although it has worked on every
  118. C system the authors have tried). If ITOL is not 11
  119. C then this common block is indeed standard Fortran.
  120. C TOL :IN Double Precision.
  121. C Convergence criterion, as described above.
  122. C ITMAX :IN Integer.
  123. C Maximum number of iterations.
  124. C ITER :IN Integer.
  125. C The iteration for which to check for convergence.
  126. C ERR :OUT Double Precision.
  127. C Error estimate of error in final approximate solution, as
  128. C defined by ITOL. Letting norm() denote the Euclidean
  129. C norm, ERR is defined as follows..
  130. C
  131. C If ITOL=0, then ERR = norm(SB*(B-A*X(L)))/norm(SB*B),
  132. C for right or no preconditioning, and
  133. C ERR = norm(SB*(M-inverse)*(B-A*X(L)))/
  134. C norm(SB*(M-inverse)*B),
  135. C for left preconditioning.
  136. C If ITOL=1, then ERR = norm(SB*(B-A*X(L)))/norm(SB*B),
  137. C since right or no preconditioning
  138. C being used.
  139. C If ITOL=2, then ERR = norm(SB*(M-inverse)*(B-A*X(L)))/
  140. C norm(SB*(M-inverse)*B),
  141. C since left preconditioning is being
  142. C used.
  143. C If ITOL=3, then ERR = Max |(Minv*(B-A*X(L)))(i)/x(i)|
  144. C i=1,n
  145. C If ITOL=11, then ERR = norm(SB*(X(L)-SOLN))/norm(SB*SOLN).
  146. C IUNIT :IN Integer.
  147. C Unit number on which to write the error at each iteration,
  148. C if this is desired for monitoring convergence. If unit
  149. C number is 0, no writing will occur.
  150. C R :INOUT Double Precision R(N).
  151. C Work array used in calling routine. It contains
  152. C information necessary to compute the residual RL = B-A*XL.
  153. C Z :WORK Double Precision Z(N).
  154. C Workspace used to hold the pseudo-residual M z = r.
  155. C DZ :WORK Double Precision DZ(N).
  156. C Workspace used to hold temporary vector(s).
  157. C RWORK :WORK Double Precision RWORK(USER DEFINED).
  158. C Double Precision array that can be used by MSOLVE.
  159. C IWORK :WORK Integer IWORK(USER DEFINED).
  160. C Integer array that can be used by MSOLVE.
  161. C RNRM :IN Double Precision.
  162. C Norm of the current residual. Type of norm depends on ITOL.
  163. C BNRM :IN Double Precision.
  164. C Norm of the right hand side. Type of norm depends on ITOL.
  165. C SB :IN Double Precision SB(N).
  166. C Scaling vector for B.
  167. C SX :IN Double Precision SX(N).
  168. C Scaling vector for X.
  169. C JSCAL :IN Integer.
  170. C Flag indicating if scaling arrays SB and SX are being
  171. C used in the calling routine DPIGMR.
  172. C JSCAL=0 means SB and SX are not used and the
  173. C algorithm will perform as if all
  174. C SB(i) = 1 and SX(i) = 1.
  175. C JSCAL=1 means only SX is used, and the algorithm
  176. C performs as if all SB(i) = 1.
  177. C JSCAL=2 means only SB is used, and the algorithm
  178. C performs as if all SX(i) = 1.
  179. C JSCAL=3 means both SB and SX are used.
  180. C KMP :IN Integer
  181. C The number of previous vectors the new vector VNEW
  182. C must be made orthogonal to. (KMP .le. MAXL)
  183. C LGMR :IN Integer
  184. C The number of GMRES iterations performed on the current call
  185. C to DPIGMR (i.e., # iterations since the last restart) and
  186. C the current order of the upper Hessenberg
  187. C matrix HES.
  188. C MAXL :IN Integer
  189. C The maximum allowable order of the matrix H.
  190. C MAXLP1 :IN Integer
  191. C MAXPL1 = MAXL + 1, used for dynamic dimensioning of HES.
  192. C V :IN Double Precision V(N,MAXLP1)
  193. C The N by (LGMR+1) array containing the LGMR
  194. C orthogonal vectors V(*,1) to V(*,LGMR).
  195. C Q :IN Double Precision Q(2*MAXL)
  196. C A double precision array of length 2*MAXL containing the
  197. C components of the Givens rotations used in the QR
  198. C decomposition of HES.
  199. C SNORMW :IN Double Precision
  200. C A scalar containing the scaled norm of VNEW before it
  201. C is renormalized in DPIGMR.
  202. C PROD :IN Double Precision
  203. C The product s1*s2*...*sl = the product of the sines of the
  204. C Givens rotations used in the QR factorization of the
  205. C Hessenberg matrix HES.
  206. C R0NRM :IN Double Precision
  207. C The scaled norm of initial residual R0.
  208. C HES :IN Double Precision HES(MAXLP1,MAXL)
  209. C The upper triangular factor of the QR decomposition
  210. C of the (LGMR+1) by LGMR upper Hessenberg matrix whose
  211. C entries are the scaled inner-products of A*V(*,I)
  212. C and V(*,K).
  213. C JPRE :IN Integer
  214. C Preconditioner type flag.
  215. C (See description of IGWK(4) in DGMRES.)
  216. C
  217. C *Description
  218. C When using the GMRES solver, the preferred value for ITOL
  219. C is 0. This is due to the fact that when ITOL=0 the norm of
  220. C the residual required in the stopping test is obtained for
  221. C free, since this value is already calculated in the GMRES
  222. C algorithm. The variable RNRM contains the appropriate
  223. C norm, which is equal to norm(SB*(RL - A*XL)) when right or
  224. C no preconditioning is being performed, and equal to
  225. C norm(SB*Minv*(RL - A*XL)) when using left preconditioning.
  226. C Here, norm() is the Euclidean norm. Nonzero values of ITOL
  227. C require additional work to calculate the actual scaled
  228. C residual or its scaled/preconditioned form, and/or the
  229. C approximate solution XL. Hence, these values of ITOL will
  230. C not be as efficient as ITOL=0.
  231. C
  232. C *Cautions:
  233. C This routine will attempt to write to the Fortran logical output
  234. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  235. C this logical unit is attached to a file or terminal before calling
  236. C this routine with a non-zero value for IUNIT. This routine does
  237. C not check for the validity of a non-zero IUNIT unit number.
  238. C
  239. C This routine does not verify that ITOL has a valid value.
  240. C The calling routine should make such a test before calling
  241. C ISDGMR, as is done in DGMRES.
  242. C
  243. C***SEE ALSO DGMRES
  244. C***ROUTINES CALLED D1MACH, DCOPY, DNRM2, DRLCAL, DSCAL, DXLCAL
  245. C***COMMON BLOCKS DSLBLK
  246. C***REVISION HISTORY (YYMMDD)
  247. C 890404 DATE WRITTEN
  248. C 890404 Previous REVISION DATE
  249. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  250. C 890922 Numerous changes to prologue to make closer to SLATEC
  251. C standard. (FNF)
  252. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  253. C 910411 Prologue converted to Version 4.0 format. (BAB)
  254. C 910502 Corrected conversion errors, etc. (FNF)
  255. C 910502 Removed MSOLVE from ROUTINES CALLED list. (FNF)
  256. C 910506 Made subsidiary to DGMRES. (FNF)
  257. C 920407 COMMON BLOCK renamed DSLBLK. (WRB)
  258. C 920511 Added complete declaration section. (WRB)
  259. C 921026 Corrected D to E in output format. (FNF)
  260. C 921113 Corrected C***CATEGORY line. (FNF)
  261. C***END PROLOGUE ISDGMR
  262. C .. Scalar Arguments ..
  263. DOUBLE PRECISION BNRM, ERR, PROD, R0NRM, RNRM, SNORMW, TOL
  264. INTEGER ISYM, ITER, ITMAX, ITOL, IUNIT, JPRE, JSCAL, KMP, LGMR,
  265. + MAXL, MAXLP1, N, NELT, NMSL
  266. C .. Array Arguments ..
  267. DOUBLE PRECISION A(*), B(*), DZ(*), HES(MAXLP1, MAXL), Q(*), R(*),
  268. + RWORK(*), SB(*), SX(*), V(N,*), X(*), XL(*), Z(*)
  269. INTEGER IA(*), IWORK(*), JA(*)
  270. C .. Subroutine Arguments ..
  271. EXTERNAL MSOLVE
  272. C .. Arrays in Common ..
  273. DOUBLE PRECISION SOLN(1)
  274. C .. Local Scalars ..
  275. DOUBLE PRECISION DXNRM, FUZZ, RAT, RATMAX, SOLNRM, TEM
  276. INTEGER I, IELMAX
  277. C .. External Functions ..
  278. DOUBLE PRECISION D1MACH, DNRM2
  279. EXTERNAL D1MACH, DNRM2
  280. C .. External Subroutines ..
  281. EXTERNAL DCOPY, DRLCAL, DSCAL, DXLCAL
  282. C .. Intrinsic Functions ..
  283. INTRINSIC ABS, MAX, SQRT
  284. C .. Common blocks ..
  285. COMMON /DSLBLK/ SOLN
  286. C .. Save statement ..
  287. SAVE SOLNRM
  288. C***FIRST EXECUTABLE STATEMENT ISDGMR
  289. ISDGMR = 0
  290. IF ( ITOL.EQ.0 ) THEN
  291. C
  292. C Use input from DPIGMR to determine if stop conditions are met.
  293. C
  294. ERR = RNRM/BNRM
  295. ENDIF
  296. IF ( (ITOL.GT.0) .AND. (ITOL.LE.3) ) THEN
  297. C
  298. C Use DRLCAL to calculate the scaled residual vector.
  299. C Store answer in R.
  300. C
  301. IF ( LGMR.NE.0 ) CALL DRLCAL(N, KMP, LGMR, MAXL, V, Q, R,
  302. $ SNORMW, PROD, R0NRM)
  303. IF ( ITOL.LE.2 ) THEN
  304. C err = ||Residual||/||RightHandSide||(2-Norms).
  305. ERR = DNRM2(N, R, 1)/BNRM
  306. C
  307. C Unscale R by R0NRM*PROD when KMP < MAXL.
  308. C
  309. IF ( (KMP.LT.MAXL) .AND. (LGMR.NE.0) ) THEN
  310. TEM = 1.0D0/(R0NRM*PROD)
  311. CALL DSCAL(N, TEM, R, 1)
  312. ENDIF
  313. ELSEIF ( ITOL.EQ.3 ) THEN
  314. C err = Max |(Minv*Residual)(i)/x(i)|
  315. C When JPRE .lt. 0, R already contains Minv*Residual.
  316. IF ( JPRE.GT.0 ) THEN
  317. CALL MSOLVE(N, R, DZ, NELT, IA, JA, A, ISYM, RWORK,
  318. $ IWORK)
  319. NMSL = NMSL + 1
  320. ENDIF
  321. C
  322. C Unscale R by R0NRM*PROD when KMP < MAXL.
  323. C
  324. IF ( (KMP.LT.MAXL) .AND. (LGMR.NE.0) ) THEN
  325. TEM = 1.0D0/(R0NRM*PROD)
  326. CALL DSCAL(N, TEM, R, 1)
  327. ENDIF
  328. C
  329. FUZZ = D1MACH(1)
  330. IELMAX = 1
  331. RATMAX = ABS(DZ(1))/MAX(ABS(X(1)),FUZZ)
  332. DO 25 I = 2, N
  333. RAT = ABS(DZ(I))/MAX(ABS(X(I)),FUZZ)
  334. IF( RAT.GT.RATMAX ) THEN
  335. IELMAX = I
  336. RATMAX = RAT
  337. ENDIF
  338. 25 CONTINUE
  339. ERR = RATMAX
  340. IF( RATMAX.LE.TOL ) ISDGMR = 1
  341. IF( IUNIT.GT.0 ) WRITE(IUNIT,1020) ITER, IELMAX, RATMAX
  342. RETURN
  343. ENDIF
  344. ENDIF
  345. IF ( ITOL.EQ.11 ) THEN
  346. C
  347. C Use DXLCAL to calculate the approximate solution XL.
  348. C
  349. IF ( (LGMR.NE.0) .AND. (ITER.GT.0) ) THEN
  350. CALL DXLCAL(N, LGMR, X, XL, XL, HES, MAXLP1, Q, V, R0NRM,
  351. $ DZ, SX, JSCAL, JPRE, MSOLVE, NMSL, RWORK, IWORK,
  352. $ NELT, IA, JA, A, ISYM)
  353. ELSEIF ( ITER.EQ.0 ) THEN
  354. C Copy X to XL to check if initial guess is good enough.
  355. CALL DCOPY(N, X, 1, XL, 1)
  356. ELSE
  357. C Return since this is the first call to DPIGMR on a restart.
  358. RETURN
  359. ENDIF
  360. C
  361. IF ((JSCAL .EQ. 0) .OR.(JSCAL .EQ. 2)) THEN
  362. C err = ||x-TrueSolution||/||TrueSolution||(2-Norms).
  363. IF ( ITER.EQ.0 ) SOLNRM = DNRM2(N, SOLN, 1)
  364. DO 30 I = 1, N
  365. DZ(I) = XL(I) - SOLN(I)
  366. 30 CONTINUE
  367. ERR = DNRM2(N, DZ, 1)/SOLNRM
  368. ELSE
  369. IF (ITER .EQ. 0) THEN
  370. SOLNRM = 0
  371. DO 40 I = 1,N
  372. SOLNRM = SOLNRM + (SX(I)*SOLN(I))**2
  373. 40 CONTINUE
  374. SOLNRM = SQRT(SOLNRM)
  375. ENDIF
  376. DXNRM = 0
  377. DO 50 I = 1,N
  378. DXNRM = DXNRM + (SX(I)*(XL(I)-SOLN(I)))**2
  379. 50 CONTINUE
  380. DXNRM = SQRT(DXNRM)
  381. C err = ||SX*(x-TrueSolution)||/||SX*TrueSolution|| (2-Norms).
  382. ERR = DXNRM/SOLNRM
  383. ENDIF
  384. ENDIF
  385. C
  386. IF( IUNIT.NE.0 ) THEN
  387. IF( ITER.EQ.0 ) THEN
  388. WRITE(IUNIT,1000) N, ITOL, MAXL, KMP
  389. ENDIF
  390. WRITE(IUNIT,1010) ITER, RNRM/BNRM, ERR
  391. ENDIF
  392. IF ( ERR.LE.TOL ) ISDGMR = 1
  393. C
  394. RETURN
  395. 1000 FORMAT(' Generalized Minimum Residual(',I3,I3,') for ',
  396. $ 'N, ITOL = ',I5, I5,
  397. $ /' ITER',' Natural Err Est',' Error Estimate')
  398. 1010 FORMAT(1X,I4,1X,D16.7,1X,D16.7)
  399. 1020 FORMAT(1X,' ITER = ',I5, ' IELMAX = ',I5,
  400. $ ' |R(IELMAX)/X(IELMAX)| = ',D12.5)
  401. C------------- LAST LINE OF ISDGMR FOLLOWS ----------------------------
  402. END