FieldPVCoordinates.java

  1. /* Copyright 2002-2020 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.RealFieldElement;
  20. import org.hipparchus.analysis.differentiation.FDSFactory;
  21. import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
  22. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
  23. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.util.FastMath;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;
  28. import org.orekit.time.TimeShiftable;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  355.     }

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

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

  369.         return new FieldVector3D<>(x, y, z);
  370.     }

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

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

  384.         return new FieldVector3D<>(x, y, z);
  385.     }

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

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

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

  482.     }


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

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

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

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

  512.     }

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

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

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

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

  549.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

  715. }