-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
273 lines (219 loc) · 7.12 KB
/
example.py
File metadata and controls
273 lines (219 loc) · 7.12 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import time
import matplotlib.pyplot as plt
import numpy as np
import scipy.linalg as sla
from sempy.derivative import reference_derivative_matrix
from sempy.gradient import (
gradient,
gradient_2d,
gradient_transpose,
gradient_transpose_2d,
)
from sempy.iterative import cg, pcg
from sempy.mass import reference_mass_matrix_1d
from sempy.meshes.box import reference_2d
from sempy.meshes.curved import trapezoid
example_2d = 0
plot_on = 0
elapsed_cg = []
elapsed_fdm = []
niters_cg = []
niters_fdm = []
orders = []
for N in range(2, 10):
n = N + 1
if example_2d:
X, Y = reference_2d(N)
G, J, B = geometric_factors_2d(X, Y, n)
else:
X, Y, Z = trapezoid(N)
G, J, B = geometric_factors(X, Y, Z, n)
def fdm_d_inv_2d(p):
n = p + 1
Bh = reference_mass_matrix_1d(p)
Dh = reference_derivative_matrix(p)
Ah = Dh.T @ Bh @ Dh
Ah = 0.5 * (Ah + Ah.T)
I = np.identity(n, dtype=np.float64)
Ry = I[1:, :]
Ax = Rx @ Ah @ Rx.T
nx = Ax.shape[0]
Ay = Ry @ Ah @ Ry.T
ny = Ay.shape[0]
Bx = Rx @ Bh @ Rx.T
By = Ry @ Bh @ Ry.T
Lx, Sx = sla.eigh(Ax, Bx)
Lx = np.diag(Lx)
Ix = np.identity(nx, dtype=np.float64)
Ly, Sy = sla.eigh(Ay, By)
Ly = np.diag(Ly)
Iy = np.identity(ny, dtype=np.float64)
Lx = Lx.real
Ly = Ly.real
D = np.kron(Iy, Lx) + np.kron(Ly, Ix)
dinv = 1.0 / np.diag(D)
return Rx, Ry, Sx.real, Sy.real, dinv
def fdm_d_inv(p):
n = p + 1
Bh = reference_mass_matrix_1d(p)
Dh = reference_derivative_matrix(p)
Ah = Dh.T @ Bh @ Dh
Ah = 0.5 * (Ah + Ah.T)
I = np.identity(n, dtype=np.float64)
Rx = Rz = I
Ry = I[1:, :]
Ax = Rx @ Ah @ Rx.T
nx = Ax.shape[0]
Ay = Ry @ Ah @ Ry.T
ny = Ay.shape[0]
Az = Rz @ Ah @ Rz.T
nz = Az.shape[0]
Bx = Rx @ Bh @ Rx.T
By = Ry @ Bh @ Ry.T
Bz = Rz @ Bh @ Rz.T
Lx, Sx = sla.eigh(Ax, Bx)
Lx = np.diag(Lx)
Ix = np.identity(nx, dtype=np.float64)
Ly, Sy = sla.eigh(Ay, By)
Ly = np.diag(Ly)
Iy = np.identity(ny, dtype=np.float64)
Lz, Sz = sla.eigh(Az, Bz)
Lz = np.diag(Lz)
Iz = np.identity(nz, dtype=np.float64)
Lx = Lx.real
Ly = Ly.real
Lz = Lz.real
D = (
np.kron(Iz, np.kron(Iy, Lx))
+ np.kron(Iz, np.kron(Ly, Ix))
+ np.kron(Lz, np.kron(Iy, Ix))
)
dinv = 1.0 / np.diag(D)
return Rx, Ry, Rz, Sx.real, Sy.real, Sz.real, dinv
def mask(W):
return np.dot(R.T, np.dot(R, W))
def Ax_2d(x):
Ux, Uy = gradient_2d(x, n)
Wx = G[0, 0, :] * Ux + G[0, 1, :] * Uy
Wy = G[1, 0, :] * Ux + G[1, 1, :] * Uy
W = gradient_transpose_2d(Wx, Wy, n)
return mask(W)
def Ax(x):
Ux, Uy, Uz = gradient(x, n)
Wx = G[0, 0, :] * Ux + G[0, 1, :] * Uy + G[0, 2, :] * Uz
Wy = G[1, 0, :] * Ux + G[1, 1, :] * Uy + G[1, 2, :] * Uz
Wz = G[2, 0, :] * Ux + G[2, 1, :] * Uy + G[2, 2, :] * Uz
W = gradient_transpose(Wx, Wy, Wz, n)
return mask(W)
Minv = 1.0 / (B * J)
def precon_mass(r):
return Minv * r
def fast_kron_2d(Sy, Sx, U):
nx, mx = Sx.shape
ny, my = Sy.shape
U = U.reshape((my, mx))
U = np.dot(U, Sx.T)
V = U.reshape((my, nx))
U = np.dot(Sy, V)
return U.reshape((nx * ny,))
def fast_kron(Sz, Sy, Sx, U):
nx, mx = Sx.shape
ny, my = Sy.shape
nz, mz = Sz.shape
U = U.reshape((my * mz, mx))
U = np.dot(U, Sx.T)
U = U.reshape((mz, my, nx))
V = np.zeros((mz, ny, nx))
for i in range(mz):
V[i, :, :] = np.dot(Sy, U[i, :, :])
V = V.reshape((mz, nx * ny))
U = np.dot(Sz, V)
return U.reshape((nx * ny * nz,))
if example_2d:
Rx, Ry, Sx, Sy, dinv = fdm_d_inv_2d(N)
R = np.kron(Ry, Rx)
else:
Rx, Ry, Rz, Sx, Sy, Sz, dinv = fdm_d_inv(N)
R = np.kron(Rz, np.kron(Ry, Rx))
def precon_fdm_2d(r):
r = np.dot(R, r)
b = fast_kron_2d(Sy.T, Sx.T, r)
b = dinv * b
return np.dot(R.T, fast_kron_2d(Sy, Sx, b))
def precon_fdm(r):
r = np.dot(R, r)
b = fast_kron(Sz.T, Sy.T, Sx.T, r)
b = dinv * b
return np.dot(R.T, fast_kron(Sz, Sy, Sx, b))
def precon_jacobi_2d(r):
r = np.dot(R, r)
b = fast_kron_2d(Sy.T, Sx.T, r)
b = dinv * b
return np.dot(R.T, fast_kron_2d(Sy, Sx, b))
def precon_jacobi(r):
r = np.dot(R, r)
b = fast_kron(Sz.T, Sy.T, Sx.T, r)
b = dinv * b
return np.dot(R.T, fast_kron(Sz, Sy, Sx, b))
if example_2d:
b = np.exp(10 * Y) * np.sin(10 * X)
b = mask(b.reshape((n * n,)) * B * J)
else:
b = np.exp(10 * Y * Z) * np.sin(10 * X)
b = mask(b.reshape((n * n * n,)) * B * J)
tol = 1.0e-8
maxit = 1000
verbose = 0
t = time.process_time()
if example_2d:
x_cg, niter_cg = cg(Ax_2d, b, tol, maxit, verbose)
x_mass, niter_mass = pcg(Ax_2d, precon_mass, b, tol, maxit, verbose)
x_jacobi, niter_jacobi = pcg(
Ax_2d, precon_jacobi_2d, b, tol, maxit, verbose
)
x_fdm, niter_fdm = pcg(Ax_2d, precon_fdm_2d, b, tol, maxit, verbose)
else:
x_cg, niter_cg = cg(Ax, b, tol, maxit, verbose)
tt = time.process_time() - t
elapsed_cg.append(tt)
# x_mass ,niter_mass =pcg(Ax,precon_mass ,b,tol,maxit,verbose)
# x_jacobi,niter_jacobi=pcg(Ax,precon_jacobi,b,tol,maxit,verbose)
t = time.process_time()
x_fdm, niter_fdm = pcg(Ax, precon_fdm, b, tol, maxit, verbose)
tt = time.process_time() - t
elapsed_fdm.append(tt)
niters_fdm.append(niter_fdm)
niters_cg.append(niter_cg)
orders.append(N)
plt.figure()
plt.plot(orders, elapsed_cg, "-o")
plt.title("Order vs Elapsed time for CG", fontsize=20)
plt.xlim(1, N + 1)
plt.xlabel("N - order", fontsize=16)
plt.ylabel("time (s)", fontsize=16)
plt.savefig("elapsed_cg.pdf", bbox_inches="tight")
plt.figure()
plt.plot(orders, niters_cg, "-o")
plt.title("Order vs number of iterations for CG", fontsize=20)
plt.xlim(2, N + 1)
plt.ylabel("# iterations", fontsize=16)
plt.xlabel("N - order", fontsize=16)
plt.savefig("niter_cg.pdf", bbox_inches="tight")
plt.figure()
plt.semilogy(orders, elapsed_fdm, "b-o", label="pcg(fdm)")
plt.semilogy(orders, elapsed_cg, "g-o", label="cg")
plt.title("Order vs Elapsed time", fontsize=20)
plt.xlim(1, N + 1)
plt.xlabel("N - order", fontsize=16)
plt.ylabel("time (s)", fontsize=16)
plt.legend(loc=0)
plt.savefig("elapsed_fdm_cg.pdf", bbox_inches="tight")
plt.figure()
plt.semilogy(orders, niters_fdm, "b-o", label="pcg(fdm)")
plt.semilogy(orders, niters_cg, "g-o", label="cg")
plt.title("Order vs number of iterations for FDM", fontsize=20)
plt.xlim(2, N + 1)
plt.ylabel("# iterations", fontsize=16)
plt.xlabel("N - order", fontsize=16)
plt.legend(loc=0)
plt.savefig("niter_fdm_cg.pdf", bbox_inches="tight")