isscgs.f 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. *DECK ISSCGS
  2. INTEGER FUNCTION ISSCGS (N, B, X, NELT, IA, JA, A, ISYM, MATVEC,
  3. + MSOLVE, ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, R0, P, Q,
  4. + U, V1, V2, RWORK, IWORK, AK, BK, BNRM, SOLNRM)
  5. C***BEGIN PROLOGUE ISSCGS
  6. C***SUBSIDIARY
  7. C***PURPOSE Preconditioned BiConjugate Gradient Squared Stop Test.
  8. C This routine calculates the stop test for the BiConjugate
  9. C Gradient Squared iteration scheme. It returns a non-zero
  10. C if the error estimate (the type of which is determined by
  11. C ITOL) is less than the user specified tolerance TOL.
  12. C***LIBRARY SLATEC (SLAP)
  13. C***CATEGORY D2A4, D2B4
  14. C***TYPE SINGLE PRECISION (ISSCGS-S, ISDCGS-D)
  15. C***KEYWORDS ITERATIVE PRECONDITION, NON-SYMMETRIC LINEAR SYSTEM, SLAP,
  16. C SPARSE, STOP TEST
  17. C***AUTHOR Greenbaum, Anne, (Courant Institute)
  18. C Seager, Mark K., (LLNL)
  19. C Lawrence Livermore National Laboratory
  20. C PO BOX 808, L-60
  21. C Livermore, CA 94550 (510) 423-3141
  22. C seager@llnl.gov
  23. C***DESCRIPTION
  24. C
  25. C *Usage:
  26. C INTEGER N, NELT, IA(NELT), JA(NELT), ISYM, ITOL, ITMAX, ITER
  27. C INTEGER IERR, IUNIT, IWORK(USER DEFINED)
  28. C REAL B(N), X(N), A(N), TOL, ERR, R(N), R0(N), P(N)
  29. C REAL Q(N), U(N), V1(N), V2(N)
  30. C REAL RWORK(USER DEFINED), AK, BK, BNRM, SOLNRM
  31. C EXTERNAL MATVEC, MSOLVE
  32. C
  33. C IF( ISSCGS(N, B, X, NELT, IA, JA, A, ISYM, MATVEC, MSOLVE, ITOL,
  34. C $ TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, R0, P, Q, U, V1,
  35. C $ V2, RWORK, IWORK, AK, BK, BNRM, SOLNRM) .NE. 0 )
  36. C $ THEN ITERATION DONE
  37. C
  38. C *Arguments:
  39. C N :IN Integer
  40. C Order of the Matrix.
  41. C B :IN Real B(N).
  42. C Right-hand side vector.
  43. C X :INOUT Real X(N).
  44. C On input X is your initial guess for solution vector.
  45. C On output X is the final approximate solution.
  46. C NELT :IN Integer.
  47. C Number of Non-Zeros stored in A.
  48. C IA :IN Integer IA(NELT).
  49. C JA :IN Integer JA(NELT).
  50. C A :IN Real A(NELT).
  51. C These arrays contain the matrix data structure for A.
  52. C It could take any form. See "Description" in SLAP routine
  53. C SCGS for more details.
  54. C ISYM :IN Integer.
  55. C Flag to indicate symmetric storage format.
  56. C If ISYM=0, all non-zero entries of the matrix are stored.
  57. C If ISYM=1, the matrix is symmetric, and only the upper
  58. C or lower triangle of the matrix is stored.
  59. C MATVEC :EXT External.
  60. C Name of a routine which performs the matrix vector multiply
  61. C operation Y = A*X given A and X. The name of the MATVEC
  62. C routine must be declared external in the calling program.
  63. C The calling sequence of MATVEC is:
  64. C CALL MATVEC( N, X, Y, NELT, IA, JA, A, ISYM )
  65. C Where N is the number of unknowns, Y is the product A*X upon
  66. C return, X is an input vector. NELT, IA, JA, A, and ISYM
  67. C define the SLAP matrix data structure.
  68. C MSOLVE :EXT External.
  69. C Name of a routine which solves a linear system MZ = R for Z
  70. C given R with the preconditioning matrix M (M is supplied via
  71. C RWORK and IWORK arrays). The name of the MSOLVE routine
  72. C must be declared external in the calling program. The
  73. C calling sequence of MSOLVE is:
  74. C CALL MSOLVE(N, R, Z, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  75. C Where N is the number of unknowns, R is the right-hand side
  76. C vector, and Z is the solution upon return. NELT, IA, JA, A,
  77. C and ISYM define the SLAP matrix data structure.
  78. C RWORK is a real array that can be used to pass necessary
  79. C preconditioning information and/or workspace to MSOLVE.
  80. C IWORK is an integer work array for the same purpose as RWORK.
  81. C ITOL :IN Integer.
  82. C Flag to indicate type of convergence criterion.
  83. C If ITOL=1, iteration stops when the 2-norm of the residual
  84. C divided by the 2-norm of the right-hand side is less than TOL.
  85. C This routine must calculate the residual from R = A*X - B.
  86. C This is unnatural and hence expensive for this type of iter-
  87. C ative method. ITOL=2 is *STRONGLY* recommended.
  88. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  89. C residual divided by the 2-norm of M-inv times the right hand
  90. C side is less than TOL, where M-inv time a vector is the pre-
  91. C conditioning step. This is the *NATURAL* stopping for this
  92. C iterative method and is *STRONGLY* recommended.
  93. C ITOL=11 is often useful for checking and comparing different
  94. C routines. For this case, the user must supply the "exact"
  95. C solution or a very accurate approximation (one with an error
  96. C much less than TOL) through a common block,
  97. C COMMON /SSLBLK/ SOLN( )
  98. C If ITOL=11, iteration stops when the 2-norm of the difference
  99. C between the iterative approximation and the user-supplied
  100. C solution divided by the 2-norm of the user-supplied solution
  101. C is less than TOL. Note that this requires the user to set up
  102. C the "COMMON /SSLBLK/ SOLN(LENGTH)" in the calling routine.
  103. C The routine with this declaration should be loaded before the
  104. C stop test so that the correct length is used by the loader.
  105. C This procedure is not standard Fortran and may not work
  106. C correctly on your system (although it has worked on every
  107. C system the authors have tried). If ITOL is not 11 then this
  108. C common block is indeed standard Fortran.
  109. C TOL :IN Real.
  110. C Convergence criterion, as described above.
  111. C ITMAX :IN Integer.
  112. C Maximum number of iterations.
  113. C ITER :IN Integer.
  114. C Current iteration count. (Must be zero on first call.)
  115. C ERR :OUT Real.
  116. C Error estimate of error in final approximate solution, as
  117. C defined by ITOL.
  118. C IERR :OUT Integer.
  119. C Error flag. IERR is set to 3 if ITOL is not one of the
  120. C acceptable values, see above.
  121. C IUNIT :IN Integer.
  122. C Unit number on which to write the error at each iteration,
  123. C if this is desired for monitoring convergence. If unit
  124. C number is 0, no writing will occur.
  125. C R :IN Real R(N).
  126. C The residual r = b - Ax.
  127. C R0 :WORK Real R0(N).
  128. C P :DUMMY Real P(N).
  129. C Q :DUMMY Real Q(N).
  130. C U :DUMMY Real U(N).
  131. C V1 :DUMMY Real V1(N).
  132. C Real arrays used for workspace.
  133. C V2 :WORK Real V2(N).
  134. C If ITOL.eq.1 then V2 is used to hold A * X - B on every call.
  135. C If ITOL.eq.2 then V2 is used to hold M-inv * B on the first
  136. C call.
  137. C If ITOL.eq.11 then V2 is used to X - SOLN.
  138. C RWORK :WORK Real RWORK(USER DEFINED).
  139. C Real array that can be used for workspace in MSOLVE.
  140. C IWORK :WORK Integer IWORK(USER DEFINED).
  141. C Integer array that can be used for workspace in MSOLVE.
  142. C AK :IN Real.
  143. C Current iterate BiConjugate Gradient iteration parameter.
  144. C BK :IN Real.
  145. C Current iterate BiConjugate Gradient iteration parameter.
  146. C BNRM :INOUT Real.
  147. C Norm of the right hand side. Type of norm depends on ITOL.
  148. C Calculated only on the first call.
  149. C SOLNRM :INOUT Real.
  150. C 2-Norm of the true solution, SOLN. Only computed and used
  151. C if ITOL = 11.
  152. C
  153. C *Function Return Values:
  154. C 0 : Error estimate (determined by ITOL) is *NOT* less than the
  155. C specified tolerance, TOL. The iteration must continue.
  156. C 1 : Error estimate (determined by ITOL) is less than the
  157. C specified tolerance, TOL. The iteration can be considered
  158. C complete.
  159. C
  160. C *Cautions:
  161. C This routine will attempt to write to the Fortran logical output
  162. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  163. C this logical unit is attached to a file or terminal before calling
  164. C this routine with a non-zero value for IUNIT. This routine does
  165. C not check for the validity of a non-zero IUNIT unit number.
  166. C
  167. C***SEE ALSO SCGS
  168. C***ROUTINES CALLED R1MACH, SNRM2
  169. C***COMMON BLOCKS SSLBLK
  170. C***REVISION HISTORY (YYMMDD)
  171. C 871119 DATE WRITTEN
  172. C 881213 Previous REVISION DATE
  173. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  174. C 890922 Numerous changes to prologue to make closer to SLATEC
  175. C standard. (FNF)
  176. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  177. C 891003 Removed C***REFER TO line, per MKS.
  178. C 910411 Prologue converted to Version 4.0 format. (BAB)
  179. C 910502 Removed MATVEC and MSOLVE from ROUTINES CALLED list. (FNF)
  180. C 910506 Made subsidiary to SCGS. (FNF)
  181. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  182. C 920511 Added complete declaration section. (WRB)
  183. C 920930 Corrected to not print AK,BK when ITER=0. (FNF)
  184. C 921026 Changed 1.0E10 to R1MACH(2). (FNF)
  185. C 921113 Corrected C***CATEGORY line. (FNF)
  186. C***END PROLOGUE ISSCGS
  187. C .. Scalar Arguments ..
  188. REAL AK, BK, BNRM, ERR, SOLNRM, TOL
  189. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, N, NELT
  190. C .. Array Arguments ..
  191. REAL A(NELT), B(N), P(N), Q(N), R(N), R0(N), RWORK(*), U(N),
  192. + V1(N), V2(N), X(N)
  193. INTEGER IA(NELT), IWORK(*), JA(NELT)
  194. C .. Subroutine Arguments ..
  195. EXTERNAL MATVEC, MSOLVE
  196. C .. Arrays in Common ..
  197. REAL SOLN(1)
  198. C .. Local Scalars ..
  199. INTEGER I
  200. C .. External Functions ..
  201. REAL R1MACH, SNRM2
  202. EXTERNAL R1MACH, SNRM2
  203. C .. Common blocks ..
  204. COMMON /SSLBLK/ SOLN
  205. C***FIRST EXECUTABLE STATEMENT ISSCGS
  206. ISSCGS = 0
  207. C
  208. IF( ITOL.EQ.1 ) THEN
  209. C err = ||Residual||/||RightHandSide|| (2-Norms).
  210. IF(ITER .EQ. 0) BNRM = SNRM2(N, B, 1)
  211. CALL MATVEC(N, X, V2, NELT, IA, JA, A, ISYM )
  212. DO 5 I = 1, N
  213. V2(I) = V2(I) - B(I)
  214. 5 CONTINUE
  215. ERR = SNRM2(N, V2, 1)/BNRM
  216. ELSE IF( ITOL.EQ.2 ) THEN
  217. C -1 -1
  218. C err = ||M Residual||/||M RightHandSide|| (2-Norms).
  219. IF(ITER .EQ. 0) THEN
  220. CALL MSOLVE(N, B, V2, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  221. BNRM = SNRM2(N, V2, 1)
  222. ENDIF
  223. ERR = SNRM2(N, R, 1)/BNRM
  224. ELSE IF( ITOL.EQ.11 ) THEN
  225. C err = ||x-TrueSolution||/||TrueSolution|| (2-Norms).
  226. IF(ITER .EQ. 0) SOLNRM = SNRM2(N, SOLN, 1)
  227. DO 10 I = 1, N
  228. V2(I) = X(I) - SOLN(I)
  229. 10 CONTINUE
  230. ERR = SNRM2(N, V2, 1)/SOLNRM
  231. ELSE
  232. C
  233. C If we get here ITOL is not one of the acceptable values.
  234. ERR = R1MACH(2)
  235. IERR = 3
  236. ENDIF
  237. C
  238. C Print the error and coefficients AK, BK on each step,
  239. C if desired.
  240. IF(IUNIT .NE. 0) THEN
  241. IF( ITER.EQ.0 ) THEN
  242. WRITE(IUNIT,1000) N, ITOL
  243. WRITE(IUNIT,1010) ITER, ERR
  244. ELSE
  245. WRITE(IUNIT,1010) ITER, ERR, AK, BK
  246. ENDIF
  247. ENDIF
  248. IF(ERR .LE. TOL) ISSCGS = 1
  249. C
  250. RETURN
  251. 1000 FORMAT(' Preconditioned BiConjugate Gradient Squared for ',
  252. $ 'N, ITOL = ',I5, I5,
  253. $ /' ITER',' Error Estimate',' Alpha',
  254. $ ' Beta')
  255. 1010 FORMAT(1X,I4,1X,E16.7,1X,E16.7,1X,E16.7)
  256. C------------- LAST LINE OF ISSCGS FOLLOWS ----------------------------
  257. END