Skip to main content

hydr_structures/structures/culvert/barrel/
arcs.rs

1//! Constructors of the barrels built from tangent circular arcs.
2//!
3//! Each derived radius follows from tangency of neighbouring arcs. The closed forms keep every
4//! difference between positive terms:
5//!
6//! | Shape | Derived radius |
7//! | --- | --- |
8//! | Horizontal ellipse | `R = r_c + (x_c^2 + e^2) / (2 e)`, `x_c = B/2 - r_c`, `e = D/2 - r_c` |
9//! | Vertical ellipse | `r_c = r + (f^2 + e^2) / (2 f)`, `f = B/2 - r`, `e = D/2 - r` |
10//! | Pipe-arch | `r_t = r_c + (x_c^2 + g^2) / (2 g)`, `g = D - y_c - r_c`, `y_c = r_b - h` |
11//! | Flat-floor arch | `r = (B^2/4 + D^2) / (2 D)` |
12//!
13//! The pipe-arch corner centres sit at height `y_c`, with
14//! `h^2 = (r_b - B/2) (r_b + B/2 - 2 r_c)`. Its rules on `y_c` hold for the exact section:
15//! the host enclosure certifies them, so the kernel enclosures of the same forms agree.
16//!
17//! The rules and derived radii are evaluated in a [`Frame`] that scales the rise into `[1, 2)`
18//! by a power of two. Every rule is homogeneous in length, so it holds in the frame exactly when
19//! it holds in metres, and no square or product underflows or overflows there.
20
21use super::enclosure::Enclosure;
22use super::frame::Frame;
23use super::{CulvertBarrel, CulvertBarrelShape, dimension, scales};
24use crate::structures::StructureError;
25
26impl CulvertBarrel {
27    /// Admits a four-arc horizontal ellipse of span `span_m`, rise `rise_m` and side radius
28    /// `side_radius_m`.
29    ///
30    /// The invert and crown arcs share one radius, derived from tangency. Rejects a span at or
31    /// below the rise and a side radius at or above half the rise. Also rejects as the box
32    /// constructor does, and when a radius cubed, scaled by 64, overflows F64.
33    pub fn horizontal_ellipse(
34        span_m: f64,
35        rise_m: f64,
36        side_radius_m: f64,
37    ) -> Result<Self, StructureError> {
38        dimension("span_m", "span", span_m)?;
39        dimension("rise_m", "rise", rise_m)?;
40        dimension("side_radius_m", "side radius", side_radius_m)?;
41        if span_m <= rise_m {
42            return Err(StructureError::definition(format!(
43                "span_m={span_m:?},rise_m={rise_m:?}: a horizontal ellipse needs span above rise. Use vertical_ellipse for a rise above the span"
44            )));
45        }
46        if side_radius_m >= 0.5 * rise_m {
47            return Err(StructureError::definition(format!(
48                "rise_m={rise_m:?},side_radius_m={side_radius_m:?}: a horizontal ellipse needs a side radius below half the rise. Supply a smaller side radius"
49            )));
50        }
51        let fields = format!("span_m={span_m:?},rise_m={rise_m:?},side_radius_m={side_radius_m:?}");
52        scales(span_m, rise_m, &[("side", side_radius_m)], &fields)?;
53        let frame = Frame::new(rise_m, &[span_m, side_radius_m], &fields)?;
54        let radius = ellipse_radius(
55            frame.scaled(span_m),
56            frame.scaled(rise_m),
57            frame.scaled(side_radius_m),
58        );
59        scales(
60            span_m,
61            rise_m,
62            &[("invert and crown", frame.back(radius.upper))],
63            &fields,
64        )?;
65        Ok(Self::new(
66            CulvertBarrelShape::HorizontalEllipse,
67            span_m,
68            rise_m,
69            &[side_radius_m],
70        ))
71    }
72
73    /// Admits a four-arc vertical ellipse of span `span_m`, rise `rise_m` and end radius
74    /// `end_radius_m`.
75    ///
76    /// The two side arcs share one radius, derived from tangency. Rejects a rise at or below
77    /// the span and an end radius at or above half the span. Also rejects as the box
78    /// constructor does, and when a radius cubed, scaled by 64, overflows F64.
79    pub fn vertical_ellipse(
80        span_m: f64,
81        rise_m: f64,
82        end_radius_m: f64,
83    ) -> Result<Self, StructureError> {
84        dimension("span_m", "span", span_m)?;
85        dimension("rise_m", "rise", rise_m)?;
86        dimension("end_radius_m", "end radius", end_radius_m)?;
87        if rise_m <= span_m {
88            return Err(StructureError::definition(format!(
89                "span_m={span_m:?},rise_m={rise_m:?}: a vertical ellipse needs rise above span. Use horizontal_ellipse for a span above the rise"
90            )));
91        }
92        if end_radius_m >= 0.5 * span_m {
93            return Err(StructureError::definition(format!(
94                "span_m={span_m:?},end_radius_m={end_radius_m:?}: a vertical ellipse needs an end radius below half the span. Supply a smaller end radius"
95            )));
96        }
97        let fields = format!("span_m={span_m:?},rise_m={rise_m:?},end_radius_m={end_radius_m:?}");
98        scales(span_m, rise_m, &[("end", end_radius_m)], &fields)?;
99        let frame = Frame::new(rise_m, &[span_m, end_radius_m], &fields)?;
100        // The side radius is the horizontal-ellipse radius of the section turned a quarter turn.
101        let radius = ellipse_radius(
102            frame.scaled(rise_m),
103            frame.scaled(span_m),
104            frame.scaled(end_radius_m),
105        );
106        scales(
107            span_m,
108            rise_m,
109            &[("side", frame.back(radius.upper))],
110            &fields,
111        )?;
112        Ok(Self::new(
113            CulvertBarrelShape::VerticalEllipse,
114            span_m,
115            rise_m,
116            &[end_radius_m],
117        ))
118    }
119
120    /// Admits a pipe-arch of span `span_m`, rise `rise_m`, corner radius `corner_radius_m` and
121    /// bottom radius `bottom_radius_m`.
122    ///
123    /// The top radius is derived from tangency. Rejects a corner radius at or above half the
124    /// span, a bottom radius at or below half the span, a rise at or below the corner-centre
125    /// height plus the corner radius, and a rise above the corner centres of more than half
126    /// the span, which would put the widest point on the top arc. Also rejects as the box
127    /// constructor does, and when a radius cubed, scaled by 64, overflows F64.
128    pub fn pipe_arch(
129        span_m: f64,
130        rise_m: f64,
131        corner_radius_m: f64,
132        bottom_radius_m: f64,
133    ) -> Result<Self, StructureError> {
134        dimension("span_m", "span", span_m)?;
135        dimension("rise_m", "rise", rise_m)?;
136        dimension("corner_radius_m", "corner radius", corner_radius_m)?;
137        dimension("bottom_radius_m", "bottom radius", bottom_radius_m)?;
138        if corner_radius_m >= 0.5 * span_m {
139            return Err(StructureError::definition(format!(
140                "span_m={span_m:?},corner_radius_m={corner_radius_m:?}: a pipe-arch needs a corner radius below half the span. Supply a smaller corner radius"
141            )));
142        }
143        if bottom_radius_m <= 0.5 * span_m {
144            return Err(StructureError::definition(format!(
145                "span_m={span_m:?},bottom_radius_m={bottom_radius_m:?}: a pipe-arch needs a bottom radius above half the span. Supply a larger bottom radius"
146            )));
147        }
148        let fields = format!(
149            "span_m={span_m:?},rise_m={rise_m:?},corner_radius_m={corner_radius_m:?},bottom_radius_m={bottom_radius_m:?}"
150        );
151        let given = [("corner", corner_radius_m), ("bottom", bottom_radius_m)];
152        scales(span_m, rise_m, &given, &fields)?;
153        let frame = Frame::new(rise_m, &[span_m, corner_radius_m, bottom_radius_m], &fields)?;
154        let rise = frame.scaled(rise_m);
155        let corner = frame.scaled(corner_radius_m);
156        let layout = PipeArch::new(
157            frame.scaled(span_m),
158            rise,
159            corner,
160            frame.scaled(bottom_radius_m),
161        );
162        if layout.gap.lower.is_nan() || layout.gap.lower <= 0.0 {
163            let top = Enclosure::exact(corner).add(layout.corner_height);
164            let top = frame.back_enclosure(top);
165            return Err(StructureError::definition(format!(
166                "{fields}: a pipe-arch needs a rise above the corner-centre height plus the corner radius, [{:?}, {:?}] m. Supply a larger rise or a larger bottom radius",
167                top.lower, top.upper
168            )));
169        }
170        if layout.reach.lower.is_nan() || layout.reach.lower < 0.0 {
171            let above = Enclosure::exact(rise).sub(layout.corner_height);
172            let above = frame.back_enclosure(above);
173            return Err(StructureError::definition(format!(
174                "{fields}: a pipe-arch needs the rise above the corner centres, [{:?}, {:?}] m, at most half the span. Supply a smaller rise or a smaller bottom radius",
175                above.lower, above.upper
176            )));
177        }
178        let top = layout.top_radius();
179        scales(span_m, rise_m, &[("top", frame.back(top.upper))], &fields)?;
180        Ok(Self::new(
181            CulvertBarrelShape::PipeArch,
182            span_m,
183            rise_m,
184            &[corner_radius_m, bottom_radius_m],
185        ))
186    }
187
188    /// Admits a flat-floor arch of floor chord `span_m` and rise `rise_m`.
189    ///
190    /// The arc radius is derived. Rejects a rise above the span or below a quarter of the
191    /// span. Also rejects as the box constructor does.
192    pub fn arch(span_m: f64, rise_m: f64) -> Result<Self, StructureError> {
193        dimension("span_m", "span", span_m)?;
194        dimension("rise_m", "rise", rise_m)?;
195        if rise_m > span_m {
196            return Err(StructureError::definition(format!(
197                "span_m={span_m:?},rise_m={rise_m:?}: a flat-floor arch needs a rise of at most the span. Supply a rise between a quarter of the span and the span"
198            )));
199        }
200        if rise_m < 0.25 * span_m {
201            return Err(StructureError::definition(format!(
202                "span_m={span_m:?},rise_m={rise_m:?}: a flat-floor arch needs a rise of at least a quarter of the span. Supply a rise between a quarter of the span and the span"
203            )));
204        }
205        let fields = format!("span_m={span_m:?},rise_m={rise_m:?}");
206        scales(span_m, rise_m, &[], &fields)?;
207        let frame = Frame::new(rise_m, &[span_m], &fields)?;
208        let radius = arch_radius(frame.scaled(span_m), frame.scaled(rise_m));
209        scales(
210            span_m,
211            rise_m,
212            &[("arc", frame.back(radius.upper))],
213            &fields,
214        )?;
215        Ok(Self::new(CulvertBarrelShape::Arch, span_m, rise_m, &[]))
216    }
217}
218
219/// Encloses `R = r_c + (x_c^2 + e^2) / (2 e)` of a horizontal ellipse.
220fn ellipse_radius(span_m: f64, rise_m: f64, side_m: f64) -> Enclosure {
221    let half = Enclosure::exact(span_m).mul(Enclosure::exact(0.5));
222    let half_rise = Enclosure::exact(rise_m).mul(Enclosure::exact(0.5));
223    let side = Enclosure::exact(side_m);
224    let offset = half.sub(side);
225    let lift = half_rise.sub(side);
226    let sum = offset.mul(offset).add(lift.mul(lift));
227    let twice = Enclosure::exact(2.0).mul(lift);
228    side.add(sum.div(twice))
229}
230
231/// Encloses `r = (B^2/4 + D^2) / (2 D)` of a flat-floor arch.
232fn arch_radius(span_m: f64, rise_m: f64) -> Enclosure {
233    let half = Enclosure::exact(span_m).mul(Enclosure::exact(0.5));
234    let rise = Enclosure::exact(rise_m);
235    let sum = half.mul(half).add(rise.mul(rise));
236    sum.div(Enclosure::exact(2.0).mul(rise))
237}
238
239/// Pipe-arch quantities that decide admission.
240struct PipeArch {
241    offset: Enclosure,
242    corner: Enclosure,
243    /// `y_c = r_b - h`.
244    corner_height: Enclosure,
245    /// `g = D - y_c - r_c`, positive when the top arc closes above the corner arcs.
246    gap: Enclosure,
247    /// `B/2 - (D - y_c)`, nonnegative when the widest point lies on the corner arcs.
248    reach: Enclosure,
249}
250
251impl PipeArch {
252    fn new(span_m: f64, rise_m: f64, corner_m: f64, bottom_m: f64) -> Self {
253        let half = Enclosure::exact(span_m).mul(Enclosure::exact(0.5));
254        let corner = Enclosure::exact(corner_m);
255        let bottom = Enclosure::exact(bottom_m);
256        let offset = half.sub(corner);
257        let doubled = Enclosure::exact(2.0).mul(corner);
258        let lift = bottom.sub(half).mul(bottom.add(half).sub(doubled));
259        let rise_to_centre = lift.nonnegative().sqrt();
260        let corner_height = bottom.sub(rise_to_centre);
261        let above = Enclosure::exact(rise_m).sub(corner_height);
262        Self {
263            offset,
264            corner,
265            corner_height,
266            gap: above.sub(corner),
267            reach: half.sub(above),
268        }
269    }
270
271    /// `r_t = r_c + (x_c^2 + g^2) / (2 g)`.
272    fn top_radius(&self) -> Enclosure {
273        let sum = self.offset.mul(self.offset).add(self.gap.mul(self.gap));
274        let twice = Enclosure::exact(2.0).mul(self.gap);
275        self.corner.add(sum.div(twice))
276    }
277}
278
279#[cfg(test)]
280mod tests {
281    use super::*;
282    use crate::structures::StructureInputErrorKind;
283
284    fn message(result: Result<CulvertBarrel, StructureError>) -> Option<String> {
285        match result {
286            Err(StructureError::InvalidInput {
287                kind: StructureInputErrorKind::Definition,
288                context,
289                element: None,
290            }) => Some(context),
291            _ => None,
292        }
293    }
294
295    #[test]
296    fn every_rule_rejects_with_its_values_and_action() {
297        let cases = [
298            (
299                CulvertBarrel::horizontal_ellipse(1.2, 1.5, 0.3),
300                "span_m=1.2,rise_m=1.5: a horizontal ellipse needs span above rise. Use vertical_ellipse for a rise above the span",
301            ),
302            (
303                CulvertBarrel::horizontal_ellipse(2.0, 1.0, 0.5),
304                "rise_m=1.0,side_radius_m=0.5: a horizontal ellipse needs a side radius below half the rise. Supply a smaller side radius",
305            ),
306            (
307                CulvertBarrel::horizontal_ellipse(2.0, 1.0, -0.1),
308                "side_radius_m=-0.1: supply a finite positive barrel side radius in metres",
309            ),
310            (
311                CulvertBarrel::vertical_ellipse(1.5, 1.2, 0.3),
312                "span_m=1.5,rise_m=1.2: a vertical ellipse needs rise above span. Use horizontal_ellipse for a span above the rise",
313            ),
314            (
315                CulvertBarrel::vertical_ellipse(1.0, 2.0, 0.5),
316                "span_m=1.0,end_radius_m=0.5: a vertical ellipse needs an end radius below half the span. Supply a smaller end radius",
317            ),
318            (
319                CulvertBarrel::vertical_ellipse(1.0, 2.0, f64::NAN),
320                "end_radius_m=NaN: supply a finite positive barrel end radius in metres",
321            ),
322            (
323                CulvertBarrel::pipe_arch(2.0, 1.5, 1.0, 2.0),
324                "span_m=2.0,corner_radius_m=1.0: a pipe-arch needs a corner radius below half the span. Supply a smaller corner radius",
325            ),
326            (
327                CulvertBarrel::pipe_arch(2.0, 1.5, 0.5, 1.0),
328                "span_m=2.0,bottom_radius_m=1.0: a pipe-arch needs a bottom radius above half the span. Supply a larger bottom radius",
329            ),
330            (
331                CulvertBarrel::pipe_arch(2.0, 1.0, 0.5, 1.01),
332                "span_m=2.0,rise_m=1.0,corner_radius_m=0.5,bottom_radius_m=1.01: a pipe-arch needs a rise above the corner-centre height plus the corner radius",
333            ),
334            (
335                CulvertBarrel::pipe_arch(2.0, 1.8, 0.5, 2.0),
336                "span_m=2.0,rise_m=1.8,corner_radius_m=0.5,bottom_radius_m=2.0: a pipe-arch needs the rise above the corner centres",
337            ),
338            (
339                CulvertBarrel::pipe_arch(2.0, 1.5, 0.0, 2.0),
340                "corner_radius_m=0.0: supply a finite positive barrel corner radius in metres",
341            ),
342            (
343                CulvertBarrel::pipe_arch(2.0, 1.5, 0.5, f64::INFINITY),
344                "bottom_radius_m=inf: supply a finite positive barrel bottom radius in metres",
345            ),
346            (
347                CulvertBarrel::arch(2.0, 2.5),
348                "span_m=2.0,rise_m=2.5: a flat-floor arch needs a rise of at most the span. Supply a rise between a quarter of the span and the span",
349            ),
350            (
351                CulvertBarrel::arch(2.0, 0.4),
352                "span_m=2.0,rise_m=0.4: a flat-floor arch needs a rise of at least a quarter of the span. Supply a rise between a quarter of the span and the span",
353            ),
354            (
355                CulvertBarrel::arch(-2.0, 0.4),
356                "span_m=-2.0: supply a finite positive barrel span in metres",
357            ),
358        ];
359        for (result, expected) in cases {
360            let text = message(result);
361            assert!(
362                text.as_deref().is_some_and(|t| t.contains(expected)),
363                "{text:?} lacks {expected:?}"
364            );
365        }
366    }
367
368    #[test]
369    fn radius_and_scale_overflow_reject() {
370        let cases = [
371            (
372                CulvertBarrel::pipe_arch(2.0, 1.5, 0.5, 1e110),
373                "the bottom radius cubed times 64 overflows F64",
374            ),
375            (
376                CulvertBarrel::horizontal_ellipse(1e100, 1e-100, 4e-101),
377                "the invert and crown radius cubed times 64 overflows F64",
378            ),
379            (
380                CulvertBarrel::vertical_ellipse(1e-100, 1e100, 4e-101),
381                "the side radius cubed times 64 overflows F64",
382            ),
383            (
384                CulvertBarrel::arch(1e300, 1e300),
385                "B D times 64 overflows F64",
386            ),
387            (
388                CulvertBarrel::arch(1e-300, 1e-300),
389                "rise times 2^-30 is subnormal",
390            ),
391        ];
392        for (result, expected) in cases {
393            let text = message(result);
394            assert!(
395                text.as_deref().is_some_and(|t| t.contains(expected)),
396                "{text:?} lacks {expected:?}"
397            );
398        }
399    }
400
401    #[test]
402    fn rule_limits_admit_the_nearest_valid_dimensions() -> Result<(), StructureError> {
403        // Exact comparisons: the nearest F64 inside each limit admits.
404        CulvertBarrel::horizontal_ellipse(1.0_f64.next_up(), 1.0, 0.25)?;
405        CulvertBarrel::horizontal_ellipse(2.0, 1.0, 0.5_f64.next_down())?;
406        CulvertBarrel::vertical_ellipse(1.0, 1.0_f64.next_up(), 0.25)?;
407        CulvertBarrel::vertical_ellipse(1.0, 2.0, 0.5_f64.next_down())?;
408        // A bottom radius just above half the span lifts the corner centres to B/2.
409        CulvertBarrel::pipe_arch(2.0, 1.8, 0.5, 1.0_f64.next_up())?;
410        CulvertBarrel::arch(2.0, 2.0)?;
411        CulvertBarrel::arch(2.0, 0.5)?;
412        Ok(())
413    }
414
415    /// Frozen criterion: every evidence barrel scaled by `2^-900` or `2^300` admits, and each
416    /// rule rejects at those scales as it does in metres.
417    #[test]
418    fn admission_holds_at_every_representable_scale() {
419        type Build = fn(f64) -> Result<CulvertBarrel, StructureError>;
420        let admitted: [Build; 4] = [
421            |k| CulvertBarrel::horizontal_ellipse(1.524 * k, 0.9652 * k, 0.419_475_92 * k),
422            |k| CulvertBarrel::vertical_ellipse(0.9652 * k, 1.524 * k, 0.419_475_92 * k),
423            |k| CulvertBarrel::pipe_arch(1.8542 * k, 1.397 * k, 0.4572 * k, 1.938_528 * k),
424            |k| CulvertBarrel::arch(3.048 * k, 1.6002 * k),
425        ];
426        let rejected: [(Build, &str); 2] = [
427            (
428                |k| CulvertBarrel::pipe_arch(2.0 * k, 1.0 * k, 0.5 * k, 1.01 * k),
429                "a pipe-arch needs a rise above the corner-centre height plus the corner radius",
430            ),
431            (
432                |k| CulvertBarrel::pipe_arch(2.0 * k, 1.8 * k, 0.5 * k, 2.0 * k),
433                "a pipe-arch needs the rise above the corner centres",
434            ),
435        ];
436        for scale in [2f64.powi(-900), 1.0, 2f64.powi(300)] {
437            for (shape, build) in admitted.iter().enumerate() {
438                let barrel = build(scale);
439                assert!(barrel.is_ok(), "shape {shape} at {scale:e}: {barrel:?}");
440            }
441            for (build, expected) in rejected {
442                let text = message(build(scale));
443                assert!(
444                    text.as_deref().is_some_and(|t| t.contains(expected)),
445                    "{text:?} lacks {expected:?} at {scale:e}"
446                );
447            }
448        }
449        // Dimensions too far apart to share one frame reject with an action.
450        let text = message(CulvertBarrel::vertical_ellipse(1e-300, 1e300, 1e-301));
451        let expected = "the dimensions differ in size by more than F64 can scale to a unit rise. Supply dimensions closer in size";
452        assert!(
453            text.as_deref().is_some_and(|t| t.contains(expected)),
454            "{text:?} lacks {expected:?}"
455        );
456    }
457
458    #[test]
459    fn admitted_arc_barrels_report_their_inputs() -> Result<(), StructureError> {
460        let ellipse = CulvertBarrel::horizontal_ellipse(1.524, 0.9652, 0.419_475_92)?;
461        assert_eq!(ellipse.shape(), CulvertBarrelShape::HorizontalEllipse);
462        assert_eq!((ellipse.span_m(), ellipse.rise_m()), (1.524, 0.9652));
463        assert_eq!(ellipse.given_radius_m(), Some(&[0.419_475_92][..]));
464        let turned = CulvertBarrel::vertical_ellipse(0.9652, 1.524, 0.419_475_92)?;
465        assert_eq!(turned.shape(), CulvertBarrelShape::VerticalEllipse);
466        assert_eq!(turned.given_radius_m(), Some(&[0.419_475_92][..]));
467        let pipe = CulvertBarrel::pipe_arch(1.8542, 1.397, 0.4572, 1.938_528)?;
468        assert_eq!(pipe.shape(), CulvertBarrelShape::PipeArch);
469        assert_eq!(pipe.given_radius_m(), Some(&[0.4572, 1.938_528][..]));
470        let arch = CulvertBarrel::arch(3.048, 1.6002)?;
471        assert_eq!(arch.shape(), CulvertBarrelShape::Arch);
472        assert_eq!((arch.span_m(), arch.rise_m()), (3.048, 1.6002));
473        assert_eq!(arch.given_radius_m(), None);
474        for barrel in [ellipse, turned, pipe, arch] {
475            assert!(barrel.shape().arced());
476        }
477        Ok(())
478    }
479
480    #[test]
481    fn pipe_arch_rules_certify_the_aisi_layout() {
482        // AISI Table 1.9 row 1 in metres: corner centres near 21 in above the invert.
483        let layout = PipeArch::new(1.8542, 1.397, 0.4572, 1.938_528);
484        let inches = 0.0254;
485        assert!(layout.corner_height.lower > 20.9 * inches);
486        assert!(layout.corner_height.upper < 21.1 * inches);
487        assert!(layout.gap.lower > 0.0);
488        assert!(layout.reach.lower > 0.0);
489        let top = layout.top_radius();
490        assert!(top.lower > 36.0 * inches && top.upper < 37.5 * inches);
491    }
492}