15 REAL,
DIMENSION(400) :: x_exp, y_exp, t_exp, p_exp
16 REAL,
DIMENSION(400,2) :: deviation
19 PUBLIC :: kij_fit_lij, kij_fit
30 SUBROUTINE kij_fit_lij
35 use utilities
, only: file_open, paus
38 INTEGER :: i, nadjust, prin
39 REAL,
ALLOCATABLE :: optpars(:)
40 REAL :: fmin, t0, h0, machep
41 REAL :: dummy2, dummy3, dummy4, dummy5
42 REAL :: rms1, rms2, aad_m1, aad_m2
43 CHARACTER :: fitin*50, fitin2*71, fitout*80, dummy1*30
49 REAL,
ALLOCATABLE :: g(:)
58 write (*,*)
' CHOOSE THE INPUT FILE (./input_file/kij-dat/...) :' 59 write (*,*)
' SPECIFY FULL NAME:' 69 fitin2 =
'./input_file/kij-dat/'//fitin
71 CALL file_open(fitin2,87)
79 READ (87,*) dummy1,dummy2,dummy3,dummy4,dummy5
81 IF (dummy1 /=
'end')
THEN 82 if ( .NOT.( dummy2 == 1.0 .OR. dummy3 == 1.0 ) .AND. &
83 .NOT.(dummy2 == 0.0 .AND. dummy3 == 0.0 ) )
then 87 p_exp(i) = dummy4 * 1.e5
88 t_exp(i) = dummy5 + 273.15
96 write (*,*)
' CHOOSE THE EOS :' 97 write (*,*)
' 0: SAFT EOS ' 98 write (*,*)
' 1: PC-SAFT EOS ' 99 write (*,*)
' 2: SRK EOS ' 100 write (*,*)
' 3: PR EOS ' 104 write (*,*)
' FURTHER SPECIFY THE EOS :' 105 write (*,*)
' 0: PC-SAFT EOS ' 106 write (*,*)
' 1: PCP-SAFT EOS ' 107 write (*,*)
' 2: PCIP-SAFT EOS ' 111 write (*,*)
' CHOOSE THE BINARY PARAMETERS TO BE ADJUSTED :' 112 write (*,*)
' 1: kij ' 113 write (*,*)
' 2: kij AND lij' 115 IF ( nadjust == 2 )
THEN 117 CALL set_default_eos_numerical
118 write (*,*)
'calculation conducted with numerical derivatives (press enter)' 121 ALLOCATE ( optpars(nadjust) )
124 IF ( eos == 1 ) fitout =
'./output_file/kij-pc-saft/'//fitout
125 IF ( eos == 0 ) fitout =
'./output_file/kij-saft/'//fitout
126 IF ( eos == 2 ) fitout =
'./output_file/kij-srk/'//fitout
127 IF ( eos == 3 ) fitout =
'./output_file/kij-pr/'//fitout
128 OPEN ( 88, file=fitout )
133 write (*,
'(a,F12.5)')
' current value of kij=',kij(1,2)
134 write (*,
'(a)')
' do you want to give another starting value? ( 1 : yes, 0 : no)' 136 if ( i == 1 )
read (*,*) kij(1,2)
144 optpars(1) = kij(1,2) + 1.0
145 IF (nadjust == 2) optpars(2) = lij(1,2) + 1.0
150 IF (nadjust > 1)
THEN 152 ALLOCATE ( g(nadjust) )
153 CALL newton_opt_2d ( bindev_kij_p, optpars, nadjust, 1.e-8, 1.e-8, g, fmin)
162 call dim1min( t0, h0, nadjust, optpars, bindev_kij_p, fmin )
166 write (*,*)
' Ergebnis: kij=',kij(1,2),
' AAD=',fmin /
real( n_exp )
167 write(88,*)
' Ergebnis: kij=',kij(1,2),
' AAD=',fmin /
real( n_exp )
168 write (*,*)
' Ergebnis: lij=',lij(1,2)
169 write(88,*)
' Ergebnis: lij=',lij(1,2)
176 write (88,*)
'experimental data' 178 write (88,
'(i4,4(2x,G13.6))') i,x_exp(i),y_exp(i), p_exp(i)/1.e5,t_exp(i)-273.15
181 write (88,*)
'calculated values' 183 write (88,
'(i4,4(2x,G13.6))') i,x_exp(i)+deviation(i,1), &
184 y_exp(i)+deviation(i,2), &
185 p_exp(i)/1.e5,t_exp(i)-273.15
189 write (*,*)
' DEV% ph_1=',100.0*deviation(i,1),
' DEV% ph_2=',100.0*deviation(i,2)
190 write (88,*)
' DEVIATION% phase_1=',100.0*deviation(i,1), &
191 ' DEVIATION% phase_2=',100.0*deviation(i,2)
192 rms1 = rms1 + deviation(i,1)**2
193 rms2 = rms2 + deviation(i,2)**2
194 aad_m1 = aad_m1 + abs( deviation(i,1) )
195 aad_m2 = aad_m2 + abs( deviation(i,2) )
197 rms1 = 100.0 * ( rms1 /
real( n_exp ) )**0.5
198 rms2 = 100.0 * ( rms2 /
real( n_exp ) )**0.5
199 aad_m1 = 100.0 * aad_m1 /
real( n_exp )
200 aad_m2 = 100.0 * aad_m2 /
real( n_exp )
206 write(*,*)
' AAD% phase_1=',aad_m1
207 write(*,*)
' AAD% phase_2=',aad_m2
209 write(*,*)
' RMS% phase_1=',rms1
210 write(*,*)
' RMS% phase_2=',rms2
216 write(88,*)
' AAD% phase_1=',aad_m1
217 write(88,*)
' AAD% phase_2=',aad_m2
219 write(88,*)
' RMS% phase_1=',rms1
220 write(88,*)
' RMS% phase_2=',rms2
223 write(*,*)
' Result: kij=',kij(1,2)
224 IF ( nadjust == 2 )
write(*,*)
' lij=',lij(1,2)
225 write(88,*)
' Result: kij=',kij(1,2)
226 IF ( nadjust == 2 )
write(88,*)
' lij=',lij(1,2)
231 DEALLOCATE ( optpars )
234 END SUBROUTINE kij_fit_lij
243 SUBROUTINE bindev_kij_p ( fmin, optpars, nadjust )
250 INTEGER,
INTENT(IN) :: nadjust
251 REAL,
INTENT(IN) :: optpars(:)
252 REAL,
INTENT(IN OUT) :: fmin
255 INTEGER :: i, converg, iterate_t
258 REAL :: dev_x_i, dev_y_i, weigh_x_p, weigh_x_y
259 REAL,
DIMENSION(400) :: dev
261 real,
dimension(nc) :: rhoi1, rhoi2
262 CHARACTER (LEN=1) :: report_success_step
263 CHARACTER (LEN=30) :: report_character
267 kij(1,2) = optpars(1) - 1.0
270 IF (nadjust == 2) lij(1,2) = optpars(2)-1.0
271 IF (nadjust == 2) lij(2,1) = - lij(1,2)
284 report_character =
' ' 285 report_success_step =
' ' 298 if ( x_sta == 0.0 ) x_sta = 0.001
299 if ( x_sta == 1.0 ) x_sta = 0.999
300 if ( y_sta == 0.0 ) y_sta = 0.001
301 if ( y_sta == 1.0 ) y_sta = 0.999
305 xi(1,1) = 1.0 - x_sta
306 xi(2,1) = 1.0 - y_sta
312 xif(1) = 0.5 * ( xi(1,1) + xi(2,1) )
313 xif(2) = 1.0 - xif(1)
319 call rachford_rice ( converg, rhoi1, rhoi2 )
320 if ( converg == 1 )
then 321 CALL occupy_val_init ( rhoi1, rhoi2 )
323 report_success_step =
'A' 331 IF ( converg /= 1 )
THEN 334 xif( 1:ncomp ) = xi( 1, 1:ncomp )
335 call start_var_fixed_composition ( converg )
336 if ( converg == 1 )
then 338 call restore_converged
339 report_success_step =
'B' 348 IF ( converg /= 1 )
THEN 351 call scan_compositions ( converg )
352 if ( converg == 1 )
then 354 call restore_converged
355 report_success_step =
'C' 365 IF (converg == 1 .AND. abs(val_conv(1)/val_conv(2)-1.0) > 0.25 )
THEN 371 if ( val_init(1)/val_init(2) < 0.6 )
then 372 write (*,*)
'swap A',i
384 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) dev_x_i = (xi(1,2)-x_exp(i))**2
385 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) dev_y_i = (xi(2,2)-y_exp(i))**2
387 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) deviation(i,1) = xi(1,2)-x_exp(i)
388 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) deviation(i,2) = xi(2,2)-y_exp(i)
395 xi(1,1) = 1.0 - xi(1,2)
398 call bubble_point_rachford_rice ( iterate_t, converg, rhoi1, rhoi2 )
399 IF ( converg == 1 )
CALL occupy_val_init ( rhoi1, rhoi2 )
400 IF ( converg == 1 ) val_conv = val_init
406 if ( converg == 1 )
then 407 dev(i) = ( weigh_x_p * 1.0/dev_x_i &
408 + ( 1.0-weigh_x_p )*( val_conv(4)/p_exp(i)-1.0 )**(-2) )**(-1)
409 dev(i) = weigh_x_y * dev(i) + ( 1.0 - weigh_x_y ) * dev_y_i
411 dev(i) = weigh_x_y * dev_x_i + ( 1.0 - weigh_x_y ) * dev_y_i
413 if ( converg /= 1 )
write (*,*)
'second step not converged, i=',i
415 ELSE IF (converg == 1 .AND. abs(val_conv(1)/val_conv(2)-1.0) <= 0.25 )
THEN 421 if ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 .AND. abs(xi(1,2)-x_exp(i)) > abs(xi(2,2)-x_exp(i)) )
then 422 write (*,*)
'swap1',i
427 if ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 .AND. abs(xi(2,2)-y_exp(i)) > abs(xi(1,2)-y_exp(i)) )
then 428 write (*,*)
'swap2',i
439 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) dev(i) = (xi(1,2)-x_exp(i))**2
440 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) dev(i) = dev(i) + (xi(2,2)-y_exp(i))**2
442 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) deviation(i,1) = xi(1,2)-x_exp(i)
443 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) deviation(i,2) = xi(2,2)-y_exp(i)
454 xi(1,1) = 1.0 - xi(1,2)
455 xi(2,1:2) = xi(1,1:2)
459 call bubble_point_rachford_rice ( iterate_t, converg, rhoi1, rhoi2 )
460 if ( converg == 1 )
then 461 CALL occupy_val_init ( rhoi1, rhoi2 )
469 if ( converg == 1 ) dev(i) = 5.0*( val_conv(4)/p_exp(i)-1.0 )**2
470 if ( converg == 1 ) report_character =
'only delta(p) converged' 471 if ( converg /= 1 )
write (*,
'(a,i4,a,2G18.11)')
' point number ',i, &
472 ' did not converge, x,y=',x_exp(i),y_exp(i)
473 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) deviation(i,1) = 0.0
474 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) deviation(i,2) = 0.0
483 error = sum( dev(1:n_exp) )
486 write(*,
'(a,f14.8,a,3(f14.8))')
'deviation=',error,
' kij=',optpars(1:nadjust) - 1.0
488 END SUBROUTINE bindev_kij_p
501 use utilities
, only: file_open, paus
504 INTEGER :: i, j, k, opt
505 REAL :: dummy2, dummy3, dummy4
506 REAL :: kijorg, devges(5)
507 REAL :: dist, kijsav(5), devopt, dummy5, rms1, rms2
508 REAL :: aad1(400,5), aad2(400,5), aad_m1, aad_m2, maxerr(2,3)
509 REAL :: max_t, min_t, max_p, min_p, aver_y
510 REAL :: fmin, optpars(1)
511 CHARACTER :: fitin*50, fitin2*71, fitout*80, dummy1*30
530 write (*,*)
' CHOOSE THE INPUT FILE (./input_file/kij-dat/...) :' 531 write (*,*)
' SPECIFY FULL NAME:' 535 fitin2 =
'./input_file/kij-dat/'//fitin
537 CALL file_open(fitin2,87)
541 READ (87,*) compna(1)
542 READ (87,*) compna(2)
546 READ (87,*) dummy1,dummy2,dummy3,dummy4,dummy5
548 IF (dummy1 ==
'end')
THEN 554 p_exp(i) = dummy4 * 1.e5
555 t_exp(i) = dummy5 + 273.15
560 write (*,*)
' CHOOSE THE EOS :' 561 write (*,*)
' 0: SAFT EOS ' 562 write (*,*)
' 1: PC-SAFT EOS ' 563 write (*,*)
' 2: SRK EOS ' 564 write (*,*)
' 3: PR EOS ' 568 write (*,*)
' FURTHER SPECIFY THE EOS :' 569 write (*,*)
' 0: PC-SAFT EOS ' 570 write (*,*)
' 1: PCP-SAFT EOS ' 571 write (*,*)
' 2: PCIP-SAFT EOS ' 573 IF(pol == 2 .AND. num == 0)
write(*,*)
'switch to num. derivatives' 574 IF(pol == 2 .AND. num == 0) num=1
578 IF (eos == 1) fitout =
'./output_file/kij-pc-saft/'//fitout
579 IF (eos == 0) fitout =
'./output_file/kij-saft/'//fitout
580 IF (eos == 2) fitout =
'./output_file/kij-srk/'//fitout
581 IF (eos == 3) fitout =
'./output_file/kij-pr/'//fitout
582 OPEN (88,file=fitout)
594 kij(1,2) = kijorg - 1.0 * dist
596 kij(1,2) = kijorg - 0.5 * dist
600 kij(1,2) = kijorg + 0.5 * dist
602 kij(1,2) = kijorg + 1.0 * dist
606 optpars(1) = kij(1,2) + 1.0
609 call bindev_kij ( fmin, optpars, 1 )
611 write (*,
'(a,i3,2(f14.8))')
' deviation',j,devges(j),kij(1,2)
614 aad1(i,j) = deviation(i,1)
615 aad2(i,j) = deviation(i,2)
625 IF (devges(k) < devopt)
THEN 633 IF ( opt /= 1 .AND. opt /= 5 ) dist = 0.25*dist
635 write(*,
'(a,f14.8,a,2(f14.8))')
' new kij',kij(1,2),
' +/-',dist,devopt
639 IF (dist <= 0.00005)
EXIT kij_adjust
647 maxerr(1,1) = 100.0 * aad1(1,opt)
648 maxerr(1,2) = t_exp(1)
649 maxerr(1,3) = p_exp(1)
650 maxerr(2,1) = 100.0 * aad2(1,opt)
651 maxerr(2,2) = t_exp(1)
652 maxerr(2,3) = p_exp(1)
654 write (88,
'(i4,4(2x,G13.6))') i,x_exp(i),y_exp(i),p_exp(i)/1.e5,t_exp(i)-273.15
658 IF( (100.0 * abs(aad1(i,opt))) > maxerr(1,1) )
THEN 659 maxerr(1,1) = 100.0 * abs(aad1(i,opt))
660 maxerr(1,2) = t_exp(i)
661 maxerr(1,3) = p_exp(i)
663 IF( (100.0 * abs(aad2(i,opt))) > maxerr(2,1) )
THEN 664 maxerr(2,1) = 100.0 * abs(aad2(i,opt))
665 maxerr(2,2) = t_exp(i)
666 maxerr(2,3) = p_exp(i)
668 write (*,*)
' DEV% ph_1=', 100.0 * aad1(i,opt),
' DEV% ph_2=',100.0 * aad2(i,opt)
669 write (88,*)
' DEVIATION% phase_1=',100.0 * aad1(i,opt), &
670 ' DEVIATION% phase_2=',100.0 * aad2(i,opt)
671 rms1 = rms1 + aad1(i,opt)**2
672 rms2 = rms2 + aad2(i,opt)**2
673 aad_m1 = aad_m1 + abs(aad1(i,opt))
674 aad_m2 = aad_m2 + abs(aad2(i,opt))
676 rms1 = 100.0 * ( rms1 /
real( n_exp ) )**0.5
677 rms2 = 100.0 * ( rms2 /
real( n_exp ) )**0.5
678 aad_m1 = 100.0 * aad_m1 /
real( n_exp )
679 aad_m2 = 100.0 * aad_m2 /
real( n_exp )
682 write(*,*)
' MAX.DEV% ph_1=',maxerr(1,1),
' AT',(maxerr(1,i),i=2,3)
683 write(*,*)
' MAX.DEV% ph_2=',maxerr(2,1),
' AT',(maxerr(2,i),i=2,3)
685 write(*,*)
' AAD% phase_1=',aad_m1
686 write(*,*)
' AAD% phase_2=',aad_m2
688 write(*,*)
' RMS% phase_1=',rms1
689 write(*,*)
' RMS% phase_2=',rms2
692 write(88,*)
' MAX. DEVIATION% phase_1=',maxerr(1,1),
' AT',(maxerr(1,i),i=2,3)
693 write(88,*)
' MAX. DEVIATION% phase_2=',maxerr(2,1),
' AT',(maxerr(2,i),i=2,3)
695 write(88,*)
' AAD% phase_1=',aad_m1
696 write(88,*)
' AAD% phase_2=',aad_m2
698 write(88,*)
' RMS% phase_1=',rms1
699 write(88,*)
' RMS% phase_2=',rms2
702 write(*,*)
' Result: kij=',kij(1,2)
703 write(88,*)
' Result: kij=',kij(1,2)
711 aver_y = y_exp(1) /
real( n_exp )
713 IF (t_exp(i) > max_t) max_t = t_exp(i)
714 IF (t_exp(i) < min_t) min_t = t_exp(i)
715 IF (p_exp(i) > max_p) max_p = p_exp(i)
716 IF (p_exp(i) < min_p) min_p = p_exp(i)
717 aver_y = aver_y + y_exp(i) /
real( n_exp )
719 write (88,
'(a,1x,12(1x,e12.5),1x,a)') fitin,kij(1,2),aad_m1 &
720 ,aad_m2,rms1,rms2,maxerr(1,1),maxerr(2,1),min_t-273.15 &
721 ,max_t-273.15,min_p/1.e5,max_p/1.e5,aver_y
726 END SUBROUTINE kij_fit
733 SUBROUTINE bindev_kij ( fmin, optpars, nadjust )
739 INTEGER,
INTENT(IN) :: nadjust
740 REAL,
INTENT(IN) :: optpars(:)
741 REAL,
INTENT(IN OUT) :: fmin
744 INTEGER :: i, k, ph, ph_split, converg
747 REAL,
DIMENSION(400) :: dev
752 kij(1,2) = optpars(1) - 1.0
755 IF (nadjust == 2) lij(1,2) = optpars(2)-1.0
757 IF (nadjust == 2) lij(2,1) = - lij(1,2)
772 if ( x_sta == 0.0 ) x_sta = 0.01
773 if ( x_sta == 1.0 ) x_sta = 0.99
774 if ( y_sta == 0.0 ) y_sta = 0.01
775 if ( y_sta == 1.0 ) y_sta = 0.99
777 lnx(1,2) = log( x_sta )
778 lnx(2,2) = log( y_sta )
779 lnx(1,1) = log( 1.0 - x_sta )
780 lnx(2,1) = log( 1.0 - y_sta )
787 val_init(4+k+(ph-1)*ncomp) = lnx(ph,k)
791 CALL objective_ctrl (converg)
793 IF (converg /= 1)
THEN 797 xif( 1:ncomp ) = xi( 1, 1:ncomp )
798 call start_var_fixed_composition ( converg )
800 IF (converg /= 1)
THEN 801 write (*,*)
' NO SOLUTION FOUND FOR REG. PRESSURE',i
803 xif(2) = ( x_exp(i) + y_exp(i) ) / 2.0
804 xif(1) = 1.0 - xif(2)
805 p = p_exp(i) - 0.1*p_exp(i)
808 call start_var_fixed_composition ( converg )
809 IF (converg == 1)
THEN 813 call phase_equilib (end_x,steps,converg)
815 IF (converg == 1)
write (*,*)
' NOW A SOLUTION WAS FOUND ?, CHECK !',i,p_exp(i)
822 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) dev(i) = (xi(1,2)-x_exp(i))**2
823 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) dev(i) = dev(i) + (xi(2,2)-y_exp(i))**2
825 IF ( x_exp(i) /= 0.0 .AND. x_exp(i) /= 1.0 ) deviation(i,1) = xi(1,2)-x_exp(i)
826 IF ( y_exp(i) /= 0.0 .AND. y_exp(i) /= 1.0 ) deviation(i,2) = xi(2,2)-y_exp(i)
833 error = sum( dev(1:n_exp) )
836 write(*,
'(a,f14.8,a,3(f14.8))')
'deviation=',error,
' kij=',optpars(1:nadjust) - 1.0
838 END SUBROUTINE bindev_kij
841 end module kij_fitting
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW Module STARTING_VALUES This m...
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains paramete...