import os import sys import streamlit as st # -------------------------------------------------------------- # STREAMLIT CONFIG # -------------------------------------------------------------- st.set_page_config( page_title="Quantum Physics Simulation", page_icon="⚛️", layout="wide" ) import numpy as np import plotly.graph_objects as go st.markdown(""" """, unsafe_allow_html=True) # ------------------------- # Path Setup # ------------------------- FILE_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(FILE_DIR, "..", "..")) if PROJECT_ROOT not in sys.path: sys.path.append(PROJECT_ROOT) # ------------------------- # IMPORTS # ------------------------- # --- PIB simulation --- from app.core.simulations.simulation_pib import ( calc_1d_wavefunction, plot_probability_3d, plot_fourier_spectrum, plot_expectation_values, ) # --- PIB animations --- from app.core.animations.pib_animation import ( animate_pib_pov, animate_wavefunction_graph_auto, ) # --- PIB theory --- from app.core.theory.theory_pib import THEORY_SECTIONS as PIB_THEORY # --- HYDROGEN simulation --- from app.core.simulations.simulation_hydrogen import ( plot_radial_wavefunction, plot_radial_probability, plot_energy_levels as plot_hydrogen_energy_levels, plot_hydrogen_3d, plot_hydrogen_orbital, plot_hydrogen_nodes, plot_hydrogen_phase ) # --- HYDROGEN theory --- from app.core.theory.theory_hydrogen import THEORY_SECTIONS as HYDRO_THEORY # --- HYDROGEN animation --- from app.core.animations.hydrogen_animation import ( animate_hydrogen_wavefunction ) # -------------------------------------------------------------- # FIX MATHJAX FOR LATEX # -------------------------------------------------------------- st.markdown(""" """, unsafe_allow_html=True) st.markdown(""" """, unsafe_allow_html=True) # -------------------------------------------------------------- # SIDEBAR UI # -------------------------------------------------------------- st.sidebar.title("⚛ Controls") system = st.sidebar.selectbox( "Select System", ["Particle in a Box (PIB)", "Hydrogen Atom"] ) mode = st.sidebar.radio("Mode", ["Theory", "Graphs", "Animation"]) dimension = st.sidebar.selectbox("Display Mode", ["2D", "3D"]) # --- PIB controls --- n_pib = st.sidebar.number_input("PIB: Quantum number n", 1, 12, 1) L_pib = st.sidebar.number_input("PIB: Box Length L", 1.0, 100.0, 10.0) animation_type = st.sidebar.selectbox( "PIB Animation Type", ["Particle POV", "Auto Changing (n & L)"] ) speed = st.sidebar.slider("Animation Speed", 0.2, 2.5, 1.0) # --- Hydrogen controls --- n_h = st.sidebar.number_input("H: n", 1, 8, 1) l_h = st.sidebar.number_input("H: l", 0, 7, 0) m_h = st.sidebar.number_input("H: m", -5, 5, 0) rmax_bohr = st.sidebar.number_input("H: rmax (Bohr radii)", 10.0, 800.0, 200.0) # Bohr radius in meters A0 = 5.29177210903e-11 rmax_m = rmax_bohr * A0 # -------------------------------------------------------------- # MAIN TITLE # -------------------------------------------------------------- st.title("⚛ Quantum Physics Simulation") # ========================================================= # THEORY MODE # ========================================================= if mode == "Theory": st.header("📘 Theory") if system == "Particle in a Box (PIB)": for title, content in PIB_THEORY.items(): with st.expander(title): st.markdown(content, unsafe_allow_html=True) else: for title, content in HYDRO_THEORY.items(): with st.expander(title): st.markdown(content, unsafe_allow_html=True) # ========================================================= # GRAPH MODE # ========================================================= elif mode == "Graphs": st.header("📊 Graphs & Visualizations") # ------------------ PIB ------------------ if system == "Particle in a Box (PIB)": x, psi, prob, energy = calc_1d_wavefunction(n_pib, L_pib) if dimension == "3D": fig3d = plot_probability_3d(n=n_pib, m=1, L=L_pib, resolution=120) st.plotly_chart(fig3d, use_container_width=True) else: fig = go.Figure() fig.add_trace(go.Scatter(x=x, y=psi, name="ψ(x)")) fig.add_trace(go.Scatter(x=x, y=prob, name="|ψ|²")) st.plotly_chart(fig, use_container_width=True) st.subheader("Energy Levels (n=1–12)") ns = np.arange(1, 13) energies = (ns ** 2) * np.pi ** 2 / (2 * L_pib ** 2) fig_e = go.Figure() fig_e.add_trace(go.Scatter(x=ns, y=energies, mode="lines+markers")) st.plotly_chart(fig_e, use_container_width=True) st.subheader("Fourier Spectrum") st.plotly_chart(plot_fourier_spectrum(n_pib, L_pib), use_container_width=True) st.subheader("Expectation Values") st.plotly_chart(plot_expectation_values(n_pib, L_pib), use_container_width=True) # ------------------ HYDROGEN ------------------ else: if dimension == "3D": st.subheader("3D Hydrogen Visualizations") view_mode = st.selectbox( "Visualization Type", ["Probability Density", "Orbital Shape", "Nodes", "Phase"] ) try: if view_mode == "Probability Density": fig3d = plot_hydrogen_3d(n_h) elif view_mode == "Orbital Shape": fig3d = plot_hydrogen_orbital(n_h, l_h, m_h) elif view_mode == "Nodes": fig3d = plot_hydrogen_nodes(n_h, l_h, m_h) else: fig3d = plot_hydrogen_phase(n_h, l_h, m_h) st.plotly_chart(fig3d, use_container_width=True) except Exception as e: st.error(f"Hydrogen 3D plot failed: {e}") else: st.subheader("Radial Wavefunction") st.plotly_chart( plot_radial_wavefunction(n_h, l_h, rmax_m), use_container_width=True ) st.subheader("Radial Probability Distribution") st.plotly_chart( plot_radial_probability(n_h, l_h, rmax_m), use_container_width=True ) st.subheader("Energy Levels (n=1–8)") st.plotly_chart(plot_hydrogen_energy_levels(max_n=8), use_container_width=True) # ========================================================= # ANIMATION MODE # ========================================================= elif mode == "Animation": st.header("🎞 Animation") # ----------- PIB ----------- if system == "Particle in a Box (PIB)": if animation_type == "Particle POV": fig = animate_pib_pov( L=L_pib, n=n_pib, speed=speed, display_mode=dimension ) st.plotly_chart(fig, use_container_width=True) else: n_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] L_values = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80] fig = animate_wavefunction_graph_auto( n_values=n_values, L_values=L_values, frames_per_step=int(60 / speed) ) st.plotly_chart(fig, use_container_width=True) # ----------- HYDROGEN ----------- else: st.subheader("Hydrogen Wavefunction Animation") fig = animate_hydrogen_wavefunction( n=n_h, l=l_h, m=m_h, speed=speed, display_mode=dimension ) st.plotly_chart(fig, use_container_width=True)