nyx_space/propagators/
options.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
222
223
224
225
/*
    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 std::fmt;

use crate::time::{Duration, Unit};

use super::ErrorControl;
use anise::frames::Frame;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

/// Stores the integrator options, including the minimum and maximum step sizes, and the central body to perform the integration.
///
/// Note that different step sizes and max errors are only used for adaptive
/// methods. To use a fixed step integrator, initialize the options using `with_fixed_step`, and
/// use whichever adaptive step integrator is desired.  For example, initializing an RK45 with
/// fixed step options will lead to an RK4 being used instead of an RK45.
#[derive(Clone, Copy, Debug, TypedBuilder, Serialize, Deserialize, PartialEq)]
#[builder(doc)]
pub struct IntegratorOptions {
    #[builder(default_code = "60.0 * Unit::Second")]
    pub init_step: Duration,
    #[builder(default_code = "0.001 * Unit::Second")]
    pub min_step: Duration,
    #[builder(default_code = "2700.0 * Unit::Second")]
    pub max_step: Duration,
    #[builder(default = 1e-12)]
    pub tolerance: f64,
    #[builder(default = 50)]
    pub attempts: u8,
    #[builder(default = false)]
    pub fixed_step: bool,
    #[builder(default)]
    pub error_ctrl: ErrorControl,
    /// If a frame is specified and the propagator state is in a different frame, it it changed to this frame prior to integration.
    /// Note, when setting this, it's recommended to call `strip` on the Frame.
    #[builder(default, setter(strip_option))]
    pub integration_frame: Option<Frame>,
}

impl IntegratorOptions {
    /// `with_adaptive_step` initializes an `PropOpts` such that the integrator is used with an
    ///  adaptive step size. The number of attempts is currently fixed to 50 (as in GMAT).
    pub fn with_adaptive_step(
        min_step: Duration,
        max_step: Duration,
        tolerance: f64,
        error_ctrl: ErrorControl,
    ) -> Self {
        IntegratorOptions {
            init_step: max_step,
            min_step,
            max_step,
            tolerance,
            attempts: 50,
            fixed_step: false,
            error_ctrl,
            integration_frame: None,
        }
    }

    pub fn with_adaptive_step_s(
        min_step: f64,
        max_step: f64,
        tolerance: f64,
        error_ctrl: ErrorControl,
    ) -> Self {
        Self::with_adaptive_step(
            min_step * Unit::Second,
            max_step * Unit::Second,
            tolerance,
            error_ctrl,
        )
    }

    /// `with_fixed_step` initializes an `PropOpts` such that the integrator is used with a fixed
    ///  step size.
    pub fn with_fixed_step(step: Duration) -> Self {
        IntegratorOptions {
            init_step: step,
            min_step: step,
            max_step: step,
            tolerance: 0.0,
            fixed_step: true,
            attempts: 0,
            error_ctrl: ErrorControl::RSSCartesianStep,
            integration_frame: None,
        }
    }

    pub fn with_fixed_step_s(step: f64) -> Self {
        Self::with_fixed_step(step * Unit::Second)
    }

    /// Returns the default options with a specific tolerance.
    #[allow(clippy::field_reassign_with_default)]
    pub fn with_tolerance(tolerance: f64) -> Self {
        let mut opts = Self::default();
        opts.tolerance = tolerance;
        opts
    }

    /// Creates a propagator with the provided max step, and sets the initial step to that value as well.
    #[allow(clippy::field_reassign_with_default)]
    pub fn with_max_step(max_step: Duration) -> Self {
        let mut opts = Self::default();
        opts.set_max_step(max_step);
        opts
    }

    /// Returns a string with the information about these options
    pub fn info(&self) -> String {
        format!("{self}")
    }

    /// Set the maximum step size and sets the initial step to that value if currently greater
    pub fn set_max_step(&mut self, max_step: Duration) {
        if self.init_step > max_step {
            self.init_step = max_step;
        }
        self.max_step = max_step;
    }

    /// Set the minimum step size and sets the initial step to that value if currently smaller
    pub fn set_min_step(&mut self, min_step: Duration) {
        if self.init_step < min_step {
            self.init_step = min_step;
        }
        self.min_step = min_step;
    }
}

impl fmt::Display for IntegratorOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.fixed_step {
            write!(f, "fixed step: {:e}", self.min_step,)
        } else {
            write!(
                f,
                "min_step: {:e}, max_step: {:e}, tol: {:e}, attempts: {}",
                self.min_step, self.max_step, self.tolerance, self.attempts,
            )
        }
    }
}

impl Default for IntegratorOptions {
    /// `default` returns the same default options as GMAT.
    fn default() -> IntegratorOptions {
        IntegratorOptions {
            init_step: 60.0 * Unit::Second,
            min_step: 0.001 * Unit::Second,
            max_step: 2700.0 * Unit::Second,
            tolerance: 1e-12,
            attempts: 50,
            fixed_step: false,
            error_ctrl: ErrorControl::RSSCartesianStep,
            integration_frame: None,
        }
    }
}

#[cfg(test)]
mod ut_integr_opts {
    use hifitime::Unit;

    use crate::propagators::{ErrorControl, IntegratorOptions};

    #[test]
    fn test_options() {
        let opts = IntegratorOptions::with_fixed_step_s(1e-1);
        assert_eq!(opts.min_step, 1e-1 * Unit::Second);
        assert_eq!(opts.max_step, 1e-1 * Unit::Second);
        assert!(opts.tolerance.abs() < f64::EPSILON);
        assert!(opts.fixed_step);

        let opts =
            IntegratorOptions::with_adaptive_step_s(1e-2, 10.0, 1e-12, ErrorControl::RSSStep);
        assert_eq!(opts.min_step, 1e-2 * Unit::Second);
        assert_eq!(opts.max_step, 10.0 * Unit::Second);
        assert!((opts.tolerance - 1e-12).abs() < f64::EPSILON);
        assert!(!opts.fixed_step);

        let opts: IntegratorOptions = Default::default();
        assert_eq!(opts.init_step, 60.0 * Unit::Second);
        assert_eq!(opts.min_step, 0.001 * Unit::Second);
        assert_eq!(opts.max_step, 2700.0 * Unit::Second);
        assert!((opts.tolerance - 1e-12).abs() < f64::EPSILON);
        assert_eq!(opts.attempts, 50);
        assert!(!opts.fixed_step);

        let opts = IntegratorOptions::with_max_step(1.0 * Unit::Second);
        assert_eq!(opts.init_step, 1.0 * Unit::Second);
        assert_eq!(opts.min_step, 0.001 * Unit::Second);
        assert_eq!(opts.max_step, 1.0 * Unit::Second);
        assert!((opts.tolerance - 1e-12).abs() < f64::EPSILON);
        assert_eq!(opts.attempts, 50);
        assert!(!opts.fixed_step);
    }

    #[test]
    fn test_serde() {
        let opts = IntegratorOptions::default();
        let serialized = toml::to_string(&opts).unwrap();
        println!("{serialized}");
        let deserd: IntegratorOptions = toml::from_str(&serialized).unwrap();
        assert_eq!(deserd, opts);
    }
}