program shell_tube_design
    implicit none

    ! Inputs
    integer :: N_shell
    double precision :: T_hi, T_ho, T_ci, T_co
    double precision :: m_h, m_c, Cp_h, Cp_c
    double precision :: U

    ! Solved properties
    double precision :: C_h, C_c, C_min, C_max, C_r
    double precision :: Q_h, Q_c, Q
    double precision :: P, R, P_1, K, F, LMTD_cf, A_req
    double precision :: dT1, dT2, denom_val, val_num, val_den
    double precision :: eff, NTU

    integer :: iostat_val, i
    logical :: has_cross_error

    ! Read inputs
    read(*,*,iostat=iostat_val) N_shell
    if (iostat_val /= 0) then
        write(*,*) 'ERROR: Invalid shell passes input.'
        stop
    end if

    read(*,*,iostat=iostat_val) T_hi
    read(*,*,iostat=iostat_val) T_ho
    read(*,*,iostat=iostat_val) T_ci
    read(*,*,iostat=iostat_val) T_co
    read(*,*,iostat=iostat_val) m_h
    read(*,*,iostat=iostat_val) m_c
    read(*,*,iostat=iostat_val) Cp_h
    read(*,*,iostat=iostat_val) Cp_c
    read(*,*,iostat=iostat_val) U

    if (N_shell < 1) N_shell = 1

    ! 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

    ! Counter flow LMTD (ideal baseline)
    dT1 = T_hi - T_co
    dT2 = T_ho - T_ci
    
    if (dT1 <= 0.0d0 .or. dT2 <= 0.0d0) then
        LMTD_cf = 0.0d0
        has_cross_error = .true.
    else if (abs(dT1 - dT2) < 1.0d-6) then
        LMTD_cf = dT1
    else
        LMTD_cf = (dT1 - dT2) / log(dT1 / dT2)
    end if

    ! P and R values for shell-and-tube correction factor F
    has_cross_error = .false.
    if (T_hi - T_ci <= 0.0d0) then
        has_cross_error = .true.
    else
        P = (T_co - T_ci) / (T_hi - T_ci)
        if (T_co - T_ci /= 0.0d0) then
            R = (T_hi - T_ho) / (T_co - T_ci)
        else
            R = 1.0d0
        end if
    end if

    if (.not. has_cross_error) then
        ! Check boundaries for P and R
        if (P < 0.0d0 .or. P >= 1.0d0 .or. R < 0.0d0) then
            has_cross_error = .true.
        else
            ! Avoid division by zero when R=1 by shifting slightly
            if (abs(R - 1.0d0) < 1.0d-5) then
                R = 1.0001d0
            end if

            ! Compute single pass effectiveness P_1
            if (1.0d0 - P * R <= 0.0d0 .or. 1.0d0 - P <= 0.0d0) then
                has_cross_error = .true.
            else
                K = ((1.0d0 - P * R) / (1.0d0 - P))**(1.0d0 / dble(N_shell))
                if (abs(K - R) < 1.0d-8) then
                    P_1 = P / dble(N_shell)
                else
                    P_1 = (1.0d0 - K) / (R - K)
                end if
            end if
        end if
    end if

    F = -1.0d0
    if (.not. has_cross_error) then
        ! Calculate F factor for 1-2 pass shell-and-tube arrangement
        denom_val = 2.0d0 - P_1 * (R + 1.0d0 + sqrt(R**2 + 1.0d0))
        if (denom_val <= 0.0d0) then
            has_cross_error = .true.
        else
            val_num = sqrt(R**2 + 1.0d0) * log((1.0d0 - P_1) / (1.0d0 - R * P_1))
            val_den = (R - 1.0d0) * log((2.0d0 - P_1 * (R + 1.0d0 - sqrt(R**2 + 1.0d0))) / denom_val)
            if (val_den /= 0.0d0) then
                F = val_num / val_den
                if (F <= 0.0d0 .or. F > 1.0d0) then
                    has_cross_error = .true.
                end if
            else
                has_cross_error = .true.
            end if
        end if
    end if

    ! Required Area
    if (.not. has_cross_error .and. LMTD_cf > 0.0d0 .and. U > 0.0d0) then
        A_req = Q / (U * F * LMTD_cf)
    else
        A_req = 0.0d0
        F = 0.0d0
    end if

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

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

    ! Output results
    write(*,'(A,I2)') 'N_SHELL=', N_shell
    write(*,'(A,F14.4)') 'T_HI=', T_hi
    write(*,'(A,F14.4)') 'T_HO=', T_ho
    write(*,'(A,F14.4)') 'T_CI=', T_ci
    write(*,'(A,F14.4)') 'T_CO=', T_co
    write(*,'(A,F14.4)') 'M_H=', m_h
    write(*,'(A,F14.4)') 'M_C=', m_c
    write(*,'(A,F14.2)') 'CP_H=', Cp_h
    write(*,'(A,F14.2)') 'CP_C=', Cp_c
    write(*,'(A,F14.2)') 'U=', U
    write(*,'(A,F14.2)') 'Q=', Q
    write(*,'(A,F14.6)') 'P=', P
    write(*,'(A,F14.6)') 'R=', R
    write(*,'(A,F14.6)') 'P1=', P_1
    write(*,'(A,F14.6)') 'F_FACTOR=', F
    write(*,'(A,F14.4)') 'LMTD_CF=', LMTD_cf
    write(*,'(A,F14.4)') 'A_REQ=', A_req
    write(*,'(A,F14.4)') 'EFF=', eff * 100.0d0
    write(*,'(A,F14.4)') 'NTU=', NTU
    if (has_cross_error) then
        write(*,'(A,I1)') 'HAS_CROSS_ERROR=', 1
    else
        write(*,'(A,I1)') 'HAS_CROSS_ERROR=', 0
    end if

    ! Temperature profile list along exchanger
    write(*,'(A)') '--- TEMPERATURE PROFILE ALONG EXCHANGER --------------------'
    write(*,'(A)') '  Position %    T_hot [C]    T_cold [C]'
    write(*,'(A)') '  --------------------------------------------------'
    do i = 0, 20
        ! We write profiles assuming parallel shell/tube layout for visualization
        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 do

end program shell_tube_design
