-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavefunction1d.py
141 lines (98 loc) · 2.92 KB
/
wavefunction1d.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 6 15:19:07 2018
@author: kalas
"""
# Construct a matrix which represent the schrodinger equation on matrix form
#
# Initially created by J Robert Johansson, <robert@riken.jp>
# Modified by Wenyuan Zhang, <wzhang@physics.rutgers.edu>
#
import numpy as np
#
# kronecker delta, optionally modify so that it also take the boundary
# conditions into account?
#
def mod_kron(n, m):
return (n == m)
def assemble_K(N, k, x_min, x_max, sparse=False):
"""
Assemble the matrix representation of the kinetic energy contribution
to the Hamiltonian.
k = -hbar**2 / 2 m
"""
dx = (x_min - x_max) / N
K = np.zeros((N,N)).astype(np.complex)
for m in range(0, N):
for n in range(0,N):
K[m, n] = k / (dx ** 2) * (mod_kron(m + 1, n) - 2 * mod_kron(m, n) + mod_kron(m - 1, n))
return K
def assemble_V(N, u, sparse=False):
"""
Assemble the matrix representation of the potential energy contribution
to the Hamiltonian.
"""
V = np.zeros((N,N)).astype(np.complex)
for m in range(N):
for n in range(N):
V[m, n] = u[m] * mod_kron(m, n)
return V
def basis_step_evalute(N, u, x):
"""
"""
return u
def assemble_u_potential(N, u_func, x, args, sparse=False):
"""
"""
return u_func(x, args)
def wavefunction_norm(x, psi):
"""
Calculate the norm of the given wavefunction.
"""
dx = x[1] - x[0]
return (psi.conj() * psi).sum() * dx
def wavefunction_normalize(x, psi):
"""
Normalize the given wavefunction.
"""
return psi / np.sqrt(wavefunction_norm(x, psi))
def expectation_value(x, operator, psi):
"""
Evaluate the expectation value of a given operator and wavefunction.
"""
dx = x[1] - x[0]
return (psi.conj() * operator * psi).sum() * dx
def inner_product(x, psi1, psi2):
"""
Evaluate the inner product of two wavefunctions, psi1 and psi2, on a space
described by X1 and X2.
"""
dx = x[1] - x[0]
return (psi1.conj() * psi2).sum() * dx
def derivative(x, psi):
"""
Evaluate the expectation value of a given operator and wavefunction.
"""
dx = x[1] - x[0]
N = len(psi)
dpsi = np.zeros(N, dtype=np.complex)
def _idx_wrap(M, m):
return m if m < M else m - M
for n in range(N):
dpsi[n] = (psi[_idx_wrap(N, n+1)] - psi[n-1]) / (2 * dx)
return dpsi
def print_matrix(A):
"""
Print real part of matrix matrix to stdout
"""
print('\n'.join([' '.join(['{:.3}'.format(item.real) for item in row])
for row in A]))
def solve_eigenproblem(H):
"""
Solve an eigenproblem and return the eigenvalues and eigenvectors.
"""
vals, vecs = np.linalg.eig(H)
idx = np.real(vals).argsort()
vals = vals[idx]
vecs = vecs.T[idx]
return vals, vecs