File size: 3,239 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import annotations
import numpy as np
from typing import Callable, Optional
from .constants import HBAR, MASS_ELECTRON


class TDSESolver:
    def __init__(

        self,

        x: np.ndarray,

        potential_fn: Callable[[np.ndarray], np.ndarray],

        mass: float = MASS_ELECTRON,

        method: str = "crank-nicolson",

    ):
        self.x = x
        self.dx = x[1] - x[0]
        self.V = potential_fn(x)
        self.mass = mass
        self.method = method.lower()

        self._setup_kinetic_operator()

    # ------------------------------------------------------
    # Kinetic operator (finite difference)
    # ------------------------------------------------------
    def _setup_kinetic_operator(self):
        N = len(self.x)
        coeff = -HBAR**2 / (2 * self.mass * self.dx**2)

        diag = np.ones(N) * -2
        off = np.ones(N - 1)
        laplacian = np.diag(diag) + np.diag(off, 1) + np.diag(off, -1)

        self.H = coeff * laplacian + np.diag(self.V)

    # ------------------------------------------------------
    def evolve(

        self,

        psi: np.ndarray,

        dt: float,

        steps: int,

        callback: Optional[Callable[[int, np.ndarray], None]] = None,

    ) -> np.ndarray:

        if self.method == "crank-nicolson":
            return self._evolve_crank_nicolson(psi, dt, steps, callback)
        elif self.method == "split-step":
            return self._evolve_split_step(psi, dt, steps, callback)

        raise ValueError(f"Unknown TDSE method: {self.method}")

    # ------------------------------------------------------
    # Crank–Nicolson
    # ------------------------------------------------------
    def _evolve_crank_nicolson(self, psi, dt, steps, callback):
        N = len(psi)
        I = np.identity(N)

        A = I + (1j * dt / (2 * HBAR)) * self.H
        B = I - (1j * dt / (2 * HBAR)) * self.H
        A_inv = np.linalg.inv(A)

        for step in range(steps):
            psi = A_inv @ (B @ psi)
            psi /= np.sqrt(np.sum(np.abs(psi)**2) * self.dx)

            if callback and step % 10 == 0:
                callback(step, psi)

        return psi

    # ------------------------------------------------------
    # Split-step Fourier
    # ------------------------------------------------------
    def _evolve_split_step(self, psi, dt, steps, callback):
        N = len(psi)
        k = 2 * np.pi * np.fft.fftfreq(N, d=self.dx)

        expV = np.exp(-1j * self.V * dt / (2 * HBAR))
        expK = np.exp(-1j * (HBAR * k**2) / (2 * self.mass) * dt / HBAR)

        for step in range(steps):
            psi *= expV
            psi_k = np.fft.fft(psi)
            psi_k *= expK
            psi = np.fft.ifft(psi_k)
            psi *= expV

            psi /= np.sqrt(np.sum(np.abs(psi)**2) * self.dx)

            if callback and step % 10 == 0:
                callback(step, psi)

        return psi

    # ------------------------------------------------------
    def energy_expectation(self, psi):
        return np.real(np.vdot(psi, self.H @ psi)) * self.dx

    def get_potential(self):
        return self.V