FieldOrbit.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.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.  * @see Orbit
  63.  * @param <T> type of the field elements
  64.  */
  65. public abstract class FieldOrbit<T extends CalculusFieldElement<T>>
  66.     implements FieldPVCoordinatesProvider<T>, FieldTimeStamped<T>, FieldTimeShiftable<FieldOrbit<T>, T> {

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

  69.     /** Frame in which are defined the orbital parameters. */
  70.     private final Frame frame;

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

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

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

  79.     /** Field-valued zero.
  80.      * @since 12.0
  81.      * */
  82.     private final T zero;

  83.     /** Field used by this class.
  84.      * @since 12.0
  85.      */
  86.     private final Field<T> field;

  87.     /** Computed position.
  88.      * @since 12.0
  89.      */
  90.     private FieldVector3D<T> position;

  91.     /** Computed PVCoordinates. */
  92.     private TimeStampedFieldPVCoordinates<T> pvCoordinates;

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

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

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

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

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

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

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

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

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

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

  176.         } else {
  177.             final FieldVector3D<T> a = pva.getAcceleration();
  178.             if (a == null) {
  179.                 return false;
  180.             }

  181.             final FieldVector3D<T> p = pva.getPosition();
  182.             final T r2 = p.getNormSq();

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

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

  197.     }

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

  205.     /** Get the orbit type.
  206.      * @return orbit type
  207.      */
  208.     public abstract OrbitType getType();

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

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

  227.     /**Transforms the FieldOrbit instance into an Orbit instance.
  228.      * @return Orbit instance with same properties
  229.      */
  230.     public abstract Orbit toOrbit();

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

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

  244.     /** Get the first component of the equinoctial eccentricity vector.
  245.      * @return first component of the equinoctial eccentricity vector
  246.      */
  247.     public abstract T getEquinoctialEx();

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

  255.     /** Get the second component of the equinoctial eccentricity vector.
  256.      * @return second component of the equinoctial eccentricity vector
  257.      */
  258.     public abstract T getEquinoctialEy();

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

  266.     /** Get the first component of the inclination vector.
  267.      * @return first component of the inclination vector
  268.      */
  269.     public abstract T getHx();

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

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

  281.     /** Get the second component of the inclination vector derivative.
  282.      * @return second component of the inclination vector derivative
  283.      */
  284.     public abstract T getHyDot();

  285.     /** Get the eccentric longitude argument.
  286.      * @return E + ω + Ω eccentric longitude argument (rad)
  287.      */
  288.     public abstract T getLE();

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

  296.     /** Get the true longitude argument.
  297.      * @return v + ω + Ω true longitude argument (rad)
  298.      */
  299.     public abstract T getLv();

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

  307.     /** Get the mean longitude argument.
  308.      * @return M + ω + Ω mean longitude argument (rad)
  309.      */
  310.     public abstract T getLM();

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

  318.     // Additional orbital elements

  319.     /** Get the eccentricity.
  320.      * @return eccentricity
  321.      */
  322.     public abstract T getE();

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

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

  337.     /** Get the inclination derivative.
  338.      * @return inclination derivative (rad/s)
  339.      */
  340.     public abstract T getIDot();

  341.     /** Check if orbit includes non-Keplerian rates.
  342.      * @return true if orbit includes non-Keplerian derivatives
  343.      * @see #getADot()
  344.      * @see #getEquinoctialExDot()
  345.      * @see #getEquinoctialEyDot()
  346.      * @see #getHxDot()
  347.      * @see #getHyDot()
  348.      * @see #getLEDot()
  349.      * @see #getLvDot()
  350.      * @see #getLMDot()
  351.      * @see #getEDot()
  352.      * @see #getIDot()
  353.      * @since 13.0
  354.      */
  355.     public boolean hasNonKeplerianAcceleration() {
  356.         return hasNonKeplerianAcceleration(getPVCoordinates(), getMu());
  357.     }

  358.     /** Get the central attraction coefficient used for position and velocity conversions (m³/s²).
  359.      * @return central attraction coefficient used for position and velocity conversions (m³/s²)
  360.      */
  361.     public T getMu() {
  362.         return mu;
  363.     }

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

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

  384.     /** Get the derivative of the mean anomaly with respect to the semi major axis.
  385.      * @return derivative of the mean anomaly with respect to the semi major axis
  386.      */
  387.     public T getMeanAnomalyDotWrtA() {
  388.         return getKeplerianMeanMotion().divide(getA()).multiply(-1.5);
  389.     }

  390.     /** Get the date of orbital parameters.
  391.      * @return date of the orbital parameters
  392.      */
  393.     public FieldAbsoluteDate<T> getDate() {
  394.         return date;
  395.     }

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

  405.         // If output frame requested is the same as definition frame,
  406.         // PV coordinates are returned directly
  407.         if (outputFrame == frame) {
  408.             return pvCoordinates;
  409.         }

  410.         // Else, PV coordinates are transformed to output frame
  411.         final FieldTransform<T> t = frame.getTransformTo(outputFrame, date);
  412.         return t.transformPVCoordinates(pvCoordinates);
  413.     }

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

  418.     /** {@inheritDoc} */
  419.     @Override
  420.     public FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> otherDate, final Frame otherFrame) {
  421.         return shiftedBy(otherDate.durationFrom(getDate())).getPosition(otherFrame);
  422.     }

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

  433.         // If output frame requested is the same as definition frame,
  434.         // Position vector is returned directly
  435.         if (outputFrame == frame) {
  436.             return position;
  437.         }

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

  441.     }

  442.     /** Get the position in definition frame.
  443.      * @return position in the definition frame
  444.      * @see #getPVCoordinates()
  445.      * @since 12.0
  446.      */
  447.     public FieldVector3D<T> getPosition() {
  448.         if (position == null) {
  449.             position = initPosition();
  450.         }
  451.         return position;
  452.     }

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

  464.     /** Getter for Field-valued one.
  465.      * @return one
  466.      * */
  467.     protected T getOne() {
  468.         return one;
  469.     }

  470.     /** Getter for Field-valued zero.
  471.      * @return zero
  472.      * */
  473.     protected T getZero() {
  474.         return zero;
  475.     }

  476.     /** Getter for Field.
  477.      * @return CalculusField
  478.      * */
  479.     protected Field<T> getField() {
  480.         return field;
  481.     }

  482.     /** Compute the position coordinates from the canonical parameters.
  483.      * @return computed position coordinates
  484.      * @since 12.0
  485.      */
  486.     protected abstract FieldVector3D<T> initPosition();

  487.     /** Compute the position/velocity coordinates from the canonical parameters.
  488.      * @return computed position/velocity coordinates
  489.      */
  490.     protected abstract TimeStampedFieldPVCoordinates<T> initPVCoordinates();

  491.     /**
  492.      * Create a new object representing the same physical orbital state, but attached to a different reference frame.
  493.      * If the new frame is not inertial, an exception will be thrown.
  494.      *
  495.      * @param inertialFrame reference frame of output orbit
  496.      * @return orbit with different frame
  497.      * @since 13.0
  498.      */
  499.     public abstract FieldOrbit<T> inFrame(Frame inertialFrame);

  500.     /** Get a time-shifted orbit.
  501.      * <p>
  502.      * The orbit can be slightly shifted to close dates. This shift is based on
  503.      * a simple Keplerian model. It is <em>not</em> intended as a replacement
  504.      * for proper orbit and attitude propagation but should be sufficient for
  505.      * small time shifts or coarse accuracy.
  506.      * </p>
  507.      * @param dt time shift in seconds
  508.      * @return a new orbit, shifted with respect to the instance (which is immutable)
  509.      */
  510.     public abstract FieldOrbit<T> shiftedBy(T dt);

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

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

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

  554.     }

  555.     /** Compute the Jacobian of the Cartesian parameters with respect to the orbital parameters.
  556.      * <p>
  557.      * Element {@code jacobian[i][j]} is the derivative of Cartesian coordinate i of the orbit with
  558.      * respect to orbital parameter j. This means each row corresponds to one Cartesian coordinate
  559.      * x, y, z, xdot, ydot, zdot whereas columns 0 to 5 correspond to the orbital parameters.
  560.      * </p>
  561.      * @param type type of the position angle to use
  562.      * @param jacobian placeholder 6x6 (or larger) matrix to be filled with the Jacobian, if matrix
  563.      * is larger than 6x6, only the 6x6 upper left corner will be modified
  564.      */
  565.     public void getJacobianWrtParameters(final PositionAngleType type, final T[][] jacobian) {

  566.         final T[][] cachedJacobian;
  567.         synchronized (this) {
  568.             switch (type) {
  569.                 case MEAN :
  570.                     if (jacobianWrtParametersMean == null) {
  571.                         // first call, we need to compute the Jacobian and cache it
  572.                         jacobianWrtParametersMean = createInverseJacobian(type);
  573.                     }
  574.                     cachedJacobian = jacobianWrtParametersMean;
  575.                     break;
  576.                 case ECCENTRIC :
  577.                     if (jacobianWrtParametersEccentric == null) {
  578.                         // first call, we need to compute the Jacobian and cache it
  579.                         jacobianWrtParametersEccentric = createInverseJacobian(type);
  580.                     }
  581.                     cachedJacobian = jacobianWrtParametersEccentric;
  582.                     break;
  583.                 case TRUE :
  584.                     if (jacobianWrtParametersTrue == null) {
  585.                         // first call, we need to compute the Jacobian and cache it
  586.                         jacobianWrtParametersTrue = createInverseJacobian(type);
  587.                     }
  588.                     cachedJacobian = jacobianWrtParametersTrue;
  589.                     break;
  590.                 default :
  591.                     throw new OrekitInternalError(null);
  592.             }
  593.         }

  594.         // fill the user-provided array
  595.         for (int i = 0; i < cachedJacobian.length; ++i) {
  596.             System.arraycopy(cachedJacobian[i], 0, jacobian[i], 0, cachedJacobian[i].length);
  597.         }

  598.     }

  599.     /** Create an inverse Jacobian.
  600.      * @param type type of the position angle to use
  601.      * @return inverse Jacobian
  602.      */
  603.     private T[][] createInverseJacobian(final PositionAngleType type) {

  604.         // get the direct Jacobian
  605.         final T[][] directJacobian = MathArrays.buildArray(getA().getField(), 6, 6);
  606.         getJacobianWrtCartesian(type, directJacobian);

  607.         // invert the direct Jacobian
  608.         final FieldMatrix<T> matrix = MatrixUtils.createFieldMatrix(directJacobian);
  609.         final FieldDecompositionSolver<T> solver = new FieldLUDecomposition<>(matrix).getSolver();
  610.         return solver.getInverse().getData();

  611.     }

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

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

  634.     /** Compute the Jacobian of the orbital parameters with true angle with respect to the Cartesian parameters.
  635.      * <p>
  636.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  637.      * respect to Cartesian coordinate j. This means each row correspond to one orbital parameter
  638.      * whereas columns 0 to 5 correspond to the Cartesian coordinates x, y, z, xDot, yDot and zDot.
  639.      * </p>
  640.      * @return 6x6 Jacobian matrix
  641.      * @see #computeJacobianMeanWrtCartesian()
  642.      * @see #computeJacobianEccentricWrtCartesian()
  643.      */
  644.     protected abstract T[][] computeJacobianTrueWrtCartesian();

  645.     /** Add the contribution of the Keplerian motion to parameters derivatives
  646.      * <p>
  647.      * This method is used by integration-based propagators to evaluate the part of Keplerian
  648.      * motion to evolution of the orbital state.
  649.      * </p>
  650.      * @param type type of the position angle in the state
  651.      * @param gm attraction coefficient to use
  652.      * @param pDot array containing orbital state derivatives to update (the Keplerian
  653.      * part must be <em>added</em> to the array components, as the array may already
  654.      * contain some non-zero elements corresponding to non-Keplerian parts)
  655.      */
  656.     public abstract void addKeplerContribution(PositionAngleType type, T gm, T[] pDot);

  657.         /** Fill a Jacobian half row with a single vector.
  658.      * @param a coefficient of the vector
  659.      * @param v 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 void fillHalfRow(final T a, final FieldVector3D<T> v, final T[] row, final int j) {
  664.         row[j]     = a.multiply(v.getX());
  665.         row[j + 1] = a.multiply(v.getY());
  666.         row[j + 2] = a.multiply(v.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 row Jacobian matrix row
  674.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  675.      */
  676.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  677.                                       final T[] row, final int j) {
  678.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX());
  679.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY());
  680.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ());

  681.     }

  682.     /** Fill a Jacobian half row with a linear combination of vectors.
  683.      * @param a1 coefficient of the first vector
  684.      * @param v1 first vector
  685.      * @param a2 coefficient of the second vector
  686.      * @param v2 second vector
  687.      * @param a3 coefficient of the third vector
  688.      * @param v3 third vector
  689.      * @param row Jacobian matrix row
  690.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  691.      */
  692.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1,
  693.                                final T a2, final FieldVector3D<T> v2,
  694.                                final T a3, final FieldVector3D<T> v3,
  695.                                       final T[] row, final int j) {
  696.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX());
  697.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY());
  698.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ());

  699.     }

  700.     /** Fill a Jacobian half row with a linear combination of vectors.
  701.      * @param a1 coefficient of the first vector
  702.      * @param v1 first vector
  703.      * @param a2 coefficient of the second vector
  704.      * @param v2 second vector
  705.      * @param a3 coefficient of the third vector
  706.      * @param v3 third vector
  707.      * @param a4 coefficient of the fourth vector
  708.      * @param v4 fourth vector
  709.      * @param row Jacobian matrix row
  710.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  711.      */
  712.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  713.                                       final T a3, final FieldVector3D<T> v3, final T a4, final FieldVector3D<T> v4,
  714.                                       final T[] row, final int j) {
  715.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX());
  716.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY());
  717.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ(), a4, v4.getZ());

  718.     }

  719.     /** Fill a Jacobian half row with a linear combination of vectors.
  720.      * @param a1 coefficient of the first vector
  721.      * @param v1 first vector
  722.      * @param a2 coefficient of the second vector
  723.      * @param v2 second vector
  724.      * @param a3 coefficient of the third vector
  725.      * @param v3 third vector
  726.      * @param a4 coefficient of the fourth vector
  727.      * @param v4 fourth vector
  728.      * @param a5 coefficient of the fifth vector
  729.      * @param v5 fifth vector
  730.      * @param row Jacobian matrix row
  731.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  732.      */
  733.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  734.                                       final T a3, final FieldVector3D<T> v3, final T a4, final FieldVector3D<T> v4,
  735.                                       final T a5, final FieldVector3D<T> v5,
  736.                                       final T[] row, final int j) {
  737.         row[j]     = a1.linearCombination(a1, v1.getX(), a2, v2.getX(), a3, v3.getX(), a4, v4.getX()).add(a5.multiply(v5.getX()));
  738.         row[j + 1] = a1.linearCombination(a1, v1.getY(), a2, v2.getY(), a3, v3.getY(), a4, v4.getY()).add(a5.multiply(v5.getY()));
  739.         row[j + 2] = a1.linearCombination(a1, v1.getZ(), a2, v2.getZ(), a3, v3.getZ(), a4, v4.getZ()).add(a5.multiply(v5.getZ()));

  740.     }

  741.     /** Fill a Jacobian half row with a linear combination of vectors.
  742.      * @param a1 coefficient of the first vector
  743.      * @param v1 first vector
  744.      * @param a2 coefficient of the second vector
  745.      * @param v2 second vector
  746.      * @param a3 coefficient of the third vector
  747.      * @param v3 third vector
  748.      * @param a4 coefficient of the fourth vector
  749.      * @param v4 fourth vector
  750.      * @param a5 coefficient of the fifth vector
  751.      * @param v5 fifth vector
  752.      * @param a6 coefficient of the sixth vector
  753.      * @param v6 sixth vector
  754.      * @param row Jacobian matrix row
  755.      * @param j index of the first element to set (row[j], row[j+1] and row[j+2] will all be set)
  756.      */
  757.     protected void fillHalfRow(final T a1, final FieldVector3D<T> v1, final T a2, final FieldVector3D<T> v2,
  758.                                       final T a3, final FieldVector3D<T> v3, final T a4, final FieldVector3D<T> v4,
  759.                                       final T a5, final FieldVector3D<T> v5, final T a6, final FieldVector3D<T> v6,
  760.                                       final T[] row, final int j) {
  761.         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()));
  762.         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()));
  763.         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()));

  764.     }


  765. }