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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//!
//! # Methods for astronomy
//!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
use ;
use ;
use constant;
use Spherical;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// # Radec coordinate system
///
/// ## Definition
/// Right ascension and declination of the object in the sky. The values are stored as `f64` internally, and in radians.
/// The distance is kept in meters internally, for homogeneousness.
///
/// ## Example
/// ```
/// # use scilib::astronomy::Radec;
/// let radec = Radec { ra: 0.3, dec: 1.2, dist_earth: None };
/// assert_eq!(radec.ra, 0.3);
/// assert_eq!(radec.dec, 1.2);
/// assert_eq!(radec.dist_earth, None);
/// ```
/// # Display
///
/// Shows both ra and dec in degrees, which are more easily readable.
/// Implementing required methods
/// # Conversion to spherical coordinates
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// # Apparent magnitude
///
/// ## Definition
/// Uses the standard reference luminosity to compute the apparent magnitude of an object.
/// $$
/// m = -2.5\log\left( \frac{F}{F_\circ} \right)
/// $$
/// Where $F$ is the irradiance of the object, computed with $F = I(L, d)$
/// and $F_\circ$ is the apparent bolometric luminosity.
///
/// ## Inputs
/// - `lum`: luminosity of the object ($L$), in watts ($W$)
/// - `dist`: distance from the observer ($d$), in meters ($m$)
///
/// Returns the apparent magnitude of the object, dimensionless.
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::apparent_mag;
/// let alpha_b = 0.4 * constant::SUN_L; // Alpha centauri B, roughly
/// let distance = 4.403 * constant::LY; // Seen from Earth
/// let res = apparent_mag(alpha_b, distance);
/// assert!((res - 1.36).abs() <= 0.03);
/// ```
/// # Absolute magnitude
///
/// ## Definition
/// Same approach that the apparent magnitude, but set at a distance of 10pc.
/// $$
/// M = -2.5\log\left( \frac{L}{L_\circ} \right)
/// $$
/// Where $L_\circ$ is the absolute bolometric magnitude.
///
/// ## Inputs
/// - `lum`: luminosity of the object ($L$), in watts ($W$)
///
/// Returns the magnitude of the object, dimensionless.
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::absolute_mag;
/// let abs = absolute_mag(constant::SUN_L); // Sun absolute magnitude
/// assert!((abs - 4.74).abs() <= 0.02); // As defined IAU
/// ```
/// # Distance modulus
///
/// ## Definition
/// Computes the distance of an object based on its apparent and absolute magnitudes:
/// $$
/// d = 10^{1 + \frac{m-M}{5}}
/// $$
/// Where $m$ is the apparent magnitude and $M$ is the absolute magnitude.
///
/// ## Inputs
/// - `m_app`: apparent magnitude ($m$), dimensionless
/// - `m_abs`: absolute magnitude ($M$), dimensionless
///
/// Returns the distance in parsecs ($pc$).
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::{ apparent_mag, absolute_mag, distance_mod };
/// let app = apparent_mag(constant::SUN_L, 10.0 * constant::PC);
/// let abs = absolute_mag(constant::SUN_L);
/// let res = distance_mod(app, abs) / constant::PC;
/// assert!((res - 10.0).abs() <= 1e-5);
/// ```
/// # Equilibrium temperature
///
/// ## Definition
/// Knowing the stellar effective temperature, its radius, the distance from the star
/// and the albedo of the considered planet, we can compute the equilibrium temperature
/// found at the planet, computed with:
/// $$
/// T_\mathrm{eq} = T_\mathrm{star} \sqrt{\frac{R_\mathrm{star}}{2d}} (1 - A)
/// $$
/// Where $T_\mathrm{star}$ and $R_\mathrm{star}$ are the effective temperature of the host star and its radius,
/// $d$ is the distance from the star and $A$ is the albedo of the planet.
///
/// ## Inputs
/// - `star_t`: star effective temperature ($T_\mathrm{star}$), in kelvins ($K$)
/// - `star_rad`: star radius ($R_\mathrm{star}$), in meters ($m$)
/// - `dist`: distance to host star ($d$), in meters ($m$)
/// - `albedo`: albedo of the planet ($A$), dimensionless
///
/// Returns the temperature in kelvins ($K$).
/// # Object irradiance
///
/// ## Definition
/// Computes the irradiance at a certain distance from an object, using its luminosity.
/// The `luminosity` is in Watts and the `distance` is in meters, result is in `W.m-2`.
/// The formula used is:
/// $$
/// I(L, d) = \frac{L}{4\pi d^2}
/// $$
/// Where $L$ is the luminosity and $d$ is the distance.
///
/// ## Inputs
/// - `luminosity`: luminosity of the object ($L$), in watts ($W$)
/// - `distance`: distance form the object ($d$), in meters ($m$)
///
/// Returns the irradiance in watt per square meters ($W.m^{-2}$)
///
/// ## Example
/// ```rust
/// # use scilib::constant;
/// # use scilib::astronomy;
/// // Computing the solar irradiance at the Earth
/// let p_earth: f64 = astronomy::irradiance(constant::SUN_L, constant::AU);
/// assert!((p_earth - 1361.0).abs() < 1.0);
///
/// // Computing the irradiance from the sun at Alpha-Centauri
/// let p: f64 = astronomy::irradiance(constant::SUN_L, 4.3 * constant::LY);
/// assert!((p - 1.84e-8).abs() < 0.1e-8);
/// ```
/// # Energy received by an object
///
/// ## Definition
/// Computes the received energy by an object of a given surface, at a known distance.
/// Makes use of the `irradiance` function to compute the surfacic power output by the object,
/// it is defined as:
/// $$
/// E = I(L, d) S
/// $$
/// Where $I(L, d)$ is the irradiance function and $S$ is the surface of the object.
///
/// ## Inputs
/// - `luminosity`: luminosity of the object ($L$), in watts ($W$)
/// - `distance`: distance form the object ($d$), in meters ($m$)
/// - `surface`: exposed surface of the object ($S$), in square meters ($m^2$)
///
/// Returns the irradiance of the object in watts ($W$).
/// # Planetary luminosity
///
/// ## Definition
/// Computes the luminosity of a planet, based on the received irradiance and the albedo:
/// $$
/// L_\mathrm{P} = E_\mathrm{received} A = I(L, d) S A
/// $$
/// Where $S$ is the surface, $A$ the albedo, and $E_\mathrm{received} = I(L, d) S$ is the energy
/// received by the planet.
///
/// ## Inputs
/// - `albedo`: albedo of the object ($A$), dimensionless
/// - `received`: power received by the object ($I(L, d)$), in watts ($W$)
///
/// Returns the luminosity in watts ($W$).
/// # Luminosity using Stefan-Boltzmann
///
/// ## Definition
/// Computes the expected luminosity of a star using the Stefan-Boltzmann constant, from:
/// $$
/// L = 4\pi\sigma R^2 T^4
/// $$
///
/// ## Inputs
/// - `radius`: radius of the star ($R$), in meters ($m$)
/// - `temperature`: effective temperature of the star ($T$), in kelvins ($K$)
///
/// Returns the luminosity of the star in watts ($W$).
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::{ luminosity, absolute_mag };
/// let sun = luminosity(constant::SUN_RADIUS, constant:: SUN_TEFF);
/// assert!((sun - constant::SUN_L).abs() / constant::SUN_L <= 1.0e-3);
///
/// let computed = absolute_mag(sun);
/// let theory = absolute_mag(constant::SUN_L);
/// assert!((computed - theory).abs() / theory <= 1.0e-4);
/// ```
/// # Rayleigh criterion
/// ## Definition
/// The Rayleigh criterion qualifies the minimum separation of two points that can be resolved
/// as two distinct points by an instrument. It equates simply as:
/// $$
/// \epsilon = 1.22\frac{\lambda}{D}
/// $$
/// Where $\lambda$ is the considered wavelength and $D$ is the diameter of the instrument.
///
/// ## Inputs
/// - `lambda`: considered wavelength ($\lambda$), in meters ($m$)
/// - `d`: the diameter of the instrument ($d$), in meters ($m$)
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::rayleigh_criterion;
/// let wave: f64 = 500.0 * constant::NANO; // Greenish light
/// let diam: f64 = 8.2; // VLT diameter
/// let criterion = rayleigh_criterion(wave, diam);
/// assert!((criterion - 7.43902439024e-8).abs() < 1.0e-8)
/// ```
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// # Orbital speed of an object
/// ## Definition
/// Computation for the velocity of an object on an elliptical orbit. The definition is:
/// $$
/// v = \sqrt{GM\left( \frac{2}{R} - \frac{1}{a} \right)}
/// $$
/// Where $G$ is the gravitational constant, $M$ is the mass of the orbited body, $R$ is the current distance
/// of the object from the center of mass of the orbited body and $a$ is the semi-major axis of the orbit.
///
/// ## Inputs
/// - `mass`: the mass of the center body ($M$), in kilograms ($kg$)
/// - `r`: distance from the center of focal point ($R$), in meters ($m$)
/// - `a`: semi-major axis of the orbit ($a$), in meters ($m$)
///
/// Returns $v$, the velocity of an object at a given altitude.
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::orbital_speed;
/// let rad = constant::EARTH_RADIUS + 421.0e3; // ISS perigee
/// let semi = 6795.0e3; // ISS semi-major axis
/// let v = orbital_speed(constant::EARTH_MASS, rad, semi);
/// assert!((v - 7654.416466312335).abs() < 1.0e-10);
/// ```
/// # Impact parameter $b$
/// ## Definition
/// The impact parameter for an exoplanet orbiting a star is defined as:
/// $$
/// b = \frac{a\cos(i)}{R_\mathrm{star}} \left( \frac{1-e^2}{1 + e\sin(\omega)} \right)
/// $$
///
/// ## Inputs
/// - `a`: semi-major axis ($a$), in meters ($m$)
/// - `radius_star`: the radius of the host star ($R_s$), in meters ($m$)
/// - `i`: inclination of the planet's orbit ($i$), in degrees
/// - `e`: eccentricity of the planet ($e$)
/// - `w`: argument of periapsis of the orbit ($\omega$), in degrees
///
/// Returns $b$, the impact parameter of the planet.
///
/// ## Example
/// ```
/// # use scilib::constant;
/// # use scilib::astronomy::impact_parameter;
/// let r_star: f64 = 0.834 * constant::SUN_RADIUS;
/// let a: f64 = 0.07697 * constant::AU;
/// let i: f64 = 88.7;
/// let e: f64 = 0.0;
/// let w: f64 = 0.0;
/// let b: f64 = impact_parameter(a, r_star, i, e, w);
///
/// assert!((b - 0.474).abs() <= 0.025);
/// ```
///# Total transit duration
///
/// ## Definition
/// The total duration of the transit, from the **start** of the ingress to the **end** of the egress.
/// $$
/// T_{\mathrm{tot}} = \frac{P}{\pi}\arcsin\left( \frac{R_\mathrm{star} \sqrt{(1+k)^2 - b^2}}{a\sin(i)} \right)
/// $$
///
/// ## Inputs
/// - `p`: the period of the planet ($P$), in the desired units
/// - `r_star`: the stellar radius ($R_\mathrm{star}$), in meters ($m$)
/// - `a`: semi-major axis ($a$), in meters ($m$)
/// - `k`: planet to star ratio ($k$)
/// - `b`: impact parameter of the system ($b$)
/// - `i`: inclination of the planet's orbit ($i$), in degrees
///
/// Returns the total transit duration, in the same unit as the period.
///# Full transit duration
///
/// ## Definition
/// The duration of the full transit, from the **end** of the ingress to the **start** of the egress.
/// $$
/// T_{\mathrm{full}} = \frac{P}{\pi}\arcsin\left( \frac{R_\mathrm{star} \sqrt{(1-k)^2 - b^2}}{a\sin(i)} \right)
/// $$
///
/// ## Inputs
/// - `p`: the period of the planet ($P$), in the desired units
/// - `r_star`: the stellar radius ($R_\mathrm{star}$), in meters ($m$)
/// - `a`: semi-major axis ($a$), in meters ($m$)
/// - `k`: planet to star ratio ($k$)
/// - `b`: impact parameter of the system ($b$)
/// - `i`: inclination of the planet's orbit ($i$), in degrees
///
/// Returns the full transit duration, in the same unit as the period.
/// # Transit probability
///
/// ## Definitions
/// The probability is computed based on the occulted fraction of the line of longitude.
/// Allowing for grazing eclipse, we have:
/// $$
/// p = \frac{R_\mathrm{star} + R_\mathrm{planet}}{a} \frac{1 + e\sin(\omega)}{1 - e^2}
/// $$
///
/// ## Inputs
/// - `r_star`: the stellar radius ($R_\mathrm{star}$), in meters ($m$)
/// - `r_planet`: the planetary radius ($R_\mathrm{planet}$), in meters ($m$)
/// - `a`: semi-major axis ($a$), in meters ($m$)
/// - `e`: eccentricity of the planet ($e$)
/// - `w`: argument of periapsis of the orbit ($\omega$), in degrees
///
/// Returns the probability of transit.
/// # Hill radius
/// ## Definition
/// The  gives an estimate of the sphere
/// of influence of an object given its mass and its parent body mass. This function provides
/// the standard approximation for this value, which is:
/// $$
/// r_H\approx a(1 - e)\sqrt\[3\]{\frac{m}{3M}}
/// $$
///
/// ## Inputs
/// - `m`: mass of the considered body ($m$), in kilograms ($kg$)
/// - `m_parent`: mass of the parent body ($M$), in kilograms ($kg$)
/// - `a`: semi-major axis of the considered body ($a$), in meters ($m$)
/// - `e`: eccentricity of the considered body ($e$), unit less
///
/// Returns the approximate Hill radius of the considered body.
///
/// ## Example
/// ```
/// # use scilib::astronomy::hill_radius;
/// # use scilib::constant as cst;
/// let earth_hill_sphere: f64 = hill_radius(cst::EARTH_MASS, cst::SUN_MASS, cst::AU, 0.01671022);
/// assert!((earth_hill_sphere / 1e9 - 1.4714).abs() < 0.2e-3);
/// ```
/// # Exact Hill radius
/// ## Definitions
/// Similar to `hill_radius`, but solves the equation of the Hill radius:
/// $$
/// \frac{m}{r_H^2} - \frac{M}{r^2}\left( 1 - \frac{2r_H}{r} \right)^{-2} +
/// \frac{M}{r^2}\left( 1 - \frac{r_H}{r} \right) = 0
/// $$
/// This is more costly than the approximation, but yields more accurate values.
/// Note that $m<<M$ for the equation to be valid.
///
/// ## Inputs
/// - `m`: mass of the considered body ($m$), in kilograms ($kg$)
/// - `m_parent`: mass of the parent body ($M$), in kilograms ($kg$)
/// - `r`: distance between both objects ($r$), in meters ($m$)
///
/// Returns the exact hill radius of an object.
/// # The actual Hill radius equation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////