MultiLayerModel.java

  1. /* Copyright 2013-2022 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.rugged.refraction;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  22. import org.hipparchus.util.FastMath;
  23. import org.orekit.bodies.GeodeticPoint;
  24. import org.orekit.rugged.errors.RuggedException;
  25. import org.orekit.rugged.errors.RuggedMessages;
  26. import org.orekit.rugged.intersection.IntersectionAlgorithm;
  27. import org.orekit.rugged.utils.ExtendedEllipsoid;
  28. import org.orekit.rugged.utils.NormalizedGeodeticPoint;

  29. /**
  30.  * Atmospheric refraction model based on multiple layers with associated refractive index.
  31.  * @author Sergio Esteves
  32.  * @author Luc Maisonobe
  33.  * @author Guylaine Prat
  34.  * @since 2.0
  35.  */
  36. public class MultiLayerModel extends AtmosphericRefraction {

  37.     /** Observed body ellipsoid. */
  38.     private final ExtendedEllipsoid ellipsoid;

  39.     /** Constant refraction layers. */
  40.     private final List<ConstantRefractionLayer> refractionLayers;

  41.     /** Atmosphere lowest altitude (m). */
  42.     private final double atmosphereLowestAltitude;

  43.     /** Simple constructor.
  44.      * <p>
  45.      * This model uses a built-in set of layers.
  46.      * </p>
  47.      * @param ellipsoid the ellipsoid to be used.
  48.      */
  49.     public MultiLayerModel(final ExtendedEllipsoid ellipsoid) {

  50.         super();

  51.         this.ellipsoid = ellipsoid;

  52.         this.refractionLayers = new ArrayList<>(15);
  53.         this.refractionLayers.add(new ConstantRefractionLayer(100000.00, 1.000000));
  54.         this.refractionLayers.add(new ConstantRefractionLayer( 50000.00, 1.000000));
  55.         this.refractionLayers.add(new ConstantRefractionLayer( 40000.00, 1.000001));
  56.         this.refractionLayers.add(new ConstantRefractionLayer( 30000.00, 1.000004));
  57.         this.refractionLayers.add(new ConstantRefractionLayer( 23000.00, 1.000012));
  58.         this.refractionLayers.add(new ConstantRefractionLayer( 18000.00, 1.000028));
  59.         this.refractionLayers.add(new ConstantRefractionLayer( 14000.00, 1.000052));
  60.         this.refractionLayers.add(new ConstantRefractionLayer( 11000.00, 1.000083));
  61.         this.refractionLayers.add(new ConstantRefractionLayer(  9000.00, 1.000106));
  62.         this.refractionLayers.add(new ConstantRefractionLayer(  7000.00, 1.000134));
  63.         this.refractionLayers.add(new ConstantRefractionLayer(  5000.00, 1.000167));
  64.         this.refractionLayers.add(new ConstantRefractionLayer(  3000.00, 1.000206));
  65.         this.refractionLayers.add(new ConstantRefractionLayer(  1000.00, 1.000252));
  66.         this.refractionLayers.add(new ConstantRefractionLayer(     0.00, 1.000278));
  67.         this.refractionLayers.add(new ConstantRefractionLayer( -1000.00, 1.000306));

  68.         // get the lowest altitude of the atmospheric model
  69.         this.atmosphereLowestAltitude = refractionLayers.get(refractionLayers.size() - 1).getLowestAltitude();
  70.     }

  71.     /** Simple constructor.
  72.      * @param ellipsoid the ellipsoid to be used.
  73.      * @param refractionLayers the refraction layers to be used with this model (layers can be in any order).
  74.      */
  75.     public MultiLayerModel(final ExtendedEllipsoid ellipsoid, final List<ConstantRefractionLayer> refractionLayers) {

  76.         super();
  77.         // at this stage no optimization is set up: no optimization grid is defined

  78.         this.ellipsoid = ellipsoid;
  79.         this.refractionLayers = new ArrayList<>(refractionLayers);

  80.         // sort the layers from the highest (index = 0) to the lowest (index = size - 1)
  81.         Collections.sort(this.refractionLayers,
  82.             (l1, l2) -> Double.compare(l2.getLowestAltitude(), l1.getLowestAltitude()));

  83.         // get the lowest altitude of the model
  84.         atmosphereLowestAltitude = this.refractionLayers.get(this.refractionLayers.size() - 1).getLowestAltitude();
  85.     }

  86.     /** Compute the (position, LOS) of the intersection with the lowest atmospheric layer.
  87.      * @param satPos satellite position, in body frame
  88.      * @param satLos satellite line of sight, in body frame
  89.      * @param rawIntersection intersection point without refraction correction
  90.      * @return the intersection position and LOS with the lowest atmospheric layer
  91.      * @since 2.1
  92.      */
  93.     private IntersectionLOS computeToLowestAtmosphereLayer(
  94.                             final Vector3D satPos, final Vector3D satLos,
  95.                             final NormalizedGeodeticPoint rawIntersection) {

  96.         if (rawIntersection.getAltitude() < atmosphereLowestAltitude) {
  97.             throw new RuggedException(RuggedMessages.NO_LAYER_DATA, rawIntersection.getAltitude(),
  98.                                       atmosphereLowestAltitude);
  99.         }

  100.         Vector3D pos = satPos;
  101.         Vector3D los = satLos.normalize();

  102.         // Compute the intersection point with the lowest layer of atmosphere
  103.         double n1 = -1;
  104.         GeodeticPoint gp = ellipsoid.transform(satPos, ellipsoid.getBodyFrame(), null);

  105.         // Perform the exact computation (no optimization)
  106.         // TBN: the refractionLayers is ordered from the highest to the lowest
  107.         for (ConstantRefractionLayer refractionLayer : refractionLayers) {

  108.             if (refractionLayer.getLowestAltitude() > gp.getAltitude()) {
  109.                 continue;
  110.             }

  111.             final double n2 = refractionLayer.getRefractiveIndex();

  112.             if (n1 > 0) {

  113.                 // when we get here, we have already performed one iteration in the loop
  114.                 // so gp is the los intersection with the layers interface (it was a
  115.                 // point on ground at loop initialization, but is overridden at each iteration)

  116.                 // get new los by applying Snell's law at atmosphere layers interfaces
  117.                 // we avoid computing sequences of inverse-trigo/trigo/inverse-trigo functions
  118.                 // we just use linear algebra and square roots, it is faster and more accurate

  119.                 // at interface crossing, the interface normal is z, the local zenith direction
  120.                 // the ray direction (i.e. los) is u in the upper layer and v in the lower layer
  121.                 // v is in the (u, zenith) plane, so we can say
  122.                 //  (1) v = α u + β z
  123.                 // with α>0 as u and v are roughly in the same direction as the ray is slightly bent

  124.                 // let θ₁ be the los incidence angle at interface crossing
  125.                 // θ₁ = π - angle(u, zenith) is between 0 and π/2 for a downwards observation
  126.                 // let θ₂ be the exit angle at interface crossing
  127.                 // from Snell's law, we have n₁ sin θ₁ = n₂ sin θ₂ and θ₂ is also between 0 and π/2
  128.                 // we have:
  129.                 //   (2) u·z = -cos θ₁
  130.                 //   (3) v·z = -cos θ₂
  131.                 // combining equations (1), (2) and (3) and remembering z·z = 1 as z is normalized , we get
  132.                 //   (4) β = α cos θ₁ - cos θ₂
  133.                 // with all the expressions above, we can rewrite the fact v is normalized:
  134.                 //       1 = v·v
  135.                 //         = α² u·u + 2αβ u·z + β² z·z
  136.                 //         = α² - 2αβ cos θ₁ + β²
  137.                 //         = α² - 2α² cos² θ₁ + 2 α cos θ₁ cos θ₂ + α² cos² θ₁ - 2 α cos θ₁ cos θ₂ + cos² θ₂
  138.                 //         = α²(1 - cos² θ₁) + cos² θ₂
  139.                 // hence α² = (1 - cos² θ₂)/(1 - cos² θ₁)
  140.                 //          = sin² θ₂ / sin² θ₁
  141.                 // as α is positive, and both θ₁ and θ₂ are between 0 and π/2, we finally get
  142.                 //       α  = sin θ₂ / sin θ₁
  143.                 //   (5) α  = n₁/n₂
  144.                 // the α coefficient is independent from the incidence angle,
  145.                 // it depends only on the ratio of refractive indices!
  146.                 //
  147.                 // back to equation (4) and using again the fact θ₂ is between 0 and π/2, we can now write
  148.                 //       β = α cos θ₁ - cos θ₂
  149.                 //         = n₁/n₂ cos θ₁ - cos θ₂
  150.                 //         = n₁/n₂ cos θ₁ - √(1 - sin² θ₂)
  151.                 //         = n₁/n₂ cos θ₁ - √(1 - (n₁/n₂)² sin² θ₁)
  152.                 //         = n₁/n₂ cos θ₁ - √(1 - (n₁/n₂)² (1 - cos² θ₁))
  153.                 //         = n₁/n₂ cos θ₁ - √(1 - (n₁/n₂)² + (n₁/n₂)² cos² θ₁)
  154.                 //   (6) β = -k - √(k² - ζ)
  155.                 // where ζ = (n₁/n₂)² - 1 and k = n₁/n₂ u·z, which is negative, and close to -1 for
  156.                 // nadir observations. As we expect atmosphere models to have small transitions between
  157.                 // layers, we have to handle accurately the case where n₁/n₂ ≈ 1 so ζ ≈ 0. In this case,
  158.                 // a cancellation occurs inside the square root: √(k² - ζ) ≈ √k² ≈ -k (because k is negative).
  159.                 // So β ≈ -k + k ≈ 0 and another cancellation occurs, outside of the square root.
  160.                 // This means that despite equation (6) is mathematically correct, it is prone to numerical
  161.                 // errors when consecutive layers have close refractive indices. A better equivalent
  162.                 // expression is needed. The fact β is close to 0 in this case was expected because
  163.                 // equation (1) reads v = α u + β z, and α = n₁/n₂, so when n₁/n₂ ≈ 1, we have
  164.                 // α ≈ 1 and β ≈ 0, so v ≈ u: when two layers have similar refractive indices, the
  165.                 // propagation direction is almost unchanged.
  166.                 //
  167.                 // The first step for the accurate computation of β is to compute ζ = (n₁/n₂)² - 1
  168.                 // accurately and avoid a cancellation just after a division (which is the least accurate
  169.                 // of the four operations) and a squaring. We will simply use:
  170.                 //   ζ = (n₁/n₂)² - 1
  171.                 //     = (n₁ - n₂) (n₁ + n₂) / n₂²
  172.                 // The cancellation is still there, but it occurs in the subtraction n₁ - n₂, which are
  173.                 // the most accurate values we can get.
  174.                 // The second step for the accurate computation of β is to rewrite equation (6)
  175.                 // by both multiplying and dividing by the dual factor -k + √(k² - ζ):
  176.                 //     β = -k - √(k² - ζ)
  177.                 //       = (-k - √(k² - ζ)) * (-k + √(k² - ζ)) / (-k + √(k² - ζ))
  178.                 //       = (k² - (k² - ζ)) / (-k + √(k² - ζ))
  179.                 // (7) β = ζ / (-k + √(k² - ζ))
  180.                 // expression (7) is more stable numerically than expression (6), because when ζ ≈ 0
  181.                 // its denominator is about -2k, there are no cancellation anymore after the square root.
  182.                 // β is computed with the same accuracy as ζ
  183.                 final double alpha = n1 / n2;
  184.                 final double k     = alpha * Vector3D.dotProduct(los, gp.getZenith());
  185.                 final double zeta  = (n1 - n2) * (n1 + n2) / (n2 * n2);
  186.                 final double beta  = zeta / (FastMath.sqrt(k * k - zeta) - k);
  187.                 los = new Vector3D(alpha, los, beta, gp.getZenith());
  188.             }

  189.             // In case the altitude of the intersection without atmospheric refraction
  190.             // is above the lowest altitude of the atmosphere: stop the search
  191.             if (rawIntersection.getAltitude() > refractionLayer.getLowestAltitude()) {
  192.                 break;
  193.             }

  194.             // Get for the intersection point: the position and the LOS
  195.             pos = ellipsoid.pointAtAltitude(pos, los, refractionLayer.getLowestAltitude());
  196.             gp  = ellipsoid.transform(pos, ellipsoid.getBodyFrame(), null);

  197.             n1 = n2;
  198.         }
  199.         return new IntersectionLOS(pos, los);
  200.     }

  201.     /** {@inheritDoc} */
  202.     @Override
  203.     public NormalizedGeodeticPoint applyCorrection(final Vector3D satPos, final Vector3D satLos,
  204.                                                    final NormalizedGeodeticPoint rawIntersection,
  205.                                                    final IntersectionAlgorithm algorithm) {

  206.         final IntersectionLOS intersectionLOS = computeToLowestAtmosphereLayer(satPos, satLos, rawIntersection);
  207.         final Vector3D pos = intersectionLOS.getIntersectionPos();
  208.         final Vector3D los = intersectionLOS.getIntersectionLos();

  209.         // at this stage the pos belongs to the lowest atmospheric layer.
  210.         // We can compute the intersection of line of sight (los) with Digital Elevation Model
  211.         // as usual (without atmospheric refraction).
  212.         return algorithm.refineIntersection(ellipsoid, pos, los, rawIntersection);
  213.     }

  214. } // end of class MultiLayerModel

  215. /** Container for the (position, LOS) of the intersection with the lowest atmospheric layer.
  216.  * @author Guylaine Prat
  217.  * @since 2.1
  218.  */
  219. class IntersectionLOS {

  220.     /** Position of the intersection with the lowest atmospheric layer. */
  221.     private Vector3D intersectionPos;
  222.     /** LOS of the intersection with the lowest atmospheric layer. */
  223.     private Vector3D intersectionLos;

  224.     /** Default constructor.
  225.      * @param intersectionPos position of the intersection
  226.      * @param intersectionLos los of the intersection
  227.      */
  228.     IntersectionLOS(final Vector3D intersectionPos, final Vector3D intersectionLos) {
  229.         this.intersectionPos = intersectionPos;
  230.         this.intersectionLos = intersectionLos;
  231.     }
  232.     public Vector3D getIntersectionPos() {
  233.         return intersectionPos;
  234.     }

  235.     public Vector3D getIntersectionLos() {
  236.         return intersectionLos;
  237.     }
  238. }