FieldEventsLogger.java

  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.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<FieldEventsLogger.FieldLoggedEvent<T>>();
  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 FieldAbstractDetector<FieldLoggingWrapper, 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<FieldEventsLogger.FieldLoggedEvent<T>>(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.         /** Simple constructor.
  107.          * @param detectorN detector for event that was triggered
  108.          * @param stateN state at event trigger date
  109.          * @param increasingN indicator if the event switching function was increasing
  110.          * or decreasing at event occurrence date
  111.          */
  112.         private FieldLoggedEvent(final FieldEventDetector<T> detectorN, final FieldSpacecraftState<T> stateN, 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 extends FieldAbstractDetector<FieldLoggingWrapper, T> {

  140.         /** Wrapped events 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.getMaxCheckInterval(), detector.getThreshold(),
  147.                  detector.getMaxIterationCount(), null,
  148.                  detector);
  149.         }

  150.         /** Private constructor with full parameters.
  151.          * <p>
  152.          * This constructor is private as users are expected to use the builder
  153.          * API with the various {@code withXxx()} methods to set up the instance
  154.          * in a readable manner without using a huge amount of parameters.
  155.          * </p>
  156.          * @param maxCheck maximum checking interval
  157.          * @param threshold convergence threshold (s)
  158.          * @param maxIter maximum number of iterations in the event time search
  159.          * @param handler event handler to call at event occurrences
  160.          * @param detector events detector to wrap
  161.          * @since 6.1
  162.          */
  163.         private FieldLoggingWrapper(final FieldAdaptableInterval<T> maxCheck, final T threshold,
  164.                                     final int maxIter, final FieldEventHandler<T> handler,
  165.                                     final FieldEventDetector<T> detector) {
  166.             super(maxCheck, threshold, maxIter, handler);
  167.             this.detector = detector;
  168.         }

  169.         /** {@inheritDoc} */
  170.         @Override
  171.         protected FieldLoggingWrapper create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
  172.                                              final int newMaxIter, final FieldEventHandler<T> newHandler) {
  173.             return new FieldLoggingWrapper(newMaxCheck, newThreshold, newMaxIter, newHandler, detector);
  174.         }

  175.         /** Log an event.
  176.          * @param state state at event trigger date
  177.          * @param increasing indicator if the event switching function was increasing
  178.          */
  179.         public void logEvent(final FieldSpacecraftState<T> state, final boolean increasing) {
  180.             log.add(new FieldLoggedEvent<>(detector, state, increasing));
  181.         }

  182.         /** {@inheritDoc} */
  183.         public void init(final FieldSpacecraftState<T> s0,
  184.                          final FieldAbsoluteDate<T> t) {
  185.             super.init(s0, t);
  186.             detector.init(s0, t);
  187.         }

  188.         /** {@inheritDoc} */
  189.         public T g(final FieldSpacecraftState<T> s) {
  190.             return detector.g(s);
  191.         }

  192.         /** {@inheritDoc} */
  193.         public FieldEventHandler<T> getHandler() {

  194.             final FieldEventHandler<T> handler = detector.getHandler();

  195.             return new FieldEventHandler<T>() {

  196.                 /** {@inheritDoc} */
  197.                 public Action eventOccurred(final FieldSpacecraftState<T> s,
  198.                                             final FieldEventDetector<T> d,
  199.                                             final boolean increasing) {
  200.                     logEvent(s, increasing);
  201.                     return handler.eventOccurred(s, detector, increasing);
  202.                 }

  203.                 /** {@inheritDoc} */
  204.                 @Override
  205.                 public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> d,
  206.                                                           final FieldSpacecraftState<T> oldState) {
  207.                     return handler.resetState(detector, oldState);
  208.                 }

  209.             };
  210.         }

  211.     }

  212. }