abc_eq_opt.f 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. PROGRAM ABC
  2. IMPLICIT NONE
  3. INTEGER LDA
  4. PARAMETER (LDA=1000)
  5. C Model parameters and functions
  6. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  7. DOUBLE PRECISION T, DT
  8. C Values for DNSQE routine
  9. DOUBLE PRECISION SMCOST(LDA), K(LDA), JAK(LDA,LDA),Z(LDA)
  10. INTEGER N, C, IT, IPVT(LDA), JOB
  11. DOUBLE PRECISION ZERO, ONE, X, SMC, JSMC, B(LDA),RCOND,SC,KK
  12. DOUBLE PRECISION SOMT, SOMK, CST, UC
  13. INTEGER I, J
  14. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  15. COMMON /CENTRAL/ C
  16. DATA L,VF,CAP,AA,BB,GG /5.D0,15.D0,6.D0,10.D0,8.D0,15.D0/
  17. PRINT*, "============================="
  18. PRINT*, "====== OPTIMUM =============="
  19. PRINT*, "============================="
  20. CALL ONEMASS(X,SC)
  21. PRINT '(2F9.3)', X, SC
  22. N = 2
  23. C = 2
  24. K(1) = 0.0D0
  25. K(2) = X
  26. CALL DISPLAY(N,C,K)
  27. DO 10, I = 1, 3
  28. CALL ITERATION(N,C,K,SC)
  29. CALL DISPLAY(N,C,K)
  30. 10 CONTINUE
  31. C KK = 2.205D0
  32. C PRINT*, N
  33. C KK = K(4)
  34. C PRINT*, AA*(KK*DT(KK)+T(KK)) + GG*SOMT(N,K,4,N-1)+
  35. C & GG*DT(KK)*SOMK(N,K,4,N-1)
  36. C PRINT*, C
  37. C PRINT*, SMC(N,C,K,4)
  38. C PRINT*, K(1:4)
  39. PRINT*, "============================="
  40. PRINT*, "====== EQUILIBRIUM =========="
  41. PRINT*, "============================="
  42. CALL EQONEMASS(K,CST,C)
  43. CALL EQDISPLAY(2,C,K)
  44. N = 2
  45. C CALL EQCSOLVE(N,C,K,8.5D0)
  46. CALL EQITERATION(N,C,K,SC)
  47. CALL EQDISPLAY(N,C,K)
  48. PRINT*, "== OK =="
  49. CALL EQITER2(N,C,K,SC)
  50. C PRINT*, SC
  51. C CALL EQCSOLVE(N,C,K,49.13D0)
  52. C CALL EQDISPLAY(N,C,K)
  53. C PRINT*, AA*T(0.D0)+BB*(T(K(1))+T(K(2)) )
  54. CALL EQITERATION(N,C,K,SC)
  55. CALL EQDISPLAY(N,C,K)
  56. CALL EQCSOLVE(N, C, K, 35.0D0)
  57. CALL EQDISPLAY(N,C,K)
  58. CALL EQCSOLVE(N, C, K, 45.0D0)
  59. CALL EQDISPLAY(N,C,K)
  60. CALL EQCSOLVE(N, C, K, 52.075D0)
  61. CALL EQDISPLAY(N,C,K)
  62. CALL EQCSOLVE(N, C, K, 52.0833D0)
  63. CALL EQDISPLAY(N,C,K)
  64. N = N + 1
  65. K(N) = 0.D0
  66. CALL EQCSOLVE(N, C, K, 83.3033D0)
  67. CALL EQDISPLAY(N,C,K)
  68. N = N + 1
  69. K(7) = 0.D0
  70. K(6) = K(5)
  71. K(5) = K(4)
  72. K(4) = K(3)
  73. K(3) = K(2)
  74. K(2) = K(1)
  75. K(1) = 0.D0
  76. C = C + 1
  77. CALL EQDISPLAY(N,C,K)
  78. CALL EQCSOLVE(N, C, K, 100.6833D0)
  79. CALL EQDISPLAY(N,C,K)
  80. C PRINT*, UC(N,C,K,5)-UC(N,C,K,4)
  81. C CALL EQITERATION(N,C,K,SC)
  82. C CALL EQITER2(N,C,K,SC)
  83. C CALL EQDISPLAY(N,C,K)
  84. C PRINT*, "UC ---------> ", SC
  85. C CALL EQITER2(N,C,K,SC)
  86. C PRINT*, "UC ---------> ", SC
  87. C CALL EQDISPLAY(N,C,K)
  88. C N = N + 1
  89. C K(N) = 0.0D0
  90. C CALL EQCSOLVE(N, C, K, 55.075D0)
  91. C CALL EQDISPLAY(N,C,K)
  92. C CALL EQCSOLVE(N, C, K, 85.075D0)
  93. C CALL EQDISPLAY(N,C,K)
  94. C CALL EQCSOLVE(N, C, K, SC)
  95. C PRINT*, AA*T(0.0D0)+GG*(T(K(3))+T(K(4)))
  96. C CALL EQITERATION(N,C,K,SC)
  97. C CALL EQDISPLAY(N,C,K)
  98. END
  99. SUBROUTINE DISPLAY (N,C,K)
  100. IMPLICIT NONE
  101. INTEGER N, C, I
  102. DOUBLE PRECISION K(N), SMC, T
  103. DOUBLE PRECISION SOMK
  104. PRINT*
  105. PRINT '(I5,A1,2F9.3)', 0,' ', 0.00D0 , SMC(N,C,K,0)
  106. DO 10, I = 1, N
  107. IF(I.EQ.C) THEN
  108. PRINT '(I5,A1,3F9.3)', I,'*', K(I), SMC(N,C,K,I), T(K(I))
  109. ELSE
  110. PRINT '(I5,A1,3F9.3)', I,' ', K(I), SMC(N,C,K,I), T(K(I))
  111. ENDIF
  112. 10 CONTINUE
  113. PRINT '(I5,A1,2F9.3)', N+1,' ', 0.00D0 , SMC(N,C,K,N+1)
  114. PRINT*
  115. PRINT '(A12,F9.3)', "Population :", SOMK(N,K,1,N)
  116. RETURN
  117. END
  118. SUBROUTINE EQDISPLAY (N,C,K)
  119. IMPLICIT NONE
  120. INTEGER N, C, I
  121. DOUBLE PRECISION K(N), UC, T
  122. DOUBLE PRECISION SOMK
  123. PRINT*
  124. PRINT '(I5,A1,2F9.3)', 0,' ', 0.00D0 , UC(N,C,K,0)
  125. DO 10, I = 1, N
  126. IF(I.EQ.C) THEN
  127. PRINT '(I5,A1,3F9.3)', I,'*', K(I), UC(N,C,K,I), T(K(I))
  128. ELSE
  129. PRINT '(I5,A1,3F9.3)', I,' ', K(I), UC(N,C,K,I), T(K(I))
  130. ENDIF
  131. 10 CONTINUE
  132. PRINT '(I5,A1,2F9.3)', N+1,' ', 0.00D0 , UC(N,C,K,N+1)
  133. PRINT*
  134. PRINT '(A12,F9.3)', "Population :", SOMK(N,K,1,N)
  135. RETURN
  136. END
  137. SUBROUTINE ITERATION(N,C,K,SC)
  138. IMPLICIT NONE
  139. INTEGER N, C, I
  140. DOUBLE PRECISION SC, VAL, K(N+1), SMC
  141. CALL ITER1(N,C,K,SC)
  142. CALL CSOLVE(N, C, K, SC)
  143. VAL = SMC(N,C,K,N) - SMC(N,C,K,N+1)
  144. IF (VAL.LT.0.0D0) THEN
  145. DO 3, I = N+1, 2, -1
  146. 3 K(I) = K(I-1)
  147. K(1) = 0.0D0
  148. C = C + 1
  149. N = N + 1
  150. ELSE
  151. CALL ITER2(N,C,K,SC)
  152. CALL CSOLVE(N, C, K, SC)
  153. N = N + 1
  154. K(N) = 0.0D0
  155. ENDIF
  156. RETURN
  157. END
  158. SUBROUTINE ITER1(N,C,K,SC)
  159. C Find marginal social cost SC such that mass
  160. C 0 (late arrival) is just to become active
  161. C Newton algorithm is used for this one dimensional problem.
  162. C The jacobian is approximated
  163. C using finite differences.
  164. IMPLICIT NONE
  165. INTEGER N, C, LDA, IT,ITMAX
  166. PARAMETER(LDA=1000)
  167. DOUBLE PRECISION SMC, K(N), H, VAL0,VAL1,VAL2,X,DIFF,SC,TOL
  168. DOUBLE PRECISION TMP
  169. ITMAX = 25
  170. H = 0.01D0
  171. X = SC
  172. IT = 0
  173. TOL = 1.0D-9
  174. 1 CONTINUE
  175. IT = IT + 1
  176. TMP = SMC(N,C,K,0)
  177. CALL CSOLVE(N,C,K,X)
  178. VAL0 = X - SMC(N,C,K,0)
  179. CALL CSOLVE(N,C,K,X+H)
  180. VAL1 = X+H - SMC(N,C,K,0)
  181. CALL CSOLVE(N,C,K,X-H)
  182. VAL2 = X-H - SMC(N,C,K,0)
  183. DIFF = (VAL2-VAL1)/(2.0D0*H)
  184. X = X + VAL0 / DIFF
  185. C PRINT *, X, VAL0, DIFF, IT, K(1:3)
  186. IF(ABS(VAL0).GT.TOL.AND.IT.LT.ITMAX) GOTO 1
  187. SC = X
  188. RETURN
  189. END
  190. SUBROUTINE ITER2(N,C,K,SC)
  191. C Find marginal social cost SC such that mass
  192. C N+1 (late arrival) is just to become active
  193. C Newton algorithm is used for this one dimensional problem.
  194. C The jacobian is approximated
  195. C using finite differences.
  196. IMPLICIT NONE
  197. INTEGER N, C, LDA, IT,ITMAX
  198. PARAMETER(LDA=1000)
  199. DOUBLE PRECISION SMC, K(N), H, VAL0,VAL1,VAL2,X,DIFF,SC,TOL
  200. DOUBLE PRECISION TMP
  201. ITMAX = 25
  202. H = 0.01D0
  203. X = SC
  204. IT = 0
  205. TOL = 1.0D-9
  206. 1 CONTINUE
  207. IT = IT + 1
  208. TMP = SMC(N,C,K,N+1)
  209. CALL CSOLVE(N,C,K,X)
  210. VAL0 = X - SMC(N,C,K,N+1)
  211. CALL CSOLVE(N,C,K,X+H)
  212. VAL1 = X+H - SMC(N,C,K,N+1)
  213. CALL CSOLVE(N,C,K,X-H)
  214. VAL2 = X-H - SMC(N,C,K,N+1)
  215. DIFF = (VAL2-VAL1)/(2.0D0*H)
  216. X = X + VAL0 / DIFF
  217. C PRINT *, X, VAL0, DIFF, IT, K(1:3)
  218. IF(ABS(VAL0).GT.TOL.AND.IT.LT.ITMAX) GOTO 1
  219. SC = X
  220. RETURN
  221. END
  222. SUBROUTINE CSOLVE(N,C,K,SC)
  223. C
  224. C Given N and C find K(1)..K(N) such that
  225. C the marginal social cost is equal to SC;
  226. C The computation is performed with active
  227. C being unchanged.
  228. C
  229. IMPLICIT NONE
  230. INTEGER N, C, LDA, I, J, JOB, IT
  231. PARAMETER (LDA=1000)
  232. DOUBLE PRECISION K(LDA), JAK(LDA,LDA),SMCOST(LDA), TOL
  233. DOUBLE PRECISION B(LDA), RCOND, Z(N), SC, SMC, JSMC, DENORM
  234. INTEGER IPVT(N), ITMAX
  235. ITMAX = 25
  236. TOL = 1.0D-9
  237. IT = 0
  238. 1 CONTINUE
  239. IT = IT + 1
  240. DO 10, I = 1, N
  241. B(I) = SMC(N,C,K,I) - SC
  242. DO 10, J = 1, N
  243. JAK(I,J) = JSMC(N,C,K,I,J)
  244. 10 CONTINUE
  245. JOB = 0
  246. CALL DGECO(JAK,LDA,N,IPVT,RCOND,Z)
  247. CALL DGESL(JAK,LDA,N,IPVT,B,JOB)
  248. DO 20, I = 1, N
  249. K(I) = K(I) - B(I)
  250. C PRINT*, IT, I, K(I), B(I), DENORM(N, B)
  251. 20 CONTINUE
  252. IF(DENORM(N,B).LT.TOL) GOTO 30
  253. IF(IT.LT.ITMAX) GOTO 1
  254. PRINT*, "Maximum number of iterations reached !"
  255. 30 CONTINUE
  256. RETURN
  257. END
  258. SUBROUTINE SMC1(N,C,K,SMCOST)
  259. IMPLICIT NONE
  260. INTEGER N
  261. DOUBLE PRECISION K(N), SMCOST(N+2), T, DT
  262. INTEGER I, J, C
  263. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  264. DOUBLE PRECISION TMP1, TMP2, TMP3, ZERO, SOMT, SOMK
  265. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  266. ZERO = 0.0D0
  267. C MASSES THAT ARRIVE EARLY
  268. DO 10, I = 1, C-1
  269. SMCOST(I) = AA * ( K(I) * DT( K(I) ) + T( K(I)) )
  270. & + BB * SOMT(N,K,I+1,C)
  271. & + BB * DT(K(I)) * SOMK(N,K,1,I-1)
  272. & - K(N)
  273. 10 CONTINUE
  274. C THE MASS THAT ARRIVES ON TIME (MASS C)
  275. SMCOST(C) = AA * ( K(C) * DT( K(C) ) + T( K( C )) )
  276. & + BB * DT(K(C)) * SOMK(N,K,1,C-1)
  277. & - K(N)
  278. C THE MASSES THAT ARRIVE LATE
  279. DO 30, I = C + 1, N
  280. SMCOST(I) = AA * ( K(I) * DT( K(I) ) + T( K(I)) )
  281. & + GG * SOMT(N,K,C+1,I)
  282. & + GG * DT(K(I)) * SOMK(N,K,I,N-1)
  283. & - K(N)
  284. 30 CONTINUE
  285. C THE NEW EARLY MASS
  286. SMCOST(N+1) = AA * T(ZERO) + BB * SOMT(N,K,1,C) - K(N)
  287. C THE NEW LATE MASS
  288. SMCOST(N+2) = AA * T(ZERO) + GG * (SOMT(N,K,C+1,N)+T(ZERO)) - K(N)
  289. RETURN
  290. END
  291. FUNCTION F(K)
  292. IMPLICIT NONE
  293. DOUBLE PRECISION K, F
  294. DOUBLE PRECISION T, DT
  295. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  296. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  297. F = AA * ( K * DT(K) + T(K) - T(0.0D0) ) - BB * T(K)
  298. RETURN
  299. END
  300. FUNCTION T(K)
  301. IMPLICIT NONE
  302. DOUBLE PRECISION K
  303. DOUBLE PRECISION T
  304. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  305. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  306. T = L / ( VF * ( 1.0D0 - K / CAP ) )
  307. RETURN
  308. END
  309. FUNCTION DT(K)
  310. IMPLICIT NONE
  311. DOUBLE PRECISION K
  312. DOUBLE PRECISION DT, T
  313. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  314. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  315. DT = VF / ( CAP * L ) * T ( K ) ** 2
  316. RETURN
  317. END
  318. FUNCTION DDT(K)
  319. IMPLICIT NONE
  320. DOUBLE PRECISION K
  321. DOUBLE PRECISION DDT, DT, T
  322. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  323. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  324. DDT = 2.0D0 * VF / ( CAP * L ) * T ( K ) * DT ( K )
  325. RETURN
  326. END
  327. SUBROUTINE ONEMASS(X, CST)
  328. IMPLICIT NONE
  329. DOUBLE PRECISION F, X, CST
  330. DOUBLE PRECISION B, C, R, RE, AE
  331. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  332. DOUBLE PRECISION T, DT
  333. INTEGER IFLAG
  334. EXTERNAL F
  335. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  336. B = 0.D0
  337. C = CAP * 0.9D0
  338. R = CAP / 2.0D0
  339. RE = 1.0D-9
  340. AE = 1.0D-9
  341. CALL DFZERO(F, B, C, R, RE, AE, IFLAG)
  342. CST = AA*( B * DT( B ) + T( B ) )
  343. X = B
  344. RETURN
  345. END
  346. FUNCTION SOMT(N,K,I0,I1)
  347. IMPLICIT NONE
  348. INTEGER N, I0, I1, I
  349. DOUBLE PRECISION K(N), TMP, SOMT, T
  350. TMP = 0.0D0
  351. DO 1, I = I0, I1
  352. TMP = TMP + T(K(I))
  353. 1 CONTINUE
  354. SOMT = TMP
  355. RETURN
  356. END
  357. FUNCTION SOMK(N,K,I0,I1)
  358. IMPLICIT NONE
  359. INTEGER N, I0, I1, I
  360. DOUBLE PRECISION K(N), TMP, SOMK
  361. TMP = 0.0D0
  362. DO 1, I = I0, I1
  363. TMP = TMP + K(I)
  364. 1 CONTINUE
  365. SOMK = TMP
  366. RETURN
  367. END
  368. FUNCTION JSMC(N,C,K,I,J)
  369. IMPLICIT NONE
  370. INTEGER N
  371. DOUBLE PRECISION K(N), K1(N),K2(N), T, DT
  372. INTEGER I, J, C, IT
  373. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  374. DOUBLE PRECISION SOMT, SOMK, SMC, JSMC, H
  375. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  376. H = 0.01D0
  377. DO 1, IT = 1, N
  378. K1(IT) = K(IT)
  379. K2(IT) = K(IT)
  380. 1 CONTINUE
  381. K1(J) = K(J) + H
  382. K2(J) = K(J) - H
  383. JSMC = ( SMC(N,C,K1,I) - SMC(N,C,K2,I) ) / (2.0D0 * H)
  384. RETURN
  385. END
  386. FUNCTION SMC(N,C,K,I)
  387. IMPLICIT NONE
  388. INTEGER N
  389. DOUBLE PRECISION K(N), T, DT
  390. INTEGER I, C
  391. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  392. DOUBLE PRECISION SOMT, SOMK, SMC
  393. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  394. IF (I.GE.1.AND.I.LT.C) THEN
  395. SMC = AA * ( K(I) * DT( K(I) ) + T( K(I)) )
  396. & + BB * SOMT(N,K,I+1,C)
  397. & + BB * DT(K(I)) * SOMK(N,K,1,I-1)
  398. ELSEIF(I.EQ.C) THEN
  399. SMC = AA * ( K(C) * DT( K(C) ) + T( K( C )) )
  400. & + BB * DT(K(C)) * SOMK(N,K,1,C-1)
  401. ELSEIF(I.GT.C.AND.I.LE.N) THEN
  402. SMC = AA * ( K(I) * DT( K(I) ) + T( K(I)) )
  403. & + GG * SOMT(N,K,C+1,I)
  404. & + GG * DT(K(I)) * SOMK(N,K,I,N)
  405. ELSEIF(I.EQ.0) THEN
  406. SMC = AA * T(0.0D0) + BB * SOMT(N,K,1,C)
  407. ELSEIF(I.EQ.N+1) THEN
  408. SMC = AA * T(0.0D0) + GG * SOMT(N,K,C+1,N+1)
  409. ELSE
  410. SMC = 0.0D0
  411. PRINT*, "Inconsistent call to SCM"
  412. ENDIF
  413. RETURN
  414. END
  415. FUNCTION UC(N,C,K,I)
  416. IMPLICIT NONE
  417. INTEGER N
  418. DOUBLE PRECISION K(N), T, DT
  419. INTEGER I, C
  420. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  421. DOUBLE PRECISION SOMT, SOMK, UC
  422. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  423. IF (I.GE.1.AND.I.LT.C) THEN
  424. UC = AA * T( K(I)) + BB * SOMT(N,K,I+1,C)
  425. ELSEIF(I.EQ.C) THEN
  426. UC = AA * T( K( C ) )
  427. ELSEIF(I.GT.C.AND.I.LE.N) THEN
  428. UC = AA * T( K(I) ) + GG * SOMT(N,K,C+1,I)
  429. ELSEIF(I.EQ.0) THEN
  430. UC = AA * T(0.0D0) + BB * SOMT(N,K,1,C)
  431. ELSEIF(I.EQ.N+1) THEN
  432. UC = AA * T(0.0D0) + GG * SOMT(N,K,C+1,N+1)
  433. ELSE
  434. UC = 0.0D0
  435. PRINT*, "Inconsistent call to UC"
  436. ENDIF
  437. RETURN
  438. END
  439. FUNCTION JUC(N,C,K,I,J)
  440. IMPLICIT NONE
  441. INTEGER N
  442. DOUBLE PRECISION K(N), K1(N),K2(N), T, DT
  443. INTEGER I, J, C, IT
  444. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  445. DOUBLE PRECISION SOMT, SOMK, SMC, JUC, UC, H
  446. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  447. H = 0.01D0
  448. DO 1, IT = 1, N
  449. K1(IT) = K(IT)
  450. K2(IT) = K(IT)
  451. 1 CONTINUE
  452. K1(J) = K(J) + H
  453. K2(J) = K(J) - H
  454. JUC = ( UC(N,C,K1,I) - UC(N,C,K2,I) ) / (2.0D0 * H)
  455. RETURN
  456. END
  457. SUBROUTINE EQONEMASS(K, CST, C)
  458. C Compute the equilibrium value of oppulatio where the second
  459. C mass after 0 (the central mass) becomes active
  460. IMPLICIT NONE
  461. DOUBLE PRECISION F, X, CST, K(*)
  462. DOUBLE PRECISION L, VF, CAP, AA, BB, GG
  463. DOUBLE PRECISION T, DT, DIFF, TOL
  464. INTEGER IFLAG, IT, ITMAX, C
  465. EXTERNAL F
  466. COMMON /PARAM/ L, VF, CAP, AA, BB, GG
  467. TOL=1.0D-9
  468. ITMAX = 25
  469. C initial solution
  470. X = CAP/2.0D0
  471. C
  472. C 1. Use Newton iterates to find the zero of equation :
  473. C ALPHA * T(K_0) = ALPHA T(0) + BETA T(K_0)
  474. C or
  475. C T(K_0) - ALPHA/(ALPHA-BETA) T(0) = 0
  476. C
  477. C 2. Use Newton iterates to find the zero of equation :
  478. C ALPHA T(K_0) = (ALPHA + GAMMA) T(0)
  479. C or
  480. C T(K_0) - (ALPHA+GAMMA)/ALPHA T(0) = 0
  481. C
  482. IT = 0
  483. IF(GG.GE.AA*BB/(AA-BB)) GOTO 10
  484. IF(GG.LT.AA*BB/(AA-BB)) GOTO 20
  485. 10 CONTINUE
  486. IT = IT + 1
  487. DIFF = ( T(X) - AA * T(0.0D0) / (AA-BB) ) / DT(X)
  488. X = X - DIFF
  489. IF(ABS(DIFF).LT.TOL ) GOTO 11
  490. IF(IT .LT.ITMAX) GOTO 10
  491. PRINT*, "Maximum number of iterations reached by EQONEMASS"
  492. 11 CONTINUE
  493. K(1) = 0.0D0
  494. K(2) = X
  495. C = 2
  496. GOTO 100
  497. 20 CONTINUE
  498. IT = IT + 1
  499. DIFF = ( T(X) - (AA+GG) * T(0.0D0) / AA ) / DT(X)
  500. X = X - DIFF
  501. IF(ABS(DIFF).LT.TOL ) GOTO 21
  502. IF( IT .LT.ITMAX) GOTO 20
  503. PRINT*, "Maximum number of iterations reached by EQONEMASS"
  504. 21 CONTINUE
  505. K(1) = X
  506. K(2) = 0.0D0
  507. C = 1
  508. GOTO 100
  509. 100 CONTINUE
  510. CST = AA * T(X)
  511. RETURN
  512. END
  513. SUBROUTINE EQITERATION(N,C,K,SC)
  514. IMPLICIT NONE
  515. INTEGER N, C, I
  516. DOUBLE PRECISION SC, VAL, K(N+1), UC
  517. CALL EQITER2(N,C,K,SC)
  518. CALL EQCSOLVE(N, C, K, SC)
  519. VAL = UC(N,C,K,1) - UC(N,C,K,0)
  520. IF (VAL.LT.0.0D0) THEN
  521. N = N + 1
  522. K(N+1) = 0.0D0
  523. ELSE
  524. CALL EQITER1(N,C,K,SC)
  525. CALL EQCSOLVE(N, C, K, SC)
  526. DO 3, I = N+1, 2, -1
  527. 3 K(I) = K(I-1)
  528. K(1) = 0.0D0
  529. C = C + 1
  530. N = N + 1
  531. ENDIF
  532. RETURN
  533. END
  534. SUBROUTINE EQCSOLVE(N,C,K,CBAR)
  535. C
  536. C Given N and C find K(1)..K(N) such that
  537. C the marginal social cost is equal to CBAR;
  538. C The computation is performed with the
  539. C set of active masses being unchanged.
  540. C
  541. IMPLICIT NONE
  542. INTEGER N, C, LDA, I, J, JOB, IT
  543. PARAMETER (LDA=1000)
  544. DOUBLE PRECISION K(LDA), JAK(LDA,LDA), TOL
  545. DOUBLE PRECISION B(LDA), RCOND, Z(N), CBAR, UC, JUC, DENORM
  546. INTEGER IPVT(N), ITMAX
  547. ITMAX = 25
  548. TOL = 1.0D-9
  549. IT = 0
  550. 1 CONTINUE
  551. IT = IT + 1
  552. DO 10, I = 1, N
  553. B(I) = UC(N,C,K,I) - CBAR
  554. DO 10, J = 1, N
  555. JAK(I,J) = JUC(N,C,K,I,J)
  556. 10 CONTINUE
  557. JOB = 0
  558. CALL DGECO(JAK,LDA,N,IPVT,RCOND,Z)
  559. CALL DGESL(JAK,LDA,N,IPVT,B,JOB)
  560. DO 20, I = 1, N
  561. K(I) = K(I) - B(I)
  562. C PRINT*, IT, I, K(I), B(I), DENORM(N, B), CBAR
  563. 20 CONTINUE
  564. IF(DENORM(N,B).LT.TOL) GOTO 30
  565. IF(IT.LT.ITMAX) GOTO 1
  566. PRINT*, "Maximum number of iterations reached !"
  567. 30 CONTINUE
  568. RETURN
  569. END
  570. SUBROUTINE EQITER1(N,C,K,SC)
  571. C Find marginal social cost SC such that mass
  572. C 0 (early arrival) is just to become active
  573. C Newton algorithm is used for this one dimensional
  574. C problem.
  575. C The jacobian is approximated
  576. C using finite differences.
  577. IMPLICIT NONE
  578. INTEGER N, C, LDA, IT,ITMAX
  579. PARAMETER(LDA=1000)
  580. DOUBLE PRECISION UC, K(N), H, VAL0,VAL1,VAL2,X,DIFF,SC,TOL
  581. DOUBLE PRECISION TMP
  582. ITMAX = 25
  583. H = 0.01D0
  584. X = SC
  585. IT = 0
  586. TOL = 1.0D-9
  587. 1 CONTINUE
  588. IT = IT + 1
  589. TMP = UC(N,C,K,0)
  590. CALL EQCSOLVE(N,C,K,X)
  591. VAL0 = X - UC(N,C,K,0)
  592. CALL EQCSOLVE(N,C,K,X+H)
  593. VAL1 = X+H - UC(N,C,K,0)
  594. CALL EQCSOLVE(N,C,K,X-H)
  595. VAL2 = X-H - UC(N,C,K,0)
  596. DIFF = (VAL2-VAL1)/(2.0D0*H)
  597. X = X + VAL0 / DIFF
  598. IF(ABS(VAL0).GT.TOL.AND.IT.LT.ITMAX) GOTO 1
  599. SC = X
  600. RETURN
  601. END
  602. SUBROUTINE EQITER2(N,C,K,SC)
  603. C Find marginal social cost SC such that mass
  604. C N+1 (late arrival) is just to become active
  605. C Newton algorithm is used for this one dimensional problem.
  606. C The jacobian is approximated
  607. C using finite differences.
  608. IMPLICIT NONE
  609. INTEGER N, C, LDA, IT,ITMAX
  610. PARAMETER(LDA=1000)
  611. DOUBLE PRECISION UC, K(N), H, VAL0,VAL1,VAL2,X,DIFF,SC,TOL
  612. DOUBLE PRECISION TMP
  613. ITMAX = 25
  614. H = 0.01D0
  615. X = SC
  616. IT = 0
  617. TOL = 1.0D-9
  618. 1 CONTINUE
  619. IT = IT + 1
  620. K(N+1) = 0.0D0
  621. TMP = UC(N,C,K,N+1)
  622. CALL EQCSOLVE(N,C,K,X)
  623. VAL0 = X - UC(N,C,K,N+1)
  624. CALL EQCSOLVE(N,C,K,X+H)
  625. VAL1 = X+H - UC(N,C,K,N+1)
  626. CALL EQCSOLVE(N,C,K,X-H)
  627. VAL2 = X-H - UC(N,C,K,N+1)
  628. DIFF = (VAL2-VAL1)/(2.0D0*H)
  629. X = X + VAL0 / DIFF
  630. C PRINT *, X, VAL0, DIFF, IT, K(1:3)
  631. IF(ABS(VAL0).GT.TOL.AND.IT.LT.ITMAX) GOTO 1
  632. SC = X
  633. RETURN
  634. END