JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGJSBBase.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Module: FGJSBBase.cpp
4  Author: Jon S. Berndt
5  Date started: 07/01/01
6  Purpose: Encapsulates the JSBBase object
7 
8  ------------- Copyright (C) 2001 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
18  details.
19 
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26 
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 
30 HISTORY
31 --------------------------------------------------------------------------------
32 07/01/01 JSB Created
33 
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 INCLUDES
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 
38 #define BASE
39 
40 #include "FGJSBBase.h"
41 #include <iostream>
42 #include <sstream>
43 #include <cstdlib>
44 
45 using namespace std;
46 
47 namespace JSBSim {
48 
49 IDENT(IdSrc,"$Id: FGJSBBase.cpp,v 1.42 2016/01/17 18:42:52 bcoconni Exp $");
50 IDENT(IdHdr,ID_JSBBASE);
51 
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 CLASS IMPLEMENTATION
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 
56 #ifndef _MSC_VER
57  char FGJSBBase::highint[5] = {27, '[', '1', 'm', '\0' };
58  char FGJSBBase::halfint[5] = {27, '[', '2', 'm', '\0' };
59  char FGJSBBase::normint[6] = {27, '[', '2', '2', 'm', '\0' };
60  char FGJSBBase::reset[5] = {27, '[', '0', 'm', '\0' };
61  char FGJSBBase::underon[5] = {27, '[', '4', 'm', '\0' };
62  char FGJSBBase::underoff[6] = {27, '[', '2', '4', 'm', '\0' };
63  char FGJSBBase::fgblue[6] = {27, '[', '3', '4', 'm', '\0' };
64  char FGJSBBase::fgcyan[6] = {27, '[', '3', '6', 'm', '\0' };
65  char FGJSBBase::fgred[6] = {27, '[', '3', '1', 'm', '\0' };
66  char FGJSBBase::fggreen[6] = {27, '[', '3', '2', 'm', '\0' };
67  char FGJSBBase::fgdef[6] = {27, '[', '3', '9', 'm', '\0' };
68 #else
69  char FGJSBBase::highint[5] = {'\0' };
70  char FGJSBBase::halfint[5] = {'\0' };
71  char FGJSBBase::normint[6] = {'\0' };
72  char FGJSBBase::reset[5] = {'\0' };
73  char FGJSBBase::underon[5] = {'\0' };
74  char FGJSBBase::underoff[6] = {'\0' };
75  char FGJSBBase::fgblue[6] = {'\0' };
76  char FGJSBBase::fgcyan[6] = {'\0' };
77  char FGJSBBase::fgred[6] = {'\0' };
78  char FGJSBBase::fggreen[6] = {'\0' };
79  char FGJSBBase::fgdef[6] = {'\0' };
80 #endif
81 
82 const double FGJSBBase::radtodeg = 57.295779513082320876798154814105;
83 const double FGJSBBase::degtorad = 0.017453292519943295769236907684886;
84 const double FGJSBBase::hptoftlbssec = 550.0;
85 const double FGJSBBase::psftoinhg = 0.014138;
86 const double FGJSBBase::psftopa = 47.88;
87 const double FGJSBBase::ktstofps = 1.68781;
88 const double FGJSBBase::fpstokts = 1.0/ktstofps;
89 const double FGJSBBase::inchtoft = 0.08333333;
90 const double FGJSBBase::in3tom3 = 1.638706E-5;
91 const double FGJSBBase::m3toft3 = 1.0/(fttom*fttom*fttom);
92 const double FGJSBBase::inhgtopa = 3386.38;
93 const double FGJSBBase::fttom = 0.3048;
94 double FGJSBBase::Reng = 1716.56; // Gas constant for Air (ft-lb/slug-R)
95 double FGJSBBase::Rstar = 1545.348; // Universal gas constant
96 double FGJSBBase::Mair = 28.9645; //
97 const double FGJSBBase::SHRatio = 1.40;
98 
99 // Note that definition of lbtoslug by the inverse of slugtolb and not
100 // to a different constant you can also get from some tables will make
101 // lbtoslug*slugtolb == 1 up to the magnitude of roundoff. So converting from
102 // slug to lb and back will yield to the original value you started with up
103 // to the magnitude of roundoff.
104 // Taken from units gnu commandline tool
105 const double FGJSBBase::slugtolb = 32.174049;
106 const double FGJSBBase::lbtoslug = 1.0/slugtolb;
107 const double FGJSBBase::kgtolb = 2.20462;
108 const double FGJSBBase::kgtoslug = 0.06852168;
109 
110 const string FGJSBBase::needed_cfg_version = "2.0";
111 const string FGJSBBase::JSBSim_version = "1.0 " __DATE__ " " __TIME__ ;
112 
113 queue <FGJSBBase::Message> FGJSBBase::Messages;
114 FGJSBBase::Message FGJSBBase::localMsg;
115 unsigned int FGJSBBase::messageId = 0;
116 
117 int FGJSBBase::gaussian_random_number_phase = 0;
118 
119 short FGJSBBase::debug_lvl = 1;
120 
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 
123 void FGJSBBase::PutMessage(const Message& msg)
124 {
125  Messages.push(msg);
126 }
127 
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 
130 void FGJSBBase::PutMessage(const string& text)
131 {
132  Message msg;
133  msg.text = text;
134  msg.messageId = messageId++;
135  msg.subsystem = "FDM";
136  msg.type = Message::eText;
137  Messages.push(msg);
138 }
139 
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 
142 void FGJSBBase::PutMessage(const string& text, bool bVal)
143 {
144  Message msg;
145  msg.text = text;
146  msg.messageId = messageId++;
147  msg.subsystem = "FDM";
148  msg.type = Message::eBool;
149  msg.bVal = bVal;
150  Messages.push(msg);
151 }
152 
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 
155 void FGJSBBase::PutMessage(const string& text, int iVal)
156 {
157  Message msg;
158  msg.text = text;
159  msg.messageId = messageId++;
160  msg.subsystem = "FDM";
161  msg.type = Message::eInteger;
162  msg.iVal = iVal;
163  Messages.push(msg);
164 }
165 
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 
168 void FGJSBBase::PutMessage(const string& text, double dVal)
169 {
170  Message msg;
171  msg.text = text;
172  msg.messageId = messageId++;
173  msg.subsystem = "FDM";
174  msg.type = Message::eDouble;
175  msg.dVal = dVal;
176  Messages.push(msg);
177 }
178 
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 
181 void FGJSBBase::ProcessMessage(void)
182 {
183  if (Messages.empty()) return;
184  localMsg = Messages.front();
185 
186  while (SomeMessages()) {
187  switch (localMsg.type) {
188  case JSBSim::FGJSBBase::Message::eText:
189  cout << localMsg.messageId << ": " << localMsg.text << endl;
190  break;
191  case JSBSim::FGJSBBase::Message::eBool:
192  cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.bVal << endl;
193  break;
194  case JSBSim::FGJSBBase::Message::eInteger:
195  cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.iVal << endl;
196  break;
197  case JSBSim::FGJSBBase::Message::eDouble:
198  cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.dVal << endl;
199  break;
200  default:
201  cerr << "Unrecognized message type." << endl;
202  break;
203  }
204  Messages.pop();
205  if (SomeMessages()) localMsg = Messages.front();
206  else break;
207  }
208 
209 }
210 
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 
213 FGJSBBase::Message* FGJSBBase::ProcessNextMessage(void)
214 {
215  if (Messages.empty()) return NULL;
216  localMsg = Messages.front();
217 
218  Messages.pop();
219  return &localMsg;
220 }
221 
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 
224 void FGJSBBase::disableHighLighting(void)
225 {
226  highint[0]='\0';
227  halfint[0]='\0';
228  normint[0]='\0';
229  reset[0]='\0';
230  underon[0]='\0';
231  underoff[0]='\0';
232  fgblue[0]='\0';
233  fgcyan[0]='\0';
234  fgred[0]='\0';
235  fggreen[0]='\0';
236  fgdef[0]='\0';
237 }
238 
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 
241 string FGJSBBase::CreateIndexedPropertyName(const string& Property, int index)
242 {
243  ostringstream buf;
244  buf << Property << '[' << index << ']';
245  return buf.str();
246 }
247 
248 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 
250 double FGJSBBase::GaussianRandomNumber(void)
251 {
252  static double V1, V2, S;
253  double X;
254 
255  if (gaussian_random_number_phase == 0) {
256  V1 = V2 = S = X = 0.0;
257 
258  do {
259  double U1 = (double)rand() / RAND_MAX;
260  double U2 = (double)rand() / RAND_MAX;
261 
262  V1 = 2 * U1 - 1;
263  V2 = 2 * U2 - 1;
264  S = V1 * V1 + V2 * V2;
265  } while(S >= 1 || S == 0);
266 
267  X = V1 * sqrt(-2 * log(S) / S);
268  } else
269  X = V2 * sqrt(-2 * log(S) / S);
270 
271  gaussian_random_number_phase = 1 - gaussian_random_number_phase;
272 
273  return X;
274 }
275 
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 
278 double FGJSBBase::PitotTotalPressure(double mach, double p)
279 {
280  if (mach < 0) return p;
281  if (mach < 1) //calculate total pressure assuming isentropic flow
282  return p*pow((1 + 0.2*mach*mach),3.5);
283  else {
284  // shock in front of pitot tube, we'll assume its normal and use
285  // the Rayleigh Pitot Tube Formula, i.e. the ratio of total
286  // pressure behind the shock to the static pressure in front of
287  // the normal shock assumption should not be a bad one -- most supersonic
288  // aircraft place the pitot probe out front so that it is the forward
289  // most point on the aircraft. The real shock would, of course, take
290  // on something like the shape of a rounded-off cone but, here again,
291  // the assumption should be good since the opening of the pitot probe
292  // is very small and, therefore, the effects of the shock curvature
293  // should be small as well. AFAIK, this approach is fairly well accepted
294  // within the aerospace community
295 
296  // The denominator below is zero for Mach ~ 0.38, for which
297  // we'll never be here, so we're safe
298 
299  return p*166.92158*pow(mach,7.0)/pow(7*mach*mach-1,2.5);
300  }
301 }
302 
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 
305 double FGJSBBase::VcalibratedFromMach(double mach, double p, double psl, double rhosl)
306 {
307  double pt = PitotTotalPressure(mach, p);
308  double A = pow(((pt-p)/psl+1), 1./3.5);
309 
310  return sqrt(7*psl/rhosl*(A-1));
311 }
312 
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 
315 double FGJSBBase::MachFromVcalibrated(double vcas, double p, double psl, double rhosl)
316 {
317  double pt = p + psl*(pow(1+vcas*vcas*rhosl/(7.0*psl),3.5)-1);
318 
319  if (pt/p < 1.89293)
320  return sqrt(5.0*(pow(pt/p, 1./3.5) -1)); // Mach < 1
321  else {
322  // Mach >= 1
323  double mach = sqrt(0.77666*pt/p); // Initial guess is based on a quadratic approximation of the Rayleigh formula
324  double delta = 1.;
325  double target = pt/(166.92158*p);
326  int iter = 0;
327 
328  // Find the root with Newton-Raphson. Since the differential is never zero,
329  // the function is monotonic and has only one root with a multiplicity of one.
330  // Convergence is certain.
331  while (delta > 1E-5 && iter < 10) {
332  double m2 = mach*mach; // Mach^2
333  double m6 = m2*m2*m2; // Mach^6
334  delta = mach*m6/pow(7.0*m2-1.0,2.5) - target;
335  double diff = 7.0*m6*(2.0*m2-1)/pow(7.0*m2-1.0,3.5); // Never zero when Mach >= 1
336  mach -= delta/diff;
337  iter++;
338  }
339 
340  return mach;
341  }
342 }
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 
345 } // namespace JSBSim
346 
STL namespace.
JSBSim Message structure.
Definition: FGJSBBase.h:89