-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschrodingersim.py
101 lines (82 loc) · 2.61 KB
/
schrodingersim.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Run a Schrodinger simulation and collects the results
Authors:
- Andrea Maiani <[email protected]>
- Ciro Pentangelo <[email protected]>
"""
import numpy as np
from schrodinger import Schrodinger
class SchrodingerSimulation:
"""
Class which run the spit-step engine and collects the results
"""
def __init__(self, Nx, dx, t0, Nt, dt, psi_x0, normalize=False, simname=""):
"""
Parameters
----------
Nx : integer
Number of cells of the spatial lattice
dx : float
Length of the spatial cell
t0 : float
Initial time
Nt : float
Number of time steps
dt : float
Width of time step
psi_x0: numpy array
Initial wavefuncion
normalize: bool
Normalization at each step
simname: string
Simulation name (for saving results and plotting)
"""
self.Nx = Nx
self.dx = dx
self.x = self.dx * (np.arange(self.Nx) - 0.5 * self.Nx)
self.Nt = Nt
self.dt = dt
self.t = t0 + self.dt * np.arange(self.Nt)
self.k = None
self.psi_x = np.zeros((Nt, Nx), dtype=np.complex128)
self.psi_k = np.zeros((Nt, Nx), dtype=np.complex128)
self.V_x = np.zeros((Nt, Nx), dtype=np.complex128)
self.psi_norm = np.zeros(Nt)
psi_x0 = np.asarray(psi_x0)
assert psi_x0.shape == (self.Nx,)
self.psi_x[0] = psi_x0
self.wf = None
self.normalize = normalize
self.simname = simname
def t2i(self, t):
"""
Converts the time t into an index i for accessing the arrays
"""
return np.int((t - self.t[0]) / self.dt)
def set_potential(self, V_x):
"""
Set the potential
"""
V_x = np.asarray(V_x)
assert V_x.shape == (self.Nt, self.Nx)
self.V_x = V_x
def set_static_potential(self, V_x):
"""
Set a static potential
"""
assert V_x.shape == (1, self.Nx)
self.V_x = np.repeat(V_x, self.Nt, axis=0)
def run(self):
"""
Run the simulation
"""
self.wf = Schrodinger(self.x, self.psi_x[0], self.V_x[0], k0=-25, t0=self.t[0])
self.k = self.wf.k
for i in range(int(self.Nt)):
self.wf.V_x = self.V_x[i]
self.wf.time_step(self.dt, Nsteps=1, normalize=self.normalize)
self.psi_x[i] = self.wf.psi_x
self.psi_k[i] = self.wf.psi_k
self.psi_norm[i] = self.wf.norm