Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 1241–1340 of 1640. Source text is displayed literally.
1241 ' sawtooth grows by the predicted factor per step', dev < 1e-12 and abs(gr[0] - abs(1 + pfac - math.sqrt(pfac * pfac + 2 * pfac))) < 1e-9, 'deviation %.1e; growth %.4f' % (dev, gr[0]))1242 1243 1244def wave_num(phi_, J, ratio, nsteps, dxx):1245 s = ratio * ratio1246 o_ = [phi_(j * dxx) for j in range(-J, J + 1)]1247 c_ = [o_[j] + 0.5 * s * (o_[j + 1] - 2 * o_[j] + o_[j - 1]) if 0 < j < 2 * J else o_[j] for j in range(2 * J + 1)]1248 for _ in range(nsteps - 1):1249 n_ = [0.0] * (2 * J + 1)1250 for j in range(1, 2 * J):1251 n_[j] = s * (c_[j + 1] + c_[j - 1]) + 2 * (1 - s) * c_[j] - o_[j]1252 o_, c_ = c_, n_1253 return c_[J]1254 1255 1256base = lambda x: math.exp(-x * x)1257pert = lambda x: base(x) + (math.exp(-1 / (1 - ((x - 1.0) / 0.2) ** 2)) if abs(x - 1.0) < 0.2 else 0.0)1258same = all(wave_num(base, 200, 2.0, n_, 1.0 / (2 * n_)) == wave_num(pert, 200, 2.0, n_, 1.0 / (2 * n_)) for n_ in (10, 20, 40))1259true_diff = 0.5 * (pert(1.0) + pert(-1.0)) - 0.5 * (base(1.0) + base(-1.0))1260check('N5 the stability condition is causality: at c dt = 2 dx the scheme at (0, 1) reads data only from [-1/2, 1/2], so it returns the same'1261 ' number for two data sets that differ near x = 1, at every refinement, while the exact solutions differ there by 1/(2e)',1262 same and abs(true_diff - 0.5 / math.e) < 1e-15)1263Ng = 161264mgrid = Ng - 11265 1266 1267def sweeps(method, omega=1.0, tol=1e-6, cap=5000):1268 e_ = [[random.uniform(-1, 1) if 0 < i < Ng and 0 < j < Ng else 0.0 for j in range(Ng + 1)] for i in range(Ng + 1)]1269 n0 = math.sqrt(sum(v * v for r in e_ for v in r))1270 for it in range(1, cap):1271 if method == 'J':1272 e_ = [[0.25 * (e_[i + 1][j] + e_[i - 1][j] + e_[i][j + 1] + e_[i][j - 1]) if 0 < i < Ng and 0 < j < Ng else 0.0 for j in range(Ng + 1)] for i in range(Ng + 1)]1273 else:1274 for i in range(1, Ng):1275 for j in range(1, Ng):1276 e_[i][j] += omega * (0.25 * (e_[i + 1][j] + e_[i - 1][j] + e_[i][j + 1] + e_[i][j - 1]) - e_[i][j])1277 if math.sqrt(sum(v * v for r in e_ for v in r)) < tol * n0:1278 return it1279 return cap1280 1281 1282random.seed(7)1283itJ, itG = sweeps('J'), sweeps('G')1284om = 2 / (1 + math.sin(PI / Ng))1285itS = sweeps('S', om)1286random.seed(20261118)1287rhoJ = math.cos(PI / Ng)1288check('N6 iteration for Laplace\'s equation on a 16 x 16 grid: Jacobi contracts by cos(pi/N) per sweep, Gauss-Seidel by its square (twice as fast,'1289 ' as the source says), and over-relaxation with omega = 2/(1 + sin(pi/N)) is an order of magnitude faster still',1290 abs(itJ / itG - 2) < 0.15 and itS < itG / 5 and 0.7 * math.log(1e-6) / math.log(rhoJ) < itJ <= math.log(1e-6) / math.log(rhoJ), 'sweeps to reduce a random error by 1e-6: %d, %d, %d; bound for Jacobi %d; omega = %.4f' % (itJ, itG, itS, math.log(1e-6) / math.log(rhoJ), om))1291 1292 1293def fem1d(N, f):1294 h = 1.0 / N1295 Kf = [[0.0] * (N - 1) for _ in range(N - 1)]1296 b_ = [0.0] * (N - 1)1297 for e in range(N): # element [e h, (e+1) h]1298 for a in (0, 1):1299 ia = e + a - 11300 if not 0 <= ia < N - 1:1301 continue1302 hat = (lambda x, e=e, a=a: (x - e * h) / h if a else ((e + 1) * h - x) / h)1303 b_[ia] += simpson(lambda x: f(x) * hat(x), e * h, (e + 1) * h, 40)1304 for bb in (0, 1):1305 ib = e + bb - 11306 if 0 <= ib < N - 1:1307 Kf[ia][ib] += (1.0 if a == bb else -1.0) / h1308 return solve(Kf, b_), Kf1309 1310 1311u8, K8 = fem1d(8, lambda x: PI ** 2 * math.sin(PI * x))1312u16, _ = fem1d(16, lambda x: PI ** 2 * math.sin(PI * x))1313nod = max(abs(u8[i] - math.sin(PI * (i + 1) / 8)) for i in range(7))1314 1315 1316def en_err(uh, N):1317 h = 1.0 / N1318 vals = [0.0] + uh + [0.0]1319 return math.sqrt(sum(simpson(lambda x: (PI * math.cos(PI * x) - (vals[e + 1] - vals[e]) / h) ** 2, e * h, (e + 1) * h, 40) for e in range(N)))1320 1321 1322check('N7 finite elements in one dimension: the stiffness matrix of hat functions is the difference Laplacian divided by h; the Galerkin solution'1323 ' is exact at the nodes; the energy error halves with h', all(abs(K8[i][j] - lap1(7)[i][j] * 8) < 1e-12 for i in range(7) for j in range(7))1324 and nod < 1e-9 and abs(en_err(u8, 8) / en_err(u16, 16) - 2) < 0.02, 'nodal error %.1e; energy errors %.4f, %.4f' % (nod, en_err(u8, 8), en_err(u16, 16)))1325 1326 1327def p1_stiffness(tri):1328 (x1, y1), (x2, y2), (x3, y3) = tri1329 area = abs((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 21330 g = [((y2 - y3), (x3 - x2)), ((y3 - y1), (x1 - x3)), ((y1 - y2), (x2 - x1))]1331 return [[(g[a][0] * g[b][0] + g[a][1] * g[b][1]) / (4 * area) for b in range(3)] for a in range(3)]1332 1333 1334Kg = {}1335for i in range(4):1336 for j in range(4):1337 for tri in (((i, j), (i + 1, j), (i, j + 1)), ((i + 1, j), (i + 1, j + 1), (i, j + 1))):1338 ke = p1_stiffness(tri)1339 for a in range(3):1340 for b in range(3):