SPH
TwoBody.cpp
Go to the documentation of this file.
1 #include "post/TwoBody.h"
2 #include "physics/Constants.h"
3 
5 
7  if (sqr(L[Z]) > (1._f - EPS) * getSqrLength(L)) {
8  // Longitude of the ascending node undefined, return 0 (this is a valid case, not an error the data)
9  return 0._f;
10  } else {
11  return -atan2(L[X], L[Y]);
12  }
13 }
14 
16  if (e < EPS) {
17  return 0._f;
18  }
19  const Float Omega = this->ascendingNode();
20  const Vector OmegaDir(cos(Omega), sin(Omega), 0._f); // direction of the ascending node
21  const Float omega = acos(dot(OmegaDir, getNormalized(K)));
22  if (K[Z] < 0._f) {
23  return 2._f * PI - omega;
24  } else {
25  return omega;
26  }
27 }
28 
30  return a * (1._f - e);
31 }
32 
34  return a * sqrt(1._f - e * e);
35 }
36 
38  const Float mu,
39  const Vector& r,
40  const Vector& v) {
41  const Float E = 0.5_f * mu * getSqrLength(v) - Constants::gravity * M * mu / getLength(r);
42  if (E >= 0._f) {
43  // parabolic or hyperbolic trajectory
44  return NOTHING;
45  }
46 
47  // http://sirrah.troja.mff.cuni.cz/~davok/scripta-NB1.pdf
48  Elements elements;
49  elements.a = -Constants::gravity * mu * M / (2._f * E);
50 
51  const Vector L = mu * cross(r, v); // angular momentum
52  SPH_ASSERT(L != Vector(0._f));
53  elements.i = acos(L[Z] / getLength(L));
54  elements.e = sqrt(1._f + 2._f * E * getSqrLength(L) / (sqr(Constants::gravity) * pow<3>(mu) * sqr(M)));
55 
56  elements.K = cross(v, L) - Constants::gravity * mu * M * getNormalized(r);
57  elements.L = L;
58 
59  return elements;
60 }
61 
62 Float Kepler::solveKeplersEquation(const Float M, const Float e, const Size iterCnt) {
63  Float E = M;
64  for (Size iter = 0; iter < iterCnt; ++iter) {
65  E = E - (E - e * sin(E) - M) / (1._f - e * cos(E));
66  }
67  return E;
68 }
69 
71  const Float cosv = (cos(u) - e) / (1 - e * cos(u));
72  const Float sinv = sqrt(1 - sqr(e)) * sin(u) / (1._f - e * cos(u));
73  return atan2(sinv, cosv);
74 }
75 
77  const Float cosu = (e + cos(v)) / (1 + e * cos(v));
78  const Float sinu = sqrt(1 - sqr(e)) * sin(v) / (1 + e * cos(v));
79  return atan2(sinu, cosu);
80 }
81 
82 Vector Kepler::position(const Float a, const Float e, const Float u) {
83  return a * Vector(cos(u) - e, sqrt(1 - sqr(e)) * sin(u), 0);
84 }
85 
86 Vector Kepler::velocity(const Float a, const Float e, const Float u, const Float n) {
87  return n * a / (1 - e * cos(u)) * Vector(-sin(u), sqrt(1 - sqr(e)) * cos(u), 0);
88 }
89 
90 Float Kepler::meanMotion(const Float a, const Float m_total) {
91  return sqrt(Constants::gravity * m_total / pow<3>(a));
92 }
93 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Definitions of physical constaints (in SI units).
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE T sin(const T f)
Definition: MathUtils.h:296
INLINE T cos(const T f)
Definition: MathUtils.h:291
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
constexpr INLINE Float pow< 3 >(const Float v)
Definition: MathUtils.h:132
INLINE T acos(const T f)
Definition: MathUtils.h:306
INLINE T atan2(const T y, const T x)
Definition: MathUtils.h:321
constexpr Float E
Definition: MathUtils.h:365
constexpr Float PI
Mathematical constants.
Definition: MathUtils.h:361
#define NAMESPACE_SPH_END
Definition: Object.h:12
const NothingType NOTHING
Definition: Optional.h:16
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
INLINE BasicVector< float > cross(const BasicVector< float > &v1, const BasicVector< float > &v2)
Cross product between two vectors.
Definition: Vector.h:559
BasicVector< Float > Vector
Definition: Vector.h:539
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
INLINE Vector getNormalized(const Vector &v)
Definition: Vector.h:590
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
constexpr Float gravity
Gravitational constant (CODATA 2014)
Definition: Constants.h:29
Optional< Elements > computeOrbitalElements(const Float M, const Float mu, const Vector &r, const Vector &v)
Computes the orbital elements, given position and velocity of a body.
Definition: TwoBody.cpp:37
Float trueAnomalyToEccentricAnomaly(const Float v, const Float e)
Computes the eccentric anomaly from the true anomaly and the eccentricity.
Definition: TwoBody.cpp:76
Float eccentricAnomalyToTrueAnomaly(const Float u, const Float e)
Computes the true anomaly from the eccentric anomaly and the eccentricity.
Definition: TwoBody.cpp:70
Float solveKeplersEquation(const Float M, const Float e, const Size iterCnt=10)
Computes the eccentric anomaly by solving the Kepler's equation.
Definition: TwoBody.cpp:62
Float meanMotion(const Float a, const Float m_total)
Computes the mean motion from the Kepler's 3rd law.
Definition: TwoBody.cpp:90
Vector position(const Float a, const Float e, const Float u)
Computes the position on the elliptic trajectory.
Definition: TwoBody.cpp:82
Vector velocity(const Float a, const Float e, const Float u, const Float n)
Computes the velocity vector on the elliptic trajectory.
Definition: TwoBody.cpp:86
Object holding Keplerian orbital elements of a body.
Definition: TwoBody.h:15
Float e
Excentricity.
Definition: TwoBody.h:17
Float periapsisArgument() const
Computes the argument of periapsis of the orbit. In the singular case e=0, returns 0.
Definition: TwoBody.cpp:15
Vector K
Laplace vector, integral of motion with direction towards pericenter.
Definition: TwoBody.h:21
Float pericenterDist() const
Computes the distance of the pericenter.
Definition: TwoBody.cpp:29
Float ascendingNode() const
Computes the longitude of the ascending node. In the singular case i=0, reutrns 0.
Definition: TwoBody.cpp:6
Float i
Inclination with respect to the z=0 plane.
Definition: TwoBody.h:18
Float a
Semi-major axis.
Definition: TwoBody.h:16
Float semiminorAxis() const
Computes the semi-minor axis.
Definition: TwoBody.cpp:33
Vector L
Angular momentum, perpendicular to the orbital plane.
Definition: TwoBody.h:20