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 1421–1520 of 1640. Source text is displayed literally.

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 - V1441    if abs(q2) < 1e-14:1442        return [[1.0, w], [0.0, 1.0]]1443    q = cmath.sqrt(q2)1444    return [[cmath.cos(q * w), cmath.sin(q * w) / q], [-q * cmath.sin(q * w), cmath.cos(q * w)]]1445 1446 1447def mm2(A, B):1448    return [[A[0][0] * B[0][0] + A[0][1] * B[1][0], A[0][0] * B[0][1] + A[0][1] * B[1][1]], [A[1][0] * B[0][0] + A[1][1] * B[1][0], A[1][0] * B[0][1] + A[1][1] * B[1][1]]]1449 1450 1451def RT(Pm, k):1452    # P (1 + R, ik(1 - R)) = (T, ikT)1453    a = [[Pm[0][0] - 1j * k * Pm[0][1], -1.0], [Pm[1][0] - 1j * k * Pm[1][1], -1j * k]]1454    b = [-(Pm[0][0] + 1j * k * Pm[0][1]), -(Pm[1][0] + 1j * k * Pm[1][1])]1455    det_ = a[0][0] * a[1][1] - a[0][1] * a[1][0]1456    return (b[0] * a[1][1] - a[0][1] * b[1]) / det_, (a[0][0] * b[1] - b[0] * a[1][0]) / det_1457 1458 1459segs = [(2.0, 0.7), (0.0, 0.5), (-1.5, 0.9), (3.0, 0.4)]1460oku, okd, okf = True, True, True1461for k in (0.6, 1.3, 2.4):1462    Pm = [[1.0, 0.0], [0.0, 1.0]]1463    for V, w in segs:1464        Pm = mm2(prop(k, V, w), Pm)1465    R_, T_ = RT(Pm, k)1466    oku = oku and abs(abs(R_) ** 2 + abs(T_) ** 2 - 1) < 1e-121467    okd = okd and abs(Pm[0][0] * Pm[1][1] - Pm[0][1] * Pm[1][0] - 1) < 1e-121468    # the same matrix by integrating the equation across the whole potential1469    cols = []1470    for y0 in ([1.0, 0.0], [0.0, 1.0]):1471        y, x = list(y0), 0.01472        for V, w in segs:1473            hstep = w / 4001474            for _ in range(400):1475                y = rk4(lambda yy, V=V: [yy[1], (V - k * k) * yy[0]], y, hstep)1476        cols.append(y)1477    okf = okf and max(abs(cols[j][i] - Pm[i][j]) for i in range(2) for j in range(2)) < 1e-81478check('P5 scattering by a potential: transfer matrices compose under concatenation (a functor to SL(2)), and the resulting reflection and transmission'1479      ' coefficients satisfy |R|^2 + |T|^2 = 1, the source\'s theorem', oku and okd and okf)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',