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
FGEngine.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGEngine.cpp
4 Author: Jon Berndt
5 Date started: 01/21/99
6 Called by: FGAircraft
7
8 ------------- Copyright (C) 1999 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 ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 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--------------------------------------------------------------------------------
29See header file.
30
31HISTORY
32--------------------------------------------------------------------------------
3301/21/99 JSB Created
3409/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
35 += Thrust (thanks to Tony Peden)
36
37%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include "FGEngine.h"
42#include "FGPropeller.h"
43#include "FGNozzle.h"
44#include "FGRotor.h"
45#include "input_output/FGXMLElement.h"
46
47using namespace std;
48
49namespace JSBSim {
50
51/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52CLASS IMPLEMENTATION
53%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55FGEngine::FGEngine(int engine_number, struct Inputs& input)
56 : in(input), EngineNumber(engine_number)
57{
58 Type = etUnknown;
59 SLFuelFlowMax = 0.0;
60 FuelExpended = 0.0;
61 MaxThrottle = 1.0;
62 MinThrottle = 0.0;
63 FuelDensity = 6.02;
64 Debug(0);
65}
66
67//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69FGEngine::~FGEngine()
70{
71 delete Thruster;
72 Debug(1);
73}
74
75//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76
77void FGEngine::ResetToIC(void)
78{
79 Starter = false;
80 FuelExpended = 0.0;
81 Starved = Running = Cranking = false;
82 PctPower = 0.0;
83 FuelFlow_gph = 0.0;
84 FuelFlow_pph = 0.0;
85 FuelFlowRate = 0.0;
86 FuelFreeze = false;
87 FuelUsedLbs = 0.0;
88 Thruster->ResetToIC();
89}
90
91//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92
93double FGEngine::CalcFuelNeed(void)
94{
95 FuelFlowRate = SLFuelFlowMax*PctPower;
96 FuelExpended = FuelFlowRate*in.TotalDeltaT;
97 if (!Starved) FuelUsedLbs += FuelExpended;
98 return FuelExpended;
99}
100
101//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103unsigned int FGEngine::GetSourceTank(unsigned int i) const
104{
105 if (i < SourceTanks.size()) {
106 return SourceTanks[i];
107 } else {
108 throw("No such source tank is available for this engine");
109 }
110}
111
112//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114double FGEngine::GetThrust(void) const
115{
116 return Thruster->GetThrust();
117}
118
119//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120
121const FGColumnVector3& FGEngine::GetBodyForces(void)
122{
123 return Thruster->GetBodyForces();
124}
125
126//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128const FGColumnVector3& FGEngine::GetMoments(void)
129{
130 return Thruster->GetMoments();
131}
132
133//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134
135void FGEngine::LoadThrusterInputs()
136{
137 Thruster->in.TotalDeltaT = in.TotalDeltaT;
138 Thruster->in.H_agl = in.H_agl;
139 Thruster->in.PQRi = in.PQRi;
140 Thruster->in.AeroPQR = in.AeroPQR;
141 Thruster->in.AeroUVW = in.AeroUVW;
142 Thruster->in.Density = in.Density;
143 Thruster->in.Pressure = in.Pressure;
144 Thruster->in.Soundspeed = in.Soundspeed;
145 Thruster->in.Alpha = in.alpha;
146 Thruster->in.Beta = in.beta;
147 Thruster->in.Vt = in.Vt;
148}
149
150//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152void FGEngine::LoadThruster(FGFDMExec* exec, Element *thruster_element)
153{
154 if (thruster_element->FindElement("propeller")) {
155 Element *document = thruster_element->FindElement("propeller");
156 Thruster = new FGPropeller(exec, document, EngineNumber);
157 } else if (thruster_element->FindElement("nozzle")) {
158 Element *document = thruster_element->FindElement("nozzle");
159 Thruster = new FGNozzle(exec, document, EngineNumber);
160 } else if (thruster_element->FindElement("rotor")) {
161 Element *document = thruster_element->FindElement("rotor");
162 Thruster = new FGRotor(exec, document, EngineNumber);
163 } else if (thruster_element->FindElement("direct")) {
164 Element *document = thruster_element->FindElement("direct");
165 Thruster = new FGThruster(exec, document, EngineNumber);
166 } else {
167 XMLLogException err(thruster_element);
168 err << " Unknown thruster type\n";
169 throw err;
170 }
171
172 Debug(2);
173}
174
175//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176
177bool FGEngine::Load(FGFDMExec *exec, Element *engine_element)
178{
179 Element* parent_element = engine_element->GetParent();
180 Element* local_element;
181 FGColumnVector3 location, orientation;
182
183 auto PropertyManager = exec->GetPropertyManager();
184
185 Name = engine_element->GetAttributeValue("name");
186
187 // Call ModelFunctions loader
188 FGModelFunctions::Load(engine_element, exec, to_string((int)EngineNumber));
189
190 // If engine location and/or orientation is supplied issue a warning since they
191 // are ignored. What counts is the location and orientation of the thruster.
192 local_element = parent_element->FindElement("location");
193 if (local_element) {
194 FGLogging log(LogLevel::WARN);
195 log << "Engine location ignored, only thruster location is used.\n";
196 }
197
198 local_element = parent_element->FindElement("orient");
199 if (local_element) {
200 FGLogging log(LogLevel::WARN);
201 log << "Engine orientation ignored, only thruster orientation is used.\n";
202 }
203
204 // Load thruster
205 local_element = parent_element->FindElement("thruster");
206 if (local_element) {
207 try {
208 LoadThruster(exec, local_element);
209 } catch (std::string& str) {
210 XMLLogException err(local_element);
211 err << "Error loading engine " << Name << ". " << str << "\n";
212 throw err;
213 } catch (LogException &e) {
214 XMLLogException err(e, local_element);
215 throw err;
216 }
217 } else {
218 FGLogging log(LogLevel::ERROR);
219 log << "No thruster definition supplied with engine definition.\n";
220 }
221
222 ResetToIC(); // initialize dynamic terms
223
224 // Load feed tank[s] references
225 local_element = parent_element->FindElement("feed");
226 while (local_element) {
227 int tankID = (int)local_element->GetDataAsNumber();
228 SourceTanks.push_back(tankID);
229 local_element = parent_element->FindNextElement("feed");
230 }
231
232 string property_name, base_property_name;
233 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
234
235 property_name = base_property_name + "/set-running";
236 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
237 property_name = base_property_name + "/thrust-lbs";
238 PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
239 property_name = base_property_name + "/fuel-flow-rate-pps";
240 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
241 property_name = base_property_name + "/fuel-flow-rate-gph";
242 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRateGPH);
243 property_name = base_property_name + "/fuel-used-lbs";
244 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelUsedLbs);
245
246 PostLoad(engine_element, exec, to_string((int)EngineNumber));
247
248 Debug(0);
249
250 return true;
251}
252
253//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254// The bitmasked value choices are as follows:
255// unset: In this case (the default) JSBSim would only print
256// out the normally expected messages, essentially echoing
257// the config files as they are read. If the environment
258// variable is not set, debug_lvl is set to 1 internally
259// 0: This requests JSBSim not to output any messages
260// whatsoever.
261// 1: This value explicity requests the normal JSBSim
262// startup messages
263// 2: This value asks for a message to be printed out when
264// a class is instantiated
265// 4: When this value is set, a message is displayed when a
266// FGModel object executes its Run() method
267// 8: When this value is set, various runtime state variables
268// are printed out periodically
269// 16: When set various parameters are sanity checked and
270// a message is printed out when they go out of bounds
271
272void FGEngine::Debug(int from)
273{
274 if (debug_lvl <= 0) return;
275
276 if (debug_lvl & 1) { // Standard console startup message output
277 if (from == 0) { // Constructor
278
279 }
280 if (from == 2) { // After thruster loading
281 FGLogging log(LogLevel::DEBUG);
282 log << " X = " << Thruster->GetLocationX() << "\n";
283 log << " Y = " << Thruster->GetLocationY() << "\n";
284 log << " Z = " << Thruster->GetLocationZ() << "\n";
285 log << " Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees\n";
286 log << " Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees\n";
287 }
288 }
289 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
290 FGLogging log(LogLevel::DEBUG);
291 if (from == 0) log << "Instantiated: FGEngine\n";
292 if (from == 1) log << "Destroyed: FGEngine\n";
293 }
294 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
295 }
296 if (debug_lvl & 8 ) { // Runtime state variables
297 }
298 if (debug_lvl & 16) { // Sanity checking
299 }
300 if (debug_lvl & 64) {
301 if (from == 0) { // Constructor
302 }
303 }
304}
305}
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71
Type
The possible types of an SGPropertyNode.
Definition props.hxx:153