use super::inputs::ArcInputs;
use crate::structures::numerics::culvert::{Arithmetic, BarrelClient, choose};
use numr::dtype::DType;
use numr::ops::BinaryOps;
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval};
pub(super) struct ThreeRadius<R: Runtime<DType = DType>> {
pub(super) rise: Interval<R>,
pub(super) offset: Interval<R>,
pub(super) height: Interval<R>,
pub(super) bottom: Interval<R>,
pub(super) corner: Interval<R>,
pub(super) top: Interval<R>,
pub(super) sin1: Interval<R>,
pub(super) cos1: Interval<R>,
pub(super) sin2: Interval<R>,
pub(super) minus_cos2: Interval<R>,
}
pub(super) struct FlatArch<R: Runtime<DType = DType>> {
pub(super) span: Interval<R>,
pub(super) rise: Interval<R>,
pub(super) radius: Interval<R>,
pub(super) sin0: Interval<R>,
pub(super) cap_cos: Interval<R>,
}
fn constant<R>(
a: &mut Arithmetic<'_, R>,
like: &Tensor<R>,
value: f64,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let tensor = Tensor::<R>::full_scalar(like.shape(), DType::F64, value, like.device())?;
a.point(&tensor)
}
pub(super) fn copy<R>(a: &mut Arithmetic<'_, R>, x: &Interval<R>) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
a.bounds(x.lower(), x.upper())
}
pub(super) fn nonnegative<R>(
a: &mut Arithmetic<'_, R>,
x: &Interval<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let zero = Tensor::<R>::zeros(x.lower().shape(), DType::F64, x.lower().device())?;
let lower = c.maximum(x.lower(), &zero)?;
let upper = c.maximum(x.upper(), &zero)?;
a.bounds(&lower, &upper)
}
fn tangency<R>(
a: &mut Arithmetic<'_, R>,
x: &Interval<R>,
y: &Interval<R>,
two: &Interval<R>,
) -> ArithmeticResult<Interval<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let xx = a.mul(x, x)?;
let yy = a.mul(y, y)?;
let sum = a.add(&xx, &yy)?;
let twice = a.mul(two, y)?;
a.div_positive_interval(&sum, &twice)
}
fn pipe_arch<R>(
a: &mut Arithmetic<'_, R>,
[b, d, rc, rb]: &[Tensor<R>; 4],
) -> ArithmeticResult<ThreeRadius<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let point_half = constant(a, b, 0.5)?;
let two = constant(a, b, 2.0)?;
let span = a.point(b)?;
let rise = a.point(d)?;
let corner = a.point(rc)?;
let bottom = a.point(rb)?;
let half = a.mul(&span, &point_half)?;
let offset = a.sub(&half, &corner)?;
let doubled = a.mul(&two, &corner)?;
let below = a.sub(&bottom, &half)?;
let wide = a.add(&bottom, &half)?;
let beside = a.sub(&wide, &doubled)?;
let lift = a.mul(&below, &beside)?;
let lift = nonnegative(a, &lift)?;
let rise_to_centre = a.sqrt_nonnegative(&lift)?;
let height = a.sub(&bottom, &rise_to_centre)?;
let above = a.sub(&rise, &height)?;
let gap = a.sub(&above, &corner)?;
let reach = a.sub(&half, &above)?;
let reach = nonnegative(a, &reach)?;
let spread = tangency(a, &offset, &gap, &two)?;
let top = a.add(&corner, &spread)?;
let inner = a.sub(&bottom, &corner)?;
let sin1 = a.div_positive_interval(&offset, &inner)?;
let cos1 = a.div_positive_interval(&rise_to_centre, &inner)?;
let sin2 = a.div_positive_interval(&offset, &spread)?;
let widened = a.add(&gap, &offset)?;
let product = a.mul(&reach, &widened)?;
let twice = a.mul(&two, &gap)?;
let overhang = a.div_positive_interval(&product, &twice)?;
let minus_cos2 = a.div_positive_interval(&overhang, &spread)?;
Ok(ThreeRadius {
rise,
offset,
height,
bottom,
corner,
top,
sin1,
cos1,
sin2,
minus_cos2,
})
}
fn horizontal<R>(
a: &mut Arithmetic<'_, R>,
[b, d, rc]: &[Tensor<R>; 3],
) -> ArithmeticResult<ThreeRadius<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let point_half = constant(a, b, 0.5)?;
let two = constant(a, b, 2.0)?;
let span = a.point(b)?;
let rise = a.point(d)?;
let corner = a.point(rc)?;
let half = a.mul(&span, &point_half)?;
let half_rise = a.mul(&rise, &point_half)?;
let offset = a.sub(&half, &corner)?;
let lift = a.sub(&half_rise, &corner)?;
let spread = tangency(a, &offset, &lift, &two)?;
let radius = a.add(&corner, &spread)?;
let narrow = a.sub(&half, &half_rise)?;
let sum = a.add(&half, &half_rise)?;
let doubled = a.mul(&two, &corner)?;
let wide = a.sub(&sum, &doubled)?;
let product = a.mul(&narrow, &wide)?;
let twice = a.mul(&two, &lift)?;
let rise_to_centre = a.div_positive_interval(&product, &twice)?;
let sin1 = a.div_positive_interval(&offset, &spread)?;
let cos1 = a.div_positive_interval(&rise_to_centre, &spread)?;
Ok(ThreeRadius {
rise,
offset,
height: half_rise,
bottom: copy(a, &radius)?,
corner,
top: radius,
sin2: copy(a, &sin1)?,
minus_cos2: copy(a, &cos1)?,
sin1,
cos1,
})
}
fn vertical<R>(
a: &mut Arithmetic<'_, R>,
[b, d, r]: &[Tensor<R>; 3],
) -> ArithmeticResult<ThreeRadius<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let point_half = constant(a, b, 0.5)?;
let two = constant(a, b, 2.0)?;
let zero = constant(a, b, 0.0)?;
let span = a.point(b)?;
let rise = a.point(d)?;
let end = a.point(r)?;
let half = a.mul(&span, &point_half)?;
let half_rise = a.mul(&rise, &point_half)?;
let offset_turned = a.sub(&half_rise, &end)?;
let lift_turned = a.sub(&half, &end)?;
let spread = tangency(a, &offset_turned, &lift_turned, &two)?;
let corner = a.add(&end, &spread)?;
let narrow = a.sub(&half_rise, &half)?;
let sum = a.add(&half_rise, &half)?;
let doubled = a.mul(&two, &end)?;
let wide = a.sub(&sum, &doubled)?;
let product = a.mul(&narrow, &wide)?;
let twice = a.mul(&two, &lift_turned)?;
let minus_offset = a.div_positive_interval(&product, &twice)?;
let offset = a.sub(&zero, &minus_offset)?;
let sin1 = a.div_positive_interval(&minus_offset, &spread)?;
let cos1 = a.div_positive_interval(&offset_turned, &spread)?;
Ok(ThreeRadius {
rise,
offset,
height: half_rise,
bottom: copy(a, &end)?,
corner,
top: end,
sin2: copy(a, &sin1)?,
minus_cos2: copy(a, &cos1)?,
sin1,
cos1,
})
}
pub(super) fn three_radius<R>(
a: &mut Arithmetic<'_, R>,
inputs: &ArcInputs<R>,
) -> ArithmeticResult<ThreeRadius<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let pipe = pipe_arch(a, &inputs.pipe_arch_operands)?;
let wide = horizontal(a, &inputs.horizontal_operands)?;
let tall = vertical(a, &inputs.vertical_operands)?;
let mut pick = |p: &Interval<R>, h: &Interval<R>, v: &Interval<R>| {
let ellipse = choose(a, &inputs.horizontal, h, v)?;
choose(a, &inputs.pipe_arch, p, &ellipse)
};
Ok(ThreeRadius {
rise: pick(&pipe.rise, &wide.rise, &tall.rise)?,
offset: pick(&pipe.offset, &wide.offset, &tall.offset)?,
height: pick(&pipe.height, &wide.height, &tall.height)?,
bottom: pick(&pipe.bottom, &wide.bottom, &tall.bottom)?,
corner: pick(&pipe.corner, &wide.corner, &tall.corner)?,
top: pick(&pipe.top, &wide.top, &tall.top)?,
sin1: pick(&pipe.sin1, &wide.sin1, &tall.sin1)?,
cos1: pick(&pipe.cos1, &wide.cos1, &tall.cos1)?,
sin2: pick(&pipe.sin2, &wide.sin2, &tall.sin2)?,
minus_cos2: pick(&pipe.minus_cos2, &wide.minus_cos2, &tall.minus_cos2)?,
})
}
pub(super) fn flat_arch<R>(
a: &mut Arithmetic<'_, R>,
[b, d]: &[Tensor<R>; 2],
) -> ArithmeticResult<FlatArch<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let point_half = constant(a, b, 0.5)?;
let two = constant(a, b, 2.0)?;
let span = a.point(b)?;
let rise = a.point(d)?;
let half = a.mul(&span, &point_half)?;
let half_square = a.mul(&half, &half)?;
let rise_square = a.mul(&rise, &rise)?;
let sum = a.add(&half_square, &rise_square)?;
let twice = a.mul(&two, &rise)?;
let radius = a.div_positive_interval(&sum, &twice)?;
let area = a.mul(&span, &rise)?;
let sin0 = a.div_positive_interval(&area, &sum)?;
let narrow = a.sub(&half, &rise)?;
let wide = a.add(&half, &rise)?;
let product = a.mul(&narrow, &wide)?;
let cap_cos = a.div_positive_interval(&product, &sum)?;
Ok(FlatArch {
span,
rise,
radius,
sin0,
cap_cos,
})
}