cuap3mpy 0.1.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.
- cuap3mpy/__init__.py +37 -0
- cuap3mpy/__main__.py +109 -0
- cuap3mpy/cache.py +12 -0
- cuap3mpy/core/__init__.py +21 -0
- cuap3mpy/core/config.py +125 -0
- cuap3mpy/core/cosmology.py +245 -0
- cuap3mpy/core/dither.py +59 -0
- cuap3mpy/core/linear_power.py +310 -0
- cuap3mpy/core/particles.py +281 -0
- cuap3mpy/core/pinned.py +19 -0
- cuap3mpy/core/time_schedule.py +558 -0
- cuap3mpy/demo.py +59 -0
- cuap3mpy/forces/__init__.py +10 -0
- cuap3mpy/forces/patch_pp_gravity.py +202 -0
- cuap3mpy/forces/pm_gravity.py +52 -0
- cuap3mpy/gravity/__init__.py +1 -0
- cuap3mpy/gravity/reference.py +100 -0
- cuap3mpy/gravity/root_pm/__init__.py +12 -0
- cuap3mpy/gravity/root_pm/mesh.py +246 -0
- cuap3mpy/gravity/root_pm/potential.py +299 -0
- cuap3mpy/gravity/root_pm/solver.py +252 -0
- cuap3mpy/gravity/short_range/__init__.py +5 -0
- cuap3mpy/gravity/short_range/cell_list.py +155 -0
- cuap3mpy/gravity/short_range/core/__init__.py +23 -0
- cuap3mpy/gravity/short_range/core/fit.py +212 -0
- cuap3mpy/gravity/short_range/core/kernel.py +47 -0
- cuap3mpy/gravity/short_range/core/solver.py +119 -0
- cuap3mpy/gravity/short_range/mesh.py +247 -0
- cuap3mpy/gravity/short_range/pairs/__init__.py +12 -0
- cuap3mpy/gravity/short_range/pairs/fit.py +270 -0
- cuap3mpy/gravity/short_range/pairs/solver.py +432 -0
- cuap3mpy/gravity/short_range/patch/__init__.py +14 -0
- cuap3mpy/gravity/short_range/patch/kernel.py +113 -0
- cuap3mpy/gravity/short_range/patch/plan.py +259 -0
- cuap3mpy/gravity/short_range/patch/solver.py +255 -0
- cuap3mpy/halos/__init__.py +13 -0
- cuap3mpy/halos/catalog.py +168 -0
- cuap3mpy/halos/fof.py +151 -0
- cuap3mpy/halos/slab.py +378 -0
- cuap3mpy/ic/__init__.py +5 -0
- cuap3mpy/ic/abacus_rvzel.py +327 -0
- cuap3mpy/ic/cross_pancake.py +54 -0
- cuap3mpy/ic/gadget.py +107 -0
- cuap3mpy/ic/lattice_lpt.py +554 -0
- cuap3mpy/ic/plummer.py +102 -0
- cuap3mpy/ic/zeldovich.py +48 -0
- cuap3mpy/power.py +91 -0
- cuap3mpy/resident.py +130 -0
- cuap3mpy/run_lpt_streaming.py +107 -0
- cuap3mpy/run_music.py +81 -0
- cuap3mpy/simulation.py +259 -0
- cuap3mpy/snapshot.py +352 -0
- cuap3mpy/streaming/__init__.py +1 -0
- cuap3mpy/streaming/host.py +112 -0
- cuap3mpy/streaming/root.py +132 -0
- cuap3mpy/streaming/sim.py +269 -0
- cuap3mpy/streaming/tile_force.py +109 -0
- cuap3mpy/streaming/tiles.py +165 -0
- cuap3mpy/validate.py +617 -0
- cuap3mpy-0.1.0.dist-info/METADATA +225 -0
- cuap3mpy-0.1.0.dist-info/RECORD +65 -0
- cuap3mpy-0.1.0.dist-info/WHEEL +5 -0
- cuap3mpy-0.1.0.dist-info/entry_points.txt +2 -0
- cuap3mpy-0.1.0.dist-info/licenses/LICENSE +28 -0
- cuap3mpy-0.1.0.dist-info/top_level.txt +1 -0
cuap3mpy/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""cuap3mpy -- GPU cosmological N-body simulation (CuPy).
|
|
2
|
+
|
|
3
|
+
import cuap3mpy as cap
|
|
4
|
+
sim = cap.Simulation(N=512, box=350.0, Om0=0.3, sigma8=0.8)
|
|
5
|
+
sim.run("snap.hdf5")
|
|
6
|
+
sim.P.pos, sim.P.vel, sim.halos
|
|
7
|
+
|
|
8
|
+
Every CuPy allocation goes through the driver's stream-ordered pool
|
|
9
|
+
(``cudaMallocAsync``), set here once before any array exists: physical
|
|
10
|
+
pages map into fresh address ranges without block-splitting fragmentation
|
|
11
|
+
(L100N512 peaks at 16.0 GiB live and completes on a 24 GB card).
|
|
12
|
+
|
|
13
|
+
``Simulation`` (simulation.py) is the one-call entry point; the solver
|
|
14
|
+
itself is ``core`` (state), ``gravity`` + ``forces`` (the force split),
|
|
15
|
+
``resident`` / ``streaming`` (the two drivers), ``ic`` and ``halos``.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import cupy as cp
|
|
19
|
+
|
|
20
|
+
pool = cp.cuda.MemoryAsyncPool()
|
|
21
|
+
cp.cuda.set_allocator(pool.malloc)
|
|
22
|
+
|
|
23
|
+
__version__ = "0.1.0"
|
|
24
|
+
|
|
25
|
+
from .core import Config
|
|
26
|
+
from .power import power_spectrum
|
|
27
|
+
from .simulation import Simulation, find_halos
|
|
28
|
+
from .snapshot import (
|
|
29
|
+
Halos,
|
|
30
|
+
load_halos,
|
|
31
|
+
load_ids,
|
|
32
|
+
load_snapshot,
|
|
33
|
+
save_gadget,
|
|
34
|
+
save_halos,
|
|
35
|
+
save_output,
|
|
36
|
+
save_snapshot,
|
|
37
|
+
)
|
cuap3mpy/__main__.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Unified command-line entry point in physical units (Mpc/h, km/s, Msun/h).
|
|
2
|
+
|
|
3
|
+
python -m cuap3mpy --N 512 --box 350 --out snap.hdf5 --halos halos.hdf5
|
|
4
|
+
python -m cuap3mpy --music IC.hdf5 --zout 2,1 --ids --out snap.hdf5
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import builtins
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
from . import pool
|
|
12
|
+
from .simulation import Simulation
|
|
13
|
+
|
|
14
|
+
t0 = time.perf_counter()
|
|
15
|
+
orig_print = builtins.print
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def stamped(msg="", flush=False):
|
|
19
|
+
orig_print(f"[{time.perf_counter() - t0:8.1f}s] {msg}", flush=flush)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def parse_args():
|
|
23
|
+
p = argparse.ArgumentParser(
|
|
24
|
+
prog="cuap3mpy",
|
|
25
|
+
description="GPU cosmological N-body simulation (adaptive P3M on CuPy).",
|
|
26
|
+
)
|
|
27
|
+
p.add_argument("--N", type=int, default=512,
|
|
28
|
+
help="Particle lattice and root PM grid size N (N^3 particles).")
|
|
29
|
+
p.add_argument("--box", type=float, default=350.0,
|
|
30
|
+
help="Comoving box size in Mpc/h (default: 350).")
|
|
31
|
+
p.add_argument("--music", type=str, default=None,
|
|
32
|
+
help="Path to a MUSIC / Gadget HDF5 initial-condition file.")
|
|
33
|
+
p.add_argument("--out", type=str, default=None,
|
|
34
|
+
help="Output snapshot path (.hdf5/.h5 -> Gadget HDF5, else directory).")
|
|
35
|
+
p.add_argument("--halos", type=str, default=None,
|
|
36
|
+
help="Output z=0 FoF halo catalog path (.hdf5/.h5 or .npz).")
|
|
37
|
+
p.add_argument("--no-halos", action="store_true",
|
|
38
|
+
help="Skip z=0 FoF halo finding when --halos is not given.")
|
|
39
|
+
p.add_argument("--ids", action="store_true",
|
|
40
|
+
help="Carry persistent particle IDs across steps and snapshots.")
|
|
41
|
+
p.add_argument("--zout", type=str, default="",
|
|
42
|
+
help="Comma-separated intermediate snapshot redshifts (e.g. 3,2,1,0.5).")
|
|
43
|
+
p.add_argument("--Om0", type=float, default=0.3)
|
|
44
|
+
p.add_argument("--OmL", type=float, default=0.7)
|
|
45
|
+
p.add_argument("--Ob0", type=float, default=0.048)
|
|
46
|
+
p.add_argument("--h", type=float, default=0.7)
|
|
47
|
+
p.add_argument("--sigma8", type=float, default=0.8)
|
|
48
|
+
p.add_argument("--ns", type=float, default=0.96)
|
|
49
|
+
p.add_argument("--z-init", type=float, default=99.0)
|
|
50
|
+
p.add_argument("--seed", type=int, default=42)
|
|
51
|
+
p.add_argument("--transfer", choices=("eh98", "camb", "class"), default="eh98")
|
|
52
|
+
p.add_argument("--Tcmb", type=float, default=2.725)
|
|
53
|
+
p.add_argument("--Neff", type=float, default=3.044)
|
|
54
|
+
p.add_argument("--w0", type=float, default=-1.0)
|
|
55
|
+
p.add_argument("--wa", type=float, default=0.0)
|
|
56
|
+
p.add_argument("--ic", choices=("gaussian", "fixed", "inverted", "fixed_inverted"),
|
|
57
|
+
default="gaussian")
|
|
58
|
+
p.add_argument("--softening", type=float, default=0.05,
|
|
59
|
+
help="Nominal softening eps in root cells dx (default: 0.05).")
|
|
60
|
+
p.add_argument("--softening-law", choices=("gadget_spline", "single_spline"),
|
|
61
|
+
default="gadget_spline")
|
|
62
|
+
p.add_argument("--streaming", choices=("auto", "true", "false"), default="auto")
|
|
63
|
+
p.add_argument("--steps", type=int, default=None,
|
|
64
|
+
help="Optional step cap for short test runs.")
|
|
65
|
+
return p.parse_args()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def main():
|
|
69
|
+
args = parse_args()
|
|
70
|
+
builtins.print = stamped
|
|
71
|
+
|
|
72
|
+
zout = ()
|
|
73
|
+
if args.zout:
|
|
74
|
+
zout = tuple([float(z) for z in args.zout.split(",")])
|
|
75
|
+
streaming = "auto"
|
|
76
|
+
if args.streaming == "true":
|
|
77
|
+
streaming = True
|
|
78
|
+
elif args.streaming == "false":
|
|
79
|
+
streaming = False
|
|
80
|
+
|
|
81
|
+
sim = Simulation(
|
|
82
|
+
N=args.N, box=args.box,
|
|
83
|
+
Om0=args.Om0, OmL=args.OmL, Ob0=args.Ob0, h=args.h,
|
|
84
|
+
sigma8=args.sigma8, ns=args.ns, z_init=args.z_init, seed=args.seed,
|
|
85
|
+
transfer=args.transfer, Tcmb=args.Tcmb, Neff=args.Neff,
|
|
86
|
+
w0=args.w0, wa=args.wa, ic=args.ic, zout=zout, ids=args.ids,
|
|
87
|
+
softening=args.softening, softening_law=args.softening_law,
|
|
88
|
+
streaming=streaming, music=args.music,
|
|
89
|
+
)
|
|
90
|
+
if args.steps is not None:
|
|
91
|
+
sim.driver.schedule.nsteps = min(args.steps, sim.driver.schedule.nsteps)
|
|
92
|
+
nsteps = sim.driver.schedule.nsteps
|
|
93
|
+
|
|
94
|
+
want_halos = (args.halos is not None) or (not args.no_halos)
|
|
95
|
+
t1 = time.perf_counter()
|
|
96
|
+
sim.run(args.out, halos=want_halos)
|
|
97
|
+
dt = time.perf_counter() - t1
|
|
98
|
+
|
|
99
|
+
if args.halos is not None:
|
|
100
|
+
sim.save_halos(args.halos)
|
|
101
|
+
print(f"halos saved: {args.halos} ({sim.halos.n} groups)", flush=True)
|
|
102
|
+
elif sim.halos is not None:
|
|
103
|
+
print(f"FoF halos: {sim.halos.n} groups", flush=True)
|
|
104
|
+
print(f"DONE: {nsteps} steps, run+post {dt:.1f} s, "
|
|
105
|
+
f"pool peak {pool.total_bytes() / 2**30:.2f} GiB", flush=True)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
main()
|
cuap3mpy/cache.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Table cache root: ``$CUAP3MPY_CACHE``, else ``~/.cache/cuap3mpy``.
|
|
2
|
+
|
|
3
|
+
Force and transfer tables are built on first use and saved under this
|
|
4
|
+
root; every file or directory name spells out the parameters it depends
|
|
5
|
+
on. Deleting the root forces a rebuild.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
cache_root = Path(os.environ.get(
|
|
12
|
+
"CUAP3MPY_CACHE", Path.home() / ".cache" / "cuap3mpy"))
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Simulation state and cosmological inputs.
|
|
2
|
+
|
|
3
|
+
Reading order:
|
|
4
|
+
config.py the one dataclass every module reads
|
|
5
|
+
particles.py SoA store: uint32 fixed-point positions +
|
|
6
|
+
int32 fixed-point velocities, with the CUDA
|
|
7
|
+
pack / drift kernels; impulse-based, no
|
|
8
|
+
acceleration array
|
|
9
|
+
cosmology.py FLRW background E(a) (matter, radiation,
|
|
10
|
+
Lambda), leapfrog clocks, growth (one RK4 for
|
|
11
|
+
table and LPT)
|
|
12
|
+
linear_power.py z=0 P(k), sigma8-normalized: Eisenstein--Hu fit
|
|
13
|
+
or the cached CAMB/CLASS transfer table
|
|
14
|
+
time_schedule.py formation-shaped power-law nodes and the dt
|
|
15
|
+
tables the driver steps with
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .config import Config
|
|
19
|
+
from .particles import Particles
|
|
20
|
+
from .cosmology import Cosmology
|
|
21
|
+
from .linear_power import LinearPower
|
cuap3mpy/core/config.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Simulation inputs shared by the production solver.
|
|
2
|
+
|
|
3
|
+
Cosmological runs always end at ``a = 1`` and obtain their power schedule
|
|
4
|
+
from the ``time_schedule`` package. Gravity owns its cache policy and location.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import math
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# ``eps`` is the nominal softening reported by comparison codes; the
|
|
12
|
+
# spline support h depends on the law (Gadget two-piece cubic spline
|
|
13
|
+
# matches AREPO; single_spline matches Abacus).
|
|
14
|
+
h_over_eps = {
|
|
15
|
+
"gadget_spline": 2.8,
|
|
16
|
+
"single_spline": 2.15517,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class Config:
|
|
22
|
+
# --- Grid ---
|
|
23
|
+
N: int = 512
|
|
24
|
+
box_size: float = 500000.0
|
|
25
|
+
|
|
26
|
+
# --- Units (CGS base) ---
|
|
27
|
+
unit_length_in_cm: float = 3.08568e21 # 1 ckpc
|
|
28
|
+
unit_mass_in_g: float = 1.98892e43 # 10^10 Msun
|
|
29
|
+
unit_velocity_in_cm_per_s: float = 1.0e5 # 1 km/s
|
|
30
|
+
|
|
31
|
+
# --- Cosmology ---
|
|
32
|
+
cosmology: bool = True
|
|
33
|
+
Om0: float = 0.3
|
|
34
|
+
OmL: float = 0.7
|
|
35
|
+
Ob0: float = 0.048
|
|
36
|
+
h: float = 0.7
|
|
37
|
+
sigma8: float = 0.8
|
|
38
|
+
ns: float = 0.96
|
|
39
|
+
a_init: float = 0.01
|
|
40
|
+
Tcmb: float = 2.725 # K
|
|
41
|
+
Neff: float = 3.044 # massless neutrino species (camb/class only)
|
|
42
|
+
# Dark energy w(a) = w0 + wa (1 - a) (CPL); -1, 0 is Lambda.
|
|
43
|
+
w0: float = -1.0
|
|
44
|
+
wa: float = 0.0
|
|
45
|
+
# Linear transfer function: "eh98" (Eisenstein--Hu fit), "camb" or
|
|
46
|
+
# "class" (Boltzmann tables, built once and cached; core/linear_power.py).
|
|
47
|
+
transfer: str = "eh98"
|
|
48
|
+
|
|
49
|
+
# --- Particles ---
|
|
50
|
+
Np: int = 512 # particles per dimension
|
|
51
|
+
ids: bool = False # carry persistent particle IDs across steps
|
|
52
|
+
|
|
53
|
+
# --- Gravity ---
|
|
54
|
+
softening_dx: float = 0.05 # nominal eps in root dx
|
|
55
|
+
softening_law: str = "gadget_spline"
|
|
56
|
+
# Both crossovers are 5090 economics, re-swept whenever the kernels
|
|
57
|
+
# change speed (scripts/measure_core_crossover.py sweeps the core
|
|
58
|
+
# one on saved snapshots). The core crossover trades N^2
|
|
59
|
+
# covered-pair work for linear mesh work. BCC18 patch and core
|
|
60
|
+
# (923f6b8), z = 0, one evaluation, mid + fine rung, ms, resident
|
|
61
|
+
# N512 eps 0.02: L50 (200, 4000) 1515 -> (100, 1000) 1184, L100
|
|
62
|
+
# 1183 -> 975. The core crossover is sharp (L50 at trigger 100:
|
|
63
|
+
# 250 / 500 / 1000 / 2000 give 1517 / 1265 / 1184 / 1244); the
|
|
64
|
+
# trigger is flat over 75-150 (L100 best 75 at 957).
|
|
65
|
+
patch_trigger_rho: float = 100.0 # patch crossover
|
|
66
|
+
core_fft_threshold: float = 1000.0 # core crossover
|
|
67
|
+
patch_batch: int = 384 # patch FFT memory/performance
|
|
68
|
+
|
|
69
|
+
# --- Velocity storage ---
|
|
70
|
+
# Velocities are int32 fixed point: p = q * dv with quantum
|
|
71
|
+
# dv = vmax * a / 2^31 (a = 1 without cosmology), so vmax bounds the
|
|
72
|
+
# peculiar velocity per component. CDM never approaches 1e4 km/s;
|
|
73
|
+
# exotic models (exothermic SIDM, thermal relics) raise it.
|
|
74
|
+
vmax: float = 1.0e4 # km/s
|
|
75
|
+
|
|
76
|
+
# --- Time integration ---
|
|
77
|
+
dt: float = 0.01 # non-cosmology fixed step
|
|
78
|
+
t_final: float = -1.0
|
|
79
|
+
|
|
80
|
+
# --- I/O ---
|
|
81
|
+
output_every: int = 20 # progress line every this many nodes
|
|
82
|
+
# Snapshot redshifts before the end (z > 0; a = 1 ends every run): each
|
|
83
|
+
# a = 1/(1+z) becomes a root node (TimeSchedule.outputs).
|
|
84
|
+
zout: tuple = ()
|
|
85
|
+
|
|
86
|
+
# --- Derived: units ---
|
|
87
|
+
@property
|
|
88
|
+
def unit_time_in_s(self):
|
|
89
|
+
return self.unit_length_in_cm / self.unit_velocity_in_cm_per_s
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def G(self):
|
|
93
|
+
return (6.67430e-8
|
|
94
|
+
* self.unit_mass_in_g * self.unit_time_in_s**2
|
|
95
|
+
/ self.unit_length_in_cm**3)
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def H0(self):
|
|
99
|
+
return 100.0 * self.h * 1e5 / 3.08568e24 * self.unit_time_in_s
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def rho_crit(self):
|
|
103
|
+
return 3.0 * self.H0**2 / (8.0 * math.pi * self.G)
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def box_hmpc(self):
|
|
107
|
+
"""Box size in Mpc/h."""
|
|
108
|
+
return self.box_size * self.unit_length_in_cm / 3.08568e24 * self.h
|
|
109
|
+
|
|
110
|
+
# --- Derived: grid & particles ---
|
|
111
|
+
@property
|
|
112
|
+
def dx(self):
|
|
113
|
+
return self.box_size / self.N
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def softening_h(self):
|
|
117
|
+
"""Spline support in root cells dx."""
|
|
118
|
+
return h_over_eps[self.softening_law] * self.softening_dx
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def mp(self):
|
|
122
|
+
"""Particle mass in code units."""
|
|
123
|
+
if self.cosmology:
|
|
124
|
+
return self.Om0 * self.rho_crit * self.box_size**3 / self.Np**3
|
|
125
|
+
return self.box_size**3 / self.Np**3
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""FLRW background, leapfrog clocks and linear growth for comoving N-body.
|
|
2
|
+
|
|
3
|
+
Notation: ``E(a) = H(a)/H0``; ``Om0`` is the CDM+baryon density the
|
|
4
|
+
particles carry, ``Omega_m(a) = Om0 a^-3 / E^2``; ``Or`` photons plus
|
|
5
|
+
``Neff`` massless neutrinos; ``OmL`` the dark energy today, with
|
|
6
|
+
w(a) = w0 + wa (1 - a) (CPL; -1, 0 is Lambda); ' is d/dln a.
|
|
7
|
+
|
|
8
|
+
E^2 = Om0 a^-3 + Or a^-4 + OmL a^(-3 (1 + w0 + wa)) e^(-3 wa (1 - a))
|
|
9
|
+
|
|
10
|
+
exactly the background CAMB and CLASS compute for this model (flat,
|
|
11
|
+
smooth dark energy, massless neutrinos). For ``transfer = "eh98"``
|
|
12
|
+
Or = 0 and OmL is ``cfg.OmL`` (the fit has no radiation); for
|
|
13
|
+
"camb"/"class" Or follows from Tcmb and Neff and OmL = 1 - Om0 - Or, so
|
|
14
|
+
the transfer function, the expansion and the growth are one cosmology.
|
|
15
|
+
|
|
16
|
+
Growth is the N-body's own linear equation: only CDM+baryons cluster,
|
|
17
|
+
everything in E(a) sets the friction,
|
|
18
|
+
|
|
19
|
+
D1'' + (2 + dlnE/dlna) D1' = 1.5 Omega_m(a) D1
|
|
20
|
+
D2'' + (2 + dlnE/dlna) D2' = 1.5 Omega_m(a) (D2 - D1^2)
|
|
21
|
+
|
|
22
|
+
so back-scaling the z = 0 spectrum with D1 returns it at z = 0. One RK4
|
|
23
|
+
integration from ``astart`` (deep in the radiation era, a_eq ~ 3e-4)
|
|
24
|
+
serves both ``growth_table`` (D, f for the time schedule) and
|
|
25
|
+
``lpt_growth`` (D1, D2, f1, f2 for 2LPT). Matter+Lambda reduces it to
|
|
26
|
+
the former closed forms: dlnE/dlna = -1.5 Omega_m(a).
|
|
27
|
+
|
|
28
|
+
Usage:
|
|
29
|
+
cosmo = Cosmology(cfg) # Om0, OmL, w0, wa, h, a_init, H0,
|
|
30
|
+
# transfer, Tcmb, Neff
|
|
31
|
+
|
|
32
|
+
kicks = cosmo.da2dt_kick(alist) # per-node kick dt
|
|
33
|
+
drifts = cosmo.da2dt_drift(alist) # per-interval drift dt
|
|
34
|
+
pf = cosmo.poisson_factor()
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
import math
|
|
38
|
+
|
|
39
|
+
import cupy as cp
|
|
40
|
+
import numpy as np
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
ngrowth = 8193 # growth_table log-a samples
|
|
44
|
+
nper = 256 # growth RK4 steps per e-fold of a
|
|
45
|
+
astart = 1.0e-8 # growth integration start
|
|
46
|
+
rho_crit_h2 = 2.77536627e11 # Msun/h per (Mpc/h)^3
|
|
47
|
+
amin = 1.0e-9 # proper-time table a = amin .. 1,
|
|
48
|
+
nt = 65536 # uniform in ln a
|
|
49
|
+
# SI constants for Omega_gamma (the values CAMB uses)
|
|
50
|
+
c_SI = 2.99792458e8 # m/s
|
|
51
|
+
G_SI = 6.6743e-11 # m^3 / (kg s^2)
|
|
52
|
+
sigma_SB = 5.670374419e-8 # W / (m^2 K^4)
|
|
53
|
+
Mpc_m = 3.085677581e22 # m
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class Cosmology:
|
|
57
|
+
"""FLRW cosmology for comoving N-body.
|
|
58
|
+
|
|
59
|
+
Variables:
|
|
60
|
+
x : comoving position
|
|
61
|
+
p = a^2 dx/dt : conjugate momentum
|
|
62
|
+
|
|
63
|
+
Equations of motion stepping in a:
|
|
64
|
+
dx/da = p / (a^3 H(a)) -> drift
|
|
65
|
+
dp/da = -nabla(Phi) / (a H(a)) -> kick
|
|
66
|
+
|
|
67
|
+
Poisson equation:
|
|
68
|
+
nabla^2 Phi = 4 pi G (rho - rho_bar) / a
|
|
69
|
+
|
|
70
|
+
With proper units (G, H0 set correctly), 4*pi*G*rho_bar
|
|
71
|
+
automatically equals (3/2)*Om0*H0^2. The only cosmological
|
|
72
|
+
modification to the Poisson source is division by a.
|
|
73
|
+
|
|
74
|
+
E, H, Omega_m and proper_h0 take Python floats or NumPy arrays.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(self, cfg):
|
|
78
|
+
self.Om0 = cfg.Om0
|
|
79
|
+
self.OmL = cfg.OmL
|
|
80
|
+
self.w0 = cfg.w0
|
|
81
|
+
self.wa = cfg.wa
|
|
82
|
+
self.h = cfg.h
|
|
83
|
+
self.a = cfg.a_init
|
|
84
|
+
self.H0 = cfg.H0
|
|
85
|
+
|
|
86
|
+
self.Or = 0.0
|
|
87
|
+
if cfg.transfer != "eh98":
|
|
88
|
+
# Omega_gamma = (4 sigma_SB Tcmb^4 / c^3) / (3 H0^2 / 8 pi G);
|
|
89
|
+
# each neutrino species adds 7/8 (4/11)^(4/3) of it.
|
|
90
|
+
H0_SI = 1.0e5 * cfg.h / Mpc_m # 1/s
|
|
91
|
+
Og = (4.0 * sigma_SB * cfg.Tcmb**4 / c_SI**3
|
|
92
|
+
/ (3.0 * H0_SI**2 / (8.0 * math.pi * G_SI)))
|
|
93
|
+
self.Or = Og * (
|
|
94
|
+
1.0 + cfg.Neff * 7.0 / 8.0 * (4.0 / 11.0) ** (4.0 / 3.0))
|
|
95
|
+
self.OmL = 1.0 - cfg.Om0 - self.Or
|
|
96
|
+
|
|
97
|
+
# H0 t = int dlna / E: below amin E ~ a^-n with n = -dlnE/dlna,
|
|
98
|
+
# so H0 t = 1 / (n E); above it a trapezoid on a ln a table.
|
|
99
|
+
self.lna = np.linspace(math.log(amin), 0.0, nt)
|
|
100
|
+
a = np.exp(self.lna)
|
|
101
|
+
invE = 1.0 / self.E(a)
|
|
102
|
+
t = np.empty(nt)
|
|
103
|
+
t[0] = invE[0] / -self.dlnE_dlna(a[0])
|
|
104
|
+
t[1:] = t[0] + np.cumsum(
|
|
105
|
+
0.5 * (invE[:-1] + invE[1:]) * np.diff(self.lna))
|
|
106
|
+
self.lnt = np.log(t)
|
|
107
|
+
|
|
108
|
+
def dark_energy(self, a):
|
|
109
|
+
"""CPL dark-energy density over today's critical density."""
|
|
110
|
+
return (self.OmL * a ** (-3.0 * (1.0 + self.w0 + self.wa))
|
|
111
|
+
* np.exp(-3.0 * self.wa * (1.0 - a)))
|
|
112
|
+
|
|
113
|
+
def E(self, a):
|
|
114
|
+
"""E(a) = H(a)/H0."""
|
|
115
|
+
return np.sqrt(self.Om0 * a**-3 + self.Or * a**-4
|
|
116
|
+
+ self.dark_energy(a))
|
|
117
|
+
|
|
118
|
+
def dlnE_dlna(self, a):
|
|
119
|
+
"""Logarithmic slope of E(a); dark energy scales as
|
|
120
|
+
a^(-3 (1 + w(a)))."""
|
|
121
|
+
w = self.w0 + self.wa * (1.0 - a)
|
|
122
|
+
return -(1.5 * self.Om0 * a**-3
|
|
123
|
+
+ 2.0 * self.Or * a**-4
|
|
124
|
+
+ 1.5 * (1.0 + w) * self.dark_energy(a)) / self.E(a) ** 2
|
|
125
|
+
|
|
126
|
+
def H(self, a):
|
|
127
|
+
"""Hubble parameter H(a) in code units."""
|
|
128
|
+
return self.H0 * self.E(a)
|
|
129
|
+
|
|
130
|
+
def Omega_m(self, a):
|
|
131
|
+
"""CDM+baryon density parameter Omega_m(a)."""
|
|
132
|
+
return self.Om0 * a**-3 / self.E(a) ** 2
|
|
133
|
+
|
|
134
|
+
def proper_h0(self, a):
|
|
135
|
+
"""Return H0 times proper time through a."""
|
|
136
|
+
return np.exp(np.interp(np.log(a), self.lna, self.lnt))
|
|
137
|
+
|
|
138
|
+
def kick_halves(self, alist):
|
|
139
|
+
"""Return incoming and outgoing halves of every node kick."""
|
|
140
|
+
a = np.asarray(alist, dtype=np.float64)
|
|
141
|
+
mid = 0.5 * (a[:-1] + a[1:])
|
|
142
|
+
lo = np.concatenate((a[:1], mid))
|
|
143
|
+
hi = np.concatenate((mid, a[-1:]))
|
|
144
|
+
a1 = 0.5 * (lo + a)
|
|
145
|
+
a2 = 0.5 * (a + hi)
|
|
146
|
+
return (a - lo) / (a1 * self.H(a1)), (hi - a) / (a2 * self.H(a2))
|
|
147
|
+
|
|
148
|
+
def da2dt_kick(self, alist):
|
|
149
|
+
"""Kick dt per NODE (Quinn et al. 1997: dt = int da/(a H)).
|
|
150
|
+
|
|
151
|
+
One da, two clocks — kicks use 1/(aH), drifts 1/(a**3 H) —
|
|
152
|
+
because p = a**2 dx/dt. Kicks live on nodes, drifts on
|
|
153
|
+
intervals (leapfrog staggering): node k's kick covers its dual
|
|
154
|
+
interval, neighboring midpoint to neighboring midpoint (clamped
|
|
155
|
+
at the ends), integrated in two halves meeting AT the node —
|
|
156
|
+
K/2 (DK)^{n-1} D K/2. The two-piece quadrature matters: one
|
|
157
|
+
midpoint evaluation per interval costs 8% more schedule steps
|
|
158
|
+
at equal linear tolerance.
|
|
159
|
+
"""
|
|
160
|
+
incoming, outgoing = self.kick_halves(alist)
|
|
161
|
+
return incoming + outgoing
|
|
162
|
+
|
|
163
|
+
def da2dt_drift(self, alist):
|
|
164
|
+
"""Drift dt per interval."""
|
|
165
|
+
a = np.asarray(alist, dtype=np.float64)
|
|
166
|
+
a_mid = 0.5 * (a[:-1] + a[1:])
|
|
167
|
+
return (a[1:] - a[:-1]) / (a_mid**3 * self.H(a_mid))
|
|
168
|
+
|
|
169
|
+
def poisson_factor(self):
|
|
170
|
+
"""Multiplicative factor on Poisson source: 1/a.
|
|
171
|
+
|
|
172
|
+
With proper units: 4*pi*G*rho_bar/a = (3/2)*Om0*H0^2/a.
|
|
173
|
+
Without units (G=1, H0=1): need manual correction.
|
|
174
|
+
"""
|
|
175
|
+
return 1.0 / self.a
|
|
176
|
+
|
|
177
|
+
@property
|
|
178
|
+
def z(self):
|
|
179
|
+
return 1.0 / self.a - 1.0
|
|
180
|
+
|
|
181
|
+
def growth_table(self, a_init):
|
|
182
|
+
"""Return ``(a, D, f)`` CuPy arrays on a log grid ending at ``a = 1``,
|
|
183
|
+
D normalized to 1 there and f = D1'/D1."""
|
|
184
|
+
a_start = min(1.0e-6, a_init * 1.0e-3)
|
|
185
|
+
lna = np.linspace(math.log(a_start), 0.0, ngrowth)
|
|
186
|
+
state = self.growth(lna)
|
|
187
|
+
D = state[:, 0] / state[-1, 0]
|
|
188
|
+
f = state[:, 1] / state[:, 0]
|
|
189
|
+
return cp.asarray(np.exp(lna)), cp.asarray(D), cp.asarray(f)
|
|
190
|
+
|
|
191
|
+
def lpt_growth(self, a):
|
|
192
|
+
"""Return normalized ``(D1, D2, f1, f2)`` at scale factor ``a``."""
|
|
193
|
+
state = self.growth(np.array([math.log(a), 0.0]))
|
|
194
|
+
d1, v1, d2, v2 = state[0]
|
|
195
|
+
d1_today = state[1, 0]
|
|
196
|
+
return (
|
|
197
|
+
d1 / d1_today,
|
|
198
|
+
d2 / d1_today**2,
|
|
199
|
+
v1 / d1,
|
|
200
|
+
v2 / d2,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
def growth(self, lnas):
|
|
204
|
+
"""RK4 states (D1, D1', D2, D2') at the increasing nodes ``lnas``.
|
|
205
|
+
|
|
206
|
+
Start at ``astart`` with a_eq = Or/Om0: D1 the Meszaros growing
|
|
207
|
+
mode a_eq + 1.5 a (exact for matter+radiation), D2 its radiation
|
|
208
|
+
particular solution -1.5 a_eq a plus the matter one -3/7 D1^2;
|
|
209
|
+
for eh98 (a_eq = 0) both are the matter-era modes exactly.
|
|
210
|
+
"""
|
|
211
|
+
aeq = self.Or / self.Om0
|
|
212
|
+
a = astart
|
|
213
|
+
state = np.array([
|
|
214
|
+
aeq + 1.5 * a,
|
|
215
|
+
1.5 * a,
|
|
216
|
+
-1.5 * aeq * a - 3.0 / 7.0 * (1.5 * a) ** 2,
|
|
217
|
+
-1.5 * aeq * a - 6.0 / 7.0 * (1.5 * a) ** 2,
|
|
218
|
+
])
|
|
219
|
+
lna = math.log(astart)
|
|
220
|
+
out = np.empty((len(lnas), 4))
|
|
221
|
+
for i in range(len(lnas)):
|
|
222
|
+
n = max(1, math.ceil((lnas[i] - lna) * nper))
|
|
223
|
+
h = (lnas[i] - lna) / n
|
|
224
|
+
for j in range(n):
|
|
225
|
+
k1 = self.growth_rhs(lna + j * h, state)
|
|
226
|
+
k2 = self.growth_rhs(lna + (j + 0.5) * h, state + 0.5 * h * k1)
|
|
227
|
+
k3 = self.growth_rhs(lna + (j + 0.5) * h, state + 0.5 * h * k2)
|
|
228
|
+
k4 = self.growth_rhs(lna + (j + 1) * h, state + h * k3)
|
|
229
|
+
state = state + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6
|
|
230
|
+
lna = lnas[i]
|
|
231
|
+
out[i] = state
|
|
232
|
+
return out
|
|
233
|
+
|
|
234
|
+
def growth_rhs(self, lna, state):
|
|
235
|
+
"""d/dlna of (D1, D1', D2, D2')."""
|
|
236
|
+
d1, v1, d2, v2 = state
|
|
237
|
+
a = math.exp(lna)
|
|
238
|
+
drag = 2.0 + self.dlnE_dlna(a)
|
|
239
|
+
gravity = 1.5 * self.Omega_m(a)
|
|
240
|
+
return np.array([
|
|
241
|
+
v1,
|
|
242
|
+
-drag * v1 + gravity * d1,
|
|
243
|
+
v2,
|
|
244
|
+
-drag * v2 + gravity * d2 - gravity * d1 * d1,
|
|
245
|
+
])
|
cuap3mpy/core/dither.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Stochastic rounding for the fixed-point velocity deposits.
|
|
2
|
+
|
|
3
|
+
Every kick writes ``q += floor(x + u)`` with ``u in [0, 1)`` hashed
|
|
4
|
+
from (lane, salt): the expected value is exactly ``x``, so impulses
|
|
5
|
+
smaller than half a quantum accumulate in expectation instead of dying
|
|
6
|
+
in the round-to-nearest dead zone. Insurance for physics with
|
|
7
|
+
persistently tiny kicks (cold streams, weak-force regimes); on
|
|
8
|
+
L350N512 it measured identical to plain rint.
|
|
9
|
+
|
|
10
|
+
The lane is hashed from three words of the particle's own position
|
|
11
|
+
plus the component, ``lane(x, y, z, comp)`` -- the packed q where the
|
|
12
|
+
kernel holds it (drift, root gather), the float bits of the in-cell
|
|
13
|
+
fraction in the cell-list kernels (pairs, patch, core) -- and the salt
|
|
14
|
+
names the (step, stage); never a launch counter or an array index. So
|
|
15
|
+
the draw a particle gets depends only on where it is and which kick
|
|
16
|
+
this is: the same whether the particles are kicked in one launch or
|
|
17
|
+
tile by tile, in batches or at once, which is what lets a streamed run
|
|
18
|
+
be compared bit for bit with the resident one. It relies on tile-local
|
|
19
|
+
cell lists keeping the fraction bit for bit: positions are re-indexed
|
|
20
|
+
by cell, never rescaled.
|
|
21
|
+
|
|
22
|
+
``source`` is the CUDA device functions, prepended to each kick
|
|
23
|
+
module's kernel string; the driver sets ``dither.step`` at every node
|
|
24
|
+
and each stage draws ``stage_salt(STAGE)``.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
|
|
29
|
+
source = r'''
|
|
30
|
+
__device__ inline unsigned int lane(
|
|
31
|
+
unsigned int qx, unsigned int qy, unsigned int qz, unsigned int comp)
|
|
32
|
+
{
|
|
33
|
+
return (qx * 73856093u ^ qy * 19349663u ^ qz * 83492791u) + comp;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
__device__ inline double dither(unsigned int i, unsigned int salt)
|
|
37
|
+
{
|
|
38
|
+
unsigned int x = i * 2654435761u ^ salt * 2246822519u;
|
|
39
|
+
x ^= x >> 16; x *= 2654435761u;
|
|
40
|
+
x ^= x >> 13; x *= 3266489917u;
|
|
41
|
+
x ^= x >> 16;
|
|
42
|
+
return (double)x * (1.0 / 4294967296.0);
|
|
43
|
+
}
|
|
44
|
+
'''
|
|
45
|
+
|
|
46
|
+
# Stages of one node.
|
|
47
|
+
DRIFT = 0
|
|
48
|
+
PAIRS = 1
|
|
49
|
+
PATCH = 2
|
|
50
|
+
CORE = 3
|
|
51
|
+
PM = 4
|
|
52
|
+
PAIRS_FINE = 5 # the q8/core-delta pair launch; band keeps PAIRS
|
|
53
|
+
|
|
54
|
+
step = 0 # the driver sets dither.step at every node
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def stage_salt(stage):
|
|
58
|
+
"""The salt of one kick stage at the current step."""
|
|
59
|
+
return np.uint32(8 * step + stage)
|