Hand-wave / app.py
AhiBucket's picture
Upload app.py with huggingface_hub
2d541d6 verified
import streamlit as st
# ==========================================
# 0. PAGE CONFIGURATION & STYLING
# ==========================================
st.set_page_config(
page_title="Pili-Pili Quantum Solver | Ahilan Kumaresan",
page_icon="🍟",
layout="wide",
initial_sidebar_state="expanded"
)
import numpy as np
import matplotlib.pyplot as plt
import math
import time
try:
import mediapipe as mp
import cv2
import plotly.graph_objects as go
from plotly.subplots import make_subplots
except ImportError as e:
st.error(f"CRITICAL ERROR: Failed to import required libraries. {e}")
st.stop()
# Import physics engine
try:
import functions as f
except ImportError as e:
st.error(f"CRITICAL ERROR: Failed to import physics engine. {e}")
st.stop()
# ==========================================
# 0. SESSION STATE (for camera flow)
# ==========================================
if 'countdown_finished' not in st.session_state:
st.session_state.countdown_finished = False
if 'V_user_defined' not in st.session_state:
st.session_state.V_user_defined = None
# Custom CSS for a professional look
st.markdown("""
<style>
.main {
background-color: #0e1117;
}
.stButton>button {
width: 100%;
border-radius: 5px;
height: 3em;
background-color: #262730;
color: white;
border: 1px solid #4b4b4b;
}
.stButton>button:hover {
border-color: #00ADB5;
color: #00ADB5;
}
h1, h2, h3 {
color: #00ADB5;
font-family: 'Helvetica Neue', sans-serif;
}
</style>
""", unsafe_allow_html=True)
# ==========================================
# 1. SIDEBAR: PERSONALIZATION & NAV
# ==========================================
with st.sidebar:
st.title("Quantum Solver 2.0")
st.caption("v2.1 - HF Fix")
st.markdown("---")
# Navigation
page = st.radio("Navigation", ["Simulator", "Benchmarks & Verification", "Theory & Method"])
st.markdown("---")
# Author Profile
st.markdown("### About Moi")
st.markdown("""
**Ahilan Kumaresan**
*Aspiring Mathematical & Computational Physicist*
Developing Interative and accurate numerical tools for quantum mechanics.
""")
st.info("Verified against Analytical Solutions & QMSolve Package.")
# ==========================================
# 2. HELPER FUNCTIONS (Plotting)
# ==========================================
def plot_interactive(E, psi, V, x, nos=5):
"""
Creates a professional interactive Plotly chart for wavefunctions and energy levels.
"""
# Limit states
states = min(nos, len(E))
# Create subplots: Main plot (Potential + Psi) and Side plot (Energy Levels)
fig = make_subplots(
rows=1, cols=2,
column_widths=[0.8, 0.2],
shared_yaxes=True,
horizontal_spacing=0.02,
subplot_titles=("Wavefunctions & Potential", "Energy Spectrum")
)
# Scaling factor for wavefunctions
if len(E) >= 2:
scale = (E[1] - E[0]) * 0.4
else:
scale = max(E[0] * 0.1, 0.5)
max_E = E[states-1] if states > 0 else 10
window_height = max_E * 1.5
# Get x coordinates for internal points (matching psi dimensions)
x_internal = x[1:-1]
V_internal = V[1:-1]
# 1. Plot Potential V(x) - using internal points for better visibility
V_clipped = np.clip(V_internal, 0, window_height)
fig.add_trace(
go.Scatter(
x=x_internal.tolist() if hasattr(x_internal, 'tolist') else x_internal,
y=V_clipped.tolist() if hasattr(V_clipped, 'tolist') else V_clipped,
mode='lines',
name='V(x)',
line=dict(color='#FFFFFF', width=2.5),
hovertemplate='V(x): %{y:.2f}<extra></extra>'
),
row=1, col=1
)
# 2. Plot Wavefunctions (shifted by Energy)
colors = ['#00ADB5', '#FF2E63', '#F38181', '#FCE38A', '#EAFFD0',
'#95E1D3', '#FFB6C1', '#DDA0DD', '#87CEEB', '#98FB98']
for n in range(states):
# Normalize wavefunction amplitude
psi_n = psi[:, n]
max_amp = np.max(np.abs(psi_n))
if max_amp > 1e-9:
psi_n = psi_n / max_amp
else:
psi_n = psi_n
# Shift by energy
y_shifted = psi_n * scale + E[n]
# Hide where potential is infinite
y_shifted[V_internal > 1e5] = np.nan
color = colors[n % len(colors)]
# Ensure arrays match in length
if len(x_internal) != len(y_shifted):
# Fallback: truncate to minimum length
min_len = min(len(x_internal), len(y_shifted))
x_plot = x_internal[:min_len]
y_plot = y_shifted[:min_len]
else:
x_plot = x_internal
y_plot = y_shifted
fig.add_trace(
go.Scatter(
x=x_plot.tolist() if hasattr(x_plot, 'tolist') else x_plot,
y=y_plot.tolist() if hasattr(y_plot, 'tolist') else y_plot,
mode='lines',
name=f'n={n+1}, E={E[n]:.4f}',
line=dict(color=color, width=2),
hovertemplate=f'n={n+1}<br>E={E[n]:.4f}<br>x: %{{x:.2f}}<br>ψ: %{{y:.2f}}<extra></extra>'
),
row=1, col=1
)
# Add Energy Level to Side Bar
fig.add_trace(
go.Scatter(
x=[0, 1], y=[E[n], E[n]],
mode='lines',
line=dict(color=color, width=3),
showlegend=False,
hovertemplate=f'E_{n+1}={E[n]:.4f}<extra></extra>'
),
row=1, col=2
)
# Layout Styling - Enhanced dark mode
fig.update_layout(
template="plotly_dark",
height=600,
margin=dict(l=20, r=20, t=50, b=20),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
font=dict(size=10)
),
hovermode="closest",
plot_bgcolor='#0e1117',
paper_bgcolor='#0e1117',
font=dict(color='#FAFAFA')
)
fig.update_xaxes(
title_text="Position (a.u.)",
row=1, col=1,
gridcolor='#2a2a2a',
showgrid=True
)
fig.update_xaxes(
showticklabels=False,
row=1, col=2,
showgrid=False
)
fig.update_yaxes(
title_text="Energy (Hartree)",
range=[0, max_E * 1.2],
row=1, col=1,
gridcolor='#2a2a2a',
showgrid=True
)
return fig
# ==========================================
# 3. HELPER: MediaPipe hand → 1D potential
# ==========================================
def process_frame_to_potential(frame):
"""
Takes a BGR frame (OpenCV) and returns:
pot_profile: 1D array in [0,1] representing V(x) profile
msg: human-friendly label
Modes:
- 2 hands → Square well (0 inside, 1 outside)
- 1 hand → QHO-like parabola
"""
try:
mp_hands = mp.solutions.hands
with mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.5) as hands:
h, w, _ = frame.shape
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
res = hands.process(rgb)
if not res.multi_hand_landmarks:
return None, "No Hands Detected, But Cute Smile :)"
# --- LOGIC: Square Well vs QHO ---
# 1. Square Well (2 Hands)
if len(res.multi_hand_landmarks) >= 2:
INDEX_TIP_ID = 8
x_coords = [lm.landmark[INDEX_TIP_ID].x * w for lm in res.multi_hand_landmarks]
x_coords.sort()
xL_hand, xR_hand = x_coords[0], x_coords[1]
well_width = xR_hand - xL_hand
center_screen = w / 2
centered_L = center_screen - (well_width / 2)
centered_R = center_screen + (well_width / 2)
x_space = np.linspace(0, w, 400)
pot_profile = np.ones_like(x_space)
pot_profile[(x_space > centered_L) & (x_space < centered_R)] = 0
return pot_profile, "Square Well (Captured)"
# 2. Harmonic Oscillator (1 Hand)
elif len(res.multi_hand_landmarks) == 1:
lm = res.multi_hand_landmarks[0]
THUMB = lm.landmark[4]
INDEX = lm.landmark[8]
dx = INDEX.x - THUMB.x
dy = INDEX.y - THUMB.y
dist = math.sqrt(dx**2 + dy**2)
# Map pinch distance → curvature
A = np.interp(dist, [0.05, 0.3], [100.0, 1.0])
x_space = np.linspace(-1, 1, 400)
pot_profile = A * (x_space**2)
pot_profile = np.clip(pot_profile, 0, 100)
pot_profile = pot_profile / 100.0 # normalize 0..1
return pot_profile, f"Harmonic Oscillator (k={A:.1f})"
except Exception as e:
return None, f"MediaPipe Error: {e}"
return None, "Error"
# ==========================================
# 4. PAGE: SIMULATOR
# ==========================================
if page == "Simulator":
st.title("Pili-Pili - Quantum Potential Solver")
st.markdown("Show a potential with your hands or select a preset to solve the **Time-Independent Schrödinger Equation**.")
# Shared grid for all modes
L = 50
N_GRID = 1000
x_full, dx, x_internal = f.make_grid(L, N_GRID)
V_full_to_solve = None
status_msg = ""
col1, col2 = st.columns([1, 3])
with col1:
st.subheader("Controls")
# Settings
potential_mode = st.selectbox(
"Potential Type",
[
"Static Square Well",
"Static Harmonic Oscillator",
"Double Well",
"Hand Gesture (Camera)"
]
)
nos_user = st.slider("Eigenstates to Plot", 1, 10, 5)
# ---- STATIC MODES ----
if potential_mode == "Static Square Well":
width = st.slider("Well Width", 1.0, 20.0, 10.0)
V_physics = np.zeros_like(x_internal)
V_physics[np.abs(x_internal) > width/2] = 200
V_full_to_solve = np.pad(V_physics, (1,1), constant_values=1e10)
status_msg = f"Static Square Well (width = {width:.1f})"
elif potential_mode == "Static Harmonic Oscillator":
k = st.slider("Spring Constant (k)", 0.1, 50.0, 5.0)
V_physics = 0.5 * k * x_internal**2
# scale a bit so it shows nicely under energies
V_physics = V_physics / np.max(V_physics) * 50
V_full_to_solve = np.pad(V_physics, (1,1), constant_values=1e10)
status_msg = f"Static Harmonic Oscillator (k = {k:.2f})"
elif potential_mode == "Double Well":
sep = st.slider("Separation", 0.5, 5.0, 2.0)
depth = st.slider("Depth", 0.1, 5.0, 1.0)
V_physics = depth * ((x_internal**2 - sep**2)**2)
V_physics = V_physics / np.max(V_physics) * 50
V_full_to_solve = np.pad(V_physics, (1,1), constant_values=1e10)
status_msg = f"Double Well (sep = {sep:.2f}, depth = {depth:.2f})"
# ---- HAND-GESTURE / CAMERA MODE ----
elif potential_mode == "Hand Gesture (Camera)":
st.subheader("Hand Gesture Controls")
st.info(
"1. Click **'Start Countdown'**. (IGNORE)\n"
"2. Get your **two hands** ready for a Square Well, "
"or **one-hand pinch** for a Harmonic Oscillator.\n"
"3. When you'r ready, use **'Take a snapshot'**."
)
st.subheader("Hand Gesture Input")
img_file = st.camera_input("Take a Snapshot")
if img_file:
file_bytes = np.asarray(bytearray(img_file.read()), dtype=np.uint8)
frame = cv2.imdecode(file_bytes, 1)
frame = cv2.flip(frame, 1)
V_raw, msg = process_frame_to_potential(frame)
if V_raw is not None:
st.success(f"Detected: {msg}")
st.session_state.V_user_defined = V_raw
# Map to simulation grid
V_interpolated = np.interp(
np.linspace(0, 1, len(x_internal)),
np.linspace(0, 1, len(V_raw)),
V_raw
)
V_physics = V_interpolated * 200.0
V_full_to_solve = np.pad(V_physics, (1,1), constant_values=1e10)
status_msg = f"Camera Potential: {msg}"
else:
st.error(msg)
# --------- RIGHT COLUMN: SOLVE & PLOT ----------
with col2:
if V_full_to_solve is not None:
start_time = time.time()
T = f.kinetic_operator(len(x_internal), dx)
E, psi = f.solve(T, V_full_to_solve, dx)
solve_time = time.time() - start_time
if status_msg:
st.markdown(f"**Potential:** {status_msg}")
st.markdown(f"**Solver Status:** ✅ Converged in {solve_time:.3f} s")
fig = plot_interactive(E, psi, V_full_to_solve, x_full, nos=nos_user)
st.plotly_chart(fig, use_container_width=True)
# Eigenenergies panel
st.markdown("### Eigenenergies")
cols = st.columns(nos_user)
for i in range(nos_user):
if i < len(E):
cols[i].metric(f"n={i}", f"{E[i]:.4f} Ha")
else:
if potential_mode == "Hand Gesture (Camera)":
st.info("Follow the instructions on the left to capture a potential from your hands.")
else:
st.info("Select parameters on the left to generate a potential and solve.")
# ==========================================
# 5. PAGE: BENCHMARKS
# ==========================================
elif page == "Benchmarks & Verification":
st.title("🛡️ Verification & Accuracy")
st.markdown("""
This solver has been rigorously tested against known analytical solutions and external libraries to ensure physical accuracy.
""")
tab1, tab2, tab3 = st.tabs(["Analytical Benchmarks", "QMSolve Comparison", "Code"])
with tab1:
st.subheader("1. Infinite Square Well")
st.markdown("Particle in a box of length $L=20$. Error < 0.003%.")
st.table({
"State (n)": [1, 2, 3, 4, 5],
"Analytic E": [0.012337, 0.049348, 0.111033, 0.197392, 0.308425],
"Numerical E": [0.012337, 0.049348, 0.111032, 0.197389, 0.308419],
"% Error": ["0.0001%", "0.0003%", "0.0007%", "0.0013%", "0.0021%"]
})
st.subheader("2. Harmonic Oscillator")
st.markdown("Standard QHO with $k=1$. Error < 0.02%.")
st.table({
"State (n)": [0, 1, 2, 3, 4],
"Analytic E": [0.5, 1.5, 2.5, 3.5, 4.5],
"Numerical E": [0.499980, 1.499902, 2.499746, 3.499512, 4.499200],
"% Error": ["0.0039%", "0.0065%", "0.0101%", "0.0139%", "0.0178%"]
})
with tab2:
st.subheader("Cross-Verification: Double Well Potential")
st.markdown("""
Comparison with the Python package `QMSolve` for a Double Well potential (no simple analytic solution).
**Agreement within 0.25%**.
""")
col_a, col_b = st.columns(2)
with col_a:
st.markdown("**Parameters:** $V(x) = 2(x^2 - 1)^2$")
st.table({
"State (n)": [0, 1, 2, 3, 4],
"psi_solve2 (Ha)": [1.400886, 2.092533, 4.455252, 6.917808, 9.872632],
"QMSolve (Ha)": [1.402472, 2.097767, 4.466368, 6.936807, 9.900227],
"% Difference": ["0.11%", "0.25%", "0.25%", "0.27%", "0.28%"]
})
with col_b:
st.info("Note: QMSolve uses eV units. Results were converted to Hartree (1 Ha ≈ 27.211 eV) for comparison.")
with tab3:
st.subheader("Code Verification")
st.code("""
def kinetic_operator(N, dx, hbar=1, m=1):
# 3-point central difference stencil for 2nd derivative
main_diagonal = (1/dx**2) * np.diag(-2 * np.ones(N))
off_diagonal1 = (1/dx**2) * np.diag(np.ones(N-1), -1)
off_diagonal2 = (1/dx**2) * np.diag(np.ones(N-1), 1)
D2 = (main_diagonal + off_diagonal1 + off_diagonal2)
# Kinetic Energy Operator T = -hbar^2 / 2m * d^2/dx^2
T = (-(hbar**2 / (2*m)) * D2)
return T
""", language="python")
st.code("""
def harmonic(x,k,center=0.0):
# A Parabola, setting the global k-value.
global Last_k_value
Last_k_value = k
constant_factor = 1
potential = 0.5*k*(x - center)**2
return constant_factor * potential
""")
# ==========================================
# 6. PAGE: THEORY
# ==========================================
elif page == "Theory & Method":
st.title("📖 Theory & Methodology")
st.markdown("### The Time-Independent Schrödinger Equation")
st.latex(r" \hat{H}\psi(x) = E\psi(x) ")
st.latex(r" \left[ -\frac{\hbar^2}{2m}\frac{d^2}{dx^2} + V(x) \right]\psi(x) = E\psi(x) ")
st.markdown("### Numerical Method: Finite Difference")
st.markdown(r"""
We discretize the spatial domain $x$ into a grid of $N$ points. The second derivative is approximated using the **Central Difference Formula**:
""")
st.latex(r" \frac{d^2\psi}{dx^2} \approx \frac{\psi_{i+1} - 2\psi_i + \psi_{i-1}}{\Delta x^2} ")
st.markdown(r"""
This transforms the differential operator into a **Tridiagonal Matrix** equation:
""")
st.latex(r" \mathbf{H}\mathbf{\psi} = E\mathbf{\psi} ")
st.markdown(r"""
Where $\mathbf{H}$ is an $N \times N$ matrix. We then use `numpy.linalg.eigh` to solve for the eigenvalues ($E$) and eigenvectors ($\psi$).
""")
st.markdown("### Implementation Details")
st.markdown(r"""
- **Grid Size:** Dynamic (default 1000–2000 points)
- **Boundary Conditions:** Dirichlet ($ \psi(0) = \psi(L) = 0 $) via infinite walls at grid edges.
- **Units:** Hartree Atomic Units ($\hbar=1, m=1$).
""")