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

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

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

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

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

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

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

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

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

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

  105.         /** Simple constructor.
  106.          * @param detectorN detector for event that was triggered
  107.          * @param stateN state at event trigger date
  108.          * @param increasingN indicator if the event switching function was increasing
  109.          * or decreasing at event occurrence date
  110.          */
  111.         private FieldLoggedEvent(final FieldEventDetector<T> detectorN, final FieldSpacecraftState<T> stateN,
  112.                                  final boolean increasingN) {
  113.             detector   = detectorN;
  114.             state      = stateN;
  115.             increasing = increasingN;
  116.         }

  117.         /** Get the event detector triggered.
  118.          * @return event detector triggered
  119.          */
  120.         public FieldEventDetector<T> getEventDetector() {
  121.             return detector;
  122.         }

  123.         /** Get the triggering state.
  124.          * @return triggering state
  125.          * @see FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean)
  126.          */
  127.         public FieldSpacecraftState<T> getState() {
  128.             return state;
  129.         }

  130.         /** Get the Increasing/decreasing status of the event.
  131.          * @return increasing/decreasing status of the event
  132.          * @see FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean)
  133.          */
  134.         public boolean isIncreasing() {
  135.             return increasing;
  136.         }

  137.     }

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

  140.         /** Wrapped detector. */
  141.         private final FieldEventDetector<T> detector;

  142.         /** Simple constructor.
  143.          * @param detector events detector to wrap
  144.          */
  145.         FieldLoggingWrapper(final FieldEventDetector<T> detector) {
  146.             this.detector = detector;
  147.         }

  148.         @Override
  149.         public FieldEventDetector<T> getDetector() {
  150.             return detector;
  151.         }

  152.         /** Log an event.
  153.          * @param state state at event trigger date
  154.          * @param increasing indicator if the event switching function was increasing
  155.          */
  156.         public void logEvent(final FieldSpacecraftState<T> state, final boolean increasing) {
  157.             log.add(new FieldLoggedEvent<>(getDetector(), state, increasing));
  158.         }

  159.         /** {@inheritDoc} */
  160.         @Override
  161.         public FieldEventHandler<T> getHandler() {

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

  163.             return new FieldEventHandler<T>() {

  164.                 /** {@inheritDoc} */
  165.                 public Action eventOccurred(final FieldSpacecraftState<T> s,
  166.                                             final FieldEventDetector<T> d,
  167.                                             final boolean increasing) {
  168.                     logEvent(s, increasing);
  169.                     return handler.eventOccurred(s, getDetector(), increasing);
  170.                 }

  171.                 /** {@inheritDoc} */
  172.                 @Override
  173.                 public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> d,
  174.                                                           final FieldSpacecraftState<T> oldState) {
  175.                     return handler.resetState(getDetector(), oldState);
  176.                 }

  177.             };
  178.         }

  179.     }

  180. }