TriggerDate.java

  1. /* Copyright 2002-2025 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.maneuvers.jacobians;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.linear.MatrixUtils;
  20. import org.hipparchus.linear.QRDecomposition;
  21. import org.hipparchus.linear.RealMatrix;
  22. import org.hipparchus.linear.RealVector;
  23. import org.orekit.forces.maneuvers.Maneuver;
  24. import org.orekit.forces.maneuvers.trigger.ManeuverTriggersResetter;
  25. import org.orekit.propagation.AdditionalDataProvider;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.utils.TimeSpanMap;

  30. /** Generator for one column of a Jacobian matrix for special case of trigger dates.
  31.  * <p>
  32.  * Typical use cases for this are estimation of maneuver start and stop date during
  33.  * either orbit determination or maneuver optimization.
  34.  * </p>
  35.  * <p>
  36.  * Let \((t_0, y_0)\) be the state at propagation start, \((t_1, y_1)\) be the state at
  37.  * maneuver trigger time, \((t_t, y_t)\) be the state at any arbitrary time \(t\) during
  38.  * propagation, and \(f_m(t, y)\) be the contribution of the maneuver to the global
  39.  * ODE \(\frac{dy}{dt} = f(t, y)\). We are interested in the Jacobian column
  40.  * \(\frac{\partial y_t}{\partial t_1}\).
  41.  * </p>
  42.  * <p>
  43.  * There are two parts in this Jacobian: the primary part corresponds to the full contribution
  44.  * of the acceleration due to the maneuver as it is delayed by a small amount \(dt_1\), whereas
  45.  * the secondary part corresponds to change of acceleration after maneuver start as the mass
  46.  * depletion is delayed and therefore the spacecraft mass is different from the mass for nominal
  47.  * start time.
  48.  * </p>
  49.  * <p>
  50.  * The primary part is computed as follows. After trigger time \(t_1\) (according to propagation direction),
  51.  * \[\frac{\partial y_t}{\partial t_1} = \pm \frac{\partial y_t}{\partial y_1} f_m(t_1, y_1)\]
  52.  * where the sign depends on \(t_1\) being a start or stop trigger and propagation being forward
  53.  * or backward.
  54.  * </p>
  55.  * <p>
  56.  * We don't have \(\frac{\partial y_t}{\partial y_1}\) available if \(t_1 \neq t_0\), but we
  57.  * have \(\frac{\partial y_t}{\partial y_0}\) at any time since it can be computed by integrating
  58.  * variational equations for numerical propagation or by other closed form expressions for analytical
  59.  * propagators. We use the classical composition rule to recover the state transition matrix with
  60.  * respect to intermediate time \(t_1\):
  61.  * \[\frac{\partial y_t}{\partial y_0} = \frac{\partial y_t}{\partial y_1} \frac{\partial y_1}{\partial y_0}\]
  62.  * We deduce
  63.  * \[\frac{\partial y_t}{\partial y_1} = \frac{\partial y_t}{\partial y_0} \left(\frac{\partial y_1}{\partial y_0}\right)^{-1}\]
  64.  * </p>
  65.  * <p>
  66.  * The contribution of the primary part to the Jacobian column can therefore be computed using the following
  67.  * closed-form expression:
  68.  * \[\frac{\partial y_t}{\partial t_1}
  69.  * = \pm \frac{\partial y_t}{\partial y_0} \left(\frac{\partial y_1}{\partial y_0}\right)^{-1} f_m(t_1, y_1)
  70.  * = \frac{\partial y_t}{\partial y_0} c_1\]
  71.  * where \(c_1\) is the signed contribution of maneuver at \(t_1\) and is computed at trigger time
  72.  * by solving \(\frac{\partial y_1}{\partial y_0} c_1 = \pm f_m(t_1, y_1)\).
  73.  * </p>
  74.  * <p>
  75.  * As the primary part of the column is generated using a closed-form expression, this generator
  76.  * implements the {@link AdditionalDataProvider} interface and stores the column directly
  77.  * in the primary state during propagation.
  78.  * </p>
  79.  * <p>
  80.  * As the closed-form expression requires picking \(c_1\) at trigger time \(t_1\), it works only
  81.  * if propagation starts outside of the maneuver and passes over \(t_1\) during integration.
  82.  * </p>
  83.  * <p>
  84.  * The secondary part is computed as follows. We have acceleration \(\vec{\Gamma} = \frac{\vec{F}}{m}\) and
  85.  * \(m = m_0 - q (t - t_s)\), where \(m\) is current mass, \(m_0\) is initial mass and \(t_s\) is
  86.  * maneuver trigger time. A delay \(dt_s\) on trigger time induces delaying mass depletion.
  87.  * We get:
  88.  * \[d\vec{\Gamma} = \frac{-\vec{F}}{m^2} dm = \frac{-\vec{F}}{m^2} q dt_s = -\vec{\Gamma}\frac{q}{m} dt_s\]
  89.  * From this total differential, we extract the partial derivative of the acceleration
  90.  * \[\frac{\partial\vec{\Gamma}}{\partial t_s} = -\vec{\Gamma}\frac{q}{m}\]
  91.  * </p>
  92.  * <p>
  93.  * The contribution of the secondary part to the Jacobian column can therefore be computed by integrating
  94.  * the partial derivative of the acceleration, to get the partial derivative of the position.
  95.  * </p>
  96.  * <p>
  97.  * As the secondary part of the column is generated using a differential equation, a separate
  98.  * underlying generator implementing the {@link AdditionalDerivativesProvider} interface is set up to
  99.  * perform the integration during propagation.
  100.  * </p>
  101.  * <p>
  102.  * This generator takes care to sum up the primary and secondary parts so the full column of the Jacobian
  103.  * is computed.
  104.  * </p>
  105.  * <p>
  106.  * The implementation takes care to <em>not</em> resetting \(c_1\) at propagation start.
  107.  * This allows to get proper Jacobian if we interrupt propagation in the middle of a maneuver
  108.  * and restart propagation where it left.
  109.  * </p>
  110.  * @author Luc Maisonobe
  111.  * @since 11.1
  112.  * @see MedianDate
  113.  * @see Duration
  114.  */
  115. public class TriggerDate
  116.     implements ManeuverTriggersResetter, AdditionalDataProvider<double[]> {

  117.     /** Dimension of the state. */
  118.     private static final int STATE_DIMENSION = 6;

  119.     /** Threshold for decomposing state transition matrix at trigger time. */
  120.     private static final double DECOMPOSITION_THRESHOLD = 1.0e-10;

  121.     /** Name of the state for State Transition Matrix. */
  122.     private final String stmName;

  123.     /** Name of the parameter corresponding to the column. */
  124.     private final String triggerName;

  125.     /** Mass depletion effect. */
  126.     private final MassDepletionDelay massDepletionDelay;

  127.     /** Start/stop management flag. */
  128.     private final boolean manageStart;

  129.     /** Maneuver force model. */
  130.     private final Maneuver maneuver;

  131.     /** Event detector threshold. */
  132.     private final double threshold;

  133.     /** Signed contribution of maneuver at trigger time ±(∂y₁/∂y₀)⁻¹ fₘ(t₁, y₁). */
  134.     private TimeSpanMap<double[]> contribution;

  135.     /** Trigger date. */
  136.     private AbsoluteDate trigger;

  137.     /** Indicator for forward propagation. */
  138.     private boolean forward;

  139.     /** Simple constructor.
  140.      * @param stmName name of State Transition Matrix state
  141.      * @param triggerName name of the parameter corresponding to the trigger date column
  142.      * @param manageStart if true, we compute derivatives with respect to maneuver start
  143.      * @param maneuver maneuver force model
  144.      * @param threshold event detector threshold
  145.      */
  146.     public TriggerDate(final String stmName, final String triggerName, final boolean manageStart,
  147.                        final Maneuver maneuver, final double threshold) {
  148.         this.stmName            = stmName;
  149.         this.triggerName        = triggerName;
  150.         this.massDepletionDelay = new MassDepletionDelay(triggerName, manageStart, maneuver);
  151.         this.manageStart        = manageStart;
  152.         this.maneuver           = maneuver;
  153.         this.threshold          = threshold;
  154.         this.contribution       = null;
  155.         this.trigger            = null;
  156.         this.forward            = true;
  157.     }

  158.     /** {@inheritDoc} */
  159.     @Override
  160.     public String getName() {
  161.         return triggerName;
  162.     }

  163.     /** {@inheritDoc}
  164.      * <p>
  165.      * The column state can be computed only if the State Transition Matrix state is available.
  166.      * </p>
  167.      */
  168.     @Override
  169.     public boolean yields(final SpacecraftState state) {
  170.         return !(state.hasAdditionalData(stmName) && state.hasAdditionalData(massDepletionDelay.getName()));
  171.     }

  172.     /** Get the mass depletion effect processor.
  173.      * @return mass depletion effect processor
  174.      */
  175.     public MassDepletionDelay getMassDepletionDelay() {
  176.         return massDepletionDelay;
  177.     }

  178.     /** {@inheritDoc} */
  179.     @Override
  180.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {

  181.         // note that we reset contribution or triggered ONLY at start or if we change
  182.         // propagation direction
  183.         // this allows to get proper Jacobian if we interrupt propagation
  184.         // in the middle of a maneuver and restart propagation where it left
  185.         final boolean newForward = target.isAfterOrEqualTo(initialState);
  186.         if (contribution == null || (forward ^ newForward)) {
  187.             contribution = new TimeSpanMap<>(null);
  188.             trigger      = null;
  189.         }

  190.         forward = newForward;

  191.     }

  192.     /** {@inheritDoc} */
  193.     @Override
  194.     public double[] getAdditionalData(final SpacecraftState state) {
  195.         // we check contribution rather than triggered because this method
  196.         // is called after maneuverTriggered and before resetState,
  197.         // when preparing the old state to be reset
  198.         final double[] c = contribution == null ? null : contribution.get(state.getDate());
  199.         if (c == null) {
  200.             // no thrust, no effect
  201.             return new double[STATE_DIMENSION];
  202.         } else {

  203.             // primary effect: full maneuver contribution at (delayed) trigger date
  204.             final double[] effect = getStm(state).operate(c);

  205.             // secondary effect: maneuver change throughout thrust as mass depletion is delayed
  206.             final double[] secondary = state.getAdditionalState(massDepletionDelay.getName());

  207.             // sum up both effects
  208.             for (int i = 0; i < effect.length; ++i) {
  209.                 effect[i] += secondary[i];
  210.             }

  211.             return effect;

  212.         }
  213.     }

  214.     /** {@inheritDoc}*/
  215.     @Override
  216.     public void maneuverTriggered(final SpacecraftState state, final boolean start) {
  217.         trigger = (start == manageStart) ? state.getDate() : null;
  218.     }

  219.     /** {@inheritDoc}*/
  220.     @Override
  221.     public SpacecraftState resetState(final SpacecraftState state) {

  222.         if (trigger == null) {
  223.             // this is not the maneuver trigger we expected (start vs. stop)
  224.             return state;
  225.         }

  226.         // get the acceleration near trigger time
  227.         final SpacecraftState stateWhenFiring = state.shiftedBy((manageStart ? 2 : -2) * threshold);
  228.         final Vector3D        acceleration    = maneuver.acceleration(stateWhenFiring, maneuver.getParameters(state.getDate()));

  229.         // initialize derivatives computation
  230.         final double     sign = (forward == manageStart) ? -1 : +1;
  231.         final RealVector rhs  = MatrixUtils.createRealVector(STATE_DIMENSION);
  232.         rhs.setEntry(3, sign * acceleration.getX());
  233.         rhs.setEntry(4, sign * acceleration.getY());
  234.         rhs.setEntry(5, sign * acceleration.getZ());

  235.         // get State Transition Matrix with respect to Cartesian parameters at trigger time
  236.         final RealMatrix dY1dY0 = getStm(state);

  237.         // store contribution factor for derivatives scm = ±(∂y₁/∂y₀)⁻¹ fₘ(t₁, y₁)
  238.         final double[] c = new QRDecomposition(dY1dY0, DECOMPOSITION_THRESHOLD).getSolver().solve(rhs).toArray();
  239.         if (forward) {
  240.             contribution.addValidAfter(c, state.getDate(), false);
  241.         } else {
  242.             contribution.addValidBefore(c, state.getDate(), false);
  243.         }

  244.         // return unchanged state
  245.         return state;

  246.     }

  247.     /** Extract State Transition Matrix with respect to Cartesian parameters.
  248.      * @param state state containing the State Transition Matrix
  249.      * @return State Transition Matrix
  250.      */
  251.     private RealMatrix getStm(final SpacecraftState state) {
  252.         final double[] p = state.getAdditionalState(stmName);
  253.         final RealMatrix dYdY0 = MatrixUtils.createRealMatrix(STATE_DIMENSION, STATE_DIMENSION);
  254.         int index = 0;
  255.         for (int i = 0; i < STATE_DIMENSION; ++i) {
  256.             for (int j = 0; j < STATE_DIMENSION; ++j) {
  257.                 dYdY0.setEntry(i, j, p[index++]);
  258.             }
  259.         }
  260.         return dYdY0;
  261.     }

  262. }