"""Parallel version of build_series.py (same root scan, same coefficient formulas), for q_max = 480. Roots for each order 2n are found independently (multiprocessing); coefficients as in build_series.py.""" import sys, json, time, numpy as np from multiprocessing import Pool from scipy.integrate import quad from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) from pbvp import * import pbvp # For q >~ 300 the value ce_2n(0,q) used by pbvp as the initial value of Ce becomes smaller than roundoff # (cancellation in the Fourier sum), so its sign is unreliable and the root scan stalls. The radial ODE is linear, # so the initial value only scales Ce: here Ce is started at 1 instead. Roots and the series V are unchanged by this # choice (the coefficient N/D absorbs the scale). Mode is replaced by ModeN for building AND evaluating this file. class ModeN(pbvp.Mode): def __init__(self, n2, q): self.n2, self.q = n2, q self.a, self.A = ce_coeffs(n2, q) self.solA = radial(self.a, q, 1.0, 'A', alpha0*1.0000001) self.solC = radial(self.a, q, (self.A*(-1.0)**np.arange(len(self.A))).sum(), 'C', gamma0*1.0000001) self.C0 = self.solC.sol(gamma0)[0] def roots_qN(n2, qmax, dq=0.2, rel=0.015): def f(q): a, A = ce_coeffs(n2, q); return radial(a, q, 1.0, 'A', alpha0, dense=False).y[0, -1] qs = [dq] while qs[-1] < qmax: qs.append(qs[-1] + max(dq, rel*qs[-1])) vals = [f(q) for q in qs]; out = [] for i in range(len(qs)-1): if np.sign(vals[i]) != np.sign(vals[i+1]): out.append(brentq(f, qs[i], qs[i+1], xtol=1e-14, rtol=1e-14)) return out QMAX = float(sys.argv[1]) if len(sys.argv) > 1 and sys.argv[1].replace('.', '').isdigit() else 480.0 def roots_for(n2): t = time.time(); rs = roots_qN(n2, QMAX, dq=0.2, rel=0.015) print(f' order {n2}: {len(rs)} roots in {time.time()-t:.0f}s', flush=True); return n2, rs def coef_for(args): n2, m, q = args M = ModeN(n2, q) Ia = [quad(lambda t, k=k: M.Afun(t)**k[0]*np.cosh(2*t)**k[1], 0, alpha0, limit=400, epsabs=0, epsrel=1e-12)[0] for k in [(1, 1), (1, 0), (2, 1), (2, 0)]] Ib = [quad(lambda t, k=k: M.Bfun(t)**k[0]*np.cos(2*t)**k[1], -np.pi, np.pi, limit=400, epsabs=0, epsrel=1e-12)[0] for k in [(1, 0), (1, 1), (2, 0), (2, 1)]] Nq = Ia[0]*Ib[0] - Ia[1]*Ib[1]; Dq = Ia[2]*Ib[2] - Ia[3]*Ib[3] green = -(1/(2*q))*M.dA(alpha0)*2*np.pi*M.A[0] al = np.linspace(0, alpha0*(1-1e-9), 4001); Av = M.Afun(al) nz = int(np.sum(np.sign(Av[1:]) != np.sign(Av[:-1]))) return dict(nzeros=nz, n2=n2, m=m, q=q, a=M.a, coef=Nq/Dq, N=Nq, D=Dq, green=green, C0=M.C0, Ce_alpha0=float(M.Afun(alpha0)), Ib0=Ib[0], Ib2=Ib[2]) if __name__ == '__main__': t0 = time.time() with Pool(8) as P: res = dict(P.map(roots_for, range(0, int(sys.argv[2]) if len(sys.argv) > 2 else 80, 2), chunksize=1)) orders = sorted(n2 for n2 in res if res[n2]) assert orders == list(range(0, orders[-1]+1, 2)), orders modes = [(n2, m, q) for n2 in orders for m, q in enumerate(res[n2])] print(f'{len(modes)} modes with q <= {QMAX}, orders 2n = 0..{orders[-1]}; roots in {time.time()-t0:.0f}s', flush=True) with Pool(8) as P: out = P.map(coef_for, modes, chunksize=2) bad = [(o['n2'], o['m'], o['nzeros']) for o in out if o['nzeros'] != o['m']] print('Sturm check: modes whose number of interior zeros of Ce in (0,alpha0) != m:', bad) json.dump(out, open(f'series_Q{int(QMAX)}.json', 'w'), indent=0) g = max(abs(o['N']-o['green'])/max(abs(o['N']), 1e-300) for o in out) print(f'max |quadrature - Green| / |N| = {g:.1e}; max |Ce(alpha0)| = {max(abs(o["Ce_alpha0"]) for o in out):.1e};' f' max |int B^2 - pi| = {max(abs(o["Ib2"]-np.pi) for o in out):.1e}; total {time.time()-t0:.0f}s')