autd3_core/environment/
mod.rs

1use core::f32::consts::PI;
2
3use crate::common::{METER, ULTRASOUND_FREQ};
4
5#[cfg(not(feature = "std"))]
6use num_traits::float::Float;
7
8#[non_exhaustive]
9#[repr(C)]
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Environment {
12    pub sound_speed: f32,
13}
14
15impl Environment {
16    /// Creates a new environment with the default sound speed (340m/s).
17    pub fn new() -> Self {
18        Self {
19            sound_speed: 340.0 * METER,
20        }
21    }
22
23    /// Sets the sound speed of envs from the temperature.
24    ///
25    /// This is equivalent to `Self::set_sound_speed_from_temp_with(t, 1.4, 8.314_463, 28.9647e-3)`.
26    pub fn set_sound_speed_from_temp(&mut self, t: f32) {
27        self.set_sound_speed_from_temp_with(t, 1.4, 8.314_463, 28.9647e-3);
28    }
29
30    /// Sets the sound speed of envs from the temperature `t`, heat capacity ratio `k`, gas constant `r`, and molar mass `m` [kg/mol].
31    pub fn set_sound_speed_from_temp_with(&mut self, t: f32, k: f32, r: f32, m: f32) {
32        self.sound_speed = (k * r * (273.15 + t) / m).sqrt() * METER;
33    }
34
35    /// Gets the wavelength of the ultrasound.
36    #[must_use]
37    pub const fn wavelength(&self) -> f32 {
38        self.sound_speed / ULTRASOUND_FREQ.hz() as f32
39    }
40
41    /// Gets the wavenumber of the ultrasound.
42    #[must_use]
43    pub const fn wavenumber(&self) -> f32 {
44        2.0 * PI * ULTRASOUND_FREQ.hz() as f32 / self.sound_speed
45    }
46}
47
48impl Default for Environment {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54#[cfg(test)]
55pub(crate) mod tests {
56    use super::*;
57    use crate::common::mm;
58
59    #[rstest::rstest]
60    #[case(340.29525e3, 15.)]
61    #[case(343.23497e3, 20.)]
62    #[case(349.04013e3, 30.)]
63    fn set_sound_speed_from_temp(#[case] expected: f32, #[case] temp: f32) {
64        let mut env = Environment::new();
65        env.set_sound_speed_from_temp(temp);
66        approx::assert_abs_diff_eq!(expected * mm, env.sound_speed, epsilon = 1e-3);
67    }
68
69    #[rstest::rstest]
70    #[case(8.5, 340e3)]
71    #[case(10., 400e3)]
72    fn wavelength(#[case] expect: f32, #[case] c: f32) {
73        let mut env = Environment::new();
74        env.sound_speed = c;
75        approx::assert_abs_diff_eq!(expect, env.wavelength());
76    }
77
78    #[rstest::rstest]
79    #[case(0.739_198_27, 340e3)]
80    #[case(0.628_318_55, 400e3)]
81    fn wavenumber(#[case] expect: f32, #[case] c: f32) {
82        let mut env = Environment::new();
83        env.sound_speed = c;
84        approx::assert_abs_diff_eq!(expect, env.wavenumber());
85    }
86}