1use super::enclosure::Enclosure;
22use super::frame::Frame;
23use super::{CulvertBarrel, CulvertBarrelShape, dimension, scales};
24use crate::structures::StructureError;
25
26impl CulvertBarrel {
27 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 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 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 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 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
219fn 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
231fn 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
239struct PipeArch {
241 offset: Enclosure,
242 corner: Enclosure,
243 corner_height: Enclosure,
245 gap: Enclosure,
247 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 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 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 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 #[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 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 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}