Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 460–559 of 1640. Source text is displayed literally.
460n = 7461D = [[(-1 if j == i else 1 if j == i + 1 else 0) for j in range(n)] for i in range(n - 1)]462DtD = mm(tr(D), D)463check('W3 conservation plus a constitutive law: the Neumann Laplacian of a path is D^T D for the incidence matrix D, and its columns sum to zero'464 ' (mass is conserved)', all(DtD[i][j] == lap1(n, 'N')[i][j] for i in range(n) for j in range(n)) and all(abs(sum(DtD[i][j] for i in range(n))) < 1e-12 for j in range(n)))465L = lap1(20)466u = [random.uniform(-1, 1) for _ in range(20)]467mono, uu = True, list(u)468for _ in range(200):469 e0 = dot(uu, uu)470 uu = rk4(lambda y: [-z for z in mv(L, y)], uu, 0.01)471 mono = mono and dot(uu, uu) <= e0472st = u + [random.uniform(-1, 1) for _ in range(20)]473energy = lambda s: 0.5 * dot(s[20:], s[20:]) + 0.5 * dot(s[:20], mv(L, s[:20]))474E0 = energy(st)475for _ in range(1000):476 st = rk4(lambda s: s[20:] + [-z for z in mv(L, s[:20])], st, 0.005)477check('W4 the two energy laws on a grid of 20 points: the square norm decreases along the diffusion flow, and the wave energy is conserved',478 mono and abs(energy(st) - E0) < 1e-8 * E0, 'relative drift of the wave energy over time 5: %.1e' % (abs(energy(st) - E0) / E0))479LN = [[int(v) for v in r] for r in lap1(6, 'N')]480f_ok, f_bad = [1, -2, 3, 0, -1, -1], [1, -2, 3, 0, -1, 0]481check('W5 the Neumann problem: kernel the constants, solvable exactly when the source has mean zero (exact ranks)',482 rank_exact(LN) == 5 and rank_exact([r + [b] for r, b in zip(LN, f_ok)]) == 5 and rank_exact([r + [b] for r, b in zip(LN, f_bad)]) == 6)483check('W6 the source\'s first example of non-uniqueness: u\'\' + u = 0, u(0) = u(L) = 0 has a non-zero solution exactly when sin L = 0',484 abs(math.sin(PI)) < 1e-15 and abs(math.sin(2.0)) > 0.5)485 486sec('3. Waves')487c = 2.0488phi = lambda x: math.exp(-x * x)489psi = lambda x: x * math.exp(-x * x)490Psi = lambda x: -0.5 * math.exp(-x * x) # antiderivative of psi491dal = lambda x, t, ph=phi, PS=Psi: 0.5 * (ph(x + c * t) + ph(x - c * t)) + (PS(x + c * t) - PS(x - c * t)) / (2 * c)492pts = [(0.3, 0.2), (-1.1, 0.7), (2.0, 1.3)]493res = max(abs(d2(lambda t: dal(x, t), t) - c * c * d2(lambda xx: dal(xx, t), x)) for x, t in pts)494ini = max(abs(dal(x, 0.0) - phi(x)) + abs(d1(lambda t: dal(x, t), 0.0) - psi(x)) for x in (-1.0, 0.2, 0.9))495check('V1 d\'Alembert\'s formula solves the wave equation with the given data', res < 1e-6 and ini < 1e-9, 'residual %.1e' % res)496par = 0.0497for _ in range(50):498 xi, eta, h, kk = random.uniform(-2, 2), random.uniform(-2, 2), random.uniform(0.1, 1), random.uniform(0.1, 1)499 U = lambda a, b: dal((a + b) / 2, (a - b) / (2 * c))500 par = max(par, abs(U(xi + h, eta + kk) - U(xi + h, eta) - U(xi, eta + kk) + U(xi, eta)))501check('V2 in light-cone coordinates the equation is u_{xi eta} = 0: the sum over opposite corners of every characteristic parallelogram agrees',502 par < 1e-14, 'largest defect %.1e' % par)503x0, t0 = 0.4, 0.5504bump = lambda x, a: (math.exp(-1 / (1 - (x - a) ** 2)) if abs(x - a) < 1 else 0.0)505out_ = dal(x0, t0, lambda x: phi(x) + bump(x, x0 + c * t0 + 1.5)) - dal(x0, t0)506in_ = dal(x0, t0, lambda x: phi(x) + bump(x, x0 + c * t0 - 0.5)) - dal(x0, t0)507check('V3 causality: data changed outside the interval of dependence do not change the solution; data changed inside do', out_ == 0.0 and abs(in_) > 1e-3)508ut = lambda x, t: d1(lambda s: dal(x, s), t)509ux = lambda x, t: d1(lambda s: dal(s, t), x)510En = [simpson(lambda x: 0.5 * (ut(x, t) ** 2 + c * c * ux(x, t) ** 2), -12, 12, 1200) for t in (0.0, 0.7, 1.9)]511check('V4 energy is conserved', max(En) - min(En) < 1e-7, 'E = %.8f, %.8f, %.8f' % tuple(En))512odd = lambda g: (lambda x: g(x) if x > 0 else -g(-x) if x < 0 else 0.0)513ph2 = lambda x: x * math.exp(-(x - 2) ** 2)514ps2 = lambda x: math.sin(x) * math.exp(-x)515Iq = lambda g, a, b: simpson(g, a, b, 400)516vodd = lambda x, t: 0.5 * (odd(ph2)(x + c * t) + odd(ph2)(x - c * t)) + Iq(odd(ps2), x - c * t, x + c * t) / (2 * c)517vsrc = lambda x, t: (0.5 * (ph2(x + c * t) + ph2(x - c * t)) + Iq(ps2, x - c * t, x + c * t) / (2 * c) if x > c * t else518 0.5 * (ph2(c * t + x) - ph2(c * t - x)) + Iq(ps2, c * t - x, c * t + x) / (2 * c))519dv = max(abs(vodd(x, t) - vsrc(x, t)) for x, t in [(0.5, 0.1), (0.5, 1.0), (3.0, 0.4), (1.0, 2.0)])520check('V5 reflection: the Dirichlet problem on the half-line is the odd part of the problem on the line; the result is the source\'s'521 ' two-region formula and vanishes at the boundary', dv < 1e-6 and abs(vodd(0.0, 0.8)) < 1e-12, 'largest difference %.1e' % dv)522l = 1.0523ph3 = lambda x: x * (1 - x)524 525 526def ext(x):527 x = (x + l) % (2 * l) - l528 return ph3(x) if x >= 0 else -ph3(-x)529 530 531vim = lambda x, t: 0.5 * (ext(x + c * t) + ext(x - c * t))532vfs = lambda x, t: sum(8 / (n * PI) ** 3 * math.cos(n * PI * c * t / l) * math.sin(n * PI * x / l) for n in range(1, 4001, 2))533dv = max(abs(vim(x, t) - vfs(x, t)) for x, t in [(0.3, 0.2), (0.7, 1.4), (0.1, 3.3)])534check('V6 the finite interval: the sum over the reflection group (the infinite dihedral group) equals the sum over eigenfunctions,'535 ' and the motion has period 2l/c', dv < 1e-7 and abs(vim(0.3, 0.2 + 2 * l / c) - vim(0.3, 0.2)) < 1e-14, 'largest difference %.1e' % dv)536duh = lambda x, t: simpson(lambda s: simpson(lambda y: math.cos(y), x - c * (t - s), x + c * (t - s), 200), 0.0, t, 400) / (2 * c)537dd = max(abs(duh(x, t) - math.cos(x) * (1 - math.cos(c * t)) / c ** 2) for x, t in [(0.3, 0.9), (1.7, 2.2)])538check('V7 Duhamel: the source\'s formula for the forced wave equation, against the closed form for the force cos x', dd < 1e-7, 'difference %.1e' % dd)539dal2 = lambda x, t: dal(x, t, lambda z: math.exp(-(z - 1) ** 2), lambda z: 0.3 * math.exp(-z * z) * z)540Om = [simpson(lambda x: dal(x, t) * d1(lambda s: dal2(x, s), t) - d1(lambda s: dal(x, s), t) * dal2(x, t), -12, 12, 1200) for t in (0.0, 1.3)]541check('V8 the solution space is symplectic: the Wronskian form of two solutions does not depend on the time at which it is computed',542 abs(Om[0] - Om[1]) < 1e-7 and abs(Om[0]) > 1e-3, 'Omega = %.8f, %.8f' % tuple(Om))543 544sec('4. Diffusions')545kd = 0.7546S = lambda x, t: heat(x, t, kd)547res = max(abs(d1(lambda t: S(x, t), t) - kd * d2(lambda xx: S(xx, t), x)) for x, t in [(0.3, 0.5), (-1.2, 1.1)])548ck = abs(simpson(lambda y: S(0.4 - y, 0.3) * S(y - (-0.2), 0.9), -15, 15, 3000) - S(0.6, 1.2))549check('D1 the heat kernel is a Markov semigroup: it solves the equation, has total mass one, and composes (Chapman-Kolmogorov)',550 res < 1e-7 and abs(simpson(lambda y: S(y, 0.4), -15, 15, 3000) - 1) < 1e-12 and ck < 1e-12, 'residual %.1e, composition defect %.1e' % (res, ck))551dq = max(abs(simpson(lambda y: S(x - y, t), 0, 30, 6000) - 0.5 * (1 + math.erf(x / math.sqrt(4 * kd * t)))) for x, t in [(0.3, 0.2), (-1.0, 0.8)])552check('D2 a unit step diffuses into (1 + erf(x / sqrt(4kt)))/2', dq < 1e-9)553 554 555def step_matrix(n, s, bc='D'):556 Lm = lap1(n, bc)557 return [[(1.0 if i == j else 0.0) - s * Lm[i][j] for j in range(n)] for i in range(n)]558 559