-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiftMethod.py
More file actions
176 lines (133 loc) · 6.45 KB
/
shiftMethod.py
File metadata and controls
176 lines (133 loc) · 6.45 KB
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
import numpy as np
from MGCMTStencilMaker import MGCMTStencilMaker
from MGCMTSolver import MGCMTSolver
from MGCMTProcessor import MGCMTProcessor
import scipy.sparse.linalg as sparsela
import scipy.sparse as sparse
import matplotlib.pyplot as plt
import time
# NOTE: EIGSH USES ITERATIVE METHOD !! (Implicitly Restarted Lanczos Method) (KRYLOV METHODE)
# NOTE: HOW TO HANDLE THE SHIFT DURING A VCYCLE OR TWOGRID (Works right now but pay attention to lowest grid level. Possible that eigenvector can't be represented on lowest grid.)
# NOTE: DEFINE A CONVERGENCE CRITERIUM (Define a boundary for residu AND max iterations in case of no convergence)
solver = MGCMTSolver()
stencil_maker = MGCMTStencilMaker()
processor = MGCMTProcessor()
gridsize = 2**8
laplacian = stencil_maker.laplacian(gridsize)
hamiltonian = (-1 / np.pi ** 2) * laplacian
machine_eps = np.finfo(float).eps
tolerance = 10 ** (-4)
num_eigenvalues = 6
gridspacing = 1. / gridsize
x_axis = np.arange(0, 1, gridspacing)
#Exact values
#########################################################
eigenvalues_exact = [i**2 for i in xrange(1, num_eigenvalues + 1)]
eigenvectors_exact = np.zeros((gridsize, num_eigenvalues))
for i in xrange(gridsize):
for j in xrange(num_eigenvalues):
eigenvectors_exact[i, j] = np.sin((j+1) * np.pi * x_axis[i])
for j in xrange(num_eigenvalues):
eigenvectors_exact[:, j] = eigenvectors_exact[:, j] / np.linalg.norm(eigenvectors_exact[:, j])
###########################################################
#Eigs method
############################################
eigenvalues_eigs, v = sparsela.eigsh(hamiltonian, which='SM', k=num_eigenvalues, tol=tolerance)
eigenvectors_eigs = np.zeros((gridsize, num_eigenvalues))
for i, array in enumerate(v):
for j in xrange(num_eigenvalues):
eigenvectors_eigs[i, j] = array[j]
residu_eigs = np.zeros((1, num_eigenvalues))
for i in xrange(gridsize):
for j in xrange(num_eigenvalues):
residu_eigs[0, j] += (eigenvectors_eigs[i, j] - eigenvectors_exact[i, j]) ** 2
###########################################
# Shift method
########################################################
# Generate guesses for eigenvalues to use as shift
bad_gridsize = gridsize / 2
bad_laplacian = stencil_maker.laplacian(bad_gridsize)
bad_hamiltonian = (-1 / np.pi ** 2) * bad_laplacian
eigenvalues_shift = np.zeros((1, num_eigenvalues))
eigenvectors_shift = np.zeros((gridsize, num_eigenvalues))
bad_eigenvalues, bad_eigenvectors = sparsela.eigsh(bad_hamiltonian, k=num_eigenvalues, which='SM', tol=tolerance)
print "Initial guess eigenvalues: ", bad_eigenvalues
bad_eigenvectors = np.array(bad_eigenvectors)
w0 = np.zeros((gridsize, 1))
for i in xrange(num_eigenvalues):
v0 = processor.interpolate(bad_eigenvectors[:, i], stencil_maker, gridsize) # Interpolate eigenvector guess to fine grid
v0 /= np.linalg.norm(v0)
shifted_matrix = hamiltonian - sparse.eye(gridsize) * bad_eigenvalues[i] # Create the shifted matrix A-mu*I
v = v0
for j in xrange(4):
#w = solver.vcycle(w0, v, hamiltonian, stencil_maker, shift=bad_eigenvalues[i])
w = w0
for k in xrange(5):
w = solver.vcycle(w, v, hamiltonian, stencil_maker, shift=bad_eigenvalues[i], lowest_level=16)
#w = solver.twogrid(w, v, hamiltonian, stencil_maker, shift=bad_eigenvalues[i])
#print j, np.linalg.norm(hamiltonian.dot(w)-bad_eigenvalues[i]*w - v)
print j, np.linalg.norm(v - shifted_matrix * w, ord=np.inf)
#w = solver.wjacobi(w0, v, shifted_matrix)
v = w / np.linalg.norm(w)
#eigenvalue = bad_eigenvalues[i] + (w.conj().T.dot(v)) / (v.conj().T.dot(v)) # kind of works, but cant derive it
# Ugly because i can't use np.transpose(v) * hamiltonian * v. Matrix multiplication dies
eigenvalue = np.dot(v.conj().T, hamiltonian.dot(v))
eigenvalues_shift[0, i] = eigenvalue
eigenvectors_shift[:, i] = v[:, 0]
#######################################################################################
print "Eigenvalues Exact: ", eigenvalues_exact
print "Eigenvalues Eigsh: ", eigenvalues_eigs
print "Eigenvalues Shift: ", eigenvalues_shift
fig_exact, axarray = plt.subplots(num_eigenvalues / 2, 2, sharex='col', sharey='row')
row_index = 0
column_index = 0
for i in xrange(num_eigenvalues):
if i != 0:
if i % 2 == 0:
row_index += 1
if column_index > 0:
column_index -= 1
else:
column_index += 1
psi_sq = np.ma.conjugate(eigenvectors_exact[:, i]) * eigenvectors_exact[:, i]
axarray[row_index, column_index].plot(x_axis, psi_sq, c=np.random.rand(3,))
axarray[row_index, column_index].set_title("n = " + str(i+1))
plt.suptitle("Exact")
fig_exact.text(0.5, 0.04, 'x', ha='center', va='center')
fig_exact.text(0.06, 0.5, '|Psi(x)|^2', ha='center', va='center', rotation='vertical')
fig_shift, axarray = plt.subplots(num_eigenvalues / 2, 2, sharex='col', sharey='row')
row_index = 0
column_index = 0
for i in xrange(num_eigenvalues):
if i != 0:
if i % 2 == 0:
row_index += 1
if column_index > 0:
column_index -= 1
else:
column_index += 1
psi_sq = np.ma.conjugate(eigenvectors_shift[:, i]) * eigenvectors_shift[:, i]
axarray[row_index, column_index].plot(x_axis, psi_sq, c=np.random.rand(3,))
axarray[row_index, column_index].set_title("n = " + str(i+1))
plt.suptitle("Shift Method")
fig_shift.text(0.5, 0.04, 'x', ha='center', va='center')
fig_shift.text(0.06, 0.5, '|Psi(x)|^2', ha='center', va='center', rotation='vertical')
# row and column sharing
fig_eigs, axarray = plt.subplots(num_eigenvalues / 2, 2, sharex='col', sharey='row')
row_index = 0
column_index = 0
for i in xrange(num_eigenvalues):
if i != 0:
if i % 2 == 0:
row_index += 1
if column_index > 0:
column_index -= 1
else:
column_index += 1
psi_sq = np.ma.conjugate(eigenvectors_eigs[:, i]) * eigenvectors_eigs[:, i]
axarray[row_index, column_index].plot(x_axis, psi_sq, c=np.random.rand(3,))
axarray[row_index, column_index].set_title("n = " + str(i+1))
plt.suptitle("Eigsh Method")
fig_eigs.text(0.5, 0.04, 'x', ha='center', va='center')
fig_eigs.text(0.06, 0.5, '|Psi(x)|^2', ha='center', va='center', rotation='vertical')
plt.show()