Gravity Operators
gravity
Gravitational Poisson solver using FFT spectral methods.
Solves the cosmological Poisson equation in conformal time:
where \(\delta\rho = \rho - \bar{\rho}\) is the overdensity. In Fourier space (periodic BCs):
(gauge choice: \(\langle \phi \rangle = 0\)).
The gravitational acceleration \(g = -\nabla\phi\) in Fourier space:
Performance design
Factory functions (make_gravitational_poisson_solver, make_gravitational_force)
precompute k-space arrays at construction time and embed them as compile-time
constants in the returned JIT-compiled callable. This means:
- No retracing when the returned function is called inside
jax.jit. - Safe for
jax.vmapover batch dimensions (all inputs are arrays). - Safe for
jax.pmapacross devices. - The k=0 singularity is resolved with
jnp.where(no Python branching), so XLA sees a single, branch-free computation graph. - Real-valued FFT (
rfftn/irfftn) halves the memory footprint and reduces FFT work by ~2× compared to the full complex transform.
The mean density is removed automatically: the k=0 mode is set to zero, so the solver accepts either \(\rho\) (total density) or \(\delta\rho\) (overdensity) — the result is identical because the homogeneous background contributes only to the k=0 mode.
References
- Hockney & Eastwood, "Computer Simulation Using Particles" (1981), Ch. 6
- Springel, "GADGET-2" (2005), MNRAS 364, 1105, Appendix A
- Dodelson & Schmidt, "Modern Cosmology" (2020), Eq. (5.54)
make_gravitational_poisson_solver(grid_shape, dx)
Return a JIT-compiled gravitational Poisson solver for a fixed grid.
Builds and caches the k-space inverse-Laplacian kernel once. The
returned callable accepts only JAX arrays (plus Python/JAX scalars for
a and M_pl), making it fully composable with jax.jit,
jax.vmap, and jax.pmap.
Parameters
grid_shape:
Spatial dimensions, e.g. (N,), (Nx, Ny), (Nx, Ny, Nz).
dx:
Uniform lattice spacing.
Returns
solve : Callable
solve(rho, a, M_pl) -> phi
- ``rho`` – density or overdensity field, shape ``grid_shape``.
The k=0 mode is automatically zeroed (mean removed).
- ``a`` – scale factor (scalar JAX array or Python float).
- ``M_pl`` – reduced Planck mass; ``G = 1 / M_pl²``.
- ``phi`` – gravitational potential, shape ``grid_shape``.
Examples
.. code-block:: python
solve = make_gravitational_poisson_solver((128, 128, 128), dx=1.0)
phi = solve(delta_rho, a=cosmo.a, M_pl=cosmo.M_pl)
# vmap over a batch of density fields:
batched_solve = jax.vmap(solve, in_axes=(0, None, None))
phi_batch = batched_solve(delta_rho_batch, a, M_pl)
Source code in jaxlatt/operators/gravity.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
make_gravitational_force(grid_shape, dx)
Return a JIT-compiled gravitational acceleration solver for a fixed grid.
Computes \(g = -\nabla\phi\) from the density field in a single FFT round-trip by multiplying phi_k by \(-i k_i\) for each spatial direction, avoiding a separate gradient pass after the Poisson solve.
The returned array has shape (ndim, *grid_shape) with g[i]
being the acceleration along axis i. This layout matches the PM
particle-mesh interface convention.
Parameters
grid_shape: Spatial dimensions. dx: Uniform lattice spacing.
Returns
force_fn : Callable
force_fn(rho, a, M_pl) -> Array[ndim, *grid_shape]
Examples
.. code-block:: python
force_fn = make_gravitational_force((128, 128, 128), dx=1.0)
g = force_fn(delta_rho, a=cosmo.a, M_pl=cosmo.M_pl)
# g[0], g[1], g[2] ←→ gx, gy, gz
# Parallel across devices:
pforce = jax.pmap(force_fn, in_axes=(0, None, None))
g = pforce(delta_rho_sharded, a, M_pl)
Source code in jaxlatt/operators/gravity.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
gravitational_potential(rho, dx, a, M_pl)
Compute gravitational potential \(\phi\) from density field (1D/2D/3D).
Solves \(\nabla^2 \phi = 4\pi G \, a^2 \, \delta\rho\) via FFT. The mean of rho is removed
automatically (k=0 mode is zeroed).
.. note::
This rebuilds the k-space kernel on every call. Use
:func:make_gravitational_poisson_solver in tight loops or inside
jax.jit/jax.vmap.
Parameters
rho:
Density or overdensity field, shape (N,), (Nx, Ny), or
(Nx, Ny, Nz).
dx:
Lattice spacing.
a:
Scale factor.
M_pl:
Reduced Planck mass.
Returns
phi : Array
Gravitational potential, same shape as rho.
Source code in jaxlatt/operators/gravity.py
gravitational_force(rho, dx, a, M_pl)
Compute gravitational acceleration \(g = -\nabla\phi\) from density field (1D/2D/3D).
Returns an array of shape (ndim, *rho.shape), where result[i]
is the acceleration component along axis i.
.. note::
This rebuilds the k-space kernel on every call. Use
:func:make_gravitational_force in tight loops or inside
jax.jit/jax.vmap.
Parameters
rho: Density or overdensity field. dx: Lattice spacing. a: Scale factor. M_pl: Reduced Planck mass.
Returns
g : Array
Gravitational acceleration, shape (ndim, *rho.shape).