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.errors.OrekitException;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.time.AbsoluteDate;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  125.     }

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

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

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

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

  149.                 extremeT = s.getDate();

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

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

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

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

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

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

  181.                 extremeT = s.getDate();

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

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

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

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

  194.             }
  195.         }

  196.     }

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

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

  204.         /** {@inheritDoc} */
  205.         @Override
  206.         public SpacecraftState resetState(final EventSlopeFilter<T> ef, final SpacecraftState oldState)
  207.             throws OrekitException {
  208.             return ef.rawDetector.resetState(oldState);
  209.         }

  210.     }

  211. }