PVCoordinates.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 java.io.Serializable;

  19. import org.hipparchus.analysis.differentiation.DSFactory;
  20. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  21. import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
  22. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  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/Acceleration triplets.
  30.  * <p>
  31.  * The state can be slightly shifted to close dates. This shift is based on
  32.  * a simple quadratic 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 AngularCoordinates}.
  38.  * </p>
  39.  * <p>Instances of this class are guaranteed to be immutable.</p>
  40.  * @author Fabien Maussion
  41.  * @author Luc Maisonobe
  42.  */
  43. public class PVCoordinates implements TimeShiftable<PVCoordinates>, Serializable {

  44.     /** Fixed position/velocity at origin (both p, v and a are zero vectors). */
  45.     public static final PVCoordinates ZERO = new PVCoordinates(Vector3D.ZERO, Vector3D.ZERO, Vector3D.ZERO);

  46.     /** Serializable UID. */
  47.     private static final long serialVersionUID = 20140407L;

  48.     /** The position. */
  49.     private final Vector3D position;

  50.     /** The velocity. */
  51.     private final Vector3D velocity;

  52.     /** The acceleration. */
  53.     private final Vector3D acceleration;

  54.     /** Simple constructor.
  55.      * <p> Set the Coordinates to default : (0 0 0), (0 0 0), (0 0 0).</p>
  56.      */
  57.     public PVCoordinates() {
  58.         position     = Vector3D.ZERO;
  59.         velocity     = Vector3D.ZERO;
  60.         acceleration = Vector3D.ZERO;
  61.     }

  62.     /** Builds a PVCoordinates triplet with zero acceleration.
  63.      * <p>Acceleration is set to zero</p>
  64.      * @param position the position vector (m)
  65.      * @param velocity the velocity vector (m/s)
  66.      */
  67.     public PVCoordinates(final Vector3D position, final Vector3D velocity) {
  68.         this.position     = position;
  69.         this.velocity     = velocity;
  70.         this.acceleration = Vector3D.ZERO;
  71.     }

  72.     /** Builds a PVCoordinates triplet.
  73.      * @param position the position vector (m)
  74.      * @param velocity the velocity vector (m/s)
  75.      * @param acceleration the acceleration vector (m/s²)
  76.      */
  77.     public PVCoordinates(final Vector3D position, final Vector3D velocity, final Vector3D acceleration) {
  78.         this.position     = position;
  79.         this.velocity     = velocity;
  80.         this.acceleration = acceleration;
  81.     }

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

  93.     /** Subtractive constructor.
  94.      * <p>Build a relative PVCoordinates from a start and an end position.</p>
  95.      * <p>The PVCoordinates built will be end - start.</p>
  96.      * @param start Starting PVCoordinates
  97.      * @param end ending PVCoordinates
  98.      */
  99.     public PVCoordinates(final PVCoordinates start, final PVCoordinates end) {
  100.         this.position     = end.position.subtract(start.position);
  101.         this.velocity     = end.velocity.subtract(start.velocity);
  102.         this.acceleration = end.acceleration.subtract(start.acceleration);
  103.     }

  104.     /** Linear constructor.
  105.      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
  106.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
  107.      * @param a1 first scale factor
  108.      * @param pv1 first base (unscaled) PVCoordinates
  109.      * @param a2 second scale factor
  110.      * @param pv2 second base (unscaled) PVCoordinates
  111.      */
  112.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  113.                          final double a2, final PVCoordinates pv2) {
  114.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position);
  115.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity);
  116.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration);
  117.     }

  118.     /** Linear constructor.
  119.      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
  120.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
  121.      * @param a1 first scale factor
  122.      * @param pv1 first base (unscaled) PVCoordinates
  123.      * @param a2 second scale factor
  124.      * @param pv2 second base (unscaled) PVCoordinates
  125.      * @param a3 third scale factor
  126.      * @param pv3 third base (unscaled) PVCoordinates
  127.      */
  128.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  129.                          final double a2, final PVCoordinates pv2,
  130.                          final double a3, final PVCoordinates pv3) {
  131.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
  132.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
  133.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
  134.     }

  135.     /** Linear constructor.
  136.      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
  137.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
  138.      * @param a1 first scale factor
  139.      * @param pv1 first base (unscaled) PVCoordinates
  140.      * @param a2 second scale factor
  141.      * @param pv2 second base (unscaled) PVCoordinates
  142.      * @param a3 third scale factor
  143.      * @param pv3 third base (unscaled) PVCoordinates
  144.      * @param a4 fourth scale factor
  145.      * @param pv4 fourth base (unscaled) PVCoordinates
  146.      */
  147.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  148.                          final double a2, final PVCoordinates pv2,
  149.                          final double a3, final PVCoordinates pv3,
  150.                          final double a4, final PVCoordinates pv4) {
  151.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,
  152.                                     a3, pv3.position,     a4, pv4.position);
  153.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,
  154.                                     a3, pv3.velocity,     a4, pv4.velocity);
  155.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration,
  156.                                     a3, pv3.acceleration, a4, pv4.acceleration);
  157.     }

  158.     /** Builds a PVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  159.      * <p>
  160.      * The vector components must have time as their only derivation parameter and
  161.      * have consistent derivation orders.
  162.      * </p>
  163.      * @param p vector with time-derivatives embedded within the coordinates
  164.      */
  165.     public PVCoordinates(final FieldVector3D<DerivativeStructure> p) {
  166.         position = new Vector3D(p.getX().getReal(), p.getY().getReal(), p.getZ().getReal());
  167.         if (p.getX().getOrder() >= 1) {
  168.             velocity = new Vector3D(p.getX().getPartialDerivative(1),
  169.                                     p.getY().getPartialDerivative(1),
  170.                                     p.getZ().getPartialDerivative(1));
  171.             if (p.getX().getOrder() >= 2) {
  172.                 acceleration = new Vector3D(p.getX().getPartialDerivative(2),
  173.                                             p.getY().getPartialDerivative(2),
  174.                                             p.getZ().getPartialDerivative(2));
  175.             } else {
  176.                 acceleration = Vector3D.ZERO;
  177.             }
  178.         } else {
  179.             velocity     = Vector3D.ZERO;
  180.             acceleration = Vector3D.ZERO;
  181.         }
  182.     }

  183.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  184.      * <p>
  185.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  186.      * to the user-specified order.
  187.      * </p>
  188.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  189.      * @return vector with time-derivatives embedded within the coordinates
  190.      */
  191.     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order) {

  192.         final DSFactory factory;
  193.         final DerivativeStructure x;
  194.         final DerivativeStructure y;
  195.         final DerivativeStructure z;
  196.         switch(order) {
  197.             case 0 :
  198.                 factory = new DSFactory(1, order);
  199.                 x = factory.build(position.getX());
  200.                 y = factory.build(position.getY());
  201.                 z = factory.build(position.getZ());
  202.                 break;
  203.             case 1 :
  204.                 factory = new DSFactory(1, order);
  205.                 x = factory.build(position.getX(), velocity.getX());
  206.                 y = factory.build(position.getY(), velocity.getY());
  207.                 z = factory.build(position.getZ(), velocity.getZ());
  208.                 break;
  209.             case 2 :
  210.                 factory = new DSFactory(1, order);
  211.                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
  212.                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
  213.                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
  214.                 break;
  215.             default :
  216.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  217.         }

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

  219.     }

  220.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative1}&gt;.
  221.      * <p>
  222.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  223.      * to the order 1.
  224.      * </p>
  225.      * @return vector with time-derivatives embedded within the coordinates
  226.      * @see #toUnivariateDerivative2Vector()
  227.      * @since 10.2
  228.      */
  229.     public FieldVector3D<UnivariateDerivative1> toUnivariateDerivative1Vector() {

  230.         final UnivariateDerivative1 x = new UnivariateDerivative1(position.getX(), velocity.getX());
  231.         final UnivariateDerivative1 y = new UnivariateDerivative1(position.getY(), velocity.getY());
  232.         final UnivariateDerivative1 z = new UnivariateDerivative1(position.getZ(), velocity.getZ());

  233.         return new FieldVector3D<>(x, y, z);
  234.     }

  235.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative2}&gt;.
  236.      * <p>
  237.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  238.      * to the order 2.
  239.      * </p>
  240.      * @return vector with time-derivatives embedded within the coordinates
  241.      * @see #toUnivariateDerivative1Vector()
  242.      * @since 10.2
  243.      */
  244.     public FieldVector3D<UnivariateDerivative2> toUnivariateDerivative2Vector() {

  245.         final UnivariateDerivative2 x = new UnivariateDerivative2(position.getX(), velocity.getX(), acceleration.getX());
  246.         final UnivariateDerivative2 y = new UnivariateDerivative2(position.getY(), velocity.getY(), acceleration.getY());
  247.         final UnivariateDerivative2 z = new UnivariateDerivative2(position.getZ(), velocity.getZ(), acceleration.getZ());

  248.         return new FieldVector3D<>(x, y, z);
  249.     }

  250.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link DerivativeStructure}&gt;.
  251.      * <p>
  252.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  253.      * to the user-specified order. As both the instance components {@link #getPosition() position},
  254.      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
  255.      * {@link DerivativeStructure#getPartialDerivative(int...) derivatives} of the components
  256.      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
  257.      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
  258.      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
  259.      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
  260.      * </p>
  261.      * <p>
  262.      * If derivation order is 1, the first derivative of acceleration will be computed as a
  263.      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
  264.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  265.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  266.      * </p>
  267.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  268.      * @return pv coordinates with time-derivatives embedded within the coordinates
  269.      * @since 9.2
  270.      */
  271.     public FieldPVCoordinates<DerivativeStructure> toDerivativeStructurePV(final int order) {

  272.         final DSFactory factory;
  273.         final DerivativeStructure x0;
  274.         final DerivativeStructure y0;
  275.         final DerivativeStructure z0;
  276.         final DerivativeStructure x1;
  277.         final DerivativeStructure y1;
  278.         final DerivativeStructure z1;
  279.         final DerivativeStructure x2;
  280.         final DerivativeStructure y2;
  281.         final DerivativeStructure z2;
  282.         switch(order) {
  283.             case 0 :
  284.                 factory = new DSFactory(1, order);
  285.                 x0 = factory.build(position.getX());
  286.                 y0 = factory.build(position.getY());
  287.                 z0 = factory.build(position.getZ());
  288.                 x1 = factory.build(velocity.getX());
  289.                 y1 = factory.build(velocity.getY());
  290.                 z1 = factory.build(velocity.getZ());
  291.                 x2 = factory.build(acceleration.getX());
  292.                 y2 = factory.build(acceleration.getY());
  293.                 z2 = factory.build(acceleration.getZ());
  294.                 break;
  295.             case 1 : {
  296.                 factory = new DSFactory(1, order);
  297.                 final double   r2            = position.getNormSq();
  298.                 final double   r             = FastMath.sqrt(r2);
  299.                 final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
  300.                 final double   a             = acceleration.getNorm();
  301.                 final double   aOr           = a / r;
  302.                 final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  303.                 x0 = factory.build(position.getX(),     velocity.getX());
  304.                 y0 = factory.build(position.getY(),     velocity.getY());
  305.                 z0 = factory.build(position.getZ(),     velocity.getZ());
  306.                 x1 = factory.build(velocity.getX(),     acceleration.getX());
  307.                 y1 = factory.build(velocity.getY(),     acceleration.getY());
  308.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
  309.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
  310.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
  311.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
  312.                 break;
  313.             }
  314.             case 2 : {
  315.                 factory = new DSFactory(1, order);
  316.                 final double   r2              = position.getNormSq();
  317.                 final double   r               = FastMath.sqrt(r2);
  318.                 final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
  319.                 final double   a               = acceleration.getNorm();
  320.                 final double   aOr             = a / r;
  321.                 final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  322.                 final double   v2              = velocity.getNormSq();
  323.                 final double   pa              = Vector3D.dotProduct(position, acceleration);
  324.                 final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
  325.                 final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
  326.                                                               4 * aOr * pvOr2 - aj / (a * r), velocity);
  327.                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
  328.                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
  329.                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  330.                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  331.                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  332.                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  333.                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  334.                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  335.                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
  336.                 break;
  337.             }
  338.             default :
  339.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  340.         }

  341.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  342.                                         new FieldVector3D<>(x1, y1, z1),
  343.                                         new FieldVector3D<>(x2, y2, z2));

  344.     }

  345.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative1}&gt;.
  346.      * <p>
  347.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  348.      * to the order 1.
  349.      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
  350.      * </p>
  351.      * @return pv coordinates with time-derivatives embedded within the coordinates
  352.      * @since 10.2
  353.      */
  354.     public FieldPVCoordinates<UnivariateDerivative1> toUnivariateDerivative1PV() {

  355.         final double   r2            = position.getNormSq();
  356.         final double   r             = FastMath.sqrt(r2);
  357.         final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
  358.         final double   a             = acceleration.getNorm();
  359.         final double   aOr           = a / r;
  360.         final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);

  361.         final UnivariateDerivative1 x0 = new UnivariateDerivative1(position.getX(),     velocity.getX());
  362.         final UnivariateDerivative1 y0 = new UnivariateDerivative1(position.getY(),     velocity.getY());
  363.         final UnivariateDerivative1 z0 = new UnivariateDerivative1(position.getZ(),     velocity.getZ());
  364.         final UnivariateDerivative1 x1 = new UnivariateDerivative1(velocity.getX(),     acceleration.getX());
  365.         final UnivariateDerivative1 y1 = new UnivariateDerivative1(velocity.getY(),     acceleration.getY());
  366.         final UnivariateDerivative1 z1 = new UnivariateDerivative1(velocity.getZ(),     acceleration.getZ());
  367.         final UnivariateDerivative1 x2 = new UnivariateDerivative1(acceleration.getX(), keplerianJerk.getX());
  368.         final UnivariateDerivative1 y2 = new UnivariateDerivative1(acceleration.getY(), keplerianJerk.getY());
  369.         final UnivariateDerivative1 z2 = new UnivariateDerivative1(acceleration.getZ(), keplerianJerk.getZ());

  370.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  371.                                         new FieldVector3D<>(x1, y1, z1),
  372.                                         new FieldVector3D<>(x2, y2, z2));

  373.     }

  374.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative2}&gt;.
  375.      * <p>
  376.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  377.      * to the order 2.
  378.      * As derivation order is 2, the second derivative of velocity (which
  379.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  380.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  381.      * </p>
  382.      * @return pv coordinates with time-derivatives embedded within the coordinates
  383.      * @since 10.2
  384.      */
  385.     public FieldPVCoordinates<UnivariateDerivative2> toUnivariateDerivative2PV() {

  386.         final double   r2              = position.getNormSq();
  387.         final double   r               = FastMath.sqrt(r2);
  388.         final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
  389.         final double   a               = acceleration.getNorm();
  390.         final double   aOr             = a / r;
  391.         final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  392.         final double   v2              = velocity.getNormSq();
  393.         final double   pa              = Vector3D.dotProduct(position, acceleration);
  394.         final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
  395.         final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
  396.                                                       4 * aOr * pvOr2 - aj / (a * r), velocity);

  397.         final UnivariateDerivative2 x0 = new UnivariateDerivative2(position.getX(),     velocity.getX(),      acceleration.getX());
  398.         final UnivariateDerivative2 y0 = new UnivariateDerivative2(position.getY(),     velocity.getY(),      acceleration.getY());
  399.         final UnivariateDerivative2 z0 = new UnivariateDerivative2(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  400.         final UnivariateDerivative2 x1 = new UnivariateDerivative2(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  401.         final UnivariateDerivative2 y1 = new UnivariateDerivative2(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  402.         final UnivariateDerivative2 z1 = new UnivariateDerivative2(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  403.         final UnivariateDerivative2 x2 = new UnivariateDerivative2(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  404.         final UnivariateDerivative2 y2 = new UnivariateDerivative2(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  405.         final UnivariateDerivative2 z2 = new UnivariateDerivative2(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());

  406.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  407.                                         new FieldVector3D<>(x1, y1, z1),
  408.                                         new FieldVector3D<>(x2, y2, z2));

  409.     }

  410.     /** Estimate velocity between two positions.
  411.      * <p>Estimation is based on a simple fixed velocity translation
  412.      * during the time interval between the two positions.</p>
  413.      * @param start start position
  414.      * @param end end position
  415.      * @param dt time elapsed between the dates of the two positions
  416.      * @return velocity allowing to go from start to end positions
  417.      */
  418.     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
  419.         final double scale = 1.0 / dt;
  420.         return new Vector3D(scale, end, -scale, start);
  421.     }

  422.     /** Get a time-shifted state.
  423.      * <p>
  424.      * The state can be slightly shifted to close dates. This shift is based on
  425.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  426.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  427.      * for either small time shifts or coarse accuracy.
  428.      * </p>
  429.      * @param dt time shift in seconds
  430.      * @return a new state, shifted with respect to the instance (which is immutable)
  431.      */
  432.     public PVCoordinates shiftedBy(final double dt) {
  433.         return new PVCoordinates(new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
  434.                                  new Vector3D(1, velocity, dt, acceleration),
  435.                                  acceleration);
  436.     }

  437.     /** Gets the position.
  438.      * @return the position vector (m).
  439.      */
  440.     public Vector3D getPosition() {
  441.         return position;
  442.     }

  443.     /** Gets the velocity.
  444.      * @return the velocity vector (m/s).
  445.      */
  446.     public Vector3D getVelocity() {
  447.         return velocity;
  448.     }

  449.     /** Gets the acceleration.
  450.      * @return the acceleration vector (m/s²).
  451.      */
  452.     public Vector3D getAcceleration() {
  453.         return acceleration;
  454.     }

  455.     /** Gets the momentum.
  456.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  457.      * and &otimes; is cross product. To get the real physical angular momentum
  458.      * you need to multiply this vector by the mass.</p>
  459.      * <p>The returned vector is recomputed each time this method is called, it
  460.      * is not cached.</p>
  461.      * @return a new instance of the momentum vector (m²/s).
  462.      */
  463.     public Vector3D getMomentum() {
  464.         return Vector3D.crossProduct(position, velocity);
  465.     }

  466.     /**
  467.      * Get the angular velocity (spin) of this point as seen from the origin.
  468.      *
  469.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  470.      * angular momentum} and is computed by ω = p &times; v / ||p||²
  471.      *
  472.      * @return the angular velocity vector
  473.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  474.      *      Wikipedia</a>
  475.      */
  476.     public Vector3D getAngularVelocity() {
  477.         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
  478.     }

  479.     /** Get the opposite of the instance.
  480.      * @return a new position-velocity which is opposite to the instance
  481.      */
  482.     public PVCoordinates negate() {
  483.         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
  484.     }

  485.     /** Normalize the position part of the instance.
  486.      * <p>
  487.      * The computed coordinates first component (position) will be a
  488.      * normalized vector, the second component (velocity) will be the
  489.      * derivative of the first component (hence it will generally not
  490.      * be normalized), and the third component (acceleration) will be the
  491.      * derivative of the second component (hence it will generally not
  492.      * be normalized).
  493.      * </p>
  494.      * @return a new instance, with first component normalized and
  495.      * remaining component computed to have consistent derivatives
  496.      */
  497.     public PVCoordinates normalize() {
  498.         final double   inv     = 1.0 / position.getNorm();
  499.         final Vector3D u       = new Vector3D(inv, position);
  500.         final Vector3D v       = new Vector3D(inv, velocity);
  501.         final Vector3D w       = new Vector3D(inv, acceleration);
  502.         final double   uv      = Vector3D.dotProduct(u, v);
  503.         final double   v2      = Vector3D.dotProduct(v, v);
  504.         final double   uw      = Vector3D.dotProduct(u, w);
  505.         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
  506.         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
  507.         return new PVCoordinates(u, uDot, uDotDot);
  508.     }

  509.     /** Compute the cross-product of two instances.
  510.      * @param pv1 first instances
  511.      * @param pv2 second instances
  512.      * @return the cross product v1 ^ v2 as a new instance
  513.      */
  514.     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
  515.         final Vector3D p1 = pv1.position;
  516.         final Vector3D v1 = pv1.velocity;
  517.         final Vector3D a1 = pv1.acceleration;
  518.         final Vector3D p2 = pv2.position;
  519.         final Vector3D v2 = pv2.velocity;
  520.         final Vector3D a2 = pv2.acceleration;
  521.         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
  522.                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
  523.                                               1, Vector3D.crossProduct(v1, p2)),
  524.                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
  525.                                               2, Vector3D.crossProduct(v1, v2),
  526.                                               1, Vector3D.crossProduct(a1, p2)));
  527.     }

  528.     /** Return a string representation of this position/velocity pair.
  529.      * @return string representation of this position/velocity pair
  530.      */
  531.     public String toString() {
  532.         final String comma = ", ";
  533.         return new StringBuffer().append('{').append("P(").
  534.                 append(position.getX()).append(comma).
  535.                 append(position.getY()).append(comma).
  536.                 append(position.getZ()).append("), V(").
  537.                 append(velocity.getX()).append(comma).
  538.                 append(velocity.getY()).append(comma).
  539.                 append(velocity.getZ()).append("), A(").
  540.                 append(acceleration.getX()).append(comma).
  541.                 append(acceleration.getY()).append(comma).
  542.                 append(acceleration.getZ()).append(")}").toString();
  543.     }

  544.     /** Replace the instance with a data transfer object for serialization.
  545.      * @return data transfer object that will be serialized
  546.      */
  547.     private Object writeReplace() {
  548.         return new DTO(this);
  549.     }

  550.     /** Internal class used only for serialization. */
  551.     private static class DTO implements Serializable {

  552.         /** Serializable UID. */
  553.         private static final long serialVersionUID = 20140723L;

  554.         /** Double values. */
  555.         private double[] d;

  556.         /** Simple constructor.
  557.          * @param pv instance to serialize
  558.          */
  559.         private DTO(final PVCoordinates pv) {
  560.             this.d = new double[] {
  561.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  562.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  563.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
  564.             };
  565.         }

  566.         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
  567.          * @return replacement {@link PVCoordinates}
  568.          */
  569.         private Object readResolve() {
  570.             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
  571.                                      new Vector3D(d[3], d[4], d[5]),
  572.                                      new Vector3D(d[6], d[7], d[8]));
  573.         }

  574.     }

  575. }