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.propagation;
18  
19  import org.orekit.time.AbsoluteDate;
20  
21  /** This interface allows to modify {@link SpacecraftState} and set up additional data.
22   * <p>
23   * A data can be any type of java Object (i.e., double[], String, class, etc.)
24   * </p>
25   * <p>
26   * {@link Propagator Propagators} generate {@link SpacecraftState states} that contain at
27   * least orbit, attitude, and mass. These states may however also contain {@link
28   * SpacecraftState#addAdditionalData(String, Object) additional data}. Instances of classes
29   * implementing this interface are intended to be registered to propagators so they can either
30   * modify the basic components (orbit, attitude and mass) or add additional data incrementally
31   * after having computed the basic components.
32   * </p>
33   * <p>
34   * Some additional data may depend on previous additional data to
35   * be already available the before they can be computed. It may even be impossible to compute some
36   * of these additional data at some time if they depend on conditions that are fulfilled only
37   * after propagation as started or some event has occurred. As the propagator builds the complete
38   * state incrementally, looping over the registered providers, it must call their {@link
39   * #update(SpacecraftState) update} methods in an order that fulfill these dependencies that
40   * may be time-dependent and are not related to the order in which the providers are registered to
41   * the propagator. This reordering is performed each time the complete state is built, using a yield
42   * mechanism. The propagator first pushes all providers in a stack and then empty the stack, one provider
43   * at a time, taking care to select only providers that do <em>not</em> {@link
44   * #yields(SpacecraftState) yield} when asked. Consider for example a case where providers A, B and C
45   * have been registered and provider B needs in fact the additional data generated by provider C. Then
46   * when a complete state is built, the propagator puts the three providers in a new stack, and then starts the incremental
47   * generation of additional data. It first checks provider A which does not yield so it is popped from
48   * the stack and the additional data it generates is added. Then provider B is checked, but it yields
49   * because state from provider C is not yet available. So propagator checks provider C which does not
50   * yield, so it is popped out of the stack and applied. At this stage, provider B is the only remaining one
51   * in the stack, so it is checked again, but this time it does not yield because the state from provider
52   * C is available as it has just been added, so provider B is popped from the stack and applied. The stack
53   * is now empty and the propagator can return the completed state.
54   * </p>
55   * <p>
56   * It is possible that at some stages in the propagation, a subset of the providers registered to a
57   * propagator all yield and cannot {@link #update(SpacecraftState) update} the data. This happens
58   * for example during the initialization phase of a propagator that
59   * computes State Transition Matrices or Jacobian matrices. These features are managed as secondary equations
60   * in the ODE integrator, and initialized after the primary equations (which correspond to orbit) have
61   * been initialized. So when the primary equation are initialized, the providers that depend on the secondary
62   * state will all yield. This behavior is expected. Another case occurs when users set up additional data
63   * that induce a dependency loop (data A depending on data B which depends on data C which depends on
64   * data A). In this case, the three corresponding providers will wait for each other and indefinitely yield.
65   * This second case is a deadlock and results from a design error of the additional data management at
66   * application level. The propagator cannot know it in advance if a subset of providers that all yield is
67   * normal or not. So at propagator level, when either situation is detected, the propagator just gives up and
68   * returns the most complete state it was able to compute, without generating any error. Errors will indeed
69   * not be triggered in the first case (once the primary equations have been initialized, the secondary
70   * equations will be initialized too), and they will be triggered in the second case as soon as user attempts
71   * to retrieve an additional data that was not added.
72   * </p>
73   * @see org.orekit.propagation.Propagator
74   * @see org.orekit.propagation.integration.AdditionalDerivativesProvider
75   * @author Luc Maisonobe
76   * @since 13.0
77   */
78  public interface AdditionalDataProvider<T> {
79  
80      /** Get the name of the additional data.
81       * <p>
82       * If a provider just modifies one of the basic elements (orbit, attitude
83       * or mass) without adding any new data, it should return the empty string
84       * as its name.
85       * </p>
86       * @return name of the additional data (names containing "orekit"
87       * with any case are reserved for the library internal use)
88       */
89      String getName();
90  
91      /** Initialize the additional data provider at the start of propagation.
92       * @param initialState initial spacecraft state information at the start of propagation
93       * @param target       date of propagation
94       */
95      default void init(final SpacecraftState initialState, final AbsoluteDate target) {
96          // nothing by default
97      }
98  
99      /** Check if this provider should yield so another provider has an opportunity to add missing parts.
100      * <p>
101      * Decision to yield is often based on an additional data being {@link SpacecraftState#hasAdditionalData(String)
102      * already available} in the provided {@code state} (but it could theoretically also depend on
103      * an additional state derivative being {@link SpacecraftState#hasAdditionalStateDerivative(String)
104      * already available}, or any other criterion). If for example a provider needs the state transition
105      * matrix, it could implement this method as:
106      * </p>
107      * <pre>{@code
108      * public boolean yields(final SpacecraftState state) {
109      *     return !state.hasAdditionalData("STM");
110      * }
111      * }</pre>
112      * <p>
113      * The default implementation returns {@code false}, meaning that state data can be
114      * {@link #getAdditionalData(SpacecraftState) generated} immediately.
115      * </p>
116      * @param state state to handle
117      * @return true if this provider should yield so another provider has an opportunity to add missing parts
118      * as the state is incrementally built up
119      */
120     default boolean yields(SpacecraftState state) {
121         return false;
122     }
123 
124     /** Get the additional data.
125      * @param state spacecraft state to which additional data should correspond
126      * @return additional state corresponding to spacecraft state
127      */
128     T getAdditionalData(SpacecraftState state);
129 
130     /** Update a state.
131      * @param state spacecraft state to update
132      * @return updated state
133      */
134     default SpacecraftState update(final SpacecraftState state) {
135         return state.addAdditionalData(getName(), getAdditionalData(state));
136     }
137 
138 }