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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Eclipse Detection and Solar Lighting Conditions
//!
//! This module provides algorithms for determining satellite eclipse conditions,
//! solar beta angle calculations, and sun-synchronous orbit analysis.
//!
//! # Overview
//!
//! Satellites in Earth orbit periodically pass through Earth's shadow, experiencing
//! eclipses that affect:
//! - **Solar panel power generation** - Zero power during umbra
//! - **Thermal conditions** - Rapid temperature changes
//! - **Attitude control** - Star tracker availability
//! - **Battery cycling** - Charge/discharge patterns
//!
//! # Shadow Regions
//!
//! Earth's shadow consists of two regions:
//! - **Umbra**: Complete shadow where the Sun is fully blocked by Earth
//! - **Penumbra**: Partial shadow where the Sun is partially blocked
//!
//! # Shadow Models
//!
//! This implementation uses the **conical shadow model** which:
//! - Treats Earth's shadow as a cone (not cylinder)
//! - Accounts for the Sun's finite angular size
//! - Provides accurate penumbra calculations
//! - Works for all orbit altitudes (LEO, MEO, GEO)
//!
//! ## Algorithm
//!
//! Following Vallado's Algorithm 34:
//!
//! 1. **Check night side**: dot(r_sat, r_sun) < 0
//! 2. **Calculate shadow angles**:
//! - Umbra angle: α_u = arctan((R_☉ - R_⊕) / |r_sun|)
//! - Penumbra angle: α_p = arctan((R_☉ + R_⊕) / |r_sun|)
//! 3. **Calculate satellite angle**:
//! - θ_sat = arcsin(R_⊕ / |r_sat|)
//! 4. **Calculate shadow angle**:
//! - θ_shadow = angle between (-r_sun) and r_sat
//! 5. **Determine state**:
//! - Umbra: θ_shadow < α_u + θ_sat
//! - Penumbra: θ_shadow < α_p + θ_sat
//! - Sunlit: otherwise
//!
//! # Beta Angle
//!
//! The **solar beta angle** (β) is the angle between a satellite's orbital
//! plane and the geocentric Sun vector. It determines eclipse duration:
//!
//! - **β = 0°**: Maximum eclipse duration (orbit plane contains Sun)
//! - **β = 90°**: No eclipses (orbit plane perpendicular to Sun)
//! - **|β| > ~70°**: Continuous sunlight for typical LEO satellites
//!
//! Formula:
//! ```text
//! β = arcsin[sin(i)·cos(Ω - λ_☉)]
//! ```
//! where:
//! - i = orbital inclination
//! - Ω = right ascension of ascending node (RAAN)
//! - λ_☉ = ecliptic longitude of Sun
//!
//! For more precise calculations (Vallado):
//! ```text
//! β = arcsin[cos(Γ)·sin(Ω)·sin(i) - sin(Γ)·cos(ε)·cos(Ω)·sin(i) + sin(Γ)·sin(ε)·cos(i)]
//! ```
//! where:
//! - Γ = ecliptic true solar longitude
//! - ε = obliquity of ecliptic (≈ 23.45°)
//!
//! # Sun-Synchronous Orbits
//!
//! A **sun-synchronous orbit** maintains a constant angle between the orbital
//! plane and the Sun direction by matching the orbital precession rate to
//! Earth's orbital rate around the Sun (≈0.9856°/day).
//!
//! Required RAAN rate:
//! ```text
//! dΩ/dt = 0.9856° / day
//! ```
//!
//! For a circular orbit with J2 perturbations:
//! ```text
//! dΩ/dt = -3/2 · (R_⊕/a)² · J2 · n · cos(i)
//! ```
//!
//! Solving for inclination:
//! ```text
//! i = arccos(-dΩ/dt / (3/2 · (R_⊕/a)² · J2 · n))
//! ```
//!
//! # References
//!
//! - **Vallado, D. A.** (2013). Fundamentals of Astrodynamics and Applications (4th ed.).
//! Algorithm 34: Eclipse shadow calculation
//! - **Curtis, H. D.** (2013). Orbital Mechanics for Engineering Students (3rd ed.).
//! Chapter 3: Orbital elements, Section 12: Eclipse prediction
//! - **Wertz, J. R., & Larson, W. J.** (1999). Space Mission Analysis and Design (3rd ed.).
//! Chapter 5: Spacecraft thermal control
//! - <https://en.wikipedia.org/wiki/Beta_angle>
//! - <https://en.wikipedia.org/wiki/Sun-synchronous_orbit>
use Vector3;
use PI;
use cratePoliastroResult;
/// Eclipse state for a satellite
/// Constants for eclipse calculations
/// Determine eclipse state for a satellite position
///
/// Uses the conical shadow model (Vallado Algorithm 34) to determine whether
/// a satellite is in sunlight, penumbra, or umbra.
///
/// # Arguments
///
/// * `r_sat` - Satellite position vector in ECI frame (m)
/// * `r_sun` - Sun position vector from Earth in ECI frame (m)
///
/// # Returns
///
/// The eclipse state (Sunlit, Penumbra, or Umbra)
///
/// # Algorithm
///
/// 1. Check if satellite is on night side: dot(r_sat, r_sun) < 0
/// 2. Calculate umbra and penumbra angles
/// 3. Calculate satellite's angular radius from Earth
/// 4. Determine shadow state based on angular comparisons
///
/// # Example
///
/// ```rust,ignore
/// use nalgebra::Vector3;
/// use astrora_core::satellite::eclipse::compute_eclipse_state;
///
/// // Satellite at 400 km altitude (LEO)
/// let r_sat = Vector3::new(6778e3, 0.0, 0.0);
/// // Sun at 1 AU
/// let r_sun = Vector3::new(1.496e11, 0.0, 0.0);
///
/// let state = compute_eclipse_state(&r_sat, &r_sun);
/// // state will be Sunlit (satellite and sun on same side)
/// ```
/// Calculate solar beta angle for a satellite orbit
///
/// The beta angle (β) is the angle between the orbital plane and the Sun vector.
/// It determines eclipse duration and thermal conditions.
///
/// # Arguments
///
/// * `inclination` - Orbital inclination (radians)
/// * `raan` - Right Ascension of Ascending Node (radians)
/// * `solar_longitude` - Ecliptic longitude of the Sun (radians)
///
/// # Returns
///
/// Beta angle in radians, range [-π/2, +π/2]
///
/// # Formula
///
/// Simplified:
/// ```text
/// β = arcsin[sin(i)·sin(Ω - λ_☉)]
/// ```
///
/// For small beta angles and circular orbits, this approximation is sufficient.
///
/// # Example
///
/// ```rust,ignore
/// use astrora_core::satellite::eclipse::solar_beta_angle;
/// use std::f64::consts::PI;
///
/// // ISS-like orbit: 51.6° inclination, RAAN = 90°
/// let i = 51.6_f64.to_radians();
/// let raan = (90.0_f64).to_radians();
/// let solar_lon = 0.0; // Sun at vernal equinox
///
/// let beta = solar_beta_angle(i, raan, solar_lon);
/// println!("Beta angle: {:.2}°", beta.to_degrees());
/// ```
/// Calculate precise solar beta angle using full Vallado formula
///
/// This is more accurate than the simplified version, accounting for
/// Earth's axial tilt (obliquity of ecliptic).
///
/// # Arguments
///
/// * `inclination` - Orbital inclination (radians)
/// * `raan` - Right Ascension of Ascending Node (radians)
/// * `solar_longitude` - True ecliptic longitude of Sun (radians, Γ)
///
/// # Returns
///
/// Beta angle in radians, range [-π/2, +π/2]
///
/// # Formula (Vallado)
///
/// ```text
/// β = arcsin[cos(Γ)·sin(Ω)·sin(i) - sin(Γ)·cos(ε)·cos(Ω)·sin(i) + sin(Γ)·sin(ε)·cos(i)]
/// ```
/// where ε ≈ 23.44° is the obliquity of the ecliptic
///
/// # Example
///
/// ```rust,ignore
/// use astrora_core::satellite::eclipse::solar_beta_angle_precise;
///
/// let i = 51.6_f64.to_radians();
/// let raan = (90.0_f64).to_radians();
/// let solar_lon = 0.0;
///
/// let beta = solar_beta_angle_precise(i, raan, solar_lon);
/// ```
/// Calculate required inclination for a sun-synchronous orbit
///
/// A sun-synchronous orbit precesses at the same rate as Earth orbits the Sun,
/// maintaining a constant angle between the orbital plane and Sun direction.
///
/// # Arguments
///
/// * `semi_major_axis` - Semi-major axis of the orbit (m)
/// * `eccentricity` - Orbital eccentricity (0 for circular)
/// * `j2` - Earth's J2 zonal harmonic coefficient (default: 1.08263e-3)
/// * `earth_radius` - Earth's equatorial radius (m, default: 6378137.0)
/// * `mu` - Earth's gravitational parameter (m³/s², default: 3.986004418e14)
///
/// # Returns
///
/// Required inclination in radians for sun-synchronous orbit
///
/// # Formula
///
/// Required RAAN rate for sun-synchronous:
/// ```text
/// dΩ/dt = 0.9856°/day ≈ 1.991e-7 rad/s
/// ```
///
/// J2 perturbation RAAN rate:
/// ```text
/// dΩ/dt = -3/2 · (R_e/a)² · J2 · n · cos(i)
/// ```
///
/// Where n is the mean motion:
/// ```text
/// n = sqrt(μ/a³)
/// ```
///
/// Solving for i:
/// ```text
/// i = arccos(-dΩ/dt / (3/2 · (R_e/a)² · J2 · n))
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use astrora_core::satellite::eclipse::sun_synchronous_inclination;
/// use astrora_core::core::constants::{EARTH_MU, EARTH_RADIUS, EARTH_J2};
///
/// // Typical sun-sync orbit at 600 km
/// let altitude = 600e3; // m
/// let a = EARTH_RADIUS + altitude;
///
/// let inclination = sun_synchronous_inclination(a, 0.0, EARTH_J2, EARTH_RADIUS, EARTH_MU)?;
/// println!("Sun-sync inclination: {:.2}°", inclination.to_degrees());
/// // Output: ~97.8° (retrograde orbit)
/// ```
/// Calculate eclipse duration for a circular orbit
///
/// Estimates the maximum eclipse duration per orbit based on beta angle
/// and orbit altitude.
///
/// # Arguments
///
/// * `semi_major_axis` - Semi-major axis of the orbit (m)
/// * `beta_angle` - Solar beta angle (radians)
/// * `mu` - Earth's gravitational parameter (m³/s²)
///
/// # Returns
///
/// Eclipse duration in seconds (0 if no eclipse)
///
/// # Formula
///
/// For circular orbit, the eclipse fraction is approximately:
/// ```text
/// f_eclipse = (1/π) · arccos[sqrt(h² - R²) / (R·|sin(β)|)]
/// ```
/// where:
/// - h = semi-major axis
/// - R = Earth radius
/// - β = beta angle
///
/// Then: eclipse_duration = f_eclipse · orbital_period
///
/// For |β| > β_critical, no eclipse occurs, where:
/// ```text
/// β_critical = arcsin(R/h)
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use astrora_core::satellite::eclipse::eclipse_duration;
/// use astrora_core::core::constants::{EARTH_MU, EARTH_RADIUS};
///
/// // ISS at 400 km altitude
/// let a = EARTH_RADIUS + 400e3;
/// let beta = 0.0; // Maximum eclipse
///
/// let duration = eclipse_duration(a, beta, EARTH_MU)?;
/// println!("Eclipse duration: {:.1} minutes", duration / 60.0);
/// // Output: ~37 minutes
/// ```