r"""
Yukawa: the range of the nuclear force sets the meson mass
=============================================================

In 1935 Hideki Yukawa asked why the force holding protons and neutrons
together reaches only about a femtometre. If a force is carried by a
quantum of mass :math:`m`, the uncertainty principle lets that quantum
exist for :math:`\Delta t\sim\hbar/mc^2`, so it can travel at most about
:math:`a=\hbar/mc`. The static field of a massive carrier is then not
Coulomb's :math:`1/r` but

.. math::  V(r) = -g^2\,\frac{e^{-r/a}}{r},

which dies off exponentially beyond the range :math:`a`. Running the
argument backwards, a 1.4 fm range predicts a carrier of about
140 MeV, some 270 electron masses. The pion was found in 1947 at
139.6 MeV.

This example uses :func:`~physicskit.nuclear.forces.meson_mass_from_range`,
:func:`~physicskit.nuclear.forces.yukawa_range`, and
:func:`~physicskit.nuclear.forces.yukawa_potential`.
"""

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

from physicskit.nuclear.forces import meson_mass_from_range, yukawa_potential, yukawa_range
from physicskit.nuclear.units import M_ELECTRON, M_PION

# %%
# From range to mass
# ----------------------
# Yukawa's own estimate: a nuclear-force range of order 2 fm, then
# the modern 1.4 fm.
for a in (2.0, 1.4):
    m = meson_mass_from_range(a)
    print(f"range {a:.1f} fm  ->  carrier mass {m:6.1f} MeV  = {m / M_ELECTRON:5.0f} m_e")
a_pi = yukawa_range(M_PION)
print(f"\nmeasured pion mass {M_PION:.2f} MeV  ->  Compton range hbar/(m_pi c) = {a_pi:.3f} fm")

# %%
# Yukawa versus Coulomb
# -------------------------
# Same strength at short distance, but the massive carrier's potential
# falls by a factor :math:`e` for every range :math:`a` beyond its Coulomb
# :math:`1/r` envelope. The plot shows :math:`r\,|V(r)|` so that a Coulomb
# force is a flat line.
r = np.linspace(0.05, 6.0, 400)
V_pi = yukawa_potential(r, mass=M_PION)
V_heavy = yukawa_potential(r, mass=meson_mass_from_range(0.7))
V_coul = -1.0 / r

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4.2))
ax1.plot(r, -r * V_coul, "k--", label="massless carrier (Coulomb)")
ax1.plot(r, -r * V_pi, color="firebrick", label=f"pion, a = {a_pi:.2f} fm")
ax1.plot(r, -r * V_heavy, color="steelblue", label="heavier meson, a = 0.70 fm")
ax1.axvline(a_pi, color="firebrick", ls=":", lw=1)
ax1.set_xlabel("r [fm]")
ax1.set_ylabel(r"$r\,|V(r)|/g^2$")
ax1.set_title(r"Exchange of a massive quantum: $e^{-r/a}$")
ax1.legend(fontsize=8)

ranges = np.linspace(0.3, 3.0, 200)
ax2.plot(ranges, meson_mass_from_range(ranges), color="firebrick")
ax2.axhline(M_PION, color="0.5", ls="--", label=r"$m_\pi$ = 139.6 MeV (found 1947)")
ax2.axvline(1.4, color="0.5", ls=":")
ax2.set_xlabel("range of the force a [fm]")
ax2.set_ylabel(r"carrier mass $\hbar c / a$ [MeV]")
ax2.set_title("Yukawa's prediction: mass from range")
ax2.legend(fontsize=8)
fig.tight_layout()

plt.show()

# %%
# Check
# -----
# hbar c / 1.4 fm is ~141 MeV (~275 m_e); beyond the 1/r factor the
# potential drops by exactly 1/e per range.
assert abs(meson_mass_from_range(1.4) - 140.9) < 0.1
assert 250 < meson_mass_from_range(1.4) / M_ELECTRON < 300
ratio = (yukawa_potential(1.0 + a_pi) * (1.0 + a_pi)) / (yukawa_potential(1.0) * 1.0)
assert abs(ratio - np.exp(-1)) < 1e-12
