An early-in-development crate intended to eventually include lots of utilities commonly used in cosmology, including
dt
and you are able to step_forward(dt)
. Useful alongside another algorithm (e.g. nbody or fluid code) where timesteps are not known in advance.dt
spacing and a final time or scale_factor and get a Box<[T]>
time series for a
and dadt
. Useful when evaluating on a time grid known in advance.P(k)
and correlation function ΞΎ(r)
using transfer function from (Eisenstein & Hu '98).```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::
// 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.