hpr_atmos/barometer.rs
1//! What a barometric altimeter reads in the day's air.
2
3use crate::{AtmosError, Atmosphere, Ussa76};
4
5/// A barometric altimeter on a pad, in the air of a day: it reads the standard atmosphere's
6/// altitude of the day's pressure where it is, less that at the pad, `H(p(z)) ā H(p(zā))`
7/// ([`Ussa76::pressure_altitude_m`]).
8///
9/// In the standard atmosphere itself it reads the geopotential height climbed. On a warmer day
10/// the air is thinner, pressure falls more slowly with height, and it reads less than that: in
11/// the troposphere, with the sea-level pressure unchanged, by the ratio of the standard's
12/// sea-level temperature to the day's.
13#[derive(Debug)]
14pub struct Barometer<'a> {
15 day: &'a dyn Atmosphere,
16 site_msl_m: f64,
17 standard: Ussa76,
18 pad_altitude_m: f64,
19}
20
21impl<'a> Barometer<'a> {
22 /// A barometer on a pad `pad_m` above a site `site_msl_m` above sea level, in `day`'s air.
23 ///
24 /// # Errors
25 ///
26 /// [`AtmosError`] if the day has no air at the pad.
27 pub fn on_pad(
28 day: &'a dyn Atmosphere,
29 site_msl_m: f64,
30 pad_m: f64,
31 ) -> Result<Self, AtmosError> {
32 let standard = Ussa76::standard();
33 let pad_altitude_m =
34 standard.pressure_altitude_m(day.air(site_msl_m + pad_m)?.air.pressure_pa)?;
35 Ok(Self {
36 day,
37 site_msl_m,
38 standard,
39 pad_altitude_m,
40 })
41 }
42
43 /// What it reads at `height_m` above the site, m.
44 ///
45 /// # Errors
46 ///
47 /// [`AtmosError`] if the day has no air there.
48 pub fn reading_m(&self, height_m: f64) -> Result<f64, AtmosError> {
49 let pressure_pa = self.day.air(self.site_msl_m + height_m)?.air.pressure_pa;
50 Ok(self.standard.pressure_altitude_m(pressure_pa)? - self.pad_altitude_m)
51 }
52
53 /// The height above the site at which it reads `reading_m`, m: the inverse of
54 /// [`Self::reading_m`], which turns a logged barometric altitude into the height the rocket
55 /// climbed in the day's air ([the private collection's logged apogees, ADR-184][adr-184]).
56 ///
57 /// [adr-184]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0184-logged-apogees-of-the-private-collection.md
58 ///
59 /// The reading rises with height wherever the day's pressure falls with height, so the
60 /// height is bisected between the site and the first doubling of the reading's size that
61 /// reads past it, to the resolution of `f64`.
62 ///
63 /// # Errors
64 ///
65 /// [`AtmosError::Domain`] for a reading that is not finite, below what the site
66 /// itself reads, or above what the day reads 100 km up; and what [`Self::reading_m`] raises.
67 pub fn height_m(&self, reading_m: f64) -> Result<f64, AtmosError> {
68 const TOP_M: f64 = 100_000.0;
69 let domain = || AtmosError::Domain {
70 what: "barometric reading",
71 value: reading_m,
72 };
73 if !reading_m.is_finite() {
74 return Err(domain());
75 }
76 let mut low = 0.0;
77 if self.reading_m(low)? > reading_m {
78 return Err(domain());
79 }
80 let mut high = (2.0 * reading_m.abs()).clamp(100.0, TOP_M);
81 while self.reading_m(high)? < reading_m {
82 if high >= TOP_M {
83 return Err(domain());
84 }
85 low = high;
86 high = (2.0 * high).min(TOP_M);
87 }
88 loop {
89 let middle = 0.5 * (low + high);
90 if middle <= low || middle >= high {
91 return Ok(middle);
92 }
93 if self.reading_m(middle)? < reading_m {
94 low = middle;
95 } else {
96 high = middle;
97 }
98 }
99 }
100}