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