hydr_structures/structures/actuator/
batch.rs1use crate::structures::tensor::actuator::{
4 TensorActuatorContext, TensorActuatorState, TensorActuatorStep,
5};
6use numr::dtype::DType;
7use numr::runtime::Runtime;
8use std::sync::Arc;
9
10#[derive(Clone, Debug)]
12pub struct GateActuatorBatch<R: Runtime<DType = DType>> {
13 pub(crate) context: Arc<TensorActuatorContext<R>>,
14 pub(crate) state: TensorActuatorState<R>,
15 pub(crate) time: f64,
16 pub(crate) next_target: usize,
17 pub(crate) steps: usize,
18 pub(crate) last: Option<Arc<TensorActuatorStep<R>>>,
19}
20
21use super::{
22 GateActuatorDefinition, GateActuatorEvent, GateActuatorInitialState, GateActuatorSchedule,
23 GateActuatorTrial, GateTargetUpdate,
24};
25use crate::structures::{StructureBatch, StructureElement, StructureError};
26
27impl<R: Runtime<DType = DType>> StructureBatch<R> {
28 pub fn with_gate_actuators(
30 &self,
31 definitions: Vec<GateActuatorDefinition>,
32 initial_states: Vec<GateActuatorInitialState>,
33 schedule: GateActuatorSchedule,
34 ) -> Result<GateActuatorBatch<R>, StructureError>
35 where
36 R::Client: numr::ops::BinaryOps<R>
37 + numr::ops::CompareOps<R>
38 + numr::ops::ConditionalOps<R>
39 + numr::ops::ReduceOps<R>
40 + numr::ops::ScalarOps<R>
41 + numr::ops::TwoSumOps<R>
42 + numr::ops::TypeConversionOps<R>
43 + numr::ops::UnaryOps<R>,
44 {
45 let context = Arc::new(TensorActuatorContext::new(
46 Arc::clone(&self.context),
47 definitions,
48 initial_states,
49 schedule,
50 )?);
51 let state = crate::structures::numerics::actuator::initial(&context)?;
52 Ok(GateActuatorBatch {
53 time: context.schedule.initial_time_s,
54 context,
55 state,
56 next_target: 0,
57 steps: 0,
58 last: None,
59 })
60 }
61}
62impl<R: Runtime<DType = DType>> GateActuatorBatch<R> {
63 pub fn elements(&self) -> &[StructureElement] {
65 &self.context.structures.elements
66 }
67 pub fn definitions(&self) -> &[GateActuatorDefinition] {
69 &self.context.definitions
70 }
71 pub fn initial_states(&self) -> &[GateActuatorInitialState] {
73 &self.context.initial_states
74 }
75 pub fn schedule(&self) -> &GateActuatorSchedule {
77 &self.context.schedule
78 }
79 pub fn time(&self) -> f64 {
81 self.time
82 }
83 pub fn accepted_steps(&self) -> usize {
85 self.steps
86 }
87 pub fn next_event(&self) -> Result<Option<GateActuatorEvent>, StructureError>
89 where
90 R::Client: numr::ops::BinaryOps<R>
91 + numr::ops::CompareOps<R>
92 + numr::ops::ConditionalOps<R>
93 + numr::ops::ReduceOps<R>,
94 {
95 crate::structures::numerics::actuator::next_event(
96 &self.context,
97 &self.state,
98 self.time,
99 self.next_target,
100 )
101 }
102 #[doc(hidden)]
109 pub fn next_coordinated_event(&self) -> Result<Option<GateActuatorEvent>, StructureError>
110 where
111 R::Client: numr::ops::BinaryOps<R>
112 + numr::ops::CompareOps<R>
113 + numr::ops::ConditionalOps<R>
114 + numr::ops::ReduceOps<R>,
115 {
116 crate::structures::numerics::actuator::next_coordinated_event(
117 &self.context,
118 &self.state,
119 self.time,
120 self.next_target,
121 )
122 }
123 pub(crate) fn event_at(&self, end_s: f64) -> Result<Option<GateActuatorEvent>, StructureError>
126 where
127 R::Client: numr::ops::BinaryOps<R>
128 + numr::ops::CompareOps<R>
129 + numr::ops::ConditionalOps<R>
130 + numr::ops::ReduceOps<R>,
131 {
132 crate::structures::numerics::actuator::event_at(
133 &self.context,
134 &self.state,
135 (self.time, self.next_target),
136 end_s,
137 )
138 }
139 pub fn trial_to(
141 &self,
142 end_s: f64,
143 target_updates: &[GateTargetUpdate],
144 ) -> Result<GateActuatorTrial<R>, StructureError>
145 where
146 R::Client: numr::ops::BinaryOps<R>
147 + numr::ops::CompareOps<R>
148 + numr::ops::ConditionalOps<R>
149 + numr::ops::ReduceOps<R>
150 + numr::ops::ScalarOps<R>
151 + numr::ops::TwoSumOps<R>
152 + numr::ops::TypeConversionOps<R>
153 + numr::ops::UnaryOps<R>,
154 {
155 self.complete(self.prepare_to(end_s)?, target_updates)
156 }
157}