r"""
Bohr and Wheeler: the liquid-drop fission barrier
====================================================

Months after fission was discovered, Bohr and Wheeler (1939) worked out
when a charged liquid drop splits. Stretch a sphere into a spheroid with
quadrupole deformation :math:`\alpha_2`. The surface energy rises by
:math:`\tfrac25E_S\alpha_2^2` and the Coulomb energy falls by
:math:`\tfrac15E_C\alpha_2^2`, so

.. math::  \Delta E = \tfrac25E_S(1-x)\,\alpha_2^2,\qquad
   x=\frac{E_C}{2E_S}=\frac{Z^2/A}{(Z^2/A)_{\rm crit}} .

Below :math:`x=1` the sphere is stable against small stretching, but
large deformations still reach a saddle point and split. The saddle's
height is the fission barrier. Near :math:`x=1` it vanishes as
:math:`(1-x)^3`. Bohr and Wheeler also explained why slow neutrons
split :sup:`235`\ U but not :sup:`238`\ U: the binding energy of the
captured neutron clears the barrier only in the compound nucleus
:sup:`236`\ U.

This example uses :func:`~physicskit.nuclear.fission.deformation_energy`,
:func:`~physicskit.nuclear.fission.fissility`,
:func:`~physicskit.nuclear.fission.critical_z2_over_a`, and
:func:`~physicskit.nuclear.fission.fission_barrier`.
"""

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

from physicskit.nuclear.fission import critical_z2_over_a, deformation_energy, fissility, fission_barrier
from physicskit.nuclear.structure import semf_binding_energy

# %%
# The critical charge
# -----------------------
z2a_crit = critical_z2_over_a()
print(f"(Z^2/A)_crit = 2 a_S / a_C = {z2a_crit:.1f}")
for name, (Z, A) in {"Pb-208": (82, 208), "U-236": (92, 236), "Pu-240": (94, 240), "Fm-256": (100, 256)}.items():
    print(f"  {name:7s} x = {fissility(Z, A):.3f}   barrier = {fission_barrier(Z, A):5.1f} MeV")

# %%
# Why U-235 fissions with slow neutrons and U-238 does not
# ------------------------------------------------------------
# The energy delivered by a captured zero-energy neutron is its
# separation energy in the compound nucleus. The pairing term gives the
# even-even :sup:`236`\ U an extra MeV or so.
S_n_236 = semf_binding_energy(92, 236) - semf_binding_energy(92, 235)
S_n_239 = semf_binding_energy(92, 239) - semf_binding_energy(92, 238)
print(f"\nneutron separation energy: U-236 {S_n_236:.2f} MeV, U-239 {S_n_239:.2f} MeV")
print("measured barriers are ~6 MeV for both; the liquid drop alone overestimates them (the shell correction, Strutinsky 1967)")

# %%
# Energy of a stretched drop
# ------------------------------
alpha = np.linspace(0, 0.3, 200)
x_grid = np.linspace(0.3, 1.1, 300)
A_ref = 240.0
Z_grid = np.sqrt(x_grid * z2a_crit * A_ref)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4.2))
for name, (Z, A), c in [("Pb-208", (82, 208), "0.5"), ("U-236", (92, 236), "firebrick"), ("Z=130, A=310", (130, 310), "steelblue")]:
    ax1.plot(alpha, deformation_energy(Z, A, alpha), color=c, label=f"{name}, x = {fissility(Z, A):.2f}")
ax1.axhline(0, color="k", lw=0.8)
ax1.set_xlabel(r"quadrupole deformation $\alpha_2$")
ax1.set_ylabel(r"$\Delta E = \frac{2}{5}E_S(1-x)\alpha_2^2$ [MeV]")
ax1.set_title("Small stretching: stable below x = 1")
ax1.legend(fontsize=8)
ax2.plot(x_grid, fission_barrier(Z_grid, A_ref, order=1), color="steelblue", ls="--", label=r"leading $(1-x)^3$")
ax2.plot(x_grid, fission_barrier(Z_grid, A_ref), color="firebrick", label=r"with $(1-x)^4$ term")
ax2.axvline(fissility(92, 236), color="0.5", ls=":", label="U-236")
ax2.set_xlabel("fissility x")
ax2.set_ylabel(r"barrier $E_f$ [MeV]  (A = 240)")
ax2.set_title("Bohr-Wheeler barrier vanishes at x = 1")
ax2.legend(fontsize=8)
fig.tight_layout()

plt.show()

# %%
# Check
# -----
# (Z^2/A)_crit ~ 50; the barrier is zero beyond x = 1, scales as (1-x)^3
# near it, and the even-even compound U-236 gets more energy from neutron
# capture than U-239 does.
assert 49 < z2a_crit < 51
assert fission_barrier(130, 310) == 0.0
A = 250.0
Ef = fission_barrier(np.sqrt(np.array([0.98, 0.99]) * z2a_crit * A), A, order=1)
assert abs(Ef[0] / Ef[1] - 8.0) < 1e-9
assert S_n_236 > S_n_239 + 0.5
