nmbackend 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. nmbackend/__init__.py +22 -0
  2. nmbackend/adapters/__init__.py +4 -0
  3. nmbackend/adapters/rift.py +95 -0
  4. nmbackend/constants.py +55 -0
  5. nmbackend/cooling/README.md +159 -0
  6. nmbackend/cooling/__init__.py +13 -0
  7. nmbackend/cooling/build_nscool.sh +51 -0
  8. nmbackend/cooling/isothermal.py +76 -0
  9. nmbackend/cooling/nscool_nmbackend.patch +310 -0
  10. nmbackend/cooling/physics/__init__.py +18 -0
  11. nmbackend/cooling/physics/_spline.py +71 -0
  12. nmbackend/cooling/physics/blanket.py +66 -0
  13. nmbackend/cooling/physics/conduct.py +196 -0
  14. nmbackend/cooling/physics/core_conduct.py +68 -0
  15. nmbackend/cooling/physics/core_nu.py +268 -0
  16. nmbackend/cooling/physics/crust.py +113 -0
  17. nmbackend/cooling/physics/crust_nu.py +93 -0
  18. nmbackend/cooling/physics/curve.py +42 -0
  19. nmbackend/cooling/physics/envelope.py +37 -0
  20. nmbackend/cooling/physics/gaps.py +164 -0
  21. nmbackend/cooling/physics/geometry.py +131 -0
  22. nmbackend/cooling/physics/heat.py +125 -0
  23. nmbackend/cooling/physics/native.py +138 -0
  24. nmbackend/cooling/physics/reference.py +53 -0
  25. nmbackend/cooling/physics/star.py +225 -0
  26. nmbackend/cooling/physics/superfluid.py +41 -0
  27. nmbackend/cooling/physics/whole.py +136 -0
  28. nmbackend/cooling/runner.py +583 -0
  29. nmbackend/cooling/validate.py +120 -0
  30. nmbackend/data/crust_sly_dh2001.dat +81 -0
  31. nmbackend/data/frib_scenario_ladder.yaml +100 -0
  32. nmbackend/durca.py +111 -0
  33. nmbackend/ejecta.py +614 -0
  34. nmbackend/eos/__init__.py +19 -0
  35. nmbackend/eos/base.py +523 -0
  36. nmbackend/eos/compose.py +251 -0
  37. nmbackend/eos/coolcomp.py +67 -0
  38. nmbackend/eos/crust.py +318 -0
  39. nmbackend/eos/ddrmf.py +388 -0
  40. nmbackend/eos/hybrid.py +173 -0
  41. nmbackend/eos/leptons.py +74 -0
  42. nmbackend/eos/metamodel.py +303 -0
  43. nmbackend/eos/polytrope.py +33 -0
  44. nmbackend/eos/rmft.py +450 -0
  45. nmbackend/eos/skyrme.py +352 -0
  46. nmbackend/inference/__init__.py +13 -0
  47. nmbackend/inference/fisher.py +174 -0
  48. nmbackend/inference/synthetic.py +83 -0
  49. nmbackend/ipad.py +102 -0
  50. nmbackend/lab_likelihood.py +587 -0
  51. nmbackend/nscool_io.py +112 -0
  52. nmbackend/postmerger.py +264 -0
  53. nmbackend/priors/__init__.py +16 -0
  54. nmbackend/priors/bridge.py +437 -0
  55. nmbackend/priors/bridge_jax.py +435 -0
  56. nmbackend/priors/gp_pca.py +325 -0
  57. nmbackend/reference/__init__.py +11 -0
  58. nmbackend/reference/reprimand.py +108 -0
  59. nmbackend/reference/scipy_tov.py +289 -0
  60. nmbackend/rift/__init__.py +26 -0
  61. nmbackend/rift/compressed.py +194 -0
  62. nmbackend/rift/evidence.py +276 -0
  63. nmbackend/rift/sequence.py +428 -0
  64. nmbackend/rotating/__init__.py +7 -0
  65. nmbackend/rotating/rns.py +197 -0
  66. nmbackend/rotating/tabulated.py +249 -0
  67. nmbackend/thermal/__init__.py +1 -0
  68. nmbackend/thermal/diffusion.py +86 -0
  69. nmbackend/tov/__init__.py +15 -0
  70. nmbackend/tov/central.py +72 -0
  71. nmbackend/tov/family.py +155 -0
  72. nmbackend/tov/phase_transition.py +93 -0
  73. nmbackend/tov/solver.py +475 -0
  74. nmbackend/validation/__init__.py +5 -0
  75. nmbackend/validation/model_diagnostic.py +67 -0
  76. nmbackend/validation/reference_matrix.py +216 -0
  77. nmbackend-0.4.0.dist-info/METADATA +168 -0
  78. nmbackend-0.4.0.dist-info/RECORD +81 -0
  79. nmbackend-0.4.0.dist-info/WHEEL +5 -0
  80. nmbackend-0.4.0.dist-info/licenses/LICENSE +21 -0
  81. nmbackend-0.4.0.dist-info/top_level.txt +1 -0
nmbackend/eos/rmft.py ADDED
@@ -0,0 +1,450 @@
1
+ """Relativistic mean-field theory (RMFT) nuclear EOS: sigma-omega-rho with nonlinear
2
+ scalar self-couplings (Boguta-Bodmer form).
3
+
4
+ This is the "critically physical" model from the project notes -- a self-consistent
5
+ field-theoretic EOS, in contrast to the empirical-parameter metamodel. Nucleons
6
+ (n, p) interact via exchange of a scalar sigma (attraction, generates an effective
7
+ mass m* = M - g_sigma sigma), a vector omega (repulsion), and an isovector rho
8
+ (symmetry). In the mean-field approximation the meson fields are classical and
9
+ uniform; sigma is fixed self-consistently by its gap equation while omega and rho
10
+ are algebraic in the densities.
11
+
12
+ Conventions (Glendenning; ``Phi = g_sigma * sigma``, working in hbar*c = 1, momenta
13
+ and masses in fm^-1, densities in fm^-3, energy density in fm^-4 -> MeV/fm^3)::
14
+
15
+ m* = M - Phi
16
+ Phi/C_sigma + b M Phi^2 + c Phi^3 = rho_s(m*) (self-consistent)
17
+ W = C_omega n_B (= g_omega omega)
18
+ R = (1/2) C_rho n_3 (= g_rho rho ; n_3 = n_p - n_n)
19
+ (with an omega-rho coupling Lambda_v, W and R solve the coupled pair
20
+ n_B = W/C_omega + 2 Lambda_v W R^2, n_3/2 = R/C_rho + 2 Lambda_v W^2 R)
21
+ mu_i = sqrt(kF_i^2 + m*^2) + W + t3_i R (t3 = +1/2 p, -1/2 n)
22
+
23
+ with C_x = (g_x/m_x)^2. Energy density and pressure::
24
+
25
+ eps = sum_i eps_kin(kF_i, m*) + (1/2)Phi^2/C_sigma + (1/3)b M Phi^3
26
+ + (1/4)c Phi^4 + (1/2)W^2/C_omega + (1/2)R^2/C_rho
27
+ + 3 Lambda_v W^2 R^2 + eps_leptons
28
+ p = mu_n n_B - eps (Gibbs at T=0; thermodynamically consistent)
29
+
30
+ beta-equilibrium (mu_n - mu_p = mu_e = mu_mu) + charge neutrality
31
+ (n_p = n_e + n_mu) fix the proton fraction; see :meth:`RMFTEOS.composition`.
32
+
33
+ The optional isoscalar-isovector coupling ``Lambda_v`` (Horowitz & Piekarewicz,
34
+ PRL 86, 5647 (2001); the FSU family) softens the symmetry energy at fixed
35
+ saturation point and E_sym. ``couplings_from_saturation`` maps (n0, B/A, K,
36
+ m*/M, E_sym, L) to couplings, so an RMF family can be sampled in saturation
37
+ properties.
38
+
39
+ Default couplings: GM1 (Glendenning & Moszkowski 1991), whose saturation point is
40
+ n0~0.153 fm^-3, B/A~16.3 MeV, K~300 MeV, m*/M~0.70, a_sym~32.5 MeV -- used to
41
+ validate this implementation (see tests/ and validate script).
42
+
43
+ Motivated by standard RMF theory and MUSES ``cmf_solver`` (Chiral Mean Field) as a
44
+ reference generator; see ATTRIBUTION.md / docs/roadmap.md.
45
+ """
46
+ from __future__ import annotations
47
+
48
+ import math
49
+ import jax
50
+ import jax.numpy as jnp
51
+
52
+ from ..constants import HBARC
53
+ from .base import EOSModel, Composition, compiled_method, compiled_table
54
+ from .leptons import lepton_sector
55
+ from .crust import (N_T_DEFAULT, check_crust, core_grid, finish_table_flags,
56
+ monotone_core)
57
+
58
+ jax.config.update("jax_enable_x64", True)
59
+
60
+ _PI2 = math.pi ** 2
61
+ _M_N_FM = 939.0 / HBARC # nucleon mass in fm^-1 (use a single M for n,p)
62
+
63
+ #: GM1 parameter set (Glendenning & Moszkowski 1991). C_x = (g_x/m_x)^2 in fm^2.
64
+ GM1 = dict(
65
+ M_mev=939.0,
66
+ C_sigma=11.785, C_omega=7.148, C_rho=4.410, # fm^2
67
+ b=0.002947, c=-0.001070, # dimensionless
68
+ )
69
+ # NOTE on other parameter sets (e.g. NL3): a naive conversion of the published
70
+ # (g_sigma, g2, g3) to this (g/m)^2 + b,c convention did NOT reproduce NL3's
71
+ # saturation -- the scalar gap equation had NO root at NL3's known physical point
72
+ # (the meson terms came out ~2x too small vs rho_s), i.e. a coupling-normalization
73
+ # mismatch, not a solver problem (GM1 is exact through the same code). Sets with
74
+ # c < 0 also have a nonlinear potential unbounded below (metastable physical branch).
75
+ # Adding a new set therefore requires verifying its couplings against its own
76
+ # saturation point first. See docs/backlog.md.
77
+ DEFAULT_PARAMS = dict(GM1)
78
+
79
+
80
+ # --- relativistic Fermi-gas integrals (degeneracy 2), all in fm units ---
81
+ def _kf(n_i): # Fermi momentum (fm^-1) from density (fm^-3)
82
+ return (3.0 * _PI2 * jnp.maximum(n_i, 1e-30)) ** (1.0 / 3.0)
83
+
84
+
85
+ def _rho_s_one(kf, mstar): # scalar density of one species (fm^-3)
86
+ ef = jnp.sqrt(kf**2 + mstar**2)
87
+ return (mstar / (2.0 * _PI2)) * (
88
+ kf * ef - mstar**2 * jnp.log((kf + ef) / mstar))
89
+
90
+
91
+ def _eps_kin_one(kf, mstar): # kinetic energy density of one species (fm^-4)
92
+ ef = jnp.sqrt(kf**2 + mstar**2)
93
+ return (1.0 / (8.0 * _PI2)) * (
94
+ kf * (2.0 * kf**2 + mstar**2) * ef
95
+ - mstar**4 * jnp.log((kf + ef) / mstar))
96
+
97
+
98
+ def _solve_phi(n_n, n_p, p, ngrid=320, nbis=48):
99
+ """Self-consistent scalar field Phi (fm^-1) by FIRST-ROOT bracketed bisection.
100
+
101
+ The scalar gap equation can be non-convex (sets with b, c < 0, e.g. NL3) with
102
+ several roots. The *physical* branch is the one continuously connected from
103
+ m* = M (Phi = 0) at zero density, which is always the **lowest-Phi root** (resid
104
+ starts negative at Phi=0 and the first up-crossing is physical; higher crossings
105
+ are spurious). We locate the first '-' -> '+' sign change of resid on a Phi grid
106
+ and bisect there -- robust for ALL parameter sets and fully vectorized over the
107
+ density array. Differentiable (the bracket index is piecewise-constant, like a
108
+ searchsorted; gradients flow through the bisected endpoints).
109
+ """
110
+ M = p["M_mev"] / HBARC
111
+ Cs, b, c = p["C_sigma"], p["b"], p["c"]
112
+ kfn, kfp = _kf(n_n), _kf(n_p)
113
+ shape = jnp.shape(kfn)
114
+ kfn = jnp.reshape(kfn, (-1,))
115
+ kfp = jnp.reshape(kfp, (-1,)) # (D,)
116
+
117
+ def resid(Phi): # broadcasts Phi with (D,) kf
118
+ mstar = M - Phi
119
+ rho_s = _rho_s_one(kfn, mstar) + _rho_s_one(kfp, mstar)
120
+ return Phi / Cs + b * M * Phi**2 + c * Phi**3 - rho_s
121
+
122
+ Phi_grid = jnp.linspace(1e-6, 0.985 * M, ngrid) # (G,)
123
+ rg = resid(Phi_grid[:, None]) # (G, D)
124
+ cross = (rg[:-1] < 0.0) & (rg[1:] >= 0.0) # first up-crossing per column
125
+ i = jnp.argmax(cross, axis=0) # (D,)
126
+ lo, hi = Phi_grid[i], Phi_grid[i + 1] # (D,)
127
+
128
+ def body(_k, c2):
129
+ lo, hi = c2
130
+ mid = 0.5 * (lo + hi)
131
+ r = resid(mid)
132
+ return jnp.where(r < 0.0, mid, lo), jnp.where(r < 0.0, hi, mid)
133
+
134
+ lo, hi = jax.lax.fori_loop(0, nbis, body, (lo, hi))
135
+ return jnp.reshape(0.5 * (lo + hi), shape)
136
+
137
+
138
+ def _leptons(n_charge):
139
+ """Electrons + muons at charge density n_charge = n_p (fm^-3); see
140
+ :func:`nmbackend.eos.leptons.lepton_sector`.
141
+ Returns (mu_e [fm^-1], n_e, n_mu [fm^-3], eps_lep [fm^-4])."""
142
+ return lepton_sector(n_charge, muons=True)
143
+
144
+
145
+ def _vector_fields(n_B, n3, p, n_iter=40):
146
+ """Mean vector fields W = g_omega omega, R = g_rho rho (fm^-1).
147
+
148
+ Without the omega-rho coupling they are algebraic. With ``Lambda_v`` they
149
+ solve n_B = W/C_omega + 2 Lambda_v W R^2 and n3/2 = R/C_rho + 2 Lambda_v W^2 R,
150
+ by fixed-point iteration (a contraction: 2 Lambda_v R^2 << 1/C_omega).
151
+ """
152
+ Co, Cr = p["C_omega"], p["C_rho"]
153
+ lam = p.get("Lambda_v", 0.0)
154
+ W, R = Co * n_B, 0.5 * Cr * n3
155
+ if isinstance(lam, (int, float)) and lam == 0.0:
156
+ return W, R
157
+
158
+ def body(_i, c):
159
+ W, R = c
160
+ W = n_B / (1.0 / Co + 2.0 * lam * R**2)
161
+ R = 0.5 * n3 / (1.0 / Cr + 2.0 * lam * W**2)
162
+ return W, R
163
+
164
+ return jax.lax.fori_loop(0, n_iter, body, (W, R))
165
+
166
+
167
+ def landau_masses(params, n, x_p):
168
+ """(m*_n/M, m*_p/M): Landau masses at the Fermi surface.
169
+
170
+ The single-particle energy is sqrt(k^2 + m_D^2) + W + t3 R, so the Landau
171
+ mass k_F / (d e/dk) is E*_F = sqrt(k_F^2 + m_D^2), with Dirac mass
172
+ m_D = M - Phi (the ``mstar_frac`` of :func:`couplings_from_saturation`)."""
173
+ p = dict(DEFAULT_PARAMS)
174
+ p.update(params or {})
175
+ M = p["M_mev"] / HBARC
176
+ n = jnp.asarray(n, float)
177
+ x_p = jnp.asarray(x_p, float)
178
+ n_n, n_p = (1.0 - x_p) * n, x_p * n
179
+ kfn, kfp = _kf(n_n), _kf(n_p)
180
+
181
+ def resid(Phi): # scalar gap equation (see _solve_phi)
182
+ ms = M - Phi
183
+ return (Phi / p["C_sigma"] + p["b"] * M * Phi**2 + p["c"] * Phi**3
184
+ - _rho_s_one(kfn, ms) - _rho_s_one(kfp, ms))
185
+
186
+ # _solve_phi bisects, which carries no gradient; one Newton step from a
187
+ # stop-gradient root gives the implicit derivative of m_D
188
+ Phi0 = jax.lax.stop_gradient(_solve_phi(n_n, n_p, p))
189
+ Phi = Phi0 - resid(Phi0) / jax.grad(lambda x: jnp.sum(resid(x)))(Phi0)
190
+ m_D = M - Phi
191
+ ef = lambda kf: jnp.sqrt(kf**2 + m_D**2) / M
192
+ return ef(kfn), ef(kfp)
193
+
194
+
195
+ def _solve_xp(n_B, p, n_iter=70):
196
+ """beta-equilibrium proton fraction with electrons AND muons.
197
+
198
+ Condition mu_n - mu_p = mu_e = mu_mu, with charge neutrality
199
+ n_p = n_e + n_mu (``_leptons``).
200
+ mu_n - mu_p = (E*_Fn - 1/2 R) - (E*_Fp + 1/2 R) = E*_Fn - E*_Fp - R.
201
+ """
202
+ M = p["M_mev"] / HBARC
203
+
204
+ def g(x_p):
205
+ n_p, n_n = x_p * n_B, (1.0 - x_p) * n_B
206
+ Phi = _solve_phi(n_n, n_p, p)
207
+ mstar = M - Phi
208
+ kfn, kfp = _kf(n_n), _kf(n_p)
209
+ efn = jnp.sqrt(kfn**2 + mstar**2)
210
+ efp = jnp.sqrt(kfp**2 + mstar**2)
211
+ _, R = _vector_fields(n_B, n_p - n_n, p)
212
+ mu_e = efn - efp - R # required by beta-eq (fm^-1)
213
+ mu_e_actual = _leptons(x_p * n_B)[0] # e + mu, charge neutrality
214
+ return mu_e - mu_e_actual # >0 -> need more protons
215
+
216
+ lo = jnp.full_like(n_B, 1e-4)
217
+ hi = jnp.full_like(n_B, 0.5 - 1e-6)
218
+
219
+ def body(_, c):
220
+ lo, hi = c
221
+ mid = 0.5 * (lo + hi)
222
+ gm = g(mid) # g DECREASES with x_p (more p -> less mu_e needed,
223
+ # more electrons) -> root where gm crosses 0 downward
224
+ lo = jnp.where(gm > 0.0, mid, lo)
225
+ hi = jnp.where(gm > 0.0, hi, mid)
226
+ return lo, hi
227
+
228
+ lo, hi = jax.lax.fori_loop(0, n_iter, body, (lo, hi))
229
+ x0 = jax.lax.stop_gradient(0.5 * (lo + hi))
230
+ gx = g(x0)
231
+ dg = jax.grad(lambda x: jnp.sum(g(x)))(x0)
232
+ return x0 - gx / dg
233
+
234
+
235
+ def _eps_p_of_n(n_B, x_p, p, leptons=True):
236
+ """eps, p, mu_n at (n_B, x_p) with Phi solved per-density (independent).
237
+
238
+ Used for the per-density paths and as a back-compatible wrapper. The EOS table
239
+ instead uses the continuation solver (``_solve_grid``) for robustness with
240
+ strongly nonlinear sets. ``leptons=False`` -> pure nuclear matter (no leptons).
241
+ """
242
+ n_p, n_n = x_p * n_B, (1.0 - x_p) * n_B
243
+ Phi = _solve_phi(n_n, n_p, p)
244
+ return _eps_p_from_state(n_B, x_p, Phi, p, leptons)
245
+
246
+
247
+ def _eps_p_from_state(n_B, x_p, Phi, p, leptons=True):
248
+ """eps (MeV/fm^3), p (MeV/fm^3), mu_n (MeV) given a *solved* scalar field Phi."""
249
+ M = p["M_mev"] / HBARC
250
+ Cs, Co, Cr, b, c = p["C_sigma"], p["C_omega"], p["C_rho"], p["b"], p["c"]
251
+ lam = p.get("Lambda_v", 0.0)
252
+ n_p, n_n = x_p * n_B, (1.0 - x_p) * n_B
253
+ mstar = M - Phi
254
+ kfn, kfp = _kf(n_n), _kf(n_p)
255
+ W, R = _vector_fields(n_B, n_p - n_n, p)
256
+
257
+ eps_kin = _eps_kin_one(kfn, mstar) + _eps_kin_one(kfp, mstar)
258
+ eps_field = (0.5 * Phi**2 / Cs + (1.0 / 3.0) * b * M * Phi**3
259
+ + 0.25 * c * Phi**4 + 0.5 * W**2 / Co + 0.5 * R**2 / Cr
260
+ + 3.0 * lam * W**2 * R**2)
261
+ eps_lep = _leptons(x_p * n_B)[3] if leptons else 0.0 * n_B # e + mu (n_p charge)
262
+ eps = eps_kin + eps_field + eps_lep
263
+
264
+ mu_n = jnp.sqrt(kfn**2 + mstar**2) + W - 0.5 * R # baryon chem. pot.
265
+ if leptons:
266
+ # eps + p = sum_i mu_i n_i = mu_n n_B (after beta-eq + charge neutrality)
267
+ pres = mu_n * n_B - eps
268
+ else:
269
+ # pure nuclear matter: p = sum_i mu_i n_i - eps, mu_i = E*_Fi + W + t3 R
270
+ mu_p = jnp.sqrt(kfp**2 + mstar**2) + W + 0.5 * R
271
+ pres = mu_n * n_n + mu_p * n_p - eps
272
+ return eps * HBARC, pres * HBARC, mu_n * HBARC # -> MeV/fm^3, MeV
273
+
274
+
275
+ def symmetry_energy(n_B, Phi, p):
276
+ """Symmetry energy S(n) [MeV] (analytic RMF form) given the symmetric-matter
277
+ scalar field Phi: S = (1/6) k_F^2/E*_F + (1/8) C_rho_eff n, with
278
+ C_rho_eff = C_rho / (1 + 2 Lambda_v C_rho W^2) and W = C_omega n."""
279
+ mstar = p["M_mev"] / HBARC - Phi
280
+ kf = _kf(n_B / 2.0)
281
+ ef = jnp.sqrt(kf**2 + mstar**2)
282
+ W = p["C_omega"] * n_B
283
+ cr_eff = p["C_rho"] / (1.0 + 2.0 * p.get("Lambda_v", 0.0) * p["C_rho"] * W**2)
284
+ return ((1.0 / 6.0) * kf**2 / ef + (1.0 / 8.0) * cr_eff * n_B) * HBARC
285
+
286
+
287
+ def couplings_from_saturation(n0=0.153, BA=-16.3, K=300.0, mstar_frac=0.70,
288
+ J=32.5, L=None, M_mev=939.0):
289
+ """RMF couplings reproducing a saturation point (Glendenning's inversion).
290
+
291
+ Inputs in fm^-3 (n0) and MeV (BA, K, J, L). C_omega follows from p = 0,
292
+ (C_sigma, b, c) from the linear system {gap equation, E/A, K} at n0, and
293
+ C_rho from J. If ``L`` is given, an omega-rho coupling Lambda_v lowers the
294
+ slope from its Lambda_v = 0 value L0 at fixed J; L must lie in
295
+ (L0 - 6 J_rho, L0], where J_rho = C_rho n0/8 is the rho part of J.
296
+
297
+ Returns a params dict (C_sigma, C_omega, C_rho, b, c, Lambda_v, M_mev) plus
298
+ ``L0``. Raises ValueError if the saturation point is not reachable
299
+ (C_sigma <= 0, J below its kinetic part, or L outside the range above).
300
+ """
301
+ import numpy as np
302
+ M = M_mev / HBARC
303
+ mstar = mstar_frac * M
304
+ Phi = M - mstar
305
+ kf = float(_kf(n0 / 2.0))
306
+ ef = math.sqrt(kf**2 + mstar**2)
307
+ W0 = (M_mev + BA) / HBARC - ef # p = 0: E/A = E*_F + W
308
+ Co = W0 / n0
309
+
310
+ def rho_s(n, ms):
311
+ return 2.0 * _rho_s_one(_kf(n / 2.0), ms)
312
+
313
+ rs0 = float(rho_s(n0, mstar))
314
+ drs_dn = float(jax.grad(rho_s, 0)(n0, mstar))
315
+ drs_dm = float(jax.grad(rho_s, 1)(n0, mstar))
316
+ eps_kin = 2.0 * float(_eps_kin_one(kf, mstar))
317
+ U0 = n0 * (M_mev + BA) / HBARC - eps_kin - 0.5 * Co * n0**2
318
+ dkf = kf / (3.0 * n0)
319
+ # K = 9 n0 dmu/dn, mu = E*_F(n) + C_omega n -> dPhi/dn
320
+ dPhi = (kf * dkf - ef * (K / HBARC / (9.0 * n0) - Co)) / mstar
321
+ Upp = drs_dn / dPhi - drs_dm # U''(Phi) from d(gap)/dn
322
+ A = np.array([[Phi, M * Phi**2, Phi**3],
323
+ [Phi**2 / 2.0, M * Phi**3 / 3.0, Phi**4 / 4.0],
324
+ [1.0, 2.0 * M * Phi, 3.0 * Phi**2]])
325
+ inv_cs, b, c = np.linalg.solve(A, np.array([rs0, U0, Upp]))
326
+ if inv_cs <= 0.0:
327
+ raise ValueError("saturation point needs C_sigma <= 0")
328
+ T = kf**2 / (6.0 * ef) # kinetic part of E_sym
329
+ cr_eff = 8.0 * (J / HBARC - T) / n0
330
+ if cr_eff <= 0.0:
331
+ raise ValueError("J below its kinetic part; C_rho <= 0")
332
+ def_dn = (kf * dkf - mstar * dPhi) / ef # dE*_F/dn (m* = M - Phi)
333
+ dT = kf * dkf / (3.0 * ef) - T * def_dn / ef
334
+ J_rho = cr_eff * n0 / 8.0
335
+ L0 = 3.0 * n0 * dT * HBARC + 3.0 * J_rho * HBARC
336
+ lam, Cr = 0.0, cr_eff
337
+ if L is not None:
338
+ q = (L0 - L) / (3.0 * J_rho * HBARC)
339
+ if not (0.0 <= q < 2.0):
340
+ raise ValueError(f"L={L} outside ({L0 - 6 * J_rho * HBARC:.1f}, "
341
+ f"{L0:.1f}] for this saturation point")
342
+ x = q / (2.0 - q) # x = 2 Lambda_v C_rho W0^2
343
+ Cr = cr_eff * (1.0 + x)
344
+ lam = x / (2.0 * Cr * W0**2)
345
+ return dict(M_mev=M_mev, C_sigma=1.0 / inv_cs, C_omega=Co, C_rho=Cr,
346
+ b=b, c=c, Lambda_v=lam, L0=L0)
347
+
348
+
349
+ def saturation_properties(params=None, n_start=0.005, n_hi=0.22, npts=440):
350
+ """Return GM1/NL3-style saturation diagnostics for a parameter set:
351
+ n0, B/A (MeV), K (MeV), m*/M, E_sym (MeV), L (MeV).
352
+
353
+ The scan starts at LOW density (m* ~ M, where the scalar root is unambiguous)
354
+ so the continuation solver tracks the physical branch up to saturation -- this
355
+ is what makes it work for strongly nonlinear sets (NL3) and not just GM1."""
356
+ p = dict(DEFAULT_PARAMS)
357
+ if params:
358
+ p.update(params)
359
+ import numpy as np
360
+ M = p["M_mev"]
361
+ ns = jnp.linspace(n_start, n_hi, npts)
362
+ Phi_arr = _solve_phi(ns / 2.0, ns / 2.0, p) # symmetric, robust first-root
363
+ eps, pres, _ = jax.vmap(lambda n, Ph: _eps_p_from_state(n, 0.5, Ph, p,
364
+ leptons=False))(ns, Phi_arr)
365
+ Esym_arr = jax.vmap(lambda n, Ph: symmetry_energy(n, Ph, p))(ns, Phi_arr)
366
+ eps, pres, ns_np = np.asarray(eps), np.asarray(pres), np.asarray(ns)
367
+ Phi_np, Esym_np = np.asarray(Phi_arr), np.asarray(Esym_arr)
368
+ # saturation = binding-energy minimum (where p = 0 with dp/dn > 0), NOT the
369
+ # trivial p ~ 0 at the low-density boundary.
370
+ i0 = int(np.argmin(eps / ns_np - M))
371
+ n0 = float(ns_np[i0])
372
+ BA = float(eps[i0] / n0 - M)
373
+ K = float(9.0 * (pres[i0 + 1] - pres[i0 - 1]) / (ns_np[i0 + 1] - ns_np[i0 - 1]))
374
+ mstar_frac = (M / HBARC - float(Phi_np[i0])) * HBARC / M
375
+ Esym = float(Esym_np[i0])
376
+ L = float(3.0 * n0 * (Esym_np[i0 + 1] - Esym_np[i0 - 1])
377
+ / (ns_np[i0 + 1] - ns_np[i0 - 1]))
378
+ return dict(n0=n0, BA=BA, K=K, mstar_frac=mstar_frac, E_sym=Esym, L=L)
379
+
380
+
381
+ class RMFTEOS(EOSModel):
382
+ """Self-consistent sigma-omega-rho relativistic mean-field EOS."""
383
+
384
+ name = "RMFTEOS"
385
+
386
+ def __init__(self, n_min=1e-3, n_max=1.2, n_points=200,
387
+ n_surface=1e-7, gamma_crust=1.3,
388
+ crust="sly", n_t=N_T_DEFAULT,
389
+ allow_unstable_core=False):
390
+ self.n_grid = jnp.exp(jnp.linspace(math.log(n_min), math.log(n_max),
391
+ n_points))
392
+ self.n_surface = n_surface
393
+ self.gamma_crust = gamma_crust
394
+ self.crust = check_crust(crust, n_surface)
395
+ self.n_t = n_t
396
+ self.allow_unstable_core = allow_unstable_core
397
+
398
+ def _table_grid(self):
399
+ """Uniform-matter nodes of the EOS table (above n_t with the SLy crust)."""
400
+ if self.crust == "sly" and self.n_surface is not None:
401
+ return core_grid(self.n_grid, self.n_t)
402
+ return self.n_grid
403
+
404
+ # --- symmetric nuclear matter (for saturation-property validation) ---
405
+ def symmetric(self, n_B, params=None):
406
+ p = dict(DEFAULT_PARAMS)
407
+ if params:
408
+ p.update(params)
409
+ eps, pres, _ = _eps_p_of_n(jnp.asarray(n_B), 0.5, p, leptons=False)
410
+ return eps, pres # MeV/fm^3
411
+
412
+ def composition(self, params: dict = None) -> Composition:
413
+ """beta-equilibrium (x_p, x_e, x_mu) on ``self.n_grid``, the same solve
414
+ that builds :meth:`eos_table`."""
415
+ p = dict(DEFAULT_PARAMS)
416
+ if params:
417
+ p.update(params)
418
+ return compiled_method(self, "_composition", p)
419
+
420
+ def _composition(self, p):
421
+ n = self.n_grid
422
+ x_p = _solve_xp(n, p)
423
+ _, n_e, n_mu, _ = _leptons(x_p * n)
424
+ return Composition(n=n, x_p=x_p, x_e=n_e / n, x_mu=n_mu / n)
425
+
426
+ def landau_masses(self, params, comp):
427
+ """(m*_n/M, m*_p/M) on ``comp`` (see :func:`landau_masses`)."""
428
+ # _solve_phi bisects: compiled, or it recompiles on every call
429
+ return compiled_method(self, "_landau", params or {}, comp.n, comp.x_p)
430
+
431
+ @staticmethod
432
+ def _landau(params, n, x_p):
433
+ return landau_masses(params, n, x_p)
434
+
435
+ def eos_table(self, params: dict = None):
436
+ p = dict(DEFAULT_PARAMS)
437
+ if params:
438
+ p.update(params)
439
+ return compiled_table(self, p)
440
+
441
+ def _table_flags(self, p):
442
+ n = self._table_grid()
443
+ x_p = _solve_xp(n, p) # beta-equilibrium
444
+ Phi = _solve_phi((1.0 - x_p) * n, x_p * n, p) # robust first-root, vec
445
+ eps, pres, _ = jax.vmap(lambda ni, xi, Ph: _eps_p_from_state(ni, xi, Ph, p))(
446
+ n, x_p, Phi)
447
+ pres, ln_nu = monotone_core(n, pres, self.n_t)
448
+ return finish_table_flags(n, eps, pres, self.crust, self.n_t,
449
+ self.n_surface, self.gamma_crust, ln_nu,
450
+ self.allow_unstable_core)