ssdbcg.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. *DECK SSDBCG
  2. SUBROUTINE SSDBCG (N, B, X, NELT, IA, JA, A, ISYM, ITOL, TOL,
  3. + ITMAX, ITER, ERR, IERR, IUNIT, RWORK, LENW, IWORK, LENIW)
  4. C***BEGIN PROLOGUE SSDBCG
  5. C***PURPOSE Diagonally Scaled BiConjugate Gradient Sparse Ax=b Solver.
  6. C Routine to solve a linear system Ax = b using the
  7. C BiConjugate Gradient method with diagonal scaling.
  8. C***LIBRARY SLATEC (SLAP)
  9. C***CATEGORY D2A4, D2B4
  10. C***TYPE SINGLE PRECISION (SSDBCG-S, DSDBCG-D)
  11. C***KEYWORDS ITERATIVE PRECONDITION, NON-SYMMETRIC LINEAR SYSTEM, SLAP,
  12. C SPARSE
  13. C***AUTHOR Greenbaum, Anne, (Courant Institute)
  14. C Seager, Mark K., (LLNL)
  15. C Lawrence Livermore National Laboratory
  16. C PO BOX 808, L-60
  17. C Livermore, CA 94550 (510) 423-3141
  18. C seager@llnl.gov
  19. C***DESCRIPTION
  20. C
  21. C *Usage:
  22. C INTEGER N, NELT, IA(NELT), JA(NELT), ISYM, ITOL, ITMAX
  23. C INTEGER ITER, IERR, IUNIT, LENW, IWORK(10), LENIW
  24. C REAL B(N), X(N), A(NELT), TOL, ERR, RWORK(8*N)
  25. C
  26. C CALL SSDBCG(N, B, X, NELT, IA, JA, A, ISYM, ITOL, TOL,
  27. C $ ITMAX, ITER, ERR, IERR, IUNIT, RWORK, LENW, IWORK, LENIW )
  28. C
  29. C *Arguments:
  30. C N :IN Integer
  31. C Order of the Matrix.
  32. C B :IN Real B(N).
  33. C Right-hand side vector.
  34. C X :INOUT Real X(N).
  35. C On input X is your initial guess for solution vector.
  36. C On output X is the final approximate solution.
  37. C NELT :IN Integer.
  38. C Number of Non-Zeros stored in A.
  39. C IA :INOUT Integer IA(NELT).
  40. C JA :INOUT Integer JA(NELT).
  41. C A :INOUT Real A(NELT).
  42. C These arrays should hold the matrix A in either the SLAP
  43. C Triad format or the SLAP Column format. See "Description",
  44. C below. If the SLAP Triad format is chosen it is changed
  45. C internally to the SLAP Column format.
  46. C ISYM :IN Integer.
  47. C Flag to indicate symmetric storage format.
  48. C If ISYM=0, all non-zero entries of the matrix are stored.
  49. C If ISYM=1, the matrix is symmetric, and only the upper
  50. C or lower triangle of the matrix is stored.
  51. C ITOL :IN Integer.
  52. C Flag to indicate type of convergence criterion.
  53. C If ITOL=1, iteration stops when the 2-norm of the residual
  54. C divided by the 2-norm of the right-hand side is less than TOL.
  55. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  56. C residual divided by the 2-norm of M-inv times the right hand
  57. C side is less than TOL, where M-inv is the inverse of the
  58. C diagonal of A.
  59. C ITOL=11 is often useful for checking and comparing different
  60. C routines. For this case, the user must supply the "exact"
  61. C solution or a very accurate approximation (one with an error
  62. C much less than TOL) through a common block,
  63. C COMMON /SSLBLK/ SOLN( )
  64. C If ITOL=11, iteration stops when the 2-norm of the difference
  65. C between the iterative approximation and the user-supplied
  66. C solution divided by the 2-norm of the user-supplied solution
  67. C is less than TOL. Note that this requires the user to set up
  68. C the "COMMON /SSLBLK/ SOLN(LENGTH)" in the calling routine.
  69. C The routine with this declaration should be loaded before the
  70. C stop test so that the correct length is used by the loader.
  71. C This procedure is not standard Fortran and may not work
  72. C correctly on your system (although it has worked on every
  73. C system the authors have tried). If ITOL is not 11 then this
  74. C common block is indeed standard Fortran.
  75. C TOL :INOUT Real.
  76. C Convergence criterion, as described above. (Reset if IERR=4.)
  77. C ITMAX :IN Integer.
  78. C Maximum number of iterations.
  79. C ITER :OUT Integer.
  80. C Number of iterations required to reach convergence, or
  81. C ITMAX+1 if convergence criterion could not be achieved in
  82. C ITMAX iterations.
  83. C ERR :OUT Real.
  84. C Error estimate of error in final approximate solution, as
  85. C defined by ITOL.
  86. C IERR :OUT Integer.
  87. C Return error flag.
  88. C IERR = 0 => All went well.
  89. C IERR = 1 => Insufficient space allocated for WORK or IWORK.
  90. C IERR = 2 => Method failed to converge in ITMAX steps.
  91. C IERR = 3 => Error in user input.
  92. C Check input values of N, ITOL.
  93. C IERR = 4 => User error tolerance set too tight.
  94. C Reset to 500*R1MACH(3). Iteration proceeded.
  95. C IERR = 5 => Preconditioning matrix, M, is not positive
  96. C definite. (r,z) < 0.
  97. C IERR = 6 => Matrix A is not positive definite. (p,Ap) < 0.
  98. C IUNIT :IN Integer.
  99. C Unit number on which to write the error at each iteration,
  100. C if this is desired for monitoring convergence. If unit
  101. C number is 0, no writing will occur.
  102. C RWORK :WORK Real RWORK(LENW).
  103. C Real array used for workspace.
  104. C LENW :IN Integer.
  105. C Length of the real workspace, RWORK. LENW >= 8*N.
  106. C IWORK :WORK Integer IWORK(LENIW).
  107. C Used to hold pointers into the RWORK array.
  108. C LENIW :IN Integer.
  109. C Length of the integer workspace, IWORK. LENIW >= 10.
  110. C Upon return the following locations of IWORK hold information
  111. C which may be of use to the user:
  112. C IWORK(9) Amount of Integer workspace actually used.
  113. C IWORK(10) Amount of Real workspace actually used.
  114. C
  115. C *Description:
  116. C This routine performs preconditioned BiConjugate gradient
  117. C method on the Non-Symmetric positive definite linear system
  118. C Ax=b. The preconditioner is M = DIAG(A), the diagonal of the
  119. C matrix A. This is the simplest of preconditioners and
  120. C vectorizes very well.
  121. C
  122. C The Sparse Linear Algebra Package (SLAP) utilizes two matrix
  123. C data structures: 1) the SLAP Triad format or 2) the SLAP
  124. C Column format. The user can hand this routine either of the
  125. C of these data structures and SLAP will figure out which on
  126. C is being used and act accordingly.
  127. C
  128. C =================== S L A P Triad format ===================
  129. C
  130. C This routine requires that the matrix A be stored in the
  131. C SLAP Triad format. In this format only the non-zeros are
  132. C stored. They may appear in *ANY* order. The user supplies
  133. C three arrays of length NELT, where NELT is the number of
  134. C non-zeros in the matrix: (IA(NELT), JA(NELT), A(NELT)). For
  135. C each non-zero the user puts the row and column index of that
  136. C matrix element in the IA and JA arrays. The value of the
  137. C non-zero matrix element is placed in the corresponding
  138. C location of the A array. This is an extremely easy data
  139. C structure to generate. On the other hand it is not too
  140. C efficient on vector computers for the iterative solution of
  141. C linear systems. Hence, SLAP changes this input data
  142. C structure to the SLAP Column format for the iteration (but
  143. C does not change it back).
  144. C
  145. C Here is an example of the SLAP Triad storage format for a
  146. C 5x5 Matrix. Recall that the entries may appear in any order.
  147. C
  148. C 5x5 Matrix SLAP Triad format for 5x5 matrix on left.
  149. C 1 2 3 4 5 6 7 8 9 10 11
  150. C |11 12 0 0 15| A: 51 12 11 33 15 53 55 22 35 44 21
  151. C |21 22 0 0 0| IA: 5 1 1 3 1 5 5 2 3 4 2
  152. C | 0 0 33 0 35| JA: 1 2 1 3 5 3 5 2 5 4 1
  153. C | 0 0 0 44 0|
  154. C |51 0 53 0 55|
  155. C
  156. C =================== S L A P Column format ==================
  157. C
  158. C This routine requires that the matrix A be stored in the
  159. C SLAP Column format. In this format the non-zeros are stored
  160. C counting down columns (except for the diagonal entry, which
  161. C must appear first in each "column") and are stored in the
  162. C real array A. In other words, for each column in the matrix
  163. C put the diagonal entry in A. Then put in the other non-zero
  164. C elements going down the column (except the diagonal) in
  165. C order. The IA array holds the row index for each non-zero.
  166. C The JA array holds the offsets into the IA, A arrays for the
  167. C beginning of each column. That is, IA(JA(ICOL)),
  168. C A(JA(ICOL)) points to the beginning of the ICOL-th column in
  169. C IA and A. IA(JA(ICOL+1)-1), A(JA(ICOL+1)-1) points to the
  170. C end of the ICOL-th column. Note that we always have
  171. C JA(N+1) = NELT+1, where N is the number of columns in the
  172. C matrix and NELT is the number of non-zeros in the matrix.
  173. C
  174. C Here is an example of the SLAP Column storage format for a
  175. C 5x5 Matrix (in the A and IA arrays '|' denotes the end of a
  176. C column):
  177. C
  178. C 5x5 Matrix SLAP Column format for 5x5 matrix on left.
  179. C 1 2 3 4 5 6 7 8 9 10 11
  180. C |11 12 0 0 15| A: 11 21 51 | 22 12 | 33 53 | 44 | 55 15 35
  181. C |21 22 0 0 0| IA: 1 2 5 | 2 1 | 3 5 | 4 | 5 1 3
  182. C | 0 0 33 0 35| JA: 1 4 6 8 9 12
  183. C | 0 0 0 44 0|
  184. C |51 0 53 0 55|
  185. C
  186. C *Side Effects:
  187. C The SLAP Triad format (IA, JA, A) is modified internally to
  188. C be the SLAP Column format. See above.
  189. C
  190. C *Cautions:
  191. C This routine will attempt to write to the Fortran logical output
  192. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  193. C this logical unit is attached to a file or terminal before calling
  194. C this routine with a non-zero value for IUNIT. This routine does
  195. C not check for the validity of a non-zero IUNIT unit number.
  196. C
  197. C***SEE ALSO SBCG, SLUBCG
  198. C***REFERENCES (NONE)
  199. C***ROUTINES CALLED SBCG, SCHKW, SS2Y, SSDI, SSDS, SSMTV, SSMV
  200. C***REVISION HISTORY (YYMMDD)
  201. C 871119 DATE WRITTEN
  202. C 881213 Previous REVISION DATE
  203. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  204. C 890921 Removed TeX from comments. (FNF)
  205. C 890922 Numerous changes to prologue to make closer to SLATEC
  206. C standard. (FNF)
  207. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  208. C 910411 Prologue converted to Version 4.0 format. (BAB)
  209. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  210. C 920511 Added complete declaration section. (WRB)
  211. C 921113 Corrected C***CATEGORY line. (FNF)
  212. C***END PROLOGUE SSDBCG
  213. C .. Parameters ..
  214. INTEGER LOCRB, LOCIB
  215. PARAMETER (LOCRB=1, LOCIB=11)
  216. C .. Scalar Arguments ..
  217. REAL ERR, TOL
  218. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, LENIW, LENW, N, NELT
  219. C .. Array Arguments ..
  220. REAL A(N), B(N), RWORK(LENW), X(N)
  221. INTEGER IA(NELT), IWORK(LENIW), JA(NELT)
  222. C .. Local Scalars ..
  223. INTEGER LOCDIN, LOCDZ, LOCIW, LOCP, LOCPP, LOCR, LOCRR, LOCW,
  224. + LOCZ, LOCZZ
  225. C .. External Subroutines ..
  226. EXTERNAL SBCG, SCHKW, SS2Y, SSDI, SSDS, SSMTV, SSMV
  227. C***FIRST EXECUTABLE STATEMENT SSDBCG
  228. C
  229. IERR = 0
  230. IF( N.LT.1 .OR. NELT.LT.1 ) THEN
  231. IERR = 3
  232. RETURN
  233. ENDIF
  234. C
  235. C Change the SLAP input matrix IA, JA, A to SLAP-Column format.
  236. CALL SS2Y( N, NELT, IA, JA, A, ISYM )
  237. C
  238. C Set up the workspace.
  239. LOCIW = LOCIB
  240. C
  241. LOCDIN = LOCRB
  242. LOCR = LOCDIN + N
  243. LOCZ = LOCR + N
  244. LOCP = LOCZ + N
  245. LOCRR = LOCP + N
  246. LOCZZ = LOCRR + N
  247. LOCPP = LOCZZ + N
  248. LOCDZ = LOCPP + N
  249. LOCW = LOCDZ + N
  250. C
  251. C Check the workspace allocations.
  252. CALL SCHKW( 'SSDBCG', LOCIW, LENIW, LOCW, LENW, IERR, ITER, ERR )
  253. IF( IERR.NE.0 ) RETURN
  254. C
  255. IWORK(4) = LOCDIN
  256. IWORK(9) = LOCIW
  257. IWORK(10) = LOCW
  258. C
  259. C Compute the inverse of the diagonal of the matrix.
  260. CALL SSDS(N, NELT, IA, JA, A, ISYM, RWORK(LOCDIN))
  261. C
  262. C Perform the Diagonally Scaled BiConjugate gradient algorithm.
  263. CALL SBCG(N, B, X, NELT, IA, JA, A, ISYM, SSMV, SSMTV,
  264. $ SSDI, SSDI, ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT,
  265. $ RWORK(LOCR), RWORK(LOCZ), RWORK(LOCP),
  266. $ RWORK(LOCRR), RWORK(LOCZZ), RWORK(LOCPP),
  267. $ RWORK(LOCDZ), RWORK(1), IWORK(1))
  268. RETURN
  269. C------------- LAST LINE OF SSDBCG FOLLOWS ----------------------------
  270. END