ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
CalcXdotPhiDotGravityJ2J3.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16
17#ifndef CALC_XDOT_GRAVITY_J2_J3_HPP
18#define CALC_XDOT_GRAVITY_J2_J3_HPP
19
20#include "utils/Rates.hpp"
22
23#define NUM_STATES_GRAV 6
24#define XDOT_PHIDOT_GRAVITY_J2_J3_STATES 42
25
26namespace clockwerk {
27
28 template <typename T>
30 public:
31 /// @brief Function to calculate rate of change in 6-element orbit position with J2, J3
32 /// @param time The reference time
33 /// @param state The state vector for the system as [x, y, z, xDot, yDot, zDot; phi as 42x1]
34 /// @param out_rates Implicit return of rates based on time, state as [xDot, yDot, zDot, xDDot, yDDot, zDDot, phiDot as 42x1]
35 int calculateRates(T time, const std::array<T, XDOT_PHIDOT_GRAVITY_J2_J3_STATES> &state,
36 std::array<T, XDOT_PHIDOT_GRAVITY_J2_J3_STATES> &out_rates);
37
38 /// @brief The gravitational parameter for the planet. Must be set prior to calculation
39 T mu;
40
41 /// @brief The J2 parameter for the planet. Must be set prior to calculation.
42 T J2;
43
44 /// @brief The J3 parameter for the planet. Must be set prior to calculation.
45 T J3;
46
47 /// @brief The reference radius for the planet. Must be set prior to calculation.
48 T R;
49 private:
50 // A matrix for rate of phidot
53 };
54
55 template <typename T>
56 int CalcXdotPhiDotGravityJ2J3<T>::calculateRates(T time, const std::array<T, XDOT_PHIDOT_GRAVITY_J2_J3_STATES> &state,
57 std::array<T, XDOT_PHIDOT_GRAVITY_J2_J3_STATES> &out_rates) {
58 // Precalculate some parameters
59 T muRR = mu*R*R;
60 T pc_j2 = muRR*J2;
61 T pc_j3 = muRR*J3*R;
62 T r = std::sqrt(state[0]*state[0] + state[1]*state[1] + state[2]*state[2]);
63 T r3 = r*r*r;
64 T r5 = r3*r*r;
65 T r7 = r5*r*r;
66 T r9 = r7*r*r;
67 T z2 = state[2]*state[2];
68
69 // Now precalc divided parameters
70 T mu_over_r3, pc_15_2_r7, pc_3_2_r5, pc_35_2_r9;
71 int err = safeDivide(-mu, r3, mu_over_r3); if(err) {return err;}
72 err = safeDivide(7.5*state[2], r7, pc_15_2_r7); if(err) {return err;}
73 err = safeDivide(1.5, r5, pc_3_2_r5); if(err) {return err;}
74 err = safeDivide(17.5*z2*state[2], r9, pc_35_2_r9); if(err) {return err;}
75
76 // Actually calculate gravity
77 out_rates[0] = state[3];
78 out_rates[1] = state[4];
79 out_rates[2] = state[5];
80 // mu; J2; J3
81 out_rates[3] = mu_over_r3*state[0] +
82 pc_j2*(pc_15_2_r7*state[0]*state[2] - pc_3_2_r5*state[0]) +
83 pc_j3*(-pc_15_2_r7*state[0] + pc_35_2_r9*state[0]);
84 out_rates[4] = mu_over_r3*state[1] +
85 pc_j2*(pc_15_2_r7*state[1]*state[2] - pc_3_2_r5*state[1]) +
86 pc_j3*(-pc_15_2_r7*state[1] + pc_35_2_r9*state[1]);
87 out_rates[5] = mu_over_r3*state[2] +
88 pc_j2*(pc_15_2_r7*z2 - 3.0*pc_3_2_r5*state[2]) +
89 pc_j3*(-2.0*pc_15_2_r7*state[2] + pc_35_2_r9*state[2] + pc_3_2_r5);
90
91 // Now calculate based on our Phi matrix
92 _phi_tmp.setFromArray(&state[6]); // Reconstruct our phi from input array
93 calcDfDzGravityJ2J3(state, mu, J2, J3, R, _A); // Calculate our A matrix
94 _phi_tmp = _A*_phi_tmp; // Calculate phi dot
95 _phi_tmp.getAsArray(&out_rates[6]); // Set on our array
96
97 return NO_ERROR;
98 }
99
100}
101
102#endif
#define NUM_STATES_GRAV
Definition CalcXdotPhiDotGravityJ2J3.hpp:23
#define XDOT_PHIDOT_GRAVITY_J2_J3_STATES
Definition CalcXdotPhiDotGravityJ2J3.hpp:24
Definition CalcXdotPhiDotGravityJ2J3.hpp:29
T J2
The J2 parameter for the planet. Must be set prior to calculation.
Definition CalcXdotPhiDotGravityJ2J3.hpp:42
T R
The reference radius for the planet. Must be set prior to calculation.
Definition CalcXdotPhiDotGravityJ2J3.hpp:48
int calculateRates(T time, const std::array< T, 42 > &state, std::array< T, 42 > &out_rates)
Function to calculate rate of change in 6-element orbit position with J2, J3.
Definition CalcXdotPhiDotGravityJ2J3.hpp:56
T J3
The J3 parameter for the planet. Must be set prior to calculation.
Definition CalcXdotPhiDotGravityJ2J3.hpp:45
T mu
The gravitational parameter for the planet. Must be set prior to calculation.
Definition CalcXdotPhiDotGravityJ2J3.hpp:39
Matrix math implementation.
Definition Matrix.hpp:54
Definition Rates.hpp:27
#define NO_ERROR
Definition clockwerkerrors.h:31