cosmology: a rust crate for cosmology

An early-in-development crate intended to eventually include lots of utilities commonly used in cosmology, including

Current Features

Planned Features

Sample Usage

```rust use cosmology::scalefactor::{CosmologicalParameters, ScaleFactor, LITTLEHTOBIG_H};

// Specify cosmological parameters let params = CosmologicalParameters { omegam0: 1.0, omegade0: 0.0, omegar0: 0.0, omegak0: 0.0, w: 1.0, h: 0.7, };

// Specify initial redshift let z0 = 9.0; let a0 = 1.0 / (1.0 + z0);

// Specify max dloga let max_dloga = 0.01;

// Expectation (analytic solution of the Friedmann equation) // For a matter dominated universe, this is a(t) = (t / (2/3/H0))^(2/3) let hubble = 0.7 * LITTLEHTOBIGH; let ageofuniverse = 2.0 / 3.0 / hubble; let expected = |t: f64| (t / ageofuniverse).powf(2.0 / 3.0);

// Initialize ScaleFactor let mut scalefactor = ScaleFactor::new( params, z0, maxdloga, // Obtained via inverting the expected relationship Some(ageofuniverse * a0.powf(3.0 / 2.0)), );

// On-the-fly // Initialize vectors which collect values let mut a = vec![scalefactor.geta()]; let mut dadt = vec![scalefactor.getdadt()]; let mut t = vec![scalefactor.gettime()]; let dt = 100.0; let mut aexpected = vec![scalefactor.get_a()];

while a.last().unwrap() < &1.0 { // Evolve scale factor scalefactor.stepforward(dt);

// Add (t, a) to vec
t.push(scale_factor.get_time());
a.push(scale_factor.get_a());
dadt.push(scale_factor.get_dadt());

// Here you can do something that requires a, dadt, at your specified time t.
// do_something(&a, &dadt);

// Calculate expected value
a_expected.push(expected(*t.last().unwrap()));

}

// Calculate the avg of L1 loss between the calculated values and the expected values let avgdiff = a .iter() .zip(aexpected) .map(|(&actual, expected)| (actual - expected).abs()) .sum::() / a.len() as f64;

// If --nocapture, print value println!("avgdiff for matter-only universe {avgdiff:.2e}");

// Check that the avg of the L1 loss between the calculated values and the expected values // is under this threshold const ERRORTOLERANCE: f64 = 1e-10; assert!(avgdiff < ERROR_TOLERANCE); ```

I work on a lot of things including non-equilibrium fluid dynamics, cosmology (large scale structure and scalar field dark matter), machine learning, distributed systems, smart contracts, (financial) derivatives. I am a Rust zealot and was surprised to see that there is a gap to be filled in the astrophysics/cosmology crate space. This is a crate that I am looking forward to developing.

I first began to look for a crate like this one because I needed to solve the Friedmann equations for a cosmological scalar field solver I've been working on. I found no such crate. So, here I am building it. This is presently in a very early stage but I look very forward to adding features over time. Feel free to submit PRs or suggestions for new features.