capra_core/common/
mod.rs

1//! Commonly used items for decompression models and dive planning
2
3use num_traits::cast::FromPrimitive;
4use std::isize;
5use time::Duration;
6
7/// A default, placeholder descent rate (measured in m min^-1).
8pub const DEFAULT_DESCENT_RATE: isize = 30;
9
10/// A default, placeholder ascent rate (measured in m min^-1). This is the maximum rate recommended by major instruction agencies.
11pub const DEFAULT_ASCENT_RATE: isize = -18;
12
13/// Density of fresh water (measured in kg m^-3).
14pub const DENSITY_FRESHWATER: f64 = 997.0;
15
16/// Average density of salt water (measured in kg m^-3).
17pub const DENSITY_SALTWATER: f64 = 1023.6;
18
19pub mod dive_segment;
20pub mod gas;
21pub mod otu;
22pub mod tank;
23
24pub use dive_segment::DiveSegment;
25pub use dive_segment::DiveSegmentError;
26pub use dive_segment::SegmentType;
27
28pub use gas::Gas;
29pub use gas::GasError;
30
31pub use tank::Tank;
32
33/// Helper function to convert pressure to the equivalent depth of water that would induce it.
34/// # Arguments
35/// * `bar` - Pressure measured in bars
36/// * `metres_per_bar` - Depth of water required to induce 1 bar of pressure
37pub fn bar_mtr(bar: f64, metres_per_bar: f64) -> f64 {
38    (bar - 1.0) * metres_per_bar
39}
40
41/// Helper function to convert a depth of water to the pressure it will induce.
42/// # Arguments
43/// * `mtr` - Depth of water.
44/// * `metres_per_bar` - Depth of water required to induce 1 bar of pressure.
45pub fn mtr_bar(mtr: f64, metres_per_bar: f64) -> f64 {
46    (mtr / metres_per_bar) + 1.0
47}
48
49/// Helper function to calculate the time taken to change depths, given a rate.
50/// # Arguments
51/// * `rate` - Rate of depth change
52/// * `depth_1` - First depth
53/// * `depth_2` - Second depth
54/// # Panics
55/// Function will panic if the time taken exceeds [`i64::MAX`].
56pub fn time_taken(rate: isize, depth_1: usize, depth_2: usize) -> Duration {
57    let delta_depth = ((depth_1 as isize) - (depth_2 as isize)).abs();
58    let rate_seconds = rate.abs() as f64 / 60.0;
59    Duration::seconds(
60        i64::from_f64(delta_depth as f64 / rate_seconds).expect("overflow in time taken"),
61    )
62}