1   /* Copyright 2002-2021 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  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.analysis.differentiation.FDSFactory;
22  import org.hipparchus.analysis.differentiation.FieldDerivative;
23  import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
24  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
25  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
26  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
27  import org.hipparchus.util.FastMath;
28  import org.orekit.errors.OrekitException;
29  import org.orekit.errors.OrekitMessages;
30  import org.orekit.time.TimeShiftable;
31  
32  /** Simple container for Position/Velocity pairs, using {@link CalculusFieldElement}.
33   * <p>
34   * The state can be slightly shifted to close dates. This shift is based on
35   * a simple linear 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 FieldAngularCoordinates}.
41   * </p>
42   * <p>Instances of this class are guaranteed to be immutable.</p>
43   * @param <T> the type of the field elements
44   * @author Luc Maisonobe
45   * @since 6.0
46   * @see PVCoordinates
47   */
48  public class FieldPVCoordinates<T extends CalculusFieldElement<T>>
49      implements TimeShiftable<FieldPVCoordinates<T>> {
50  
51      /** The position. */
52      private final FieldVector3D<T> position;
53  
54      /** The velocity. */
55      private final FieldVector3D<T> velocity;
56  
57      /** The acceleration. */
58      private final FieldVector3D<T> acceleration;
59  
60      /** Builds a FieldPVCoordinates triplet with zero acceleration.
61       * @param position the position vector (m)
62       * @param velocity the velocity vector (m/s)
63       */
64      public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity) {
65          this.position     = position;
66          this.velocity     = velocity;
67          final T zero      = position.getX().getField().getZero();
68          this.acceleration = new FieldVector3D<>(zero, zero, zero);
69      }
70  
71      /** Builds a FieldPVCoordinates triplet.
72       * @param position the position vector (m)
73       * @param velocity the velocity vector (m/s)
74       * @param acceleration the acceleration vector (m/s²)
75       */
76      public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity,
77                                final FieldVector3D<T> acceleration) {
78          this.position     = position;
79          this.velocity     = velocity;
80          this.acceleration = acceleration;
81      }
82  
83      /** Builds a FieldPVCoordinates from a field and a regular PVCoordinates.
84       * @param field field for the components
85       * @param pv PVCoordinates triplet to convert
86       */
87      public FieldPVCoordinates(final Field<T> field, final PVCoordinates pv) {
88          this.position     = new FieldVector3D<>(field, pv.getPosition());
89          this.velocity     = new FieldVector3D<>(field, pv.getVelocity());
90          this.acceleration = new FieldVector3D<>(field, pv.getAcceleration());
91      }
92  
93      /** Multiplicative constructor.
94       * <p>Build a PVCoordinates from another one and a scale factor.</p>
95       * <p>The PVCoordinates built will be a * pv</p>
96       * @param a scale factor
97       * @param pv base (unscaled) PVCoordinates
98       */
99      public FieldPVCoordinates(final double a, final FieldPVCoordinates<T> pv) {
100         position     = new FieldVector3D<>(a, pv.position);
101         velocity     = new FieldVector3D<>(a, pv.velocity);
102         acceleration = new FieldVector3D<>(a, pv.acceleration);
103     }
104 
105     /** Multiplicative constructor.
106      * <p>Build a PVCoordinates from another one and a scale factor.</p>
107      * <p>The PVCoordinates built will be a * pv</p>
108      * @param a scale factor
109      * @param pv base (unscaled) PVCoordinates
110      */
111     public FieldPVCoordinates(final T a, final FieldPVCoordinates<T> pv) {
112         position     = new FieldVector3D<>(a, pv.position);
113         velocity     = new FieldVector3D<>(a, pv.velocity);
114         acceleration = new FieldVector3D<>(a, pv.acceleration);
115     }
116 
117     /** Multiplicative constructor.
118      * <p>Build a PVCoordinates from another one and a scale factor.</p>
119      * <p>The PVCoordinates built will be a * pv</p>
120      * @param a scale factor
121      * @param pv base (unscaled) PVCoordinates
122      */
123     public FieldPVCoordinates(final T a, final PVCoordinates pv) {
124         position     = new FieldVector3D<>(a, pv.getPosition());
125         velocity     = new FieldVector3D<>(a, pv.getVelocity());
126         acceleration = new FieldVector3D<>(a, pv.getAcceleration());
127     }
128 
129     /** Subtractive constructor.
130      * <p>Build a relative PVCoordinates from a start and an end position.</p>
131      * <p>The PVCoordinates built will be end - start.</p>
132      * @param start Starting PVCoordinates
133      * @param end ending PVCoordinates
134      */
135     public FieldPVCoordinates(final FieldPVCoordinates<T> start, final FieldPVCoordinates<T> end) {
136         this.position     = end.position.subtract(start.position);
137         this.velocity     = end.velocity.subtract(start.velocity);
138         this.acceleration = end.acceleration.subtract(start.acceleration);
139     }
140 
141     /** Linear constructor.
142      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
143      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
144      * @param a1 first scale factor
145      * @param pv1 first base (unscaled) PVCoordinates
146      * @param a2 second scale factor
147      * @param pv2 second base (unscaled) PVCoordinates
148      */
149     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
150                               final double a2, final FieldPVCoordinates<T> pv2) {
151         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
152         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
153         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
154     }
155 
156     /** Linear constructor.
157      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
158      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
159      * @param a1 first scale factor
160      * @param pv1 first base (unscaled) PVCoordinates
161      * @param a2 second scale factor
162      * @param pv2 second base (unscaled) PVCoordinates
163      */
164     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
165                               final T a2, final FieldPVCoordinates<T> pv2) {
166         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
167         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
168         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
169     }
170 
171     /** Linear constructor.
172      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
173      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
174      * @param a1 first scale factor
175      * @param pv1 first base (unscaled) PVCoordinates
176      * @param a2 second scale factor
177      * @param pv2 second base (unscaled) PVCoordinates
178      */
179     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
180                               final T a2, final PVCoordinates pv2) {
181         position     = new FieldVector3D<>(a1, pv1.getPosition(), a2, pv2.getPosition());
182         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(), a2, pv2.getVelocity());
183         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration());
184     }
185 
186     /** Linear constructor.
187      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
188      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
189      * @param a1 first scale factor
190      * @param pv1 first base (unscaled) PVCoordinates
191      * @param a2 second scale factor
192      * @param pv2 second base (unscaled) PVCoordinates
193      * @param a3 third scale factor
194      * @param pv3 third base (unscaled) PVCoordinates
195      */
196     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
197                               final double a2, final FieldPVCoordinates<T> pv2,
198                               final double a3, final FieldPVCoordinates<T> pv3) {
199         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
200         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
201         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
202     }
203 
204     /** Linear constructor.
205      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
206      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
207      * @param a1 first scale factor
208      * @param pv1 first base (unscaled) PVCoordinates
209      * @param a2 second scale factor
210      * @param pv2 second base (unscaled) PVCoordinates
211      * @param a3 third scale factor
212      * @param pv3 third base (unscaled) PVCoordinates
213      */
214     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
215                               final T a2, final FieldPVCoordinates<T> pv2,
216                               final T a3, final FieldPVCoordinates<T> pv3) {
217         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
218         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
219         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
220     }
221 
222     /** Linear constructor.
223      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
224      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
225      * @param a1 first scale factor
226      * @param pv1 first base (unscaled) PVCoordinates
227      * @param a2 second scale factor
228      * @param pv2 second base (unscaled) PVCoordinates
229      * @param a3 third scale factor
230      * @param pv3 third base (unscaled) PVCoordinates
231      */
232     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
233                               final T a2, final PVCoordinates pv2,
234                               final T a3, final PVCoordinates pv3) {
235         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),     a3, pv3.getPosition());
236         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),     a3, pv3.getVelocity());
237         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(), a3, pv3.getAcceleration());
238     }
239 
240     /** Linear constructor.
241      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
242      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
243      * @param a1 first scale factor
244      * @param pv1 first base (unscaled) PVCoordinates
245      * @param a2 second scale factor
246      * @param pv2 second base (unscaled) PVCoordinates
247      * @param a3 third scale factor
248      * @param pv3 third base (unscaled) PVCoordinates
249      * @param a4 fourth scale factor
250      * @param pv4 fourth base (unscaled) PVCoordinates
251      */
252     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
253                               final double a2, final FieldPVCoordinates<T> pv2,
254                               final double a3, final FieldPVCoordinates<T> pv3,
255                               final double a4, final FieldPVCoordinates<T> pv4) {
256         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
257         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
258         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
259     }
260 
261     /** Linear constructor.
262      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
263      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
264      * @param a1 first scale factor
265      * @param pv1 first base (unscaled) PVCoordinates
266      * @param a2 second scale factor
267      * @param pv2 second base (unscaled) PVCoordinates
268      * @param a3 third scale factor
269      * @param pv3 third base (unscaled) PVCoordinates
270      * @param a4 fourth scale factor
271      * @param pv4 fourth base (unscaled) PVCoordinates
272      */
273     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
274                               final T a2, final FieldPVCoordinates<T> pv2,
275                               final T a3, final FieldPVCoordinates<T> pv3,
276                               final T a4, final FieldPVCoordinates<T> pv4) {
277         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
278         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
279         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
280     }
281 
282     /** Linear constructor.
283      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
284      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
285      * @param a1 first scale factor
286      * @param pv1 first base (unscaled) PVCoordinates
287      * @param a2 second scale factor
288      * @param pv2 second base (unscaled) PVCoordinates
289      * @param a3 third scale factor
290      * @param pv3 third base (unscaled) PVCoordinates
291      * @param a4 fourth scale factor
292      * @param pv4 fourth base (unscaled) PVCoordinates
293      */
294     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
295                               final T a2, final PVCoordinates pv2,
296                               final T a3, final PVCoordinates pv3,
297                               final T a4, final PVCoordinates pv4) {
298         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),
299                                            a3, pv3.getPosition(),     a4, pv4.getPosition());
300         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),
301                                            a3, pv3.getVelocity(),     a4, pv4.getVelocity());
302         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(),
303                                            a3, pv3.getAcceleration(), a4, pv4.getAcceleration());
304     }
305 
306     /** Builds a FieldPVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link FieldDerivativeStructure}&gt;.
307      * <p>
308      * The vector components must have time as their only derivation parameter and
309      * have consistent derivation orders.
310      * </p>
311      * @param p vector with time-derivatives embedded within the coordinates
312      * @param <U> type of the derivative
313      * @since 9.2
314      */
315     public <U extends FieldDerivative<T, U>> FieldPVCoordinates(final FieldVector3D<U> p) {
316         position = new FieldVector3D<>(p.getX().getValue(), p.getY().getValue(), p.getZ().getValue());
317         if (p.getX().getOrder() >= 1) {
318             velocity = new FieldVector3D<>(p.getX().getPartialDerivative(1),
319                                            p.getY().getPartialDerivative(1),
320                                            p.getZ().getPartialDerivative(1));
321             if (p.getX().getOrder() >= 2) {
322                 acceleration = new FieldVector3D<>(p.getX().getPartialDerivative(2),
323                                                    p.getY().getPartialDerivative(2),
324                                                    p.getZ().getPartialDerivative(2));
325             } else {
326                 acceleration = FieldVector3D.getZero(position.getX().getField());
327             }
328         } else {
329             final FieldVector3D<T> zero = FieldVector3D.getZero(position.getX().getField());
330             velocity     = zero;
331             acceleration = zero;
332         }
333     }
334 
335     /** Get fixed position/velocity at origin (both p, v and a are zero vectors).
336      * @param field field for the components
337      * @param <T> the type of the field elements
338      * @return a new fixed position/velocity at origin
339      */
340     public static <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getZero(final Field<T> field) {
341         return new FieldPVCoordinates<>(field, PVCoordinates.ZERO);
342     }
343 
344     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldDerivativeStructure}&gt;.
345      * <p>
346      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
347      * to the user-specified order.
348      * </p>
349      * @param order derivation order for the vector components (must be either 0, 1 or 2)
350      * @return vector with time-derivatives embedded within the coordinates
351           * @since 9.2
352      */
353     public FieldVector3D<FieldDerivativeStructure<T>> toDerivativeStructureVector(final int order) {
354 
355         final FDSFactory<T> factory;
356         final FieldDerivativeStructure<T> x;
357         final FieldDerivativeStructure<T> y;
358         final FieldDerivativeStructure<T> z;
359         switch(order) {
360             case 0 :
361                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
362                 x = factory.build(position.getX());
363                 y = factory.build(position.getY());
364                 z = factory.build(position.getZ());
365                 break;
366             case 1 :
367                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
368                 x = factory.build(position.getX(), velocity.getX());
369                 y = factory.build(position.getY(), velocity.getY());
370                 z = factory.build(position.getZ(), velocity.getZ());
371                 break;
372             case 2 :
373                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
374                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
375                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
376                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
377                 break;
378             default :
379                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
380         }
381 
382         return new FieldVector3D<>(x, y, z);
383 
384     }
385 
386     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative1}&gt;.
387      * <p>
388      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
389      * to the order 1.
390      * </p>
391      * @return vector with time-derivatives embedded within the coordinates
392      * @see #toUnivariateDerivative2Vector()
393      * @since 10.2
394      */
395     public FieldVector3D<FieldUnivariateDerivative1<T>> toUnivariateDerivative1Vector() {
396 
397         final FieldUnivariateDerivative1<T> x = new FieldUnivariateDerivative1<>(position.getX(), velocity.getX());
398         final FieldUnivariateDerivative1<T> y = new FieldUnivariateDerivative1<>(position.getY(), velocity.getY());
399         final FieldUnivariateDerivative1<T> z = new FieldUnivariateDerivative1<>(position.getZ(), velocity.getZ());
400 
401         return new FieldVector3D<>(x, y, z);
402     }
403 
404     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative2}&gt;.
405      * <p>
406      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
407      * to the order 2.
408      * </p>
409      * @return vector with time-derivatives embedded within the coordinates
410      * @see #toUnivariateDerivative1Vector()
411      * @since 10.2
412      */
413     public FieldVector3D<FieldUnivariateDerivative2<T>> toUnivariateDerivative2Vector() {
414 
415         final FieldUnivariateDerivative2<T> x = new FieldUnivariateDerivative2<>(position.getX(), velocity.getX(), acceleration.getX());
416         final FieldUnivariateDerivative2<T> y = new FieldUnivariateDerivative2<>(position.getY(), velocity.getY(), acceleration.getY());
417         final FieldUnivariateDerivative2<T> z = new FieldUnivariateDerivative2<>(position.getZ(), velocity.getZ(), acceleration.getZ());
418 
419         return new FieldVector3D<>(x, y, z);
420     }
421 
422     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldDerivativeStructure}&gt;.
423      * <p>
424      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
425      * to the user-specified order. As both the instance components {@link #getPosition() position},
426      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
427      * {@link FieldDerivativeStructure#getPartialDerivative(int...) derivatives} of the components
428      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
429      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
430      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
431      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
432      * </p>
433      * <p>
434      * If derivation order is 1, the first derivative of acceleration will be computed as a
435      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
436      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
437      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
438      * </p>
439      * @param order derivation order for the vector components (must be either 0, 1 or 2)
440      * @return pv coordinates with time-derivatives embedded within the coordinates
441           * @since 9.2
442      */
443     public FieldPVCoordinates<FieldDerivativeStructure<T>> toDerivativeStructurePV(final int order) {
444 
445         final FDSFactory<T> factory;
446         final FieldDerivativeStructure<T> x0;
447         final FieldDerivativeStructure<T> y0;
448         final FieldDerivativeStructure<T> z0;
449         final FieldDerivativeStructure<T> x1;
450         final FieldDerivativeStructure<T> y1;
451         final FieldDerivativeStructure<T> z1;
452         final FieldDerivativeStructure<T> x2;
453         final FieldDerivativeStructure<T> y2;
454         final FieldDerivativeStructure<T> z2;
455         switch(order) {
456             case 0 :
457                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
458                 x0 = factory.build(position.getX());
459                 y0 = factory.build(position.getY());
460                 z0 = factory.build(position.getZ());
461                 x1 = factory.build(velocity.getX());
462                 y1 = factory.build(velocity.getY());
463                 z1 = factory.build(velocity.getZ());
464                 x2 = factory.build(acceleration.getX());
465                 y2 = factory.build(acceleration.getY());
466                 z2 = factory.build(acceleration.getZ());
467                 break;
468             case 1 : {
469                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
470                 final T                r2            = position.getNormSq();
471                 final T                r             = r2.sqrt();
472                 final T                pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
473                 final T                a             = acceleration.getNorm();
474                 final T                aOr           = a.divide(r);
475                 final FieldVector3D<T> keplerianJerk = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
476                                                                            aOr.negate(), velocity);
477                 x0 = factory.build(position.getX(),     velocity.getX());
478                 y0 = factory.build(position.getY(),     velocity.getY());
479                 z0 = factory.build(position.getZ(),     velocity.getZ());
480                 x1 = factory.build(velocity.getX(),     acceleration.getX());
481                 y1 = factory.build(velocity.getY(),     acceleration.getY());
482                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
483                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
484                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
485                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
486                 break;
487             }
488             case 2 : {
489                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
490                 final T                r2              = position.getNormSq();
491                 final T                r               = r2.sqrt();
492                 final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
493                 final T                a               = acceleration.getNorm();
494                 final T                aOr             = a.divide(r);
495                 final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
496                                                                              aOr.negate(), velocity);
497                 final T                v2              = velocity.getNormSq();
498                 final T                pa              = FieldVector3D.dotProduct(position, acceleration);
499                 final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
500                 final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
501                                                                              aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
502                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
503                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
504                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
505                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
506                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
507                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
508                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
509                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
510                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
511                 break;
512             }
513             default :
514                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
515         }
516 
517         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
518                                         new FieldVector3D<>(x1, y1, z1),
519                                         new FieldVector3D<>(x2, y2, z2));
520 
521     }
522 
523 
524     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative1}&gt;.
525      * <p>
526      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
527      * to the order 1.
528      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
529      * </p>
530      * @return pv coordinates with time-derivatives embedded within the coordinates
531      * @since 10.2
532      */
533     public FieldPVCoordinates<FieldUnivariateDerivative1<T>> toUnivariateDerivative1PV() {
534 
535         final T   r2            = position.getNormSq();
536         final T   r             = FastMath.sqrt(r2);
537         final T   pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
538         final T   a             = acceleration.getNorm();
539         final T   aOr           = a.divide(r);
540         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
541                                                                      aOr.negate(), velocity);
542 
543         final FieldUnivariateDerivative1<T> x0 = new FieldUnivariateDerivative1<>(position.getX(),     velocity.getX());
544         final FieldUnivariateDerivative1<T> y0 = new FieldUnivariateDerivative1<>(position.getY(),     velocity.getY());
545         final FieldUnivariateDerivative1<T> z0 = new FieldUnivariateDerivative1<>(position.getZ(),     velocity.getZ());
546         final FieldUnivariateDerivative1<T> x1 = new FieldUnivariateDerivative1<>(velocity.getX(),     acceleration.getX());
547         final FieldUnivariateDerivative1<T> y1 = new FieldUnivariateDerivative1<>(velocity.getY(),     acceleration.getY());
548         final FieldUnivariateDerivative1<T> z1 = new FieldUnivariateDerivative1<>(velocity.getZ(),     acceleration.getZ());
549         final FieldUnivariateDerivative1<T> x2 = new FieldUnivariateDerivative1<>(acceleration.getX(), keplerianJerk.getX());
550         final FieldUnivariateDerivative1<T> y2 = new FieldUnivariateDerivative1<>(acceleration.getY(), keplerianJerk.getY());
551         final FieldUnivariateDerivative1<T> z2 = new FieldUnivariateDerivative1<>(acceleration.getZ(), keplerianJerk.getZ());
552 
553         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
554                                         new FieldVector3D<>(x1, y1, z1),
555                                         new FieldVector3D<>(x2, y2, z2));
556 
557     }
558 
559     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative2}&gt;.
560      * <p>
561      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
562      * to the order 2.
563      * As derivation order is 2, the second derivative of velocity (which
564      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
565      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
566      * </p>
567      * @return pv coordinates with time-derivatives embedded within the coordinates
568      * @since 10.2
569      */
570     public FieldPVCoordinates<FieldUnivariateDerivative2<T>> toUnivariateDerivative2PV() {
571 
572         final T                r2              = position.getNormSq();
573         final T                r               = r2.sqrt();
574         final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
575         final T                a               = acceleration.getNorm();
576         final T                aOr             = a.divide(r);
577         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
578                                                                      aOr.negate(), velocity);
579         final T                v2              = velocity.getNormSq();
580         final T                pa              = FieldVector3D.dotProduct(position, acceleration);
581         final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
582         final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
583                                                                      aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
584 
585         final FieldUnivariateDerivative2<T> x0 = new FieldUnivariateDerivative2<>(position.getX(),     velocity.getX(),      acceleration.getX());
586         final FieldUnivariateDerivative2<T> y0 = new FieldUnivariateDerivative2<>(position.getY(),     velocity.getY(),      acceleration.getY());
587         final FieldUnivariateDerivative2<T> z0 = new FieldUnivariateDerivative2<>(position.getZ(),     velocity.getZ(),      acceleration.getZ());
588         final FieldUnivariateDerivative2<T> x1 = new FieldUnivariateDerivative2<>(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
589         final FieldUnivariateDerivative2<T> y1 = new FieldUnivariateDerivative2<>(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
590         final FieldUnivariateDerivative2<T> z1 = new FieldUnivariateDerivative2<>(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
591         final FieldUnivariateDerivative2<T> x2 = new FieldUnivariateDerivative2<>(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
592         final FieldUnivariateDerivative2<T> y2 = new FieldUnivariateDerivative2<>(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
593         final FieldUnivariateDerivative2<T> z2 = new FieldUnivariateDerivative2<>(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
594 
595         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
596                                         new FieldVector3D<>(x1, y1, z1),
597                                         new FieldVector3D<>(x2, y2, z2));
598 
599     }
600 
601     /** Estimate velocity between two positions.
602      * <p>Estimation is based on a simple fixed velocity translation
603      * during the time interval between the two positions.</p>
604      * @param start start position
605      * @param end end position
606      * @param dt time elapsed between the dates of the two positions
607      * @param <T> the type of the field elements
608      * @return velocity allowing to go from start to end positions
609      */
610     public static <T extends CalculusFieldElement<T>> FieldVector3D<T> estimateVelocity(final FieldVector3D<T> start,
611                                                                                     final FieldVector3D<T> end,
612                                                                                     final double dt) {
613         final double scale = 1.0 / dt;
614         return new FieldVector3D<>(scale, end, -scale, start);
615     }
616 
617     /** Get a time-shifted state.
618      * <p>
619      * The state can be slightly shifted to close dates. This shift is based on
620      * a simple quadratic model. It is <em>not</em> intended as a replacement for
621      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
622      * for either small time shifts or coarse accuracy.
623      * </p>
624      * @param dt time shift in seconds
625      * @return a new state, shifted with respect to the instance (which is immutable)
626      */
627     public FieldPVCoordinates<T> shiftedBy(final double dt) {
628         return new FieldPVCoordinates<>(new FieldVector3D<>(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
629                                         new FieldVector3D<>(1, velocity, dt, acceleration),
630                                         acceleration);
631     }
632 
633     /** Get a time-shifted state.
634      * <p>
635      * The state can be slightly shifted to close dates. This shift is based on
636      * a simple quadratic model. It is <em>not</em> intended as a replacement for
637      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
638      * for either small time shifts or coarse accuracy.
639      * </p>
640      * @param dt time shift in seconds
641      * @return a new state, shifted with respect to the instance (which is immutable)
642      */
643     public FieldPVCoordinates<T> shiftedBy(final T dt) {
644         final T one = dt.getField().getOne();
645         return new FieldPVCoordinates<>(new FieldVector3D<>(one, position,
646                                                             dt, velocity,
647                                                             dt.multiply(dt).multiply(0.5), acceleration),
648                                         new FieldVector3D<>(one, velocity,
649                                                             dt, acceleration),
650                                         acceleration);
651     }
652 
653     /** Gets the position.
654      * @return the position vector (m).
655      */
656     public FieldVector3D<T> getPosition() {
657         return position;
658     }
659 
660     /** Gets the velocity.
661      * @return the velocity vector (m/s).
662      */
663     public FieldVector3D<T> getVelocity() {
664         return velocity;
665     }
666 
667     /** Gets the acceleration.
668      * @return the acceleration vector (m/s²).
669      */
670     public FieldVector3D<T> getAcceleration() {
671         return acceleration;
672     }
673 
674     /** Gets the momentum.
675      * <p>This vector is the p &otimes; v where p is position, v is velocity
676      * and &otimes; is cross product. To get the real physical angular momentum
677      * you need to multiply this vector by the mass.</p>
678      * <p>The returned vector is recomputed each time this method is called, it
679      * is not cached.</p>
680      * @return a new instance of the momentum vector (m²/s).
681      */
682     public FieldVector3D<T> getMomentum() {
683         return FieldVector3D.crossProduct(position, velocity);
684     }
685 
686     /**
687      * Get the angular velocity (spin) of this point as seen from the origin.
688      *
689      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
690      * angular * momentum} and is computed by ω = p &times; v / ||p||²
691      *
692      * @return the angular velocity vector
693      * @see <a href="http://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
694      *      Wikipedia</a>
695      */
696     public FieldVector3D<T> getAngularVelocity() {
697         return this.getMomentum().scalarMultiply(
698                 this.getPosition().getNormSq().reciprocal());
699     }
700 
701     /** Get the opposite of the instance.
702      * @return a new position-velocity which is opposite to the instance
703      */
704     public FieldPVCoordinates<T> negate() {
705         return new FieldPVCoordinates<>(position.negate(), velocity.negate(), acceleration.negate());
706     }
707 
708     /** Normalize the position part of the instance.
709      * <p>
710      * The computed coordinates first component (position) will be a
711      * normalized vector, the second component (velocity) will be the
712      * derivative of the first component (hence it will generally not
713      * be normalized), and the third component (acceleration) will be the
714      * derivative of the second component (hence it will generally not
715      * be normalized).
716      * </p>
717      * @return a new instance, with first component normalized and
718      * remaining component computed to have consistent derivatives
719      */
720     public FieldPVCoordinates<T> normalize() {
721         final T   inv     = position.getNorm().reciprocal();
722         final FieldVector3D<T> u       = new FieldVector3D<>(inv, position);
723         final FieldVector3D<T> v       = new FieldVector3D<>(inv, velocity);
724         final FieldVector3D<T> w       = new FieldVector3D<>(inv, acceleration);
725         final T   uv      = FieldVector3D.dotProduct(u, v);
726         final T   v2      = FieldVector3D.dotProduct(v, v);
727         final T   uw      = FieldVector3D.dotProduct(u, w);
728         final FieldVector3D<T> uDot    = new FieldVector3D<>(inv.getField().getOne(), v,
729                                                              uv.multiply(-1), u);
730         final FieldVector3D<T> uDotDot = new FieldVector3D<>(inv.getField().getOne(), w,
731                                                              uv.multiply(-2), v,
732                                                              uv.multiply(uv).multiply(3).subtract(v2).subtract(uw), u);
733         return new FieldPVCoordinates<>(u, uDot, uDotDot);
734     }
735 
736     /** Compute the cross-product of two instances.
737      * @param pv2 second instances
738      * @return the cross product v1 ^ v2 as a new instance
739      */
740     public FieldPVCoordinates<T> crossProduct(final FieldPVCoordinates<T> pv2) {
741         final FieldVector3D<T> p1 = position;
742         final FieldVector3D<T> v1 = velocity;
743         final FieldVector3D<T> a1 = acceleration;
744         final FieldVector3D<T> p2 = pv2.position;
745         final FieldVector3D<T> v2 = pv2.velocity;
746         final FieldVector3D<T> a2 = pv2.acceleration;
747         return new FieldPVCoordinates<>(FieldVector3D.crossProduct(p1, p2),
748                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, v2),
749                                                             1, FieldVector3D.crossProduct(v1, p2)),
750                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, a2),
751                                                             2, FieldVector3D.crossProduct(v1, v2),
752                                                             1, FieldVector3D.crossProduct(a1, p2)));
753     }
754 
755     /** Convert to a constant position-velocity.
756      * @return a constant position-velocity
757      */
758     public PVCoordinates toPVCoordinates() {
759         return new PVCoordinates(position.toVector3D(), velocity.toVector3D(), acceleration.toVector3D());
760     }
761 
762     /** Return a string representation of this position/velocity pair.
763      * @return string representation of this position/velocity pair
764      */
765     public String toString() {
766         final String comma = ", ";
767         return new StringBuffer().append('{').append("P(").
768                                   append(position.getX().getReal()).append(comma).
769                                   append(position.getY().getReal()).append(comma).
770                                   append(position.getZ().getReal()).append("), V(").
771                                   append(velocity.getX().getReal()).append(comma).
772                                   append(velocity.getY().getReal()).append(comma).
773                                   append(velocity.getZ().getReal()).append("), A(").
774                                   append(acceleration.getX().getReal()).append(comma).
775                                   append(acceleration.getY().getReal()).append(comma).
776                                   append(acceleration.getZ().getReal()).append(")}").toString();
777     }
778 
779 }