Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-pde.py

Source for Partial Differential Equations in Higher Categories

View paper · Download original file

Lines 1480–1579 of 1640. Source text is displayed literally.

1480mK, kK = 0.8, 1.71481wK = math.sqrt(cl * cl * kK * kK + mK * mK)1482ukg = lambda x, t: math.cos(kK * x - wK * t)1483check('P6 the Klein-Gordon equation: plane waves with omega^2 = c^2 k^2 + m^2; the group velocity is below c, the phase velocity above, and their product is c^2',1484      abs(d2(lambda t: ukg(0.3, t), 0.4) - cl * cl * d2(lambda x: ukg(x, 0.4), 0.3) + mK * mK * ukg(0.3, 0.4)) < 1e-6 and cl * cl * kK / wK < cl < wK / kK)1485sig = [[[0, 1], [1, 0]], [[0, -1j], [1j, 0]], [[1, 0], [0, -1]]]1486g0 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 0], [0, 0, 0, -1]]1487gs = [g0] + [[[0, 0, s_[0][0], s_[0][1]], [0, 0, s_[1][0], s_[1][1]], [-s_[0][0], -s_[0][1], 0, 0], [-s_[1][0], -s_[1][1], 0, 0]] for s_ in sig]1488cm = lambda A, B: [[sum(A[i][k] * B[k][j] for k in range(4)) for j in range(4)] for i in range(4)]1489eta = [1, -1, -1, -1]1490okc = all(all(abs(cm(gs[a], gs[b])[i][j] + cm(gs[b], gs[a])[i][j] - (2 * eta[a] if a == b and i == j else 0)) < 1e-15 for i in range(4) for j in range(4)) for a in range(4) for b in range(4))1491pvec = [0.7, -0.3, 1.1, 0.4]1492slash = [[sum(pvec[a] * gs[a][i][j] for a in range(4)) for j in range(4)] for i in range(4)]1493sq_ = cm(slash, slash)1494m2 = pvec[0] ** 2 - sum(v * v for v in pvec[1:])1495check('P7 the Dirac matrices of the source satisfy the Clifford relations, so the Dirac operator squares to the Klein-Gordon operator',1496      okc and all(abs(sq_[i][j] - (m2 if i == j else 0)) < 1e-14 for i in range(4) for j in range(4)))1497Lk = [[lap1(6)[i][j] + (0.5 if i == j else 0.0) for j in range(6)] for i in range(6)]1498dt = 0.31499I6 = eye(6)1500half = [[I6[i][j] - 0.5 * dt * dt * Lk[i][j] for j in range(6)] for i in range(6)]1501# velocity Verlet: u' = (I - dt^2 L/2) u + dt v ; v' = v - dt/2 (L u + L u')1502Muu, Muv = half, [[dt * I6[i][j] for j in range(6)] for i in range(6)]1503LM = mm(Lk, Muu)1504Mvu = [[-0.5 * dt * (Lk[i][j] + LM[i][j]) for j in range(6)] for i in range(6)]1505LMv = mm(Lk, Muv)1506Mvv = [[I6[i][j] - 0.5 * dt * LMv[i][j] for j in range(6)] for i in range(6)]1507Mfull = [Muu[i] + Muv[i] for i in range(6)] + [Mvu[i] + Mvv[i] for i in range(6)]1508Jm = [[(1.0 if j == i + 6 else -1.0 if j == i - 6 else 0.0) for j in range(12)] for i in range(12)]1509sympl = mm(tr(Mfull), mm(Jm, Mfull))1510Eu = [I6[i] + Muv[i] for i in range(6)] + [[-dt * Lk[i][j] for j in range(6)] + I6[i] for i in range(6)]1511sympE = mm(tr(Eu), mm(Jm, Eu))1512check('P8 the solution space of a wave equation is symplectic and a good scheme keeps it so: the leapfrog step satisfies M^T J M = J; the Euler'1513      ' step does not', max(abs(sympl[i][j] - Jm[i][j]) for i in range(12) for j in range(12)) < 1e-12 and max(abs(sympE[i][j] - Jm[i][j]) for i in range(12) for j in range(12)) > 1e-2)1514 1515sec('14. Nonlinear equations')1516tb = math.sqrt(math.e / 2)1517mono = lambda t: min(1 + t * (-2 * z * math.exp(-z * z)) for z in [i * 0.001 for i in range(-4000, 4001)])1518check('X1 characteristics of u_t + u u_x = 0 first cross at t = -1/min phi\'; for phi = exp(-x^2) this is sqrt(e/2)', mono(tb - 1e-3) > 0 > mono(tb + 1e-3), 'breaking time %.5f' % tb)1519ust = lambda x, t: (1 + 2 * t * x - math.sqrt(1 + 4 * t * x)) / (2 * t * t)1520check('X2 the source\'s example with phi = x^2: u = (1 + 2tx - sqrt(1 + 4tx))/(2t^2) solves the equation and tends to x^2 as t -> 0',1521      max(abs(d1(lambda t: ust(x, t), t) + ust(x, t) * d1(lambda xx: ust(xx, t), x)) for x, t in [(0.5, 0.3), (2.0, 1.0)]) < 1e-8 and abs(ust(0.7, 1e-3) - 0.49) < 2e-3)1522 1523 1524def burgers(u0, conservative, T, dxx, lam=0.5):1525    u_ = list(u0)1526    for _ in range(round(T / (lam * dxx))):1527        if conservative:1528            Fl = [0.5 * v * v for v in u_]1529            u_ = [u_[0]] + [u_[j] - lam * (Fl[j] - Fl[j - 1]) for j in range(1, len(u_))]1530        else:1531            u_ = [u_[0]] + [u_[j] - lam * u_[j] * (u_[j] - u_[j - 1]) for j in range(1, len(u_))]1532    return u_1533 1534 1535dxx = 0.011536xs_ = [-1 + dxx * (j + 0.5) for j in range(400)]1537shock0 = [1.0 if x < 0 else 0.0 for x in xs_]1538front = lambda u_: next(xs_[j] for j in range(len(u_)) if u_[j] < 0.5)1539fc, fn = front(burgers(shock0, True, 2.0, dxx)), front(burgers(shock0, False, 2.0, dxx))1540rare = burgers([0.0 if x < 0 else 1.0 for x in xs_], True, 2.0, dxx)1541mid = rare[min(range(400), key=lambda j: abs(xs_[j] - 1.0))]1542check('X3 weak solutions are fixed by the conservation form: for the step from 1 to 0 a conservative scheme moves the shock at the Rankine-Hugoniot'1543      ' speed 1/2, a non-conservative one leaves it where it was; the step from 0 to 1 opens into the fan x/t',1544      abs(fc - 1.0) < 0.02 and abs(fn) < 0.02 and abs(mid - 0.5) < 0.05, 'front after time 2: conservative %.3f, non-conservative %.3f; fan value at x = t/2: %.3f' % (fc, fn, mid))1545eps = 0.051546terms_ = [(0.6, 1.3), (-0.4, 0.7), (1.5, 0.2)]1547ph_ = lambda x, t: sum(w * math.exp(a * x + eps * a * a * t) for a, w in terms_)1548uch = lambda x, t: -2 * eps * d1(lambda xx: ph_(xx, t), x) / ph_(x, t)1549uch2 = lambda x, t: -2 * eps * sum(w * a * math.exp(a * x + eps * a * a * t) for a, w in terms_) / ph_(x, t)1550rb_ = max(abs(d1(lambda t: uch2(x, t), t) + uch2(x, t) * d1(lambda xx: uch2(xx, t), x) - eps * d2(lambda xx: uch2(xx, t), x)) for x, t in [(0.2, 0.3), (-1.0, 1.0)])1551prof = lambda x, t, e: 1 / (1 + math.exp((x - t / 2) / (2 * e)))1552check('X4 Cole-Hopf is a morphism of equations: u = -2 eps phi_x/phi carries solutions of the heat equation to solutions of the viscous Burgers'1553      ' equation; the viscous profile from 1 to 0 travels at speed 1/2 and steepens to the shock as eps -> 0', rb_ < 1e-7 and prof(0.9, 2.0, 0.01) > 0.99 and prof(1.1, 2.0, 0.01) < 0.01, 'residual %.1e' % rb_)1554cK = 1.71555sol1 = lambda x, t: 0.5 * cK / math.cosh(0.5 * math.sqrt(cK) * (x - cK * t)) ** 21556rk_ = max(abs(d1(lambda t: sol1(x, t), t) + d3(lambda xx: sol1(xx, t), x) + 6 * sol1(x, t) * d1(lambda xx: sol1(xx, t), x)) for x, t in [(0.3, 0.2), (2.0, 1.0), (-1.0, 0.0)])1557check('X5 the soliton of the source solves the Korteweg-de Vries equation; its mass is 2 sqrt c and its momentum (2/3) c^{3/2}', rk_ < 1e-61558      and abs(simpson(lambda x: sol1(x, 0.0), -40, 40, 8000) - 2 * math.sqrt(cK)) < 1e-9 and abs(simpson(lambda x: sol1(x, 0.0) ** 2, -40, 40, 8000) - 2 / 3 * cK ** 1.5) < 1e-9, 'residual %.1e' % rk_)1559k1, k2 = 1.6, 1.01560Ak = ((k1 - k2) / (k1 + k2)) ** 21561 1562 1563def two(x, t):1564    e1_, e2_ = math.exp(k1 * x - k1 ** 3 * t), math.exp(k2 * x - k2 ** 3 * t)1565    F = 1 + e1_ + e2_ + Ak * e1_ * e2_1566    Fx = k1 * e1_ + k2 * e2_ + Ak * (k1 + k2) * e1_ * e2_1567    Fxx = k1 * k1 * e1_ + k2 * k2 * e2_ + Ak * (k1 + k2) ** 2 * e1_ * e2_1568    return 2 * (F * Fxx - Fx * Fx) / (F * F)1569 1570 1571r2s = max(abs(d1(lambda t: two(x, t), t) + d3(lambda xx: two(xx, t), x) + 6 * two(x, t) * d1(lambda xx: two(xx, t), x)) for x, t in [(0.3, 0.2), (2.0, 1.0), (-1.0, -0.7), (0.0, 0.0)])1572masses = [simpson(lambda x: two(x, t), -80, 120, 20000) for t in (-6.0, 0.0, 6.0)]1573 1574 1575def peak(t, guess):1576    a, b = guess - 1.5, guess + 1.51577    for _ in range(80):1578        m1, m2_ = a + (b - a) * 0.382, a + (b - a) * 0.6181579        if two(m1, t) < two(m2_, t):