FieldPVCoordinates.java

  1. /* Copyright 2002-2022 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.utils;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.analysis.differentiation.FDSFactory;
  21. import org.hipparchus.analysis.differentiation.FieldDerivative;
  22. import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
  23. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
  24. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.util.FastMath;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.time.TimeShiftable;

  30. /** Simple container for Position/Velocity pairs, using {@link CalculusFieldElement}.
  31.  * <p>
  32.  * The state can be slightly shifted to close dates. This shift is based on
  33.  * a simple linear model. It is <em>not</em> intended as a replacement for
  34.  * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  35.  * for either small time shifts or coarse accuracy.
  36.  * </p>
  37.  * <p>
  38.  * This class is the angular counterpart to {@link FieldAngularCoordinates}.
  39.  * </p>
  40.  * <p>Instances of this class are guaranteed to be immutable.</p>
  41.  * @param <T> the type of the field elements
  42.  * @author Luc Maisonobe
  43.  * @since 6.0
  44.  * @see PVCoordinates
  45.  */
  46. public class FieldPVCoordinates<T extends CalculusFieldElement<T>>
  47.     implements TimeShiftable<FieldPVCoordinates<T>> {

  48.     /** The position. */
  49.     private final FieldVector3D<T> position;

  50.     /** The velocity. */
  51.     private final FieldVector3D<T> velocity;

  52.     /** The acceleration. */
  53.     private final FieldVector3D<T> acceleration;

  54.     /** Builds a FieldPVCoordinates triplet with zero acceleration.
  55.      * @param position the position vector (m)
  56.      * @param velocity the velocity vector (m/s)
  57.      */
  58.     public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity) {
  59.         this.position     = position;
  60.         this.velocity     = velocity;
  61.         final T zero      = position.getX().getField().getZero();
  62.         this.acceleration = new FieldVector3D<>(zero, zero, zero);
  63.     }

  64.     /** Builds a FieldPVCoordinates triplet.
  65.      * @param position the position vector (m)
  66.      * @param velocity the velocity vector (m/s)
  67.      * @param acceleration the acceleration vector (m/s²)
  68.      */
  69.     public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity,
  70.                               final FieldVector3D<T> acceleration) {
  71.         this.position     = position;
  72.         this.velocity     = velocity;
  73.         this.acceleration = acceleration;
  74.     }

  75.     /** Builds a FieldPVCoordinates from a field and a regular PVCoordinates.
  76.      * @param field field for the components
  77.      * @param pv PVCoordinates triplet to convert
  78.      */
  79.     public FieldPVCoordinates(final Field<T> field, final PVCoordinates pv) {
  80.         this.position     = new FieldVector3D<>(field, pv.getPosition());
  81.         this.velocity     = new FieldVector3D<>(field, pv.getVelocity());
  82.         this.acceleration = new FieldVector3D<>(field, pv.getAcceleration());
  83.     }

  84.     /** Multiplicative constructor.
  85.      * <p>Build a PVCoordinates from another one and a scale factor.</p>
  86.      * <p>The PVCoordinates built will be a * pv</p>
  87.      * @param a scale factor
  88.      * @param pv base (unscaled) PVCoordinates
  89.      */
  90.     public FieldPVCoordinates(final double a, final FieldPVCoordinates<T> pv) {
  91.         position     = new FieldVector3D<>(a, pv.position);
  92.         velocity     = new FieldVector3D<>(a, pv.velocity);
  93.         acceleration = new FieldVector3D<>(a, pv.acceleration);
  94.     }

  95.     /** Multiplicative constructor.
  96.      * <p>Build a PVCoordinates from another one and a scale factor.</p>
  97.      * <p>The PVCoordinates built will be a * pv</p>
  98.      * @param a scale factor
  99.      * @param pv base (unscaled) PVCoordinates
  100.      */
  101.     public FieldPVCoordinates(final T a, final FieldPVCoordinates<T> pv) {
  102.         position     = new FieldVector3D<>(a, pv.position);
  103.         velocity     = new FieldVector3D<>(a, pv.velocity);
  104.         acceleration = new FieldVector3D<>(a, pv.acceleration);
  105.     }

  106.     /** Multiplicative constructor.
  107.      * <p>Build a PVCoordinates from another one and a scale factor.</p>
  108.      * <p>The PVCoordinates built will be a * pv</p>
  109.      * @param a scale factor
  110.      * @param pv base (unscaled) PVCoordinates
  111.      */
  112.     public FieldPVCoordinates(final T a, final PVCoordinates pv) {
  113.         position     = new FieldVector3D<>(a, pv.getPosition());
  114.         velocity     = new FieldVector3D<>(a, pv.getVelocity());
  115.         acceleration = new FieldVector3D<>(a, pv.getAcceleration());
  116.     }

  117.     /** Subtractive constructor.
  118.      * <p>Build a relative PVCoordinates from a start and an end position.</p>
  119.      * <p>The PVCoordinates built will be end - start.</p>
  120.      * @param start Starting PVCoordinates
  121.      * @param end ending PVCoordinates
  122.      */
  123.     public FieldPVCoordinates(final FieldPVCoordinates<T> start, final FieldPVCoordinates<T> end) {
  124.         this.position     = end.position.subtract(start.position);
  125.         this.velocity     = end.velocity.subtract(start.velocity);
  126.         this.acceleration = end.acceleration.subtract(start.acceleration);
  127.     }

  128.     /** Linear constructor.
  129.      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
  130.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
  131.      * @param a1 first scale factor
  132.      * @param pv1 first base (unscaled) PVCoordinates
  133.      * @param a2 second scale factor
  134.      * @param pv2 second base (unscaled) PVCoordinates
  135.      */
  136.     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
  137.                               final double a2, final FieldPVCoordinates<T> pv2) {
  138.         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
  139.         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
  140.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
  141.     }

  142.     /** Linear constructor.
  143.      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
  144.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
  145.      * @param a1 first scale factor
  146.      * @param pv1 first base (unscaled) PVCoordinates
  147.      * @param a2 second scale factor
  148.      * @param pv2 second base (unscaled) PVCoordinates
  149.      */
  150.     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
  151.                               final T a2, final FieldPVCoordinates<T> pv2) {
  152.         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
  153.         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
  154.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
  155.     }

  156.     /** Linear constructor.
  157.      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
  158.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
  159.      * @param a1 first scale factor
  160.      * @param pv1 first base (unscaled) PVCoordinates
  161.      * @param a2 second scale factor
  162.      * @param pv2 second base (unscaled) PVCoordinates
  163.      */
  164.     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
  165.                               final T a2, final PVCoordinates pv2) {
  166.         position     = new FieldVector3D<>(a1, pv1.getPosition(), a2, pv2.getPosition());
  167.         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(), a2, pv2.getVelocity());
  168.         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration());
  169.     }

  170.     /** Linear constructor.
  171.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  172.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  173.      * @param a1 first scale factor
  174.      * @param pv1 first base (unscaled) PVCoordinates
  175.      * @param a2 second scale factor
  176.      * @param pv2 second base (unscaled) PVCoordinates
  177.      * @param a3 third scale factor
  178.      * @param pv3 third base (unscaled) PVCoordinates
  179.      */
  180.     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
  181.                               final double a2, final FieldPVCoordinates<T> pv2,
  182.                               final double a3, final FieldPVCoordinates<T> pv3) {
  183.         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
  184.         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
  185.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
  186.     }

  187.     /** Linear constructor.
  188.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  189.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  190.      * @param a1 first scale factor
  191.      * @param pv1 first base (unscaled) PVCoordinates
  192.      * @param a2 second scale factor
  193.      * @param pv2 second base (unscaled) PVCoordinates
  194.      * @param a3 third scale factor
  195.      * @param pv3 third base (unscaled) PVCoordinates
  196.      */
  197.     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
  198.                               final T a2, final FieldPVCoordinates<T> pv2,
  199.                               final T a3, final FieldPVCoordinates<T> pv3) {
  200.         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
  201.         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
  202.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
  203.     }

  204.     /** Linear constructor.
  205.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  206.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  207.      * @param a1 first scale factor
  208.      * @param pv1 first base (unscaled) PVCoordinates
  209.      * @param a2 second scale factor
  210.      * @param pv2 second base (unscaled) PVCoordinates
  211.      * @param a3 third scale factor
  212.      * @param pv3 third base (unscaled) PVCoordinates
  213.      */
  214.     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
  215.                               final T a2, final PVCoordinates pv2,
  216.                               final T a3, final PVCoordinates pv3) {
  217.         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),     a3, pv3.getPosition());
  218.         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),     a3, pv3.getVelocity());
  219.         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(), a3, pv3.getAcceleration());
  220.     }

  221.     /** Linear constructor.
  222.      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
  223.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  224.      * @param a1 first scale factor
  225.      * @param pv1 first base (unscaled) PVCoordinates
  226.      * @param a2 second scale factor
  227.      * @param pv2 second base (unscaled) PVCoordinates
  228.      * @param a3 third scale factor
  229.      * @param pv3 third base (unscaled) PVCoordinates
  230.      * @param a4 fourth scale factor
  231.      * @param pv4 fourth base (unscaled) PVCoordinates
  232.      */
  233.     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
  234.                               final double a2, final FieldPVCoordinates<T> pv2,
  235.                               final double a3, final FieldPVCoordinates<T> pv3,
  236.                               final double a4, final FieldPVCoordinates<T> pv4) {
  237.         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
  238.         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
  239.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
  240.     }

  241.     /** Linear constructor.
  242.      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
  243.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  244.      * @param a1 first scale factor
  245.      * @param pv1 first base (unscaled) PVCoordinates
  246.      * @param a2 second scale factor
  247.      * @param pv2 second base (unscaled) PVCoordinates
  248.      * @param a3 third scale factor
  249.      * @param pv3 third base (unscaled) PVCoordinates
  250.      * @param a4 fourth scale factor
  251.      * @param pv4 fourth base (unscaled) PVCoordinates
  252.      */
  253.     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
  254.                               final T a2, final FieldPVCoordinates<T> pv2,
  255.                               final T a3, final FieldPVCoordinates<T> pv3,
  256.                               final T a4, final FieldPVCoordinates<T> pv4) {
  257.         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
  258.         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
  259.         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
  260.     }

  261.     /** Linear constructor.
  262.      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
  263.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  264.      * @param a1 first scale factor
  265.      * @param pv1 first base (unscaled) PVCoordinates
  266.      * @param a2 second scale factor
  267.      * @param pv2 second base (unscaled) PVCoordinates
  268.      * @param a3 third scale factor
  269.      * @param pv3 third base (unscaled) PVCoordinates
  270.      * @param a4 fourth scale factor
  271.      * @param pv4 fourth base (unscaled) PVCoordinates
  272.      */
  273.     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
  274.                               final T a2, final PVCoordinates pv2,
  275.                               final T a3, final PVCoordinates pv3,
  276.                               final T a4, final PVCoordinates pv4) {
  277.         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),
  278.                                            a3, pv3.getPosition(),     a4, pv4.getPosition());
  279.         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),
  280.                                            a3, pv3.getVelocity(),     a4, pv4.getVelocity());
  281.         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(),
  282.                                            a3, pv3.getAcceleration(), a4, pv4.getAcceleration());
  283.     }

  284.     /** Builds a FieldPVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link FieldDerivativeStructure}&gt;.
  285.      * <p>
  286.      * The vector components must have time as their only derivation parameter and
  287.      * have consistent derivation orders.
  288.      * </p>
  289.      * @param p vector with time-derivatives embedded within the coordinates
  290.      * @param <U> type of the derivative
  291.      * @since 9.2
  292.      */
  293.     public <U extends FieldDerivative<T, U>> FieldPVCoordinates(final FieldVector3D<U> p) {
  294.         position = new FieldVector3D<>(p.getX().getValue(), p.getY().getValue(), p.getZ().getValue());
  295.         if (p.getX().getOrder() >= 1) {
  296.             velocity = new FieldVector3D<>(p.getX().getPartialDerivative(1),
  297.                                            p.getY().getPartialDerivative(1),
  298.                                            p.getZ().getPartialDerivative(1));
  299.             if (p.getX().getOrder() >= 2) {
  300.                 acceleration = new FieldVector3D<>(p.getX().getPartialDerivative(2),
  301.                                                    p.getY().getPartialDerivative(2),
  302.                                                    p.getZ().getPartialDerivative(2));
  303.             } else {
  304.                 acceleration = FieldVector3D.getZero(position.getX().getField());
  305.             }
  306.         } else {
  307.             final FieldVector3D<T> zero = FieldVector3D.getZero(position.getX().getField());
  308.             velocity     = zero;
  309.             acceleration = zero;
  310.         }
  311.     }

  312.     /** Get fixed position/velocity at origin (both p, v and a are zero vectors).
  313.      * @param field field for the components
  314.      * @param <T> the type of the field elements
  315.      * @return a new fixed position/velocity at origin
  316.      */
  317.     public static <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getZero(final Field<T> field) {
  318.         return new FieldPVCoordinates<>(field, PVCoordinates.ZERO);
  319.     }

  320.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldDerivativeStructure}&gt;.
  321.      * <p>
  322.      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
  323.      * to the user-specified order.
  324.      * </p>
  325.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  326.      * @return vector with time-derivatives embedded within the coordinates
  327.           * @since 9.2
  328.      */
  329.     public FieldVector3D<FieldDerivativeStructure<T>> toDerivativeStructureVector(final int order) {

  330.         final FDSFactory<T> factory;
  331.         final FieldDerivativeStructure<T> x;
  332.         final FieldDerivativeStructure<T> y;
  333.         final FieldDerivativeStructure<T> z;
  334.         switch (order) {
  335.             case 0 :
  336.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  337.                 x = factory.build(position.getX());
  338.                 y = factory.build(position.getY());
  339.                 z = factory.build(position.getZ());
  340.                 break;
  341.             case 1 :
  342.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  343.                 x = factory.build(position.getX(), velocity.getX());
  344.                 y = factory.build(position.getY(), velocity.getY());
  345.                 z = factory.build(position.getZ(), velocity.getZ());
  346.                 break;
  347.             case 2 :
  348.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  349.                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
  350.                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
  351.                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
  352.                 break;
  353.             default :
  354.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  355.         }

  356.         return new FieldVector3D<>(x, y, z);

  357.     }

  358.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative1}&gt;.
  359.      * <p>
  360.      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
  361.      * to the order 1.
  362.      * </p>
  363.      * @return vector with time-derivatives embedded within the coordinates
  364.      * @see #toUnivariateDerivative2Vector()
  365.      * @since 10.2
  366.      */
  367.     public FieldVector3D<FieldUnivariateDerivative1<T>> toUnivariateDerivative1Vector() {

  368.         final FieldUnivariateDerivative1<T> x = new FieldUnivariateDerivative1<>(position.getX(), velocity.getX());
  369.         final FieldUnivariateDerivative1<T> y = new FieldUnivariateDerivative1<>(position.getY(), velocity.getY());
  370.         final FieldUnivariateDerivative1<T> z = new FieldUnivariateDerivative1<>(position.getZ(), velocity.getZ());

  371.         return new FieldVector3D<>(x, y, z);
  372.     }

  373.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative2}&gt;.
  374.      * <p>
  375.      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
  376.      * to the order 2.
  377.      * </p>
  378.      * @return vector with time-derivatives embedded within the coordinates
  379.      * @see #toUnivariateDerivative1Vector()
  380.      * @since 10.2
  381.      */
  382.     public FieldVector3D<FieldUnivariateDerivative2<T>> toUnivariateDerivative2Vector() {

  383.         final FieldUnivariateDerivative2<T> x = new FieldUnivariateDerivative2<>(position.getX(), velocity.getX(), acceleration.getX());
  384.         final FieldUnivariateDerivative2<T> y = new FieldUnivariateDerivative2<>(position.getY(), velocity.getY(), acceleration.getY());
  385.         final FieldUnivariateDerivative2<T> z = new FieldUnivariateDerivative2<>(position.getZ(), velocity.getZ(), acceleration.getZ());

  386.         return new FieldVector3D<>(x, y, z);
  387.     }

  388.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldDerivativeStructure}&gt;.
  389.      * <p>
  390.      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
  391.      * to the user-specified order. As both the instance components {@link #getPosition() position},
  392.      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
  393.      * {@link FieldDerivativeStructure#getPartialDerivative(int...) derivatives} of the components
  394.      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
  395.      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
  396.      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
  397.      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
  398.      * </p>
  399.      * <p>
  400.      * If derivation order is 1, the first derivative of acceleration will be computed as a
  401.      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
  402.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  403.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  404.      * </p>
  405.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  406.      * @return pv coordinates with time-derivatives embedded within the coordinates
  407.           * @since 9.2
  408.      */
  409.     public FieldPVCoordinates<FieldDerivativeStructure<T>> toDerivativeStructurePV(final int order) {

  410.         final FDSFactory<T> factory;
  411.         final FieldDerivativeStructure<T> x0;
  412.         final FieldDerivativeStructure<T> y0;
  413.         final FieldDerivativeStructure<T> z0;
  414.         final FieldDerivativeStructure<T> x1;
  415.         final FieldDerivativeStructure<T> y1;
  416.         final FieldDerivativeStructure<T> z1;
  417.         final FieldDerivativeStructure<T> x2;
  418.         final FieldDerivativeStructure<T> y2;
  419.         final FieldDerivativeStructure<T> z2;
  420.         switch (order) {
  421.             case 0 :
  422.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  423.                 x0 = factory.build(position.getX());
  424.                 y0 = factory.build(position.getY());
  425.                 z0 = factory.build(position.getZ());
  426.                 x1 = factory.build(velocity.getX());
  427.                 y1 = factory.build(velocity.getY());
  428.                 z1 = factory.build(velocity.getZ());
  429.                 x2 = factory.build(acceleration.getX());
  430.                 y2 = factory.build(acceleration.getY());
  431.                 z2 = factory.build(acceleration.getZ());
  432.                 break;
  433.             case 1 : {
  434.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  435.                 final T                r2            = position.getNormSq();
  436.                 final T                r             = r2.sqrt();
  437.                 final T                pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
  438.                 final T                a             = acceleration.getNorm();
  439.                 final T                aOr           = a.divide(r);
  440.                 final FieldVector3D<T> keplerianJerk = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  441.                                                                            aOr.negate(), velocity);
  442.                 x0 = factory.build(position.getX(),     velocity.getX());
  443.                 y0 = factory.build(position.getY(),     velocity.getY());
  444.                 z0 = factory.build(position.getZ(),     velocity.getZ());
  445.                 x1 = factory.build(velocity.getX(),     acceleration.getX());
  446.                 y1 = factory.build(velocity.getY(),     acceleration.getY());
  447.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
  448.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
  449.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
  450.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
  451.                 break;
  452.             }
  453.             case 2 : {
  454.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  455.                 final T                r2              = position.getNormSq();
  456.                 final T                r               = r2.sqrt();
  457.                 final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
  458.                 final T                a               = acceleration.getNorm();
  459.                 final T                aOr             = a.divide(r);
  460.                 final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  461.                                                                              aOr.negate(), velocity);
  462.                 final T                v2              = velocity.getNormSq();
  463.                 final T                pa              = FieldVector3D.dotProduct(position, acceleration);
  464.                 final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
  465.                 final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
  466.                                                                              aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
  467.                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
  468.                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
  469.                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  470.                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  471.                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  472.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  473.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  474.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  475.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
  476.                 break;
  477.             }
  478.             default :
  479.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  480.         }

  481.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  482.                                         new FieldVector3D<>(x1, y1, z1),
  483.                                         new FieldVector3D<>(x2, y2, z2));

  484.     }


  485.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative1}&gt;.
  486.      * <p>
  487.      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
  488.      * to the order 1.
  489.      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
  490.      * </p>
  491.      * @return pv coordinates with time-derivatives embedded within the coordinates
  492.      * @since 10.2
  493.      */
  494.     public FieldPVCoordinates<FieldUnivariateDerivative1<T>> toUnivariateDerivative1PV() {

  495.         final T   r2            = position.getNormSq();
  496.         final T   r             = FastMath.sqrt(r2);
  497.         final T   pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
  498.         final T   a             = acceleration.getNorm();
  499.         final T   aOr           = a.divide(r);
  500.         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  501.                                                                      aOr.negate(), velocity);

  502.         final FieldUnivariateDerivative1<T> x0 = new FieldUnivariateDerivative1<>(position.getX(),     velocity.getX());
  503.         final FieldUnivariateDerivative1<T> y0 = new FieldUnivariateDerivative1<>(position.getY(),     velocity.getY());
  504.         final FieldUnivariateDerivative1<T> z0 = new FieldUnivariateDerivative1<>(position.getZ(),     velocity.getZ());
  505.         final FieldUnivariateDerivative1<T> x1 = new FieldUnivariateDerivative1<>(velocity.getX(),     acceleration.getX());
  506.         final FieldUnivariateDerivative1<T> y1 = new FieldUnivariateDerivative1<>(velocity.getY(),     acceleration.getY());
  507.         final FieldUnivariateDerivative1<T> z1 = new FieldUnivariateDerivative1<>(velocity.getZ(),     acceleration.getZ());
  508.         final FieldUnivariateDerivative1<T> x2 = new FieldUnivariateDerivative1<>(acceleration.getX(), keplerianJerk.getX());
  509.         final FieldUnivariateDerivative1<T> y2 = new FieldUnivariateDerivative1<>(acceleration.getY(), keplerianJerk.getY());
  510.         final FieldUnivariateDerivative1<T> z2 = new FieldUnivariateDerivative1<>(acceleration.getZ(), keplerianJerk.getZ());

  511.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  512.                                         new FieldVector3D<>(x1, y1, z1),
  513.                                         new FieldVector3D<>(x2, y2, z2));

  514.     }

  515.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative2}&gt;.
  516.      * <p>
  517.      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
  518.      * to the order 2.
  519.      * As derivation order is 2, the second derivative of velocity (which
  520.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  521.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  522.      * </p>
  523.      * @return pv coordinates with time-derivatives embedded within the coordinates
  524.      * @since 10.2
  525.      */
  526.     public FieldPVCoordinates<FieldUnivariateDerivative2<T>> toUnivariateDerivative2PV() {

  527.         final T                r2              = position.getNormSq();
  528.         final T                r               = r2.sqrt();
  529.         final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
  530.         final T                a               = acceleration.getNorm();
  531.         final T                aOr             = a.divide(r);
  532.         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  533.                                                                      aOr.negate(), velocity);
  534.         final T                v2              = velocity.getNormSq();
  535.         final T                pa              = FieldVector3D.dotProduct(position, acceleration);
  536.         final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
  537.         final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
  538.                                                                      aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);

  539.         final FieldUnivariateDerivative2<T> x0 = new FieldUnivariateDerivative2<>(position.getX(),     velocity.getX(),      acceleration.getX());
  540.         final FieldUnivariateDerivative2<T> y0 = new FieldUnivariateDerivative2<>(position.getY(),     velocity.getY(),      acceleration.getY());
  541.         final FieldUnivariateDerivative2<T> z0 = new FieldUnivariateDerivative2<>(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  542.         final FieldUnivariateDerivative2<T> x1 = new FieldUnivariateDerivative2<>(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  543.         final FieldUnivariateDerivative2<T> y1 = new FieldUnivariateDerivative2<>(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  544.         final FieldUnivariateDerivative2<T> z1 = new FieldUnivariateDerivative2<>(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  545.         final FieldUnivariateDerivative2<T> x2 = new FieldUnivariateDerivative2<>(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  546.         final FieldUnivariateDerivative2<T> y2 = new FieldUnivariateDerivative2<>(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  547.         final FieldUnivariateDerivative2<T> z2 = new FieldUnivariateDerivative2<>(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());

  548.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  549.                                         new FieldVector3D<>(x1, y1, z1),
  550.                                         new FieldVector3D<>(x2, y2, z2));

  551.     }

  552.     /** Estimate velocity between two positions.
  553.      * <p>Estimation is based on a simple fixed velocity translation
  554.      * during the time interval between the two positions.</p>
  555.      * @param start start position
  556.      * @param end end position
  557.      * @param dt time elapsed between the dates of the two positions
  558.      * @param <T> the type of the field elements
  559.      * @return velocity allowing to go from start to end positions
  560.      */
  561.     public static <T extends CalculusFieldElement<T>> FieldVector3D<T> estimateVelocity(final FieldVector3D<T> start,
  562.                                                                                     final FieldVector3D<T> end,
  563.                                                                                     final double dt) {
  564.         final double scale = 1.0 / dt;
  565.         return new FieldVector3D<>(scale, end, -scale, start);
  566.     }

  567.     /** Get a time-shifted state.
  568.      * <p>
  569.      * The state can be slightly shifted to close dates. This shift is based on
  570.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  571.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  572.      * for either small time shifts or coarse accuracy.
  573.      * </p>
  574.      * @param dt time shift in seconds
  575.      * @return a new state, shifted with respect to the instance (which is immutable)
  576.      */
  577.     public FieldPVCoordinates<T> shiftedBy(final double dt) {
  578.         return new FieldPVCoordinates<>(new FieldVector3D<>(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
  579.                                         new FieldVector3D<>(1, velocity, dt, acceleration),
  580.                                         acceleration);
  581.     }

  582.     /** Get a time-shifted state.
  583.      * <p>
  584.      * The state can be slightly shifted to close dates. This shift is based on
  585.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  586.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  587.      * for either small time shifts or coarse accuracy.
  588.      * </p>
  589.      * @param dt time shift in seconds
  590.      * @return a new state, shifted with respect to the instance (which is immutable)
  591.      */
  592.     public FieldPVCoordinates<T> shiftedBy(final T dt) {
  593.         final T one = dt.getField().getOne();
  594.         return new FieldPVCoordinates<>(new FieldVector3D<>(one, position,
  595.                                                             dt, velocity,
  596.                                                             dt.multiply(dt).multiply(0.5), acceleration),
  597.                                         new FieldVector3D<>(one, velocity,
  598.                                                             dt, acceleration),
  599.                                         acceleration);
  600.     }

  601.     /** Gets the position.
  602.      * @return the position vector (m).
  603.      */
  604.     public FieldVector3D<T> getPosition() {
  605.         return position;
  606.     }

  607.     /** Gets the velocity.
  608.      * @return the velocity vector (m/s).
  609.      */
  610.     public FieldVector3D<T> getVelocity() {
  611.         return velocity;
  612.     }

  613.     /** Gets the acceleration.
  614.      * @return the acceleration vector (m/s²).
  615.      */
  616.     public FieldVector3D<T> getAcceleration() {
  617.         return acceleration;
  618.     }

  619.     /** Gets the momentum.
  620.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  621.      * and &otimes; is cross product. To get the real physical angular momentum
  622.      * you need to multiply this vector by the mass.</p>
  623.      * <p>The returned vector is recomputed each time this method is called, it
  624.      * is not cached.</p>
  625.      * @return a new instance of the momentum vector (m²/s).
  626.      */
  627.     public FieldVector3D<T> getMomentum() {
  628.         return FieldVector3D.crossProduct(position, velocity);
  629.     }

  630.     /**
  631.      * Get the angular velocity (spin) of this point as seen from the origin.
  632.      *
  633.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  634.      * angular * momentum} and is computed by ω = p &times; v / ||p||²
  635.      *
  636.      * @return the angular velocity vector
  637.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  638.      *      Wikipedia</a>
  639.      */
  640.     public FieldVector3D<T> getAngularVelocity() {
  641.         return this.getMomentum().scalarMultiply(
  642.                 this.getPosition().getNormSq().reciprocal());
  643.     }

  644.     /** Get the opposite of the instance.
  645.      * @return a new position-velocity which is opposite to the instance
  646.      */
  647.     public FieldPVCoordinates<T> negate() {
  648.         return new FieldPVCoordinates<>(position.negate(), velocity.negate(), acceleration.negate());
  649.     }

  650.     /** Normalize the position part of the instance.
  651.      * <p>
  652.      * The computed coordinates first component (position) will be a
  653.      * normalized vector, the second component (velocity) will be the
  654.      * derivative of the first component (hence it will generally not
  655.      * be normalized), and the third component (acceleration) will be the
  656.      * derivative of the second component (hence it will generally not
  657.      * be normalized).
  658.      * </p>
  659.      * @return a new instance, with first component normalized and
  660.      * remaining component computed to have consistent derivatives
  661.      */
  662.     public FieldPVCoordinates<T> normalize() {
  663.         final T   inv     = position.getNorm().reciprocal();
  664.         final FieldVector3D<T> u       = new FieldVector3D<>(inv, position);
  665.         final FieldVector3D<T> v       = new FieldVector3D<>(inv, velocity);
  666.         final FieldVector3D<T> w       = new FieldVector3D<>(inv, acceleration);
  667.         final T   uv      = FieldVector3D.dotProduct(u, v);
  668.         final T   v2      = FieldVector3D.dotProduct(v, v);
  669.         final T   uw      = FieldVector3D.dotProduct(u, w);
  670.         final FieldVector3D<T> uDot    = new FieldVector3D<>(inv.getField().getOne(), v,
  671.                                                              uv.multiply(-1), u);
  672.         final FieldVector3D<T> uDotDot = new FieldVector3D<>(inv.getField().getOne(), w,
  673.                                                              uv.multiply(-2), v,
  674.                                                              uv.multiply(uv).multiply(3).subtract(v2).subtract(uw), u);
  675.         return new FieldPVCoordinates<>(u, uDot, uDotDot);
  676.     }

  677.     /** Compute the cross-product of two instances.
  678.      * @param pv2 second instances
  679.      * @return the cross product v1 ^ v2 as a new instance
  680.      */
  681.     public FieldPVCoordinates<T> crossProduct(final FieldPVCoordinates<T> pv2) {
  682.         final FieldVector3D<T> p1 = position;
  683.         final FieldVector3D<T> v1 = velocity;
  684.         final FieldVector3D<T> a1 = acceleration;
  685.         final FieldVector3D<T> p2 = pv2.position;
  686.         final FieldVector3D<T> v2 = pv2.velocity;
  687.         final FieldVector3D<T> a2 = pv2.acceleration;
  688.         return new FieldPVCoordinates<>(FieldVector3D.crossProduct(p1, p2),
  689.                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, v2),
  690.                                                             1, FieldVector3D.crossProduct(v1, p2)),
  691.                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, a2),
  692.                                                             2, FieldVector3D.crossProduct(v1, v2),
  693.                                                             1, FieldVector3D.crossProduct(a1, p2)));
  694.     }

  695.     /** Convert to a constant position-velocity.
  696.      * @return a constant position-velocity
  697.      */
  698.     public PVCoordinates toPVCoordinates() {
  699.         return new PVCoordinates(position.toVector3D(), velocity.toVector3D(), acceleration.toVector3D());
  700.     }

  701.     /** Return a string representation of this position/velocity pair.
  702.      * @return string representation of this position/velocity pair
  703.      */
  704.     public String toString() {
  705.         final String comma = ", ";
  706.         return new StringBuilder().append('{').append("P(").
  707.                                   append(position.getX().getReal()).append(comma).
  708.                                   append(position.getY().getReal()).append(comma).
  709.                                   append(position.getZ().getReal()).append("), V(").
  710.                                   append(velocity.getX().getReal()).append(comma).
  711.                                   append(velocity.getY().getReal()).append(comma).
  712.                                   append(velocity.getZ().getReal()).append("), A(").
  713.                                   append(acceleration.getX().getReal()).append(comma).
  714.                                   append(acceleration.getY().getReal()).append(comma).
  715.                                   append(acceleration.getZ().getReal()).append(")}").toString();
  716.     }

  717. }