Debian Codegraph @

src/sol.rs

raw symbols SCIP
1 /*! Solar Dynamics and handling of planets orbits
2
3 This function has two main types, [`Planet`] and [`Sun`] With methods for:
4
5 * Cartesian Coordinates
6 * Distance from earth
7 * Magnitude
8 * Angular Diameter
9
10 Planets also have methods for:
11
12 * Phase angle
13 * Illuminated fraction
14
15 ```
16 use pracstro::{time, sol};
17
18 let now = time::Date::now();
19 for p in sol::PLANETS {
20 let (ra, de) = p.location(now).equatorial();
21 let ((rah, ram, _), (ded, dem, _)) = (ra.clock(), de.to_latitude().degminsec());
22 println!("{:<10} {:>2}h{:02} RA {:>3}°{:02}' De {:.2} AU", p.name, rah, ram, ded, dem, p.distance(now));
23 }
24 ```
25
26 Orbital property and correction numbers from <https://ssd.jpl.nasa.gov/planets/approx_pos.html>
27 */
28
29 use crate::{coord, time};
30
31 /// A blank type that represents the sun
32 pub struct Sun;
33 /// The Sun
34 pub const SUN: Sun = Sun;
35 impl Sun {
36 /// The geocentric rectangular coordinates of the sun relative to the earth, in AU
37 ///
38 /// The inverse of the location of the earth relative to the sun
39 pub fn locationcart(&self, d: time::Date) -> (f64, f64, f64) {
40 let (x, y, z) = EARTH.locationcart(d);
41 (-x, -y, -z)
42 }
43
44 /// Calculate the coordinates of the sun at a given time
45 pub fn location(&self, d: time::Date) -> coord::Coord {
46 let (x, y, z) = self.locationcart(d);
47 coord::Coord::from_cartesian(x, y, z)
48 }
49
50 /// Calculate the distance to the sun, in AU
51 pub fn distance(&self, d: time::Date) -> f64 {
52 let (tx, ty, tz) = self.locationcart(d);
53 (tx * tx + ty * ty + tz * tz).sqrt()
54 }
55
56 /// Calculate the angular diameter of the sun
57 pub fn angdia(&self, d: time::Date) -> time::Angle {
58 time::Angle::from_degrees(0.5333333333) / self.distance(d)
59 }
60
61 /// Visual Magnitude of the sun
62 pub fn magnitude(&self, d: time::Date) -> f64 {
63 5.0 * self.distance(d).log10() - 26.74
64 }
65 }
66
67 /// Generalized Planet Structure containing keplerian orbital properties and corrections.
68 ///
69 /// Ephemeris for planets uses Keplerian motion with correction for perturbations of other planets
70 /// Error is at most 10' for most use, well within range of wanted accuracy.
71 #[derive(Clone, Debug, PartialEq)]
72 pub struct Planet {
73 /// Planet Name
74 pub name: &'static str,
75 /// Semi-Major Axis (AU)
76 pub a: f64,
77 /// Eccentricity
78 pub e: f64,
79 /// Inclination (Degrees)
80 pub i: f64,
81 /// Mean longitude (Degrees)
82 pub l: f64,
83 /// Longitude of the Periapsis (Degrees)
84 pub w: f64,
85 /// Longitude of the ascending node (Degrees)
86 pub o: f64,
87 /// Correction rates for all 6 preceding properties.
88 pub rates: [f64; 6],
89 /// Correction values for the mean anomaly, needed in larger planets
90 pub extra: Option<(f64, f64, f64, f64)>,
91 // Physical Properties
92 /// Angular Diameter at 1AU (Degrees)
93 pub theta0: time::Angle,
94 /// Visual Magnitude at 1AU
95 pub v0: f64,
96 }
97 impl Planet {
98 /// Returns the heliocentric location of the planets as rectangular coordinates as relative to the Sun, in AU
99 ///
100 /// From <https://ssd.jpl.nasa.gov/planets/approx_pos.html>
101 pub fn locationcart(&self, d: time::Date) -> (f64, f64, f64) {
102 let t = d.centuries();
103 let a = self.a + self.rates[0] * t;
104 let e = self.e + self.rates[1] * t;
105 let i = time::Angle::from_degrees(self.i + self.rates[2] * t);
106 let l = time::Angle::from_degrees(self.l + self.rates[3] * t);
107 let w = time::Angle::from_degrees(self.w + self.rates[4] * t);
108 let o = time::Angle::from_degrees(self.o + self.rates[5] * t);
109 let ww = w - o;
110 let mut m = (l - w).degrees();
111 if let Some((b, c, s, f)) = self.extra {
112 m = m + b * t * t + c * ((f * t).to_radians().cos()) + s * ((f * t).to_radians().sin());
113 }
114 m = time::Angle::from_degrees(m).to_latitude().degrees();
115
116 fn kepler(m: f64, e: f64, ee: f64) -> f64 {
117 let dm = m - (ee - e.to_degrees() * (ee.to_radians().sin()));
118 dm / (1.0 - e * (ee.to_radians()).cos())
119 }
120 let mut ee = m + 57.29578 * e * (m.to_radians().sin());
121 let mut de: f64 = 1.0;
122 while de.abs() > 1e-7 {
123 de = kepler(m, e, ee);
124 ee += de;
125 }
126
127 let xp = a * ((ee.to_radians()).cos() - e);
128 let yp = a * (1.0 - e * e).sqrt() * (ee.to_radians().sin());
129
130 let xecl = (ww.cos() * o.cos() - ww.sin() * o.sin() * i.cos()) * xp
131 + (-ww.sin() * o.cos() - ww.cos() * o.sin() * i.cos()) * yp;
132 let yecl = (ww.cos() * o.sin() + ww.sin() * o.cos() * i.cos()) * xp
133 + (-ww.sin() * o.sin() + ww.cos() * o.cos() * i.cos()) * yp;
134 let zecl = (ww.sin() * i.sin()) * xp + (ww.cos() * i.sin()) * yp;
135
136 let eps = 23.43928_f64.to_radians();
137 let tx = xecl;
138 let ty = eps.cos() * yecl - eps.sin() * zecl;
139 let tz = eps.sin() * yecl + eps.cos() * zecl;
140
141 (tx, ty, tz)
142 }
143
144 /// Returns coordinates as subtracted from the earths coordinates
145 pub fn location(&self, d: time::Date) -> coord::Coord {
146 let c = self.locationcart(d);
147 let e = EARTH.locationcart(d);
148
149 coord::Coord::from_cartesian(c.0 - e.0, c.1 - e.1, c.2 - e.2)
150 }
151
152 /// Returns distance in AU
153 pub fn distance(&self, d: time::Date) -> f64 {
154 let c = self.locationcart(d);
155 let e = EARTH.locationcart(d);
156 let (tx, ty, tz) = (c.0 - e.0, c.1 - e.1, c.2 - e.2);
157
158 (tx * tx + ty * ty + tz * tz).sqrt()
159 }
160
161 /// Returns angular diameter of the planet at current time
162 pub fn angdia(&self, d: time::Date) -> time::Angle {
163 self.theta0 / self.distance(d)
164 }
165
166 fn sun_distance(&self, d: time::Date) -> f64 {
167 let (tx, ty, tz) = self.locationcart(d);
168 (tx * tx + ty * ty + tz * tz).sqrt()
169 }
170
171 /// Get apparent magnitude of a planet
172 pub fn magnitude(&self, d: time::Date) -> f64 {
173 5.0 * ((self.distance(d) * self.sun_distance(d)) / self.illumfrac(d).sqrt()).log10()
174 + self.v0
175 }
176
177 /// Gets the phase angle of a planet
178 ///
179 /// This is simple trig work with the triangle between the planet, earth, and sun.
180 pub fn phaseangle(&self, d: time::Date) -> time::Angle {
181 let sep = SUN.location(d).dist(self.location(d));
182 let (tx, ty, tz) = self.locationcart(d);
183 let sp = (tx * tx + ty * ty + tz * tz).sqrt();
184 let upa = time::Angle::asin(SUN.distance(d) * (sep.sin() / sp));
185 if (tx * tx + ty * ty + tz * tz).sqrt() < 1.0 {
186 upa
187 } else {
188 upa + time::Angle::from_degrees(180.0)
189 }
190 }
191
192 /// Gets the illuminated fraction of the planets surface
193 pub fn illumfrac(&self, d: time::Date) -> f64 {
194 // Todo: Replace one with distance to the sun in AU
195 0.5 * (1.0 - self.phaseangle(d).cos())
196 }
197 }
198
199 /// Mercury
200 pub const MERCURY: Planet = Planet {
201 name: "Mercury",
202 a: 0.38709927,
203 e: 0.20563593,
204 i: 7.00497902,
205 l: 252.25032350,
206 w: 77.45779628,
207 o: 48.33076593,
208 rates: [
209 0.00000037,
210 0.00001906,
211 -0.00594749,
212 149472.67411175,
213 0.16047689,
214 -0.12534081,
215 ],
216 extra: None,
217 theta0: time::Angle::from_degrees(0.0017972222),
218 v0: -0.42,
219 };
220 /// Venus
221 pub const VENUS: Planet = Planet {
222 name: "Venus",
223 a: 0.72333566,
224 e: 0.00677672,
225 i: 3.39467605,
226 l: 181.97909950,
227 w: 131.60246718,
228 o: 76.67984255,
229 rates: [
230 0.00000390,
231 -0.00004107,
232 -0.00078890,
233 58517.81538729,
234 0.00268329,
235 -0.27769418,
236 ],
237 extra: None,
238 theta0: time::Angle::from_degrees(0.0047),
239 v0: -4.4,
240 };
241 /// Earth (Technically the Earth-Moon Barycenter)
242 pub const EARTH: Planet = Planet {
243 name: "Earth",
244 a: 1.00000261,
245 e: 0.01671123,
246 i: -0.00001531,
247 l: 100.46457166,
248 w: 102.93768193,
249 o: 0.0,
250 rates: [
251 0.00000562,
252 -0.00004392,
253 -0.01294668,
254 35999.37244981,
255 0.32327364,
256 0.0,
257 ],
258 extra: None,
259 theta0: time::Angle::from_degrees(180.0),
260 v0: -12.0,
261 };
262 /// Mars
263 pub const MARS: Planet = Planet {
264 name: "Mars",
265 a: 1.52371034,
266 e: 0.09339410,
267 i: 1.84969142,
268 l: -4.55343205,
269 w: -23.94362959,
270 o: 49.55953891,
271 rates: [
272 0.00001847,
273 0.00007882,
274 -0.00813131,
275 19140.30268499,
276 0.44441088,
277 -0.29257343,
278 ],
279 extra: None,
280 theta0: time::Angle::from_degrees(0.0026),
281 v0: -1.52,
282 };
283 /// Jupiter
284 pub const JUPITER: Planet = Planet {
285 name: "Jupiter",
286 a: 5.20248019,
287 e: 0.04853590,
288 i: 1.29861416,
289 l: 34.33479152,
290 w: 14.27495244,
291 o: 100.29282654,
292 rates: [
293 -0.00002864,
294 0.00018026,
295 -0.00322699,
296 3034.90371757,
297 0.18199196,
298 0.13024619,
299 ],
300 extra: Some((-0.00012452, 0.06064060, -0.35635438, 38.35125000)),
301 theta0: time::Angle::from_degrees(0.05465),
302 v0: -9.4,
303 };
304 /// Saturn
305 pub const SATURN: Planet = Planet {
306 name: "Saturn",
307 a: 9.54149883,
308 e: 0.05550825,
309 i: 2.49424102,
310 l: 50.07571329,
311 w: 92.86136063,
312 o: 113.63998702,
313 rates: [
314 -0.00003065,
315 -0.00032044,
316 0.00451969,
317 1222.11494724,
318 0.54179478,
319 -0.25015002,
320 ],
321 extra: Some((0.00025899, -0.13434469, 0.87320147, 38.35125000)),
322 theta0: time::Angle::from_degrees(0.046),
323 v0: -8.9,
324 };
325 /// Uranus
326 pub const URANUS: Planet = Planet {
327 name: "Uranus",
328 a: 19.18797948,
329 e: 0.04685740,
330 i: 0.77298127,
331 l: 314.20276625,
332 w: 172.43404441,
333 o: 73.96250215,
334 rates: [
335 -0.00020455,
336 -0.00001550,
337 -0.00180155,
338 428.49512595,
339 0.09266985,
340 0.05739699,
341 ],
342 extra: Some((0.00058331, -0.97731848, 0.17689245, 7.67025000)),
343 theta0: time::Angle::from_degrees(0.0182777777),
344 v0: -7.19,
345 };
346 /// Neptune
347 pub const NEPTUNE: Planet = Planet {
348 name: "Neptune",
349 a: 30.06952752,
350 e: 0.00895439,
351 i: 1.77005520,
352 l: 304.22289287,
353 w: 46.68158724,
354 o: 131.78635853,
355 rates: [
356 0.00006447,
357 0.00000818,
358 0.00022400,
359 218.46515314,
360 0.01009938,
361 -0.00606302,
362 ],
363 extra: Some((-0.00041348, 0.68346318, -0.10162547, 7.67025000)),
364 theta0: time::Angle::from_degrees(0.0172777777),
365 v0: -6.87,
366 };
367 /// Pluto
368 pub const PLUTO: Planet = Planet {
369 name: "Pluto",
370 a: 39.48686035,
371 e: 0.24885238,
372 i: 17.14104260,
373 l: 238.96535011,
374 w: 224.09702598,
375 o: 110.30167986,
376 rates: [
377 0.00449751,
378 0.00006016,
379 0.00000501,
380 145.18042903,
381 -0.00968827,
382 -0.00809981,
383 ],
384 extra: None,
385 theta0: time::Angle::from_degrees(0.0022777777),
386 v0: -1.0,
387 };
388
389 /// Defines the planets in order
390 ///
391 /// Can be used in a iterator to loop over planets
392 ///
393 /// ```
394 /// use pracstro::*;
395 /// for p in sol::PLANETS {
396 /// println!("{} AU", p.distance(time::Date::now()));
397 /// }
398 /// ```
399 pub const PLANETS: [&Planet; 9] = [
400 &MERCURY, &VENUS, &EARTH, &MARS, &JUPITER, &SATURN, &URANUS, &NEPTUNE, &PLUTO,
401 ];
402
403 #[cfg(test)]
404 mod tests {
405 use super::*;
406
407 #[test]
408 fn test_sunpos() {
409 assert_eq!(
410 SUN.location(time::Date::from_julian(2268932.541667)),
411 coord::Coord::from_equatorial(
412 time::Angle::from_degminsec(298, 29, 42.42),
413 time::Angle::from_degminsec(-21, 4, 0.91664)
414 )
415 );
416 }
417
418 // "Is this a reliable way of getting the ecliptic longitude of the sun?"
419 #[test]
420 fn test_lambdasun() {
421 assert_eq!(
422 SUN.location(time::Date::from_calendar(
423 1980,
424 7,
425 27,
426 time::Angle::default()
427 ))
428 .ecliptic(time::Date::from_calendar(
429 1980,
430 7,
431 27,
432 time::Angle::default()
433 ))
434 .0,
435 time::Angle::from_degminsec(124, 23, 40.8)
436 )
437 }
438
439 #[test]
440 fn test_planet() {
441 assert_eq!(
442 VENUS.location(time::Date::from_calendar(
443 2025,
444 3,
445 12,
446 time::Angle::default()
447 )),
448 coord::Coord::from_equatorial(
449 time::Angle::from_clock(0, 17, 44.5),
450 time::Angle::from_degminsec(10, 54, 50.7)
451 )
452 );
453 assert_eq!(
454 JUPITER.location(time::Date::from_julian(2460748.41871)),
455 coord::Coord::from_equatorial(
456 time::Angle::from_clock(4, 47, 10.5),
457 time::Angle::from_degminsec(22, 01, 7.7)
458 )
459 );
460 assert_eq!(
461 MARS.distance(time::Date::from_julian(2460748.41871)),
462 0.9721731869765856
463 );
464 assert_eq!(
465 JUPITER.distance(time::Date::from_julian(2460748.41871)),
466 5.183932727328779
467 );
468 }
469
470 #[test]
471 fn test_phase() {
472 assert_eq!(
473 VENUS.illumfrac(time::Date::from_calendar(
474 2025,
475 03,
476 24,
477 time::Angle::default()
478 )),
479 0.010520980535268565
480 );
481 assert_eq!(
482 MARS.illumfrac(time::Date::from_calendar(
483 2025,
484 03,
485 24,
486 time::Angle::default()
487 )),
488 0.9103262022711702
489 );
490 assert_eq!(
491 VENUS.illumfrac(time::Date::from_calendar(
492 1996,
493 07,
494 22,
495 time::Angle::default()
496 )),
497 0.30982782608980997
498 );
499 }
500 }