r"""
Breit and Wigner: the shape of a nuclear resonance
=====================================================

Slow neutrons were captured by some nuclei with cross sections thousands
of times their geometric size, but only in narrow bands of energy. In
1936 Breit and Wigner explained this with Bohr's compound nucleus. The
neutron and target fuse into a quasi-bound state of energy :math:`E_R`
and lifetime :math:`\hbar/\Gamma`, which later decays into some channel
:math:`b`. The cross section is

.. math::  \sigma_{ab}(E)=\frac{\pi}{k^2}\,g\,
   \frac{\Gamma_a\Gamma_b}{(E-E_R)^2+\Gamma^2/4},

a Lorentzian whose full width at half maximum is the total width
:math:`\Gamma`. Its peak, :math:`4\pi g\Gamma_a\Gamma_b/(k^2\Gamma^2)`,
is set by the neutron *wavelength*, not the nuclear size.

This example plots :func:`~physicskit.nuclear.reactions.breit_wigner_cross_section`
for the famous 6.67 eV neutron-capture resonance of :sup:`238`\ U, the
one that soaks up neutrons in a reactor as they slow down.
"""

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

from physicskit.nuclear.reactions import breit_wigner_cross_section
from physicskit.nuclear.units import HBARC, M_NEUTRON

BARN = 100.0  # fm^2

# %%
# Resonance parameters
# ------------------------
# s-wave, spin-0 target, so :math:`g=1`. Widths in MeV.
E_R = 6.67e-6
Gamma_n, Gamma_g = 1.5e-9, 23e-9
Gamma = Gamma_n + Gamma_g
mu = M_NEUTRON * 238 / 239
k2 = 2 * mu * E_R / HBARC**2

E = np.linspace(E_R - 8 * Gamma, E_R + 8 * Gamma, 2001)
sigma_capture = breit_wigner_cross_section(E, E_R, Gamma, Gamma_n, Gamma_g, mu) / BARN
sigma_elastic = breit_wigner_cross_section(E, E_R, Gamma, Gamma_n, Gamma_n, mu) / BARN
peak = sigma_capture.max()
R_nuc = 1.2 * 238 ** (1 / 3)
print(f"capture peak: {peak:.2e} b at {E[np.argmax(sigma_capture)] * 1e6:.3f} eV")
print(f"geometric pi R^2 of U-238: {np.pi * R_nuc**2 / BARN:.2f} b  -> peak is {peak / (np.pi * R_nuc**2 / BARN):.0e} times larger")
print(f"neutron reduced wavelength 1/k = {1 / np.sqrt(k2):.0f} fm vs nuclear radius {R_nuc:.1f} fm")
print(f"compound-nucleus lifetime hbar/Gamma = {6.582e-22 / Gamma:.1e} s")

# %%
# Measuring the width
# -----------------------
half = sigma_capture >= peak / 2
fwhm = E[half][-1] - E[half][0]
print(f"FWHM read off the curve: {fwhm * 1e9:.1f} meV  (Gamma = {Gamma * 1e9:.1f} meV)")

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4.2))
x = (E - E_R) * 1e9
ax1.plot(x, sigma_capture, color="firebrick", label=r"capture $(n,\gamma)$")
ax1.plot(x, sigma_elastic, color="steelblue", label="elastic resonance part")
ax1.axvspan(-Gamma / 2 * 1e9, Gamma / 2 * 1e9, color="0.85", label=r"$E_R\pm\Gamma/2$")
ax1.set_xlabel(r"$E - E_R$ [meV]")
ax1.set_ylabel(r"$\sigma$ [b]")
ax1.set_title(r"$^{238}$U + n at $E_R$ = 6.67 eV")
ax1.legend(fontsize=8)

E_wide = np.geomspace(1e-6, 2e-5, 4000)
ax2.loglog(E_wide * 1e6, breit_wigner_cross_section(E_wide, E_R, Gamma, Gamma_n, Gamma_g, mu) / BARN, color="firebrick")
ax2.axhline(np.pi * R_nuc**2 / BARN, color="0.5", ls="--", label=r"geometric $\pi R^2$")
ax2.set_xlabel("neutron energy [eV]")
ax2.set_ylabel(r"$\sigma_{n\gamma}$ [b]")
ax2.set_title("A narrow spike far above the nuclear size")
ax2.legend(fontsize=8)
fig.tight_layout()

plt.show()

# %%
# Check
# -----
# The peak equals 4 pi g Gn Gg / (k^2 G^2), about 2.2e4 b, and the
# FWHM equals the total width Gamma (to grid resolution).
assert abs(breit_wigner_cross_section(E_R, E_R, Gamma, Gamma_n, Gamma_g, mu) - 4 * np.pi * Gamma_n * Gamma_g / (k2 * Gamma**2)) < 1e-6
assert 1.8e4 < peak < 2.6e4
assert abs(fwhm / Gamma - 1) < 0.02
