r"""
Bohm diffusion: plasma leaking across a magnetic field
=========================================================

A magnetic field ties each charged particle to a field line, and in
classical theory it escapes only by collisions, each moving its guiding
center about one Larmor radius. That gives a fully ionized plasma the
cross-field diffusion coefficient (Chen, section 5.8)

.. math::

    D_\perp = \frac{\eta_\perp\, n\, k(T_e + T_i)}{B^2},

which falls as :math:`1/B^2`. Bohm (1949, with Burhop and Massey), measuring
plasma loss from arc discharges for the Manhattan Project's isotope
separators, found a much faster rate that fell only as :math:`1/B`,

.. math::

    D_B = \frac{1}{16}\frac{kT_e}{eB},

independent of density and collisions. Chen attributes it to turbulent
:math:`\mathbf{E}\times\mathbf{B}` convection. For two decades many
stellarators and mirrors leaked at Bohm's rate, until the T-3 tokamak
beat it in 1968. This example compares
:func:`~physicskit.plasma.collisions.classical_cross_field_diffusion` with
:func:`~physicskit.plasma.collisions.bohm_diffusion`.
"""

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

from physicskit.plasma.collisions import bohm_diffusion, classical_cross_field_diffusion

# %%
# Scaling with the field
# -------------------------
# A 100 eV hydrogen plasma at :math:`10^{19}\ \mathrm{m^{-3}}` with
# :math:`\ln\Lambda = 10`. Doubling the field quarters classical diffusion
# but only halves Bohm diffusion, so a stronger magnet helps far less than
# classical theory promised.
n, T_e, T_i, lnL = 1e19, 100.0, 100.0, 10.0
B = np.geomspace(0.05, 10.0, 200)
D_cl = classical_cross_field_diffusion(n, T_e, T_i, B, lnL)
D_B = bohm_diffusion(T_e, B)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4))
ax1.loglog(B, D_cl, color="steelblue", label=r"classical $\propto 1/B^2$")
ax1.loglog(B, D_B, color="firebrick", label=r"Bohm $kT_e/16eB \propto 1/B$")
ax1.set_xlabel("B [T]")
ax1.set_ylabel(r"$D_\perp$ [m$^2$/s]")
ax1.set_title(r"$n = 10^{19}$ m$^{-3}$, $T = 100$ eV")
ax1.legend(fontsize=8)

# %%
# Confinement time
# -------------------
# A column of radius :math:`a` loses its lowest diffusion mode, the Bessel
# profile :math:`J_0(2.405\,r/a)`, in :math:`\tau = (a/2.405)^2/D`.
# At 1 T in a 0.5 m column Bohm gives milliseconds, classical seconds or more.
a = 0.5
tau_cl = (a / 2.405) ** 2 / D_cl
tau_B = (a / 2.405) ** 2 / D_B
ax2.loglog(B, tau_cl, color="steelblue", label="classical")
ax2.loglog(B, tau_B, color="firebrick", label="Bohm")
ax2.set_xlabel("B [T]")
ax2.set_ylabel(r"confinement time $\tau$ [s]")
ax2.set_title(f"Column of radius {a} m")
ax2.legend(fontsize=8)
fig.tight_layout()
i1 = np.argmin(np.abs(B - 1.0))
print(f"At B = 1 T: D_classical = {D_cl[i1]:.2e} m^2/s, D_Bohm = {D_B[i1]:.2f} m^2/s")
print(f"            tau_classical = {tau_cl[i1]:.1f} s, tau_Bohm = {tau_B[i1] * 1e3:.1f} ms")

plt.show()

# %%
# Check
# -----
# D_Bohm = T_e[eV] / (16 B), scaling as 1/B; classical scales as 1/B^2 and
# is thousands of times smaller at 1 T.
assert abs(bohm_diffusion(100.0, 1.0) - 6.25) < 1e-12
assert np.allclose(D_B * B, T_e / 16)
assert np.allclose(D_cl * B**2, D_cl[0] * B[0] ** 2)
assert D_B[i1] / D_cl[i1] > 1e3
