33 SUBROUTINE phase_equilib (end_x,steps,converg)
39 REAL,
INTENT(IN) :: end_x
40 REAL,
INTENT(IN) :: steps
41 INTEGER,
INTENT(OUT) :: converg
44 INTEGER :: k, count1,count2,runindex,maxiter
45 REAL :: delta_x,delta_org,val_org,runvar
46 CHARACTER (LEN=2) :: compon
47 LOGICAL :: continue_cycle
54 IF (running(1:2) ==
'd1') runindex = 1
55 IF (running(1:2) ==
'd2') runindex = 2
56 IF (running(1:1) ==
't') runindex = 3
57 IF (running(1:1) ==
'p') runindex = 4
58 IF (running(1:2) ==
'x1') compon = running(3:3)
59 IF (running(1:2) ==
'x1')
READ (compon,*) k
60 IF (running(1:2) ==
'x1') runindex = 4+k
61 IF (running(1:2) ==
'x2') compon = running(3:3)
62 IF (running(1:2) ==
'x2')
READ (compon,*) k
63 IF (running(1:2) ==
'x2') runindex = 4+ncomp+k
64 IF (running(1:2) ==
'l1') compon = running(3:3)
65 IF (running(1:2) ==
'l1')
READ (compon,*) k
66 IF (running(1:2) ==
'l1') runindex = 4+k
67 IF (running(1:2) ==
'l2') compon = running(3:3)
68 IF (running(1:2) ==
'l2')
READ (compon,*) k
69 IF (running(1:2) ==
'l2') runindex = 4+ncomp+k
76 IF ( ncomp >= 3 ) maxiter = 1000
79 delta_x = ( end_x - val_init(runindex) ) / steps
80 delta_org = ( end_x - val_init(runindex) ) / steps
81 val_org = val_init(runindex)
82 IF ( running(1:1) ==
'x' )
THEN 83 delta_x = ( end_x - exp(val_init(runindex)) ) / steps
84 delta_org = ( end_x - exp(val_init(runindex)) ) / steps
85 val_org = exp(val_init(runindex))
88 continue_cycle = .true.
94 DO WHILE ( continue_cycle )
101 CALL objective_ctrl (converg)
103 IF (converg == 1)
THEN 104 val_init( 1:(4+ncomp*nphas) ) = val_conv( 1:(4+ncomp*nphas) )
105 IF (outp >= 1 .AND. (abs(delta_x) > 0.1*abs(delta_org) .OR. count2 == 2))
CALL output
108 delta_x = delta_x / 2.0
109 IF (num == 2) delta_x = delta_x / 2.0
110 val_init(runindex) = val_org
111 IF (running(1:1) ==
'x') val_init(runindex) = log(val_org)
112 continue_cycle = .true.
115 runvar = val_init(runindex)
116 IF (running(1:1) ==
'x') runvar = exp(val_init(runindex))
118 IF ( end_x == 0.0 .AND. running(1:1) /=
'x' )
THEN 119 IF ( abs(runvar-end_x) < 1.e-8 ) continue_cycle = .false.
120 ELSE IF ( abs((runvar-end_x)/end_x) < 1.e-8 )
THEN 122 continue_cycle = .false.
123 ELSE IF ( count1 == maxiter )
THEN 124 WRITE (*,*)
' MAX. NO OF ITERATIONS',count1
126 continue_cycle = .false.
127 ELSE IF ( abs(delta_x) < 1.e-5*abs(delta_org) )
THEN 130 continue_cycle = .false.
132 continue_cycle = .true.
134 IF (abs(runvar+delta_x-end_x) > abs(runvar-end_x)) delta_x = end_x - runvar
135 val_init(runindex) = runvar + delta_x
136 IF (running(1:1) ==
'x') val_init(runindex) = log(runvar+delta_x)
139 IF (abs(delta_x) < abs(delta_org) .AND. count2 >= 5)
THEN 140 delta_x = delta_x * 2.0
146 END SUBROUTINE phase_equilib
183 SUBROUTINE objective_ctrl (converg)
193 INTEGER,
INTENT(OUT) :: converg
197 SUBROUTINE objec_fct ( iter_no, y, residu, dummy )
198 INTEGER,
INTENT(IN) :: iter_no
199 REAL,
INTENT(IN) :: y(iter_no)
200 REAL,
INTENT(OUT) :: residu(iter_no)
201 INTEGER,
INTENT(IN OUT) :: dummy
202 END SUBROUTINE objec_fct
205 INTEGER :: info, k, posn, i
206 REAL,
ALLOCATABLE :: y(:), diag(:), residu(:)
207 REAL :: x_init, x_solut, r_diff1, r_diff2, totres
208 REAL :: r_thrash, x_thrash
209 CHARACTER (LEN=2) :: compon
211 INTEGER :: ph_split, promising_min
212 REAL :: eta_trial, phi_2_start
213 REAL,
dimension(nc) :: xif_save, rhoi_feed, rhoi1, rhoi2
214 LOGICAL :: convergence
219 ALLOCATE( y(n_unkw), diag(n_unkw), residu(n_unkw) )
221 IF (num == 0) acc_a = 1.e-7
222 IF (num == 0) step_a = 2.e-8
223 IF (num == 1) acc_a = 1.e-7
224 IF (num == 1) step_a = 2.e-8
225 IF (num == 2) acc_a = 5.e-7
226 IF (num == 2) step_a = 1.e-7
235 IF (it(i) ==
't') y(posn) = val_init(3)
236 IF (it(i) ==
'p') y(posn) = val_init(4)
237 IF (it(i) ==
'lnp') y(posn) = log( val_init(4) )
238 IF (it(i) ==
'fls') y(posn) = alpha
239 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1') compon = it(i)(3:3)
240 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1')
READ (compon,*) k
241 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1') y(posn) = val_init(4+k)
242 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2') compon = it(i)(3:3)
243 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2')
READ (compon,*) k
244 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2') y(posn) = val_init(4+ncomp+k)
245 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3') compon = it(i)(3:3)
246 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3')
READ (compon,*) k
247 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3') y(posn) = val_init(4+ncomp+ncomp+k)
258 IF (lnx(1,i) /= 0.0 .AND. lnx(2,i) /= 0.0)
THEN 259 x_init = x_init + abs( 1.0 - lnx(2,i)/lnx(1,i) )
261 x_init = x_init + abs( 1.0 - exp(lnx(2,i))/exp(lnx(1,i)) )
269 CALL hbrd (objec_fct, n_unkw, y, residu, step_a, acc_a, info, diag)
270 IF ( info <= 1 .AND. sum( abs( residu(1:n_unkw) ) ) > 100.0*acc_a ) &
271 CALL hbrd (objec_fct, n_unkw, y, residu, step_a, acc_a, info, diag)
279 IF ( lnx(1,i) /= 0.0 .AND. lnx(2,i) /= 0.0 )
THEN 280 x_solut = x_solut + abs( 1.0 - lnx(2,i)/lnx(1,i) )
282 IF (lnx(1,i) < 300.0 .AND. lnx(1,i) > -300.0 ) &
283 x_solut = x_solut + abs( 1.0 - exp(lnx(2,i))/exp(lnx(1,i)) )
286 r_diff1 = abs( 1.0 - dense(1)/dense(2) )
287 IF ( val_conv(2) > 0.0 )
THEN 288 r_diff2 = abs( 1.0 - val_conv(1)/val_conv(2) )
293 totres = sum( abs( residu(1:n_unkw) ) )
297 if (num > 0 ) r_thrash = r_thrash * 10.0
298 if (num > 0 ) x_thrash = x_thrash * 100.0
302 IF ( info >= 2 ) convergence = .false.
303 IF ( abs( 1.0- dense(1)/dense(2) ) < r_thrash .AND. x_solut < x_thrash )
THEN 304 IF ( x_init > 0.050 ) convergence = .false.
305 IF ( ( abs( 1.0- dense(1)/dense(2) ) + x_solut ) < 1.e-7 ) convergence = .false.
307 IF ( r_diff2 /= 0.0 .AND. r_diff2 > (4.0*r_diff1) .AND. bindiag == 1 ) convergence = .false.
308 IF ( ncomp == 1 .AND. totres > 100.0*acc_a ) convergence = .false.
309 IF ( ncomp == 1 .AND. r_diff1 < 1.e-5 ) convergence = .false.
310 IF ( totres > 1000.0*acc_a .AND. .NOT. generate_starting_val ) convergence = .false.
311 IF ( totres > 10000.0*acc_a .AND. generate_starting_val ) convergence = .false.
317 IF ( convergence )
THEN 332 if ( check_stability_of_phases )
then 333 my_f( 1:ncomp ) = my_cal(1,1:ncomp)
334 rhoi_feed( 1:ncomp ) = rhoi_cal( maxloc(dense(1:2), 1), 1:ncomp )
335 xif_save(1:ncomp) = xif(1:ncomp)
336 xif(1:ncomp) = 0.95 * rhoi_cal( maxloc(dense(1:2), 1), 1:ncomp ) &
337 / sum( rhoi_cal( maxloc(dense(1:2), 1), 1:ncomp ) ) &
338 + 0.05 * rhoi_cal( minloc(dense(1:2), 1), 1:ncomp ) &
339 / sum( rhoi_cal( minloc(dense(1:2), 1), 1:ncomp ) )
340 if ( minval( dense(1:2) ) < 0.18 ) eta_trial = 0.40
341 if ( minval( dense(1:2) ) >=0.18 ) eta_trial = p / ( kbol*1.e30 * t ) &
342 * pi/6.0 * sum( rhoi_feed( 1:ncomp ) / sum(rhoi_feed( 1:ncomp )) * mseg(1:ncomp) * dhs(1:ncomp)**3 )
343 call phase_stability ( rhoi_feed, eta_trial, ph_split, rhoi_trial )
345 if ( ph_split == 1 )
then 346 rhoi1( 1:ncomp ) = rhoi_trial( 1:ncomp )
347 rhoi2( 1:ncomp ) = rhoi_feed( 1:ncomp )
348 call tangent_plane_line_search ( rhoi1, rhoi2, phi_2_start, promising_min )
350 dense( minloc(dense(1:2), 1) ) = pi/6.0 *sum( rhoi_trial(1:ncomp) * mseg(1:ncomp) * dhs(1:ncomp)**3 )
351 call rachford_rice ( converg, rhoi1, rhoi2 )
352 if ( converg == 1 )
then 353 call occupy_val_init ( rhoi1, rhoi2 )
354 if ( ncomp == 2 )
CALL select_sum_rel (1,0,1)
355 if ( ncomp == 2 )
CALL select_sum_rel (2,0,2)
357 if ( outp > 0 )
write (*,*)
' ' 358 if ( outp > 0 )
write (*,*)
'===== the type (LLE, VLE) of phase equilibrium changed =====' 359 if ( outp > 0 )
write (*,*)
' ' 361 call restore_converged
363 if ( outp > 0 .AND. ph_split == 1 )
write (*,*)
'calc. phase equilibr. not stable (press enter)' 364 if ( outp > 0 .AND. ph_split == 1 )
read (*,*)
367 call restore_converged
370 xif(1:ncomp) = xif_save(1:ncomp)
379 DEALLOCATE( y, diag, residu )
381 END SUBROUTINE objective_ctrl
398 SUBROUTINE objec_fct ( iter_no, y, residu, dummy )
402 use utilities
, only: x_summation
406 INTEGER,
INTENT(IN) :: iter_no
407 REAL,
INTENT(IN) :: y(iter_no)
408 REAL,
INTENT(OUT) :: residu(iter_no)
409 INTEGER,
INTENT(IN OUT) :: dummy
412 INTEGER :: i, ph,k,posn, skip,phase
413 REAL :: lnphi(np,nc),isofugacity
414 CHARACTER (LEN=2) :: compon
424 IF (it(i) ==
't') t = y(posn)
425 IF (it(i) ==
'p') p = y(posn)
426 IF (it(i) ==
'lnp') p = exp( y(posn) )
427 IF (it(i) ==
'fls') alpha = y(posn)
428 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1') compon = it(i)(3:3)
429 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1')
READ (compon,*) k
430 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'1') lnx(1,k) = y(posn)
431 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2') compon = it(i)(3:3)
432 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2')
READ (compon,*) k
433 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'2') lnx(2,k) = y(posn)
434 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3') compon = it(i)(3:3)
435 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3')
READ (compon,*) k
436 IF (it(i)(1:1) ==
'x' .AND. it(i)(2:2) ==
'3') lnx(3,k) = y(posn)
440 IF (lnx(1,k) > 0.0) lnx(1,k) = 0.0
441 IF (lnx(2,k) > 0.0) lnx(2,k) = 0.0
448 IF (p < 1.e-100) p = 1.e-12
449 IF ( p /= p ) p = 1000.0
450 IF ( t /= t ) t = 300.0
451 IF ( alpha /= alpha ) alpha = 0.5
459 IF ( lnx(ph,i) < -300.0 )
THEN 462 xi(ph,i) = exp( lnx(ph,i) )
471 IF (ncomp > 1)
CALL x_summation
477 CALL fugacity (lnphi)
486 IF (n_unkw < (ncomp*(nphas-1))) skip = ncomp*(nphas-1) - n_unkw
487 IF ((i+skip-ncomp*(phase-2)) > ncomp) phase = phase + 1
488 residu(i) = isofugacity((i+skip-ncomp*(phase-2)),phase,lnphi)
489 if ( density_error(phase) /= 0.0 ) residu(i) = residu(i) + sign( density_error(phase), residu(i) ) * 0.001
492 END SUBROUTINE objec_fct
503 REAL FUNCTION isofugacity (i,phase,lnphi)
509 INTEGER,
INTENT(IN) :: i
510 INTEGER,
INTENT(IN) :: phase
511 REAL,
INTENT(IN) :: lnphi(np,nc)
522 isofugacity = scaling(i) *( lnphi(p2,i)+lnx(p2,i)-lnx(p1,i)-lnphi(p1,i) )
530 END FUNCTION isofugacity
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains paramete...
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW Module STARTING_VALUES This m...
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains paramete...