JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGInertial.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGInertial.cpp
4 Author: Jon S. Berndt
5 Date started: 09/13/00
6 Purpose: Encapsulates the inertial frame forces (coriolis and centrifugal)
7
8 ------------- Copyright (C) 2000 Jon S. Berndt (jon@jsbsim.org) -------------
9
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 2 of the License, or (at your option) any
13 later version.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 details.
19
20 You should have received a copy of the GNU Lesser General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc., 59
22 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 Further information about the GNU Lesser General Public License can also be
25 found on the world wide web at http://www.gnu.org.
26
27FUNCTIONAL DESCRIPTION
28--------------------------------------------------------------------------------
29
30HISTORY
31--------------------------------------------------------------------------------
3209/13/00 JSB Created
33
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35INCLUDES
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38#include "FGFDMExec.h"
39#include "FGInertial.h"
40#include "input_output/FGXMLElement.h"
41#include "input_output/FGLog.h"
42#include "GeographicLib/Geodesic.hpp"
43
44using namespace std;
45
46namespace JSBSim {
47
48/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49CLASS IMPLEMENTATION
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52
53FGInertial::FGInertial(FGFDMExec* fgex)
54 : FGModel(fgex)
55{
56 Name = "Earth";
57
58 // Earth defaults
59 double RotationRate = 0.00007292115;
60 GM = 14.0764417572E15; // WGS84 value
61 J2 = 1.08262982E-03; // WGS84 value for J2
62 a = 20925646.32546; // WGS84 semimajor axis length in feet
63 b = 20855486.5951; // WGS84 semiminor axis length in feet
64 gravType = gtWGS84;
65
66 // Lunar defaults
67 /*
68 double RotationRate = 0.0000026617;
69 GM = 1.7314079E14; // Lunar GM
70 J2 = 2.033542482111609E-4; // value for J2
71 a = 5702559.05; // semimajor axis length in feet
72 b = 5695439.63; // semiminor axis length in feet
73 */
74
75 vOmegaPlanet = { 0.0, 0.0, RotationRate };
76 GroundCallback = std::make_unique<FGDefaultGroundCallback>(a, b);
77
78 bind();
79
80 Debug(0);
81}
82
83//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85FGInertial::~FGInertial(void)
86{
87 Debug(1);
88}
89
90//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92bool FGInertial::Load(Element* el)
93{
94 if (!Upload(el, true)) return false;
95
96 Name = el->GetAttributeValue("name");
97
98 if (el->FindElement("semimajor_axis"))
99 a = el->FindElementValueAsNumberConvertTo("semimajor_axis", "FT");
100 else if (el->FindElement("equatorial_radius"))
101 a = el->FindElementValueAsNumberConvertTo("equatorial_radius", "FT");
102 if (el->FindElement("semiminor_axis"))
103 b = el->FindElementValueAsNumberConvertTo("semiminor_axis", "FT");
104 else if (el->FindElement("polar_radius"))
105 b = el->FindElementValueAsNumberConvertTo("polar_radius", "FT");
106 // Trigger GeographicLib exceptions if the equatorial or polar radii are
107 // ill-defined.
108 // This intercepts the exception before being thrown by a destructor.
109 GeographicLib::Geodesic geod(a, 1.-b/a);
110
111 if (el->FindElement("rotation_rate")) {
112 double RotationRate = el->FindElementValueAsNumberConvertTo("rotation_rate", "RAD/SEC");
113 vOmegaPlanet = {0., 0., RotationRate};
114 }
115 if (el->FindElement("GM"))
116 GM = el->FindElementValueAsNumberConvertTo("GM", "FT3/SEC2");
117 if (el->FindElement("J2"))
118 J2 = el->FindElementValueAsNumber("J2"); // Dimensionless
119
120 GroundCallback->SetEllipse(a, b);
121
122 // Messages to warn the user about possible inconsistencies.
123 if (debug_lvl > 0) {
124 FGLogging log(LogLevel::WARN);
125 if (a != b && J2 == 0.0)
126 log << "Gravitational constant J2 is null for a non-spherical planet." << endl;
127 if (a == b && J2 != 0.0)
128 log << "Gravitational constant J2 is non-zero for a spherical planet." << endl;
129 }
130
131 Debug(2);
132
133 return true;
134}
135
136//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138bool FGInertial::Run(bool Holding)
139{
140 // Fast return if we have nothing to do ...
141 if (FGModel::Run(Holding)) return true;
142 if (Holding) return false;
143
144 // Gravitation accel
145 switch (gravType) {
146 case gtStandard:
147 {
148 double radius = in.Position.GetRadius();
149 vGravAccel = -(GetGAccel(radius) / radius) * in.Position;
150 }
151 break;
152 case gtWGS84:
153 vGravAccel = GetGravityJ2(in.Position);
154 break;
155 }
156
157 return false;
158}
159
160//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162FGMatrix33 FGInertial::GetTl2ec(const FGLocation& location) const
163{
164 FGColumnVector3 North, Down, East{-location(eY), location(eX), 0.};
165
166 switch (gravType) {
167 case gtStandard:
168 {
169 Down = location;
170 Down *= -1.0;
171 }
172 break;
173 case gtWGS84:
174 {
175 FGLocation sea_level = location;
176 sea_level.SetPositionGeodetic(location.GetLongitude(),
177 location.GetGeodLatitudeRad(), 0.0);
178 Down = GetGravityJ2(location);
179 Down -= vOmegaPlanet*(vOmegaPlanet*sea_level);}
180 }
181 Down.Normalize();
182 East.Normalize();
183 North = East*Down;
184
185 return FGMatrix33{North(eX), East(eX), Down(eX),
186 North(eY), East(eY), Down(eY),
187 North(eZ), 0.0, Down(eZ)};
188}
189
190//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191
192double FGInertial::GetGAccel(double r) const
193{
194 return GM/(r*r);
195}
196
197//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198//
199// Calculate the WGS84 gravitation value in ECEF frame. Pass in the ECEF
200// position via the position parameter. The J2Gravity value returned is in ECEF
201// frame, and therefore may need to be expressed (transformed) in another frame,
202// depending on how it is used. See Stevens and Lewis eqn. 1.4-16.
203
204FGColumnVector3 FGInertial::GetGravityJ2(const FGLocation& position) const
205{
206 FGColumnVector3 J2Gravity;
207
208 // Gravitation accel
209 double r = position.GetRadius();
210 double sinLat = sin(position.GetLatitude());
211
212 double adivr = a/r;
213 double preCommon = 1.5*J2*adivr*adivr;
214 double xy = 1.0 - 5.0*(sinLat*sinLat);
215 double z = 3.0 - 5.0*(sinLat*sinLat);
216 double GMOverr2 = GM/(r*r);
217
218 J2Gravity(1) = -GMOverr2 * ((1.0 + (preCommon * xy)) * position(eX)/r);
219 J2Gravity(2) = -GMOverr2 * ((1.0 + (preCommon * xy)) * position(eY)/r);
220 J2Gravity(3) = -GMOverr2 * ((1.0 + (preCommon * z)) * position(eZ)/r);
221
222 return J2Gravity;
223}
224
225//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
227void FGInertial::SetAltitudeAGL(FGLocation& location, double altitudeAGL)
228{
229 FGColumnVector3 vDummy;
230 FGLocation contact;
231 contact.SetEllipse(a, b);
232 GroundCallback->GetAGLevel(location, contact, vDummy, vDummy, vDummy);
233 double groundHeight = contact.GetGeodAltitude();
234 double longitude = location.GetLongitude();
235 double geodLat = location.GetGeodLatitudeRad();
236 location.SetPositionGeodetic(longitude, geodLat,
237 groundHeight + altitudeAGL);
238}
239
240//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242void FGInertial::SetGravityType(int gt)
243{
244 // Messages to warn the user about possible inconsistencies.
245 FGLogging log(LogLevel::WARN);
246 switch (gt)
247 {
248 case eGravType::gtStandard:
249 if (a != b)
250 log << "Standard gravity model has been set for a non-spherical planet" << endl;
251 break;
252 case eGravType::gtWGS84:
253 if (J2 == 0.0)
254 log << "WGS84 gravity model has been set without specifying the J2 gravitational constant." << endl;
255 }
256
257 gravType = gt;
258}
259
260//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
262void FGInertial::bind(void)
263{
264 PropertyManager->Tie("inertial/sea-level-radius_ft", &in.Position,
265 &FGLocation::GetSeaLevelRadius);
266 PropertyManager->Tie("simulation/gravity-model", this, &FGInertial::GetGravityType,
267 &FGInertial::SetGravityType);
268}
269
270//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271// The bitmasked value choices are as follows:
272// unset: In this case (the default) JSBSim would only print
273// out the normally expected messages, essentially echoing
274// the config files as they are read. If the environment
275// variable is not set, debug_lvl is set to 1 internally
276// 0: This requests JSBSim not to output any messages
277// whatsoever.
278// 1: This value explicity requests the normal JSBSim
279// startup messages
280// 2: This value asks for a message to be printed out when
281// a class is instantiated
282// 4: When this value is set, a message is displayed when a
283// FGModel object executes its Run() method
284// 8: When this value is set, various runtime state variables
285// are printed out periodically
286// 16: When set various parameters are sanity checked and
287// a message is printed out when they go out of bounds
288
289void FGInertial::Debug(int from)
290{
291 if (debug_lvl <= 0) return;
292
293 if (debug_lvl & 1) { // Standard console startup message output
294 FGLogging log(LogLevel::DEBUG);
295 if (from == 0) {} // Constructor
296 if (from == 2) { // Loading
297 log << endl << " Planet " << Name << endl
298 << " Semi major axis: " << a << endl
299 << " Semi minor axis: " << b << endl
300 << " Rotation rate : " << scientific << vOmegaPlanet(eZ) << endl
301 << " GM : " << GM << endl
302 << " J2 : " << J2 << endl << defaultfloat << endl;
303 }
304 }
305 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
306 FGLogging log(LogLevel::DEBUG);
307 if (from == 0) log << "Instantiated: FGInertial" << endl;
308 if (from == 1) log << "Destroyed: FGInertial" << endl;
309 }
310 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
311 }
312 if (debug_lvl & 8 ) { // Runtime state variables
313 }
314 if (debug_lvl & 16) { // Sanity checking
315 }
316 if (debug_lvl & 64) {
317 if (from == 0) { // Constructor
318 }
319 }
320}
321}
This class implements a 3 element column vector.
FGColumnVector3 & Normalize(void)
Normalize.
FGLocation holds an arbitrary location in the Earth centered Earth fixed reference frame (ECEF).
Definition FGLocation.h:152
void SetEllipse(double semimajor, double semiminor)
Sets the semimajor and semiminor axis lengths for this planet.
double GetGeodLatitudeRad(void) const
Get the GEODETIC latitude in radians.
Definition FGLocation.h:258
double GetLongitude() const
Get the longitude.
Definition FGLocation.h:234
double GetGeodAltitude(void) const
Gets the geodetic altitude in feet.
Definition FGLocation.h:279
void SetPositionGeodetic(double lon, double lat, double height)
Sets the longitude, latitude and the distance above the reference spheroid.
Handles matrix math operations.
Definition FGMatrix33.h:70
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71