Ahilan Kumaresan commited on
Commit ·
3a2ef98
1
Parent(s): 7a95c88
Fix: Improve plot display - show potential V(x) clearly, enhance dark mode, match original colors
Browse files
app.py
CHANGED
|
@@ -100,34 +100,35 @@ def plot_interactive(E, psi, V, x, nos=5):
|
|
| 100 |
|
| 101 |
# Scaling factor for wavefunctions
|
| 102 |
if len(E) >= 2:
|
| 103 |
-
scale = (E[1] - E[0]) * 0.
|
| 104 |
else:
|
| 105 |
-
scale =
|
| 106 |
|
| 107 |
max_E = E[states-1] if states > 0 else 10
|
| 108 |
window_height = max_E * 1.5
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
fig.add_trace(
|
| 114 |
go.Scatter(
|
| 115 |
-
x=
|
| 116 |
y=V_clipped.tolist() if hasattr(V_clipped, 'tolist') else V_clipped,
|
| 117 |
mode='lines',
|
| 118 |
-
name='
|
| 119 |
-
line=dict(color='
|
| 120 |
-
|
| 121 |
-
fillcolor='rgba(255, 255, 255, 0.1)'
|
| 122 |
),
|
| 123 |
row=1, col=1
|
| 124 |
)
|
| 125 |
|
| 126 |
# 2. Plot Wavefunctions (shifted by Energy)
|
| 127 |
-
colors = ['#00ADB5', '#FF2E63', '#F38181', '#FCE38A', '#EAFFD0'
|
| 128 |
-
|
| 129 |
-
# Get x coordinates for internal points (matching psi dimensions)
|
| 130 |
-
x_internal = x[1:-1]
|
| 131 |
|
| 132 |
for n in range(states):
|
| 133 |
# Normalize wavefunction amplitude
|
|
@@ -135,9 +136,14 @@ def plot_interactive(E, psi, V, x, nos=5):
|
|
| 135 |
max_amp = np.max(np.abs(psi_n))
|
| 136 |
if max_amp > 1e-9:
|
| 137 |
psi_n = psi_n / max_amp
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# Shift by energy
|
| 140 |
y_shifted = psi_n * scale + E[n]
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
color = colors[n % len(colors)]
|
| 143 |
|
|
@@ -156,9 +162,9 @@ def plot_interactive(E, psi, V, x, nos=5):
|
|
| 156 |
x=x_plot.tolist() if hasattr(x_plot, 'tolist') else x_plot,
|
| 157 |
y=y_plot.tolist() if hasattr(y_plot, 'tolist') else y_plot,
|
| 158 |
mode='lines',
|
| 159 |
-
name=f'
|
| 160 |
line=dict(color=color, width=2),
|
| 161 |
-
|
| 162 |
),
|
| 163 |
row=1, col=1
|
| 164 |
)
|
|
@@ -170,23 +176,48 @@ def plot_interactive(E, psi, V, x, nos=5):
|
|
| 170 |
mode='lines',
|
| 171 |
line=dict(color=color, width=3),
|
| 172 |
showlegend=False,
|
| 173 |
-
|
| 174 |
),
|
| 175 |
row=1, col=2
|
| 176 |
)
|
| 177 |
|
| 178 |
-
# Layout Styling
|
| 179 |
fig.update_layout(
|
| 180 |
template="plotly_dark",
|
| 181 |
height=600,
|
| 182 |
margin=dict(l=20, r=20, t=50, b=20),
|
| 183 |
-
legend=dict(
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
)
|
| 186 |
|
| 187 |
-
fig.update_xaxes(
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
return fig
|
| 192 |
|
|
|
|
| 100 |
|
| 101 |
# Scaling factor for wavefunctions
|
| 102 |
if len(E) >= 2:
|
| 103 |
+
scale = (E[1] - E[0]) * 0.4
|
| 104 |
else:
|
| 105 |
+
scale = max(E[0] * 0.1, 0.5)
|
| 106 |
|
| 107 |
max_E = E[states-1] if states > 0 else 10
|
| 108 |
window_height = max_E * 1.5
|
| 109 |
|
| 110 |
+
# Get x coordinates for internal points (matching psi dimensions)
|
| 111 |
+
x_internal = x[1:-1]
|
| 112 |
+
V_internal = V[1:-1]
|
| 113 |
+
|
| 114 |
+
# 1. Plot Potential V(x) - using internal points for better visibility
|
| 115 |
+
V_clipped = np.clip(V_internal, 0, window_height)
|
| 116 |
|
| 117 |
fig.add_trace(
|
| 118 |
go.Scatter(
|
| 119 |
+
x=x_internal.tolist() if hasattr(x_internal, 'tolist') else x_internal,
|
| 120 |
y=V_clipped.tolist() if hasattr(V_clipped, 'tolist') else V_clipped,
|
| 121 |
mode='lines',
|
| 122 |
+
name='V(x)',
|
| 123 |
+
line=dict(color='#FFFFFF', width=2.5),
|
| 124 |
+
hovertemplate='V(x): %{y:.2f}<extra></extra>'
|
|
|
|
| 125 |
),
|
| 126 |
row=1, col=1
|
| 127 |
)
|
| 128 |
|
| 129 |
# 2. Plot Wavefunctions (shifted by Energy)
|
| 130 |
+
colors = ['#00ADB5', '#FF2E63', '#F38181', '#FCE38A', '#EAFFD0',
|
| 131 |
+
'#95E1D3', '#FFB6C1', '#DDA0DD', '#87CEEB', '#98FB98']
|
|
|
|
|
|
|
| 132 |
|
| 133 |
for n in range(states):
|
| 134 |
# Normalize wavefunction amplitude
|
|
|
|
| 136 |
max_amp = np.max(np.abs(psi_n))
|
| 137 |
if max_amp > 1e-9:
|
| 138 |
psi_n = psi_n / max_amp
|
| 139 |
+
else:
|
| 140 |
+
psi_n = psi_n
|
| 141 |
|
| 142 |
# Shift by energy
|
| 143 |
y_shifted = psi_n * scale + E[n]
|
| 144 |
+
|
| 145 |
+
# Hide where potential is infinite
|
| 146 |
+
y_shifted[V_internal > 1e5] = np.nan
|
| 147 |
|
| 148 |
color = colors[n % len(colors)]
|
| 149 |
|
|
|
|
| 162 |
x=x_plot.tolist() if hasattr(x_plot, 'tolist') else x_plot,
|
| 163 |
y=y_plot.tolist() if hasattr(y_plot, 'tolist') else y_plot,
|
| 164 |
mode='lines',
|
| 165 |
+
name=f'n={n+1}, E={E[n]:.4f}',
|
| 166 |
line=dict(color=color, width=2),
|
| 167 |
+
hovertemplate=f'n={n+1}<br>E={E[n]:.4f}<br>x: %{{x:.2f}}<br>ψ: %{{y:.2f}}<extra></extra>'
|
| 168 |
),
|
| 169 |
row=1, col=1
|
| 170 |
)
|
|
|
|
| 176 |
mode='lines',
|
| 177 |
line=dict(color=color, width=3),
|
| 178 |
showlegend=False,
|
| 179 |
+
hovertemplate=f'E_{n+1}={E[n]:.4f}<extra></extra>'
|
| 180 |
),
|
| 181 |
row=1, col=2
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# Layout Styling - Enhanced dark mode
|
| 185 |
fig.update_layout(
|
| 186 |
template="plotly_dark",
|
| 187 |
height=600,
|
| 188 |
margin=dict(l=20, r=20, t=50, b=20),
|
| 189 |
+
legend=dict(
|
| 190 |
+
orientation="h",
|
| 191 |
+
yanchor="bottom",
|
| 192 |
+
y=1.02,
|
| 193 |
+
xanchor="right",
|
| 194 |
+
x=1,
|
| 195 |
+
font=dict(size=10)
|
| 196 |
+
),
|
| 197 |
+
hovermode="closest",
|
| 198 |
+
plot_bgcolor='#0e1117',
|
| 199 |
+
paper_bgcolor='#0e1117',
|
| 200 |
+
font=dict(color='#FAFAFA')
|
| 201 |
)
|
| 202 |
|
| 203 |
+
fig.update_xaxes(
|
| 204 |
+
title_text="Position (a.u.)",
|
| 205 |
+
row=1, col=1,
|
| 206 |
+
gridcolor='#2a2a2a',
|
| 207 |
+
showgrid=True
|
| 208 |
+
)
|
| 209 |
+
fig.update_xaxes(
|
| 210 |
+
showticklabels=False,
|
| 211 |
+
row=1, col=2,
|
| 212 |
+
showgrid=False
|
| 213 |
+
)
|
| 214 |
+
fig.update_yaxes(
|
| 215 |
+
title_text="Energy (Hartree)",
|
| 216 |
+
range=[0, max_E * 1.2],
|
| 217 |
+
row=1, col=1,
|
| 218 |
+
gridcolor='#2a2a2a',
|
| 219 |
+
showgrid=True
|
| 220 |
+
)
|
| 221 |
|
| 222 |
return fig
|
| 223 |
|