Spaces:
Sleeping
Sleeping
Download solvers.py from j25aiml013/physics-simulation: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/spaces/j25aiml013/physics-simulation/resolve/main/solvers.py
- Command line
-
hf download hf://spaces/j25aiml013/physics-simulation/solvers.py
-
curl -L -o solvers.py https://huggingface.co/spaces/j25aiml013/physics-simulation/resolve/main/solvers.py
2.86 kB
| # app/core/physics_engine/solvers.py | |
| import numpy as np | |
| import scipy.sparse as sp | |
| import scipy.sparse.linalg as spla | |
| from typing import Callable, Optional | |
| class TDSESolver: | |
| """ | |
| Simple 1D Crank-Nicolson TDSE solver for psi evolution on a uniform grid. | |
| Usage: | |
| solver = TDSESolver(x=grid_x, potential_fn=V_fn, mass=1.0, hbar=1.0) | |
| psi_next = solver.evolve(psi_current, dt=0.01, steps=1) | |
| Notes: | |
| - This uses dimensionless units by default (ħ=1, m=1); adjust if you want physical units. | |
| - The solver assumes Dirichlet boundary conditions psi=0 at ends (infinite well). | |
| """ | |
| def __init__(self, x: np.ndarray, potential_fn: Optional[Callable[[np.ndarray], np.ndarray]] = None, mass: float = 1.0, hbar: float = 1.0): | |
| self.x = x | |
| self.N = len(x) | |
| self.dx = x[1] - x[0] | |
| self.mass = mass | |
| self.hbar = hbar | |
| if potential_fn is None: | |
| self.V = np.zeros_like(x) | |
| else: | |
| self.V = potential_fn(x).astype(float) | |
| # Build second derivative operator (tridiagonal Laplacian) | |
| dx2 = self.dx ** 2 | |
| main = -2.0 * np.ones(self.N) / dx2 | |
| off = 1.0 * np.ones(self.N - 1) / dx2 | |
| lap = sp.diags([off, main, off], offsets=[-1, 0, 1], format="csc") | |
| # Hamiltonian H = -(ħ^2 / 2m) * Lap + V (diagonal) | |
| kinetic = -(self.hbar ** 2) / (2.0 * self.mass) * lap | |
| potential = sp.diags(self.V, 0, format="csc") | |
| self.H = kinetic + potential | |
| # For boundary conditions (Dirichlet psi=0): we'll not evolve edges (keep them zero) by manipulating matrices | |
| # Pre-factor for Crank-Nicolson: (I + i dt H / 2ħ) and (I - i dt H / 2ħ) | |
| self._I = sp.eye(self.N, format="csc") | |
| def _build_matrices(self, dt: float): | |
| i_factor = 1j * dt / (2.0 * self.hbar) | |
| A = (self._I - i_factor * self.H).tocsc() | |
| B = (self._I + i_factor * self.H).tocsc() | |
| # Force Dirichlet by zeroing first and last rows/cols | |
| A = A.tolil() | |
| B = B.tolil() | |
| for idx in (0, self.N - 1): | |
| A[idx, :] = 0; A[:, idx] = 0; A[idx, idx] = 1.0 | |
| B[idx, :] = 0; B[:, idx] = 0; B[idx, idx] = 1.0 | |
| return A.tocsc(), B.tocsc() | |
| def evolve(self, psi: np.ndarray, dt: float = 0.01, steps: int = 1) -> np.ndarray: | |
| """ | |
| Evolve psi by 'steps' iterations of time-step dt using Crank-Nicolson. | |
| """ | |
| psi = psi.astype(np.complex128).copy() | |
| A, B = self._build_matrices(dt) | |
| # Pre-factor LU decomposition of A for speed | |
| lu = spla.factorized(A) | |
| for _ in range(steps): | |
| rhs = B.dot(psi) | |
| # enforce Dirichlet boundaries in rhs | |
| rhs[0] = 0.0 | |
| rhs[-1] = 0.0 | |
| psi = lu(rhs) | |
| return psi | |