#!/usr/bin/env python3
"""Part 3, cards 26-32 (the average, and when the program gives up): the numbers nobody had measured.

    OMP_NUM_THREADS=1 PY find_film_extra_E.py          about 10 minutes on one hot core; writes find-film-extra-E.json
    OMP_NUM_THREADS=1 PY find_film_extra_E.py --derive  only the block `derived`, from that file

slide2      rms(S^k Phi_{2T/3} q* - q*) on the exact frame, k = 0, 1, 2, 3
avg_story   seed 1, sweep by sweep: rms distance to the exact orbit of q, slide(third), slide(slide(two_thirds)) and the average
avg_seeds   the same on seeds 1-6, summed up: how often the average is nearer the film than the nearest of its three frames
no_average  seed 1 with the last line removed and the T step on: the first sweep with T <= 0
waves       a single wave on the rest state, one real free period, one real bare average, one real sweep, against the formula
clocks      why every arg mu sits near 0 or +-120 degrees: at the rest state, the angle omega(k) T/3 by shell, at these settings and section 15's
page        what the demos read: the modes to draw on the mu plane, and the waves' table
"""
import json
import math
import os
import sys

import numpy as np

HERE = os.path.dirname(os.path.abspath(__file__))
F = os.path.dirname(HERE)
sys.path.insert(0, HERE)
import twisted_average as ta  # noqa: E402

N, L, a, b, dt = ta.N, ta.L, ta.a, ta.b, ta.dt
OUT = os.path.join(F, 'results', 'find-film-extra-E.json')
DATA = json.load(open(os.path.join(F, 'src', 'page', 'part3-data.json')))
MODES = json.load(open(os.path.join(F, 'results', 'find-film-modes.json')))
DYN = json.load(open(os.path.join(F, 'results', 'verify-dynamics.json')))


def rms(x):
    return float(np.sqrt(np.mean(x * x)))


def exact():
    import base64
    q = np.frombuffer(base64.b64decode(DATA['frame_f64']), dtype='<f8').reshape(2, N, N).copy()
    return q, float(DATA['T_star'])


QS, TS = exact()


def rk4_steps(q, t, steps):
    h = t / steps
    for _ in range(steps):
        k1 = ta.rate(q)
        k2 = ta.rate(q + h / 2 * k1)
        k3 = ta.rate(q + h / 2 * k2)
        k4 = ta.rate(q + h * k3)
        q = q + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    return q


def film_at(phi):
    """As find_film_modes.py: at most T/3 of integration at the map's step length, then slides."""
    phi = phi % TS
    k = int(phi // (TS / 3))
    rest_ = phi - k * TS / 3
    x = rk4_steps(QS, rest_, max(1, int(round(rest_ / (TS / 321))))) if rest_ > 1e-14 else QS.copy()
    for _ in range((3 - k) % 3):
        x = ta.slide(x)
    return x


GRID_N = 144
GRID = np.array([film_at(j * TS / GRID_N) for j in range(GRID_N)])


def dist_orbit(x):
    """Rms distance from x to the nearest point of the exact orbit: the nearest of 144 frames, then Gauss-Newton on the instant."""
    d = np.sqrt(np.mean((GRID - x) ** 2, axis=(1, 2, 3)))
    j = int(np.argmin(d))
    t = j * TS / GRID_N
    for _ in range(6):
        f = film_at(t)
        fd = ta.rate(f)
        step = float(np.sum((x - f) * fd) / np.sum(fd * fd))
        step = max(-TS / GRID_N, min(TS / GRID_N, step))
        t += step
        if abs(step) < 1e-13:
            break
    return rms(x - film_at(t)), (t % TS) / TS


def seed_noise(seed):
    rest = np.array([a, b / a])[:, None, None]
    return rest + 0.1 * np.random.default_rng(seed).standard_normal((2, N, N))


def story(seed, keep=True, tol=1e-10):
    """The program, byte for byte, with four distances read on the way."""
    q = seed_noise(seed)
    q = (q + ta.turn(q) + ta.turn(ta.turn(q))) / 3
    T = 6.355
    rows = []
    for sweep in range(1, 400):
        q = ta.simulate(q, T)
        third = ta.simulate(q, T / 3)
        two_thirds = ta.simulate(third, T / 3)
        s1 = ta.slide(third)
        s2 = ta.slide(ta.slide(two_thirds))
        lag = s1 - q
        rl = float(np.sqrt(np.mean(lag * lag)))
        avg = (q + s1 + s2) / 3
        dq, phi = dist_orbit(q)
        row = {'sweep': sweep, 'T': float(T), 'rms': rl, 'q': dq, 's1': dist_orbit(s1)[0], 's2': dist_orbit(s2)[0], 'avg': dist_orbit(avg)[0], 'jump': rms(avg - q), 'spread': rms(s2 - q), 'std_u': float(np.std(q[0])), 'phi': phi}
        rows.append(row)
        if rl < tol:
            return rows, q, T
        speed = ta.rate(q)
        T -= np.clip(3 * np.sum(lag * speed) / np.sum(speed * speed), -0.1, 0.1)
        row['Tnext'] = float(T)
        q = avg
    raise RuntimeError('no convergence')


def main():
    out = {'about': 'Numbers for cards 26-32 of Part 3, by find_film_extra_E.py. Distances are rms over all 2 N N numbers.'}

    # ---- slide2
    third = ta.simulate(QS, TS / 3)
    two = ta.simulate(third, TS / 3)
    rows = []
    f = two
    for k in range(4):
        rows.append({'k': k, 'rms': rms(f - QS)})
        f = ta.slide(f)
    out['slide2'] = {'what': 'rms(S^k two_thirds - q*) on the exact frame', 'rows': rows, 'three_slides_exact': float(np.max(np.abs(ta.slide(ta.slide(ta.slide(two))) - two))), 'third_one_slide': rms(ta.slide(third) - QS)}
    write(out)

    # ---- the story, seed 1
    r1, q1, T1 = story(1)
    out['avg_story'] = {'seed': 1, 'sweeps': len(r1), 'rows': r1}
    write(out)

    # ---- seeds 1-6
    summary = []
    nearer = total = 0
    best_gain = []
    for seed in range(1, 7):
        rr = r1 if seed == 1 else story(seed)[0]
        k = 0
        for row in rr[:-1]:
            nearest = min(row['q'], row['s1'], row['s2'])
            total += 1
            if row['avg'] < nearest:
                nearer += 1
                k += 1
            best_gain.append(row['avg'] / nearest)
        summary.append({'seed': seed, 'sweeps': len(rr), 'avg_nearer': k})
    out['avg_seeds'] = {'rows': summary, 'avg_nearer': {'k': nearer, 'n': total}, 'ratio_median': float(np.median(best_gain))}
    write(out)

    # ---- no average, T step on, seed 1
    q = seed_noise(1)
    q = (q + ta.turn(q) + ta.turn(ta.turn(q))) / 3
    T = 6.355
    first = None
    for sweep in range(1, 200):
        if T <= 0:
            first = sweep - 1
            break
        q = ta.simulate(q, T)
        third = ta.simulate(q, T / 3)
        lag = ta.slide(third) - q
        if rms(lag) < 1e-10:
            break
        speed = ta.rate(q)
        T -= np.clip(3 * np.sum(lag * speed) / np.sum(speed * speed), -0.1, 0.1)
    out['no_average'] = {'seed': 1, 'first_sweep_T_le_0': first, 'std_u_there': float(np.std(q[0])), 'lag_there': rms(ta.slide(ta.simulate(q, max(T, 0) / 3) if T > 0 else q) - q)}
    write(out)

    # ---- single waves: free period alone, average alone, both
    rest = MODES['m5_three_waves']['rest']
    ell = np.array([complex(*rest['left'][0]), complex(*rest['left'][1])])
    r = np.array([complex(*rest['right'][0]), complex(*rest['right'][1])])
    sigma0, omega = rest['lambda0']
    T0 = rest['T0']
    q0 = np.array([a, b / a])[:, None, None]
    y, x = np.indices((N, N))
    c = 2 / (3 * (L / N) ** 2)

    def amp(field, m, n):
        A = ell[0] * (field[0] - q0[0]) + ell[1] * (field[1] - q0[1])
        return np.mean(A * np.exp(-2j * np.pi * (m * x + n * y) / N))

    waves = []
    for m, n, name in [(0, 0, 'uniform'), (1, 0, 'star'), (-1, 0, 'cut'), (-2, 0, 'second star'), (1, 1, 'shell 6')]:
        th = 2 * np.pi * m / N
        ph = 2 * np.pi * n / N
        Lam = c * (2 * np.cos(th) + 2 * np.cos(ph) + 2 * np.cos(th + ph) - 6)
        sig = sigma0 + Lam
        lam = np.exp((sig + 1j * omega) * T0 / 3) * np.exp(2j * np.pi * ((2 * m + n) % 3) / 3)
        wave = np.exp(2j * np.pi * (m * x + n * y) / N)
        eps = 1e-6
        qw = q0 + eps * np.array([np.real(r[0] * wave), np.real(r[1] * wave)])
        a0 = amp(qw, m, n)
        free = ta.simulate(qw, T0)
        t1 = ta.simulate(qw, T0 / 3)
        t2 = ta.simulate(t1, T0 / 3)
        bare = (qw + ta.slide(t1) + ta.slide(ta.slide(t2))) / 3
        t1f = ta.simulate(free, T0 / 3)
        t2f = ta.simulate(t1f, T0 / 3)
        both = (free + ta.slide(t1f) + ta.slide(ta.slide(t2f))) / 3
        waves.append({'m': m, 'n': n, 'name': name, 'class': (2 * m + n) % 3, 'sigma': float(sig), 'lambda_abs': float(abs(lam)), 'lambda_arg': float(np.degrees(np.angle(lam))),
                      'free_formula': float(abs(lam) ** 3), 'free_measured': float(abs(amp(free, m, n) / a0)),
                      'bare_formula': float(abs(1 + lam + lam * lam) / 3), 'bare_measured': float(abs(amp(bare, m, n) / a0)),
                      'both_formula': float(abs(lam ** 3 * (1 + lam + lam * lam) / 3)), 'both_measured': float(abs(amp(both, m, n) / a0))})
    out['waves'] = {'T0': T0, 'rows': waves, 'by_name': {w['name']: w for w in waves}, 'worst_relative_error': max(max(abs(w[k + '_measured'] / w[k + '_formula'] - 1) for k in ('free', 'bare', 'both')) for w in waves)}
    write(out)

    # ---- the clocks: omega(k) T / 3 at the rest state, here and at section 15's settings
    def clock(a_, b_, du, dv, L_, T_, shells=(0, 2, 6, 8, 14)):
        rows = []
        for sh in shells:
            k2 = (2 * math.pi / L_) ** 2 * (2 / 3) * sh
            J = np.array([[b_ - 1 - du * k2, a_ * a_], [-b_, -a_ * a_ - dv * k2]])
            ev = np.linalg.eigvals(J)
            om = float(abs(ev[0].imag))
            rows.append({'shell': sh, 'omega': om, 'sigma': float(ev[0].real), 'angle_deg': float(np.degrees(om * T_ / 3))})
        return rows
    fl = DYN['6_floquet']
    out['clocks'] = {'this_film': {'T': TS, 'rows': clock(1.0, 2.3, 1.0, 1.0, 40.0, TS)}, 'true_film': {'T': 9.55705, 'settings': 'a=1, b=3.2, Du=1, Dv=2.5, L=17.4', 'rows': clock(1.0, 3.2, 1.0, 2.5, 17.4, 9.55705)}}
    # the true film's unison, from the check that measured it
    out['true_film_unison'] = {'source': 'verify-dynamics.json 6_floquet', **find_unison(fl)}
    write(out)

    # ---- what the demos read
    seen = []
    def add(tbl):
        for row in tbl:
            mu = row.get('mu')
            if mu is None:
                continue
            key = (round(row['abs3'], 5), round(abs(row['arg']), 2))
            if key in seen:
                continue
            seen.append(key)
            dots.append([round(mu[0], 4), round(mu[1], 4)])
            if abs(mu[1]) > 1e-6:
                dots.append([round(mu[0], 4), round(-mu[1], 4)])
    dots = []
    add(DATA['spectrum_sym'])
    add(DATA['spectrum_chi'])
    add(DATA['spectrum_whole'])
    out['page'] = {'dots': dots, 'waves': waves, 'story': [{k: (float('%.4g' % row[k]) if k in row else None) for k in ('sweep', 'q', 's1', 's2', 'avg', 'jump', 'spread', 'rms')} for row in r1], 'story_T': [[row['T'], row.get('Tnext', row['T'])] for row in r1]}
    out['n_dots'] = len(dots)
    derive(out)
    write(out)
    print(json.dumps({k: out[k] for k in ('slide2', 'avg_seeds', 'no_average', 'clocks', 'true_film_unison', 'n_dots')}, indent=0)[:3000])
    for row in r1:
        print(row['sweep'], '%.4f' % row['T'], *['%.3e' % row[k] for k in ('rms', 'q', 's1', 's2', 'avg', 'jump', 'spread')], '%.3f' % row['std_u'], '%.3f' % row['phi'])
    for w in waves:
        print(w)


def derive(out):
    """Cheap numbers read off the blocks above; `--derive` redoes only these, from the file."""
    third = ta.simulate(QS, TS / 3)
    two = ta.simulate(third, TS / 3)
    avg = (QS + ta.slide(third) + ta.slide(ta.slide(two))) / 3
    rows = out['avg_story']['rows']
    last = rows[-2]  # the last sweep that reaches the average line
    mu = DATA['modes']['slow']['mu'][0]
    out['derived'] = {
        'avg_on_film': rms(avg - QS),
        'late_sweep': last['sweep'],
        'late': {'s1_over_q': last['s1'] / last['q'], 's2_over_q': last['s2'] / last['q'], 'avg_over_q': last['avg'] / last['q'], 'jump_over_q': last['jump'] / last['q'], 'spread_over_lag': last['spread'] / last['rms']},
        'slow': {'mu': mu, 'mu2': mu * mu, 'bare': (1 + mu + mu * mu) / 3, 'one_plus_mu': 1 + mu},
        'sweep4': {k: rows[3][k] for k in ('q', 's1', 's2', 'avg', 'jump')},
    }
    runs = json.load(open(os.path.join(F, 'results', 'find-film-runs.json')))['page']
    vm = json.load(open(os.path.join(F, 'results', 'verify-find-film-modes.json')))
    fm = MODES['m3_factor_is_real']['rows']
    f = fm[2]['file_abs_formula']
    d = out['derived']
    d['per10'] = math.log(10) / math.log(1 / f)
    d['count_C1'] = math.log(1e-10) / math.log(f)
    d['unison_cross_k'] = int(math.ceil(math.log(1 / fm[0]['bare_abs_formula']) / math.log(fm[0]['abs_mu3'])))
    comb = runs['b_comb']['table']
    d['comb_b'] = [comb[0]['b'], comb[-1]['b']]
    d['comb_step'] = round(comb[1]['b'] - comb[0]['b'], 4)
    d['comb_per_b'] = comb[0]['n']
    rest = [r for r in runs['b'] if r['classes'].get('rest') == r['n'] and r['raised'] == 0]
    d['rest_b'] = [rest[0]['b'], rest[-1]['b']]
    d['rest_n'] = rest[0]['n']
    slow = [r for r in runs['b_slow_window']['raised']]
    d['slow_b'] = [slow[0]['b'], slow[-1]['b']]
    d['slow_n'] = slow[0]['n']
    th = [r for r in runs['b_why'] if r['tag'].startswith('thirds-') and 2.35 < r['b'] < 3.25]
    d['thirds_b'] = [th[0]['b'], th[-1]['b']]
    d['thirds'] = {'k': sum(r['returned'] for r in th), 'n': sum(r['n'] for r in th)}
    d['thirds_per_b'] = th[0]['n']
    d['crash_dt'] = min(r['dt'] for r in runs['dt']['fine'] if r.get('crash'))
    d['crash_noise'] = min(r['amp'] for r in runs['noise'] if r.get('crash'))
    d['star_spread_max'] = max(MODES['m5_three_waves']['star_spread_largest_by_sweep_20_seeds'])
    d['cut_slope'] = vm['g_waves']['cut_vs_star_log_slope_sweeps_3_to_6']['median']
    ar = vm['c_arnoldi_real_sweep']
    leading = [fm[4]['file_abs_formula'], f]  # the star imbalance and the slow mode: read against the exact formula
    d['arnoldi_gap_max'] = max(abs(x['arnoldi'][0] - v) for x in ar['sym'] + ar['whole'] for v in leading if abs(x['arnoldi'][0] - v) < 1e-5)


def find_unison(fl):
    """The largest pair in the true film's table: walk the block for rows with abs3 near 5.89."""
    hits = []

    def walk(x):
        if isinstance(x, dict):
            vals = {k: v for k, v in x.items() if isinstance(v, (int, float))}
            for k, v in vals.items():
                if 5.8 < v < 6.0:
                    hits.append(vals)
                    break
            for v in x.values():
                walk(v)
        elif isinstance(x, list):
            for v in x:
                walk(v)
    walk(fl)
    return hits[0] if hits else {}


def write(out):
    def clean(x):
        if isinstance(x, dict):
            return {k: clean(v) for k, v in x.items()}
        if isinstance(x, (list, tuple)):
            return [clean(v) for v in x]
        if isinstance(x, (np.floating,)):
            return float(x)
        if isinstance(x, (np.integer,)):
            return int(x)
        return x
    with open(OUT + '.tmp', 'w') as f:
        json.dump(clean(out), f, indent=0)
    os.replace(OUT + '.tmp', OUT)


if __name__ == '__main__':
    if '--derive' in sys.argv:
        o = json.load(open(OUT))
        derive(o)
        write(o)
        print(json.dumps(o['derived'], indent=0))
    else:
        main()
