-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_functions_periodic.py
More file actions
178 lines (141 loc) · 4.78 KB
/
plot_functions_periodic.py
File metadata and controls
178 lines (141 loc) · 4.78 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
#!/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 ConvectionDiffusionMlsSim import ConvectionDiffusionMlsSim
def gaussian(points):
A = 1.0
x0 = 0.5
y0 = 0.5
xsigma = 0.15
ysigma = 0.15
return np.exp(-0.5*A*( (points[:,0] - x0)**2/xsigma**2 +
(points[:,1] - y0)**2/ysigma**2 ) )
def hat(points):
return np.hstack((points > 0.25, points < 0.75)).all(1).astype('float64')
def g(points):
k = 1
return np.sin(k*np.pi*points[:,0]) * np.sinh(k*np.pi*points[:,1])
def one(points):
return np.ones(len(points), dtype='float64')
def x(points):
return points[:,0]
def y(points):
return points[:,1]
def xpy(points):
return points[:,0] + points[:,1]
def x2(points):
return points[:,0]**2
def y2(points):
return points[:,1]**2
def xy(points):
return points[:,0] * points[:,1]
def x2y2(points):
return points[:,0]**2 * points[:,1]**2
def sinx(points):
return np.sin(np.pi*points[:,0])
def sin2x(points):
return np.sin(2*np.pi*points[:,0])
def siny(points):
return np.sin(np.pi*points[:,1])
def sin2y(points):
return np.sin(2*np.pi*points[:,1])
def sinxy(points):
return np.sin(np.pi*(points[:,0]*points[:,1]))
def sinxpy(points):
return np.sin(np.pi*(points[:,0]+points[:,1]))
def sinxsiny(points):
return np.sin(np.pi*points[:,0])*np.sin(np.pi*points[:,1])
func = x
dt = 0.1
velocity = np.array([0.1, 0.1], dtype='float64')
diffusivity = 0.0
kwargs={
'N' : 8,
'dt' : dt,
'u0' : gaussian,
'velocity' : velocity,
'diffusivity' : diffusivity,
'Nquad' : 2,
'support' : 3,
'form' : 'cubic',
'quadrature' : 'gaussian',
'basis' : 'linear'}
# Initialize simulation
mls = ConvectionDiffusionMlsSim(**kwargs)
N = 64
points = ( np.indices((N+1, N+1), dtype='float64').T.reshape(-1,2) ) / N
indices = np.arange(mls.nNodes, dtype = 'uint32')
phi_tmp = np.apply_along_axis(mls.phi, 1, points, mls.nodes)
phis = np.empty((len(points), mls.nNodes), dtype='float64')
for i in range(mls.nNodes):
phis[:,i] = np.sum(phi_tmp[:,mls.periodicIndices == i], axis=1)
A_tmp = np.apply_along_axis(mls.phi, 1, mls.uNodes(), mls.nodes)
A = np.empty((mls.nNodes, mls.nNodes), dtype='float64')
for i in range(mls.nNodes):
A[:,i] = np.sum(A_tmp[:,mls.periodicIndices == i], axis=1)
b = func(mls.uNodes())
u = la.solve(A,b)
approximate_function = phis@u
exact_function = func(points)
# clear the current figure, if opened, and set parameters
fig = plt.gcf()
fig.clf()
fig.set_size_inches(15,6)
mpl.rc('axes', titlesize='xx-large', labelsize='x-large')
mpl.rc('xtick', labelsize='large')
mpl.rc('ytick', labelsize='large')
plt.subplots_adjust(hspace = 0.3, wspace = 0.1)
# 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)
# plt.colorbar(surf, vmin=-np.max(np.abs(difference)),
# vmax=np.max(np.abs(difference)))
# 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)