Orbit.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.orbits;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.linear.DecompositionSolver;
  20. import org.hipparchus.linear.MatrixUtils;
  21. import org.hipparchus.linear.QRDecomposition;
  22. import org.hipparchus.linear.RealMatrix;
  23. import org.hipparchus.util.FastMath;
  24. import org.hipparchus.util.MathArrays;
  25. import org.orekit.errors.OrekitIllegalArgumentException;
  26. import org.orekit.errors.OrekitInternalError;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.frames.StaticTransform;
  30. import org.orekit.frames.Transform;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.time.TimeOffset;
  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>, PVCoordinatesProvider {

  62.     /** Absolute tolerance when checking if the rate of the position angle is Keplerian or not. */
  63.     protected static final double TOLERANCE_POSITION_ANGLE_RATE = 1e-15;

  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 Vector3D position;

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

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

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

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

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

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

  86.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with true angle. */
  87.     private 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.      * @since 9.0
  211.      */
  212.     public abstract double getADot();

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

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

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

  229.     /** Get the second component of the equinoctial eccentricity vector derivative.
  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 derivative
  234.      * @since 9.0
  235.      */
  236.     public abstract double getEquinoctialEyDot();

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

  241.     /** Get the first component of the inclination vector derivative.
  242.      * <p>
  243.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  244.      * </p>
  245.      * @return first component of the inclination vector derivative

  246.      * @since 9.0
  247.      */
  248.     public abstract double getHxDot();

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

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

  261.     /** Get the eccentric longitude argument.
  262.      * @return E + ω + Ω eccentric longitude argument (rad)
  263.      */
  264.     public abstract double getLE();

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

  273.     /** Get the true longitude argument.
  274.      * @return v + ω + Ω true longitude argument (rad)
  275.      */
  276.     public abstract double getLv();

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

  285.     /** Get the mean longitude argument.
  286.      * @return M + ω + Ω mean longitude argument (rad)
  287.      */
  288.     public abstract double getLM();

  289.     /** Get the mean longitude argument derivative.
  290.      * <p>
  291.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  292.      * </p>
  293.      * @return d(M + ω + Ω)/dt mean longitude argument derivative (rad/s)
  294.      * @since 9.0
  295.      */
  296.     public abstract double getLMDot();

  297.     // Additional orbital elements

  298.     /** Get the eccentricity.
  299.      * @return eccentricity
  300.      */
  301.     public abstract double getE();

  302.     /** Get the eccentricity derivative.
  303.      * <p>
  304.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  305.      * </p>
  306.      * @return eccentricity derivative
  307.      * @since 9.0
  308.      */
  309.     public abstract double getEDot();

  310.     /** Get the inclination.
  311.      * @return inclination (rad)
  312.      */
  313.     public abstract double getI();

  314.     /** Get the inclination derivative.
  315.      * <p>
  316.      * If the orbit was created without derivatives, the value returned is {@link Double#NaN}.
  317.      * </p>
  318.      * @return inclination derivative (rad/s)
  319.      * @since 9.0
  320.      */
  321.     public abstract double getIDot();

  322.     /** Check if orbit includes non-Keplerian rates.
  323.      * @return true if orbit includes non-Keplerian derivatives
  324.      * @see #getADot()
  325.      * @see #getEquinoctialExDot()
  326.      * @see #getEquinoctialEyDot()
  327.      * @see #getHxDot()
  328.      * @see #getHyDot()
  329.      * @see #getLEDot()
  330.      * @see #getLvDot()
  331.      * @see #getLMDot()
  332.      * @see #getEDot()
  333.      * @see #getIDot()
  334.      * @since 13.0
  335.      */
  336.     public boolean hasNonKeplerianAcceleration() {
  337.         return hasNonKeplerianAcceleration(getPVCoordinates(), getMu());
  338.     }

  339.     /** Get the central acceleration constant.
  340.      * @return central acceleration constant
  341.      */
  342.     public double getMu() {
  343.         return mu;
  344.     }

  345.     /** Get the Keplerian period.
  346.      * <p>The Keplerian period is computed directly from semi major axis
  347.      * and central acceleration constant.</p>
  348.      * @return Keplerian period in seconds, or positive infinity for hyperbolic orbits
  349.      */
  350.     public double getKeplerianPeriod() {
  351.         final double a = getA();
  352.         return isElliptical() ? 2.0 * FastMath.PI * a * FastMath.sqrt(a / mu) : Double.POSITIVE_INFINITY;
  353.     }

  354.     /** Get the Keplerian mean motion.
  355.      * <p>The Keplerian mean motion is computed directly from semi major axis
  356.      * and central acceleration constant.</p>
  357.      * @return Keplerian mean motion in radians per second
  358.      */
  359.     public double getKeplerianMeanMotion() {
  360.         final double absA = FastMath.abs(getA());
  361.         return FastMath.sqrt(mu / absA) / absA;
  362.     }

  363.     /** Get the derivative of the mean anomaly with respect to the semi major axis.
  364.      * @return derivative of the mean anomaly with respect to the semi major axis
  365.      */
  366.     public double getMeanAnomalyDotWrtA() {
  367.         return -1.5 * getKeplerianMeanMotion() / getA();
  368.     }

  369.     /** Get the date of orbital parameters.
  370.      * @return date of the orbital parameters
  371.      */
  372.     public AbsoluteDate getDate() {
  373.         return date;
  374.     }

  375.     /** Get the {@link TimeStampedPVCoordinates} in a specified frame.
  376.      * @param outputFrame frame in which the position/velocity coordinates shall be computed
  377.      * @return pvCoordinates in the specified output frame
  378.           * @see #getPVCoordinates()
  379.      */
  380.     public TimeStampedPVCoordinates getPVCoordinates(final Frame outputFrame) {
  381.         if (pvCoordinates == null) {
  382.             pvCoordinates = initPVCoordinates();
  383.         }

  384.         // If output frame requested is the same as definition frame,
  385.         // PV coordinates are returned directly
  386.         if (outputFrame == frame) {
  387.             return pvCoordinates;
  388.         }

  389.         // Else, PV coordinates are transformed to output frame
  390.         final Transform t = frame.getTransformTo(outputFrame, date);
  391.         return t.transformPVCoordinates(pvCoordinates);
  392.     }

  393.     /** {@inheritDoc} */
  394.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate otherDate, final Frame otherFrame) {
  395.         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(otherFrame);
  396.     }

  397.     /** {@inheritDoc} */
  398.     @Override
  399.     public Vector3D getPosition(final AbsoluteDate otherDate, final Frame otherFrame) {
  400.         return shiftedBy(otherDate.durationFrom(getDate())).getPosition(otherFrame);
  401.     }

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

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

  417.         // Else, position vector is transformed to output frame
  418.         final StaticTransform t = frame.getStaticTransformTo(outputFrame, date);
  419.         return t.transformPosition(position);

  420.     }

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

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

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

  448.     /** Compute the position/velocity coordinates from the canonical parameters.
  449.      * @return computed position/velocity coordinates
  450.      */
  451.     protected abstract TimeStampedPVCoordinates initPVCoordinates();

  452.     /**
  453.      * Create a new object representing the same physical orbital state, but attached to a different reference frame.
  454.      * If the new frame is not inertial, an exception will be thrown.
  455.      *
  456.      * @param inertialFrame reference frame of output orbit
  457.      * @return orbit with different frame
  458.      * @since 13.0
  459.      */
  460.     public abstract Orbit inFrame(Frame inertialFrame);

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

  475.     /** Get a time-shifted orbit.
  476.      * <p>
  477.      * The orbit can be slightly shifted to close dates. The shifting model is a
  478.      * Keplerian one if no derivatives are available in the orbit, or Keplerian
  479.      * plus quadratic effect of the non-Keplerian acceleration if derivatives are
  480.      * available. Shifting is <em>not</em> intended as a replacement for proper
  481.      * orbit propagation but should be sufficient for small time shifts or coarse
  482.      * accuracy.
  483.      * </p>
  484.      * @param dt time shift
  485.      * @return a new orbit, shifted with respect to the instance (which is immutable)
  486.      */
  487.     @Override
  488.     public abstract Orbit shiftedBy(TimeOffset dt);

  489.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  490.      * <p>
  491.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  492.      * respect to Cartesian coordinate j. This means each row corresponds to one orbital parameter
  493.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  494.      * </p>
  495.      * @param type type of the position angle to use
  496.      * @param jacobian placeholder 6x6 (or larger) matrix to be filled with the Jacobian, if matrix
  497.      * is larger than 6x6, only the 6x6 upper left corner will be modified
  498.      */
  499.     public void getJacobianWrtCartesian(final PositionAngleType type, final double[][] jacobian) {

  500.         final double[][] cachedJacobian;
  501.         synchronized (this) {
  502.             switch (type) {
  503.                 case MEAN :
  504.                     if (jacobianMeanWrtCartesian == null) {
  505.                         // first call, we need to compute the Jacobian and cache it
  506.                         jacobianMeanWrtCartesian = computeJacobianMeanWrtCartesian();
  507.                     }
  508.                     cachedJacobian = jacobianMeanWrtCartesian;
  509.                     break;
  510.                 case ECCENTRIC :
  511.                     if (jacobianEccentricWrtCartesian == null) {
  512.                         // first call, we need to compute the Jacobian and cache it
  513.                         jacobianEccentricWrtCartesian = computeJacobianEccentricWrtCartesian();
  514.                     }
  515.                     cachedJacobian = jacobianEccentricWrtCartesian;
  516.                     break;
  517.                 case TRUE :
  518.                     if (jacobianTrueWrtCartesian == null) {
  519.                         // first call, we need to compute the Jacobian and cache it
  520.                         jacobianTrueWrtCartesian = computeJacobianTrueWrtCartesian();
  521.                     }
  522.                     cachedJacobian = jacobianTrueWrtCartesian;
  523.                     break;
  524.                 default :
  525.                     throw new OrekitInternalError(null);
  526.             }
  527.         }

  528.         // fill the user provided array
  529.         for (int i = 0; i < cachedJacobian.length; ++i) {
  530.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  531.         }

  532.     }

  533.     /** Compute the Jacobian of the Cartesian parameters with respect to the orbital parameters.
  534.      * <p>
  535.      * Element {@code jacobian[i][j]} is the derivative of Cartesian coordinate i of the orbit with
  536.      * respect to orbital parameter j. This means each row corresponds to one Cartesian coordinate
  537.      * x, y, z, xdot, ydot, zdot whereas columns 0 to 5 correspond to the orbital parameters.
  538.      * </p>
  539.      * @param type type of the position angle to use
  540.      * @param jacobian placeholder 6x6 (or larger) matrix to be filled with the Jacobian, if matrix
  541.      * is larger than 6x6, only the 6x6 upper left corner will be modified
  542.      */
  543.     public void getJacobianWrtParameters(final PositionAngleType type, final double[][] jacobian) {

  544.         final double[][] cachedJacobian;
  545.         synchronized (this) {
  546.             switch (type) {
  547.                 case MEAN :
  548.                     if (jacobianWrtParametersMean == null) {
  549.                         // first call, we need to compute the Jacobian and cache it
  550.                         jacobianWrtParametersMean = createInverseJacobian(type);
  551.                     }
  552.                     cachedJacobian = jacobianWrtParametersMean;
  553.                     break;
  554.                 case ECCENTRIC :
  555.                     if (jacobianWrtParametersEccentric == null) {
  556.                         // first call, we need to compute the Jacobian and cache it
  557.                         jacobianWrtParametersEccentric = createInverseJacobian(type);
  558.                     }
  559.                     cachedJacobian = jacobianWrtParametersEccentric;
  560.                     break;
  561.                 case TRUE :
  562.                     if (jacobianWrtParametersTrue == null) {
  563.                         // first call, we need to compute the Jacobian and cache it
  564.                         jacobianWrtParametersTrue = createInverseJacobian(type);
  565.                     }
  566.                     cachedJacobian = jacobianWrtParametersTrue;
  567.                     break;
  568.                 default :
  569.                     throw new OrekitInternalError(null);
  570.             }
  571.         }

  572.         // fill the user-provided array
  573.         for (int i = 0; i < cachedJacobian.length; ++i) {
  574.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  575.         }

  576.     }

  577.     /** Create an inverse Jacobian.
  578.      * @param type type of the position angle to use
  579.      * @return inverse Jacobian
  580.      */
  581.     private double[][] createInverseJacobian(final PositionAngleType type) {

  582.         // get the direct Jacobian
  583.         final double[][] directJacobian = new double[6][6];
  584.         getJacobianWrtCartesian(type, directJacobian);

  585.         // invert the direct Jacobian
  586.         final RealMatrix matrix = MatrixUtils.createRealMatrix(directJacobian);
  587.         final DecompositionSolver solver = new QRDecomposition(matrix).getSolver();
  588.         return solver.getInverse().getData();

  589.     }

  590.     /** Compute the Jacobian of the orbital parameters with mean 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 #computeJacobianEccentricWrtCartesian()
  601.      * @see #computeJacobianTrueWrtCartesian()
  602.      */
  603.     protected abstract double[][] computeJacobianMeanWrtCartesian();

  604.     /** Compute the Jacobian of the orbital parameters with eccentric 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 #computeJacobianTrueWrtCartesian()
  616.      */
  617.     protected abstract double[][] computeJacobianEccentricWrtCartesian();

  618.     /** Compute the Jacobian of the orbital parameters with true angle with respect to the Cartesian parameters.
  619.      * <p>
  620.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  621.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  622.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  623.      * </p>
  624.      * <p>
  625.      * The array returned by this method will not be modified.
  626.      * </p>
  627.      * @return 6x6 Jacobian matrix
  628.      * @see #computeJacobianMeanWrtCartesian()
  629.      * @see #computeJacobianEccentricWrtCartesian()
  630.      */
  631.     protected abstract double[][] computeJacobianTrueWrtCartesian();

  632.     /** Add the contribution of the Keplerian motion to parameters derivatives
  633.      * <p>
  634.      * This method is used by integration-based propagators to evaluate the part of Keplerian
  635.      * motion to evolution of the orbital state.
  636.      * </p>
  637.      * @param type type of the position angle in the state
  638.      * @param gm attraction coefficient to use
  639.      * @param pDot array containing orbital state derivatives to update (the Keplerian
  640.      * part must be <em>added</em> to the array components, as the array may already
  641.      * contain some non-zero elements corresponding to non-Keplerian parts)
  642.      */
  643.     public abstract void addKeplerContribution(PositionAngleType type, double gm, double[] pDot);

  644.         /** Fill a Jacobian half row with a single vector.
  645.      * @param a coefficient of the vector
  646.      * @param v vector
  647.      * @param row Jacobian matrix row
  648.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  649.      */
  650.     protected static void fillHalfRow(final double a, final Vector3D v, final double[] row, final int j) {
  651.         row[j]     = a * v.getX();
  652.         row[j + 1] = a * v.getY();
  653.         row[j + 2] = a * v.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 row Jacobian matrix row
  661.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  662.      */
  663.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  664.                                       final double[] row, final int j) {
  665.         row[j]     = MathArrays.linearCombination(a1, v1.getX(), a2, v2.getX());
  666.         row[j + 1] = MathArrays.linearCombination(a1, v1.getY(), a2, v2.getY());
  667.         row[j + 2] = MathArrays.linearCombination(a1, v1.getZ(), a2, v2.getZ());
  668.     }

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

  686.     /** Fill a Jacobian half row with a linear combination of vectors.
  687.      * @param a1 coefficient of the first vector
  688.      * @param v1 first vector
  689.      * @param a2 coefficient of the second vector
  690.      * @param v2 second vector
  691.      * @param a3 coefficient of the third vector
  692.      * @param v3 third vector
  693.      * @param a4 coefficient of the fourth vector
  694.      * @param v4 fourth 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[] row, final int j) {
  701.         row[j]     = MathArrays.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX());
  702.         row[j + 1] = MathArrays.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY());
  703.         row[j + 2] = MathArrays.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ(), a4, v4.getZ());
  704.     }

  705.     /** Fill a Jacobian half row with a linear combination of vectors.
  706.      * @param a1 coefficient of the first vector
  707.      * @param v1 first vector
  708.      * @param a2 coefficient of the second vector
  709.      * @param v2 second vector
  710.      * @param a3 coefficient of the third vector
  711.      * @param v3 third vector
  712.      * @param a4 coefficient of the fourth vector
  713.      * @param v4 fourth vector
  714.      * @param a5 coefficient of the fifth vector
  715.      * @param v5 fifth vector
  716.      * @param row Jacobian matrix row
  717.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  718.      */
  719.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  720.                                       final double a3, final Vector3D v3, final double a4, final Vector3D v4,
  721.                                       final double a5, final Vector3D v5,
  722.                                       final double[] row, final int j) {
  723.         final double[] a = new double[] {
  724.             a1, a2, a3, a4, a5
  725.         };
  726.         row[j]     = MathArrays.linearCombination(a, new double[] {
  727.             v1.getX(), v2.getX(), v3.getX(), v4.getX(), v5.getX()
  728.         });
  729.         row[j + 1] = MathArrays.linearCombination(a, new double[] {
  730.             v1.getY(), v2.getY(), v3.getY(), v4.getY(), v5.getY()
  731.         });
  732.         row[j + 2] = MathArrays.linearCombination(a, new double[] {
  733.             v1.getZ(), v2.getZ(), v3.getZ(), v4.getZ(), v5.getZ()
  734.         });
  735.     }

  736.     /** Fill a Jacobian half row with a linear combination of vectors.
  737.      * @param a1 coefficient of the first vector
  738.      * @param v1 first vector
  739.      * @param a2 coefficient of the second vector
  740.      * @param v2 second vector
  741.      * @param a3 coefficient of the third vector
  742.      * @param v3 third vector
  743.      * @param a4 coefficient of the fourth vector
  744.      * @param v4 fourth vector
  745.      * @param a5 coefficient of the fifth vector
  746.      * @param v5 fifth vector
  747.      * @param a6 coefficient of the sixth vector
  748.      * @param v6 sixth vector
  749.      * @param row Jacobian matrix row
  750.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  751.      */
  752.     protected static void fillHalfRow(final double a1, final Vector3D v1, final double a2, final Vector3D v2,
  753.                                       final double a3, final Vector3D v3, final double a4, final Vector3D v4,
  754.                                       final double a5, final Vector3D v5, final double a6, final Vector3D v6,
  755.                                       final double[] row, final int j) {
  756.         final double[] a = new double[] {
  757.             a1, a2, a3, a4, a5, a6
  758.         };
  759.         row[j]     = MathArrays.linearCombination(a, new double[] {
  760.             v1.getX(), v2.getX(), v3.getX(), v4.getX(), v5.getX(), v6.getX()
  761.         });
  762.         row[j + 1] = MathArrays.linearCombination(a, new double[] {
  763.             v1.getY(), v2.getY(), v3.getY(), v4.getY(), v5.getY(), v6.getY()
  764.         });
  765.         row[j + 2] = MathArrays.linearCombination(a, new double[] {
  766.             v1.getZ(), v2.getZ(), v3.getZ(), v4.getZ(), v5.getZ(), v6.getZ()
  767.         });
  768.     }

  769. }