issbcg.f 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. *DECK ISSBCG
  2. INTEGER FUNCTION ISSBCG (N, B, X, NELT, IA, JA, A, ISYM, MSOLVE,
  3. + ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, Z, P, RR, ZZ, PP,
  4. + DZ, RWORK, IWORK, AK, BK, BNRM, SOLNRM)
  5. C***BEGIN PROLOGUE ISSBCG
  6. C***SUBSIDIARY
  7. C***PURPOSE Preconditioned BiConjugate Gradient Stop Test.
  8. C This routine calculates the stop test for the BiConjugate
  9. C Gradient iteration scheme. It returns a non-zero if the
  10. C error estimate (the type of which is determined by ITOL)
  11. C is less than the user specified tolerance TOL.
  12. C***LIBRARY SLATEC (SLAP)
  13. C***CATEGORY D2A4, D2B4
  14. C***TYPE SINGLE PRECISION (ISSBCG-S, ISDBCG-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), Z(N), P(N)
  29. C REAL RR(N), ZZ(N), PP(N), DZ(N)
  30. C REAL RWORK(USER DEFINED), AK, BK, BNRM, SOLNRM
  31. C EXTERNAL MSOLVE
  32. C
  33. C IF( ISSBCG(N, B, X, NELT, IA, JA, A, ISYM, MSOLVE, ITOL, TOL,
  34. C $ ITMAX, ITER, ERR, IERR, IUNIT, R, Z, P, RR, ZZ, PP, DZ,
  35. C $ 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 the SLAP
  53. C routine SBCG 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 MSOLVE :EXT External.
  60. C Name of a routine which solves a linear system MZ = R for Z
  61. C given R with the preconditioning matrix M (M is supplied via
  62. C RWORK and IWORK arrays). The name of the MSOLVE routine
  63. C must be declared external in the calling program. The
  64. C calling sequence of MSOLVE is:
  65. C CALL MSOLVE(N, R, Z, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  66. C Where N is the number of unknowns, R is the right-hand side
  67. C vector, and Z is the solution upon return. NELT, IA, JA, A,
  68. C and ISYM define the SLAP matrix data structure.
  69. C RWORK is a real array that can be used to pass necessary
  70. C preconditioning information and/or workspace to MSOLVE.
  71. C IWORK is an integer work array for the same purpose as RWORK.
  72. C ITOL :IN Integer.
  73. C Flag to indicate type of convergence criterion.
  74. C If ITOL=1, iteration stops when the 2-norm of the residual
  75. C divided by the 2-norm of the right-hand side is less than TOL.
  76. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  77. C residual divided by the 2-norm of M-inv times the right hand
  78. C side is less than TOL, where M-inv is the inverse of the
  79. C diagonal of A.
  80. C ITOL=11 is often useful for checking and comparing different
  81. C routines. For this case, the user must supply the "exact"
  82. C solution or a very accurate approximation (one with an error
  83. C much less than TOL) through a common block,
  84. C COMMON /SSLBLK/ SOLN( )
  85. C If ITOL=11, iteration stops when the 2-norm of the difference
  86. C between the iterative approximation and the user-supplied
  87. C solution divided by the 2-norm of the user-supplied solution
  88. C is less than TOL. Note that this requires the user to set up
  89. C the "COMMON /SSLBLK/ SOLN(LENGTH)" in the calling routine.
  90. C The routine with this declaration should be loaded before the
  91. C stop test so that the correct length is used by the loader.
  92. C This procedure is not standard Fortran and may not work
  93. C correctly on your system (although it has worked on every
  94. C system the authors have tried). If ITOL is not 11 then this
  95. C common block is indeed standard Fortran.
  96. C TOL :IN Real.
  97. C Convergence criterion, as described above.
  98. C ITMAX :IN Integer.
  99. C Maximum number of iterations.
  100. C ITER :IN Integer.
  101. C Current iteration count. (Must be zero on first call.)
  102. C ERR :OUT Real.
  103. C Error estimate of error in final approximate solution, as
  104. C defined by ITOL.
  105. C IERR :OUT Integer.
  106. C Error flag. IERR is set to 3 if ITOL is not one of the
  107. C acceptable values, see above.
  108. C IUNIT :IN Integer.
  109. C Unit number on which to write the error at each iteration,
  110. C if this is desired for monitoring convergence. If unit
  111. C number is 0, no writing will occur.
  112. C R :IN Real R(N).
  113. C The residual r = b - Ax.
  114. C Z :WORK Real Z(N).
  115. C P :DUMMY Real P(N).
  116. C RR :DUMMY Real RR(N).
  117. C ZZ :DUMMY Real ZZ(N).
  118. C PP :DUMMY Real PP(N).
  119. C Real arrays used for workspace.
  120. C DZ :WORK Real DZ(N).
  121. C If ITOL.eq.0 then DZ is used to hold M-inv * B on the first
  122. C call. If ITOL.eq.11 then DZ is used to hold X-SOLN.
  123. C RWORK :WORK Real RWORK(USER DEFINED).
  124. C Real array that can be used for workspace in MSOLVE
  125. C and MTSOLV.
  126. C IWORK :WORK Integer IWORK(USER DEFINED).
  127. C Integer array that can be used for workspace in MSOLVE
  128. C and MTSOLV.
  129. C AK :IN Real.
  130. C Current iterate BiConjugate Gradient iteration parameter.
  131. C BK :IN Real.
  132. C Current iterate BiConjugate Gradient iteration parameter.
  133. C BNRM :INOUT Real.
  134. C Norm of the right hand side. Type of norm depends on ITOL.
  135. C Calculated only on the first call.
  136. C SOLNRM :INOUT Real.
  137. C 2-Norm of the true solution, SOLN. Only computed and used
  138. C if ITOL = 11.
  139. C
  140. C *Function Return Values:
  141. C 0 : Error estimate (determined by ITOL) is *NOT* less than the
  142. C specified tolerance, TOL. The iteration must continue.
  143. C 1 : Error estimate (determined by ITOL) is less than the
  144. C specified tolerance, TOL. The iteration can be considered
  145. C complete.
  146. C
  147. C *Cautions:
  148. C This routine will attempt to write to the Fortran logical output
  149. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  150. C this logical unit is attached to a file or terminal before calling
  151. C this routine with a non-zero value for IUNIT. This routine does
  152. C not check for the validity of a non-zero IUNIT unit number.
  153. C
  154. C***SEE ALSO SBCG
  155. C***ROUTINES CALLED R1MACH, SNRM2
  156. C***COMMON BLOCKS SSLBLK
  157. C***REVISION HISTORY (YYMMDD)
  158. C 871119 DATE WRITTEN
  159. C 881213 Previous REVISION DATE
  160. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  161. C 890922 Numerous changes to prologue to make closer to SLATEC
  162. C standard. (FNF)
  163. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  164. C 891003 Removed C***REFER TO line, per MKS.
  165. C 910411 Prologue converted to Version 4.0 format. (BAB)
  166. C 910502 Removed MSOLVE from ROUTINES CALLED list. (FNF)
  167. C 910506 Made subsidiary to SBCG. (FNF)
  168. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  169. C 920511 Added complete declaration section. (WRB)
  170. C 920930 Corrected to not print AK,BK when ITER=0. (FNF)
  171. C 921026 Changed 1.0E10 to R1MACH(2). (FNF)
  172. C 921113 Corrected C***CATEGORY line. (FNF)
  173. C***END PROLOGUE ISSBCG
  174. C .. Scalar Arguments ..
  175. REAL AK, BK, BNRM, ERR, SOLNRM, TOL
  176. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, N, NELT
  177. C .. Array Arguments ..
  178. REAL A(NELT), B(N), DZ(N), P(N), PP(N), R(N), RR(N), RWORK(*),
  179. + X(N), Z(N), ZZ(N)
  180. INTEGER IA(NELT), IWORK(*), JA(NELT)
  181. C .. Subroutine Arguments ..
  182. EXTERNAL MSOLVE
  183. C .. Arrays in Common ..
  184. REAL SOLN(1)
  185. C .. Local Scalars ..
  186. INTEGER I
  187. C .. External Functions ..
  188. REAL R1MACH, SNRM2
  189. EXTERNAL R1MACH, SNRM2
  190. C .. Common blocks ..
  191. COMMON /SSLBLK/ SOLN
  192. C***FIRST EXECUTABLE STATEMENT ISSBCG
  193. ISSBCG = 0
  194. C
  195. IF( ITOL.EQ.1 ) THEN
  196. C err = ||Residual||/||RightHandSide|| (2-Norms).
  197. IF(ITER .EQ. 0) BNRM = SNRM2(N, B, 1)
  198. ERR = SNRM2(N, R, 1)/BNRM
  199. ELSE IF( ITOL.EQ.2 ) THEN
  200. C -1 -1
  201. C err = ||M Residual||/||M RightHandSide|| (2-Norms).
  202. IF(ITER .EQ. 0) THEN
  203. CALL MSOLVE(N, B, DZ, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  204. BNRM = SNRM2(N, DZ, 1)
  205. ENDIF
  206. ERR = SNRM2(N, Z, 1)/BNRM
  207. ELSE IF( ITOL.EQ.11 ) THEN
  208. C err = ||x-TrueSolution||/||TrueSolution|| (2-Norms).
  209. IF(ITER .EQ. 0) SOLNRM = SNRM2(N, SOLN, 1)
  210. DO 10 I = 1, N
  211. DZ(I) = X(I) - SOLN(I)
  212. 10 CONTINUE
  213. ERR = SNRM2(N, DZ, 1)/SOLNRM
  214. ELSE
  215. C
  216. C If we get here ITOL is not one of the acceptable values.
  217. ERR = R1MACH(2)
  218. IERR = 3
  219. ENDIF
  220. C
  221. IF(IUNIT .NE. 0) THEN
  222. IF( ITER.EQ.0 ) THEN
  223. WRITE(IUNIT,1000) N, ITOL
  224. WRITE(IUNIT,1010) ITER, ERR
  225. ELSE
  226. WRITE(IUNIT,1010) ITER, ERR, AK, BK
  227. ENDIF
  228. ENDIF
  229. IF(ERR .LE. TOL) ISSBCG = 1
  230. C
  231. RETURN
  232. 1000 FORMAT(' Preconditioned BiConjugate Gradient for N, ITOL = ',
  233. $ I5,I5,/' ITER',' Error Estimate',' Alpha',
  234. $ ' Beta')
  235. 1010 FORMAT(1X,I4,1X,E16.7,1X,E16.7,1X,E16.7)
  236. C------------- LAST LINE OF ISSBCG FOLLOWS ----------------------------
  237. END