1   /* Copyright 2002-2024 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.analytical;
18  
19  
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.hipparchus.CalculusFieldElement;
24  import org.hipparchus.util.MathArrays;
25  import org.orekit.attitudes.AttitudeProvider;
26  import org.orekit.attitudes.FieldAttitude;
27  import org.orekit.attitudes.FrameAlignedProvider;
28  import org.orekit.orbits.FieldOrbit;
29  import org.orekit.orbits.Orbit;
30  import org.orekit.orbits.OrbitType;
31  import org.orekit.orbits.PositionAngleType;
32  import org.orekit.propagation.FieldSpacecraftState;
33  import org.orekit.time.FieldAbsoluteDate;
34  import org.orekit.utils.FieldArrayDictionary;
35  import org.orekit.utils.FieldTimeSpanMap;
36  import org.orekit.utils.ParameterDriver;
37  
38  /** Simple Keplerian orbit propagator.
39   * @see FieldOrbit
40   * @author Guylaine Prat
41   * @param <T> type of the field elements
42   */
43  public class FieldKeplerianPropagator<T extends CalculusFieldElement<T>> extends FieldAbstractAnalyticalPropagator<T> {
44  
45  
46      /** All states. */
47      private transient FieldTimeSpanMap<FieldSpacecraftState<T>, T> states;
48  
49      /** Build a propagator from orbit only.
50       * <p>The central attraction coefficient μ is set to the same value used
51       * for the initial orbit definition. Mass and attitude provider are set to
52       * unspecified non-null arbitrary values.</p>
53       *
54       * @param initialFieldOrbit initial orbit
55       * @see #FieldKeplerianPropagator(FieldOrbit, AttitudeProvider)
56       */
57      public FieldKeplerianPropagator(final FieldOrbit<T> initialFieldOrbit) {
58          this(initialFieldOrbit, FrameAlignedProvider.of(initialFieldOrbit.getFrame()),
59               initialFieldOrbit.getMu(), initialFieldOrbit.getA().getField().getZero().newInstance(DEFAULT_MASS));
60      }
61  
62      /** Build a propagator from orbit and central attraction coefficient μ.
63       * <p>Mass and attitude provider are set to unspecified non-null arbitrary values.</p>
64       *
65       * @param initialFieldOrbit initial orbit
66       * @param mu central attraction coefficient (m³/s²)
67       * @see #FieldKeplerianPropagator(FieldOrbit, AttitudeProvider, CalculusFieldElement)
68       */
69      public FieldKeplerianPropagator(final FieldOrbit<T> initialFieldOrbit, final T mu) {
70          this(initialFieldOrbit, FrameAlignedProvider.of(initialFieldOrbit.getFrame()),
71               mu, initialFieldOrbit.getA().getField().getZero().newInstance(DEFAULT_MASS));
72      }
73  
74      /** Build a propagator from orbit and attitude provider.
75       * <p>The central attraction coefficient μ is set to the same value
76       * used for the initial orbit definition. Mass is set to an unspecified
77       * non-null arbitrary value.</p>
78       * @param initialFieldOrbit initial orbit
79       * @param attitudeProv  attitude provider
80       */
81      public FieldKeplerianPropagator(final FieldOrbit<T> initialFieldOrbit,
82                                      final AttitudeProvider attitudeProv) {
83          this(initialFieldOrbit, attitudeProv, initialFieldOrbit.getMu(),
84                  initialFieldOrbit.getA().getField().getZero().newInstance(DEFAULT_MASS));
85      }
86  
87      /** Build a propagator from orbit, attitude provider and central attraction
88       * coefficient μ.
89       * <p>Mass is set to an unspecified non-null arbitrary value.</p>
90       * @param initialFieldOrbit initial orbit
91       * @param attitudeProv attitude provider
92       * @param mu central attraction coefficient (m³/s²)
93       */
94      public FieldKeplerianPropagator(final FieldOrbit<T> initialFieldOrbit,
95                                      final AttitudeProvider attitudeProv,
96                                      final T mu) {
97          this(initialFieldOrbit, attitudeProv, mu, initialFieldOrbit.getA().getField().getZero().newInstance(DEFAULT_MASS));
98      }
99  
100     /** Build propagator from orbit, attitude provider, central attraction
101      * coefficient μ and mass.
102      * @param initialOrbit initial orbit
103      * @param attitudeProv attitude provider
104      * @param mu central attraction coefficient (m³/s²)
105      * @param mass spacecraft mass (kg)
106      */
107     public FieldKeplerianPropagator(final FieldOrbit<T> initialOrbit, final AttitudeProvider attitudeProv,
108                                     final T mu, final T mass) {
109 
110         super(initialOrbit.getA().getField(), attitudeProv);
111 
112         // ensure the orbit use the specified mu and has no non-Keplerian derivatives
113         final FieldSpacecraftState<T> initial = fixState(initialOrbit,
114                                                          getAttitudeProvider().getAttitude(initialOrbit,
115                                                                                            initialOrbit.getDate(),
116                                                                                            initialOrbit.getFrame()),
117                                                          mass, mu, null, null);
118         states = new FieldTimeSpanMap<>(initial, initialOrbit.getA().getField());
119         super.resetInitialState(initial);
120     }
121 
122     /** Fix state to use a specified mu and remove derivatives.
123      * <p>
124      * This ensures the propagation model (which is based on calling
125      * {@link Orbit#shiftedBy(double)}) is Keplerian only and uses a specified mu.
126      * </p>
127      * @param orbit orbit to fix
128      * @param attitude current attitude
129      * @param mass current mass
130      * @param mu gravity coefficient to use
131      * @param additionalStates additional states (may be null)
132      * @param additionalStatesderivatives additional states derivatives (may be null)
133      * @return fixed orbit
134      */
135     private FieldSpacecraftState<T> fixState(final FieldOrbit<T> orbit, final FieldAttitude<T> attitude, final T mass, final T mu,
136                                              final FieldArrayDictionary<T> additionalStates,
137                                              final FieldArrayDictionary<T> additionalStatesderivatives) {
138         final OrbitType type = orbit.getType();
139         final T[] stateVector = MathArrays.buildArray(mass.getField(), 6);
140         final PositionAngleType positionAngleType = PositionAngleType.MEAN;
141         type.mapOrbitToArray(orbit, positionAngleType, stateVector, null);
142         final FieldOrbit<T> fixedOrbit = type.mapArrayToOrbit(stateVector, null, positionAngleType,
143                                                               orbit.getDate(), mu, orbit.getFrame());
144         FieldSpacecraftState<T> fixedState = new FieldSpacecraftState<>(fixedOrbit, attitude, mass);
145         if (additionalStates != null) {
146             for (final FieldArrayDictionary<T>.Entry entry : additionalStates.getData()) {
147                 fixedState = fixedState.addAdditionalState(entry.getKey(), entry.getValue());
148             }
149         }
150         if (additionalStatesderivatives != null) {
151             for (final FieldArrayDictionary<T>.Entry entry : additionalStatesderivatives.getData()) {
152                 fixedState = fixedState.addAdditionalStateDerivative(entry.getKey(), entry.getValue());
153             }
154         }
155         return fixedState;
156     }
157 
158     /** {@inheritDoc} */
159     public void resetInitialState(final FieldSpacecraftState<T> state) {
160 
161         // ensure the orbit use the specified mu and has no non-Keplerian derivatives
162         final FieldSpacecraftState<T> formerInitial = getInitialState();
163         final T mu = formerInitial == null ? state.getMu() : formerInitial.getMu();
164         final FieldSpacecraftState<T> fixedState = fixState(state.getOrbit(),
165                                                             state.getAttitude(),
166                                                             state.getMass(),
167                                                             mu,
168                                                             state.getAdditionalStatesValues(),
169                                                             state.getAdditionalStatesDerivatives());
170 
171         states = new FieldTimeSpanMap<>(fixedState, state.getDate().getField());
172         super.resetInitialState(fixedState);
173 
174     }
175 
176     /** {@inheritDoc} */
177     protected void resetIntermediateState(final FieldSpacecraftState<T> state, final boolean forward) {
178         if (forward) {
179             states.addValidAfter(state, state.getDate());
180         } else {
181             states.addValidBefore(state, state.getDate());
182         }
183         stateChanged(state);
184     }
185 
186     /** {@inheritDoc} */
187     protected FieldOrbit<T> propagateOrbit(final FieldAbsoluteDate<T> date, final T[] parameters) {
188         // propagate orbit
189         FieldOrbit<T> orbit = states.get(date).getOrbit();
190         do {
191             // we use a loop here to compensate for very small date shifts error
192             // that occur with long propagation time
193             orbit = orbit.shiftedBy(date.durationFrom(orbit.getDate()));
194         } while (!date.equals(orbit.getDate()));
195         return orbit;
196     }
197 
198     /** {@inheritDoc}*/
199     protected T getMass(final FieldAbsoluteDate<T> date) {
200         return states.get(date).getMass();
201     }
202 
203     /** {@inheritDoc} */
204     @Override
205     public List<ParameterDriver> getParametersDrivers() {
206         // Keplerian propagation model does not have parameter drivers.
207         return Collections.emptyList();
208     }
209 
210 }