ssjac.f 12 KB

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