LCOV - code coverage report
Current view: top level - models - FGAtmosphere.cpp (source / functions) Hit Total Coverage
Test: JSBSim-Coverage-Statistics Lines: 187 321 58.3 %
Date: 2010-08-24 Functions: 13 23 56.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 37 113 32.7 %

           Branch data     Line data    Source code
       1                 :            : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       2                 :            : 
       3                 :            :  Module:       FGAtmosphere.cpp
       4                 :            :  Author:       Jon Berndt
       5                 :            :                Implementation of 1959 Standard Atmosphere added by Tony Peden
       6                 :            :  Date started: 11/24/98
       7                 :            :  Purpose:      Models the atmosphere
       8                 :            :  Called by:    FGSimExec
       9                 :            : 
      10                 :            :  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
      11                 :            : 
      12                 :            :  This program is free software; you can redistribute it and/or modify it under
      13                 :            :  the terms of the GNU Lesser General Public License as published by the Free Software
      14                 :            :  Foundation; either version 2 of the License, or (at your option) any later
      15                 :            :  version.
      16                 :            : 
      17                 :            :  This program is distributed in the hope that it will be useful, but WITHOUT
      18                 :            :  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      19                 :            :  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      20                 :            :  details.
      21                 :            : 
      22                 :            :  You should have received a copy of the GNU Lesser General Public License along with
      23                 :            :  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
      24                 :            :  Place - Suite 330, Boston, MA  02111-1307, USA.
      25                 :            : 
      26                 :            :  Further information about the GNU Lesser General Public License can also be found on
      27                 :            :  the world wide web at http://www.gnu.org.
      28                 :            : 
      29                 :            : FUNCTIONAL DESCRIPTION
      30                 :            : --------------------------------------------------------------------------------
      31                 :            : Models the atmosphere. The equation used below was determined by a third order
      32                 :            : curve fit using Excel. The data is from the ICAO atmosphere model.
      33                 :            : 
      34                 :            : HISTORY
      35                 :            : --------------------------------------------------------------------------------
      36                 :            : 11/24/98   JSB   Created
      37                 :            : 07/23/99   TP    Added implementation of 1959 Standard Atmosphere
      38                 :            :                  Moved calculation of Mach number to FGPropagate
      39                 :            :                  Later updated to '76 model
      40                 :            : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      41                 :            : COMMENTS, REFERENCES,  and NOTES
      42                 :            : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      43                 :            : [1]   Anderson, John D. "Introduction to Flight, Third Edition", McGraw-Hill,
      44                 :            :       1989, ISBN 0-07-001641-0
      45                 :            : 
      46                 :            : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      47                 :            : INCLUDES
      48                 :            : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      49                 :            : 
      50                 :            : #include "FGAtmosphere.h"
      51                 :            : #include "FGAircraft.h"
      52                 :            : #include "FGPropagate.h"
      53                 :            : #include "FGInertial.h"
      54                 :            : #include "FGAuxiliary.h"
      55                 :            : #include "FGFDMExec.h"
      56                 :            : #include "input_output/FGPropertyManager.h"
      57                 :            : #include <iostream>
      58                 :            : #include <cstdlib>
      59                 :            : 
      60                 :            : using namespace std;
      61                 :            : 
      62                 :            : namespace JSBSim {
      63                 :            : 
      64                 :            : static const char *IdSrc = "$Id: FGAtmosphere.cpp,v 1.37 2010/08/22 03:53:10 jberndt Exp $";
      65                 :            : static const char *IdHdr = ID_ATMOSPHERE;
      66                 :            : 
      67                 :            : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      68                 :            : CLASS IMPLEMENTATION
      69                 :            : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      70                 :            : 
      71                 :          1 : FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
      72                 :            : {
      73                 :          1 :   Name = "FGAtmosphere";
      74                 :          1 :   lastIndex = 0;
      75                 :          1 :   h = 0.0;
      76                 :          1 :   psiw = 0.0;
      77                 :          1 :   htab[0]=0;
      78                 :          1 :   htab[1]= 36089.0;
      79                 :          1 :   htab[2]= 65617.0;
      80                 :          1 :   htab[3]=104987.0;
      81                 :          1 :   htab[4]=154199.0;
      82                 :          1 :   htab[5]=167322.0;
      83                 :          1 :   htab[6]=232940.0;
      84                 :          1 :   htab[7]=278385.0; //ft.
      85                 :            : 
      86                 :          1 :   MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
      87                 :            : //  SetTurbType( ttCulp );
      88                 :            :   SetTurbType( ttNone );
      89                 :          1 :   TurbGain = 1.0;
      90                 :          1 :   TurbRate = 10.0;
      91                 :          1 :   Rhythmicity = 0.1;
      92                 :          1 :   spike = target_time = strength = 0.0;
      93                 :          1 :   wind_from_clockwise = 0.0;
      94                 :          1 :   SutherlandConstant = 198.72; // deg Rankine
      95                 :          1 :   Beta = 2.269690E-08; // slug/(sec ft R^0.5)
      96                 :            : 
      97                 :          1 :   T_dev_sl = T_dev = delta_T = 0.0;
      98                 :          1 :   StandardTempOnly = false;
      99                 :          1 :   first_pass = true;
     100                 :          1 :   vGustNED.InitMatrix();
     101                 :          1 :   vTurbulenceNED.InitMatrix();
     102                 :            : 
     103                 :          1 :   bind();
     104                 :          1 :   Debug(0);
     105                 :          1 : }
     106                 :            : 
     107                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     108                 :            : 
     109                 :          1 : FGAtmosphere::~FGAtmosphere()
     110                 :            : {
     111                 :          1 :   Debug(1);
     112                 :          1 : }
     113                 :            : 
     114                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     115                 :            : 
     116                 :          1 : bool FGAtmosphere::InitModel(void)
     117                 :            : {
     118         [ -  + ]:          1 :   if (!FGModel::InitModel()) return false;
     119                 :            : 
     120                 :            :   UseInternal();  // this is the default
     121                 :            : 
     122                 :          1 :   Calculate(h);
     123                 :          1 :   StdSLtemperature = SLtemperature = 518.67;
     124                 :          1 :   StdSLpressure    = SLpressure = 2116.22;
     125                 :          1 :   StdSLdensity     = SLdensity = 0.00237767;
     126                 :          1 :   StdSLsoundspeed  = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
     127                 :          1 :   rSLtemperature = 1.0/StdSLtemperature;
     128                 :          1 :   rSLpressure    = 1.0/StdSLpressure;
     129                 :          1 :   rSLdensity     = 1.0/StdSLdensity;
     130                 :          1 :   rSLsoundspeed  = 1.0/StdSLsoundspeed;
     131                 :            : 
     132                 :          1 :   return true;
     133                 :            : }
     134                 :            : 
     135                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     136                 :            : 
     137                 :      54008 : bool FGAtmosphere::Run(void)
     138                 :            : {
     139         [ -  + ]:      54008 :   if (FGModel::Run()) return true;
     140         [ -  + ]:      54008 :   if (FDMExec->Holding()) return false;
     141                 :            : 
     142                 :      54008 :   RunPreFunctions();
     143                 :            : 
     144                 :      54008 :   T_dev = 0.0;
     145                 :     108016 :   h = Propagate->GetAltitudeASL();
     146                 :            : 
     147         [ +  - ]:      54008 :   if (!useExternal) {
     148                 :      54008 :     Calculate(h);
     149                 :      54008 :     CalculateDerived();
     150                 :            :   } else {
     151                 :          0 :     CalculateDerived();
     152                 :            :   }
     153                 :            : 
     154                 :      54008 :   RunPostFunctions();
     155                 :            : 
     156                 :      54008 :   Debug(2);
     157                 :      54008 :   return false;
     158                 :            : }
     159                 :            : 
     160                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     161                 :            : //
     162                 :            : // See reference 1
     163                 :            : 
     164                 :     162025 : void FGAtmosphere::Calculate(double altitude)
     165                 :            : {
     166                 :            :   double slope, reftemp, refpress;
     167                 :     162025 :   int i = lastIndex;
     168                 :            : 
     169         [ -  + ]:     162025 :   if (altitude < htab[lastIndex]) {
     170         [ #  # ]:          0 :     if (altitude <= 0) {
     171                 :          0 :       i = 0;
     172                 :          0 :       altitude=0;
     173                 :            :     } else {
     174                 :          0 :        i = lastIndex-1;
     175         [ #  # ]:          0 :        while (htab[i] > altitude) i--;
     176                 :            :     }
     177         [ +  + ]:     162025 :   } else if (altitude > htab[lastIndex+1]) {
     178         [ +  + ]:     107565 :     if (altitude >= htab[7]) {
     179                 :     107559 :       i = 7;
     180                 :     107559 :       altitude = htab[7];
     181                 :            :     } else {
     182                 :          6 :       i = lastIndex+1;
     183         [ -  + ]:          6 :       while (htab[i+1] < altitude) i++;
     184                 :            :     }
     185                 :            :   }
     186                 :            : 
     187   [ +  +  +  +  :     162025 :   switch(i) {
             +  +  +  +  
                      - ]
     188                 :            :   case 0: // Sea level
     189                 :      24106 :     slope     = -0.00356616; // R/ft.
     190                 :      24106 :     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
     191                 :      24106 :     refpress  = 2116.22;    // psf
     192                 :            :     //refdens   = 0.00237767;  // slugs/cubic ft.
     193                 :      24106 :     break;
     194                 :            :   case 1:     // 36089 ft. or 11 km
     195                 :       6366 :     slope     = 0;
     196                 :       6366 :     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
     197                 :       6366 :     refpress  = 472.763;
     198                 :            :     //refdens   = 0.000706032;
     199                 :       6366 :     break;
     200                 :            :   case 2:     // 65616 ft. or 20 km
     201                 :       5994 :     slope     = 0.00054864;
     202                 :       5994 :     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
     203                 :       5994 :     refpress  = 114.636;
     204                 :            :     //refdens   = 0.000171306;
     205                 :       5994 :     break;
     206                 :            :   case 3:     // 104986 ft. or 32 km
     207                 :       5784 :     slope     = 0.001536192;
     208                 :       5784 :     reftemp   = 411.57; // in degrees Rankine, 228.65 Kelvin
     209                 :       5784 :     refpress  = 18.128;
     210                 :            :     //refdens   = 1.18422e-05;
     211                 :       5784 :     break;
     212                 :            :   case 4:     // 154199 ft. 47 km
     213                 :       1365 :     slope     = 0;
     214                 :       1365 :     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
     215                 :       1365 :     refpress  = 2.316;
     216                 :            :     //refdens   = 4.00585e-7;
     217                 :       1365 :     break;
     218                 :            :   case 5:     // 167322 ft. or 51 km
     219                 :       6435 :     slope     = -0.001536192;
     220                 :       6435 :     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
     221                 :       6435 :     refpress  = 1.398;
     222                 :            :     //refdens   = 8.17102e-7;
     223                 :       6435 :     break;
     224                 :            :   case 6:     // 232940 ft. or 71 km
     225                 :       4416 :     slope     = -0.00109728;
     226                 :       4416 :     reftemp   = 386.368; // in degrees Rankine, 214.649 Kelvin
     227                 :       4416 :     refpress  = 0.0826;
     228                 :            :     //refdens   = 8.77702e-9;
     229                 :       4416 :     break;
     230                 :            :   case 7:     // 278385 ft. or 84.8520 km
     231                 :     107559 :     slope     = 0;
     232                 :     107559 :     reftemp   = 336.5; // in degrees Rankine, 186.94 Kelvin
     233                 :     107559 :     refpress  = 0.00831;
     234                 :            :     //refdens   = 2.19541e-10;
     235                 :     107559 :     break;
     236                 :            :   default:     // sea level
     237                 :          0 :     slope     = -0.00356616; // R/ft.
     238                 :          0 :     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
     239                 :          0 :     refpress  = 2116.22;    // psf
     240                 :            :     //refdens   = 0.00237767;  // slugs/cubic ft.
     241                 :            :     break;
     242                 :            : 
     243                 :            :   }
     244                 :            : 
     245                 :            :   // If delta_T is set, then that is our temperature deviation at any altitude.
     246                 :            :   // If not, then we'll estimate a deviation based on the sea level deviation (if set).
     247                 :            : 
     248         [ +  + ]:     162025 :   if(!StandardTempOnly) {
     249                 :     108017 :     T_dev = 0.0;
     250         [ -  + ]:     108017 :     if (delta_T != 0.0) {
     251                 :          0 :       T_dev = delta_T;
     252                 :            :     } else {
     253 [ +  + ][ -  + ]:     108017 :       if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
     254                 :          0 :         T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
     255                 :            :       }
     256                 :            :     }
     257                 :     108017 :     reftemp+=T_dev;
     258                 :            :   }
     259                 :            : 
     260         [ +  + ]:     162025 :   if (slope == 0) {
     261                 :     115290 :     intTemperature = reftemp;
     262                 :     115290 :     intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
     263                 :     115290 :     intDensity = intPressure/(Reng*intTemperature);
     264                 :            :   } else {
     265                 :      46735 :     intTemperature = reftemp+slope*(altitude-htab[i]);
     266                 :      46735 :     intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
     267                 :      46735 :     intDensity = intPressure/(Reng*intTemperature);
     268                 :            :   }
     269                 :            :   
     270                 :     162025 :   lastIndex=i;
     271                 :     162025 : }
     272                 :            : 
     273                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     274                 :            : // Calculate parameters derived from T, P and rho
     275                 :            : // Sum gust and turbulence values in NED frame into the wind vector.
     276                 :            : 
     277                 :      54008 : void FGAtmosphere::CalculateDerived(void)
     278                 :            : {
     279                 :     108016 :   T_dev = (*temperature) - GetTemperature(h);
     280                 :            : 
     281         [ +  - ]:      54008 :   if (T_dev == 0.0) density_altitude = h;
     282                 :          0 :   else              density_altitude = 518.67/0.00356616 * (1.0 - pow(GetDensityRatio(),0.235));
     283                 :            : 
     284         [ -  + ]:      54008 :   if (turbType != ttNone) Turbulence();
     285                 :            : 
     286                 :     108016 :   vTotalWindNED = vWindNED + vGustNED + vTurbulenceNED;
     287                 :            : 
     288                 :            :    // psiw (Wind heading) is the direction the wind is blowing towards
     289         [ -  + ]:      54008 :   if (vWindNED(eX) != 0.0) psiw = atan2( vWindNED(eY), vWindNED(eX) );
     290         [ -  + ]:      54008 :   if (psiw < 0) psiw += 2*M_PI;
     291                 :            : 
     292                 :      54008 :   soundspeed = sqrt(SHRatio*Reng*(*temperature));
     293                 :            : 
     294                 :      54008 :   intViscosity = Beta * pow(intTemperature, 1.5) / (SutherlandConstant + intTemperature);
     295                 :      54008 :   intKinematicViscosity = intViscosity / intDensity;
     296                 :      54008 : }
     297                 :            : 
     298                 :            : 
     299                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     300                 :            : // Get the standard atmospheric properties at a specified altitude
     301                 :            : 
     302                 :      54008 : void FGAtmosphere::GetStdAtmosphere(double altitude) {
     303                 :      54008 :   StandardTempOnly = true;
     304                 :      54008 :   Calculate(altitude);
     305                 :      54008 :   StandardTempOnly = false;
     306                 :      54008 :   atmosphere.Temperature = intTemperature;
     307                 :      54008 :   atmosphere.Pressure = intPressure;
     308                 :      54008 :   atmosphere.Density = intDensity;
     309                 :            : 
     310                 :            :   // Reset the internal atmospheric state
     311                 :      54008 :   Calculate(h);
     312                 :      54008 : }
     313                 :            : 
     314                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     315                 :            : // Get the standard pressure at a specified altitude
     316                 :            : 
     317                 :          0 : double FGAtmosphere::GetPressure(double altitude) {
     318                 :          0 :   GetStdAtmosphere(altitude);
     319                 :          0 :   return atmosphere.Pressure;
     320                 :            : }
     321                 :            : 
     322                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     323                 :            : // Get the standard temperature at a specified altitude
     324                 :            : 
     325                 :          0 : double FGAtmosphere::GetTemperature(double altitude) {
     326                 :      54008 :   GetStdAtmosphere(altitude);
     327                 :          0 :   return atmosphere.Temperature;
     328                 :            : }
     329                 :            : 
     330                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     331                 :            : // Get the standard density at a specified altitude
     332                 :            : 
     333                 :          0 : double FGAtmosphere::GetDensity(double altitude) {
     334                 :          0 :   GetStdAtmosphere(altitude);
     335                 :          0 :   return atmosphere.Density;
     336                 :            : }
     337                 :            : 
     338                 :            : 
     339                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     340                 :            : // square a value, but preserve the original sign
     341                 :            : 
     342                 :            : static inline double square_signed (double value)
     343                 :            : {
     344 [ #  # ][ #  # ]:          0 :     if (value < 0)
     345                 :          0 :         return value * value * -1;
     346                 :            :     else
     347                 :          0 :         return value * value;
     348                 :            : }
     349                 :            : 
     350                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     351                 :            : //
     352                 :            : // psi is the angle that the wind is blowing *towards*
     353                 :            : 
     354                 :          0 : void FGAtmosphere::SetWindspeed(double speed)
     355                 :            : {
     356         [ #  # ]:          0 :   if (vWindNED.Magnitude() == 0.0) {
     357                 :          0 :     psiw = 0.0;
     358                 :          0 :     vWindNED(eNorth) = speed;
     359                 :            :   } else {
     360                 :          0 :     vWindNED(eNorth) = speed * cos(psiw);
     361                 :          0 :     vWindNED(eEast) = speed * sin(psiw);
     362                 :          0 :     vWindNED(eDown) = 0.0;
     363                 :            :   }
     364                 :          0 : }
     365                 :            : 
     366                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     367                 :            : 
     368                 :          1 : double FGAtmosphere::GetWindspeed(void) const
     369                 :            : {
     370                 :          1 :   return vWindNED.Magnitude();
     371                 :            : }
     372                 :            : 
     373                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     374                 :            : //
     375                 :            : // psi is the angle that the wind is blowing *towards*
     376                 :            : 
     377                 :          0 : void FGAtmosphere::SetWindPsi(double dir)
     378                 :            : {
     379                 :          0 :   double mag = GetWindspeed();
     380                 :          0 :   psiw = dir;
     381                 :          0 :   SetWindspeed(mag);  
     382                 :          0 : }
     383                 :            : 
     384                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     385                 :            : 
     386                 :          0 : void FGAtmosphere::Turbulence(void)
     387                 :            : {
     388                 :          0 :   double DeltaT = rate*FDMExec->GetDeltaT();
     389                 :            : 
     390   [ #  #  #  # ]:          0 :   switch (turbType) {
     391                 :            :   case ttStandard: {
     392                 :            :     // TurbGain = TurbGain * TurbGain * 100.0; // what is this!?
     393                 :            : 
     394                 :          0 :     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
     395                 :          0 :     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
     396                 :          0 :     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
     397                 :            : 
     398                 :          0 :     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
     399                 :            :                                 // Scale the magnitude so that it moves
     400                 :            :                                 // away from the peaks
     401                 :            :     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
     402                 :          0 :                          (1 + fabs(Magnitude)));
     403                 :          0 :     MagnitudeAccel    += MagnitudedAccelDt*TurbRate*DeltaT;
     404                 :          0 :     Magnitude         += MagnitudeAccel*DeltaT;
     405                 :          0 :     Magnitude          = fabs(Magnitude);
     406                 :            : 
     407                 :          0 :     vDirectiondAccelDt.Normalize();
     408                 :            : 
     409                 :            :                                 // deemphasise non-vertical forces
     410                 :          0 :     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
     411                 :          0 :     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
     412                 :            : 
     413                 :          0 :     vDirectionAccel += vDirectiondAccelDt*TurbRate*DeltaT;
     414                 :          0 :     vDirectionAccel.Normalize();
     415                 :          0 :     vDirection      += vDirectionAccel*DeltaT;
     416                 :            : 
     417                 :          0 :     vDirection.Normalize();
     418                 :            : 
     419                 :            :                                 // Diminish turbulence within three wingspans
     420                 :            :                                 // of the ground
     421                 :          0 :     vTurbulenceNED = TurbGain * Magnitude * vDirection;
     422                 :          0 :     double HOverBMAC = Auxiliary->GetHOverBMAC();
     423         [ #  # ]:          0 :     if (HOverBMAC < 3.0)
     424                 :          0 :         vTurbulenceNED *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
     425                 :            : 
     426                 :            :     // I don't believe these next two statements calculate the proper gradient over
     427                 :            :     // the aircraft body. One reason is because this has no relationship with the
     428                 :            :     // orientation or velocity of the aircraft, which it must have. What is vTurbulenceGrad
     429                 :            :     // supposed to represent? And the direction and magnitude of the turbulence can change,
     430                 :            :     // so both accelerations need to be accounted for, no?
     431                 :            : 
     432                 :            :     // Need to determine the turbulence change in body axes between two time points.
     433                 :            : 
     434                 :          0 :     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
     435                 :          0 :     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
     436                 :            : 
     437         [ #  # ]:          0 :     if (Aircraft->GetWingSpan() > 0) {
     438                 :          0 :       vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
     439                 :            :     } else {
     440                 :          0 :       vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
     441                 :            :     }
     442                 :            : //     if (Aircraft->GetHTailArm() != 0.0)
     443                 :            : //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
     444                 :            : //     else
     445                 :            : //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
     446                 :            : 
     447         [ #  # ]:          0 :     if (Aircraft->GetVTailArm() > 0)
     448                 :          0 :       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
     449                 :            :     else
     450                 :          0 :       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
     451                 :            : 
     452                 :            :                                 // Clear the horizontal forces
     453                 :            :                                 // actually felt by the plane, now
     454                 :            :                                 // that we've used them to calculate
     455                 :            :                                 // moments.
     456                 :            :                                 // Why? (JSB)
     457                 :            : //    vTurbulenceNED(eX) = 0.0;
     458                 :            : //    vTurbulenceNED(eY) = 0.0;
     459                 :            : 
     460                 :            :     break;
     461                 :            :   }
     462                 :            :   case ttBerndt: { // This is very experimental and incomplete at the moment.
     463                 :            : 
     464                 :          0 :     vDirectiondAccelDt(eX) = GaussianRandomNumber();
     465                 :          0 :     vDirectiondAccelDt(eY) = GaussianRandomNumber();
     466                 :          0 :     vDirectiondAccelDt(eZ) = GaussianRandomNumber();
     467                 :            : /*
     468                 :            :     MagnitudedAccelDt = GaussianRandomNumber();
     469                 :            :     MagnitudeAccel    += MagnitudedAccelDt * DeltaT;
     470                 :            :     Magnitude         += MagnitudeAccel * DeltaT;
     471                 :            : */
     472                 :          0 :     Magnitude         += GaussianRandomNumber() * DeltaT;
     473                 :            : 
     474                 :          0 :     vDirectiondAccelDt.Normalize();
     475                 :          0 :     vDirectionAccel += TurbRate * vDirectiondAccelDt * DeltaT;
     476                 :          0 :     vDirectionAccel.Normalize();
     477                 :          0 :     vDirection      += vDirectionAccel*DeltaT;
     478                 :            : 
     479                 :            :     // Diminish z-vector within two wingspans of the ground
     480                 :          0 :     double HOverBMAC = Auxiliary->GetHOverBMAC();
     481         [ #  # ]:          0 :     if (HOverBMAC < 2.0) vDirection(eZ) *= HOverBMAC / 2.0;
     482                 :            : 
     483                 :          0 :     vDirection.Normalize();
     484                 :            : 
     485                 :          0 :     vTurbulenceNED = TurbGain*Magnitude * vDirection;
     486                 :          0 :     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
     487                 :            : 
     488                 :          0 :     vBodyTurbGrad = Propagate->GetTl2b() * vTurbulenceGrad;
     489                 :          0 :     vTurbPQR(eP) = vBodyTurbGrad(eY) / Aircraft->GetWingSpan();
     490         [ #  # ]:          0 :     if (Aircraft->GetHTailArm() > 0)
     491                 :          0 :       vTurbPQR(eQ) = vBodyTurbGrad(eZ) / Aircraft->GetHTailArm();
     492                 :            :     else
     493                 :          0 :       vTurbPQR(eQ) = vBodyTurbGrad(eZ) / 10.0;
     494                 :            : 
     495         [ #  # ]:          0 :     if (Aircraft->GetVTailArm() > 0)
     496                 :          0 :       vTurbPQR(eR) = vBodyTurbGrad(eX) / Aircraft->GetVTailArm();
     497                 :            :     else
     498                 :          0 :       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
     499                 :            : 
     500                 :            :     break;
     501                 :            :   }
     502                 :            :   case ttCulp: { 
     503                 :            : 
     504                 :          0 :     vTurbPQR(eP) = wind_from_clockwise;
     505         [ #  # ]:          0 :     if (TurbGain == 0.0) return;
     506                 :            :   
     507                 :            :     // keep the inputs within allowable limts for this model
     508         [ #  # ]:          0 :     if (TurbGain < 0.0) TurbGain = 0.0;
     509         [ #  # ]:          0 :     if (TurbGain > 1.0) TurbGain = 1.0;
     510         [ #  # ]:          0 :     if (TurbRate < 0.0) TurbRate = 0.0;
     511         [ #  # ]:          0 :     if (TurbRate > 30.0) TurbRate = 30.0;
     512         [ #  # ]:          0 :     if (Rhythmicity < 0.0) Rhythmicity = 0.0;
     513         [ #  # ]:          0 :     if (Rhythmicity > 1.0) Rhythmicity = 1.0;
     514                 :            : 
     515                 :            :     // generate a sine wave corresponding to turbulence rate in hertz
     516                 :          0 :     double time = FDMExec->GetSimTime();
     517                 :          0 :     double sinewave = sin( time * TurbRate * 6.283185307 );
     518                 :            : 
     519                 :          0 :     double random = 0.0;
     520         [ #  # ]:          0 :     if (target_time == 0.0) {
     521                 :          0 :       strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
     522                 :          0 :       target_time = time + 0.71 + (random * 0.5);
     523                 :            :     }
     524         [ #  # ]:          0 :     if (time > target_time) {
     525                 :          0 :       spike = 1.0;
     526                 :          0 :       target_time = 0.0;
     527                 :            :     }    
     528                 :            : 
     529                 :            :     // max vertical wind speed in fps, corresponds to TurbGain = 1.0
     530                 :          0 :     double max_vs = 40;
     531                 :            : 
     532                 :          0 :     vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
     533                 :          0 :     double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
     534                 :            : 
     535                 :            :     // Vertical component of turbulence.
     536                 :          0 :     vTurbulenceNED(3) = sinewave * max_vs * TurbGain * Rhythmicity;
     537                 :          0 :     vTurbulenceNED(3)+= delta;
     538                 :          0 :     double HOverBMAC = Auxiliary->GetHOverBMAC();
     539         [ #  # ]:          0 :     if (HOverBMAC < 3.0)
     540                 :          0 :         vTurbulenceNED(3) *= HOverBMAC * 0.3333;
     541                 :            :  
     542                 :            :     // Yaw component of turbulence.
     543                 :          0 :     vTurbulenceNED(1) = sin( delta * 3.0 );
     544                 :          0 :     vTurbulenceNED(2) = cos( delta * 3.0 );
     545                 :            : 
     546                 :            :     // Roll component of turbulence. Clockwise vortex causes left roll.
     547                 :          0 :     vTurbPQR(eP) += delta * 0.04;
     548                 :            : 
     549                 :          0 :     spike = spike * 0.9;
     550                 :            :     break;
     551                 :            :   }
     552                 :            :   default:
     553                 :            :     break;
     554                 :            :   }
     555                 :            : }
     556                 :            : 
     557                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     558                 :            : 
     559                 :          0 : void FGAtmosphere::UseExternal(void)
     560                 :            : {
     561                 :          0 :   temperature=&exTemperature;
     562                 :          0 :   pressure=&exPressure;
     563                 :          0 :   density=&exDensity;
     564                 :          0 :   useExternal=true;
     565                 :          0 : }
     566                 :            : 
     567                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     568                 :            : 
     569                 :          0 : void FGAtmosphere::UseInternal(void)
     570                 :            : {
     571                 :          1 :   temperature=&intTemperature;
     572                 :          1 :   pressure=&intPressure;
     573                 :          1 :   density=&intDensity;
     574                 :          1 :   useExternal=false;
     575                 :          0 : }
     576                 :            : 
     577                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     578                 :            : 
     579                 :          1 : void FGAtmosphere::bind(void)
     580                 :            : {
     581                 :            :   typedef double (FGAtmosphere::*PMF)(int) const;
     582                 :            :   typedef double (FGAtmosphere::*PMFv)(void) const;
     583                 :            :   typedef int (FGAtmosphere::*PMFt)(void) const;
     584                 :            :   typedef void   (FGAtmosphere::*PMFd)(int,double);
     585                 :            :   typedef void   (FGAtmosphere::*PMFi)(int);
     586                 :          1 :   PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
     587                 :          1 :   PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
     588                 :          1 :   PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
     589                 :          1 :   PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
     590                 :          1 :   PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
     591                 :          1 :   PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
     592                 :          1 :   PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
     593                 :          1 :   PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
     594                 :          1 :   PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
     595                 :          1 :   PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
     596                 :          1 :   PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
     597                 :          1 :   PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
     598                 :          1 :   PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi, &FGAtmosphere::SetWindPsi);
     599                 :          1 :   PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
     600                 :          1 :   PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
     601                 :          1 :   PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
     602                 :            : 
     603                 :            :   PropertyManager->Tie("atmosphere/wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetWindNED,
     604                 :          1 :                                                           (PMFd)&FGAtmosphere::SetWindNED);
     605                 :            :   PropertyManager->Tie("atmosphere/wind-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetWindNED,
     606                 :          1 :                                                           (PMFd)&FGAtmosphere::SetWindNED);
     607                 :            :   PropertyManager->Tie("atmosphere/wind-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetWindNED,
     608                 :          1 :                                                           (PMFd)&FGAtmosphere::SetWindNED);
     609                 :            :   PropertyManager->Tie("atmosphere/wind-mag-fps", this, &FGAtmosphere::GetWindspeed,
     610                 :          1 :                                                         &FGAtmosphere::SetWindspeed);
     611                 :          1 :   PropertyManager->Tie("atmosphere/total-wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTotalWindNED);
     612                 :          1 :   PropertyManager->Tie("atmosphere/total-wind-east-fps",  this, eEast,  (PMF)&FGAtmosphere::GetTotalWindNED);
     613                 :          1 :   PropertyManager->Tie("atmosphere/total-wind-down-fps",  this, eDown,  (PMF)&FGAtmosphere::GetTotalWindNED);
     614                 :            : 
     615                 :            :   PropertyManager->Tie("atmosphere/gust-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetGustNED,
     616                 :          1 :                                                           (PMFd)&FGAtmosphere::SetGustNED);
     617                 :            :   PropertyManager->Tie("atmosphere/gust-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetGustNED,
     618                 :          1 :                                                           (PMFd)&FGAtmosphere::SetGustNED);
     619                 :            :   PropertyManager->Tie("atmosphere/gust-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetGustNED,
     620                 :          1 :                                                           (PMFd)&FGAtmosphere::SetGustNED);
     621                 :            : 
     622                 :            :   PropertyManager->Tie("atmosphere/turb-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTurbNED,
     623                 :          1 :                                                           (PMFd)&FGAtmosphere::SetTurbNED);
     624                 :            :   PropertyManager->Tie("atmosphere/turb-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetTurbNED,
     625                 :          1 :                                                           (PMFd)&FGAtmosphere::SetTurbNED);
     626                 :            :   PropertyManager->Tie("atmosphere/turb-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetTurbNED,
     627                 :          1 :                                                           (PMFd)&FGAtmosphere::SetTurbNED);
     628                 :            : 
     629                 :          1 :   PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
     630                 :          1 :   PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
     631                 :          1 :   PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
     632                 :          2 :   PropertyManager->Tie("atmosphere/turb-type", this, (PMFt)&FGAtmosphere::GetTurbType, (PMFi)&FGAtmosphere::SetTurbType);
     633                 :          1 :   PropertyManager->Tie("atmosphere/turb-rate", this, &FGAtmosphere::GetTurbRate, &FGAtmosphere::SetTurbRate);
     634                 :          1 :   PropertyManager->Tie("atmosphere/turb-gain", this, &FGAtmosphere::GetTurbGain, &FGAtmosphere::SetTurbGain);
     635                 :            :   PropertyManager->Tie("atmosphere/turb-rhythmicity", this, &FGAtmosphere::GetRhythmicity,
     636                 :          1 :                                                             &FGAtmosphere::SetRhythmicity);
     637                 :          1 : }
     638                 :            : 
     639                 :            : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     640                 :            : //    The bitmasked value choices are as follows:
     641                 :            : //    unset: In this case (the default) JSBSim would only print
     642                 :            : //       out the normally expected messages, essentially echoing
     643                 :            : //       the config files as they are read. If the environment
     644                 :            : //       variable is not set, debug_lvl is set to 1 internally
     645                 :            : //    0: This requests JSBSim not to output any messages
     646                 :            : //       whatsoever.
     647                 :            : //    1: This value explicity requests the normal JSBSim
     648                 :            : //       startup messages
     649                 :            : //    2: This value asks for a message to be printed out when
     650                 :            : //       a class is instantiated
     651                 :            : //    4: When this value is set, a message is displayed when a
     652                 :            : //       FGModel object executes its Run() method
     653                 :            : //    8: When this value is set, various runtime state variables
     654                 :            : //       are printed out periodically
     655                 :            : //    16: When set various parameters are sanity checked and
     656                 :            : //       a message is printed out when they go out of bounds
     657                 :            : 
     658                 :      54010 : void FGAtmosphere::Debug(int from)
     659                 :            : {
     660         [ +  - ]:      54010 :   if (debug_lvl <= 0) return;
     661                 :            : 
     662                 :      54010 :   if (debug_lvl & 1) { // Standard console startup message output
     663                 :            :     if (from == 0) { // Constructor
     664                 :            :     }
     665                 :            :   }
     666         [ -  + ]:      54010 :   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
     667         [ #  # ]:          0 :     if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
     668         [ #  # ]:          0 :     if (from == 1) cout << "Destroyed:    FGAtmosphere" << endl;
     669                 :            :   }
     670                 :      54010 :   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
     671                 :            :   }
     672                 :      54010 :   if (debug_lvl & 8 ) { // Runtime state variables
     673                 :            :   }
     674                 :      54010 :   if (debug_lvl & 16) { // Sanity checking
     675                 :            :   }
     676         [ -  + ]:      54010 :   if (debug_lvl & 128) { // Turbulence
     677 [ #  # ][ #  # ]:          0 :     if (first_pass && from == 2) {
     678                 :          0 :       first_pass = false;
     679                 :            :       cout << "vTurbulenceNED(X), vTurbulenceNED(Y), vTurbulenceNED(Z), "
     680                 :            :            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
     681                 :            :            << "vDirection(X), vDirection(Y), vDirection(Z), "
     682                 :            :            << "Magnitude, "
     683                 :          0 :            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
     684                 :            :     } 
     685         [ #  # ]:          0 :     if (from == 2) {
     686                 :          0 :       cout << vTurbulenceNED << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
     687                 :            :     }
     688                 :            :   }
     689         [ -  + ]:      54010 :   if (debug_lvl & 64) {
     690         [ #  # ]:          0 :     if (from == 0) { // Constructor
     691                 :          0 :       cout << IdSrc << endl;
     692                 :          0 :       cout << IdHdr << endl;
     693                 :            :     }
     694                 :            :   }
     695                 :            : }
     696                 :            : 
     697 [ +  + ][ +  - ]:         12 : } // namespace JSBSim

Generated by: LCOV version 1.9