Orbit.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 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 a = pva.getAcceleration();
  151.         if (a == null) {
  152.             return false;
  153.         }

  154.         final Vector3D p = pva.getPosition();
  155.         final double r2 = p.getNormSq();

  156.         // Check if acceleration is relatively close to 0 compared to the Keplerian acceleration
  157.         final double tolerance = mu * 1e-9;
  158.         final Vector3D aTimesR2 = a.scalarMultiply(r2);
  159.         if (aTimesR2.getNorm() < tolerance) {
  160.             return false;
  161.         }

  162.         if ((aTimesR2.add(p.normalize().scalarMultiply(mu))).getNorm() > tolerance) {
  163.             // we have a relevant acceleration, we can compute derivatives
  164.             return true;
  165.         } else {
  166.             // the provided acceleration is either too small to be reliable (probably even 0), or NaN
  167.             return false;
  168.         }
  169.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  305.     // Additional orbital elements

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  407.     /** {@inheritDoc} */
  408.     @Override
  409.     public Vector3D getPosition(final AbsoluteDate otherDate, final Frame otherFrame) {
  410.         return shiftedBy(otherDate.durationFrom(getDate())).getPosition(otherFrame);
  411.     }

  412.     /** Get the position in a specified frame.
  413.      * @param outputFrame frame in which the position coordinates shall be computed
  414.      * @return position in the specified output frame
  415.      * @see #getPosition()
  416.      * @since 12.0
  417.      */
  418.     public Vector3D getPosition(final Frame outputFrame) {
  419.         if (position == null) {
  420.             position = initPosition();
  421.         }

  422.         // If output frame requested is the same as definition frame,
  423.         // Position vector is returned directly
  424.         if (outputFrame == frame) {
  425.             return position;
  426.         }

  427.         // Else, position vector is transformed to output frame
  428.         final StaticTransform t = frame.getStaticTransformTo(outputFrame, date);
  429.         return t.transformPosition(position);

  430.     }

  431.     /** Get the position in definition frame.
  432.      * @return position in the definition frame
  433.      * @see #getPVCoordinates()
  434.      * @since 12.0
  435.      */
  436.     public Vector3D getPosition() {
  437.         if (position == null) {
  438.             position = initPosition();
  439.         }
  440.         return position;
  441.     }

  442.     /** Get the {@link TimeStampedPVCoordinates} in definition frame.
  443.      * @return pvCoordinates in the definition frame
  444.      * @see #getPVCoordinates(Frame)
  445.      */
  446.     public TimeStampedPVCoordinates getPVCoordinates() {
  447.         if (pvCoordinates == null) {
  448.             pvCoordinates = initPVCoordinates();
  449.             position      = pvCoordinates.getPosition();
  450.         }
  451.         return pvCoordinates;
  452.     }

  453.     /** Compute the position coordinates from the canonical parameters.
  454.      * @return computed position coordinates
  455.      * @since 12.0
  456.      */
  457.     protected abstract Vector3D initPosition();

  458.     /** Compute the position/velocity coordinates from the canonical parameters.
  459.      * @return computed position/velocity coordinates
  460.      */
  461.     protected abstract TimeStampedPVCoordinates initPVCoordinates();

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

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

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

  514.         // fill the user provided array
  515.         for (int i = 0; i < cachedJacobian.length; ++i) {
  516.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  517.         }

  518.     }

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

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

  558.         // fill the user-provided array
  559.         for (int i = 0; i < cachedJacobian.length; ++i) {
  560.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  561.         }

  562.     }

  563.     /** Create an inverse Jacobian.
  564.      * @param type type of the position angle to use
  565.      * @return inverse Jacobian
  566.      */
  567.     private double[][] createInverseJacobian(final PositionAngleType type) {

  568.         // get the direct Jacobian
  569.         final double[][] directJacobian = new double[6][6];
  570.         getJacobianWrtCartesian(type, directJacobian);

  571.         // invert the direct Jacobian
  572.         final RealMatrix matrix = MatrixUtils.createRealMatrix(directJacobian);
  573.         final DecompositionSolver solver = new QRDecomposition(matrix).getSolver();
  574.         return solver.getInverse().getData();

  575.     }

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

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

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

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

  630.         /** Fill a Jacobian half row with a single vector.
  631.      * @param a coefficient of the vector
  632.      * @param v vector
  633.      * @param row Jacobian matrix row
  634.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  635.      */
  636.     protected static void fillHalfRow(final double a, final Vector3D v, final double[] row, final int j) {
  637.         row[j]     = a * v.getX();
  638.         row[j + 1] = a * v.getY();
  639.         row[j + 2] = a * v.getZ();
  640.     }

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

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

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

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

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

  755. }