IodLambert.java

  1. /* Copyright 2002-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.estimation.iod;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.estimation.measurements.Position;
  23. import org.orekit.frames.Frame;
  24. import org.orekit.orbits.KeplerianOrbit;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.utils.PVCoordinates;

  27. /**
  28.  * Lambert initial orbit determination, assuming Keplerian motion.
  29.  * An orbit is determined from two position vectors.
  30.  *
  31.  * References:
  32.  *  Battin, R.H., An Introduction to the Mathematics and Methods of Astrodynamics, AIAA Education, 1999.
  33.  *  Lancaster, E.R. and Blanchard, R.C., A Unified Form of Lambert’s Theorem, Goddard Space Flight Center, 1968.
  34.  *
  35.  * @author Joris Olympio
  36.  * @since 8.0
  37.  */
  38. public class IodLambert {

  39.     /** gravitational constant. */
  40.     private final double mu;

  41.     /** Creator.
  42.      *
  43.      * @param mu gravitational constant
  44.      */
  45.     public IodLambert(final double mu) {
  46.         this.mu = mu;
  47.     }

  48.     /** Estimate an initial orbit from two position measurements.
  49.      * <p>
  50.      * The logic for setting {@code posigrade} and {@code nRev} is that the
  51.      * sweep angle Δυ travelled by the object between {@code t1} and {@code t2} is
  52.      * 2π {@code nRev +1} - α if {@code posigrade} is false and 2π {@code nRev} + α
  53.      * if {@code posigrade} is true, where α is the separation angle between
  54.      * {@code p1} and {@code p2}, which is always computed between 0 and π
  55.      * (because in 3D without a normal reference, vector angles cannot go past π).
  56.      * </p>
  57.      * <p>
  58.      * This implies that {@code posigrade} should be set to true if {@code p2} is
  59.      * located in the half orbit starting at {@code p1} and it should be set to
  60.      * false if {@code p2} is located in the half orbit ending at {@code p1},
  61.      * regardless of the number of periods between {@code t1} and {@code t2},
  62.      * and {@code nRev} should be set accordingly.
  63.      * </p>
  64.      * <p>
  65.      * As an example, if {@code t2} is less than half a period after {@code t1},
  66.      * then {@code posigrade} should be {@code true} and {@code nRev} should be 0.
  67.      * If {@code t2} is more than half a period after {@code t1} but less than
  68.      * one period after {@code t1}, {@code posigrade} should be {@code false} and
  69.      * {@code nRev} should be 0.
  70.      * </p>
  71.      * @param frame     measurements frame
  72.      * @param posigrade flag indicating the direction of motion
  73.      * @param nRev      number of revolutions
  74.      * @param p1        first position measurement
  75.      * @param p2        second position measurement
  76.      * @return an initial orbit estimation
  77.      * @since 11.0
  78.      */
  79.     public KeplerianOrbit estimate(final Frame frame, final boolean posigrade,
  80.                                    final int nRev, final Position p1,  final Position p2) {
  81.         return estimate(frame, posigrade, nRev,
  82.                         p1.getPosition(), p1.getDate(), p2.getPosition(), p2.getDate());
  83.     }

  84.     /** Estimate a Keplerian orbit given two position vectors and a duration.
  85.      * <p>
  86.      * The logic for setting {@code posigrade} and {@code nRev} is that the
  87.      * sweep angle Δυ travelled by the object between {@code t1} and {@code t2} is
  88.      * 2π {@code nRev +1} - α if {@code posigrade} is false and 2π {@code nRev} + α
  89.      * if {@code posigrade} is true, where α is the separation angle between
  90.      * {@code p1} and {@code p2}, which is always computed between 0 and π
  91.      * (because in 3D without a normal reference, vector angles cannot go past π).
  92.      * </p>
  93.      * <p>
  94.      * This implies that {@code posigrade} should be set to true if {@code p2} is
  95.      * located in the half orbit starting at {@code p1} and it should be set to
  96.      * false if {@code p2} is located in the half orbit ending at {@code p1},
  97.      * regardless of the number of periods between {@code t1} and {@code t2},
  98.      * and {@code nRev} should be set accordingly.
  99.      * </p>
  100.      * <p>
  101.      * As an example, if {@code t2} is less than half a period after {@code t1},
  102.      * then {@code posigrade} should be {@code true} and {@code nRev} should be 0.
  103.      * If {@code t2} is more than half a period after {@code t1} but less than
  104.      * one period after {@code t1}, {@code posigrade} should be {@code false} and
  105.      * {@code nRev} should be 0.
  106.      * </p>
  107.      * @param frame     frame
  108.      * @param posigrade flag indicating the direction of motion
  109.      * @param nRev      number of revolutions
  110.      * @param p1        position vector 1
  111.      * @param t1        date of observation 1
  112.      * @param p2        position vector 2
  113.      * @param t2        date of observation 2
  114.      * @return  an initial Keplerian orbit estimate
  115.      */
  116.     public KeplerianOrbit estimate(final Frame frame, final boolean posigrade,
  117.                                    final int nRev,
  118.                                    final Vector3D p1, final AbsoluteDate t1,
  119.                                    final Vector3D p2, final AbsoluteDate t2) {

  120.         final double r1 = p1.getNorm();
  121.         final double r2 = p2.getNorm();
  122.         final double tau = t2.durationFrom(t1); // in seconds

  123.         // Exception if t2 < t1
  124.         if (tau < 0.0) {
  125.             throw new OrekitException(OrekitMessages.NON_CHRONOLOGICAL_DATES_FOR_OBSERVATIONS, t1, t2, -tau);
  126.         }

  127.         // normalizing constants
  128.         final double R = FastMath.max(r1, r2); // in m
  129.         final double V = FastMath.sqrt(mu / R);  // in m/s
  130.         final double T = R / V; // in seconds

  131.         // sweep angle
  132.         double dth = Vector3D.angle(p1, p2);
  133.         // compute the number of revolutions
  134.         if (!posigrade) {
  135.             dth = 2 * FastMath.PI - dth;
  136.         }

  137.         // velocity vectors in the orbital plane, in the R-T frame
  138.         final double[] Vdep = new double[2];

  139.         // call Lambert's problem solver
  140.         final boolean exitflag = solveLambertPb(r1 / R, r2 / R, dth, tau / T, nRev, Vdep);

  141.         if (exitflag) {
  142.             // basis vectors
  143.             // normal to the orbital arc plane
  144.             final Vector3D Pn = p1.crossProduct(p2);
  145.             // perpendicular to the radius vector, in the orbital arc plane
  146.             final Vector3D Pt = Pn.crossProduct(p1);

  147.             // tangential velocity norm
  148.             double RT = Pt.getNorm();
  149.             if (!posigrade) {
  150.                 RT = -RT;
  151.             }

  152.             // velocity vector at P1
  153.             final Vector3D Vel1 = new Vector3D(V * Vdep[0] / r1, p1,
  154.                                                V * Vdep[1] / RT, Pt);

  155.             // compute the equivalent Keplerian orbit
  156.             return new KeplerianOrbit(new PVCoordinates(p1, Vel1), frame, t1, mu);
  157.         }

  158.         return null;
  159.     }

  160.     /** Lambert's solver.
  161.      * Assume mu=1.
  162.      *
  163.      * @param r1 radius 1
  164.      * @param r2  radius 2
  165.      * @param dth sweep angle
  166.      * @param tau time of flight
  167.      * @param mRev number of revs
  168.      * @param V1 velocity at departure in (T, N) basis
  169.      * @return something
  170.      */
  171.     boolean solveLambertPb(final double r1, final double r2, final double dth, final double tau,
  172.                            final int mRev, final double[] V1) {
  173.         // decide whether to use the left or right branch (for multi-revolution
  174.         // problems), and the long- or short way.
  175.         final boolean leftbranch = dth < FastMath.PI;
  176.         int longway = 1;
  177.         if (dth > FastMath.PI) {
  178.             longway = -1;
  179.         }

  180.         final int m = FastMath.abs(mRev);
  181.         final double rtof = FastMath.abs(tau);
  182.         final double theta = dth;

  183.         // non-dimensional chord ||r2-r1||
  184.         final double chord = FastMath.sqrt(r1 * r1 + r2 * r2 - 2 * r1 * r2 * FastMath.cos(theta));

  185.         // non-dimensional semi-perimeter of the triangle
  186.         final double speri = 0.5 * (r1 + r2 + chord);

  187.         // minimum energy ellipse semi-major axis
  188.         final double minSma = speri / 2.;

  189.         // lambda parameter (Eq 7.6)
  190.         final double lambda = longway * FastMath.sqrt(1 - chord / speri);

  191.         // reference tof value for the Newton solver
  192.         final double logt = FastMath.log(rtof);

  193.         // initialisation of the iterative root finding process (secant method)
  194.         // initial values
  195.         //  -1 < x < 1  =>  Elliptical orbits
  196.         //  x = 1           Parabolic orbit
  197.         //  x > 1           Hyperbolic orbits
  198.         final double in1;
  199.         final double in2;
  200.         double x1;
  201.         double x2;
  202.         if (m == 0) {
  203.             // one revolution, one solution. Define the left and right asymptotes.
  204.             in1 = -0.6523333;
  205.             in2 = 0.6523333;
  206.             x1   = FastMath.log(1 + in1);
  207.             x2   = FastMath.log(1 + in2);
  208.         } else {
  209.             // select initial values, depending on the branch
  210.             if (!leftbranch) {
  211.                 in1 = -0.523334;
  212.                 in2 = -0.223334;
  213.             } else {
  214.                 in1 = 0.723334;
  215.                 in2 = 0.523334;
  216.             }
  217.             x1 = FastMath.tan(in1 * 0.5 * FastMath.PI);
  218.             x2 = FastMath.tan(in2 * 0.5 * FastMath.PI);
  219.         }

  220.         // initial estimates for the tof
  221.         final double tof1 = timeOfFlight(in1, longway, m, minSma, speri, chord);
  222.         final double tof2 = timeOfFlight(in2, longway, m, minSma, speri, chord);

  223.         // initial bounds for y
  224.         double y1;
  225.         double y2;
  226.         if (m == 0) {
  227.             y1 = FastMath.log(tof1) - logt;
  228.             y2 = FastMath.log(tof2) - logt;
  229.         } else {
  230.             y1 = tof1 - rtof;
  231.             y2 = tof2 - rtof;
  232.         }

  233.         // Solve for x with the secant method
  234.         double err = 1e20;
  235.         int iterations = 0;
  236.         final double tol = 1e-13;
  237.         final int maxiter = 50;
  238.         double xnew = 0;
  239.         while (err > tol && iterations < maxiter) {
  240.             // new x
  241.             xnew = (x1 * y2 - y1 * x2) / (y2 - y1);

  242.             // evaluate new time of flight
  243.             final double x;
  244.             if (m == 0) {
  245.                 x = FastMath.exp(xnew) - 1;
  246.             } else {
  247.                 x = FastMath.atan(xnew) * 2 / FastMath.PI;
  248.             }

  249.             final double tof = timeOfFlight(x, longway, m, minSma, speri, chord);

  250.             // new value of y
  251.             final double ynew;
  252.             if (m == 0) {
  253.                 ynew = FastMath.log(tof) - logt;
  254.             } else {
  255.                 ynew = tof - rtof;
  256.             }

  257.             // save previous and current values for the next iteration
  258.             x1 = x2;
  259.             x2 = xnew;
  260.             y1 = y2;
  261.             y2 = ynew;

  262.             // update error
  263.             err = FastMath.abs(x1 - xnew);

  264.             // increment number of iterations
  265.             ++iterations;
  266.         }

  267.         // failure to converge
  268.         if (err > tol) {
  269.             return false;
  270.         }

  271.         // convert converged value of x
  272.         final double x;
  273.         if (m == 0) {
  274.             x = FastMath.exp(xnew) - 1;
  275.         } else {
  276.             x = FastMath.atan(xnew) * 2 / FastMath.PI;
  277.         }

  278.         // Solution for the semi-major axis (Eq. 7.20)
  279.         final double sma = minSma / (1 - x * x);

  280.         // compute velocities
  281.         final double eta;
  282.         if (x < 1) {
  283.             // ellipse, Eqs. 7.7, 7.17
  284.             final double alfa = 2 * FastMath.acos(x);
  285.             final double beta = longway * 2 * FastMath.asin(FastMath.sqrt((speri - chord) / (2. * sma)));
  286.             final double psi  = (alfa - beta) / 2;
  287.             // Eq. 7.21
  288.             final double sinPsi = FastMath.sin(psi);
  289.             final double etaSq = 2 * sma * sinPsi * sinPsi / speri;
  290.             eta  = FastMath.sqrt(etaSq);
  291.         } else {
  292.             // hyperbola
  293.             final double gamma = 2 * FastMath.acosh(x);
  294.             final double delta = longway * 2 * FastMath.asinh(FastMath.sqrt((chord - speri) / (2 * sma)));
  295.             //
  296.             final double psi  = (gamma - delta) / 2.;
  297.             final double sinhPsi = FastMath.sinh(psi);
  298.             final double etaSq = -2 * sma * sinhPsi * sinhPsi / speri;
  299.             eta  = FastMath.sqrt(etaSq);
  300.         }

  301.         // radial and tangential directions for departure velocity (Eq. 7.36)
  302.         final double VR1 = (1. / eta) * FastMath.sqrt(1. / minSma) * (2 * lambda * minSma / r1 - (lambda + x * eta));
  303.         final double VT1 = (1. / eta) * FastMath.sqrt(1. / minSma) * FastMath.sqrt(r2 / r1) * FastMath.sin(dth / 2);
  304.         V1[0] = VR1;
  305.         V1[1] = VT1;

  306.         return true;
  307.     }

  308.     /** Compute the time of flight of a given arc of orbit.
  309.      * The time of flight is evaluated via the Lagrange expression.
  310.      *
  311.      * @param x          x
  312.      * @param longway    solution number; the long way or the short war
  313.      * @param mrev       number of revolutions of the arc of orbit
  314.      * @param minSma     minimum possible semi-major axis
  315.      * @param speri      semi-parameter of the arc of orbit
  316.      * @param chord      chord of the arc of orbit
  317.      * @return the time of flight for the given arc of orbit
  318.      */
  319.     private double timeOfFlight(final double x, final int longway, final int mrev, final double minSma,
  320.                                 final double speri, final double chord) {

  321.         final double a = minSma / (1 - x * x);

  322.         final double tof;
  323.         if (FastMath.abs(x) < 1) {
  324.             // Lagrange form of the time of flight equation Eq. (7.9)
  325.             // elliptical orbit (note: mu = 1)
  326.             final double beta = longway * 2 * FastMath.asin(FastMath.sqrt((speri - chord) / (2. * a)));
  327.             final double alfa = 2 * FastMath.acos(x);
  328.             tof = a * FastMath.sqrt(a) * ((alfa - FastMath.sin(alfa)) - (beta - FastMath.sin(beta)) + 2 * FastMath.PI * mrev);
  329.         } else {
  330.             // hyperbolic orbit
  331.             final double alfa = 2 * FastMath.acosh(x);
  332.             final double beta = longway * 2 * FastMath.asinh(FastMath.sqrt((speri - chord) / (-2. * a)));
  333.             tof = -a * FastMath.sqrt(-a) * ((FastMath.sinh(alfa) - alfa) - (FastMath.sinh(beta) - beta));
  334.         }

  335.         return tof;
  336.     }
  337. }