Skip to main content

hydr_structures/structures/actuator/
batch.rs

1//! Immutable accepted ideal-actuator batches and positive-duration trials.
2
3use crate::structures::tensor::actuator::{
4    TensorActuatorContext, TensorActuatorState, TensorActuatorStep,
5};
6use numr::dtype::DType;
7use numr::runtime::Runtime;
8use std::sync::Arc;
9
10/// Accepted persistent ideal response state bound to the complete actual structure context.
11#[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    /// Bind an explicit ideal finite-rate response to an ordered actual-gate subset.
29    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    /// Complete bound original structure definitions in their original order.
64    pub fn elements(&self) -> &[StructureElement] {
65        &self.context.structures.elements
66    }
67    /// Immutable ordered actuator definitions and explicit policies.
68    pub fn definitions(&self) -> &[GateActuatorDefinition] {
69        &self.context.definitions
70    }
71    /// Original supplied initial positions and targets without bit normalization.
72    pub fn initial_states(&self) -> &[GateActuatorInitialState] {
73        &self.context.initial_states
74    }
75    /// Original finite target calendar and supported horizon.
76    pub fn schedule(&self) -> &GateActuatorSchedule {
77        &self.context.schedule
78    }
79    /// Accepted response time in seconds.
80    pub fn time(&self) -> f64 {
81        self.time
82    }
83    /// Number of accepted positive-duration response intervals.
84    pub fn accepted_steps(&self) -> usize {
85        self.steps
86    }
87    /// Next strictly later target, arrival boundary, or terminal event. Terminal state returns None.
88    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    /// Next strictly later target, arrival window, or terminal event of a coordinator.
103    ///
104    /// Each certified arrival window `[lower, upper]` encloses one arrival instant, so it
105    /// counts as one event. A window ahead of the accepted time reports its upper bound. A
106    /// window the accepted time lies inside reports nothing more. `prepare_coordinated_to`
107    /// admits every end up to this event. Terminal state returns None.
108    #[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    /// The event exactly at `end_s`, or `None` when `end_s` holds no target, pending arrival
124    /// bound, or terminal horizon.
125    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    /// Produce a complete candidate without crossing any original command or arrival event.
140    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}