FieldAdditionalStateProvider.java

  1. /* Copyright 2002-2022 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;

  18. import org.hipparchus.CalculusFieldElement;

  19. /** This interface represents providers for additional state data beyond {@link SpacecraftState}.
  20.  * <p>
  21.  * {@link FieldPropagator Propagators} generate {@link FieldSpacecraftState states} that contain at
  22.  * least orbit, attitude, and mass. These states may however also contain {@link
  23.  * FieldSpacecraftState#addAdditionalState(String, CalculusFieldElement...) additional states}. Instances of classes
  24.  * implementing this interface are intended to be registered to propagators so they can add these
  25.  * additional states incrementally after having computed the basic components
  26.  * (orbit, attitude and mass).
  27.  * </p>
  28.  * <p>
  29.  * Some additional states may depend on previous additional states to
  30.  * be already available the before they can be computed. It may even be impossible to compute some
  31.  * of these additional states at some time if they depend on conditions that are fulfilled only
  32.  * after propagation as started or some event has occurred. As the propagator builds the complete
  33.  * state incrementally, looping over the registered providers, it must call their {@link
  34.  * #getAdditionalState(FieldSpacecraftState) getAdditionalState} methods in an order that fulfill these dependencies that
  35.  * may be time-dependent and are not related to the order in which the providers are registered to
  36.  * the propagator. This reordering is performed each time the complete state is built, using a yield
  37.  * mechanism. The propagator first pushes all providers in a stack and then empty the stack, one provider
  38.  * at a time, taking care to select only providers that do <em>not</em> {@link
  39.  * #yield(FieldSpacecraftState) yield} when asked. Consider for example a case where providers A, B and C
  40.  * have been registered and provider B needs in fact the additional state generated by provider C. Then
  41.  * when a complete state is built, the propagator puts the three providers in a new stack, and then starts the incremental
  42.  * generation of additional states. It first checks provider A which does not yield so it is popped from
  43.  * the stack and the additional state it generates is added. Then provider B is checked, but it yields
  44.  * because state from provider C is not yet available. So propagator checks provider C which does not
  45.  * yield, so it is popped out of the stack and applied. At this stage, provider B is the only remaining one
  46.  * in the stack, so it is checked again, but this time it does not yield because the state from provider
  47.  * C is available as it has just been added, so provider B is popped from the stack and applied. The stack
  48.  * is now empty and the propagator can return the completed state.
  49.  * </p>
  50.  * <p>
  51.  * It is possible that at some stages in the propagation, a subset of the providers registered to a
  52.  * propagator all yied and cannot {@link #getAdditionalState(FieldSpacecraftState) retrieve} their additional
  53.  * state. This happens for example during the initialization phase of a propagator that
  54.  * computes State Transition Matrices or Jacobian matrices. These features are managed as secondary equations
  55.  * in the ODE integrator, and initialized after the primary equations (which correspond to orbit) have
  56.  * been initialized. So when the primary equation are initialized, the providers that depend on the secondary
  57.  * state will all yield. This behavior is expected. Another case occurs when users set up additional states
  58.  * that induce a dependency loop (state A depending on state B which depends on state C which depends on
  59.  * state A). In this case, the three corresponding providers will wait for each other and indefinitely yield.
  60.  * This second case is a deadlock and results from a design error of the additional states management at
  61.  * application level. The propagator cannot know it in advance if a subset of providers that all yield is
  62.  * normal or not. So at propagator level, when either situation is detected, the propagator just gives up and
  63.  * returns the most complete state it was able to compute, without generating any error. Errors will indeed
  64.  * not be triggered in the first case (once the primary equations have been initialized, the secondary
  65.  * equations will be initialized too), and they will be triggered in the second case as soon as user attempts
  66.  * to retrieve an additional state that was not added.
  67.  * </p>
  68.  * @see org.orekit.propagation.FieldPropagator
  69.  * @see org.orekit.propagation.integration.FieldAdditionalDerivativesProvider
  70.  * @author Luc Maisonobe
  71.  */
  72. public interface FieldAdditionalStateProvider<T extends CalculusFieldElement<T>> {

  73.     /** Get the name of the additional state.
  74.      * @return name of the additional state
  75.      */
  76.     String getName();

  77.     /** Check if this provider should yield so another provider has an opportunity to add missing parts.
  78.      * <p>
  79.      * Decision to yield is often based on an additional state being {@link FieldSpacecraftState#hasAdditionalState(String)
  80.      * already available} in the provided {@code state} (but it could theoretically also depend on
  81.      * an additional state derivative being {@link FieldSpacecraftState#hasAdditionalStateDerivative(String)
  82.      * already available}, or any other criterion). If for example a provider needs the state transition
  83.      * matrix, it could implement this method as:
  84.      * </p>
  85.      * <pre>{@code
  86.      * public boolean yield(final FieldSpacecraftState state) {
  87.      *     return !state.getAdditionalStates().containsKey("STM");
  88.      * }
  89.      * }</pre>
  90.      * <p>
  91.      * The default implementation returns {@code false}, meaning that state data can be
  92.      * {@link #getAdditionalState(FieldSpacecraftState) generated} immediately.
  93.      * </p>
  94.      * @param state state to handle
  95.      * @return true if this provider should yield so another provider has an opportunity to add missing parts
  96.      * as the state is incrementally built up
  97.      * @since 11.1
  98.      */
  99.     default boolean yield(FieldSpacecraftState<T> state) {
  100.         return false;
  101.     }

  102.     /** Get the additional state.
  103.      * @param state spacecraft state to which additional state should correspond
  104.      * @return additional state corresponding to spacecraft state
  105.      */
  106.     T[] getAdditionalState(FieldSpacecraftState<T> state);

  107. }