isdcg.f 10.0 KB

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