r"""
The Eightfold Way: the baryon octet and the meson nonet
=========================================================

In 1961 Gell-Mann and, independently, Ne'eman sorted the hadrons by two
additive quantum numbers, the isospin projection :math:`I_3` and the
hypercharge :math:`Y = B + S`. Plotted on those axes, the eight spin-1/2
baryons fall on a hexagon with two states at its centre, and the nine
pseudoscalar mesons fill the same hexagon with three. These are weight
diagrams of the group SU(3), the "Eightfold Way". Gell-Mann then showed
that breaking the symmetry to first order gives the octet's
Gell-Mann-Okubo mass relation, :math:`2(m_N + m_\Xi) = 3m_\Lambda + m_\Sigma`.
This example draws both diagrams with
:func:`~physicskit.particle.quark_model.weight_diagram` and tests the
relation with :func:`~physicskit.particle.quark_model.gell_mann_okubo`.
"""

# %%
import matplotlib.pyplot as plt
import numpy as np

from physicskit.particle.quark_model import (
    BARYON_OCTET,
    MESON_NONET,
    gell_mann_okubo,
    quark_quantum_numbers,
    weight_diagram,
)

octet = weight_diagram("baryon_octet")
nonet = weight_diagram("meson_nonet")
for name, (I3, Y) in octet.items():
    print(f"{name:7s} I3 = {I3:+.1f}  Y = {Y:+.0f}  mass = {BARYON_OCTET[name].mass:8.2f} MeV")

# %%
# The two weight diagrams
# ---------------------------
# Each row of constant :math:`Y` is an isospin multiplet, and each row has
# one more unit of strangeness, and more mass, than the one above it.
fig, axes = plt.subplots(1, 2, figsize=(11, 5))
for ax, diagram, title in ((axes[0], octet, "Baryon octet ($J^P = 1/2^+$)"), (axes[1], nonet, "Meson nonet ($J^P = 0^-$)")):
    hexagon = [(-0.5, 1), (0.5, 1), (1, 0), (0.5, -1), (-0.5, -1), (-1, 0), (-0.5, 1)]
    ax.plot(*zip(*hexagon), color="0.7", zorder=0)
    centre = [name for name, p in diagram.items() if p == (0.0, 0.0)]
    for name, (I3, Y) in diagram.items():
        offset = 0.12 * (centre.index(name) - (len(centre) - 1) / 2) if name in centre else 0.0
        ax.scatter(I3 + offset, Y, s=80, color="steelblue")
        ax.annotate(name, (I3 + offset, Y), textcoords="offset points", xytext=(6, 6))
    ax.set_xlabel("$I_3$")
    ax.set_ylabel("hypercharge $Y$")
    ax.set_xlim(-1.5, 1.6)
    ax.set_ylim(-1.5, 1.5)
    ax.set_aspect("equal")
    ax.set_title(title)
fig.tight_layout()

# %%
# The Gell-Mann-Okubo mass relation
# -------------------------------------
# With isospin-averaged masses, the relation predicts the
# :math:`\Lambda` from the nucleon, :math:`\Sigma` and :math:`\Xi`.
m_N = np.mean([BARYON_OCTET[n].mass for n in ("p", "n")])
m_Sigma = np.mean([BARYON_OCTET[n].mass for n in ("Sigma+", "Sigma0", "Sigma-")])
m_Xi = np.mean([BARYON_OCTET[n].mass for n in ("Xi0", "Xi-")])
m_Lambda = gell_mann_okubo(m_N, m_Sigma, m_Xi)
print(f"\nGMO: m_Lambda = {m_Lambda:.1f} MeV, measured {BARYON_OCTET['Lambda'].mass:.1f} MeV")

plt.show()

# %%
# Check
# -----
# The octet is a hexagon with two states at its centre, the nonet has
# three, every member obeys Q = I3 + Y/2, and GMO holds within 1%.
assert list(octet.values()).count((0.0, 0.0)) == 2 and len(octet) == 8
assert list(nonet.values()).count((0.0, 0.0)) == 3 and len(nonet) == 9
for name, (I3, Y) in octet.items():
    assert abs(quark_quantum_numbers(BARYON_OCTET[name].quarks)["charge"] - (I3 + Y / 2)) < 1e-10
for name, (I3, Y) in nonet.items():
    assert abs(quark_quantum_numbers(MESON_NONET[name].quarks)["charge"] - (I3 + Y / 2)) < 1e-10
assert abs(m_Lambda / BARYON_OCTET["Lambda"].mass - 1) < 0.01
