FieldCircularLatitudeArgumentUtility.java

  1. /* Copyright 2022-2024 Romain Serra
  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.orbits;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.util.FastMath;
  20. import org.hipparchus.util.FieldSinCos;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;

  23. /**
  24.  * Utility methods for converting between different latitude arguments used by {@link FieldCircularOrbit}.
  25.  * @author Romain Serra
  26.  * @see FieldCircularOrbit
  27.  * @since 12.1
  28.  */
  29. public class FieldCircularLatitudeArgumentUtility {

  30.     /** Tolerance for stopping criterion in iterative conversion from mean to eccentric angle. */
  31.     private static final double TOLERANCE_CONVERGENCE = 1.0e-12;

  32.     /** Maximum number of iterations in iterative conversion from mean to eccentric angle. */
  33.     private static final int MAXIMUM_ITERATION = 50;

  34.     /** Private constructor for utility class. */
  35.     private FieldCircularLatitudeArgumentUtility() {
  36.         // nothing here (utils class)
  37.     }

  38.     /**
  39.      * Computes the true latitude argument from the eccentric latitude argument.
  40.      *
  41.      * @param <T>    Type of the field elements
  42.      * @param ex     e cos(ω), first component of circular eccentricity vector
  43.      * @param ey     e sin(ω), second component of circular eccentricity vector
  44.      * @param alphaE = E + ω eccentric latitude argument (rad)
  45.      * @return the true latitude argument.
  46.      */
  47.     public static <T extends CalculusFieldElement<T>> T eccentricToTrue(final T ex, final T ey, final T alphaE) {
  48.         final T epsilon               = eccentricAndTrueEpsilon(ex, ey);
  49.         final FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  50.         final T cosAlphaE             = scAlphaE.cos();
  51.         final T sinAlphaE             = scAlphaE.sin();
  52.         final T num                   = ex.multiply(sinAlphaE).subtract(ey.multiply(cosAlphaE));
  53.         final T den                   = epsilon.add(1).subtract(ex.multiply(cosAlphaE)).subtract(ey.multiply(sinAlphaE));
  54.         return alphaE.add(eccentricAndTrueAtan(num, den));
  55.     }

  56.     /**
  57.      * Computes the eccentric latitude argument from the true latitude argument.
  58.      *
  59.      * @param <T>    Type of the field elements
  60.      * @param ex     e cos(ω), first component of circular eccentricity vector
  61.      * @param ey     e sin(ω), second component of circular eccentricity vector
  62.      * @param alphaV = v + ω true latitude argument (rad)
  63.      * @return the eccentric latitude argument.
  64.      */
  65.     public static <T extends CalculusFieldElement<T>> T trueToEccentric(final T ex, final T ey, final T alphaV) {
  66.         final T epsilon               = eccentricAndTrueEpsilon(ex, ey);
  67.         final FieldSinCos<T> scAlphaV = FastMath.sinCos(alphaV);
  68.         final T cosAlphaV             = scAlphaV.cos();
  69.         final T sinAlphaV             = scAlphaV.sin();
  70.         final T num                   = ey.multiply(cosAlphaV).subtract(ex.multiply(sinAlphaV));
  71.         final T den                   = epsilon.add(1).add(ex.multiply(cosAlphaV).add(ey.multiply(sinAlphaV)));
  72.         return alphaV.add(eccentricAndTrueAtan(num, den));
  73.     }

  74.     /**
  75.      * Computes an intermediate quantity for conversions between true and eccentric.
  76.      *
  77.      * @param <T>    Type of the field elements
  78.      * @param ex e cos(ω), first component of circular eccentricity vector
  79.      * @param ey e sin(ω), second component of circular eccentricity vector
  80.      * @return intermediate variable referred to as epsilon.
  81.      */
  82.     private static <T extends CalculusFieldElement<T>> T eccentricAndTrueEpsilon(final T ex, final T ey) {
  83.         return (ex.square().negate().subtract(ey.square()).add(1.)).sqrt();
  84.     }

  85.     /**
  86.      * Computes another intermediate quantity for conversions between true and eccentric.
  87.      *
  88.      * @param <T>    Type of the field elements
  89.      * @param num numerator for angular conversion
  90.      * @param den denominator for angular conversion
  91.      * @return arc-tangent of ratio of inputs times two.
  92.      */
  93.     private static <T extends CalculusFieldElement<T>> T eccentricAndTrueAtan(final T num, final T den) {
  94.         return (num.divide(den)).atan().multiply(2);
  95.     }

  96.     /**
  97.      * Computes the eccentric latitude argument from the mean latitude argument.
  98.      *
  99.      * @param <T>    Type of the field elements
  100.      * @param ex     e cos(ω), first component of circular eccentricity vector
  101.      * @param ey     e sin(ω), second component of circular eccentricity vector
  102.      * @param alphaM = M + ω  mean latitude argument (rad)
  103.      * @return the eccentric latitude argument.
  104.      */
  105.     public static <T extends CalculusFieldElement<T>> T meanToEccentric(final T ex, final T ey, final T alphaM) {
  106.         // Generalization of Kepler equation to circular parameters
  107.         // with alphaE = PA + E and
  108.         //      alphaM = PA + M = alphaE - ex.sin(alphaE) + ey.cos(alphaE)

  109.         T alphaE                = alphaM;
  110.         T shift;
  111.         T alphaEMalphaM         = alphaM.getField().getZero();
  112.         boolean hasConverged;
  113.         int    iter     = 0;
  114.         do {
  115.             final FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  116.             final T f2 = ex.multiply(scAlphaE.sin()).subtract(ey.multiply(scAlphaE.cos()));
  117.             final T f1 = ex.negate().multiply(scAlphaE.cos()).subtract(ey.multiply(scAlphaE.sin())).add(1);
  118.             final T f0 = alphaEMalphaM.subtract(f2);

  119.             final T f12 = f1.multiply(2);
  120.             shift = f0.multiply(f12).divide(f1.multiply(f12).subtract(f0.multiply(f2)));

  121.             alphaEMalphaM  = alphaEMalphaM.subtract(shift);
  122.             alphaE         = alphaM.add(alphaEMalphaM);

  123.             hasConverged = FastMath.abs(shift.getReal()) <= TOLERANCE_CONVERGENCE;
  124.         } while (++iter < MAXIMUM_ITERATION && !hasConverged);

  125.         if (!hasConverged) {
  126.             throw new OrekitException(OrekitMessages.UNABLE_TO_COMPUTE_ECCENTRIC_LATITUDE_ARGUMENT, iter);
  127.         }
  128.         return alphaE;

  129.     }

  130.     /**
  131.      * Computes the mean latitude argument from the eccentric latitude argument.
  132.      *
  133.      * @param <T>    Type of the field elements
  134.      * @param ex     e cos(ω), first component of circular eccentricity vector
  135.      * @param ey     e sin(ω), second component of circular eccentricity vector
  136.      * @param alphaE = E + ω  eccentric latitude argument (rad)
  137.      * @return the mean latitude argument.
  138.      */
  139.     public static <T extends CalculusFieldElement<T>> T eccentricToMean(final T ex, final T ey, final T alphaE) {
  140.         final FieldSinCos<T> scAlphaE = FastMath.sinCos(alphaE);
  141.         return alphaE.subtract(ex.multiply(scAlphaE.sin()).subtract(ey.multiply(scAlphaE.cos())));
  142.     }

  143.     /**
  144.      * Computes the mean latitude argument from the eccentric latitude argument.
  145.      *
  146.      * @param <T>    Type of the field elements
  147.      * @param ex     e cos(ω), first component of circular eccentricity vector
  148.      * @param ey     e sin(ω), second component of circular eccentricity vector
  149.      * @param alphaV = V + ω  true latitude argument (rad)
  150.      * @return the mean latitude argument.
  151.      */
  152.     public static <T extends CalculusFieldElement<T>> T trueToMean(final T ex, final T ey, final T alphaV) {
  153.         final T alphaE = trueToEccentric(ex, ey, alphaV);
  154.         return eccentricToMean(ex, ey, alphaE);
  155.     }

  156.     /**
  157.      * Computes the true latitude argument from the eccentric latitude argument.
  158.      *
  159.      * @param <T>    Type of the field elements
  160.      * @param ex     e cos(ω), first component of circular eccentricity vector
  161.      * @param ey     e sin(ω), second component of circular eccentricity vector
  162.      * @param alphaM = M + ω  mean latitude argument (rad)
  163.      * @return the true latitude argument.
  164.      */
  165.     public static <T extends CalculusFieldElement<T>> T meanToTrue(final T ex, final T ey, final T alphaM) {
  166.         final T alphaE = meanToEccentric(ex, ey, alphaM);
  167.         return eccentricToTrue(ex, ey, alphaE);
  168.     }

  169. }