astrora_core 0.1.1

Astrora - Rust-backed astrodynamics library - core computational components
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
//! Error types for astrora astrodynamics calculations
//!
//! This module defines a comprehensive error hierarchy for orbital mechanics
//! computations, including numerical failures, physical constraint violations,
//! and conversion errors. All errors integrate seamlessly with PyO3 for
//! automatic conversion to Python exceptions.

use pyo3::exceptions::{PyRuntimeError, PyValueError, PyTypeError, PyArithmeticError};
use pyo3::PyErr;
use thiserror::Error;

/// Main error type for astrora operations
///
/// This enum covers all error cases that can occur during astrodynamics
/// calculations, from numerical convergence failures to physical constraint
/// violations.
#[derive(Debug, Error)]
pub enum PoliastroError {
    // ========================================================================
    // Numerical and Mathematical Errors
    // ========================================================================
    /// Iterative solver failed to converge within allowed iterations
    ///
    /// Common in Kepler's equation solving, Lambert's problem, and
    /// root-finding algorithms.
    #[error("Convergence failure: {method} failed to converge after {iterations} iterations (tolerance: {tolerance})")]
    ConvergenceFailure {
        method: String,
        iterations: usize,
        tolerance: f64,
    },

    /// Numerical value is invalid (NaN, Infinity, or denormal)
    #[error("Invalid numerical value encountered: {context} = {value}")]
    InvalidNumericalValue {
        context: String,
        value: f64,
    },

    /// Division by zero or near-zero value
    #[error("Division by zero or near-zero value in {context}: divisor = {divisor}")]
    DivisionByZero {
        context: String,
        divisor: f64,
    },

    /// Numerical instability detected
    #[error("Numerical instability detected in {operation}: {details}")]
    NumericalInstability {
        operation: String,
        details: String,
    },

    /// Matrix is singular or near-singular
    #[error("Singular matrix encountered in {context}: determinant = {determinant}")]
    SingularMatrix {
        context: String,
        determinant: f64,
    },

    // ========================================================================
    // Physical Constraint Violations
    // ========================================================================
    /// Physical parameter violates required constraints
    #[error("Invalid parameter '{parameter}': value {value} {constraint}")]
    InvalidParameter {
        parameter: String,
        value: f64,
        constraint: String,
    },

    /// Orbital eccentricity is out of valid range
    #[error("Invalid eccentricity: {value} (must be >= 0)")]
    InvalidEccentricity {
        value: f64,
    },

    /// Semi-major axis is invalid
    #[error("Invalid semi-major axis: {value} km (must be > 0 for elliptic orbits)")]
    InvalidSemiMajorAxis {
        value: f64,
    },

    /// Inclination is out of valid range [0, π]
    #[error("Invalid inclination: {value} rad (must be in [0, π])")]
    InvalidInclination {
        value: f64,
    },

    /// Angular value is out of valid range
    #[error("Invalid angle '{name}': {value} rad (expected range: [{min}, {max}])")]
    InvalidAngle {
        name: String,
        value: f64,
        min: f64,
        max: f64,
    },

    /// Orbit violates energy conservation
    #[error("Energy conservation violated: ΔE = {delta_energy} (tolerance: {tolerance})")]
    EnergyNotConserved {
        delta_energy: f64,
        tolerance: f64,
    },

    /// Orbit violates angular momentum conservation
    #[error("Angular momentum conservation violated: ΔL = {delta_momentum} (tolerance: {tolerance})")]
    MomentumNotConserved {
        delta_momentum: f64,
        tolerance: f64,
    },

    // ========================================================================
    // State and Coordinate Errors
    // ========================================================================
    /// State vector contains invalid values
    #[error("Invalid state vector: {reason}")]
    InvalidStateVector {
        reason: String,
    },

    /// Position vector is zero or near-zero
    #[error("Zero or near-zero position vector: |r| = {magnitude} km")]
    ZeroPosition {
        magnitude: f64,
    },

    /// Velocity vector is zero or near-zero
    #[error("Zero or near-zero velocity vector: |v| = {magnitude} km/s")]
    ZeroVelocity {
        magnitude: f64,
    },

    /// Coordinate transformation failed
    #[error("Coordinate transformation failed from {from_frame} to {to_frame}: {reason}")]
    TransformationFailure {
        from_frame: String,
        to_frame: String,
        reason: String,
    },

    /// Singularity in orbital element conversion
    ///
    /// Occurs for circular orbits (e=0), equatorial orbits (i=0), or
    /// critical inclination cases.
    #[error("Singularity in orbital elements: {singularity_type} (consider using {alternative})")]
    OrbitalSingularity {
        singularity_type: String,
        alternative: String,
    },

    // ========================================================================
    // Integration and Propagation Errors
    // ========================================================================
    /// Numerical integrator failed
    #[error("Integration failure in {integrator}: {reason}")]
    IntegrationFailure {
        integrator: String,
        reason: String,
    },

    /// Time step is too large for accurate integration
    #[error("Time step too large: {step_size} s (recommended: < {max_recommended} s for {orbit_type})")]
    TimeStepTooLarge {
        step_size: f64,
        max_recommended: f64,
        orbit_type: String,
    },

    /// Propagation diverged or became unstable
    #[error("Propagation diverged after {time} s: {reason}")]
    PropagationDivergence {
        time: f64,
        reason: String,
    },

    /// Propagation failed for a specific context
    #[error("Propagation failed in {context}: {source}")]
    PropagationFailed {
        context: String,
        source: Box<PoliastroError>,
    },

    // ========================================================================
    // Input Validation Errors
    // ========================================================================
    /// Input value is out of acceptable range
    #[error("Value out of range for '{parameter}': {value} (expected: [{min}, {max}])")]
    OutOfRange {
        parameter: String,
        value: f64,
        min: f64,
        max: f64,
    },

    /// Incompatible units provided
    #[error("Incompatible units: expected {expected}, got {actual}")]
    IncompatibleUnits {
        expected: String,
        actual: String,
    },

    /// Invalid time value or epoch
    #[error("Invalid time value: {reason}")]
    InvalidTime {
        reason: String,
    },

    /// Missing required parameter
    #[error("Missing required parameter: {parameter}")]
    MissingParameter {
        parameter: String,
    },

    // ========================================================================
    // Orbit Type Errors
    // ========================================================================
    /// Operation not supported for this orbit type
    #[error("Operation '{operation}' not supported for {orbit_type} orbits")]
    UnsupportedOrbitType {
        operation: String,
        orbit_type: String,
    },

    /// Orbit type cannot be determined
    #[error("Cannot determine orbit type: {reason}")]
    AmbiguousOrbitType {
        reason: String,
    },

    // ========================================================================
    // General Errors
    // ========================================================================
    /// Computation error with custom message
    #[error("Computation error: {message}")]
    ComputationError {
        message: String,
    },

    /// Not implemented yet
    #[error("Not implemented: {feature}")]
    NotImplemented {
        feature: String,
    },

    /// Internal error (bug in the library)
    #[error("Internal error: {message} (this is a bug, please report it)")]
    InternalError {
        message: String,
    },
}

/// Result type alias for astrora operations
///
/// This is a convenience alias for `Result<T, PoliastroError>` that should
/// be used throughout the library for consistency.
///
/// Note: The type is still called PoliastroError/PoliastroResult internally
/// for backward compatibility, but represents astrora's error handling.
pub type PoliastroResult<T> = Result<T, PoliastroError>;

// ============================================================================
// PyO3 Integration
// ============================================================================

impl From<PoliastroError> for PyErr {
    /// Convert PoliastroError to Python exception
    ///
    /// Maps different error variants to appropriate Python exception types:
    /// - Numerical errors → ArithmeticError
    /// - Invalid parameters → ValueError
    /// - Not implemented → NotImplementedError (via RuntimeError)
    /// - Internal errors → RuntimeError
    fn from(err: PoliastroError) -> PyErr {
        use PoliastroError::*;

        match err {
            // Numerical/mathematical errors → ArithmeticError
            ConvergenceFailure { .. }
            | DivisionByZero { .. }
            | NumericalInstability { .. }
            | SingularMatrix { .. } => PyArithmeticError::new_err(err.to_string()),

            // Invalid numerical values → ValueError
            InvalidNumericalValue { .. } => PyValueError::new_err(err.to_string()),

            // Physical constraint violations → ValueError
            InvalidParameter { .. }
            | InvalidEccentricity { .. }
            | InvalidSemiMajorAxis { .. }
            | InvalidInclination { .. }
            | InvalidAngle { .. }
            | EnergyNotConserved { .. }
            | MomentumNotConserved { .. } => PyValueError::new_err(err.to_string()),

            // State and coordinate errors → ValueError
            InvalidStateVector { .. }
            | ZeroPosition { .. }
            | ZeroVelocity { .. }
            | OrbitalSingularity { .. } => PyValueError::new_err(err.to_string()),

            // Transformation errors → RuntimeError
            TransformationFailure { .. } => PyRuntimeError::new_err(err.to_string()),

            // Integration errors → RuntimeError
            IntegrationFailure { .. }
            | PropagationDivergence { .. }
            | PropagationFailed { .. } => PyRuntimeError::new_err(err.to_string()),

            // Validation errors → ValueError
            OutOfRange { .. }
            | InvalidTime { .. }
            | MissingParameter { .. }
            | TimeStepTooLarge { .. } => PyValueError::new_err(err.to_string()),

            // Unit errors → TypeError
            IncompatibleUnits { .. } => PyTypeError::new_err(err.to_string()),

            // Orbit type errors → ValueError
            UnsupportedOrbitType { .. }
            | AmbiguousOrbitType { .. } => PyValueError::new_err(err.to_string()),

            // General errors → RuntimeError
            ComputationError { .. }
            | NotImplemented { .. }
            | InternalError { .. } => PyRuntimeError::new_err(err.to_string()),
        }
    }
}

// ============================================================================
// Convenience Constructors
// ============================================================================

impl PoliastroError {
    /// Create a convergence failure error
    pub fn convergence_failure(method: impl Into<String>, iterations: usize, tolerance: f64) -> Self {
        Self::ConvergenceFailure {
            method: method.into(),
            iterations,
            tolerance,
        }
    }

    /// Create an invalid parameter error
    pub fn invalid_parameter(
        parameter: impl Into<String>,
        value: f64,
        constraint: impl Into<String>,
    ) -> Self {
        Self::InvalidParameter {
            parameter: parameter.into(),
            value,
            constraint: constraint.into(),
        }
    }

    /// Create an out of range error
    pub fn out_of_range(parameter: impl Into<String>, value: f64, min: f64, max: f64) -> Self {
        Self::OutOfRange {
            parameter: parameter.into(),
            value,
            min,
            max,
        }
    }

    /// Create an invalid state vector error
    pub fn invalid_state(reason: impl Into<String>) -> Self {
        Self::InvalidStateVector {
            reason: reason.into(),
        }
    }

    /// Create a not implemented error
    pub fn not_implemented(feature: impl Into<String>) -> Self {
        Self::NotImplemented {
            feature: feature.into(),
        }
    }

    /// Create an internal error (indicates a bug)
    pub fn internal(message: impl Into<String>) -> Self {
        Self::InternalError {
            message: message.into(),
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_convergence_failure() {
        let err = PoliastroError::convergence_failure("Newton-Raphson", 100, 1e-10);
        assert!(err.to_string().contains("Newton-Raphson"));
        assert!(err.to_string().contains("100"));
    }

    #[test]
    fn test_invalid_parameter() {
        let err = PoliastroError::invalid_parameter("mass", -10.0, "must be positive");
        assert!(err.to_string().contains("mass"));
        assert!(err.to_string().contains("-10"));
        assert!(err.to_string().contains("positive"));
    }

    #[test]
    fn test_out_of_range() {
        let err = PoliastroError::out_of_range("eccentricity", 1.5, 0.0, 1.0);
        assert!(err.to_string().contains("eccentricity"));
        assert!(err.to_string().contains("1.5"));
    }

    #[test]
    fn test_invalid_eccentricity() {
        let err = PoliastroError::InvalidEccentricity { value: -0.5 };
        assert!(err.to_string().contains("eccentricity"));
        assert!(err.to_string().contains("-0.5"));
    }

    #[test]
    fn test_division_by_zero() {
        let err = PoliastroError::DivisionByZero {
            context: "orbital period calculation".to_string(),
            divisor: 0.0,
        };
        assert!(err.to_string().contains("Division by zero"));
        assert!(err.to_string().contains("orbital period"));
    }

    #[test]
    fn test_orbital_singularity() {
        let err = PoliastroError::OrbitalSingularity {
            singularity_type: "circular orbit (e=0)".to_string(),
            alternative: "equinoctial elements".to_string(),
        };
        assert!(err.to_string().contains("Singularity"));
        assert!(err.to_string().contains("circular"));
        assert!(err.to_string().contains("equinoctial"));
    }

    #[test]
    fn test_not_implemented() {
        let err = PoliastroError::not_implemented("GPU acceleration");
        assert!(err.to_string().contains("Not implemented"));
        assert!(err.to_string().contains("GPU"));
    }

    #[test]
    fn test_internal_error() {
        let err = PoliastroError::internal("unexpected state in propagator");
        assert!(err.to_string().contains("Internal error"));
        assert!(err.to_string().contains("bug"));
    }

    #[test]
    fn test_energy_conservation() {
        let err = PoliastroError::EnergyNotConserved {
            delta_energy: 1e-6,
            tolerance: 1e-10,
        };
        let msg = err.to_string();
        assert!(msg.contains("Energy"));
        // Check for delta energy value (may be formatted as 0.000001 or 1e-6)
        assert!(msg.contains("0.000001") || msg.contains("1e-6"));
    }

    #[test]
    fn test_incompatible_units() {
        let err = PoliastroError::IncompatibleUnits {
            expected: "meters".to_string(),
            actual: "radians".to_string(),
        };
        assert!(err.to_string().contains("units"));
        assert!(err.to_string().contains("meters"));
        assert!(err.to_string().contains("radians"));
    }
}