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.CalculusFieldElement;
  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.  * </p>
  33.  *
  34.  * @author Evan Ward
  35.  * @see RecordAndContinue
  36.  * @since 9.3
  37.  * @param <T> type of the field element
  38.  */
  39. public class FieldRecordAndContinue <T extends CalculusFieldElement<T>> implements FieldEventHandler<T> {

  40.     /** A single event detected during propagation.
  41.      * @param <T> type of the field element
  42.      */
  43.     public static class Event<T extends CalculusFieldElement<T>> {

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

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

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

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

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

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

  101.     /** Observed events. */
  102.     private final List<Event<T>> events;

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

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

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

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

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

  143. }