program datacenter_pue_thermal
    implicit none
    integer :: contain_type, iostat_val
    double precision :: P_IT_kW, P_cool_kW, P_ups_kW, P_aux_kW
    double precision :: P_total_kW, PUE_val, DCiE_pct, dTrack_C, T_cold_C, T_hot_C
    double precision :: CFM_total, Q_m3h, RAI_pct, CFM_per_kW
    character(len=32) :: efficiency_class

    ! Read inputs
    read(*,*,iostat=iostat_val) P_IT_kW         ! IT Equipment Power [kW] (e.g. 500.0)
    read(*,*,iostat=iostat_val) P_cool_kW       ! Cooling Infrastructure Power [kW] (e.g. 150.0)
    read(*,*,iostat=iostat_val) P_ups_kW        ! UPS & Distribution Losses [kW] (e.g. 45.0)
    read(*,*,iostat=iostat_val) P_aux_kW        ! Lighting & Aux Power [kW] (e.g. 15.0)
    read(*,*,iostat=iostat_val) dTrack_C        ! Server Delta-T [deg C] (e.g. 12.0)
    read(*,*,iostat=iostat_val) T_cold_C        ! Cold Aisle Supply Temp [deg C] (e.g. 22.0)
    read(*,*,iostat=iostat_val) contain_type    ! 1=Open Aisle, 2=Cold Aisle Cont, 3=Hot Aisle Cont

    if (iostat_val /= 0) then
        write(*,*) 'ERROR: Invalid input data for data center thermal analysis.'
        stop
    end if

    if (P_IT_kW <= 0.0d0 .or. dTrack_C <= 0.0d0) then
        write(*,*) 'ERROR: IT load and delta T must be positive.'
        stop
    end if

    P_total_kW = P_IT_kW + P_cool_kW + P_ups_kW + P_aux_kW
    PUE_val = P_total_kW / P_IT_kW
    DCiE_pct = (1.0d0 / PUE_val) * 100.0d0

    T_hot_C = T_cold_C + dTrack_C

    ! Airflow calculation (1.006 kJ/kg.K, rho=1.18 kg/m3)
    ! Q = P_IT / (rho * cp * dT)
    CFM_total = (1756.0d0 * P_IT_kW) / dTrack_C
    CFM_per_kW = CFM_total / P_IT_kW
    Q_m3h = CFM_total * 1.699d0

    if (contain_type == 1) then
        RAI_pct = 65.0d0 ! Open Aisle (significant recirculation)
    else if (contain_type == 2) then
        RAI_pct = 92.0d0 ! Cold Aisle Containment
    else
        RAI_pct = 96.0d0 ! Hot Aisle Containment
    end if

    if (PUE_val <= 1.20d0) then
        efficiency_class = 'HYPERSCALE WORLD-CLASS (PUE <= 1.2)'
    else if (PUE_val <= 1.40d0) then
        efficiency_class = 'VERY EFFICIENT ENTERPRISE (PUE <= 1.4)'
    else if (PUE_val <= 1.70d0) then
        efficiency_class = 'AVERAGE LEGACY DATA CENTER (PUE <= 1.7)'
    else
        efficiency_class = 'POOR EFFICIENCY (PUE > 1.7)'
    end if

    ! Output Results
    write(*,'(A)') '============================================================'
    write(*,'(A)') ' THERMOFLUIDCALC — DATA CENTER PUE & THERMAL COOLING ENGINE'
    write(*,'(A)') '============================================================'
    write(*,'(A,F10.1,A,F10.1,A)') 'IT Load / Total Power     = ', P_IT_kW, ' kW / ', P_total_kW, ' kW'
    write(*,'(A,F10.3)')    'POWER USAGE EFFECTIVENESS = ', PUE_val
    write(*,'(A,F10.1,A)')  'DCiE Efficiency Metric    = ', DCiE_pct, ' %'
    write(*,'(A,A)')        'Energy Efficiency Class   = ', trim(efficiency_class)
    write(*,'(A)') '------------------------------------------------------------'
    write(*,'(A,F10.1,A,F6.1,A)') 'Cold / Hot Aisle Temp     = ', T_cold_C, ' C / ', T_hot_C, ' C'
    write(*,'(A,F10.1,A)')  'Server Delta-T Rise       = ', dTrack_C, ' C'
    write(*,'(A,F10.1,A)')  'Required Airflow Rate     = ', CFM_total, ' CFM'
    write(*,'(A,F10.1,A)')  'Volumetric Airflow (m3/h) = ', Q_m3h, ' m3/h'
    write(*,'(A,F10.1,A)')  'Specific Airflow Metric   = ', CFM_per_kW, ' CFM/kW'
    write(*,'(A,F10.1,A)')  'Return Air Index (RAI)    = ', RAI_pct, ' %'
    write(*,'(A)') '============================================================'

end program datacenter_pue_thermal
