use crate::structures::tensor::actuator::{
TensorActuatorContext, TensorActuatorState, TensorActuatorStep,
};
use numr::dtype::DType;
use numr::runtime::Runtime;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct GateActuatorBatch<R: Runtime<DType = DType>> {
pub(crate) context: Arc<TensorActuatorContext<R>>,
pub(crate) state: TensorActuatorState<R>,
pub(crate) time: f64,
pub(crate) next_target: usize,
pub(crate) steps: usize,
pub(crate) last: Option<Arc<TensorActuatorStep<R>>>,
}
use super::{
GateActuatorDefinition, GateActuatorEvent, GateActuatorInitialState, GateActuatorSchedule,
GateActuatorTrial, GateTargetUpdate,
};
use crate::structures::{StructureBatch, StructureElement, StructureError};
impl<R: Runtime<DType = DType>> StructureBatch<R> {
pub fn with_gate_actuators(
&self,
definitions: Vec<GateActuatorDefinition>,
initial_states: Vec<GateActuatorInitialState>,
schedule: GateActuatorSchedule,
) -> Result<GateActuatorBatch<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 context = Arc::new(TensorActuatorContext::new(
Arc::clone(&self.context),
definitions,
initial_states,
schedule,
)?);
let state = crate::structures::numerics::actuator::initial(&context)?;
Ok(GateActuatorBatch {
time: context.schedule.initial_time_s,
context,
state,
next_target: 0,
steps: 0,
last: None,
})
}
}
impl<R: Runtime<DType = DType>> GateActuatorBatch<R> {
pub fn elements(&self) -> &[StructureElement] {
&self.context.structures.elements
}
pub fn definitions(&self) -> &[GateActuatorDefinition] {
&self.context.definitions
}
pub fn initial_states(&self) -> &[GateActuatorInitialState] {
&self.context.initial_states
}
pub fn schedule(&self) -> &GateActuatorSchedule {
&self.context.schedule
}
pub fn time(&self) -> f64 {
self.time
}
pub fn accepted_steps(&self) -> usize {
self.steps
}
pub fn next_event(&self) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
crate::structures::numerics::actuator::next_event(
&self.context,
&self.state,
self.time,
self.next_target,
)
}
#[doc(hidden)]
pub fn next_coordinated_event(&self) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
crate::structures::numerics::actuator::next_coordinated_event(
&self.context,
&self.state,
self.time,
self.next_target,
)
}
pub(crate) fn event_at(&self, end_s: f64) -> Result<Option<GateActuatorEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
crate::structures::numerics::actuator::event_at(
&self.context,
&self.state,
(self.time, self.next_target),
end_s,
)
}
pub fn trial_to(
&self,
end_s: f64,
target_updates: &[GateTargetUpdate],
) -> Result<GateActuatorTrial<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.complete(self.prepare_to(end_s)?, target_updates)
}
}