! ============================================================
! ThermoFluidCalc — Hydraulic Jump Calculator
! Solve for conjugate state y2, energy loss, jump length, etc.
! Supports: rectangular horizontal channel
! ============================================================
program hydraulic_jump
    implicit none

    real(8), parameter :: g = 9.81d0
    real(8), parameter :: PI = 3.141592653589793d0
    real(8), parameter :: RHO = 1000.0d0
    real(8), parameter :: NU = 1.0d-6

    real(8) :: y1, b, flow_val, y2
    integer :: input_mode
    real(8) :: V1, V2, Q, q_width, Fr1, Fr2, E1, E2, h_L, efficiency
    real(8) :: h_j, L_j, P_loss, Re1, Re2
    character(len=20) :: mode_name, regime1, regime2
    character(len=60) :: jump_class, energy_diss_desc

    ! Read inputs
    read(*,*) y1          ! Upstream depth [m]
    read(*,*) b           ! Channel width [m]
    read(*,*) input_mode  ! 1 = Given V1, 2 = Given Q, 3 = Given Fr1
    read(*,*) flow_val   ! Flow rate parameter value

    ! Validate geometry
    if (y1 <= 0.0d0 .or. b <= 0.0d0) then
        write(*,'(A)') "ERROR: Upstream depth y1 and width b must be positive."
        stop
    end if

    ! Compute flow state depending on input mode
    select case (input_mode)
    case (1)
        mode_name = 'Upstream Velocity V1'
        V1 = flow_val
        Q = V1 * y1 * b
        Fr1 = V1 / sqrt(g * y1)
    case (2)
        mode_name = 'Total Discharge Q'
        Q = flow_val
        V1 = Q / (y1 * b)
        Fr1 = V1 / sqrt(g * y1)
    case (3)
        mode_name = 'Upstream Froude Fr1'
        Fr1 = flow_val
        V1 = Fr1 * sqrt(g * y1)
        Q = V1 * y1 * b
    case default
        write(*,'(A)') "ERROR: Invalid input mode."
        stop
    end select

    ! Verify that the flow is supercritical (Fr1 > 1.0)
    if (Fr1 <= 1.0d0) then
        write(*,'(A,F10.4)') "ERROR: Upstream flow must be supercritical (Fr1 > 1.0). Current Fr1 = ", Fr1
        stop
    end if

    ! Compute downstream conjugate state
    y2 = (y1 / 2.0d0) * (-1.0d0 + sqrt(1.0d0 + 8.0d0 * Fr1**2))
    V2 = Q / (y2 * b)
    Fr2 = V2 / sqrt(g * y2)
    q_width = Q / b

    ! Specific Energy
    E1 = y1 + V1**2 / (2.0d0 * g)
    E2 = y2 + V2**2 / (2.0d0 * g)
    h_L = E1 - E2
    efficiency = (E2 / E1) * 100.0d0
    h_j = y2 - y1

    ! Power dissipated
    P_loss = RHO * g * Q * h_L / 1000.0d0 ! in kW

    ! Reynolds Numbers
    Re1 = V1 * y1 / NU
    Re2 = V2 * y2 / NU

    ! Jump length and classification
    if (Fr1 <= 1.7d0) then
        jump_class = 'Undular Jump'
        energy_diss_desc = 'Very low energy dissipation (< 5%)'
        L_j = 4.0d0 * y2
    else if (Fr1 <= 2.5d0) then
        jump_class = 'Weak Jump'
        energy_diss_desc = 'Low energy dissipation (5% to 15%)'
        L_j = 4.0d0 * y2 + (Fr1 - 1.7d0)/(2.5d0 - 1.7d0) * 1.0d0 * y2
    else if (Fr1 <= 4.5d0) then
        jump_class = 'Oscillating Jump'
        energy_diss_desc = 'Moderate energy dissipation (15% to 45%)'
        L_j = 5.0d0 * y2 + (Fr1 - 2.5d0)/(4.5d0 - 2.5d0) * 1.1d0 * y2
    else if (Fr1 <= 9.0d0) then
        jump_class = 'Steady Jump'
        energy_diss_desc = 'High energy dissipation (45% to 70%)'
        L_j = 6.1d0 * y2
    else
        jump_class = 'Strong Jump'
        energy_diss_desc = 'Very high energy dissipation (> 70%)'
        L_j = 6.1d0 * y2 - (Fr1 - 9.0d0)/(20.0d0 - 9.0d0) * 0.5d0 * y2
        if (L_j < 5.6d0 * y2) L_j = 5.6d0 * y2
    end if

    ! Regime descriptors
    regime1 = 'Supercritical (Fr>1)'
    if (Fr2 < 1.0d0) then
        regime2 = 'Subcritical (Fr<1)'
    else
        regime2 = 'Critical (Fr=1)'
    end if

    ! === OUTPUT ASCII REPORT ===
    write(*,'(A)') '============================================================'
    write(*,'(A)') '                 HYDRAULIC JUMP CALCULATOR'
    write(*,'(A)') '============================================================'
    write(*,*)
    write(*,'(A)') '--- INPUT CONFIGURATION ---'
    write(*,'(A,F12.4,A)')   '  Channel Width (b)       = ', b, ' m'
    write(*,'(A,A)')         '  Input Selection Mode    = ', trim(mode_name)
    write(*,'(A,F12.4,A)')   '  Upstream Depth (y1)     = ', y1, ' m'
    select case (input_mode)
    case (1)
        write(*,'(A,F12.4,A)') '  Upstream Velocity (V1)  = ', V1, ' m/s'
    case (2)
        write(*,'(A,F12.4,A)') '  Total Discharge (Q)     = ', Q, ' m3/s'
    case (3)
        write(*,'(A,F12.4)')   '  Upstream Froude (Fr1)   = ', Fr1
    end select
    write(*,*)
    write(*,'(A)') '--- CONJUGATE STATE PROPERTIES ---'
    write(*,'(A,F12.4,A)')   '  Upstream Depth (y1)     = ', y1, ' m'
    write(*,'(A,F12.4,A)')   '  Downstream Depth (y2)   = ', y2, ' m'
    write(*,'(A,F12.4,A)')   '  Upstream Velocity (V1)  = ', V1, ' m/s'
    write(*,'(A,F12.4,A)')   '  Downstream Velocity (V2)= ', V2, ' m/s'
    write(*,'(A,F12.4)')     '  Upstream Froude (Fr1)   = ', Fr1
    write(*,'(A,F12.4)')     '  Downstream Froude (Fr2) = ', Fr2
    write(*,'(A,A)')         '  Upstream Flow Regime    = ', trim(regime1)
    write(*,'(A,A)')         '  Downstream Flow Regime  = ', trim(regime2)
    write(*,'(A,F12.4,A)')   '  Total Discharge (Q)     = ', Q, ' m3/s'
    write(*,'(A,F12.4,A)')   '  Discharge per Width (q) = ', q_width, ' m2/s'
    write(*,'(A,ES12.4)')    '  Upstream Reynolds (Re1) = ', Re1
    write(*,'(A,ES12.4)')    '  Downstream Reynolds(Re2)= ', Re2
    write(*,*)
    write(*,'(A)') '--- JUMP HYDRAULICS & LOSSES ---'
    write(*,'(A,F12.4,A)')   '  Upstream Energy (E1)    = ', E1, ' m'
    write(*,'(A,F12.4,A)')   '  Downstream Energy (E2)  = ', E2, ' m'
    write(*,'(A,F12.4,A)')   '  Energy Head Loss (h_L)  = ', h_L, ' m'
    write(*,'(A,F12.2,A)')   '  Jump Efficiency (E2/E1) = ', efficiency, ' %'
    write(*,'(A,F12.4,A)')   '  Jump Height (y2 - y1)   = ', h_j, ' m'
    write(*,'(A,F12.4,A)')   '  Estimated Jump Length   = ', L_j, ' m'
    write(*,'(A,F12.4,A)')   '  Power Dissipated        = ', P_loss, ' kW'
    write(*,*)
    write(*,'(A)') '--- FLOW REGIME CLASSIFICATION ---'
    write(*,'(A,A)')         '  Jump Classification     = ', trim(jump_class)
    write(*,'(A,A)')         '  Energy Dissipation      = ', trim(energy_diss_desc)
    write(*,*)
    write(*,'(A)') '--- EQUATIONS USED ---'
    write(*,'(A)') '  Conjugate Depth:  y2 = (y1/2) * [-1 + sqrt(1 + 8*Fr1^2)]'
    write(*,'(A)') '  Specific Energy:  E = y + V^2 / (2g)'
    write(*,'(A)') '  Head Loss:        h_L = E1 - E2 = (y2 - y1)^3 / (4 * y1 * y2)'
    write(*,'(A)') '  Power Dissipated: P = rho * g * Q * h_L'
    write(*,'(A)') '============================================================'

end program hydraulic_jump
