Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 1341–1440 of 1640. Source text is displayed literally.
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)}1380 Fc = [(v, d) for v in V for d in range(3)] # face at v normal to direction d1381 fi = {f_: a for a, f_ in enumerate(Fc)}1382 d0_ = [[0] * len(V) for _ in E]1383 for (v, d), a in ei.items():1384 d0_[a][vi[sh(v, d)]] += 11385 d0_[a][vi[v]] -= 11386 d1_ = [[0] * len(E) for _ in Fc]1387 for (v, d), a in fi.items():1388 p, q = (d + 1) % 3, (d + 2) % 31389 d1_[a][ei[(v, p)]] += 11390 d1_[a][ei[(sh(v, p), q)]] += 11391 d1_[a][ei[(sh(v, q), p)]] -= 11392 d1_[a][ei[(v, q)]] -= 11393 d2_ = [[0] * len(Fc) for _ in V]1394 for v, a in vi.items():1395 for d in range(3):1396 d2_[a][fi[(sh(v, d), d)]] += 11397 d2_[a][fi[(v, d)]] -= 11398 return d0_, d1_, d2_1399 1400 1401def zero_prod(A, B):1402 Bt = list(zip(*B))1403 return all(sum(a * b for a, b in zip(r, c_)) == 0 for r in A for c_ in Bt)1404 1405 1406e0, e1, e2 = torus_complex(3)1407t0, t1, t2 = torus_complex(2)1408rk = (rank_exact(t0), rank_exact(t1), rank_exact(t2))1409bt = (8 - rk[0], 24 - rk[0] - rk[1], 24 - rk[1] - rk[2], 8 - rk[2])1410Apot = [random.randint(-3, 3) for _ in range(81)]1411chi = [random.randint(-3, 3) for _ in range(27)]1412F1 = [sum(a * b for a, b in zip(r, Apot)) for r in e1]1413F2 = [sum(a * b for a, b in zip(r, [x + sum(p * q for p, q in zip(rr, chi)) for x, rr in zip(Apot, e0)])) for r in e1]1414check('P1 on a cubical 3-torus curl grad = 0 and div curl = 0 exactly; the field strength dA is unchanged by a gauge transformation;'1415 ' the Betti numbers are (1, 3, 3, 1), so there are closed potentials that are not gradients', zero_prod(e1, e0) and zero_prod(e2, e1) and F1 == F2 and bt == (1, 3, 3, 1))1416cl = 2.01417fw = lambda s: math.exp(-s * s) * math.sin(2 * s)1418Ey = lambda x, t: fw(x - cl * t)1419Bz = lambda x, t: fw(x - cl * t)1420mres = max(abs(d1(lambda t: Ey(0.3, t), 0.2) - cl * (-d1(lambda x: Bz(x, 0.2), 0.3))), abs(d1(lambda t: Bz(0.3, t), 0.2) + cl * d1(lambda x: Ey(x, 0.2), 0.3)))1421check('P2 Maxwell\'s equations in the source\'s form: the transverse plane wave E = (0, f(x - ct), 0), B = (0, 0, f(x - ct)) solves them', mres < 1e-8)1422gam, rho0 = 1.4, 1.21423cs = math.sqrt(gam * rho0 ** (gam - 1))1424trA, detA = 0.0, -(gam * rho0 ** (gam - 1))1425eigs = sorted([(trA - math.sqrt(trA * trA - 4 * detA)) / 2, (trA + math.sqrt(trA * trA - 4 * detA)) / 2])1426check('P3 acoustics is the tangent of fluid dynamics: the linearization of the one-dimensional Euler equations at rest has characteristic speeds'1427 ' plus and minus the sound speed', abs(eigs[0] + cs) < 1e-12 and abs(eigs[1] - cs) < 1e-12, 'sound speed %.5f' % cs)1428c1, c2 = 1.0, 3.01429Rr, Tt = (c2 - c1) / (c2 + c1), 2 * c2 / (c2 + c1)1430fin = lambda s: math.exp(-(s + 4) ** 2)1431uL = lambda x, t: fin(x - c1 * t) + Rr * fin(-x - c1 * t)1432uR = lambda x, t: Tt * fin((c1 / c2) * (x - c2 * t))1433jump = max(abs(uL(0.0, t) - uR(0.0, t)) + abs(d1(lambda x: uL(x, t), 0.0) - d1(lambda x: uR(x, t), 0.0)) for t in (3.0, 4.0, 4.6))1434check('P4 scattering on a string of two materials: reflection (c2-c1)/(c2+c1), transmission 2c2/(c2+c1); the solution and its slope are continuous,'1435 ' and the energy flux balances', jump < 1e-9 and abs(Rr ** 2 + (c1 / c2) * Tt ** 2 - 1) < 1e-15)1436 1437 1438def prop(k, V, w):1439 """transfer matrix of -psi'' + V psi = k^2 psi across a segment of constant potential, in the basis (psi, psi')"""1440 q2 = k * k - V