nyx_space/md/opti/multipleshooting/
ctrlnodes.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
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
/*
    Nyx, blazing fast astrodynamics
    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use anise::prelude::Frame;

use crate::md::prelude::{Objective, StateParameter};
use crate::time::Epoch;
use crate::NyxError;
use serde_derive::{Deserialize, Serialize};
use std::convert::Into;
use std::str::FromStr;

use super::multishoot::MultishootNode;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NodesSerde {
    pub nodes: Vec<NodeSerde>,
}

impl NodesSerde {
    pub fn to_node_vec(&self) -> Result<Vec<Node>, NyxError> {
        let mut rtn = Vec::with_capacity(self.nodes.len());
        for n in &self.nodes {
            rtn.push(n.to_node()?)
        }
        Ok(rtn)
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NodeSerde {
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub vmag: Option<f64>,
    pub epoch: String,
    pub frame: Frame,
}

impl NodeSerde {
    pub fn to_node(&self) -> Result<Node, NyxError> {
        let epoch = Epoch::from_str(&self.epoch).unwrap();

        Ok(Node {
            x: self.x,
            y: self.y,
            z: self.z,
            vmag: self.vmag.unwrap_or(0.0),
            frame: self.frame,
            epoch,
        })
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Node {
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub vmag: f64,
    pub epoch: Epoch,
    pub frame: Frame,
}

impl Node {
    pub fn rmag(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
    }
}

impl MultishootNode<3> for Node {
    fn epoch(&self) -> Epoch {
        self.epoch
    }

    fn update_component(&mut self, component: usize, add_val: f64) {
        match component {
            0 => self.x += add_val,
            1 => self.y += add_val,
            2 => self.z += add_val,
            3 => self.vmag += add_val,
            _ => unreachable!(),
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<[Objective; 3]> for Node {
    fn into(self) -> [Objective; 3] {
        [
            Objective::new(StateParameter::X, self.x),
            Objective::new(StateParameter::Y, self.y),
            Objective::new(StateParameter::Z, self.z),
        ]
    }
}

impl MultishootNode<4> for Node {
    fn epoch(&self) -> Epoch {
        self.epoch
    }

    fn update_component(&mut self, component: usize, add_val: f64) {
        match component {
            0 => self.x += add_val,
            1 => self.y += add_val,
            2 => self.z += add_val,
            3 => self.vmag += add_val,
            _ => unreachable!(),
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<[Objective; 4]> for Node {
    fn into(self) -> [Objective; 4] {
        [
            Objective::new(StateParameter::X, self.x),
            Objective::new(StateParameter::Y, self.y),
            Objective::new(StateParameter::Z, self.z),
            Objective::new(StateParameter::Vmag, self.vmag),
        ]
    }
}

#[allow(clippy::from_over_into)]
impl Into<NodeSerde> for Node {
    fn into(self) -> NodeSerde {
        NodeSerde {
            x: self.x,
            y: self.y,
            z: self.z,
            vmag: Some(self.vmag),
            frame: self.frame,
            epoch: self.epoch.to_string(),
        }
    }
}

#[allow(clippy::from_over_into)]
impl Into<NodeSerde> for &Node {
    fn into(self) -> NodeSerde {
        NodeSerde {
            x: self.x,
            y: self.y,
            z: self.z,
            vmag: Some(self.vmag),
            frame: self.frame,
            epoch: self.epoch.to_string(),
        }
    }
}

#[test]
fn test_nodeserde() {
    use toml;

    let str_nodes = r#"[[nodes]]
x = -394.37164017582654
y = -80.02184491079583
z = -1702.1160791417442
vmag = 0.0
epoch = "2023-11-25T14:11:46.789000034 UTC"

[nodes.frame]
ephemeris_id = 301
orientation_id = 1

[[nodes]]
x = -381.68254116206856
y = -48.21573534985666
z = -1705.829637126235
vmag = 0.0
epoch = "2023-11-25T14:12:06.789000034 UTC"

[nodes.frame]
ephemeris_id = 301
orientation_id = 1

[[nodes]]
x = -368.8474537620047
y = -16.401929604226403
z = -1708.8692139449731
vmag = 0.0
epoch = "2023-11-25T14:12:26.789000034 UTC"

[nodes.frame]
ephemeris_id = 301
orientation_id = 1
"#;

    let toml_nodes: NodesSerde = toml::from_str(str_nodes).unwrap();

    let nodes = toml_nodes.to_node_vec().unwrap();

    dbg!(&nodes);

    let v = NodesSerde {
        nodes: nodes.iter().map(|n| n.into()).collect::<Vec<NodeSerde>>(),
    };
    let toml_ser = toml::to_string(&v).unwrap();

    println!("GOT\n{}\n\nWANTED:{}", toml_ser, str_nodes);

    assert_eq!(toml_ser, str_nodes);
}