-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_functions_dirichlet.py
More file actions
272 lines (231 loc) · 9.11 KB
/
plot_functions_dirichlet.py
File metadata and controls
272 lines (231 loc) · 9.11 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 17 16:25:47 2020
@author: Samuel A. Maloney
"""
import numpy as np
import scipy.linalg as la
import matplotlib as mpl
import matplotlib.pyplot as plt
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
from PoissonMlsSim import PoissonMlsSim
def g(points):
k = 1
return np.sin(k*np.pi*points[:,0]) * np.sinh(k*np.pi*points[:,1])
def one(points, derivative=0):
if derivative == 0:
return np.ones(len(points))
elif derivative in [1, 2]:
return np.zeros((len(points), 2))
def x(points, derivative=0):
if derivative == 0:
return points[:,0]
elif derivative == 1:
return np.hstack((np.ones(len(points)), np.zeros(len(points))))
elif derivative == 2:
return np.zeros((len(points), 2))
def y(points, derivative=0):
if derivative == 0:
return points[:,1]
elif derivative == 1:
return np.hstack((np.zeros(len(points)), np.ones(len(points))))
elif derivative == 2:
return np.zeros((len(points), 2))
def xpy(points, derivative=0):
if derivative == 0:
return points[:,0] + points[:,1]
elif derivative == 1:
return np.ones((len(points), 2))
elif derivative == 2:
return np.zeros((len(points), 2))
def x2(points, derivative=0):
if derivative == 0:
return points[:,0]**2
elif derivative == 1:
return np.hstack((2.*points[:,0:1], np.zeros(len(points))))
elif derivative == 2:
return np.hstack((2.*np.ones(len(points)), np.zeros(len(points))))
def y2(points, derivative=0):
if derivative == 0:
return points[:,1]**2
elif derivative == 1:
return np.hstack((np.zeros(len(points)), 2.*points[:,1:2]))
elif derivative == 2:
return np.hstack((np.zeros(len(points)), 2.*np.ones(len(points))))
def xy(points, derivative=0):
if derivative == 0:
return points[:,0] * points[:,1]
elif derivative == 1:
return np.hstack((points[:,1:2], points[:,0:1]))
elif derivative == 2:
return 2.*np.zeros((len(points), 2))
def x2y2(points, derivative=0):
if derivative == 0:
return points[:,0]**2 * points[:,1]**2
elif derivative in [1, 2]:
raise NotImplementedError("Derivatives not defined for given func.")
def x2py2(points, derivative=0):
if derivative == 0:
return points[:,0]**2 + points[:,1]**2
elif derivative == 1:
return np.hstack((2.*points[:,0:1], 2.*points[:,1:2]))
elif derivative == 2:
return 2.*np.ones((len(points), 2))
def sinx(points, derivative=0):
if derivative == 0:
return np.sin(np.pi*points[:,0])
elif derivative == 1:
return np.hstack((np.pi*np.cos(np.pi*points[:,0]), np.zeros(len(points))))
elif derivative == 2:
return np.hstack((-np.pi**2*np.sin(np.pi*points[:,0]), np.zeros(len(points))))
def sin2x(points, derivative=0):
if derivative == 0:
return np.sin(2*np.pi*points[:,0])
elif derivative == 1:
return np.hstack((2*np.pi*np.cos(2*np.pi*points[:,0]), np.zeros(len(points))))
elif derivative == 2:
return np.hstack((-4*np.pi**2*np.sin(2*np.pi*points[:,0]), np.zeros(len(points))))
def siny(points, derivative=0):
if derivative == 0:
return np.sin(np.pi*points[:,1])
elif derivative == 1:
return np.hstack((np.zeros(len(points)), np.pi*np.cos(np.pi*points[:,1])))
elif derivative == 2:
return np.hstack((np.zeros(len(points)), -np.pi**2*np.sin(np.pi*points[:,1])))
def sin2y(points, derivative=0):
if derivative == 0:
return np.sin(2*np.pi*points[:,1])
elif derivative == 1:
return np.hstack((np.zeros(len(points)), 2*np.pi*np.cos(2*np.pi*points[:,1])))
elif derivative == 2:
return np.hstack((np.zeros(len(points)), -4*np.pi**2*np.sin(2*np.pi*points[:,1])))
def sinxy(points, derivative=0):
if derivative == 0:
return np.sin(np.pi*(points[:,0]*points[:,1]))
elif derivative in [1, 2]:
raise NotImplementedError("Derivatives not defined for given func.")
def sinxpy(points, derivative=0):
if derivative == 0:
return np.sin(np.pi*(points[:,0]+points[:,1]))
elif derivative in [1, 2]:
raise NotImplementedError("Derivatives not defined for given func.")
def sinxsiny(points, derivative=0):
if derivative == 0:
return np.sin(np.pi*points[:,0])*np.sin(np.pi*points[:,1])
elif derivative in [1, 2]:
raise NotImplementedError("Derivatives not defined for given func.")
func = xy
kwargs={
'Nquad' : 2,
'support' : ('circular', 3),
'form' : 'quartic',
'method' : 'galerkin',
'quadrature' : 'uniform',
'basis' : 'quadratic'}
mls = PoissonMlsSim(8, g, **kwargs)
n = 64
points = ( np.indices((n+1, n+1), dtype='float64').T.reshape(-1,2) ) / n
phis = np.zeros((len(points), mls.nNodes), dtype='float64')
dphidx = np.zeros((len(points), mls.nNodes), dtype='float64')
dphidy = np.zeros((len(points), mls.nNodes), dtype='float64')
for i, point in enumerate(points):
indices, local_phis = mls.phi(point)
gradphi = mls.dphi(point)[2]
phis[i, indices] += local_phis
dphidx[i, indices] += gradphi[:,0]
dphidy[i, indices] += gradphi[:,1]
A = np.zeros((mls.nNodes, mls.nNodes), dtype='float64')
for i, node in enumerate(mls.nodes):
indices, local_phis = mls.phi(node)
A[i, indices] += local_phis
u = la.solve(A, func(mls.nodes))
approximate_function = phis@u
exact_function = func(points)
##### Begin Plotting Routines #####
# clear the current figure, if opened, and set parameters
fig = plt.gcf()
fig.clf()
fig.set_size_inches(7.75,3)
plt.subplots_adjust(hspace = 0.3, wspace = 0.2)
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
exact_gradient = func(points, derivative=1)
approximate_dx = dphidx@u
approximate_dy = dphidy@u
# plot the error in dx
difference_dx = approximate_dx - exact_gradient[:,0]
ax = plt.subplot(121, projection='3d')
surf = ax.plot_trisurf(points[:,0], points[:,1], difference_dx,
cmap='seismic', linewidth=0, antialiased=False,
vmin=-np.max(np.abs(difference_dx)),
vmax=np.max(np.abs(difference_dx)))
plt.colorbar(surf, shrink=0.75, aspect=7)
# ax.zaxis.set_ticks([0, 0.001, 0.002, 0.003])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title('dx Error')
# plot the error in dy
difference_dy = approximate_dy - exact_gradient[:,1]
ax = plt.subplot(122, projection='3d')
surf = ax.plot_trisurf(points[:,0], points[:,1], difference_dy,
cmap='seismic', linewidth=0, antialiased=False,
vmin=-np.max(np.abs(difference_dy)),
vmax=np.max(np.abs(difference_dy)))
plt.colorbar(surf, shrink=0.75, aspect=7)
# ax.zaxis.set_ticks([0, 0.001, 0.002, 0.003])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title('dy Error')
# # plot the result
# # plt.subplot(221)
# # plt.tripcolor(points[:,0], points[:,1], approximate_function, shading='gouraud')
# # plt.colorbar()
# ax = plt.subplot(121, projection='3d')
# surf = ax.plot_trisurf(points[:,0], points[:,1], approximate_function,
# cmap='viridis', linewidth=0, antialiased=False
# # , vmin=0, vmax=2
# )
# # ax.zaxis.set_ticks([0,0.5,1,1.5,2])
# plt.colorbar(surf, shrink=0.75, aspect=7)
# plt.xlabel(r'$x$')
# plt.ylabel(r'$y$')
# plt.title('MLS Approximation')
# # # plot the result
# # # plt.subplot(222)
# # # plt.tripcolor(points[:,0], points[:,1], exact_function, shading='gouraud')
# # # plt.colorbar()
# # ax = plt.subplot(222, projection='3d')
# # surf = ax.plot_trisurf(points[:,0], points[:,1], exact_function,
# # cmap='viridis', linewidth=0, antialiased=False)
# # plt.colorbar(surf, shrink=0.75, aspect=7)
# # plt.xlabel(r'$x$')
# # plt.ylabel(r'$y$')
# # plt.title('Exact Function')
# # plot the error
# difference = approximate_function - exact_function
# # plt.subplot(223)
# # plt.tripcolor(points[:,0], points[:,1], difference, shading='gouraud')
# # plt.colorbar()
# ax = plt.subplot(122, projection='3d')
# surf = ax.plot_trisurf(points[:,0], points[:,1], difference,
# cmap='seismic', linewidth=0, antialiased=False,
# vmin=-np.max(np.abs(difference)),
# vmax=np.max(np.abs(difference)))
# # ax.axes.set_zlim3d(bottom=-np.max(np.abs(difference)),
# # top=np.max(np.abs(difference)))
# plt.colorbar(surf, shrink=0.75, aspect=7)
# # ax.zaxis.set_ticks([0, 0.001, 0.002, 0.003])
# plt.xlabel(r'$x$')
# plt.ylabel(r'$y$')
# plt.title('Error')
# plt.savefig('MLS_xy.pdf', bbox_inches='tight', pad_inches=0)