FieldOrbit.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 org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.differentiation.Derivative;
  21. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  22. import org.hipparchus.linear.FieldDecompositionSolver;
  23. import org.hipparchus.linear.FieldLUDecomposition;
  24. import org.hipparchus.linear.FieldMatrix;
  25. import org.hipparchus.linear.MatrixUtils;
  26. import org.hipparchus.util.MathArrays;
  27. import org.orekit.errors.OrekitIllegalArgumentException;
  28. import org.orekit.errors.OrekitInternalError;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.frames.FieldStaticTransform;
  31. import org.orekit.frames.FieldTransform;
  32. import org.orekit.frames.Frame;
  33. import org.orekit.time.FieldAbsoluteDate;
  34. import org.orekit.time.FieldTimeShiftable;
  35. import org.orekit.time.FieldTimeStamped;
  36. import org.orekit.utils.FieldPVCoordinates;
  37. import org.orekit.utils.FieldPVCoordinatesProvider;
  38. import org.orekit.utils.TimeStampedFieldPVCoordinates;
  39. import org.orekit.utils.TimeStampedPVCoordinates;

  40. /**
  41.  * This class handles orbital parameters.

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

  66.     /** Frame in which are defined the orbital parameters. */
  67.     private final Frame frame;

  68.     /** Date of the orbital parameters. */
  69.     private final FieldAbsoluteDate<T> date;

  70.     /** Value of mu used to compute position and velocity (m³/s²). */
  71.     private final T mu;

  72.     /** Field-valued one.
  73.      * @since 12.0
  74.      * */
  75.     private final T one;

  76.     /** Field-valued zero.
  77.      * @since 12.0
  78.      * */
  79.     private final T zero;

  80.     /** Field used by this class.
  81.      * @since 12.0
  82.      */
  83.     private final Field<T> field;

  84.     /** Computed position.
  85.      * @since 12.0
  86.      */
  87.     private transient FieldVector3D<T> position;

  88.     /** Computed PVCoordinates. */
  89.     private transient TimeStampedFieldPVCoordinates<T> pvCoordinates;

  90.     /** Jacobian of the orbital parameters with mean angle with respect to the Cartesian coordinates. */
  91.     private transient T[][] jacobianMeanWrtCartesian;

  92.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with mean angle. */
  93.     private transient T[][] jacobianWrtParametersMean;

  94.     /** Jacobian of the orbital parameters with eccentric angle with respect to the Cartesian coordinates. */
  95.     private transient T[][] jacobianEccentricWrtCartesian;

  96.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with eccentric angle. */
  97.     private transient T[][] jacobianWrtParametersEccentric;

  98.     /** Jacobian of the orbital parameters with true angle with respect to the Cartesian coordinates. */
  99.     private transient T[][] jacobianTrueWrtCartesian;

  100.     /** Jacobian of the Cartesian coordinates with respect to the orbital parameters with true angle. */
  101.     private transient T[][] jacobianWrtParametersTrue;

  102.     /** Default constructor.
  103.      * Build a new instance with arbitrary default elements.
  104.      * @param frame the frame in which the parameters are defined
  105.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  106.      * @param date date of the orbital parameters
  107.      * @param mu central attraction coefficient (m^3/s^2)
  108.      * @exception IllegalArgumentException if frame is not a {@link
  109.      * Frame#isPseudoInertial pseudo-inertial frame}
  110.      */
  111.     protected FieldOrbit(final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  112.         throws IllegalArgumentException {
  113.         ensurePseudoInertialFrame(frame);
  114.         this.field = mu.getField();
  115.         this.zero = this.field.getZero();
  116.         this.one = this.field.getOne();
  117.         this.date                      = date;
  118.         this.mu                        = mu;
  119.         this.pvCoordinates             = null;
  120.         this.frame                     = frame;
  121.         jacobianMeanWrtCartesian       = null;
  122.         jacobianWrtParametersMean      = null;
  123.         jacobianEccentricWrtCartesian  = null;
  124.         jacobianWrtParametersEccentric = null;
  125.         jacobianTrueWrtCartesian       = null;
  126.         jacobianWrtParametersTrue      = null;
  127.     }

  128.     /** Set the orbit from Cartesian parameters.
  129.      *
  130.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  131.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  132.      * use {@code mu} and the position to compute the acceleration, including
  133.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  134.      *
  135.      * @param fieldPVCoordinates the position and velocity in the inertial frame
  136.      * @param frame the frame in which the {@link TimeStampedPVCoordinates} are defined
  137.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  138.      * @param mu central attraction coefficient (m^3/s^2)
  139.      * @exception IllegalArgumentException if frame is not a {@link
  140.      * Frame#isPseudoInertial pseudo-inertial frame}
  141.      */
  142.     protected FieldOrbit(final TimeStampedFieldPVCoordinates<T> fieldPVCoordinates, final Frame frame, final T mu)
  143.         throws IllegalArgumentException {
  144.         ensurePseudoInertialFrame(frame);
  145.         this.field = mu.getField();
  146.         this.zero = this.field.getZero();
  147.         this.one = this.field.getOne();
  148.         this.date = fieldPVCoordinates.getDate();
  149.         this.mu = mu;
  150.         if (fieldPVCoordinates.getAcceleration().getNormSq().getReal() == 0.0) {
  151.             // the acceleration was not provided,
  152.             // compute it from Newtonian attraction
  153.             final T r2 = fieldPVCoordinates.getPosition().getNormSq();
  154.             final T r3 = r2.multiply(r2.sqrt());
  155.             this.pvCoordinates = new TimeStampedFieldPVCoordinates<>(fieldPVCoordinates.getDate(),
  156.                                                                      fieldPVCoordinates.getPosition(),
  157.                                                                      fieldPVCoordinates.getVelocity(),
  158.                                                                      new FieldVector3D<>(r3.reciprocal().multiply(mu.negate()), fieldPVCoordinates.getPosition()));
  159.         } else {
  160.             this.pvCoordinates = fieldPVCoordinates;
  161.         }
  162.         this.frame = frame;
  163.     }

  164.     /** Check if Cartesian coordinates include non-Keplerian acceleration.
  165.      * @param pva Cartesian coordinates
  166.      * @param mu central attraction coefficient
  167.      * @param <T> type of the field elements
  168.      * @return true if Cartesian coordinates include non-Keplerian acceleration
  169.      */
  170.     protected static <T extends CalculusFieldElement<T>> boolean hasNonKeplerianAcceleration(final FieldPVCoordinates<T> pva, final T mu) {

  171.         if (mu.getField().getZero() instanceof Derivative<?>) {
  172.             return Orbit.hasNonKeplerianAcceleration(pva.toPVCoordinates(), mu.getReal()); // for performance

  173.         } else {
  174.             final FieldVector3D<T> a = pva.getAcceleration();
  175.             if (a == null) {
  176.                 return false;
  177.             }

  178.             final FieldVector3D<T> p = pva.getPosition();
  179.             final T r2 = p.getNormSq();

  180.             // Check if acceleration is relatively close to 0 compared to the Keplerian acceleration
  181.             final double tolerance = mu.getReal() * 1e-9;
  182.             final FieldVector3D<T> aTimesR2 = a.scalarMultiply(r2);
  183.             if (aTimesR2.getNorm().getReal() < tolerance) {
  184.                 return false;
  185.             }

  186.             if ((aTimesR2.add(p.normalize().scalarMultiply(mu))).getNorm().getReal() > tolerance) {
  187.                 // we have a relevant acceleration, we can compute derivatives
  188.                 return true;
  189.             } else {
  190.                 // the provided acceleration is either too small to be reliable (probably even 0), or NaN
  191.                 return false;
  192.             }
  193.         }

  194.     }

  195.     /** Returns true if and only if the orbit is elliptical i.e. has a non-negative semi-major axis.
  196.      * @return true if getA() is strictly greater than 0
  197.      * @since 12.0
  198.      */
  199.     public boolean isElliptical() {
  200.         return getA().getReal() > 0.;
  201.     }

  202.     /** Get the orbit type.
  203.      * @return orbit type
  204.      */
  205.     public abstract OrbitType getType();

  206.     /** Ensure the defining frame is a pseudo-inertial frame.
  207.      * @param frame frame to check
  208.      * @exception IllegalArgumentException if frame is not a {@link
  209.      * Frame#isPseudoInertial pseudo-inertial frame}
  210.      */
  211.     private static void ensurePseudoInertialFrame(final Frame frame)
  212.         throws IllegalArgumentException {
  213.         if (!frame.isPseudoInertial()) {
  214.             throw new OrekitIllegalArgumentException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME,
  215.                                                      frame.getName());
  216.         }
  217.     }

  218.     /** Get the frame in which the orbital parameters are defined.
  219.      * @return frame in which the orbital parameters are defined
  220.      */
  221.     public Frame getFrame() {
  222.         return frame;
  223.     }

  224.     /**Transforms the FieldOrbit instance into an Orbit instance.
  225.      * @return Orbit instance with same properties
  226.      */
  227.     public abstract Orbit toOrbit();

  228.     /** Get the semi-major axis.
  229.      * <p>Note that the semi-major axis is considered negative for hyperbolic orbits.</p>
  230.      * @return semi-major axis (m)
  231.      */
  232.     public abstract T getA();

  233.     /** Get the semi-major axis derivative.
  234.      * <p>Note that the semi-major axis is considered negative for hyperbolic orbits.</p>
  235.      * <p>
  236.      * If the orbit was created without derivatives, the value returned is null.
  237.      * </p>
  238.      * @return semi-major axis  derivative (m/s)
  239.      */
  240.     public abstract T getADot();

  241.     /** Get the first component of the equinoctial eccentricity vector.
  242.      * @return first component of the equinoctial eccentricity vector
  243.      */
  244.     public abstract T getEquinoctialEx();

  245.     /** Get the first component of the equinoctial eccentricity vector.
  246.      * <p>
  247.      * If the orbit was created without derivatives, the value returned is null.
  248.      * </p>
  249.      * @return first component of the equinoctial eccentricity vector
  250.      */
  251.     public abstract T getEquinoctialExDot();

  252.     /** Get the second component of the equinoctial eccentricity vector.
  253.      * @return second component of the equinoctial eccentricity vector
  254.      */
  255.     public abstract T getEquinoctialEy();

  256.     /** Get the second component of the equinoctial eccentricity vector.
  257.      * <p>
  258.      * If the orbit was created without derivatives, the value returned is null.
  259.      * </p>
  260.      * @return second component of the equinoctial eccentricity vector
  261.      */
  262.     public abstract T getEquinoctialEyDot();

  263.     /** Get the first component of the inclination vector.
  264.      * @return first component of the inclination vector
  265.      */
  266.     public abstract T getHx();

  267.     /** Get the first component of the inclination vector derivative.
  268.      * <p>
  269.      * If the orbit was created without derivatives, the value returned is null.
  270.      * </p>
  271.      * @return first component of the inclination vector derivative
  272.      */
  273.     public abstract T getHxDot();

  274.     /** Get the second component of the inclination vector.
  275.      * @return second component of the inclination vector
  276.      */
  277.     public abstract T getHy();

  278.     /** Get the second component of the inclination vector derivative.
  279.      * @return second component of the inclination vector derivative
  280.      */
  281.     public abstract T getHyDot();

  282.     /** Get the eccentric longitude argument.
  283.      * @return E + ω + Ω eccentric longitude argument (rad)
  284.      */
  285.     public abstract T getLE();

  286.     /** Get the eccentric longitude argument derivative.
  287.      * <p>
  288.      * If the orbit was created without derivatives, the value returned is null.
  289.      * </p>
  290.      * @return d(E + ω + Ω)/dt eccentric longitude argument derivative (rad/s)
  291.      */
  292.     public abstract T getLEDot();

  293.     /** Get the true longitude argument.
  294.      * @return v + ω + Ω true longitude argument (rad)
  295.      */
  296.     public abstract T getLv();

  297.     /** Get the true longitude argument derivative.
  298.      * <p>
  299.      * If the orbit was created without derivatives, the value returned is null.
  300.      * </p>
  301.      * @return d(v + ω + Ω)/dt true longitude argument derivative (rad/s)
  302.      */
  303.     public abstract T getLvDot();

  304.     /** Get the mean longitude argument.
  305.      * @return M + ω + Ω mean longitude argument (rad)
  306.      */
  307.     public abstract T getLM();

  308.     /** Get the mean longitude argument derivative.
  309.      * <p>
  310.      * If the orbit was created without derivatives, the value returned is null.
  311.      * </p>
  312.      * @return d(M + ω + Ω)/dt mean longitude argument derivative (rad/s)
  313.      */
  314.     public abstract T getLMDot();

  315.     // Additional orbital elements

  316.     /** Get the eccentricity.
  317.      * @return eccentricity
  318.      */
  319.     public abstract T getE();

  320.     /** Get the eccentricity derivative.
  321.      * <p>
  322.      * If the orbit was created without derivatives, the value returned is null.
  323.      * </p>
  324.      * @return eccentricity derivative
  325.      */
  326.     public abstract T getEDot();

  327.     /** Get the inclination.
  328.      * <p>
  329.      * If the orbit was created without derivatives, the value returned is null.
  330.      * </p>
  331.      * @return inclination (rad)
  332.      */
  333.     public abstract T getI();

  334.     /** Get the inclination derivative.
  335.      * @return inclination derivative (rad/s)
  336.      */
  337.     public abstract T getIDot();

  338.     /** Check if orbit includes derivatives.
  339.      * @return true if orbit includes derivatives
  340.      * @see #getADot()
  341.      * @see #getEquinoctialExDot()
  342.      * @see #getEquinoctialEyDot()
  343.      * @see #getHxDot()
  344.      * @see #getHyDot()
  345.      * @see #getLEDot()
  346.      * @see #getLvDot()
  347.      * @see #getLMDot()
  348.      * @see #getEDot()
  349.      * @see #getIDot()
  350.      * @since 9.0
  351.      */
  352.     public abstract boolean hasDerivatives();

  353.     /** Get the central attraction coefficient used for position and velocity conversions (m³/s²).
  354.      * @return central attraction coefficient used for position and velocity conversions (m³/s²)
  355.      */
  356.     public T getMu() {
  357.         return mu;
  358.     }

  359.     /** Get the Keplerian period.
  360.      * <p>The Keplerian period is computed directly from semi major axis
  361.      * and central acceleration constant.</p>
  362.      * @return Keplerian period in seconds, or positive infinity for hyperbolic orbits
  363.      */
  364.     public T getKeplerianPeriod() {
  365.         final T a = getA();
  366.         return isElliptical() ?
  367.                a.multiply(a.getPi().multiply(2.0)).multiply(a.divide(mu).sqrt()) :
  368.                zero.add(Double.POSITIVE_INFINITY);
  369.     }

  370.     /** Get the Keplerian mean motion.
  371.      * <p>The Keplerian mean motion is computed directly from semi major axis
  372.      * and central acceleration constant.</p>
  373.      * @return Keplerian mean motion in radians per second
  374.      */
  375.     public T getKeplerianMeanMotion() {
  376.         final T absA = getA().abs();
  377.         return absA.reciprocal().multiply(mu).sqrt().divide(absA);
  378.     }

  379.     /** Get the derivative of the mean anomaly with respect to the semi major axis.
  380.      * @return derivative of the mean anomaly with respect to the semi major axis
  381.      */
  382.     public T getMeanAnomalyDotWrtA() {
  383.         return getKeplerianMeanMotion().divide(getA()).multiply(-1.5);
  384.     }

  385.     /** Get the date of orbital parameters.
  386.      * @return date of the orbital parameters
  387.      */
  388.     public FieldAbsoluteDate<T> getDate() {
  389.         return date;
  390.     }

  391.     /** Get the {@link TimeStampedPVCoordinates} in a specified frame.
  392.      * @param outputFrame frame in which the position/velocity coordinates shall be computed
  393.      * @return FieldPVCoordinates in the specified output frame
  394.           * @see #getPVCoordinates()
  395.      */
  396.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final Frame outputFrame) {
  397.         if (pvCoordinates == null) {
  398.             pvCoordinates = initPVCoordinates();
  399.         }

  400.         // If output frame requested is the same as definition frame,
  401.         // PV coordinates are returned directly
  402.         if (outputFrame == frame) {
  403.             return pvCoordinates;
  404.         }

  405.         // Else, PV coordinates are transformed to output frame
  406.         final FieldTransform<T> t = frame.getTransformTo(outputFrame, date);
  407.         return t.transformPVCoordinates(pvCoordinates);
  408.     }

  409.     /** {@inheritDoc} */
  410.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> otherDate, final Frame otherFrame) {
  411.         return shiftedBy(otherDate.durationFrom(getDate())).getPVCoordinates(otherFrame);
  412.     }

  413.     /** {@inheritDoc} */
  414.     @Override
  415.     public FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> otherDate, final Frame otherFrame) {
  416.         return shiftedBy(otherDate.durationFrom(getDate())).getPosition(otherFrame);
  417.     }

  418.     /** Get the position in a specified frame.
  419.      * @param outputFrame frame in which the position coordinates shall be computed
  420.      * @return position in the specified output frame
  421.      * @see #getPosition()
  422.      * @since 12.0
  423.      */
  424.     public FieldVector3D<T> getPosition(final Frame outputFrame) {
  425.         if (position == null) {
  426.             position = initPosition();
  427.         }

  428.         // If output frame requested is the same as definition frame,
  429.         // Position vector is returned directly
  430.         if (outputFrame == frame) {
  431.             return position;
  432.         }

  433.         // Else, position vector is transformed to output frame
  434.         final FieldStaticTransform<T> t = frame.getStaticTransformTo(outputFrame, date);
  435.         return t.transformPosition(position);

  436.     }

  437.     /** Get the position in definition frame.
  438.      * @return position in the definition frame
  439.      * @see #getPVCoordinates()
  440.      * @since 12.0
  441.      */
  442.     public FieldVector3D<T> getPosition() {
  443.         if (position == null) {
  444.             position = initPosition();
  445.         }
  446.         return position;
  447.     }

  448.     /** Get the {@link TimeStampedPVCoordinates} in definition frame.
  449.      * @return FieldPVCoordinates in the definition frame
  450.      * @see #getPVCoordinates(Frame)
  451.      */
  452.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates() {
  453.         if (pvCoordinates == null) {
  454.             pvCoordinates = initPVCoordinates();
  455.             position      = pvCoordinates.getPosition();
  456.         }
  457.         return pvCoordinates;
  458.     }

  459.     /** Getter for Field-valued one.
  460.      * @return one
  461.      * */
  462.     protected T getOne() {
  463.         return one;
  464.     }

  465.     /** Getter for Field-valued zero.
  466.      * @return zero
  467.      * */
  468.     protected T getZero() {
  469.         return zero;
  470.     }

  471.     /** Getter for Field.
  472.      * @return CalculusField
  473.      * */
  474.     protected Field<T> getField() {
  475.         return field;
  476.     }

  477.     /** Compute the position coordinates from the canonical parameters.
  478.      * @return computed position coordinates
  479.      * @since 12.0
  480.      */
  481.     protected abstract FieldVector3D<T> initPosition();

  482.     /** Compute the position/velocity coordinates from the canonical parameters.
  483.      * @return computed position/velocity coordinates
  484.      */
  485.     protected abstract TimeStampedFieldPVCoordinates<T> initPVCoordinates();

  486.     /** Get a time-shifted orbit.
  487.      * <p>
  488.      * The orbit can be slightly shifted to close dates. This shift is based on
  489.      * a simple Keplerian model. It is <em>not</em> intended as a replacement
  490.      * for proper orbit and attitude propagation but should be sufficient for
  491.      * small time shifts or coarse accuracy.
  492.      * </p>
  493.      * @param dt time shift in seconds
  494.      * @return a new orbit, shifted with respect to the instance (which is immutable)
  495.      */
  496.     public abstract FieldOrbit<T> shiftedBy(T dt);

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

  508.         final T[][] cachedJacobian;
  509.         synchronized (this) {
  510.             switch (type) {
  511.                 case MEAN :
  512.                     if (jacobianMeanWrtCartesian == null) {
  513.                         // first call, we need to compute the Jacobian and cache it
  514.                         jacobianMeanWrtCartesian = computeJacobianMeanWrtCartesian();
  515.                     }
  516.                     cachedJacobian = jacobianMeanWrtCartesian;
  517.                     break;
  518.                 case ECCENTRIC :
  519.                     if (jacobianEccentricWrtCartesian == null) {
  520.                         // first call, we need to compute the Jacobian and cache it
  521.                         jacobianEccentricWrtCartesian = computeJacobianEccentricWrtCartesian();
  522.                     }
  523.                     cachedJacobian = jacobianEccentricWrtCartesian;
  524.                     break;
  525.                 case TRUE :
  526.                     if (jacobianTrueWrtCartesian == null) {
  527.                         // first call, we need to compute the Jacobian and cache it
  528.                         jacobianTrueWrtCartesian = computeJacobianTrueWrtCartesian();
  529.                     }
  530.                     cachedJacobian = jacobianTrueWrtCartesian;
  531.                     break;
  532.                 default :
  533.                     throw new OrekitInternalError(null);
  534.             }
  535.         }

  536.         // fill the user provided array
  537.         for (int i = 0; i < cachedJacobian.length; ++i) {
  538.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  539.         }

  540.     }

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

  552.         final T[][] cachedJacobian;
  553.         synchronized (this) {
  554.             switch (type) {
  555.                 case MEAN :
  556.                     if (jacobianWrtParametersMean == null) {
  557.                         // first call, we need to compute the Jacobian and cache it
  558.                         jacobianWrtParametersMean = createInverseJacobian(type);
  559.                     }
  560.                     cachedJacobian = jacobianWrtParametersMean;
  561.                     break;
  562.                 case ECCENTRIC :
  563.                     if (jacobianWrtParametersEccentric == null) {
  564.                         // first call, we need to compute the Jacobian and cache it
  565.                         jacobianWrtParametersEccentric = createInverseJacobian(type);
  566.                     }
  567.                     cachedJacobian = jacobianWrtParametersEccentric;
  568.                     break;
  569.                 case TRUE :
  570.                     if (jacobianWrtParametersTrue == null) {
  571.                         // first call, we need to compute the Jacobian and cache it
  572.                         jacobianWrtParametersTrue = createInverseJacobian(type);
  573.                     }
  574.                     cachedJacobian = jacobianWrtParametersTrue;
  575.                     break;
  576.                 default :
  577.                     throw new OrekitInternalError(null);
  578.             }
  579.         }

  580.         // fill the user-provided array
  581.         for (int i = 0; i < cachedJacobian.length; ++i) {
  582.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  583.         }

  584.     }

  585.     /** Create an inverse Jacobian.
  586.      * @param type type of the position angle to use
  587.      * @return inverse Jacobian
  588.      */
  589.     private T[][] createInverseJacobian(final PositionAngleType type) {

  590.         // get the direct Jacobian
  591.         final T[][] directJacobian = MathArrays.buildArray(getA().getField(), 6, 6);
  592.         getJacobianWrtCartesian(type, directJacobian);

  593.         // invert the direct Jacobian
  594.         final FieldMatrix<T> matrix = MatrixUtils.createFieldMatrix(directJacobian);
  595.         final FieldDecompositionSolver<T> solver = new FieldLUDecomposition<>(matrix).getSolver();
  596.         return solver.getInverse().getData();

  597.     }

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

  609.     /** Compute the Jacobian of the orbital parameters with eccentric angle with respect to the Cartesian parameters.
  610.      * <p>
  611.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  612.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  613.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  614.      * </p>
  615.      * @return 6x6 Jacobian matrix
  616.      * @see #computeJacobianMeanWrtCartesian()
  617.      * @see #computeJacobianTrueWrtCartesian()
  618.      */
  619.     protected abstract T[][] computeJacobianEccentricWrtCartesian();

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

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

  643.         /** Fill a Jacobian half row with a single vector.
  644.      * @param a coefficient of the vector
  645.      * @param v 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 void fillHalfRow(final T a, final FieldVector3D<T> v, final T[] row, final int j) {
  650.         row[j]     = a.multiply(v.getX());
  651.         row[j + 1] = a.multiply(v.getY());
  652.         row[j + 2] = a.multiply(v.getZ());
  653.     }

  654.     /** Fill a Jacobian half row with a linear combination of vectors.
  655.      * @param a1 coefficient of the first vector
  656.      * @param v1 first vector
  657.      * @param a2 coefficient of the second vector
  658.      * @param v2 second vector
  659.      * @param row Jacobian matrix row
  660.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  661.      */
  662.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  663.                                       final T[] row, final int j) {
  664.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX());
  665.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY());
  666.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ());

  667.     }

  668.     /** Fill a Jacobian half row with a linear combination of vectors.
  669.      * @param a1 coefficient of the first vector
  670.      * @param v1 first vector
  671.      * @param a2 coefficient of the second vector
  672.      * @param v2 second vector
  673.      * @param a3 coefficient of the third vector
  674.      * @param v3 third vector
  675.      * @param row Jacobian matrix row
  676.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  677.      */
  678.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1,
  679.                                final T a2, final FieldVector3D<T> v2,
  680.                                final T a3, final FieldVector3D<T> v3,
  681.                                       final T[] row, final int j) {
  682.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX());
  683.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY());
  684.         row[j + 2] = a1.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 void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  699.                                       final T a3, final FieldVector3D<T> v3, final T a4, final FieldVector3D<T> v4,
  700.                                       final T[] row, final int j) {
  701.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX());
  702.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY());
  703.         row[j + 2] = a1.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 void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  720.                                       final T a3, final FieldVector3D<T> v3, final T a4, final FieldVector3D<T> v4,
  721.                                       final T a5, final FieldVector3D<T> v5,
  722.                                       final T[] row, final int j) {
  723.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX()).add(a5.multiply(v5.getX()));
  724.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY()).add(a5.multiply(v5.getY()));
  725.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ(), a4, v4.getZ()).add(a5.multiply(v5.getZ()));

  726.     }

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

  750.     }


  751. }