hstcrt.f 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. *DECK HSTCRT
  2. SUBROUTINE HSTCRT (A, B, M, MBDCND, BDA, BDB, C, D, N, NBDCND,
  3. + BDC, BDD, ELMBDA, F, IDIMF, PERTRB, IERROR, W)
  4. C***BEGIN PROLOGUE HSTCRT
  5. C***PURPOSE Solve the standard five-point finite difference
  6. C approximation on a staggered grid to the Helmholtz equation
  7. C in Cartesian coordinates.
  8. C***LIBRARY SLATEC (FISHPACK)
  9. C***CATEGORY I2B1A1A
  10. C***TYPE SINGLE PRECISION (HSTCRT-S)
  11. C***KEYWORDS ELLIPTIC, FISHPACK, HELMHOLTZ, PDE
  12. C***AUTHOR Adams, J., (NCAR)
  13. C Swarztrauber, P. N., (NCAR)
  14. C Sweet, R., (NCAR)
  15. C***DESCRIPTION
  16. C
  17. C HSTCRT solves the standard five-point finite difference
  18. C approximation on a staggered grid to the Helmholtz equation in
  19. C Cartesian coordinates
  20. C
  21. C (d/dX)(dU/dX) + (d/dY)(dU/dY) + LAMBDA*U = F(X,Y)
  22. C
  23. C * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  24. C
  25. C * * * * * * * * Parameter Description * * * * * * * * * *
  26. C
  27. C * * * * * * On Input * * * * * *
  28. C
  29. C A,B
  30. C The range of X, i.e. A .LE. X .LE. B. A must be less than B.
  31. C
  32. C M
  33. C The number of grid points in the interval (A,B). The grid points
  34. C in the X-direction are given by X(I) = A + (I-0.5)dX for
  35. C I=1,2,...,M where dX =(B-A)/M. M must be greater than 2.
  36. C
  37. C MBDCND
  38. C Indicates the type of boundary conditions at X = A and X = B.
  39. C
  40. C = 0 If the solution is periodic in X,
  41. C U(M+I,J) = U(I,J).
  42. C
  43. C = 1 If the solution is specified at X = A and X = B.
  44. C
  45. C = 2 If the solution is specified at X = A and the derivative
  46. C of the solution with respect to X is specified at X = B.
  47. C
  48. C = 3 If the derivative of the solution with respect to X is
  49. C specified at X = A and X = B.
  50. C
  51. C = 4 If the derivative of the solution with respect to X is
  52. C specified at X = A and the solution is specified at X = B.
  53. C
  54. C BDA
  55. C A one-dimensional array of length N that specifies the boundary
  56. C values (if any) of the solution at X = A. When MBDCND = 1 or 2,
  57. C
  58. C BDA(J) = U(A,Y(J)) , J=1,2,...,N.
  59. C
  60. C When MBDCND = 3 or 4,
  61. C
  62. C BDA(J) = (d/dX)U(A,Y(J)) , J=1,2,...,N.
  63. C
  64. C BDB
  65. C A one-dimensional array of length N that specifies the boundary
  66. C values of the solution at X = B. When MBDCND = 1 or 4
  67. C
  68. C BDB(J) = U(B,Y(J)) , J=1,2,...,N.
  69. C
  70. C When MBDCND = 2 or 3
  71. C
  72. C BDB(J) = (d/dX)U(B,Y(J)) , J=1,2,...,N.
  73. C
  74. C C,D
  75. C The range of Y, i.e. C .LE. Y .LE. D. C must be less
  76. C than D.
  77. C
  78. C N
  79. C The number of unknowns in the interval (C,D). The unknowns in
  80. C the Y-direction are given by Y(J) = C + (J-0.5)DY,
  81. C J=1,2,...,N, where DY = (D-C)/N. N must be greater than 2.
  82. C
  83. C NBDCND
  84. C Indicates the type of boundary conditions at Y = C
  85. C and Y = D.
  86. C
  87. C = 0 If the solution is periodic in Y, i.e.
  88. C U(I,J) = U(I,N+J).
  89. C
  90. C = 1 If the solution is specified at Y = C and Y = D.
  91. C
  92. C = 2 If the solution is specified at Y = C and the derivative
  93. C of the solution with respect to Y is specified at Y = D.
  94. C
  95. C = 3 If the derivative of the solution with respect to Y is
  96. C specified at Y = C and Y = D.
  97. C
  98. C = 4 If the derivative of the solution with respect to Y is
  99. C specified at Y = C and the solution is specified at Y = D.
  100. C
  101. C BDC
  102. C A one dimensional array of length M that specifies the boundary
  103. C values of the solution at Y = C. When NBDCND = 1 or 2,
  104. C
  105. C BDC(I) = U(X(I),C) , I=1,2,...,M.
  106. C
  107. C When NBDCND = 3 or 4,
  108. C
  109. C BDC(I) = (d/dY)U(X(I),C), I=1,2,...,M.
  110. C
  111. C When NBDCND = 0, BDC is a dummy variable.
  112. C
  113. C BDD
  114. C A one-dimensional array of length M that specifies the boundary
  115. C values of the solution at Y = D. When NBDCND = 1 or 4,
  116. C
  117. C BDD(I) = U(X(I),D) , I=1,2,...,M.
  118. C
  119. C When NBDCND = 2 or 3,
  120. C
  121. C BDD(I) = (d/dY)U(X(I),D) , I=1,2,...,M.
  122. C
  123. C When NBDCND = 0, BDD is a dummy variable.
  124. C
  125. C ELMBDA
  126. C The constant LAMBDA in the Helmholtz equation. If LAMBDA is
  127. C greater than 0, a solution may not exist. However, HSTCRT will
  128. C attempt to find a solution.
  129. C
  130. C F
  131. C A two-dimensional array that specifies the values of the right
  132. C side of the Helmholtz equation. For I=1,2,...,M and J=1,2,...,N
  133. C
  134. C F(I,J) = F(X(I),Y(J)) .
  135. C
  136. C F must be dimensioned at least M X N.
  137. C
  138. C IDIMF
  139. C The row (or first) dimension of the array F as it appears in the
  140. C program calling HSTCRT. This parameter is used to specify the
  141. C variable dimension of F. IDIMF must be at least M.
  142. C
  143. C W
  144. C A one-dimensional array that must be provided by the user for
  145. C work space. W may require up to 13M + 4N + M*INT(log2(N))
  146. C locations. The actual number of locations used is computed by
  147. C HSTCRT and is returned in the location W(1).
  148. C
  149. C
  150. C * * * * * * On Output * * * * * *
  151. C
  152. C F
  153. C Contains the solution U(I,J) of the finite difference
  154. C approximation for the grid point (X(I),Y(J)) for
  155. C I=1,2,...,M, J=1,2,...,N.
  156. C
  157. C PERTRB
  158. C If a combination of periodic or derivative boundary conditions is
  159. C specified for a Poisson equation (LAMBDA = 0), a solution may not
  160. C exist. PERTRB is a constant, calculated and subtracted from F,
  161. C which ensures that a solution exists. HSTCRT then computes this
  162. C solution, which is a least squares solution to the original
  163. C approximation. This solution plus any constant is also a
  164. C solution; hence, the solution is not unique. The value of PERTRB
  165. C should be small compared to the right side F. Otherwise, a
  166. C solution is obtained to an essentially different problem. This
  167. C comparison should always be made to insure that a meaningful
  168. C solution has been obtained.
  169. C
  170. C IERROR
  171. C An error flag that indicates invalid input parameters.
  172. C Except for numbers 0 and 6, a solution is not attempted.
  173. C
  174. C = 0 No error
  175. C
  176. C = 1 A .GE. B
  177. C
  178. C = 2 MBDCND .LT. 0 or MBDCND .GT. 4
  179. C
  180. C = 3 C .GE. D
  181. C
  182. C = 4 N .LE. 2
  183. C
  184. C = 5 NBDCND .LT. 0 or NBDCND .GT. 4
  185. C
  186. C = 6 LAMBDA .GT. 0
  187. C
  188. C = 7 IDIMF .LT. M
  189. C
  190. C = 8 M .LE. 2
  191. C
  192. C Since this is the only means of indicating a possibly
  193. C incorrect call to HSTCRT, the user should test IERROR after
  194. C the call.
  195. C
  196. C W
  197. C W(1) contains the required length of W.
  198. C
  199. C *Long Description:
  200. C
  201. C * * * * * * * Program Specifications * * * * * * * * * * * *
  202. C
  203. C Dimension of BDA(N),BDB(N),BDC(M),BDD(M),F(IDIMF,N),
  204. C Arguments W(See argument list)
  205. C
  206. C Latest June 1, 1977
  207. C Revision
  208. C
  209. C Subprograms HSTCRT,POISTG,POSTG2,GENBUN,POISD2,POISN2,POISP2,
  210. C Required COSGEN,MERGE,TRIX,TRI3,PIMACH
  211. C
  212. C Special NONE
  213. C Conditions
  214. C
  215. C Common NONE
  216. C Blocks
  217. C
  218. C I/O NONE
  219. C
  220. C Precision Single
  221. C
  222. C Specialist Roland Sweet
  223. C
  224. C Language FORTRAN
  225. C
  226. C History Written by Roland Sweet at NCAR in January , 1977
  227. C
  228. C Algorithm This subroutine defines the finite-difference
  229. C equations, incorporates boundary data, adjusts the
  230. C right side when the system is singular and calls
  231. C either POISTG or GENBUN which solves the linear
  232. C system of equations.
  233. C
  234. C Space 8131(decimal) = 17703(octal) locations on the
  235. C Required NCAR Control Data 7600
  236. C
  237. C Timing and The execution time T on the NCAR Control Data
  238. C Accuracy 7600 for subroutine HSTCRT is roughly proportional
  239. C to M*N*log2(N). Some typical values are listed in
  240. C the table below.
  241. C The solution process employed results in a loss
  242. C of no more than FOUR significant digits for N and M
  243. C as large as 64. More detailed information about
  244. C accuracy can be found in the documentation for
  245. C subroutine POISTG which is the routine that
  246. C actually solves the finite difference equations.
  247. C
  248. C
  249. C M(=N) MBDCND NBDCND T(MSECS)
  250. C ----- ------ ------ --------
  251. C
  252. C 32 1-4 1-4 56
  253. C 64 1-4 1-4 230
  254. C
  255. C Portability American National Standards Institute Fortran.
  256. C The machine dependent constant PI is defined in
  257. C function PIMACH.
  258. C
  259. C Required COS
  260. C Resident
  261. C Routines
  262. C
  263. C Reference Schumann, U. and R. Sweet,'A Direct Method For
  264. C The Solution Of Poisson's Equation With Neumann
  265. C Boundary Conditions On A Staggered Grid Of
  266. C Arbitrary Size,' J. COMP. PHYS. 20(1976),
  267. C PP. 171-182.
  268. C
  269. C * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  270. C
  271. C***REFERENCES U. Schumann and R. Sweet, A direct method for the
  272. C solution of Poisson's equation with Neumann boundary
  273. C conditions on a staggered grid of arbitrary size,
  274. C Journal of Computational Physics 20, (1976),
  275. C pp. 171-182.
  276. C***ROUTINES CALLED GENBUN, POISTG
  277. C***REVISION HISTORY (YYMMDD)
  278. C 801001 DATE WRITTEN
  279. C 890531 Changed all specific intrinsics to generic. (WRB)
  280. C 890531 REVISION DATE from Version 3.2
  281. C 891214 Prologue converted to Version 4.0 format. (BAB)
  282. C 920501 Reformatted the REFERENCES section. (WRB)
  283. C***END PROLOGUE HSTCRT
  284. C
  285. C
  286. DIMENSION F(IDIMF,*) ,BDA(*) ,BDB(*) ,BDC(*) ,
  287. 1 BDD(*) ,W(*)
  288. C***FIRST EXECUTABLE STATEMENT HSTCRT
  289. IERROR = 0
  290. IF (A .GE. B) IERROR = 1
  291. IF (MBDCND.LT.0 .OR. MBDCND.GT.4) IERROR = 2
  292. IF (C .GE. D) IERROR = 3
  293. IF (N .LE. 2) IERROR = 4
  294. IF (NBDCND.LT.0 .OR. NBDCND.GT.4) IERROR = 5
  295. IF (IDIMF .LT. M) IERROR = 7
  296. IF (M .LE. 2) IERROR = 8
  297. IF (IERROR .NE. 0) RETURN
  298. NPEROD = NBDCND
  299. MPEROD = 0
  300. IF (MBDCND .GT. 0) MPEROD = 1
  301. DELTAX = (B-A)/M
  302. TWDELX = 1./DELTAX
  303. DELXSQ = 2./DELTAX**2
  304. DELTAY = (D-C)/N
  305. TWDELY = 1./DELTAY
  306. DELYSQ = DELTAY**2
  307. TWDYSQ = 2./DELYSQ
  308. NP = NBDCND+1
  309. MP = MBDCND+1
  310. C
  311. C DEFINE THE A,B,C COEFFICIENTS IN W-ARRAY.
  312. C
  313. ID2 = M
  314. ID3 = ID2+M
  315. ID4 = ID3+M
  316. S = (DELTAY/DELTAX)**2
  317. ST2 = 2.*S
  318. DO 101 I=1,M
  319. W(I) = S
  320. J = ID2+I
  321. W(J) = -ST2+ELMBDA*DELYSQ
  322. J = ID3+I
  323. W(J) = S
  324. 101 CONTINUE
  325. C
  326. C ENTER BOUNDARY DATA FOR X-BOUNDARIES.
  327. C
  328. GO TO (111,102,102,104,104),MP
  329. 102 DO 103 J=1,N
  330. F(1,J) = F(1,J)-BDA(J)*DELXSQ
  331. 103 CONTINUE
  332. W(ID2+1) = W(ID2+1)-W(1)
  333. GO TO 106
  334. 104 DO 105 J=1,N
  335. F(1,J) = F(1,J)+BDA(J)*TWDELX
  336. 105 CONTINUE
  337. W(ID2+1) = W(ID2+1)+W(1)
  338. 106 GO TO (111,107,109,109,107),MP
  339. 107 DO 108 J=1,N
  340. F(M,J) = F(M,J)-BDB(J)*DELXSQ
  341. 108 CONTINUE
  342. W(ID3) = W(ID3)-W(1)
  343. GO TO 111
  344. 109 DO 110 J=1,N
  345. F(M,J) = F(M,J)-BDB(J)*TWDELX
  346. 110 CONTINUE
  347. W(ID3) = W(ID3)+W(1)
  348. 111 CONTINUE
  349. C
  350. C ENTER BOUNDARY DATA FOR Y-BOUNDARIES.
  351. C
  352. GO TO (121,112,112,114,114),NP
  353. 112 DO 113 I=1,M
  354. F(I,1) = F(I,1)-BDC(I)*TWDYSQ
  355. 113 CONTINUE
  356. GO TO 116
  357. 114 DO 115 I=1,M
  358. F(I,1) = F(I,1)+BDC(I)*TWDELY
  359. 115 CONTINUE
  360. 116 GO TO (121,117,119,119,117),NP
  361. 117 DO 118 I=1,M
  362. F(I,N) = F(I,N)-BDD(I)*TWDYSQ
  363. 118 CONTINUE
  364. GO TO 121
  365. 119 DO 120 I=1,M
  366. F(I,N) = F(I,N)-BDD(I)*TWDELY
  367. 120 CONTINUE
  368. 121 CONTINUE
  369. DO 123 I=1,M
  370. DO 122 J=1,N
  371. F(I,J) = F(I,J)*DELYSQ
  372. 122 CONTINUE
  373. 123 CONTINUE
  374. IF (MPEROD .EQ. 0) GO TO 124
  375. W(1) = 0.
  376. W(ID4) = 0.
  377. 124 CONTINUE
  378. PERTRB = 0.
  379. IF (ELMBDA) 133,126,125
  380. 125 IERROR = 6
  381. GO TO 133
  382. 126 GO TO (127,133,133,127,133),MP
  383. 127 GO TO (128,133,133,128,133),NP
  384. C
  385. C FOR SINGULAR PROBLEMS MUST ADJUST DATA TO INSURE THAT A SOLUTION
  386. C WILL EXIST.
  387. C
  388. 128 CONTINUE
  389. S = 0.
  390. DO 130 J=1,N
  391. DO 129 I=1,M
  392. S = S+F(I,J)
  393. 129 CONTINUE
  394. 130 CONTINUE
  395. PERTRB = S/(M*N)
  396. DO 132 J=1,N
  397. DO 131 I=1,M
  398. F(I,J) = F(I,J)-PERTRB
  399. 131 CONTINUE
  400. 132 CONTINUE
  401. PERTRB = PERTRB/DELYSQ
  402. C
  403. C SOLVE THE EQUATION.
  404. C
  405. 133 CONTINUE
  406. IF (NPEROD .EQ. 0) GO TO 134
  407. CALL POISTG (NPEROD,N,MPEROD,M,W(1),W(ID2+1),W(ID3+1),IDIMF,F,
  408. 1 IERR1,W(ID4+1))
  409. GO TO 135
  410. 134 CONTINUE
  411. CALL GENBUN (NPEROD,N,MPEROD,M,W(1),W(ID2+1),W(ID3+1),IDIMF,F,
  412. 1 IERR1,W(ID4+1))
  413. 135 CONTINUE
  414. W(1) = W(ID4+1)+3*M
  415. RETURN
  416. END