gmt_dos_clients_crseo/ngao/
optical_model.rs

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
use std::{cell::RefCell, rc::Rc, sync::Arc};

use crseo::{
    wavefrontsensor::PhaseSensor, Atmosphere, CrseoError, FromBuilder, Gmt, SegmentWiseSensor,
    Source,
};
use gmt_dos_clients_domeseeing::{DomeSeeing, DomeSeeingError};
use gmt_dos_clients_io::optics::SegmentD7Piston;
use interface::{select::Selector, Data, TimerMarker, UniqueIdentifier, Units, Update, Write};

use crate::{DetectorFrame, OpticalModelBuilder};

mod m1;
mod m2;
mod stats;

#[derive(Debug, thiserror::Error)]
pub enum OpticalModelError {
    #[error(transparent)]
    CRSEO(#[from] CrseoError),
    #[error(transparent)]
    DomeSeeing(#[from] DomeSeeingError),
}

/// GMT optical model
///
/// ```no_run
/// use gmt_dos_clients_crseo::OpticalModel;
/// let optical_model_builder = OpticalModel::builder().build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct OpticalModel<T = PhaseSensor> {
    pub gmt: Gmt,
    pub src: Rc<RefCell<Source>>,
    pub atm: Option<Atmosphere>,
    pub dome_seeing: Option<DomeSeeing>,
    pub tau: f64,
    pub piston: Option<Arc<Vec<f64>>>,
    pub sensor: Option<T>,
}

impl Default for OpticalModel<PhaseSensor> {
    fn default() -> Self {
        <OpticalModelBuilder as Default>::default().build().unwrap()
    }
}

unsafe impl<T> Send for OpticalModel<T> {}
unsafe impl<T> Sync for OpticalModel<T> {}

impl<T> OpticalModel<T>
where
    T: FromBuilder,
    T::ComponentBuilder: Default,
{
    /// Return the [OpticalModelBuilder]
    pub fn builder() -> OpticalModelBuilder<T::ComponentBuilder> {
        Default::default()
    }
}

impl<T> Units for OpticalModel<T> {}
impl<T> Selector for OpticalModel<T> {}

impl<T> TimerMarker for OpticalModel<T> {}
impl<T> Update for OpticalModel<T>
where
    T: SegmentWiseSensor,
    OpticalModel<T>: Send + Sync,
{
    fn update(&mut self) {
        self.src.borrow_mut().through(&mut self.gmt).xpupil();
        if let Some(atm) = &mut self.atm {
            atm.secs += self.tau;
            self.src.borrow_mut().through(atm);
        }
        if let Some(dome_seeing) = &mut self.dome_seeing {
            self.src
                .borrow_mut()
                .add_same(dome_seeing.next().unwrap().as_slice());
        }
        if let Some(piston) = self.piston.as_deref() {
            self.src.borrow_mut().add_piston(piston.as_slice());
        }
        if let Some(sensor) = &mut self.sensor {
            sensor.propagate(&mut *self.src.borrow_mut())
        }
    }
}

// impl<T> Write<GuideStar> for OpticalModel<T> {
//     fn write(&mut self) -> Option<Data<GuideStar>> {
//         Some(Data::new(self.src.clone()))
//     }
// }

impl<T> Write<DetectorFrame> for OpticalModel<T>
where
    T: SegmentWiseSensor,
    DetectorFrame: UniqueIdentifier<DataType = crseo::Frame>,
{
    fn write(&mut self) -> Option<Data<DetectorFrame>> {
        self.sensor.as_mut().map(|sensor| {
            let frame = SegmentWiseSensor::frame(sensor);
            <T as crseo::WavefrontSensor>::reset(sensor);
            Data::new(frame)
        })
    }
}

impl<T: SegmentWiseSensor, const E: i32> Write<SegmentD7Piston<E>> for OpticalModel<T> {
    fn write(&mut self) -> Option<Data<SegmentD7Piston<E>>> {
        let data = self.src.borrow_mut().segment_wfe();
        let p7 = data[6].0;
        // let data = &self.segment_wfe;
        Some(
            data.into_iter()
                .map(|(p, _)| (p - p7) * 10_f64.powi(-E))
                .collect::<Vec<_>>()
                .into(),
        )
    }
}

// #[derive(interface::UID)]
// #[uid(data = (Vec<f32>,Vec<bool>))]
pub enum GmtWavefront {}
impl UniqueIdentifier for GmtWavefront {
    type DataType = (Vec<f32>, Vec<bool>);
}

impl<T: SegmentWiseSensor> Write<GmtWavefront> for OpticalModel<T> {
    fn write(&mut self) -> Option<Data<GmtWavefront>> {
        let src = &mut *self.src.borrow_mut();
        let amplitude: Vec<_> = src.amplitude().into_iter().map(|a| a > 0.).collect();
        let phase = src.phase();
        let phase: Vec<_> = amplitude
            .iter()
            .zip(phase)
            .filter(|(&a, _)| a)
            .map(|(_, &p)| p)
            .collect();
        Some(Data::new((phase, amplitude)))
    }
}