﻿program ideal_gas_calculator
    implicit none
    
    double precision :: P, V, T, n, m, M_mol, R_u, R_spec
    double precision :: rho, Cp, Cv, gamma_g, a_sound
    integer :: solve_for  ! 1=P, 2=V, 3=T, 4=n
    integer :: gas_type   ! 1=air, 2=N2, 3=O2, 4=CO2, 5=H2, 6=He, 7=CH4, 8=custom
    character(len=30) :: gas_name, solve_name
    double precision :: pi
    
    ! Additional calculations
    double precision :: P2, V2, T2
    double precision :: W_iso, W_adia, Q_iso
    integer :: i
    
    pi = 3.14159265358979d0
    R_u = 8.314d0  ! Universal gas constant [J/(mol.K)]
    
    ! Read inputs
    read(*,*) solve_for
    read(*,*) gas_type
    
    ! Set gas properties
    select case(gas_type)
    case(1)
        gas_name = 'Air'
        M_mol = 28.97d-3  ! kg/mol
        gamma_g = 1.4d0
        Cp = 1005.0d0
    case(2)
        gas_name = 'Nitrogen (N2)'
        M_mol = 28.014d-3
        gamma_g = 1.4d0
        Cp = 1040.0d0
    case(3)
        gas_name = 'Oxygen (O2)'
        M_mol = 31.998d-3
        gamma_g = 1.395d0
        Cp = 918.0d0
    case(4)
        gas_name = 'Carbon Dioxide (CO2)'
        M_mol = 44.01d-3
        gamma_g = 1.289d0
        Cp = 844.0d0
    case(5)
        gas_name = 'Hydrogen (H2)'
        M_mol = 2.016d-3
        gamma_g = 1.41d0
        Cp = 14307.0d0
    case(6)
        gas_name = 'Helium (He)'
        M_mol = 4.003d-3
        gamma_g = 1.667d0
        Cp = 5193.0d0
    case(7)
        gas_name = 'Methane (CH4)'
        M_mol = 16.043d-3
        gamma_g = 1.32d0
        Cp = 2226.0d0
    case default
        gas_name = 'Custom Gas'
        read(*,*) M_mol
        M_mol = M_mol * 1.0d-3  ! Convert g/mol to kg/mol
        read(*,*) gamma_g
        Cp = gamma_g * R_u / (M_mol * (gamma_g - 1.0d0))
    end select
    
    R_spec = R_u / M_mol
    Cv = Cp / gamma_g
    
    ! Read known values and solve
    select case(solve_for)
    case(1)  ! Solve for P
        solve_name = 'Pressure (P)'
        read(*,*) V    ! m3
        read(*,*) T    ! K
        read(*,*) n    ! mol
        P = n * R_u * T / V
        m = n * M_mol
    case(2)  ! Solve for V
        solve_name = 'Volume (V)'
        read(*,*) P    ! Pa
        read(*,*) T    ! K
        read(*,*) n    ! mol
        V = n * R_u * T / P
        m = n * M_mol
    case(3)  ! Solve for T
        solve_name = 'Temperature (T)'
        read(*,*) P    ! Pa
        read(*,*) V    ! m3
        read(*,*) n    ! mol
        T = P * V / (n * R_u)
        m = n * M_mol
    case(4)  ! Solve for n
        solve_name = 'Amount (n)'
        read(*,*) P    ! Pa
        read(*,*) V    ! m3
        read(*,*) T    ! K
        n = P * V / (R_u * T)
        m = n * M_mol
    end select
    
    ! Density
    rho = P / (R_spec * T)
    
    ! Speed of sound
    a_sound = sqrt(gamma_g * R_spec * T)
    
    ! ============================================
    ! OUTPUT
    ! ============================================
    write(*,'(A)') '============================================================'
    write(*,'(A)') '   IDEAL GAS LAW CALCULATOR'
    write(*,'(A)') '============================================================'
    write(*,*)
    write(*,'(A,A)')        '  Solving for             = ', trim(solve_name)
    write(*,'(A,A)')        '  Gas                     = ', trim(gas_name)
    write(*,*)
    write(*,'(A)') '--- GAS PROPERTIES ---'
    write(*,'(A,F12.4,A)')  '  Molar Mass (M)          = ', M_mol*1000, ' g/mol'
    write(*,'(A,F12.4,A)')  '  Gas Constant (R)        = ', R_spec, ' J/(kg.K)'
    write(*,'(A,F12.4)')    '  Specific Heat Ratio (gamma) = ', gamma_g
    write(*,'(A,F12.2,A)')  '  Cp                      = ', Cp, ' J/(kg.K)'
    write(*,'(A,F12.2,A)')  '  Cv                      = ', Cv, ' J/(kg.K)'
    write(*,*)
    write(*,'(A)') '--- STATE VARIABLES ---'
    write(*,'(A,F12.2,A)')  '  Pressure (P)            = ', P, ' Pa'
    write(*,'(A,F12.4,A)')  '  Pressure                = ', P/1000, ' kPa'
    write(*,'(A,F12.6,A)')  '  Pressure                = ', P/101325, ' atm'
    write(*,'(A,F12.6,A)')  '  Pressure                = ', P/1.0d5, ' bar'
    write(*,*)
    write(*,'(A,ES12.4,A)') '  Volume (V)              = ', V, ' m3'
    write(*,'(A,F12.4,A)')  '  Volume                  = ', V*1000, ' L'
    write(*,*)
    write(*,'(A,F12.2,A)')  '  Temperature (T)         = ', T, ' K'
    write(*,'(A,F12.2,A)')  '  Temperature             = ', T-273.15d0, ' deg-C'
    write(*,'(A,F12.2,A)')  '  Temperature             = ', T*1.8d0-459.67d0, ' deg-F'
    write(*,*)
    write(*,'(A,F12.6,A)')  '  Amount (n)              = ', n, ' mol'
    write(*,'(A,F12.6,A)')  '  Mass (m)                = ', m, ' kg'
    write(*,'(A,F12.4,A)')  '  Mass                    = ', m*1000, ' g'
    write(*,*)
    write(*,'(A)') '--- DERIVED PROPERTIES ---'
    write(*,'(A,F12.4,A)')  '  Density (rho)             = ', rho, ' kg/m3'
    write(*,'(A,F12.4,A)')  '  Specific Volume (v)     = ', 1.0d0/rho, ' m3/kg'
    write(*,'(A,F12.2,A)')  '  Speed of Sound (a)      = ', a_sound, ' m/s'
    write(*,*)
    
    ! Process calculations
    write(*,'(A)') '--- PROCESS CALCULATIONS ---'
    write(*,'(A)') '  (For expansion/compression from current state)'
    write(*,*)
    
    ! Isothermal compression/expansion to 2x pressure
    P2 = 2.0d0 * P
    V2 = n * R_u * T / P2
    W_iso = n * R_u * T * log(V / V2)  ! work done BY the gas
    Q_iso = W_iso  ! For isothermal: Q = W
    
    write(*,'(A)') '  Isothermal Process (T = const, P -> 2P):'
    write(*,'(A,F12.4,A)')  '    Final Volume           = ', V2*1000, ' L'
    write(*,'(A,F12.4,A)')  '    Work done by gas       = ', W_iso, ' J'
    write(*,'(A,F12.4,A)')  '    Heat transfer          = ', Q_iso, ' J'
    write(*,*)
    
    ! Isentropic compression to 2x pressure
    T2 = T * (P2/P)**((gamma_g-1.0d0)/gamma_g)
    V2 = n * R_u * T2 / P2
    W_adia = n * Cv * M_mol * (T - T2)  ! work done BY gas (negative for compression)
    
    write(*,'(A)') '  Isentropic Process (s = const, P -> 2P):'
    write(*,'(A,F12.2,A)')  '    Final Temperature      = ', T2, ' K'
    write(*,'(A,F12.4,A)')  '    Final Volume           = ', V2*1000, ' L'
    write(*,'(A,F12.4,A)')  '    Work done by gas       = ', W_adia, ' J'
    write(*,'(A)')          '    Heat transfer          =  0 J (adiabatic)'
    write(*,*)
    
    ! P-V data for chart
    write(*,'(A)') '--- P-V DIAGRAM DATA ---'
    write(*,'(A)') '  V [L]       P_iso [kPa]   P_isen [kPa]'
    write(*,'(A)') '  ---'
    
    do i = 1, 25
        V2 = V * (0.4d0 + dble(i-1) * 1.6d0 / 24.0d0)
        
        ! Isothermal: PV = nRT -> P = nRT/V
        P2 = n * R_u * T / V2
        
        ! Isentropic: PV^gamma = const -> P = P1(V1/V2)^gamma
        T2 = P * V**gamma_g / V2**gamma_g
        
        write(*,'(F10.4,2X,F12.4,4X,F12.4)') V2*1000, P2/1000, T2/1000
    end do
    
    write(*,*)
    write(*,'(A)') '--- EQUATION USED ---'
    write(*,'(A)') '  PV = nRT (ideal gas law)'
    write(*,'(A)') '  R_universal = 8.314 J/(mol.K)'
    write(*,'(A)') '  Assumptions: ideal gas behavior, no intermolecular forces'
    
end program ideal_gas_calculator
