Skip to content
CosmologicalEmulatorsPublic

About

No description, website, or topics provided.

Resources

Stars

6 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

170 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Capse.jl

Capse

Documentation Documentation Build Status codecov arXiv

Code Style: Blue ColPrac Size

๐Ÿš€ High-Performance CMB Power Spectrum Emulation

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.

โœจ Key Features

  • โšก 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

๐Ÿ“ฆ Installation

using Pkg
Pkg.add(url="https://github.com/CosmologicalEmulators/Capse.jl")

๐ŸŽฎ Quick Start

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.

CAMB Mnu-w0-wa-CDM emulators

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.

๐Ÿ“Š Performance Benchmarks

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)

๐Ÿ”ฅ Advanced Features

Batch Processing

# Process 100 cosmologies simultaneously
params_batch = rand(6, 100)
Cโ„“_batch = Capse.get_Cโ„“(params_batch, Cโ„“_emu)

GPU Acceleration

using CUDA, Adapt

# Load and move to GPU
Cโ„“_emu = Capse.load_emulator("weights/", emu=Capse.LuxEmulator)
Cโ„“_emu_gpu = adapt(CuArray, Cโ„“_emu)

Gradient-Based Optimization

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]

๐Ÿ Python Integration

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)

๐Ÿ“š Documentation

๐ŸŽ“ Citation

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},
}

๐Ÿค Contributing

We welcome contributions! Please follow the BlueStyle coding standards and see ColPrac for collaboration guidelines.

Development Setup

using Pkg
Pkg.develop(url="https://github.com/CosmologicalEmulators/Capse.jl")
Pkg.test("Capse")

๐Ÿ‘ฅ Authors

  • 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

๐Ÿ“„ License

MIT License - see LICENSE for details.


Made with โค๏ธ by the Cosmological Emulators Team

About

No description, website, or topics provided.

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages