-
Notifications
You must be signed in to change notification settings - Fork 3
/
Hamiltonian_solver_FDM_3D.py
214 lines (132 loc) · 4.91 KB
/
Hamiltonian_solver_FDM_3D.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from scipy.sparse.linalg import eigs
from scipy.sparse import diags, dia_matrix
import open3d as o3d
def makeSphereWellMatrix (n, inW, outW):
well_matrix = np.empty((n, n, n))
#00100
#01110
#00100
for i in range(0, n):
for j in range(0, n):
for k in range(0, n):
if math.dist([i, j, k], [(n-1)/2, (n-1)/2, (n-1)/2]) <= (n-1)/2:
well_matrix[i][j][k] = inW
else:
well_matrix[i][j][k] = outW
return well_matrix
def tetrahedron (radius = 1, translation = 0):
v1 = [0, 0, 0]
v2 = [1, 1, 0]
v3 = [0, 1, 1]
v4 = [1, 0, 1]
points = np.array([v1, v2, v3, v4])
points[:, 0] = radius * points[:,0] + translation[0]
points[:, 1] = radius * points[:,1] + translation[1]
points[:, 2] = radius * points[:,2] + translation[2]
return points
def PointInTetrahedron(arr, p):
a = (SameSide(arr[0], arr[1], arr[2], arr[3], p) and
SameSide(arr[1], arr[2], arr[3], arr[0], p) and
SameSide(arr[2], arr[3], arr[0], arr[1], p) and
SameSide(arr[3], arr[0], arr[1], arr[2], p) )
return a
def SameSide(v1, v2, v3, v4, p):
normal = np.cross(v2 - v1, v3 - v1)
dotV4 = np.dot(normal, v4 - v1)
dotP = np.dot(normal, p - v1)
s1 = math.copysign(1, dotV4)
s2 = math.copysign(1, dotP)
return s1 == s2
def makeTetrahedronWellMatrix (n, inW, outW):
well_matrix = np.empty((n, n, n))
tetr = tetrahedron(n - 20, (0, 0, 0))
#00100
#01110
#00100
for i in range(0, n):
for j in range(0, n):
for k in range(0, n):
if PointInTetrahedron(tetr, (i, j, k)):
well_matrix[i][j][k] = inW
else:
well_matrix[i][j][k] = outW
#well_matrix[0][0][0] = 1
return well_matrix
def general_potential_3d(matrixWell3D, N, Elevels):
position_mesh = np.matrix.flatten( matrixWell3D )
No_points = N**3
x_intervals = np.linspace(0, 1, N)
increment = pow(x_intervals[1], 2)
incrementValue = -1/increment
zeroV = 6 / increment
diagmNN = [incrementValue * position_mesh[i] for i in range(0, No_points - N**2 )]
diagmN = [incrementValue * position_mesh[i] for i in range(0, No_points - N )]
diagm1 = [incrementValue * position_mesh[i] for i in range(0, No_points - 1 )]
diag0 = [ zeroV for i in range(0, No_points )]
diagp1 = [incrementValue * position_mesh[i] for i in range(0, No_points - 1 )]
diagpN = [incrementValue * position_mesh[i] for i in range(0, No_points - N )]
diagpNN = [incrementValue * position_mesh[i] for i in range(0, No_points - N**2 )]
diagsK = [-N*N, -N, -1, 0, 1, N, N*N]
diagsV = [diagmNN, diagmN, diagm1, diag0, diagp1, diagpN, diagpNN]
Hamiltonian = diags(diagsV, diagsK, format = 'dia')
print('Hamiltonian done')
################################################################################
#Hamiltonian.tocsr()
e_values, e_vec = eigs(Hamiltonian, k = Elevels )
print('All Hamiltonian done')
################################################################################
return [e_values, e_vec]
def displayVec (vectorToImage):
plot = plt.imshow( vectorToImage, cmap='nipy_spectral')
#plot = plt.imshow( vectorToImage, cmap='nipy_spectral', interpolation='gaussian')
plt.show()
plt.close()
def toList(arr, n):
temp = []
for i in range(0, n):
for j in range(0, n):
for k in range(0, n):
if arr[i][j][k] >= 0.00001:
color = 1 - arr[i][j][k]
temp.append([i, j, k])
return np.array(temp)
def run():
print('start')
level_to_show = 11
Elevels = 25
N = 50
#mesh = makeSphereWellMatrix(N, 1, 0)
mesh = makeTetrahedronWellMatrix(N, 1, 0)
e_values, e_vec = general_potential_3d(mesh, N, Elevels)
if 1:
np.save('data_E_vectors_Tetrahedron' + str(N) +'x'+ str(N) +'x'+ str(N) + 'e' + str(Elevels) , e_vec)
Elevel = pow(np.absolute( e_vec[:, level_to_show].reshape(N, N, N) ), 2)
'''
for i in range(N):
ar = mesh[:,:, i]
#print(ar)
displayVec(ar)
'''
xyz = toList(Elevel, N)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
o3d.visualization.draw_geometries([pcd])
'''
for i in range(0, N):
displayVec(Elevel[:,:, i])
'''
def test():
N = 70
k = makeTetrahedronWellMatrix(N, 1, 0)
xyz = toList(k, N)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
o3d.visualization.draw_geometries([pcd])
if __name__ == '__main__':
run()