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
@@ -0,0 +1,120 @@
1
+ """Validation figure and G0 timing for the NSCool driver.
2
+
3
+ python -m nmbackend.cooling.validate --fig nscool_validation.png
4
+ python -m nmbackend.cooling.validate --timing 10
5
+
6
+ Panel (a) compares the driver with NSCool's bundled Model_1 output
7
+ (Teff_Try_KEEP.dat: APR, 1.4 Msun, SFB / gap "a" / T73, iron envelope).
8
+ Panel (b) shows the mass dependence on the same EOS; panel (c) shows p1S0
9
+ scalings (Zhou+2026 Eq. 10) quenching dUrca at 2.1 Msun.
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import argparse
14
+ import platform
15
+ import tempfile
16
+ import time
17
+
18
+ import numpy as np
19
+
20
+ from .runner import GapModel, NSCool, parse_teff_file
21
+
22
+ BLUE, ORANGE, INK, MUTED = "#2a78d6", "#eb6834", "#222222", "#6b6a64"
23
+ RAMP = ["#9ec5f4", "#6da7ec", "#3987e5", "#256abf", "#104281"]
24
+
25
+
26
+ def make_figure(path: str, nscool: NSCool) -> None:
27
+ import matplotlib
28
+ matplotlib.use("Agg")
29
+ import matplotlib.pyplot as plt
30
+
31
+ ref = parse_teff_file(nscool.home / "Model_1" / "Teff_Try_KEEP.dat")
32
+ release = GapModel(ca_n3p2=2.0) # n3P2 rate of the bundled output
33
+ prof = nscool.run(profile=nscool.home / "TOV/Profile/Prof_APR_Cat_1.4.dat",
34
+ gaps=release)
35
+ tov = nscool.run(mass=1.4, gaps=release)
36
+
37
+ fig, ax = plt.subplots(1, 3, figsize=(13, 4.2), sharey=True)
38
+ for a in ax:
39
+ a.set_xscale("log"); a.set_yscale("log")
40
+ a.set_xlim(1e-1, 3e6); a.set_ylim(8e4, 4e6)
41
+ a.set_xlabel("age [yr]")
42
+ a.grid(True, which="major", color="#e4e3dc", lw=0.6)
43
+ for s in ("top", "right"):
44
+ a.spines[s].set_visible(False)
45
+ ax[0].set_ylabel(r"$T_{\rm eff}^\infty$ [K]")
46
+
47
+ a = ax[0]
48
+ a.plot(ref.t_yr[::4], ref.Teff_inf[::4], "o", ms=5, mfc="none", mec=INK,
49
+ label="NSCool bundled output")
50
+ a.plot(prof.t_yr, prof.Teff_inf, color=BLUE, lw=2, label="driver, bundled profile")
51
+ a.plot(tov.t_yr, tov.Teff_inf, color=ORANGE, lw=2, ls="--",
52
+ label="driver, TOV at 1.4")
53
+ dev = np.max(np.abs(prof.Teff_inf / ref.Teff_inf - 1))
54
+ a.set_title(f"(a) APR 1.4 Msun: max |dT/T| = {dev:.1e}", fontsize=10, loc="left")
55
+ a.legend(frameon=False, fontsize=8, loc="lower left")
56
+
57
+ a = ax[1]
58
+ for m, c in zip((1.0, 1.4, 1.8, 2.0, 2.1), RAMP):
59
+ cc = nscool.run(mass=m)
60
+ a.plot(cc.t_yr, cc.Teff_inf, color=c, lw=2, label=f"{m:.1f}")
61
+ a.set_title("(b) mass [Msun], Model_1 gaps", fontsize=10, loc="left")
62
+ a.legend(frameon=False, fontsize=8, loc="lower left")
63
+
64
+ a = ax[2]
65
+ # CCDK p1S0 (SFP1S0=7), n3P2 off as in Zhou+2026. Unscaled, the gap
66
+ # closes at n = 0.58 fm^-3, below the dUrca core; s_x >= 2 covers it.
67
+ for (sx, sy), c, ls in zip(((1.0, 1.0), (2.0, 1.0), (3.0, 1.0), (2.0, 0.2)),
68
+ (INK, BLUE, ORANGE, MUTED), ("-", "-", "-", "--")):
69
+ cc = nscool.run(mass=2.1, gaps=GapModel(p1s0=7.0, n3p2=0.0,
70
+ sx_p1s0=sx, sy_p1s0=sy))
71
+ a.plot(cc.t_yr, cc.Teff_inf, color=c, lw=2, ls=ls,
72
+ label=fr"$s_x={sx}$, $s_y={sy}$")
73
+ a.set_title("(c) 2.1 Msun (dUrca), CCDK p1S0, no n3P2", fontsize=10, loc="left")
74
+ a.legend(frameon=False, fontsize=8, loc="lower left")
75
+
76
+ fig.tight_layout()
77
+ fig.savefig(path, dpi=150)
78
+
79
+
80
+ def time_curves(nscool: NSCool, repeats: int) -> None:
81
+ """Wall time per curve, one process at a time (NSCool is single-threaded)."""
82
+ print(f"host {platform.node()} cpu {platform.processor() or platform.machine()}")
83
+ cases = [("1.4, bundled profile", dict(profile=nscool.home / "TOV/Profile/Prof_APR_Cat_1.4.dat")),
84
+ ("1.4, TOV + cool", dict(mass=1.4)),
85
+ ("2.0, TOV + cool (dUrca)", dict(mass=2.0)),
86
+ ("1.4, eta=1e-9", dict(mass=1.4, eta=1e-9))]
87
+ for label, kw in cases:
88
+ w, n = [], 0
89
+ for _ in range(repeats):
90
+ t0 = time.perf_counter()
91
+ c = nscool.run(**kw)
92
+ w.append(time.perf_counter() - t0)
93
+ n = len(c.t_yr)
94
+ w = np.array(w)
95
+ print(f"{label:28s} steps {n:4d} wall median {np.median(w):.2f} s"
96
+ f" min {w.min():.2f} max {w.max():.2f}")
97
+ w = []
98
+ with tempfile.TemporaryDirectory(dir=nscool.workroot) as d:
99
+ for _ in range(repeats):
100
+ t0 = time.perf_counter()
101
+ nscool.make_profile(nscool.default_eos, 1.4, f"{d}/prof.dat")
102
+ w.append(time.perf_counter() - t0)
103
+ print(f"{'TOV alone':28s} wall median {np.median(w):.2f} s")
104
+
105
+
106
+ def main(argv=None) -> None:
107
+ p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
108
+ p.add_argument("--fig")
109
+ p.add_argument("--timing", type=int, default=0, metavar="REPEATS")
110
+ p.add_argument("--workroot", help="parent of per-run directories")
111
+ a = p.parse_args(argv)
112
+ n = NSCool(workroot=a.workroot)
113
+ if a.fig:
114
+ make_figure(a.fig, n)
115
+ if a.timing:
116
+ time_curves(n, a.timing)
117
+
118
+
119
+ if __name__ == "__main__":
120
+ main()
@@ -0,0 +1,81 @@
1
+ # SLy unified EOS below 0.16 fm^-3: BPS + Haensel-Pichon (1994) outer crust below
2
+ # 5e10 g/cm^3, Douchin & Haensel (2001, astro-ph/0111092) SLy4 inner crust above;
3
+ # rows above the crust-core transition (0.0760 fm^-3) are SLy4 npe-mu matter.
4
+ # Source: Ioffe SLy table (Haensel), as redistributed in jesterTOV/crust_files/DH_crust.dat (MIT).
5
+ # Columns: n_B [fm^-3] rho = eps/c^2 [g/cm^3] P [dyn/cm^2]
6
+ 6.295E-12 1.044E+04 9.744E+18
7
+ 1.581E-11 2.622E+04 4.968E+19
8
+ 3.972E-11 6.587E+04 2.431E+20
9
+ 9.976E-11 1.654E+05 1.151E+21
10
+ 2.506E-10 4.156E+05 5.266E+21
11
+ 6.294E-10 1.044E+06 2.318E+22
12
+ 1.581E-09 2.622E+06 9.755E+22
13
+ 3.972E-09 6.588E+06 3.911E+23
14
+ 5.000E-09 8.293E+06 5.259E+23
15
+ 9.976E-09 1.655E+07 1.435E+24
16
+ 1.990E-08 3.302E+07 3.833E+24
17
+ 3.973E-08 6.592E+07 1.006E+25
18
+ 7.926E-08 1.315E+08 2.604E+25
19
+ 1.581E-07 2.625E+08 6.676E+25
20
+ 1.991E-07 3.305E+08 8.738E+25
21
+ 3.156E-07 5.239E+08 1.629E+26
22
+ 5.001E-07 8.303E+08 3.029E+26
23
+ 6.296E-07 1.046E+09 4.129E+26
24
+ 9.667E-07 1.606E+09 6.860E+26
25
+ 1.532E-06 2.545E+09 1.272E+27
26
+ 2.507E-06 4.166E+09 2.356E+27
27
+ 3.974E-06 6.606E+09 4.362E+27
28
+ 4.830E-06 8.031E+09 5.662E+27
29
+ 6.081E-06 1.011E+10 7.702E+27
30
+ 7.930E-06 1.319E+10 1.048E+28
31
+ 9.982E-06 1.661E+10 1.425E+28
32
+ 1.257E-05 2.091E+10 1.938E+28
33
+ 1.522E-05 2.533E+10 2.503E+28
34
+ 1.991E-05 3.315E+10 3.404E+28
35
+ 2.507E-05 4.174E+10 4.628E+28
36
+ 2.791E-05 4.652E+10 5.367E+28
37
+ 3.807E-05 6.347E+10 7.913E+28
38
+ 5.063E-05 8.444E+10 1.127E+29
39
+ 6.592E-05 1.100E+11 1.561E+29
40
+ 8.433E-05 1.408E+11 2.109E+29
41
+ 1.062E-04 1.774E+11 2.790E+29
42
+ 1.321E-04 2.207E+11 3.625E+29
43
+ 1.624E-04 2.714E+11 4.635E+29
44
+ 1.690E-04 2.825E+11 4.859E+29
45
+ 1.759E-04 2.940E+11 5.093E+29
46
+ 1.830E-04 3.058E+11 5.334E+29
47
+ 1.902E-04 3.180E+11 5.584E+29
48
+ 1.977E-04 3.305E+11 5.843E+29
49
+ 2.054E-04 3.434E+11 6.109E+29
50
+ 2.079E-04 3.476E+11 6.197E+29
51
+ 2.082E-04 3.481E+11 6.208E+29
52
+ 2.091E-04 3.495E+11 6.215E+29
53
+ 2.160E-04 3.612E+11 6.357E+29
54
+ 2.231E-04 3.730E+11 6.468E+29
55
+ 2.311E-04 3.865E+11 6.581E+29
56
+ 2.401E-04 4.016E+11 6.700E+29
57
+ 2.500E-04 4.180E+11 6.823E+29
58
+ 2.643E-04 4.420E+11 6.995E+29
59
+ 3.053E-04 5.108E+11 7.468E+29
60
+ 3.533E-04 5.912E+11 8.015E+29
61
+ 4.076E-04 6.822E+11 8.644E+29
62
+ 4.680E-04 7.834E+11 9.367E+29
63
+ 5.341E-04 8.943E+11 1.019E+30
64
+ 6.059E-04 1.015E+12 1.113E+30
65
+ 7.661E-04 1.283E+12 1.337E+30
66
+ 1.262E-03 2.114E+12 2.155E+30
67
+ 1.895E-03 3.177E+12 3.427E+30
68
+ 2.673E-03 4.483E+12 5.268E+30
69
+ 3.606E-03 6.051E+12 7.798E+30
70
+ 4.710E-03 7.906E+12 1.115E+31
71
+ 7.496E-03 1.259E+13 2.089E+31
72
+ 1.120E-02 1.882E+13 3.584E+31
73
+ 1.600E-02 2.692E+13 5.761E+31
74
+ 2.207E-02 3.717E+13 8.812E+31
75
+ 2.948E-02 4.968E+13 1.295E+32
76
+ 4.268E-02 7.202E+13 2.162E+32
77
+ 6.220E-02 1.051E+14 3.847E+32
78
+ 7.317E-02 1.237E+14 5.046E+32
79
+ 7.596E-02 1.284E+14 5.371E+32
80
+ 1.000E-01 1.694E+14 9.915E+32
81
+ 1.500E-01 2.550E+14 2.983E+33
@@ -0,0 +1,100 @@
1
+ # PROVENANCE: this file is a COPY, carried in NuclearMatter-Backend so the
2
+ # package has no live cross-repo dependency (per the FRIB-SCOPING section 8
3
+ # infrastructure decision: scenario data is shared as small files copied with
4
+ # provenance headers).
5
+ #
6
+ # origin : nmb-papers (matter-backend-papers) scoping/frib_scenario_ladder.yaml
7
+ # -- the single source of truth; update THERE first, then re-copy.
8
+ # copied : 2026-09-05 (origin version 1, dated 2026-08-27)
9
+ #
10
+ # EVERY NUMBER IN THIS FILE IS AN ASSUMPTION, not a literature value: no
11
+ # published paper commits to "sigma_L = +/-X by year Y" for FRIB.
12
+ #
13
+ # Interpretation carried by this copy: nmbackend.lab_likelihood applies
14
+ # psym2n0_frac to the SYMMETRY pressure P_sym = n^2 dS/dn, not to the
15
+ # neutron-matter pressure P_PNM = n^2 (de_sat/dn + dS/dn); the two differ at
16
+ # 2 n_sat by the isoscalar term. Encoding choice flagged for RO review; if
17
+ # the source ladder later pins the other convention, update the module.
18
+ #
19
+ # The original header follows.
20
+ #
21
+ # ---------------------------------------------------------------------------
22
+ # FRIB scenario ladder — single source of truth for laboratory-prior
23
+ # scenarios used across the nmb-papers FRIB track (paper-1 frib_scenarios
24
+ # analysis, p5-crossover forecasts; the MB-N2 nmbackend module consumes a
25
+ # copy with a provenance header when it lands there).
26
+ #
27
+ # EVERY NUMBER IN THIS FILE IS AN ASSUMPTION, not a literature value: no
28
+ # published paper commits to "sigma_L = ±X by year Y" for FRIB
29
+ # (scoping/FRIB-SCOPING.md §1). Program-scope citations:
30
+ # arXiv:2301.13253, arXiv:2410.06144, arXiv:2606.29588; density-sensitivity
31
+ # assignments after Tsang et al. arXiv:2310.11588.
32
+ #
33
+ # sigma values are lab-prior REPLACEMENT widths in MeV (matching
34
+ # ad_symmetry.py PRIOR_SD semantics); absent keys keep the current prior.
35
+
36
+ version: 1
37
+ date: 2026-08-27
38
+ provenance: "scoping/FRIB-SCOPING.md sections 1-2 and the section 8 decision log"
39
+
40
+ scenarios:
41
+ - name: "GW-only"
42
+ tag: "Gw"
43
+ epoch: "baseline"
44
+ assumption: true
45
+ lab_sigma: {}
46
+ basis: >-
47
+ Baseline: hero-catalogue GW measurement alone, current prior
48
+ (reproduces committed symmetry_energy_results.json posterior_N and
49
+ ad_symmetry_results.json ad_posterior_sd).
50
+
51
+ - name: "+FRIB-2028"
52
+ tag: "Near"
53
+ epoch: "2026-2028"
54
+ assumption: true
55
+ lab_sigma: {L_sym: 22.0, E_sym: 2.0}
56
+ basis: >-
57
+ Near-term (now-2028) FRIB structure campaigns: masses/IAS/dipole-response
58
+ give S0 to ±(1-2) MeV while L stays ±15-25 MeV (FRIB-SCOPING.md §1,
59
+ after Tsang et al. arXiv:2310.11588 sensitivity assignments; program
60
+ scope arXiv:2301.13253, 2410.06144). sigma_L=22 sits mid-range of the
61
+ near-term band; ASSUMED ladder value, no citable "±X by year Y" exists.
62
+
63
+ - name: "+FRIB400"
64
+ tag: "Four"
65
+ epoch: "post-2031 (FRIB400 has no CD-0)"
66
+ assumption: true
67
+ lab_sigma: {L_sym: 15.0, E_sym: 1.5}
68
+ basis: >-
69
+ FRIB400 era (post-2031, no CD-0 yet): TPC pion program targets L toward
70
+ ±15-20 MeV if transport UQ succeeds; FRIB400 reaches E_sym(2n0)
71
+ ±10-15 MeV (FRIB-SCOPING.md §1; whitepapers arXiv:2301.13253,
72
+ 2606.29588). sigma_L=15, sigma_S0=1.5 ASSUMED as the optimistic end of
73
+ that ladder.
74
+
75
+ - name: "+FRIB400+Ksym"
76
+ tag: "FourK"
77
+ epoch: "post-2031 (FRIB400 has no CD-0)"
78
+ assumption: true
79
+ lab_sigma: {L_sym: 15.0, E_sym: 1.5, K_sym: 80.0}
80
+ psym2n0_frac: 0.30
81
+ basis: >-
82
+ Machinery-capability variant: FRIB400 P(2n0) to ~±30% mapped
83
+ heuristically onto a curvature width sigma_Ksym=80 MeV (prior 114).
84
+ ILLUSTRATIVE assumption only — a pressure-at-density likelihood
85
+ (Huth arXiv:2107.06229 / Tsang arXiv:2310.11588 encoding) is the honest
86
+ form and is deferred to the crossover paper.
87
+
88
+ # Calendar axis for the p5-crossover epoch mapping. Coarse, ASSUMED, and
89
+ # revisable in one place; the crossover paper's Methods must cite this block
90
+ # as its epoch assumption set.
91
+ gw_epochs:
92
+ assumption: true
93
+ note: >-
94
+ Detector-era assumptions for the crossover paper's calendar axis. Spans
95
+ and event counts are planning assumptions, not schedule commitments; the
96
+ 3G start in particular is a placeholder pending real observatory
97
+ schedules.
98
+ entries:
99
+ - {epoch: "O5", span: "2028-2030", reference: "catalogue growth at O5 sensitivity; rates per population assumptions in paper-3 machinery"}
100
+ - {epoch: "3G-early", span: "2035-", reference: "ET+CE; paper-3 tracer-SMC 3G study used 1.2e4 events; Fisher-I used ~2.5e5/yr at an optimistic 1000 Gpc^-3 yr^-1"}
nmbackend/durca.py ADDED
@@ -0,0 +1,111 @@
1
+ """Nucleonic direct-Urca (dUrca) onset from a beta-equilibrium composition.
2
+
3
+ The process n -> p + l + nubar (l = e, mu) conserves momentum at the Fermi
4
+ surfaces only if the Fermi momenta close a triangle, k_Fn <= k_Fp + k_Fl
5
+ (Lattimer, Pethick, Prakash & Haensel, PRL 66, 2701 (1991)). With
6
+ k_Fi = (3 pi^2 x_i n)^(1/3), charge neutrality and npe matter this is
7
+ x_p >= 1/9; muons lower the threshold toward ~0.148 at high density
8
+ (the upper end of the npe mu band quoted in LPPH).
9
+
10
+ Conventions for "no onset":
11
+ * ``n_DU = inf`` if the triangle never closes on the composition grid.
12
+ * ``M_DU = inf`` if n_DU is above the central density of the maximum-mass
13
+ star (no stable star runs dUrca), including n_DU = inf.
14
+ NaN is not used as a sentinel; it means a failed solve.
15
+
16
+ Numpy post-processing layer (not differentiable end-to-end).
17
+ """
18
+ from __future__ import annotations
19
+
20
+ import math
21
+ from typing import NamedTuple
22
+
23
+ import numpy as np
24
+
25
+ from .tov.solver import solve_structure
26
+
27
+ __all__ = ["DUrcaOnset", "fermi_momenta", "durca_onset", "durca_mass",
28
+ "NPE_THRESHOLD"]
29
+
30
+ #: npe-matter threshold proton fraction: k_Fn = k_Fp + k_Fe with x_e = x_p
31
+ NPE_THRESHOLD = 1.0 / 9.0
32
+
33
+ _3PI2 = 3.0 * math.pi ** 2
34
+
35
+
36
+ class DUrcaOnset(NamedTuple):
37
+ """dUrca onset for one composition (densities in fm^-3)."""
38
+ n_DU: float # electron channel onset density (inf if none)
39
+ x_p_DU: float # proton fraction at n_DU (nan if none)
40
+ n_DU_mu: float # muon channel onset density (inf if none)
41
+ x_p_DU_mu: float # proton fraction at n_DU_mu (nan if none)
42
+
43
+
44
+ def fermi_momenta(comp):
45
+ """(k_Fn, k_Fp, k_Fe, k_Fmu) in fm^-1 from a Composition."""
46
+ n = np.asarray(comp.n, float)
47
+ xp = np.asarray(comp.x_p, float)
48
+ kf = lambda x: np.cbrt(_3PI2 * np.clip(x, 0.0, None) * n)
49
+ return (kf(1.0 - xp), kf(xp), kf(np.asarray(comp.x_e, float)),
50
+ kf(np.asarray(comp.x_mu, float)))
51
+
52
+
53
+ def _first_crossing(n, f, xp, active=None):
54
+ """Lowest n where f = k_Fp + k_Fl - k_Fn goes from < 0 to >= 0.
55
+
56
+ ``active`` masks points where the lepton is present (muons); the onset is
57
+ only accepted at such points. Linear interpolation between grid nodes.
58
+ """
59
+ ok = np.ones_like(f, bool) if active is None else active
60
+ if ok[0] and f[0] >= 0.0:
61
+ return float(n[0]), float(xp[0]) # open from the grid start
62
+ for i in range(1, len(n)):
63
+ if ok[i] and f[i] >= 0.0:
64
+ if not ok[i - 1] or f[i - 1] >= 0.0: # opens as the lepton appears
65
+ return float(n[i]), float(xp[i])
66
+ t = -f[i - 1] / (f[i] - f[i - 1])
67
+ return (float(n[i - 1] + t * (n[i] - n[i - 1])),
68
+ float(xp[i - 1] + t * (xp[i] - xp[i - 1])))
69
+ return math.inf, math.nan
70
+
71
+
72
+ def durca_onset(comp, n_min=0.08) -> DUrcaOnset:
73
+ """Onset densities of the electron and muon dUrca channels.
74
+
75
+ Only densities n >= ``n_min`` (fm^-3) are searched. The default sits near
76
+ the crust-core transition: below it uniform npe matter is not the ground
77
+ state, and a Taylor-expanded S(n) can give spurious large x_p there.
78
+ Only the first opening is reported; a re-closing at higher density is
79
+ ignored. Resolution is that of the composition grid (linear in n).
80
+ """
81
+ kn, kp, ke, kmu = fermi_momenta(comp)
82
+ n = np.asarray(comp.n, float)
83
+ xp = np.asarray(comp.x_p, float)
84
+ s = n >= n_min
85
+ n, xp, kn, kp, ke, kmu = (a[s] for a in (n, xp, kn, kp, ke, kmu))
86
+ n_e, x_e = _first_crossing(n, kp + ke - kn, xp)
87
+ n_m, x_m = _first_crossing(n, kp + kmu - kn, xp, active=kmu > 0.0)
88
+ return DUrcaOnset(n_e, x_e, n_m, x_m)
89
+
90
+
91
+ def durca_mass(table, n_DU, h_max_stable=None, **tov_kw):
92
+ """Gravitational mass (Msun) of the star whose central density is n_DU.
93
+
94
+ ``table`` : EOSTable of the same model (its ``n`` grid maps n -> h).
95
+ ``h_max_stable`` : central pseudo-enthalpy of the maximum-mass star, e.g.
96
+ ``fam.h_c[argmax(where(fam.stable, fam.M_msun, -inf))]``. If omitted,
97
+ n_DU is only checked against the table range and the caller owns the
98
+ stability check.
99
+ Returns inf when n_DU is inf or above the maximum-mass central density.
100
+ Extra keywords go to :func:`nmbackend.tov.solver.solve_structure`.
101
+ """
102
+ if not np.isfinite(n_DU):
103
+ return math.inf
104
+ n = np.asarray(table.n, float)
105
+ h = np.asarray(table.h, float)
106
+ if n_DU > n[-1]:
107
+ return math.inf
108
+ h_DU = float(np.interp(math.log(n_DU), np.log(n), h))
109
+ if h_max_stable is not None and h_DU > float(h_max_stable):
110
+ return math.inf
111
+ return float(solve_structure(table, h_DU, **tov_kw).M_msun)