-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
246 lines (208 loc) · 8.05 KB
/
functions.py
File metadata and controls
246 lines (208 loc) · 8.05 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
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator
def plot_solution_3d( sol, h ):
n = int(1./h + 1)
X = np.arange(0., 1.+h, h)
Y = np.arange(0., 1.+h, h)
X, Y = np.meshgrid(X, Y)
Z = sol.reshape(n,n)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(0.0, 0.01)
fig.tight_layout(pad=1.0)
plt.xlabel('x')
plt.ylabel('y')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib.patches as patches
def plot_solution_2d(solgrad,h):
fig, ax = plt.subplots()
# Make data.
X = np.arange(0., 1.+h, h)
Y = np.arange(0., 1.+h, h)
X, Y = np.meshgrid(X, Y)
Z = solgrad.reshape(int(1./h+1),int(1./h+1))
# Plot the surface.
surf = ax.pcolor(X, Y, Z[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Create a Rectangle patch
#rect = patches.Rectangle((0.1, 0.1), 0.2, 0.2, linewidth=0.5, edgecolor='k', facecolor='none')
# Add the patch to the Axes
#ax.add_patch(rect)
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
hx = 0.1
# Add a grid
plt.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
plt.minorticks_on()
plt.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
major_ticks = np.arange(0., 1.+hx, hx)
minor_ticks = np.arange(0., 1.+hx, hx/5)
plt.xlabel('x')
plt.ylabel('y')
plt.xticks(major_ticks)
plt.yticks(major_ticks)
plt.show()
def plot_residual(res):
fig, ax = plt.subplots()
ax.plot(res)
ax.set_title('Residuals of PCG')
ax.set_yscale('log')
ax.set_xlabel('Iteration i')
ax.set_ylabel('Residual')
plt.show()
print(f'End residual: {res[-1]}')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib.patches as patches
def plot_dualsolutions(solcg, solpcg, h):
hx = 0.1
fig, axs = plt.subplots(2, 1, figsize=(5, 8)) # Increase the figure size to make the plot area wider
ax1 = axs[0]
ax2 = axs[1]
fig.subplots_adjust(wspace=0.4, hspace=0.3) # Increase the padding between subplots vertically
# Make data.
X = np.arange(0., 1.+h, h)
Y = np.arange(0., 1.+h, h)
X, Y = np.meshgrid(X, Y)
Z1 = solcg.reshape(int(1./h+1),int(1./h+1))
Z2 = solpcg.reshape(int(1./h+1),int(1./h+1))
# Plot the surface.
surf1 = ax1.pcolor(X, Y, Z1[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False, vmin=np.min(Z1), vmax=np.max(Z2))
surf2 = ax2.pcolor(X, Y, Z2[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False, vmin=np.min(Z1), vmax=np.max(Z2))
# Add a grid to ax1
ax1.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.minorticks_on()
ax1.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.set_xticks(np.arange(0., 1.+hx, hx))
ax1.set_yticks(np.arange(0., 1.+hx, hx))
# Add a color bar which maps values to colors.
cbar1 = fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
cbar2 = fig.colorbar(surf2, ax=ax2, shrink=0.5, aspect=5)
hx = 0.1
# Add a grid
ax1.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.minorticks_on()
ax1.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.set_xticks(np.arange(0., 1.+hx, hx))
ax1.set_yticks(np.arange(0., 1.+hx, hx))
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')
ax1.set_title('$\mu_x = 0.1$')
ax2.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax2.minorticks_on()
ax2.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax2.set_xticks(np.arange(0., 1.+hx, hx))
ax2.set_yticks(np.arange(0., 1.+hx, hx))
ax2.set_xlabel('$x$')
ax2.set_ylabel('$y$')
ax2.set_title('$\mu_x = 0.01$')
plt.show()
def plot_trisolutions(solcg0, solcg, solpcg, h):
hx = 0.1
fig, axs = plt.subplots(1, 3, figsize=(12, 4)) # Increase the figure size to make the plot area wider
ax0 = axs[0]
ax1 = axs[1]
ax2 = axs[2]
fig.subplots_adjust(wspace=0.4) # Increase the padding between subplots
# Make data.
X = np.arange(0., 1.+h, h)
Y = np.arange(0., 1.+h, h)
X, Y = np.meshgrid(X, Y)
Z0 = solcg0.reshape(int(1./h+1),int(1./h+1))
Z1 = solcg.reshape(int(1./h+1),int(1./h+1))
Z2 = solpcg.reshape(int(1./h+1),int(1./h+1))
# Plot the surface.
surf0 = ax0.pcolor(X, Y, Z0[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False)
surf1 = ax1.pcolor(X, Y, Z1[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False)
surf2 = ax2.pcolor(X, Y, Z2[:-1,:-1], shading='flat', cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Add a grid to ax1
ax0.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax0.minorticks_on()
ax0.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax0.set_xticks(np.arange(0., 1.+hx, hx))
ax0.set_yticks(np.arange(0., 1.+hx, hx))
# Add a color bar which maps values to colors.
cbar0 = fig.colorbar(surf0, ax=ax0, shrink=0.5, aspect=5)
cbar1 = fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
cbar2 = fig.colorbar(surf2, ax=ax2, shrink=0.5, aspect=5)
hx = 0.1
# Add a grid
ax0.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax0.minorticks_on()
ax0.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax0.set_xticks(np.arange(0., 1.+hx, hx))
ax0.set_yticks(np.arange(0., 1.+hx, hx))
ax0.set_xlabel('$x$')
ax0.set_ylabel('$y$')
ax0.set_title('$\mu_x = 1$')
ax1.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.minorticks_on()
ax1.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax1.set_xticks(np.arange(0., 1.+hx, hx))
ax1.set_yticks(np.arange(0., 1.+hx, hx))
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')
ax1.set_title('$\mu_x = 0.1$')
ax2.grid(which='major', axis='both', linestyle='-', color='k', linewidth=0.2)
ax2.minorticks_on()
ax2.grid(which='minor', axis='both', linestyle='-', color='k', linewidth=0.2)
ax2.set_xticks(np.arange(0., 1.+hx, hx))
ax2.set_yticks(np.arange(0., 1.+hx, hx))
ax2.set_xlabel('$x$')
ax2.set_ylabel('$y$')
ax2.set_title('$\mu_x = 0.01$')
plt.show()
def compare_residuals(res, res0, res1, h):
hx = 0.1
fig, ax = plt.subplots()
ax.plot(res)
ax.plot(res0)
ax.plot(res1)
ax.legend(['mux = 1', 'mux = 0.1', 'mux = 0.01'])
ax.set_title('Residuals of PCG')
ax.set_yscale('log')
ax.set_xlabel('Iteration i')
ax.set_ylabel('Residual')
plt.show()
def compare_residuals(res, res0, res1, h):
hx = 0.1
fig, ax = plt.subplots()
ax.plot(res)
ax.plot(res0)
ax.plot(res1)
ax.legend(['PCG $\mu_x = 1$', 'PCG $\mu_x = 0.1$', 'PCG $\mu_x = 0.01$'])
ax.set_title('Residuals of PCG')
ax.set_yscale('log')
ax.set_xlabel('Iteration i')
ax.set_ylabel('Residual')
plt.show()
def compare_residuals2(rescg, rescg0, rescg1, res, res0, res1, h):
hx = 0.1
fig, ax = plt.subplots()
ax.plot(rescg, linestyle='--')
ax.plot(rescg0, linestyle='--')
ax.plot(rescg1, linestyle='--')
ax.plot(res)
ax.plot(res0)
ax.plot(res1)
ax.legend(['CG $\mu_x = 1$','CG $\mu_x = 0.1$','CG $\mu_x = 0.01$','PCG $\mu_x = 1$', 'PCG $\mu_x = 0.1$', 'PCG $\mu_x = 0.01$'])
ax.set_title('Residuals of PCG and CG algorithms for varying $\mu_x$')
ax.set_yscale('log')
ax.set_xlabel('Iteration i')
ax.set_ylabel('Residual')
plt.show()