FieldEquinoctialLongitudeArgumentUtility.java

  1. /* Copyright 2022-2025 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.OrekitInternalError;
  23. import org.orekit.errors.OrekitMessages;

  24. /**
  25.  * Utility methods for converting between different longitude arguments used by {@link FieldEquinoctialOrbit}.
  26.  * @author Romain Serra
  27.  * @see FieldEquinoctialOrbit
  28.  * @since 12.1
  29.  */
  30. public class FieldEquinoctialLongitudeArgumentUtility {

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

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

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

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

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

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

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

  97.     /**
  98.      * Computes the eccentric longitude argument from the mean longitude argument.
  99.      *
  100.      * @param <T> Type of the field elements
  101.      * @param ex  e cos(ω), first component of eccentricity vector
  102.      * @param ey  e sin(ω), second component of eccentricity vector
  103.      * @param lM  = M + ω + Ω  mean longitude argument (rad)
  104.      * @return the eccentric longitude argument.
  105.      */
  106.     public static <T extends CalculusFieldElement<T>> T meanToEccentric(final T ex, final T ey, final T lM) {
  107.         // Generalization of Kepler equation to equinoctial parameters
  108.         // with lE = PA + RAAN + E and
  109.         //      lM = PA + RAAN + M = lE - ex.sin(lE) + ey.cos(lE)
  110.         T lE = lM;
  111.         T shift;
  112.         T lEmlM = lM.getField().getZero();
  113.         boolean hasConverged;
  114.         int iter = 0;
  115.         do {
  116.             final FieldSinCos<T> scLE = FastMath.sinCos(lE);
  117.             final T f2 = ex.multiply(scLE.sin()).subtract(ey.multiply(scLE.cos()));
  118.             final T f1 = ex.multiply(scLE.cos()).add(ey.multiply(scLE.sin())).negate().add(1);
  119.             final T f0 = lEmlM.subtract(f2);

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

  122.             lEmlM = lEmlM.subtract(shift);
  123.             lE     = lM.add(lEmlM);

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

  126.         if (!hasConverged) {
  127.             throw new OrekitException(OrekitMessages.UNABLE_TO_COMPUTE_ECCENTRIC_LONGITUDE_ARGUMENT, iter);
  128.         }
  129.         return lE;

  130.     }

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

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

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

  170.     /**
  171.      * Convert argument of longitude.
  172.      * @param oldType old position angle type
  173.      * @param l old value for argument of longitude
  174.      * @param ex ex
  175.      * @param ey ey
  176.      * @param newType new position angle type
  177.      * @param <T> field type
  178.      * @return converted argument of longitude
  179.      * @since 12.2
  180.      */
  181.     public static <T extends CalculusFieldElement<T>> T convertL(final PositionAngleType oldType, final T l,
  182.                                                                  final T ex, final T ey, final PositionAngleType newType) {
  183.         if (oldType == newType) {
  184.             return l;

  185.         } else {
  186.             switch (newType) {

  187.                 case ECCENTRIC:
  188.                     if (oldType == PositionAngleType.MEAN) {
  189.                         return FieldEquinoctialLongitudeArgumentUtility.meanToEccentric(ex, ey, l);
  190.                     } else {
  191.                         return FieldEquinoctialLongitudeArgumentUtility.trueToEccentric(ex, ey, l);
  192.                     }

  193.                 case MEAN:
  194.                     if (oldType == PositionAngleType.TRUE) {
  195.                         return FieldEquinoctialLongitudeArgumentUtility.trueToMean(ex, ey, l);
  196.                     } else {
  197.                         return FieldEquinoctialLongitudeArgumentUtility.eccentricToMean(ex, ey, l);
  198.                     }

  199.                 case TRUE:
  200.                     if (oldType == PositionAngleType.MEAN) {
  201.                         return FieldEquinoctialLongitudeArgumentUtility.meanToTrue(ex, ey, l);
  202.                     } else {
  203.                         return FieldEquinoctialLongitudeArgumentUtility.eccentricToTrue(ex, ey, l);
  204.                     }

  205.                 default:
  206.                     throw new OrekitInternalError(null);
  207.             }
  208.         }
  209.     }
  210. }