Astro  0.0.0
A C++ library for space dynamics
Loading...
Searching...
No Matches
Utilities.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Astro project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * e-mail: davide.stocco@unitn.it e-mail: enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef ASTRO_UTILITIES_HH
14#define ASTRO_UTILITIES_HH
15
16#include "Astro.hh"
17
18namespace Astro {
19
20 /*\
21 | ____
22 | | _ \ _____ _____ _ __
23 | | |_) / _ \ \ /\ / / _ \ '__|
24 | | __/ (_) \ V V / __/ |
25 | |_| \___/ \_/\_/ \___|_|
26 |
27 \*/
28
34 Real Power2(Real x) {return x*x;}
35
41 template<typename Real>
42 Real Power3(Real x) {return x*x*x;}
43
49 Real Power4(Real x) {return x*x*x*x;}
50
51 /*\
52 | _ _
53 | / \ _ __ __ _| | ___
54 | / _ \ | '_ \ / _` | |/ _ \
55 | / ___ \| | | | (_| | | __/
56 | /_/ \_\_| |_|\__, |_|\___|
57 | |___/
58 \*/
59
60 static Real const PI = Real(3.141592653589793238462643383279502884197);
61 static Real const PIMUL2 = Real(6.283185307179586476925286766559005768394);
62 static Real const PIDIV2 = Real(1.570796326794896619231321691639751442098);
63 static Real const DEG_TO_RAD = Real(0.017453292519943295769236907684886127134);
64 static Real const RAD_TO_DEG = Real(57.29577951308232087679815481410517033240);
65
73
81
89 {
90 x = std::fmod(x, PIMUL2);
91 while (x < Real(0.0)) {x += PIMUL2;}
92 while (x > PIMUL2) {x -= PIMUL2;}
93 return x;
94 }
95
103 {
104 x = std::fmod(x, PIMUL2);
105 while (x < -PI) {x += PIMUL2;}
106 while (x > PI) {x -= PIMUL2;}
107 return x;
108 }
109
110 /*\
111 | ____ _ _
112 | | _ \‍(_)___| |_ __ _ _ __ ___ ___
113 | | | | | / __| __/ _` | '_ \ / __/ _ \
114 | | |_| | \__ \ || (_| | | | | (_| __/
115 | |____/|_|___/\__\__,_|_| |_|\___\___|
116 |
117 \*/
118
119 static Real const AU_TO_KM{1.49597870707e+08};
120 static Real const KM_TO_AU{1.0/AU_TO_KM};
121 static Real const KM_TO_M{1000.0};
122 static Real const M_TO_KM{1.0/KM_TO_M};
123 static Real const AU_TO_M{AU_TO_KM*KM_TO_M};
124 static Real const M_TO_AU{AU_TO_KM*KM_TO_M};
125
126 // Time units
127 static Real const DAY_TO_SEC{86400.0};
128 static Real const SEC_TO_DAY{1.0/DAY_TO_SEC};
129
130 static Real const KG_M_SEC2_TO_KG_AU_DAY2{M_TO_AU/(SEC_TO_DAY*SEC_TO_DAY)}; // Kg * m/s^2 => Kg * UA / day^2
131
132 static Real const gravity_kg_m_s2{9.80665};
134
135
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137
138 Real KM_To_M(Real x) {return x * KM_TO_M;}
139 Real M_To_KM(Real x) {return x * M_TO_KM;}
140
141 Real KM_To_AU(Real x) {return x * KM_TO_AU;}
142 Real AU_To_KM(Real x) {return x * AU_TO_KM;}
143
144 Real M_To_AU(Real x) {return x * M_TO_AU;}
145 Real AU_To_M(Real x) {return x * AU_TO_M;}
146
147 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148
149 Real KM_S_To_M_S(Real x) {return x * KM_TO_M;}
150 Real M_S_To_KM_S(Real x) {return x * M_TO_KM;}
151
152 Real KM_S_To_AU_S(Real x) {return x * (KM_TO_AU/SEC_TO_DAY);}
154
155 Real M_S_To_AU_S(Real x) {return x * (M_TO_AU/SEC_TO_DAY);}
157
158 Real M_S_To_AU_DAY(Real x) {return x * (M_TO_AU/DAY_TO_SEC);}
160
161 Real KM_S_To_AU_DAY(Real x) {return x * (KM_TO_AU/DAY_TO_SEC);}
163
164 Real M_S_To_KM_DAY(Real x) {return x * (M_TO_KM/DAY_TO_SEC);}
166
167 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168
170
171 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172
174
175 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176
177 Real KM3_S2_To_KM3_DAY2(Real x) {return x * (1.0/(SEC_TO_DAY*SEC_TO_DAY));}
178
179 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180
181 Real KM3_S2_To_AU3_DAY2(Real x) {return x * (1.0/(AU_TO_KM*AU_TO_KM*AU_TO_KM)/(SEC_TO_DAY*SEC_TO_DAY));}
182
183 /*\
184 | _____ _
185 | |_ _(_)_ __ ___ ___
186 | | | | | '_ ` _ \ / _ \
187 | | | | | | | | | | __/
188 | |_| |_|_| |_| |_|\___|
189 |
190 \*/
191
200 {
201 if (month <= 2)
202 {
203 year -= 1;
204 month += 12;
205 }
206 Integer A{year / 100};
207 Integer B{2 - A + (A / 4)};
208 Real JD{std::floor(365.25 * (year + 4716))
209 + std::floor(30.6001 * (month + 1))
210 + day + B - 1524.5};
211 return JD;
212 }
213
214 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
215
224 {
225 return JulianDate(year, month, day) - Real{2400000.5};
226 }
227
228 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
229
237 void JD_To_Date(Real JD, Integer & year, Integer & month, Integer & day)
238 {
239 Integer Z{Integer(std::floor(JD + 0.5))};
240 Real F{(JD + 0.5) - Z};
241 Integer A{Z};
242 if (Z >= 2299161)
243 {
244 Integer alpha{Integer((Z - 1867216.25) / 36524.25)};
245 A = Z + 1 + alpha - alpha / 4;
246 }
247 Integer B{A + 1524};
248 Integer C{Integer((B - 122.1) / 365.25)};
249 Integer D{Integer(365.25 * C)};
250 Integer E{Integer((B - D) / 30.6001)};
251 Real dayD{B - D - std::floor(30.6001 * E) + F};
252 day = Integer(dayD);
253 if (E < 14)
254 month = E - 1;
255 else
256 month = E - 13;
257 if (month > 2)
258 year = C - 4716;
259 else
260 year = C - 4715;
261 }
262
263 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
264
272 void MJD_To_Date(Real MJD, Integer & year, Integer & month, Integer & day)
273 {
274 JD_To_Date(MJD + Real{2400000.5}, year, month, day);
275 }
276
277 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
278
279} // namespace Astro
280
281#endif // ASTRO_UTILITIES_HH
The namespace for the Astro library.
Definition Astro.hh:73
static Real const DAY_TO_SEC
Definition Utilities.hh:127
static Real const PI
Definition Utilities.hh:60
Real M_S_To_AU_S(Real x)
Definition Utilities.hh:155
Real KM_To_AU(Real x)
Definition Utilities.hh:141
Real KM_S_To_AU_S(Real x)
Definition Utilities.hh:152
Real KM3_S2_To_KM3_DAY2(Real x)
Definition Utilities.hh:177
Real AU_S_To_KM_S(Real x)
Definition Utilities.hh:153
Real AU_DAY_To_KM_S(Real x)
Definition Utilities.hh:162
int Integer
Definition Astro.hh:85
Real Power2(Real x)
Definition Utilities.hh:34
static Real const AU_TO_M
Definition Utilities.hh:123
Real JulianDate(Integer year, Integer month, Integer day)
Definition Utilities.hh:199
Real M_To_AU(Real x)
Definition Utilities.hh:144
Real AU_S_To_M_S(Real x)
Definition Utilities.hh:156
Real M_S_To_KM_S(Real x)
Definition Utilities.hh:150
static Real const KG_M_SEC2_TO_KG_AU_DAY2
Definition Utilities.hh:130
Real Deg_To_Rad(Real x)
Definition Utilities.hh:72
Real AU_DAY_To_M_S(Real x)
Definition Utilities.hh:159
Real Power4(Real x)
Definition Utilities.hh:49
Real AU_DAY3_To_KM_S3(Real x)
Definition Utilities.hh:173
static Real const RAD_TO_DEG
Definition Utilities.hh:64
Real Power3(Real x)
Definition Utilities.hh:42
static Real const DEG_TO_RAD
Definition Utilities.hh:63
Real AngleInRange(Real x)
Definition Utilities.hh:88
Real AU_To_M(Real x)
Definition Utilities.hh:145
static Real const KM_TO_M
Definition Utilities.hh:121
static Real const AU_TO_KM
Definition Utilities.hh:119
Real Rad_To_Deg(Real x)
Definition Utilities.hh:80
Real KM_DAY_To_M_S(Real x)
Definition Utilities.hh:165
void MJD_To_Date(Real MJD, Integer &year, Integer &month, Integer &day)
Definition Utilities.hh:272
Real AngleInRangeSym(Real x)
Definition Utilities.hh:102
double Real
Definition Astro.hh:84
Real KM_S_To_M_S(Real x)
Definition Utilities.hh:149
Real M_To_KM(Real x)
Definition Utilities.hh:139
Real M_S_To_AU_DAY(Real x)
Definition Utilities.hh:158
Real M_S_To_KM_DAY(Real x)
Definition Utilities.hh:164
Real KM_S_To_AU_DAY(Real x)
Definition Utilities.hh:161
void JD_To_Date(Real JD, Integer &year, Integer &month, Integer &day)
Definition Utilities.hh:237
Real AU_To_KM(Real x)
Definition Utilities.hh:142
static Real const gravity_kg_AU_DAY2
Definition Utilities.hh:133
Real KM3_S2_To_AU3_DAY2(Real x)
Definition Utilities.hh:181
Real ModifiedJulianDate(Integer year, Integer month, Integer day)
Definition Utilities.hh:223
static Real const PIMUL2
Definition Utilities.hh:61
static Real const gravity_kg_m_s2
Definition Utilities.hh:132
Real KM_To_M(Real x)
Definition Utilities.hh:138
Real AU_DAY2_To_KM_S2(Real x)
Definition Utilities.hh:169
static Real const PIDIV2
Definition Utilities.hh:62