j25aiml013 commited on
Commit
ea3d81d
·
verified ·
1 Parent(s): 0457f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -57
app.py CHANGED
@@ -1,33 +1,39 @@
 
 
1
  import streamlit as st
2
 
3
  # --------------------------------------------------------------
4
- # MUST BE FIRST STREAMLIT COMMAND
5
  # --------------------------------------------------------------
 
6
  st.set_page_config(
7
  page_title="Quantum Physics Simulation",
8
  page_icon="⚛️",
9
  layout="wide"
10
  )
11
 
12
- # --------------------------------------------------------------
13
- # Standard imports (no Streamlit!)
14
- # --------------------------------------------------------------
15
- import os
16
- import sys
17
  import numpy as np
18
  import plotly.graph_objects as go
19
 
20
- # --------------------------------------------------------------
 
 
 
 
 
 
21
  # Path Setup
22
- # --------------------------------------------------------------
23
  FILE_DIR = os.path.dirname(os.path.abspath(__file__))
24
- PROJECT_ROOT = os.path.abspath(os.path.join(FILE_DIR))
25
  if PROJECT_ROOT not in sys.path:
26
  sys.path.append(PROJECT_ROOT)
27
 
28
- # --------------------------------------------------------------
29
- # IMPORTS (must NOT run Streamlit at import time)
30
- # --------------------------------------------------------------
 
 
31
  from app.core.simulations.simulation_pib import (
32
  calc_1d_wavefunction,
33
  plot_probability_3d,
@@ -35,13 +41,16 @@ from app.core.simulations.simulation_pib import (
35
  plot_expectation_values,
36
  )
37
 
 
38
  from app.core.animations.pib_animation import (
39
  animate_pib_pov,
40
  animate_wavefunction_graph_auto,
41
  )
42
 
 
43
  from app.core.theory.theory_pib import THEORY_SECTIONS as PIB_THEORY
44
 
 
45
  from app.core.simulations.simulation_hydrogen import (
46
  plot_radial_wavefunction,
47
  plot_radial_probability,
@@ -52,18 +61,19 @@ from app.core.simulations.simulation_hydrogen import (
52
  plot_hydrogen_phase
53
  )
54
 
 
55
  from app.core.theory.theory_hydrogen import THEORY_SECTIONS as HYDRO_THEORY
56
 
 
57
  from app.core.animations.hydrogen_animation import (
58
  animate_hydrogen_wavefunction
59
  )
60
 
61
  # --------------------------------------------------------------
62
- # SAFE JAVASCRIPT BLOCKS
63
  # --------------------------------------------------------------
64
  st.markdown("""
65
  <script>
66
- window.PlotlyConfig = {MathJaxConfig: 'local'};
67
  MathJax = {
68
  tex: { inlineMath: [['$', '$'], ['\\\\(', '\\\\)']] }
69
  };
@@ -71,93 +81,181 @@ MathJax = {
71
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
72
  """, unsafe_allow_html=True)
73
 
 
 
 
 
 
 
 
 
74
  # --------------------------------------------------------------
75
- # MAIN APP STARTS HERE
76
  # --------------------------------------------------------------
77
-
78
- st.title("⚛ Quantum Physics Simulation")
79
-
80
  st.sidebar.title("⚛ Controls")
81
 
82
- system = st.sidebar.selectbox("Select System", ["Particle in a Box (PIB)", "Hydrogen Atom"])
 
 
 
 
83
  mode = st.sidebar.radio("Mode", ["Theory", "Graphs", "Animation"])
 
84
  dimension = st.sidebar.selectbox("Display Mode", ["2D", "3D"])
85
 
86
- # PIB controls
87
- n_pib = st.sidebar.number_input("PIB: n", 1, 12, 1)
88
  L_pib = st.sidebar.number_input("PIB: Box Length L", 1.0, 100.0, 10.0)
89
- animation_type = st.sidebar.selectbox("PIB Animation Type", ["Particle POV", "Auto Changing"])
 
 
 
 
 
90
  speed = st.sidebar.slider("Animation Speed", 0.2, 2.5, 1.0)
91
 
92
- # Hydrogen controls
93
  n_h = st.sidebar.number_input("H: n", 1, 8, 1)
94
  l_h = st.sidebar.number_input("H: l", 0, 7, 0)
95
  m_h = st.sidebar.number_input("H: m", -5, 5, 0)
96
- rmax_bohr = st.sidebar.number_input("H: rmax (Bohr radii)", 10.0, 600.0, 200.0)
 
 
97
  A0 = 5.29177210903e-11
98
  rmax_m = rmax_bohr * A0
99
 
100
- # ===========================
 
 
 
 
 
101
  # THEORY MODE
102
- # ===========================
103
  if mode == "Theory":
 
 
104
  if system == "Particle in a Box (PIB)":
105
  for title, content in PIB_THEORY.items():
106
- st.expander(title).markdown(content, unsafe_allow_html=True)
 
107
  else:
108
  for title, content in HYDRO_THEORY.items():
109
- st.expander(title).markdown(content, unsafe_allow_html=True)
 
110
 
111
- # ===========================
112
- # GRAPHS MODE
113
- # ===========================
114
  elif mode == "Graphs":
115
- if system == "Particle in a Box (PIB)":
 
 
 
116
  x, psi, prob, energy = calc_1d_wavefunction(n_pib, L_pib)
117
 
118
  if dimension == "3D":
119
- st.plotly_chart(plot_probability_3d(n_pib, 1, L_pib, 100), use_container_width=True)
 
120
  else:
121
  fig = go.Figure()
122
- fig.add_trace(go.Scatter(x=x, y=psi))
123
- fig.add_trace(go.Scatter(x=x, y=prob))
124
  st.plotly_chart(fig, use_container_width=True)
125
 
 
 
 
 
 
 
 
126
  st.subheader("Fourier Spectrum")
127
- st.plotly_chart(plot_fourier_spectrum(n_pib, L_pib))
 
 
 
128
 
 
129
  else:
130
  if dimension == "3D":
131
- view = st.selectbox("Visualization", ["Probability", "Orbital", "Nodes", "Phase"])
132
- if view == "Probability":
133
- fig = plot_hydrogen_3d(n_h)
134
- elif view == "Orbital":
135
- fig = plot_hydrogen_orbital(n_h, l_h, m_h)
136
- elif view == "Nodes":
137
- fig = plot_hydrogen_nodes(n_h, l_h, m_h)
138
- else:
139
- fig = plot_hydrogen_phase(n_h, l_h, m_h)
140
- st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
141
  else:
142
- st.plotly_chart(plot_radial_wavefunction(n_h, l_h, rmax_m))
143
- st.plotly_chart(plot_radial_probability(n_h, l_h, rmax_m))
 
 
 
 
 
 
 
 
 
144
 
145
- st.subheader("Energy Levels")
146
- st.plotly_chart(plot_hydrogen_energy_levels(8))
147
 
148
- # ===========================
149
  # ANIMATION MODE
150
- # ===========================
151
- else:
 
 
 
152
  if system == "Particle in a Box (PIB)":
 
153
  if animation_type == "Particle POV":
154
- fig = animate_pib_pov(L_pib, n_pib, speed, dimension)
 
 
 
 
 
 
 
155
  else:
 
 
 
156
  fig = animate_wavefunction_graph_auto(
157
- n_values=list(range(1, 13)), L_values=list(range(10, 80, 5)), frames_per_step=int(60 / speed)
 
 
158
  )
159
- st.plotly_chart(fig, use_container_width=True)
160
 
 
161
  else:
162
- fig = animate_hydrogen_wavefunction(n_h, l_h, m_h, speed, dimension)
 
 
 
 
 
 
 
 
 
163
  st.plotly_chart(fig, use_container_width=True)
 
1
+ import os
2
+ import sys
3
  import streamlit as st
4
 
5
  # --------------------------------------------------------------
6
+ # STREAMLIT CONFIG
7
  # --------------------------------------------------------------
8
+
9
  st.set_page_config(
10
  page_title="Quantum Physics Simulation",
11
  page_icon="⚛️",
12
  layout="wide"
13
  )
14
 
 
 
 
 
 
15
  import numpy as np
16
  import plotly.graph_objects as go
17
 
18
+ st.markdown("""
19
+ <script>
20
+ window.PlotlyConfig = {MathJaxConfig: 'local'};
21
+ </script>
22
+ """, unsafe_allow_html=True)
23
+
24
+ # -------------------------
25
  # Path Setup
26
+ # -------------------------
27
  FILE_DIR = os.path.dirname(os.path.abspath(__file__))
28
+ PROJECT_ROOT = os.path.abspath(os.path.join(FILE_DIR, "..", ".."))
29
  if PROJECT_ROOT not in sys.path:
30
  sys.path.append(PROJECT_ROOT)
31
 
32
+ # -------------------------
33
+ # IMPORTS
34
+ # -------------------------
35
+
36
+ # --- PIB simulation ---
37
  from app.core.simulations.simulation_pib import (
38
  calc_1d_wavefunction,
39
  plot_probability_3d,
 
41
  plot_expectation_values,
42
  )
43
 
44
+ # --- PIB animations ---
45
  from app.core.animations.pib_animation import (
46
  animate_pib_pov,
47
  animate_wavefunction_graph_auto,
48
  )
49
 
50
+ # --- PIB theory ---
51
  from app.core.theory.theory_pib import THEORY_SECTIONS as PIB_THEORY
52
 
53
+ # --- HYDROGEN simulation ---
54
  from app.core.simulations.simulation_hydrogen import (
55
  plot_radial_wavefunction,
56
  plot_radial_probability,
 
61
  plot_hydrogen_phase
62
  )
63
 
64
+ # --- HYDROGEN theory ---
65
  from app.core.theory.theory_hydrogen import THEORY_SECTIONS as HYDRO_THEORY
66
 
67
+ # --- HYDROGEN animation ---
68
  from app.core.animations.hydrogen_animation import (
69
  animate_hydrogen_wavefunction
70
  )
71
 
72
  # --------------------------------------------------------------
73
+ # FIX MATHJAX FOR LATEX
74
  # --------------------------------------------------------------
75
  st.markdown("""
76
  <script>
 
77
  MathJax = {
78
  tex: { inlineMath: [['$', '$'], ['\\\\(', '\\\\)']] }
79
  };
 
81
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
82
  """, unsafe_allow_html=True)
83
 
84
+ st.markdown("""
85
+ <style>
86
+ #MainMenu {visibility: hidden;}
87
+ footer {visibility: hidden;}
88
+ .block-container {padding-top: 1.2rem;}
89
+ </style>
90
+ """, unsafe_allow_html=True)
91
+
92
  # --------------------------------------------------------------
93
+ # SIDEBAR UI
94
  # --------------------------------------------------------------
 
 
 
95
  st.sidebar.title("⚛ Controls")
96
 
97
+ system = st.sidebar.selectbox(
98
+ "Select System",
99
+ ["Particle in a Box (PIB)", "Hydrogen Atom"]
100
+ )
101
+
102
  mode = st.sidebar.radio("Mode", ["Theory", "Graphs", "Animation"])
103
+
104
  dimension = st.sidebar.selectbox("Display Mode", ["2D", "3D"])
105
 
106
+ # --- PIB controls ---
107
+ n_pib = st.sidebar.number_input("PIB: Quantum number n", 1, 12, 1)
108
  L_pib = st.sidebar.number_input("PIB: Box Length L", 1.0, 100.0, 10.0)
109
+
110
+ animation_type = st.sidebar.selectbox(
111
+ "PIB Animation Type",
112
+ ["Particle POV", "Auto Changing (n & L)"]
113
+ )
114
+
115
  speed = st.sidebar.slider("Animation Speed", 0.2, 2.5, 1.0)
116
 
117
+ # --- Hydrogen controls ---
118
  n_h = st.sidebar.number_input("H: n", 1, 8, 1)
119
  l_h = st.sidebar.number_input("H: l", 0, 7, 0)
120
  m_h = st.sidebar.number_input("H: m", -5, 5, 0)
121
+ rmax_bohr = st.sidebar.number_input("H: rmax (Bohr radii)", 10.0, 800.0, 200.0)
122
+
123
+ # Bohr radius in meters
124
  A0 = 5.29177210903e-11
125
  rmax_m = rmax_bohr * A0
126
 
127
+ # --------------------------------------------------------------
128
+ # MAIN TITLE
129
+ # --------------------------------------------------------------
130
+ st.title("⚛ Quantum Physics Simulation")
131
+
132
+ # =========================================================
133
  # THEORY MODE
134
+ # =========================================================
135
  if mode == "Theory":
136
+ st.header("📘 Theory")
137
+
138
  if system == "Particle in a Box (PIB)":
139
  for title, content in PIB_THEORY.items():
140
+ with st.expander(title):
141
+ st.markdown(content, unsafe_allow_html=True)
142
  else:
143
  for title, content in HYDRO_THEORY.items():
144
+ with st.expander(title):
145
+ st.markdown(content, unsafe_allow_html=True)
146
 
147
+ # =========================================================
148
+ # GRAPH MODE
149
+ # =========================================================
150
  elif mode == "Graphs":
151
+ st.header("📊 Graphs & Visualizations")
152
+
153
+ # ------------------ PIB ------------------
154
+ if system == "Particle in a Box (PIB)":
155
  x, psi, prob, energy = calc_1d_wavefunction(n_pib, L_pib)
156
 
157
  if dimension == "3D":
158
+ fig3d = plot_probability_3d(n=n_pib, m=1, L=L_pib, resolution=120)
159
+ st.plotly_chart(fig3d, use_container_width=True)
160
  else:
161
  fig = go.Figure()
162
+ fig.add_trace(go.Scatter(x=x, y=psi, name="ψ(x)"))
163
+ fig.add_trace(go.Scatter(x=x, y=prob, name="|ψ|²"))
164
  st.plotly_chart(fig, use_container_width=True)
165
 
166
+ st.subheader("Energy Levels (n=1–12)")
167
+ ns = np.arange(1, 13)
168
+ energies = (ns ** 2) * np.pi ** 2 / (2 * L_pib ** 2)
169
+ fig_e = go.Figure()
170
+ fig_e.add_trace(go.Scatter(x=ns, y=energies, mode="lines+markers"))
171
+ st.plotly_chart(fig_e, use_container_width=True)
172
+
173
  st.subheader("Fourier Spectrum")
174
+ st.plotly_chart(plot_fourier_spectrum(n_pib, L_pib), use_container_width=True)
175
+
176
+ st.subheader("Expectation Values")
177
+ st.plotly_chart(plot_expectation_values(n_pib, L_pib), use_container_width=True)
178
 
179
+ # ------------------ HYDROGEN ------------------
180
  else:
181
  if dimension == "3D":
182
+ st.subheader("3D Hydrogen Visualizations")
183
+
184
+ view_mode = st.selectbox(
185
+ "Visualization Type",
186
+ ["Probability Density", "Orbital Shape", "Nodes", "Phase"]
187
+ )
188
+
189
+ try:
190
+ if view_mode == "Probability Density":
191
+ fig3d = plot_hydrogen_3d(n_h)
192
+ elif view_mode == "Orbital Shape":
193
+ fig3d = plot_hydrogen_orbital(n_h, l_h, m_h)
194
+ elif view_mode == "Nodes":
195
+ fig3d = plot_hydrogen_nodes(n_h, l_h, m_h)
196
+ else:
197
+ fig3d = plot_hydrogen_phase(n_h, l_h, m_h)
198
+
199
+ st.plotly_chart(fig3d, use_container_width=True)
200
+
201
+ except Exception as e:
202
+ st.error(f"Hydrogen 3D plot failed: {e}")
203
+
204
  else:
205
+ st.subheader("Radial Wavefunction")
206
+ st.plotly_chart(
207
+ plot_radial_wavefunction(n_h, l_h, rmax_m),
208
+ use_container_width=True
209
+ )
210
+
211
+ st.subheader("Radial Probability Distribution")
212
+ st.plotly_chart(
213
+ plot_radial_probability(n_h, l_h, rmax_m),
214
+ use_container_width=True
215
+ )
216
 
217
+ st.subheader("Energy Levels (n=1–8)")
218
+ st.plotly_chart(plot_hydrogen_energy_levels(max_n=8), use_container_width=True)
219
 
220
+ # =========================================================
221
  # ANIMATION MODE
222
+ # =========================================================
223
+ elif mode == "Animation":
224
+ st.header("🎞 Animation")
225
+
226
+ # ----------- PIB -----------
227
  if system == "Particle in a Box (PIB)":
228
+
229
  if animation_type == "Particle POV":
230
+ fig = animate_pib_pov(
231
+ L=L_pib,
232
+ n=n_pib,
233
+ speed=speed,
234
+ display_mode=dimension
235
+ )
236
+ st.plotly_chart(fig, use_container_width=True)
237
+
238
  else:
239
+ n_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
240
+ L_values = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80]
241
+
242
  fig = animate_wavefunction_graph_auto(
243
+ n_values=n_values,
244
+ L_values=L_values,
245
+ frames_per_step=int(60 / speed)
246
  )
247
+ st.plotly_chart(fig, use_container_width=True)
248
 
249
+ # ----------- HYDROGEN -----------
250
  else:
251
+ st.subheader("Hydrogen Wavefunction Animation")
252
+
253
+ fig = animate_hydrogen_wavefunction(
254
+ n=n_h,
255
+ l=l_h,
256
+ m=m_h,
257
+ speed=speed,
258
+ display_mode=dimension
259
+ )
260
+
261
  st.plotly_chart(fig, use_container_width=True)