program kalina_cycle_ammonia_water
    implicit none
    integer :: iostat_val
    double precision :: zb_ammonia_frac, P_high_bar, P_low_bar, T_sep_C, m_dot_basic_kgs
    double precision :: eta_turb, eta_pump, T_source_in_C, T_source_out_C
    double precision :: x_rich, x_lean, vapor_frac, m_dot_vap, m_dot_lean
    double precision :: h_vap, h_lean, h_basic_in, w_turb, w_pump, w_net, q_in, eta_th, P_net_kW
    double precision :: Q_in_kW, Q_absorb_kW, deltaT_glide_C

    ! Read inputs
    read(*,*,iostat=iostat_val) zb_ammonia_frac   ! Basic Ammonia Mass Fraction (e.g. 0.70)
    read(*,*,iostat=iostat_val) P_high_bar        ! High Pressure [bar] (e.g. 30.0)
    read(*,*,iostat=iostat_val) P_low_bar         ! Low Pressure (Condenser/Absorber) [bar] (e.g. 4.5)
    read(*,*,iostat=iostat_val) T_sep_C           ! Separator Temperature [deg C] (e.g. 115.0)
    read(*,*,iostat=iostat_val) m_dot_basic_kgs   ! Basic Mixture Mass Flow [kg/s] (e.g. 20.0)
    read(*,*,iostat=iostat_val) eta_turb          ! Turbine Isentropic Efficiency (e.g. 0.85)
    read(*,*,iostat=iostat_val) eta_pump          ! Pump Isentropic Efficiency (e.g. 0.75)
    read(*,*,iostat=iostat_val) T_source_in_C     ! Brine/Heat Source Inlet Temp [deg C] (e.g. 140.0)

    if (iostat_val /= 0) then
        write(*,*) 'ERROR: Invalid input data for Kalina cycle calculation.'
        stop
    end if

    if (zb_ammonia_frac <= 0.1d0 .or. zb_ammonia_frac >= 0.99d0 .or. P_high_bar <= P_low_bar) then
        write(*,*) 'ERROR: Ammonia fraction must be between 0.1 and 0.99, and P_high > P_low.'
        stop
    end if

    ! Binary Ammonia-Water Phase Equilibrium Approximation
    x_rich = min(0.98d0, zb_ammonia_frac + 0.22d0 * (1.0d0 - zb_ammonia_frac))
    x_lean = max(0.20d0, zb_ammonia_frac - 0.28d0 * zb_ammonia_frac)
    vapor_frac = (zb_ammonia_frac - x_lean) / (x_rich - x_lean)
    if (vapor_frac < 0.1d0) vapor_frac = 0.1d0
    if (vapor_frac > 0.9d0) vapor_frac = 0.9d0

    m_dot_vap = m_dot_basic_kgs * vapor_frac
    m_dot_lean = m_dot_basic_kgs * (1.0d0 - vapor_frac)
    deltaT_glide_C = 25.0d0 * (1.0d0 - abs(zb_ammonia_frac - 0.5d0))

    ! Enthalpies (kJ/kg)
    h_vap = 1450.0d0 + 2.5d0 * (T_sep_C - 80.0d0) + 180.0d0 * x_rich
    h_lean = 320.0d0 + 4.18d0 * (T_sep_C - 50.0d0)
    h_basic_in = 120.0d0

    ! Turbine Expansion of Rich Vapor
    w_turb = eta_turb * (h_vap - (h_vap * (P_low_bar / P_high_bar)**0.22d0))
    ! Pump Work of Basic Liquid
    w_pump = ((P_high_bar - P_low_bar) * 100.0d0 / (900.0d0 * eta_pump))

    ! Cycle Heat and Power
    q_in = (vapor_frac * h_vap + (1.0d0 - vapor_frac) * h_lean) - h_basic_in
    w_net = (vapor_frac * w_turb) - w_pump
    eta_th = (w_net / q_in) * 100.0d0
    P_net_kW = m_dot_basic_kgs * w_net
    Q_in_kW = m_dot_basic_kgs * q_in
    Q_absorb_kW = Q_in_kW - P_net_kW

    ! Output Results
    write(*,'(A)') '============================================================'
    write(*,'(A)') ' THERMOFLUIDCALC — KALINA AMMONIA-WATER (NH3-H2O) CYCLE'
    write(*,'(A)') '============================================================'
    write(*,'(A,F10.2,A,F10.2,A)') 'Basic Ammonia Fraction zb = ', zb_ammonia_frac*100.0d0, ' % / Glide = ', deltaT_glide_C, ' C'
    write(*,'(A,F10.2,A,F10.2,A)') 'P_high / P_low (Absorber) = ', P_high_bar, ' bar / ', P_low_bar, ' bar'
    write(*,'(A,F10.2,A,F10.2,A)') 'Rich Vapor / Lean Fraction= ', x_rich*100.0d0, ' % / ', x_lean*100.0d0, ' %'
    write(*,'(A)') '------------------------------------------------------------'
    write(*,'(A,F10.2,A)')  'NET POWER OUTPUT (P_net)  = ', P_net_kW, ' kW'
    write(*,'(A,F10.2,A)')  'THERMAL EFFICIENCY (eta)  = ', eta_th, ' %'
    write(*,'(A,F10.2,A,F10.2,A)') 'Rich Vapor Flow / Lean    = ', m_dot_vap, ' kg/s / ', m_dot_lean, ' kg/s'
    write(*,'(A,F10.2,A)')  'Evaporator Heat Duty      = ', Q_in_kW, ' kW'
    write(*,'(A,F10.2,A)')  'Absorber Cooling Duty     = ', Q_absorb_kW, ' kW'
    write(*,'(A)') '============================================================'

end program kalina_cycle_ammonia_water
