gravitax 1.0.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.
- gravitax/__init__.py +105 -0
- gravitax/_gradsolve_rodas5p_step.py +312 -0
- gravitax/_nu_quadrature.py +208 -0
- gravitax/_verbose.py +96 -0
- gravitax/ap_imex.py +800 -0
- gravitax/api.py +476 -0
- gravitax/assembly.py +2627 -0
- gravitax/background.py +412 -0
- gravitax/block_solver.py +277 -0
- gravitax/constants.py +147 -0
- gravitax/data/__init__.py +32 -0
- gravitax/data/gpu_settings/grids/chi_box2s_N2000.npy +0 -0
- gravitax/data/gpu_settings/grids/chi_box2s_late4.npy +0 -0
- gravitax/data/gpu_settings/grids/chi_default.npy +0 -0
- gravitax/data/gpu_settings/grids/k_fine_box2s_N6000.npy +0 -0
- gravitax/data/gpu_settings/profiles/profile_tf5_late4.npz +0 -0
- gravitax/data/gpu_settings/settings/accurate_cmb.env +26 -0
- gravitax/data/gpu_settings/settings/accurate_massive_cmb.env +23 -0
- gravitax/data/gpu_settings/settings/accurate_massive_pk.env +9 -0
- gravitax/data/gpu_settings/settings/accurate_pk_grad.env +11 -0
- gravitax/data/gpu_settings/settings/balanced.env +37 -0
- gravitax/data/gpu_settings/settings/balanced_massive.env +26 -0
- gravitax/data/gpu_settings/settings/default.env +44 -0
- gravitax/data/gpu_settings/settings/default_derivatives.env +51 -0
- gravitax/data/gpu_settings/settings/default_derivatives_b1.env +54 -0
- gravitax/data/gpu_settings/settings/default_derivatives_b512.env +54 -0
- gravitax/data/gpu_settings/settings/default_massive.env +50 -0
- gravitax/data/gpu_settings/settings/default_massive_derivatives.env +45 -0
- gravitax/data/gpu_settings/settings/default_pk_derivatives.env +5 -0
- gravitax/data/gpu_settings/settings/default_pk_large.env +15 -0
- gravitax/data/gpu_settings/settings/default_pk_small.env +21 -0
- gravitax/data/highl5000_preset/README.md +57 -0
- gravitax/data/highl5000_preset/chi_E2.npy +0 -0
- gravitax/data/highl5000_preset/highl5000.env +19 -0
- gravitax/data/highl5000_preset/highl5000_nl.env +5 -0
- gravitax/data/highl5000_preset/kf_p4_k07.npy +0 -0
- gravitax/data/highl5000_preset/make_chi_reion.py +25 -0
- gravitax/data/highl5000_preset/make_kfine_l5000.py +52 -0
- gravitax/data/highl5000_preset/preset.json +30 -0
- gravitax/data/reference_preset/box_grids.json +72 -0
- gravitax/data/reference_preset/chi_box.npy +0 -0
- gravitax/data/reference_preset/k_fine_box.npy +0 -0
- gravitax/data/reference_preset/kernel_cost_profile.npz +0 -0
- gravitax/data/reference_preset/preset.env +20 -0
- gravitax/data/reference_preset/preset.json +64 -0
- gravitax/data/reference_preset/preset_mnu.json +21 -0
- gravitax/data/reference_preset/tols_panels149.npz +0 -0
- gravitax/gpu/__init__.py +8 -0
- gravitax/gpu/cosmo_pool.py +114 -0
- gravitax/gpu/e2e_kernel.py +1397 -0
- gravitax/gpu/highl.py +188 -0
- gravitax/gpu/mnu_e2e.py +2226 -0
- gravitax/gpu/mnu_two_launch.py +1078 -0
- gravitax/gpu/pk_d1_problem.py +277 -0
- gravitax/gpu/pk_lane.py +195 -0
- gravitax/gpu/pk_p5.py +534 -0
- gravitax/gpu/pk_p5_tan.py +184 -0
- gravitax/gpu/pk_route.py +672 -0
- gravitax/gpu/pk_speed.py +323 -0
- gravitax/gpu/pre_pipeline.py +48 -0
- gravitax/gpu/pre_tangents.py +698 -0
- gravitax/gpu/rec_tangents.py +66 -0
- gravitax/gpu/tangent_layout.py +205 -0
- gravitax/gpu_api.py +428 -0
- gravitax/gpu_api_grad.py +712 -0
- gravitax/gpu_api_pk.py +301 -0
- gravitax/grids.py +422 -0
- gravitax/grids_jax.py +98 -0
- gravitax/halofit.py +200 -0
- gravitax/hlo_check.py +159 -0
- gravitax/hmcode2016.py +747 -0
- gravitax/hmcode2020.py +389 -0
- gravitax/joint.py +531 -0
- gravitax/kernel_spec.py +1171 -0
- gravitax/kernel_spec_mg.py +246 -0
- gravitax/lensing.py +467 -0
- gravitax/lensing_corr.py +432 -0
- gravitax/lensing_jvp.py +29 -0
- gravitax/lensing_pallas.py +224 -0
- gravitax/los_batched.py +188 -0
- gravitax/los_gemm.py +853 -0
- gravitax/los_integration.py +615 -0
- gravitax/modified_gravity.py +433 -0
- gravitax/nonlinear_lensing.py +254 -0
- gravitax/parallel.py +126 -0
- gravitax/params.py +193 -0
- gravitax/perturbations.py +1580 -0
- gravitax/power_spectrum.py +1169 -0
- gravitax/presets.py +157 -0
- gravitax/primordial.py +13 -0
- gravitax/psi_route.py +57 -0
- gravitax/rec_kernel_spec.py +205 -0
- gravitax/rodas5p.py +225 -0
- gravitax/rsa.py +172 -0
- gravitax/rsa_mnu.py +125 -0
- gravitax/solvers.py +1458 -0
- gravitax/source_cache.py +148 -0
- gravitax/sparse_jac.py +273 -0
- gravitax/spline.py +514 -0
- gravitax/step_lane.py +156 -0
- gravitax/thermodynamics.py +2160 -0
- gravitax/theta_inversion.py +203 -0
- gravitax/utils.py +27 -0
- gravitax/weyl_highk.py +116 -0
- gravitax-1.0.0.dist-info/METADATA +242 -0
- gravitax-1.0.0.dist-info/RECORD +109 -0
- gravitax-1.0.0.dist-info/WHEEL +5 -0
- gravitax-1.0.0.dist-info/licenses/LICENSE +21 -0
- gravitax-1.0.0.dist-info/top_level.txt +1 -0
gravitax/__init__.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""gravitax - Differentiable Boltzmann solver in JAX.
|
|
2
|
+
|
|
3
|
+
Computes CMB angular power spectra (TT, EE, TE, BB), lensing potential,
|
|
4
|
+
matter power spectrum P(k), and sigma_8 for spatially flat cosmologies: LCDM, optionally
|
|
5
|
+
with massive neutrinos (degenerate hierarchy only) and w0/wa fluid dark energy. Non-zero omk is
|
|
6
|
+
rejected by compute_cls and compute_pk (NotImplementedError).
|
|
7
|
+
Full autodiff support via JAX.
|
|
8
|
+
|
|
9
|
+
High-level API
|
|
10
|
+
--------------------------------
|
|
11
|
+
set_params / get_results : parameter creation and pipeline execution.
|
|
12
|
+
BoltzmannParams : Parameter container.
|
|
13
|
+
BoltzmannResults : Results container.
|
|
14
|
+
|
|
15
|
+
Low-level API
|
|
16
|
+
-------------
|
|
17
|
+
compute_cls : Unlensed CMB power spectra from background + thermodynamics.
|
|
18
|
+
compute_lensed_cls : Apply lensing to unlensed spectra.
|
|
19
|
+
compute_pk : Linear matter power spectrum P(k, z).
|
|
20
|
+
compute_pk_batched : P(k, z) for a BATCH of cosmologies in one launch (jit(vmap)).
|
|
21
|
+
sigma8 : RMS matter fluctuation amplitude.
|
|
22
|
+
|
|
23
|
+
Submodules
|
|
24
|
+
----------
|
|
25
|
+
gravitax.background : Friedmann equation, H(a), eta(a), neutrino background.
|
|
26
|
+
gravitax.thermodynamics : RECFAST recombination, reionization, visibility.
|
|
27
|
+
gravitax.parallel : pmap CPU parallelism utilities.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
__version__ = "1.0.0"
|
|
31
|
+
|
|
32
|
+
import os as _os
|
|
33
|
+
|
|
34
|
+
# Set before the ODE-solver import so that compiled solvers are written to the persistent cache.
|
|
35
|
+
_os.environ.setdefault("EQX_ON_ERROR", "nan")
|
|
36
|
+
|
|
37
|
+
import jax
|
|
38
|
+
|
|
39
|
+
jax.config.update("jax_enable_x64", True)
|
|
40
|
+
|
|
41
|
+
# Persistent JIT compilation cache: avoids recompiling on subsequent runs.
|
|
42
|
+
#
|
|
43
|
+
# The default `jax_persistent_cache_min_compile_time_secs = 1.0` filters out sub-second
|
|
44
|
+
# primitive compiles; we lower it to 0 so every artifact is persisted. Combined with the
|
|
45
|
+
# setting above, this lets the cache hold the ODE solver compiles as well.
|
|
46
|
+
_cache_dir = _os.environ.get(
|
|
47
|
+
"BOLTZMANN_CACHE_DIR",
|
|
48
|
+
_os.path.join(_os.path.expanduser("~"), ".cache", "boltzmann_jax"),
|
|
49
|
+
)
|
|
50
|
+
try:
|
|
51
|
+
_os.makedirs(_cache_dir, exist_ok=True)
|
|
52
|
+
except OSError as _err:
|
|
53
|
+
import tempfile as _tempfile
|
|
54
|
+
import warnings as _warnings
|
|
55
|
+
|
|
56
|
+
_tmp_cache = _os.path.join(_tempfile.gettempdir(), f"boltzmann_jax_{_os.getuid()}")
|
|
57
|
+
_warnings.warn(f"gravitax: JAX cache dir {_cache_dir!r} unusable ({_err}); using {_tmp_cache!r}. "
|
|
58
|
+
"Set BOLTZMANN_CACHE_DIR to choose another.", RuntimeWarning, stacklevel=2)
|
|
59
|
+
_cache_dir = _tmp_cache
|
|
60
|
+
_os.makedirs(_cache_dir, exist_ok=True)
|
|
61
|
+
jax.config.update("jax_compilation_cache_dir", _cache_dir)
|
|
62
|
+
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0.0)
|
|
63
|
+
jax.config.update("jax_persistent_cache_min_entry_size_bytes", 0)
|
|
64
|
+
|
|
65
|
+
from gravitax.params import CosmologicalParams, default_params
|
|
66
|
+
from gravitax.assembly import compute_cls, compute_cls_batch
|
|
67
|
+
from gravitax.api import BoltzmannParams, BoltzmannResults, get_results, set_params
|
|
68
|
+
from gravitax.power_spectrum import compute_pk, compute_pk_batched, sigma8
|
|
69
|
+
from gravitax.joint import compute_cls_and_pk
|
|
70
|
+
from gravitax.lensing import compute_lensed_cls
|
|
71
|
+
from gravitax.los_integration import precompute_bessel_tables
|
|
72
|
+
from gravitax.gpu_api import GPUSpectra, gpu_spectra
|
|
73
|
+
from gravitax.gpu_api_grad import GPUDerivatives, gpu_derivatives
|
|
74
|
+
from gravitax.gpu_api_pk import GPUPkDerivatives, gpu_pk_derivatives
|
|
75
|
+
from gravitax._verbose import set_verbose
|
|
76
|
+
|
|
77
|
+
__all__ = [
|
|
78
|
+
# GPU route at the named settings (gravitax.gpu_api; needs a CUDA card and gradsolve)
|
|
79
|
+
"gpu_spectra",
|
|
80
|
+
"GPUSpectra",
|
|
81
|
+
"gpu_derivatives",
|
|
82
|
+
"GPUDerivatives",
|
|
83
|
+
"gpu_pk_derivatives",
|
|
84
|
+
"GPUPkDerivatives",
|
|
85
|
+
# Messages (quiet by default)
|
|
86
|
+
"set_verbose",
|
|
87
|
+
# High-level API
|
|
88
|
+
"set_params",
|
|
89
|
+
"get_results",
|
|
90
|
+
"BoltzmannParams",
|
|
91
|
+
"BoltzmannResults",
|
|
92
|
+
# Parameters
|
|
93
|
+
"CosmologicalParams",
|
|
94
|
+
"default_params",
|
|
95
|
+
# Low-level pipeline
|
|
96
|
+
"compute_cls",
|
|
97
|
+
"compute_cls_and_pk",
|
|
98
|
+
"compute_cls_batch",
|
|
99
|
+
"compute_pk",
|
|
100
|
+
"compute_pk_batched",
|
|
101
|
+
"sigma8",
|
|
102
|
+
"compute_lensed_cls",
|
|
103
|
+
# Caches / helpers
|
|
104
|
+
"precompute_bessel_tables",
|
|
105
|
+
]
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
# ----------------------------------------------------------------------------------------------
|
|
2
|
+
# Source : gradsolve (ECLIPSE-AI4Science/gradsolve),
|
|
3
|
+
# file gradsolve/solvers/rodas5p_step.py, for the jac= / solve_w= hooks and the public
|
|
4
|
+
# rodas5p_stages_and_error.
|
|
5
|
+
# Adapted from gradsolve; MIT licence.
|
|
6
|
+
# Tableau: Rodas5P (Steinebach, 2023). The coefficient values are those published by the author with
|
|
7
|
+
# the method in its reference implementation (Steinebach, 2023, p. 12).
|
|
8
|
+
#
|
|
9
|
+
# MIT License
|
|
10
|
+
#
|
|
11
|
+
# Copyright (c) 2026 Alessio Spurio Mancini
|
|
12
|
+
#
|
|
13
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
# in the Software without restriction, including without limitation the rights
|
|
16
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
# furnished to do so, subject to the following conditions:
|
|
19
|
+
#
|
|
20
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
# copies or substantial portions of the Software.
|
|
22
|
+
#
|
|
23
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
# SOFTWARE.
|
|
30
|
+
"""Single Rodas5P step + tableau — the method core of the stiff high-order solver.
|
|
31
|
+
|
|
32
|
+
Implements the Steinebach (2023) Rodas5P Rosenbrock-Wanner order-5(4) pair. The tableau is
|
|
33
|
+
stored in a/C form and converted at import to the alpha/Gamma form the driver uses.
|
|
34
|
+
The step uses an in-step Jacobian by automatic differentiation and dt==0 identity padding.
|
|
35
|
+
|
|
36
|
+
Optional hooks, all through :func:`rodas5p_stages_and_error`: ``jac=`` (a Jacobian
|
|
37
|
+
function ``J(t, y, p)`` or a matrix, replacing the in-step df/dy), ``solve_w=`` (the
|
|
38
|
+
linear solver of W = I - gamma*dt*J). The default solver factors W once per step
|
|
39
|
+
(jax.scipy.linalg.lu_factor) and reuses the factors in the 8 stage solves (lu_solve), wrapped
|
|
40
|
+
in lax.custom_linear_solve exactly as jnp.linalg.solve is: values and derivatives equal
|
|
41
|
+
those of a per-stage jnp.linalg.solve; W is factored once per step.
|
|
42
|
+
|
|
43
|
+
Reference: G. Steinebach, BIT Numer. Math. 63:27 (2023), doi:10.1007/s10543-023-00967-x.
|
|
44
|
+
Stored-form convention: a = alpha*Gamma^-1, C = diag(Gamma^-1) - Gamma^-1, b = m^T Gamma^-1,
|
|
45
|
+
btilde = (m-mhat)^T Gamma^-1.
|
|
46
|
+
"""
|
|
47
|
+
from __future__ import annotations
|
|
48
|
+
|
|
49
|
+
import jax
|
|
50
|
+
import jax.numpy as jnp
|
|
51
|
+
import numpy as np
|
|
52
|
+
|
|
53
|
+
from typing import NamedTuple, Optional
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class StepResult(NamedTuple):
|
|
57
|
+
"""One trial step's outputs."""
|
|
58
|
+
y_next: object # (dim,) propagated solution, equal to advance(...)
|
|
59
|
+
y_err: object # (dim,) embedded error estimate (already dt-scaled)
|
|
60
|
+
fsal: Optional[object] # trailing-stage derivative to reuse as next k1, or None
|
|
61
|
+
|
|
62
|
+
# ============================ TABLEAU (stored a/C form) ==============================
|
|
63
|
+
# Rodas5P, 8 stages (Steinebach 2023).
|
|
64
|
+
# The C matrix is stored 8x7 in the source (strictly lower, trailing all-zero column dropped);
|
|
65
|
+
# it is padded to 8x8. b = (last row of A)[:7] + [1]; btilde = e8 (stiffly-accurate structure).
|
|
66
|
+
_S = 8
|
|
67
|
+
RODAS5P_GAMMA_DIAG = 0.21193756319429014
|
|
68
|
+
_A_STORED = np.array([
|
|
69
|
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
70
|
+
[3.0, 0, 0, 0, 0, 0, 0, 0],
|
|
71
|
+
[2.849394379747939, 0.45842242204463923, 0, 0, 0, 0, 0, 0],
|
|
72
|
+
[-6.954028509809101, 2.489845061869568, -10.358996098473584, 0, 0, 0, 0, 0],
|
|
73
|
+
[2.8029986275628964, 0.5072464736228206, -0.3988312541770524, -0.04721187230404641, 0, 0, 0, 0],
|
|
74
|
+
[-7.502846399306121, 2.561846144803919, -11.627539656261098, -0.18268767659942256, 0.030198172008377946, 0, 0, 0],
|
|
75
|
+
[-7.502846399306121, 2.561846144803919, -11.627539656261098, -0.18268767659942256, 0.030198172008377946, 1, 0, 0],
|
|
76
|
+
[-7.502846399306121, 2.561846144803919, -11.627539656261098, -0.18268767659942256, 0.030198172008377946, 1, 1, 0],
|
|
77
|
+
], dtype=np.float64)
|
|
78
|
+
_C7 = np.array([ # stored 8x7 strictly-lower C (trailing zero column dropped)
|
|
79
|
+
[0, 0, 0, 0, 0, 0, 0],
|
|
80
|
+
[-14.155112264123755, 0, 0, 0, 0, 0, 0],
|
|
81
|
+
[-17.97296035885952, -2.859693295451294, 0, 0, 0, 0, 0],
|
|
82
|
+
[147.12150275711716, -1.41221402718213, 71.68940251302358, 0, 0, 0, 0],
|
|
83
|
+
[165.43517024871676, -0.4592823456491126, 42.90938336958603, -5.961986721573306, 0, 0, 0],
|
|
84
|
+
[24.854864614690072, -3.0009227002832186, 47.4931110020768, 5.5814197821558125, -0.6610691825249471, 0, 0],
|
|
85
|
+
[30.91273214028599, -3.1208243349937974, 77.79954646070892, 34.28646028294783, -19.097331116725623, -28.087943162872662, 0],
|
|
86
|
+
[37.80277123390563, -3.2571969029072276, 112.26918849496327, 66.9347231244047, -40.06618937091002, -54.66780262877968, -9.48861652309627],
|
|
87
|
+
], dtype=np.float64)
|
|
88
|
+
_C_STORED = np.zeros((_S, _S))
|
|
89
|
+
_C_STORED[:, :7] = _C7
|
|
90
|
+
_B_STORED = np.concatenate([_A_STORED[7, :7], [1.0]]) # b = last A row + 1
|
|
91
|
+
_BTILDE_STORED = np.eye(_S)[7] # btilde = e8
|
|
92
|
+
RODAS5P_C = np.array([0.0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716,
|
|
93
|
+
0.4288403609558664, 1.0, 1.0, 1.0], dtype=np.float64) # abscissae
|
|
94
|
+
RODAS5P_D = np.array([0.21193756319429014, -0.42387512638858027, -0.3384627126235924,
|
|
95
|
+
1.8046452872882734, 2.325825639765069, 0.0, 0.0, 0.0], dtype=np.float64) # df/dt
|
|
96
|
+
# ==================================================================================
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _convert_stored_to_alpha_gamma():
|
|
100
|
+
"""a/C (stored) -> alpha/Gamma/m/mhat (driver). Gamma^-1 = (1/gamma)I - C ; Gamma = inv ;
|
|
101
|
+
alpha = a@Gamma ; m = b@Gamma ; mhat = m - btilde@Gamma. Run once at import."""
|
|
102
|
+
g = RODAS5P_GAMMA_DIAG
|
|
103
|
+
ginv = np.eye(_S) / g - _C_STORED
|
|
104
|
+
Gam = np.linalg.inv(ginv) # lower triangular, diagonal gamma
|
|
105
|
+
Alpha = _A_STORED @ Gam # strictly lower
|
|
106
|
+
M = _B_STORED @ Gam
|
|
107
|
+
Mhat = M - _BTILDE_STORED @ Gam
|
|
108
|
+
return Alpha, Gam, M, Mhat
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
RODAS5P_ALPHA, RODAS5P_GAMMA, RODAS5P_M, RODAS5P_MHAT = _convert_stored_to_alpha_gamma()
|
|
112
|
+
_N_STAGES = _S
|
|
113
|
+
_GAMMA_ROWSUM = RODAS5P_GAMMA.sum(axis=1) # gamma_i (incl. the diagonal gamma) == RODAS5P_D
|
|
114
|
+
|
|
115
|
+
# ==================== DENSE-OUTPUT TABLEAU (stored H form) ============================
|
|
116
|
+
# Rodas5P's continuous-extension weights H form a 3x8 array in the same stored convention
|
|
117
|
+
# as A/C. Three dense vectors D_r = sum_i H[r,i] * u_i are formed from the a/C-form stage
|
|
118
|
+
# vectors u_i and interpolated as
|
|
119
|
+
# y(Theta) = (1-Theta)*y0 + Theta*(y1 + (1-Theta)*(D1 + Theta*(D2 + Theta*D3)))
|
|
120
|
+
# This driver produces alpha/Gamma increments k_i with u = Gamma @ k,
|
|
121
|
+
# so the conversion for these increments is hd = H @ Gamma.
|
|
122
|
+
_H_STORED = np.array([
|
|
123
|
+
[25.948786856663858, -2.5579724845846235, 10.433815404888879, -2.3679251022685204,
|
|
124
|
+
0.524948541321073, 1.1241088310450404, 0.4272876194431874, -0.17202221070155493],
|
|
125
|
+
[-9.91568850695171, -0.9689944594115154, 3.0438037242978453, -24.495224566215796,
|
|
126
|
+
20.176138334709044, 15.98066361424651, -6.789040303419874, -6.710236069923372],
|
|
127
|
+
[11.419903575922262, 2.8879645146136994, 72.92137995996029, 80.12511834622643,
|
|
128
|
+
-52.072871366152654, -59.78993625266729, -0.15582684282751913, 4.883087185713722],
|
|
129
|
+
], dtype=np.float64)
|
|
130
|
+
# ======================================================================================
|
|
131
|
+
|
|
132
|
+
#: Dense weights in this driver's k_i convention, (3, 8). Row sums are zero by the extension's own
|
|
133
|
+
#: consistency condition.
|
|
134
|
+
RODAS5P_HD = _H_STORED @ RODAS5P_GAMMA
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def rodas5p_dense_weights(theta):
|
|
138
|
+
"""b_i(theta) for y(t0 + theta*dt) = y0 + sum_i b_i(theta) * k_i.
|
|
139
|
+
|
|
140
|
+
Quartic in theta. The theta*(1-theta) factorization is required, not cosmetic: it makes
|
|
141
|
+
``b_i(1) == m_i`` hold bitwise. Order 4 continuous extension on the order-5 step.
|
|
142
|
+
|
|
143
|
+
``th == 1.0`` returns ``y_r`` exactly.
|
|
144
|
+
"""
|
|
145
|
+
th = jnp.asarray(theta)
|
|
146
|
+
inner = RODAS5P_HD[0] + th * (RODAS5P_HD[1] + th * RODAS5P_HD[2])
|
|
147
|
+
return th * RODAS5P_M + th * (1.0 - th) * inner
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _default_solve_w(W):
|
|
151
|
+
"""The default solver of W x = b: ONE LU factorization of W per step
|
|
152
|
+
(``jax.scipy.linalg.lu_factor``), reused by every stage solve (``lu_solve``), each wrapped
|
|
153
|
+
in ``lax.custom_linear_solve`` exactly as ``jnp.linalg.solve`` wraps its own factorization:
|
|
154
|
+
the factors are taken of ``stop_gradient(W)``, the matvec is the same HIGHEST-precision
|
|
155
|
+
dot_general. So values AND derivatives
|
|
156
|
+
equal those of a per-stage ``jnp.linalg.solve(W, b)``. Returns ``solve(b) -> x``."""
|
|
157
|
+
lu_piv = jax.scipy.linalg.lu_factor(jax.lax.stop_gradient(W))
|
|
158
|
+
|
|
159
|
+
def matvec(x):
|
|
160
|
+
return jax.lax.dot_general(W, x, (((W.ndim - 1,), (x.ndim - 1,)), ((), ())),
|
|
161
|
+
precision=jax.lax.Precision.HIGHEST)
|
|
162
|
+
|
|
163
|
+
def solve(b):
|
|
164
|
+
return jax.lax.custom_linear_solve(
|
|
165
|
+
matvec, b,
|
|
166
|
+
solve=lambda _, x: jax.scipy.linalg.lu_solve(lu_piv, x, trans=0),
|
|
167
|
+
transpose_solve=lambda _, x: jax.scipy.linalg.lu_solve(lu_piv, x, trans=1))
|
|
168
|
+
return solve
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _jacobian(f, t, y, p, jac):
|
|
172
|
+
"""df/dy: in-step automatic differentiation (default), ``jac(t, y, p)`` (a callable), or ``jac`` (a matrix)."""
|
|
173
|
+
D = y.shape[-1]
|
|
174
|
+
if jac is None:
|
|
175
|
+
return jax.jacfwd(f, argnums=1)(t, y, p)
|
|
176
|
+
J = jac(t, y, p) if callable(jac) else jac
|
|
177
|
+
J = jnp.asarray(J, dtype=y.dtype)
|
|
178
|
+
if J.shape != (D, D):
|
|
179
|
+
raise ValueError(f"jac must give a ({D}, {D}) Jacobian df/dy, got shape {J.shape}")
|
|
180
|
+
return J
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _rodas5p_stages(f, t, y, dt, p, jac=None, solve_w=None):
|
|
184
|
+
"""Evaluate the s ROW stages; df/dy (AD by default, or ``jac``) + AD df/dt; W solved by
|
|
185
|
+
``solve_w(W)`` (default :func:`_default_solve_w`). Returns list k[0..s-1]."""
|
|
186
|
+
D = y.shape[-1]
|
|
187
|
+
eye = jnp.eye(D, dtype=y.dtype)
|
|
188
|
+
g = jnp.asarray(RODAS5P_GAMMA_DIAG, dtype=y.dtype)
|
|
189
|
+
J = _jacobian(f, t, y, p, jac)
|
|
190
|
+
f_t = jax.jacfwd(f, argnums=0)(t, y, p)
|
|
191
|
+
# The padded rows are masked so that dt == 0 is an identity in value and gradient.
|
|
192
|
+
nz = dt != 0
|
|
193
|
+
J = jnp.where(nz, J, jnp.zeros_like(J))
|
|
194
|
+
f_t = jnp.where(nz, f_t, jnp.zeros_like(f_t))
|
|
195
|
+
W = eye - dt * g * J
|
|
196
|
+
solve = (_default_solve_w if solve_w is None else solve_w)(W)
|
|
197
|
+
k = [None] * _N_STAGES
|
|
198
|
+
for i in range(_N_STAGES):
|
|
199
|
+
y_arg = y
|
|
200
|
+
accum = jnp.zeros_like(y)
|
|
201
|
+
for j in range(i):
|
|
202
|
+
y_arg = y_arg + float(RODAS5P_ALPHA[i, j]) * k[j]
|
|
203
|
+
accum = accum + float(RODAS5P_GAMMA[i, j]) * k[j]
|
|
204
|
+
t_arg = t + float(RODAS5P_C[i]) * dt
|
|
205
|
+
rhs = dt * f(t_arg, y_arg, p) + dt * (J @ accum) + (dt * dt * float(_GAMMA_ROWSUM[i])) * f_t
|
|
206
|
+
k[i] = solve(rhs)
|
|
207
|
+
return k
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def rodas5p_advance(f, t, y, dt, p):
|
|
211
|
+
"""One Rodas5P step (value-only). dt==0 is an exact identity in value and gradient."""
|
|
212
|
+
k = _rodas5p_stages(f, t, y, dt, p)
|
|
213
|
+
out = y
|
|
214
|
+
for i in range(_N_STAGES):
|
|
215
|
+
out = out + float(RODAS5P_M[i]) * k[i]
|
|
216
|
+
return out
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def rodas5p_trial_step(f, t, y, dt, p, k1=None):
|
|
220
|
+
"""One Rodas5P trial step -> StepResult(y_next, sum((m_i-mhat_i)k_i), fsal=None). y_next
|
|
221
|
+
equal to rodas5p_advance. k1 ignored (Rosenbrock is not FSAL)."""
|
|
222
|
+
del k1
|
|
223
|
+
k = _rodas5p_stages(f, t, y, dt, p)
|
|
224
|
+
y_next = y
|
|
225
|
+
y_err = jnp.zeros_like(y)
|
|
226
|
+
for i in range(_N_STAGES):
|
|
227
|
+
y_next = y_next + float(RODAS5P_M[i]) * k[i]
|
|
228
|
+
y_err = y_err + float(RODAS5P_M[i] - RODAS5P_MHAT[i]) * k[i]
|
|
229
|
+
return StepResult(y_next=y_next, y_err=y_err, fsal=None)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def rodas5p_dense_eval(coeffs, theta):
|
|
233
|
+
"""Evaluate the continuous extension from the 5-vector bundle produced by
|
|
234
|
+
:func:`rodas5p_stages_and_dense`, in Horner form:
|
|
235
|
+
|
|
236
|
+
y(t0 + theta*dt) = (1-th)*y_l + th*(y_r + (1-th)*(D1 + th*(D2 + th*D3)))
|
|
237
|
+
|
|
238
|
+
Algebraically identical to ``y_l + sum_i b_i(theta) k_i`` with the
|
|
239
|
+
:func:`rodas5p_dense_weights` weights -- the two agree -- but it needs about 5
|
|
240
|
+
vector operations per requested time.
|
|
241
|
+
|
|
242
|
+
``theta`` broadcasts against the (dim,) bundle vectors, so a (k, 1) column evaluates all
|
|
243
|
+
k requested times of a step at once.
|
|
244
|
+
|
|
245
|
+
Endpoints are exact, not merely accurate: th == 1.0 makes (1-th) exactly 0.0 and returns
|
|
246
|
+
``y_r`` -- the step output itself; th == 0.0 likewise returns ``y_l``. """
|
|
247
|
+
y_l, y_r, d1, d2, d3 = coeffs
|
|
248
|
+
th = jnp.asarray(theta)
|
|
249
|
+
om = 1.0 - th
|
|
250
|
+
return om * y_l + th * (y_r + om * (d1 + th * (d2 + th * d3)))
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def rodas5p_stages_and_dense(f, t, y, dt, p):
|
|
254
|
+
"""One Rodas5P step plus its continuous-extension coefficients.
|
|
255
|
+
|
|
256
|
+
Returns ``(y_next, coeffs)`` with ``coeffs = (y_left, y_right, D1, D2, D3)`` -- the
|
|
257
|
+
5-vector Horner bundle, where ``D_r = sum_i hd[r, i] * k_i`` are the dense vectors in
|
|
258
|
+
this driver's alpha/Gamma increment convention. Feed the bundle to :func:`rodas5p_dense_eval`.
|
|
259
|
+
|
|
260
|
+
``y_next`` equals :func:`rodas5p_advance` (same weights, same accumulation
|
|
261
|
+
order).
|
|
262
|
+
"""
|
|
263
|
+
k = _rodas5p_stages(f, t, y, dt, p)
|
|
264
|
+
y_next = y
|
|
265
|
+
for i in range(_N_STAGES):
|
|
266
|
+
y_next = y_next + float(RODAS5P_M[i]) * k[i]
|
|
267
|
+
dense = []
|
|
268
|
+
for r in range(3):
|
|
269
|
+
acc = jnp.zeros_like(y)
|
|
270
|
+
for i in range(_N_STAGES):
|
|
271
|
+
acc = acc + float(RODAS5P_HD[r, i]) * k[i]
|
|
272
|
+
dense.append(acc)
|
|
273
|
+
return y_next, (y, y_next, dense[0], dense[1], dense[2])
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def rodas5p_stages_and_error(f, t, y, dt, p, *, jac=None, solve_w=None):
|
|
277
|
+
"""One Rodas5P step from ONE stage evaluation: ``(y_next, y_err, coeffs)``.
|
|
278
|
+
|
|
279
|
+
``y_next`` and ``y_err`` are those of :func:`rodas5p_trial_step` and ``coeffs`` the
|
|
280
|
+
5-vector dense bundle ``(y_left, y_right, D1, D2, D3)`` of :func:`rodas5p_stages_and_dense`
|
|
281
|
+
(feed it to :func:`rodas5p_dense_eval`), all with the same weights in the same accumulation
|
|
282
|
+
order, so with the default hooks each output is equal to the corresponding function's.
|
|
283
|
+
|
|
284
|
+
Hooks (keyword only; None = the default behaviour):
|
|
285
|
+
|
|
286
|
+
``jac``: df/dy, either a callable ``jac(t, y, p) -> (D, D)`` or a ``(D, D)`` matrix, used in
|
|
287
|
+
place of the in-step automatic-differentiation Jacobian. The time derivative df/dt stays
|
|
288
|
+
an automatic derivative in t (one pass of f in the scalar t). The dt == 0
|
|
289
|
+
masking applies to it as to the AD Jacobian. A supplied Jacobian equal to the automatic one gives a
|
|
290
|
+
bitwise-equal step. Derivatives of the step with respect to y or p then flow through ``jac``
|
|
291
|
+
(it must be differentiable for the step to be differentiable; a constant matrix contributes
|
|
292
|
+
no derivative of J).
|
|
293
|
+
|
|
294
|
+
``solve_w``: ``solve_w(W) -> solve``, where ``W = I - gamma*dt*J`` (dense, ``(D, D)``) and
|
|
295
|
+
``solve(b)`` returns ``x`` with ``W x = b``. It is called once per step and its ``solve`` 8
|
|
296
|
+
times (one per stage), so a factorization done in ``solve_w`` is shared by the 8 stages.
|
|
297
|
+
Default: :func:`_default_solve_w` (one ``jax.scipy.linalg.lu_factor``, 8 ``lu_solve``).
|
|
298
|
+
"""
|
|
299
|
+
k = _rodas5p_stages(f, t, y, dt, p, jac=jac, solve_w=solve_w)
|
|
300
|
+
y_next = y
|
|
301
|
+
y_err = jnp.zeros_like(y)
|
|
302
|
+
for i in range(_N_STAGES):
|
|
303
|
+
y_next = y_next + float(RODAS5P_M[i]) * k[i]
|
|
304
|
+
y_err = y_err + float(RODAS5P_M[i] - RODAS5P_MHAT[i]) * k[i]
|
|
305
|
+
dense = []
|
|
306
|
+
for r in range(3):
|
|
307
|
+
acc = jnp.zeros_like(y)
|
|
308
|
+
for i in range(_N_STAGES):
|
|
309
|
+
acc = acc + float(RODAS5P_HD[r, i]) * k[i]
|
|
310
|
+
dense.append(acc)
|
|
311
|
+
return y_next, y_err, (y, y_next, dense[0], dense[1], dense[2])
|
|
312
|
+
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""Momentum quadrature for massive neutrinos.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
``neutrino_momentum_bins(nqmax)`` returns nodes q_i (comoving momentum in units of k_B T_nu0 / c) and weights
|
|
5
|
+
w_i such that, for smooth g,
|
|
6
|
+
|
|
7
|
+
sum_i w_i g(q_i) ~ (1/F) int_0^inf q^3 f0(q) g(q) dq, f0(q) = 1/(e^q + 1), F = 7 pi^4 / 120.
|
|
8
|
+
|
|
9
|
+
F is the Fermi-Dirac integral int_0^inf q^3/(e^q + 1) dq = (1 - 2^-3) Gamma(4) zeta(4) = (7/8)(6)(pi^4/90).
|
|
10
|
+
|
|
11
|
+
The nodes and raw weights r_i are published values. The r_i weight the
|
|
12
|
+
kernel K(q) = (1/4)(-q^3 f0 dlnf0/dlnq), with dlnf0/dlnq = q d ln f0/dq = -q/(1 + e^-q). Integrating by
|
|
13
|
+
parts, int_0^inf K dq = -(1/4) int q^4 f0'(q) dq = int q^3 f0 dq = F, which is why sum_i r_i ~ F. Since
|
|
14
|
+
q^3 f0 g = K * g / (-(1/4) dlnf0/dlnq), the weights for q^3 f0 / F are
|
|
15
|
+
|
|
16
|
+
w_i = r_i / (-(1/4) dlnf0/dlnq (q_i)) / F.
|
|
17
|
+
|
|
18
|
+
Gauss-Laguerre rule for the massive-neutrino momentum integral, nqmax = 15 or 25. Gauss-Laguerre quadrature for
|
|
19
|
+
int_0^inf e^-q h(q) dq: the nodes x_i are the eigenvalues of the symmetric tridiagonal Jacobi matrix of the Laguerre
|
|
20
|
+
polynomials (diagonal 2i + 1, off-diagonal i, i = 1..n-1) and the weights W_i the squared first components of its
|
|
21
|
+
normalised eigenvectors (Golub and Welsch 1969). Since q^3 f0 = e^-q q^3 / (1 + e^-q), the weights for q^3 f0 / F are
|
|
22
|
+
W_i x_i^3 / (1 + e^-x_i) / F, rescaled so that sum_i w_i (-(1/4) dlnf0/dlnq)(x_i) = 1 exactly, the massless limit
|
|
23
|
+
of the moment sums.
|
|
24
|
+
|
|
25
|
+
Origin of the values. The nodes and weights are fixed double-precision literals: the published 3/4/5-node nodes
|
|
26
|
+
and raw weights converted with a correctly rounded exponential; the 15- and 25-node Gauss-Laguerre rules computed
|
|
27
|
+
once (Golub-Welsch, numpy) and stored, so the generated kernels are identical on every platform. dlnf0/dlnq at the
|
|
28
|
+
nodes is stored the same way (correctly rounded exponential). The functions that compute these values,
|
|
29
|
+
_reference_weights_computed and _laguerre_rule_computed, are kept to regenerate and check them;
|
|
30
|
+
nothing evaluates them at run time.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
import math
|
|
34
|
+
import operator
|
|
35
|
+
|
|
36
|
+
import jax
|
|
37
|
+
import jax.numpy as jnp
|
|
38
|
+
import numpy as np
|
|
39
|
+
|
|
40
|
+
__all__ = ["neutrino_momentum_bins", "momentum_bins_py"]
|
|
41
|
+
|
|
42
|
+
_F = 7.0 * math.pi**4 / 120.0 # int_0^inf q^3 / (e^q + 1) dq = 5.682196976983475
|
|
43
|
+
|
|
44
|
+
# The published nodes q_i and raw kernel weights r_i.
|
|
45
|
+
_NODES = {
|
|
46
|
+
3: (0.913201, 3.37517, 7.79184),
|
|
47
|
+
4: (0.7, 2.62814, 5.90428, 12.0),
|
|
48
|
+
5: (0.583165, 2.0, 4.0, 7.26582, 13.0),
|
|
49
|
+
}
|
|
50
|
+
_RAW_WEIGHTS = {
|
|
51
|
+
3: (0.0687359, 3.31435, 2.29911),
|
|
52
|
+
4: (0.0200251, 1.84539, 3.52736, 0.289427),
|
|
53
|
+
5: (0.0081201, 0.689407, 2.8063, 2.05156, 0.12681),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# Node counts of the converged-reference rule (Gauss-Laguerre, see the module docstring).
|
|
58
|
+
_LAGUERRE_COUNTS = (15, 25)
|
|
59
|
+
|
|
60
|
+
# ---- Stored literals (module docstring, "Origin of the values") -------------------------------------------------------
|
|
61
|
+
# Weights w_i = r_i / (-(1/4) dlnf0/dlnq(q_i)) / F of the 3/4/5-node rules, dlnf0/dlnq = -q / (1 + e^-q) at every rule's
|
|
62
|
+
# nodes, and the 15- and 25-node Gauss-Laguerre nodes and normalised weights.
|
|
63
|
+
_WEIGHTS = {
|
|
64
|
+
3: (0.07424595492265794, 0.7149178198492006, 0.20779868625923412),
|
|
65
|
+
4: (
|
|
66
|
+
0.030138514067557253, 0.5299858824467688, 0.42170580487915044,
|
|
67
|
+
0.01697868972590004,
|
|
68
|
+
),
|
|
69
|
+
5: (
|
|
70
|
+
0.015272743889458974, 0.2754948815680875, 0.5029215264780437,
|
|
71
|
+
0.19890549110502412, 0.006866806957077977,
|
|
72
|
+
),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_DLNF0_DLNQ = {
|
|
76
|
+
3: (-0.6517102217690142, -3.2635175527957307, -7.788622581582067),
|
|
77
|
+
4: (
|
|
78
|
+
-0.46773144051771626, -2.4511369016216507, -5.88821844129163,
|
|
79
|
+
-11.999926269904773,
|
|
80
|
+
),
|
|
81
|
+
5: (
|
|
82
|
+
-0.3742725836357634, -1.7615941559557646, -3.928055160151634,
|
|
83
|
+
-7.260744537489071, -12.999970615784127,
|
|
84
|
+
),
|
|
85
|
+
15: (
|
|
86
|
+
-0.04882891515126998, -0.30583363457228324, -0.9375735403744465,
|
|
87
|
+
-2.0573857866815644, -3.5762945074975963, -5.401550448738175,
|
|
88
|
+
-7.562000602089377, -10.119821175156936, -13.130256428801362,
|
|
89
|
+
-16.654406734211424, -20.776478879748964, -25.623894226538095,
|
|
90
|
+
-31.40751916975322, -38.53068330648599, -48.026085572685794,
|
|
91
|
+
),
|
|
92
|
+
25: (
|
|
93
|
+
-0.02915603028953906, -0.1716922687608565, -0.49754899634757216,
|
|
94
|
+
-1.0915786947873136, -1.982004698097702, -3.113221548380534,
|
|
95
|
+
-4.426157097856318, -5.9133488210285705, -7.5960974493094415,
|
|
96
|
+
-9.496036110853378, -11.628911368466312, -14.007946420862062,
|
|
97
|
+
-16.647124616479786, -19.562897949041762, -22.775241983908867,
|
|
98
|
+
-26.308772390870182, -30.194291163313775, -34.471097571921995,
|
|
99
|
+
-39.190608803937415, -44.42234933616203, -50.264574993833556,
|
|
100
|
+
-56.864967173940144, -64.46667061595416, -73.53423479210014,
|
|
101
|
+
-85.26015556249598,
|
|
102
|
+
),
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_LAGUERRE_NODES = {
|
|
106
|
+
15: (
|
|
107
|
+
0.09330781201728329, 0.49269174030188795, 1.2155954120709518,
|
|
108
|
+
2.269949526203742, 3.66762272175144, 5.425336627413554,
|
|
109
|
+
7.565916226613064, 10.120228568019114, 13.130282482175726,
|
|
110
|
+
16.654407708329945, 20.776478899448765, 25.623894226728783,
|
|
111
|
+
31.407519169753936, 38.53068330648599, 48.026085572685794,
|
|
112
|
+
),
|
|
113
|
+
25: (
|
|
114
|
+
0.0567047754527055, 0.29901089858699026, 0.7359095554350176,
|
|
115
|
+
1.3691831160351913, 2.201326053721466, 3.235675803558032,
|
|
116
|
+
4.476496615073828, 5.929083762700441, 7.5998993099567445,
|
|
117
|
+
9.496749220932434, 11.629014911778754, 14.007957976545073,
|
|
118
|
+
16.64712559728879, 19.562898011469063, 22.77524198683505,
|
|
119
|
+
26.30877239096889, 30.1942911633161, 34.47109757192203,
|
|
120
|
+
39.190608803937415, 44.42234933616203, 50.264574993833556,
|
|
121
|
+
56.864967173940144, 64.46667061595416, 73.53423479210014,
|
|
122
|
+
85.26015556249598,
|
|
123
|
+
),
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_LAGUERRE_WEIGHTS = {
|
|
127
|
+
15: (
|
|
128
|
+
1.6327548136477205e-05, 0.004471080005157562, 0.06413106360821881,
|
|
129
|
+
0.23586723647246358, 0.3403976127507694, 0.23962161371970192,
|
|
130
|
+
0.09236391859058124, 0.020369988727901377, 0.0025735502408267903,
|
|
131
|
+
0.00018099152756987876, 6.672306929026951e-06, 1.1612221783820848e-07,
|
|
132
|
+
7.941447163077167e-10, 1.492976305988992e-12, 3.1203025885078277e-16,
|
|
133
|
+
),
|
|
134
|
+
25: (
|
|
135
|
+
2.269009079073892e-06, 0.0006798252144186268, 0.012148059154527203,
|
|
136
|
+
0.06706217584730742, 0.17443603221665446, 0.2564890519127027,
|
|
137
|
+
0.23890596969077207, 0.1519123552799414, 0.06888156520146735,
|
|
138
|
+
0.02278250411319731, 0.005553404453059345, 0.0010002556202865522,
|
|
139
|
+
0.00013271706640249236, 1.2867692535675845e-05, 8.997709112342895e-07,
|
|
140
|
+
4.453234572392031e-08, 1.5206836057252579e-09, 3.46199795105314e-11,
|
|
141
|
+
5.016847803938094e-13, 4.341542658227206e-15, 2.0483306664711097e-17,
|
|
142
|
+
4.591791157060563e-20, 3.901096436651588e-23, 8.179429442481025e-27,
|
|
143
|
+
1.435232814894645e-31,
|
|
144
|
+
),
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _reference_weights_computed(n, digits=50):
|
|
149
|
+
"""(weights, dlnf0/dlnq) of the n-node published rule, from _NODES and _RAW_WEIGHTS with the exponential evaluated
|
|
150
|
+
in extended precision (`digits` digits) and rounded to double; every other operation in double precision, in the order
|
|
151
|
+
-q / (1 + e^-q), then r / (-0.25 dlnf0/dlnq) / F. Regenerates _WEIGHTS[n] and _DLNF0_DLNQ[n] (needs an extended-precision library)."""
|
|
152
|
+
import mpmath as mp
|
|
153
|
+
with mp.workdps(digits):
|
|
154
|
+
e = [float(mp.exp(-mp.mpf(q))) for q in _NODES[n]]
|
|
155
|
+
dlf = tuple(-q / (1.0 + ei) for q, ei in zip(_NODES[n], e))
|
|
156
|
+
w = tuple(r / (-0.25 * d) / _F for r, d in zip(_RAW_WEIGHTS[n], dlf))
|
|
157
|
+
return w, dlf
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _laguerre_rule_computed(n):
|
|
161
|
+
"""(nodes, weights) of the n-node converged-reference rule computed by Golub-Welsch with numpy, as tuples of Python
|
|
162
|
+
floats. Values are stored as fixed literals in _LAGUERRE_NODES / _LAGUERRE_WEIGHTS."""
|
|
163
|
+
i = np.arange(n, dtype=np.float64)
|
|
164
|
+
jacobi = np.diag(2.0 * i + 1.0) + np.diag(i[1:], 1) + np.diag(i[1:], -1)
|
|
165
|
+
x, vec = np.linalg.eigh(jacobi)
|
|
166
|
+
w = vec[0] ** 2 * x**3 / (1.0 + np.exp(-x)) / _F
|
|
167
|
+
w = w / np.sum(w * 0.25 * x / (1.0 + np.exp(-x)))
|
|
168
|
+
return tuple(x.tolist()), tuple(w.tolist())
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _laguerre_rule(n):
|
|
172
|
+
"""(nodes, weights) of the n-node converged-reference rule: the stored literals."""
|
|
173
|
+
return _LAGUERRE_NODES[n], _LAGUERRE_WEIGHTS[n]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _check_nqmax(nqmax):
|
|
177
|
+
allowed = ", ".join(str(k) for k in sorted(_NODES) + list(_LAGUERRE_COUNTS))
|
|
178
|
+
try:
|
|
179
|
+
if isinstance(nqmax, bool):
|
|
180
|
+
raise TypeError
|
|
181
|
+
key = operator.index(nqmax)
|
|
182
|
+
except TypeError:
|
|
183
|
+
raise ValueError(f"nqmax must be one of {allowed} (a Python int, static under jax.jit); got {nqmax!r}") from None
|
|
184
|
+
if key not in _NODES and key not in _LAGUERRE_COUNTS:
|
|
185
|
+
raise ValueError(f"nqmax must be one of {allowed}; got {nqmax!r}")
|
|
186
|
+
return key
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def momentum_bins_py(nqmax=3):
|
|
190
|
+
"""(q, w, dlnf0/dlnq) of the momentum quadrature as tuples of Python floats (the stored literals), so that a traced
|
|
191
|
+
graph holds them as constants. Same nqmax as neutrino_momentum_bins."""
|
|
192
|
+
key = _check_nqmax(nqmax)
|
|
193
|
+
if key in _LAGUERRE_COUNTS:
|
|
194
|
+
return _LAGUERRE_NODES[key], _LAGUERRE_WEIGHTS[key], _DLNF0_DLNQ[key]
|
|
195
|
+
return tuple(float(x) for x in _NODES[key]), _WEIGHTS[key], _DLNF0_DLNQ[key]
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def neutrino_momentum_bins(nqmax=3):
|
|
199
|
+
"""Nodes and weights of the massive-neutrino momentum quadrature.
|
|
200
|
+
|
|
201
|
+
nqmax: number of nodes, a Python int in {3, 4, 5} (the published nodes) or {15, 25} (the converged-reference
|
|
202
|
+
Gauss-Laguerre rule), static under ``jax.jit``.
|
|
203
|
+
Returns ``(q, w)``, float64 arrays of length nqmax: the stored literals (module docstring), identical eagerly,
|
|
204
|
+
under jit and on every platform.
|
|
205
|
+
"""
|
|
206
|
+
q, w, _ = momentum_bins_py(nqmax)
|
|
207
|
+
with jax.ensure_compile_time_eval():
|
|
208
|
+
return jnp.asarray(q, dtype=jnp.float64), jnp.asarray(w, dtype=jnp.float64)
|