-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoisson_test.py
350 lines (288 loc) · 10.8 KB
/
Poisson_test.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- coding: utf-8 -*-
"""
@author: Samuel A. Maloney
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse.linalg as sp_la
import fcifem
# import fcifem_periodic as fcifem
from timeit import default_timer
class slantedTestProblem:
xmax = 1.
ymax = 1.
xfac = 2*np.pi/xmax
yfac = 2*np.pi/ymax
n = 8
n2 = n*n
yf2 = yfac*yfac
_2nyf2 = 2*n*yf2
n2xf2pyf2 = n2*(xfac*xfac + yf2)
n2xf2pyf2pyf2 = n2xf2pyf2 + yf2
A = 0.5 / n2xf2pyf2
B = 0.5 / (n2xf2pyf2pyf2 - _2nyf2*_2nyf2/n2xf2pyf2pyf2)
C = B*_2nyf2 / n2xf2pyf2pyf2
aA = abs(A)
aB = abs(B)
aC = abs(C)
umax = aA + aB + aC
dudxMax = umax*xfac*n
dudyMax = yfac*(aA*n + (aB+aC)*(1+n))
dudQMax = yfac*(aB+aC)/np.sqrt(2)
dfdxMax = xfac*n
dfdyMax = yfac*(n + 0.5)
dfdQMax = 0.5*yfac/np.sqrt(2)
def __call__(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
yarg = self.yfac*y
return 0.5*np.sin(self.n*(yarg - self.xfac*x))*(1 + np.sin(yarg))
def solution(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
yarg = self.yfac*y
xyarg = self.n*(yarg - self.xfac*x)
return self.A*np.sin(xyarg) + self.B*np.sin(yarg)*np.sin(xyarg) \
+ self.C*np.cos(yarg)*np.cos(xyarg)
class simplifiedSlantProblem:
xmax = 1.
ymax = 1.
n = 2
xfac = 2*np.pi/xmax
yfac = 2*np.pi/ymax
umax = 1/(2*n*n*(yfac*yfac + xfac*xfac))
dudxMax = umax*xfac*n
dudyMax = umax*yfac*n
dudQMax = 0
def __call__(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
return 0.5*np.sin(self.n*(self.yfac*y - self.xfac*x))
def solution(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
return self.umax * np.sin(self.n*(self.yfac*y - self.xfac*x))
class sinXsinY:
xmax = 1.
ymax = 1.
xfac = 2*np.pi/xmax
yfac = 2*np.pi/ymax
umax = (1 / (xfac**2 + yfac**2))
dudxMax = umax*xfac
dudyMax = umax*yfac
def __call__(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
return np.sin(self.xfac*x)*np.sin(self.yfac*y)
def solution(self, p):
return self.umax * self(p)
class linearPatch:
xmax = 1.
ymax = 1.
umax = 1.
b = 0.05
# define a such that (0, 0) maps to (xmax, 1) for given b and xmax
a = (1 - b*xmax)/xmax**2
def __call__(self, p):
nPoints = p.size // 2
return np.zeros(nPoints)
def solution(self, p):
x = p.reshape(-1,2)[:,0]
y = p.reshape(-1,2)[:,1]
return 1*x + 2*y
# f = slantedTestProblem()
# f = simplifiedSlantProblem()
# f = sinXsinY()
f = linearPatch()
# mapping = fcifem.mappings.SinusoidalMapping(0.2, -0.25*f.xmax, f.xmax)
mapping = fcifem.mappings.LinearMapping(1/f.xmax)
# mapping = fcifem.mappings.StraightMapping()
perturbation = 0.1
kwargs={
'mapping' : mapping,
'dt' : 1.,
'velocity' : np.array([0., 0.]),
'diffusivity' : 1., # Makes diffusivity matrix K into Poisson operator
'px' : perturbation,
'py' : perturbation,
'seed' : 42,
'xmax' : f.xmax }
# allocate arrays for convergence testing
start = 1
stop = 6
nSamples = np.rint(stop - start + 1).astype('int')
NX_array = np.logspace(start, stop, num=nSamples, base=2, dtype='int')
E_inf = np.empty(nSamples)
E_2 = np.empty(nSamples)
t_setup = np.empty(nSamples)
t_solve = np.empty(nSamples)
dxi = []
print('Poisson_test.py\n')
# loop over N to test convergence where N is the number of
# grid cells along one dimension, each cell forms 2 triangles
# therefore number of nodes equals (N+1)*(N+1)
for iN, NX in enumerate(NX_array):
start_time = default_timer()
NY = 1*NX
# NX = 16
NQX = 1
# NQX = max(NY//NX, 1)
NQY = NY
Qord = 2
# allocate arrays and compute grid
sim = fcifem.FciFemSim(NX, NY, **kwargs)
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationLinearVCI
sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationConservativeLinearVCI
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationConservativeLinearVCIold
##### These require the fcifem_periodic version of the module #####
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationQuadraticVCI
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationConservativePointVCI
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationConservativeCellVCI
# sim.computeSpatialDiscretization = sim.computeSpatialDiscretizationConservativeNodeVCI
sim.setInitialConditions(f)
print(f'NX = {NX},\tNY = {NY},\tnDoFs = {sim.nDoFs}')
# Assemble the mass matrix and forcing term
sim.computeSpatialDiscretization(f, NQX=NQX, NQY=NQY, Qord=Qord, quadType='g',
massLumping=False)
try:
dxi.append(sim.xi[1:])
except:
pass
# sim.K.data[0] = 1.
# sim.K.data[1:sim.K.indptr[1]] = 0.
# sim.b[0] = f.solution(sim.DoFs[0])
##### Enforce exact solution constraints directly #####
# sim.K.data[0] = 1.
# sim.K.data[1:sim.K.indptr[1]] = 0.
# sim.b[0] = 0.
# n = int(NY/2)
# sim.K.data[sim.K.indptr[n]:sim.K.indptr[n+1]] = 0.
# sim.K[n,n] = 1.
# sim.b[n] = 0., label='Expected'
# n = int(NX*NY/2)
# sim.K.data[sim.K.indptr[n]:sim.K.indptr[n+1]] = 0.
# sim.K[n,n] = 1.
# sim.b[n] = 0.
# # n = int(NX*NY*3/4)
# # sim.K.data[sim.K.indptr[n]:sim.K.indptr[n+1]] = 0.
# # sim.K[n,n] = 1.
# # sim.b[n] = 0.
# Centre point
n = int(NX*NY/2 + NY/2)
sim.K.data[sim.K.indptr[n]:sim.K.indptr[n+1]] = 0.
sim.K[n,n] = 1.
sim.b[n] = f.solution(sim.DoFs[n])
for n, node in enumerate(sim.DoFs):
if node.prod() == 0.:
sim.K.data[sim.K.indptr[n]:sim.K.indptr[n+1]] = 0.
sim.K[n,n] = 1.
sim.b[n] = f.solution(sim.DoFs[n])
t_setup[iN] = default_timer()-start_time
print(f'setup time = {t_setup[iN]:.8e} s')
start_time = default_timer()
# Solve for the approximate solution
# u = sp_la.spsolve(sim.K, sim.b)
tolerance = 1e-10
sim.u, info = sp_la.lgmres(sim.K, sim.b, tol=tolerance, atol=tolerance)
t_solve[iN] = default_timer()-start_time
print(f'solve time = {t_solve[iN]:.8e} s')
start_time = default_timer()
# compute the analytic solution and normalized error norms
uExact = f.solution(sim.DoFs)
E_inf[iN] = np.linalg.norm(sim.u - uExact, np.inf) / f.umax
E_2[iN] = np.linalg.norm(sim.u - uExact)/np.sqrt(sim.nDoFs) / f.umax
print(f'max error = {E_inf[iN]:.8e}')
print(f'L2 error = {E_2[iN]:.8e}\n', flush=True)
# print summary
print(f'xmax = {f.xmax}, {mapping}')
print(f'px = {kwargs["px"]}, py = {kwargs["py"]}, seed = {kwargs["seed"]}')
print(f'NQX = {NQX}, NQY = {NQY//NY}*NY, Qord = {Qord}')
print(f'VCI: {sim.vci} using {sim.vci_solver}\n')
with np.printoptions(formatter={'float': lambda x: format(x, '.8e')}):
print('E_2 =', repr(E_2))
print('E_inf =', repr(E_inf))
print('t_setup =', repr(t_setup))
print('t_solve =', repr(t_solve))
#%% Plotting
plt.rc('pdf', fonttype=42)
plt.rc('text', usetex=True)
plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
# fontsize : int or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
# plt.rc('font', size='small')
# plt.rc('legend', fontsize='small')
# plt.rc('axes', titlesize='medium', labelsize='medium')
# plt.rc('xtick', labelsize='small')
# plt.rc('ytick', labelsize='small')
# plt.rc('figure', titlesize='large')
# clear the current figure, if opened, and set parameters
fig = plt.figure(figsize=(7.75, 3))
fig.subplots_adjust(hspace=0.3, wspace=0.3)
# sim.generatePlottingPoints(nx=1, ny=1)
sim.generatePlottingPoints(nx=int(max(NY/NX,1)), ny=int(max(NX/NY,1)))
sim.computePlottingSolution()
# vmin = np.min(sim.U)
# vmax = np.max(sim.U)
exactSol = f.solution(np.vstack((sim.X,sim.Y)).T)
error = sim.U - exactSol
maxAbsErr = np.max(np.abs(error))
# maxAbsErr = np.max(np.abs(sim.u - uExact))
vmin = -maxAbsErr
vmax = maxAbsErr
ax1 = plt.subplot(121)
ax1.set_title('Final Solution')
# field = ax1.tripcolor(sim.X, sim.Y, error, shading='gouraud'
# ,cmap='seismic', vmin=vmin, vmax=vmax)
# field = ax1.tripcolor(sim.DoFs[:,0], sim.DoFs[:,1], sim.u - uExact
# ,shading='gouraud', cmap='seismic', vmin=vmin, vmax=vmax)
field = ax1.tripcolor(sim.X, sim.Y, sim.U, shading='gouraud')
# field = ax1.tripcolor(sim.X, sim.Y, exactSol, shading='gouraud')
# field = ax1.tripcolor(sim.X, sim.Y, f(np.vstack((sim.X,sim.Y)).T), shading='gouraud')
x = np.linspace(0, sim.nodeX[-1], 100)
for yi in [0.4, 0.5, 0.6]:
try:
ax1.plot(x, [sim.BC.mapping(np.array([[0, yi]]), i) for i in x], 'k')
except:
ax1.plot(x, [sim.mapping(np.array([[0, yi]]), i) % 1 for i in x], 'k')
# for xi in sim.nodeX:
# ax1.plot([xi, xi], [0, 1], 'k:')
# ax.plot(sim.X[np.argmax(sim.U)], sim.Y[np.argmax(sim.U)],
# 'g+', markersize=10)
# cbar = plt.colorbar(field, format='%.0e')
cbar = plt.colorbar(field)
cbar.formatter.set_powerlimits((0, 0))
ax1.set_xlabel(r'$x$')
ax1.set_ylabel(r'$y$', rotation=0)
if abs(f.xmax - 2*np.pi) < 1e-10:
ax1.set_xticks(np.linspace(0, f.xmax, 5),
['0', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$'])
# plt.xticks(np.linspace(0, 2*np.pi, 7),
# ['0',r'$\pi/3$',r'$2\pi/3$',r'$\pi$',r'$4\pi/3$',r'$5\pi/3$',r'$2\pi$'])
else:
ax1.set_xticks(np.linspace(0, f.xmax, 6))
ax1.margins(0,0)
# plot the error convergence
ax2 = plt.subplot(122)
logN = np.log(NX_array)
ax2.semilogy(logN, E_inf, '.-', label=r'$E_\infty$')
ax2.semilogy(logN, E_2, '.-', label=r'$E_2$')
# ax2.minorticks_off()
ax2.set_xticks(logN, labels=NX_array)
ax2.set_xlabel(r'$NX$')
ax2.set_ylabel(r'Magnitude of Error Norm')
# plot the intra-step order of convergence
ax2R = ax2.twinx()
logE_inf = np.log(E_inf)
logE_2 = np.log(E_2)
order_inf = (logE_inf[0:-1] - logE_inf[1:])/(logN[1:] - logN[0:-1])
order_2 = (logE_2[0:-1] - logE_2[1:])/(logN[1:] - logN[0:-1])
intraN = 0.5 * (logN[:-1] + logN[1:])
ax2R.plot(intraN, order_inf, '.:', linewidth=1, label=r'$E_\infty$ order')
ax2R.plot(intraN, order_2, '.:', linewidth=1, label=r'$E_2$ order')
ax2R.axhline(2, linestyle=':', color='k', linewidth=1, label='Expected')
ax2R.set_ylim(0, 5)
ax2R.set_yticks(np.linspace(0,5,6))
# ax2R.set_lim(0, 3)
# ax2R.set_yticks([0, 0.5, 1, 1.5, 2, 2.5, 3])
ax2R.set_ylabel(r'Intra-step Order of Convergence')
ax2.legend()
# lines, labels = ax1.get_legend_handles_labels()