FieldKeplerianAnomalyUtility.java

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

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.exception.MathIllegalStateException;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.FieldSinCos;
  23. import org.hipparchus.util.MathUtils;
  24. import org.hipparchus.util.Precision;
  25. import org.orekit.errors.OrekitMessages;

  26. /**
  27.  * Utility methods for converting between different Keplerian anomalies.
  28.  * @author Luc Maisonobe
  29.  * @author Andrea Antolino
  30.  * @author Andrew Goetz
  31.  */
  32. public class FieldKeplerianAnomalyUtility {

  33.     /** First coefficient to compute elliptic Kepler equation solver starter. */
  34.     private static final double A;

  35.     /** Second coefficient to compute elliptic Kepler equation solver starter. */
  36.     private static final double B;

  37.     static {
  38.         final double k1 = 3 * FastMath.PI + 2;
  39.         final double k2 = FastMath.PI - 1;
  40.         final double k3 = 6 * FastMath.PI - 1;
  41.         A = 3 * k2 * k2 / k1;
  42.         B = k3 * k3 / (6 * k1);
  43.     }

  44.     /** Private constructor for utility class. */
  45.     private FieldKeplerianAnomalyUtility() {
  46.         // Nothing to do
  47.     }

  48.     /**
  49.      * Computes the elliptic true anomaly from the elliptic mean anomaly.
  50.      * @param <T> field type
  51.      * @param e eccentricity such that 0 &le; e &lt; 1
  52.      * @param M elliptic mean anomaly (rad)
  53.      * @return elliptic true anomaly (rad)
  54.      */
  55.     public static <T extends CalculusFieldElement<T>> T ellipticMeanToTrue(final T e, final T M) {
  56.         final T E = ellipticMeanToEccentric(e, M);
  57.         final T v = ellipticEccentricToTrue(e, E);
  58.         return v;
  59.     }

  60.     /**
  61.      * Computes the elliptic mean anomaly from the elliptic true anomaly.
  62.      * @param <T> field type
  63.      * @param e eccentricity such that 0 &le; e &lt; 1
  64.      * @param v elliptic true anomaly (rad)
  65.      * @return elliptic mean anomaly (rad)
  66.      */
  67.     public static <T extends CalculusFieldElement<T>> T ellipticTrueToMean(final T e, final T v) {
  68.         final T E = ellipticTrueToEccentric(e, v);
  69.         final T M = ellipticEccentricToMean(e, E);
  70.         return M;
  71.     }

  72.     /**
  73.      * Computes the elliptic true anomaly from the elliptic eccentric anomaly.
  74.      * @param <T> field type
  75.      * @param e eccentricity such that 0 &le; e &lt; 1
  76.      * @param E elliptic eccentric anomaly (rad)
  77.      * @return elliptic true anomaly (rad)
  78.      */
  79.     public static <T extends CalculusFieldElement<T>> T ellipticEccentricToTrue(final T e, final T E) {
  80.         final T beta = e.divide(e.multiply(e).negate().add(1).sqrt().add(1));
  81.         final FieldSinCos<T> scE = FastMath.sinCos(E);
  82.         return E.add(beta.multiply(scE.sin()).divide(beta.multiply(scE.cos()).subtract(1).negate()).atan().multiply(2));
  83.     }

  84.     /**
  85.      * Computes the elliptic eccentric anomaly from the elliptic true anomaly.
  86.      * @param <T> field type
  87.      * @param e eccentricity such that 0 &le; e &lt; 1
  88.      * @param v elliptic true anomaly (rad)
  89.      * @return elliptic eccentric anomaly (rad)
  90.      */
  91.     public static <T extends CalculusFieldElement<T>> T ellipticTrueToEccentric(final T e, final T v) {
  92.         final T beta = e.divide(e.multiply(e).negate().add(1).sqrt().add(1));
  93.         final FieldSinCos<T> scv = FastMath.sinCos(v);
  94.         return v.subtract((beta.multiply(scv.sin()).divide(beta.multiply(scv.cos()).add(1))).atan().multiply(2));
  95.     }

  96.     /**
  97.      * Computes the elliptic eccentric anomaly from the elliptic mean anomaly.
  98.      * <p>
  99.      * The algorithm used here for solving hyperbolic Kepler equation is from Odell,
  100.      * A.W., Gooding, R.H. "Procedures for solving Kepler's equation." Celestial
  101.      * Mechanics 38, 307–334 (1986). https://doi.org/10.1007/BF01238923
  102.      * </p>
  103.      * @param <T> field type
  104.      * @param e eccentricity such that 0 &le; e &lt; 1
  105.      * @param M elliptic mean anomaly (rad)
  106.      * @return elliptic eccentric anomaly (rad)
  107.      */
  108.     public static <T extends CalculusFieldElement<T>> T ellipticMeanToEccentric(final T e, final T M) {
  109.         // reduce M to [-PI PI) interval
  110.         final T reducedM = MathUtils.normalizeAngle(M, M.getField().getZero());

  111.         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
  112.         T E;
  113.         if (reducedM.abs().getReal() < 1.0 / 6.0) {
  114.             if (FastMath.abs(reducedM.getReal()) < Precision.SAFE_MIN) {
  115.                 // this is an Orekit change to the S12 starter, mainly used when T is some kind
  116.                 // of derivative structure. If reducedM is 0.0, the derivative of cbrt is
  117.                 // infinite which induces NaN appearing later in the computation. As in this
  118.                 // case E and M are almost equal, we initialize E with reducedM
  119.                 E = reducedM;
  120.             } else {
  121.                 // this is the standard S12 starter
  122.                 E = reducedM.add(e.multiply((reducedM.multiply(6).cbrt()).subtract(reducedM)));
  123.             }
  124.         } else {
  125.             final T pi = e.getPi();
  126.             if (reducedM.getReal() < 0) {
  127.                 final T w = reducedM.add(pi);
  128.                 E = reducedM.add(e.multiply(w.multiply(A).divide(w.negate().add(B)).subtract(pi).subtract(reducedM)));
  129.             } else {
  130.                 final T w = reducedM.negate().add(pi);
  131.                 E = reducedM
  132.                         .add(e.multiply(w.multiply(A).divide(w.negate().add(B)).negate().subtract(reducedM).add(pi)));
  133.             }
  134.         }

  135.         final T e1 = e.negate().add(1);
  136.         final boolean noCancellationRisk = (e1.getReal() + E.getReal() * E.getReal() / 6) >= 0.1;

  137.         // perform two iterations, each consisting of one Halley step and one
  138.         // Newton-Raphson step
  139.         for (int j = 0; j < 2; ++j) {

  140.             final T f;
  141.             T fd;
  142.             final FieldSinCos<T> scE = FastMath.sinCos(E);
  143.             final T fdd = e.multiply(scE.sin());
  144.             final T fddd = e.multiply(scE.cos());

  145.             if (noCancellationRisk) {

  146.                 f = (E.subtract(fdd)).subtract(reducedM);
  147.                 fd = fddd.negate().add(1);
  148.             } else {

  149.                 f = eMeSinE(e, E).subtract(reducedM);
  150.                 final T s = E.multiply(0.5).sin();
  151.                 fd = e1.add(e.multiply(s).multiply(s).multiply(2));
  152.             }
  153.             final T dee = f.multiply(fd).divide(f.multiply(fdd).multiply(0.5).subtract(fd.multiply(fd)));

  154.             // update eccentric anomaly, using expressions that limit underflow problems
  155.             final T w = fd.add(dee.multiply(fdd.add(dee.multiply(fddd.divide(3)))).multiply(0.5));
  156.             fd = fd.add(dee.multiply(fdd.add(dee.multiply(fddd).multiply(0.5))));
  157.             E = E.subtract(f.subtract(dee.multiply(fd.subtract(w))).divide(fd));

  158.         }

  159.         // expand the result back to original range
  160.         E = E.add(M).subtract(reducedM);
  161.         return E;
  162.     }

  163.     /**
  164.      * Accurate computation of E - e sin(E).
  165.      * <p>
  166.      * This method is used when E is close to 0 and e close to 1, i.e. near the
  167.      * perigee of almost parabolic orbits
  168.      * </p>
  169.      * @param <T> field type
  170.      * @param e eccentricity
  171.      * @param E eccentric anomaly (rad)
  172.      * @return E - e sin(E)
  173.      */
  174.     private static <T extends CalculusFieldElement<T>> T eMeSinE(final T e, final T E) {
  175.         T x = (e.negate().add(1)).multiply(E.sin());
  176.         final T mE2 = E.square().negate();
  177.         T term = E;
  178.         double d = 0;
  179.         // the inequality test below IS intentional and should NOT be replaced by a
  180.         // check with a small tolerance
  181.         for (T x0 = E.getField().getZero().add(Double.NaN); !Double.valueOf(x.getReal())
  182.                 .equals(x0.getReal());) {
  183.             d += 2;
  184.             term = term.multiply(mE2.divide(d * (d + 1)));
  185.             x0 = x;
  186.             x = x.subtract(term);
  187.         }
  188.         return x;
  189.     }

  190.     /**
  191.      * Computes the elliptic mean anomaly from the elliptic eccentric anomaly.
  192.      * @param <T> field type
  193.      * @param e eccentricity such that 0 &le; e &lt; 1
  194.      * @param E elliptic eccentric anomaly (rad)
  195.      * @return elliptic mean anomaly (rad)
  196.      */
  197.     public static <T extends CalculusFieldElement<T>> T ellipticEccentricToMean(final T e, final T E) {
  198.         return E.subtract(e.multiply(E.sin()));
  199.     }

  200.     /**
  201.      * Computes the hyperbolic true anomaly from the hyperbolic mean anomaly.
  202.      * @param <T> field type
  203.      * @param e eccentricity &gt; 1
  204.      * @param M hyperbolic mean anomaly
  205.      * @return hyperbolic true anomaly (rad)
  206.      */
  207.     public static <T extends CalculusFieldElement<T>> T hyperbolicMeanToTrue(final T e, final T M) {
  208.         final T H = hyperbolicMeanToEccentric(e, M);
  209.         final T v = hyperbolicEccentricToTrue(e, H);
  210.         return v;
  211.     }

  212.     /**
  213.      * Computes the hyperbolic mean anomaly from the hyperbolic true anomaly.
  214.      * @param <T> field type
  215.      * @param e eccentricity &gt; 1
  216.      * @param v hyperbolic true anomaly (rad)
  217.      * @return hyperbolic mean anomaly
  218.      */
  219.     public static <T extends CalculusFieldElement<T>> T hyperbolicTrueToMean(final T e, final T v) {
  220.         final T H = hyperbolicTrueToEccentric(e, v);
  221.         final T M = hyperbolicEccentricToMean(e, H);
  222.         return M;
  223.     }

  224.     /**
  225.      * Computes the hyperbolic true anomaly from the hyperbolic eccentric anomaly.
  226.      * @param <T> field type
  227.      * @param e eccentricity &gt; 1
  228.      * @param H hyperbolic eccentric anomaly
  229.      * @return hyperbolic true anomaly (rad)
  230.      */
  231.     public static <T extends CalculusFieldElement<T>> T hyperbolicEccentricToTrue(final T e, final T H) {
  232.         final T s = e.add(1).divide(e.subtract(1)).sqrt();
  233.         final T tanH = H.multiply(0.5).tanh();
  234.         return s.multiply(tanH).atan().multiply(2);
  235.     }

  236.     /**
  237.      * Computes the hyperbolic eccentric anomaly from the hyperbolic true anomaly.
  238.      * @param <T> field type
  239.      * @param e eccentricity &gt; 1
  240.      * @param v hyperbolic true anomaly (rad)
  241.      * @return hyperbolic eccentric anomaly
  242.      */
  243.     public static <T extends CalculusFieldElement<T>> T hyperbolicTrueToEccentric(final T e, final T v) {
  244.         final FieldSinCos<T> scv = FastMath.sinCos(v);
  245.         final T sinhH = e.multiply(e).subtract(1).sqrt().multiply(scv.sin()).divide(e.multiply(scv.cos()).add(1));
  246.         return sinhH.asinh();
  247.     }

  248.     /**
  249.      * Computes the hyperbolic eccentric anomaly from the hyperbolic mean anomaly.
  250.      * <p>
  251.      * The algorithm used here for solving hyperbolic Kepler equation is from
  252.      * Gooding, R.H., Odell, A.W. "The hyperbolic Kepler equation (and the elliptic
  253.      * equation revisited)." Celestial Mechanics 44, 267–282 (1988).
  254.      * https://doi.org/10.1007/BF01235540
  255.      * </p>
  256.      * @param <T> field type
  257.      * @param e eccentricity &gt; 1
  258.      * @param M hyperbolic mean anomaly
  259.      * @return hyperbolic eccentric anomaly
  260.      */
  261.     public static <T extends CalculusFieldElement<T>> T hyperbolicMeanToEccentric(final T e, final T M) {
  262.         final Field<T> field = e.getField();
  263.         final T zero = field.getZero();
  264.         final T one = field.getOne();
  265.         final T two = zero.newInstance(2.0);
  266.         final T three = zero.newInstance(3.0);
  267.         final T half = zero.newInstance(0.5);
  268.         final T onePointFive = zero.newInstance(1.5);
  269.         final T fourThirds = zero.newInstance(4.0).divide(3.0);

  270.         // Solve L = S - g * asinh(S) for S = sinh(H).
  271.         final T L = M.divide(e);
  272.         final T g = e.reciprocal();
  273.         final T g1 = one.subtract(g);

  274.         // Starter based on Lagrange's theorem.
  275.         T S = L;
  276.         if (L.isZero()) {
  277.             return M.getField().getZero();
  278.         }
  279.         final T cl = L.multiply(L).add(one).sqrt();
  280.         final T al = L.asinh();
  281.         final T w = g.multiply(g).multiply(al).divide(cl.multiply(cl).multiply(cl));
  282.         S = one.subtract(g.divide(cl));
  283.         S = L.add(g.multiply(al).divide(S.multiply(S).multiply(S)
  284.                 .add(w.multiply(L).multiply(onePointFive.subtract(fourThirds.multiply(g)))).cbrt()));

  285.         // Two iterations (at most) of Halley-then-Newton process.
  286.         for (int i = 0; i < 2; ++i) {
  287.             final T s0 = S.multiply(S);
  288.             final T s1 = s0.add(one);
  289.             final T s2 = s1.sqrt();
  290.             final T s3 = s1.multiply(s2);
  291.             final T fdd = g.multiply(S).divide(s3);
  292.             final T fddd = g.multiply(one.subtract(two.multiply(s0))).divide(s1.multiply(s3));

  293.             T f;
  294.             T fd;
  295.             if (s0.divide(6.0).add(g1).getReal() >= 0.5) {
  296.                 f = S.subtract(g.multiply(S.asinh())).subtract(L);
  297.                 fd = one.subtract(g.divide(s2));
  298.             } else {
  299.                 // Accurate computation of S - (1 - g1) * asinh(S)
  300.                 // when (g1, S) is close to (0, 0).
  301.                 final T t = S.divide(one.add(one.add(S.multiply(S)).sqrt()));
  302.                 final T tsq = t.square();
  303.                 T x = S.multiply(g1.add(g.multiply(tsq)));
  304.                 T term = two.multiply(g).multiply(t);
  305.                 T twoI1 = one;
  306.                 T x0;
  307.                 int j = 0;
  308.                 do {
  309.                     if (++j == 1000000) {
  310.                         // This isn't expected to happen, but it protects against an infinite loop.
  311.                         throw new MathIllegalStateException(
  312.                                 OrekitMessages.UNABLE_TO_COMPUTE_HYPERBOLIC_ECCENTRIC_ANOMALY, j);
  313.                     }
  314.                     twoI1 = twoI1.add(2.0);
  315.                     term = term.multiply(tsq);
  316.                     x0 = x;
  317.                     x = x.subtract(term.divide(twoI1));
  318.                 } while (x.getReal() != x0.getReal());
  319.                 f = x.subtract(L);
  320.                 fd = s0.divide(s2.add(one)).add(g1).divide(s2);
  321.             }
  322.             final T ds = f.multiply(fd).divide(half.multiply(f).multiply(fdd).subtract(fd.multiply(fd)));
  323.             final T stemp = S.add(ds);
  324.             if (S.getReal() == stemp.getReal()) {
  325.                 break;
  326.             }
  327.             f = f.add(ds.multiply(fd.add(half.multiply(ds.multiply(fdd.add(ds.divide(three).multiply(fddd)))))));
  328.             fd = fd.add(ds.multiply(fdd.add(half.multiply(ds).multiply(fddd))));
  329.             S = stemp.subtract(f.divide(fd));
  330.         }

  331.         final T H = S.asinh();
  332.         return H;
  333.     }

  334.     /**
  335.      * Computes the hyperbolic mean anomaly from the hyperbolic eccentric anomaly.
  336.      * @param <T> field type
  337.      * @param e eccentricity &gt; 1
  338.      * @param H hyperbolic eccentric anomaly
  339.      * @return hyperbolic mean anomaly
  340.      */
  341.     public static <T extends CalculusFieldElement<T>> T hyperbolicEccentricToMean(final T e, final T H) {
  342.         return e.multiply(H.sinh()).subtract(H);
  343.     }

  344. }