FieldPVCoordinates.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.geometry.euclidean.threed.FieldVector3D;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.time.TimeShiftable;

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

  44.     /** The position. */
  45.     private final FieldVector3D<T> position;

  46.     /** The velocity. */
  47.     private final FieldVector3D<T> velocity;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  354.     }

  355.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldDerivativeStructure}&gt;.
  356.      * <p>
  357.      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
  358.      * to the user-specified order. As both the instance components {@link #getPosition() position},
  359.      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
  360.      * {@link FieldDerivativeStructure#getPartialDerivative(int...) derivatives} of the components
  361.      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
  362.      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
  363.      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
  364.      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
  365.      * </p>
  366.      * <p>
  367.      * If derivation order is 1, the first derivative of acceleration will be computed as a
  368.      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
  369.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  370.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  371.      * </p>
  372.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  373.      * @return pv coordinates with time-derivatives embedded within the coordinates
  374.      * @exception OrekitException if the user specified order is too large
  375.      * @since 9.2
  376.      */
  377.     public FieldPVCoordinates<FieldDerivativeStructure<T>> toDerivativeStructurePV(final int order)
  378.         throws OrekitException {

  379.         final FDSFactory<T> factory;
  380.         final FieldDerivativeStructure<T> x0;
  381.         final FieldDerivativeStructure<T> y0;
  382.         final FieldDerivativeStructure<T> z0;
  383.         final FieldDerivativeStructure<T> x1;
  384.         final FieldDerivativeStructure<T> y1;
  385.         final FieldDerivativeStructure<T> z1;
  386.         final FieldDerivativeStructure<T> x2;
  387.         final FieldDerivativeStructure<T> y2;
  388.         final FieldDerivativeStructure<T> z2;
  389.         switch(order) {
  390.             case 0 :
  391.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  392.                 x0 = factory.build(position.getX());
  393.                 y0 = factory.build(position.getY());
  394.                 z0 = factory.build(position.getZ());
  395.                 x1 = factory.build(velocity.getX());
  396.                 y1 = factory.build(velocity.getY());
  397.                 z1 = factory.build(velocity.getZ());
  398.                 x2 = factory.build(acceleration.getX());
  399.                 y2 = factory.build(acceleration.getY());
  400.                 z2 = factory.build(acceleration.getZ());
  401.                 break;
  402.             case 1 : {
  403.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  404.                 final T                r2            = position.getNormSq();
  405.                 final T                r             = r2.sqrt();
  406.                 final T                pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
  407.                 final T                a             = acceleration.getNorm();
  408.                 final T                aOr           = a.divide(r);
  409.                 final FieldVector3D<T> keplerianJerk = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  410.                                                                            aOr.negate(), velocity);
  411.                 x0 = factory.build(position.getX(),     velocity.getX());
  412.                 y0 = factory.build(position.getY(),     velocity.getY());
  413.                 z0 = factory.build(position.getZ(),     velocity.getZ());
  414.                 x1 = factory.build(velocity.getX(),     acceleration.getX());
  415.                 y1 = factory.build(velocity.getY(),     acceleration.getY());
  416.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
  417.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
  418.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
  419.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
  420.                 break;
  421.             }
  422.             case 2 : {
  423.                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
  424.                 final T                r2              = position.getNormSq();
  425.                 final T                r               = r2.sqrt();
  426.                 final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
  427.                 final T                a               = acceleration.getNorm();
  428.                 final T                aOr             = a.divide(r);
  429.                 final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
  430.                                                                              aOr.negate(), velocity);
  431.                 final T                v2              = velocity.getNormSq();
  432.                 final T                pa              = FieldVector3D.dotProduct(position, acceleration);
  433.                 final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
  434.                 final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
  435.                                                                              aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
  436.                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
  437.                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
  438.                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  439.                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  440.                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  441.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  442.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  443.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  444.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
  445.                 break;
  446.             }
  447.             default :
  448.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  449.         }

  450.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  451.                                         new FieldVector3D<>(x1, y1, z1),
  452.                                         new FieldVector3D<>(x2, y2, z2));

  453.     }

  454.     /** Estimate velocity between two positions.
  455.      * <p>Estimation is based on a simple fixed velocity translation
  456.      * during the time interval between the two positions.</p>
  457.      * @param start start position
  458.      * @param end end position
  459.      * @param dt time elapsed between the dates of the two positions
  460.      * @param <T> the type of the field elements
  461.      * @return velocity allowing to go from start to end positions
  462.      */
  463.     public static <T extends RealFieldElement<T>> FieldVector3D<T> estimateVelocity(final FieldVector3D<T> start,
  464.                                                                                     final FieldVector3D<T> end,
  465.                                                                                     final double dt) {
  466.         final double scale = 1.0 / dt;
  467.         return new FieldVector3D<>(scale, end, -scale, start);
  468.     }

  469.     /** Get a time-shifted state.
  470.      * <p>
  471.      * The state can be slightly shifted to close dates. This shift is based on
  472.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  473.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  474.      * for either small time shifts or coarse accuracy.
  475.      * </p>
  476.      * @param dt time shift in seconds
  477.      * @return a new state, shifted with respect to the instance (which is immutable)
  478.      */
  479.     public FieldPVCoordinates<T> shiftedBy(final double dt) {
  480.         return new FieldPVCoordinates<>(new FieldVector3D<>(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
  481.                                         new FieldVector3D<>(1, velocity, dt, acceleration),
  482.                                         acceleration);
  483.     }

  484.     /** Get a time-shifted state.
  485.      * <p>
  486.      * The state can be slightly shifted to close dates. This shift is based on
  487.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  488.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  489.      * for either small time shifts or coarse accuracy.
  490.      * </p>
  491.      * @param dt time shift in seconds
  492.      * @return a new state, shifted with respect to the instance (which is immutable)
  493.      */
  494.     public FieldPVCoordinates<T> shiftedBy(final T dt) {
  495.         final T one = dt.getField().getOne();
  496.         return new FieldPVCoordinates<>(new FieldVector3D<>(one, position,
  497.                                                             dt, velocity,
  498.                                                             dt.multiply(dt).multiply(0.5), acceleration),
  499.                                         new FieldVector3D<>(one, velocity,
  500.                                                             dt, acceleration),
  501.                                         acceleration);
  502.     }

  503.     /** Gets the position.
  504.      * @return the position vector (m).
  505.      */
  506.     public FieldVector3D<T> getPosition() {
  507.         return position;
  508.     }

  509.     /** Gets the velocity.
  510.      * @return the velocity vector (m/s).
  511.      */
  512.     public FieldVector3D<T> getVelocity() {
  513.         return velocity;
  514.     }

  515.     /** Gets the acceleration.
  516.      * @return the acceleration vector (m/s²).
  517.      */
  518.     public FieldVector3D<T> getAcceleration() {
  519.         return acceleration;
  520.     }

  521.     /** Gets the momentum.
  522.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  523.      * and &otimes; is cross product. To get the real physical angular momentum
  524.      * you need to multiply this vector by the mass.</p>
  525.      * <p>The returned vector is recomputed each time this method is called, it
  526.      * is not cached.</p>
  527.      * @return a new instance of the momentum vector (m²/s).
  528.      */
  529.     public FieldVector3D<T> getMomentum() {
  530.         return FieldVector3D.crossProduct(position, velocity);
  531.     }

  532.     /**
  533.      * Get the angular velocity (spin) of this point as seen from the origin.
  534.      *
  535.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  536.      * angular * momentum} and is computed by ω = p &times; v / ||p||²
  537.      *
  538.      * @return the angular velocity vector
  539.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  540.      *      Wikipedia</a>
  541.      */
  542.     public FieldVector3D<T> getAngularVelocity() {
  543.         return this.getMomentum().scalarMultiply(
  544.                 this.getPosition().getNormSq().reciprocal());
  545.     }

  546.     /** Get the opposite of the instance.
  547.      * @return a new position-velocity which is opposite to the instance
  548.      */
  549.     public FieldPVCoordinates<T> negate() {
  550.         return new FieldPVCoordinates<>(position.negate(), velocity.negate(), acceleration.negate());
  551.     }

  552.     /** Normalize the position part of the instance.
  553.      * <p>
  554.      * The computed coordinates first component (position) will be a
  555.      * normalized vector, the second component (velocity) will be the
  556.      * derivative of the first component (hence it will generally not
  557.      * be normalized), and the third component (acceleration) will be the
  558.      * derivative of the second component (hence it will generally not
  559.      * be normalized).
  560.      * </p>
  561.      * @return a new instance, with first component normalized and
  562.      * remaining component computed to have consistent derivatives
  563.      */
  564.     public FieldPVCoordinates<T> normalize() {
  565.         final T   inv     = position.getNorm().reciprocal();
  566.         final FieldVector3D<T> u       = new FieldVector3D<>(inv, position);
  567.         final FieldVector3D<T> v       = new FieldVector3D<>(inv, velocity);
  568.         final FieldVector3D<T> w       = new FieldVector3D<>(inv, acceleration);
  569.         final T   uv      = FieldVector3D.dotProduct(u, v);
  570.         final T   v2      = FieldVector3D.dotProduct(v, v);
  571.         final T   uw      = FieldVector3D.dotProduct(u, w);
  572.         final FieldVector3D<T> uDot    = new FieldVector3D<>(inv.getField().getOne(), v,
  573.                                                              uv.multiply(-1), u);
  574.         final FieldVector3D<T> uDotDot = new FieldVector3D<>(inv.getField().getOne(), w,
  575.                                                              uv.multiply(-2), v,
  576.                                                              uv.multiply(uv).multiply(3).subtract(v2).subtract(uw), u);
  577.         return new FieldPVCoordinates<>(u, uDot, uDotDot);
  578.     }

  579.     /** Compute the cross-product of two instances.
  580.      * @param pv2 second instances
  581.      * @return the cross product v1 ^ v2 as a new instance
  582.      */
  583.     public FieldPVCoordinates<T> crossProduct(final FieldPVCoordinates<T> pv2) {
  584.         final FieldVector3D<T> p1 = position;
  585.         final FieldVector3D<T> v1 = velocity;
  586.         final FieldVector3D<T> a1 = acceleration;
  587.         final FieldVector3D<T> p2 = pv2.position;
  588.         final FieldVector3D<T> v2 = pv2.velocity;
  589.         final FieldVector3D<T> a2 = pv2.acceleration;
  590.         return new FieldPVCoordinates<>(FieldVector3D.crossProduct(p1, p2),
  591.                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, v2),
  592.                                                             1, FieldVector3D.crossProduct(v1, p2)),
  593.                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, a2),
  594.                                                             2, FieldVector3D.crossProduct(v1, v2),
  595.                                                             1, FieldVector3D.crossProduct(a1, p2)));
  596.     }

  597.     /** Convert to a constant position-velocity.
  598.      * @return a constant position-velocity
  599.      */
  600.     public PVCoordinates toPVCoordinates() {
  601.         return new PVCoordinates(position.toVector3D(), velocity.toVector3D(), acceleration.toVector3D());
  602.     }

  603.     /** Return a string representation of this position/velocity pair.
  604.      * @return string representation of this position/velocity pair
  605.      */
  606.     public String toString() {
  607.         final String comma = ", ";
  608.         return new StringBuffer().append('{').append("P(").
  609.                                   append(position.getX().getReal()).append(comma).
  610.                                   append(position.getY().getReal()).append(comma).
  611.                                   append(position.getZ().getReal()).append("), V(").
  612.                                   append(velocity.getX().getReal()).append(comma).
  613.                                   append(velocity.getY().getReal()).append(comma).
  614.                                   append(velocity.getZ().getReal()).append("), A(").
  615.                                   append(acceleration.getX().getReal()).append(comma).
  616.                                   append(acceleration.getY().getReal()).append(comma).
  617.                                   append(acceleration.getZ().getReal()).append(")}").toString();
  618.     }

  619. }