FieldEventsLogger.java

  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.events;


  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.ode.events.Action;
  22. import org.orekit.propagation.FieldSpacecraftState;
  23. import org.orekit.propagation.events.handlers.FieldEventHandler;
  24. import org.orekit.time.FieldAbsoluteDate;

  25. /** This class logs events detectors events during propagation.
  26.  *
  27.  * <p>As {@link FieldEventDetector events detectors} are triggered during
  28.  * orbit propagation, an event specific {@link
  29.  * FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean) eventOccurred}
  30.  * method is called. This class can be used to add a global logging
  31.  * feature registering all events with their corresponding states in
  32.  * a chronological sequence (or reverse-chronological if propagation
  33.  * occurs backward).
  34.  * <p>This class works by wrapping user-provided {@link FieldEventDetector
  35.  * events detectors} before they are registered to the propagator. The
  36.  * wrapper monitor the calls to {@link
  37.  * FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean) eventOccurred}
  38.  * and store the corresponding events as {@link FieldLoggedEvent} instances.
  39.  * After propagation is complete, the user can retrieve all the events
  40.  * that have occurred at once by calling method {@link #getLoggedEvents()}.</p>
  41.  *
  42.  * @author Luc Maisonobe
  43.  * @param <T> type of the field elements
  44.  */
  45. public class FieldEventsLogger<T extends CalculusFieldElement<T>> {

  46.     /** List of occurred events. */
  47.     private final List<FieldLoggedEvent<T>> log;

  48.     /** Simple constructor.
  49.      * <p>
  50.      * Build an empty logger for events detectors.
  51.      * </p>
  52.      */
  53.     public FieldEventsLogger() {
  54.         log = new ArrayList<>();
  55.     }

  56.     /** Monitor an event detector.
  57.      * <p>
  58.      * In order to monitor an event detector, it must be wrapped thanks to
  59.      * this method as follows:
  60.      * </p>
  61.      * <pre>
  62.      * Propagator propagator = new XyzPropagator(...);
  63.      * EventsLogger logger = new EventsLogger();
  64.      * FieldEventDetector&lt;T&gt; detector = new UvwDetector(...);
  65.      * propagator.addEventDetector(logger.monitorDetector(detector));
  66.      * </pre>
  67.      * <p>
  68.      * Note that the event detector returned by the {@link
  69.      * FieldLoggedEvent#getEventDetector() getEventDetector} method in
  70.      * {@link FieldLoggedEvent FieldLoggedEvent} instances returned by {@link
  71.      * #getLoggedEvents()} are the {@code monitoredDetector} instances
  72.      * themselves, not the wrapping detector returned by this method.
  73.      * </p>
  74.      * @param monitoredDetector event detector to monitor
  75.      * @return the wrapping detector to add to the propagator
  76.      */
  77.     public FieldEventDetector<T> monitorDetector(final FieldEventDetector<T> monitoredDetector) {
  78.         return new FieldLoggingWrapper(monitoredDetector);
  79.     }

  80.     /** Clear the logged events.
  81.      */
  82.     public void clearLoggedEvents() {
  83.         log.clear();
  84.     }

  85.     /** Get an immutable copy of the logged events.
  86.      * <p>
  87.      * The copy is independent of the logger. It is preserved
  88.      * event if the {@link #clearLoggedEvents() clearLoggedEvents} method
  89.      * is called and the logger reused in another propagation.
  90.      * </p>
  91.      * @return an immutable copy of the logged events
  92.      */
  93.     public List<FieldLoggedEvent<T>> getLoggedEvents() {
  94.         return new ArrayList<>(log);
  95.     }

  96.     /** Class for logged events entries.
  97.      * @param <T> type of the field elements
  98.      */
  99.     public static class FieldLoggedEvent <T extends CalculusFieldElement<T>> {

  100.         /** Event detector triggered. */
  101.         private final FieldEventDetector<T> detector;

  102.         /** Triggering state. */
  103.         private final FieldSpacecraftState<T> state;

  104.         /** Increasing/decreasing status. */
  105.         private final boolean increasing;

  106.         /** Reset state if any, otherwise event state. */
  107.         private final FieldSpacecraftState<T> resetState;

  108.         /** Constructor.
  109.          * @param detectorN detector for event that was triggered
  110.          * @param stateN state at event trigger date
  111.          * @param resetStateN reset state if any, otherwise same as event state
  112.          * @param increasingN indicator if the event switching function was increasing
  113.          * or decreasing at event occurrence date
  114.          * @since 13.1
  115.          */
  116.         private FieldLoggedEvent(final FieldEventDetector<T> detectorN, final FieldSpacecraftState<T> stateN,
  117.                                  final FieldSpacecraftState<T> resetStateN, final boolean increasingN) {
  118.             detector = detectorN;
  119.             state      = stateN;
  120.             resetState = resetStateN;
  121.             increasing = increasingN;
  122.         }

  123.         /** Get the event detector triggered.
  124.          * @return event detector triggered
  125.          */
  126.         public FieldEventDetector<T> getEventDetector() {
  127.             return detector;
  128.         }

  129.         /** Get the triggering state.
  130.          * @return triggering state
  131.          * @see FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean)
  132.          */
  133.         public FieldSpacecraftState<T> getState() {
  134.             return state;
  135.         }

  136.         /**
  137.          * Get the reset state.
  138.          * @return reset state
  139.          * @since 13.1
  140.          */
  141.         public FieldSpacecraftState<T> getResetState() {
  142.             return resetState;
  143.         }

  144.         /** Get the Increasing/decreasing status of the event.
  145.          * @return increasing/decreasing status of the event
  146.          * @see FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean)
  147.          */
  148.         public boolean isIncreasing() {
  149.             return increasing;
  150.         }

  151.     }

  152.     /** Internal wrapper for events detectors. */
  153.     private class FieldLoggingWrapper implements FieldDetectorModifier<T> {

  154.         /** Wrapped detector. */
  155.         private final FieldEventDetector<T> wrappedDetector;

  156.         /** Simple constructor.
  157.          * @param detector events detector to wrap
  158.          */
  159.         FieldLoggingWrapper(final FieldEventDetector<T> detector) {
  160.             this.wrappedDetector = detector;
  161.         }

  162.         @Override
  163.         public FieldEventDetector<T> getDetector() {
  164.             return wrappedDetector;
  165.         }

  166.         /** Log an event.
  167.          * @param state state at event trigger date
  168.          * @param resetState reset state if any, otherwise same as event state
  169.          * @param increasing indicator if the event switching function was increasing
  170.          */
  171.         void logEvent(final FieldSpacecraftState<T> state, final FieldSpacecraftState<T> resetState,
  172.                       final boolean increasing) {
  173.             log.add(new FieldLoggedEvent<>(getDetector(), state, resetState, increasing));
  174.         }

  175.         /** {@inheritDoc} */
  176.         @Override
  177.         public FieldEventHandler<T> getHandler() {

  178.             final FieldEventHandler<T> handler = getDetector().getHandler();

  179.             return new FieldEventHandler<T>() {

  180.                 private FieldSpacecraftState<T> lastTriggeringState = null;
  181.                 private FieldSpacecraftState<T> lastResetState = null;

  182.                 @Override
  183.                 public void init(final FieldSpacecraftState<T> initialState, final FieldAbsoluteDate<T> target,
  184.                                  final FieldEventDetector<T> detector) {
  185.                     FieldEventHandler.super.init(initialState, target, detector);
  186.                     lastResetState = null;
  187.                     lastTriggeringState = null;
  188.                 }

  189.                 /** {@inheritDoc} */
  190.                 @Override
  191.                 public Action eventOccurred(final FieldSpacecraftState<T> s,
  192.                                             final FieldEventDetector<T> d,
  193.                                             final boolean increasing) {
  194.                     final Action action = handler.eventOccurred(s, getDetector(), increasing);
  195.                     if (action == Action.RESET_STATE) {
  196.                         lastResetState = resetState(getDetector(), s);
  197.                     } else {
  198.                         lastResetState = s;
  199.                     }
  200.                     lastTriggeringState = s;
  201.                     logEvent(s, lastResetState, increasing);
  202.                     return action;
  203.                 }

  204.                 /** {@inheritDoc} */
  205.                 @Override
  206.                 public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> d,
  207.                                                           final FieldSpacecraftState<T> oldState) {
  208.                     if (lastTriggeringState != oldState) {
  209.                         lastTriggeringState = oldState;
  210.                         lastResetState = handler.resetState(getDetector(), oldState);
  211.                     }
  212.                     return lastResetState;
  213.                 }

  214.             };
  215.         }

  216.     }

  217. }