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/ejecta.py ADDED
@@ -0,0 +1,614 @@
1
+ """Differentiable ejecta-conditions surrogate (the bridge "mmapipe" segment).
2
+
3
+ Maps binary parameters + EOS-derived stellar quantities to merger-ejecta
4
+ conditions -- dynamical ejecta mass and velocity, remnant disk mass, and an
5
+ electron-fraction / lanthanide-proxy summary -- via published NR fitting
6
+ formulae, JAX end-to-end, so that composed with the metamodel + TOV machinery
7
+ one gets d(ejecta)/d(NEP) gradients (FRIB-SCOPING section 5: the
8
+ FRIB -> E_sym -> EOS -> ejecta -> nucleosynthesis chain).
9
+
10
+ Every quantity is returned as a :class:`UQ` (value, sigma_fit, sigma_formula)
11
+ -- never a bare number. ``sigma_fit`` encodes the calibration papers' own
12
+ residual scatter; ``sigma_formula`` is the spread across independent fit
13
+ families evaluated on the same inputs. These two systematics are what the
14
+ bridge's honest error bars are made of; downstream consumers (n2n trajectory
15
+ gradients, p5-crossover composition claims) must carry both.
16
+
17
+ Extensibility: fit families live in registries (``MEJ_FAMILIES`` etc.) of
18
+ :class:`FitFamily` entries -- a predictor plus a sigma model plus provenance
19
+ -- and ``ejecta_conditions`` accepts registry overrides. The input struct
20
+ :class:`EjectaInputs` already carries aligned spins (chi1, chi2); every
21
+ family implemented today IGNORES spin (none of the published calibrations
22
+ include it), but a spin-aware fit is a registry entry away, with no API
23
+ change. The same route serves future replacements for these fits, which are
24
+ known to be crude (factor-~2-level residuals; see each family's sigma note).
25
+
26
+ Implemented fit families (all coefficients verified against the source PDFs;
27
+ house one-strike rule as in ``postmerger.py``: PDF-verified, sign-suspect
28
+ until anchored):
29
+
30
+ - ``radice2018`` Radice et al. 2018, ApJ 869, 130 (arXiv:1809.11161).
31
+ M_ej: Eq. (18) [Dietrich & Ujevic form, refit], coefficients
32
+ Eqs. (20)-(21); v_ej: Eqs. (22)-(23); M_disk: Eq. (25)
33
+ (tanh in Lambda-tilde). Calibrated on their fiducial
34
+ microphysical-EOS simulations without neutrino re-absorption,
35
+ q close to 1.
36
+ - ``kf2020`` Kruger & Foucart 2020, PRD 101, 103002 (arXiv:2002.07728).
37
+ M_dyn: Eq. (6) (compactness-based, no baryon masses needed);
38
+ M_disk: Eq. (4) (compactness of the lighter star).
39
+ Calibration: C_lighter in [0.135, 0.219], mass ratio
40
+ (lighter/heavier) in [0.775, 1]. No velocity fit.
41
+ - ``nedora2021`` Nedora et al. 2022, CQG 39, 015008 (arXiv:2011.11110).
42
+ Second-order polynomials P_2^2(q, Lambda-tilde), their
43
+ Eq. (6), in the calibration their conclusion recommends
44
+ (M0RefSet + M0/M1Set: "datasets with the most advanced
45
+ physics input", i.e. neutrino emission AND absorption).
46
+ Provides M_ej, v_ej, <Ye>, and M_disk. q = m_heavy/m_light
47
+ >= 1; combined calibration range roughly q <= 1.3 (M0/M1Set)
48
+ to 1.8 (M0RefSet), Lambda-tilde in [340, 1440].
49
+
50
+ Electron-fraction component model (coarse, every number cited):
51
+
52
+ - tidal (cold) component Ye ~ 0.10: Radice et al. 2018 Sec. 3.1: the tidal
53
+ tail is "partially reprocessed by weak reactions to slightly higher values
54
+ of Ye ~ 0.1"; pre-reprocessing values are lower. Carried as 0.10 +/- 0.05.
55
+ - shocked/polar component Ye ~ 0.30: same source: shocked material is
56
+ "reprocessed to Ye > 0.25", with a neutrino-wind component "with Ye up to
57
+ 0.4". Encoded as 0.30 +/- 0.08 (covers 0.25-0.4 at ~1 sigma).
58
+ - mass-weighted <Ye> comes from the Nedora et al. P_2^2 fit; the tidal mass
59
+ fraction is then INFERRED by inverting the two-component mixture, so the
60
+ component split inherits its (q, Lambda-tilde) dependence from a published
61
+ fit rather than from an invented one.
62
+ - lanthanide proxy: smooth step at Ye_crit = 0.26 +/- 0.04, from Lippuner &
63
+ Roberts 2015, ApJ 815, 82 (arXiv:1508.03133), abstract: "the ejecta is
64
+ lanthanide-free for Ye >~ 0.22-0.30, depending on s and tau".
65
+
66
+ Differentiability contract: all outputs support jax.grad / jax.jacfwd with
67
+ respect to (m1, m2) and the EOS inputs (Lambda_i, R_i). Clamps use
68
+ jnp.maximum / jnp.clip, so gradients are exactly zero in clamped (physically
69
+ saturated) regimes rather than NaN. ``ejecta_from_family`` composes with
70
+ ``tov.family.Family`` interpolators; ``tests/test_ejecta.py`` checks
71
+ d M_ej / d L_sym through the metamodel + TOV stack against finite differences.
72
+ """
73
+ from __future__ import annotations
74
+
75
+ from dataclasses import dataclass
76
+ from typing import Callable, NamedTuple, Optional
77
+
78
+ import jax.numpy as jnp
79
+
80
+ from .constants import GMSUN_KM
81
+
82
+ __all__ = [
83
+ "UQ", "EjectaInputs", "EjectaConditions", "FitFamily",
84
+ "lambda_tilde", "baryonic_mass",
85
+ "radice2018_mej", "radice2018_vej", "radice2018_mdisk",
86
+ "kf2020_mdyn", "kf2020_mdisk",
87
+ "nedora2021_mej", "nedora2021_vej", "nedora2021_ye", "nedora2021_mdisk",
88
+ "MEJ_FAMILIES", "VEJ_FAMILIES", "MDISK_FAMILIES",
89
+ "prompt_collapse_disk_factor", "lanthanide_weight",
90
+ "ejecta_conditions", "ejecta_from_family",
91
+ "YE_TIDAL", "YE_SHOCKED", "YE_LANTHANIDE_CRIT",
92
+ ]
93
+
94
+
95
+ class UQ(NamedTuple):
96
+ """A value with its two systematic error components.
97
+
98
+ ``sigma_fit`` residual scatter of the underlying calibration(s), as
99
+ published (or explicitly encoded from published
100
+ statements -- see each fit's sigma note).
101
+ ``sigma_formula`` spread (population std) across the independent fit
102
+ families evaluated at the same inputs; zero when only
103
+ one family exists for the quantity.
104
+ """
105
+ value: jnp.ndarray
106
+ sigma_fit: jnp.ndarray
107
+ sigma_formula: jnp.ndarray
108
+
109
+ @property
110
+ def sigma(self):
111
+ """Total 1-sigma: fit scatter and inter-formula spread in quadrature."""
112
+ return jnp.sqrt(self.sigma_fit ** 2 + self.sigma_formula ** 2)
113
+
114
+
115
+ class EjectaInputs(NamedTuple):
116
+ """Binary + EOS-derived inputs to the fit families.
117
+
118
+ Masses in Msun, radii in km, Lambda dimensionless, chi = aligned
119
+ dimensionless spins (accepted for forward compatibility; all currently
120
+ published calibrations are non-spinning and ignore them). Derived
121
+ quantities (compactness, Lambda-tilde, heavy/light ordering) are provided
122
+ as methods so every family sees one convention.
123
+ """
124
+ m1: jnp.ndarray
125
+ m2: jnp.ndarray
126
+ lambda1: jnp.ndarray
127
+ lambda2: jnp.ndarray
128
+ r1_km: jnp.ndarray
129
+ r2_km: jnp.ndarray
130
+ chi1: jnp.ndarray = 0.0
131
+ chi2: jnp.ndarray = 0.0
132
+
133
+ @property
134
+ def c1(self):
135
+ return GMSUN_KM * self.m1 / self.r1_km
136
+
137
+ @property
138
+ def c2(self):
139
+ return GMSUN_KM * self.m2 / self.r2_km
140
+
141
+ @property
142
+ def lam_tilde(self):
143
+ return lambda_tilde(self.m1, self.m2, self.lambda1, self.lambda2)
144
+
145
+ @property
146
+ def q_heavy(self):
147
+ """Mass ratio m_heavy/m_light >= 1 (Nedora et al. convention)."""
148
+ return (jnp.maximum(self.m1, self.m2) / jnp.minimum(self.m1, self.m2))
149
+
150
+ def light(self):
151
+ """(mass, compactness) of the lighter star, differentiably selected.
152
+
153
+ The equal-mass tie is broken on compactness (more compact counts as
154
+ "lighter"), so the selection is invariant under swapping star labels
155
+ even for the EOS-inconsistent input m1 == m2 with r1 != r2.
156
+ """
157
+ one_is_light = ((self.m1 < self.m2)
158
+ | ((self.m1 == self.m2) & (self.c1 >= self.c2)))
159
+ m = jnp.where(one_is_light, self.m1, self.m2)
160
+ c = jnp.where(one_is_light, self.c1, self.c2)
161
+ return m, c
162
+
163
+
164
+ @dataclass(frozen=True)
165
+ class FitFamily:
166
+ """One published fitting formula: predictor, sigma model, provenance.
167
+
168
+ ``predict(x)`` maps :class:`EjectaInputs` to the quantity; ``sigma(v, x)``
169
+ maps (predicted value, inputs) to the family's own 1-sigma residual
170
+ scatter. ``cite`` is the arXiv id; ``notes`` records calibration range
171
+ and caveats. Registering a new (e.g. spin-dependent) family means adding
172
+ one of these to the relevant registry -- nothing else changes.
173
+ """
174
+ name: str
175
+ cite: str
176
+ predict: Callable
177
+ sigma: Callable
178
+ notes: str = ""
179
+
180
+
181
+ class EjectaConditions(NamedTuple):
182
+ """Ejecta-conditions summary. Masses in Msun, velocities in c, Ye pure."""
183
+ m_dyn: UQ # dynamical ejecta mass
184
+ v_dyn: UQ # mass-averaged asymptotic dynamical-ejecta velocity
185
+ m_disk: UQ # remnant accretion-disk mass
186
+ ye_dyn: UQ # mass-averaged electron fraction of dynamical ejecta
187
+ ye_tidal: UQ # characteristic Ye, tidal component (literature const)
188
+ ye_shocked: UQ # characteristic Ye, shocked component (literature const)
189
+ f_tidal: UQ # tidal mass fraction inferred from the Ye mixture
190
+ x_lanthanide: UQ # lanthanide-rich mass-fraction proxy of the dyn. ejecta
191
+ m_dyn_by: dict # per-family central values (Msun)
192
+ v_dyn_by: dict # per-family central values (c)
193
+ m_disk_by: dict # per-family central values (Msun)
194
+
195
+
196
+ # ----------------------------------------------------------------------------
197
+ # helpers
198
+ # ----------------------------------------------------------------------------
199
+
200
+ def lambda_tilde(m1, m2, lambda1, lambda2):
201
+ """Reduced (effective) tidal deformability Lambda-tilde.
202
+
203
+ Standard mass weighting (Favata 2014 / Wade et al. 2014); same weights as
204
+ ``postmerger.dtilde``, so equal masses give lambda_tilde(m,m,L,L) = L.
205
+ """
206
+ mt5 = (m1 + m2) ** 5
207
+ return (16.0 / 13.0) * ((m1 + 12.0 * m2) * m1 ** 4 * lambda1
208
+ + (m2 + 12.0 * m1) * m2 ** 4 * lambda2) / mt5
209
+
210
+
211
+ def baryonic_mass(m, C):
212
+ """Baryonic mass from gravitational mass and compactness.
213
+
214
+ Kruger & Foucart 2020 Eq. (7): M_b = M (1 + 0.6 C / (1 - 0.5 C)); the
215
+ approximation they themselves use to evaluate baryon-mass-based ejecta
216
+ fits when only (M, R) are known.
217
+ """
218
+ return m * (1.0 + 0.6 * C / (1.0 - 0.5 * C))
219
+
220
+
221
+ # ----------------------------------------------------------------------------
222
+ # Radice et al. 2018 (arXiv:1809.11161)
223
+ # ----------------------------------------------------------------------------
224
+
225
+ # Eq. (20)-(21), verified from the PDF.
226
+ _R18_MEJ = dict(alpha=-0.657, beta=4.254, gamma=-32.61, delta=5.205, n=-0.773)
227
+ # Eq. (23), verified.
228
+ _R18_VEJ = dict(alpha=-0.287, beta=0.494, gamma=-3.000)
229
+ # Eq. (25), verified; floor 1e-3 Msun.
230
+ _R18_MDISK = dict(alpha=0.084, beta=0.127, gamma=567.1, delta=405.14)
231
+
232
+
233
+ def radice2018_mej(x: EjectaInputs):
234
+ """Dynamical ejecta mass [Msun], Radice et al. 2018 Eq. (18).
235
+
236
+ M_ej/(1e-3 Msun) = [alpha (M_b/M_a)^(1/3) (1-2C_a)/C_a
237
+ + beta (M_b/M_a)^n + gamma (1 - M_a/M_a*)] M_a*
238
+ + (a <-> b) + delta,
239
+ with M* the baryonic masses (formed here via the KF2020 Eq. (7)
240
+ approximation). Negative predictions are clamped to zero.
241
+ """
242
+ p = _R18_MEJ
243
+ mb1 = baryonic_mass(x.m1, x.c1)
244
+ mb2 = baryonic_mass(x.m2, x.c2)
245
+
246
+ def one(ma, mb, ca, mstar_a):
247
+ return (p["alpha"] * (mb / ma) ** (1.0 / 3.0) * (1.0 - 2.0 * ca) / ca
248
+ + p["beta"] * (mb / ma) ** p["n"]
249
+ + p["gamma"] * (1.0 - ma / mstar_a)) * mstar_a
250
+
251
+ fit = one(x.m1, x.m2, x.c1, mb1) + one(x.m2, x.m1, x.c2, mb2) + p["delta"]
252
+ return 1.0e-3 * jnp.maximum(fit, 0.0)
253
+
254
+
255
+ def radice2018_vej(x: EjectaInputs):
256
+ """Mass-averaged asymptotic ejecta velocity [c], Radice et al. 2018 Eq. (22).
257
+
258
+ v = [alpha (M_a/M_b)(1 + gamma C_a)] + (a <-> b) + beta.
259
+ """
260
+ p = _R18_VEJ
261
+ v = (p["alpha"] * (x.m1 / x.m2) * (1.0 + p["gamma"] * x.c1)
262
+ + p["alpha"] * (x.m2 / x.m1) * (1.0 + p["gamma"] * x.c2) + p["beta"])
263
+ return jnp.maximum(v, 0.0)
264
+
265
+
266
+ def radice2018_mdisk(x: EjectaInputs):
267
+ """Remnant disk mass [Msun], Radice et al. 2018 Eq. (25).
268
+
269
+ M_disk = max(1e-3, alpha + beta * tanh((Lambda-tilde - gamma)/delta)).
270
+ Calibrated on comparable-mass binaries; they warn that large mass
271
+ asymmetry and prompt collapse are outside the fit.
272
+ """
273
+ p = _R18_MDISK
274
+ m = (p["alpha"]
275
+ + p["beta"] * jnp.tanh((x.lam_tilde - p["gamma"]) / p["delta"]))
276
+ return jnp.maximum(m, 1.0e-3)
277
+
278
+
279
+ # ----------------------------------------------------------------------------
280
+ # Kruger & Foucart 2020 (arXiv:2002.07728)
281
+ # ----------------------------------------------------------------------------
282
+
283
+ # Eq. (6), verified.
284
+ _KF20_MDYN = dict(a=-9.3335, b=114.17, c=-337.56, n=1.5465)
285
+ # Eq. (4), verified; floor 5e-4 * M_lighter.
286
+ _KF20_MDISK = dict(a=-8.1324, c=1.4820, d=1.7784)
287
+
288
+
289
+ def kf2020_mdyn(x: EjectaInputs):
290
+ """Dynamical ejecta mass [Msun], Kruger & Foucart 2020 Eq. (6).
291
+
292
+ M_dyn/(1e-3 Msun) = [a/C_1 + b (M_2/M_1)^n + c C_1] M_1 + (1 <-> 2),
293
+ negative values -> 0. Vanishes for both very compact and very extended
294
+ stars (physical prompt-collapse / no-shock limits, their Sec. III).
295
+ """
296
+ p = _KF20_MDYN
297
+
298
+ def one(ma, mb, ca):
299
+ return (p["a"] / ca + p["b"] * (mb / ma) ** p["n"] + p["c"] * ca) * ma
300
+
301
+ fit = one(x.m1, x.m2, x.c1) + one(x.m2, x.m1, x.c2)
302
+ return 1.0e-3 * jnp.maximum(fit, 0.0)
303
+
304
+
305
+ def kf2020_mdisk(x: EjectaInputs):
306
+ """Remnant disk mass [Msun], Kruger & Foucart 2020 Eq. (4).
307
+
308
+ M_disk = M_light * max(5e-4, (a C_light + c)^d), with the compactness of
309
+ the LIGHTER star (their M1). Anchor (their Table I): C1 = 0.167,
310
+ M1 = 1.5 -> M_disk = 0.037 Msun. The (a C + c)^d factor exceeds 1 for
311
+ C < 0.06 (far outside the C in [0.135, 0.219] calibration); we cap it at
312
+ 1 -- i.e. M_disk <= M_light, a generous baryon-budget ceiling that is OUR
313
+ regularization, not the paper's.
314
+ """
315
+ p = _KF20_MDISK
316
+ m_light, c_light = x.light()
317
+ base = jnp.maximum(p["a"] * c_light + p["c"], 0.0)
318
+ return m_light * jnp.clip(base ** p["d"], 5.0e-4, 1.0)
319
+
320
+
321
+ # ----------------------------------------------------------------------------
322
+ # Nedora et al. 2021 (arXiv:2011.11110)
323
+ # ----------------------------------------------------------------------------
324
+
325
+ # P_2^2(q, Lt) = b0 + b1 q + b2 Lt + b3 q^2 + b4 q Lt + b5 Lt^2, their Eq. (6),
326
+ # q = m_heavy/m_light >= 1. Rows "& M0/M1Set" of Table IV (ejecta) and
327
+ # Table VI (disk): the calibration recommended in their conclusion. Verified
328
+ # from the PDF.
329
+ _N21_LOG10_MEJ = (-1.32, -3.82e-1, -4.47e-3, -3.39e-1, 3.21e-3, 4.31e-7)
330
+ _N21_VEJ = (5.94e-1, -1.48e-1, -8.62e-4, -5.02e-2, 3.25e-4, 3.16e-7)
331
+ _N21_YE = (2.55e-1, 3.83e-2, 2.36e-4, -6.66e-2, -1.92e-4, -1.86e-8)
332
+ _N21_MDISK = (-1.85, 2.59, 7.07e-4, -7.33e-1, -8.08e-4, 2.75e-7)
333
+
334
+
335
+ def _p22(coef, q, lt):
336
+ b0, b1, b2, b3, b4, b5 = coef
337
+ return b0 + b1 * q + b2 * lt + b3 * q ** 2 + b4 * q * lt + b5 * lt ** 2
338
+
339
+
340
+ def nedora2021_mej(x: EjectaInputs):
341
+ """Dynamical ejecta mass [Msun] = 10**P_2^2(q, Lt), Nedora et al. Table IV
342
+ (the fitted quantity is log10(M_ej/Msun))."""
343
+ return 10.0 ** _p22(_N21_LOG10_MEJ, x.q_heavy, x.lam_tilde)
344
+
345
+
346
+ def nedora2021_vej(x: EjectaInputs):
347
+ """Mean asymptotic ejecta velocity [c] = P_2^2(q, Lt), Nedora et al.
348
+
349
+ The polynomial grows quadratically in Lambda-tilde beyond its calibration
350
+ ceiling (~1440) and turns superluminal near Lt ~ 2700; we clip to [0, 1)
351
+ -- the exact kinematic bound, OUR regularization -- but any value beyond
352
+ ~0.35 c already signals extrapolation far outside the fit.
353
+ """
354
+ return jnp.clip(_p22(_N21_VEJ, x.q_heavy, x.lam_tilde), 0.0, 0.99)
355
+
356
+
357
+ def nedora2021_ye(x: EjectaInputs):
358
+ """Mass-averaged dynamical-ejecta Ye = P_2^2(q, Lt), Nedora et al.
359
+
360
+ Anchor: over the restricted common range q in (1, 1.2), Lt in (350, 850)
361
+ their M0RefSet + M0/M1Set mean is <Ye> = 0.20 +/- 0.04 (Sec. III C).
362
+ Clipped to [0.01, 0.5] outside the calibrated region.
363
+ """
364
+ return jnp.clip(_p22(_N21_YE, x.q_heavy, x.lam_tilde), 0.01, 0.5)
365
+
366
+
367
+ def nedora2021_mdisk(x: EjectaInputs):
368
+ """Remnant disk mass [Msun] = P_2^2(q, Lt), Nedora et al. Table VI.
369
+
370
+ The calibration is performed as log10(data) vs log10(P_2^2), i.e. the
371
+ polynomial gives the mass directly. Their P22 fit publishes NO floor
372
+ (the -3.0 floor in their Eqs. 11-12 belongs to the tanh-form rewrites,
373
+ which they note P22 is "free from"); the [1e-3 Msun, M_light] clip here
374
+ is OUR gradient-safe regularization: the polynomial goes negative for
375
+ compact binaries and grows past the binary's own mass beyond the
376
+ Lambda-tilde ~ 1440 calibration ceiling.
377
+ """
378
+ return jnp.clip(_p22(_N21_MDISK, x.q_heavy, x.lam_tilde),
379
+ 1.0e-3, x.light()[0])
380
+
381
+
382
+ # ----------------------------------------------------------------------------
383
+ # family registries (extension point: add spin-aware / future fits here)
384
+ # ----------------------------------------------------------------------------
385
+ # sigma models -- each an explicit encoding of a published statement:
386
+ # * radice2018 M_ej: their Eq. (17) numerical floor is 0.5*M + 5e-5, but
387
+ # Sec. 4.1 reports fit-vs-data "differences ... as large as ~200%"; we read
388
+ # 200% as a ~2-sigma excursion and carry sigma = 1.0*M + 5e-5 Msun.
389
+ # * kf2020 M_dyn: sigma = 0.004 Msun, their Gaussian fit to the residuals.
390
+ # * nedora2021 M_ej: conclusion quotes accuracy "to a factor of ~2", 68%
391
+ # fractional range (-0.8, +0.2); encoded as sigma = 0.5*M.
392
+ # * radice2018 v_ej: fiducial numerical uncertainty 0.02 c, residuals "as
393
+ # large as 0.1 c"; 0.1 c read as ~2 sigma -> sigma = 0.05 c.
394
+ # * nedora2021 v_ej: "typically to ~50%", 68% range (-0.4, +0.2) -> 0.3*v.
395
+ # * disk masses: Radice Eq. (24) / KF2020 Eq. (3) both use
396
+ # Delta M_disk = 0.5*M + 5e-4; Nedora 68% range (-0.4, +0.2) -> 0.3*M + 5e-4.
397
+
398
+ MEJ_FAMILIES = {
399
+ "radice2018": FitFamily(
400
+ "radice2018", "1809.11161", radice2018_mej,
401
+ lambda v, x: 1.0 * v + 5.0e-5,
402
+ "DU-form refit; microphysical EOS, no nu-absorption, q ~ 1"),
403
+ "kf2020": FitFamily(
404
+ "kf2020", "2002.07728", kf2020_mdyn,
405
+ lambda v, x: jnp.full_like(jnp.asarray(v, float), 4.0e-3),
406
+ "compactness form; calibrated incl. asymmetric + near-threshold runs"),
407
+ "nedora2021": FitFamily(
408
+ "nedora2021", "2011.11110", nedora2021_mej,
409
+ lambda v, x: 0.5 * v,
410
+ "P22(q, Lt), M0RefSet+M0/M1Set (recommended row)"),
411
+ }
412
+
413
+ VEJ_FAMILIES = {
414
+ "radice2018": FitFamily(
415
+ "radice2018", "1809.11161", radice2018_vej,
416
+ lambda v, x: jnp.full_like(jnp.asarray(v, float), 0.05), ""),
417
+ "nedora2021": FitFamily(
418
+ "nedora2021", "2011.11110", nedora2021_vej,
419
+ lambda v, x: 0.3 * v, ""),
420
+ }
421
+
422
+ MDISK_FAMILIES = {
423
+ "radice2018": FitFamily(
424
+ "radice2018", "1809.11161", radice2018_mdisk,
425
+ lambda v, x: 0.5 * v + 5.0e-4, "tanh in Lambda-tilde"),
426
+ "kf2020": FitFamily(
427
+ "kf2020", "2002.07728", kf2020_mdisk,
428
+ lambda v, x: 0.5 * v + 5.0e-4, "lighter-star compactness"),
429
+ "nedora2021": FitFamily(
430
+ "nedora2021", "2011.11110", nedora2021_mdisk,
431
+ lambda v, x: 0.3 * v + 5.0e-4, "P22(q, Lt)"),
432
+ }
433
+
434
+
435
+ # ----------------------------------------------------------------------------
436
+ # electron fraction, lanthanide proxy, prompt collapse
437
+ # ----------------------------------------------------------------------------
438
+
439
+ # Component Ye constants: see module docstring for the exact quotes. Kept as
440
+ # Python floats (not jnp arrays) so no dtype is baked in at import time --
441
+ # import-time jnp.asarray would stamp float32 whenever this module is imported
442
+ # before jax x64 is enabled, silently perturbing results at the 1e-8 level.
443
+ YE_TIDAL = UQ(0.10, 0.05, 0.0)
444
+ YE_SHOCKED = UQ(0.30, 0.08, 0.0)
445
+ # Lippuner & Roberts 2015: lanthanide-free for Ye >~ 0.22-0.30 (s, tau dep.).
446
+ YE_LANTHANIDE_CRIT = UQ(0.26, 0.04, 0.0)
447
+ # Smoothing width of the lanthanide step in Ye: OUR regularization choice
448
+ # (comparable to the s,tau-induced blurring of the threshold), not published.
449
+ _LAN_WIDTH = 0.03
450
+
451
+
452
+ def lanthanide_weight(ye, ye_crit=None):
453
+ """Smooth lanthanide-rich indicator in [0, 1]: sigmoid((Ye_c - Ye)/w)."""
454
+ yc = YE_LANTHANIDE_CRIT.value if ye_crit is None else ye_crit
455
+ return 1.0 / (1.0 + jnp.exp((ye - yc) / _LAN_WIDTH))
456
+
457
+
458
+ def prompt_collapse_disk_factor(m_tot, m_thresh, width=0.06):
459
+ """Smooth disk-suppression factor in [0, 1] for prompt collapse.
460
+
461
+ Kruger & Foucart 2020 (quoting the Coughlin et al. total-mass disk fit)
462
+ state that a binary with "M_tot >= 0.95 M_thr will not result in a
463
+ significant accretion disk"; the tanh width of that fit is d = 0.05957 in
464
+ M_tot/M_thr. Encoded as a sigmoid centered at 0.95 M_thr with that
465
+ width -- note this is STRICTER than the underlying Coughlin fit (whose
466
+ tanh is centered at c = 1.0474; the 0.95 quote is roughly its ~2-sigma
467
+ point), i.e. conservative on the disk. Coarse gate, applied only when a
468
+ threshold mass is supplied, and applied to the DISK ONLY: dynamical
469
+ ejecta and the Ye split are deliberately left ungated (the KF20
470
+ compactness form already sends M_dyn -> 0 for very compact stars, but
471
+ the Nedora M_ej and <Ye> fits are not collapse-aware -- near-threshold
472
+ blue-component predictions inherit that bias). The gate's location
473
+ uncertainty is at least the M_thresh fit systematics
474
+ (see ``postmerger.MthreshFit``).
475
+ """
476
+ return 1.0 / (1.0 + jnp.exp(-(0.95 - m_tot / m_thresh) / width))
477
+
478
+
479
+ # ----------------------------------------------------------------------------
480
+ # combined surrogate
481
+ # ----------------------------------------------------------------------------
482
+
483
+ def _combine(families: dict, x: EjectaInputs):
484
+ """Evaluate a family registry: mean value, RMS sigma_fit, population
485
+ spread across families. Returns (UQ, {name: value}).
486
+
487
+ The spread uses a where-guarded sqrt so that a single-family registry
488
+ (variance identically zero) gives sigma_formula = 0 with a zero -- not
489
+ NaN -- gradient, keeping the differentiability contract for registry
490
+ overrides.
491
+ """
492
+ by = {name: fam.predict(x) for name, fam in families.items()}
493
+ vals = jnp.stack(list(by.values()))
494
+ sigs = jnp.stack([jnp.broadcast_to(families[n].sigma(by[n], x),
495
+ jnp.shape(vals[0])) for n in by])
496
+ mean = jnp.mean(vals, axis=0)
497
+ var = jnp.mean((vals - mean) ** 2, axis=0)
498
+ spread = jnp.where(var > 0.0, jnp.sqrt(jnp.where(var > 0.0, var, 1.0)), 0.0)
499
+ return UQ(mean, jnp.sqrt(jnp.mean(sigs ** 2, axis=0)), spread), by
500
+
501
+
502
+ def ejecta_conditions(m1, m2, lambda1, lambda2, r1_km, r2_km,
503
+ chi1=0.0, chi2=0.0,
504
+ m_thresh: Optional[float] = None,
505
+ mej_families: Optional[dict] = None,
506
+ vej_families: Optional[dict] = None,
507
+ mdisk_families: Optional[dict] = None,
508
+ ) -> EjectaConditions:
509
+ """Ejecta conditions + systematics from binary and EOS-derived inputs.
510
+
511
+ Parameters: gravitational masses [Msun], dimensionless tidal
512
+ deformabilities Lambda_i, radii [km]; optionally aligned spins (carried,
513
+ unused by current families), a threshold mass [Msun] to gate the disk for
514
+ prompt collapse (e.g. from the ``postmerger`` M_thresh fits), and registry
515
+ overrides to swap or extend the fit families per quantity.
516
+
517
+ Inputs may be scalars or broadcastable jnp arrays; the result is a pytree
518
+ of :class:`UQ` entries, differentiable end-to-end.
519
+ """
520
+ x = EjectaInputs(
521
+ m1=jnp.asarray(m1, float), m2=jnp.asarray(m2, float),
522
+ lambda1=jnp.asarray(lambda1, float), lambda2=jnp.asarray(lambda2, float),
523
+ r1_km=jnp.asarray(r1_km, float), r2_km=jnp.asarray(r2_km, float),
524
+ chi1=jnp.asarray(chi1, float), chi2=jnp.asarray(chi2, float))
525
+
526
+ m_dyn, mej_by = _combine(mej_families or MEJ_FAMILIES, x)
527
+ v_dyn, vej_by = _combine(vej_families or VEJ_FAMILIES, x)
528
+
529
+ mdisk_reg = mdisk_families or MDISK_FAMILIES
530
+ if m_thresh is None:
531
+ m_disk, mdisk_by = _combine(mdisk_reg, x)
532
+ else:
533
+ gate = prompt_collapse_disk_factor(x.m1 + x.m2, m_thresh)
534
+ # Gated sigma is floored at 5e-4 Msun (the Radice/KF20 published disk
535
+ # floors): deep in prompt collapse the papers assert "no SIGNIFICANT
536
+ # disk", not a disk known to better than its floor -- without the
537
+ # floor the gate would shrink the error bar to ~0 exactly where the
538
+ # prediction is least trustworthy.
539
+ gated = {
540
+ n: FitFamily(f.name, f.cite,
541
+ (lambda xx, _f=f: gate * _f.predict(xx)),
542
+ (lambda v, xx, _f=f:
543
+ jnp.maximum(gate * _f.sigma(v, xx), 5.0e-4)),
544
+ f.notes)
545
+ for n, f in mdisk_reg.items()}
546
+ m_disk, mdisk_by = _combine(gated, x)
547
+
548
+ # --- electron fraction & lanthanide proxy ---
549
+ ye = nedora2021_ye(x)
550
+ # sigma_fit = 0.03: Nedora's fiducial Delta Ye = 0.01 scaled by the
551
+ # recommended calibration's chi2_nu = 9.6 (sqrt(9.6)*0.01 ~= 0.03); their
552
+ # all-datasets accuracy quote is ~0.1, treated as an extreme bound.
553
+ ye_dyn = UQ(ye, jnp.broadcast_to(jnp.asarray(0.03), jnp.shape(ye)),
554
+ jnp.zeros(jnp.shape(ye)))
555
+
556
+ # Tidal mass fraction from inverting the two-component Ye mixture:
557
+ # <Ye> = f * Ye_tidal + (1 - f) * Ye_shocked. Clip to [0, 1]; the clip's
558
+ # zero gradient outside the branch is the physically saturated case.
559
+ # KNOWN DEAD ZONE: for q >~ 1.6 at low Lambda-tilde the Nedora <Ye>
560
+ # falls below Ye_tidal = 0.10, the inversion saturates at f = 1 (mixture
561
+ # no longer reproduces <Ye>), and d(f, x_lan)/d(inputs) vanish -- the
562
+ # component split is meaningful only while <Ye> lies inside
563
+ # [Ye_tidal, Ye_shocked]; ye_dyn itself remains valid throughout.
564
+ span = YE_SHOCKED.value - YE_TIDAL.value
565
+ f = jnp.clip((YE_SHOCKED.value - ye) / span, 0.0, 1.0)
566
+ df = jnp.sqrt((ye_dyn.sigma_fit / span) ** 2
567
+ + (f * YE_TIDAL.sigma_fit / span) ** 2
568
+ + ((1.0 - f) * YE_SHOCKED.sigma_fit / span) ** 2)
569
+ f_tidal = UQ(f, df, jnp.zeros(jnp.shape(f)))
570
+
571
+ # Lanthanide-rich mass-fraction proxy: component-weighted smooth step.
572
+ def xlan(yc):
573
+ return (f * lanthanide_weight(YE_TIDAL.value, yc)
574
+ + (1.0 - f) * lanthanide_weight(YE_SHOCKED.value, yc))
575
+ x0 = xlan(YE_LANTHANIDE_CRIT.value)
576
+ # half-range over the published Ye_crit band as the threshold systematic,
577
+ # plus the mixture-fraction uncertainty
578
+ dx_crit = 0.5 * jnp.abs(
579
+ xlan(YE_LANTHANIDE_CRIT.value + YE_LANTHANIDE_CRIT.sigma_fit)
580
+ - xlan(YE_LANTHANIDE_CRIT.value - YE_LANTHANIDE_CRIT.sigma_fit))
581
+ dx_f = jnp.abs(lanthanide_weight(YE_TIDAL.value)
582
+ - lanthanide_weight(YE_SHOCKED.value)) * f_tidal.sigma_fit
583
+ x_lan = UQ(x0, jnp.sqrt(dx_crit ** 2 + dx_f ** 2), jnp.zeros(jnp.shape(x0)))
584
+
585
+ return EjectaConditions(
586
+ m_dyn=m_dyn, v_dyn=v_dyn, m_disk=m_disk, ye_dyn=ye_dyn,
587
+ ye_tidal=YE_TIDAL, ye_shocked=YE_SHOCKED, f_tidal=f_tidal,
588
+ x_lanthanide=x_lan,
589
+ m_dyn_by=mej_by, v_dyn_by=vej_by, m_disk_by=mdisk_by,
590
+ )
591
+
592
+
593
+ def ejecta_from_family(family, m1, m2, m_thresh=None, **kw) -> EjectaConditions:
594
+ """Ejecta conditions from a TOV ``Family`` (the bridge composition point).
595
+
596
+ Pulls Lambda(m) and R(m) from the family's stable-branch interpolators, so
597
+ jax.jacfwd of any output with respect to the nuclear empirical parameters
598
+ flows through metamodel -> TOV -> here (see tests/test_ejecta.py).
599
+ Extra keyword arguments pass through to :func:`ejecta_conditions`.
600
+
601
+ DOMAIN: valid only for m1, m2 <= m_max(family). Above M_max the
602
+ stable-branch interpolators HOLD near their last stable values (the
603
+ off-branch sentinel in ``tov.family``), so a star that should be a black
604
+ hole silently yields plausible-looking BNS ejecta with no warning and a
605
+ smooth-but-wrong NEP gradient. Callers scanning nuclear parameters that
606
+ move M_max (the bridge use case) must guard against m > m_max(family)
607
+ themselves.
608
+ """
609
+ from .tov.family import lambda_of_mass, radius_of_mass
610
+ return ejecta_conditions(
611
+ m1, m2,
612
+ lambda_of_mass(family, m1), lambda_of_mass(family, m2),
613
+ radius_of_mass(family, m1), radius_of_mass(family, m2),
614
+ m_thresh=m_thresh, **kw)
@@ -0,0 +1,19 @@
1
+ """EOS models for NuclearMatter-Backend."""
2
+ from .base import EOSModel, EOSTable, Composition, build_h_and_cs2
3
+ from .metamodel import MetaModelEOS, DEFAULT_PARAMS as METAMODEL_DEFAULTS
4
+ from .polytrope import PolytropeEOS
5
+ from .hybrid import HybridEOS, TransitionInfo
6
+ from .rmft import RMFTEOS, GM1
7
+ from .ddrmf import DDRMFEOS, DD2, DDME2
8
+ from .skyrme import SkyrmeEOS, SLY4, BSKG3
9
+ from .compose import (load_compose, load_simple, load_cmf, write_compose,
10
+ eos_table_from_arrays, TabularEOS)
11
+
12
+ __all__ = [
13
+ "EOSModel", "EOSTable", "Composition", "build_h_and_cs2",
14
+ "MetaModelEOS", "METAMODEL_DEFAULTS", "PolytropeEOS",
15
+ "HybridEOS", "TransitionInfo", "RMFTEOS", "GM1",
16
+ "DDRMFEOS", "DD2", "DDME2", "SkyrmeEOS", "SLY4", "BSKG3",
17
+ "load_compose", "load_simple", "load_cmf", "write_compose",
18
+ "eos_table_from_arrays", "TabularEOS",
19
+ ]