Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 1280–1379 of 1640. Source text is displayed literally.
1280 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):1341 Kg[(tri[a], tri[b])] = Kg.get((tri[a], tri[b]), 0.0) + ke[a][b]1342row = {q: Kg.get(((2, 2), q), 0.0) for q in [(2, 2), (3, 2), (1, 2), (2, 3), (2, 1), (3, 3), (1, 1), (3, 1), (1, 3)]}1343check('N8 finite elements in the plane: linear elements on a uniform triangulation of the square give the five-point scheme',1344 [round(v, 12) for v in row.values()] == [4, -1, -1, -1, -1, 0, 0, 0, 0])1345mr = 61346Vn = [('i', k) for k in range(mr)] + [('o', k) for k in range(mr)]1347vid = {v: k for k, v in enumerate(Vn)}1348tris = []1349for k in range(mr):1350 k1 = (k + 1) % mr1351 tris += [(('i', k), ('i', k1), ('o', k)), (('i', k1), ('o', k1), ('o', k))]1352edges = sorted({tuple(sorted((vid[t[a]], vid[t[b]]))) for t in tris for a, b in ((0, 1), (1, 2), (0, 2))})1353eid = {e: k for k, e in enumerate(edges)}1354d0 = [[(1 if v == e[1] else -1 if v == e[0] else 0) for v in range(len(Vn))] for e in edges]1355d1m = []1356for t in tris:1357 a, b, c3 = (vid[v] for v in t)1358 rowt = [0] * len(edges)1359 for (p, q) in ((a, b), (b, c3), (c3, a)):1360 rowt[eid[tuple(sorted((p, q)))]] += 1 if p < q else -11361 d1m.append(rowt)1362dd0 = all(sum(d1m[t][e] * d0[e][v] for e in range(len(edges))) == 0 for t in range(len(tris)) for v in range(len(Vn)))1363rk0, rk1 = rank_exact(d0), rank_exact(d1m)1364betti = (len(Vn) - rk0, len(edges) - rk0 - rk1, len(tris) - rk1)1365check('N9 a discrete de Rham complex: on a triangulated annulus d d = 0 and the Betti numbers are (1, 1, 0); the one harmonic 1-cochain is the'1366 ' discrete counterpart of the class of d theta', dd0 and betti == (1, 1, 0), 'cells %d, %d, %d' % (len(Vn), len(edges), len(tris)))1367rich = lambda s, th: max(abs(-2 * s * (1 - math.cos(th)) + sg * math.sqrt((2 * s * (1 - math.cos(th))) ** 2 + 1)) for sg in (1, -1))1368check('N10 consistency is not enough (Lax): the centred three-level scheme for the heat equation is second-order accurate and has an amplification'1369 ' factor larger than one for every s > 0', all(rich(s, th) > 1 for s in (0.001, 0.1, 0.5) for th in (0.3, PI)), 'at s = 0.1, k dx = pi: %.4f' % rich(0.1, PI))1370 1371sec('13. Fields')1372 1373 1374def torus_complex(n):1375 V = [(i, j, k) for i in range(n) for j in range(n) for k in range(n)]1376 vi = {v: a for a, v in enumerate(V)}1377 sh = lambda v, d: tuple((v[q] + (1 if q == d else 0)) % n for q in range(3))1378 E = [(v, d) for v in V for d in range(3)]1379 ei = {e: a for a, e in enumerate(E)}