| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGAtmosphere.cpp |
| 4 | Author: Jon Berndt, Tony Peden |
| 5 | Date started: 6/2011 |
| 6 | Purpose: Models an atmosphere interface class |
| 7 | Called by: FGFDMExec |
| 8 | |
| 9 | ------------- Copyright (C) 2011 Jon S. Berndt ([email protected]) ------------- |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify it under |
| 12 | the terms of the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 19 | details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License along |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on the world wide web at http://www.gnu.org. |
| 27 | |
| 28 | FUNCTIONAL DESCRIPTION |
| 29 | -------------------------------------------------------------------------------- |
| 30 | This models a base atmosphere class to serve as a common interface to any |
| 31 | derived atmosphere models. |
| 32 | |
| 33 | HISTORY |
| 34 | -------------------------------------------------------------------------------- |
| 35 | 6/18/2011 Started Jon S. Berndt |
| 36 | |
| 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | COMMENTS, REFERENCES, and NOTES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | |
| 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 42 | INCLUDES |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 44 | |
| 45 | #include "FGFDMExec.h" |
| 46 | #include "FGAtmosphere.h" |
| 47 | |
| 48 | using namespace std; |
| 49 | |
| 50 | namespace JSBSim { |
| 51 | |
| 52 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 53 | CLASS IMPLEMENTATION |
| 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 55 | |
| 56 | FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) |
| 57 | : FGModel(fdmex), |
| 58 | StdDaySLsoundspeed(sqrt(SHRatio*Reng0*StdDaySLtemperature)) |
| 59 | { |
| 60 | Name = "FGAtmosphere"; |
| 61 | |
| 62 | bind(); |
| 63 | Debug(0); |
| 64 | } |
| 65 | |
| 66 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 67 | |
| 68 | FGAtmosphere::~FGAtmosphere() |
| 69 | { |
| 70 | Debug(1); |
| 71 | } |
| 72 | |
| 73 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 | |
| 75 | bool FGAtmosphere::InitModel(void) |
| 76 | { |
| 77 | if (!FGModel::InitModel()) return false; |
| 78 | |
| 79 | SLtemperature = Temperature = StdDaySLtemperature; |
| 80 | SLpressure = Pressure = StdDaySLpressure; |
| 81 | SLdensity = Density = Pressure/(Reng*Temperature); |
| 82 | SLsoundspeed = Soundspeed = StdDaySLsoundspeed; |
| 83 | Calculate(0.0); |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 89 | |
| 90 | bool FGAtmosphere::Run(bool Holding) |
| 91 | { |
| 92 | if (FGModel::Run(Holding)) return true; |
| 93 | if (Holding) return false; |
| 94 | |
| 95 | Calculate(in.altitudeASL); |
| 96 | |
| 97 | Debug(2); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 102 | // Using pressure in Outer Space between stars in the Milky Way. |
| 103 | |
| 104 | double FGAtmosphere::ValidatePressure(double p, const string& msg, bool quiet) const |
| 105 | { |
| 106 | const double MinPressure = ConvertToPSF(1E-15, ePascals); |
| 107 | if (p < MinPressure) { |
| 108 | if (!quiet) { |
| 109 | cerr << msg << " " << p << " is too low." << endl |
| 110 | << msg << " is capped to " << MinPressure << endl; |
| 111 | } |
| 112 | return MinPressure; |
| 113 | } |
| 114 | return p; |
| 115 | } |
| 116 | |
| 117 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | // Make sure that the ambient temperature never drops to zero. |
| 119 | // According to Wikipedia, 1K is the temperature at the coolest natural place |
| 120 | // currently (2023) known in the Universe: the Boomerang Nebula |
| 121 | |
| 122 | double FGAtmosphere::ValidateTemperature(double t, const string& msg, bool quiet) const |
| 123 | { |
| 124 | // Minimum known temperature in the universe currently |
| 125 | constexpr double minUniverseTemperature = KelvinToRankine(1.0); |
| 126 | |
| 127 | if (t < minUniverseTemperature) { |
| 128 | if (!quiet) { |
| 129 | cerr << msg << " " << t << " is too low." << endl |
| 130 | << msg << " is capped to " << minUniverseTemperature << endl; |
| 131 | } |
| 132 | return minUniverseTemperature; |
| 133 | } |
| 134 | return t; |
| 135 | } |
| 136 | |
| 137 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 | |
| 139 | void FGAtmosphere::Calculate(double altitude) |
| 140 | { |
| 141 | FGPropertyNode* node = PropertyManager->GetNode(); |
| 142 | double t =0.0; |
| 143 | if (!PropertyManager->HasNode("atmosphere/override/temperature")) |
| 144 | t = GetTemperature(altitude); |
| 145 | else |
| 146 | t = node->GetDouble("atmosphere/override/temperature"); |
| 147 | Temperature = ValidateTemperature(t, "", true); |
| 148 | |
| 149 | double p = 0.0; |
| 150 | if (!PropertyManager->HasNode("atmosphere/override/pressure")) |
| 151 | p = GetPressure(altitude); |
| 152 | else |
| 153 | p = node->GetDouble("atmosphere/override/pressure"); |
| 154 | Pressure = ValidatePressure(p, "", true); |
| 155 | |
| 156 | if (!PropertyManager->HasNode("atmosphere/override/density")) |
| 157 | Density = Pressure/(Reng*Temperature); |
| 158 | else |
| 159 | Density = node->GetDouble("atmosphere/override/density"); |
| 160 | |
| 161 | Soundspeed = sqrt(SHRatio*Reng*Temperature); |
| 162 | PressureAltitude = CalculatePressureAltitude(Pressure, altitude); |
| 163 | DensityAltitude = CalculateDensityAltitude(Density, altitude); |
| 164 | |
| 165 | Viscosity = Beta * pow(Temperature, 1.5) / (SutherlandConstant + Temperature); |
| 166 | KinematicViscosity = Viscosity / Density; |
| 167 | } |
| 168 | |
| 169 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 170 | |
| 171 | void FGAtmosphere::SetPressureSL(ePressure unit, double pressure) |
| 172 | { |
| 173 | double press = ConvertToPSF(pressure, unit); |
| 174 | |
| 175 | SLpressure = ValidatePressure(press, "Sea Level pressure"); |
| 176 | SLdensity = GetDensity(0.0); |
| 177 | } |
| 178 | |
| 179 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 180 | // Get the modeled density at a specified altitude |
| 181 | |
| 182 | double FGAtmosphere::GetDensity(double altitude) const |
| 183 | { |
| 184 | return GetPressure(altitude)/(Reng * GetTemperature(altitude)); |
| 185 | } |
| 186 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 187 | // Get the sound speed at a specified altitude |
| 188 | |
| 189 | double FGAtmosphere::GetSoundSpeed(double altitude) const |
| 190 | { |
| 191 | return sqrt(SHRatio * Reng * GetTemperature(altitude)); |
| 192 | } |
| 193 | |
| 194 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 195 | // This function sets the sea level temperature. |
| 196 | // Internally, the Rankine scale is used for calculations, so any temperature |
| 197 | // supplied must be converted to that unit. |
| 198 | |
| 199 | void FGAtmosphere::SetTemperatureSL(double t, eTemperature unit) |
| 200 | { |
| 201 | double temp = ConvertToRankine(t, unit); |
| 202 | |
| 203 | SLtemperature = ValidateTemperature(temp, "Sea Level temperature"); |
| 204 | SLdensity = GetDensity(0.0); |
| 205 | SLsoundspeed = GetSoundSpeed(0.0); |
| 206 | } |
| 207 | |
| 208 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 209 | |
| 210 | double FGAtmosphere::ConvertToRankine(double t, eTemperature unit) const |
| 211 | { |
| 212 | double targetTemp=0; // in degrees Rankine |
| 213 | |
| 214 | switch(unit) { |
| 215 | case eFahrenheit: |
| 216 | targetTemp = t + 459.67; |
| 217 | break; |
| 218 | case eCelsius: |
| 219 | targetTemp = (t + 273.15) * 1.8; |
| 220 | break; |
| 221 | case eRankine: |
| 222 | targetTemp = t; |
| 223 | break; |
| 224 | case eKelvin: |
| 225 | targetTemp = t*1.8; |
| 226 | break; |
| 227 | default: |
| 228 | throw BaseException("Undefined temperature unit given"); |
| 229 | } |
| 230 | |
| 231 | return targetTemp; |
| 232 | } |
| 233 | |
| 234 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 235 | |
| 236 | double FGAtmosphere::ConvertFromRankine(double t, eTemperature unit) const |
| 237 | { |
| 238 | double targetTemp=0; |
| 239 | |
| 240 | switch(unit) { |
| 241 | case eFahrenheit: |
| 242 | targetTemp = t - 459.67; |
| 243 | break; |
| 244 | case eCelsius: |
| 245 | targetTemp = t/1.8 - 273.15; |
| 246 | break; |
| 247 | case eRankine: |
| 248 | targetTemp = t; |
| 249 | break; |
| 250 | case eKelvin: |
| 251 | targetTemp = t/1.8; |
| 252 | break; |
| 253 | default: |
| 254 | throw BaseException("Undefined temperature unit given"); |
| 255 | } |
| 256 | |
| 257 | return targetTemp; |
| 258 | } |
| 259 | |
| 260 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | |
| 262 | double FGAtmosphere::ConvertToPSF(double p, ePressure unit) const |
| 263 | { |
| 264 | double targetPressure=0; // Pressure in PSF |
| 265 | |
| 266 | switch(unit) { |
| 267 | case ePSF: |
| 268 | targetPressure = p; |
| 269 | break; |
| 270 | case eMillibars: |
| 271 | targetPressure = p*2.08854342; |
| 272 | break; |
| 273 | case ePascals: |
| 274 | targetPressure = p*0.0208854342; |
| 275 | break; |
| 276 | case eInchesHg: |
| 277 | targetPressure = p*70.7180803; |
| 278 | break; |
| 279 | default: |
| 280 | throw BaseException("Undefined pressure unit given"); |
| 281 | } |
| 282 | |
| 283 | return targetPressure; |
| 284 | } |
| 285 | |
| 286 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 287 | |
| 288 | double FGAtmosphere::ConvertFromPSF(double p, ePressure unit) const |
| 289 | { |
| 290 | double targetPressure=0; // Pressure |
| 291 | |
| 292 | switch(unit) { |
| 293 | case ePSF: |
| 294 | targetPressure = p; |
| 295 | break; |
| 296 | case eMillibars: |
| 297 | targetPressure = p/2.08854342; |
| 298 | break; |
| 299 | case ePascals: |
| 300 | targetPressure = p/0.0208854342; |
| 301 | break; |
| 302 | case eInchesHg: |
| 303 | targetPressure = p/70.7180803; |
| 304 | break; |
| 305 | default: |
| 306 | throw BaseException("Undefined pressure unit given"); |
| 307 | } |
| 308 | |
| 309 | return targetPressure; |
| 310 | } |
| 311 | |
| 312 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 313 | |
| 314 | void FGAtmosphere::bind(void) |
| 315 | { |
| 316 | PropertyManager->Tie("atmosphere/T-R", this, &FGAtmosphere::GetTemperature); |
| 317 | PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, &FGAtmosphere::GetDensity); |
| 318 | PropertyManager->Tie("atmosphere/P-psf", this, &FGAtmosphere::GetPressure); |
| 319 | PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed); |
| 320 | PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL); |
| 321 | PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL); |
| 322 | PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL); |
| 323 | PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio); |
| 324 | PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio); |
| 325 | PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio); |
| 326 | PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio); |
| 327 | PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude); |
| 328 | PropertyManager->Tie("atmosphere/pressure-altitude", this, &FGAtmosphere::GetPressureAltitude); |
| 329 | } |
| 330 | |
| 331 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 332 | // The bitmasked value choices are as follows: |
| 333 | // unset: In this case (the default) JSBSim would only print |
| 334 | // out the normally expected messages, essentially echoing |
| 335 | // the config files as they are read. If the environment |
| 336 | // variable is not set, debug_lvl is set to 1 internally |
| 337 | // 0: This requests JSBSim not to output any messages |
| 338 | // whatsoever. |
| 339 | // 1: This value explicity requests the normal JSBSim |
| 340 | // startup messages |
| 341 | // 2: This value asks for a message to be printed out when |
| 342 | // a class is instantiated |
| 343 | // 4: When this value is set, a message is displayed when a |
| 344 | // FGModel object executes its Run() method |
| 345 | // 8: When this value is set, various runtime state variables |
| 346 | // are printed out periodically |
| 347 | // 16: When set various parameters are sanity checked and |
| 348 | // a message is printed out when they go out of bounds |
| 349 | |
| 350 | void FGAtmosphere::Debug(int from) |
| 351 | { |
| 352 | if (debug_lvl <= 0) return; |
| 353 | |
| 354 | if (debug_lvl & 1) { // Standard console startup message output |
| 355 | if (from == 0) { // Constructor |
| 356 | } |
| 357 | } |
| 358 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 359 | if (from == 0) std::cout << "Instantiated: FGAtmosphere" << std::endl; |
| 360 | if (from == 1) std::cout << "Destroyed: FGAtmosphere" << std::endl; |
| 361 | } |
| 362 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 363 | } |
| 364 | if (debug_lvl & 8 ) { // Runtime state variables |
| 365 | } |
| 366 | if (debug_lvl & 16) { // Sanity checking |
| 367 | } |
| 368 | if (debug_lvl & 128) { // |
| 369 | } |
| 370 | if (debug_lvl & 64) { |
| 371 | if (from == 0) { // Constructor |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | } // namespace JSBSim |