IodGooding.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.frames.Frame;
  21. import org.orekit.orbits.KeplerianOrbit;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.utils.PVCoordinates;

  24. /**
  25.  * Gooding angles only initial orbit determination, assuming Keplerian motion.
  26.  * An orbit is determined from three angular observations.
  27.  *
  28.  * Reference:
  29.  *    Gooding, R.H., A New Procedure for Orbit Determination Based on Three Lines of Sight (Angles only),
  30.  *    Technical Report 93004, April 1993
  31.  *
  32.  * @author Joris Olympio
  33.  * @since 8.0
  34.  */
  35. public class IodGooding {

  36.     /** Frame of the observation. */
  37.     private final Frame frame;

  38.     /** Gravitationnal constant. */
  39.     private final double mu;

  40.     /** Normalizing constant for distances. */
  41.     private double R;

  42.     /** Normalizing constant for velocities. */
  43.     private double V;

  44.     /** Normalizing constant for duration. */
  45.     private double T;

  46.     /** observer position 1. */
  47.     private Vector3D vObserverPosition1;

  48.     /** observer position 2. */
  49.     private Vector3D vObserverPosition2;

  50.     /** observer position 3. */
  51.     private Vector3D vObserverPosition3;

  52.     /** Date of the first observation. * */
  53.     private AbsoluteDate date1;

  54.     /** Radius of point 1 (X-R1). */
  55.     private double R1;
  56.     /** Radius of point 2 (X-R2). */
  57.     private double R2;
  58.     /** Radius of point 3 (X-R3). */
  59.     private double R3;

  60.     /** Range of point 1 (O1-R1). */
  61.     private double rho1;
  62.     /** Range of point 2 (O2-R2). */
  63.     private double rho2;
  64.     /** Range of point 3 (O3-R3). */
  65.     private double rho3;

  66.     /** working variable. */
  67.     private double D1;

  68.     /** working variable. */
  69.     private double D3;

  70.     /** factor for FD. */
  71.     private double facFiniteDiff;

  72.     /** Simple Lambert's problem solver. */
  73.     private IodLambert lambert;

  74.     /** Creator.
  75.      *
  76.      * @param frame Frame for the observations
  77.      * @param mu  gravitational constant
  78.      */
  79.     public IodGooding(final Frame frame, final double mu) {
  80.         this.mu = mu;
  81.         this.frame = frame;

  82.         this.rho1 = 0;
  83.         this.rho2 = 0;
  84.         this.rho3 = 0;
  85.     }

  86.     /** Get the range for observation (1).
  87.      *
  88.      * @return the range for observation (1).
  89.      */
  90.     public double getRange1() {
  91.         return rho1 * R;
  92.     }

  93.     /** Get the range for observation (2).
  94.      *
  95.      * @return the range for observation (2).
  96.      */
  97.     public double getRange2() {
  98.         return rho2 * R;
  99.     }

  100.     /** Get the range for observation (3).
  101.      *
  102.      * @return the range for observation (3).
  103.      */
  104.     public double getRange3() {
  105.         return rho3 * R;
  106.     }

  107.     /** Orbit got from Observed Three Lines of Sight (angles only).
  108.      *
  109.      * @param O1 Observer position 1
  110.      * @param O2 Observer position 2
  111.      * @param O3 Observer position 3
  112.      * @param lineOfSight1 line of sight 1
  113.      * @param dateObs1 date of observation 1
  114.      * @param lineOfSight2 line of sight 2
  115.      * @param dateObs2 date of observation 1
  116.      * @param lineOfSight3 line of sight 3
  117.      * @param dateObs3 date of observation 1
  118.      * @param rho1init initial guess of the range problem. range 1, in meters
  119.      * @param rho3init initial guess of the range problem. range 3, in meters
  120.      * @return an estimate of the Keplerian orbit
  121.      */
  122.     public KeplerianOrbit estimate(final Vector3D O1, final Vector3D O2, final Vector3D O3,
  123.                                    final Vector3D lineOfSight1, final AbsoluteDate dateObs1,
  124.                                    final Vector3D lineOfSight2, final AbsoluteDate dateObs2,
  125.                                    final Vector3D lineOfSight3, final AbsoluteDate dateObs3,
  126.                                    final double rho1init, final double rho3init) {

  127.         this.date1 = dateObs1;

  128.         // normalizing coefficients
  129.         R = FastMath.max(rho1init, rho3init);
  130.         V = FastMath.sqrt(mu / R);
  131.         T = R / V;

  132.         // Initialize Lambert's problem solver for non-dimensional units.
  133.         lambert = new IodLambert(1.);

  134.         this.vObserverPosition1 = O1.scalarMultiply(1. / R);
  135.         this.vObserverPosition2 = O2.scalarMultiply(1. / R);
  136.         this.vObserverPosition3 = O3.scalarMultiply(1. / R);

  137.         final int maxiter = 100; // maximum iter

  138.         // solve the range problem
  139.         solveRangeProblem(rho1init / R, rho3init / R,
  140.                           dateObs3.durationFrom(dateObs1) / T, dateObs2.durationFrom(dateObs1) / T,
  141.                           0,
  142.                           true,
  143.                           lineOfSight1, lineOfSight2, lineOfSight3,
  144.                           maxiter);

  145.         // use the Gibbs problem to get the orbit now that we have three position vectors.
  146.         final IodGibbs gibbs = new IodGibbs(mu);
  147.         final Vector3D p1 = vObserverPosition1.add(lineOfSight1.scalarMultiply(rho1)).scalarMultiply(R);
  148.         final Vector3D p2 = vObserverPosition2.add(lineOfSight2.scalarMultiply(rho2)).scalarMultiply(R);
  149.         final Vector3D p3 = vObserverPosition3.add(lineOfSight3.scalarMultiply(rho3)).scalarMultiply(R);
  150.         return gibbs.estimate(frame, p1, dateObs1, p2, dateObs2, p3, dateObs3);
  151.     }

  152.     /** Solve the range problem when three line of sight are given.
  153.      *
  154.      * @param rho1init   initial value for range R1, in meters
  155.      * @param rho3init   initial value for range R3, in meters
  156.      * @param T13   time of flight 1->3, in seconds
  157.      * @param T12   time of flight 1->2, in seconds
  158.      * @param nrev number of revolutions
  159.      * @param direction  posigrade (true) or retrograde
  160.      * @param lineOfSight1  line of sight 1
  161.      * @param lineOfSight2  line of sight 2
  162.      * @param lineOfSight3  line of sight 3
  163.      * @param maxIterations         max iter
  164.      * @return nothing
  165.      */
  166.     private boolean solveRangeProblem(final double rho1init, final double rho3init,
  167.                                       final double T13, final double T12,
  168.                                       final int nrev,
  169.                                       final boolean direction,
  170.                                       final Vector3D lineOfSight1,
  171.                                       final Vector3D lineOfSight2,
  172.                                       final Vector3D lineOfSight3,
  173.                                       final int maxIterations) {
  174.         final double ARBF = 1e-6;   // finite differences step
  175.         boolean withHalley = true;  // use Halley's method
  176.         final double cvtol = 1e-14; // convergence tolerance

  177.         rho1 = rho1init;
  178.         rho3 = rho3init;


  179.         int iter = 0;
  180.         double stoppingCriterion = 10 * cvtol;
  181.         while ((iter < maxIterations) && (FastMath.abs(stoppingCriterion) > cvtol))  {
  182.             facFiniteDiff = ARBF;

  183.             // proposed in the original algorithm by Gooding.
  184.             // We change the threshold to maxIterations / 2.
  185.             if (iter >= (maxIterations / 2)) {
  186.                 withHalley = true;
  187.             }

  188.             // tentative position for R2
  189.             final Vector3D P2 = getPositionOnLoS2(lineOfSight1, rho1,
  190.                                                   lineOfSight3, rho3,
  191.                                                   T13, T12, nrev, direction);

  192.             if (P2 == null) {
  193.                 modifyIterate(lineOfSight1, lineOfSight2, lineOfSight3);
  194.             } else {
  195.                 //
  196.                 R2 = P2.getNorm();

  197.                 // compute current line of sight for (2) and the associated range
  198.                 final Vector3D C = P2.subtract(vObserverPosition2);
  199.                 rho2 = C.getNorm();

  200.                 final double R10 = R1;
  201.                 final double R30 = R3;

  202.                 // indicate how close we are to the direction of sight for measurement (2)
  203.                 // for convergence test
  204.                 final double CR = lineOfSight2.dotProduct(C);

  205.                 // construct a basis and the function f and g.
  206.                 // Specifically, f is the P-coordinate and g the N-coordinate.
  207.                 // They should be zero when line of sight 2 and current direction for 2 from O2 are aligned.
  208.                 final Vector3D u = lineOfSight2.crossProduct(C);
  209.                 final Vector3D P = (u.crossProduct(lineOfSight2)).normalize();
  210.                 final Vector3D EN = (lineOfSight2.crossProduct(P)).normalize();

  211.                 // if EN is zero we have a solution!
  212.                 final double ENR = EN.getNorm();
  213.                 if (ENR == 0.) {
  214.                     return true;
  215.                 }

  216.                 // Coordinate along 'F function'
  217.                 final double Fc = P.dotProduct(C);
  218.                 //Gc = EN.dotProduct(C);

  219.                 // Now get partials, by finite differences
  220.                 final double[] FD = new double[2];
  221.                 final double[] GD = new double[2];
  222.                 computeDerivatives(rho1, rho3,
  223.                                    R10, R30,
  224.                                    lineOfSight1, lineOfSight3,
  225.                                    P, EN,
  226.                                    Fc,
  227.                                    T13, T12,
  228.                                    withHalley,
  229.                                    nrev,
  230.                                    direction,
  231.                                    FD, GD);

  232.                 // terms of the Jacobian
  233.                 final double fr1 = FD[0];
  234.                 final double fr3 = FD[1];
  235.                 final double gr1 = GD[0];
  236.                 final double gr3 = GD[1];
  237.                 // determinant of the Jacobian matrix
  238.                 final double detj = fr1 * gr3 - fr3 * gr1;

  239.                 // compute the Newton step
  240.                 D3 = -gr3 * Fc / detj;
  241.                 D1 = gr1 * Fc / detj;

  242.                 // update current values of ranges
  243.                 rho1 = rho1 + D3;
  244.                 rho3 = rho3 + D1;

  245.                 // convergence tests
  246.                 final double den = FastMath.max(CR, R2);
  247.                 stoppingCriterion = Fc / den;
  248.             }

  249.             ++iter;
  250.         } // end while

  251.         return true;
  252.     }

  253.     /** Change the current iterate if Lambert's problem solver failed to find a solution.
  254.      *
  255.      * @param lineOfSight1 line of sight 1
  256.      * @param lineOfSight2 line of sight 2
  257.      * @param lineOfSight3 line of sight 3
  258.      */
  259.     private void modifyIterate(final Vector3D lineOfSight1,
  260.                                final Vector3D lineOfSight2,
  261.                                final Vector3D lineOfSight3) {
  262.         // This is a modifier proposed in the initial paper of Gooding.
  263.         // Try to avoid Lambert-fail, by common-perpendicular starters
  264.         final Vector3D R13 = vObserverPosition3.subtract(vObserverPosition1);
  265.         D1 = R13.dotProduct(lineOfSight1);
  266.         D3 = R13.dotProduct(lineOfSight3);
  267.         final double D2 = lineOfSight1.dotProduct(lineOfSight3);
  268.         final double D4 = 1. - D2 * D2;
  269.         rho1 = FastMath.max((D1 - D3 * D2) / D4, 0.);
  270.         rho3 = FastMath.max((D1 * D2 - D3) / D4, 0.);
  271.     }

  272.     /** Compute the derivatives by finite-differences for the range problem.
  273.      * Specifically, we are trying to solve the problem:
  274.      *      f(x, y) = 0
  275.      *      g(x, y) = 0
  276.      * So, in a Newton-Raphson process, we would need the derivatives:
  277.      *  fx, fy, gx, gy
  278.      * Enventually,
  279.      *    dx =-f*gy / D
  280.      *    dy = f*gx / D
  281.      * where D is the determinant of the Jacobian matrix.
  282.      *
  283.      *
  284.      * @param x    current range 1
  285.      * @param y    current range 3
  286.      * @param R10   current radius 1
  287.      * @param R30   current radius 3
  288.      * @param lineOfSight1  line of sight
  289.      * @param lineOfSight3  line of sight
  290.      * @param Pin   basis vector
  291.      * @param Ein   basis vector
  292.      * @param F     value of the f-function
  293.      * @param T13   time of flight 1->3, in seconds
  294.      * @param T12   time of flight 1->2, in seconds
  295.      * @param withHalley    use Halley iterative method
  296.      * @param nrev  number of revolutions
  297.      * @param direction direction of motion
  298.      * @param FD    derivatives of f wrt (rho1, rho3) by finite differences
  299.      * @param GD    derivatives of g wrt (rho1, rho3) by finite differences
  300.      */
  301.     private void computeDerivatives(final double x, final double y,
  302.                                     final double R10, final double R30,
  303.                                     final Vector3D lineOfSight1, final Vector3D lineOfSight3,
  304.                                     final Vector3D Pin,
  305.                                     final Vector3D Ein,
  306.                                     final double F,
  307.                                     final double T13, final double T12,
  308.                                     final boolean withHalley,
  309.                                     final int nrev,
  310.                                     final boolean direction,
  311.                                     final double[] FD, final double[] GD) {

  312.         final Vector3D P = Pin.normalize();
  313.         final Vector3D EN = Ein.normalize();

  314.         // Now get partials, by finite differences
  315.         // steps for the differentiation
  316.         final double dx = facFiniteDiff * x;
  317.         final double dy = facFiniteDiff * y;

  318.         final Vector3D Cm1 = getPositionOnLoS2 (lineOfSight1, x - dx,
  319.                                                 lineOfSight3, y,
  320.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  321.         final double Fm1 = P.dotProduct(Cm1);
  322.         final double Gm1 = EN.dotProduct(Cm1);

  323.         final Vector3D Cp1 = getPositionOnLoS2 (lineOfSight1, x + dx,
  324.                                                 lineOfSight3, y,
  325.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  326.         final double Fp1  = P.dotProduct(Cp1);
  327.         final double Gp1 = EN.dotProduct(Cp1);

  328.         // derivatives df/drho1 and dg/drho1
  329.         final double Fx = (Fp1 - Fm1) / (2 * dx);
  330.         final double Gx = (Gp1 - Gm1) / (2 * dx);

  331.         final Vector3D Cm3 = getPositionOnLoS2 (lineOfSight1, x,
  332.                                                 lineOfSight3, y - dy,
  333.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  334.         final double Fm3 = P.dotProduct(Cm3);
  335.         final double Gm3 = EN.dotProduct(Cm3);

  336.         final Vector3D Cp3 = getPositionOnLoS2 (lineOfSight1, x,
  337.                                                 lineOfSight3, y + dy,
  338.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  339.         final double Fp3 = P.dotProduct(Cp3);
  340.         final double Gp3 = EN.dotProduct(Cp3);

  341.         // derivatives df/drho3 and dg/drho3
  342.         final double Fy = (Fp3 - Fm3) / (2. * dy);
  343.         final double Gy = (Gp3 - Gm3) / (2. * dy);
  344.         final double detJac = Fx * Gy - Fy * Gx;

  345.         // Coefficients for the classical Newton-Raphson iterative method
  346.         FD[0] = Fx / detJac;
  347.         FD[1] = Fy / detJac;
  348.         GD[0] = Gx / detJac;
  349.         GD[1] = Gy / detJac;

  350.         // Modified Newton-Raphson process, with Halley's method to have cubic convergence.
  351.         // This requires computing second order derivatives.
  352.         if (withHalley) {
  353.             //
  354.             final double hrho1Sq = dx * dx;
  355.             final double hrho3Sq = dy * dy;

  356.             // Second order derivatives: d^2f / drho1^2 and d^2g / drho3^2
  357.             final double Fxx = (Fp1 + Fm1 - 2 * F) / hrho1Sq;
  358.             final double Gxx = (Gp1 + Gm1 - 2 * F) / hrho1Sq;
  359.             final double Fyy = (Fp3 + Fp3 - 2 * F) / hrho3Sq;
  360.             final double Gyy = (Gm3 + Gm3 - 2 * F) / hrho3Sq;

  361.             final Vector3D Cp13 = getPositionOnLoS2 (lineOfSight1, x + dx,
  362.                                                      lineOfSight3, y + dy,
  363.                                                      T13, T12, nrev, direction).subtract(vObserverPosition2);

  364.             // f function value at (x1+dx1, x3+dx3)
  365.             final double Fp13 = P.dotProduct(Cp13) - F;
  366.             // g function value at (x1+dx1, x3+dx3)
  367.             final double Gp13 = EN.dotProduct(Cp13);

  368.             final Vector3D Cm13 = getPositionOnLoS2 (lineOfSight1, x + dx,
  369.                                                      lineOfSight3, y + dy,
  370.                                                      T13, T12, nrev, direction).subtract(vObserverPosition2);

  371.             // f function value at (x1+dx1, x3+dx3)
  372.             final double Fm13 = P.dotProduct(Cm13) - F;
  373.             // g function value at (x1+dx1, x3+dx3)
  374.             final double Gm13 = EN.dotProduct(Cm13);

  375.             // Second order derivatives: d^2f / drho1drho3 and d^2g / drho1drho3
  376.             //double Fxy = Fp13 / (dx * dy) - (Fx / dy + Fy / dx) -
  377.             //                0.5 * (Fxx * dx / dy + Fyy * dy / dx);
  378.             //double Gxy = Gp13 / (dx * dy) - (Gx / dy + Gy / dx) -
  379.             //                0.5 * (Gxx * dx / dy + Gyy * dy / dx);
  380.             final double Fxy = (Fp13 + Fm13) / (2 * dx * dy) - 1.0 * (Fxx / 2 + Fyy / 2) - F / (dx * dy);
  381.             final double Gxy = (Gp13 + Gm13) / (2 * dx * dy) - 1.0 * (Gxx / 2 + Gyy / 2) - F / (dx * dy);

  382.             // delta Newton Raphson, 1st order step
  383.             final double dx3NR = -Gy * F / detJac;
  384.             final double dx1NR = Gx * F / detJac;

  385.             // terms of the Jacobian, considering the development, after linearization
  386.             // of the second order Taylor expansion around the Newton Raphson iterate:
  387.             // (fx + 1/2 * fxx * dx* + 1/2 * fxy * dy*) * dx
  388.             //      + (fy + 1/2 * fyy * dy* + 1/2 * fxy * dx*) * dy
  389.             // where: dx* and dy* would be the step of the Newton raphson process.
  390.             final double FxH = Fx + 0.5 * (Fxx * dx3NR + Fxy * dx1NR);
  391.             final double FyH = Fy + 0.5 * (Fxy * dx3NR + Fxx * dx1NR);
  392.             final double GxH = Gx + 0.5 * (Gxx * dx3NR + Gxy * dx1NR);
  393.             final double GyH = Gy + 0.5 * (Gxy * dx3NR + Fxy * dx1NR);

  394.             // New Halley's method "Jacobian"
  395.             FD[0] = FxH;
  396.             FD[1] = FyH;
  397.             GD[0] = GxH;
  398.             GD[1] = GyH;
  399.         }
  400.     }

  401.     /** Calculate the position along sight-line.
  402.      *
  403.      * @param E1 line of sight 1
  404.      * @param RO1 distance along E1
  405.      * @param E3 line of sight 3
  406.      * @param RO3 distance along E3
  407.      * @param T12   time of flight
  408.      * @param T13   time of flight
  409.      * @param nRev number of revolutions
  410.      * @param posigrade direction of motion
  411.      * @return (R2-O2)
  412.      */
  413.     private Vector3D getPositionOnLoS2(final Vector3D E1, final double RO1,
  414.                                        final Vector3D E3, final double RO3,
  415.                                        final double T13, final double T12,
  416.                                        final double nRev, final boolean posigrade) {
  417.         final Vector3D P1 = vObserverPosition1.add(E1.scalarMultiply(RO1));
  418.         R1 = P1.getNorm();

  419.         final Vector3D P3 = vObserverPosition3.add(E3.scalarMultiply(RO3));
  420.         R3 = P3.getNorm();

  421.         final Vector3D P13 = P1.crossProduct(P3);

  422.         // sweep angle
  423.         // (Fails only if either R1 or R2 is zero)
  424.         double TH = FastMath.atan2(P13.getNorm(), P1.dotProduct(P3));

  425.         // compute the number of revolutions
  426.         if (!posigrade) {
  427.             TH = FastMath.PI - TH;
  428.         }
  429.         TH = TH + nRev * FastMath.PI;

  430.         // Solve the Lambert's problem to get the velocities at endpoints
  431.         final double[] V1 = new double[2];
  432.         // work with non-dimensional units (MU=1)
  433.         final boolean exitflag = lambert.solveLambertPb(R1, R3, TH, T13, 0, V1);

  434.         if (exitflag) {
  435.             // basis vectors
  436.             final Vector3D Pn = P1.crossProduct(P3);
  437.             final Vector3D Pt = Pn.crossProduct(P1);

  438.             // tangential velocity norm
  439.             double RT = Pt.getNorm();
  440.             if (!posigrade) {
  441.                 RT = -RT;
  442.             }

  443.             // velocity vector at P1
  444.             final Vector3D Vel1 = new  Vector3D(V1[0] / R1, P1, V1[1] / RT, Pt);

  445.             // estimate the position at the second observation time
  446.             // propagate (P1, V1) during TAU + T12 to get (P2, V2)
  447.             final Vector3D P2 = propagatePV(P1, Vel1, T12);

  448.             return P2;
  449.         }

  450.         return null;
  451.     }

  452.     /** Propagate a solution (Kepler).
  453.      *
  454.      * @param P1  initial position vector
  455.      * @param V1  initial velocity vector
  456.      * @param tau propagation time
  457.      * @return final position vector
  458.      */
  459.     private Vector3D propagatePV(final Vector3D P1, final Vector3D V1, final double tau) {
  460.         final PVCoordinates pv1 = new PVCoordinates(P1, V1);
  461.         // create a Keplerian orbit. Assume MU = 1.
  462.         final KeplerianOrbit orbit = new KeplerianOrbit(pv1, frame, date1, 1.);
  463.         return orbit.shiftedBy(tau).getPVCoordinates().getPosition();
  464.     }

  465. }