EventSlopeFilter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF 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.Arrays;

  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.EventHandler;
  21. import org.orekit.time.AbsoluteDate;

  22. /** Wrapper used to detect only increasing or decreasing events.
  23.  *
  24.  * <p>This class is heavily based on the class EventFilter from the
  25.  * Hipparchus library. The changes performed consist in replacing
  26.  * raw types (double and double arrays) with space dynamics types
  27.  * ({@link AbsoluteDate}, {@link SpacecraftState}).</p>
  28.  *
  29.  * <p>General {@link EventDetector events} are defined implicitly
  30.  * by a {@link EventDetector#g(SpacecraftState) g function} crossing
  31.  * zero. This function needs to be continuous in the event neighborhood,
  32.  * and its sign must remain consistent between events. This implies that
  33.  * during an orbit propagation, events triggered are alternately events
  34.  * for which the function increases from negative to positive values,
  35.  * and events for which the function decreases from positive to
  36.  * negative values.
  37.  * </p>
  38.  *
  39.  * <p>Sometimes, users are only interested in one type of event (say
  40.  * increasing events for example) and not in the other type. In these
  41.  * cases, looking precisely for all events location and triggering
  42.  * events that will later be ignored is a waste of computing time.</p>
  43.  *
  44.  * <p>Users can wrap a regular {@link EventDetector event detector} in
  45.  * an instance of this class and provide this wrapping instance to
  46.  * a {@link org.orekit.propagation.Propagator}
  47.  * in order to avoid wasting time looking for uninteresting events.
  48.  * The wrapper will intercept the calls to the {@link
  49.  * EventDetector#g(SpacecraftState) g function} and to the {@link
  50.  * EventDetector#eventOccurred(SpacecraftState, boolean)
  51.  * eventOccurred} method in order to ignore uninteresting events. The
  52.  * wrapped regular {@link EventDetector event detector} will then see only
  53.  * the interesting events, i.e. either only {@code increasing} events or
  54.  * only {@code decreasing} events. The number of calls to the {@link
  55.  * EventDetector#g(SpacecraftState) g function} will also be reduced.</p>
  56.  * @see EventEnablingPredicateFilter
  57.  */

  58. public class EventSlopeFilter<T extends EventDetector> extends AbstractDetector<EventSlopeFilter<T>> {

  59.     /** Serializable UID. */
  60.     private static final long serialVersionUID = 20130409L;

  61.     /** Number of past transformers updates stored. */
  62.     private static final int HISTORY_SIZE = 100;

  63.     /** Wrapped event detector. */
  64.     private final T rawDetector;

  65.     /** Filter to use. */
  66.     private final FilterType filter;

  67.     /** Transformers of the g function. */
  68.     private final Transformer[] transformers;

  69.     /** Update time of the transformers. */
  70.     private final AbsoluteDate[] updates;

  71.     /** Indicator for forward integration. */
  72.     private boolean forward;

  73.     /** Extreme time encountered so far. */
  74.     private AbsoluteDate extremeT;

  75.     /** Wrap an {@link EventDetector event detector}.
  76.      * @param rawDetector event detector to wrap
  77.      * @param filter filter to use
  78.      */
  79.     public EventSlopeFilter(final T rawDetector, final FilterType filter) {
  80.         this(rawDetector.getMaxCheckInterval(), rawDetector.getThreshold(),
  81.              rawDetector.getMaxIterationCount(), new LocalHandler<T>(),
  82.              rawDetector, filter);
  83.     }

  84.     /** Private constructor with full parameters.
  85.      * <p>
  86.      * This constructor is private as users are expected to use the builder
  87.      * API with the various {@code withXxx()} methods to set up the instance
  88.      * in a readable manner without using a huge amount of parameters.
  89.      * </p>
  90.      * @param maxCheck maximum checking interval (s)
  91.      * @param threshold convergence threshold (s)
  92.      * @param maxIter maximum number of iterations in the event time search
  93.      * @param handler event handler to call at event occurrences
  94.      * @param rawDetector event detector to wrap
  95.      * @param filter filter to use
  96.      * @since 6.1
  97.      */
  98.     private EventSlopeFilter(final double maxCheck, final double threshold,
  99.                              final int maxIter, final EventHandler<? super EventSlopeFilter<T>> handler,
  100.                              final T rawDetector, final FilterType filter) {
  101.         super(maxCheck, threshold, maxIter, handler);
  102.         this.rawDetector  = rawDetector;
  103.         this.filter       = filter;
  104.         this.transformers = new Transformer[HISTORY_SIZE];
  105.         this.updates      = new AbsoluteDate[HISTORY_SIZE];
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     protected EventSlopeFilter<T> create(final double newMaxCheck, final double newThreshold,
  110.                                          final int newMaxIter, final EventHandler<? super EventSlopeFilter<T>> newHandler) {
  111.         return new EventSlopeFilter<T>(newMaxCheck, newThreshold, newMaxIter, newHandler, rawDetector, filter);
  112.     }

  113.     /**  {@inheritDoc} */
  114.     public void init(final SpacecraftState s0,
  115.                      final AbsoluteDate t) {
  116.         super.init(s0, t);

  117.         // delegate to raw detector
  118.         rawDetector.init(s0, t);

  119.         // initialize events triggering logic
  120.         forward  = t.compareTo(s0.getDate()) >= 0;
  121.         extremeT = forward ? AbsoluteDate.PAST_INFINITY : AbsoluteDate.FUTURE_INFINITY;
  122.         Arrays.fill(transformers, Transformer.UNINITIALIZED);
  123.         Arrays.fill(updates, extremeT);

  124.     }

  125.     /**  {@inheritDoc} */
  126.     public double g(final SpacecraftState s) {

  127.         final double rawG = rawDetector.g(s);

  128.         // search which transformer should be applied to g
  129.         if (forward) {
  130.             final int last = transformers.length - 1;
  131.             if (extremeT.compareTo(s.getDate()) < 0) {
  132.                 // we are at the forward end of the history

  133.                 // check if a new rough root has been crossed
  134.                 final Transformer previous = transformers[last];
  135.                 final Transformer next     = filter.selectTransformer(previous, rawG, forward);
  136.                 if (next != previous) {
  137.                     // there is a root somewhere between extremeT and t.
  138.                     // the new transformer is valid for t (this is how we have just computed
  139.                     // it above), but it is in fact valid on both sides of the root, so
  140.                     // it was already valid before t and even up to previous time. We store
  141.                     // the switch at extremeT for safety, to ensure the previous transformer
  142.                     // is not applied too close of the root
  143.                     System.arraycopy(updates,      1, updates,      0, last);
  144.                     System.arraycopy(transformers, 1, transformers, 0, last);
  145.                     updates[last]      = extremeT;
  146.                     transformers[last] = next;
  147.                 }

  148.                 extremeT = s.getDate();

  149.                 // apply the transform
  150.                 return next.transformed(rawG);

  151.             } else {
  152.                 // we are in the middle of the history

  153.                 // select the transformer
  154.                 for (int i = last; i > 0; --i) {
  155.                     if (updates[i].compareTo(s.getDate()) <= 0) {
  156.                         // apply the transform
  157.                         return transformers[i].transformed(rawG);
  158.                     }
  159.                 }

  160.                 return transformers[0].transformed(rawG);

  161.             }
  162.         } else {
  163.             if (s.getDate().compareTo(extremeT) < 0) {
  164.                 // we are at the backward end of the history

  165.                 // check if a new rough root has been crossed
  166.                 final Transformer previous = transformers[0];
  167.                 final Transformer next     = filter.selectTransformer(previous, rawG, forward);
  168.                 if (next != previous) {
  169.                     // there is a root somewhere between extremeT and t.
  170.                     // the new transformer is valid for t (this is how we have just computed
  171.                     // it above), but it is in fact valid on both sides of the root, so
  172.                     // it was already valid before t and even up to previous time. We store
  173.                     // the switch at extremeT for safety, to ensure the previous transformer
  174.                     // is not applied too close of the root
  175.                     System.arraycopy(updates,      0, updates,      1, updates.length - 1);
  176.                     System.arraycopy(transformers, 0, transformers, 1, transformers.length - 1);
  177.                     updates[0]      = extremeT;
  178.                     transformers[0] = next;
  179.                 }

  180.                 extremeT = s.getDate();

  181.                 // apply the transform
  182.                 return next.transformed(rawG);

  183.             } else {
  184.                 // we are in the middle of the history

  185.                 // select the transformer
  186.                 for (int i = 0; i < updates.length - 1; ++i) {
  187.                     if (s.getDate().compareTo(updates[i]) <= 0) {
  188.                         // apply the transform
  189.                         return transformers[i].transformed(rawG);
  190.                     }
  191.                 }

  192.                 return transformers[updates.length - 1].transformed(rawG);

  193.             }
  194.         }

  195.     }

  196.     /** Local handler. */
  197.     private static class LocalHandler<T extends EventDetector> implements EventHandler<EventSlopeFilter<T>> {

  198.         /** {@inheritDoc} */
  199.         public Action eventOccurred(final SpacecraftState s, final EventSlopeFilter<T> ef, final boolean increasing) {
  200.             return ef.rawDetector.eventOccurred(s, ef.filter.getTriggeredIncreasing());
  201.         }

  202.         /** {@inheritDoc} */
  203.         @Override
  204.         public SpacecraftState resetState(final EventSlopeFilter<T> ef, final SpacecraftState oldState) {
  205.             return ef.rawDetector.resetState(oldState);
  206.         }

  207.     }

  208. }