PVCoordinates.java

  1. /* Copyright 2002-2023 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.Derivative;
  21. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  22. import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
  23. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.Blendable;
  28. import org.hipparchus.util.FastMath;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.time.TimeShiftable;

  32. /** Simple container for Position/Velocity/Acceleration triplets.
  33.  * <p>
  34.  * The state can be slightly shifted to close dates. This shift is based on
  35.  * a simple quadratic model. It is <em>not</em> intended as a replacement for
  36.  * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  37.  * for either small time shifts or coarse accuracy.
  38.  * </p>
  39.  * <p>
  40.  * This class is the angular counterpart to {@link AngularCoordinates}.
  41.  * </p>
  42.  * <p>Instances of this class are guaranteed to be immutable.</p>
  43.  * @author Fabien Maussion
  44.  * @author Luc Maisonobe
  45.  */
  46. public class PVCoordinates implements TimeShiftable<PVCoordinates>, Blendable<PVCoordinates>, Serializable {

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

  49.     /** Serializable UID. */
  50.     private static final long serialVersionUID = 20140407L;

  51.     /** The position. */
  52.     private final Vector3D position;

  53.     /** The velocity. */
  54.     private final Vector3D velocity;

  55.     /** The acceleration. */
  56.     private final Vector3D acceleration;

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

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

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

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

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

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

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

  138.     /** Linear constructor.
  139.      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
  140.      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</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.      * @param a3 third scale factor
  146.      * @param pv3 third base (unscaled) PVCoordinates
  147.      * @param a4 fourth scale factor
  148.      * @param pv4 fourth base (unscaled) PVCoordinates
  149.      */
  150.     public PVCoordinates(final double a1, final PVCoordinates pv1,
  151.                          final double a2, final PVCoordinates pv2,
  152.                          final double a3, final PVCoordinates pv3,
  153.                          final double a4, final PVCoordinates pv4) {
  154.         position     = new Vector3D(a1, pv1.position,     a2, pv2.position,
  155.                                     a3, pv3.position,     a4, pv4.position);
  156.         velocity     = new Vector3D(a1, pv1.velocity,     a2, pv2.velocity,
  157.                                     a3, pv3.velocity,     a4, pv4.velocity);
  158.         acceleration = new Vector3D(a1, pv1.acceleration, a2, pv2.acceleration,
  159.                                     a3, pv3.acceleration, a4, pv4.acceleration);
  160.     }

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

  187.     /**
  188.      * Builds PV coordinates with the givne position, zero velocity, and zero
  189.      * acceleration.
  190.      *
  191.      * @param position position vector (m)
  192.      */
  193.     public PVCoordinates(final Vector3D position) {
  194.         this(position, Vector3D.ZERO);
  195.     }

  196.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link DerivativeStructure}&gt;.
  197.      * <p>
  198.      * The {@link DerivativeStructure} coordinates correspond to time-derivatives up
  199.      * to the user-specified order.
  200.      * </p>
  201.      * @param order derivation order for the vector components (must be either 0, 1 or 2)
  202.      * @return vector with time-derivatives embedded within the coordinates
  203.      */
  204.     public FieldVector3D<DerivativeStructure> toDerivativeStructureVector(final int order) {

  205.         final DSFactory factory;
  206.         final DerivativeStructure x;
  207.         final DerivativeStructure y;
  208.         final DerivativeStructure z;
  209.         switch (order) {
  210.             case 0 :
  211.                 factory = new DSFactory(1, order);
  212.                 x = factory.build(position.getX());
  213.                 y = factory.build(position.getY());
  214.                 z = factory.build(position.getZ());
  215.                 break;
  216.             case 1 :
  217.                 factory = new DSFactory(1, order);
  218.                 x = factory.build(position.getX(), velocity.getX());
  219.                 y = factory.build(position.getY(), velocity.getY());
  220.                 z = factory.build(position.getZ(), velocity.getZ());
  221.                 break;
  222.             case 2 :
  223.                 factory = new DSFactory(1, order);
  224.                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
  225.                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
  226.                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
  227.                 break;
  228.             default :
  229.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  230.         }

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

  232.     }

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

  243.         final UnivariateDerivative1 x = new UnivariateDerivative1(position.getX(), velocity.getX());
  244.         final UnivariateDerivative1 y = new UnivariateDerivative1(position.getY(), velocity.getY());
  245.         final UnivariateDerivative1 z = new UnivariateDerivative1(position.getZ(), velocity.getZ());

  246.         return new FieldVector3D<>(x, y, z);
  247.     }

  248.     /** Transform the instance to a {@link FieldVector3D}&lt;{@link UnivariateDerivative2}&gt;.
  249.      * <p>
  250.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  251.      * to the order 2.
  252.      * </p>
  253.      * @return vector with time-derivatives embedded within the coordinates
  254.      * @see #toUnivariateDerivative1Vector()
  255.      * @since 10.2
  256.      */
  257.     public FieldVector3D<UnivariateDerivative2> toUnivariateDerivative2Vector() {

  258.         final UnivariateDerivative2 x = new UnivariateDerivative2(position.getX(), velocity.getX(), acceleration.getX());
  259.         final UnivariateDerivative2 y = new UnivariateDerivative2(position.getY(), velocity.getY(), acceleration.getY());
  260.         final UnivariateDerivative2 z = new UnivariateDerivative2(position.getZ(), velocity.getZ(), acceleration.getZ());

  261.         return new FieldVector3D<>(x, y, z);
  262.     }

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

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

  354.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  355.                                         new FieldVector3D<>(x1, y1, z1),
  356.                                         new FieldVector3D<>(x2, y2, z2));

  357.     }

  358.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative1}&gt;.
  359.      * <p>
  360.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  361.      * to the order 1.
  362.      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
  363.      * </p>
  364.      * @return pv coordinates with time-derivatives embedded within the coordinates
  365.      * @since 10.2
  366.      */
  367.     public FieldPVCoordinates<UnivariateDerivative1> toUnivariateDerivative1PV() {

  368.         final double   r2            = position.getNormSq();
  369.         final double   r             = FastMath.sqrt(r2);
  370.         final double   pvOr2         = Vector3D.dotProduct(position, velocity) / r2;
  371.         final double   a             = acceleration.getNorm();
  372.         final double   aOr           = a / r;
  373.         final Vector3D keplerianJerk = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);

  374.         final UnivariateDerivative1 x0 = new UnivariateDerivative1(position.getX(),     velocity.getX());
  375.         final UnivariateDerivative1 y0 = new UnivariateDerivative1(position.getY(),     velocity.getY());
  376.         final UnivariateDerivative1 z0 = new UnivariateDerivative1(position.getZ(),     velocity.getZ());
  377.         final UnivariateDerivative1 x1 = new UnivariateDerivative1(velocity.getX(),     acceleration.getX());
  378.         final UnivariateDerivative1 y1 = new UnivariateDerivative1(velocity.getY(),     acceleration.getY());
  379.         final UnivariateDerivative1 z1 = new UnivariateDerivative1(velocity.getZ(),     acceleration.getZ());
  380.         final UnivariateDerivative1 x2 = new UnivariateDerivative1(acceleration.getX(), keplerianJerk.getX());
  381.         final UnivariateDerivative1 y2 = new UnivariateDerivative1(acceleration.getY(), keplerianJerk.getY());
  382.         final UnivariateDerivative1 z2 = new UnivariateDerivative1(acceleration.getZ(), keplerianJerk.getZ());

  383.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  384.                                         new FieldVector3D<>(x1, y1, z1),
  385.                                         new FieldVector3D<>(x2, y2, z2));

  386.     }

  387.     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link UnivariateDerivative2}&gt;.
  388.      * <p>
  389.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  390.      * to the order 2.
  391.      * As derivation order is 2, the second derivative of velocity (which
  392.      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
  393.      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
  394.      * </p>
  395.      * @return pv coordinates with time-derivatives embedded within the coordinates
  396.      * @since 10.2
  397.      */
  398.     public FieldPVCoordinates<UnivariateDerivative2> toUnivariateDerivative2PV() {

  399.         final double   r2              = position.getNormSq();
  400.         final double   r               = FastMath.sqrt(r2);
  401.         final double   pvOr2           = Vector3D.dotProduct(position, velocity) / r2;
  402.         final double   a               = acceleration.getNorm();
  403.         final double   aOr             = a / r;
  404.         final Vector3D keplerianJerk   = new Vector3D(-3 * pvOr2, acceleration, -aOr, velocity);
  405.         final double   v2              = velocity.getNormSq();
  406.         final double   pa              = Vector3D.dotProduct(position, acceleration);
  407.         final double   aj              = Vector3D.dotProduct(acceleration, keplerianJerk);
  408.         final Vector3D keplerianJounce = new Vector3D(-3 * (v2 + pa) / r2 + 15 * pvOr2 * pvOr2 - aOr, acceleration,
  409.                                                       4 * aOr * pvOr2 - aj / (a * r), velocity);

  410.         final UnivariateDerivative2 x0 = new UnivariateDerivative2(position.getX(),     velocity.getX(),      acceleration.getX());
  411.         final UnivariateDerivative2 y0 = new UnivariateDerivative2(position.getY(),     velocity.getY(),      acceleration.getY());
  412.         final UnivariateDerivative2 z0 = new UnivariateDerivative2(position.getZ(),     velocity.getZ(),      acceleration.getZ());
  413.         final UnivariateDerivative2 x1 = new UnivariateDerivative2(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
  414.         final UnivariateDerivative2 y1 = new UnivariateDerivative2(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
  415.         final UnivariateDerivative2 z1 = new UnivariateDerivative2(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
  416.         final UnivariateDerivative2 x2 = new UnivariateDerivative2(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
  417.         final UnivariateDerivative2 y2 = new UnivariateDerivative2(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
  418.         final UnivariateDerivative2 z2 = new UnivariateDerivative2(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());

  419.         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
  420.                                         new FieldVector3D<>(x1, y1, z1),
  421.                                         new FieldVector3D<>(x2, y2, z2));

  422.     }

  423.     /** Estimate velocity between two positions.
  424.      * <p>Estimation is based on a simple fixed velocity translation
  425.      * during the time interval between the two positions.</p>
  426.      * @param start start position
  427.      * @param end end position
  428.      * @param dt time elapsed between the dates of the two positions
  429.      * @return velocity allowing to go from start to end positions
  430.      */
  431.     public static Vector3D estimateVelocity(final Vector3D start, final Vector3D end, final double dt) {
  432.         final double scale = 1.0 / dt;
  433.         return new Vector3D(scale, end, -scale, start);
  434.     }

  435.     /** Get a time-shifted state.
  436.      * <p>
  437.      * The state can be slightly shifted to close dates. This shift is based on
  438.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement for
  439.      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
  440.      * for either small time shifts or coarse accuracy.
  441.      * </p>
  442.      * @param dt time shift in seconds
  443.      * @return a new state, shifted with respect to the instance (which is immutable)
  444.      */
  445.     public PVCoordinates shiftedBy(final double dt) {
  446.         return new PVCoordinates(positionShiftedBy(dt),
  447.                                  new Vector3D(1, velocity, dt, acceleration),
  448.                                  acceleration);
  449.     }

  450.     /**
  451.      * Get a time-shifted position. Same as {@link #shiftedBy(double)} except
  452.      * that only the sifted position is returned.
  453.      * <p>
  454.      * The state can be slightly shifted to close dates. This shift is based on
  455.      * a simple Taylor expansion. It is <em>not</em> intended as a replacement
  456.      * for proper orbit propagation (it is not even Keplerian!) but should be
  457.      * sufficient for either small time shifts or coarse accuracy.
  458.      * </p>
  459.      *
  460.      * @param dt time shift in seconds
  461.      * @return a new state, shifted with respect to the instance (which is
  462.      * immutable)
  463.      */
  464.     public Vector3D positionShiftedBy(final double dt) {
  465.         return new Vector3D(1, position, dt, velocity, 0.5 * dt * dt, acceleration);
  466.     }

  467.     /** Gets the position.
  468.      * @return the position vector (m).
  469.      */
  470.     public Vector3D getPosition() {
  471.         return position;
  472.     }

  473.     /** Gets the velocity.
  474.      * @return the velocity vector (m/s).
  475.      */
  476.     public Vector3D getVelocity() {
  477.         return velocity;
  478.     }

  479.     /** Gets the acceleration.
  480.      * @return the acceleration vector (m/s²).
  481.      */
  482.     public Vector3D getAcceleration() {
  483.         return acceleration;
  484.     }

  485.     /** Gets the momentum.
  486.      * <p>This vector is the p &otimes; v where p is position, v is velocity
  487.      * and &otimes; is cross product. To get the real physical angular momentum
  488.      * you need to multiply this vector by the mass.</p>
  489.      * <p>The returned vector is recomputed each time this method is called, it
  490.      * is not cached.</p>
  491.      * @return a new instance of the momentum vector (m²/s).
  492.      */
  493.     public Vector3D getMomentum() {
  494.         return Vector3D.crossProduct(position, velocity);
  495.     }

  496.     /**
  497.      * Get the angular velocity (spin) of this point as seen from the origin.
  498.      *
  499.      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
  500.      * angular momentum} and is computed by ω = p &times; v / ||p||²
  501.      *
  502.      * @return the angular velocity vector
  503.      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
  504.      *      Wikipedia</a>
  505.      */
  506.     public Vector3D getAngularVelocity() {
  507.         return this.getMomentum().scalarMultiply(1.0 / this.getPosition().getNormSq());
  508.     }

  509.     /** Get the opposite of the instance.
  510.      * @return a new position-velocity which is opposite to the instance
  511.      */
  512.     public PVCoordinates negate() {
  513.         return new PVCoordinates(position.negate(), velocity.negate(), acceleration.negate());
  514.     }

  515.     /** Normalize the position part of the instance.
  516.      * <p>
  517.      * The computed coordinates first component (position) will be a
  518.      * normalized vector, the second component (velocity) will be the
  519.      * derivative of the first component (hence it will generally not
  520.      * be normalized), and the third component (acceleration) will be the
  521.      * derivative of the second component (hence it will generally not
  522.      * be normalized).
  523.      * </p>
  524.      * @return a new instance, with first component normalized and
  525.      * remaining component computed to have consistent derivatives
  526.      */
  527.     public PVCoordinates normalize() {
  528.         final double   inv     = 1.0 / position.getNorm();
  529.         final Vector3D u       = new Vector3D(inv, position);
  530.         final Vector3D v       = new Vector3D(inv, velocity);
  531.         final Vector3D w       = new Vector3D(inv, acceleration);
  532.         final double   uv      = Vector3D.dotProduct(u, v);
  533.         final double   v2      = Vector3D.dotProduct(v, v);
  534.         final double   uw      = Vector3D.dotProduct(u, w);
  535.         final Vector3D uDot    = new Vector3D(1, v, -uv, u);
  536.         final Vector3D uDotDot = new Vector3D(1, w, -2 * uv, v, 3 * uv * uv - v2 - uw, u);
  537.         return new PVCoordinates(u, uDot, uDotDot);
  538.     }

  539.     /** Compute the cross-product of two instances.
  540.      * @param pv1 first instances
  541.      * @param pv2 second instances
  542.      * @return the cross product v1 ^ v2 as a new instance
  543.      */
  544.     public static PVCoordinates crossProduct(final PVCoordinates pv1, final PVCoordinates pv2) {
  545.         final Vector3D p1 = pv1.position;
  546.         final Vector3D v1 = pv1.velocity;
  547.         final Vector3D a1 = pv1.acceleration;
  548.         final Vector3D p2 = pv2.position;
  549.         final Vector3D v2 = pv2.velocity;
  550.         final Vector3D a2 = pv2.acceleration;
  551.         return new PVCoordinates(Vector3D.crossProduct(p1, p2),
  552.                                  new Vector3D(1, Vector3D.crossProduct(p1, v2),
  553.                                               1, Vector3D.crossProduct(v1, p2)),
  554.                                  new Vector3D(1, Vector3D.crossProduct(p1, a2),
  555.                                               2, Vector3D.crossProduct(v1, v2),
  556.                                               1, Vector3D.crossProduct(a1, p2)));
  557.     }

  558.     /** Return a string representation of this position/velocity pair.
  559.      * @return string representation of this position/velocity pair
  560.      */
  561.     public String toString() {
  562.         final String comma = ", ";
  563.         return new StringBuilder().append('{').append("P(").
  564.                 append(position.getX()).append(comma).
  565.                 append(position.getY()).append(comma).
  566.                 append(position.getZ()).append("), V(").
  567.                 append(velocity.getX()).append(comma).
  568.                 append(velocity.getY()).append(comma).
  569.                 append(velocity.getZ()).append("), A(").
  570.                 append(acceleration.getX()).append(comma).
  571.                 append(acceleration.getY()).append(comma).
  572.                 append(acceleration.getZ()).append(")}").toString();
  573.     }

  574.     /** Replace the instance with a data transfer object for serialization.
  575.      * @return data transfer object that will be serialized
  576.      */
  577.     private Object writeReplace() {
  578.         return new DTO(this);
  579.     }

  580.     /** {@inheritDoc} */
  581.     @Override
  582.     public PVCoordinates blendArithmeticallyWith(final PVCoordinates other, final double blendingValue)
  583.             throws MathIllegalArgumentException {
  584.         final Vector3D blendedPosition     = position.blendArithmeticallyWith(other.position, blendingValue);
  585.         final Vector3D blendedVelocity     = velocity.blendArithmeticallyWith(other.velocity, blendingValue);
  586.         final Vector3D blendedAcceleration = acceleration.blendArithmeticallyWith(other.acceleration, blendingValue);

  587.         return new PVCoordinates(blendedPosition, blendedVelocity, blendedAcceleration);
  588.     }

  589.     /** Internal class used only for serialization. */
  590.     private static class DTO implements Serializable {

  591.         /** Serializable UID. */
  592.         private static final long serialVersionUID = 20140723L;

  593.         /** Double values. */
  594.         private double[] d;

  595.         /** Simple constructor.
  596.          * @param pv instance to serialize
  597.          */
  598.         private DTO(final PVCoordinates pv) {
  599.             this.d = new double[] {
  600.                 pv.getPosition().getX(),     pv.getPosition().getY(),     pv.getPosition().getZ(),
  601.                 pv.getVelocity().getX(),     pv.getVelocity().getY(),     pv.getVelocity().getZ(),
  602.                 pv.getAcceleration().getX(), pv.getAcceleration().getY(), pv.getAcceleration().getZ(),
  603.             };
  604.         }

  605.         /** Replace the deserialized data transfer object with a {@link PVCoordinates}.
  606.          * @return replacement {@link PVCoordinates}
  607.          */
  608.         private Object readResolve() {
  609.             return new PVCoordinates(new Vector3D(d[0], d[1], d[2]),
  610.                                      new Vector3D(d[3], d[4], d[5]),
  611.                                      new Vector3D(d[6], d[7], d[8]));
  612.         }

  613.     }

  614. }