Capse.jl is a Julia package for ultra-fast emulation of Cosmic Microwave Background (CMB) Angular Power Spectra. Capse.jl delivers cosmological computations 1,000,000ร faster than traditional Boltzmann codes while maintaining research-grade accuracy.
- โก Lightning Fast: ~45 microseconds per evaluation (vs ~ seconds for CAMB/CLASS)
- ๐ง Flexible Backends: CPU-optimized (
SimpleChains.jl) and GPU-ready (Lux.jl) - โป๏ธ Auto-Differentiable: Full gradient support for modern inference pipelines
- ๐ Python Compatible: Seamless integration via
jaxcapse
using Pkg
Pkg.add(url="https://github.com/CosmologicalEmulators/Capse.jl")using Capse
# Load pre-trained emulator (weights available on Zenodo)
Cโ_emu = Capse.load_emulator("path/to/weights/")
# Set cosmological parameters
# [ฯb, ฯc, h, ns, ฯ, As] - check ordering with get_emulator_description()
params = [0.02237, 0.1200, 0.6736, 0.9649, 0.0544, 2.042e-9]
# Compute power spectrum in microseconds!
Cโ = Capse.get_Cโ(params, Cโ_emu)
# get_Cโ transparently interpolates subsampled emulators when appropriate.
โ_values = Capse.get_โgrid(Cโ_emu) # Always matches Cโ
โ_training = Capse.get_training_โgrid(Cโ_emu)For automatically interpolated grids, source bounds within 0.1 of an integer
are snapped to that integer. Bounds farther away are moved inward.
The five CAMB + CosmoRec models
(TT, TE, EE, BB, PP) are available in
Capse.trained_emulators["CAMB_MNUW0WACDM"]:
params = [3.044, 0.965, 0.054, 67.4, 0.02237, 0.120, 0.06, -1.0, 0.0]
tt = Capse.trained_emulators["CAMB_MNUW0WACDM"]["TT"]
Dโ_TT = Capse.get_Cโ(params, tt)
@assert Capse.get_โgrid(tt) == collect(2:9500)The input order is ln10As, ns, tau, H0, omega_b, omega_c, Mnu, w0, wa, with
w0 + wa < -0.5. These models were not trained at exactly Mnu = 0.
l.npy must contain exactly one multipole per neural-network output; loading a
grid/output length mismatch throws ArgumentError. Older artifacts with a
larger stored grid must correct l.npy to their actual output grid before loading.
Despite the method name, the CMB predictions are lensed Dโ in ฮผKยฒ, not Cโ;
PP returns [โ(โ+1)]ยฒ Cโแต แต /(2ฯ) (dimensionless). For Mooncake reverse-mode
inference, load the desired component from the installed artifact with
emu=Capse.LuxEmulator rather than the default SimpleChains backend.
Resolve the artifact through Capse's manifest. Calling Capse.artifact"..."
from Main searches for an artifact manifest belonging to Main, not Capse:
using Artifacts, Capse
manifest = joinpath(pkgdir(Capse), "Artifacts.toml")
tree = artifact_hash("CAMB_MNUW0WACDM", manifest)
isnothing(tree) && error("CAMB_MNUW0WACDM artifact is not bound")
tt = Capse.load_emulator(joinpath(artifact_path(tree), "TT"); emu=Capse.LuxEmulator)Bundled models always use their registered postprocessing_name. The archived
postprocessing.jl files are retained for provenance, not as a selectable
fallback. For user-supplied legacy directories without a named postprocessor,
load_emulator still includes the Julia file; ln10As_index and tau_index
apply only to named postprocessing.
| Method | Time per Evaluation | Speedup |
|---|---|---|
CAMB (high accuracy) |
~60 seconds | 1ร |
Capse.jl |
~45 ฮผs | ~1,300,000ร |
Benchmarks on Intel Core i7-13700H (single core)
# Process 100 cosmologies simultaneously
params_batch = rand(6, 100)
Cโ_batch = Capse.get_Cโ(params_batch, Cโ_emu)using CUDA, Adapt
# Load and move to GPU
Cโ_emu = Capse.load_emulator("weights/", emu=Capse.LuxEmulator)
Cโ_emu_gpu = adapt(CuArray, Cโ_emu)using Zygote
function loss(params)
Cโ_theory = Capse.get_Cโ(params, Cโ_emu)
return sum((Cโ_theory - Cโ_observed).^2)
end
grad = gradient(loss, params)[1]Use Capse.jl seamlessly from Python:
import jaxcapse
import jax.numpy as jnp
# Access the bundled TT emulator using its nine-parameter input order.
emu = jaxcapse.trained_emulators["camb_mnuw0wacdm"]["TT"]
params = jnp.array([3.044, 0.965, 0.054, 67.4, 0.02237, 0.12, 0.06, -1.0, 0.0])
cl = emu.get_Cl(params)- Stable Documentation - Latest release
- Development Documentation - Current development branch
- API Reference - Detailed function documentation
If you use Capse.jl in your research, please cite our release paper:
@article{Bonici2024Capse,
author = {Bonici, Marco and Bianchini, Federico and Ruiz-Zapatero, Jaime},
journal = {The Open Journal of Astrophysics},
doi = {10.21105/astro.2307.14339},
year = {2024},
month = {jan 30},
publisher = {Maynooth Academic Publishing},
title = {Capse.jl: efficient and auto-differentiable {CMB} power spectra emulation},
volume = {7},
}We welcome contributions! Please follow the BlueStyle coding standards and see ColPrac for collaboration guidelines.
using Pkg
Pkg.develop(url="https://github.com/CosmologicalEmulators/Capse.jl")
Pkg.test("Capse")- Marco Bonici - Waterloo Center for Astrophysics
- Federico Bianchini - Kavli Institute for Particle Physics and Cosmology
- Jaime Ruiz-Zapatero - Advanced Research Computing Centre, UCL
- Marius Millea - UC Davis & Berkeley Center for Cosmological Physics
MIT License - see LICENSE for details.
