gmt_dos-actors 3.0.0

Giant Magellan Telescope Dynamic Optical Simulation Actor Model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*!
# [Actor](crate::Actor)s clients

The module holds the implementation of the different clients that can be assigned to [Actor]s.

Any structure can become a client to an Actor if it implements the [Update] trait with either or both [Read] and [Write] traits.

# Example

## Logging

A simple logger with a single entry:
```
use dos_actors::prelude::*;
let logging = Logging::<f64>::default();
```
A logger with 2 entries and pre-allocated with 1000 elements:
```
use dos_actors::prelude::*;
let logging = Logging::<f64>::default().n_entry(2).capacity(1_000);
```
## Signals

A constant signal for 100 steps
```
use dos_actors::prelude::*;
let signal = Signals::new(1, 100).signals(Signal::Constant(3.14));
```

A 2 outputs signal made of a constant and a sinusoid for 100 steps
```
use dos_actors::prelude::*;
let signal = Signals::new(2, 100)
               .output_signal(0, Signal::Constant(3.14))
               .output_signal(1, Signal::Sinusoid{
                                        amplitude: 1f64,
                                        sampling_frequency_hz: 1000f64,
                                        frequency_hz: 20f64,
                                        phase_s: 0f64
               });
```
## Rate transitionner

A rate transition actor for a named output/input pair sampling a [Vec]
```
use dos_actors::prelude::*;
#[derive(UID)]
enum MyIO {};
let sampler = Sampler::<Vec<f64>, MyIO>::default();
```

## Alias to input/output UID

Creating an alias to an already existing [UniqueIdentifier] (UID)
```
use std::sync::Arc;
use dos_actors::{
    io::{Data, Write},
    Size,
};
use uid::UniqueIdentifier;
use uid_derive::UID;

// Original UID
#[derive(UID)]
#[uid(data = "u8")]
pub enum A {}
pub struct Client {}
impl Write<A> for Client {
    fn write(&mut self) -> Option<Arc<Data<A>>> {
        Some(Arc::new(Data::new(10u8)))
    }
}
impl Size<A> for Client {
    fn len(&self) -> usize {
        123
    }
}

// A alias with `Write` and `Size` trait implementation for `Client`
#[derive(UID)]
#[alias(name = "A", client = "Client", traits = "Write,Size")]
pub enum B {}

let _: <A as UniqueIdentifier>::Data = 1u8;
let _: <B as UniqueIdentifier>::Data = 2u8;

let mut client = Client {};
 println!(
    "Client Write<B>: {:?}",
    <Client as Write<B>>::write(&mut client)
);
println!(
    "Client Size<B>: {:?}",
    <Client as Size<B>>::len(&mut client)
);
```

[Actor]: crate::actor
*/

#[cfg(feature = "windloads")]
pub mod windloads;

#[cfg(feature = "fem")]
pub mod fem;

#[cfg(feature = "mount-ctrl")]
pub mod mount;

#[cfg(feature = "m1-ctrl")]
pub mod m1;

#[cfg(feature = "apache-arrow")]
pub mod arrow_client;

#[cfg(feature = "fsm")]
pub mod fsm;

#[cfg(feature = "asm")]
pub mod asm;

#[cfg(feature = "crseo")]
pub mod ceo;

#[cfg(feature = "lom")]
pub mod lom;

#[cfg(feature = "dta")]
pub mod dta;

pub mod gmt_state;

#[cfg(feature = "ceo")]
mod dome_seeing;

use crate::{
    io::{Data, Read, Write},
    UniqueIdentifier, Update,
};
use linya::{Bar, Progress};
use std::{
    any::type_name,
    fmt::Display,
    marker::PhantomData,
    mem::take,
    ops::{Add, Mul, Sub, SubAssign},
    sync::{Arc, Mutex},
};

mod signals;
#[doc(inline)]
pub use signals::{OneSignal, Signal, Signals};

#[derive(Debug)]
pub(crate) struct ProgressBar {
    progress: Arc<Mutex<Progress>>,
    bar: Bar,
}

/// Simple digital timer
pub struct Timer {
    tick: usize,
    progress_bar: Option<ProgressBar>,
}
impl Timer {
    /// Initializes the timer based on the duration in # of samples
    pub fn new(duration: usize) -> Self {
        Self {
            tick: 1 + duration,
            progress_bar: None,
        }
    }
    pub fn progress(self) -> Self {
        let mut progress = Progress::new();
        let bar: Bar = progress.bar(self.tick, "Timer:");
        Self {
            progress_bar: Some(ProgressBar {
                progress: Arc::new(Mutex::new(progress)),
                bar,
            }),
            ..self
        }
    }
    pub fn progress_with(self, progress: Arc<Mutex<Progress>>) -> Self {
        let bar: Bar = progress.lock().unwrap().bar(self.tick, "Timer:");
        Self {
            progress_bar: Some(ProgressBar { progress, bar }),
            ..self
        }
    }
}
impl Update for Timer {
    fn update(&mut self) {
        if let Some(pb) = self.progress_bar.as_mut() {
            pb.progress.lock().unwrap().inc_and_draw(&pb.bar, 1)
        }
        self.tick -= 1;
    }
}
pub enum Tick {}
pub type Void = ();
impl UniqueIdentifier for Tick {
    type Data = Void;
}
impl Write<Tick> for Timer {
    fn write(&mut self) -> Option<Arc<Data<Tick>>> {
        if self.tick > 0 {
            Some(Arc::new(Data::new(())))
        } else {
            None
        }
    }
}
pub(crate) trait TimerMarker {}
impl<T: TimerMarker> Read<Tick> for T {
    fn read(&mut self, _: Arc<Data<Tick>>) {}
}

/// Simple data logging
///
/// Accumulates all the inputs in a single [Vec]
#[derive(Debug)]
pub struct Logging<T> {
    data: Vec<T>,
    n_sample: usize,
    n_entry: usize,
}

impl<T> std::ops::Deref for Logging<T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T> Default for Logging<T> {
    fn default() -> Self {
        Self {
            n_entry: 1,
            data: Vec::new(),
            n_sample: 0,
        }
    }
}
impl<T> Logging<T> {
    /// Sets the # of entries to be logged (default: 1)
    pub fn n_entry(self, n_entry: usize) -> Self {
        Self { n_entry, ..self }
    }
    /// Pre-allocates the size of the vector holding the data
    pub fn capacity(self, capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            ..self
        }
    }
    /// Returns the # of time samples
    pub fn len(&self) -> usize {
        self.n_sample / self.n_entry
    }
    /// Returns the sum of the entry sizes
    pub fn n_data(&self) -> usize {
        self.data.len() / self.len()
    }
    /// Checks if the logger is empty
    pub fn is_empty(&self) -> bool {
        self.n_sample == 0
    }
    /// Returns data chunks the size of the entries
    pub fn chunks(&self) -> impl Iterator<Item = &[T]> {
        self.data.chunks(self.n_data())
    }
}

impl<T> Display for Logging<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "Logging: ({}x{})={}",
            self.n_data(),
            self.len(),
            self.data.len()
        )
    }
}

impl<T> Update for Logging<T> {}
impl<T: Clone, U: UniqueIdentifier<Data = Vec<T>>> Read<U> for Logging<T> {
    fn read(&mut self, data: Arc<Data<U>>) {
        log::debug!("receive {} input: {:}", type_name::<U>(), data.len(),);
        self.data.extend((**data).clone());
        self.n_sample += 1;
    }
}

/// Rate transitionner
#[derive(Debug)]
pub struct Sampler<T, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T> = U> {
    input: Arc<Data<U>>,
    output: PhantomData<V>,
}
impl<T, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T>> Sampler<T, U, V> {
    /// Creates a new sampler with initial condition
    pub fn new(init: T) -> Self {
        Self {
            input: Arc::new(Data::new(init)),
            output: PhantomData,
        }
    }
}
impl<T: Default, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T>> Default
    for Sampler<T, U, V>
{
    fn default() -> Self {
        Self {
            input: Arc::new(Data::new(T::default())),
            output: PhantomData,
        }
    }
}
impl<T, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T>> Update for Sampler<T, U, V> {}
impl<T, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T>> Read<U> for Sampler<T, U, V> {
    fn read(&mut self, data: Arc<Data<U>>) {
        self.input = data;
    }
}
impl<T: Clone, U: UniqueIdentifier<Data = T>, V: UniqueIdentifier<Data = T>> Write<V>
    for Sampler<T, U, V>
{
    fn write(&mut self) -> Option<Arc<Data<V>>> {
        Some(Arc::new(Data::new((**self.input).clone())))
    }
}

/// Concatenates data into a [Vec]
pub struct Concat<T>(Vec<T>);
impl<T: Default> Default for Concat<T> {
    fn default() -> Self {
        Self(Vec::new())
    }
}
impl<T> Update for Concat<T> {}
impl<T: Clone + Default, U: UniqueIdentifier<Data = T>> Read<U> for Concat<T> {
    fn read(&mut self, data: Arc<Data<U>>) {
        self.0.push((*data).clone());
    }
}
impl<T: Clone, U: UniqueIdentifier<Data = Vec<T>>> Write<U> for Concat<T> {
    fn write(&mut self) -> Option<Arc<Data<U>>> {
        Some(Arc::new(Data::new(take(&mut self.0))))
    }
}

/// Integral controller
#[derive(Default)]
pub struct Integrator<U: UniqueIdentifier> {
    gain: U::Data,
    mem: U::Data,
    zero: U::Data,
    uid: PhantomData<U>,
}
impl<T, U> Integrator<U>
where
    T: Default + Clone,
    U: UniqueIdentifier<Data = Vec<T>>,
{
    /// Creates a new integral controller
    pub fn new(n_data: usize) -> Self {
        Self {
            gain: vec![Default::default(); n_data],
            mem: vec![Default::default(); n_data],
            zero: vec![Default::default(); n_data],
            uid: PhantomData,
        }
    }
    /// Sets a unique gain
    pub fn gain(self, gain: T) -> Self {
        Self {
            gain: vec![gain; self.mem.len()],
            ..self
        }
    }
    /// Sets the gain vector
    pub fn gain_vector(self, gain: Vec<T>) -> Self {
        assert_eq!(
            gain.len(),
            self.mem.len(),
            "gain vector length error: expected {} found {}",
            gain.len(),
            self.mem.len()
        );
        Self { gain, ..self }
    }
    /// Sets the integrator zero point
    pub fn zero(self, zero: Vec<T>) -> Self {
        Self { zero, ..self }
    }
}
impl<T, U> Update for Integrator<U> where U: UniqueIdentifier<Data = Vec<T>> {}
impl<T, U> Read<U> for Integrator<U>
where
    T: Copy + Mul<Output = T> + Sub<Output = T> + SubAssign,
    U: UniqueIdentifier<Data = Vec<T>>,
{
    fn read(&mut self, data: Arc<Data<U>>) {
        self.mem
            .iter_mut()
            .zip(&self.gain)
            .zip(&self.zero)
            .zip(&**data)
            .for_each(|(((x, g), z), u)| *x -= *g * (*u - *z));
    }
}
impl<T, V, U> Write<V> for Integrator<U>
where
    T: Copy + Add<Output = T>,
    V: UniqueIdentifier<Data = Vec<T>>,
    U: UniqueIdentifier<Data = Vec<T>>,
{
    fn write(&mut self) -> Option<Arc<Data<V>>> {
        let y: Vec<T> = self
            .mem
            .iter()
            .zip(&self.zero)
            .map(|(m, z)| *m + *z)
            .collect();
        Some(Arc::new(Data::new(y)))
    }
}

/// Discrete data sets
pub struct Source<T> {
    n: usize,
    data: Vec<T>,
}
impl<T> Source<T> {
    pub fn new(data: Vec<T>, n: usize) -> Self {
        Source { n, data }
    }
}
impl<T> Update for Source<T> {}

impl<T, V> Write<V> for Source<T>
where
    V: UniqueIdentifier<Data = Vec<T>>,
{
    fn write(&mut self) -> Option<Arc<Data<V>>> {
        if self.data.is_empty() {
            None
        } else {
            let y: Vec<T> = self.data.drain(..self.n).collect();
            Some(Arc::new(Data::new(y)))
        }
    }
}

/// Smooth a signal with a time varying [Weight] input
pub struct Smooth {
    weight: f64,
    data: Vec<f64>,
    data0: Option<Vec<f64>>,
}
impl Smooth {
    pub fn new() -> Self {
        Self {
            weight: 0f64,
            data: Vec::new(),
            data0: None,
        }
    }
}
impl Update for Smooth {}
/// Weight signal
use uid_derive::UID;
#[derive(UID)]
#[uid(data = "f64")]
pub enum Weight {}
impl Read<Weight> for Smooth {
    fn read(&mut self, data: Arc<Data<Weight>>) {
        let w: &f64 = &data;
        self.weight = *w;
    }
}
impl<U: UniqueIdentifier<Data = Vec<f64>>> Read<U> for Smooth {
    fn read(&mut self, data: Arc<Data<U>>) {
        let u: &[f64] = &data;
        self.data = u.to_vec();
        if self.data0.is_none() {
            self.data0 = Some(self.data.clone());
        }
    }
}
impl<U: UniqueIdentifier<Data = Vec<f64>>> Write<U> for Smooth {
    fn write(&mut self) -> Option<Arc<Data<U>>> {
        let y: Vec<_> = self.data.iter().map(|&u| u * self.weight).collect();
        Some(Arc::new(Data::new(y)))
    }
}

#[cfg(feature = "nalgebra")]
mod gain {
    use super::{Arc, Data, Read, UniqueIdentifier, Update, Write};
    use nalgebra as na;
    /// Gain
    pub struct Gain {
        u: na::DVector<f64>,
        y: na::DVector<f64>,
        mat: na::DMatrix<f64>,
    }
    impl Gain {
        pub fn new(mat: na::DMatrix<f64>) -> Self {
            Self {
                u: na::DVector::zeros(mat.ncols()),
                y: na::DVector::zeros(mat.nrows()),
                mat,
            }
        }
    }
    impl Update for Gain {
        fn update(&mut self) {
            self.y = &self.mat * &self.u;
        }
    }
    impl<U: UniqueIdentifier<Data = Vec<f64>>> Read<U> for Gain {
        fn read(&mut self, data: Arc<Data<U>>) {
            self.u = na::DVector::from_row_slice(&data);
        }
    }
    impl<U: UniqueIdentifier<Data = Vec<f64>>> Write<U> for Gain {
        fn write(&mut self) -> Option<Arc<Data<U>>> {
            Some(Arc::new(Data::new(self.y.as_slice().to_vec())))
        }
    }
}
#[cfg(feature = "nalgebra")]
pub use gain::Gain;