]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/atmosphere/FGMSIS.cpp
Merge branch 'jmt/gps'
[flightgear.git] / src / FDM / JSBSim / models / atmosphere / FGMSIS.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGMSIS.cpp
4  Author:       David Culp
5                (incorporated into C++ JSBSim class heirarchy, see model authors below)
6  Date started: 12/14/03
7  Purpose:      Models the MSIS-00 atmosphere
8
9  ------------- Copyright (C) 2003  David P. Culp (davidculp2@comcast.net) ------
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 Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  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 with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 Models the MSIS-00 atmosphere. Provides temperature and density to FGAtmosphere,
31 given day-of-year, time-of-day, altitude, latitude, longitude and local time.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/14/03   DPC   Created
36 01/11/04   DPC   Derived from FGAtmosphere
37
38  --------------------------------------------------------------------
39  ---------  N R L M S I S E - 0 0    M O D E L    2 0 0 1  ----------
40  --------------------------------------------------------------------
41
42  This file is part of the NRLMSISE-00  C source code package - release
43  20020503
44
45  The NRLMSISE-00 model was developed by Mike Picone, Alan Hedin, and
46  Doug Drob. They also wrote a NRLMSISE-00 distribution package in
47  FORTRAN which is available at
48  http://uap-www.nrl.navy.mil/models_web/msis/msis_home.htm
49
50  Dominik Brodowski implemented and maintains this C version. You can
51  reach him at devel@brodo.de. See the file "DOCUMENTATION" for details,
52  and check http://www.brodo.de/english/pub/nrlmsise/index.html for
53  updated releases of this package.
54 */
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 INCLUDES
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 #include "FGMSIS.h"
61 #include "FGState.h"
62 #include <cmath>          /* maths functions */
63 #include <iostream>        // for cout, endl
64
65 using namespace std;
66
67 namespace JSBSim {
68
69 static const char *IdSrc = "$Id$";
70 static const char *IdHdr = ID_MSIS;
71
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 EXTERNAL GLOBAL DATA
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75
76   /* POWER7 */
77   extern double pt[150];
78   extern double pd[9][150];
79   extern double ps[150];
80   extern double pdl[2][25];
81   extern double ptl[4][100];
82   extern double pma[10][100];
83   extern double sam[100];
84
85   /* LOWER7 */
86   extern double ptm[10];
87   extern double pdm[8][10];
88   extern double pavgm[10];
89
90 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 CLASS IMPLEMENTATION
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
93
94
95 MSIS::MSIS(FGFDMExec* fdmex) : FGAtmosphere(fdmex)
96 {
97   Name = "MSIS";
98   bind();
99   Debug(0);
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 MSIS::~MSIS()
105 {
106   Debug(1);
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 bool MSIS::InitModel(void)
112 {
113   FGModel::InitModel();
114
115   unsigned int i;
116
117   flags.switches[0] = 0;
118   for (i=1;i<24;i++) flags.switches[i] = 1;
119
120   for (i=0;i<7;i++) aph.a[i] = 100.0;
121
122   // set some common magnetic flux values
123   input.f107A = 150.0;
124   input.f107 = 150.0;
125   input.ap = 4.0;
126
127   SLtemperature = intTemperature = 518.0;
128   SLpressure    = intPressure = 2116.7;
129   SLdensity     = intDensity = 0.002378;
130   SLsoundspeed  = sqrt(2403.0832 * SLtemperature);
131   rSLtemperature = 1.0/intTemperature;
132   rSLpressure    = 1.0/intPressure;
133   rSLdensity     = 1.0/intDensity;
134   rSLsoundspeed  = 1.0/SLsoundspeed;
135   temperature = &intTemperature;
136   pressure = &intPressure;
137   density = &intDensity;
138
139   UseInternal();
140
141   return true;
142 }
143
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 bool MSIS::Run(void)
147 {
148   if (FGModel::Run()) return true;
149   if (FDMExec->Holding()) return false;
150
151   //do temp, pressure, and density first
152   if (!useExternal) {
153     // get sea-level values
154     Calculate(Auxiliary->GetDayOfYear(),
155               Auxiliary->GetSecondsInDay(),
156               0.0,
157               Propagate->GetLocation().GetLatitudeDeg(),
158               Propagate->GetLocation().GetLongitudeDeg());
159     SLtemperature = output.t[1] * 1.8;
160     SLdensity     = output.d[5] * 1.940321;
161     SLpressure    = 1716.488 * SLdensity * SLtemperature;
162     SLsoundspeed  = sqrt(2403.0832 * SLtemperature);
163     rSLtemperature = 1.0/SLtemperature;
164     rSLpressure    = 1.0/SLpressure;
165     rSLdensity     = 1.0/SLdensity;
166     rSLsoundspeed  = 1.0/SLsoundspeed;
167
168     // get at-altitude values
169     Calculate(Auxiliary->GetDayOfYear(),
170               Auxiliary->GetSecondsInDay(),
171               Propagate->GetAltitudeASL(),
172               Propagate->GetLocation().GetLatitudeDeg(),
173               Propagate->GetLocation().GetLongitudeDeg());
174     intTemperature = output.t[1] * 1.8;
175     intDensity     = output.d[5] * 1.940321;
176     intPressure    = 1716.488 * intDensity * intTemperature;
177     //cout << "T=" << intTemperature << " D=" << intDensity << " P=";
178     //cout << intPressure << " a=" << soundspeed << endl;
179   }
180
181   CalculateDerived();
182
183   Debug(2);
184
185   return false;
186 }
187
188 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189
190 void MSIS::Calculate(int day, double sec, double alt, double lat, double lon)
191 {
192   input.year = 2000;
193   input.doy = day;
194   input.sec = sec;
195   input.alt = alt / 3281;  //feet to kilometers
196   input.g_lat = lat;
197   input.g_long = lon;
198
199   input.lst = (sec/3600) + (lon/15);
200   if (input.lst > 24.0) input.lst -= 24.0;
201   if (input.lst < 0.0) input.lst = 24 - input.lst;
202
203   gtd7d(&input, &flags, &output);
204 }
205
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207
208
209 void MSIS::UseExternal(void){
210   // do nothing, external control not allowed
211 }
212
213
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215
216
217 void MSIS::tselec(struct nrlmsise_flags *flags)
218 {
219   int i;
220   for (i=0;i<24;i++) {
221     if (i!=9) {
222       if (flags->switches[i]==1)
223         flags->sw[i]=1;
224       else
225         flags->sw[i]=0;
226       if (flags->switches[i]>0)
227         flags->swc[i]=1;
228       else
229         flags->swc[i]=0;
230     } else {
231       flags->sw[i]=flags->switches[i];
232       flags->swc[i]=flags->switches[i];
233     }
234   }
235 }
236
237
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239
240 void MSIS::glatf(double lat, double *gv, double *reff)
241 {
242   double dgtr = 1.74533E-2;
243   double c2;
244   c2 = cos(2.0*dgtr*lat);
245   *gv = 980.616 * (1.0 - 0.0026373 * c2);
246   *reff = 2.0 * (*gv) / (3.085462E-6 + 2.27E-9 * c2) * 1.0E-5;
247 }
248
249 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250
251 double MSIS::ccor(double alt, double r, double h1, double zh)
252 {
253 /*        CHEMISTRY/DISSOCIATION CORRECTION FOR MSIS MODELS
254  *         ALT - altitude
255  *         R - target ratio
256  *         H1 - transition scale length
257  *         ZH - altitude of 1/2 R
258  */
259   double e;
260   double ex;
261   e = (alt - zh) / h1;
262   if (e>70)
263     return exp(0.0);
264   if (e<-70)
265     return exp(r);
266   ex = exp(e);
267   e = r / (1.0 + ex);
268   return exp(e);
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 double MSIS::ccor2(double alt, double r, double h1, double zh, double h2)
274 {
275 /*        CHEMISTRY/DISSOCIATION CORRECTION FOR MSIS MODELS
276  *         ALT - altitude
277  *         R - target ratio
278  *         H1 - transition scale length
279  *         ZH - altitude of 1/2 R
280  *         H2 - transition scale length #2 ?
281  */
282   double e1, e2;
283   double ex1, ex2;
284   double ccor2v;
285   e1 = (alt - zh) / h1;
286   e2 = (alt - zh) / h2;
287   if ((e1 > 70) || (e2 > 70))
288     return exp(0.0);
289   if ((e1 < -70) && (e2 < -70))
290     return exp(r);
291   ex1 = exp(e1);
292   ex2 = exp(e2);
293   ccor2v = r / (1.0 + 0.5 * (ex1 + ex2));
294   return exp(ccor2v);
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298
299 double MSIS::scalh(double alt, double xm, double temp)
300 {
301   double g;
302   double rgas=831.4;
303   g = gsurf / (pow((1.0 + alt/re),2.0));
304   g = rgas * temp / (g * xm);
305   return g;
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 double MSIS::dnet (double dd, double dm, double zhm, double xmm, double xm)
311 {
312 /*       TURBOPAUSE CORRECTION FOR MSIS MODELS
313  *        Root mean density
314  *         DD - diffusive density
315  *         DM - full mixed density
316  *         ZHM - transition scale length
317  *         XMM - full mixed molecular weight
318  *         XM  - species molecular weight
319  *         DNET - combined density
320  */
321   double a;
322   double ylog;
323   a  = zhm / (xmm-xm);
324   if (!((dm>0) && (dd>0))) {
325     cerr << "dnet log error " << dm << ' ' << dd << ' ' << xm << ' ' << endl;
326     if ((dd==0) && (dm==0))
327       dd=1;
328     if (dm==0)
329       return dd;
330     if (dd==0)
331       return dm;
332   }
333   ylog = a * log(dm/dd);
334   if (ylog<-10)
335     return dd;
336   if (ylog>10)
337     return dm;
338   a = dd*pow((1.0 + exp(ylog)),(1.0/a));
339   return a;
340 }
341
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343
344 void MSIS::splini (double *xa, double *ya, double *y2a, int n, double x, double *y)
345 {
346 /*      INTEGRATE CUBIC SPLINE FUNCTION FROM XA(1) TO X
347  *       XA,YA: ARRAYS OF TABULATED FUNCTION IN ASCENDING ORDER BY X
348  *       Y2A: ARRAY OF SECOND DERIVATIVES
349  *       N: SIZE OF ARRAYS XA,YA,Y2A
350  *       X: ABSCISSA ENDPOINT FOR INTEGRATION
351  *       Y: OUTPUT VALUE
352  */
353   double yi=0;
354   int klo=0;
355   int khi=1;
356   double xx, h, a, b, a2, b2;
357   while ((x>xa[klo]) && (khi<n)) {
358     xx=x;
359     if (khi<(n-1)) {
360       if (x<xa[khi])
361         xx=x;
362       else
363         xx=xa[khi];
364     }
365     h = xa[khi] - xa[klo];
366     a = (xa[khi] - xx)/h;
367     b = (xx - xa[klo])/h;
368     a2 = a*a;
369     b2 = b*b;
370     yi += ((1.0 - a2) * ya[klo] / 2.0 + b2 * ya[khi] / 2.0 + ((-(1.0+a2*a2)/4.0 + a2/2.0) * y2a[klo] + (b2*b2/4.0 - b2/2.0) * y2a[khi]) * h * h / 6.0) * h;
371     klo++;
372     khi++;
373   }
374   *y = yi;
375 }
376
377 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378
379 void MSIS::splint (double *xa, double *ya, double *y2a, int n, double x, double *y)
380 {
381 /*      CALCULATE CUBIC SPLINE INTERP VALUE
382  *       ADAPTED FROM NUMERICAL RECIPES BY PRESS ET AL.
383  *       XA,YA: ARRAYS OF TABULATED FUNCTION IN ASCENDING ORDER BY X
384  *       Y2A: ARRAY OF SECOND DERIVATIVES
385  *       N: SIZE OF ARRAYS XA,YA,Y2A
386  *       X: ABSCISSA FOR INTERPOLATION
387  *       Y: OUTPUT VALUE
388  */
389   int klo=0;
390   int khi=n-1;
391   int k;
392   double h;
393   double a, b, yi;
394   while ((khi-klo)>1) {
395     k=(khi+klo)/2;
396     if (xa[k]>x)
397       khi=k;
398     else
399       klo=k;
400   }
401   h = xa[khi] - xa[klo];
402   if (h==0.0)
403     cerr << "bad XA input to splint" << endl;
404   a = (xa[khi] - x)/h;
405   b = (x - xa[klo])/h;
406   yi = a * ya[klo] + b * ya[khi] + ((a*a*a - a) * y2a[klo] + (b*b*b - b) * y2a[khi]) * h * h/6.0;
407   *y = yi;
408 }
409
410 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411
412 void MSIS::spline (double *x, double *y, int n, double yp1, double ypn, double *y2)
413 {
414 /*       CALCULATE 2ND DERIVATIVES OF CUBIC SPLINE INTERP FUNCTION
415  *       ADAPTED FROM NUMERICAL RECIPES BY PRESS ET AL
416  *       X,Y: ARRAYS OF TABULATED FUNCTION IN ASCENDING ORDER BY X
417  *       N: SIZE OF ARRAYS X,Y
418  *       YP1,YPN: SPECIFIED DERIVATIVES AT X[0] AND X[N-1]; VALUES
419  *                >= 1E30 SIGNAL SIGNAL SECOND DERIVATIVE ZERO
420  *       Y2: OUTPUT ARRAY OF SECOND DERIVATIVES
421  */
422   double *u;
423   double sig, p, qn, un;
424   int i, k;
425   u=new double[n];
426   if (u==NULL) {
427     cerr << "Out Of Memory in spline - ERROR" << endl;
428     return;
429   }
430   if (yp1>0.99E30) {
431     y2[0]=0;
432     u[0]=0;
433   } else {
434     y2[0]=-0.5;
435     u[0]=(3.0/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yp1);
436   }
437   for (i=1;i<(n-1);i++) {
438     sig = (x[i]-x[i-1])/(x[i+1] - x[i-1]);
439     p = sig * y2[i-1] + 2.0;
440     y2[i] = (sig - 1.0) / p;
441     u[i] = (6.0 * ((y[i+1] - y[i])/(x[i+1] - x[i]) -(y[i] - y[i-1]) / (x[i] - x[i-1]))/(x[i+1] - x[i-1]) - sig * u[i-1])/p;
442   }
443   if (ypn>0.99E30) {
444     qn = 0;
445     un = 0;
446   } else {
447     qn = 0.5;
448     un = (3.0 / (x[n-1] - x[n-2])) * (ypn - (y[n-1] - y[n-2])/(x[n-1] - x[n-2]));
449   }
450   y2[n-1] = (un - qn * u[n-2]) / (qn * y2[n-2] + 1.0);
451   for (k=n-2;k>=0;k--)
452     y2[k] = y2[k] * y2[k+1] + u[k];
453
454   delete u;
455 }
456
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458
459 double MSIS::zeta(double zz, double zl)
460 {
461   return ((zz-zl)*(re+zl)/(re+zz));
462 }
463
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465
466 double MSIS::densm(double alt, double d0, double xm, double *tz, int mn3,
467                      double *zn3, double *tn3, double *tgn3, int mn2, double *zn2,
468                      double *tn2, double *tgn2)
469 {
470 /*      Calculate Temperature and Density Profiles for lower atmos.  */
471   double xs[10], ys[10], y2out[10];
472   double rgas = 831.4;
473   double z, z1, z2, t1, t2, zg, zgdif;
474   double yd1, yd2;
475   double x, y, yi;
476   double expl, gamm, glb;
477   double densm_tmp;
478   int mn;
479   int k;
480   densm_tmp=d0;
481   if (alt>zn2[0]) {
482     if (xm==0.0)
483       return *tz;
484     else
485       return d0;
486   }
487
488   /* STRATOSPHERE/MESOSPHERE TEMPERATURE */
489   if (alt>zn2[mn2-1])
490     z=alt;
491   else
492     z=zn2[mn2-1];
493   mn=mn2;
494   z1=zn2[0];
495   z2=zn2[mn-1];
496   t1=tn2[0];
497   t2=tn2[mn-1];
498   zg = zeta(z, z1);
499   zgdif = zeta(z2, z1);
500
501   /* set up spline nodes */
502   for (k=0;k<mn;k++) {
503     xs[k]=zeta(zn2[k],z1)/zgdif;
504     ys[k]=1.0 / tn2[k];
505   }
506   yd1=-tgn2[0] / (t1*t1) * zgdif;
507   yd2=-tgn2[1] / (t2*t2) * zgdif * (pow(((re+z2)/(re+z1)),2.0));
508
509   /* calculate spline coefficients */
510   spline (xs, ys, mn, yd1, yd2, y2out);
511   x = zg/zgdif;
512   splint (xs, ys, y2out, mn, x, &y);
513
514   /* temperature at altitude */
515   *tz = 1.0 / y;
516   if (xm!=0.0) {
517     /* calaculate stratosphere / mesospehere density */
518     glb = gsurf / (pow((1.0 + z1/re),2.0));
519     gamm = xm * glb * zgdif / rgas;
520
521     /* Integrate temperature profile */
522     splini(xs, ys, y2out, mn, x, &yi);
523     expl=gamm*yi;
524     if (expl>50.0)
525       expl=50.0;
526
527     /* Density at altitude */
528     densm_tmp = densm_tmp * (t1 / *tz) * exp(-expl);
529   }
530
531   if (alt>zn3[0]) {
532     if (xm==0.0)
533       return *tz;
534     else
535       return densm_tmp;
536   }
537
538   /* troposhere / stratosphere temperature */
539   z = alt;
540   mn = mn3;
541   z1=zn3[0];
542   z2=zn3[mn-1];
543   t1=tn3[0];
544   t2=tn3[mn-1];
545   zg=zeta(z,z1);
546   zgdif=zeta(z2,z1);
547
548   /* set up spline nodes */
549   for (k=0;k<mn;k++) {
550     xs[k] = zeta(zn3[k],z1) / zgdif;
551     ys[k] = 1.0 / tn3[k];
552   }
553   yd1=-tgn3[0] / (t1*t1) * zgdif;
554   yd2=-tgn3[1] / (t2*t2) * zgdif * (pow(((re+z2)/(re+z1)),2.0));
555
556   /* calculate spline coefficients */
557   spline (xs, ys, mn, yd1, yd2, y2out);
558   x = zg/zgdif;
559   splint (xs, ys, y2out, mn, x, &y);
560
561   /* temperature at altitude */
562   *tz = 1.0 / y;
563   if (xm!=0.0) {
564     /* calaculate tropospheric / stratosphere density */
565     glb = gsurf / (pow((1.0 + z1/re),2.0));
566     gamm = xm * glb * zgdif / rgas;
567
568     /* Integrate temperature profile */
569     splini(xs, ys, y2out, mn, x, &yi);
570     expl=gamm*yi;
571     if (expl>50.0)
572       expl=50.0;
573
574     /* Density at altitude */
575     densm_tmp = densm_tmp * (t1 / *tz) * exp(-expl);
576   }
577   if (xm==0.0)
578     return *tz;
579   else
580     return densm_tmp;
581 }
582
583 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584
585 double MSIS::densu(double alt, double dlb, double tinf, double tlb, double xm,
586                      double alpha, double *tz, double zlb, double s2, int mn1,
587                      double *zn1, double *tn1, double *tgn1)
588 {
589 /*      Calculate Temperature and Density Profiles for MSIS models
590  *      New lower thermo polynomial
591  */
592   double yd2, yd1, x=0.0, y=0.0;
593   double rgas=831.4;
594   double densu_temp=1.0;
595   double za, z, zg2, tt, ta=0.0;
596   double dta, z1=0.0, z2, t1=0.0, t2, zg, zgdif=0.0;
597   int mn=0;
598   int k;
599   double glb;
600   double expl;
601   double yi;
602   double densa;
603   double gamma, gamm;
604   double xs[5], ys[5], y2out[5];
605   /* joining altitudes of Bates and spline */
606   za=zn1[0];
607   if (alt>za)
608     z=alt;
609   else
610     z=za;
611
612   /* geopotential altitude difference from ZLB */
613   zg2 = zeta(z, zlb);
614
615   /* Bates temperature */
616   tt = tinf - (tinf - tlb) * exp(-s2*zg2);
617   ta = tt;
618   *tz = tt;
619   densu_temp = *tz;
620
621   if (alt<za) {
622     /* calculate temperature below ZA
623      * temperature gradient at ZA from Bates profile */
624     dta = (tinf - ta) * s2 * pow(((re+zlb)/(re+za)),2.0);
625     tgn1[0]=dta;
626     tn1[0]=ta;
627     if (alt>zn1[mn1-1])
628       z=alt;
629     else
630       z=zn1[mn1-1];
631     mn=mn1;
632     z1=zn1[0];
633     z2=zn1[mn-1];
634     t1=tn1[0];
635     t2=tn1[mn-1];
636     /* geopotental difference from z1 */
637     zg = zeta (z, z1);
638     zgdif = zeta(z2, z1);
639     /* set up spline nodes */
640     for (k=0;k<mn;k++) {
641       xs[k] = zeta(zn1[k], z1) / zgdif;
642       ys[k] = 1.0 / tn1[k];
643     }
644     /* end node derivatives */
645     yd1 = -tgn1[0] / (t1*t1) * zgdif;
646     yd2 = -tgn1[1] / (t2*t2) * zgdif * pow(((re+z2)/(re+z1)),2.0);
647     /* calculate spline coefficients */
648     spline (xs, ys, mn, yd1, yd2, y2out);
649     x = zg / zgdif;
650     splint (xs, ys, y2out, mn, x, &y);
651     /* temperature at altitude */
652     *tz = 1.0 / y;
653     densu_temp = *tz;
654   }
655   if (xm==0)
656     return densu_temp;
657
658   /* calculate density above za */
659   glb = gsurf / pow((1.0 + zlb/re),2.0);
660   gamma = xm * glb / (s2 * rgas * tinf);
661   expl = exp(-s2 * gamma * zg2);
662   if (expl>50.0)
663       expl=50.0;
664   if (tt<=0)
665     expl=50.0;
666
667   /* density at altitude */
668   densa = dlb * pow((tlb/tt),((1.0+alpha+gamma))) * expl;
669   densu_temp=densa;
670   if (alt>=za)
671     return densu_temp;
672
673   /* calculate density below za */
674   glb = gsurf / pow((1.0 + z1/re),2.0);
675   gamm = xm * glb * zgdif / rgas;
676
677   /* integrate spline temperatures */
678   splini (xs, ys, y2out, mn, x, &yi);
679   expl = gamm * yi;
680   if (expl>50.0)
681     expl=50.0;
682   if (*tz<=0)
683     expl=50.0;
684
685   /* density at altitude */
686   densu_temp = densu_temp * pow ((t1 / *tz),(1.0 + alpha)) * exp(-expl);
687   return densu_temp;
688 }
689
690 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691
692 /*    3hr Magnetic activity functions */
693 /*    Eq. A24d */
694 double MSIS::g0(double a, double *p)
695 {
696   return (a - 4.0 + (p[25] - 1.0) * (a - 4.0 + (exp(-sqrt(p[24]*p[24]) *
697                 (a - 4.0)) - 1.0) / sqrt(p[24]*p[24])));
698 }
699
700 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701
702 /*    Eq. A24c */
703 double MSIS::sumex(double ex)
704 {
705   return (1.0 + (1.0 - pow(ex,19.0)) / (1.0 - ex) * pow(ex,0.5));
706 }
707
708 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709
710 /*    Eq. A24a */
711 double MSIS::sg0(double ex, double *p, double *ap)
712 {
713   return (g0(ap[1],p) + (g0(ap[2],p)*ex + g0(ap[3],p)*ex*ex +
714                 g0(ap[4],p)*pow(ex,3.0)  + (g0(ap[5],p)*pow(ex,4.0) +
715                 g0(ap[6],p)*pow(ex,12.0))*(1.0-pow(ex,8.0))/(1.0-ex)))/sumex(ex);
716 }
717
718 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719
720 double MSIS::globe7(double *p, struct nrlmsise_input *input,
721                       struct nrlmsise_flags *flags)
722 {
723 /*       CALCULATE G(L) FUNCTION
724  *       Upper Thermosphere Parameters */
725   double t[15];
726   int i,j;
727   int sw9=1;
728   double apd;
729   double xlong;
730   double tloc;
731   double c, s, c2, c4, s2;
732   double sr = 7.2722E-5;
733   double dgtr = 1.74533E-2;
734   double dr = 1.72142E-2;
735   double hr = 0.2618;
736   double cd32, cd18, cd14, cd39;
737   double p32, p18, p14, p39;
738   double df, dfa;
739   double f1, f2;
740   double tinf;
741   struct ap_array *ap;
742
743   tloc=input->lst;
744   for (j=0;j<14;j++)
745     t[j]=0;
746   if (flags->sw[9]>0)
747     sw9=1;
748   else if (flags->sw[9]<0)
749     sw9=-1;
750   xlong = input->g_long;
751
752   /* calculate legendre polynomials */
753   c = sin(input->g_lat * dgtr);
754   s = cos(input->g_lat * dgtr);
755   c2 = c*c;
756   c4 = c2*c2;
757   s2 = s*s;
758
759   plg[0][1] = c;
760   plg[0][2] = 0.5*(3.0*c2 -1.0);
761   plg[0][3] = 0.5*(5.0*c*c2-3.0*c);
762   plg[0][4] = (35.0*c4 - 30.0*c2 + 3.0)/8.0;
763   plg[0][5] = (63.0*c2*c2*c - 70.0*c2*c + 15.0*c)/8.0;
764   plg[0][6] = (11.0*c*plg[0][5] - 5.0*plg[0][4])/6.0;
765 /*      plg[0][7] = (13.0*c*plg[0][6] - 6.0*plg[0][5])/7.0; */
766   plg[1][1] = s;
767   plg[1][2] = 3.0*c*s;
768   plg[1][3] = 1.5*(5.0*c2-1.0)*s;
769   plg[1][4] = 2.5*(7.0*c2*c-3.0*c)*s;
770   plg[1][5] = 1.875*(21.0*c4 - 14.0*c2 +1.0)*s;
771   plg[1][6] = (11.0*c*plg[1][5]-6.0*plg[1][4])/5.0;
772 /*      plg[1][7] = (13.0*c*plg[1][6]-7.0*plg[1][5])/6.0; */
773 /*      plg[1][8] = (15.0*c*plg[1][7]-8.0*plg[1][6])/7.0; */
774   plg[2][2] = 3.0*s2;
775   plg[2][3] = 15.0*s2*c;
776   plg[2][4] = 7.5*(7.0*c2 -1.0)*s2;
777   plg[2][5] = 3.0*c*plg[2][4]-2.0*plg[2][3];
778   plg[2][6] =(11.0*c*plg[2][5]-7.0*plg[2][4])/4.0;
779   plg[2][7] =(13.0*c*plg[2][6]-8.0*plg[2][5])/5.0;
780   plg[3][3] = 15.0*s2*s;
781   plg[3][4] = 105.0*s2*s*c;
782   plg[3][5] =(9.0*c*plg[3][4]-7.*plg[3][3])/2.0;
783   plg[3][6] =(11.0*c*plg[3][5]-8.*plg[3][4])/3.0;
784
785   if (!(((flags->sw[7]==0)&&(flags->sw[8]==0))&&(flags->sw[14]==0))) {
786     stloc = sin(hr*tloc);
787     ctloc = cos(hr*tloc);
788     s2tloc = sin(2.0*hr*tloc);
789     c2tloc = cos(2.0*hr*tloc);
790     s3tloc = sin(3.0*hr*tloc);
791     c3tloc = cos(3.0*hr*tloc);
792   }
793
794   cd32 = cos(dr*(input->doy-p[31]));
795   cd18 = cos(2.0*dr*(input->doy-p[17]));
796   cd14 = cos(dr*(input->doy-p[13]));
797   cd39 = cos(2.0*dr*(input->doy-p[38]));
798   p32=p[31];
799   p18=p[17];
800   p14=p[13];
801   p39=p[38];
802
803   /* F10.7 EFFECT */
804   df = input->f107 - input->f107A;
805   dfa = input->f107A - 150.0;
806   t[0] =  p[19]*df*(1.0+p[59]*dfa) + p[20]*df*df + p[21]*dfa + p[29]*pow(dfa,2.0);
807   f1 = 1.0 + (p[47]*dfa +p[19]*df+p[20]*df*df)*flags->swc[1];
808   f2 = 1.0 + (p[49]*dfa+p[19]*df+p[20]*df*df)*flags->swc[1];
809
810   /*  TIME INDEPENDENT */
811   t[1] = (p[1]*plg[0][2]+ p[2]*plg[0][4]+p[22]*plg[0][6]) +
812         (p[14]*plg[0][2])*dfa*flags->swc[1] +p[26]*plg[0][1];
813
814   /*  SYMMETRICAL ANNUAL */
815   t[2] = p[18]*cd32;
816
817   /*  SYMMETRICAL SEMIANNUAL */
818   t[3] = (p[15]+p[16]*plg[0][2])*cd18;
819
820   /*  ASYMMETRICAL ANNUAL */
821   t[4] =  f1*(p[9]*plg[0][1]+p[10]*plg[0][3])*cd14;
822
823   /*  ASYMMETRICAL SEMIANNUAL */
824   t[5] =    p[37]*plg[0][1]*cd39;
825
826         /* DIURNAL */
827   if (flags->sw[7]) {
828     double t71, t72;
829     t71 = (p[11]*plg[1][2])*cd14*flags->swc[5];
830     t72 = (p[12]*plg[1][2])*cd14*flags->swc[5];
831     t[6] = f2*((p[3]*plg[1][1] + p[4]*plg[1][3] + p[27]*plg[1][5] + t71) * \
832          ctloc + (p[6]*plg[1][1] + p[7]*plg[1][3] + p[28]*plg[1][5] \
833             + t72)*stloc);
834 }
835
836   /* SEMIDIURNAL */
837   if (flags->sw[8]) {
838     double t81, t82;
839     t81 = (p[23]*plg[2][3]+p[35]*plg[2][5])*cd14*flags->swc[5];
840     t82 = (p[33]*plg[2][3]+p[36]*plg[2][5])*cd14*flags->swc[5];
841     t[7] = f2*((p[5]*plg[2][2]+ p[41]*plg[2][4] + t81)*c2tloc +(p[8]*plg[2][2] + p[42]*plg[2][4] + t82)*s2tloc);
842   }
843
844   /* TERDIURNAL */
845   if (flags->sw[14]) {
846     t[13] = f2 * ((p[39]*plg[3][3]+(p[93]*plg[3][4]+p[46]*plg[3][6])*cd14*flags->swc[5])* s3tloc +(p[40]*plg[3][3]+(p[94]*plg[3][4]+p[48]*plg[3][6])*cd14*flags->swc[5])* c3tloc);
847 }
848
849   /* magnetic activity based on daily ap */
850   if (flags->sw[9]==-1) {
851     ap = input->ap_a;
852     if (p[51]!=0) {
853       double exp1;
854       exp1 = exp(-10800.0*sqrt(p[51]*p[51])/(1.0+p[138]*(45.0-sqrt(input->g_lat*input->g_lat))));
855       if (exp1>0.99999)
856         exp1=0.99999;
857       if (p[24]<1.0E-4)
858         p[24]=1.0E-4;
859       apt[0]=sg0(exp1,p,ap->a);
860       /* apt[1]=sg2(exp1,p,ap->a);
861          apt[2]=sg0(exp2,p,ap->a);
862          apt[3]=sg2(exp2,p,ap->a);
863       */
864       if (flags->sw[9]) {
865         t[8] = apt[0]*(p[50]+p[96]*plg[0][2]+p[54]*plg[0][4]+ \
866      (p[125]*plg[0][1]+p[126]*plg[0][3]+p[127]*plg[0][5])*cd14*flags->swc[5]+ \
867      (p[128]*plg[1][1]+p[129]*plg[1][3]+p[130]*plg[1][5])*flags->swc[7]* \
868                  cos(hr*(tloc-p[131])));
869       }
870     }
871   } else {
872     double p44, p45;
873     apd=input->ap-4.0;
874     p44=p[43];
875     p45=p[44];
876     if (p44<0)
877       p44 = 1.0E-5;
878     apdf = apd + (p45-1.0)*(apd + (exp(-p44 * apd) - 1.0)/p44);
879     if (flags->sw[9]) {
880       t[8]=apdf*(p[32]+p[45]*plg[0][2]+p[34]*plg[0][4]+ \
881      (p[100]*plg[0][1]+p[101]*plg[0][3]+p[102]*plg[0][5])*cd14*flags->swc[5]+
882      (p[121]*plg[1][1]+p[122]*plg[1][3]+p[123]*plg[1][5])*flags->swc[7]*
883             cos(hr*(tloc-p[124])));
884     }
885   }
886
887   if ((flags->sw[10])&&(input->g_long>-1000.0)) {
888
889     /* longitudinal */
890     if (flags->sw[11]) {
891       t[10] = (1.0 + p[80]*dfa*flags->swc[1])* \
892      ((p[64]*plg[1][2]+p[65]*plg[1][4]+p[66]*plg[1][6]\
893       +p[103]*plg[1][1]+p[104]*plg[1][3]+p[105]*plg[1][5]\
894       +flags->swc[5]*(p[109]*plg[1][1]+p[110]*plg[1][3]+p[111]*plg[1][5])*cd14)* \
895           cos(dgtr*input->g_long) \
896       +(p[90]*plg[1][2]+p[91]*plg[1][4]+p[92]*plg[1][6]\
897       +p[106]*plg[1][1]+p[107]*plg[1][3]+p[108]*plg[1][5]\
898       +flags->swc[5]*(p[112]*plg[1][1]+p[113]*plg[1][3]+p[114]*plg[1][5])*cd14)* \
899       sin(dgtr*input->g_long));
900     }
901
902     /* ut and mixed ut, longitude */
903     if (flags->sw[12]){
904       t[11]=(1.0+p[95]*plg[0][1])*(1.0+p[81]*dfa*flags->swc[1])*\
905         (1.0+p[119]*plg[0][1]*flags->swc[5]*cd14)*\
906         ((p[68]*plg[0][1]+p[69]*plg[0][3]+p[70]*plg[0][5])*\
907         cos(sr*(input->sec-p[71])));
908       t[11]+=flags->swc[11]*\
909         (p[76]*plg[2][3]+p[77]*plg[2][5]+p[78]*plg[2][7])*\
910         cos(sr*(input->sec-p[79])+2.0*dgtr*input->g_long)*(1.0+p[137]*dfa*flags->swc[1]);
911     }
912
913     /* ut, longitude magnetic activity */
914     if (flags->sw[13]) {
915       if (flags->sw[9]==-1) {
916         if (p[51]) {
917           t[12]=apt[0]*flags->swc[11]*(1.+p[132]*plg[0][1])*\
918             ((p[52]*plg[1][2]+p[98]*plg[1][4]+p[67]*plg[1][6])*\
919              cos(dgtr*(input->g_long-p[97])))\
920             +apt[0]*flags->swc[11]*flags->swc[5]*\
921             (p[133]*plg[1][1]+p[134]*plg[1][3]+p[135]*plg[1][5])*\
922             cd14*cos(dgtr*(input->g_long-p[136])) \
923             +apt[0]*flags->swc[12]* \
924             (p[55]*plg[0][1]+p[56]*plg[0][3]+p[57]*plg[0][5])*\
925             cos(sr*(input->sec-p[58]));
926         }
927       } else {
928         t[12] = apdf*flags->swc[11]*(1.0+p[120]*plg[0][1])*\
929           ((p[60]*plg[1][2]+p[61]*plg[1][4]+p[62]*plg[1][6])*\
930           cos(dgtr*(input->g_long-p[63])))\
931           +apdf*flags->swc[11]*flags->swc[5]* \
932           (p[115]*plg[1][1]+p[116]*plg[1][3]+p[117]*plg[1][5])* \
933           cd14*cos(dgtr*(input->g_long-p[118])) \
934           + apdf*flags->swc[12]* \
935           (p[83]*plg[0][1]+p[84]*plg[0][3]+p[85]*plg[0][5])* \
936           cos(sr*(input->sec-p[75]));
937       }
938     }
939   }
940
941   /* parms not used: 82, 89, 99, 139-149 */
942   tinf = p[30];
943   for (i=0;i<14;i++)
944     tinf = tinf + fabs(flags->sw[i+1])*t[i];
945   return tinf;
946 }
947
948 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949
950 double MSIS::glob7s(double *p, struct nrlmsise_input *input,
951                       struct nrlmsise_flags *flags)
952 {
953 /*    VERSION OF GLOBE FOR LOWER ATMOSPHERE 10/26/99
954  */
955   double pset=2.0;
956   double t[14];
957   double tt;
958   double cd32, cd18, cd14, cd39;
959   double p32, p18, p14, p39;
960   int i,j;
961   double dr=1.72142E-2;
962   double dgtr=1.74533E-2;
963   /* confirm parameter set */
964   if (p[99]==0)
965     p[99]=pset;
966   if (p[99]!=pset) {
967     cerr << "Wrong parameter set for glob7s" << endl;
968     return -1;
969   }
970   for (j=0;j<14;j++)
971     t[j]=0.0;
972   cd32 = cos(dr*(input->doy-p[31]));
973   cd18 = cos(2.0*dr*(input->doy-p[17]));
974   cd14 = cos(dr*(input->doy-p[13]));
975   cd39 = cos(2.0*dr*(input->doy-p[38]));
976   p32=p[31];
977   p18=p[17];
978   p14=p[13];
979   p39=p[38];
980
981   /* F10.7 */
982   t[0] = p[21]*dfa;
983
984   /* time independent */
985   t[1]=p[1]*plg[0][2] + p[2]*plg[0][4] + p[22]*plg[0][6] + p[26]*plg[0][1] + p[14]*plg[0][3] + p[59]*plg[0][5];
986
987         /* SYMMETRICAL ANNUAL */
988   t[2]=(p[18]+p[47]*plg[0][2]+p[29]*plg[0][4])*cd32;
989
990         /* SYMMETRICAL SEMIANNUAL */
991   t[3]=(p[15]+p[16]*plg[0][2]+p[30]*plg[0][4])*cd18;
992
993         /* ASYMMETRICAL ANNUAL */
994   t[4]=(p[9]*plg[0][1]+p[10]*plg[0][3]+p[20]*plg[0][5])*cd14;
995
996   /* ASYMMETRICAL SEMIANNUAL */
997   t[5]=(p[37]*plg[0][1])*cd39;
998
999         /* DIURNAL */
1000   if (flags->sw[7]) {
1001     double t71, t72;
1002     t71 = p[11]*plg[1][2]*cd14*flags->swc[5];
1003     t72 = p[12]*plg[1][2]*cd14*flags->swc[5];
1004     t[6] = ((p[3]*plg[1][1] + p[4]*plg[1][3] + t71) * ctloc + (p[6]*plg[1][1] + p[7]*plg[1][3] + t72) * stloc) ;
1005   }
1006
1007   /* SEMIDIURNAL */
1008   if (flags->sw[8]) {
1009     double t81, t82;
1010     t81 = (p[23]*plg[2][3]+p[35]*plg[2][5])*cd14*flags->swc[5];
1011     t82 = (p[33]*plg[2][3]+p[36]*plg[2][5])*cd14*flags->swc[5];
1012     t[7] = ((p[5]*plg[2][2] + p[41]*plg[2][4] + t81) * c2tloc + (p[8]*plg[2][2] + p[42]*plg[2][4] + t82) * s2tloc);
1013   }
1014
1015   /* TERDIURNAL */
1016   if (flags->sw[14]) {
1017     t[13] = p[39] * plg[3][3] * s3tloc + p[40] * plg[3][3] * c3tloc;
1018   }
1019
1020   /* MAGNETIC ACTIVITY */
1021   if (flags->sw[9]) {
1022     if (flags->sw[9]==1)
1023       t[8] = apdf * (p[32] + p[45] * plg[0][2] * flags->swc[2]);
1024     if (flags->sw[9]==-1)
1025       t[8]=(p[50]*apt[0] + p[96]*plg[0][2] * apt[0]*flags->swc[2]);
1026   }
1027
1028   /* LONGITUDINAL */
1029   if (!((flags->sw[10]==0) || (flags->sw[11]==0) || (input->g_long<=-1000.0))) {
1030     t[10] = (1.0 + plg[0][1]*(p[80]*flags->swc[5]*cos(dr*(input->doy-p[81]))\
1031             +p[85]*flags->swc[6]*cos(2.0*dr*(input->doy-p[86])))\
1032       +p[83]*flags->swc[3]*cos(dr*(input->doy-p[84]))\
1033       +p[87]*flags->swc[4]*cos(2.0*dr*(input->doy-p[88])))\
1034       *((p[64]*plg[1][2]+p[65]*plg[1][4]+p[66]*plg[1][6]\
1035       +p[74]*plg[1][1]+p[75]*plg[1][3]+p[76]*plg[1][5]\
1036       )*cos(dgtr*input->g_long)\
1037       +(p[90]*plg[1][2]+p[91]*plg[1][4]+p[92]*plg[1][6]\
1038       +p[77]*plg[1][1]+p[78]*plg[1][3]+p[79]*plg[1][5]\
1039       )*sin(dgtr*input->g_long));
1040   }
1041   tt=0;
1042   for (i=0;i<14;i++)
1043     tt+=fabs(flags->sw[i+1])*t[i];
1044   return tt;
1045 }
1046
1047 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048
1049 void MSIS::gtd7(struct nrlmsise_input *input, struct nrlmsise_flags *flags,
1050                   struct nrlmsise_output *output)
1051 {
1052   double xlat;
1053   double xmm;
1054   int mn3 = 5;
1055   double zn3[5]={32.5,20.0,15.0,10.0,0.0};
1056   int mn2 = 4;
1057   double zn2[4]={72.5,55.0,45.0,32.5};
1058   double altt;
1059   double zmix=62.5;
1060   double tmp;
1061   double dm28m;
1062   double tz;
1063   double dmc;
1064   double dmr;
1065   double dz28;
1066   struct nrlmsise_output soutput;
1067   int i;
1068
1069   tselec(flags);
1070
1071   /* Latitude variation of gravity (none for sw[2]=0) */
1072   xlat=input->g_lat;
1073   if (flags->sw[2]==0)
1074     xlat=45.0;
1075   glatf(xlat, &gsurf, &re);
1076
1077   xmm = pdm[2][4];
1078
1079   /* THERMOSPHERE / MESOSPHERE (above zn2[0]) */
1080   if (input->alt>zn2[0])
1081     altt=input->alt;
1082   else
1083     altt=zn2[0];
1084
1085   tmp=input->alt;
1086   input->alt=altt;
1087   gts7(input, flags, &soutput);
1088   altt=input->alt;
1089   input->alt=tmp;
1090   if (flags->sw[0])   /* metric adjustment */
1091     dm28m=dm28*1.0E6;
1092   else
1093     dm28m=dm28;
1094   output->t[0]=soutput.t[0];
1095   output->t[1]=soutput.t[1];
1096   if (input->alt>=zn2[0]) {
1097     for (i=0;i<9;i++)
1098       output->d[i]=soutput.d[i];
1099     return;
1100   }
1101
1102 /*       LOWER MESOSPHERE/UPPER STRATOSPHERE (between zn3[0] and zn2[0])
1103  *         Temperature at nodes and gradients at end nodes
1104  *         Inverse temperature a linear function of spherical harmonics
1105  */
1106   meso_tgn2[0]=meso_tgn1[1];
1107   meso_tn2[0]=meso_tn1[4];
1108         meso_tn2[1]=pma[0][0]*pavgm[0]/(1.0-flags->sw[20]*glob7s(pma[0], input, flags));
1109         meso_tn2[2]=pma[1][0]*pavgm[1]/(1.0-flags->sw[20]*glob7s(pma[1], input, flags));
1110         meso_tn2[3]=pma[2][0]*pavgm[2]/(1.0-flags->sw[20]*flags->sw[22]*glob7s(pma[2], input, flags));
1111   meso_tgn2[1]=pavgm[8]*pma[9][0]*(1.0+flags->sw[20]*flags->sw[22]*glob7s(pma[9], input, flags))*meso_tn2[3]*meso_tn2[3]/(pow((pma[2][0]*pavgm[2]),2.0));
1112   meso_tn3[0]=meso_tn2[3];
1113
1114   if (input->alt<zn3[0]) {
1115 /*       LOWER STRATOSPHERE AND TROPOSPHERE (below zn3[0])
1116  *         Temperature at nodes and gradients at end nodes
1117  *         Inverse temperature a linear function of spherical harmonics
1118  */
1119     meso_tgn3[0]=meso_tgn2[1];
1120     meso_tn3[1]=pma[3][0]*pavgm[3]/(1.0-flags->sw[22]*glob7s(pma[3], input, flags));
1121     meso_tn3[2]=pma[4][0]*pavgm[4]/(1.0-flags->sw[22]*glob7s(pma[4], input, flags));
1122     meso_tn3[3]=pma[5][0]*pavgm[5]/(1.0-flags->sw[22]*glob7s(pma[5], input, flags));
1123     meso_tn3[4]=pma[6][0]*pavgm[6]/(1.0-flags->sw[22]*glob7s(pma[6], input, flags));
1124     meso_tgn3[1]=pma[7][0]*pavgm[7]*(1.0+flags->sw[22]*glob7s(pma[7], input, flags)) *meso_tn3[4]*meso_tn3[4]/(pow((pma[6][0]*pavgm[6]),2.0));
1125   }
1126
1127         /* LINEAR TRANSITION TO FULL MIXING BELOW zn2[0] */
1128
1129   dmc=0;
1130   if (input->alt>zmix)
1131     dmc = 1.0 - (zn2[0]-input->alt)/(zn2[0] - zmix);
1132   dz28=soutput.d[2];
1133
1134   /**** N2 density ****/
1135   dmr=soutput.d[2] / dm28m - 1.0;
1136   output->d[2]=densm(input->alt,dm28m,xmm, &tz, mn3, zn3, meso_tn3, meso_tgn3, mn2, zn2, meso_tn2, meso_tgn2);
1137   output->d[2]=output->d[2] * (1.0 + dmr*dmc);
1138
1139   /**** HE density ****/
1140   dmr = soutput.d[0] / (dz28 * pdm[0][1]) - 1.0;
1141   output->d[0] = output->d[2] * pdm[0][1] * (1.0 + dmr*dmc);
1142
1143   /**** O density ****/
1144   output->d[1] = 0;
1145   output->d[8] = 0;
1146
1147   /**** O2 density ****/
1148   dmr = soutput.d[3] / (dz28 * pdm[3][1]) - 1.0;
1149   output->d[3] = output->d[2] * pdm[3][1] * (1.0 + dmr*dmc);
1150
1151   /**** AR density ***/
1152   dmr = soutput.d[4] / (dz28 * pdm[4][1]) - 1.0;
1153   output->d[4] = output->d[2] * pdm[4][1] * (1.0 + dmr*dmc);
1154
1155   /**** Hydrogen density ****/
1156   output->d[6] = 0;
1157
1158   /**** Atomic nitrogen density ****/
1159   output->d[7] = 0;
1160
1161   /**** Total mass density */
1162   output->d[5] = 1.66E-24 * (4.0 * output->d[0] + 16.0 * output->d[1] +
1163                      28.0 * output->d[2] + 32.0 * output->d[3] + 40.0 * output->d[4]
1164                      + output->d[6] + 14.0 * output->d[7]);
1165
1166   if (flags->sw[0])
1167     output->d[5]=output->d[5]/1000;
1168
1169   /**** temperature at altitude ****/
1170   dd = densm(input->alt, 1.0, 0, &tz, mn3, zn3, meso_tn3, meso_tgn3,
1171                    mn2, zn2, meso_tn2, meso_tgn2);
1172   output->t[1]=tz;
1173
1174 }
1175
1176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177
1178 void MSIS::gtd7d(struct nrlmsise_input *input, struct nrlmsise_flags *flags,
1179                    struct nrlmsise_output *output)
1180 {
1181   gtd7(input, flags, output);
1182   output->d[5] = 1.66E-24 * (4.0 * output->d[0] + 16.0 * output->d[1] +
1183                    28.0 * output->d[2] + 32.0 * output->d[3] + 40.0 * output->d[4]
1184                    + output->d[6] + 14.0 * output->d[7] + 16.0 * output->d[8]);
1185 }
1186
1187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1188
1189 void MSIS::ghp7(struct nrlmsise_input *input, struct nrlmsise_flags *flags,
1190                   struct nrlmsise_output *output, double press)
1191 {
1192   double bm = 1.3806E-19;
1193   double rgas = 831.4;
1194   double test = 0.00043;
1195   double ltest = 12;
1196   double pl, p;
1197   double zi = 0.0;
1198   double z;
1199   double cl, cl2;
1200   double ca, cd;
1201   double xn, xm, diff;
1202   double g, sh;
1203   int l;
1204   pl = log10(press);
1205   if (pl >= -5.0) {
1206     if (pl>2.5)
1207       zi = 18.06 * (3.00 - pl);
1208     else if ((pl>0.075) && (pl<=2.5))
1209       zi = 14.98 * (3.08 - pl);
1210     else if ((pl>-1) && (pl<=0.075))
1211       zi = 17.80 * (2.72 - pl);
1212     else if ((pl>-2) && (pl<=-1))
1213       zi = 14.28 * (3.64 - pl);
1214     else if ((pl>-4) && (pl<=-2))
1215       zi = 12.72 * (4.32 -pl);
1216     else if (pl<=-4)
1217       zi = 25.3 * (0.11 - pl);
1218     cl = input->g_lat/90.0;
1219     cl2 = cl*cl;
1220     if (input->doy<182)
1221       cd = (1.0 - (double) input->doy) / 91.25;
1222     else
1223       cd = ((double) input->doy) / 91.25 - 3.0;
1224     ca = 0;
1225     if ((pl > -1.11) && (pl<=-0.23))
1226       ca = 1.0;
1227     if (pl > -0.23)
1228       ca = (2.79 - pl) / (2.79 + 0.23);
1229     if ((pl <= -1.11) && (pl>-3))
1230       ca = (-2.93 - pl)/(-2.93 + 1.11);
1231     z = zi - 4.87 * cl * cd * ca - 1.64 * cl2 * ca + 0.31 * ca * cl;
1232   } else
1233     z = 22.0 * pow((pl + 4.0),2.0) + 110.0;
1234
1235   /* iteration  loop */
1236   l = 0;
1237   do {
1238     l++;
1239     input->alt = z;
1240     gtd7(input, flags, output);
1241     z = input->alt;
1242     xn = output->d[0] + output->d[1] + output->d[2] + output->d[3] + output->d[4] + output->d[6] + output->d[7];
1243     p = bm * xn * output->t[1];
1244     if (flags->sw[0])
1245       p = p*1.0E-6;
1246     diff = pl - log10(p);
1247     if (sqrt(diff*diff)<test)
1248       return;
1249     if (l==ltest) {
1250       cerr << "ERROR: ghp7 not converging for press " << press << ", diff " << diff << endl;
1251       return;
1252     }
1253     xm = output->d[5] / xn / 1.66E-24;
1254     if (flags->sw[0])
1255       xm = xm * 1.0E3;
1256     g = gsurf / (pow((1.0 + z/re),2.0));
1257     sh = rgas * output->t[1] / (xm * g);
1258
1259     /* new altitude estimate using scale height */
1260     if (l <  6)
1261       z = z - sh * diff * 2.302;
1262     else
1263       z = z - sh * diff;
1264   } while (1==1);
1265 }
1266
1267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268
1269 void MSIS::gts7(struct nrlmsise_input *input, struct nrlmsise_flags *flags,
1270                   struct nrlmsise_output *output)
1271 {
1272 /*     Thermospheric portion of NRLMSISE-00
1273  *     See GTD7 for more extensive comments
1274  *     alt > 72.5 km!
1275  */
1276   double za;
1277   int i, j;
1278   double ddum, z;
1279   double zn1[5] = {120.0, 110.0, 100.0, 90.0, 72.5};
1280   double tinf;
1281   int mn1 = 5;
1282   double g0;
1283   double tlb;
1284   double s, z0, t0, tr12;
1285   double db01, db04, db14, db16, db28, db32, db40, db48;
1286   double zh28, zh04, zh16, zh32, zh40, zh01, zh14;
1287   double zhm28, zhm04, zhm16, zhm32, zhm40, zhm01, zhm14;
1288   double xmd;
1289   double b28, b04, b16, b32, b40, b01, b14;
1290   double tz;
1291   double g28, g4, g16, g32, g40, g1, g14;
1292   double zhf, xmm;
1293   double zc04, zc16, zc32, zc40, zc01, zc14;
1294   double hc04, hc16, hc32, hc40, hc01, hc14;
1295   double hcc16, hcc32, hcc01, hcc14;
1296   double zcc16, zcc32, zcc01, zcc14;
1297   double rc16, rc32, rc01, rc14;
1298   double rl;
1299   double g16h, db16h, tho, zsht, zmho, zsho;
1300   double dgtr=1.74533E-2;
1301   double dr=1.72142E-2;
1302   double alpha[9]={-0.38, 0.0, 0.0, 0.0, 0.17, 0.0, -0.38, 0.0, 0.0};
1303   double altl[8]={200.0, 300.0, 160.0, 250.0, 240.0, 450.0, 320.0, 450.0};
1304   double dd;
1305   double hc216, hcc232;
1306   za = pdl[1][15];
1307   zn1[0] = za;
1308   for (j=0;j<9;j++)
1309     output->d[j]=0;
1310
1311   /* TINF VARIATIONS NOT IMPORTANT BELOW ZA OR ZN1(1) */
1312   if (input->alt>zn1[0])
1313     tinf = ptm[0]*pt[0] * \
1314       (1.0+flags->sw[16]*globe7(pt,input,flags));
1315   else
1316     tinf = ptm[0]*pt[0];
1317   output->t[0]=tinf;
1318
1319   /*  GRADIENT VARIATIONS NOT IMPORTANT BELOW ZN1(5) */
1320   if (input->alt>zn1[4])
1321     g0 = ptm[3]*ps[0] * \
1322       (1.0+flags->sw[19]*globe7(ps,input,flags));
1323   else
1324     g0 = ptm[3]*ps[0];
1325   tlb = ptm[1] * (1.0 + flags->sw[17]*globe7(pd[3],input,flags))*pd[3][0];
1326   s = g0 / (tinf - tlb);
1327
1328 /*      Lower thermosphere temp variations not significant for
1329  *       density above 300 km */
1330   if (input->alt<300.0) {
1331     meso_tn1[1]=ptm[6]*ptl[0][0]/(1.0-flags->sw[18]*glob7s(ptl[0], input, flags));
1332     meso_tn1[2]=ptm[2]*ptl[1][0]/(1.0-flags->sw[18]*glob7s(ptl[1], input, flags));
1333     meso_tn1[3]=ptm[7]*ptl[2][0]/(1.0-flags->sw[18]*glob7s(ptl[2], input, flags));
1334     meso_tn1[4]=ptm[4]*ptl[3][0]/(1.0-flags->sw[18]*flags->sw[20]*glob7s(ptl[3], input, flags));
1335     meso_tgn1[1]=ptm[8]*pma[8][0]*(1.0+flags->sw[18]*flags->sw[20]*glob7s(pma[8], input, flags))*meso_tn1[4]*meso_tn1[4]/(pow((ptm[4]*ptl[3][0]),2.0));
1336   } else {
1337     meso_tn1[1]=ptm[6]*ptl[0][0];
1338     meso_tn1[2]=ptm[2]*ptl[1][0];
1339     meso_tn1[3]=ptm[7]*ptl[2][0];
1340     meso_tn1[4]=ptm[4]*ptl[3][0];
1341     meso_tgn1[1]=ptm[8]*pma[8][0]*meso_tn1[4]*meso_tn1[4]/(pow((ptm[4]*ptl[3][0]),2.0));
1342   }
1343
1344   z0 = zn1[3];
1345   t0 = meso_tn1[3];
1346   tr12 = 1.0;
1347
1348   /* N2 variation factor at Zlb */
1349   g28=flags->sw[21]*globe7(pd[2], input, flags);
1350
1351   /* VARIATION OF TURBOPAUSE HEIGHT */
1352   zhf=pdl[1][24]*(1.0+flags->sw[5]*pdl[0][24]*sin(dgtr*input->g_lat)*cos(dr*(input->doy-pt[13])));
1353   output->t[0]=tinf;
1354   xmm = pdm[2][4];
1355   z = input->alt;
1356
1357
1358         /**** N2 DENSITY ****/
1359
1360   /* Diffusive density at Zlb */
1361   db28 = pdm[2][0]*exp(g28)*pd[2][0];
1362   /* Diffusive density at Alt */
1363   output->d[2]=densu(z,db28,tinf,tlb,28.0,alpha[2],&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1364   dd=output->d[2];
1365   /* Turbopause */
1366   zh28=pdm[2][2]*zhf;
1367   zhm28=pdm[2][3]*pdl[1][5];
1368   xmd=28.0-xmm;
1369   /* Mixed density at Zlb */
1370   b28=densu(zh28,db28,tinf,tlb,xmd,(alpha[2]-1.0),&tz,ptm[5],s,mn1, zn1,meso_tn1,meso_tgn1);
1371   if ((flags->sw[15])&&(z<=altl[2])) {
1372     /*  Mixed density at Alt */
1373     dm28=densu(z,b28,tinf,tlb,xmm,alpha[2],&tz,ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1374     /*  Net density at Alt */
1375     output->d[2]=dnet(output->d[2],dm28,zhm28,xmm,28.0);
1376   }
1377
1378
1379         /**** HE DENSITY ****/
1380
1381   /*   Density variation factor at Zlb */
1382   g4 = flags->sw[21]*globe7(pd[0], input, flags);
1383   /*  Diffusive density at Zlb */
1384   db04 = pdm[0][0]*exp(g4)*pd[0][0];
1385         /*  Diffusive density at Alt */
1386   output->d[0]=densu(z,db04,tinf,tlb, 4.,alpha[0],&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1387   dd=output->d[0];
1388   if ((flags->sw[15]) && (z<altl[0])) {
1389     /*  Turbopause */
1390     zh04=pdm[0][2];
1391     /*  Mixed density at Zlb */
1392     b04=densu(zh04,db04,tinf,tlb,4.-xmm,alpha[0]-1.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1393     /*  Mixed density at Alt */
1394     dm04=densu(z,b04,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1395     zhm04=zhm28;
1396     /*  Net density at Alt */
1397     output->d[0]=dnet(output->d[0],dm04,zhm04,xmm,4.);
1398     /*  Correction to specified mixing ratio at ground */
1399     rl=log(b28*pdm[0][1]/b04);
1400     zc04=pdm[0][4]*pdl[1][0];
1401     hc04=pdm[0][5]*pdl[1][1];
1402     /*  Net density corrected at Alt */
1403     output->d[0]=output->d[0]*ccor(z,rl,hc04,zc04);
1404   }
1405
1406
1407         /**** O DENSITY ****/
1408
1409   /*  Density variation factor at Zlb */
1410   g16= flags->sw[21]*globe7(pd[1],input,flags);
1411   /*  Diffusive density at Zlb */
1412   db16 =  pdm[1][0]*exp(g16)*pd[1][0];
1413         /*   Diffusive density at Alt */
1414   output->d[1]=densu(z,db16,tinf,tlb, 16.,alpha[1],&output->t[1],ptm[5],s,mn1, zn1,meso_tn1,meso_tgn1);
1415   dd=output->d[1];
1416   if ((flags->sw[15]) && (z<=altl[1])) {
1417     /*   Turbopause */
1418     zh16=pdm[1][2];
1419     /*  Mixed density at Zlb */
1420     b16=densu(zh16,db16,tinf,tlb,16.0-xmm,(alpha[1]-1.0), &output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1421     /*  Mixed density at Alt */
1422     dm16=densu(z,b16,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1423     zhm16=zhm28;
1424     /*  Net density at Alt */
1425     output->d[1]=dnet(output->d[1],dm16,zhm16,xmm,16.);
1426     rl=pdm[1][1]*pdl[1][16]*(1.0+flags->sw[1]*pdl[0][23]*(input->f107A-150.0));
1427     hc16=pdm[1][5]*pdl[1][3];
1428     zc16=pdm[1][4]*pdl[1][2];
1429     hc216=pdm[1][5]*pdl[1][4];
1430     output->d[1]=output->d[1]*ccor2(z,rl,hc16,zc16,hc216);
1431     /*   Chemistry correction */
1432     hcc16=pdm[1][7]*pdl[1][13];
1433     zcc16=pdm[1][6]*pdl[1][12];
1434     rc16=pdm[1][3]*pdl[1][14];
1435     /*  Net density corrected at Alt */
1436     output->d[1]=output->d[1]*ccor(z,rc16,hcc16,zcc16);
1437   }
1438
1439
1440         /**** O2 DENSITY ****/
1441
1442         /*   Density variation factor at Zlb */
1443   g32= flags->sw[21]*globe7(pd[4], input, flags);
1444         /*  Diffusive density at Zlb */
1445   db32 = pdm[3][0]*exp(g32)*pd[4][0];
1446         /*   Diffusive density at Alt */
1447   output->d[3]=densu(z,db32,tinf,tlb, 32.,alpha[3],&output->t[1],ptm[5],s,mn1, zn1,meso_tn1,meso_tgn1);
1448   dd=output->d[3];
1449   if (flags->sw[15]) {
1450     if (z<=altl[3]) {
1451       /*   Turbopause */
1452       zh32=pdm[3][2];
1453       /*  Mixed density at Zlb */
1454       b32=densu(zh32,db32,tinf,tlb,32.-xmm,alpha[3]-1., &output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1455       /*  Mixed density at Alt */
1456       dm32=densu(z,b32,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1457       zhm32=zhm28;
1458       /*  Net density at Alt */
1459       output->d[3]=dnet(output->d[3],dm32,zhm32,xmm,32.);
1460       /*   Correction to specified mixing ratio at ground */
1461       rl=log(b28*pdm[3][1]/b32);
1462       hc32=pdm[3][5]*pdl[1][7];
1463       zc32=pdm[3][4]*pdl[1][6];
1464       output->d[3]=output->d[3]*ccor(z,rl,hc32,zc32);
1465     }
1466     /*  Correction for general departure from diffusive equilibrium above Zlb */
1467     hcc32=pdm[3][7]*pdl[1][22];
1468     hcc232=pdm[3][7]*pdl[0][22];
1469     zcc32=pdm[3][6]*pdl[1][21];
1470     rc32=pdm[3][3]*pdl[1][23]*(1.+flags->sw[1]*pdl[0][23]*(input->f107A-150.));
1471     /*  Net density corrected at Alt */
1472     output->d[3]=output->d[3]*ccor2(z,rc32,hcc32,zcc32,hcc232);
1473   }
1474
1475
1476         /**** AR DENSITY ****/
1477
1478         /*   Density variation factor at Zlb */
1479   g40= flags->sw[20]*globe7(pd[5],input,flags);
1480         /*  Diffusive density at Zlb */
1481   db40 = pdm[4][0]*exp(g40)*pd[5][0];
1482   /*   Diffusive density at Alt */
1483   output->d[4]=densu(z,db40,tinf,tlb, 40.,alpha[4],&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1484   dd=output->d[4];
1485   if ((flags->sw[15]) && (z<=altl[4])) {
1486     /*   Turbopause */
1487     zh40=pdm[4][2];
1488     /*  Mixed density at Zlb */
1489     b40=densu(zh40,db40,tinf,tlb,40.-xmm,alpha[4]-1.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1490     /*  Mixed density at Alt */
1491     dm40=densu(z,b40,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1492     zhm40=zhm28;
1493     /*  Net density at Alt */
1494     output->d[4]=dnet(output->d[4],dm40,zhm40,xmm,40.);
1495     /*   Correction to specified mixing ratio at ground */
1496     rl=log(b28*pdm[4][1]/b40);
1497     hc40=pdm[4][5]*pdl[1][9];
1498     zc40=pdm[4][4]*pdl[1][8];
1499     /*  Net density corrected at Alt */
1500     output->d[4]=output->d[4]*ccor(z,rl,hc40,zc40);
1501     }
1502
1503
1504         /**** HYDROGEN DENSITY ****/
1505
1506         /*   Density variation factor at Zlb */
1507   g1 = flags->sw[21]*globe7(pd[6], input, flags);
1508         /*  Diffusive density at Zlb */
1509   db01 = pdm[5][0]*exp(g1)*pd[6][0];
1510         /*   Diffusive density at Alt */
1511   output->d[6]=densu(z,db01,tinf,tlb,1.,alpha[6],&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1512   dd=output->d[6];
1513   if ((flags->sw[15]) && (z<=altl[6])) {
1514     /*   Turbopause */
1515     zh01=pdm[5][2];
1516     /*  Mixed density at Zlb */
1517     b01=densu(zh01,db01,tinf,tlb,1.-xmm,alpha[6]-1., &output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1518     /*  Mixed density at Alt */
1519     dm01=densu(z,b01,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1520     zhm01=zhm28;
1521     /*  Net density at Alt */
1522     output->d[6]=dnet(output->d[6],dm01,zhm01,xmm,1.);
1523     /*   Correction to specified mixing ratio at ground */
1524     rl=log(b28*pdm[5][1]*sqrt(pdl[1][17]*pdl[1][17])/b01);
1525     hc01=pdm[5][5]*pdl[1][11];
1526     zc01=pdm[5][4]*pdl[1][10];
1527     output->d[6]=output->d[6]*ccor(z,rl,hc01,zc01);
1528     /*   Chemistry correction */
1529     hcc01=pdm[5][7]*pdl[1][19];
1530     zcc01=pdm[5][6]*pdl[1][18];
1531     rc01=pdm[5][3]*pdl[1][20];
1532     /*  Net density corrected at Alt */
1533     output->d[6]=output->d[6]*ccor(z,rc01,hcc01,zcc01);
1534 }
1535
1536
1537         /**** ATOMIC NITROGEN DENSITY ****/
1538
1539   /*   Density variation factor at Zlb */
1540   g14 = flags->sw[21]*globe7(pd[7],input,flags);
1541         /*  Diffusive density at Zlb */
1542   db14 = pdm[6][0]*exp(g14)*pd[7][0];
1543         /*   Diffusive density at Alt */
1544   output->d[7]=densu(z,db14,tinf,tlb,14.,alpha[7],&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1545   dd=output->d[7];
1546   if ((flags->sw[15]) && (z<=altl[7])) {
1547     /*   Turbopause */
1548     zh14=pdm[6][2];
1549     /*  Mixed density at Zlb */
1550     b14=densu(zh14,db14,tinf,tlb,14.-xmm,alpha[7]-1., &output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1551     /*  Mixed density at Alt */
1552     dm14=densu(z,b14,tinf,tlb,xmm,0.,&output->t[1],ptm[5],s,mn1,zn1,meso_tn1,meso_tgn1);
1553     zhm14=zhm28;
1554     /*  Net density at Alt */
1555     output->d[7]=dnet(output->d[7],dm14,zhm14,xmm,14.);
1556     /*   Correction to specified mixing ratio at ground */
1557     rl=log(b28*pdm[6][1]*sqrt(pdl[0][2]*pdl[0][2])/b14);
1558     hc14=pdm[6][5]*pdl[0][1];
1559     zc14=pdm[6][4]*pdl[0][0];
1560     output->d[7]=output->d[7]*ccor(z,rl,hc14,zc14);
1561     /*   Chemistry correction */
1562     hcc14=pdm[6][7]*pdl[0][4];
1563     zcc14=pdm[6][6]*pdl[0][3];
1564     rc14=pdm[6][3]*pdl[0][5];
1565     /*  Net density corrected at Alt */
1566     output->d[7]=output->d[7]*ccor(z,rc14,hcc14,zcc14);
1567   }
1568
1569
1570         /**** Anomalous OXYGEN DENSITY ****/
1571
1572   g16h = flags->sw[21]*globe7(pd[8],input,flags);
1573   db16h = pdm[7][0]*exp(g16h)*pd[8][0];
1574   tho = pdm[7][9]*pdl[0][6];
1575   dd=densu(z,db16h,tho,tho,16.,alpha[8],&output->t[1],ptm[5],s,mn1, zn1,meso_tn1,meso_tgn1);
1576   zsht=pdm[7][5];
1577   zmho=pdm[7][4];
1578   zsho=scalh(zmho,16.0,tho);
1579   output->d[8]=dd*exp(-zsht/zsho*(exp(-(z-zmho)/zsht)-1.));
1580
1581
1582   /* total mass density */
1583   output->d[5] = 1.66E-24*(4.0*output->d[0]+16.0*output->d[1]+28.0*output->d[2]+32.0*output->d[3]+40.0*output->d[4]+ output->d[6]+14.0*output->d[7]);
1584   db48=1.66E-24*(4.0*db04+16.0*db16+28.0*db28+32.0*db32+40.0*db40+db01+14.0*db14);
1585
1586
1587
1588   /* temperature */
1589   z = sqrt(input->alt*input->alt);
1590   ddum = densu(z,1.0, tinf, tlb, 0.0, 0.0, &output->t[1], ptm[5], s, mn1, zn1, meso_tn1, meso_tgn1);
1591   if (flags->sw[0]) {
1592     for(i=0;i<9;i++)
1593       output->d[i]=output->d[i]*1.0E6;
1594     output->d[5]=output->d[5]/1000;
1595   }
1596 }
1597
1598
1599 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 //    The bitmasked value choices are as follows:
1601 //    unset: In this case (the default) JSBSim would only print
1602 //       out the normally expected messages, essentially echoing
1603 //       the config files as they are read. If the environment
1604 //       variable is not set, debug_lvl is set to 1 internally
1605 //    0: This requests JSBSim not to output any messages
1606 //       whatsoever.
1607 //    1: This value explicity requests the normal JSBSim
1608 //       startup messages
1609 //    2: This value asks for a message to be printed out when
1610 //       a class is instantiated
1611 //    4: When this value is set, a message is displayed when a
1612 //       FGModel object executes its Run() method
1613 //    8: When this value is set, various runtime state variables
1614 //       are printed out periodically
1615 //    16: When set various parameters are sanity checked and
1616 //       a message is printed out when they go out of bounds
1617
1618 void MSIS::Debug(int from)
1619 {
1620   if (debug_lvl <= 0) return;
1621
1622   if (debug_lvl & 1) { // Standard console startup message output
1623     if (from == 0) { // Constructor
1624     }
1625   }
1626   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1627     if (from == 0) cout << "Instantiated: MSIS" << endl;
1628     if (from == 1) cout << "Destroyed:    MSIS" << endl;
1629   }
1630   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1631   }
1632   if (debug_lvl & 8 ) { // Runtime state variables
1633   }
1634   if (debug_lvl & 16) { // Sanity checking
1635   }
1636   if (debug_lvl & 32) { // Turbulence
1637     if (first_pass && from == 2) {
1638       cout << "vTurbulenceNED(X), vTurbulenceNED(Y), vTurbulenceNED(Z), "
1639            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
1640            << "vDirection(X), vDirection(Y), vDirection(Z), "
1641            << "Magnitude, "
1642            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
1643     }
1644     if (from == 2) {
1645       cout << vTurbulenceNED << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
1646     }
1647   }
1648   if (debug_lvl & 64) {
1649     if (from == 0) { // Constructor
1650       cout << IdSrc << endl;
1651       cout << IdHdr << endl;
1652     }
1653   }
1654 }
1655
1656
1657
1658 } // namespace JSBSim
1659