"""Compose the rounds 3/4 field render: Kida-Pelz vorticity at half viscosity
on top, the Yang-Mills plaquette weave before and after APE smearing below.
Same 1080x1350 dark ink layout as compose_fields.py. Numbers are read from
render_meta_r34.json, never typed by hand.
"""
import json

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize

OUT = "/home/hatch/workspace/millennium-lab/poster"
meta = json.load(open("%s/render_meta_r34.json" % OUT))
km = meta["kida_pelz"]
ym = meta["yang_mills"]

vort = np.load("%s/kida_peak_vortmag_r34.npy" % OUT)
u = np.load("%s/kida_peak_u_r34.npy" % OUT)
v = np.load("%s/kida_peak_v_r34.npy" % OUT)
plain = np.load("%s/ym_plaquette_plain_r34.npy" % OUT)
smeared = np.load("%s/ym_plaquette_smeared_r34.npy" % OUT)

INK = "#111418"
PAPER = "#f4efe6"
BRASS = "#a9803e"
BONE = "#e8e2d4"
MUTED = "#8a8a80"
FIELD_CMAP = "magma"
WEAVE_CMAP = "RdBu_r"

fig = plt.figure(figsize=(10.8, 13.5), dpi=100)
fig.patch.set_facecolor(INK)
fig.subplots_adjust(left=0, right=1, top=1, bottom=0)

fig.text(0.06, 0.945, "MILLENNIUM LAB  |  ROUNDS 3 AND 4",
         fontsize=13, color=BRASS, weight="bold",
         family="monospace", ha="left", va="center")
fig.text(0.06, 0.905, "The field at half viscosity",
         fontsize=30, color=PAPER, weight="bold", ha="left", va="center")
fig.text(0.06, 0.868,
         "Kida-Pelz vorticity, 64 cubed, viscosity 0.0005, at peak "
         "enstrophy.",
         fontsize=13.5, color=MUTED, ha="left", va="center")

ax = fig.add_axes([0.06, 0.315, 0.88, 0.53])
ax.set_facecolor(INK)
ax.imshow(np.flipud(vort.T), cmap=FIELD_CMAP, origin="lower",
          extent=[0, 2 * np.pi, 0, 2 * np.pi], aspect="auto")
x = np.linspace(0, 2 * np.pi, 64)
y = np.linspace(0, 2 * np.pi, 64)
ax.streamplot(x, y, u.T, v.T, density=1.4, color="white", linewidth=0.45,
              arrowsize=0.5)
ax.set_xlim(0, 2 * np.pi)
ax.set_ylim(0, 2 * np.pi)
ax.set_xticks([])
ax.set_yticks([])
for s in ax.spines.values():
    s.set_color("#33383f")
ax.text(0.02, 0.97, "VORTICITY MAGNITUDE | z = pi PLANE | t = %.1f"
        % km["peak_time"], transform=ax.transAxes, fontsize=11,
        color="white", family="monospace", ha="left", va="top")
ax.text(0.02, 0.03, "STREAMLINES: IN-PLANE VELOCITY",
        transform=ax.transAxes, fontsize=11, color="white",
        family="monospace", ha="left", va="bottom")

fig.text(0.06, 0.292,
         "ENSTROPHY GROWTH %.2f x  |  PEAK t = %.1f  |  MAX |omega| %.2f"
         % (km["enstrophy_growth"], km["peak_time"],
            km["max_vorticity_peak_slice"]),
         fontsize=15, color=PAPER, weight="bold", family="monospace",
         ha="left", va="center")
fig.text(0.06, 0.270,
         "At half the viscosity the growth more than doubled, then "
         "decayed. Transient, not runaway.",
         fontsize=12.5, color=MUTED, ha="left", va="center")
fig.text(0.06, 0.256,
         "The plaquette weave before and after APE smearing. Smoothing "
         "cleaned up the 0++ gap: 1.653 at beta 2.3.",
         fontsize=11.5, color=MUTED, ha="left", va="center")


def weave(ax, values, vmin, vmax, label):
    n = values.shape[0]
    xs, ys, cs, sizes = [], [], [], []
    for i in range(n):
        for j in range(n):
            xs.append(i)
            ys.append(j)
            cs.append(values[i, j])
            sizes.append(5200)
    sc = ax.scatter(xs, ys, c=cs, s=sizes, cmap=WEAVE_CMAP, marker="D",
                    norm=Normalize(vmin=vmin, vmax=vmax),
                    edgecolors="#111418", linewidths=0.7)
    ax.set_xlim(-0.6, n - 0.4)
    ax.set_ylim(-0.6, n - 0.4)
    ax.set_aspect("equal")
    ax.set_xticks([])
    ax.set_yticks([])
    for s in ax.spines.values():
        s.set_color("#33383f")
    ax.text(0.03, 0.97, label, transform=ax.transAxes, fontsize=11,
            color="white", family="monospace", ha="left", va="top")
    return sc


ax1 = fig.add_axes([0.06, 0.045, 0.40, 0.195])
weave(ax1, plain, -0.5, 1.0, "PLAQUETTE, UNSMEARED")
ax2 = fig.add_axes([0.54, 0.045, 0.40, 0.195])
weave(ax2, smeared, 0.70, 1.0, "PLAQUETTE, APE SMEARED, 4 STEPS")

fig.text(0.06, 0.030,
         "P vs NP: the defeater fragment is polynomial, 1400 of 1400 "
         "trials. Hodge: O_X formalized, 13 theorems, 0 sorries.",
         fontsize=11, color=MUTED, ha="left", va="center")
fig.text(0.06, 0.011,
         "All fields computed from the lab's own solvers. A research "
         "render, not a solution. Others did the work.",
         fontsize=11, color=MUTED, ha="left", va="center")

fig.savefig("%s/millennium-field-render-r34.png" % OUT, dpi=100,
            facecolor=INK)
print("saved millennium-field-render-r34.png")
