genbun.f 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. *DECK GENBUN
  2. SUBROUTINE GENBUN (NPEROD, N, MPEROD, M, A, B, C, IDIMY, Y,
  3. + IERROR, W)
  4. C***BEGIN PROLOGUE GENBUN
  5. C***PURPOSE Solve by a cyclic reduction algorithm the linear system
  6. C of equations that results from a finite difference
  7. C approximation to certain 2-d elliptic PDE's on a centered
  8. C grid .
  9. C***LIBRARY SLATEC (FISHPACK)
  10. C***CATEGORY I2B4B
  11. C***TYPE SINGLE PRECISION (GENBUN-S, CMGNBN-C)
  12. C***KEYWORDS ELLIPTIC, FISHPACK, PDE, TRIDIAGONAL
  13. C***AUTHOR Adams, J., (NCAR)
  14. C Swarztrauber, P. N., (NCAR)
  15. C Sweet, R., (NCAR)
  16. C***DESCRIPTION
  17. C
  18. C Subroutine GENBUN solves the linear system of equations
  19. C
  20. C A(I)*X(I-1,J) + B(I)*X(I,J) + C(I)*X(I+1,J)
  21. C
  22. C + X(I,J-1) - 2.*X(I,J) + X(I,J+1) = Y(I,J)
  23. C
  24. C for I = 1,2,...,M and J = 1,2,...,N.
  25. C
  26. C The indices I+1 and I-1 are evaluated modulo M, i.e.,
  27. C X(0,J) = X(M,J) and X(M+1,J) = X(1,J), and X(I,0) may be equal to
  28. C 0, X(I,2), or X(I,N) and X(I,N+1) may be equal to 0, X(I,N-1), or
  29. C X(I,1) depending on an input parameter.
  30. C
  31. C
  32. C * * * * * * * * Parameter Description * * * * * * * * * *
  33. C
  34. C * * * * * * On Input * * * * * *
  35. C
  36. C NPEROD
  37. C Indicates the values that X(I,0) and X(I,N+1) are assumed to
  38. C have.
  39. C
  40. C = 0 If X(I,0) = X(I,N) and X(I,N+1) = X(I,1).
  41. C = 1 If X(I,0) = X(I,N+1) = 0 .
  42. C = 2 If X(I,0) = 0 and X(I,N+1) = X(I,N-1).
  43. C = 3 If X(I,0) = X(I,2) and X(I,N+1) = X(I,N-1).
  44. C = 4 If X(I,0) = X(I,2) and X(I,N+1) = 0.
  45. C
  46. C N
  47. C The number of unknowns in the J-direction. N must be greater
  48. C than 2.
  49. C
  50. C MPEROD
  51. C = 0 if A(1) and C(M) are not zero.
  52. C = 1 if A(1) = C(M) = 0.
  53. C
  54. C M
  55. C The number of unknowns in the I-direction. M must be greater
  56. C than 2.
  57. C
  58. C A,B,C
  59. C One-dimensional arrays of length M that specify the
  60. C coefficients in the linear equations given above. If MPEROD = 0
  61. C the array elements must not depend upon the index I, but must be
  62. C constant. Specifically, the subroutine checks the following
  63. C condition
  64. C
  65. C A(I) = C(1)
  66. C C(I) = C(1)
  67. C B(I) = B(1)
  68. C
  69. C for I=1,2,...,M.
  70. C
  71. C IDIMY
  72. C The row (or first) dimension of the two-dimensional array Y as
  73. C it appears in the program calling GENBUN. This parameter is
  74. C used to specify the variable dimension of Y. IDIMY must be at
  75. C least M.
  76. C
  77. C Y
  78. C A two-dimensional array that specifies the values of the right
  79. C side of the linear system of equations given above. Y must be
  80. C dimensioned at least M*N.
  81. C
  82. C W
  83. C A one-dimensional array that must be provided by the user for
  84. C work space. W may require up to 4*N + (10 + INT(log2(N)))*M
  85. C locations. The actual number of locations used is computed by
  86. C GENBUN and is returned in location W(1).
  87. C
  88. C
  89. C * * * * * * On Output * * * * * *
  90. C
  91. C Y
  92. C Contains the solution X.
  93. C
  94. C IERROR
  95. C An error flag that indicates invalid input parameters. Except
  96. C for number zero, a solution is not attempted.
  97. C
  98. C = 0 No error.
  99. C = 1 M .LE. 2
  100. C = 2 N .LE. 2
  101. C = 3 IDIMY .LT. M
  102. C = 4 NPEROD .LT. 0 or NPEROD .GT. 4
  103. C = 5 MPEROD .LT. 0 or MPEROD .GT. 1
  104. C = 6 A(I) .NE. C(1) or C(I) .NE. C(1) or B(I) .NE. B(1) for
  105. C some I=1,2,...,M.
  106. C = 7 A(1) .NE. 0 or C(M) .NE. 0 and MPEROD = 1
  107. C
  108. C W
  109. C W(1) contains the required length of W.
  110. C
  111. C *Long Description:
  112. C
  113. C * * * * * * * Program Specifications * * * * * * * * * * * *
  114. C
  115. C Dimension of A(M),B(M),C(M),Y(IDIMY,N),W(see parameter list)
  116. C Arguments
  117. C
  118. C Latest June 1, 1976
  119. C Revision
  120. C
  121. C Subprograms GENBUN,POISD2,POISN2,POISP2,COSGEN,MERGE,TRIX,TRI3,
  122. C Required PIMACH
  123. C
  124. C Special NONE
  125. C Conditions
  126. C
  127. C Common NONE
  128. C Blocks
  129. C
  130. C I/O NONE
  131. C
  132. C Precision Single
  133. C
  134. C Specialist Roland Sweet
  135. C
  136. C Language FORTRAN
  137. C
  138. C History Standardized April 1, 1973
  139. C Revised August 20,1973
  140. C Revised January 1, 1976
  141. C
  142. C Algorithm The linear system is solved by a cyclic reduction
  143. C algorithm described in the reference.
  144. C
  145. C Space 4944(decimal) = 11520(octal) locations on the NCAR
  146. C Required Control Data 7600.
  147. C
  148. C Timing and The execution time T on the NCAR Control Data
  149. C Accuracy 7600 for subroutine GENBUN is roughly proportional
  150. C to M*N*log2(N), but also depends on the input
  151. C parameter NPEROD. Some typical values are listed
  152. C in the table below. More comprehensive timing
  153. C charts may be found in the reference.
  154. C To measure the accuracy of the algorithm a
  155. C uniform random number generator was used to create
  156. C a solution array X for the system given in the
  157. C 'PURPOSE' with
  158. C
  159. C A(I) = C(I) = -0.5*B(I) = 1, I=1,2,...,M
  160. C
  161. C and, when MPEROD = 1
  162. C
  163. C A(1) = C(M) = 0
  164. C A(M) = C(1) = 2.
  165. C
  166. C The solution X was substituted into the given sys-
  167. C tem and, using double precision, a right side Y was
  168. C computed. Using this array Y subroutine GENBUN was
  169. C called to produce an approximate solution Z. Then
  170. C the relative error, defined as
  171. C
  172. C E = MAX(ABS(Z(I,J)-X(I,J)))/MAX(ABS(X(I,J)))
  173. C
  174. C where the two maxima are taken over all I=1,2,...,M
  175. C and J=1,2,...,N, was computed. The value of E is
  176. C given in the table below for some typical values of
  177. C M and N.
  178. C
  179. C
  180. C M (=N) MPEROD NPEROD T(MSECS) E
  181. C ------ ------ ------ -------- ------
  182. C
  183. C 31 0 0 36 6.E-14
  184. C 31 1 1 21 4.E-13
  185. C 31 1 3 41 3.E-13
  186. C 32 0 0 29 9.E-14
  187. C 32 1 1 32 3.E-13
  188. C 32 1 3 48 1.E-13
  189. C 33 0 0 36 9.E-14
  190. C 33 1 1 30 4.E-13
  191. C 33 1 3 34 1.E-13
  192. C 63 0 0 150 1.E-13
  193. C 63 1 1 91 1.E-12
  194. C 63 1 3 173 2.E-13
  195. C 64 0 0 122 1.E-13
  196. C 64 1 1 128 1.E-12
  197. C 64 1 3 199 6.E-13
  198. C 65 0 0 143 2.E-13
  199. C 65 1 1 120 1.E-12
  200. C 65 1 3 138 4.E-13
  201. C
  202. C Portability American National Standards Institute Fortran.
  203. C The machine dependent constant PI is defined in
  204. C function PIMACH.
  205. C
  206. C Required COS
  207. C Resident
  208. C Routines
  209. C
  210. C Reference Sweet, R., 'A Cyclic Reduction Algorithm For
  211. C Solving Block Tridiagonal Systems Of Arbitrary
  212. C Dimensions,' SIAM J. on Numer. Anal.,
  213. C 14(Sept., 1977), PP. 706-720.
  214. C
  215. C * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  216. C
  217. C***REFERENCES R. Sweet, A cyclic reduction algorithm for solving
  218. C block tridiagonal systems of arbitrary dimensions,
  219. C SIAM Journal on Numerical Analysis 14, (September
  220. C 1977), pp. 706-720.
  221. C***ROUTINES CALLED POISD2, POISN2, POISP2
  222. C***REVISION HISTORY (YYMMDD)
  223. C 801001 DATE WRITTEN
  224. C 861211 REVISION DATE from Version 3.2
  225. C 891214 Prologue converted to Version 4.0 format. (BAB)
  226. C 920501 Reformatted the REFERENCES section. (WRB)
  227. C***END PROLOGUE GENBUN
  228. C
  229. C
  230. DIMENSION Y(IDIMY,*)
  231. DIMENSION W(*) ,B(*) ,A(*) ,C(*)
  232. C***FIRST EXECUTABLE STATEMENT GENBUN
  233. IERROR = 0
  234. IF (M .LE. 2) IERROR = 1
  235. IF (N .LE. 2) IERROR = 2
  236. IF (IDIMY .LT. M) IERROR = 3
  237. IF (NPEROD.LT.0 .OR. NPEROD.GT.4) IERROR = 4
  238. IF (MPEROD.LT.0 .OR. MPEROD.GT.1) IERROR = 5
  239. IF (MPEROD .EQ. 1) GO TO 102
  240. DO 101 I=2,M
  241. IF (A(I) .NE. C(1)) GO TO 103
  242. IF (C(I) .NE. C(1)) GO TO 103
  243. IF (B(I) .NE. B(1)) GO TO 103
  244. 101 CONTINUE
  245. GO TO 104
  246. 102 IF (A(1).NE.0. .OR. C(M).NE.0.) IERROR = 7
  247. GO TO 104
  248. 103 IERROR = 6
  249. 104 IF (IERROR .NE. 0) RETURN
  250. MP1 = M+1
  251. IWBA = MP1
  252. IWBB = IWBA+M
  253. IWBC = IWBB+M
  254. IWB2 = IWBC+M
  255. IWB3 = IWB2+M
  256. IWW1 = IWB3+M
  257. IWW2 = IWW1+M
  258. IWW3 = IWW2+M
  259. IWD = IWW3+M
  260. IWTCOS = IWD+M
  261. IWP = IWTCOS+4*N
  262. DO 106 I=1,M
  263. K = IWBA+I-1
  264. W(K) = -A(I)
  265. K = IWBC+I-1
  266. W(K) = -C(I)
  267. K = IWBB+I-1
  268. W(K) = 2.-B(I)
  269. DO 105 J=1,N
  270. Y(I,J) = -Y(I,J)
  271. 105 CONTINUE
  272. 106 CONTINUE
  273. MP = MPEROD+1
  274. NP = NPEROD+1
  275. GO TO (114,107),MP
  276. 107 GO TO (108,109,110,111,123),NP
  277. 108 CALL POISP2 (M,N,W(IWBA),W(IWBB),W(IWBC),Y,IDIMY,W,W(IWB2),
  278. 1 W(IWB3),W(IWW1),W(IWW2),W(IWW3),W(IWD),W(IWTCOS),
  279. 2 W(IWP))
  280. GO TO 112
  281. 109 CALL POISD2 (M,N,1,W(IWBA),W(IWBB),W(IWBC),Y,IDIMY,W,W(IWW1),
  282. 1 W(IWD),W(IWTCOS),W(IWP))
  283. GO TO 112
  284. 110 CALL POISN2 (M,N,1,2,W(IWBA),W(IWBB),W(IWBC),Y,IDIMY,W,W(IWB2),
  285. 1 W(IWB3),W(IWW1),W(IWW2),W(IWW3),W(IWD),W(IWTCOS),
  286. 2 W(IWP))
  287. GO TO 112
  288. 111 CALL POISN2 (M,N,1,1,W(IWBA),W(IWBB),W(IWBC),Y,IDIMY,W,W(IWB2),
  289. 1 W(IWB3),W(IWW1),W(IWW2),W(IWW3),W(IWD),W(IWTCOS),
  290. 2 W(IWP))
  291. 112 IPSTOR = W(IWW1)
  292. IREV = 2
  293. IF (NPEROD .EQ. 4) GO TO 124
  294. 113 GO TO (127,133),MP
  295. 114 CONTINUE
  296. C
  297. C REORDER UNKNOWNS WHEN MP =0
  298. C
  299. MH = (M+1)/2
  300. MHM1 = MH-1
  301. MODD = 1
  302. IF (MH*2 .EQ. M) MODD = 2
  303. DO 119 J=1,N
  304. DO 115 I=1,MHM1
  305. MHPI = MH+I
  306. MHMI = MH-I
  307. W(I) = Y(MHMI,J)-Y(MHPI,J)
  308. W(MHPI) = Y(MHMI,J)+Y(MHPI,J)
  309. 115 CONTINUE
  310. W(MH) = 2.*Y(MH,J)
  311. GO TO (117,116),MODD
  312. 116 W(M) = 2.*Y(M,J)
  313. 117 CONTINUE
  314. DO 118 I=1,M
  315. Y(I,J) = W(I)
  316. 118 CONTINUE
  317. 119 CONTINUE
  318. K = IWBC+MHM1-1
  319. I = IWBA+MHM1
  320. W(K) = 0.
  321. W(I) = 0.
  322. W(K+1) = 2.*W(K+1)
  323. GO TO (120,121),MODD
  324. 120 CONTINUE
  325. K = IWBB+MHM1-1
  326. W(K) = W(K)-W(I-1)
  327. W(IWBC-1) = W(IWBC-1)+W(IWBB-1)
  328. GO TO 122
  329. 121 W(IWBB-1) = W(K+1)
  330. 122 CONTINUE
  331. GO TO 107
  332. C
  333. C REVERSE COLUMNS WHEN NPEROD = 4.
  334. C
  335. 123 IREV = 1
  336. NBY2 = N/2
  337. 124 DO 126 J=1,NBY2
  338. MSKIP = N+1-J
  339. DO 125 I=1,M
  340. A1 = Y(I,J)
  341. Y(I,J) = Y(I,MSKIP)
  342. Y(I,MSKIP) = A1
  343. 125 CONTINUE
  344. 126 CONTINUE
  345. GO TO (110,113),IREV
  346. 127 CONTINUE
  347. DO 132 J=1,N
  348. DO 128 I=1,MHM1
  349. MHMI = MH-I
  350. MHPI = MH+I
  351. W(MHMI) = .5*(Y(MHPI,J)+Y(I,J))
  352. W(MHPI) = .5*(Y(MHPI,J)-Y(I,J))
  353. 128 CONTINUE
  354. W(MH) = .5*Y(MH,J)
  355. GO TO (130,129),MODD
  356. 129 W(M) = .5*Y(M,J)
  357. 130 CONTINUE
  358. DO 131 I=1,M
  359. Y(I,J) = W(I)
  360. 131 CONTINUE
  361. 132 CONTINUE
  362. 133 CONTINUE
  363. C
  364. C RETURN STORAGE REQUIREMENTS FOR W ARRAY.
  365. C
  366. W(1) = IPSTOR+IWP-1
  367. RETURN
  368. END