1   /* Copyright 2002-2026 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.propagation.numerical;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.hipparchus.CalculusFieldElement;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.ode.FieldODEIntegrator;
27  import org.hipparchus.util.MathArrays;
28  import org.orekit.annotation.DefaultDataContext;
29  import org.orekit.attitudes.AttitudeProvider;
30  import org.orekit.attitudes.FieldAttitude;
31  import org.orekit.data.DataContext;
32  import org.orekit.errors.OrekitException;
33  import org.orekit.errors.OrekitIllegalArgumentException;
34  import org.orekit.errors.OrekitInternalError;
35  import org.orekit.errors.OrekitMessages;
36  import org.orekit.forces.ForceModel;
37  import org.orekit.forces.gravity.NewtonianAttraction;
38  import org.orekit.frames.Frame;
39  import org.orekit.orbits.FieldOrbit;
40  import org.orekit.orbits.OrbitType;
41  import org.orekit.orbits.PositionAngleType;
42  import org.orekit.propagation.FieldSpacecraftState;
43  import org.orekit.propagation.PropagationType;
44  import org.orekit.propagation.Propagator;
45  import org.orekit.propagation.events.FieldEventDetector;
46  import org.orekit.propagation.integration.FieldAbstractIntegratedPropagator;
47  import org.orekit.propagation.integration.FieldStateMapper;
48  import org.orekit.time.AbsoluteDate;
49  import org.orekit.time.FieldAbsoluteDate;
50  import org.orekit.utils.FieldAbsolutePVCoordinates;
51  import org.orekit.utils.ParameterDriver;
52  import org.orekit.utils.ParameterObserver;
53  import org.orekit.utils.TimeSpanMap;
54  import org.orekit.utils.TimeStampedFieldPVCoordinates;
55  
56  /** This class propagates {@link org.orekit.orbits.FieldOrbit orbits} using
57   * numerical integration.
58   * <p>Numerical propagation is much more accurate than analytical propagation
59   * like for example {@link org.orekit.propagation.analytical.KeplerianPropagator
60   * Keplerian} or {@link org.orekit.propagation.analytical.EcksteinHechlerPropagator
61   * Eckstein-Hechler}, but requires a few more steps to set up to be used properly.
62   * Whereas analytical propagators are configured only thanks to their various
63   * constructors and can be used immediately after construction, numerical propagators
64   * configuration involve setting several parameters between construction time
65   * and propagation time.</p>
66   * <p>The configuration parameters that can be set are:</p>
67   * <ul>
68   *   <li>the initial spacecraft state ({@link #setInitialState(FieldSpacecraftState)})</li>
69   *   <li>the central attraction coefficient ({@link #setMu(CalculusFieldElement)})</li>
70   *   <li>the various force models ({@link #addForceModel(ForceModel)},
71   *   {@link #removeForceModels()})</li>
72   *   <li>the {@link OrbitType type} of orbital parameters to be used for propagation
73   *   ({@link #setOrbitType(OrbitType)}),
74   *   <li>the {@link PositionAngleType type} of position angle to be used in orbital parameters
75   *   to be used for propagation where it is relevant ({@link
76   *   #setPositionAngleType(PositionAngleType)}),
77   *   <li>whether {@link org.orekit.propagation.integration.FieldAdditionalDerivativesProvider additional derivatives providers}
78   *   should be propagated along with orbital state
79   *   ({@link #addAdditionalDerivativesProvider(org.orekit.propagation.integration.FieldAdditionalDerivativesProvider)}),
80   *   <li>the discrete events that should be triggered during propagation
81   *   ({@link #addEventDetector(FieldEventDetector)},
82   *   {@link #clearEventsDetectors()})</li>
83   *   <li>the binding logic with the rest of the application ({@link #getMultiplexer()})</li>
84   * </ul>
85   * <p>From these configuration parameters, only the initial state is mandatory. The default
86   * propagation settings are in {@link OrbitType#EQUINOCTIAL equinoctial} parameters with
87   * {@link PositionAngleType#ECCENTRIC} longitude argument. If the central attraction coefficient
88   * is not explicitly specified, the one used to define the initial orbit will be used.
89   * However, specifying only the initial state and perhaps the central attraction coefficient
90   * would mean the propagator would use only Keplerian forces. In this case, the simpler {@link
91   * org.orekit.propagation.analytical.KeplerianPropagator KeplerianPropagator} class would
92   * perhaps be more effective.</p>
93   * <p>The underlying numerical integrator set up in the constructor may also have its own
94   * configuration parameters. Typical configuration parameters for adaptive stepsize integrators
95   * are the min, max and perhaps start step size as well as the absolute and/or relative errors
96   * thresholds.</p>
97   * <p>The state that is seen by the integrator is a simple seven elements double array.
98   * The six first elements are either:
99   * <ul>
100  *   <li>the {@link org.orekit.orbits.FieldEquinoctialOrbit equinoctial orbit parameters} (a, e<sub>x</sub>,
101  *   e<sub>y</sub>, h<sub>x</sub>, h<sub>y</sub>, λ<sub>M</sub> or λ<sub>E</sub>
102  *   or λ<sub>v</sub>) in meters and radians,</li>
103  *   <li>the {@link org.orekit.orbits.FieldKeplerianOrbit Keplerian orbit parameters} (a, e, i, ω, Ω,
104  *   M or E or v) in meters and radians,</li>
105  *   <li>the {@link org.orekit.orbits.FieldCircularOrbit circular orbit parameters} (a, e<sub>x</sub>, e<sub>y</sub>, i,
106  *   Ω, α<sub>M</sub> or α<sub>E</sub> or α<sub>v</sub>) in meters
107  *   and radians,</li>
108  *   <li>the {@link org.orekit.orbits.FieldCartesianOrbit Cartesian orbit parameters} (x, y, z, v<sub>x</sub>,
109  *   v<sub>y</sub>, v<sub>z</sub>) in meters and meters per seconds.
110  * </ul>
111  * The last element is the mass in kilograms.
112  * <p>The following code snippet shows a typical setting for Low Earth Orbit propagation in
113  * equinoctial parameters and true longitude argument:</p>
114  * <pre>
115  * final T          zero      = field.getZero();
116  * final T          dP        = zero.add(0.001);
117  * final T          minStep   = zero.add(0.001);
118  * final T          maxStep   = zero.add(500);
119  * final T          initStep  = zero.add(60);
120  * final double[][] tolerance = ToleranceProvider.getDefaultToleranceProvider(dP).getTolerances(orbit, OrbitType.EQUINOCTIAL);
121  * AdaptiveStepsizeFieldIntegrator&lt;T&gt; integrator = new DormandPrince853FieldIntegrator&lt;&gt;(field, minStep, maxStep, tolerance[0], tolerance[1]);
122  * integrator.setInitialStepSize(initStep);
123  * propagator = new FieldNumericalPropagator&lt;&gt;(field, integrator);
124  * </pre>
125  * <p>By default, at the end of the propagation, the propagator resets the initial state to the final state,
126  * thus allowing a new propagation to be started from there without recomputing the part already performed.
127  * This behaviour can be changed by calling {@link #setResetAtEnd(boolean)}.
128  * </p>
129  * <p>Beware the same instance cannot be used simultaneously by different threads, the class is <em>not</em>
130  * thread-safe.</p>
131 
132  * @see FieldSpacecraftState
133  * @see ForceModel
134  * @see org.orekit.propagation.sampling.FieldOrekitStepHandler
135  * @see org.orekit.propagation.sampling.FieldOrekitFixedStepHandler
136  * @see org.orekit.propagation.integration.FieldIntegratedEphemeris
137  * @see FieldTimeDerivativesEquations
138  *
139  * @author Mathieu Rom&eacute;ro
140  * @author Luc Maisonobe
141  * @author Guylaine Prat
142  * @author Fabien Maussion
143  * @author V&eacute;ronique Pommier-Maurussane
144  * @param <T> type of the field elements
145  */
146 public class FieldNumericalPropagator<T extends CalculusFieldElement<T>> extends FieldAbstractIntegratedPropagator<T> {
147 
148     /** Force models used during the extrapolation of the FieldOrbit<T>, without Jacobians. */
149     private final List<ForceModel> forceModels;
150 
151     /** boolean to ignore or not the creation of a NewtonianAttraction. */
152     private boolean ignoreCentralAttraction = false;
153 
154     /**
155      * boolean to know if a full attitude (with rates) is needed when computing derivatives for the ODE.
156      */
157     private boolean needFullAttitudeForDerivatives = true;
158 
159     /**
160      * Create a new instance of NumericalPropagator, based on orbit definition mu.
161      * After creation, the instance is empty, i.e. the attitude provider is set to an
162      * unspecified default law and there are no perturbing forces at all.
163      * This means that if {@link #addForceModel addForceModel} is not
164      * called after creation, the integrated orbit will follow a Keplerian
165      * evolution only. The defaults are {@link OrbitType#EQUINOCTIAL}
166      * for {@link #setOrbitType(OrbitType) propagation
167      * orbit type} and {@link PositionAngleType#ECCENTRIC} for {@link
168      * #setPositionAngleType(PositionAngleType) position angle type}.
169      *
170      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
171      *
172      * @param integrator numerical integrator to use for propagation.
173      * @see #FieldNumericalPropagator(FieldODEIntegrator, AttitudeProvider)
174      */
175     @DefaultDataContext
176     public FieldNumericalPropagator(final FieldODEIntegrator<T> integrator) {
177         this(integrator, Propagator.getDefaultLaw(DataContext.getDefault().getFrames()));
178     }
179 
180     /**
181      * Create a new instance of NumericalPropagator, based on orbit definition mu.
182      * After creation, the instance is empty, i.e. the attitude provider is set to an
183      * unspecified default law and there are no perturbing forces at all.
184      * This means that if {@link #addForceModel addForceModel} is not
185      * called after creation, the integrated orbit will follow a Keplerian
186      * evolution only. The defaults are {@link OrbitType#EQUINOCTIAL}
187      * for {@link #setOrbitType(OrbitType) propagation
188      * orbit type} and {@link PositionAngleType#ECCENTRIC} for {@link
189      * #setPositionAngleType(PositionAngleType) position angle type}.
190      *
191      * @param integrator       numerical integrator to use for propagation.
192      * @param attitudeProvider attitude law to use.
193      * @since 10.1
194      */
195     public FieldNumericalPropagator(final FieldODEIntegrator<T> integrator,
196                                     final AttitudeProvider attitudeProvider) {
197         super(integrator.getCurrentSignedStepsize().getField(), integrator, PropagationType.OSCULATING);
198         forceModels = new ArrayList<>();
199         initMapper(getField());
200         setAttitudeProvider(attitudeProvider);
201         setMu(getField().getZero().add(Double.NaN));
202         clearStepHandlers();
203         setOrbitType(NumericalPropagator.DEFAULT_ORBIT_TYPE);
204         setPositionAngleType(NumericalPropagator.DEFAULT_POSITION_ANGLE_TYPE);
205     }
206 
207     /** Set the flag to ignore or not the creation of a {@link NewtonianAttraction}.
208      * @param ignoreCentralAttraction if true, {@link NewtonianAttraction} is <em>not</em>
209      * added automatically if missing
210      */
211     public void setIgnoreCentralAttraction(final boolean ignoreCentralAttraction) {
212         this.ignoreCentralAttraction = ignoreCentralAttraction;
213     }
214 
215     /** Set the central attraction coefficient μ.
216      * <p>
217      * Setting the central attraction coefficient is
218      * equivalent to {@link #addForceModel(ForceModel) add}
219      * a {@link NewtonianAttraction} force model.
220      * </p>
221      * @param mu central attraction coefficient (m³/s²)
222      * @see #addForceModel(ForceModel)
223      * @see #getAllForceModels()
224      */
225     @Override
226     public void setMu(final T mu) {
227         if (ignoreCentralAttraction) {
228             superSetMu(mu);
229         } else {
230             addForceModel(new NewtonianAttraction(mu.getReal()));
231         }
232     }
233 
234     /** Set the central attraction coefficient μ only in upper class.
235      * @param mu central attraction coefficient (m³/s²)
236      */
237     private void superSetMu(final T mu) {
238         super.setMu(mu);
239     }
240 
241     /** Check if Newtonian attraction force model is available.
242      * <p>
243      * Newtonian attraction is always the last force model in the list.
244      * </p>
245      * @return true if Newtonian attraction force model is available
246      */
247     private boolean hasNewtonianAttraction() {
248         final int last = forceModels.size() - 1;
249         return last >= 0 && forceModels.get(last) instanceof NewtonianAttraction;
250     }
251 
252     /** Add a force model to the global perturbation model.
253      * <p>If this method is not called at all, the integrated orbit will follow
254      * a Keplerian evolution only.</p>
255      * @param model perturbing {@link ForceModel} to add
256      * @see #removeForceModels()
257      * @see #setMu(CalculusFieldElement)
258      */
259     public void addForceModel(final ForceModel model) {
260 
261         if (model instanceof NewtonianAttraction) {
262             // we want to add the central attraction force model
263 
264             try {
265                 // ensure we are notified of any mu change
266                 model.getParametersDrivers().getFirst().addObserver(new ParameterObserver() {
267                     /** {@inheritDoc} */
268                     @Override
269                     public void valueChanged(final double previousValue, final ParameterDriver driver, final AbsoluteDate date) {
270                         // mu PDriver should have only 1 span
271                         superSetMu(getField().getZero().newInstance(driver.getValue(date)));
272                     }
273                     /** {@inheritDoc} */
274                     @Override
275                     public void valueSpanMapChanged(final TimeSpanMap<Double> previousValue, final ParameterDriver driver) {
276                         // mu PDriver should have only 1 span
277                         superSetMu(getField().getZero().newInstance(driver.getValue()));
278                     }
279                 });
280             } catch (OrekitException oe) {
281                 // this should never happen
282                 throw new OrekitInternalError(oe);
283             }
284 
285             if (hasNewtonianAttraction()) {
286                 // there is already a central attraction model, replace it
287                 forceModels.set(forceModels.size() - 1, model);
288             } else {
289                 // there are no central attraction model yet, add it at the end of the list
290                 forceModels.add(model);
291             }
292         } else {
293             // we want to add a perturbing force model
294             if (hasNewtonianAttraction()) {
295                 // insert the new force model before Newtonian attraction,
296                 // which should always be the last one in the list
297                 forceModels.add(forceModels.size() - 1, model);
298             } else {
299                 // we only have perturbing force models up to now, just append at the end of the list
300                 forceModels.add(model);
301             }
302         }
303 
304     }
305 
306     /** Remove all perturbing force models from the global perturbation model.
307      * <p>Once all perturbing forces have been removed (and as long as no new force
308      * model is added), the integrated orbit will follow a Keplerian evolution
309      * only.</p>
310      * @see #addForceModel(ForceModel)
311      */
312     public void removeForceModels() {
313         forceModels.clear();
314     }
315 
316     /** Get all the force models, perturbing forces and Newtonian attraction included.
317      * @return list of perturbing force models, with Newtonian attraction being the
318      * last one
319      * @see #addForceModel(ForceModel)
320      * @see #setMu(CalculusFieldElement)
321      * @since 9.1
322      */
323     public List<ForceModel> getAllForceModels() {
324         return Collections.unmodifiableList(forceModels);
325     }
326 
327     /** Set propagation orbit type.
328      * @param orbitType orbit type to use for propagation
329      */
330     @Override
331     public void setOrbitType(final OrbitType orbitType) {
332         super.setOrbitType(orbitType);
333     }
334 
335     /** Get propagation parameter type.
336      * @return orbit type used for propagation
337      */
338     @Override
339     public OrbitType getOrbitType() {
340         return superGetOrbitType();
341     }
342 
343     /** Get propagation parameter type.
344      * @return orbit type used for propagation
345      */
346     private OrbitType superGetOrbitType() {
347         return super.getOrbitType();
348     }
349 
350     /** Set position angle type.
351      * <p>
352      * The position parameter type is meaningful only if {@link
353      * #getOrbitType() propagation orbit type}
354      * support it. As an example, it is not meaningful for propagation
355      * in {@link OrbitType#CARTESIAN Cartesian} parameters.
356      * </p>
357      * @param positionAngleType angle type to use for propagation
358      */
359     @Override
360     public void setPositionAngleType(final PositionAngleType positionAngleType) {
361         super.setPositionAngleType(positionAngleType);
362     }
363 
364     /** Get propagation parameter type.
365      * @return angle type to use for propagation
366      */
367     @Override
368     public PositionAngleType getPositionAngleType() {
369         return super.getPositionAngleType();
370     }
371 
372     /** Set the initial state.
373      * @param initialState initial state
374      */
375     public void setInitialState(final FieldSpacecraftState<T> initialState) {
376         resetInitialState(initialState);
377     }
378 
379     /** {@inheritDoc} */
380     @Override
381     public void resetInitialState(final FieldSpacecraftState<T> state) {
382         super.resetInitialState(state);
383         if (!hasNewtonianAttraction()) {
384             setMu(state.getOrbit().getMu());
385         }
386         setStartDate(state.getDate());
387     }
388 
389     /** {@inheritDoc} */
390     @Override
391     protected AttitudeProvider initializeAttitudeProviderForDerivatives() {
392         return needFullAttitudeForDerivatives ? getAttitudeProvider() : getFrozenAttitudeProvider();
393     }
394 
395     /** {@inheritDoc} */
396     @Override
397     protected FieldStateMapper<T> createMapper(final FieldAbsoluteDate<T> referenceDate, final T mu,
398                                                final OrbitType orbitType, final PositionAngleType positionAngleType,
399                                                final AttitudeProvider attitudeProvider, final Frame frame) {
400         return new FieldOsculatingMapper(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
401     }
402 
403     /** Internal mapper using directly osculating parameters. */
404     private class FieldOsculatingMapper extends FieldStateMapper<T> {
405 
406         /** Simple constructor.
407          * <p>
408          * The position parameter type is meaningful only if {@link
409          * #getOrbitType() propagation orbit type}
410          * support it. As an example, it is not meaningful for propagation
411          * in {@link OrbitType#CARTESIAN Cartesian} parameters.
412          * </p>
413          * @param referenceDate reference date
414          * @param mu central attraction coefficient (m³/s²)
415          * @param orbitType orbit type to use for mapping
416          * @param positionAngleType angle type to use for propagation
417          * @param attitudeProvider attitude provider
418          * @param frame inertial frame
419          */
420         FieldOsculatingMapper(final FieldAbsoluteDate<T> referenceDate, final T mu,
421                               final OrbitType orbitType, final PositionAngleType positionAngleType,
422                               final AttitudeProvider attitudeProvider, final Frame frame) {
423             super(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
424         }
425 
426         /** {@inheritDoc} */
427         @Override
428         public FieldSpacecraftState<T> mapArrayToState(final FieldAbsoluteDate<T> date, final T[] y, final T[] yDot,
429                                                        final PropagationType type) {
430             // the parameter type is ignored for the Numerical Propagator
431 
432             final T mass = y[6];
433             final T massRate = yDot == null ? mass.getField().getZero() : yDot[6];
434             if (mass.getReal() <= 0.0) {
435                 throw new OrekitException(OrekitMessages.NOT_POSITIVE_SPACECRAFT_MASS, mass.getReal());
436             }
437 
438             if (superGetOrbitType() == null) {
439                 // propagation uses absolute position-velocity-acceleration
440                 final FieldVector3D<T> p = new FieldVector3D<>(y[0],    y[1],    y[2]);
441                 final FieldVector3D<T> v = new FieldVector3D<>(y[3],    y[4],    y[5]);
442                 final FieldVector3D<T> a;
443                 final FieldAbsolutePVCoordinates<T> absPva;
444                 if (yDot == null) {
445                     absPva = new FieldAbsolutePVCoordinates<>(getFrame(), new TimeStampedFieldPVCoordinates<>(date, p, v, FieldVector3D.getZero(date.getField())));
446                 } else {
447                     a = new FieldVector3D<>(yDot[3], yDot[4], yDot[5]);
448                     absPva = new FieldAbsolutePVCoordinates<>(getFrame(), new TimeStampedFieldPVCoordinates<>(date, p, v, a));
449                 }
450 
451                 final FieldAttitude<T> attitude = getAttitudeProvider().getAttitude(absPva, date, getFrame());
452                 return new FieldSpacecraftState<>(absPva, attitude).withMassRate(massRate).withMass(mass);
453             } else {
454                 // propagation uses regular orbits
455                 final FieldOrbit<T> orbit       = superGetOrbitType().mapArrayToOrbit(y, yDot, super.getPositionAngleType(), date, getMu(), getFrame());
456                 final FieldAttitude<T> attitude = getAttitudeProvider().getAttitude(orbit, date, getFrame());
457                 return new FieldSpacecraftState<>(orbit, attitude).withMassRate(massRate).withMass(mass);
458             }
459         }
460 
461         /** {@inheritDoc} */
462         @Override
463         public void mapStateToArray(final FieldSpacecraftState<T> state, final T[] y, final T[] yDot) {
464             if (superGetOrbitType() == null) {
465                 // propagation uses absolute position-velocity-acceleration
466                 final FieldVector3D<T> p = state.getAbsPVA().getPosition();
467                 final FieldVector3D<T> v = state.getAbsPVA().getVelocity();
468                 y[0] = p.getX();
469                 y[1] = p.getY();
470                 y[2] = p.getZ();
471                 y[3] = v.getX();
472                 y[4] = v.getY();
473                 y[5] = v.getZ();
474             }
475             else {
476                 superGetOrbitType().mapOrbitToArray(state.getOrbit(), super.getPositionAngleType(), y, yDot);
477             }
478             y[6] = state.getMass();
479         }
480 
481     }
482 
483     /** {@inheritDoc} */
484     @Override
485     protected MainStateEquations<T> getMainStateEquations(final FieldODEIntegrator<T> integrator) {
486         return new Main(integrator);
487     }
488 
489     /** Internal class for osculating parameters integration. */
490     private class Main implements MainStateEquations<T>, FieldTimeDerivativesEquations<T> {
491 
492         /** Derivatives array. */
493         private final T[] yDot;
494 
495         /** Current state. */
496         private FieldSpacecraftState<T> currentState;
497 
498         /** Jacobian of the orbital parameters with respect to the Cartesian parameters. */
499         private final T[][] jacobian;
500 
501         /** Flag keeping track whether Jacobian matrix needs to be recomputed or not. */
502         private boolean recomputingJacobian;
503 
504         /** Simple constructor.
505          * @param integrator numerical integrator to use for propagation.
506          */
507         Main(final FieldODEIntegrator<T> integrator) {
508 
509             this.yDot     = MathArrays.buildArray(getField(),  7);
510             this.jacobian = MathArrays.buildArray(getField(),  6, 6);
511             this.recomputingJacobian = true;
512 
513             // feed internal event detectors
514             for (final ForceModel forceModel : forceModels) {
515                 forceModel.getFieldEventDetectors(getField()).forEach(detector -> setUpEventDetector(integrator, detector));
516             }
517             getAttitudeProvider().getFieldEventDetectors(getField()).forEach(detector -> setUpEventDetector(integrator, detector));
518 
519             // default value for Jacobian is identity
520             for (int i = 0; i < jacobian.length; ++i) {
521                 Arrays.fill(jacobian[i], getField().getZero());
522                 jacobian[i][i] = getField().getOne();
523             }
524 
525         }
526 
527         /** {@inheritDoc} */
528         @Override
529         public void init(final FieldSpacecraftState<T> initialState, final FieldAbsoluteDate<T> target) {
530             needFullAttitudeForDerivatives = forceModels.stream().anyMatch(ForceModel::dependsOnAttitudeRate);
531 
532             forceModels.forEach(fm -> fm.init(initialState, target));
533 
534             final int numberOfForces = forceModels.size();
535             final OrbitType orbitType = superGetOrbitType();
536             if (orbitType != null && orbitType != OrbitType.CARTESIAN && numberOfForces > 0) {
537                 if (numberOfForces > 1) {
538                     recomputingJacobian = true;
539                 } else {
540                     recomputingJacobian = !(forceModels.getFirst() instanceof NewtonianAttraction);
541                 }
542             } else {
543                 recomputingJacobian = false;
544             }
545         }
546 
547         /** {@inheritDoc} */
548         @Override
549         public T[] computeDerivatives(final FieldSpacecraftState<T> state) {
550             final T zero = state.getMass().getField().getZero();
551             currentState = state;
552             Arrays.fill(yDot, zero);
553             if (recomputingJacobian) {
554                 // propagation uses Jacobian matrix of orbital parameters w.r.t. Cartesian ones
555                 currentState.getOrbit().getJacobianWrtCartesian(getPositionAngleType(), jacobian);
556             }
557 
558             // compute the contributions of all perturbing forces,
559             // using the Kepler contribution at the end since
560             // NewtonianAttraction is always the last instance in the list
561             for (final ForceModel forceModel : forceModels) {
562                 forceModel.addContribution(state, this);
563             }
564 
565             if (superGetOrbitType() == null) {
566                 // position derivative is velocity, and was not added above in the force models
567                 // (it is added when orbit type is non-null because NewtonianAttraction considers it)
568                 final FieldVector3D<T> velocity = currentState.getVelocity();
569                 yDot[0] = yDot[0].add(velocity.getX());
570                 yDot[1] = yDot[1].add(velocity.getY());
571                 yDot[2] = yDot[2].add(velocity.getZ());
572             }
573 
574             return yDot.clone();
575 
576         }
577 
578         /** {@inheritDoc} */
579         @Override
580         public void addKeplerContribution(final T mu) {
581             if (superGetOrbitType() == null) {
582 
583                 // if mu is neither 0 nor NaN, we want to include Newtonian acceleration
584                 if (mu.getReal() > 0) {
585                     // velocity derivative is Newtonian acceleration
586                     final FieldVector3D<T> position = currentState.getPosition();
587                     final T r2         = position.getNorm2Sq();
588                     final T coeff      = r2.multiply(r2.sqrt()).reciprocal().negate().multiply(mu);
589                     yDot[3] = yDot[3].add(coeff.multiply(position.getX()));
590                     yDot[4] = yDot[4].add(coeff.multiply(position.getY()));
591                     yDot[5] = yDot[5].add(coeff.multiply(position.getZ()));
592                 }
593 
594             } else {
595                 // propagation uses regular orbits
596                 currentState.getOrbit().addKeplerContribution(getPositionAngleType(), mu, yDot);
597             }
598         }
599 
600         /** {@inheritDoc} */
601         @Override
602         public void addNonKeplerianAcceleration(final FieldVector3D<T> gamma) {
603             for (int i = 0; i < 6; ++i) {
604                 final T[] jRow = jacobian[i];
605                 yDot[i] = yDot[i].add(jRow[3].linearCombination(jRow[3], gamma.getX(),
606                                                                 jRow[4], gamma.getY(),
607                                                                 jRow[5], gamma.getZ()));
608             }
609         }
610 
611         /** {@inheritDoc} */
612         @Override
613         public void addMassDerivative(final T q) {
614             if (q.getReal() > 0) {
615                 throw new OrekitIllegalArgumentException(OrekitMessages.POSITIVE_FLOW_RATE, q.getReal());
616             }
617             yDot[6] = yDot[6].add(q);
618         }
619 
620     }
621 
622 }
623