use super::{arithmetic::not, evaluate::current_completion};
use crate::structures::tensor::actuator::{TensorActuatorContext, TensorActuatorState};
use crate::structures::{GateActuatorEvent, StructureError};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps, ReduceOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
fn bounds<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
state: &TensorActuatorState<R>,
time: f64,
) -> Result<(Tensor<R>, Tensor<R>), StructureError>
where
R::Client: numr::ops::BinaryOps<R> + numr::ops::CompareOps<R> + numr::ops::ConditionalOps<R>,
{
let c = &ctx.structures.client;
let now = ctx.constant(time)?;
let terminal = ctx.constant(ctx.schedule.end_s)?;
let finite = c.eq(&state.arrival.kind, &ctx.one)?;
let pending = c.mul(&finite, ¬(ctx, ¤t_completion(ctx, state)?)?)?;
let lower = c.mul(
&pending,
&c.mul(
&c.gt(&state.arrival.lower, &now)?,
&c.le(&state.arrival.lower, &terminal)?,
)?,
)?;
let upper = c.mul(
&pending,
&c.mul(
&c.gt(&state.arrival.upper, &now)?,
&c.le(&state.arrival.upper, &terminal)?,
)?,
)?;
Ok((lower, upper))
}
fn command<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
next_target: usize,
) -> (Option<f64>, f64) {
let command = ctx.schedule.target_times_s.get(next_target).copied();
let fallback = command
.unwrap_or(ctx.schedule.end_s)
.min(ctx.schedule.end_s);
(command, fallback)
}
fn earliest<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
fallback: f64,
masked: &[(&Tensor<R>, &Tensor<R>)],
time: f64,
) -> Result<f64, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
let c = &ctx.structures.client;
let base = ctx.constant(fallback)?;
let mut candidates = base.clone();
for (mask, times) in masked {
candidates = c.minimum(&candidates, &c.where_cond(mask, times, &base)?)?;
}
let time_s = c.min(&candidates, &[], false)?.item::<f64>()?;
if !time_s.is_finite() || time_s <= time {
return Err(StructureError::numerical(
"actuator next event does not strictly advance its accepted clock",
));
}
Ok(time_s)
}
pub(crate) fn event_at<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
state: &TensorActuatorState<R>,
(time, next_target): (f64, usize),
at: f64,
) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
let c = &ctx.structures.client;
let (lower, upper) = bounds(ctx, state, time)?;
let (command, _) = command(ctx, next_target);
let selected = ctx.constant(at)?;
let arrival_lower_boundary = ctx
.structures
.any(&c.mul(&lower, &c.eq(&state.arrival.lower, &selected)?)?)?;
let arrival_upper_boundary = ctx
.structures
.any(&c.mul(&upper, &c.eq(&state.arrival.upper, &selected)?)?)?;
let target_index = (command == Some(at)).then_some(next_target);
let terminal = at == ctx.schedule.end_s;
if target_index.is_none() && !arrival_lower_boundary && !arrival_upper_boundary && !terminal {
return Ok(None);
}
Ok(Some(GateActuatorEvent {
time_s: at,
target_index,
arrival_lower_boundary,
arrival_upper_boundary,
terminal,
}))
}
pub(crate) fn next_event<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
state: &TensorActuatorState<R>,
time: f64,
next_target: usize,
) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
if time == ctx.schedule.end_s {
return Ok(None);
}
let (lower, upper) = bounds(ctx, state, time)?;
let (_, fallback) = command(ctx, next_target);
let a = &state.arrival;
let time_s = earliest(
ctx,
fallback,
&[(&lower, &a.lower), (&upper, &a.upper)],
time,
)?;
event_at(ctx, state, (time, next_target), time_s)
}
pub(crate) fn next_coordinated_event<R: Runtime<DType = DType>>(
ctx: &TensorActuatorContext<R>,
state: &TensorActuatorState<R>,
time: f64,
next_target: usize,
) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
if time == ctx.schedule.end_s {
return Ok(None);
}
let (ahead, _) = bounds(ctx, state, time)?;
let (_, fallback) = command(ctx, next_target);
let a = &state.arrival;
let time_s = earliest(ctx, fallback, &[(&ahead, &a.upper)], time)?;
event_at(ctx, state, (time, next_target), time_s)
}