-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsmooth-badbcs.py
156 lines (99 loc) · 3.15 KB
/
smooth-badbcs.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
#!/usr/bin/env python
"""
an example of solving Poisson's equation via smoothing only with the
wrong (first-order) BCs.
Here, we solve
u_xx = sin(x)
u = 0 on the boundary [0,1]
The analytic solution is u(x) = -sin(x) + x sin(1)
"""
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
# the analytic solution
def true(x):
return -np.sin(x) + x*np.sin(1.0)
# the L2 error norm
def error(ilo, ihi, dx, r):
# L2 norm of elements in r, multiplied by dx to
# normalize
return np.sqrt(dx*np.sum((r[ilo:ihi+1]**2)))
# the righthand side
def f(x):
return np.sin(x)
def computeResidual(ilo, ihi, dx, phi, frhs):
r = np.zeros(len(phi))
r[ilo:ihi+1] = frhs[ilo:ihi+1] - \
(phi[ilo+1:ihi+2] - 2.0*phi[ilo:ihi+1] + phi[ilo-1:ihi])/dx**2
return r
def smoothRun(nx, badBCs=0):
xmin = 0.0
xmax = 1.0
ng = 1
# initialize the solution to zero. Put one ghost cell on either end
phi = np.zeros(nx + 2*ng, dtype=np.float64)
phinew = np.zeros(nx + 2*ng, dtype=np.float64)
ilo = ng
ihi = ng + nx - 1
# coordinates of centers
dx = (xmax - xmin)/nx
x = (np.arange(nx+2*ng) - ng + 0.5)*dx + xmin
# initialize the RHS using the function f
frhs = np.zeros(nx + 2*ng, dtype=np.float64)
frhs[ilo:ihi+1] = f(x[ilo:ihi+1])
# smooth
n = np.arange(20000) + 1
e = []
r = []
# fill the ghost cells
phi[ilo-1] = -phi[ilo]
phi[ihi+1] = -phi[ihi]
print("source norm: ", error(ilo, ihi, dx, frhs))
for i in n:
# red-black Gauss-Seidel -- first do the odd, then even points
phi[ilo:ihi+1:2] = \
0.5*(-dx*dx*frhs[ilo:ihi+1:2] + \
phi[ilo+1:ihi+2:2] + phi[ilo-1:ihi:2])
# fill the ghost cells
if (badBCs):
phi[ilo-1] = 0.0
phi[ihi+1] = 0.0
else:
phi[ilo-1] = -phi[ilo]
phi[ihi+1] = -phi[ihi]
phi[ilo+1:ihi+1:2] = \
0.5*(-dx*dx*frhs[ilo+1:ihi+1:2] + \
phi[ilo+2:ihi+2:2] + phi[ilo:ihi:2])
# fill the ghost cells
if (badBCs):
phi[ilo-1] = 0.0
phi[ihi+1] = 0.0
else:
phi[ilo-1] = -phi[ilo]
phi[ihi+1] = -phi[ihi]
# compute the true error (wrt the analytic solution)
e.append(error(ilo, ihi, dx, phi - true(x)))
# compute the residual
resid = computeResidual(ilo, ihi, dx, phi, frhs)
r.append(error(ilo, ihi, dx, resid))
r = np.array(r)
e = np.array(e)
return n, r, e
# test the multigrid solver
N = [16, 16, 32, 32, 64, 64]
c = ["r", "r:", "g", "g:", "b", "b:"]
for nx in N:
color = c.pop()
if len(c) % 2 == 0:
n, r, e = smoothRun(nx)
plt.plot(n, e, color, label="{}".format(nx))
else:
n, r, e = smoothRun(nx, badBCs=1)
plt.plot(n, e, color)
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
plt.xlabel("# of iterations")
plt.ylabel("L2 norm of true error:\n first-order BCs (solid); second-order BCs (dotted)")
plt.legend(frameon=False, fontsize="small")
plt.savefig("smooth-badBCs.pdf")