r"""
Weisskopf: single-particle estimates of gamma-ray rates
==========================================================

An excited nucleus sheds energy by emitting a photon, and the photon
carries away angular momentum :math:`L` in an electric (:math:`EL`) or
magnetic (:math:`ML`) multipole. In 1951 Weisskopf estimated the rate of
each by letting a single proton jump between orbits in a uniform sphere of
radius :math:`R=1.2A^{1/3}` fm:

.. math::  \lambda(EL)=\frac{2(L+1)}{L[(2L+1)!!]^2}\,\alpha c\,k^{2L+1}
   \left(\frac{3}{L+3}\right)^2R^{2L}.

For :math:`E` in MeV this gives :math:`\lambda(E1)=1.0\times10^{14}A^{2/3}E^3`
per second. The photon wavelength is much longer than the nucleus, so
every extra unit of angular momentum costs a factor of about
:math:`(kR)^2`. That is why high-multipole transitions are slow enough to
make long-lived isomers, and why internal conversion, which does not have
this penalty, competes with them.

This example plots :func:`~physicskit.nuclear.gamma.weisskopf_rate` for
:math:`A=125` and compares Krane's order-of-magnitude
:func:`~physicskit.nuclear.gamma.internal_conversion_coefficient` with
the multipole order.
"""

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

from physicskit.nuclear.gamma import internal_conversion_coefficient, weisskopf_rate
from physicskit.nuclear.units import HBARC

A = 125.0
E = np.geomspace(0.05, 5.0, 200)
LN2 = np.log(2)

# %%
# Half-lives of the eight lowest multipoles
# ---------------------------------------------
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4.5))
for L, color in zip(range(1, 5), ["C0", "C1", "C2", "C3"]):
    ax1.loglog(E, LN2 / weisskopf_rate(f"E{L}", E, A), color=color, label=f"E{L}")
    ax1.loglog(E, LN2 / weisskopf_rate(f"M{L}", E, A), color=color, ls="--", label=f"M{L}")
ax1.axhline(1.0, color="0.6", lw=0.8)
ax1.set_xlabel(r"$E_\gamma$ [MeV]")
ax1.set_ylabel(r"Weisskopf half-life $t_{1/2}$ [s]")
ax1.set_title(f"Weisskopf estimates, A = {A:.0f}")
ax1.legend(ncol=2, fontsize=8)

for mult in ("E1", "E2", "E3", "M1", "M4"):
    t = LN2 / weisskopf_rate(mult, 1.0, A)
    print(f"{mult}: t_1/2 = {t:.1e} s at 1 MeV")

# %%
# The (kR)^2 penalty and internal conversion
# ------------------------------------------------
R = 1.2 * A ** (1 / 3)
kR = 1.0 / HBARC * R
ratios = [weisskopf_rate(f"E{L + 1}", 1.0, A) / weisskopf_rate(f"E{L}", 1.0, A) for L in (1, 2, 3)]
print(f"kR at 1 MeV = {kR:.3f}, (kR)^2 = {kR**2:.1e}")
print("lambda(E(L+1))/lambda(EL):", ", ".join(f"{r:.1e}" for r in ratios))

Eic = np.geomspace(0.05, 1.0, 100)
for L in range(1, 5):
    ax2.loglog(Eic * 1e3, internal_conversion_coefficient(f"E{L}", Eic, 50), color=f"C{L - 1}", label=f"E{L}")
ax2.axhline(1.0, color="0.6", lw=0.8)
ax2.set_xlabel(r"$E$ [keV]")
ax2.set_ylabel(r"$\alpha_K$ (Born estimate)")
ax2.set_title("K-shell conversion, Z = 50")
ax2.legend(fontsize=8)
fig.tight_layout()

plt.show()

# %%
# Check
# -----
# The E1 coefficient is 1.0e14 A^(2/3) E^3, each step in L costs less than
# (kR)^2, and conversion grows with multipole order and falls with energy.
assert abs(weisskopf_rate("E1", 1.0, 1.0) / 1.0e14 - 1) < 0.05
assert abs(weisskopf_rate("M1", 1.0, A) / 5.6e13 - 1) < 0.02
assert all(r < kR**2 for r in ratios)
assert abs(weisskopf_rate("E1", 2.0, A) / weisskopf_rate("E1", 1.0, A) - 8) < 1e-10
assert internal_conversion_coefficient("E4", 0.1, 50) > internal_conversion_coefficient("E1", 0.1, 50)
assert internal_conversion_coefficient("E2", 0.1, 50) > internal_conversion_coefficient("E2", 1.0, 50)
