-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poisson2D.py
350 lines (290 loc) · 9.92 KB
/
Poisson2D.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Solves Poission 2D equation using Finite element method
# This is the Main file <Poisson2D.py>.
#
# -To plot the Sloution L2 and H1 errors for diffrent spacing h use <l2error_plot.py>.
# -The file <solver.py> contains a quick solver function for this problem.
# -The file <l2error.py> is the solution L2 error evaluation function.
# -The file <h1error.py> is the solution H1 error evaluation function.
#
# Problem description:
#
# The PDE is defined for 0 < x < 1, 0 < y < 1:
# - uxx - uyy = f(x)
# with boundary conditions
# u(0,y) = 0,
# u(1,y) = 0,
# u(x,0) = 0,
# u(x,1) = 0.
#
# The exact solution is:
# exact(x) = x * ( 1 - x ) * y * ( 1 - y ).
# The right hand side f(x) is:
# f(x) = 2 * x * ( 1 - x ) + 2 * y * ( 1 - y )
#
# Modified: Q1 instead using P1 in the pervious code.
# The unit square is divided into N by N squares. Bilinear finite
# element basis functions are defined, and the solution is sought as a
# piecewise linear combination of these basis functions.
#
# Author: Maged Shaaban
# magshaban[at]gmail.com
#
import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as la
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib.colors import LogNorm
from quad import *
from funs import *
from l2error import *
from h1error import *
element_linear_num = 10
node_linear_num = element_linear_num + 1
element_num = element_linear_num * element_linear_num
node_num = node_linear_num * node_linear_num
a = 0
b = 1.0
grid = np.linspace ( a, b, node_linear_num )
print( '' )
print( ' Nodes along x axis:' )
print( '' )
#
# to be modified from 1 to node_linear_num+1 wich gives 1,2,3,4,...
#
for i in range ( 0, node_linear_num ):
print( ' %d %f' %( i, grid[i] ) )
#
# Print the elements, listing the nodes in counterclockwise order.
#
if(True):
e = 0
print ( '' )
print ( ' The elements, listing the nodes in counterclockwise order' )
print ( '' )
for j in range ( 0, element_linear_num ):
y = grid[j]
for i in range ( 0, element_linear_num ):
sw = j * node_linear_num + i
se = j * node_linear_num + i + 1
nw = ( j + 1 ) * node_linear_num + i
ne = ( j + 1 ) * node_linear_num + i + 1
print ( '%4d %4d %4d %4d' % ( sw, se, ne, nw ) )
e = e + 1
#
# Set up a quadrature rule.
#
quad_num, quad_point, quad_weight = quad()
#
# x and y for each node.
#
x = np.zeros( node_linear_num * node_linear_num)
y = np.zeros( node_linear_num * node_linear_num)
v = 0
for j in range ( 0, node_linear_num ):
for i in range (0, node_linear_num):
x[v]= grid[i]
y[v] = grid[j]
v = v + 1
#
# Memory allocation.
#
A = np.zeros((node_num, node_num))
rhs = np.zeros(node_num)
for ex in range ( 0, element_linear_num ):
w = ex
e = ex + 1
xw = grid[w]
xe = grid[e]
for ey in range ( 0, element_linear_num ):
s = ey
n = ey + 1
ys = grid[s]
yn = grid[n]
sw = ey * node_linear_num + ex
se = ey * node_linear_num + ex + 1
nw = ( ey + 1 ) * node_linear_num + ex
ne = ( ey + 1 ) * node_linear_num + ex + 1
#
# The 2D quadrature rule is the 'product' of X and Y copies of the 1D rule.
#
for qx in range ( 0, quad_num ):
xq = xw + quad_point[qx] * (xe - xw)
for qy in range( 0,quad_num ):
yq = ys + quad_point[qy] * (yn - ys)
wq = quad_weight[qx] * quad_weight[qy] * (xe - xw) * (yn - ys)
#
# Evaluate all four basis functions, and their X and Y derivatives.
#
vsw = ( xe - xq ) / ( xe - xw ) * ( yn - yq ) / ( yn - ys )
vswx = ( -1.0 ) / ( xe - xw ) * ( yn - yq ) / ( yn - ys )
vswy = ( xe - xq ) / ( xe - xw ) * ( -1.0 ) / ( yn - ys )
vse = ( xq - xw ) / ( xe - xw ) * ( yn - yq ) / ( yn - ys )
vsex = ( 1.0 ) / ( xe - xw ) * ( yn - yq ) / ( yn - ys )
vsey = ( xq - xw ) / ( xe - xw ) * ( -1.0 ) / ( yn - ys )
vnw = ( xe - xq ) / ( xe - xw ) * ( yq - ys ) / ( yn - ys )
vnwx = ( -1.0 ) / ( xe - xw ) * ( yq - ys ) / ( yn - ys )
vnwy = ( xe - xq ) / ( xe - xw ) * ( 1.0 ) / ( yn - ys )
vne = ( xq - xw ) / ( xe - xw ) * ( yq - ys ) / ( yn - ys )
vnex = ( 1.0 ) / ( xe - xw ) * ( yq - ys ) / ( yn - ys )
vney = ( xq - xw ) / ( xe - xw ) * ( 1.0 ) / ( yn - ys )
#
# Compute contributions to the stiffness matrix.
#
A[sw,sw] = A[sw,sw] + wq * ( vswx * vswx + vswy * vswy )
A[sw,se] = A[sw,se] + wq * ( vswx * vsex + vswy * vsey )
A[sw,nw] = A[sw,nw] + wq * ( vswx * vnwx + vswy * vnwy )
A[sw,ne] = A[sw,ne] + wq * ( vswx * vnex + vswy * vney )
rhs[sw] = rhs[sw] + wq * vsw * rhs_fn ( xq, yq )
A[se,sw] = A[se,sw] + wq * ( vsex * vswx + vsey * vswy )
A[se,se] = A[se,se] + wq * ( vsex * vsex + vsey * vsey )
A[se,nw] = A[se,nw] + wq * ( vsex * vnwx + vsey * vnwy )
A[se,ne] = A[se,ne] + wq * ( vsex * vnex + vsey * vney )
rhs[se] = rhs[se] + wq * vse * rhs_fn ( xq, yq )
A[nw,sw] = A[nw,sw] + wq * ( vnwx * vswx + vnwy * vswy )
A[nw,se] = A[nw,se] + wq * ( vnwx * vsex + vnwy * vsey )
A[nw,nw] = A[nw,nw] + wq * ( vnwx * vnwx + vnwy * vnwy )
A[nw,ne] = A[nw,ne] + wq * ( vnwx * vnex + vnwy * vney )
rhs[nw] = rhs[nw] + wq * vnw * rhs_fn ( xq, yq )
A[ne,sw] = A[ne,sw] + wq * ( vnex * vswx + vney * vswy )
A[ne,se] = A[ne,se] + wq * ( vnex * vsex + vney * vsey )
A[ne,nw] = A[ne,nw] + wq * ( vnex * vnwx + vney * vnwy )
A[ne,ne] = A[ne,ne] + wq * ( vnex * vnex + vney * vney )
rhs[ne] = rhs[ne] + wq * vne * rhs_fn( xq, yq )
A_in = A # It will be used to plot the stifness matrix without BC.
#
# Modify the linear system to enforce the boundary conditions where
# X = 0 or 1 or Y = 0 or 1.
#
v = 0
for j in range ( 0, node_linear_num ):
for i in range ( 0, node_linear_num ):
if ( i == 0 or i == node_linear_num - 1 or j == 0 or j == node_linear_num - 1 ):
A[v,0:node_num] = 0.0
A[v,v] = 1.0
rhs[v] = 0.0
v = v + 1
#
# Solve the linear system.
#
u = la.solve(A, rhs)
u_exact = exact_fn(x, y)
#
# Compare the solution and the error at the nodes.
#
print ( '' )
print ( ' Node x y u u_exact' )
print ( '' )
v = 0
for j in range ( 0, node_linear_num ):
for i in range ( 0, node_linear_num ):
print ( ' %4d %8f %8f %14g %14g' % ( v, x[v], y[v], u[v], u_exact[v] ) )
v = v + 1
#
#to plot the mass matrix before adding the boundary conditions
#
fig, (ax1, ax2) = plt.subplots( 1, 2 )
fig.suptitle( 'The Stiffness Matrix' )
ax1.matshow( A_in )
ax2.spy( A_in )
plt.show()
#
#to plot the mass matrix
#
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('The Stiffness Matrix with BC contribution')
ax1.matshow( A )
ax2.spy( A )
plt.show()
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
z =u
# Plot the surface.
surf = ax.plot_trisurf(x, y, z, cmap=cm.Spectral,linewidth=0, antialiased=False)
# Customize the z axis.
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.7, aspect=9)
plt.title('The solution $U$')
plt.show()
x_list = x
y_list = y
z_list = u
N = int(len(u) ** .5)
z = u.reshape(N, N)
plt.title('The solution $U$ from above')
plt.imshow(z, extent=(np.amin(x_list),
np.amax(x_list),
np.amin(y_list),
np.amax(y_list)),
norm=LogNorm(), aspect='auto')
#plt.colorbar()
plt.show()
print('#####################################################\n\n')
print( ''' The Exact Solution
U_exact = xy(1-x)(1-y)
evaluated on each node is ''')
#
# plot the Exact solution
#
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
z = u_exact
# Plot the surface.
surf = ax.plot_trisurf(x, y, z, cmap=cm.Spectral,linewidth=0, antialiased=False)
# Customize the z axis.
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.7, aspect=9)
plt.title('The exact solution $U_{exact}$')
plt.show()
print( '\n\n####################################################' )
#
# Plotting the Error
#
xerror = np.zeros(node_linear_num*node_linear_num)
error = np.zeros(node_linear_num*node_linear_num)
v = 0
for j in range ( 0, node_linear_num ):
for i in range ( 0, node_linear_num ):
error [v]= u[v]- u_exact[v]
xerror[v]= v
v = v + 1
plt.plot(xerror,error,'b')
plt.xlabel('Node')
plt.ylabel('$Error$')
plt.grid()
plt.show()
fig = plt.figure()
ax = fig.gca(projection='3d')
z =error
surf = ax.plot_trisurf(x, y, z, cmap=cm.Spectral,linewidth=0, antialiased=False)
# Customize the z axis.
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter(' %.05f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.7, aspect=9)
plt.title('$Error$')
plt.show()
#
# Errors
#
# To evaluate the L2 error
print( '\n\n' )
print('\n The L2 error = ', L2_error( element_linear_num, u ) )
print( )
# To evaluate the L2 error
print('\n The H1 error = ', H1_error( element_linear_num, u ) )
print( '\n#####################################################' )
#
# Terminate.
#
print ( '' )
print ( ' >> Normal end of execution.' )