1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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.
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
33 09/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
34 += Thrust (thanks to Tony Peden)
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
46 #include "FGPropeller.h"
49 #include "input_output/FGXMLParse.h"
50 #include "math/FGColumnVector3.h"
56 static const char *IdSrc = "$Id: FGEngine.cpp,v 1.48 2011/10/31 14:54:41 bcoconni Exp $";
57 static const char *IdHdr = ID_ENGINE;
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number, struct Inputs& input)
64 : in(input), EngineNumber(engine_number)
66 Element* local_element;
67 FGColumnVector3 location, orientation;
72 EnginePitch = EngineYaw = 0.0;
78 ResetToIC(); // initialize dynamic terms
82 PropertyManager = FDMExec->GetPropertyManager();
84 Name = engine_element->GetAttributeValue("name");
86 Load(engine_element, PropertyManager, to_string(EngineNumber)); // Call ModelFunctions loader
88 // Find and set engine location
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;
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.
99 SetPlacement(location, orientation);
102 local_element = engine_element->GetParent()->FindElement("thruster");
105 if (!LoadThruster(local_element)) exit(-1);
106 } catch (std::string str) {
107 throw("Error loading engine " + Name + ". " + str);
110 cerr << "No thruster definition supplied with engine definition." << endl;
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");
121 string property_name, base_property_name;
122 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
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);
135 PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 FGEngine::~FGEngine()
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 void FGEngine::ResetToIC(void)
154 Starved = Running = Cranking = false;
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 double FGEngine::CalcFuelNeed(void)
167 FuelFlowRate = SLFuelFlowMax*PctPower;
168 FuelExpended = FuelFlowRate*in.TotalDeltaT;
169 if (!Starved) FuelUsedLbs += FuelExpended;
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 unsigned int FGEngine::GetSourceTank(unsigned int i) const
177 if (i >= 0 && i < SourceTanks.size()) {
178 return SourceTanks[i];
180 throw("No such source tank is available for this engine");
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 void FGEngine::SetPlacement(const FGColumnVector3& location,
187 const FGColumnVector3& orientation)
192 EnginePitch = orientation(ePitch);
193 EngineYaw = orientation (eYaw);
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 double FGEngine::GetThrust(void) const
200 return Thruster->GetThrust();
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 const FGColumnVector3& FGEngine::GetBodyForces(void)
207 return Thruster->GetBodyForces();
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 const FGColumnVector3& FGEngine::GetMoments(void)
214 return Thruster->GetMoments();
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 void FGEngine::LoadThrusterInputs()
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;
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 bool FGEngine::LoadThruster(Element *thruster_element)
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 = "/";
246 fullpath = enginePath + separator;
247 localpath = aircraftPath + separator + "Engines" + separator;
249 thruster_filename = thruster_element->GetAttributeValue("file");
250 if ( !thruster_filename.empty()) {
251 thruster_fullpathname = fullpath + thruster_filename + ".xml";
252 thruster_file.open(thruster_fullpathname.c_str());
253 if ( !thruster_file.is_open()) {
254 thruster_fullpathname = localpath + 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;
260 thruster_file.close();
263 thruster_file.close();
266 cerr << "No thruster filename given." << endl;
270 document = LoadXMLDocument(thruster_fullpathname);
271 document->SetParent(thruster_element);
273 thrType = document->GetName();
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);
285 Thruster->SetdeltaT(in.TotalDeltaT);
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
299 // 1: This value explicity requests the normal JSBSim
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
310 void FGEngine::Debug(int from)
312 if (debug_lvl <= 0) return;
314 if (debug_lvl & 1) { // Standard console startup message output
315 if (from == 0) { // Constructor
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;
326 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
327 if (from == 0) cout << "Instantiated: FGEngine" << endl;
328 if (from == 1) cout << "Destroyed: FGEngine" << endl;
330 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
332 if (debug_lvl & 8 ) { // Runtime state variables
334 if (debug_lvl & 16) { // Sanity checking
336 if (debug_lvl & 64) {
337 if (from == 0) { // Constructor
338 cout << IdSrc << endl;
339 cout << IdHdr << endl;