File size: 8,160 Bytes
ea3d81d
 
0457f6d
 
 
ea3d81d
0457f6d
ea3d81d
0457f6d
 
 
 
 
 
 
 
 
ea3d81d
 
 
 
 
 
 
0457f6d
ea3d81d
0457f6d
ea3d81d
0457f6d
 
 
ea3d81d
 
 
 
 
0457f6d
 
 
 
 
 
 
ea3d81d
0457f6d
 
 
 
 
ea3d81d
0457f6d
 
ea3d81d
0457f6d
 
 
 
 
 
 
 
 
 
ea3d81d
0457f6d
 
ea3d81d
0457f6d
 
 
 
 
ea3d81d
0457f6d
 
 
 
 
 
 
 
 
 
ea3d81d
 
 
 
 
 
 
 
0457f6d
ea3d81d
0457f6d
 
 
ea3d81d
 
 
 
 
0457f6d
ea3d81d
0457f6d
 
ea3d81d
 
0457f6d
ea3d81d
 
 
 
 
 
0457f6d
 
ea3d81d
0457f6d
 
 
ea3d81d
 
 
0457f6d
 
 
ea3d81d
 
 
 
 
 
0457f6d
ea3d81d
0457f6d
ea3d81d
 
0457f6d
 
ea3d81d
 
0457f6d
 
ea3d81d
 
0457f6d
ea3d81d
 
 
0457f6d
ea3d81d
 
 
 
0457f6d
 
 
ea3d81d
 
0457f6d
 
ea3d81d
 
0457f6d
 
ea3d81d
 
 
 
 
 
 
0457f6d
ea3d81d
 
 
 
0457f6d
ea3d81d
0457f6d
 
ea3d81d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0457f6d
ea3d81d
 
 
 
 
 
 
 
 
 
 
0457f6d
ea3d81d
 
0457f6d
ea3d81d
0457f6d
ea3d81d
 
 
 
 
0457f6d
ea3d81d
0457f6d
ea3d81d
 
 
 
 
 
 
 
0457f6d
ea3d81d
 
 
0457f6d
ea3d81d
 
 
0457f6d
ea3d81d
0457f6d
ea3d81d
0457f6d
ea3d81d
 
 
 
 
 
 
 
 
 
0457f6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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("""
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'};
</script>
""", 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("""
<script>
MathJax = {
  tex: { inlineMath: [['$', '$'], ['\\\\(', '\\\\)']] }
};
</script>
<script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
""", unsafe_allow_html=True)

st.markdown("""
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
.block-container {padding-top: 1.2rem;}
</style>
""", 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)