Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 1201–1300 of 1640. Source text is displayed literally.
1201 1202 1203def exp_err(s, J):1204 dx = 1.0 / J1205 steps = round(0.1 / (s * dx * dx))1206 P = step_matrix(J - 1, s)1207 u_ = [math.sin(PI * (j + 1) * dx) for j in range(J - 1)]1208 for _ in range(steps):1209 u_ = mv(P, u_)1210 return max(abs(u_[j] - math.exp(-PI * PI * 0.1) * math.sin(PI * (j + 1) * dx)) for j in range(J - 1))1211 1212 1213r2_, r6_ = exp_err(0.5, 10) / exp_err(0.5, 20), exp_err(1 / 6, 10) / exp_err(1 / 6, 20)1214check('N2 consistency: halving the mesh divides the error by 4 at s = 1/2 and by 16 at the special ratio s = 1/6', abs(r2_ - 4) < 0.2 and abs(r6_ - 16) < 1.0, 'ratios %.2f, %.2f' % (r2_, r6_))1215cn = lambda s, th: (1 - s * (1 - math.cos(th))) / (1 + s * (1 - math.cos(th)))1216lamh = 2 * (1 - math.cos(PI / 40)) * 16001217cn_err = lambda dt: abs(((1 - lamh * dt / 2) / (1 + lamh * dt / 2)) ** round(0.1 / dt) - math.exp(-lamh * 0.1))1218check('N3 Crank-Nicolson is stable for every s and of second order in time', all(abs(cn(s, th)) <= 1 for s in (0.1, 1, 10, 100, 1e4) for th in (0.1, 1.0, PI))1219 and abs(cn_err(0.01) / cn_err(0.005) - 4) < 0.1, 'error ratio %.3f' % (cn_err(0.01) / cn_err(0.005)))1220c = 1.01221exact = lambda x, t: math.exp(-(x - c * t - 1) ** 2) + 0.5 * math.sin(x + c * t)1222dx = 0.11223old = [exact(j * dx, 0.0) for j in range(-80, 81)]1224cur = [exact(j * dx, dx) for j in range(-80, 81)]1225dev = 0.01226for n_ in range(2, 40):1227 new = [cur[j + 1] + cur[j - 1] - old[j] for j in range(1, len(cur) - 1)]1228 off = n_ - 11229 dev = max(dev, max(abs(new[j] - exact((j - 80 + off) * dx, n_ * dx)) for j in range(len(new))))1230 old, cur = cur[1:-1], new1231Np = 321232gr = []1233for s in (1.1,):1234 o_, c_ = [(-1.0) ** j for j in range(Np)], [(-1.0) ** j * 1.3 for j in range(Np)]1235 for _ in range(60):1236 n_ = [s * (c_[(j + 1) % Np] + c_[j - 1]) + 2 * (1 - s) * c_[j] - o_[j] for j in range(Np)]1237 o_, c_ = c_, n_1238 gr.append(abs(c_[0] / o_[0]))1239pfac = -2 * 1.11240check('N4 the wave scheme: at c dt = dx it is the parallelogram identity and reproduces exact solutions to rounding; at (c dt/dx)^2 = 1.1 the'1241 ' sawtooth grows by the predicted factor per step', dev < 1e-12 and abs(gr[0] - abs(1 + pfac - math.sqrt(pfac * pfac + 2 * pfac))) < 1e-9, 'deviation %.1e; growth %.4f' % (dev, gr[0]))1242 1243 1244def wave_num(phi_, J, ratio, nsteps, dxx):1245 s = ratio * ratio1246 o_ = [phi_(j * dxx) for j in range(-J, J + 1)]1247 c_ = [o_[j] + 0.5 * s * (o_[j + 1] - 2 * o_[j] + o_[j - 1]) if 0 < j < 2 * J else o_[j] for j in range(2 * J + 1)]1248 for _ in range(nsteps - 1):1249 n_ = [0.0] * (2 * J + 1)1250 for j in range(1, 2 * J):1251 n_[j] = s * (c_[j + 1] + c_[j - 1]) + 2 * (1 - s) * c_[j] - o_[j]1252 o_, c_ = c_, n_1253 return c_[J]1254 1255 1256base = lambda x: math.exp(-x * x)1257pert = lambda x: base(x) + (math.exp(-1 / (1 - ((x - 1.0) / 0.2) ** 2)) if abs(x - 1.0) < 0.2 else 0.0)1258same = all(wave_num(base, 200, 2.0, n_, 1.0 / (2 * n_)) == wave_num(pert, 200, 2.0, n_, 1.0 / (2 * n_)) for n_ in (10, 20, 40))1259true_diff = 0.5 * (pert(1.0) + pert(-1.0)) - 0.5 * (base(1.0) + base(-1.0))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: