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