Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
orbits.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18#include "shambackends/math.hpp"
19#include "shammath/matrix.hpp"
22
23namespace shamphys {
24
40 inline auto get_binary_pair(double m1, double m2, double a, double e, double nu, double G) {
41
42 double M = m1 + m2;
43 // double mu = m1 * m2 / M;
44
45 double r = a * (1 - e * e) / (1 + e * sycl::cos(nu));
46
47 double x_orb = r * sycl::cos(nu);
48 double y_orb = r * sycl::sin(nu);
49
50 double h = sycl::sqrt(G * M * a * (1 - e * e));
51 double vx_orb = -G * M / h * sycl::sin(nu);
52 double vy_orb = G * M / h * (e + sycl::cos(nu));
53
54 f64_3 r_orb = {x_orb, y_orb, 0};
55 f64_3 v_orb = {vx_orb, vy_orb, 0};
56
57 f64_3 r1 = -m2 / M * r_orb;
58 f64_3 r2 = m1 / M * r_orb;
59
60 f64_3 v1 = -m2 / M * v_orb;
61 f64_3 v2 = m1 / M * v_orb;
62
63 return std::make_tuple(r1, r2, v1, v2);
64 }
65
68 double m1,
69 double m2,
70 double a,
71 double e,
72 double nu,
74
75 double G = shamunits::Constants{usys}.G();
76
77 return get_binary_pair(m1, m2, a, e, nu, G);
78 }
79
87 static f64_3x3 rotation_matrix(double roll, double pitch, double yaw) {
88
89 // clang-format off
90 // Rotation matrix around X-axis (roll)
91 f64_3x3 Rx = {1, 0, 0,
92 0, sycl::cos(roll), -sycl::sin(roll),
93 0, sycl::sin(roll), sycl::cos(roll)};
94
95 // Rotation matrix around Y-axis (pitch)
96 f64_3x3 Ry = {sycl::cos(pitch), 0, sycl::sin(pitch),
97 0, 1, 0,
98 -sycl::sin(pitch), 0, sycl::cos(pitch)};
99
100 // Rotation matrix around Z-axis (yaw)
101 f64_3x3 Rz = {sycl::cos(yaw), -sycl::sin(yaw), 0,
102 sycl::sin(yaw), sycl::cos(yaw), 0,
103 0, 0, 1};
104 // clang-format on
105
106 f64_3x3 Ryx = {};
107 f64_3x3 R = {};
108
109 // Combine the rotations (R = Rz * Ry * Rx)
112
113 return R;
114 }
115
124 static f64_3 rotate_point(const f64_3 &point, double roll, double pitch, double yaw) {
125 shammath::mat<f64, 3, 1> r = {point.x(), point.y(), point.z()};
126
127 // Get the rotation matrix
128 f64_3x3 R = rotation_matrix(roll, pitch, yaw);
129
130 // Perform the rotation by multiplying the point with the rotation matrix
131 shammath::mat<f64, 3, 1> rotated_point = {};
132 shammath::mat_prod(R.get_mdspan(), r.get_mdspan(), rotated_point.get_mdspan());
133
134 return {rotated_point.data[0], rotated_point.data[1], rotated_point.data[2]};
135 }
136
153 double m1,
154 double m2,
155 double a,
156 double e,
157 double nu,
158 double G,
159 double roll,
160 double pitch,
161 double yaw) {
162
163 auto [r1, r2, v1, v2] = get_binary_pair(m1, m2, a, e, nu, G);
164
165 r1 = rotate_point({r1.x(), r1.y(), r1.z()}, roll, pitch, yaw);
166 r2 = rotate_point({r2.x(), r2.y(), r2.z()}, roll, pitch, yaw);
167 v1 = rotate_point({v1.x(), v1.y(), v1.z()}, roll, pitch, yaw);
168 v2 = rotate_point({v2.x(), v2.y(), v2.z()}, roll, pitch, yaw);
169
170 return std::make_tuple(r1, r2, v1, v2);
171 }
172
175 double m1,
176 double m2,
177 double a,
178 double e,
179 double nu,
181 double roll,
182 double pitch,
183 double yaw) {
184 return get_binary_rotated(
185 m1, m2, a, e, nu, shamunits::Constants{usys}.G(), roll, pitch, yaw);
186 }
187
188} // namespace shamphys
Matrix class based on std::array storage and mdspan.
Definition matrix.hpp:36
std::array< T, m *n > data
The matrix data.
Definition matrix.hpp:39
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Definition matrix.hpp:42
Defines a unit system.
void mat_prod(const std::mdspan< Ta, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< Ta, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< Tb, Extents3, Layout3, Accessor3 > &output)
Compute the product of two matrices.
auto get_binary_pair(double m1, double m2, double a, double e, double nu, double G)
Convert a binary system from Keplerian to Cartesian coordinates.
Definition orbits.hpp:40
auto get_binary_rotated(double m1, double m2, double a, double e, double nu, double G, double roll, double pitch, double yaw)
Rotate a binary orbit by Euler angles and return the positions and velocities of the two objects.
Definition orbits.hpp:152
Physical constants.
constexpr T G()
get the value of G in the current unit system units