Crates.io | cosmology |
lib.rs | cosmology |
version | 0.3.2 |
source | src |
created_at | 2022-06-06 02:52:51.757189 |
updated_at | 2022-11-17 02:26:22.678895 |
description | An early-in-development crate aiming to provide a lot of utilities commonly used in cosmology. |
homepage | |
repository | https://github.com/cavemanloverboy/cosmology |
max_upload_size | |
id | 600453 |
size | 7,757,273 |
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).use cosmology::scale_factor::{CosmologicalParameters, ScaleFactor, LITTLE_H_TO_BIG_H};
// Specify cosmological parameters
let params = CosmologicalParameters {
omega_m0: 1.0,
omega_de0: 0.0,
omega_r0: 0.0,
omega_k0: 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 * LITTLE_H_TO_BIG_H;
let age_of_universe = 2.0 / 3.0 / hubble;
let expected = |t: f64| (t / age_of_universe).powf(2.0 / 3.0);
// Initialize ScaleFactor
let mut scale_factor = ScaleFactor::new(
params,
z0,
max_dloga,
// Obtained via inverting the expected relationship
Some(age_of_universe * a0.powf(3.0 / 2.0)),
);
// On-the-fly
// Initialize vectors which collect values
let mut a = vec![scale_factor.get_a()];
let mut dadt = vec![scale_factor.get_dadt()];
let mut t = vec![scale_factor.get_time()];
let dt = 100.0;
let mut a_expected = vec![scale_factor.get_a()];
while a.last().unwrap() < &1.0 {
// Evolve scale factor
scale_factor.step_forward(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 avg_diff = a
.iter()
.zip(a_expected)
.map(|(&actual, expected)| (actual - expected).abs())
.sum::<f64>()
/ a.len() as f64;
// If --nocapture, print value
println!("avg_diff for matter-only universe {avg_diff:.2e}");
// Check that the avg of the L1 loss between the calculated values and the expected values
// is under this threshold
const ERROR_TOLERANCE: f64 = 1e-10;
assert!(avg_diff < 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.