isdcgn.f 12 KB

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