HarrisPriester.java

  1. /* Copyright 2002-2023 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.models.earth.atmosphere;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.Precision;
  23. import org.hipparchus.util.SinCos;
  24. import org.orekit.bodies.OneAxisEllipsoid;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.FieldAbsoluteDate;
  30. import org.orekit.utils.PVCoordinatesProvider;


  31. /** This atmosphere model is the realization of the Modified Harris-Priester model.
  32.  * <p>
  33.  * This model is a static one that takes into account the diurnal density bulge.
  34.  * It doesn't need any space weather data but a density vs. altitude table, which
  35.  * depends on solar activity.
  36.  * </p>
  37.  * <p>
  38.  * The implementation relies on the book:<br>
  39.  * <b>Satellite Orbits</b><br>
  40.  * <i>Oliver Montenbruck, Eberhard Gill</i><br>
  41.  * Springer 2005
  42.  * </p>
  43.  * @author Pascal Parraud
  44.  */
  45. public class HarrisPriester implements Atmosphere {

  46.     /** Serializable UID.*/
  47.     private static final long serialVersionUID = 2772347498196369601L;

  48.     // Constants :

  49.     /** Default cosine exponent value. */
  50.     private static final int N_DEFAULT = 4;

  51.     /** Minimal value for calculating poxer of cosine. */
  52.     private static final double MIN_COS = 1.e-12;

  53.     /** Lag angle for diurnal bulge. */
  54.     private static final double LAG = FastMath.toRadians(30.0);

  55.     /** Lag angle sine and cosine. */
  56.     private static final SinCos SCLAG = FastMath.sinCos(LAG);

  57.     // CHECKSTYLE: stop NoWhitespaceAfter check
  58.     /** Harris-Priester min-max density (kg/m3) vs. altitude (m) table.
  59.      *  These data are valid for a mean solar activity. */
  60.     private static final double[][] ALT_RHO = {
  61.         {  100000.0, 4.974e-07, 4.974e-07 },
  62.         {  120000.0, 2.490e-08, 2.490e-08 },
  63.         {  130000.0, 8.377e-09, 8.710e-09 },
  64.         {  140000.0, 3.899e-09, 4.059e-09 },
  65.         {  150000.0, 2.122e-09, 2.215e-09 },
  66.         {  160000.0, 1.263e-09, 1.344e-09 },
  67.         {  170000.0, 8.008e-10, 8.758e-10 },
  68.         {  180000.0, 5.283e-10, 6.010e-10 },
  69.         {  190000.0, 3.617e-10, 4.297e-10 },
  70.         {  200000.0, 2.557e-10, 3.162e-10 },
  71.         {  210000.0, 1.839e-10, 2.396e-10 },
  72.         {  220000.0, 1.341e-10, 1.853e-10 },
  73.         {  230000.0, 9.949e-11, 1.455e-10 },
  74.         {  240000.0, 7.488e-11, 1.157e-10 },
  75.         {  250000.0, 5.709e-11, 9.308e-11 },
  76.         {  260000.0, 4.403e-11, 7.555e-11 },
  77.         {  270000.0, 3.430e-11, 6.182e-11 },
  78.         {  280000.0, 2.697e-11, 5.095e-11 },
  79.         {  290000.0, 2.139e-11, 4.226e-11 },
  80.         {  300000.0, 1.708e-11, 3.526e-11 },
  81.         {  320000.0, 1.099e-11, 2.511e-11 },
  82.         {  340000.0, 7.214e-12, 1.819e-11 },
  83.         {  360000.0, 4.824e-12, 1.337e-11 },
  84.         {  380000.0, 3.274e-12, 9.955e-12 },
  85.         {  400000.0, 2.249e-12, 7.492e-12 },
  86.         {  420000.0, 1.558e-12, 5.684e-12 },
  87.         {  440000.0, 1.091e-12, 4.355e-12 },
  88.         {  460000.0, 7.701e-13, 3.362e-12 },
  89.         {  480000.0, 5.474e-13, 2.612e-12 },
  90.         {  500000.0, 3.916e-13, 2.042e-12 },
  91.         {  520000.0, 2.819e-13, 1.605e-12 },
  92.         {  540000.0, 2.042e-13, 1.267e-12 },
  93.         {  560000.0, 1.488e-13, 1.005e-12 },
  94.         {  580000.0, 1.092e-13, 7.997e-13 },
  95.         {  600000.0, 8.070e-14, 6.390e-13 },
  96.         {  620000.0, 6.012e-14, 5.123e-13 },
  97.         {  640000.0, 4.519e-14, 4.121e-13 },
  98.         {  660000.0, 3.430e-14, 3.325e-13 },
  99.         {  680000.0, 2.632e-14, 2.691e-13 },
  100.         {  700000.0, 2.043e-14, 2.185e-13 },
  101.         {  720000.0, 1.607e-14, 1.779e-13 },
  102.         {  740000.0, 1.281e-14, 1.452e-13 },
  103.         {  760000.0, 1.036e-14, 1.190e-13 },
  104.         {  780000.0, 8.496e-15, 9.776e-14 },
  105.         {  800000.0, 7.069e-15, 8.059e-14 },
  106.         {  840000.0, 4.680e-15, 5.741e-14 },
  107.         {  880000.0, 3.200e-15, 4.210e-14 },
  108.         {  920000.0, 2.210e-15, 3.130e-14 },
  109.         {  960000.0, 1.560e-15, 2.360e-14 },
  110.         { 1000000.0, 1.150e-15, 1.810e-14 }
  111.     };
  112.     // CHECKSTYLE: resume NoWhitespaceAfter check

  113.     /** Cosine exponent from 2 to 6 according to inclination. */
  114.     private double n;

  115.     /** Sun position. */
  116.     private PVCoordinatesProvider sun;

  117.     /** Earth body shape. */
  118.     private OneAxisEllipsoid earth;

  119.     /** Density table. */
  120.     private double[][] tabAltRho;

  121.     /** Simple constructor for Modified Harris-Priester atmosphere model.
  122.      *  <p>The cosine exponent value is set to 4 by default.</p>
  123.      *  <p>The default embedded density table is the one given in the referenced
  124.      *  book from Montenbruck &amp; Gill. It is given for mean solar activity and
  125.      *  spreads over 100 to 1000 km.</p>
  126.      * @param sun the sun position
  127.      * @param earth the earth body shape
  128.      */
  129.     public HarrisPriester(final PVCoordinatesProvider sun,
  130.                           final OneAxisEllipsoid earth) {
  131.         this(sun, earth, ALT_RHO, N_DEFAULT);
  132.     }

  133.     /** Constructor for Modified Harris-Priester atmosphere model.
  134.      *  <p>Recommanded values for the cosine exponent spread over the range
  135.      *  2, for low inclination orbits, to 6, for polar orbits.</p>
  136.      *  <p> The default embedded density table is the one given in the referenced
  137.      *  book from Montenbruck &amp; Gill. It is given for mean solar activity and
  138.      *  spreads over 100 to 1000 km. </p>
  139.      *  @param sun the sun position
  140.      * @param earth the earth body shape
  141.      * @param n the cosine exponent
  142.      */
  143.     public HarrisPriester(final PVCoordinatesProvider sun,
  144.                           final OneAxisEllipsoid earth,
  145.                           final double n) {
  146.         this(sun, earth, ALT_RHO, n);
  147.     }

  148.     /** Constructor for Modified Harris-Priester atmosphere model.
  149.      *  <p>The provided density table must be an array such as:
  150.      *  <ul>
  151.      *   <li>tabAltRho[][0] = altitude (m)</li>
  152.      *   <li>tabAltRho[][1] = min density (kg/m³)</li>
  153.      *   <li>tabAltRho[][2] = max density (kg/m³)</li>
  154.      *  </ul>
  155.      *  <p> The altitude must be increasing without limitation in range. The
  156.      *  internal density table is a copy of the provided one.
  157.      *
  158.      *  <p>The cosine exponent value is set to 4 by default.</p>
  159.      * @param sun the sun position
  160.      * @param earth the earth body shape
  161.      * @param tabAltRho the density table
  162.      */
  163.     public HarrisPriester(final PVCoordinatesProvider sun,
  164.                           final OneAxisEllipsoid earth,
  165.                           final double[][] tabAltRho) {
  166.         this(sun, earth, tabAltRho, N_DEFAULT);
  167.     }

  168.     /** Constructor for Modified Harris-Priester atmosphere model.
  169.      *  <p>Recommanded values for the cosine exponent spread over the range
  170.      *  2, for low inclination orbits, to 6, for polar orbits.</p>
  171.      *  <p>The provided density table must be an array such as:
  172.      *  <ul>
  173.      *   <li>tabAltRho[][0] = altitude (m)</li>
  174.      *   <li>tabAltRho[][1] = min density (kg/m³)</li>
  175.      *   <li>tabAltRho[][2] = max density (kg/m³)</li>
  176.      *  </ul>
  177.      *  <p> The altitude must be increasing without limitation in range. The
  178.      *  internal density table is a copy of the provided one.
  179.      *
  180.      *  @param sun the sun position
  181.      * @param earth the earth body shape
  182.      * @param tabAltRho the density table
  183.      * @param n the cosine exponent
  184.      */
  185.     public HarrisPriester(final PVCoordinatesProvider sun,
  186.                           final OneAxisEllipsoid earth,
  187.                           final double[][] tabAltRho,
  188.                           final double n) {
  189.         this.sun   = sun;
  190.         this.earth = earth;
  191.         setTabDensity(tabAltRho);
  192.         setN(n);
  193.     }

  194.     /** {@inheritDoc} */
  195.     public Frame getFrame() {
  196.         return earth.getBodyFrame();
  197.     }

  198.     /** Set parameter N, the cosine exponent.
  199.      *  @param n the cosine exponent
  200.      */
  201.     private void setN(final double n) {
  202.         this.n = n;
  203.     }

  204.     /** Set a user define density table to deal with different solar activities.
  205.      *  @param tab density vs. altitude table
  206.      */
  207.     private void setTabDensity(final double[][] tab) {
  208.         this.tabAltRho = new double[tab.length][];
  209.         for (int i = 0; i < tab.length; i++) {
  210.             this.tabAltRho[i] = tab[i].clone();
  211.         }
  212.     }

  213.     /** Get the current density table.
  214.      *  <p>The density table is an array such as:
  215.      *  <ul>
  216.      *   <li>tabAltRho[][0] = altitude (m)</li>
  217.      *   <li>tabAltRho[][1] = min density (kg/m³)</li>
  218.      *   <li>tabAltRho[][2] = max density (kg/m³)</li>
  219.      *  </ul>
  220.      *  <p> The altitude must be increasing without limitation in range.
  221.      *
  222.      *  <p>
  223.      *  The returned density table is a copy of the current one.
  224.      *  </p>
  225.      *  @return density vs. altitude table
  226.      */
  227.     public double[][] getTabDensity() {
  228.         final double[][] copy = new double[tabAltRho.length][];
  229.         for (int i = 0; i < tabAltRho.length; i++) {
  230.             copy[i] = tabAltRho[i].clone();
  231.         }
  232.         return copy;
  233.     }

  234.     /** Get the minimal altitude for the model.
  235.      * <p>No computation is possible below this altitude.</p>
  236.      *  @return the minimal altitude (m)
  237.      */
  238.     public double getMinAlt() {
  239.         return tabAltRho[0][0];
  240.     }

  241.     /** Get the maximal altitude for the model.
  242.      * <p>Above this altitude, density is assumed to be zero.</p>
  243.      *  @return the maximal altitude (m)
  244.      */
  245.     public double getMaxAlt() {
  246.         return tabAltRho[tabAltRho.length - 1][0];
  247.     }

  248.     /** Get the local density.
  249.      * @param sunInEarth position of the Sun in Earth frame (m)
  250.      * @param posInEarth target position in Earth frame (m)
  251.      * @return the local density (kg/m³)
  252.      */
  253.     public double getDensity(final Vector3D sunInEarth, final Vector3D posInEarth) {

  254.         final double posAlt = getHeight(posInEarth);
  255.         // Check for height boundaries
  256.         if (posAlt < getMinAlt()) {
  257.             throw new OrekitException(OrekitMessages.ALTITUDE_BELOW_ALLOWED_THRESHOLD, posAlt, getMinAlt());
  258.         }
  259.         if (posAlt > getMaxAlt()) {
  260.             return 0.;
  261.         }

  262.         // Diurnal bulge apex direction
  263.         final Vector3D sunDir = sunInEarth.normalize();
  264.         final Vector3D bulDir = new Vector3D(sunDir.getX() * SCLAG.cos() - sunDir.getY() * SCLAG.sin(),
  265.                                              sunDir.getX() * SCLAG.sin() + sunDir.getY() * SCLAG.cos(),
  266.                                              sunDir.getZ());

  267.         // Cosine of angle Psi between the diurnal bulge apex and the satellite
  268.         final double cosPsi = bulDir.normalize().dotProduct(posInEarth.normalize());
  269.         // (1 + cos(Psi))/2 = cos²(Psi/2)
  270.         final double c2Psi2 = (1. + cosPsi) / 2.;
  271.         final double cPsi2  = FastMath.sqrt(c2Psi2);
  272.         final double cosPow = (cPsi2 > MIN_COS) ? c2Psi2 * FastMath.pow(cPsi2, n - 2) : 0.;

  273.         // Search altitude index in density table
  274.         int ia = 0;
  275.         while (ia < tabAltRho.length - 2 && posAlt > tabAltRho[ia + 1][0]) {
  276.             ia++;
  277.         }

  278.         // Fractional satellite height
  279.         final double dH = (tabAltRho[ia][0] - posAlt) / (tabAltRho[ia][0] - tabAltRho[ia + 1][0]);

  280.         // Min exponential density interpolation
  281.         final double rhoMin = tabAltRho[ia][1] * FastMath.pow(tabAltRho[ia + 1][1] / tabAltRho[ia][1], dH);

  282.         if (Precision.equals(cosPow, 0.)) {
  283.             return rhoMin;
  284.         } else {
  285.             // Max exponential density interpolation
  286.             final double rhoMax = tabAltRho[ia][2] * FastMath.pow(tabAltRho[ia + 1][2] / tabAltRho[ia][2], dH);
  287.             return rhoMin + (rhoMax - rhoMin) * cosPow;
  288.         }

  289.     }

  290.     /** Get the local density.
  291.      * @param sunInEarth position of the Sun in Earth frame (m)
  292.      * @param posInEarth target position in Earth frame (m)
  293.      * @return the local density (kg/m³)
  294.      * @param <T> instance of CalculusFieldElement&lt;T&gt;
  295.      */
  296.     public <T extends CalculusFieldElement<T>> T getDensity(final Vector3D sunInEarth, final FieldVector3D<T> posInEarth) {
  297.         final T zero = posInEarth.getX().getField().getZero();
  298.         final T posAlt = getHeight(posInEarth);
  299.         // Check for height boundaries
  300.         if (posAlt.getReal() < getMinAlt()) {
  301.             throw new OrekitException(OrekitMessages.ALTITUDE_BELOW_ALLOWED_THRESHOLD, posAlt, getMinAlt());
  302.         }
  303.         if (posAlt.getReal() > getMaxAlt()) {
  304.             return zero;
  305.         }

  306.         // Diurnal bulge apex direction
  307.         final Vector3D sunDir = sunInEarth.normalize();
  308.         final Vector3D bulDir = new Vector3D(sunDir.getX() * SCLAG.cos() - sunDir.getY() * SCLAG.sin(),
  309.                                              sunDir.getX() * SCLAG.sin() + sunDir.getY() * SCLAG.cos(),
  310.                                              sunDir.getZ());

  311.         // Cosine of angle Psi between the diurnal bulge apex and the satellite
  312.         final T cosPsi = posInEarth.normalize().dotProduct(bulDir.normalize());
  313.         // (1 + cos(Psi))/2 = cos²(Psi/2)
  314.         final T c2Psi2 = cosPsi.add(1.).divide(2);
  315.         final T cPsi2  = c2Psi2.sqrt();
  316.         final T cosPow = (cPsi2.getReal() > MIN_COS) ? c2Psi2.multiply(cPsi2.pow(n - 2)) : zero;

  317.         // Search altitude index in density table
  318.         int ia = 0;
  319.         while (ia < tabAltRho.length - 2 && posAlt.getReal() > tabAltRho[ia + 1][0]) {
  320.             ia++;
  321.         }

  322.         // Fractional satellite height
  323.         final T dH = posAlt.negate().add(tabAltRho[ia][0]).divide(tabAltRho[ia][0] - tabAltRho[ia + 1][0]);

  324.         // Min exponential density interpolation
  325.         final T rhoMin = zero.add(tabAltRho[ia + 1][1] / tabAltRho[ia][1]).pow(dH).multiply(tabAltRho[ia][1]);

  326.         if (Precision.equals(cosPow.getReal(), 0.)) {
  327.             return zero.add(rhoMin);
  328.         } else {
  329.             // Max exponential density interpolation
  330.             final T rhoMax = zero.add(tabAltRho[ia + 1][2] / tabAltRho[ia][2]).pow(dH).multiply(tabAltRho[ia][2]);
  331.             return rhoMin.add(rhoMax.subtract(rhoMin).multiply(cosPow));
  332.         }

  333.     }

  334.     /** Get the local density at some position.
  335.      * @param date current date
  336.      * @param position current position
  337.      * @param frame the frame in which is defined the position
  338.      * @return local density (kg/m³)
  339.           *            or if altitude is below the model minimal altitude
  340.      */
  341.     public double getDensity(final AbsoluteDate date, final Vector3D position, final Frame frame) {

  342.         // Sun position in earth frame
  343.         final Vector3D sunInEarth = sun.getPosition(date, earth.getBodyFrame());

  344.         // Target position in earth frame
  345.         final Vector3D posInEarth = frame
  346.                 .getStaticTransformTo(earth.getBodyFrame(), date)
  347.                 .transformPosition(position);

  348.         return getDensity(sunInEarth, posInEarth);
  349.     }

  350.     /** Get the local density at some position.
  351.      * @param date current date
  352.      * @param position current position
  353.      * @param <T> implements a CalculusFieldElement
  354.      * @param frame the frame in which is defined the position
  355.      * @return local density (kg/m³)
  356.           *            or if altitude is below the model minimal altitude
  357.      */
  358.     public <T extends CalculusFieldElement<T>> T getDensity(final FieldAbsoluteDate<T> date,
  359.                                                         final FieldVector3D<T> position,
  360.                                                         final Frame frame) {
  361.         // Sun position in earth frame
  362.         final Vector3D sunInEarth = sun.getPosition(date.toAbsoluteDate(), earth.getBodyFrame());

  363.         // Target position in earth frame
  364.         final FieldVector3D<T> posInEarth = frame
  365.                 .getStaticTransformTo(earth.getBodyFrame(), date.toAbsoluteDate())
  366.                 .transformPosition(position);

  367.         return getDensity(sunInEarth, posInEarth);
  368.     }

  369.     /** Get the height above the Earth for the given position.
  370.      *  <p>
  371.      *  The height computation is an approximation valid for the considered atmosphere.
  372.      *  </p>
  373.      *  @param position current position in Earth frame
  374.      *  @return height (m)
  375.      */
  376.     private double getHeight(final Vector3D position) {
  377.         final double a    = earth.getEquatorialRadius();
  378.         final double f    = earth.getFlattening();
  379.         final double e2   = f * (2. - f);
  380.         final double r    = position.getNorm();
  381.         final double sl   = position.getZ() / r;
  382.         final double cl2  = 1. - sl * sl;
  383.         final double coef = FastMath.sqrt((1. - e2) / (1. - e2 * cl2));

  384.         return r - a * coef;
  385.     }

  386.     /** Get the height above the Earth for the given position.
  387.      *  <p>
  388.      *  The height computation is an approximation valid for the considered atmosphere.
  389.      *  </p>
  390.      *  @param position current position in Earth frame
  391.      *  @param <T> instance of CalculusFieldElement<T>
  392.      *  @return height (m)
  393.      */
  394.     private <T extends CalculusFieldElement<T>> T getHeight(final FieldVector3D<T> position) {
  395.         final double a    = earth.getEquatorialRadius();
  396.         final double f    = earth.getFlattening();
  397.         final double e2   = f * (2. - f);
  398.         final T r    = position.getNorm();
  399.         final T sl   = position.getZ().divide(r);
  400.         final T cl2  = sl.multiply(sl).negate().add(1.);
  401.         final T coef = cl2.multiply(-e2).add(1.).reciprocal().multiply(1. - e2).sqrt();

  402.         return r.subtract(coef.multiply(a));
  403.     }

  404. }