sslucn.f 14 KB

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