use super::{GateActuatorBatch, GateActuatorEvent, GateActuatorOutput};
use crate::structures::numerics::actuator::{TensorActuatorLeft, left};
use crate::structures::{StructureError, StructureInputErrorKind};
use numr::dtype::DType;
use numr::runtime::Runtime;
use std::sync::Arc;
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct GateActuatorPreparedInterval<R: Runtime<DType = DType>> {
pub(crate) base: GateActuatorBatch<R>,
pub(crate) left: TensorActuatorLeft<R>,
pub(crate) frame: GateActuatorBatch<R>,
pub(crate) next_target: usize,
}
impl<R: Runtime<DType = DType>> GateActuatorPreparedInterval<R> {
#[doc(hidden)]
pub fn start_s(&self) -> f64 {
self.left.start
}
#[doc(hidden)]
pub fn end_s(&self) -> f64 {
self.left.end
}
#[doc(hidden)]
pub fn event(&self) -> Option<GateActuatorEvent> {
self.left.event
}
#[doc(hidden)]
pub fn start(&self) -> &GateActuatorBatch<R> {
&self.base
}
#[doc(hidden)]
pub fn left_outputs(&self) -> Result<Vec<GateActuatorOutput>, StructureError> {
self.frame.outputs()
}
}
impl<R: Runtime<DType = DType>> GateActuatorBatch<R> {
#[doc(hidden)]
pub fn prepare_to(&self, end_s: f64) -> Result<GateActuatorPreparedInterval<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>,
{
self.prepare(end_s, false)
}
#[doc(hidden)]
pub fn prepare_coordinated_to(
&self,
end_s: f64,
) -> Result<GateActuatorPreparedInterval<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>,
{
self.prepare(end_s, true)
}
fn prepare(
&self,
end_s: f64,
coordinated: bool,
) -> Result<GateActuatorPreparedInterval<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>,
{
let duration = end_s - self.time;
if !end_s.is_finite() || !duration.is_finite() || duration <= 0.0 {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"actuator interval=[{},{}],duration={duration}: supply a finite positive original duration",
self.time, end_s
),
));
}
let next = if coordinated {
self.next_coordinated_event()?
} else {
self.next_event()?
};
let next = next.ok_or_else(|| {
StructureError::input(
StructureInputErrorKind::UnsupportedRegime,
"actuator trial exceeds terminal support",
)
})?;
if end_s > next.time_s {
return Err(StructureError::input(
StructureInputErrorKind::UnsupportedRegime,
"actuator interval crosses the next original event: split at that event",
));
}
let event = if coordinated {
self.event_at(end_s)?
} else if end_s == next.time_s {
Some(next)
} else {
None
};
let steps = self.steps.checked_add(1).ok_or_else(|| {
StructureError::numerical("actuator accepted interval counter overflow")
})?;
let next_target = if event.and_then(|e| e.target_index).is_some() {
self.next_target.checked_add(1).ok_or_else(|| {
StructureError::numerical("actuator target event counter overflow")
})?
} else {
self.next_target
};
let left = left(&self.context, &self.state, self.time, end_s, event)?;
let frame = Self {
context: Arc::clone(&self.context),
state: left.state.clone(),
time: end_s,
next_target: self.next_target,
steps,
last: self.last.clone(),
};
Ok(GateActuatorPreparedInterval {
base: self.clone(),
left,
frame,
next_target,
})
}
}
#[cfg(test)]
mod tests {
use super::super::interval::tests::{accepted, target, update};
use crate::structures::interval_fixture::{
Cpu, TestResult, actuators, actuators_at, bits, context,
};
use crate::structures::{GateActuatorBatch, StructureError, StructureInputErrorKind};
const ARRIVING_M: f64 = 0.2;
fn moving() -> TestResult<(GateActuatorBatch<Cpu>, f64, f64)> {
let context = context()?;
let batch = actuators_at(&context, "g1", (0.3, 0.3), (0.0, 0.0))?
.trial_to(1.0, &update(1.0, ARRIVING_M))?
.into_next();
let output = batch.outputs()?.remove(0);
let lower = output.arrival_lower_s.ok_or("a lower bound")?;
let upper = output.arrival_upper_s.ok_or("an upper bound")?;
Ok((batch, lower, upper))
}
fn rejects_unsupported<T>(result: Result<T, StructureError>) -> TestResult {
let error = result.err().ok_or("the interval rejects")?;
assert_eq!(
error.input_kind(),
Some(StructureInputErrorKind::UnsupportedRegime)
);
Ok(())
}
#[test]
fn a_coordinated_interval_reaches_an_arrival_window_in_one_end() -> TestResult {
let (batch, lower, upper) = moving()?;
assert!(
1.0 < lower && lower < upper && upper < 2.0,
"[{lower},{upper}]"
);
let standalone = batch.next_event()?.ok_or("a standalone event")?;
assert_eq!(standalone.time_s, lower);
let event = batch
.next_coordinated_event()?
.ok_or("a coordinated event")?;
assert_eq!(event.time_s, upper);
assert!(event.arrival_upper_boundary && !event.arrival_lower_boundary);
assert_eq!(event.target_index, None);
let prepared = batch.prepare_coordinated_to(upper)?;
assert_eq!(prepared.event(), Some(event));
let next = batch.complete(prepared, &[])?.into_next();
let output = next.outputs()?.remove(0);
assert_eq!(output.position.opening_m.to_bits(), ARRIVING_M.to_bits());
assert_eq!(next.states()?[0].arrival_count, 1);
let after = next.next_coordinated_event()?.ok_or("the target time")?;
assert_eq!((after.time_s, after.target_index), (2.0, Some(1)));
rejects_unsupported(batch.prepare_to(upper))
}
#[test]
fn an_interval_from_inside_a_window_crosses_its_upper_bound() -> TestResult {
let (batch, lower, upper) = moving()?;
let inside = f64::from_bits(lower.to_bits() + 1);
assert!(lower < inside && inside < upper, "[{lower},{upper}]");
let prepared = batch.prepare_coordinated_to(inside)?;
assert_eq!(prepared.event(), None);
let within = batch.complete(prepared, &[])?.into_next();
let event = within.next_coordinated_event()?.ok_or("the target time")?;
assert_eq!((event.time_s, event.target_index), (2.0, Some(1)));
let prepared = within.prepare_coordinated_to(1.9)?;
assert_eq!(prepared.event(), None);
let past = within.complete(prepared, &[])?.into_next();
let output = past.outputs()?.remove(0);
assert_eq!(output.position.opening_m.to_bits(), ARRIVING_M.to_bits());
rejects_unsupported(within.prepare_to(1.9))?;
rejects_unsupported(batch.prepare_coordinated_to(1.9))
}
#[test]
fn a_coordinated_interval_at_a_standalone_end_keeps_every_bit() -> TestResult {
let context = context()?;
let mut batch = actuators(&context, "g1", 0.0, 0.0)?;
while let Some(event) = batch.next_event()? {
let packets = match event.target_index {
Some(_) => update(event.time_s, target(event.time_s)),
None => Vec::new(),
};
let coordinated = batch.prepare_coordinated_to(event.time_s)?;
assert_eq!(coordinated.event(), Some(event));
let one = batch.complete(batch.prepare_to(event.time_s)?, &packets)?;
let two = batch.complete(coordinated, &packets)?;
assert_eq!(bits(&one.states()?), bits(&two.states()?));
assert_eq!(bits(&one.outputs()?), bits(&two.outputs()?));
assert_eq!(bits(&one.diagnostics()?), bits(&two.diagnostics()?));
assert_eq!(accepted(&one.next)?, accepted(&two.next)?);
batch = one.into_next();
}
assert_eq!(batch.time(), 4.0);
Ok(())
}
}