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
//! This module defines the [`Orbit`] struct
//!
//! Additional modules split the implementation of the struct
//! into different files.

use serde::Deserialize;

use crate::{F, I};

pub mod calc;
mod io;

/// An alias to `&mut F`
type MutFRef<'a> = &'a mut F;

/// An alias to `&mut Vec<F>`
type MutVecFRef<'a> = &'a mut Vec<F>;

/// Create a struct with a common fields type
macro_rules! def_struct {
    (
        $(#[$attr:meta])*
        $vis:vis struct $name:ident($type:ty) {
            $(
                #[$field_doc:meta]
                $field:ident,
            )*
        }
    ) => {
        $(#[$attr])*
        $vis struct $name {
            $(
                #[$field_doc]
                $field: $type,
            )*
        }
    }
}

/// Create a struct with a common fields type and a default initializer
macro_rules! init_struct {
    (
        $(#[$attr:meta])*
        $vis:vis struct $name:ident($type:ty, $expr:expr) {
            $(
                #[$field_doc:meta]
                $field:ident,
            )*
        }
    ) => {
        $(#[$attr])*
        $vis struct $name {
            $(
                #[$field_doc]
                $field: $type,
            )*
        }

        impl $name {
            /// Initialize a new struct with default values
            fn new() -> Self {
                Self {
                    $(
                        $field: $expr,
                    )*
                }
            }
        }
    }
}

/// Create a struct with a common fields type, a default initializer,
/// and a method to get fields as static string slices
macro_rules! fields_struct {
    (
        $(#[$attr:meta])*
        $vis:vis struct $name:ident($type:ty, $expr:expr) {
            $(
                #[$field_doc:meta]
                $field:ident,
            )*
        }
    ) => {
        $(#[$attr])*
        $vis struct $name {
            $(
                #[$field_doc]
                $field: $type,
            )*
        }

        impl $name {
            /// Initialize a new struct with default values
            fn new() -> Self {
                Self {
                    $(
                        $field: $expr,
                    )*
                }
            }
            /// Get the fields of the struct as static string slices
            #[must_use]
            pub fn fields() -> &'static [&'static str] {
                static NAMES: &'static [&'static str] = &[$(stringify!($field)),*];
                NAMES
            }
        }
    }
}

def_struct! {
/// Initial coordinates and velocities of a globular
/// cluster in the Heliocentric Cartesian system
#[derive(Deserialize)]
pub struct HCInitials(F) {
    /// X component of the radius vector $\[ \text{kpc} \]$
    x,
    /// Standard deviation of the X component of the radius vector $\[ \text{kpc} \]$
    x_err,
    /// Y component of the radius vector $\[ \text{kpc} \]$
    y,
    /// Standard deviation of the Y component of the radius vector $\[ \text{kpc} \]$
    y_err,
    /// Z component of the radius vector $\[ \text{kpc} \]$
    z,
    /// Standard deviation of the Z component of the radius vector $\[ \text{kpc} \]$
    z_err,
    /// U component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    u,
    /// Standard deviation of the U component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    u_err,
    /// V component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    v,
    /// Standard deviation of the V component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    v_err,
    /// W component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    w,
    /// Standard deviation of the W component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    w_err,
}
}

impl HCInitials {
    /// Get mutable references to the fields
    pub fn unpack(
        &mut self,
    ) -> (
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
        MutFRef,
    ) {
        (
            &mut self.x,
            &mut self.x_err,
            &mut self.y,
            &mut self.y_err,
            &mut self.z,
            &mut self.z_err,
            &mut self.u,
            &mut self.u_err,
            &mut self.v,
            &mut self.v_err,
            &mut self.w,
            &mut self.w_err,
        )
    }
}

init_struct! {
/// Initial coordinates and velocities of a globular
/// cluster in the Galactic Cartesian system
pub struct GCaInitials(F, 0.0) {
    /// X component of the radius vector $\[ \text{kpc} \]$
    x,
    /// Y component of the radius vector $\[ \text{kpc} \]$
    y,
    /// Z component of the radius vector $\[ \text{kpc} \]$
    z,
    /// U component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    u,
    /// V component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    v,
    /// W component of the velocity vector $\[ \text{km} \\, \text{s}^{-1} \]$
    w,
}
}

impl GCaInitials {
    /// Get mutable references to the fields
    pub fn unpack(&mut self) -> (MutFRef, MutFRef, MutFRef, MutFRef, MutFRef, MutFRef) {
        (
            &mut self.x,
            &mut self.y,
            &mut self.z,
            &mut self.u,
            &mut self.v,
            &mut self.w,
        )
    }
}

init_struct! {
/// Initial coordinates and velocities of a globular
/// cluster in the Galactic Cylindrical system
pub struct GCyInitials(F, 0.0) {
    /// Radius $\[ \text{kpc} \]$
    r,
    /// Azimuth $\[ \text{rad} \]$
    psi,
    /// Height $\[ \text{kpc} \]$
    z,
    /// Time derivative of R $\[ \text{km} \\, \text{s}^{-1} \]$
    dr,
    /// Angular velocity $\[ \text{rad} \\, \text{s}^{-1} \]$
    dpsi,
    /// Time derivative of Z $\[ \text{km} \\, \text{s}^{-1} \]$
    dz,
}
}

impl GCyInitials {
    /// Get mutable references to the fields
    pub fn unpack(&mut self) -> (MutFRef, MutFRef, MutFRef, MutFRef, MutFRef, MutFRef) {
        (
            &mut self.r,
            &mut self.psi,
            &mut self.z,
            &mut self.dr,
            &mut self.dpsi,
            &mut self.dz,
        )
    }
}

fields_struct! {
/// Values integrated / calculated during runtime
pub struct Results(Vec<F>, Vec::<F>::new()) {
    /// Radius in the Galactic Cylindrical system $\[ \text{kpc} \]$
    r,
    /// Azimuth in the Galactic Cylindrical system $\[ \text{kpc} \]$
    psi,
    /// Height in the Galactic Cartesian / Cylindrical system $\[ \text{kpc} \]$
    z,
    /// Momentum canonically conjugate to the radius $\[ \text{kpc} \\, \text{Myr}^{-1} \]$
    p_r,
    /// Momentum canonically conjugate to the azimuth $\[ \text{kpc}^2 \\, \text{rad} \\, \text{Myr}^{-1} \]$
    p_psi,
    /// Momentum canonically conjugate to the height $\[ \text{kpc} \\, \text{Myr}^{-1} \]$
    p_z,
    /// X component of the radius vector in the Galactic Cartesian system $\[ \text{kpc} \]$
    x,
    /// Y component of the radius vector in the Galactic Cartesian system $\[ \text{kpc} \]$
    y,
    /// Total energy $\[ \text{km}^2 \\, \text{s}^{-2} \]$
    e,
    /// Apocentric distance $\[ \text{kpc} \]$
    apo,
    /// Pericentric distance $\[ \text{kpc} \]$
    peri,
}
}

impl Results {
    /// Get mutable references to the fields
    pub fn unpack(
        &mut self,
    ) -> (
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
        MutVecFRef,
    ) {
        (
            &mut self.r,
            &mut self.psi,
            &mut self.z,
            &mut self.p_r,
            &mut self.p_psi,
            &mut self.p_z,
            &mut self.x,
            &mut self.y,
            &mut self.e,
            &mut self.apo,
            &mut self.peri,
        )
    }
}

/// An orbit of a globular cluster
pub struct Orbit {
    /// ID of the object
    id: String,
    /// Initial coordinates and velocities in the Heliocentric Cartesian system
    hc_initials: HCInitials,
    /// Initial coordinates and velocities in the Galactic Cartesian system
    gca_initials: GCaInitials,
    /// Initial coordinates and velocities in the Galactic Cylindrical system
    gcy_initials: GCyInitials,
    /// Number of iterations used in the last integration
    n: I,
    /// Integrated coordinates of a globular cluster in the Galactic Cylindrical system
    results: Results,
}

impl Orbit {
    /// Initialize an orbit with an ID from the initial coordinates
    /// and velocities in the Heliocentric Cartesian system
    #[must_use]
    pub fn from(id: String, hc_initials: HCInitials) -> Self {
        Orbit {
            id,
            hc_initials,
            gca_initials: GCaInitials::new(),
            gcy_initials: GCyInitials::new(),
            n: 0,
            results: Results::new(),
        }
    }
    /// Get the ID of the object
    #[must_use]
    pub fn id(&self) -> &String {
        &self.id
    }
}