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.forces.maneuvers.trigger;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.stream.Stream;
22
23 import org.hipparchus.CalculusFieldElement;
24 import org.hipparchus.Field;
25 import org.hipparchus.ode.events.Action;
26 import org.orekit.propagation.FieldSpacecraftState;
27 import org.orekit.propagation.SpacecraftState;
28 import org.orekit.propagation.events.AbstractDetector;
29 import org.orekit.propagation.events.EventDetector;
30 import org.orekit.propagation.events.FieldAbstractDetector;
31 import org.orekit.propagation.events.FieldAdaptableInterval;
32 import org.orekit.propagation.events.FieldEventDetector;
33 import org.orekit.propagation.events.handlers.EventHandler;
34 import org.orekit.propagation.events.handlers.FieldEventHandler;
35 import org.orekit.time.AbsoluteDate;
36 import org.orekit.time.FieldAbsoluteDate;
37
38 /**
39 * Maneuver triggers based on a single event detector that defines firing intervals.
40 * <p>
41 * Firing intervals correspond to time spans with positive value of the event detector
42 * {@link EventDetector#g(SpacecraftState) g} function.
43 * </p>
44 * @param <T> type of the interval detector
45 * @see StartStopEventsTrigger
46 * @author Luc Maisonobe
47 * @since 11.1
48 */
49 public abstract class IntervalEventTrigger<T extends AbstractDetector<T>> extends AbstractManeuverTriggers {
50
51 /** Intervals detector. */
52 private final T firingIntervalDetector;
53
54 /** Cached field-based detectors. */
55 private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldEventDetector<? extends CalculusFieldElement<?>>> cached;
56
57 /** Simple constructor.
58 * <p>
59 * Note that the {@code intervalDetector} passed as an argument is used only
60 * as a <em>prototype</em> from which a new detector will be built using its
61 * {@link AbstractDetector#withHandler(EventHandler) withHandler} method to
62 * set up an internal handler. The original event handler from the prototype
63 * will be <em>ignored</em> and never called.
64 * </p>
65 * <p>
66 * If the trigger is used in a {@link org.orekit.propagation.FieldPropagator field-based propagation},
67 * the detector will be automatically converted to a field equivalent. Beware however that the
68 * {@link FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean) eventOccurred}
69 * of the converted propagator <em>will</em> call the method with the same name in the prototype
70 * detector, in order to get the correct return value.
71 * </p>
72 * @param prototypeFiringIntervalDetector prototype detector for firing interval
73 */
74 public IntervalEventTrigger(final T prototypeFiringIntervalDetector) {
75 this.firingIntervalDetector = prototypeFiringIntervalDetector.withHandler(new Handler());
76 this.cached = new HashMap<>();
77 }
78
79 /**
80 * Getter for the firing interval detector.
81 * @return firing interval detector
82 */
83 public T getFiringIntervalDetector() {
84 return firingIntervalDetector;
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 protected boolean isFiringOnInitialState(final SpacecraftState initialState, final boolean isForward) {
90
91 // set the initial value of firing
92 final double insideThrustArcG = firingIntervalDetector.g(initialState);
93 if (insideThrustArcG == 0) {
94 // bound of arc
95 // check state for the upcoming times
96 final double shift = (isForward ? 2 : -2) * firingIntervalDetector.getThreshold();
97 if (firingIntervalDetector.g(initialState.shiftedBy(shift)) > 0) {
98 // we are entering the firing interval, from start if forward, from end if backward
99 notifyResetters(initialState, isForward);
100 return true;
101 } else {
102 // we are leaving the firing interval, from end if forward, from start if backward
103 notifyResetters(initialState, !isForward);
104 return false;
105 }
106 } else {
107 return insideThrustArcG > 0;
108 }
109
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 public Stream<EventDetector> getEventDetectors() {
115 return Stream.of(firingIntervalDetector);
116 }
117
118 /** {@inheritDoc} */
119 public <S extends CalculusFieldElement<S>> Stream<FieldEventDetector<S>> getFieldEventDetectors(final Field<S> field) {
120
121 @SuppressWarnings("unchecked")
122 FieldEventDetector<S> fd = (FieldEventDetector<S>) cached.get(field);
123 if (fd == null) {
124 fd = convertAndSetUpHandler(field);
125 cached.put(field, fd);
126 }
127
128 return Stream.of(fd);
129
130 }
131
132 /** Convert a detector and set up check interval, threshold and new handler.
133 * <p>
134 * This method is not inlined in {@link #getFieldEventDetectors(Field)} because the
135 * parameterized types confuses the Java compiler.
136 * </p>
137 * @param field field to which the state belongs
138 * @param <D> type of the event detector
139 * @param <S> type of the field elements
140 * @return converted firing intervals detector
141 */
142 private <D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>> D convertAndSetUpHandler(final Field<S> field) {
143 final FieldAbstractDetector<D, S> converted = convertIntervalDetector(field, firingIntervalDetector);
144 final FieldAdaptableInterval<S> maxCheck = s -> firingIntervalDetector.getMaxCheckInterval().currentInterval(s.toSpacecraftState());
145 return converted.
146 withMaxCheck(maxCheck).
147 withThreshold(field.getZero().newInstance(firingIntervalDetector.getThreshold())).
148 withHandler(new FieldHandler<>());
149 }
150
151 /** Convert a primitive firing intervals detector into a field firing intervals detector.
152 * <p>
153 * There is not need to set up {@link FieldAbstractDetector#withMaxCheck(FieldAdaptableInterval) withMaxCheck},
154 * {@link FieldAbstractDetector#withThreshold(CalculusFieldElement) withThreshold}, or
155 * {@link FieldAbstractDetector#withHandler(org.orekit.propagation.events.handlers.FieldEventHandler) withHandler}
156 * in the converted detector, this will be done by caller.
157 * </p>
158 * <p>
159 * A skeleton implementation of this method to convert some {@code XyzDetector} into {@code FieldXyzDetector},
160 * considering these detectors are created from a date and a number parameter is:
161 * </p>
162 * <pre>{@code
163 * protected <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
164 * FieldAbstractDetector<D, S> convertIntervalDetector(final Field<S> field, final XyzDetector detector) {
165 *
166 * final FieldAbsoluteDate<S> date = new FieldAbsoluteDate<>(field, detector.getDate());
167 * final S param = field.getZero().newInstance(detector.getParam());
168 *
169 * final FieldAbstractDetector<D, S> converted = (FieldAbstractDetector<D, S>) new FieldXyzDetector<>(date, param);
170 * return converted;
171 *
172 * }
173 * }
174 * </pre>
175 * @param field field to which the state belongs
176 * @param detector primitive firing intervals detector to convert
177 * @param <D> type of the event detector
178 * @param <S> type of the field elements
179 * @return converted firing intervals detector
180 */
181 protected abstract <D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>>
182 FieldAbstractDetector<D, S> convertIntervalDetector(Field<S> field, T detector);
183
184 /** Local handler for both start and stop triggers. */
185 private class Handler implements EventHandler {
186
187 /** Propagation direction. */
188 private boolean forward;
189
190 /** {@inheritDoc} */
191 @Override
192 public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
193 forward = target.isAfterOrEqualTo(initialState);
194 initializeResetters(initialState, target);
195 }
196
197 /** {@inheritDoc} */
198 @Override
199 public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
200 if (forward) {
201 getFirings().addValidAfter(increasing, s.getDate(), false);
202 } else {
203 getFirings().addValidBefore(!increasing, s.getDate(), false);
204 }
205 notifyResetters(s, increasing);
206 return Action.RESET_STATE;
207 }
208
209 /** {@inheritDoc} */
210 @Override
211 public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
212 return applyResetters(oldState);
213 }
214
215 }
216
217 /** Local handler for both start and stop triggers.
218 * @param <S> type of the field elements
219 */
220 private class FieldHandler<D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>> implements FieldEventHandler<S> {
221
222 /** Propagation direction. */
223 private boolean forward;
224
225 /** {@inheritDoc} */
226 @Override
227 public void init(final FieldSpacecraftState<S> initialState,
228 final FieldAbsoluteDate<S> target,
229 final FieldEventDetector<S> detector) {
230 forward = target.isAfterOrEqualTo(initialState);
231 initializeResetters(initialState, target);
232 }
233
234 /** {@inheritDoc} */
235 @Override
236 public Action eventOccurred(final FieldSpacecraftState<S> s, final FieldEventDetector<S> detector, final boolean increasing) {
237 if (forward) {
238 getFirings().addValidAfter(increasing, s.getDate().toAbsoluteDate(), false);
239 } else {
240 getFirings().addValidBefore(!increasing, s.getDate().toAbsoluteDate(), false);
241 }
242 notifyResetters(s, increasing);
243 return Action.RESET_STATE;
244 }
245
246 /** {@inheritDoc} */
247 @Override
248 public FieldSpacecraftState<S> resetState(final FieldEventDetector<S> detector, final FieldSpacecraftState<S> oldState) {
249 return applyResetters(oldState);
250 }
251
252 }
253
254 }