"""Pre-registered analysis (code/RULES_reverse_flux.txt): split the axial dipole g10 of gufm1 and COV-OBS.x2 into contributions from normal and reversed radial flux at the core-mantle boundary in each geographic hemisphere, using g10 = (3/(8 pi)) (c/a)^3 * integral(Br cos theta dOmega). Reads the model files as data. Needs numpy. Run: py -3.12 -I code/reverse_flux.py Fact-Checked Science, 2026-11-09. """ import json import math import os import sys import numpy as np A_KM, C_KM = 6371.2, 3485.0 def numbers(text): out = [] for tok in text.replace('D', 'E').split(): try: out.append(float(tok)) except ValueError: pass return out class SplineModel: def __init__(self, lmax, nspl, knots, coefs, order=4): n = lmax * (lmax + 2) assert len(knots) == nspl + order and len(coefs) == nspl * n self.lmax, self.nspl, self.k = lmax, nspl, order self.U = np.array(knots, float) self.C = np.array(coefs, float).reshape(nspl, n) def basis(self, t): U, k, nspl = self.U, self.k, self.nspl assert U[k - 1] - 1e-9 <= t <= U[nspl] + 1e-9 B = np.zeros(len(U) - 1) if t >= U[nspl]: B[nspl - 1] = 1.0 else: B[np.searchsorted(U, t, side='right') - 1] = 1.0 for kk in range(2, k + 1): Bn = np.zeros(len(U) - kk) for i in range(len(U) - kk): v = 0.0 d1, d2 = U[i + kk - 1] - U[i], U[i + kk] - U[i + 1] if d1 > 0 and B[i]: v += (t - U[i]) / d1 * B[i] if d2 > 0 and B[i + 1]: v += (U[i + kk] - t) / d2 * B[i + 1] Bn[i] = v B = Bn return B[:nspl] def coeffs(self, t): return self.basis(t) @ self.C def read_gufm1(path): lines = open(path, encoding='ascii').read().split('\n') v = numbers('\n'.join(lines[1:])) lmax, nspl = int(v[0]), int(v[1]) return SplineModel(lmax, nspl, v[2:2 + nspl + 4], v[2 + nspl + 4:]) def read_covobs(path): lines = open(path, encoding='ascii').read().split('\n') head = lines[1].split() lmax, nspl, order = int(head[0]), int(head[1]), int(head[2]) return SplineModel(lmax, nspl, [float(x) for x in head[3:]], numbers('\n'.join(lines[2:])), order) def schmidt_table(theta, nmax): """P[n, m, j] Schmidt semi-normalised at colatitudes theta[j].""" c, s = np.cos(theta), np.sin(theta) P = np.zeros((nmax + 1, nmax + 1, len(theta))) P[0, 0] = 1.0 for n in range(1, nmax + 1): for m in range(0, n + 1): if n == m: f = math.sqrt((2 * n - 1) / (2 * n)) if n > 1 else 1.0 P[n, m] = f * s * P[n - 1, m - 1] else: k1 = (2 * n - 1) / math.sqrt(n * n - m * m) k2 = math.sqrt(((n - 1) ** 2 - m * m) / (n * n - m * m)) if n > 1 else 0.0 P[n, m] = k1 * c * P[n - 1, m] - (k2 * P[n - 2, m] if n > 1 else 0.0) return P def br_grid(flat, lmax_model, L, P, phi): """Br at the CMB on the (theta, phi) grid, truncated at degree L.""" nth, nph = P.shape[2], len(phi) Br = np.zeros((nth, nph)) k = 0 for n in range(1, lmax_model + 1): fac = (n + 1) * (A_KM / C_KM) ** (n + 2) for m in range(0, n + 1): if m == 0: g, h = flat[k], 0.0 k += 1 else: g, h = flat[k], flat[k + 1] k += 2 if n > L: continue Br += fac * np.outer(P[n, m], g * np.cos(m * phi) + h * np.sin(m * phi)) return Br def decompose(flat, lmax_model, L, P, theta, phi): Br = br_grid(flat, lmax_model, L, P, phi) dA = (np.sin(theta) * math.radians(1.0) * math.radians(1.0))[:, None] * np.ones((1, len(phi))) w = (3.0 / (8.0 * math.pi)) * (C_KM / A_KM) ** 3 * np.cos(theta)[:, None] * dA north = (theta < math.pi / 2)[:, None] * np.ones((1, len(phi)), bool) south = ~north parts = { 'N-normal': float((Br * w)[north & (Br < 0)].sum()), 'N-reversed': float((Br * w)[north & (Br > 0)].sum()), 'S-normal': float((Br * w)[south & (Br > 0)].sum()), 'S-reversed': float((Br * w)[south & (Br < 0)].sum()), } parts['sum'] = sum(parts.values()) parts['S-reversed area share of S hemisphere'] = float(dA[south & (Br < 0)].sum() / dA[south].sum()) return parts def main(): D, OUT = sys.argv[1], sys.argv[2] os.makedirs(OUT, exist_ok=True) theta = np.radians(np.arange(0.5, 180.0, 1.0)) phi = np.radians(np.arange(0.5, 360.0, 1.0)) P = schmidt_table(theta, 14) models = {'gufm1': (read_gufm1(os.path.join(D, 'gufm1', 'gufm1.txt')), range(1590, 1991, 10)), 'COV-OBS.x2': (read_covobs(os.path.join(D, 'cov-obs-x2', 'COV-OBS.x2-int')), range(1840, 2021, 10))} res = {} worst = 0.0 for name, (mdl, years) in models.items(): res[name] = {} for L in (14, 8): rows = {} for t in years: flat = mdl.coeffs(float(t)) parts = decompose(flat, mdl.lmax, L, P, theta, phi) parts['g10_model'] = float(flat[0]) if L == 14: worst = max(worst, abs(parts['sum'] - flat[0]) / abs(flat[0])) rows[t] = {k: round(v, 4 if 'share' in k else 1) for k, v in parts.items()} def change(a, b): if a not in rows or b not in rows: return None dg = rows[b]['g10_model'] - rows[a]['g10_model'] out = {'delta_g10': round(dg, 1)} for k in ('N-normal', 'N-reversed', 'S-normal', 'S-reversed'): d = rows[b][k] - rows[a][k] out['delta_' + k] = round(d, 1) out['share_of_delta_' + k] = round(d / dg, 3) if dg else None return out readings = {} c = change(1840, 1990) readings['R-a S-reversed carries >= half of 1840-1990 fall'] = bool(c and c['share_of_delta_S-reversed'] >= 0.5) if name == 'gufm1': ref = rows[1990]['S-reversed'] early = max(rows[t]['S-reversed'] for t in rows if t <= 1830) readings['R-b max S-reversed 1590-1830 / 1990 value'] = round(early / ref, 3) readings['R-b little reversed flux before 1840 (< 0.25)'] = bool(early / ref < 0.25) res[name]['L%d' % L] = {'epochs': {str(t): v for t, v in rows.items()}, 'change_1840_1990': c, 'change_1590_1840': change(1590, 1840), 'change_1990_2020': change(1990, 2020), 'readings': readings} res['check_sum_vs_g10_worst_relative_L14'] = float(worst) res['check_pass'] = bool(worst < 0.005) with open(os.path.join(OUT, 'reverse_flux.json'), 'w', encoding='utf-8', newline='\n') as fh: json.dump(res, fh, indent=1) print('check worst relative error', worst, 'pass', worst < 0.005) for name in models: for L in ('L14', 'L8'): r = res[name][L] print(name, L, 'readings', r['readings']) print(' 1840-1990', r['change_1840_1990']) if r['change_1590_1840']: print(' 1590-1840', r['change_1590_1840']) for t in ('1590', '1700', '1800', '1840', '1900', '1950', '1990', '2020'): if t in r['epochs']: e = r['epochs'][t] print(' %s g10 %8.1f Nn %8.1f Nr %7.1f Sn %8.1f Sr %7.1f Sr-area %.3f' % ( t, e['g10_model'], e['N-normal'], e['N-reversed'], e['S-normal'], e['S-reversed'], e['S-reversed area share of S hemisphere'])) if __name__ == '__main__': main()