isdcgs.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. *DECK ISDCGS
  2. INTEGER FUNCTION ISDCGS (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 ISDCGS
  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 DOUBLE 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 DOUBLE PRECISION B(N), X(N), A(N), TOL, ERR, R(N), R0(N), P(N)
  29. C DOUBLE PRECISION Q(N), U(N), V1(N), V2(N)
  30. C DOUBLE PRECISION RWORK(USER DEFINED), AK, BK, BNRM, SOLNRM
  31. C EXTERNAL MATVEC, MSOLVE
  32. C
  33. C IF( ISDCGS(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 Double Precision B(N).
  42. C Right-hand side vector.
  43. C X :INOUT Double Precision 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 Double Precision 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 DCGS 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 double precision array that can be used to pass
  79. C necessary preconditioning information and/or workspace to
  80. C MSOLVE.
  81. C IWORK is an integer work array for the same purpose as RWORK.
  82. C ITOL :IN Integer.
  83. C Flag to indicate type of convergence criterion.
  84. C If ITOL=1, iteration stops when the 2-norm of the residual
  85. C divided by the 2-norm of the right-hand side is less than TOL.
  86. C This routine must calculate the residual from R = A*X - B.
  87. C This is unnatural and hence expensive for this type of iter-
  88. C ative method. ITOL=2 is *STRONGLY* recommended.
  89. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  90. C residual divided by the 2-norm of M-inv times the right hand
  91. C side is less than TOL, where M-inv time a vector is the pre-
  92. C conditioning step. This is the *NATURAL* stopping for this
  93. C iterative method and is *STRONGLY* recommended.
  94. C ITOL=11 is often useful for checking and comparing different
  95. C routines. For this case, the user must supply the "exact"
  96. C solution or a very accurate approximation (one with an error
  97. C much less than TOL) through a common block,
  98. C COMMON /DSLBLK/ SOLN( )
  99. C If ITOL=11, iteration stops when the 2-norm of the difference
  100. C between the iterative approximation and the user-supplied
  101. C solution divided by the 2-norm of the user-supplied solution
  102. C is less than TOL. Note that this requires the user to set up
  103. C the "COMMON /DSLBLK/ SOLN(LENGTH)" in the calling routine.
  104. C The routine with this declaration should be loaded before the
  105. C stop test so that the correct length is used by the loader.
  106. C This procedure is not standard Fortran and may not work
  107. C correctly on your system (although it has worked on every
  108. C system the authors have tried). If ITOL is not 11 then this
  109. C common block is indeed standard Fortran.
  110. C TOL :IN Double Precision.
  111. C Convergence criterion, as described above.
  112. C ITMAX :IN Integer.
  113. C Maximum number of iterations.
  114. C ITER :IN Integer.
  115. C Current iteration count. (Must be zero on first call.)
  116. C ITMAX iterations.
  117. C ERR :OUT Double Precision.
  118. C Error estimate of error in final approximate solution, as
  119. C defined by ITOL.
  120. C IERR :OUT Integer.
  121. C Error flag. IERR is set to 3 if ITOL is not one of the
  122. C acceptable values, see above.
  123. C IUNIT :IN Integer.
  124. C Unit number on which to write the error at each iteration,
  125. C if this is desired for monitoring convergence. If unit
  126. C number is 0, no writing will occur.
  127. C R :IN Double Precision R(N).
  128. C The residual r = b - Ax.
  129. C R0 :WORK Double Precision R0(N).
  130. C P :DUMMY Double Precision P(N).
  131. C Q :DUMMY Double Precision Q(N).
  132. C U :DUMMY Double Precision U(N).
  133. C V1 :DUMMY Double Precision V1(N).
  134. C Double Precision arrays used for workspace.
  135. C V2 :WORK Double Precision V2(N).
  136. C If ITOL.eq.1 then V2 is used to hold A * X - B on every call.
  137. C If ITOL.eq.2 then V2 is used to hold M-inv * B on the first
  138. C call.
  139. C If ITOL.eq.11 then V2 is used to X - SOLN.
  140. C RWORK :WORK Double Precision RWORK(USER DEFINED).
  141. C Double Precision array that can be used for workspace in
  142. C MSOLVE.
  143. C IWORK :WORK Integer IWORK(USER DEFINED).
  144. C Integer array that can be used for workspace in MSOLVE.
  145. C AK :IN Double Precision.
  146. C Current iterate BiConjugate Gradient iteration parameter.
  147. C BK :IN Double Precision.
  148. C Current iterate BiConjugate Gradient iteration parameter.
  149. C BNRM :INOUT Double Precision.
  150. C Norm of the right hand side. Type of norm depends on ITOL.
  151. C Calculated only on the first call.
  152. C SOLNRM :INOUT Double Precision.
  153. C 2-Norm of the true solution, SOLN. Only computed and used
  154. C if ITOL = 11.
  155. C
  156. C *Function Return Values:
  157. C 0 : Error estimate (determined by ITOL) is *NOT* less than the
  158. C specified tolerance, TOL. The iteration must continue.
  159. C 1 : Error estimate (determined by ITOL) is less than the
  160. C specified tolerance, TOL. The iteration can be considered
  161. C complete.
  162. C
  163. C *Cautions:
  164. C This routine will attempt to write to the Fortran logical output
  165. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  166. C this logical unit is attached to a file or terminal before calling
  167. C this routine with a non-zero value for IUNIT. This routine does
  168. C not check for the validity of a non-zero IUNIT unit number.
  169. C
  170. C***SEE ALSO DCGS
  171. C***ROUTINES CALLED D1MACH, DNRM2
  172. C***COMMON BLOCKS DSLBLK
  173. C***REVISION HISTORY (YYMMDD)
  174. C 890404 DATE WRITTEN
  175. C 890404 Previous REVISION DATE
  176. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  177. C 890922 Numerous changes to prologue to make closer to SLATEC
  178. C standard. (FNF)
  179. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  180. C 891003 Removed C***REFER TO line, per MKS.
  181. C 910411 Prologue converted to Version 4.0 format. (BAB)
  182. C 910502 Removed MATVEC and MSOLVE from ROUTINES CALLED list. (FNF)
  183. C 910506 Made subsidiary to DCGS. (FNF)
  184. C 920407 COMMON BLOCK renamed DSLBLK. (WRB)
  185. C 920511 Added complete declaration section. (WRB)
  186. C 920930 Corrected to not print AK,BK when ITER=0. (FNF)
  187. C 921026 Changed 1.0E10 to D1MACH(2) and corrected D to E in
  188. C output format. (FNF)
  189. C 921113 Corrected C***CATEGORY line. (FNF)
  190. C***END PROLOGUE ISDCGS
  191. C .. Scalar Arguments ..
  192. DOUBLE PRECISION AK, BK, BNRM, ERR, SOLNRM, TOL
  193. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, N, NELT
  194. C .. Array Arguments ..
  195. DOUBLE PRECISION A(NELT), B(N), P(N), Q(N), R(N), R0(N), RWORK(*),
  196. + U(N), V1(N), V2(N), X(N)
  197. INTEGER IA(NELT), IWORK(*), JA(NELT)
  198. C .. Subroutine Arguments ..
  199. EXTERNAL MATVEC, MSOLVE
  200. C .. Arrays in Common ..
  201. DOUBLE PRECISION SOLN(1)
  202. C .. Local Scalars ..
  203. INTEGER I
  204. C .. External Functions ..
  205. DOUBLE PRECISION D1MACH, DNRM2
  206. EXTERNAL D1MACH, DNRM2
  207. C .. Common blocks ..
  208. COMMON /DSLBLK/ SOLN
  209. C***FIRST EXECUTABLE STATEMENT ISDCGS
  210. ISDCGS = 0
  211. C
  212. IF( ITOL.EQ.1 ) THEN
  213. C err = ||Residual||/||RightHandSide|| (2-Norms).
  214. IF(ITER .EQ. 0) BNRM = DNRM2(N, B, 1)
  215. CALL MATVEC(N, X, V2, NELT, IA, JA, A, ISYM )
  216. DO 5 I = 1, N
  217. V2(I) = V2(I) - B(I)
  218. 5 CONTINUE
  219. ERR = DNRM2(N, V2, 1)/BNRM
  220. ELSE IF( ITOL.EQ.2 ) THEN
  221. C -1 -1
  222. C err = ||M Residual||/||M RightHandSide|| (2-Norms).
  223. IF(ITER .EQ. 0) THEN
  224. CALL MSOLVE(N, B, V2, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  225. BNRM = DNRM2(N, V2, 1)
  226. ENDIF
  227. ERR = DNRM2(N, R, 1)/BNRM
  228. ELSE IF( ITOL.EQ.11 ) THEN
  229. C err = ||x-TrueSolution||/||TrueSolution|| (2-Norms).
  230. IF(ITER .EQ. 0) SOLNRM = DNRM2(N, SOLN, 1)
  231. DO 10 I = 1, N
  232. V2(I) = X(I) - SOLN(I)
  233. 10 CONTINUE
  234. ERR = DNRM2(N, V2, 1)/SOLNRM
  235. ELSE
  236. C
  237. C If we get here ITOL is not one of the acceptable values.
  238. ERR = D1MACH(2)
  239. IERR = 3
  240. ENDIF
  241. C
  242. C Print the error and Coefficients AK, BK on each step,
  243. C if desired.
  244. IF(IUNIT .NE. 0) THEN
  245. IF( ITER.EQ.0 ) THEN
  246. WRITE(IUNIT,1000) N, ITOL
  247. WRITE(IUNIT,1010) ITER, ERR
  248. ELSE
  249. WRITE(IUNIT,1010) ITER, ERR, AK, BK
  250. ENDIF
  251. ENDIF
  252. IF(ERR .LE. TOL) ISDCGS = 1
  253. C
  254. RETURN
  255. 1000 FORMAT(' Preconditioned BiConjugate Gradient Squared for ',
  256. $ 'N, ITOL = ',I5, I5,
  257. $ /' ITER',' Error Estimate',' Alpha',
  258. $ ' Beta')
  259. 1010 FORMAT(1X,I4,1X,D16.7,1X,D16.7,1X,D16.7)
  260. C------------- LAST LINE OF ISDCGS FOLLOWS ----------------------------
  261. END