Orbit.java

  1. /* Copyright 2002-2023 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 java.io.Serializable;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.linear.DecompositionSolver;
  21. import org.hipparchus.linear.MatrixUtils;
  22. import org.hipparchus.linear.QRDecomposition;
  23. import org.hipparchus.linear.RealMatrix;
  24. import org.hipparchus.util.FastMath;
  25. import org.hipparchus.util.MathArrays;
  26. import org.orekit.errors.OrekitIllegalArgumentException;
  27. import org.orekit.errors.OrekitInternalError;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.frames.Frame;
  30. import org.orekit.frames.StaticTransform;
  31. import org.orekit.frames.Transform;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.TimeShiftable;
  34. import org.orekit.time.TimeStamped;
  35. import org.orekit.utils.PVCoordinates;
  36. import org.orekit.utils.PVCoordinatesProvider;
  37. import org.orekit.utils.TimeStampedPVCoordinates;

  38. /**
  39.  * This class handles orbital parameters.

  40.  * <p>
  41.  * For user convenience, both the Cartesian and the equinoctial elements
  42.  * are provided by this class, regardless of the canonical representation
  43.  * implemented in the derived class (which may be classical Keplerian
  44.  * elements for example).
  45.  * </p>
  46.  * <p>
  47.  * The parameters are defined in a frame specified by the user. It is important
  48.  * to make sure this frame is consistent: it probably is inertial and centered
  49.  * on the central body. This information is used for example by some
  50.  * force models.
  51.  * </p>
  52.  * <p>
  53.  * Instance of this class are guaranteed to be immutable.
  54.  * </p>
  55.  * @author Luc Maisonobe
  56.  * @author Guylaine Prat
  57.  * @author Fabien Maussion
  58.  * @author V&eacute;ronique Pommier-Maurussane
  59.  */
  60. public abstract class Orbit
  61.     implements TimeStamped, TimeShiftable<Orbit>, Serializable, PVCoordinatesProvider {

  62.     /** Serializable UID. */
  63.     private static final long serialVersionUID = 438733454597999578L;

  64.     /** Frame in which are defined the orbital parameters. */
  65.     private final Frame frame;

  66.     /** Date of the orbital parameters. */
  67.     private final AbsoluteDate date;

  68.     /** Value of mu used to compute position and velocity (m³/s²). */
  69.     private final double mu;

  70.     /** Computed position.
  71.      * @since 12.0
  72.      */
  73.     private transient Vector3D position;

  74.     /** Computed PVCoordinates. */
  75.     private transient TimeStampedPVCoordinates pvCoordinates;

  76.     /** Jacobian of the orbital parameters with mean angle with respect to the Cartesian coordinates. */
  77.     private transient double[][] jacobianMeanWrtCartesian;

  78.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with mean angle. */
  79.     private transient double[][] jacobianWrtParametersMean;

  80.     /** Jacobian of the orbital parameters with eccentric angle with respect to the Cartesian coordinates. */
  81.     private transient double[][] jacobianEccentricWrtCartesian;

  82.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with eccentric angle. */
  83.     private transient double[][] jacobianWrtParametersEccentric;

  84.     /** Jacobian of the orbital parameters with true angle with respect to the Cartesian coordinates. */
  85.     private transient double[][] jacobianTrueWrtCartesian;

  86.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with true angle. */
  87.     private transient double[][] jacobianWrtParametersTrue;

  88.     /** Default constructor.
  89.      * Build a new instance with arbitrary default elements.
  90.      * @param frame the frame in which the parameters are defined
  91.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  92.      * @param date date of the orbital parameters
  93.      * @param mu central attraction coefficient (m^3/s^2)
  94.      * @exception IllegalArgumentException if frame is not a {@link
  95.      * Frame#isPseudoInertial pseudo-inertial frame}
  96.      */
  97.     protected Orbit(final Frame frame, final AbsoluteDate date, final double mu)
  98.         throws IllegalArgumentException {
  99.         ensurePseudoInertialFrame(frame);
  100.         this.date                      = date;
  101.         this.mu                        = mu;
  102.         this.pvCoordinates             = null;
  103.         this.frame                     = frame;
  104.         jacobianMeanWrtCartesian       = null;
  105.         jacobianWrtParametersMean      = null;
  106.         jacobianEccentricWrtCartesian  = null;
  107.         jacobianWrtParametersEccentric = null;
  108.         jacobianTrueWrtCartesian       = null;
  109.         jacobianWrtParametersTrue      = null;
  110.     }

  111.     /** Set the orbit from Cartesian parameters.
  112.      *
  113.      * <p> The acceleration provided in {@code pvCoordinates} is accessible using
  114.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  115.      * use {@code mu} and the position to compute the acceleration, including
  116.      * {@link #shiftedBy(double)} and {@link #getPVCoordinates(AbsoluteDate, Frame)}.
  117.      *
  118.      * @param pvCoordinates the position and velocity in the inertial frame
  119.      * @param frame the frame in which the {@link TimeStampedPVCoordinates} are defined
  120.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  121.      * @param mu central attraction coefficient (m^3/s^2)
  122.      * @exception IllegalArgumentException if frame is not a {@link
  123.      * Frame#isPseudoInertial pseudo-inertial frame}
  124.      */
  125.     protected Orbit(final TimeStampedPVCoordinates pvCoordinates, final Frame frame, final double mu)
  126.         throws IllegalArgumentException {
  127.         ensurePseudoInertialFrame(frame);
  128.         this.date = pvCoordinates.getDate();
  129.         this.mu = mu;
  130.         if (pvCoordinates.getAcceleration().getNormSq() == 0) {
  131.             // the acceleration was not provided,
  132.             // compute it from Newtonian attraction
  133.             final double r2 = pvCoordinates.getPosition().getNormSq();
  134.             final double r3 = r2 * FastMath.sqrt(r2);
  135.             this.pvCoordinates = new TimeStampedPVCoordinates(pvCoordinates.getDate(),
  136.                                                               pvCoordinates.getPosition(),
  137.                                                               pvCoordinates.getVelocity(),
  138.                                                               new Vector3D(-mu / r3, pvCoordinates.getPosition()));
  139.         } else {
  140.             this.pvCoordinates = pvCoordinates;
  141.         }
  142.         this.frame = frame;
  143.     }

  144.     /** Check if Cartesian coordinates include non-Keplerian acceleration.
  145.      * @param pva Cartesian coordinates
  146.      * @param mu central attraction coefficient
  147.      * @return true if Cartesian coordinates include non-Keplerian acceleration
  148.      */
  149.     protected static boolean hasNonKeplerianAcceleration(final PVCoordinates pva, final double mu) {

  150.         final Vector3D p = pva.getPosition();
  151.         final double r2 = p.getNormSq();
  152.         final double r  = FastMath.sqrt(r2);
  153.         final Vector3D keplerianAcceleration = new Vector3D(-mu / (r * r2), p);

  154.         // Check if acceleration is null or relatively close to 0 compared to the keplerain acceleration
  155.         final Vector3D a = pva.getAcceleration();
  156.         if (a == null || a.getNorm() < 1e-9 * keplerianAcceleration.getNorm()) {
  157.             return false;
  158.         }

  159.         final Vector3D nonKeplerianAcceleration = a.subtract(keplerianAcceleration);

  160.         if ( nonKeplerianAcceleration.getNorm() > 1e-9 * keplerianAcceleration.getNorm()) {
  161.             // we have a relevant acceleration, we can compute derivatives
  162.             return true;
  163.         } else {
  164.             // the provided acceleration is either too small to be reliable (probably even 0), or NaN
  165.             return false;
  166.         }
  167.     }

  168.     /** Returns true if and only if the orbit is elliptical i.e. has a non-negative semi-major axis.
  169.      * @return true if getA() is strictly greater than 0
  170.      * @since 12.0
  171.      */
  172.     public boolean isElliptical() {
  173.         return getA() > 0.;
  174.     }

  175.     /** Get the orbit type.
  176.      * @return orbit type
  177.      */
  178.     public abstract OrbitType getType();

  179.     /** Ensure the defining frame is a pseudo-inertial frame.
  180.      * @param frame frame to check
  181.      * @exception IllegalArgumentException if frame is not a {@link
  182.      * Frame#isPseudoInertial pseudo-inertial frame}
  183.      */
  184.     private static void ensurePseudoInertialFrame(final Frame frame)
  185.         throws IllegalArgumentException {
  186.         if (!frame.isPseudoInertial()) {
  187.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
  188.                                                      frame.getName());
  189.         }
  190.     }

  191.     /** Get the frame in which the orbital parameters are defined.
  192.      * @return frame in which the orbital parameters are defined
  193.      */
  194.     public Frame getFrame() {
  195.         return frame;
  196.     }

  197.     /** Get the semi-major axis.
  198.      * <p>Note that the semi-major axis is considered negative for hyperbolic orbits.</p>
  199.      * @return semi-major axis (m)
  200.      */
  201.     public abstract double getA();

  202.     /** Get the semi-major axis derivative.
  203.      * <p>Note that the semi-major axis is considered negative for hyperbolic orbits.</p>
  204.      * <p>
  205.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  206.      * </p>
  207.      * @return semi-major axis  derivative (m/s)
  208.      * @see #hasDerivatives()
  209.      * @since 9.0
  210.      */
  211.     public abstract double getADot();

  212.     /** Get the first component of the equinoctial eccentricity vector derivative.
  213.      * @return first component of the equinoctial eccentricity vector derivative
  214.      */
  215.     public abstract double getEquinoctialEx();

  216.     /** Get the first component of the equinoctial eccentricity vector.
  217.      * <p>
  218.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  219.      * </p>
  220.      * @return first component of the equinoctial eccentricity vector
  221.      * @see #hasDerivatives()
  222.      * @since 9.0
  223.      */
  224.     public abstract double getEquinoctialExDot();

  225.     /** Get the second component of the equinoctial eccentricity vector derivative.
  226.      * @return second component of the equinoctial eccentricity vector derivative
  227.      */
  228.     public abstract double getEquinoctialEy();

  229.     /** Get the second component of the equinoctial eccentricity vector.
  230.      * <p>
  231.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  232.      * </p>
  233.      * @return second component of the equinoctial eccentricity vector
  234.      * @see #hasDerivatives()
  235.      * @since 9.0
  236.      */
  237.     public abstract double getEquinoctialEyDot();

  238.     /** Get the first component of the inclination vector.
  239.      * @return first component of the inclination vector
  240.      */
  241.     public abstract double getHx();

  242.     /** Get the first component of the inclination vector derivative.
  243.      * <p>
  244.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  245.      * </p>
  246.      * @return first component of the inclination vector derivative
  247.      * @see #hasDerivatives()
  248.      * @since 9.0
  249.      */
  250.     public abstract double getHxDot();

  251.     /** Get the second component of the inclination vector.
  252.      * @return second component of the inclination vector
  253.      */
  254.     public abstract double getHy();

  255.     /** Get the second component of the inclination vector derivative.
  256.      * <p>
  257.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  258.      * </p>
  259.      * @return second component of the inclination vector derivative
  260.      * @see #hasDerivatives()
  261.      * @since 9.0
  262.      */
  263.     public abstract double getHyDot();

  264.     /** Get the eccentric longitude argument.
  265.      * @return E + ω + Ω eccentric longitude argument (rad)
  266.      */
  267.     public abstract double getLE();

  268.     /** Get the eccentric longitude argument derivative.
  269.      * <p>
  270.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  271.      * </p>
  272.      * @return d(E + ω + Ω)/dt eccentric longitude argument derivative (rad/s)
  273.      * @see #hasDerivatives()
  274.      * @since 9.0
  275.      */
  276.     public abstract double getLEDot();

  277.     /** Get the true longitude argument.
  278.      * @return v + ω + Ω true longitude argument (rad)
  279.      */
  280.     public abstract double getLv();

  281.     /** Get the true longitude argument derivative.
  282.      * <p>
  283.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  284.      * </p>
  285.      * @return d(v + ω + Ω)/dt true longitude argument derivative (rad/s)
  286.      * @see #hasDerivatives()
  287.      * @since 9.0
  288.      */
  289.     public abstract double getLvDot();

  290.     /** Get the mean longitude argument.
  291.      * @return M + ω + Ω mean longitude argument (rad)
  292.      */
  293.     public abstract double getLM();

  294.     /** Get the mean longitude argument derivative.
  295.      * <p>
  296.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  297.      * </p>
  298.      * @return d(M + ω + Ω)/dt mean longitude argument derivative (rad/s)
  299.      * @see #hasDerivatives()
  300.      * @since 9.0
  301.      */
  302.     public abstract double getLMDot();

  303.     // Additional orbital elements

  304.     /** Get the eccentricity.
  305.      * @return eccentricity
  306.      */
  307.     public abstract double getE();

  308.     /** Get the eccentricity derivative.
  309.      * <p>
  310.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  311.      * </p>
  312.      * @return eccentricity derivative
  313.      * @see #hasDerivatives()
  314.      * @since 9.0
  315.      */
  316.     public abstract double getEDot();

  317.     /** Get the inclination.
  318.      * @return inclination (rad)
  319.      */
  320.     public abstract double getI();

  321.     /** Get the inclination derivative.
  322.      * <p>
  323.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  324.      * </p>
  325.      * @return inclination derivative (rad/s)
  326.      * @see #hasDerivatives()
  327.      * @since 9.0
  328.      */
  329.     public abstract double getIDot();

  330.     /** Check if orbit includes derivatives.
  331.      * @return true if orbit includes derivatives
  332.      * @see #getADot()
  333.      * @see #getEquinoctialExDot()
  334.      * @see #getEquinoctialEyDot()
  335.      * @see #getHxDot()
  336.      * @see #getHyDot()
  337.      * @see #getLEDot()
  338.      * @see #getLvDot()
  339.      * @see #getLMDot()
  340.      * @see #getEDot()
  341.      * @see #getIDot()
  342.      * @since 9.0
  343.      */
  344.     public boolean hasDerivatives() {
  345.         return !Double.isNaN(getADot());
  346.     }

  347.     /** Get the central acceleration constant.
  348.      * @return central acceleration constant
  349.      */
  350.     public double getMu() {
  351.         return mu;
  352.     }

  353.     /** Get the Keplerian period.
  354.      * <p>The Keplerian period is computed directly from semi major axis
  355.      * and central acceleration constant.</p>
  356.      * @return Keplerian period in seconds, or positive infinity for hyperbolic orbits
  357.      */
  358.     public double getKeplerianPeriod() {
  359.         final double a = getA();
  360.         return isElliptical() ? 2.0 * FastMath.PI * a * FastMath.sqrt(a / mu) : Double.POSITIVE_INFINITY;
  361.     }

  362.     /** Get the Keplerian mean motion.
  363.      * <p>The Keplerian mean motion is computed directly from semi major axis
  364.      * and central acceleration constant.</p>
  365.      * @return Keplerian mean motion in radians per second
  366.      */
  367.     public double getKeplerianMeanMotion() {
  368.         final double absA = FastMath.abs(getA());
  369.         return FastMath.sqrt(mu / absA) / absA;
  370.     }

  371.     /** Get the derivative of the mean anomaly with respect to the semi major axis.
  372.      * @return derivative of the mean anomaly with respect to the semi major axis
  373.      */
  374.     public double getMeanAnomalyDotWrtA() {
  375.         return -1.5 * getKeplerianMeanMotion() / getA();
  376.     }

  377.     /** Get the date of orbital parameters.
  378.      * @return date of the orbital parameters
  379.      */
  380.     public AbsoluteDate getDate() {
  381.         return date;
  382.     }

  383.     /** Get the {@link TimeStampedPVCoordinates} in a specified frame.
  384.      * @param outputFrame frame in which the position/velocity coordinates shall be computed
  385.      * @return pvCoordinates in the specified output frame
  386.           * @see #getPVCoordinates()
  387.      */
  388.     public TimeStampedPVCoordinates getPVCoordinates(final Frame outputFrame) {
  389.         if (pvCoordinates == null) {
  390.             pvCoordinates = initPVCoordinates();
  391.         }

  392.         // If output frame requested is the same as definition frame,
  393.         // PV coordinates are returned directly
  394.         if (outputFrame == frame) {
  395.             return pvCoordinates;
  396.         }

  397.         // Else, PV coordinates are transformed to output frame
  398.         final Transform t = frame.getTransformTo(outputFrame, date);
  399.         return t.transformPVCoordinates(pvCoordinates);
  400.     }

  401.     /** {@inheritDoc} */
  402.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate otherDate, final Frame otherFrame) {
  403.         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(otherFrame);
  404.     }

  405.     /** Get the position in a specified frame.
  406.      * @param outputFrame frame in which the position coordinates shall be computed
  407.      * @return position in the specified output frame
  408.      * @see #getPosition()
  409.      * @since 12.0
  410.      */
  411.     public Vector3D getPosition(final Frame outputFrame) {
  412.         if (position == null) {
  413.             position = initPosition();
  414.         }

  415.         // If output frame requested is the same as definition frame,
  416.         // Position vector is returned directly
  417.         if (outputFrame == frame) {
  418.             return position;
  419.         }

  420.         // Else, position vector is transformed to output frame
  421.         final StaticTransform t = frame.getStaticTransformTo(outputFrame, date);
  422.         return t.transformPosition(position);

  423.     }

  424.     /** Get the position in definition frame.
  425.      * @return position in the definition frame
  426.      * @see #getPVCoordinates()
  427.      * @since 12.0
  428.      */
  429.     public Vector3D getPosition() {
  430.         if (position == null) {
  431.             position = initPosition();
  432.         }
  433.         return position;
  434.     }

  435.     /** Get the {@link TimeStampedPVCoordinates} in definition frame.
  436.      * @return pvCoordinates in the definition frame
  437.      * @see #getPVCoordinates(Frame)
  438.      */
  439.     public TimeStampedPVCoordinates getPVCoordinates() {
  440.         if (pvCoordinates == null) {
  441.             pvCoordinates = initPVCoordinates();
  442.             position      = pvCoordinates.getPosition();
  443.         }
  444.         return pvCoordinates;
  445.     }

  446.     /** Compute the position coordinates from the canonical parameters.
  447.      * @return computed position coordinates
  448.      * @since 12.0
  449.      */
  450.     protected abstract Vector3D initPosition();

  451.     /** Compute the position/velocity coordinates from the canonical parameters.
  452.      * @return computed position/velocity coordinates
  453.      */
  454.     protected abstract TimeStampedPVCoordinates initPVCoordinates();

  455.     /** Get a time-shifted orbit.
  456.      * <p>
  457.      * The orbit can be slightly shifted to close dates. The shifting model is a
  458.      * Keplerian one if no derivatives are available in the orbit, or Keplerian
  459.      * plus quadratic effect of the non-Keplerian acceleration if derivatives are
  460.      * available. Shifting is <em>not</em> intended as a replacement for proper
  461.      * orbit propagation but should be sufficient for small time shifts or coarse
  462.      * accuracy.
  463.      * </p>
  464.      * @param dt time shift in seconds
  465.      * @return a new orbit, shifted with respect to the instance (which is immutable)
  466.      */
  467.     public abstract Orbit shiftedBy(double dt);

  468.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  469.      * <p>
  470.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  471.      * respect to Cartesian coordinate j. This means each row corresponds to one orbital parameter
  472.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  473.      * </p>
  474.      * @param type type of the position angle to use
  475.      * @param jacobian placeholder 6x6 (or larger) matrix to be filled with the Jacobian, if matrix
  476.      * is larger than 6x6, only the 6x6 upper left corner will be modified
  477.      */
  478.     public void getJacobianWrtCartesian(final PositionAngleType type, final double[][] jacobian) {

  479.         final double[][] cachedJacobian;
  480.         synchronized (this) {
  481.             switch (type) {
  482.                 case MEAN :
  483.                     if (jacobianMeanWrtCartesian == null) {
  484.                         // first call, we need to compute the Jacobian and cache it
  485.                         jacobianMeanWrtCartesian = computeJacobianMeanWrtCartesian();
  486.                     }
  487.                     cachedJacobian = jacobianMeanWrtCartesian;
  488.                     break;
  489.                 case ECCENTRIC :
  490.                     if (jacobianEccentricWrtCartesian == null) {
  491.                         // first call, we need to compute the Jacobian and cache it
  492.                         jacobianEccentricWrtCartesian = computeJacobianEccentricWrtCartesian();
  493.                     }
  494.                     cachedJacobian = jacobianEccentricWrtCartesian;
  495.                     break;
  496.                 case TRUE :
  497.                     if (jacobianTrueWrtCartesian == null) {
  498.                         // first call, we need to compute the Jacobian and cache it
  499.                         jacobianTrueWrtCartesian = computeJacobianTrueWrtCartesian();
  500.                     }
  501.                     cachedJacobian = jacobianTrueWrtCartesian;
  502.                     break;
  503.                 default :
  504.                     throw new OrekitInternalError(null);
  505.             }
  506.         }

  507.         // fill the user provided array
  508.         for (int i = 0; i < cachedJacobian.length; ++i) {
  509.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  510.         }

  511.     }

  512.     /** Compute the Jacobian of the Cartesian parameters with respect to the orbital parameters.
  513.      * <p>
  514.      * Element {@code jacobian[i][j]} is the derivative of Cartesian coordinate i of the orbit with
  515.      * respect to orbital parameter j. This means each row corresponds to one Cartesian coordinate
  516.      * x, y, z, xdot, ydot, zdot whereas columns 0 to 5 correspond to the orbital parameters.
  517.      * </p>
  518.      * @param type type of the position angle to use
  519.      * @param jacobian placeholder 6x6 (or larger) matrix to be filled with the Jacobian, if matrix
  520.      * is larger than 6x6, only the 6x6 upper left corner will be modified
  521.      */
  522.     public void getJacobianWrtParameters(final PositionAngleType type, final double[][] jacobian) {

  523.         final double[][] cachedJacobian;
  524.         synchronized (this) {
  525.             switch (type) {
  526.                 case MEAN :
  527.                     if (jacobianWrtParametersMean == null) {
  528.                         // first call, we need to compute the Jacobian and cache it
  529.                         jacobianWrtParametersMean = createInverseJacobian(type);
  530.                     }
  531.                     cachedJacobian = jacobianWrtParametersMean;
  532.                     break;
  533.                 case ECCENTRIC :
  534.                     if (jacobianWrtParametersEccentric == null) {
  535.                         // first call, we need to compute the Jacobian and cache it
  536.                         jacobianWrtParametersEccentric = createInverseJacobian(type);
  537.                     }
  538.                     cachedJacobian = jacobianWrtParametersEccentric;
  539.                     break;
  540.                 case TRUE :
  541.                     if (jacobianWrtParametersTrue == null) {
  542.                         // first call, we need to compute the Jacobian and cache it
  543.                         jacobianWrtParametersTrue = createInverseJacobian(type);
  544.                     }
  545.                     cachedJacobian = jacobianWrtParametersTrue;
  546.                     break;
  547.                 default :
  548.                     throw new OrekitInternalError(null);
  549.             }
  550.         }

  551.         // fill the user-provided array
  552.         for (int i = 0; i < cachedJacobian.length; ++i) {
  553.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  554.         }

  555.     }

  556.     /** Create an inverse Jacobian.
  557.      * @param type type of the position angle to use
  558.      * @return inverse Jacobian
  559.      */
  560.     private double[][] createInverseJacobian(final PositionAngleType type) {

  561.         // get the direct Jacobian
  562.         final double[][] directJacobian = new double[6][6];
  563.         getJacobianWrtCartesian(type, directJacobian);

  564.         // invert the direct Jacobian
  565.         final RealMatrix matrix = MatrixUtils.createRealMatrix(directJacobian);
  566.         final DecompositionSolver solver = new QRDecomposition(matrix).getSolver();
  567.         return solver.getInverse().getData();

  568.     }

  569.     /** Compute the Jacobian of the orbital parameters with mean angle with respect to the Cartesian parameters.
  570.      * <p>
  571.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  572.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  573.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  574.      * </p>
  575.      * <p>
  576.      * The array returned by this method will not be modified.
  577.      * </p>
  578.      * @return 6x6 Jacobian matrix
  579.      * @see #computeJacobianEccentricWrtCartesian()
  580.      * @see #computeJacobianTrueWrtCartesian()
  581.      */
  582.     protected abstract double[][] computeJacobianMeanWrtCartesian();

  583.     /** Compute the Jacobian of the orbital parameters with eccentric angle with respect to the Cartesian parameters.
  584.      * <p>
  585.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  586.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  587.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  588.      * </p>
  589.      * <p>
  590.      * The array returned by this method will not be modified.
  591.      * </p>
  592.      * @return 6x6 Jacobian matrix
  593.      * @see #computeJacobianMeanWrtCartesian()
  594.      * @see #computeJacobianTrueWrtCartesian()
  595.      */
  596.     protected abstract double[][] computeJacobianEccentricWrtCartesian();

  597.     /** Compute the Jacobian of the orbital parameters with true angle with respect to the Cartesian parameters.
  598.      * <p>
  599.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  600.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  601.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  602.      * </p>
  603.      * <p>
  604.      * The array returned by this method will not be modified.
  605.      * </p>
  606.      * @return 6x6 Jacobian matrix
  607.      * @see #computeJacobianMeanWrtCartesian()
  608.      * @see #computeJacobianEccentricWrtCartesian()
  609.      */
  610.     protected abstract double[][] computeJacobianTrueWrtCartesian();

  611.     /** Add the contribution of the Keplerian motion to parameters derivatives
  612.      * <p>
  613.      * This method is used by integration-based propagators to evaluate the part of Keplerian
  614.      * motion to evolution of the orbital state.
  615.      * </p>
  616.      * @param type type of the position angle in the state
  617.      * @param gm attraction coefficient to use
  618.      * @param pDot array containing orbital state derivatives to update (the Keplerian
  619.      * part must be <em>added</em> to the array components, as the array may already
  620.      * contain some non-zero elements corresponding to non-Keplerian parts)
  621.      */
  622.     public abstract void addKeplerContribution(PositionAngleType type, double gm, double[] pDot);

  623.         /** Fill a Jacobian half row with a single vector.
  624.      * @param a coefficient of the vector
  625.      * @param v vector
  626.      * @param row Jacobian matrix row
  627.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  628.      */
  629.     protected static void fillHalfRow(final double a, final Vector3D v, final double[] row, final int j) {
  630.         row[j]     = a * v.getX();
  631.         row[j + 1] = a * v.getY();
  632.         row[j + 2] = a * v.getZ();
  633.     }

  634.     /** Fill a Jacobian half row with a linear combination of vectors.
  635.      * @param a1 coefficient of the first vector
  636.      * @param v1 first vector
  637.      * @param a2 coefficient of the second vector
  638.      * @param v2 second vector
  639.      * @param row Jacobian matrix row
  640.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  641.      */
  642.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  643.                                       final double[] row, final int j) {
  644.         row[j]     = MathArrays.linearCombination(a1, v1.getX(), a2, v2.getX());
  645.         row[j + 1] = MathArrays.linearCombination(a1, v1.getY(), a2, v2.getY());
  646.         row[j + 2] = MathArrays.linearCombination(a1, v1.getZ(), a2, v2.getZ());
  647.     }

  648.     /** Fill a Jacobian half row with a linear combination of vectors.
  649.      * @param a1 coefficient of the first vector
  650.      * @param v1 first vector
  651.      * @param a2 coefficient of the second vector
  652.      * @param v2 second vector
  653.      * @param a3 coefficient of the third vector
  654.      * @param v3 third vector
  655.      * @param row Jacobian matrix row
  656.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  657.      */
  658.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  659.                                       final double a3, final Vector3D v3,
  660.                                       final double[] row, final int j) {
  661.         row[j]     = MathArrays.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX());
  662.         row[j + 1] = MathArrays.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY());
  663.         row[j + 2] = MathArrays.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ());
  664.     }

  665.     /** Fill a Jacobian half row with a linear combination of vectors.
  666.      * @param a1 coefficient of the first vector
  667.      * @param v1 first vector
  668.      * @param a2 coefficient of the second vector
  669.      * @param v2 second vector
  670.      * @param a3 coefficient of the third vector
  671.      * @param v3 third vector
  672.      * @param a4 coefficient of the fourth vector
  673.      * @param v4 fourth vector
  674.      * @param row Jacobian matrix row
  675.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  676.      */
  677.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  678.                                       final double a3, final Vector3D v3, final double a4, final Vector3D v4,
  679.                                       final double[] row, final int j) {
  680.         row[j]     = MathArrays.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX());
  681.         row[j + 1] = MathArrays.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY());
  682.         row[j + 2] = MathArrays.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ(), a4, v4.getZ());
  683.     }

  684.     /** Fill a Jacobian half row with a linear combination of vectors.
  685.      * @param a1 coefficient of the first vector
  686.      * @param v1 first vector
  687.      * @param a2 coefficient of the second vector
  688.      * @param v2 second vector
  689.      * @param a3 coefficient of the third vector
  690.      * @param v3 third vector
  691.      * @param a4 coefficient of the fourth vector
  692.      * @param v4 fourth vector
  693.      * @param a5 coefficient of the fifth vector
  694.      * @param v5 fifth vector
  695.      * @param row Jacobian matrix row
  696.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  697.      */
  698.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  699.                                       final double a3, final Vector3D v3, final double a4, final Vector3D v4,
  700.                                       final double a5, final Vector3D v5,
  701.                                       final double[] row, final int j) {
  702.         final double[] a = new double[] {
  703.             a1, a2, a3, a4, a5
  704.         };
  705.         row[j]     = MathArrays.linearCombination(a, new double[] {
  706.             v1.getX(), v2.getX(), v3.getX(), v4.getX(), v5.getX()
  707.         });
  708.         row[j + 1] = MathArrays.linearCombination(a, new double[] {
  709.             v1.getY(), v2.getY(), v3.getY(), v4.getY(), v5.getY()
  710.         });
  711.         row[j + 2] = MathArrays.linearCombination(a, new double[] {
  712.             v1.getZ(), v2.getZ(), v3.getZ(), v4.getZ(), v5.getZ()
  713.         });
  714.     }

  715.     /** Fill a Jacobian half row with a linear combination of vectors.
  716.      * @param a1 coefficient of the first vector
  717.      * @param v1 first vector
  718.      * @param a2 coefficient of the second vector
  719.      * @param v2 second vector
  720.      * @param a3 coefficient of the third vector
  721.      * @param v3 third vector
  722.      * @param a4 coefficient of the fourth vector
  723.      * @param v4 fourth vector
  724.      * @param a5 coefficient of the fifth vector
  725.      * @param v5 fifth vector
  726.      * @param a6 coefficient of the sixth vector
  727.      * @param v6 sixth vector
  728.      * @param row Jacobian matrix row
  729.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  730.      */
  731.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  732.                                       final double a3, final Vector3D v3, final double a4, final Vector3D v4,
  733.                                       final double a5, final Vector3D v5, final double a6, final Vector3D v6,
  734.                                       final double[] row, final int j) {
  735.         final double[] a = new double[] {
  736.             a1, a2, a3, a4, a5, a6
  737.         };
  738.         row[j]     = MathArrays.linearCombination(a, new double[] {
  739.             v1.getX(), v2.getX(), v3.getX(), v4.getX(), v5.getX(), v6.getX()
  740.         });
  741.         row[j + 1] = MathArrays.linearCombination(a, new double[] {
  742.             v1.getY(), v2.getY(), v3.getY(), v4.getY(), v5.getY(), v6.getY()
  743.         });
  744.         row[j + 2] = MathArrays.linearCombination(a, new double[] {
  745.             v1.getZ(), v2.getZ(), v3.getZ(), v4.getZ(), v5.getZ(), v6.getZ()
  746.         });
  747.     }

  748. }