Antkeeper  0.0.1
anomaly.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_PHYSICS_ORBIT_ANOMALY_HPP
21 #define ANTKEEPER_PHYSICS_ORBIT_ANOMALY_HPP
22 
23 #include <engine/math/numbers.hpp>
24 #include <cmath>
25 
26 namespace physics {
27 namespace orbit {
28 
32 namespace anomaly {
33 
41 template <class T>
42 T true_to_eccentric(T ec, T ta);
43 
51 template <class T>
52 T true_to_mean(T ec, T ta);
53 
61 template <class T>
62 T eccentric_to_true(T ec, T ea);
63 
71 template <class T>
72 T eccentric_to_mean(T ec, T ea);
73 
85 template <class T>
86 T mean_to_eccentric(T ec, T ma, std::size_t iterations, T tolerance);
87 
97 template <class T>
98 T mean_to_true(T ec, T ma, std::size_t iterations, T tolerance);
99 
100 template <class T>
101 T true_to_eccentric(T ec, T ta)
102 {
103  // Parabolic orbit
104  if (ec == T(1))
105  return std::tan(ta * T(0.5));
106 
107  // Hyperbolic orbit
108  if (ec > T(1))
109  return std::acosh((ec + std::cos(ta)) / (T(1) + ec * std::cos(ta))) * ((ta < T(0)) ? T(-1) : T(1));
110 
111  // Elliptic orbit
112  return std::atan2(std::sqrt(T(1) - ec * ec) * std::sin(ta), std::cos(ta) + ec);
113 }
114 
115 template <class T>
116 T true_to_mean(T ec, T ta)
117 {
118  return eccentric_to_mean(ec, true_to_eccentric(ec, ta));
119 }
120 
121 template <class T>
122 T eccentric_to_true(T ec, T ea)
123 {
124  // Parabolic orbit
125  if (ec == T(1))
126  return std::atan(ea) * T(2);
127 
128  // Hyperbolic orbit
129  if (ec > T(1))
130  return std::atan(std::sqrt((ec + T(1)) / (ec - T(1))) * std::tanh(ea * T(0.5))) * T(2);
131 
132  // Elliptic orbit
133  return std::atan2(sqrt(T(1) - ec * ec) * std::sin(ea), std::cos(ea) - ec);
134 }
135 
136 template <class T>
137 T eccentric_to_mean(T ec, T ea)
138 {
139  // Parabolic orbit
140  if (ec == T(1))
141  return (ea * ea * ea) / T(6) + ea * T(0.5);
142 
143  // Hyperbolic orbit
144  if (ec > T(1))
145  return ec * std::sinh(ea) - ea;
146 
147  // Elliptic orbit
148  return ea - ec * std::sin(ea);
149 }
150 
151 template <class T>
152 T mean_to_eccentric(T ec, T ma, std::size_t iterations, T tolerance)
153 {
154  // Wrap mean anomaly to `[-pi, pi]`
155  ma = std::remainder(ma, math::two_pi<T>);
156 
157  // Third-order approximation of eccentric anomaly starting value, E0
158  const T t33 = std::cos(ma);
159  const T t34 = ec * ec;
160  const T t35 = t34 * ec;
161  T ea0 = ma + (T(-0.5) * t35 + ec + (t34 + T(1.5) * t33 * t35) * t33) * std::sin(ma);
162 
163  // Iteratively converge E0 and E1
164  for (std::size_t i = 0; i < iterations; ++i)
165  {
166  // Third-order approximation of eccentric anomaly, E1
167  const T t1 = std::cos(ea0);
168  const T t2 = T(-1) + ec * t1;
169  const T t3 = std::sin(ea0);
170  const T t4 = ec * t3;
171  const T t5 = -ea0 + t4 + ma;
172  const T t6 = t5 / (T(0.5) * t5 * t4 / t2 + t2);
173  const T ea1 = ea0 - (t5 / ((T(0.5) * t3 - (T(1) / T(6)) * t1 * t6) * ec * t6 + t2));
174 
175  // Determine solution error
176  const T error = std::abs(ea1 - ea0);
177 
178  // Set E0 to E1
179  ea0 = ea1;
180 
181  // Break if solution is within error tolerance
182  if (error < tolerance)
183  break;
184  }
185 
186  return ea0;
187 }
188 
189 template <class T>
190 T mean_to_true(T ec, T ma, std::size_t iterations, T tolerance)
191 {
192  eccentric_to_true(ec, mean_to_eccentric(ec, ma, iterations, tolerance));
193 }
194 
195 } // namespace anomaly
196 } // namespace orbit
197 } // namespace physics
198 
199 #endif // ANTKEEPER_PHYSICS_ORBIT_ANOMALY_HPP
constexpr vector< T, N > abs(const vector< T, N > &x)
Returns the absolute values of each element.
Definition: vector.hpp:985
vector< T, N > sqrt(const vector< T, N > &x)
Takes the square root of each element.
T mean_to_true(T ec, T ma, std::size_t iterations, T tolerance)
Iteratively derives the true anomaly given eccentricity and mean anomaly.
Definition: anomaly.hpp:190
T mean_to_eccentric(T ec, T ma, std::size_t iterations, T tolerance)
Iteratively derives the eccentric anomaly given eccentricity and mean anomaly.
Definition: anomaly.hpp:152
T eccentric_to_mean(T ec, T ea)
Derives the mean anomaly given eccentricity and eccentric anomaly.
Definition: anomaly.hpp:137
T true_to_mean(T ec, T ta)
Derives the mean anomaly given eccentricity and true anomaly.
Definition: anomaly.hpp:116
T eccentric_to_true(T ec, T ea)
Derives the true anomaly given eccentricity and eccentric anomaly.
Definition: anomaly.hpp:122
T true_to_eccentric(T ec, T ta)
Derives the eccentric anomaly given eccentricity and true anomaly.
Definition: anomaly.hpp:101
Physics.
Definition: constants.hpp:23