File size: 2,864 Bytes
05354e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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