CoolFace
Apppublic

SoulMAH/Thermo_Dynamics

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
app.py91 linesDownload Raw Back to root
1import gradio as gr2import matplotlib.pyplot as plt3import numpy as np4 5def calculate_thermo(process_type, P1, V1, T1, n, param_value, gamma):6    R = 8.314  # Ideal gas constant7    8    # Initialize variables9    V_range = []10    P_range = []11    12    # Logic for different processes13    if process_type == "Isobaric (Const P)":14        # param_value is final Volume (V2)15        V2 = param_value16        P2 = P117        T2 = (P2 * V2) / (n * R)18        V_range = np.linspace(V1, V2, 100)19        P_range = [P1] * 10020        21    elif process_type == "Isochoric (Const V)":22        # param_value is final Pressure (P2)23        P2 = param_value24        V2 = V125        T2 = (P2 * V2) / (n * R)26        P_range = np.linspace(P1, P2, 100)27        V_range = [V1] * 10028        29    elif process_type == "Isothermal (Const T)":30        # param_value is final Volume (V2)31        V2 = param_value32        T2 = T133        V_range = np.linspace(V1, V2, 100)34        P_range = (n * R * T1) / V_range35        P2 = P_range[-1]36        37    elif process_type == "Adiabatic (Q=0)":38        # param_value is final Volume (V2)39        V2 = param_value40        V_range = np.linspace(V1, V2, 100)41        # P1*V1^gamma = P2*V2^gamma42        constant = P1 * (V1**gamma)43        P_range = constant / (V_range**gamma)44        P2 = P_range[-1]45        T2 = (P2 * V2) / (n * R)46 47    # Plotting48    fig, ax = plt.subplots(figsize=(8, 5))49    ax.plot(V_range, P_range, lw=2, color='blue')50    ax.scatter([V1, V2], [P1, P2], color='red')51    ax.set_xlabel("Volume (m³)")52    ax.set_ylabel("Pressure (Pa)")53    ax.set_title(f"{process_type} Process")54    ax.grid(True, linestyle='--', alpha=0.7)55    56    results = f"""57    ### Results:58    * **Final Pressure (P2):** {P2:.2f} Pa59    * **Final Volume (V2):** {V2:.2f} m³60    * **Final Temperature (T2):** {T2:.2f} K61    """62    63    return fig, results64 65# Gradio Interface66with gr.Blocks(theme=gr.themes.Soft()) as demo:67    gr.Markdown("# 🌡️ Thermodynamics Process Calculator")68    gr.Markdown("Calculate and visualize gas state changes using the Ideal Gas Law.")69    70    with gr.Row():71        with gr.Column():72            process = gr.Dropdown(73                ["Isobaric (Const P)", "Isochoric (Const V)", "Isothermal (Const T)", "Adiabatic (Q=0)"], 74                label="Process Type", value="Isothermal (Const T)"75            )76            P1 = gr.Number(label="Initial Pressure (P1 in Pa)", value=100000)77            V1 = gr.Number(label="Initial Volume (V1 in m³)", value=1)78            T1 = gr.Number(label="Initial Temperature (T1 in K)", value=300)79            n = gr.Number(label="Moles (n)", value=1)80            param = gr.Number(label="Target Value (V2 for most, P2 for Isochoric)", value=2)81            gamma = gr.Slider(1.0, 1.67, step=0.01, label="Gamma (Adiabatic Index)", value=1.4)82            83            btn = gr.Button("Calculate & Plot", variant="primary")84            85        with gr.Column():86            plot = gr.Plot()87            output_text = gr.Markdown()88 89    btn.click(calculate_thermo, inputs=[process, P1, V1, T1, n, param, gamma], outputs=[plot, output_text])90 91demo.launch()