-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.py
167 lines (129 loc) · 4.49 KB
/
matrix.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
matrix.py
Definition of the matrices that discretize the Hamiltonians in 1 and
2 dimensions.
Created on: 19-04-2017.
@author: eduardo
"""
import numpy as np
import scipy.sparse as sp
def A1D(numberPoints, potentialFunc, domainStart, domainLength):
"""
Hamiltonian discretization in 1d without boundaries.
Uses Explicit method.
Input:
Number of points to evaluate on (float)
Potential function (vectorised function)
Location where domain starts (float)
Length of domain (float)
Output:
Matrix A (scipy sparse matrix)
"""
h = domainLength/numberPoints # dx
x = np.linspace(domainStart, domainStart + domainLength, numberPoints-1)
v = potentialFunc(x)
a = np.ones(numberPoints-1)*(2+(h**2 * v))
b = np.ones(numberPoints-2)*-1
A = sp.diags(a, 0) + sp.diags(b, 1) + sp.diags(b, -1)
# Periodic boundaries
# A[0,-1] = -1
# A[-1,0] = -1
return (1./h**2) * A
def A1Dfull(numberPoints, potentialFunc, domainStart, domainLength):
"""
Hamiltonian discretization in 1d with Dirichlet boundary conditions.
Uses Explicit method to compute RHS of (A.68) in jos' book.
Here we take hbar = 2m = 1.
Input:
Number of points to evaluate on (float)
Potential function (vectorised function)
Location where domain starts (float)
Length of domain (float)
Output:
Matrix A, the discretised Hamiltonian (scipy sparse matrix)
"""
h = domainLength/numberPoints # dx
x = np.linspace(domainStart, domainStart + domainLength, numberPoints+1)
v = potentialFunc(x)
a = np.ones(numberPoints+1)*(2+(h**2 * v))
b = np.ones(numberPoints)*-1
a[0] = h**2
a[numberPoints] = h**2
b[0] = 0
b[numberPoints-1] = 0
A = sp.diags(a, 0) + sp.diags(b, 1) + sp.diags(b, -1)
return (1./h**2) * A
def A2D(numberPoints, potentialFunc, domainStart, domainLength):
"""
Hamiltonian discretization in 2d without boundaries.
Here we take hbar = 2m = 1.
Input:
Number of points to evaluate in each axis direction (float)
Potential function (vectorised function)
Location where domain starts (tuple)
Length of domain (float)
Output:
Matrix A, the discretised Hamiltonian (scipy sparse matrix)
"""
h = domainLength/numberPoints # dx
o = np.ones(numberPoints-1)
x = np.linspace(domainStart[0], domainStart[0] + domainLength,
numberPoints-1)
y = np.linspace(domainStart[1], domainStart[1] + domainLength,
numberPoints-1)
x = np.kron(x, o)
y = np.kron(o, y)
v = potentialFunc(x, y)
a = np.ones(numberPoints-1)*(2)
b = np.ones(numberPoints-2)*(-1)
Id = np.identity(numberPoints-1)
A1d = sp.diags(a, 0) + sp.diags(b, 1) + sp.diags(b, -1)
A = sp.kron(Id, A1d) + sp.kron(A1d, Id) + h**2 * sp.diags(v, 0)
return (1./h**2) * A
def _Ih(numberPoints):
"""Return Identity matrix of given length with zeros on the extremes."""
Id = np.ones(numberPoints+1)
Id[0] = 0
Id[numberPoints] = 0
return sp.diags(Id)
def _Th(numberPoints, domainLength):
"""Create a section of the matrix A."""
h = domainLength/numberPoints
a = np.ones(numberPoints+1)*4.
b = np.ones(numberPoints)*(-1)
a[0] = h**2
a[numberPoints] = h**2
b[0] = 0
b[numberPoints-1] = 0
T = sp.diags(a, 0) + sp.diags(b, -1) + sp.diags(b, 1)
return T
def A2Dfull(numberPoints, potentialFunc, domainStart, domainLength):
"""Hamiltonian discretization in 2D with dirichlet boundary conditions."""
h = domainLength/numberPoints
a = np.ones(numberPoints+1)
b = np.ones(numberPoints)*(-1)
b2 = np.zeros(numberPoints+1)
a[0] = 0
a[numberPoints] = 0
b[0] = 0
b[numberPoints-1] = 0
b2[0] = 1
b2[numberPoints] = 1
Center1 = sp.diags(a, 0)
Center2 = sp.diags(b, 1)
Center3 = sp.diags(b, -1)
Bounds = sp.diags(b2, 0)
T = _Th(numberPoints, domainLength)
Id = _Ih(numberPoints)
I_N = (h**2)*sp.identity(numberPoints+1)
o = np.ones(numberPoints+1)
x = np.linspace(domainStart[0], domainStart[0] + domainLength,
numberPoints+1)
y = np.linspace(domainStart[1], domainStart[1] + domainLength,
numberPoints+1)
x = np.kron(x, o)
y = np.kron(o, y)
v = potentialFunc(x, y)
A = sp.kron(Center1, T) + sp.kron(Center2, Id) + sp.kron(Center3, Id) \
+ sp.kron(Bounds, I_N) + (h**2 * sp.diags(v, 0))
return (1./h**2)*A