FieldRecordAndContinue.java

  1. /* Contributed in the public domain.
  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.handlers;

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

  21. import org.hipparchus.RealFieldElement;
  22. import org.hipparchus.ode.events.Action;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.propagation.events.EventDetector;
  25. import org.orekit.propagation.events.FieldEventDetector;

  26. /**
  27.  * Handler that will record every time an event occurs and always return {@link
  28.  * Action#CONTINUE}.
  29.  *
  30.  * <p> As this handler stores all observed events it may consume large amounts
  31.  * of memory depending on the duration of propagation and the frequency of events.
  32.  *
  33.  * @param <T> the type of {@link EventDetector} that this event handler will handle events
  34.  *            for.
  35.  * @param <E> the type of {@link RealFieldElement} to use instead of {@code double}.
  36.  * @author Evan Ward
  37.  * @see RecordAndContinue
  38.  * @since 9.3
  39.  */
  40. public class FieldRecordAndContinue
  41.         <T extends FieldEventDetector<E>, E extends RealFieldElement<E>>
  42.         implements FieldEventHandler<T, E> {

  43.     /** A single event detected during propagation. */
  44.     public static class Event<T, F extends RealFieldElement<F>> {

  45.         /** The observed state. */
  46.         private final FieldSpacecraftState<F> state;
  47.         /** The detector. */
  48.         private final T detector;
  49.         /** The sign of the derivative of the g function. */
  50.         private final boolean increasing;

  51.         /**
  52.          * Create a new event.
  53.          *
  54.          * @param detector   of the event.
  55.          * @param state      of the event.
  56.          * @param increasing if the g function is increasing.
  57.          */
  58.         private Event(final T detector,
  59.                       final FieldSpacecraftState<F> state,
  60.                       final boolean increasing) {
  61.             this.detector = detector;
  62.             this.state = state;
  63.             this.increasing = increasing;
  64.         }

  65.         /**
  66.          * Get the detector.
  67.          *
  68.          * @return the detector that found the event.
  69.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  70.          */
  71.         public T getDetector() {
  72.             return detector;
  73.         }

  74.         /**
  75.          * Is the g() function increasing?
  76.          *
  77.          * @return if the sign of the derivative of the g function is positive (true) or
  78.          * negative (false).
  79.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  80.          */
  81.         public boolean isIncreasing() {
  82.             return increasing;
  83.         }

  84.         /**
  85.          * Get the spacecraft's state at the event.
  86.          *
  87.          * @return the satellite's state when the event was triggered.
  88.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  89.          */
  90.         public FieldSpacecraftState<F> getState() {
  91.             return state;
  92.         }

  93.         @Override
  94.         public String toString() {
  95.             return "Event{" +
  96.                     "state=" + state +
  97.                     ", increasing=" + increasing +
  98.                     ", detector=" + detector +
  99.                     '}';
  100.         }
  101.     }

  102.     /** Observed events. */
  103.     private final List<Event<T, E>> events;

  104.     /** Create a new handler using an {@link ArrayList} to store events. */
  105.     public FieldRecordAndContinue() {
  106.         this(new ArrayList<>());
  107.     }

  108.     /**
  109.      * Create a handler using the given collection to store events.
  110.      *
  111.      * @param events collection.
  112.      */
  113.     public FieldRecordAndContinue(final List<Event<T, E>> events) {
  114.         this.events = events;
  115.     }

  116.     /**
  117.      * Get the events passed to this handler.
  118.      *
  119.      * <p> Note the returned list of events is in the order the events were
  120.      * passed to this handler by calling {@link #eventOccurred(FieldSpacecraftState,
  121.      * FieldEventDetector, boolean)}. This may or may not be chronological order.
  122.      *
  123.      * <p> Also not that this method returns a view of the internal collection
  124.      * used to store events and calling any of this handler's methods may modify both the
  125.      * underlying collection and the returned view. If a snapshot of the events up to a
  126.      * certain point is needed create a copy of the returned collection.
  127.      *
  128.      * @return the events observed by the handler in the order they were observed.
  129.      */
  130.     public List<Event<T, E>> getEvents() {
  131.         return Collections.unmodifiableList(this.events);
  132.     }

  133.     /** Clear all stored events. */
  134.     public void clear() {
  135.         this.events.clear();
  136.     }

  137.     @Override
  138.     public Action eventOccurred(final FieldSpacecraftState<E> s,
  139.                                 final T detector,
  140.                                 final boolean increasing) {
  141.         events.add(new Event<>(detector, s, increasing));
  142.         return Action.CONTINUE;
  143.     }

  144.     @Override
  145.     public FieldSpacecraftState<E> resetState(final T detector,
  146.                                               final FieldSpacecraftState<E> oldState) {
  147.         return null;
  148.     }

  149. }