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

1260check('N5 the stability condition is causality: at c dt = 2 dx the scheme at (0, 1) reads data only from [-1/2, 1/2], so it returns the same'1261      ' number for two data sets that differ near x = 1, at every refinement, while the exact solutions differ there by 1/(2e)',1262      same and abs(true_diff - 0.5 / math.e) < 1e-15)1263Ng = 161264mgrid = Ng - 11265 1266 1267def sweeps(method, omega=1.0, tol=1e-6, cap=5000):1268    e_ = [[random.uniform(-1, 1) if 0 < i < Ng and 0 < j < Ng else 0.0 for j in range(Ng + 1)] for i in range(Ng + 1)]1269    n0 = math.sqrt(sum(v * v for r in e_ for v in r))1270    for it in range(1, cap):1271        if method == 'J':1272            e_ = [[0.25 * (e_[i + 1][j] + e_[i - 1][j] + e_[i][j + 1] + e_[i][j - 1]) if 0 < i < Ng and 0 < j < Ng else 0.0 for j in range(Ng + 1)] for i in range(Ng + 1)]1273        else:1274            for i in range(1, Ng):1275                for j in range(1, Ng):1276                    e_[i][j] += omega * (0.25 * (e_[i + 1][j] + e_[i - 1][j] + e_[i][j + 1] + e_[i][j - 1]) - e_[i][j])1277        if math.sqrt(sum(v * v for r in e_ for v in r)) < tol * n0:1278            return it1279    return cap1280 1281 1282random.seed(7)1283itJ, itG = sweeps('J'), sweeps('G')1284om = 2 / (1 + math.sin(PI / Ng))1285itS = sweeps('S', om)1286random.seed(20261118)1287rhoJ = math.cos(PI / Ng)1288check('N6 iteration for Laplace\'s equation on a 16 x 16 grid: Jacobi contracts by cos(pi/N) per sweep, Gauss-Seidel by its square (twice as fast,'1289      ' as the source says), and over-relaxation with omega = 2/(1 + sin(pi/N)) is an order of magnitude faster still',1290      abs(itJ / itG - 2) < 0.15 and itS < itG / 5 and 0.7 * math.log(1e-6) / math.log(rhoJ) < itJ <= math.log(1e-6) / math.log(rhoJ), 'sweeps to reduce a random error by 1e-6: %d, %d, %d; bound for Jacobi %d; omega = %.4f' % (itJ, itG, itS, math.log(1e-6) / math.log(rhoJ), om))1291 1292 1293def fem1d(N, f):1294    h = 1.0 / N1295    Kf = [[0.0] * (N - 1) for _ in range(N - 1)]1296    b_ = [0.0] * (N - 1)1297    for e in range(N):                                           # element [e h, (e+1) h]1298        for a in (0, 1):1299            ia = e + a - 11300            if not 0 <= ia < N - 1:1301                continue1302            hat = (lambda x, e=e, a=a: (x - e * h) / h if a else ((e + 1) * h - x) / h)1303            b_[ia] += simpson(lambda x: f(x) * hat(x), e * h, (e + 1) * h, 40)1304            for bb in (0, 1):1305                ib = e + bb - 11306                if 0 <= ib < N - 1:1307                    Kf[ia][ib] += (1.0 if a == bb else -1.0) / h1308    return solve(Kf, b_), Kf1309 1310 1311u8, K8 = fem1d(8, lambda x: PI ** 2 * math.sin(PI * x))1312u16, _ = fem1d(16, lambda x: PI ** 2 * math.sin(PI * x))1313nod = max(abs(u8[i] - math.sin(PI * (i + 1) / 8)) for i in range(7))1314 1315 1316def en_err(uh, N):1317    h = 1.0 / N1318    vals = [0.0] + uh + [0.0]1319    return math.sqrt(sum(simpson(lambda x: (PI * math.cos(PI * x) - (vals[e + 1] - vals[e]) / h) ** 2, e * h, (e + 1) * h, 40) for e in range(N)))1320 1321 1322check('N7 finite elements in one dimension: the stiffness matrix of hat functions is the difference Laplacian divided by h; the Galerkin solution'1323      ' is exact at the nodes; the energy error halves with h', all(abs(K8[i][j] - lap1(7)[i][j] * 8) < 1e-12 for i in range(7) for j in range(7))1324      and nod < 1e-9 and abs(en_err(u8, 8) / en_err(u16, 16) - 2) < 0.02, 'nodal error %.1e; energy errors %.4f, %.4f' % (nod, en_err(u8, 8), en_err(u16, 16)))1325 1326 1327def p1_stiffness(tri):1328    (x1, y1), (x2, y2), (x3, y3) = tri1329    area = abs((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 21330    g = [((y2 - y3), (x3 - x2)), ((y3 - y1), (x1 - x3)), ((y1 - y2), (x2 - x1))]1331    return [[(g[a][0] * g[b][0] + g[a][1] * g[b][1]) / (4 * area) for b in range(3)] for a in range(3)]1332 1333 1334Kg = {}1335for i in range(4):1336    for j in range(4):1337        for tri in (((i, j), (i + 1, j), (i, j + 1)), ((i + 1, j), (i + 1, j + 1), (i, j + 1))):1338            ke = p1_stiffness(tri)1339            for a in range(3):1340                for b in range(3):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)):