]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / models / propulsion / 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 Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  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 details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28 See header file.
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 01/21/99   JSB   Created
33 09/03/99   JSB   Changed Rocket thrust equation to correct -= Thrust instead of
34                  += Thrust (thanks to Tony Peden)
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include <iostream>
41 #include <fstream>
42 #include <cstdlib>
43
44 #include "FGEngine.h"
45 #include "FGTank.h"
46 #include "FGPropeller.h"
47 #include "FGNozzle.h"
48 #include "FGRotor.h"
49 #include "input_output/FGXMLParse.h"
50 #include "math/FGColumnVector3.h"
51
52 using namespace std;
53
54 namespace JSBSim {
55
56 static const char *IdSrc = "$Id: FGEngine.cpp,v 1.50 2012/03/17 20:46:29 jentron Exp $";
57 static const char *IdHdr = ID_ENGINE;
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 CLASS IMPLEMENTATION
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
63 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number, struct Inputs& input)
64                       : in(input), EngineNumber(engine_number)
65 {
66   Element* local_element;
67   FGColumnVector3 location, orientation;
68
69   Name = "";
70   Type = etUnknown;
71   X = Y = Z = 0.0;
72   EnginePitch = EngineYaw = 0.0;
73   SLFuelFlowMax = 0.0;
74   FuelExpended = 0.0;
75   MaxThrottle = 1.0;
76   MinThrottle = 0.0;
77
78   ResetToIC(); // initialize dynamic terms
79
80   FDMExec = exec;
81
82   PropertyManager = FDMExec->GetPropertyManager();
83
84   Name = engine_element->GetAttributeValue("name");
85
86   Load(engine_element, PropertyManager, to_string(EngineNumber)); // Call ModelFunctions loader
87
88 // Find and set engine location
89
90   local_element = engine_element->GetParent()->FindElement("location");
91   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
92   else      cerr << "No engine location found for this engine." << endl;
93
94   local_element = engine_element->GetParent()->FindElement("orient");
95   if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
96 //  else          cerr << "No engine orientation found for this engine." << endl;
97 // Jon: The engine orientation has a default and is not normally used.
98
99   SetPlacement(location, orientation);
100
101   // Load thruster
102   local_element = engine_element->GetParent()->FindElement("thruster");
103   if (local_element) {
104     try {
105       if (!LoadThruster(local_element)) exit(-1);
106     } catch (std::string str) {
107       throw("Error loading engine " + Name + ". " + str);
108     }
109   } else {
110     cerr << "No thruster definition supplied with engine definition." << endl;
111   }
112
113   // Load feed tank[s] references
114   local_element = engine_element->GetParent()->FindElement("feed");
115   while (local_element) {
116     int tankID = (int)local_element->GetDataAsNumber();
117     SourceTanks.push_back(tankID);
118     local_element = engine_element->GetParent()->FindNextElement("feed");
119   }
120
121   string property_name, base_property_name;
122   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
123
124   property_name = base_property_name + "/set-running";
125   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
126   property_name = base_property_name + "/thrust-lbs";
127   PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
128   property_name = base_property_name + "/fuel-flow-rate-pps";
129   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
130   property_name = base_property_name + "/fuel-flow-rate-gph";
131   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRateGPH);
132   property_name = base_property_name + "/fuel-used-lbs";
133   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelUsedLbs);
134
135   PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
136
137   Debug(0);
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 FGEngine::~FGEngine()
143 {
144   delete Thruster;
145   Debug(1);
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 void FGEngine::ResetToIC(void)
151 {
152   Starter = false;
153   FuelExpended = 0.0;
154   Starved = Running = Cranking = false;
155   PctPower = 0.0;
156   FuelFlow_gph = 0.0;
157   FuelFlow_pph = 0.0;
158   FuelFlowRate = 0.0;
159   FuelFreeze = false;
160   FuelUsedLbs = 0.0;
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165 double FGEngine::CalcFuelNeed(void)
166 {
167   FuelFlowRate = SLFuelFlowMax*PctPower;
168   FuelExpended = FuelFlowRate*in.TotalDeltaT;
169   if (!Starved) FuelUsedLbs += FuelExpended;
170   return FuelExpended;
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 unsigned int FGEngine::GetSourceTank(unsigned int i) const
176 {
177   if (i >= 0 && i < SourceTanks.size()) {
178     return SourceTanks[i];
179   } else {
180     throw("No such source tank is available for this engine");
181   }
182 }
183
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 void FGEngine::SetPlacement(const FGColumnVector3& location,
187                             const FGColumnVector3& orientation)
188 {
189   X = location(eX);
190   Y = location(eY);
191   Z = location(eZ);
192   EnginePitch = orientation(ePitch);
193   EngineYaw = orientation (eYaw);
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 double FGEngine::GetThrust(void) const 
199 {
200   return Thruster->GetThrust();
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 const FGColumnVector3& FGEngine::GetBodyForces(void)
206 {
207   return Thruster->GetBodyForces();
208 }
209
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 const FGColumnVector3& FGEngine::GetMoments(void)
213 {
214   return Thruster->GetMoments();
215 }
216
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 void FGEngine::LoadThrusterInputs()
220 {
221   Thruster->in.TotalDeltaT     = in.TotalDeltaT;
222   Thruster->in.H_agl           = in.H_agl;
223   Thruster->in.PQR             = in.PQR;
224   Thruster->in.AeroPQR         = in.AeroPQR;
225   Thruster->in.AeroUVW         = in.AeroUVW;
226   Thruster->in.Density         = in.Density;
227   Thruster->in.Pressure        = in.Pressure;
228   Thruster->in.Soundspeed      = in.Soundspeed;
229   Thruster->in.Alpha           = in.alpha;
230   Thruster->in.Beta            = in.beta;
231   Thruster->in.Vt              = in.Vt;
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 bool FGEngine::LoadThruster(Element *thruster_element)
237 {
238   string token, fullpath, localpath;
239   string thruster_filename, thruster_fullpathname, thrType;
240   string enginePath = FDMExec->GetEnginePath();
241   string aircraftPath = FDMExec->GetFullAircraftPath();
242   ifstream thruster_file;
243   FGColumnVector3 location, orientation;
244   string separator = "/";
245
246   fullpath = enginePath + separator;
247   localpath = aircraftPath + separator + "Engines" + separator;
248
249   thruster_filename = thruster_element->GetAttributeValue("file");
250   if ( !thruster_filename.empty()) {
251     thruster_fullpathname = localpath + thruster_filename + ".xml";
252     thruster_file.open(thruster_fullpathname.c_str());
253     if ( !thruster_file.is_open()) {
254       thruster_fullpathname = fullpath + thruster_filename + ".xml";
255       thruster_file.open(thruster_fullpathname.c_str());
256       if ( !thruster_file.is_open()) {
257         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
258         return false;
259       } else {
260         thruster_file.close();
261       }
262     } else {
263       thruster_file.close();
264     }
265   } else {
266     cerr << "No thruster filename given." << endl;
267     return false;
268   }
269
270   document = LoadXMLDocument(thruster_fullpathname);
271   document->SetParent(thruster_element);
272
273   thrType = document->GetName();
274
275   if (thrType == "propeller") {
276     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
277   } else if (thrType == "nozzle") {
278     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
279   } else if (thrType == "rotor") {
280     Thruster = new FGRotor(FDMExec, document, EngineNumber);
281   } else if (thrType == "direct") {
282     Thruster = new FGThruster( FDMExec, document, EngineNumber);
283   }
284
285   Thruster->SetdeltaT(in.TotalDeltaT);
286
287   Debug(2);
288   return true;
289 }
290
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 //    The bitmasked value choices are as follows:
293 //    unset: In this case (the default) JSBSim would only print
294 //       out the normally expected messages, essentially echoing
295 //       the config files as they are read. If the environment
296 //       variable is not set, debug_lvl is set to 1 internally
297 //    0: This requests JSBSim not to output any messages
298 //       whatsoever.
299 //    1: This value explicity requests the normal JSBSim
300 //       startup messages
301 //    2: This value asks for a message to be printed out when
302 //       a class is instantiated
303 //    4: When this value is set, a message is displayed when a
304 //       FGModel object executes its Run() method
305 //    8: When this value is set, various runtime state variables
306 //       are printed out periodically
307 //    16: When set various parameters are sanity checked and
308 //       a message is printed out when they go out of bounds
309
310 void FGEngine::Debug(int from)
311 {
312   if (debug_lvl <= 0) return;
313
314   if (debug_lvl & 1) { // Standard console startup message output
315     if (from == 0) { // Constructor
316
317     }
318     if (from == 2) { // After thruster loading
319       cout << "      X = " << Thruster->GetLocationX() << endl;
320       cout << "      Y = " << Thruster->GetLocationY() << endl;
321       cout << "      Z = " << Thruster->GetLocationZ() << endl;
322       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
323       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
324     }
325   }
326   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
327     if (from == 0) cout << "Instantiated: FGEngine" << endl;
328     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
329   }
330   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
331   }
332   if (debug_lvl & 8 ) { // Runtime state variables
333   }
334   if (debug_lvl & 16) { // Sanity checking
335   }
336   if (debug_lvl & 64) {
337     if (from == 0) { // Constructor
338       cout << IdSrc << endl;
339       cout << IdHdr << endl;
340     }
341   }
342 }
343 }