ssgs.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. *DECK SSGS
  2. SUBROUTINE SSGS (N, B, X, NELT, IA, JA, A, ISYM, ITOL, TOL, ITMAX,
  3. + ITER, ERR, IERR, IUNIT, RWORK, LENW, IWORK, LENIW)
  4. C***BEGIN PROLOGUE SSGS
  5. C***PURPOSE Gauss-Seidel Method Iterative Sparse Ax = b Solver.
  6. C Routine to solve a general linear system Ax = b using
  7. C Gauss-Seidel iteration.
  8. C***LIBRARY SLATEC (SLAP)
  9. C***CATEGORY D2A4, D2B4
  10. C***TYPE SINGLE PRECISION (SSGS-S, DSGS-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(NL+2*N+1), LENIW
  23. C REAL B(N), X(N), A(NELT), TOL, ERR, RWORK(NL+3*N)
  24. C
  25. C CALL SSGS(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 lower
  49. C 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 >= NL+3*N.
  105. C NL is the number of non-zeros in the lower triangle of the
  106. C matrix (including the diagonal).
  107. C IWORK :WORK Integer IWORK(LENIW).
  108. C Integer array used for workspace.
  109. C Upon return the following locations of IWORK hold information
  110. C which may be of use to the user:
  111. C IWORK(9) Amount of Integer workspace actually used.
  112. C IWORK(10) Amount of Real workspace actually used.
  113. C LENIW :IN Integer.
  114. C Length of the integer workspace, IWORK. LENIW >= NL+N+11.
  115. C NL is the number of non-zeros in the lower triangle of the
  116. C matrix (including the diagonal).
  117. C
  118. C *Description
  119. C The Sparse Linear Algebra Package (SLAP) utilizes two matrix
  120. C data structures: 1) the SLAP Triad format or 2) the SLAP
  121. C Column format. The user can hand this routine either of the
  122. C of these data structures and SLAP will figure out which on
  123. C is being used and act accordingly.
  124. C
  125. C =================== S L A P Triad format ===================
  126. C
  127. C This routine requires that the matrix A be stored in the
  128. C SLAP Triad format. In this format only the non-zeros are
  129. C stored. They may appear in *ANY* order. The user supplies
  130. C three arrays of length NELT, where NELT is the number of
  131. C non-zeros in the matrix: (IA(NELT), JA(NELT), A(NELT)). For
  132. C each non-zero the user puts the row and column index of that
  133. C matrix element in the IA and JA arrays. The value of the
  134. C non-zero matrix element is placed in the corresponding
  135. C location of the A array. This is an extremely easy data
  136. C structure to generate. On the other hand it is not too
  137. C efficient on vector computers for the iterative solution of
  138. C linear systems. Hence, SLAP changes this input data
  139. C structure to the SLAP Column format for the iteration (but
  140. C does not change it back).
  141. C
  142. C Here is an example of the SLAP Triad storage format for a
  143. C 5x5 Matrix. Recall that the entries may appear in any order.
  144. C
  145. C 5x5 Matrix SLAP Triad format for 5x5 matrix on left.
  146. C 1 2 3 4 5 6 7 8 9 10 11
  147. C |11 12 0 0 15| A: 51 12 11 33 15 53 55 22 35 44 21
  148. C |21 22 0 0 0| IA: 5 1 1 3 1 5 5 2 3 4 2
  149. C | 0 0 33 0 35| JA: 1 2 1 3 5 3 5 2 5 4 1
  150. C | 0 0 0 44 0|
  151. C |51 0 53 0 55|
  152. C
  153. C =================== S L A P Column format ==================
  154. C
  155. C This routine requires that the matrix A be stored in the
  156. C SLAP Column format. In this format the non-zeros are stored
  157. C counting down columns (except for the diagonal entry, which
  158. C must appear first in each "column") and are stored in the
  159. C real array A. In other words, for each column in the matrix
  160. C put the diagonal entry in A. Then put in the other non-zero
  161. C elements going down the column (except the diagonal) in
  162. C order. The IA array holds the row index for each non-zero.
  163. C The JA array holds the offsets into the IA, A arrays for the
  164. C beginning of each column. That is, IA(JA(ICOL)),
  165. C A(JA(ICOL)) points to the beginning of the ICOL-th column in
  166. C IA and A. IA(JA(ICOL+1)-1), A(JA(ICOL+1)-1) points to the
  167. C end of the ICOL-th column. Note that we always have
  168. C JA(N+1) = NELT+1, where N is the number of columns in the
  169. C matrix and NELT is the number of non-zeros in the matrix.
  170. C
  171. C Here is an example of the SLAP Column storage format for a
  172. C 5x5 Matrix (in the A and IA arrays '|' denotes the end of a
  173. C column):
  174. C
  175. C 5x5 Matrix SLAP Column format for 5x5 matrix on left.
  176. C 1 2 3 4 5 6 7 8 9 10 11
  177. C |11 12 0 0 15| A: 11 21 51 | 22 12 | 33 53 | 44 | 55 15 35
  178. C |21 22 0 0 0| IA: 1 2 5 | 2 1 | 3 5 | 4 | 5 1 3
  179. C | 0 0 33 0 35| JA: 1 4 6 8 9 12
  180. C | 0 0 0 44 0|
  181. C |51 0 53 0 55|
  182. C
  183. C *Side Effects:
  184. C The SLAP Triad format (IA, JA, A) is modified internally to be
  185. C the SLAP Column format. See above.
  186. C
  187. C *Cautions:
  188. C This routine will attempt to write to the Fortran logical output
  189. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  190. C this logical unit is attached to a file or terminal before calling
  191. C this routine with a non-zero value for IUNIT. This routine does
  192. C not check for the validity of a non-zero IUNIT unit number.
  193. C
  194. C***SEE ALSO SSJAC, SIR
  195. C***REFERENCES (NONE)
  196. C***ROUTINES CALLED SCHKW, SIR, SS2LT, SS2Y, SSLI, SSMV
  197. C***REVISION HISTORY (YYMMDD)
  198. C 871119 DATE WRITTEN
  199. C 881213 Previous REVISION DATE
  200. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  201. C 890921 Removed TeX from comments. (FNF)
  202. C 890922 Numerous changes to prologue to make closer to SLATEC
  203. C standard. (FNF)
  204. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  205. C 910411 Prologue converted to Version 4.0 format. (BAB)
  206. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  207. C 920511 Added complete declaration section. (WRB)
  208. C 921019 Corrected NEL to NL. (FNF)
  209. C***END PROLOGUE SSGS
  210. C .. Parameters ..
  211. INTEGER LOCRB, LOCIB
  212. PARAMETER (LOCRB=1, LOCIB=11)
  213. C .. Scalar Arguments ..
  214. REAL ERR, TOL
  215. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, LENIW, LENW, N, NELT
  216. C .. Array Arguments ..
  217. REAL A(N), B(N), RWORK(*), X(N)
  218. INTEGER IA(NELT), IWORK(10), JA(NELT)
  219. C .. Local Scalars ..
  220. INTEGER ICOL, J, JBGN, JEND, LOCDZ, LOCEL, LOCIEL, LOCIW, LOCJEL,
  221. + LOCR, LOCW, LOCZ, NL
  222. C .. External Subroutines ..
  223. EXTERNAL SCHKW, SIR, SS2LT, SS2Y, SSLI, SSMV
  224. C***FIRST EXECUTABLE STATEMENT SSGS
  225. C
  226. IF( N.LT.1 .OR. NELT.LT.1 ) THEN
  227. IERR = 3
  228. RETURN
  229. ENDIF
  230. C
  231. C Modify the SLAP matrix data structure to YSMP-Column.
  232. CALL SS2Y( N, NELT, IA, JA, A, ISYM )
  233. C
  234. C Count number of elements in lower triangle of the matrix.
  235. IF( ISYM.EQ.0 ) THEN
  236. NL = 0
  237. DO 20 ICOL = 1, N
  238. JBGN = JA(ICOL)
  239. JEND = JA(ICOL+1)-1
  240. DO 10 J = JBGN, JEND
  241. IF( IA(J).GE.ICOL ) NL = NL + 1
  242. 10 CONTINUE
  243. 20 CONTINUE
  244. ELSE
  245. NL = JA(N+1)-1
  246. ENDIF
  247. C
  248. C Set up the work arrays. Then store the lower triangle of
  249. C the matrix.
  250. C
  251. LOCJEL = LOCIB
  252. LOCIEL = LOCJEL + N+1
  253. LOCIW = LOCIEL + NL
  254. C
  255. LOCEL = LOCRB
  256. LOCR = LOCEL + NL
  257. LOCZ = LOCR + N
  258. LOCDZ = LOCZ + N
  259. LOCW = LOCDZ + N
  260. C
  261. C Check the workspace allocations.
  262. CALL SCHKW( 'SSGS', LOCIW, LENIW, LOCW, LENW, IERR, ITER, ERR )
  263. IF( IERR.NE.0 ) RETURN
  264. C
  265. IWORK(1) = NL
  266. IWORK(2) = LOCIEL
  267. IWORK(3) = LOCJEL
  268. IWORK(4) = LOCEL
  269. IWORK(9) = LOCIW
  270. IWORK(10) = LOCW
  271. C
  272. CALL SS2LT( N, NELT, IA, JA, A, ISYM, NL, IWORK(LOCIEL),
  273. $ IWORK(LOCJEL), RWORK(LOCEL) )
  274. C
  275. C Call iterative refinement routine.
  276. CALL SIR(N, B, X, NELT, IA, JA, A, ISYM, SSMV, SSLI,
  277. $ ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, RWORK(LOCR),
  278. $ RWORK(LOCZ), RWORK(LOCDZ), RWORK, IWORK )
  279. C
  280. C Set the amount of Integer and Real Workspace used.
  281. IWORK(9) = LOCIW+N+NELT
  282. IWORK(10) = LOCW+NELT
  283. RETURN
  284. C------------- LAST LINE OF SSGS FOLLOWS ------------------------------
  285. END