program heat_exchanger_lmtd
    implicit none

    double precision :: T_hi, T_ho, T_ci, T_co
    double precision :: m_h, m_c, Cp_h, Cp_c
    double precision :: U, A_req
    double precision :: Q_h, Q_c, Q
    double precision :: dT1, dT2, LMTD_pf, LMTD_cf
    double precision :: LMTD_used
    double precision :: C_h, C_c, C_min, C_max, C_r
    double precision :: eff
    double precision :: NTU
    integer :: config
    integer :: i
    character(len=20) :: config_name

    ! Read inputs
    read(*,*) config     ! 1=parallel flow, 2=counter flow
    read(*,*) T_hi       ! Hot fluid inlet temp [C]
    read(*,*) T_ho       ! Hot fluid outlet temp [C]
    read(*,*) T_ci       ! Cold fluid inlet temp [C]
    read(*,*) T_co       ! Cold fluid outlet temp [C]
    read(*,*) m_h        ! Hot fluid mass flow rate [kg/s]
    read(*,*) m_c        ! Cold fluid mass flow rate [kg/s]
    read(*,*) Cp_h       ! Hot fluid specific heat [J/kg.K]
    read(*,*) Cp_c       ! Cold fluid specific heat [J/kg.K]
    read(*,*) U          ! Overall heat transfer coefficient [W/m2.K]

    ! Configuration name
    if (config == 1) then
        config_name = 'Parallel Flow'
    else
        config_name = 'Counter Flow'
    end if

    ! Heat capacity rates
    C_h = m_h * Cp_h
    C_c = m_c * Cp_c

    if (C_h < C_c) then
        C_min = C_h
        C_max = C_c
    else
        C_min = C_c
        C_max = C_h
    end if
    C_r = C_min / C_max

    ! Heat transfer rates
    Q_h = C_h * (T_hi - T_ho)
    Q_c = C_c * (T_co - T_ci)
    Q = (Q_h + Q_c) / 2.0d0

    ! ============================================
    ! LMTD CALCULATION
    ! ============================================

    ! Parallel flow LMTD
    dT1 = T_hi - T_ci
    dT2 = T_ho - T_co
    if (abs(dT1 - dT2) < 1.0d-6) then
        LMTD_pf = dT1
    else if (dT1 > 0 .and. dT2 > 0) then
        LMTD_pf = (dT1 - dT2) / log(dT1 / dT2)
    else
        LMTD_pf = 0.0d0
    end if

    ! Counter flow LMTD
    dT1 = T_hi - T_co
    dT2 = T_ho - T_ci
    if (abs(dT1 - dT2) < 1.0d-6) then
        LMTD_cf = dT1
    else if (dT1 > 0 .and. dT2 > 0) then
        LMTD_cf = (dT1 - dT2) / log(dT1 / dT2)
    else
        LMTD_cf = 0.0d0
    end if

    ! Select LMTD based on configuration
    if (config == 1) then
        LMTD_used = LMTD_pf
    else
        LMTD_used = LMTD_cf
    end if

    ! Required area
    if (LMTD_used > 0 .and. U > 0) then
        A_req = Q / (U * LMTD_used)
    else
        A_req = 0.0d0
    end if

    ! Effectiveness
    if (C_min * (T_hi - T_ci) > 0) then
        eff = Q / (C_min * (T_hi - T_ci))
    else
        eff = 0.0d0
    end if

    ! NTU
    if (C_min > 0 .and. A_req > 0) then
        NTU = U * A_req / C_min
    else
        NTU = 0.0d0
    end if

    ! ============================================
    ! OUTPUT
    ! ============================================
    write(*,'(A)') '============================================================='
    write(*,'(A)') '   HEAT EXCHANGER ANALYSIS - LMTD METHOD'
    write(*,'(A)') '============================================================='
    write(*,*)
    write(*,'(A)') '--- CONFIGURATION -------------------------------------------'
    write(*,'(A,A)')        '  Flow Arrangement        = ', trim(config_name)
    write(*,*)
    write(*,'(A)') '--- INPUT PARAMETERS ----------------------------------------'
    write(*,'(A)') '  Hot Fluid:'
    write(*,'(A,F12.2,A)')  '    Inlet Temperature     = ', T_hi, ' C'
    write(*,'(A,F12.2,A)')  '    Outlet Temperature    = ', T_ho, ' C'
    write(*,'(A,F12.4,A)')  '    Mass Flow Rate        = ', m_h, ' kg/s'
    write(*,'(A,F12.2,A)')  '    Specific Heat         = ', Cp_h, ' J/kg.K'
    write(*,*)
    write(*,'(A)') '  Cold Fluid:'
    write(*,'(A,F12.2,A)')  '    Inlet Temperature     = ', T_ci, ' C'
    write(*,'(A,F12.2,A)')  '    Outlet Temperature    = ', T_co, ' C'
    write(*,'(A,F12.4,A)')  '    Mass Flow Rate        = ', m_c, ' kg/s'
    write(*,'(A,F12.2,A)')  '    Specific Heat         = ', Cp_c, ' J/kg.K'
    write(*,*)
    write(*,'(A,F12.2,A)')  '  Overall U               = ', U, ' W/m2.K'
    write(*,*)
    write(*,'(A)') '--- HEAT CAPACITY RATES -------------------------------------'
    write(*,'(A,F12.4,A)')  '  C_hot  = m_h x Cp_h    = ', C_h, ' W/K'
    write(*,'(A,F12.4,A)')  '  C_cold = m_c x Cp_c    = ', C_c, ' W/K'
    write(*,'(A,F12.4,A)')  '  C_min                   = ', C_min, ' W/K'
    write(*,'(A,F12.4,A)')  '  C_max                   = ', C_max, ' W/K'
    write(*,'(A,F12.4)')    '  C_r = C_min/C_max       = ', C_r
    write(*,*)
    write(*,'(A)') '--- ENERGY BALANCE ------------------------------------------'
    write(*,'(A,F12.4,A)')  '  Q_hot  = C_h(T_hi-T_ho)= ', Q_h, ' W'
    write(*,'(A,F12.4,A)')  '  Q_cold = C_c(T_co-T_ci)= ', Q_c, ' W'
    write(*,'(A,F12.4,A)')  '  Q (average)             = ', Q, ' W'
    write(*,'(A,F12.4,A)')  '  Energy Balance Error    = ', &
        abs(Q_h-Q_c)/max(Q_h,1.0d-10)*100, ' %'
    write(*,*)
    write(*,'(A)') '--- LMTD RESULTS --------------------------------------------'
    write(*,'(A,F12.4,A)')  '  LMTD (parallel flow)    = ', LMTD_pf, ' deg-C'
    write(*,'(A,F12.4,A)')  '  LMTD (counter flow)     = ', LMTD_cf, ' deg-C'
    write(*,'(A,F12.4,A)')  '  LMTD (used)             = ', LMTD_used, ' deg-C'
    write(*,*)
    write(*,'(A)') '--- DESIGN RESULTS ------------------------------------------'
    write(*,'(A,F12.4,A)')  '  Required Area (A)       = ', A_req, ' m2'
    write(*,'(A,F12.4)')    '  NTU = UA/C_min          = ', NTU
    write(*,'(A,F12.4,A)')  '  Effectiveness (e)       = ', eff*100, ' %'
    write(*,*)

    ! Temperature profiles for chart
    write(*,'(A)') '--- TEMPERATURE PROFILE ALONG EXCHANGER --------------------'
    write(*,'(A)') '  Position %    T_hot [C]    T_cold [C]'
    write(*,'(A)') '  --------------------------------------------------'

    do i = 0, 20
        if (config == 1) then
            ! Parallel flow: both fluids travel same direction
            write(*,'(F10.1,4X,F10.2,4X,F10.2)') dble(i)/20.0d0*100, &
                T_hi - (T_hi - T_ho) * dble(i)/20.0d0, &
                T_ci + (T_co - T_ci) * dble(i)/20.0d0
        else
            ! Counter flow: cold fluid travels opposite direction
            write(*,'(F10.1,4X,F10.2,4X,F10.2)') dble(i)/20.0d0*100, &
                T_hi - (T_hi - T_ho) * dble(i)/20.0d0, &
                T_co - (T_co - T_ci) * dble(i)/20.0d0
        end if
    end do

    write(*,*)
    write(*,'(A)') '--- FORMULAS USED -------------------------------------------'
    write(*,'(A)') '  Q = U x A x LMTD'
    write(*,'(A)') '  LMTD = (DT1 - DT2) / ln(DT1/DT2)'
    write(*,'(A)') '  Effectiveness = Q / Q_max = Q / (C_min x (T_hi - T_ci))'
    write(*,'(A)') '  NTU = U x A / C_min'

end program heat_exchanger_lmtd
