eft4py 0.0.4__tar.gz → 0.0.6__tar.gz
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.
- {eft4py-0.0.4 → eft4py-0.0.6}/PKG-INFO +7 -2
- {eft4py-0.0.4 → eft4py-0.0.6}/pyproject.toml +8 -2
- {eft4py-0.0.4 → eft4py-0.0.6}/pyproject.toml.orig +8 -2
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/__init__.py +3 -3
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/backend/__init__.py +20 -3
- eft4py-0.0.6/src/eft4py/config/__init__.py +21 -0
- eft4py-0.0.6/src/eft4py/config/factory.py +304 -0
- eft4py-0.0.6/src/eft4py/config/spec.py +314 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/core/__init__.py +6 -6
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/core/types.py +40 -9
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/models/horndeski.py +15 -22
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/__init__.py +8 -3
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/alpha_functions.py +45 -39
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/background.py +2 -2
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/compiled.py +166 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/dark_energy.py +10 -12
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/initial_conditions.py +29 -11
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/stability.py +77 -29
- eft4py-0.0.6/src/eft4py/solvers/background.py +1259 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/ode_systems/base.py +10 -10
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/ode_systems/horndeski.py +174 -50
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/strategies/base.py +6 -4
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/strategies/jax.py +33 -24
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/strategies/scipy.py +39 -20
- eft4py-0.0.6/src/eft4py/utils/expressions.py +152 -0
- eft4py-0.0.4/src/eft4py/solvers/background.py +0 -701
- {eft4py-0.0.4 → eft4py-0.0.6}/README.md +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/core/protocols.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/models/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/cosmology.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/derivatives.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/errors.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/evolution_coeffs.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/neutrinos.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/quasi_static.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/sound_horizon.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/physics/symbolic.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/py.typed +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/emulator.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/growth.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/limber.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/observables.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/potentials.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/linear/power.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/ode_systems/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/solution.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/solvers/strategies/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/__init__.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/_classy.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/hermite.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/jax_config.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/nowiggle.py +0 -0
- {eft4py-0.0.4 → eft4py-0.0.6}/src/eft4py/utils/symbols.py +0 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: eft4py
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: An Effective Field Theory toolbox for Python
|
|
5
5
|
Author: Rodrigo Calderon
|
|
6
6
|
Author-email: Rodrigo Calderon <calderon.cosmology@gmail.com>
|
|
7
7
|
Requires-Dist: sympy>=1.12
|
|
8
8
|
Requires-Dist: numpy>=1.24
|
|
9
9
|
Requires-Dist: scipy>=1.11
|
|
10
|
+
Requires-Dist: pyyaml>=6
|
|
10
11
|
Requires-Dist: pytest>=7.4 ; extra == 'dev'
|
|
11
12
|
Requires-Dist: pytest-cov>=4.1 ; extra == 'dev'
|
|
12
13
|
Requires-Dist: mypy>=1.10 ; extra == 'dev'
|
|
@@ -15,15 +16,19 @@ Requires-Dist: mkdocs-material>=9.5 ; extra == 'docs'
|
|
|
15
16
|
Requires-Dist: mkdocstrings[python]>=0.26 ; extra == 'docs'
|
|
16
17
|
Requires-Dist: mkdocs-marimo>=0.2.1 ; extra == 'docs'
|
|
17
18
|
Requires-Dist: mkdocs-jupyter>=0.25 ; extra == 'docs'
|
|
19
|
+
Requires-Dist: eft4py[jax] ; extra == 'examples'
|
|
20
|
+
Requires-Dist: marimo>=0.25.0 ; extra == 'examples'
|
|
21
|
+
Requires-Dist: matplotlib>=3.7 ; extra == 'examples'
|
|
18
22
|
Requires-Dist: jax>=0.4.0 ; extra == 'jax'
|
|
19
23
|
Requires-Dist: jaxlib>=0.4.0 ; extra == 'jax'
|
|
20
24
|
Requires-Dist: diffrax>=0.5.0 ; extra == 'jax'
|
|
21
25
|
Requires-Dist: optimistix>=0.0.10 ; extra == 'jax'
|
|
22
26
|
Requires-Dist: matplotlib>=3.7 ; extra == 'notebooks'
|
|
23
27
|
Requires-Dist: jupyter>=1.0 ; extra == 'notebooks'
|
|
24
|
-
Requires-Python: >=3.
|
|
28
|
+
Requires-Python: >=3.11
|
|
25
29
|
Provides-Extra: dev
|
|
26
30
|
Provides-Extra: docs
|
|
31
|
+
Provides-Extra: examples
|
|
27
32
|
Provides-Extra: jax
|
|
28
33
|
Provides-Extra: notebooks
|
|
29
34
|
Description-Content-Type: text/markdown
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "eft4py"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.6"
|
|
4
4
|
description = "An Effective Field Theory toolbox for Python"
|
|
5
5
|
readme = "README.md"
|
|
6
|
-
requires-python = ">=3.
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
7
|
dependencies = [
|
|
8
8
|
"sympy>=1.12",
|
|
9
9
|
"numpy>=1.24",
|
|
10
10
|
"scipy>=1.11",
|
|
11
|
+
"pyyaml>=6",
|
|
11
12
|
]
|
|
12
13
|
|
|
13
14
|
[[project.authors]]
|
|
@@ -31,6 +32,11 @@ jax = [
|
|
|
31
32
|
"diffrax>=0.5.0",
|
|
32
33
|
"optimistix>=0.0.10",
|
|
33
34
|
]
|
|
35
|
+
examples = [
|
|
36
|
+
"eft4py[jax]",
|
|
37
|
+
"marimo>=0.25.0",
|
|
38
|
+
"matplotlib>=3.7",
|
|
39
|
+
]
|
|
34
40
|
docs = [
|
|
35
41
|
"mkdocs-material>=9.5",
|
|
36
42
|
"mkdocstrings[python]>=0.26",
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "eft4py"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.6"
|
|
4
4
|
description = "An Effective Field Theory toolbox for Python"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
7
7
|
{ name = "Rodrigo Calderon", email = "calderon.cosmology@gmail.com" }
|
|
8
8
|
]
|
|
9
|
-
requires-python = ">=3.
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
10
|
dependencies = [
|
|
11
11
|
"sympy>=1.12",
|
|
12
12
|
"numpy>=1.24",
|
|
13
13
|
"scipy>=1.11",
|
|
14
|
+
"pyyaml>=6",
|
|
14
15
|
]
|
|
15
16
|
|
|
16
17
|
[project.optional-dependencies]
|
|
@@ -30,6 +31,11 @@ jax = [
|
|
|
30
31
|
"diffrax>=0.5.0",
|
|
31
32
|
"optimistix>=0.0.10",
|
|
32
33
|
]
|
|
34
|
+
examples = [
|
|
35
|
+
"eft4py[jax]",
|
|
36
|
+
"marimo>=0.25.0",
|
|
37
|
+
"matplotlib>=3.7",
|
|
38
|
+
]
|
|
33
39
|
docs = [
|
|
34
40
|
"mkdocs-material>=9.5",
|
|
35
41
|
"mkdocstrings[python]>=0.26",
|
|
@@ -17,7 +17,7 @@ The essentials are available at the top level:
|
|
|
17
17
|
`JaxBackground` needs the `jax` extra, imported only when it is created.
|
|
18
18
|
- [`HorndeskiODE`][eft4py.HorndeskiODE]: the background equations, for
|
|
19
19
|
lower-level control.
|
|
20
|
-
- [`
|
|
20
|
+
- [`BackgroundState`][eft4py.BackgroundState]: a background state (initial
|
|
21
21
|
conditions and solutions).
|
|
22
22
|
- [`ScipySolver`][eft4py.ScipySolver] and, with the `jax` extra,
|
|
23
23
|
`JAXSolver`: integrators.
|
|
@@ -39,7 +39,7 @@ Examples:
|
|
|
39
39
|
|
|
40
40
|
from importlib import metadata as _metadata
|
|
41
41
|
|
|
42
|
-
from .core.types import
|
|
42
|
+
from .core.types import BackgroundState
|
|
43
43
|
from .models.horndeski import HorndeskiModel
|
|
44
44
|
from .physics.cosmology import Cosmology
|
|
45
45
|
from .physics.errors import SingularSystemError
|
|
@@ -64,7 +64,7 @@ __all__ = [
|
|
|
64
64
|
"JaxBackground",
|
|
65
65
|
"frozen_field",
|
|
66
66
|
"HorndeskiODE",
|
|
67
|
-
"
|
|
67
|
+
"BackgroundState",
|
|
68
68
|
"ScipySolver",
|
|
69
69
|
"JAXSolver",
|
|
70
70
|
"get_symbols",
|
|
@@ -78,11 +78,28 @@ def _build(name: str) -> Backend:
|
|
|
78
78
|
if name == "jax":
|
|
79
79
|
import jax.numpy as jnp
|
|
80
80
|
|
|
81
|
+
from ..core.types import register_jax_pytrees
|
|
82
|
+
|
|
83
|
+
register_jax_pytrees() # e.g. a jitted `JaxBackground.initial_state` returns a BackgroundState
|
|
84
|
+
|
|
81
85
|
modules = [
|
|
82
86
|
{
|
|
83
|
-
"exp": jnp.exp,
|
|
84
|
-
"
|
|
85
|
-
"
|
|
87
|
+
"exp": jnp.exp,
|
|
88
|
+
"log": jnp.log,
|
|
89
|
+
"sqrt": jnp.sqrt,
|
|
90
|
+
"Abs": jnp.abs,
|
|
91
|
+
"sign": jnp.sign,
|
|
92
|
+
"sin": jnp.sin,
|
|
93
|
+
"cos": jnp.cos,
|
|
94
|
+
"tan": jnp.tan,
|
|
95
|
+
# SymPy's default printer turns Max/Min into the reductions jnp.amax/amin.
|
|
96
|
+
# Max and its derivatives appear in a clamped argument, e.g. sqrt(Max(-phi, eps)),
|
|
97
|
+
# which keeps rejected trial steps finite so reverse-mode gradients are not NaN.
|
|
98
|
+
"Max": jnp.maximum,
|
|
99
|
+
"Min": jnp.minimum,
|
|
100
|
+
"Heaviside": lambda x, h0=0.5: jnp.heaviside(x, h0),
|
|
101
|
+
# Zero away from the kink, which a solution never sits on
|
|
102
|
+
"DiracDelta": lambda x, k=0: jnp.zeros_like(x),
|
|
86
103
|
},
|
|
87
104
|
jnp,
|
|
88
105
|
]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
r"""Run files: Horndeski models and their backgrounds described in YAML.
|
|
2
|
+
|
|
3
|
+
A run file names the free couplings, writes $G_2, \dots, G_5$ as strings, gives
|
|
4
|
+
the couplings' values (numbers, or expressions of named inputs and the
|
|
5
|
+
cosmology), the initial conditions, the cosmology, the solver and the output
|
|
6
|
+
grid. [`HorndeskiFactory`][eft4py.config.HorndeskiFactory] builds the model and
|
|
7
|
+
the solver from it and solves the background:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from eft4py.config import HorndeskiFactory
|
|
11
|
+
|
|
12
|
+
result = HorndeskiFactory.from_yaml("run.yaml").run()
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
See [`eft4py.config.spec`][eft4py.config.spec] for the format.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .factory import HorndeskiFactory, RunResult
|
|
19
|
+
from .spec import ConfigError, RunSpec, load_spec
|
|
20
|
+
|
|
21
|
+
__all__ = ["ConfigError", "HorndeskiFactory", "RunResult", "RunSpec", "load_spec"]
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
r"""Build a Horndeski model and solve its background from a run file.
|
|
2
|
+
|
|
3
|
+
[`HorndeskiFactory`][eft4py.config.factory.HorndeskiFactory] turns a validated
|
|
4
|
+
[`RunSpec`][eft4py.config.spec.RunSpec] into eft4py objects:
|
|
5
|
+
|
|
6
|
+
- [`model`][eft4py.config.factory.HorndeskiFactory.model]: the
|
|
7
|
+
[`HorndeskiModel`][eft4py.HorndeskiModel], with the $G_i$ parsed from strings;
|
|
8
|
+
- [`cosmology`][eft4py.config.factory.HorndeskiFactory.cosmology]: the
|
|
9
|
+
[`Cosmology`][eft4py.Cosmology];
|
|
10
|
+
- [`background`][eft4py.config.factory.HorndeskiFactory.background]: a
|
|
11
|
+
[`ScipyBackground`][eft4py.ScipyBackground] or
|
|
12
|
+
[`JaxBackground`][eft4py.JaxBackground], whose initial conditions are compiled
|
|
13
|
+
from the run file's expressions;
|
|
14
|
+
- [`run`][eft4py.config.factory.HorndeskiFactory.run]: the solved background,
|
|
15
|
+
after tuning the `shoot` parameter so that $H(a=1) = H_0$.
|
|
16
|
+
|
|
17
|
+
Examples:
|
|
18
|
+
```python
|
|
19
|
+
from eft4py.config import HorndeskiFactory
|
|
20
|
+
|
|
21
|
+
result = HorndeskiFactory.from_yaml("galileon.yaml").run()
|
|
22
|
+
result.grid.w_de # w_DE on the output grid
|
|
23
|
+
result.values["V0"] # the tuned coupling
|
|
24
|
+
```
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import math
|
|
28
|
+
from collections.abc import Mapping
|
|
29
|
+
from functools import cached_property
|
|
30
|
+
from os import PathLike
|
|
31
|
+
from typing import Any, NamedTuple
|
|
32
|
+
|
|
33
|
+
import numpy as np
|
|
34
|
+
import sympy as sp
|
|
35
|
+
import yaml
|
|
36
|
+
|
|
37
|
+
from ..models.horndeski import HorndeskiModel
|
|
38
|
+
from ..physics.cosmology import Cosmology
|
|
39
|
+
from ..solvers.background import BackgroundGrid, JaxBackground, ScipyBackground
|
|
40
|
+
from ..utils.expressions import compile_expr, parse_expr
|
|
41
|
+
from ..utils.symbols import SymbolTable
|
|
42
|
+
from .spec import COSMOLOGY_NAMES, ConfigError, RunSpec, load_spec
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class RunResult(NamedTuple):
|
|
46
|
+
r"""The output of [`HorndeskiFactory.run`][eft4py.config.factory.HorndeskiFactory.run].
|
|
47
|
+
|
|
48
|
+
Attributes:
|
|
49
|
+
grid: The solved background on the output grid.
|
|
50
|
+
theta: The couplings the solve used, in the order of `model.parameters`
|
|
51
|
+
(the shot one tuned).
|
|
52
|
+
values: The same couplings, by name.
|
|
53
|
+
cosmology: The cosmology.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
grid: BackgroundGrid
|
|
57
|
+
theta: Any
|
|
58
|
+
values: dict[str, float]
|
|
59
|
+
cosmology: Cosmology
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _cosmology_values(cosmo: Cosmology) -> dict[str, Any]:
|
|
63
|
+
return {"h": cosmo.h, "H0": cosmo.H0, "Omega_m": cosmo.Omega_m, "Omega_r": cosmo.Omega_r,
|
|
64
|
+
"Omega_de": cosmo.Omega_de}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class HorndeskiFactory:
|
|
68
|
+
r"""Builds and solves a Horndeski model described by a run file.
|
|
69
|
+
|
|
70
|
+
The objects are built on first use and kept, so the model's equations are
|
|
71
|
+
compiled once per factory.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
spec: The run, from [`load_spec`][eft4py.config.spec.load_spec] (or
|
|
75
|
+
use [`from_dict`][eft4py.config.factory.HorndeskiFactory.from_dict]
|
|
76
|
+
and [`from_yaml`][eft4py.config.factory.HorndeskiFactory.from_yaml]).
|
|
77
|
+
|
|
78
|
+
Attributes:
|
|
79
|
+
spec: The run.
|
|
80
|
+
symbols: The model's symbol table ($\phi$, $X$, $M_{\rm pl}$, ...).
|
|
81
|
+
parameters: The couplings' symbols, in the order of $\theta$.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def __init__(self, spec: RunSpec) -> None:
|
|
85
|
+
self.spec = spec
|
|
86
|
+
self.symbols = SymbolTable()
|
|
87
|
+
self.parameters: dict[str, sp.Symbol] = {
|
|
88
|
+
name: sp.Symbol(name, real=True) for name in spec.model.parameters
|
|
89
|
+
}
|
|
90
|
+
self._cosmology_symbols = {name: sp.Symbol(name, real=True) for name in COSMOLOGY_NAMES}
|
|
91
|
+
self._a_ini = sp.Symbol("a_ini", positive=True)
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def from_dict(cls, data: Mapping[str, Any]) -> "HorndeskiFactory":
|
|
95
|
+
"""A factory for a run file given as a mapping.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
data: The run file's content.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
The factory.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
ConfigError: If the run file is malformed (see
|
|
105
|
+
[`load_spec`][eft4py.config.spec.load_spec]).
|
|
106
|
+
"""
|
|
107
|
+
return cls(load_spec(data))
|
|
108
|
+
|
|
109
|
+
@classmethod
|
|
110
|
+
def from_yaml(cls, path: str | PathLike[str]) -> "HorndeskiFactory":
|
|
111
|
+
"""A factory for a YAML run file.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
path: The file.
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
The factory.
|
|
118
|
+
|
|
119
|
+
Raises:
|
|
120
|
+
ConfigError: If the file is not a YAML mapping or the run is malformed.
|
|
121
|
+
"""
|
|
122
|
+
with open(path) as f:
|
|
123
|
+
try:
|
|
124
|
+
data = yaml.safe_load(f)
|
|
125
|
+
except yaml.YAMLError as e:
|
|
126
|
+
raise ConfigError(f"{path}: not valid YAML ({e}).") from None
|
|
127
|
+
return cls.from_dict(data)
|
|
128
|
+
|
|
129
|
+
def _parse(self, text: Any, names: Mapping[str, sp.Basic], where: str) -> sp.Expr:
|
|
130
|
+
try:
|
|
131
|
+
return parse_expr(text, names, where=where)
|
|
132
|
+
except ValueError as e: # ExpressionError: already prefixed with `where`
|
|
133
|
+
raise ConfigError(str(e)) from None
|
|
134
|
+
|
|
135
|
+
def model(self) -> HorndeskiModel:
|
|
136
|
+
"""The Horndeski model, with the $G_i$ parsed from the `model` block.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
The model; its `parameters` are the run's couplings.
|
|
140
|
+
|
|
141
|
+
Raises:
|
|
142
|
+
ConfigError: If a $G_i$ uses an unknown name or invalid syntax.
|
|
143
|
+
"""
|
|
144
|
+
return self._model
|
|
145
|
+
|
|
146
|
+
@cached_property
|
|
147
|
+
def _model(self) -> HorndeskiModel:
|
|
148
|
+
s = self.symbols
|
|
149
|
+
names = {"phi": s.phi, "X": s.X, "Mpl": s.Mpl, **self.parameters}
|
|
150
|
+
# Parsing against `names` already guarantees what HorndeskiModel.validate's symbol check
|
|
151
|
+
# would: the G_i use only phi, X, Mpl and the parameters
|
|
152
|
+
functions = {g: self._parse(getattr(self.spec.model, g), names, f"model.{g}")
|
|
153
|
+
for g in ("G2", "G3", "G4", "G5")}
|
|
154
|
+
return HorndeskiModel(**functions, symbols=s, parameters=list(self.parameters.values()))
|
|
155
|
+
|
|
156
|
+
def cosmology(self) -> Cosmology:
|
|
157
|
+
"""The cosmology of the `cosmology` block.
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
`Cosmology(h, Omega_m, Omega_r[, Omega_b])`, or
|
|
161
|
+
`Cosmology.from_physical(h, omega_b, omega_cdm, ...)` when the block
|
|
162
|
+
gives physical densities.
|
|
163
|
+
"""
|
|
164
|
+
block = dict(self.spec.cosmology)
|
|
165
|
+
if "omega_b" in block or "omega_cdm" in block:
|
|
166
|
+
if "m_ncdm" in block:
|
|
167
|
+
block["m_ncdm"] = tuple(np.atleast_1d(block["m_ncdm"]).tolist())
|
|
168
|
+
return Cosmology.from_physical(**block)
|
|
169
|
+
return Cosmology(**block)
|
|
170
|
+
|
|
171
|
+
def background(self) -> ScipyBackground | JaxBackground:
|
|
172
|
+
r"""The background solver of the `solver` block.
|
|
173
|
+
|
|
174
|
+
Its initial conditions are a function `ic(theta, cosmo)` compiled from
|
|
175
|
+
the `initial_conditions` expressions, which read the couplings by name,
|
|
176
|
+
the cosmology and `a_ini`. With the `"jax"` backend it is traceable, so
|
|
177
|
+
the solver can be jitted, vmapped and differentiated through $\theta$.
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
The solver, for the couplings in the order of `model.parameters`.
|
|
181
|
+
|
|
182
|
+
Raises:
|
|
183
|
+
ConfigError: If an initial condition uses a name other than the
|
|
184
|
+
parameters, the cosmology quantities and `a_ini`.
|
|
185
|
+
"""
|
|
186
|
+
return self._background
|
|
187
|
+
|
|
188
|
+
@cached_property
|
|
189
|
+
def _background(self) -> ScipyBackground | JaxBackground:
|
|
190
|
+
spec = self.spec
|
|
191
|
+
cls = ScipyBackground if spec.solver.backend == "scipy" else JaxBackground
|
|
192
|
+
return cls(self._model, list(self.parameters.values()), ic=self._initial_field, a_ini=spec.a_ini,
|
|
193
|
+
**spec.solver.options)
|
|
194
|
+
|
|
195
|
+
@cached_property
|
|
196
|
+
def _initial_field(self) -> Any:
|
|
197
|
+
names = {**self.parameters, **self._cosmology_symbols, "a_ini": self._a_ini}
|
|
198
|
+
exprs = [self._parse(self.spec.initial_conditions[k], names, f"initial_conditions.{k}")
|
|
199
|
+
for k in ("phi", "phi_prime")]
|
|
200
|
+
args = [*self.parameters.values(), *self._cosmology_symbols.values(), self._a_ini]
|
|
201
|
+
backend = "jax" if self.spec.solver.backend == "jax" else "numpy"
|
|
202
|
+
field = compile_expr(exprs, args, backend)
|
|
203
|
+
n, a_ini = len(self.parameters), self.spec.a_ini
|
|
204
|
+
|
|
205
|
+
def initial_field(theta: Any, cosmo: Cosmology) -> tuple[Any, Any]:
|
|
206
|
+
cosmological = _cosmology_values(cosmo).values()
|
|
207
|
+
return field(*(theta[i] for i in range(n)), *cosmological, a_ini)
|
|
208
|
+
|
|
209
|
+
return initial_field
|
|
210
|
+
|
|
211
|
+
def theta(self, cosmo: Cosmology | None = None) -> np.ndarray:
|
|
212
|
+
r"""The couplings' values from the `values` block, in the order of `model.parameters`.
|
|
213
|
+
|
|
214
|
+
Values may use `inputs`, the cosmology and other parameters; they are
|
|
215
|
+
evaluated in dependency order.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
cosmo: The cosmology; default [`cosmology`][eft4py.config.factory.HorndeskiFactory.cosmology].
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
$\theta$; the shot parameter holds its starting guess.
|
|
222
|
+
|
|
223
|
+
Raises:
|
|
224
|
+
ConfigError: If a value uses an unknown name, or values depend on
|
|
225
|
+
each other in a cycle.
|
|
226
|
+
"""
|
|
227
|
+
cosmo = self.cosmology() if cosmo is None else cosmo
|
|
228
|
+
env: dict[str, float] = {**self.spec.inputs, **{k: float(v) for k, v in _cosmology_values(cosmo).items()}}
|
|
229
|
+
inputs = {name: sp.Symbol(name, real=True) for name in self.spec.inputs}
|
|
230
|
+
names = {**inputs, **self._cosmology_symbols, **self.parameters}
|
|
231
|
+
exprs = {p: self._parse(self.spec.values[p], names, f"values.{p}") for p in self.parameters}
|
|
232
|
+
for p in self._evaluation_order(exprs):
|
|
233
|
+
subs = {names[k]: v for k, v in env.items() if k in names}
|
|
234
|
+
value = exprs[p].subs(subs)
|
|
235
|
+
if value.free_symbols: # cannot happen after ordering; guards against a parser bug
|
|
236
|
+
raise ConfigError(f"values.{p}: could not evaluate {self.spec.values[p]!r}.")
|
|
237
|
+
env[p] = float(value)
|
|
238
|
+
if not math.isfinite(env[p]):
|
|
239
|
+
raise ConfigError(f"values.{p}: {self.spec.values[p]!r} evaluates to {env[p]}.")
|
|
240
|
+
return np.array([env[p] for p in self.parameters])
|
|
241
|
+
|
|
242
|
+
def _evaluation_order(self, exprs: Mapping[str, sp.Expr]) -> list[str]:
|
|
243
|
+
"""The parameters, each after the parameters its value uses; cycles are an error."""
|
|
244
|
+
uses = {p: {q for q, sym in self.parameters.items() if sym in e.free_symbols} for p, e in exprs.items()}
|
|
245
|
+
order: list[str] = []
|
|
246
|
+
state: dict[str, str] = {}
|
|
247
|
+
|
|
248
|
+
def visit(p: str, path: list[str]) -> None:
|
|
249
|
+
if state.get(p) == "done":
|
|
250
|
+
return
|
|
251
|
+
if state.get(p) == "visiting":
|
|
252
|
+
cycle = path[path.index(p):] + [p]
|
|
253
|
+
raise ConfigError(f"values: {' -> '.join(cycle)} depend on each other in a cycle.")
|
|
254
|
+
state[p] = "visiting"
|
|
255
|
+
for q in sorted(uses[p]):
|
|
256
|
+
visit(q, path + [p])
|
|
257
|
+
state[p] = "done"
|
|
258
|
+
order.append(p)
|
|
259
|
+
|
|
260
|
+
for p in self.parameters:
|
|
261
|
+
visit(p, [])
|
|
262
|
+
return order
|
|
263
|
+
|
|
264
|
+
def ln_a(self) -> np.ndarray:
|
|
265
|
+
r"""The output grid in $\ln a$, from $\ln a_{\min}$ to 0.
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
`num` evenly spaced values.
|
|
269
|
+
"""
|
|
270
|
+
return np.linspace(math.log(self.spec.a_min), 0.0, self.spec.num)
|
|
271
|
+
|
|
272
|
+
def run(self) -> RunResult:
|
|
273
|
+
r"""Tune the `shoot` parameter so that $H(a=1) = H_0$, then solve on the output grid.
|
|
274
|
+
|
|
275
|
+
Without `shoot`, solves with the couplings as given.
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
The background, the couplings and the cosmology.
|
|
279
|
+
|
|
280
|
+
Raises:
|
|
281
|
+
ConvergenceError: With the SciPy backend, if the shooting fails.
|
|
282
|
+
(The JAX backend does not raise: check `result.grid.ok`.)
|
|
283
|
+
"""
|
|
284
|
+
cosmo = self.cosmology()
|
|
285
|
+
theta = self.theta(cosmo)
|
|
286
|
+
bg, ln_a = self._background, self.ln_a()
|
|
287
|
+
index = None if self.spec.shoot is None else list(self.parameters).index(self.spec.shoot)
|
|
288
|
+
if isinstance(bg, JaxBackground):
|
|
289
|
+
import jax.numpy as jnp
|
|
290
|
+
|
|
291
|
+
theta = jnp.asarray(theta)
|
|
292
|
+
if index is None:
|
|
293
|
+
grid = bg.solve(theta, cosmo, ln_a)
|
|
294
|
+
else:
|
|
295
|
+
theta, grid = bg.shoot_and_solve(theta, cosmo, index, ln_a)
|
|
296
|
+
else:
|
|
297
|
+
if index is not None:
|
|
298
|
+
theta = bg.shoot(theta, cosmo, index)
|
|
299
|
+
grid = bg.solve(theta, cosmo, ln_a)
|
|
300
|
+
values = {p: float(v) for p, v in zip(self.parameters, theta, strict=True)}
|
|
301
|
+
return RunResult(grid=grid, theta=theta, values=values, cosmology=cosmo)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
__all__ = ["HorndeskiFactory", "RunResult"]
|