FieldNumericalPropagator.java

  1. /* Copyright 2002-2017 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;

  21. import org.hipparchus.Field;
  22. import org.hipparchus.RealFieldElement;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.ode.FieldODEIntegrator;
  25. import org.hipparchus.util.MathArrays;
  26. import org.orekit.attitudes.AttitudeProvider;
  27. import org.orekit.attitudes.FieldAttitude;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitIllegalArgumentException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.forces.ForceModel;
  32. import org.orekit.forces.gravity.NewtonianAttraction;
  33. import org.orekit.frames.Frame;
  34. import org.orekit.orbits.FieldOrbit;
  35. import org.orekit.orbits.OrbitType;
  36. import org.orekit.orbits.PositionAngle;
  37. import org.orekit.propagation.FieldSpacecraftState;
  38. import org.orekit.propagation.SpacecraftState;
  39. import org.orekit.propagation.events.FieldEventDetector;
  40. import org.orekit.propagation.integration.FieldAbstractIntegratedPropagator;
  41. import org.orekit.propagation.integration.FieldStateMapper;
  42. import org.orekit.time.AbsoluteDate;
  43. import org.orekit.time.FieldAbsoluteDate;
  44. import org.orekit.utils.FieldPVCoordinates;
  45. import org.orekit.utils.TimeStampedFieldPVCoordinates;

  46. /** This class propagates {@link org.orekit.orbits.FieldOrbit orbits} using
  47.  * numerical integration.
  48.  * <p>Numerical propagation is much more accurate than analytical propagation
  49.  * like for example {@link org.orekit.propagation.analytical.KeplerianPropagator
  50.  * Keplerian} or {@link org.orekit.propagation.analytical.EcksteinHechlerPropagator
  51.  * Eckstein-Hechler}, but requires a few more steps to set up to be used properly.
  52.  * Whereas analytical propagators are configured only thanks to their various
  53.  * constructors and can be used immediately after construction, numerical propagators
  54.  * configuration involve setting several parameters between construction time
  55.  * and propagation time.</p>
  56.  * <p>The configuration parameters that can be set are:</p>
  57.  * <ul>
  58.  *   <li>the initial spacecraft state ({@link #setInitialState(FieldSpacecraftState)})</li>
  59.  *   <li>the central attraction coefficient ({@link #setMu(double)})</li>
  60.  *   <li>the various force models ({@link #addForceModel(ForceModel)},
  61.  *   {@link #removeForceModels()})</li>
  62.  *   <li>the {@link OrbitType type} of orbital parameters to be used for propagation
  63.  *   ({@link #setOrbitType(OrbitType)}),
  64.  *   <li>the {@link PositionAngle type} of position angle to be used in orbital parameters
  65.  *   to be used for propagation where it is relevant ({@link
  66.  *   #setPositionAngleType(PositionAngle)}),
  67.  *   <li>whether {@link org.orekit.propagation.integration.FieldAdditionalEquations additional equations}
  68.  *   should be propagated along with orbital state
  69.  *   ({@link #addAdditionalEquations(org.orekit.propagation.integration.FieldAdditionalEquations)}),
  70.  *   <li>the discrete events that should be triggered during propagation
  71.  *   ({@link #addEventDetector(FieldEventDetector)},
  72.  *   {@link #clearEventsDetectors()})</li>
  73.  *   <li>the binding logic with the rest of the application ({@link #setSlaveMode()},
  74.  *   {@link #setMasterMode(RealFieldElement, org.orekit.propagation.sampling.FieldOrekitFixedStepHandler)},
  75.  *   {@link #setMasterMode(org.orekit.propagation.sampling.FieldOrekitStepHandler)},
  76.  *   {@link #setEphemerisMode()}, {@link #getGeneratedEphemeris()})</li>
  77.  * </ul>
  78.  * <p>From these configuration parameters, only the initial state is mandatory. The default
  79.  * propagation settings are in {@link OrbitType#EQUINOCTIAL equinoctial} parameters with
  80.  * {@link PositionAngle#TRUE true} longitude argument. If the central attraction coefficient
  81.  * is not explicitly specified, the one used to define the initial orbit will be used.
  82.  * However, specifying only the initial state and perhaps the central attraction coefficient
  83.  * would mean the propagator would use only Keplerian forces. In this case, the simpler {@link
  84.  * org.orekit.propagation.analytical.KeplerianPropagator KeplerianPropagator} class would
  85.  * perhaps be more effective.</p>
  86.  * <p>The underlying numerical integrator set up in the constructor may also have its own
  87.  * configuration parameters. Typical configuration parameters for adaptive stepsize integrators
  88.  * are the min, max and perhaps start step size as well as the absolute and/or relative errors
  89.  * thresholds.</p>
  90.  * <p>The state that is seen by the integrator is a simple seven elements double array.
  91.  * The six first elements are either:
  92.  * <ul>
  93.  *   <li>the {@link org.orekit.orbits.FieldEquinoctialOrbit equinoctial orbit parameters} (a, e<sub>x</sub>,
  94.  *   e<sub>y</sub>, h<sub>x</sub>, h<sub>y</sub>, λ<sub>M</sub> or λ<sub>E</sub>
  95.  *   or λ<sub>v</sub>) in meters and radians,</li>
  96.  *   <li>the {@link org.orekit.orbits.FieldKeplerianOrbit Keplerian orbit parameters} (a, e, i, ω, Ω,
  97.  *   M or E or v) in meters and radians,</li>
  98.  *   <li>the {@link org.orekit.orbits.FieldCircularOrbit circular orbit parameters} (a, e<sub>x</sub>, e<sub>y</sub>, i,
  99.  *   Ω, α<sub>M</sub> or α<sub>E</sub> or α<sub>v</sub>) in meters
  100.  *   and radians,</li>
  101.  *   <li>the {@link org.orekit.orbits.FieldCartesianOrbit Cartesian orbit parameters} (x, y, z, v<sub>x</sub>,
  102.  *   v<sub>y</sub>, v<sub>z</sub>) in meters and meters per seconds.
  103.  * </ul>
  104.  * The last element is the mass in kilograms.
  105.  * </p>
  106.  * <p>The following code snippet shows a typical setting for Low Earth Orbit propagation in
  107.  * equinoctial parameters and true longitude argument:</p>
  108.  * <pre>
  109.  * final T          zero      = field.getZero();
  110.  * final T          dP        = zero.add(0.001);
  111.  * final T          minStep   = zero.add(0.001);
  112.  * final T          maxStep   = zero.add(500);
  113.  * final T          initStep  = zero.add(60);
  114.  * final double[][] tolerance = FieldNumericalPropagator.tolerances(dP, orbit, OrbitType.EQUINOCTIAL);
  115.  * AdaptiveStepsizeFieldIntegrator<T> integrator = new DormandPrince853FieldIntegrator<>(field, minStep, maxStep, tolerance[0], tolerance[1]);
  116.  * integrator.setInitialStepSize(initStep);
  117.  * propagator = new FieldNumericalPropagator<>(field, integrator);
  118.  * </pre>
  119.  * <p>By default, at the end of the propagation, the propagator resets the initial state to the final state,
  120.  * thus allowing a new propagation to be started from there without recomputing the part already performed.
  121.  * This behaviour can be chenged by calling {@link #setResetAtEnd(boolean)}.
  122.  * </p>
  123.  * <p>Beware the same instance cannot be used simultaneously by different threads, the class is <em>not</em>
  124.  * thread-safe.</p>

  125.  * @see FieldSpacecraftState
  126.  * @see ForceModel
  127.  * @see org.orekit.propagation.sampling.FieldOrekitStepHandler
  128.  * @see org.orekit.propagation.sampling.FieldOrekitFixedStepHandler
  129.  * @see org.orekit.propagation.integration.FieldIntegratedEphemeris
  130.  * @see FieldTimeDerivativesEquations
  131.  *
  132.  * @author Mathieu Rom&eacute;ro
  133.  * @author Luc Maisonobe
  134.  * @author Guylaine Prat
  135.  * @author Fabien Maussion
  136.  * @author V&eacute;ronique Pommier-Maurussane
  137.  */
  138. public class FieldNumericalPropagator<T extends RealFieldElement<T>> extends FieldAbstractIntegratedPropagator<T> {

  139.     /** Central body attraction. */
  140.     private NewtonianAttraction newtonianAttraction;

  141.     /** Force models used during the extrapolation of the FieldOrbit<T>, without Jacobians. */
  142.     private final List<ForceModel> forceModels;

  143.     /** Create a new instance of NumericalPropagator, based on orbit definition mu.
  144.      * After creation, the instance is empty, i.e. the attitude provider is set to an
  145.      * unspecified default law and there are no perturbing forces at all.
  146.      * This means that if {@link #addForceModel addForceModel} is not
  147.      * called after creation, the integrated orbit will follow a Keplerian
  148.      * evolution only. The defaults are {@link OrbitType#EQUINOCTIAL}
  149.      * for {@link #setOrbitType(OrbitType) propagation
  150.      * orbit type} and {@link PositionAngle#TRUE} for {@link
  151.      * #setPositionAngleType(PositionAngle) position angle type}.
  152.      * @param integrator numerical integrator to use for propagation.
  153.      * @param field Field used by default
  154.      */
  155.     public FieldNumericalPropagator(final Field<T> field, final FieldODEIntegrator<T> integrator) {
  156.         super(field, integrator, true);
  157.         forceModels = new ArrayList<ForceModel>();
  158.         initMapper();
  159.         setAttitudeProvider(DEFAULT_LAW);
  160.         setMu(Double.NaN);
  161.         setSlaveMode();
  162.         setOrbitType(OrbitType.EQUINOCTIAL);
  163.         setPositionAngleType(PositionAngle.TRUE);
  164.     }

  165.      /** Set the central attraction coefficient μ.
  166.      * @param mu central attraction coefficient (m³/s²)
  167.      * @see #addForceModel(ForceModel)
  168.      */
  169.     public void setMu(final double mu) {
  170.         super.setMu(mu);
  171.         newtonianAttraction = new NewtonianAttraction(mu);
  172.     }

  173.     /** Add a force model to the global perturbation model.
  174.      * <p>If this method is not called at all, the integrated orbit will follow
  175.      * a Keplerian evolution only.</p>
  176.      * @param model perturbing {@link ForceModel} to add
  177.      * @see #removeForceModels()
  178.      * @see #setMu(double)
  179.      */
  180.     public void addForceModel(final ForceModel model) {
  181.         forceModels.add(model);
  182.     }

  183.     /** Remove all perturbing force models from the global perturbation model.
  184.      * <p>Once all perturbing forces have been removed (and as long as no new force
  185.      * model is added), the integrated orbit will follow a Keplerian evolution
  186.      * only.</p>
  187.      * @see #addForceModel(ForceModel)
  188.      */
  189.     public void removeForceModels() {
  190.         forceModels.clear();
  191.     }

  192.     /** Get perturbing force models list.
  193.      * @return list of perturbing force models
  194.      * @see #addForceModel(ForceModel)
  195.      * @see #getNewtonianAttractionForceModel()
  196.      */
  197.     public List<ForceModel> getForceModels() {
  198.         return forceModels;
  199.     }

  200.     /** Get the Newtonian attraction from the central body force model.
  201.      * @return Newtonian attraction force model
  202.      * @see #setMu(double)
  203.      * @see #getForceModels()
  204.      */
  205.     public NewtonianAttraction getNewtonianAttractionForceModel() {
  206.         return newtonianAttraction;
  207.     }

  208.     /** Set propagation orbit type.
  209.      * @param orbitType orbit type to use for propagation
  210.      */
  211.     public void setOrbitType(final OrbitType orbitType) {
  212.         super.setOrbitType(orbitType);
  213.     }

  214.     /** Get propagation parameter type.
  215.      * @return orbit type used for propagation
  216.      */
  217.     public OrbitType getOrbitType() {
  218.         return super.getOrbitType();
  219.     }

  220.     /** Set position angle type.
  221.      * <p>
  222.      * The position parameter type is meaningful only if {@link
  223.      * #getOrbitType() propagation orbit type}
  224.      * support it. As an example, it is not meaningful for propagation
  225.      * in {@link OrbitType#CARTESIAN Cartesian} parameters.
  226.      * </p>
  227.      * @param positionAngleType angle type to use for propagation
  228.      */
  229.     public void setPositionAngleType(final PositionAngle positionAngleType) {
  230.         super.setPositionAngleType(positionAngleType);
  231.     }

  232.     /** Get propagation parameter type.
  233.      * @return angle type to use for propagation
  234.      */
  235.     public PositionAngle getPositionAngleType() {
  236.         return super.getPositionAngleType();
  237.     }

  238.     /** Set the initial state.
  239.      * @param initialState initial state
  240.      * @exception OrekitException if initial state cannot be set
  241.      */
  242.     public void setInitialState(final FieldSpacecraftState<T> initialState) throws OrekitException {
  243.         resetInitialState(initialState);
  244.     }

  245.     /** {@inheritDoc} */
  246.     public void resetInitialState(final FieldSpacecraftState<T> state) throws OrekitException {
  247.         super.resetInitialState(state);
  248.         if (newtonianAttraction == null) {
  249.             setMu(state.getMu());
  250.         }
  251.         setStartDate(state.getDate());
  252.     }

  253.     /** {@inheritDoc} */
  254.     public TimeStampedFieldPVCoordinates<T> getPVCoordinates(final FieldAbsoluteDate<T> date, final Frame frame)
  255.         throws OrekitException {
  256.         return propagate(date).getPVCoordinates(frame);
  257.     }

  258.     /** {@inheritDoc} */
  259.     protected FieldStateMapper<T> createMapper(final FieldAbsoluteDate<T> referenceDate, final double mu,
  260.                                        final OrbitType orbitType, final PositionAngle positionAngleType,
  261.                                        final AttitudeProvider attitudeProvider, final Frame frame) {
  262.         return new FieldOsculatingMapper(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  263.     }

  264.     /** Internal mapper using directly osculating parameters. */
  265.     private class FieldOsculatingMapper extends FieldStateMapper<T> {

  266.         /** Simple constructor.
  267.          * <p>
  268.          * The position parameter type is meaningful only if {@link
  269.          * #getOrbitType() propagation orbit type}
  270.          * support it. As an example, it is not meaningful for propagation
  271.          * in {@link OrbitType#CARTESIAN Cartesian} parameters.
  272.          * </p>
  273.          * @param referenceDate reference date
  274.          * @param mu central attraction coefficient (m³/s²)
  275.          * @param orbitType orbit type to use for mapping
  276.          * @param positionAngleType angle type to use for propagation
  277.          * @param attitudeProvider attitude provider
  278.          * @param frame inertial frame
  279.          */
  280.         FieldOsculatingMapper(final FieldAbsoluteDate<T> referenceDate, final double mu,
  281.                               final OrbitType orbitType, final PositionAngle positionAngleType,
  282.                               final AttitudeProvider attitudeProvider, final Frame frame) {
  283.             super(referenceDate, mu, orbitType, positionAngleType, attitudeProvider, frame);
  284.         }

  285.         /** {@inheritDoc} */
  286.         public FieldSpacecraftState<T> mapArrayToState(final FieldAbsoluteDate<T> date, final T[] y, final T[] yDot,
  287.                                                        final boolean meanOnly)
  288.             throws OrekitException {
  289.             // the parameter meanOnly is ignored for the Numerical Propagator

  290.             final T mass = y[6];
  291.             if (mass.getReal() <= 0.0) {
  292.                 throw new OrekitException(OrekitMessages.SPACECRAFT_MASS_BECOMES_NEGATIVE, mass);
  293.             }
  294.             final FieldOrbit<T> orbit       = super.getOrbitType().mapArrayToOrbit(y, yDot, super.getPositionAngleType(), date, getMu(), getFrame());
  295.             final FieldAttitude<T> attitude = getAttitudeProvider().getAttitude(orbit, date, getFrame());
  296.             return new FieldSpacecraftState<>(orbit, attitude, mass);
  297.         }

  298.         /** {@inheritDoc} */
  299.         public void mapStateToArray(final FieldSpacecraftState<T> state, final T[] y, final T[] yDot) {
  300.             super.getOrbitType().mapOrbitToArray(state.getOrbit(), super.getPositionAngleType(), y, yDot);
  301.             y[6] = state.getMass();
  302.         }

  303.     }

  304.     /** {@inheritDoc} */
  305.     protected MainStateEquations<T> getMainStateEquations(final FieldODEIntegrator<T> integrator) {
  306.         return new Main(integrator);
  307.     }

  308.     /** Internal class for osculating parameters integration. */
  309.     private class Main implements MainStateEquations<T>, FieldTimeDerivativesEquations<T> {

  310.         /** Derivatives array. */
  311.         private final T[] yDot;

  312.         /** Current orbit. */
  313.         private FieldOrbit<T> orbit;

  314.         /** Jacobian of the orbital parameters with respect to the Cartesian parameters. */
  315.         private T[][] jacobian;

  316.         /** Simple constructor.
  317.          * @param integrator numerical integrator to use for propagation.
  318.          */
  319.         Main(final FieldODEIntegrator<T> integrator) {

  320.             this.yDot     = MathArrays.buildArray(getField(),  7);
  321.             this.jacobian = MathArrays.buildArray(getField(),  6, 6);
  322.             for (final ForceModel forceModel : forceModels) {
  323.                 forceModel.getFieldEventsDetectors(getField()).forEach(detector -> setUpEventDetector(integrator, detector));
  324.             }

  325.         }

  326.         /** {@inheritDoc} */
  327.         @Override
  328.         public void init(final FieldSpacecraftState<T> initialState, final FieldAbsoluteDate<T> target)
  329.             throws OrekitException {
  330.             final SpacecraftState stateD  = initialState.toSpacecraftState();
  331.             final AbsoluteDate    targetD = target.toAbsoluteDate();
  332.             for (final ForceModel forceModel : forceModels) {
  333.                 forceModel.init(stateD, targetD);
  334.             }
  335.         }

  336.         /** {@inheritDoc} */
  337.         @Override
  338.         public T[] computeDerivatives(final FieldSpacecraftState<T> state) throws OrekitException {
  339.             final T zero = state.getA().getField().getZero();
  340.             orbit = state.getOrbit();
  341.             Arrays.fill(yDot, zero);
  342.             orbit.getJacobianWrtCartesian(getPositionAngleType(), jacobian);
  343.             // compute the contributions of all perturbing forces
  344.             for (final ForceModel forceModel : forceModels) {
  345.                 forceModel.addContribution(state, this);
  346.             }
  347.             // finalize derivatives by adding the Kepler contribution
  348.             newtonianAttraction.addContribution(state, this);
  349.             return yDot.clone();

  350.         }

  351.         /** {@inheritDoc} */
  352.         @Override
  353.         public void addKeplerContribution(final double mu) {
  354.             orbit.addKeplerContribution(getPositionAngleType(), mu, yDot);
  355.         }

  356.         /** {@inheritDoc} */
  357.         @Override
  358.         public void addNonKeplerianAcceleration(final FieldVector3D<T> gamma)
  359.             throws OrekitException {
  360.             for (int i = 0; i < 6; ++i) {
  361.                 final T[] jRow = jacobian[i];
  362.                 yDot[i] = yDot[i].add(jRow[3].linearCombination(jRow[3], gamma.getX(),
  363.                                                                 jRow[4], gamma.getY(),
  364.                                                                 jRow[5], gamma.getZ()));
  365.             }
  366.         }

  367.         /** {@inheritDoc} */
  368.         @Override
  369.         public void addMassDerivative(final T q) {
  370.             if (q.getReal() > 0) {
  371.                 throw new OrekitIllegalArgumentException(OrekitMessages.POSITIVE_FLOW_RATE, q);
  372.             }
  373.             yDot[6] = yDot[6].add(q);
  374.         }

  375.     }

  376.     /** Estimate tolerance vectors for integrators.
  377.      * <p>
  378.      * The errors are estimated from partial derivatives properties of orbits,
  379.      * starting from a scalar position error specified by the user.
  380.      * Considering the energy conservation equation V = sqrt(mu (2/r - 1/a)),
  381.      * we get at constant energy (i.e. on a Keplerian trajectory):
  382.      * <pre>
  383.      * V² r |dV| = mu |dr|
  384.      * </pre>
  385.      * So we deduce a scalar velocity error consistent with the position error.
  386.      * From here, we apply orbits Jacobians matrices to get consistent errors
  387.      * on orbital parameters.
  388.      * </p>
  389.      * <p>
  390.      * The tolerances are only <em>orders of magnitude</em>, and integrator tolerances
  391.      * are only local estimates, not global ones. So some care must be taken when using
  392.      * these tolerances. Setting 1mm as a position error does NOT mean the tolerances
  393.      * will guarantee a 1mm error position after several orbits integration.
  394.      * </p>
  395.      * @param dP user specified position error
  396.      * @param orbit reference orbit
  397.      * @param type propagation type for the meaning of the tolerance vectors elements
  398.      * (it may be different from {@code orbit.getType()})
  399.      * @return a two rows array, row 0 being the absolute tolerance error and row 1
  400.      * being the relative tolerance error
  401.      * @exception OrekitException if Jacobian is singular
  402.      * @param <T> elements type
  403.      */
  404.     public static <T extends RealFieldElement<T>> double[][] tolerances(final T dP, final FieldOrbit<T> orbit, final OrbitType type)
  405.         throws OrekitException {

  406.         // estimate the scalar velocity error
  407.         final FieldPVCoordinates<T> pv = orbit.getPVCoordinates();
  408.         final T r2 = pv.getPosition().getNormSq();
  409.         final T v  = pv.getVelocity().getNorm();
  410.         final T dV = dP.multiply(orbit.getMu()).divide(v.multiply(r2));

  411.         final double[] absTol = new double[7];
  412.         final double[] relTol = new double[7];

  413.         // we set the mass tolerance arbitrarily to 1.0e-6 kg, as mass evolves linearly
  414.         // with trust, this often has no influence at all on propagation
  415.         absTol[6] = 1.0e-6;

  416.         if (type == OrbitType.CARTESIAN) {
  417.             absTol[0] = dP.getReal();
  418.             absTol[1] = dP.getReal();
  419.             absTol[2] = dP.getReal();
  420.             absTol[3] = dV.getReal();
  421.             absTol[4] = dV.getReal();
  422.             absTol[5] = dV.getReal();
  423.         } else {

  424.             // convert the orbit to the desired type
  425.             final T[][] jacobian = MathArrays.buildArray(dP.getField(), 6, 6);
  426.             final FieldOrbit<T> converted = type.convertType(orbit);
  427.             converted.getJacobianWrtCartesian(PositionAngle.TRUE, jacobian);

  428.             for (int i = 0; i < 6; ++i) {
  429.                 final  T[] row = jacobian[i];
  430.                 absTol[i] =     row[0].abs().multiply(dP).
  431.                             add(row[1].abs().multiply(dP)).
  432.                             add(row[2].abs().multiply(dP)).
  433.                             add(row[3].abs().multiply(dV)).
  434.                             add(row[4].abs().multiply(dV)).
  435.                             add(row[5].abs().multiply(dV)).
  436.                             getReal();
  437.                 if (Double.isNaN(absTol[i])) {
  438.                     throw new OrekitException(OrekitMessages.SINGULAR_JACOBIAN_FOR_ORBIT_TYPE, type);
  439.                 }
  440.             }

  441.         }

  442.         Arrays.fill(relTol, dP.divide(r2.sqrt()).getReal());

  443.         return new double[][]{
  444.             absTol, relTol
  445.         };

  446.     }

  447. }