#!/usr/bin/env python3
"""The numbers cards 02, 03, 04, 05, 07, 08 of Part 3 need that nobody had measured. Writes find-film-extra-cardsA.json.

  frame      seed 1's returned frame: how it closes after T both ways; how far the film goes from its first frame.
  costs      at one instant of the exact film: the turn against the slide, what "costs no time" means in numbers.
  stays      the field's turn residual against time from noise, three ways: symmetrised start; not symmetrised; symmetrised, with one of
             the stencil's three directions dropped (not the program). And simulate's commutator with the turn on noise, both stencils.
  long_free  the turn residual of a returned frame under a long free run, no average: how far "it stays" goes.
  tiny_noise why small noise takes more sweeps than the growth rate says: amplitude 1e-8 with the turn once and with the turn every sweep.
  two_runs   seed 1 with and without the first line: the landing, its numbers, u as data, and the first line applied to the plane wave.
Run: PY find_film_extra_cardsA.py         (about 3 minutes, one core)"""
import base64
import json
import math
import os
import sys

import numpy as np

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

N, a, b, L, dt = ta.N, ta.a, ta.b, ta.L, ta.dt
ROOT = os.path.join(HERE, '..')
OUT = os.path.join(ROOT, 'results', 'find-film-extra-cardsA.json')
rest = np.array([a, b / a])[:, None, None]
rms = lambda x: float(np.sqrt(np.mean(x * x)))
turn, slide, simulate = ta.turn, ta.slide, ta.simulate
modes = json.load(open(os.path.join(ROOT, 'results', 'find-film-modes.json')))
data = json.load(open(os.path.join(ROOT, 'src', 'page', 'part3-data.json')))
Tstar = data['T_star']
frame = np.frombuffer(base64.b64decode(data['frame_f64']), dtype='<f8').reshape(2, N, N).copy()


def noise(seed, amp=0.1):
    return rest + amp * np.random.default_rng(seed).standard_normal((2, N, N))


def turned3(q):
    return (q + turn(q) + turn(turn(q))) / 3


def save(out):
    with open(OUT + '.tmp', 'w') as f:
        json.dump(out, f, indent=1)
    os.replace(OUT + '.tmp', OUT)


def top_pair_share(q):
    """The largest share of u's variance on one wave and its conjugate. Parseval: variance = sum over k != 0 of |c_k|^2."""
    u = q[0] - q[0].mean()
    c = np.fft.fft2(u) / N ** 2
    p = np.abs(c) ** 2
    p[0, 0] = 0
    total = p.sum()
    if total == 0:
        return 0.0
    # a pair: k and -k carry equal power, so twice the largest single one (a self-conjugate k, at N/2, has no partner: not near the top here)
    return float(2 * p.max() / total)


def rate4(q):
    """rate with the pair (1, 1), (-1, -1) dropped from the stencil: one of the three directions gets no diffusion. Not the program."""
    u, v = q
    nb = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    lap = (sum(np.roll(q, d, (1, 2)) for d in nb) - 4 * q) * 2 / (3 * (L / N) ** 2)
    return lap + np.array([a - (b + 1) * u + u * u * v, b * u - u * u * v])


def simulate4(q, t):
    steps = int(np.ceil(t / dt))
    h = t / steps
    for _ in range(steps):
        k1 = rate4(q)
        k2 = rate4(q + h / 2 * k1)
        k3 = rate4(q + h / 2 * k2)
        k4 = rate4(q + h * k3)
        q = q + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    return q


out = {'about': 'Numbers for cards 02-05, 07, 08 of Part 3. Written by find_film_extra_cardsA.py; seed 1 is numpy\'s draw, noise amplitude 0.1 unless a row says otherwise.', 'page': {}}

# ------------------------------------------------------------ the frame
q1, T1, sw1 = ta.find_film(noise(1))
film48 = [frame]
for _ in range(47):
    film48.append(simulate(film48[-1], Tstar / 48))
dists = [rms(f - frame) for f in film48]
out['frame'] = {
    'numbers_per_frame': 2 * N * N,
    'frames_in_a_period': 48,
    'frames_needed_with_the_slide': 16,
    'seed1_sweeps': sw1,
    'seed1_T': float(T1),
    'seed1_T_error': float(abs(T1 - Tstar)),
    'seed1_closes_free': rms(simulate(q1, T1) - q1),
    'seed1_closes_thirds': rms(simulate(simulate(simulate(q1, T1 / 3), T1 / 3), T1 / 3) - q1),
    'exact_furthest': max(dists),
    'exact_at_third': dists[16],
    'exact_at_half': dists[24],
    'spread_u': float(frame[0].std()),
}
save(out)

# ------------------------------------------------------------ what costs no time
out['costs'] = {
    'turn_at_an_instant': rms(turn(frame) - frame),
    'slide_at_an_instant': rms(slide(frame) - frame),
    'slide_after_T3': rms(slide(simulate(frame, Tstar / 3)) - frame),
    'turn_rate_calls': 0,
    'slide_rate_calls_to_test': int(math.ceil(Tstar / 3 / dt)) * 4,
}
save(out)

# ------------------------------------------------------------ stays
q0 = noise(1)
sym0 = turned3(q0)
DT = Tstar / 12
J = 24
sym, uns, four = sym0, q0, sym0
uns_t, four_nb = turn(q0), q0
four_nb_t = turn(q0)
rows = {'t': [], 'sym': [], 'unsym': [], 'four': [], 'comm_unsym': [], 'comm_four': []}
for j in range(J + 1):
    rows['t'].append(j * DT)
    rows['sym'].append(rms(turn(sym) - sym))
    rows['unsym'].append(rms(turn(uns) - uns))
    rows['four'].append(rms(turn(four) - four))
    rows['comm_unsym'].append(rms(uns_t - turn(uns)))
    rows['comm_four'].append(rms(four_nb_t - turn(four_nb)))
    if j < J:
        sym = simulate(sym, DT)
        uns = simulate(uns, DT)
        uns_t = simulate(uns_t, DT)
        four = simulate4(four, DT)
        four_nb = simulate4(four_nb, DT)
        four_nb_t = simulate4(four_nb_t, DT)
out['page']['stays'] = rows
out['stays'] = {
    'grid': 'T*/12, from 0 to 2 T*',
    'sym_largest': max(rows['sym']),
    'unsym_start': rows['unsym'][0],
    'unsym_at_T': rows['unsym'][12],
    'unsym_at_2T': rows['unsym'][24],
    'four_at_T': rows['four'][12],
    'four_at_2T': rows['four'][24],
    'comm_unsym_largest': max(rows['comm_unsym']),
    'comm_four_at_T': rows['comm_four'][12],
    'comm_four_at_2T': rows['comm_four'][24],
}
save(out)

# ------------------------------------------------------------ how far it stays with no average
rows = []
q = q1
for p in range(0, 251):
    if p % 25 == 0:
        rows.append({'period': p, 'turn_residual': rms(turn(q) - q), 'std_u': float(q[0].std())})
        out['long_free'] = {'rows': rows, 'note': "seed 1's returned frame, simulate(q, T) again and again, nothing else"}
        save(out)
    q = simulate(q, T1)
out['long_free']['growth_per_period_50_to_150'] = (rows[6]['turn_residual'] / rows[2]['turn_residual']) ** (1 / 100)

# ------------------------------------------------------------ tiny noise, once against every sweep
star_factor = None
for row in modes['m5_three_waves']['wave_test']:
    if row['m'] == 1 and row['n'] == 0:
        star_factor = row['factor_abs_measured']
rows = []
for seed in (1, 2, 3):
    q = noise(seed, 1e-8)
    r = {'seed': seed, 'amp': 1e-8}
    for mode in ('once', 'every'):
        o = film_loop(q, turn_mode=mode)
        r[mode] = {'sweeps': int(o['sweep']) if isinstance(o, dict) and 'sweep' in o else None, 'T': float(o['T']) if isinstance(o, dict) and 'T' in o else None}
    rows.append(r)
    out['tiny_noise'] = {'rows': rows}
    save(out)
med = lambda xs: float(np.median(xs))
out['tiny_noise'].update({
    'star_factor_per_sweep': star_factor,
    'sweeps_per_decade': math.log(10) / math.log(star_factor),
    'predicted_from_1e-6': next(r['sweeps'] for r in json.load(open(os.path.join(ROOT, 'results', 'find-film-runs.json')))['page']['noise'] if r['amp'] == 1e-6) + 2 * math.log(10) / math.log(star_factor),
    'turn_residual_at_1e-9_largest': max(x['end_turn_residual'] for x in json.load(open(os.path.join(ROOT, 'results', 'find-film-runs.json')))['R10c']['runs'] if x['tag'] == 'a1e-9'),
    'turn_residual_at_1e-9_least': min(x['end_turn_residual'] for x in json.load(open(os.path.join(ROOT, 'results', 'find-film-runs.json')))['R10c']['runs'] if x['tag'] == 'a1e-9'),
    'once_median': med([r['once']['sweeps'] for r in rows]),
    'every_median': med([r['every']['sweeps'] for r in rows]),
})
save(out)

# ------------------------------------------------------------ with and without the first line, seed 1
def traced(q, T=6.355, tol=1e-10, first=True):
    """find_film's own lines with a record per sweep. With first=True, checked below against ta.find_film."""
    if first:
        q = turned3(q)
    lags = []
    for sweep in range(1, 400):
        q = simulate(q, T)
        third = simulate(q, T / 3)
        two_thirds = simulate(third, T / 3)
        lag = slide(third) - q
        e = np.sqrt(np.mean(lag * lag))
        lags.append(float(e))
        if e < tol:
            return q, T, sweep, lags
        speed = ta.rate(q)
        T -= np.clip(3 * np.sum(lag * speed) / np.sum(speed * speed), -0.1, 0.1)
        q = (q + slide(third) + slide(slide(two_thirds))) / 3
    raise RuntimeError('no convergence')


def q16(u):
    scale = float(np.abs(u).max())
    return {'b64': base64.b64encode(np.round(u.ravel() / scale * 32767).astype('<i2').tobytes()).decode(), 'scale': scale}


with_q, with_T, with_s, with_lags = traced(noise(1))
assert with_T == T1 and with_s == sw1 and np.array_equal(with_q, q1), 'traced is find_film'
wo_q, wo_T, wo_s, wo_lags = traced(noise(1), first=False)
fixed = turned3(wo_q)
fq, fT, fs = ta.find_film(wo_q, T=float(wo_T))
out['two_runs'] = {
    'seed': 1,
    'with': {'sweeps': with_s, 'T': float(with_T), 'share': top_pair_share(with_q), 'turn_residual': rms(turn(with_q) - with_q), 'std_u': float(with_q[0].std()), 'mean_u': float(with_q[0].mean())},
    'without': {'sweeps': wo_s, 'T': float(wo_T), 'share': top_pair_share(wo_q), 'turn_residual': rms(turn(wo_q) - wo_q), 'std_u': float(wo_q[0].std()), 'mean_u': float(wo_q[0].mean())},
    'line_on_the_wave': {'std_u_after_the_line': float(fixed[0].std()), 'share_after_the_line': top_pair_share(fixed), 'sweeps': fs, 'T': float(fT), 'T_error': float(abs(fT - Tstar)), 'share': top_pair_share(fq)},
}
out['page']['two_runs'] = {'with_u': q16(with_q[0]), 'without_u': q16(wo_q[0]), 'with_lags': with_lags, 'without_lags': wo_lags}
save(out)

# ------------------------------------------------------------ small differences the text quotes
clock = modes['m9_rest']
out['clocks'] = {
    'film_minus_rest': Tstar - clock['two_pi_over_omega'],
    'cycle_minus_rest': clock['uniform_cycle']['period_at_program_dt'] - clock['two_pi_over_omega'],
    'omega_T_over_3_deg': math.degrees(clock['omega'] * 6.355 / 3),
    'omega_Tstar_over_3_deg': math.degrees(clock['omega'] * Tstar / 3),
}
save(out)
print(json.dumps({k: v for k, v in out.items() if k != 'page'}, indent=1)[:6000])
