1 /* Copyright 2002-2021 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.events;
18
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.hipparchus.CalculusFieldElement;
24 import org.hipparchus.ode.events.Action;
25 import org.orekit.propagation.FieldSpacecraftState;
26 import org.orekit.propagation.events.handlers.FieldEventHandler;
27 import org.orekit.time.FieldAbsoluteDate;
28
29 /** This class logs events detectors events during propagation.
30 *
31 * <p>As {@link FieldEventDetector events detectors} are triggered during
32 * orbit propagation, an event specific {@link
33 * FieldEventDetector#eventOccurred(FieldSpacecraftState, boolean) eventOccurred}
34 * method is called. This class can be used to add a global logging
35 * feature registering all events with their corresponding states in
36 * a chronological sequence (or reverse-chronological if propagation
37 * occurs backward).
38 * <p>This class works by wrapping user-provided {@link FieldEventDetector
39 * events detectors} before they are registered to the propagator. The
40 * wrapper monitor the calls to {@link
41 * FieldEventDetector#eventOccurred(FieldSpacecraftState, boolean) eventOccurred}
42 * and store the corresponding events as {@link FieldLoggedEvent} instances.
43 * After propagation is complete, the user can retrieve all the events
44 * that have occurred at once by calling method {@link #getLoggedEvents()}.</p>
45 *
46 * @author Luc Maisonobe
47 */
48 public class FieldEventsLogger<T extends CalculusFieldElement<T>> {
49
50
51
52 /** List of occurred events. */
53 private final List<FieldLoggedEvent<T>> log;
54
55 /** Simple constructor.
56 * <p>
57 * Build an empty logger for events detectors.
58 * </p>
59 */
60 public FieldEventsLogger() {
61 log = new ArrayList<FieldEventsLogger.FieldLoggedEvent<T>>();
62 }
63
64 /** Monitor an event detector.
65 * <p>
66 * In order to monitor an event detector, it must be wrapped thanks to
67 * this method as follows:
68 * </p>
69 * <pre>
70 * Propagator propagator = new XyzPropagator(...);
71 * EventsLogger logger = new EventsLogger();
72 * FieldEventDetector<T> detector = new UvwDetector(...);
73 * propagator.addEventDetector(logger.monitorDetector(detector));
74 * </pre>
75 * <p>
76 * Note that the event detector returned by the {@link
77 * FieldLoggedEvent#getEventDetector() getEventDetector} method in
78 * {@link FieldLoggedEvent FieldLoggedEvent} instances returned by {@link
79 * #getLoggedEvents()} are the {@code monitoredDetector} instances
80 * themselves, not the wrapping detector returned by this method.
81 * </p>
82 * @param monitoredDetector event detector to monitor
83 * @return the wrapping detector to add to the propagator
84 * @param <D> class type for the generic version
85 */
86 public <D extends FieldEventDetector<T>> FieldEventDetector<T> monitorDetector(final D monitoredDetector) {
87 return new FieldLoggingWrapper<>(monitoredDetector);
88 }
89
90 /** Clear the logged events.
91 */
92 public void clearLoggedEvents() {
93 log.clear();
94 }
95
96 /** Get an immutable copy of the logged events.
97 * <p>
98 * The copy is independent of the logger. It is preserved
99 * event if the {@link #clearLoggedEvents() clearLoggedEvents} method
100 * is called and the logger reused in another propagation.
101 * </p>
102 * @return an immutable copy of the logged events
103 */
104 public List<FieldLoggedEvent<T>> getLoggedEvents() {
105 return new ArrayList<FieldEventsLogger.FieldLoggedEvent<T>>(log);
106 }
107
108 /** Class for logged events entries. */
109 public static class FieldLoggedEvent <T extends CalculusFieldElement<T>> {
110
111 /** Event detector triggered. */
112 private final FieldEventDetector<T> detector;
113
114 /** Triggering state. */
115 private final FieldSpacecraftState<T> state;
116
117 /** Increasing/decreasing status. */
118 private final boolean increasing;
119
120 /** Simple constructor.
121 * @param detectorN detector for event that was triggered
122 * @param stateN state at event trigger date
123 * @param increasingN indicator if the event switching function was increasing
124 * or decreasing at event occurrence date
125 */
126 private FieldLoggedEvent(final FieldEventDetector<T> detectorN, final FieldSpacecraftState<T> stateN, final boolean increasingN) {
127 detector = detectorN;
128 state = stateN;
129 increasing = increasingN;
130 }
131
132 /** Get the event detector triggered.
133 * @return event detector triggered
134 */
135 public FieldEventDetector<T> getEventDetector() {
136 return detector;
137 }
138
139 /** Get the triggering state.
140 * @return triggering state
141 * @see FieldEventDetector#eventOccurred(FieldSpacecraftState, boolean)
142 */
143 public FieldSpacecraftState<T> getState() {
144 return state;
145 }
146
147 /** Get the Increasing/decreasing status of the event.
148 * @return increasing/decreasing status of the event
149 * @see FieldEventDetector#eventOccurred(FieldSpacecraftState, boolean)
150 */
151 public boolean isIncreasing() {
152 return increasing;
153 }
154
155 }
156
157 /** Internal wrapper for events detectors.
158 * @param <D> class type for the generic version
159 */
160 private class FieldLoggingWrapper<D extends FieldEventDetector<T>> extends FieldAbstractDetector<FieldLoggingWrapper<D>, T> {
161
162 /** Wrapped events detector. */
163 private final D detector;
164
165 /** Simple constructor.
166 * @param detector events detector to wrap
167 */
168 FieldLoggingWrapper(final D detector) {
169 this(detector.getMaxCheckInterval(), detector.getThreshold(),
170 detector.getMaxIterationCount(), new FieldLocalHandler<>(),
171 detector);
172 }
173
174 /** Private constructor with full parameters.
175 * <p>
176 * This constructor is private as users are expected to use the builder
177 * API with the various {@code withXxx()} methods to set up the instance
178 * in a readable manner without using a huge amount of parameters.
179 * </p>
180 * @param maxCheck maximum checking interval (s)
181 * @param threshold convergence threshold (s)
182 * @param maxIter maximum number of iterations in the event time search
183 * @param handler event handler to call at event occurrences
184 * @param detector events detector to wrap
185 * @since 6.1
186 */
187 private FieldLoggingWrapper(final T maxCheck, final T threshold,
188 final int maxIter, final FieldEventHandler<? super FieldLoggingWrapper<D>, T> handler,
189 final D detector) {
190 super(maxCheck, threshold, maxIter, handler);
191 this.detector = detector;
192 }
193
194 /** {@inheritDoc} */
195 @Override
196 protected FieldLoggingWrapper<D> create(final T newMaxCheck, final T newThreshold,
197 final int newMaxIter, final FieldEventHandler<? super FieldLoggingWrapper<D>, T> newHandler) {
198 return new FieldLoggingWrapper<>(newMaxCheck, newThreshold, newMaxIter, newHandler, detector);
199 }
200
201 /** Log an event.
202 * @param state state at event trigger date
203 * @param increasing indicator if the event switching function was increasing
204 */
205 public void logEvent(final FieldSpacecraftState<T> state, final boolean increasing) {
206 log.add(new FieldLoggedEvent<>(detector, state, increasing));
207 }
208
209 /** {@inheritDoc} */
210 public void init(final FieldSpacecraftState<T> s0,
211 final FieldAbsoluteDate<T> t) {
212 super.init(s0, t);
213 detector.init(s0, t);
214 }
215
216 /** {@inheritDoc} */
217 public T g(final FieldSpacecraftState<T> s) {
218 return detector.g(s);
219 }
220
221 }
222
223 /** Local class for handling events.
224 * @param <D> class type for the generic version
225 */
226 private class FieldLocalHandler<D extends FieldEventDetector<T>> implements FieldEventHandler<FieldLoggingWrapper<D>, T> {
227
228 /** {@inheritDoc} */
229 public Action eventOccurred(final FieldSpacecraftState<T> s, final FieldLoggingWrapper<D> wrapper, final boolean increasing) {
230 wrapper.logEvent(s, increasing);
231 return wrapper.detector.eventOccurred(s, increasing);
232 }
233
234 /** {@inheritDoc} */
235 @Override
236 public FieldSpacecraftState<T> resetState(final FieldLoggingWrapper<D> wrapper, final FieldSpacecraftState<T> oldState) {
237 return wrapper.detector.resetState(oldState);
238 }
239
240 }
241
242 }