Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 1540–1639 of 1640. Source text is displayed literally.
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):1580 a = m11581 else:1582 b = m2_1583 return 0.5 * (a + b)1584 1585 1586Tl = 12.01587shift1 = (peak(Tl, k1 * k1 * Tl - math.log(Ak) / k1) - k1 * k1 * Tl) - (peak(-Tl, -k1 * k1 * Tl) + k1 * k1 * Tl)1588shift2 = (peak(Tl, k2 * k2 * Tl) - k2 * k2 * Tl) - (peak(-Tl, -k2 * k2 * Tl - math.log(Ak) / k2) + k2 * k2 * Tl)1589check('X6 two solitons: the exact solution solves the equation, conserves mass 2(k1 + k2), and after the collision the fast soliton is ahead by'1590 ' (2/k1) log((k1+k2)/(k1-k2)) and the slow one behind by (2/k2) log((k1+k2)/(k1-k2))', r2s < 1e-5 and max(abs(m_ - 2 * (k1 + k2)) for m_ in masses) < 1e-81591 and abs(shift1 + math.log(Ak) / k1) < 1e-5 and abs(shift2 - math.log(Ak) / k2) < 1e-5, 'residual %.1e; shifts %.5f, %.5f' % (r2s, shift1, shift2))1592hq = 0.0251593xq = [-30 + hq * (i + 1) for i in range(2799)]1594spec = [tri_eigs([2 / hq ** 2 - two(x, t) for x in xq], [-1 / hq ** 2] * 2798, 2) for t in (0.0, 1.5)]1595check('X7 the flow is isospectral: the operator -d^2/dx^2 - u(., t) with the two-soliton potential has the eigenvalues -k1^2/4 and -k2^2/4 at'1596 ' every time', all(abs(sp[0] + k1 * k1 / 4) < 1e-3 and abs(sp[1] + k2 * k2 / 4) < 1e-3 for sp in spec), 'at t = 0: %.5f, %.5f; at t = 1.5: %.5f, %.5f' % (*spec[0], *spec[1]))1597cat = lambda x: 1.3 * math.cosh((x - 0.2) / 1.3)1598check('X8 calculus of variations: the catenary satisfies the Euler-Lagrange equation u u\'\' = 1 + u\'^2 of the area functional, and u/sqrt(1+u\'^2) is constant',1599 all(abs(cat(x) * d2(cat, x) - 1 - d1(cat, x) ** 2) < 1e-6 and abs(cat(x) / math.sqrt(1 + d1(cat, x) ** 2) - 1.3) < 1e-9 for x in (-0.8, 0.1, 1.4)))1600 1601 1602def ellK(m):1603 a, b = 1.0, math.sqrt(1 - m * m)1604 for _ in range(40):1605 a, b = 0.5 * (a + b), math.sqrt(a * b)1606 return PI / (2 * a)1607 1608 1609lb = 2.01610okb, lams = True, []1611for Amp in (0.2, 1.0, 2.0):1612 lam = (2 * ellK(math.sin(Amp / 2)) / lb) ** 21613 y = [Amp, 0.0]1614 for _ in range(4000):1615 y = rk4(lambda yy: [yy[1], -lam * math.sin(yy[0])], y, lb / 4000)1616 okb = okb and abs(y[1]) < 1e-8 and abs(y[0] + Amp) < 1e-81617 lams.append(lam / (PI / lb) ** 2)1618check('X9 buckling, u\'\' + lambda sin u = 0 with u\' = 0 at both ends: the first branch leaves the trivial solution at lambda = pi^2/l^2 and bends'1619 ' to the right, with lambda/lambda_1 = (2K(sin(A/2))/pi)^2 = 1 + A^2/8 + ...', okb and lams[0] < lams[1] < lams[2] and abs(lams[0] - 1 - 0.2 ** 2 / 8) < 1e-4,1620 'lambda/lambda_1 at amplitude 0.2, 1, 2: %.5f, %.5f, %.5f' % tuple(lams))1621gg, hh = 9.81, 3.01622 1623 1624def ww(kk):1625 om_ = math.sqrt(gg * kk * math.tanh(kk * hh))1626 pot = lambda x, z, t: math.cosh(kk * (z + hh)) * math.sin(kk * x - om_ * t)1627 harm = d2(lambda x: pot(x, -0.7, 0.3), 0.4) + d2(lambda z: pot(0.4, z, 0.3), -0.7)1628 surf = d2(lambda t: pot(0.4, 0.0, t), 0.3) + gg * d1(lambda z: pot(0.4, z, 0.3), 0.0)1629 bott = d1(lambda z: pot(0.4, z, 0.3), -hh)1630 return max(abs(harm), abs(surf), abs(bott)) / math.cosh(kk * hh), om_ / kk1631 1632 1633r1_, cmid = ww(0.9)1634cshal, cdeep = math.sqrt(gg * math.tanh(0.003 * hh) / 0.003), math.sqrt(gg * math.tanh(20.0 * hh) / 20.0)1635check('X10 linear water waves: the potential cosh(k(z+h)) sin(kx - omega t) is harmonic, has no flow through the bottom, and satisfies the surface'1636 ' condition exactly when omega^2 = g k tanh(kh); the speed is sqrt(gh) in shallow water and sqrt(g/k) in deep water',1637 r1_ < 1e-5 and abs(cshal - math.sqrt(gg * hh)) < 1e-3 and abs(cdeep - math.sqrt(gg / 20.0)) < 1e-12)1638 1639footer()