issgmr.f 17 KB

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