jupyterlab-codex-sidebar 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.github/workflows/unit-tests.yml +27 -0
  3. package/.jupyterlab-playwright.log +0 -0
  4. package/README.md +83 -9
  5. package/docs/images/codex-sidebar-screenshot.png +0 -0
  6. package/jupyterlab_codex/handlers.py +938 -297
  7. package/jupyterlab_codex/labextension/package.json +13 -3
  8. package/jupyterlab_codex/labextension/static/525.224526d045c727069de6.js +2 -0
  9. package/jupyterlab_codex/labextension/static/737.e7de3ad9dd6ded798340.js +1 -0
  10. package/jupyterlab_codex/labextension/static/remoteEntry.6ef5e7167763a316c000.js +1 -0
  11. package/jupyterlab_codex/protocol.py +297 -0
  12. package/jupyterlab_codex/runner.py +58 -15
  13. package/jupyterlab_codex/sessions.py +582 -97
  14. package/lib/codexChat.d.ts +13 -0
  15. package/lib/codexChat.js +2506 -0
  16. package/lib/codexChat.js.map +1 -0
  17. package/lib/codexChatAttachmentDedup.d.ts +10 -0
  18. package/lib/codexChatAttachmentDedup.js +35 -0
  19. package/lib/codexChatAttachmentDedup.js.map +1 -0
  20. package/lib/codexChatAttachmentLimit.d.ts +18 -0
  21. package/lib/codexChatAttachmentLimit.js +50 -0
  22. package/lib/codexChatAttachmentLimit.js.map +1 -0
  23. package/lib/codexChatAttachmentState.d.ts +15 -0
  24. package/lib/codexChatAttachmentState.js +16 -0
  25. package/lib/codexChatAttachmentState.js.map +1 -0
  26. package/lib/codexChatDocumentUtils.d.ts +70 -0
  27. package/lib/codexChatDocumentUtils.js +506 -0
  28. package/lib/codexChatDocumentUtils.js.map +1 -0
  29. package/lib/codexChatFormatting.d.ts +11 -0
  30. package/lib/codexChatFormatting.js +83 -0
  31. package/lib/codexChatFormatting.js.map +1 -0
  32. package/lib/codexChatNotice.d.ts +3 -0
  33. package/lib/codexChatNotice.js +74 -0
  34. package/lib/codexChatNotice.js.map +1 -0
  35. package/lib/codexChatPersistence.d.ts +35 -0
  36. package/lib/codexChatPersistence.js +158 -0
  37. package/lib/codexChatPersistence.js.map +1 -0
  38. package/lib/codexChatPrimitives.d.ts +44 -0
  39. package/lib/codexChatPrimitives.js +156 -0
  40. package/lib/codexChatPrimitives.js.map +1 -0
  41. package/lib/codexChatRender.d.ts +24 -0
  42. package/lib/codexChatRender.js +293 -0
  43. package/lib/codexChatRender.js.map +1 -0
  44. package/lib/codexChatSessionFactory.d.ts +15 -0
  45. package/lib/codexChatSessionFactory.js +45 -0
  46. package/lib/codexChatSessionFactory.js.map +1 -0
  47. package/lib/codexChatSessionKey.d.ts +3 -0
  48. package/lib/codexChatSessionKey.js +14 -0
  49. package/lib/codexChatSessionKey.js.map +1 -0
  50. package/lib/codexChatStorage.d.ts +4 -0
  51. package/lib/codexChatStorage.js +37 -0
  52. package/lib/codexChatStorage.js.map +1 -0
  53. package/lib/codexSessionResolver.d.ts +12 -0
  54. package/lib/codexSessionResolver.js +38 -0
  55. package/lib/codexSessionResolver.js.map +1 -0
  56. package/lib/handlers/activitySummarizer.d.ts +15 -0
  57. package/lib/handlers/activitySummarizer.js +327 -0
  58. package/lib/handlers/activitySummarizer.js.map +1 -0
  59. package/lib/handlers/codexMessageTypes.d.ts +30 -0
  60. package/lib/handlers/codexMessageTypes.js +2 -0
  61. package/lib/handlers/codexMessageTypes.js.map +1 -0
  62. package/lib/handlers/codexMessageUtils.d.ts +46 -0
  63. package/lib/handlers/codexMessageUtils.js +144 -0
  64. package/lib/handlers/codexMessageUtils.js.map +1 -0
  65. package/lib/handlers/handleCodexSocketMessage.d.ts +107 -0
  66. package/lib/handlers/handleCodexSocketMessage.js +78 -0
  67. package/lib/handlers/handleCodexSocketMessage.js.map +1 -0
  68. package/lib/handlers/sessionSyncHandler.d.ts +34 -0
  69. package/lib/handlers/sessionSyncHandler.js +181 -0
  70. package/lib/handlers/sessionSyncHandler.js.map +1 -0
  71. package/lib/hooks/useCodexSocket.d.ts +15 -0
  72. package/lib/hooks/useCodexSocket.js +84 -0
  73. package/lib/hooks/useCodexSocket.js.map +1 -0
  74. package/lib/index.js +1 -1
  75. package/lib/index.js.map +1 -1
  76. package/lib/panel.d.ts +1 -11
  77. package/lib/panel.js +1 -2815
  78. package/lib/panel.js.map +1 -1
  79. package/lib/protocol.d.ts +235 -0
  80. package/lib/protocol.js +278 -0
  81. package/lib/protocol.js.map +1 -0
  82. package/package.json +13 -3
  83. package/playwright.config.cjs +27 -0
  84. package/playwright.unit.config.cjs +19 -0
  85. package/pyproject.toml +1 -1
  86. package/release.sh +52 -14
  87. package/scripts/run_playwright_e2e.sh +96 -0
  88. package/scripts/run_playwright_freeze_repro.sh +58 -0
  89. package/scripts/run_playwright_queue_repro.sh +60 -0
  90. package/scripts/run_playwright_repro.sh +55 -0
  91. package/src/codexChat.tsx +3914 -0
  92. package/src/codexChatAttachmentDedup.ts +47 -0
  93. package/src/codexChatAttachmentLimit.ts +81 -0
  94. package/src/codexChatAttachmentState.ts +37 -0
  95. package/src/codexChatDocumentUtils.ts +644 -0
  96. package/src/codexChatFormatting.ts +94 -0
  97. package/src/codexChatNotice.ts +95 -0
  98. package/src/codexChatPersistence.ts +191 -0
  99. package/src/codexChatPrimitives.tsx +446 -0
  100. package/src/codexChatRender.tsx +376 -0
  101. package/src/codexChatSessionFactory.ts +79 -0
  102. package/src/codexChatSessionKey.ts +16 -0
  103. package/src/codexChatStorage.ts +36 -0
  104. package/src/codexSessionResolver.ts +56 -0
  105. package/src/handlers/activitySummarizer.ts +369 -0
  106. package/src/handlers/codexMessageTypes.ts +34 -0
  107. package/src/handlers/codexMessageUtils.ts +217 -0
  108. package/src/handlers/handleCodexSocketMessage.ts +204 -0
  109. package/src/handlers/sessionSyncHandler.ts +308 -0
  110. package/src/hooks/useCodexSocket.ts +109 -0
  111. package/src/index.ts +1 -1
  112. package/src/panel.tsx +1 -4184
  113. package/src/protocol.ts +582 -0
  114. package/style/index.css +480 -11
  115. package/test-results/.last-run.json +4 -0
  116. package/test.py +0 -0
  117. package/tests/e2e/cell-output-error-tail.spec.js +156 -0
  118. package/tests/e2e/codex-ui-test-helpers.js +138 -0
  119. package/tests/e2e/fixtures/notebooks/error-output-tail.ipynb +58 -0
  120. package/tests/e2e/fixtures/notebooks/error-output-tail.py +19 -0
  121. package/tests/e2e/fixtures/notebooks/tab1.ipynb +322 -0
  122. package/tests/e2e/fixtures/notebooks/tab1.py +272 -0
  123. package/tests/e2e/fixtures/notebooks/tab2.ipynb +252 -0
  124. package/tests/e2e/fixtures/notebooks/tab2.py +231 -0
  125. package/tests/e2e/fixtures/notebooks/tab3.ipynb +403 -0
  126. package/tests/e2e/fixtures/notebooks/tab3.py +331 -0
  127. package/tests/e2e/fixtures/notebooks/tab4.py +339 -0
  128. package/tests/e2e/freeze-notebook-tabs-repro.spec.js +295 -0
  129. package/tests/e2e/mock-codex-cli-flood.py +127 -0
  130. package/tests/e2e/mock-codex-cli-prompt-echo.py +88 -0
  131. package/tests/e2e/mock-codex-cli.py +95 -0
  132. package/tests/e2e/queue-multitab-repro.spec.js +189 -0
  133. package/tests/test_handlers.py +116 -0
  134. package/tests/test_protocol.py +169 -0
  135. package/tests/test_session_store_limits.py +50 -0
  136. package/tests/unit/codexChatAttachmentDedup.spec.ts +56 -0
  137. package/tests/unit/codexChatAttachmentLimit.spec.ts +57 -0
  138. package/tests/unit/codexChatAttachmentState.spec.ts +71 -0
  139. package/tests/unit/codexChatDocumentUtils.spec.ts +63 -0
  140. package/tests/unit/codexChatLimit.spec.ts +18 -0
  141. package/tests/unit/codexChatNotice.spec.ts +45 -0
  142. package/tests/unit/codexChatPersistence.spec.ts +199 -0
  143. package/tests/unit/codexChatSessionFactory.spec.ts +94 -0
  144. package/tests/unit/codexChatSessionKey.spec.ts +18 -0
  145. package/tests/unit/codexMessageUtils.spec.ts +89 -0
  146. package/tests/unit/codexSessionResolver.spec.ts +92 -0
  147. package/tests/unit/handleCodexSocketMessage.spec.ts +476 -0
  148. package/tsconfig.tsbuildinfo +1 -1
  149. package/webpack.config.js +6 -0
  150. package/jupyterlab_codex/labextension/static/504.335f3447c84ba3d74517.js +0 -2
  151. package/jupyterlab_codex/labextension/static/972.8e856719e40acc1ef4cb.js +0 -1
  152. package/jupyterlab_codex/labextension/static/remoteEntry.a2982f776a1f0f515640.js +0 -1
  153. /package/jupyterlab_codex/labextension/static/{504.335f3447c84ba3d74517.js.LICENSE.txt → 525.224526d045c727069de6.js.LICENSE.txt} +0 -0
@@ -0,0 +1,403 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "tab3-cell",
7
+ "metadata": {
8
+ "lines_to_next_cell": 1
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "import numpy as np\n",
13
+ "import matplotlib.pyplot as plt"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": null,
19
+ "id": "ee23365d",
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "# 1D time-dependent Schrodinger equation simulation (hbar = m = 1).\n",
24
+ "# Method: split-operator (FFT), which keeps time evolution numerically stable.\n",
25
+ "\n",
26
+ "def split_operator_step(psi_state, phase_v_half, phase_t):\n",
27
+ " # One split-operator update: V/2 -> T -> V/2\n",
28
+ " psi_state = phase_v_half * psi_state\n",
29
+ " psi_k_state = np.fft.fft(psi_state)\n",
30
+ " psi_k_state *= phase_t\n",
31
+ " psi_state = np.fft.ifft(psi_k_state)\n",
32
+ " psi_state = phase_v_half * psi_state\n",
33
+ " return psi_state"
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": null,
39
+ "id": "0f816d05",
40
+ "metadata": {},
41
+ "outputs": [],
42
+ "source": [
43
+ "# Simulation helpers\n",
44
+ "def _validate_simulation_params(\n",
45
+ " hbar, mass, grid_size, x_min, x_max, dt, steps, save_every, sigma, barrier_width, barrier_region\n",
46
+ "):\n",
47
+ " if grid_size <= 0:\n",
48
+ " raise ValueError(\"grid_size must be positive.\")\n",
49
+ " if x_max <= x_min:\n",
50
+ " raise ValueError(\"x_max must be greater than x_min.\")\n",
51
+ " if dt <= 0:\n",
52
+ " raise ValueError(\"dt must be positive.\")\n",
53
+ " if steps < 0:\n",
54
+ " raise ValueError(\"steps must be non-negative.\")\n",
55
+ " if save_every <= 0:\n",
56
+ " raise ValueError(\"save_every must be positive.\")\n",
57
+ " if mass <= 0:\n",
58
+ " raise ValueError(\"mass must be positive.\")\n",
59
+ " if hbar <= 0:\n",
60
+ " raise ValueError(\"hbar must be positive.\")\n",
61
+ " if sigma <= 0:\n",
62
+ " raise ValueError(\"sigma must be positive.\")\n",
63
+ " if barrier_width <= 0:\n",
64
+ " raise ValueError(\"barrier_width must be positive.\")\n",
65
+ " if barrier_region < 0:\n",
66
+ " raise ValueError(\"barrier_region must be non-negative.\")\n",
67
+ "\n",
68
+ "\n",
69
+ "def _initialize_system(\n",
70
+ " hbar, mass, grid_size, x_min, x_max, dt, barrier_height, barrier_width, x0, sigma, k0\n",
71
+ "):\n",
72
+ " x = np.linspace(x_min, x_max, grid_size, endpoint=False)\n",
73
+ " dx = x[1] - x[0]\n",
74
+ " k = 2.0 * np.pi * np.fft.fftfreq(grid_size, d=dx)\n",
75
+ "\n",
76
+ " V = barrier_height * np.exp(-(x / barrier_width) ** 2)\n",
77
+ " psi = np.exp(-((x - x0) ** 2) / (2.0 * sigma**2)) * np.exp(1j * k0 * x)\n",
78
+ " psi /= np.sqrt(np.sum(np.abs(psi) ** 2) * dx)\n",
79
+ "\n",
80
+ " phase_v_half = np.exp(-1j * V * dt / (2.0 * hbar))\n",
81
+ " T_k = (hbar**2) * (k**2) / (2.0 * mass)\n",
82
+ " phase_t = np.exp(-1j * T_k * dt / hbar)\n",
83
+ " return x, dx, k, V, psi, phase_v_half, phase_t\n",
84
+ "\n",
85
+ "\n",
86
+ "def _record_observables(psi, x, dx, k, hbar):\n",
87
+ " density = np.abs(psi) ** 2\n",
88
+ " norm = np.sum(density) * dx\n",
89
+ "\n",
90
+ " x_mean = np.sum(x * density) * dx\n",
91
+ " x2_mean = np.sum((x**2) * density) * dx\n",
92
+ " psi_k = np.fft.fft(psi)\n",
93
+ " p_psi = np.fft.ifft(hbar * k * psi_k)\n",
94
+ " p2_psi = np.fft.ifft((hbar * k) ** 2 * psi_k)\n",
95
+ " p_mean = np.real(np.sum(np.conj(psi) * p_psi) * dx)\n",
96
+ " p2_mean = np.real(np.sum(np.conj(psi) * p2_psi) * dx)\n",
97
+ "\n",
98
+ " x_var = max(x2_mean - x_mean**2, 0.0)\n",
99
+ " p_var = max(p2_mean - p_mean**2, 0.0)\n",
100
+ " uncertainty = np.sqrt(x_var) * np.sqrt(p_var)\n",
101
+ " return density, norm, x_mean, p_mean, uncertainty\n",
102
+ "\n",
103
+ "\n",
104
+ "def _compute_diagnostics(final_density, x, dx, barrier_region, uncertainty_history):\n",
105
+ " reflection = np.sum(final_density[x < -barrier_region]) * dx\n",
106
+ " transmission = np.sum(final_density[x > barrier_region]) * dx\n",
107
+ " near_barrier = np.sum(final_density[np.abs(x) <= barrier_region]) * dx\n",
108
+ "\n",
109
+ " return {\n",
110
+ " \"final_norm\": np.sum(final_density) * dx,\n",
111
+ " \"reflection\": reflection,\n",
112
+ " \"transmission\": transmission,\n",
113
+ " \"near_barrier\": near_barrier,\n",
114
+ " \"probability_sum\": reflection + transmission + near_barrier,\n",
115
+ " \"min_uncertainty\": float(np.min(uncertainty_history)),\n",
116
+ " }"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "id": "c896a561",
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": [
126
+ "# Main simulation\n",
127
+ "def simulate_1d_quantum_scattering(\n",
128
+ " hbar=1.0,\n",
129
+ " mass=1.0,\n",
130
+ " grid_size=2048,\n",
131
+ " x_min=-100.0,\n",
132
+ " x_max=100.0,\n",
133
+ " dt=0.05,\n",
134
+ " steps=900,\n",
135
+ " save_every=90,\n",
136
+ " barrier_height=1.6,\n",
137
+ " barrier_width=2.2,\n",
138
+ " x0=-35.0,\n",
139
+ " sigma=3.5,\n",
140
+ " k0=1.6,\n",
141
+ " barrier_region=6.0,\n",
142
+ "):\n",
143
+ " _validate_simulation_params(\n",
144
+ " hbar,\n",
145
+ " mass,\n",
146
+ " grid_size,\n",
147
+ " x_min,\n",
148
+ " x_max,\n",
149
+ " dt,\n",
150
+ " steps,\n",
151
+ " save_every,\n",
152
+ " sigma,\n",
153
+ " barrier_width,\n",
154
+ " barrier_region,\n",
155
+ " )\n",
156
+ " x, dx, k, V, psi, phase_v_half, phase_t = _initialize_system(\n",
157
+ " hbar, mass, grid_size, x_min, x_max, dt, barrier_height, barrier_width, x0, sigma, k0\n",
158
+ " )\n",
159
+ "\n",
160
+ " snapshots = []\n",
161
+ " times = []\n",
162
+ " norm_history = []\n",
163
+ " x_mean_history = []\n",
164
+ " p_mean_history = []\n",
165
+ " uncertainty_history = []\n",
166
+ "\n",
167
+ " for n in range(steps + 1):\n",
168
+ " if n % save_every == 0:\n",
169
+ " density, norm, x_mean, p_mean, uncertainty = _record_observables(psi, x, dx, k, hbar)\n",
170
+ " snapshots.append(density.copy())\n",
171
+ " times.append(n * dt)\n",
172
+ " norm_history.append(norm)\n",
173
+ " x_mean_history.append(x_mean)\n",
174
+ " p_mean_history.append(p_mean)\n",
175
+ " uncertainty_history.append(uncertainty)\n",
176
+ "\n",
177
+ " psi = split_operator_step(psi, phase_v_half, phase_t)\n",
178
+ "\n",
179
+ " final_density = np.abs(psi) ** 2\n",
180
+ " diagnostics = _compute_diagnostics(final_density, x, dx, barrier_region, uncertainty_history)\n",
181
+ "\n",
182
+ " return {\n",
183
+ " \"x\": x,\n",
184
+ " \"V\": V,\n",
185
+ " \"snapshots\": snapshots,\n",
186
+ " \"times\": times,\n",
187
+ " \"norm_history\": norm_history,\n",
188
+ " \"x_mean_history\": x_mean_history,\n",
189
+ " \"p_mean_history\": p_mean_history,\n",
190
+ " \"uncertainty_history\": uncertainty_history,\n",
191
+ " \"diagnostics\": diagnostics,\n",
192
+ " }"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "execution_count": null,
198
+ "id": "5c97768b",
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": [
202
+ "# Plot helpers\n",
203
+ "def _plot_density_panel(ax, x, snapshots, times, V):\n",
204
+ " for density, t in zip(snapshots, times):\n",
205
+ " ax.plot(x, density, label=f\"t={t:.1f}\")\n",
206
+ " vmax = np.max(V)\n",
207
+ " if vmax > 0:\n",
208
+ " scale = np.max(snapshots[0]) / vmax\n",
209
+ " ax.plot(x, V * scale, \"--\", linewidth=2, label=\"Barrier (scaled)\")\n",
210
+ " ax.set_title(\"1D Quantum Wave Packet Scattering\")\n",
211
+ " ax.set_xlabel(\"x\")\n",
212
+ " ax.set_ylabel(r\"Probability density $|\\psi|^2$\")\n",
213
+ " ax.legend()\n",
214
+ " ax.grid(alpha=0.25)\n",
215
+ "\n",
216
+ "\n",
217
+ "def _plot_norm_panel(ax, times, norm_history):\n",
218
+ " ax.plot(times, norm_history, marker=\"o\")\n",
219
+ " ax.set_title(\"Normalization Conservation Check\")\n",
220
+ " ax.set_xlabel(\"time\")\n",
221
+ " ax.set_ylabel(\"Integral |psi|^2 dx\")\n",
222
+ " ax.grid(alpha=0.25)\n",
223
+ "\n",
224
+ "\n",
225
+ "def _plot_expectation_panel(ax, times, x_mean_history, p_mean_history):\n",
226
+ " ax.plot(times, x_mean_history, marker=\"o\", label=\"<x>\")\n",
227
+ " ax.plot(times, p_mean_history, marker=\"s\", label=\"<p>\")\n",
228
+ " ax.set_title(\"Expectation Values Over Time\")\n",
229
+ " ax.set_xlabel(\"time\")\n",
230
+ " ax.set_ylabel(\"value\")\n",
231
+ " ax.legend()\n",
232
+ " ax.grid(alpha=0.25)\n",
233
+ "\n",
234
+ "\n",
235
+ "def _plot_uncertainty_panel(ax, times, uncertainty_history, hbar):\n",
236
+ " ax.plot(times, uncertainty_history, marker=\"^\", label=\"DxDp\")\n",
237
+ " ax.axhline(0.5 * hbar, linestyle=\"--\", color=\"red\", label=\"hbar/2\")\n",
238
+ " ax.set_title(\"Uncertainty Principle Check\")\n",
239
+ " ax.set_xlabel(\"time\")\n",
240
+ " ax.set_ylabel(\"DxDp\")\n",
241
+ " ax.legend()\n",
242
+ " ax.grid(alpha=0.25)"
243
+ ]
244
+ },
245
+ {
246
+ "cell_type": "code",
247
+ "execution_count": null,
248
+ "id": "0e71e915",
249
+ "metadata": {},
250
+ "outputs": [],
251
+ "source": [
252
+ "def plot_density_3d(result):\n",
253
+ " x = result[\"x\"]\n",
254
+ " times = np.array(result[\"times\"])\n",
255
+ " snapshots = np.array(result[\"snapshots\"])\n",
256
+ "\n",
257
+ " X, T = np.meshgrid(x, times)\n",
258
+ " fig = plt.figure(figsize=(10, 6))\n",
259
+ " ax = fig.add_subplot(111, projection=\"3d\")\n",
260
+ " ax.plot_surface(X, T, snapshots, cmap=\"viridis\", linewidth=0, antialiased=True)\n",
261
+ " ax.set_title(\"Probability Density Surface\")\n",
262
+ " ax.set_xlabel(\"x\")\n",
263
+ " ax.set_ylabel(\"time\")\n",
264
+ " ax.set_zlabel(r\"$|\\psi|^2$\")\n",
265
+ " plt.tight_layout()\n",
266
+ " plt.show()"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": null,
272
+ "id": "fba6b3b6",
273
+ "metadata": {},
274
+ "outputs": [],
275
+ "source": [
276
+ "# Plot entrypoint\n",
277
+ "def plot_results(result, hbar=1.0):\n",
278
+ " x = result[\"x\"]\n",
279
+ " V = result[\"V\"]\n",
280
+ " snapshots = result[\"snapshots\"]\n",
281
+ " times = result[\"times\"]\n",
282
+ " norm_history = result[\"norm_history\"]\n",
283
+ " x_mean_history = result[\"x_mean_history\"]\n",
284
+ " p_mean_history = result[\"p_mean_history\"]\n",
285
+ " uncertainty_history = result[\"uncertainty_history\"]\n",
286
+ " fig, axes = plt.subplots(4, 1, figsize=(10, 14), sharex=False)\n",
287
+ "\n",
288
+ " _plot_density_panel(axes[0], x, snapshots, times, V)\n",
289
+ " _plot_norm_panel(axes[1], times, norm_history)\n",
290
+ " _plot_expectation_panel(axes[2], times, x_mean_history, p_mean_history)\n",
291
+ " _plot_uncertainty_panel(axes[3], times, uncertainty_history, hbar)\n",
292
+ "\n",
293
+ " plt.tight_layout()\n",
294
+ " plt.show()\n",
295
+ " plot_density_3d(result)"
296
+ ]
297
+ },
298
+ {
299
+ "cell_type": "code",
300
+ "execution_count": null,
301
+ "id": "3fe0dff8",
302
+ "metadata": {},
303
+ "outputs": [],
304
+ "source": [
305
+ "def validate_diagnostics(diagnostics, hbar=1.0, tol=1e-3):\n",
306
+ " \"\"\"Return validation checks for core physical constraints.\"\"\"\n",
307
+ " uncertainty_bound = 0.5 * hbar\n",
308
+ " reflection = diagnostics[\"reflection\"]\n",
309
+ " transmission = diagnostics[\"transmission\"]\n",
310
+ " near_barrier = diagnostics[\"near_barrier\"]\n",
311
+ " finite_values = np.all(\n",
312
+ " np.isfinite(\n",
313
+ " [\n",
314
+ " diagnostics[\"final_norm\"],\n",
315
+ " reflection,\n",
316
+ " transmission,\n",
317
+ " near_barrier,\n",
318
+ " diagnostics[\"probability_sum\"],\n",
319
+ " diagnostics[\"min_uncertainty\"],\n",
320
+ " uncertainty_bound,\n",
321
+ " ]\n",
322
+ " )\n",
323
+ " )\n",
324
+ " checks = {\n",
325
+ " \"diagnostics_finite\": bool(finite_values),\n",
326
+ " \"norm_close_to_one\": bool(abs(diagnostics[\"final_norm\"] - 1.0) <= tol),\n",
327
+ " \"probability_conserved\": bool(abs(diagnostics[\"probability_sum\"] - 1.0) <= tol),\n",
328
+ " \"probability_terms_nonnegative\": bool(\n",
329
+ " reflection >= -tol and transmission >= -tol and near_barrier >= -tol\n",
330
+ " ),\n",
331
+ " \"probability_terms_le_one\": bool(\n",
332
+ " reflection <= 1.0 + tol\n",
333
+ " and transmission <= 1.0 + tol\n",
334
+ " and near_barrier <= 1.0 + tol\n",
335
+ " ),\n",
336
+ " \"uncertainty_respected\": bool(diagnostics[\"min_uncertainty\"] + tol >= uncertainty_bound),\n",
337
+ " }\n",
338
+ " checks[\"all_passed\"] = all(checks.values())\n",
339
+ " return checks"
340
+ ]
341
+ },
342
+ {
343
+ "cell_type": "code",
344
+ "execution_count": null,
345
+ "id": "0b574c28",
346
+ "metadata": {},
347
+ "outputs": [],
348
+ "source": [
349
+ "def main():\n",
350
+ " hbar = 1.0\n",
351
+ " result = simulate_1d_quantum_scattering(hbar=hbar)\n",
352
+ " diag = result[\"diagnostics\"]\n",
353
+ " checks = validate_diagnostics(diag, hbar=hbar)\n",
354
+ "\n",
355
+ " print(f\"Final normalization (should be ~1): {diag['final_norm']:.6f}\")\n",
356
+ " print(f\"Reflection probability : {diag['reflection']:.6f}\")\n",
357
+ " print(f\"Transmission probability : {diag['transmission']:.6f}\")\n",
358
+ " print(f\"Near-barrier probability : {diag['near_barrier']:.6f}\")\n",
359
+ " print(f\"R + T + Near-barrier : {diag['probability_sum']:.6f}\")\n",
360
+ " print(\n",
361
+ " \"Min uncertainty DxDp : \"\n",
362
+ " f\"{diag['min_uncertainty']:.6f} (>= {0.5 * hbar:.3f})\"\n",
363
+ " )\n",
364
+ " print(f\"Validation all passed : {checks['all_passed']}\")\n",
365
+ "\n",
366
+ " for name, passed in checks.items():\n",
367
+ " if name == \"all_passed\":\n",
368
+ " continue\n",
369
+ " print(f\" - {name:25}: {passed}\")\n",
370
+ "\n",
371
+ " plot_results(result, hbar=hbar)\n",
372
+ "\n",
373
+ "\n",
374
+ "if __name__ == \"__main__\":\n",
375
+ " main()"
376
+ ]
377
+ }
378
+ ],
379
+ "metadata": {
380
+ "jupytext": {
381
+ "formats": "ipynb,py:percent"
382
+ },
383
+ "kernelspec": {
384
+ "display_name": "Python 3 (ipykernel)",
385
+ "language": "python",
386
+ "name": "python3"
387
+ },
388
+ "language_info": {
389
+ "codemirror_mode": {
390
+ "name": "ipython",
391
+ "version": 3
392
+ },
393
+ "file_extension": ".py",
394
+ "mimetype": "text/x-python",
395
+ "name": "python",
396
+ "nbconvert_exporter": "python",
397
+ "pygments_lexer": "ipython3",
398
+ "version": "3.13.9"
399
+ }
400
+ },
401
+ "nbformat": 4,
402
+ "nbformat_minor": 5
403
+ }