-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojection.py
More file actions
166 lines (135 loc) · 5.31 KB
/
Copy pathprojection.py
File metadata and controls
166 lines (135 loc) · 5.31 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
# -*- 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
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)
f = sinXsinY()
mapping = fcifem.mappings.SinusoidalMapping(0.2, -0.25*f.xmax, f.xmax)
perturbation = 0.5
kwargs={
'mapping' : mapping,
'dt' : 1.,
'velocity' : np.array([0., 0.]),
'diffusivity' : 0.,
'px' : perturbation,
'py' : perturbation,
'seed' : 42,
'xmax' : f.xmax }
# allocate arrays for convergence testing
start = 2
stop = 5
nSamples = stop - start + 1
NX_array = np.logspace(start, stop, num=nSamples, base=2, dtype='int32')
E_inf = np.empty(nSamples, dtype='float64')
E_2 = np.empty(nSamples, dtype='float64')
# 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):
NY = NX
# allocate arrays and compute grid
sim = fcifem.FciFemSim(NX, NY, **kwargs)
sim.setInitialConditions(f)
print(f'NX = {NX},\tNY = {NY},\tnNodes = {sim.nDoFs}')
# Assemble the mass matrix and forcing term
sim.computeSpatialDiscretization(f, NQX=1, NQY=NY, Qord=2, quadType='g',
massLumping=False)
sim.u = sp_la.spsolve(sim.M, sim.b)
# compute the analytic solution and error norms
u_exact = sim.u0func(sim.nodes)
E_inf[iN] = np.linalg.norm(sim.u - u_exact, np.inf)
E_2[iN] = np.linalg.norm(sim.u - u_exact)/np.sqrt(sim.nDoFs)
print(f'max error = {E_inf[iN]}')
print(f'L2 error = {E_2[iN]}\n')
##### Begin Plotting Routines #####
# 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)
# SMALL_SIZE = 7
# MEDIUM_SIZE = 8
# BIGGER_SIZE = 10
# plt.rc('font', size=SMALL_SIZE) # controls default text sizes
# plt.rc('axes', titlesize=MEDIUM_SIZE) # fontsize of the axes title
# plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
# plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
# plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
# plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
# plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
sim.generatePlottingPoints(nx=1, ny=1)
sim.computePlottingSolution()
u_plot = np.sum(sim.phiPlot * sim.u[sim.indPlot], axis=1)
# vmin = np.min((np.min(u_plot), np.min(sim.U)))
# vmax = np.max((np.max(u_plot), np.max(sim.U)))
exact_sol = f(np.vstack((sim.X,sim.Y)).T)
error = sim.U - exact_sol
maxAbsErr = np.max(np.abs(error))
vmin = -maxAbsErr
vmax = maxAbsErr
ax1 = plt.subplot(121)
field = ax1.tripcolor(sim.X, sim.Y, error, shading='gouraud'
,cmap='seismic', vmin=vmin, vmax=vmax
)
x = np.linspace(0, sim.nodeX[-1], 100)
for yi in [0.4, 0.5, 0.6]:
ax1.plot(x, [mapping(np.array([[0, yi]]), i) 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))
plt.xlabel(r'$x$')
plt.ylabel(r'$y$', rotation=0)
if abs(f.xmax - 2*np.pi) < 1e-10:
plt.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:
plt.xticks(np.linspace(0, f.xmax, 6))
plt.margins(0,0)
# plot the error convergence
ax1 = plt.subplot(122)
plt.loglog(NX_array, E_inf, '.-', label=r'$E_\infty$ magnitude')
plt.loglog(NX_array, E_2, '.-', label=r'$E_2$ magnitude')
plt.minorticks_off()
plt.xticks(NX_array, NX_array)
plt.xlabel(r'$NX$')
plt.ylabel(r'Magnitude of Error Norm')
# plot the intra-step order of convergence
ax2 = ax1.twinx()
logN = np.log(NX_array)
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 = np.logspace(start+0.5, stop-0.5, num=nSamples-1, base=2.0)
plt.plot(intraN, order_inf, '.:', linewidth=1, label=r'$E_\infty$ order')
plt.plot(intraN, order_2, '.:', linewidth=1, label=r'$E_2$ order')
plt.plot(plt.xlim(), [2, 2], 'k:', linewidth=1, label='Expected')
plt.ylim(0, 5)
plt.yticks(np.linspace(0,5,6))
plt.ylabel(r'Intra-step Order of Convergence')
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='best')
plt.margins(0,0)
# fig.savefig("CD_MassLumped_RK4.pdf", bbox_inches = 'tight', pad_inches = 0)