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.forces.gravity;
18  
19  
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.stream.Stream;
23  
24  import org.hipparchus.Field;
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.analysis.differentiation.DerivativeStructure;
27  import org.hipparchus.analysis.differentiation.Gradient;
28  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
29  import org.hipparchus.geometry.euclidean.threed.SphericalCoordinates;
30  import org.hipparchus.geometry.euclidean.threed.Vector3D;
31  import org.hipparchus.linear.Array2DRowRealMatrix;
32  import org.hipparchus.linear.RealMatrix;
33  import org.hipparchus.util.FastMath;
34  import org.hipparchus.util.MathArrays;
35  import org.orekit.forces.AbstractForceModel;
36  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider;
37  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider.NormalizedSphericalHarmonics;
38  import org.orekit.forces.gravity.potential.TideSystem;
39  import org.orekit.forces.gravity.potential.TideSystemProvider;
40  import org.orekit.frames.Frame;
41  import org.orekit.frames.Transform;
42  import org.orekit.propagation.FieldSpacecraftState;
43  import org.orekit.propagation.SpacecraftState;
44  import org.orekit.propagation.events.EventDetector;
45  import org.orekit.propagation.events.FieldEventDetector;
46  import org.orekit.time.AbsoluteDate;
47  import org.orekit.time.FieldAbsoluteDate;
48  import org.orekit.utils.FieldPVCoordinates;
49  import org.orekit.utils.ParameterDriver;
50  
51  /** This class represents the gravitational field of a celestial body.
52   * <p>
53   * The algorithm implemented in this class has been designed by S. A. Holmes
54   * and W. E. Featherstone from Department of Spatial Sciences, Curtin University
55   * of Technology, Perth, Australia. It is described in their 2002 paper: <a
56   * href="https://www.researchgate.net/publication/226460594_A_unified_approach_to_the_Clenshaw_summation_and_the_recursive_computation_of_very_high_degree_and_order_normalised_associated_Legendre_functions">
57   * A unified approach to he Clenshaw summation and the recursive computation of
58   * very high degree and order normalised associated Legendre functions</a>
59   * (Journal of Geodesy (2002) 76: 279–299).
60   * </p>
61   * <p>
62   * This model directly uses normalized coefficients and stable recursion algorithms
63   * so it is more suited to high degree gravity fields than the classical Cunningham
64   * Droziner models which use un-normalized coefficients.
65   * </p>
66   * <p>
67   * Among the different algorithms presented in Holmes and Featherstone paper, this
68   * class implements the <em>modified forward row method</em>. All recursion coefficients
69   * are precomputed and stored for greater performance. This caching was suggested in the
70   * paper but not used due to the large memory requirements. Since 2002, even low end
71   * computers and mobile devices do have sufficient memory so this caching has become
72   * feasible nowadays.
73   * <p>
74   * @author Luc Maisonobe
75   * @since 6.0
76   */
77  
78  public class HolmesFeatherstoneAttractionModel extends AbstractForceModel implements TideSystemProvider {
79  
80      /** Exponent scaling to avoid floating point overflow.
81       * <p>The paper uses 10^280, we prefer a power of two to preserve accuracy thanks to
82       * {@link FastMath#scalb(double, int)}, so we use 2^930 which has the same order of magnitude.
83       */
84      private static final int SCALING = 930;
85  
86      /** Central attraction scaling factor.
87       * <p>
88       * We use a power of 2 to avoid numeric noise introduction
89       * in the multiplications/divisions sequences.
90       * </p>
91       */
92      private static final double MU_SCALE = FastMath.scalb(1.0, 32);
93  
94      /** Driver for gravitational parameter. */
95      private final ParameterDriver gmParameterDriver;
96  
97      /** Provider for the spherical harmonics. */
98      private final NormalizedSphericalHarmonicsProvider provider;
99  
100     /** Rotating body. */
101     private final Frame bodyFrame;
102 
103     /** Recursion coefficients g<sub>n,m</sub>/√j. */
104     private final double[] gnmOj;
105 
106     /** Recursion coefficients h<sub>n,m</sub>/√j. */
107     private final double[] hnmOj;
108 
109     /** Recursion coefficients e<sub>n,m</sub>. */
110     private final double[] enm;
111 
112     /** Scaled sectorial Pbar<sub>m,m</sub>/u<sup>m</sup> &times; 2<sup>-SCALING</sup>. */
113     private final double[] sectorial;
114 
115     /** Creates a new instance.
116      * @param centralBodyFrame rotating body frame
117      * @param provider provider for spherical harmonics
118      * @since 6.0
119      */
120     public HolmesFeatherstoneAttractionModel(final Frame centralBodyFrame,
121                                              final NormalizedSphericalHarmonicsProvider provider) {
122 
123         gmParameterDriver = new ParameterDriver(NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT,
124                                                 provider.getMu(), MU_SCALE, 0.0, Double.POSITIVE_INFINITY);
125 
126         this.provider  = provider;
127         this.bodyFrame = centralBodyFrame;
128 
129         // the pre-computed arrays hold coefficients from triangular arrays in a single
130         // storing neither diagonal elements (n = m) nor the non-diagonal element n=1, m=0
131         final int degree = provider.getMaxDegree();
132         final int size = FastMath.max(0, degree * (degree + 1) / 2 - 1);
133         gnmOj = new double[size];
134         hnmOj = new double[size];
135         enm   = new double[size];
136 
137         // pre-compute the recursion coefficients corresponding to equations 19 and 22
138         // from Holmes and Featherstone paper
139         // for cache efficiency, elements are stored in the same order they will be used
140         // later on, i.e. from rightmost column to leftmost column
141         int index = 0;
142         for (int m = degree; m >= 0; --m) {
143             final int j = (m == 0) ? 2 : 1;
144             for (int n = FastMath.max(2, m + 1); n <= degree; ++n) {
145                 final double f = (n - m) * (n + m + 1);
146                 gnmOj[index] = 2 * (m + 1) / FastMath.sqrt(j * f);
147                 hnmOj[index] = FastMath.sqrt((n + m + 2) * (n - m - 1) / (j * f));
148                 enm[index]   = FastMath.sqrt(f / j);
149                 ++index;
150             }
151         }
152 
153         // scaled sectorial terms corresponding to equation 28 in Holmes and Featherstone paper
154         sectorial    = new double[degree + 1];
155         sectorial[0] = FastMath.scalb(1.0, -SCALING);
156         sectorial[1] = FastMath.sqrt(3) * sectorial[0];
157         for (int m = 2; m < sectorial.length; ++m) {
158             sectorial[m] = FastMath.sqrt((2 * m + 1) / (2.0 * m)) * sectorial[m - 1];
159         }
160 
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     public boolean dependsOnPositionOnly() {
166         return true;
167     }
168 
169     /** {@inheritDoc} */
170     public TideSystem getTideSystem() {
171         return provider.getTideSystem();
172     }
173 
174     /** Get the central attraction coefficient μ.
175      * @return mu central attraction coefficient (m³/s²)
176      */
177     public double getMu() {
178         return gmParameterDriver.getValue();
179     }
180 
181     /** Compute the value of the gravity field.
182      * @param date current date
183      * @param position position at which gravity field is desired in body frame
184      * @param mu central attraction coefficient to use
185      * @return value of the gravity field (central and non-central parts summed together)
186      */
187     public double value(final AbsoluteDate date, final Vector3D position,
188                         final double mu) {
189         return mu / position.getNorm() + nonCentralPart(date, position, mu);
190     }
191 
192     /** Compute the non-central part of the gravity field.
193      * @param date current date
194      * @param position position at which gravity field is desired in body frame
195      * @param mu central attraction coefficient to use
196      * @return value of the non-central part of the gravity field
197      */
198     public double nonCentralPart(final AbsoluteDate date, final Vector3D position, final double mu) {
199 
200         final int degree = provider.getMaxDegree();
201         final int order  = provider.getMaxOrder();
202         final NormalizedSphericalHarmonics harmonics = provider.onDate(date);
203 
204         // allocate the columns for recursion
205         double[] pnm0Plus2 = new double[degree + 1];
206         double[] pnm0Plus1 = new double[degree + 1];
207         double[] pnm0      = new double[degree + 1];
208 
209         // compute polar coordinates
210         final double x   = position.getX();
211         final double y   = position.getY();
212         final double z   = position.getZ();
213         final double x2  = x * x;
214         final double y2  = y * y;
215         final double z2  = z * z;
216         final double r2  = x2 + y2 + z2;
217         final double r   = FastMath.sqrt (r2);
218         final double rho = FastMath.sqrt(x2 + y2);
219         final double t   = z / r;   // cos(theta), where theta is the polar angle
220         final double u   = rho / r; // sin(theta), where theta is the polar angle
221         final double tOu = z / rho;
222 
223         // compute distance powers
224         final double[] aOrN = createDistancePowersArray(provider.getAe() / r);
225 
226         // compute longitude cosines/sines
227         final double[][] cosSinLambda = createCosSinArrays(position.getX() / rho, position.getY() / rho);
228 
229         // outer summation over order
230         int    index = 0;
231         double value = 0;
232         for (int m = degree; m >= 0; --m) {
233 
234             // compute tesseral terms without derivatives
235             index = computeTesseral(m, degree, index, t, u, tOu,
236                                     pnm0Plus2, pnm0Plus1, null, pnm0, null, null);
237 
238             if (m <= order) {
239                 // compute contribution of current order to field (equation 5 of the paper)
240 
241                 // inner summation over degree, for fixed order
242                 double sumDegreeS        = 0;
243                 double sumDegreeC        = 0;
244                 for (int n = FastMath.max(2, m); n <= degree; ++n) {
245                     sumDegreeS += pnm0[n] * aOrN[n] * harmonics.getNormalizedSnm(n, m);
246                     sumDegreeC += pnm0[n] * aOrN[n] * harmonics.getNormalizedCnm(n, m);
247                 }
248 
249                 // contribution to outer summation over order
250                 value = value * u + cosSinLambda[1][m] * sumDegreeS + cosSinLambda[0][m] * sumDegreeC;
251 
252             }
253 
254             // rotate the recursion arrays
255             final double[] tmp = pnm0Plus2;
256             pnm0Plus2 = pnm0Plus1;
257             pnm0Plus1 = pnm0;
258             pnm0      = tmp;
259 
260         }
261 
262         // scale back
263         value = FastMath.scalb(value, SCALING);
264 
265         // apply the global mu/r factor
266         return mu * value / r;
267 
268     }
269 
270     /** Compute the gradient of the non-central part of the gravity field.
271      * @param date current date
272      * @param position position at which gravity field is desired in body frame
273      * @param mu central attraction coefficient to use
274      * @return gradient of the non-central part of the gravity field
275      */
276     public double[] gradient(final AbsoluteDate date, final Vector3D position, final double mu) {
277 
278         final int degree = provider.getMaxDegree();
279         final int order  = provider.getMaxOrder();
280         final NormalizedSphericalHarmonics harmonics = provider.onDate(date);
281 
282         // allocate the columns for recursion
283         double[] pnm0Plus2  = new double[degree + 1];
284         double[] pnm0Plus1  = new double[degree + 1];
285         double[] pnm0       = new double[degree + 1];
286         final double[] pnm1 = new double[degree + 1];
287 
288         // compute polar coordinates
289         final double x    = position.getX();
290         final double y    = position.getY();
291         final double z    = position.getZ();
292         final double x2   = x * x;
293         final double y2   = y * y;
294         final double z2   = z * z;
295         final double r2   = x2 + y2 + z2;
296         final double r    = FastMath.sqrt (r2);
297         final double rho2 = x2 + y2;
298         final double rho  = FastMath.sqrt(rho2);
299         final double t    = z / r;   // cos(theta), where theta is the polar angle
300         final double u    = rho / r; // sin(theta), where theta is the polar angle
301         final double tOu  = z / rho;
302 
303         // compute distance powers
304         final double[] aOrN = createDistancePowersArray(provider.getAe() / r);
305 
306         // compute longitude cosines/sines
307         final double[][] cosSinLambda = createCosSinArrays(position.getX() / rho, position.getY() / rho);
308 
309         // outer summation over order
310         int    index = 0;
311         double value = 0;
312         final double[] gradient = new double[3];
313         for (int m = degree; m >= 0; --m) {
314 
315             // compute tesseral terms with derivatives
316             index = computeTesseral(m, degree, index, t, u, tOu,
317                                     pnm0Plus2, pnm0Plus1, null, pnm0, pnm1, null);
318 
319             if (m <= order) {
320                 // compute contribution of current order to field (equation 5 of the paper)
321 
322                 // inner summation over degree, for fixed order
323                 double sumDegreeS        = 0;
324                 double sumDegreeC        = 0;
325                 double dSumDegreeSdR     = 0;
326                 double dSumDegreeCdR     = 0;
327                 double dSumDegreeSdTheta = 0;
328                 double dSumDegreeCdTheta = 0;
329                 for (int n = FastMath.max(2, m); n <= degree; ++n) {
330                     final double qSnm  = aOrN[n] * harmonics.getNormalizedSnm(n, m);
331                     final double qCnm  = aOrN[n] * harmonics.getNormalizedCnm(n, m);
332                     final double nOr   = n / r;
333                     final double s0    = pnm0[n] * qSnm;
334                     final double c0    = pnm0[n] * qCnm;
335                     final double s1    = pnm1[n] * qSnm;
336                     final double c1    = pnm1[n] * qCnm;
337                     sumDegreeS        += s0;
338                     sumDegreeC        += c0;
339                     dSumDegreeSdR     -= nOr * s0;
340                     dSumDegreeCdR     -= nOr * c0;
341                     dSumDegreeSdTheta += s1;
342                     dSumDegreeCdTheta += c1;
343                 }
344 
345                 // contribution to outer summation over order
346                 // beware that we need to order gradient using the mathematical conventions
347                 // compliant with the SphericalCoordinates class, so our lambda is its theta
348                 // (and hence at index 1) and our theta is its phi (and hence at index 2)
349                 final double sML = cosSinLambda[1][m];
350                 final double cML = cosSinLambda[0][m];
351                 value            = value       * u + sML * sumDegreeS        + cML * sumDegreeC;
352                 gradient[0]      = gradient[0] * u + sML * dSumDegreeSdR     + cML * dSumDegreeCdR;
353                 gradient[1]      = gradient[1] * u + m * (cML * sumDegreeS - sML * sumDegreeC);
354                 gradient[2]      = gradient[2] * u + sML * dSumDegreeSdTheta + cML * dSumDegreeCdTheta;
355 
356             }
357 
358             // rotate the recursion arrays
359             final double[] tmp = pnm0Plus2;
360             pnm0Plus2 = pnm0Plus1;
361             pnm0Plus1 = pnm0;
362             pnm0      = tmp;
363 
364         }
365 
366         // scale back
367         value       = FastMath.scalb(value,       SCALING);
368         gradient[0] = FastMath.scalb(gradient[0], SCALING);
369         gradient[1] = FastMath.scalb(gradient[1], SCALING);
370         gradient[2] = FastMath.scalb(gradient[2], SCALING);
371 
372         // apply the global mu/r factor
373         final double muOr = mu / r;
374         value            *= muOr;
375         gradient[0]       = muOr * gradient[0] - value / r;
376         gradient[1]      *= muOr;
377         gradient[2]      *= muOr;
378 
379         // convert gradient from spherical to Cartesian
380         return new SphericalCoordinates(position).toCartesianGradient(gradient);
381 
382     }
383 
384     /** Compute the gradient of the non-central part of the gravity field.
385      * @param date current date
386      * @param position position at which gravity field is desired in body frame
387      * @param mu central attraction coefficient to use
388      * @param <T> type of field used
389      * @return gradient of the non-central part of the gravity field
390      */
391     public <T extends CalculusFieldElement<T>> T[] gradient(final FieldAbsoluteDate<T> date, final FieldVector3D<T> position,
392                                                         final T mu) {
393 
394         final int degree = provider.getMaxDegree();
395         final int order  = provider.getMaxOrder();
396         final NormalizedSphericalHarmonics harmonics = provider.onDate(date.toAbsoluteDate());
397         final T zero = date.getField().getZero();
398         // allocate the columns for recursion
399         T[] pnm0Plus2  = MathArrays.buildArray(date.getField(), degree + 1);
400         T[] pnm0Plus1  = MathArrays.buildArray(date.getField(), degree + 1);
401         T[] pnm0       = MathArrays.buildArray(date.getField(), degree + 1);
402         final T[] pnm1 = MathArrays.buildArray(date.getField(), degree + 1);
403 
404         // compute polar coordinates
405         final T x    = position.getX();
406         final T y    = position.getY();
407         final T z    = position.getZ();
408         final T x2   = x.multiply(x);
409         final T y2   = y.multiply(y);
410         final T z2   = z.multiply(z);
411         final T r2   = x2.add(y2).add(z2);
412         final T r    = r2.sqrt();
413         final T rho2 = x2.add(y2);
414         final T rho  = rho2.sqrt();
415         final T t    = z.divide(r);   // cos(theta), where theta is the polar angle
416         final T u    = rho.divide(r); // sin(theta), where theta is the polar angle
417         final T tOu  = z.divide(rho);
418 
419         // compute distance powers
420         final T[] aOrN = createDistancePowersArray(r.reciprocal().multiply(provider.getAe()));
421 
422         // compute longitude cosines/sines
423         final T[][] cosSinLambda = createCosSinArrays(rho.reciprocal().multiply(position.getX()), rho.reciprocal().multiply(position.getY()));
424         // outer summation over order
425         int    index = 0;
426         T value = zero;
427         final T[] gradient = MathArrays.buildArray(zero.getField(), 3);
428         for (int m = degree; m >= 0; --m) {
429 
430             // compute tesseral terms with derivatives
431             index = computeTesseral(m, degree, index, t, u, tOu,
432                                     pnm0Plus2, pnm0Plus1, null, pnm0, pnm1, null);
433             if (m <= order) {
434                 // compute contribution of current order to field (equation 5 of the paper)
435 
436                 // inner summation over degree, for fixed order
437                 T sumDegreeS        = zero;
438                 T sumDegreeC        = zero;
439                 T dSumDegreeSdR     = zero;
440                 T dSumDegreeCdR     = zero;
441                 T dSumDegreeSdTheta = zero;
442                 T dSumDegreeCdTheta = zero;
443                 for (int n = FastMath.max(2, m); n <= degree; ++n) {
444                     final T qSnm  = aOrN[n].multiply(harmonics.getNormalizedSnm(n, m));
445                     final T qCnm  = aOrN[n].multiply(harmonics.getNormalizedCnm(n, m));
446                     final T nOr   = r.reciprocal().multiply(n);
447                     final T s0    = pnm0[n].multiply(qSnm);
448                     final T c0    = pnm0[n].multiply(qCnm);
449                     final T s1    = pnm1[n].multiply(qSnm);
450                     final T c1    = pnm1[n].multiply(qCnm);
451                     sumDegreeS        = sumDegreeS       .add(s0);
452                     sumDegreeC        = sumDegreeC       .add(c0);
453                     dSumDegreeSdR     = dSumDegreeSdR    .subtract(nOr.multiply(s0));
454                     dSumDegreeCdR     = dSumDegreeCdR    .subtract(nOr.multiply(c0));
455                     dSumDegreeSdTheta = dSumDegreeSdTheta.add(s1);
456                     dSumDegreeCdTheta = dSumDegreeCdTheta.add(c1);
457                 }
458 
459                 // contribution to outer summation over order
460                 // beware that we need to order gradient using the mathematical conventions
461                 // compliant with the SphericalCoordinates class, so our lambda is its theta
462                 // (and hence at index 1) and our theta is its phi (and hence at index 2)
463                 final T sML = cosSinLambda[1][m];
464                 final T cML = cosSinLambda[0][m];
465                 value            = value      .multiply(u).add(sML.multiply(sumDegreeS   )).add(cML.multiply(sumDegreeC));
466                 gradient[0]      = gradient[0].multiply(u).add(sML.multiply(dSumDegreeSdR)).add(cML.multiply(dSumDegreeCdR));
467                 gradient[1]      = gradient[1].multiply(u).add(cML.multiply(sumDegreeS).subtract(sML.multiply(sumDegreeC)).multiply(m));
468                 gradient[2]      = gradient[2].multiply(u).add(sML.multiply(dSumDegreeSdTheta)).add(cML.multiply(dSumDegreeCdTheta));
469             }
470             // rotate the recursion arrays
471             final T[] tmp = pnm0Plus2;
472             pnm0Plus2 = pnm0Plus1;
473             pnm0Plus1 = pnm0;
474             pnm0      = tmp;
475 
476         }
477         // scale back
478         value       = value.scalb(SCALING);
479         gradient[0] = gradient[0].scalb(SCALING);
480         gradient[1] = gradient[1].scalb(SCALING);
481         gradient[2] = gradient[2].scalb(SCALING);
482 
483         // apply the global mu/r factor
484         final T muOr = r.reciprocal().multiply(mu);
485         value            = value.multiply(muOr);
486         gradient[0]       = muOr.multiply(gradient[0]).subtract(value.divide(r));
487         gradient[1]      = gradient[1].multiply(muOr);
488         gradient[2]      = gradient[2].multiply(muOr);
489 
490         // convert gradient from spherical to Cartesian
491         // Cartesian coordinates
492         // remaining spherical coordinates
493         final T rPos     = position.getNorm();
494         // intermediate variables
495         final T xPos    = position.getX();
496         final T yPos    = position.getY();
497         final T zPos    = position.getZ();
498         final T rho2Pos = x.multiply(x).add(y.multiply(y));
499         final T rhoPos  = rho2.sqrt();
500         final T r2Pos   = rho2.add(z.multiply(z));
501 
502         final T[][] jacobianPos = MathArrays.buildArray(zero.getField(), 3, 3);
503 
504         // row representing the gradient of r
505         jacobianPos[0][0] = xPos.divide(rPos);
506         jacobianPos[0][1] = yPos.divide(rPos);
507         jacobianPos[0][2] = zPos.divide(rPos);
508 
509         // row representing the gradient of theta
510         jacobianPos[1][0] =  yPos.negate().divide(rho2Pos);
511         jacobianPos[1][1] =  xPos.divide(rho2Pos);
512         // jacobian[1][2] is already set to 0 at allocation time
513 
514         // row representing the gradient of phi
515         jacobianPos[2][0] = xPos.multiply(zPos).divide(rhoPos.multiply(r2Pos));
516         jacobianPos[2][1] = yPos.multiply(zPos).divide(rhoPos.multiply(r2Pos));
517         jacobianPos[2][2] = rhoPos.negate().divide(r2Pos);
518         final T[] cartGradPos = MathArrays.buildArray(zero.getField(), 3);
519         cartGradPos[0] = gradient[0].multiply(jacobianPos[0][0]).add(gradient[1].multiply(jacobianPos[1][0])).add(gradient[2].multiply(jacobianPos[2][0]));
520         cartGradPos[1] = gradient[0].multiply(jacobianPos[0][1]).add(gradient[1].multiply(jacobianPos[1][1])).add(gradient[2].multiply(jacobianPos[2][1]));
521         cartGradPos[2] = gradient[0].multiply(jacobianPos[0][2])                                      .add(gradient[2].multiply(jacobianPos[2][2]));
522         return cartGradPos;
523 
524     }
525 
526     /** Compute both the gradient and the hessian of the non-central part of the gravity field.
527      * @param date current date
528      * @param position position at which gravity field is desired in body frame
529      * @param mu central attraction coefficient to use
530      * @return gradient and hessian of the non-central part of the gravity field
531      */
532     private GradientHessian gradientHessian(final AbsoluteDate date, final Vector3D position, final double mu) {
533 
534         final int degree = provider.getMaxDegree();
535         final int order  = provider.getMaxOrder();
536         final NormalizedSphericalHarmonics harmonics = provider.onDate(date);
537 
538         // allocate the columns for recursion
539         double[] pnm0Plus2  = new double[degree + 1];
540         double[] pnm0Plus1  = new double[degree + 1];
541         double[] pnm0       = new double[degree + 1];
542         double[] pnm1Plus1  = new double[degree + 1];
543         double[] pnm1       = new double[degree + 1];
544         final double[] pnm2 = new double[degree + 1];
545 
546         // compute polar coordinates
547         final double x    = position.getX();
548         final double y    = position.getY();
549         final double z    = position.getZ();
550         final double x2   = x * x;
551         final double y2   = y * y;
552         final double z2   = z * z;
553         final double r2   = x2 + y2 + z2;
554         final double r    = FastMath.sqrt (r2);
555         final double rho2 = x2 + y2;
556         final double rho  = FastMath.sqrt(rho2);
557         final double t    = z / r;   // cos(theta), where theta is the polar angle
558         final double u    = rho / r; // sin(theta), where theta is the polar angle
559         final double tOu  = z / rho;
560 
561         // compute distance powers
562         final double[] aOrN = createDistancePowersArray(provider.getAe() / r);
563 
564         // compute longitude cosines/sines
565         final double[][] cosSinLambda = createCosSinArrays(position.getX() / rho, position.getY() / rho);
566 
567         // outer summation over order
568         int    index = 0;
569         double value = 0;
570         final double[]   gradient = new double[3];
571         final double[][] hessian  = new double[3][3];
572         for (int m = degree; m >= 0; --m) {
573 
574             // compute tesseral terms
575             index = computeTesseral(m, degree, index, t, u, tOu,
576                                     pnm0Plus2, pnm0Plus1, pnm1Plus1, pnm0, pnm1, pnm2);
577 
578             if (m <= order) {
579                 // compute contribution of current order to field (equation 5 of the paper)
580 
581                 // inner summation over degree, for fixed order
582                 double sumDegreeS               = 0;
583                 double sumDegreeC               = 0;
584                 double dSumDegreeSdR            = 0;
585                 double dSumDegreeCdR            = 0;
586                 double dSumDegreeSdTheta        = 0;
587                 double dSumDegreeCdTheta        = 0;
588                 double d2SumDegreeSdRdR         = 0;
589                 double d2SumDegreeSdRdTheta     = 0;
590                 double d2SumDegreeSdThetadTheta = 0;
591                 double d2SumDegreeCdRdR         = 0;
592                 double d2SumDegreeCdRdTheta     = 0;
593                 double d2SumDegreeCdThetadTheta = 0;
594                 for (int n = FastMath.max(2, m); n <= degree; ++n) {
595                     final double qSnm         = aOrN[n] * harmonics.getNormalizedSnm(n, m);
596                     final double qCnm         = aOrN[n] * harmonics.getNormalizedCnm(n, m);
597                     final double nOr          = n / r;
598                     final double nnP1Or2      = nOr * (n + 1) / r;
599                     final double s0           = pnm0[n] * qSnm;
600                     final double c0           = pnm0[n] * qCnm;
601                     final double s1           = pnm1[n] * qSnm;
602                     final double c1           = pnm1[n] * qCnm;
603                     final double s2           = pnm2[n] * qSnm;
604                     final double c2           = pnm2[n] * qCnm;
605                     sumDegreeS               += s0;
606                     sumDegreeC               += c0;
607                     dSumDegreeSdR            -= nOr * s0;
608                     dSumDegreeCdR            -= nOr * c0;
609                     dSumDegreeSdTheta        += s1;
610                     dSumDegreeCdTheta        += c1;
611                     d2SumDegreeSdRdR         += nnP1Or2 * s0;
612                     d2SumDegreeSdRdTheta     -= nOr * s1;
613                     d2SumDegreeSdThetadTheta += s2;
614                     d2SumDegreeCdRdR         += nnP1Or2 * c0;
615                     d2SumDegreeCdRdTheta     -= nOr * c1;
616                     d2SumDegreeCdThetadTheta += c2;
617                 }
618 
619                 // contribution to outer summation over order
620                 final double sML = cosSinLambda[1][m];
621                 final double cML = cosSinLambda[0][m];
622                 value            = value         * u + sML * sumDegreeS + cML * sumDegreeC;
623                 gradient[0]      = gradient[0]   * u + sML * dSumDegreeSdR + cML * dSumDegreeCdR;
624                 gradient[1]      = gradient[1]   * u + m * (cML * sumDegreeS - sML * sumDegreeC);
625                 gradient[2]      = gradient[2]   * u + sML * dSumDegreeSdTheta + cML * dSumDegreeCdTheta;
626                 hessian[0][0]    = hessian[0][0] * u + sML * d2SumDegreeSdRdR + cML * d2SumDegreeCdRdR;
627                 hessian[1][0]    = hessian[1][0] * u + m * (cML * dSumDegreeSdR - sML * dSumDegreeCdR);
628                 hessian[2][0]    = hessian[2][0] * u + sML * d2SumDegreeSdRdTheta + cML * d2SumDegreeCdRdTheta;
629                 hessian[1][1]    = hessian[1][1] * u - m * m * (sML * sumDegreeS + cML * sumDegreeC);
630                 hessian[2][1]    = hessian[2][1] * u + m * (cML * dSumDegreeSdTheta - sML * dSumDegreeCdTheta);
631                 hessian[2][2]    = hessian[2][2] * u + sML * d2SumDegreeSdThetadTheta + cML * d2SumDegreeCdThetadTheta;
632 
633             }
634 
635             // rotate the recursion arrays
636             final double[] tmp0 = pnm0Plus2;
637             pnm0Plus2 = pnm0Plus1;
638             pnm0Plus1 = pnm0;
639             pnm0      = tmp0;
640             final double[] tmp1 = pnm1Plus1;
641             pnm1Plus1 = pnm1;
642             pnm1      = tmp1;
643 
644         }
645 
646         // scale back
647         value = FastMath.scalb(value, SCALING);
648         for (int i = 0; i < 3; ++i) {
649             gradient[i] = FastMath.scalb(gradient[i], SCALING);
650             for (int j = 0; j <= i; ++j) {
651                 hessian[i][j] = FastMath.scalb(hessian[i][j], SCALING);
652             }
653         }
654 
655 
656         // apply the global mu/r factor
657         final double muOr = mu / r;
658         value         *= muOr;
659         gradient[0]    = muOr * gradient[0] - value / r;
660         gradient[1]   *= muOr;
661         gradient[2]   *= muOr;
662         hessian[0][0]  = muOr * hessian[0][0] - 2 * gradient[0] / r;
663         hessian[1][0]  = muOr * hessian[1][0] -     gradient[1] / r;
664         hessian[2][0]  = muOr * hessian[2][0] -     gradient[2] / r;
665         hessian[1][1] *= muOr;
666         hessian[2][1] *= muOr;
667         hessian[2][2] *= muOr;
668 
669         // convert gradient and Hessian from spherical to Cartesian
670         final SphericalCoordinates sc = new SphericalCoordinates(position);
671         return new GradientHessian(sc.toCartesianGradient(gradient),
672                                    sc.toCartesianHessian(hessian, gradient));
673 
674 
675     }
676 
677     /** Container for gradient and Hessian. */
678     private static class GradientHessian {
679 
680         /** Gradient. */
681         private final double[] gradient;
682 
683         /** Hessian. */
684         private final double[][] hessian;
685 
686         /** Simple constructor.
687          * <p>
688          * A reference to the arrays is stored, they are <strong>not</strong> cloned.
689          * </p>
690          * @param gradient gradient
691          * @param hessian hessian
692          */
693         GradientHessian(final double[] gradient, final double[][] hessian) {
694             this.gradient = gradient;
695             this.hessian  = hessian;
696         }
697 
698         /** Get a reference to the gradient.
699          * @return gradient (a reference to the internal array is returned)
700          */
701         public double[] getGradient() {
702             return gradient;
703         }
704 
705         /** Get a reference to the Hessian.
706          * @return Hessian (a reference to the internal array is returned)
707          */
708         public double[][] getHessian() {
709             return hessian;
710         }
711 
712     }
713 
714     /** Compute a/r powers array.
715      * @param aOr a/r
716      * @return array containing (a/r)<sup>n</sup>
717      */
718     private double[] createDistancePowersArray(final double aOr) {
719 
720         // initialize array
721         final double[] aOrN = new double[provider.getMaxDegree() + 1];
722         aOrN[0] = 1;
723         aOrN[1] = aOr;
724 
725         // fill up array
726         for (int n = 2; n < aOrN.length; ++n) {
727             final int p = n / 2;
728             final int q = n - p;
729             aOrN[n] = aOrN[p] * aOrN[q];
730         }
731 
732         return aOrN;
733 
734     }
735     /** Compute a/r powers array.
736      * @param aOr a/r
737      * @param <T> type of field used
738      * @return array containing (a/r)<sup>n</sup>
739      */
740     private <T extends CalculusFieldElement<T>> T[] createDistancePowersArray(final T aOr) {
741 
742         // initialize array
743         final T[] aOrN = MathArrays.buildArray(aOr.getField(), provider.getMaxDegree() + 1);
744         aOrN[0] = aOr.getField().getOne();
745         aOrN[1] = aOr;
746 
747         // fill up array
748         for (int n = 2; n < aOrN.length; ++n) {
749             final int p = n / 2;
750             final int q = n - p;
751             aOrN[n] = aOrN[p].multiply(aOrN[q]);
752         }
753 
754         return aOrN;
755 
756     }
757 
758     /** Compute longitude cosines and sines.
759      * @param cosLambda cos(λ)
760      * @param sinLambda sin(λ)
761      * @return array containing cos(m &times; λ) in row 0
762      * and sin(m &times; λ) in row 1
763      */
764     private double[][] createCosSinArrays(final double cosLambda, final double sinLambda) {
765 
766         // initialize arrays
767         final double[][] cosSin = new double[2][provider.getMaxOrder() + 1];
768         cosSin[0][0] = 1;
769         cosSin[1][0] = 0;
770         if (provider.getMaxOrder() > 0) {
771             cosSin[0][1] = cosLambda;
772             cosSin[1][1] = sinLambda;
773 
774             // fill up array
775             for (int m = 2; m < cosSin[0].length; ++m) {
776 
777                 // m * lambda is split as p * lambda + q * lambda, trying to avoid
778                 // p or q being much larger than the other. This reduces the number of
779                 // intermediate results reused to compute each value, and hence should limit
780                 // as much as possible roundoff error accumulation
781                 // (this does not change the number of floating point operations)
782                 final int p = m / 2;
783                 final int q = m - p;
784 
785                 cosSin[0][m] = cosSin[0][p] * cosSin[0][q] - cosSin[1][p] * cosSin[1][q];
786                 cosSin[1][m] = cosSin[1][p] * cosSin[0][q] + cosSin[0][p] * cosSin[1][q];
787             }
788         }
789 
790         return cosSin;
791 
792     }
793 
794     /** Compute longitude cosines and sines.
795      * @param cosLambda cos(λ)
796      * @param sinLambda sin(λ)
797      * @param <T> type of field used
798      * @return array containing cos(m &times; λ) in row 0
799      * and sin(m &times; λ) in row 1
800      */
801     private <T extends CalculusFieldElement<T>> T[][] createCosSinArrays(final T cosLambda, final T sinLambda) {
802 
803         final T one = cosLambda.getField().getOne();
804         final T zero = cosLambda.getField().getZero();
805         // initialize arrays
806         final T[][] cosSin = MathArrays.buildArray(one.getField(), 2, provider.getMaxOrder() + 1);
807         cosSin[0][0] = one;
808         cosSin[1][0] = zero;
809         if (provider.getMaxOrder() > 0) {
810             cosSin[0][1] = cosLambda;
811             cosSin[1][1] = sinLambda;
812 
813             // fill up array
814             for (int m = 2; m < cosSin[0].length; ++m) {
815 
816                 // m * lambda is split as p * lambda + q * lambda, trying to avoid
817                 // p or q being much larger than the other. This reduces the number of
818                 // intermediate results reused to compute each value, and hence should limit
819                 // as much as possible roundoff error accumulation
820                 // (this does not change the number of floating point operations)
821                 final int p = m / 2;
822                 final int q = m - p;
823 
824                 cosSin[0][m] = cosSin[0][p].multiply(cosSin[0][q]).subtract(cosSin[1][p].multiply(cosSin[1][q]));
825                 cosSin[1][m] = cosSin[1][p].multiply(cosSin[0][q]).add(cosSin[0][p].multiply(cosSin[1][q]));
826 
827             }
828         }
829 
830         return cosSin;
831 
832     }
833 
834     /** Compute one order of tesseral terms.
835      * <p>
836      * This corresponds to equations 27 and 30 of the paper.
837      * </p>
838      * @param m current order
839      * @param degree max degree
840      * @param index index in the flattened array
841      * @param t cos(θ), where θ is the polar angle
842      * @param u sin(θ), where θ is the polar angle
843      * @param tOu t/u
844      * @param pnm0Plus2 array containing scaled P<sub>n,m+2</sub>/u<sup>m+2</sup>
845      * @param pnm0Plus1 array containing scaled P<sub>n,m+1</sub>/u<sup>m+1</sup>
846      * @param pnm1Plus1 array containing scaled dP<sub>n,m+1</sub>/u<sup>m+1</sup>
847      * (may be null if second derivatives are not needed)
848      * @param pnm0 array to fill with scaled P<sub>n,m</sub>/u<sup>m</sup>
849      * @param pnm1 array to fill with scaled dP<sub>n,m</sub>/u<sup>m</sup>
850      * (may be null if first derivatives are not needed)
851      * @param pnm2 array to fill with scaled d²P<sub>n,m</sub>/u<sup>m</sup>
852      * (may be null if second derivatives are not needed)
853      * @return new value for index
854      */
855     private int computeTesseral(final int m, final int degree, final int index,
856                                 final double t, final double u, final double tOu,
857                                 final double[] pnm0Plus2, final double[] pnm0Plus1, final double[] pnm1Plus1,
858                                 final double[] pnm0, final double[] pnm1, final double[] pnm2) {
859 
860         final double u2 = u * u;
861 
862         // initialize recursion from sectorial terms
863         int n = FastMath.max(2, m);
864         if (n == m) {
865             pnm0[n] = sectorial[n];
866             ++n;
867         }
868 
869         // compute tesseral values
870         int localIndex = index;
871         while (n <= degree) {
872 
873             // value (equation 27 of the paper)
874             pnm0[n] = gnmOj[localIndex] * t * pnm0Plus1[n] - hnmOj[localIndex] * u2 * pnm0Plus2[n];
875 
876             ++localIndex;
877             ++n;
878 
879         }
880 
881         if (pnm1 != null) {
882 
883             // initialize recursion from sectorial terms
884             n = FastMath.max(2, m);
885             if (n == m) {
886                 pnm1[n] = m * tOu * pnm0[n];
887                 ++n;
888             }
889 
890             // compute tesseral values and derivatives with respect to polar angle
891             localIndex = index;
892             while (n <= degree) {
893 
894                 // first derivative (equation 30 of the paper)
895                 pnm1[n] = m * tOu * pnm0[n] - enm[localIndex] * u * pnm0Plus1[n];
896 
897                 ++localIndex;
898                 ++n;
899 
900             }
901 
902             if (pnm2 != null) {
903 
904                 // initialize recursion from sectorial terms
905                 n = FastMath.max(2, m);
906                 if (n == m) {
907                     pnm2[n] = m * (tOu * pnm1[n] - pnm0[n] / u2);
908                     ++n;
909                 }
910 
911                 // compute tesseral values and derivatives with respect to polar angle
912                 localIndex = index;
913                 while (n <= degree) {
914 
915                     // second derivative (differential of equation 30 with respect to theta)
916                     pnm2[n] = m * (tOu * pnm1[n] - pnm0[n] / u2) - enm[localIndex] * u * pnm1Plus1[n];
917 
918                     ++localIndex;
919                     ++n;
920 
921                 }
922 
923             }
924 
925         }
926 
927         return localIndex;
928 
929     }
930 
931     /** Compute one order of tesseral terms.
932      * <p>
933      * This corresponds to equations 27 and 30 of the paper.
934      * </p>
935      * @param m current order
936      * @param degree max degree
937      * @param index index in the flattened array
938      * @param t cos(θ), where θ is the polar angle
939      * @param u sin(θ), where θ is the polar angle
940      * @param tOu t/u
941      * @param pnm0Plus2 array containing scaled P<sub>n,m+2</sub>/u<sup>m+2</sup>
942      * @param pnm0Plus1 array containing scaled P<sub>n,m+1</sub>/u<sup>m+1</sup>
943      * @param pnm1Plus1 array containing scaled dP<sub>n,m+1</sub>/u<sup>m+1</sup>
944      * (may be null if second derivatives are not needed)
945      * @param pnm0 array to fill with scaled P<sub>n,m</sub>/u<sup>m</sup>
946      * @param pnm1 array to fill with scaled dP<sub>n,m</sub>/u<sup>m</sup>
947      * (may be null if first derivatives are not needed)
948      * @param pnm2 array to fill with scaled d²P<sub>n,m</sub>/u<sup>m</sup>
949      * (may be null if second derivatives are not needed)
950      * @param <T> instance of field element
951      * @return new value for index
952      */
953     private <T extends CalculusFieldElement<T>> int computeTesseral(final int m, final int degree, final int index,
954                                                                 final T t, final T u, final T tOu,
955                                                                 final T[] pnm0Plus2, final T[] pnm0Plus1, final T[] pnm1Plus1,
956                                                                 final T[] pnm0, final T[] pnm1, final T[] pnm2) {
957 
958         final T u2 = u.multiply(u);
959         final T zero = u.getField().getZero();
960         // initialize recursion from sectorial terms
961         int n = FastMath.max(2, m);
962         if (n == m) {
963             pnm0[n] = zero.add(sectorial[n]);
964             ++n;
965         }
966 
967         // compute tesseral values
968         int localIndex = index;
969         while (n <= degree) {
970 
971             // value (equation 27 of the paper)
972             pnm0[n] = t.multiply(gnmOj[localIndex]).multiply(pnm0Plus1[n]).subtract(u2.multiply(pnm0Plus2[n]).multiply(hnmOj[localIndex]));
973             ++localIndex;
974             ++n;
975 
976         }
977         if (pnm1 != null) {
978 
979             // initialize recursion from sectorial terms
980             n = FastMath.max(2, m);
981             if (n == m) {
982                 pnm1[n] = tOu.multiply(m).multiply(pnm0[n]);
983                 ++n;
984             }
985 
986             // compute tesseral values and derivatives with respect to polar angle
987             localIndex = index;
988             while (n <= degree) {
989 
990                 // first derivative (equation 30 of the paper)
991                 pnm1[n] = tOu.multiply(m).multiply(pnm0[n]).subtract(u.multiply(enm[localIndex]).multiply(pnm0Plus1[n]));
992 
993                 ++localIndex;
994                 ++n;
995 
996             }
997 
998             if (pnm2 != null) {
999 
1000                 // initialize recursion from sectorial terms
1001                 n = FastMath.max(2, m);
1002                 if (n == m) {
1003                     pnm2[n] =   tOu.multiply(pnm1[n]).subtract(pnm0[n].divide(u2)).multiply(m);
1004                     ++n;
1005                 }
1006 
1007                 // compute tesseral values and derivatives with respect to polar angle
1008                 localIndex = index;
1009                 while (n <= degree) {
1010 
1011                     // second derivative (differential of equation 30 with respect to theta)
1012                     pnm2[n] = tOu.multiply(pnm1[n]).subtract(pnm0[n].divide(u2)).multiply(m).subtract(u.multiply(pnm1Plus1[n]).multiply(enm[localIndex]));
1013                     ++localIndex;
1014                     ++n;
1015 
1016                 }
1017 
1018             }
1019 
1020         }
1021         return localIndex;
1022 
1023     }
1024 
1025     /** {@inheritDoc} */
1026     @Override
1027     public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
1028 
1029         final double mu = parameters[0];
1030 
1031         // get the position in body frame
1032         final AbsoluteDate date       = s.getDate();
1033         final Transform fromBodyFrame = bodyFrame.getTransformTo(s.getFrame(), date);
1034         final Transform toBodyFrame   = fromBodyFrame.getInverse();
1035         final Vector3D position       = toBodyFrame.transformPosition(s.getPVCoordinates().getPosition());
1036 
1037         // gradient of the non-central part of the gravity field
1038         return fromBodyFrame.transformVector(new Vector3D(gradient(date, position, mu)));
1039 
1040     }
1041 
1042     /** {@inheritDoc} */
1043     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
1044                                                                          final T[] parameters) {
1045 
1046         final T mu = parameters[0];
1047 
1048         // check for faster computation dedicated to derivatives with respect to state
1049         if (isGradientStateDerivative(s)) {
1050             @SuppressWarnings("unchecked")
1051             final FieldVector3D<Gradient> p = (FieldVector3D<Gradient>) s.getPVCoordinates().getPosition();
1052             @SuppressWarnings("unchecked")
1053             final FieldVector3D<T> a = (FieldVector3D<T>) accelerationWrtState(s.getDate().toAbsoluteDate(),
1054                                                                                s.getFrame(), p,
1055                                                                                (Gradient) mu);
1056             return a;
1057         } else if (isDSStateDerivative(s)) {
1058             @SuppressWarnings("unchecked")
1059             final FieldVector3D<DerivativeStructure> p = (FieldVector3D<DerivativeStructure>) s.getPVCoordinates().getPosition();
1060             @SuppressWarnings("unchecked")
1061             final FieldVector3D<T> a = (FieldVector3D<T>) accelerationWrtState(s.getDate().toAbsoluteDate(),
1062                                                                                s.getFrame(), p,
1063                                                                                (DerivativeStructure) mu);
1064             return a;
1065         }
1066 
1067         // get the position in body frame
1068         final FieldAbsoluteDate<T> date          = s.getDate();
1069         final Transform            fromBodyFrame = bodyFrame.getTransformTo(s.getFrame(), date.toAbsoluteDate());
1070         final Transform            toBodyFrame   = fromBodyFrame.getInverse();
1071         final FieldVector3D<T>     position      = toBodyFrame.transformPosition(s.getPVCoordinates().getPosition());
1072 
1073         // gradient of the non-central part of the gravity field
1074         return fromBodyFrame.transformVector(new FieldVector3D<>(gradient(date, position, mu)));
1075 
1076     }
1077 
1078     /** {@inheritDoc} */
1079     public Stream<EventDetector> getEventsDetectors() {
1080         return Stream.empty();
1081     }
1082 
1083     @Override
1084     /** {@inheritDoc} */
1085     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
1086         return Stream.empty();
1087     }
1088 
1089     /** Check if a field state corresponds to derivatives with respect to state.
1090      * @param state state to check
1091      * @param <T> type of the filed elements
1092      * @return true if state corresponds to derivatives with respect to state
1093      * @since 9.0
1094      */
1095     private <T extends CalculusFieldElement<T>> boolean isDSStateDerivative(final FieldSpacecraftState<T> state) {
1096         try {
1097             final DerivativeStructure dsMass = (DerivativeStructure) state.getMass();
1098             final int o = dsMass.getOrder();
1099             final int p = dsMass.getFreeParameters();
1100             if (o != 1 || p < 3) {
1101                 return false;
1102             }
1103             @SuppressWarnings("unchecked")
1104             final FieldPVCoordinates<DerivativeStructure> pv = (FieldPVCoordinates<DerivativeStructure>) state.getPVCoordinates();
1105             return isVariable(pv.getPosition().getX(), 0) &&
1106                    isVariable(pv.getPosition().getY(), 1) &&
1107                    isVariable(pv.getPosition().getZ(), 2);
1108         } catch (ClassCastException cce) {
1109             return false;
1110         }
1111     }
1112 
1113     /** Check if a field state corresponds to derivatives with respect to state.
1114      * @param state state to check
1115      * @param <T> type of the filed elements
1116      * @return true if state corresponds to derivatives with respect to state
1117      * @since 10.2
1118      */
1119     private <T extends CalculusFieldElement<T>> boolean isGradientStateDerivative(final FieldSpacecraftState<T> state) {
1120         try {
1121             final Gradient gMass = (Gradient) state.getMass();
1122             final int p = gMass.getFreeParameters();
1123             if (p < 3) {
1124                 return false;
1125             }
1126             @SuppressWarnings("unchecked")
1127             final FieldPVCoordinates<Gradient> pv = (FieldPVCoordinates<Gradient>) state.getPVCoordinates();
1128             return isVariable(pv.getPosition().getX(), 0) &&
1129                    isVariable(pv.getPosition().getY(), 1) &&
1130                    isVariable(pv.getPosition().getZ(), 2);
1131         } catch (ClassCastException cce) {
1132             return false;
1133         }
1134     }
1135 
1136     /** Check if a derivative represents a specified variable.
1137      * @param ds derivative to check
1138      * @param index index of the variable
1139      * @return true if the derivative represents a specified variable
1140      * @since 9.0
1141      */
1142     private boolean isVariable(final DerivativeStructure ds, final int index) {
1143         final double[] derivatives = ds.getAllDerivatives();
1144         boolean check = true;
1145         for (int i = 1; i < derivatives.length; ++i) {
1146             check &= derivatives[i] == ((index + 1 == i) ? 1.0 : 0.0);
1147         }
1148         return check;
1149     }
1150 
1151     /** Check if a derivative represents a specified variable.
1152      * @param g derivative to check
1153      * @param index index of the variable
1154      * @return true if the derivative represents a specified variable
1155      * @since 10.2
1156      */
1157     private boolean isVariable(final Gradient g, final int index) {
1158         final double[] derivatives = g.getGradient();
1159         boolean check = true;
1160         for (int i = 0; i < derivatives.length; ++i) {
1161             check &= derivatives[i] == ((index == i) ? 1.0 : 0.0);
1162         }
1163         return check;
1164     }
1165 
1166     /** Compute acceleration derivatives with respect to state parameters.
1167      * <p>
1168      * From a theoretical point of view, this method computes the same values
1169      * as {@link #acceleration(FieldSpacecraftState, CalculusFieldElement[])} in the
1170      * specific case of {@link DerivativeStructure} with respect to state, so
1171      * it is less general. However, it is *much* faster in this important case.
1172      * <p>
1173      * <p>
1174      * The derivatives should be computed with respect to position. The input
1175      * parameters already take into account the free parameters (6 or 7 depending
1176      * on derivation with respect to mass being considered or not) and order
1177      * (always 1). Free parameters at indices 0, 1 and 2 correspond to derivatives
1178      * with respect to position. Free parameters at indices 3, 4 and 5 correspond
1179      * to derivatives with respect to velocity (these derivatives will remain zero
1180      * as acceleration due to gravity does not depend on velocity). Free parameter
1181      * at index 6 (if present) corresponds to to derivatives with respect to mass
1182      * (this derivative will remain zero as acceleration due to gravity does not
1183      * depend on mass).
1184      * </p>
1185      * @param date current date
1186      * @param frame inertial reference frame for state (both orbit and attitude)
1187      * @param position position of spacecraft in inertial frame
1188      * @param mu central attraction coefficient to use
1189      * @return acceleration with all derivatives specified by the input parameters
1190      * own derivatives
1191      * @since 6.0
1192      */
1193     private FieldVector3D<DerivativeStructure> accelerationWrtState(final AbsoluteDate date, final Frame frame,
1194                                                                     final FieldVector3D<DerivativeStructure> position,
1195                                                                     final DerivativeStructure mu) {
1196 
1197         // free parameters
1198         final int freeParameters = mu.getFreeParameters();
1199 
1200         // get the position in body frame
1201         final Transform fromBodyFrame = bodyFrame.getTransformTo(frame, date);
1202         final Transform toBodyFrame   = fromBodyFrame.getInverse();
1203         final Vector3D positionBody   = toBodyFrame.transformPosition(position.toVector3D());
1204 
1205         // compute gradient and Hessian
1206         final GradientHessian gh   = gradientHessian(date, positionBody, mu.getReal());
1207 
1208         // gradient of the non-central part of the gravity field
1209         final double[] gInertial = fromBodyFrame.transformVector(new Vector3D(gh.getGradient())).toArray();
1210 
1211         // Hessian of the non-central part of the gravity field
1212         final RealMatrix hBody     = new Array2DRowRealMatrix(gh.getHessian(), false);
1213         final RealMatrix rot       = new Array2DRowRealMatrix(toBodyFrame.getRotation().getMatrix());
1214         final RealMatrix hInertial = rot.transpose().multiply(hBody).multiply(rot);
1215 
1216         // distribute all partial derivatives in a compact acceleration vector
1217         final double[] derivatives = new double[freeParameters + 1];
1218         final DerivativeStructure[] accDer = new DerivativeStructure[3];
1219         for (int i = 0; i < 3; ++i) {
1220 
1221             // first element is value of acceleration (i.e. gradient of field)
1222             derivatives[0] = gInertial[i];
1223 
1224             // Jacobian of acceleration (i.e. Hessian of field)
1225             derivatives[1] = hInertial.getEntry(i, 0);
1226             derivatives[2] = hInertial.getEntry(i, 1);
1227             derivatives[3] = hInertial.getEntry(i, 2);
1228 
1229             // next element is derivative with respect to parameter mu
1230             if (derivatives.length > 4 && isVariable(mu, 3)) {
1231                 derivatives[4] = gInertial[i] / mu.getReal();
1232             }
1233 
1234             accDer[i] = position.getX().getFactory().build(derivatives);
1235 
1236         }
1237 
1238         return new FieldVector3D<>(accDer);
1239 
1240     }
1241 
1242     /** Compute acceleration derivatives with respect to state parameters.
1243      * <p>
1244      * From a theoretical point of view, this method computes the same values
1245      * as {@link #acceleration(FieldSpacecraftState, CalculusFieldElement[])} in the
1246      * specific case of {@link DerivativeStructure} with respect to state, so
1247      * it is less general. However, it is *much* faster in this important case.
1248      * <p>
1249      * <p>
1250      * The derivatives should be computed with respect to position. The input
1251      * parameters already take into account the free parameters (6 or 7 depending
1252      * on derivation with respect to mass being considered or not) and order
1253      * (always 1). Free parameters at indices 0, 1 and 2 correspond to derivatives
1254      * with respect to position. Free parameters at indices 3, 4 and 5 correspond
1255      * to derivatives with respect to velocity (these derivatives will remain zero
1256      * as acceleration due to gravity does not depend on velocity). Free parameter
1257      * at index 6 (if present) corresponds to to derivatives with respect to mass
1258      * (this derivative will remain zero as acceleration due to gravity does not
1259      * depend on mass).
1260      * </p>
1261      * @param date current date
1262      * @param frame inertial reference frame for state (both orbit and attitude)
1263      * @param position position of spacecraft in inertial frame
1264      * @param mu central attraction coefficient to use
1265      * @return acceleration with all derivatives specified by the input parameters
1266      * own derivatives
1267      * @since 10.2
1268      */
1269     private FieldVector3D<Gradient> accelerationWrtState(final AbsoluteDate date, final Frame frame,
1270                                                          final FieldVector3D<Gradient> position,
1271                                                          final Gradient mu) {
1272 
1273         // free parameters
1274         final int freeParameters = mu.getFreeParameters();
1275 
1276         // get the position in body frame
1277         final Transform fromBodyFrame = bodyFrame.getTransformTo(frame, date);
1278         final Transform toBodyFrame   = fromBodyFrame.getInverse();
1279         final Vector3D positionBody   = toBodyFrame.transformPosition(position.toVector3D());
1280 
1281         // compute gradient and Hessian
1282         final GradientHessian gh   = gradientHessian(date, positionBody, mu.getReal());
1283 
1284         // gradient of the non-central part of the gravity field
1285         final double[] gInertial = fromBodyFrame.transformVector(new Vector3D(gh.getGradient())).toArray();
1286 
1287         // Hessian of the non-central part of the gravity field
1288         final RealMatrix hBody     = new Array2DRowRealMatrix(gh.getHessian(), false);
1289         final RealMatrix rot       = new Array2DRowRealMatrix(toBodyFrame.getRotation().getMatrix());
1290         final RealMatrix hInertial = rot.transpose().multiply(hBody).multiply(rot);
1291 
1292         // distribute all partial derivatives in a compact acceleration vector
1293         final double[] derivatives = new double[freeParameters];
1294         final Gradient[] accDer = new Gradient[3];
1295         for (int i = 0; i < 3; ++i) {
1296 
1297             // Jacobian of acceleration (i.e. Hessian of field)
1298             derivatives[0] = hInertial.getEntry(i, 0);
1299             derivatives[1] = hInertial.getEntry(i, 1);
1300             derivatives[2] = hInertial.getEntry(i, 2);
1301 
1302             // next element is derivative with respect to parameter mu
1303             if (derivatives.length > 3 && isVariable(mu, 3)) {
1304                 derivatives[3] = gInertial[i] / mu.getReal();
1305             }
1306 
1307             accDer[i] = new Gradient(gInertial[i], derivatives);
1308 
1309         }
1310 
1311         return new FieldVector3D<>(accDer);
1312 
1313     }
1314 
1315     /** {@inheritDoc} */
1316     public List<ParameterDriver> getParametersDrivers() {
1317         return Collections.singletonList(gmParameterDriver);
1318     }
1319 
1320 }