FieldEventSlopeFilter.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.lang.reflect.Array;
  19. import java.util.Arrays;

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

  63. public class FieldEventSlopeFilter<D extends FieldEventDetector<T>, T extends CalculusFieldElement<T>>
  64.     extends FieldAbstractDetector<FieldEventSlopeFilter<D, T>, T> {

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

  67.     /** Wrapped event detector. */
  68.     private final D rawDetector;

  69.     /** Filter to use. */
  70.     private final FilterType filter;

  71.     /** Transformers of the g function. */
  72.     private final Transformer[] transformers;

  73.     /** Update time of the transformers. */
  74.     private final FieldAbsoluteDate<T>[] updates;

  75.     /** Indicator for forward integration. */
  76.     private boolean forward;

  77.     /** Extreme time encountered so far. */
  78.     private FieldAbsoluteDate<T> extremeT;

  79.     /** Wrap an {@link EventDetector event detector}.
  80.      * @param rawDetector event detector to wrap
  81.      * @param filter filter to use
  82.      */
  83.     public FieldEventSlopeFilter(final D rawDetector, final FilterType filter) {
  84.         this(rawDetector.getMaxCheckInterval(), rawDetector.getThreshold(),
  85.              rawDetector.getMaxIterationCount(), new LocalHandler<>(),
  86.              rawDetector, filter);
  87.     }

  88.     /** Protected constructor with full parameters.
  89.      * <p>
  90.      * This constructor is not public as users are expected to use the builder
  91.      * API with the various {@code withXxx()} methods to set up the instance
  92.      * in a readable manner without using a huge amount of parameters.
  93.      * </p>
  94.      * @param maxCheck maximum checking interval
  95.      * @param threshold convergence threshold (s)
  96.      * @param maxIter maximum number of iterations in the event time search
  97.      * @param handler event handler to call at event occurrences
  98.      * @param rawDetector event detector to wrap
  99.      * @param filter filter to use
  100.      */
  101.     @SuppressWarnings("unchecked")
  102.     protected FieldEventSlopeFilter(final FieldAdaptableInterval<T> maxCheck, final T threshold,
  103.                                     final int maxIter, final FieldEventHandler<T> handler,
  104.                                     final D rawDetector, final FilterType filter) {
  105.         super(new FieldEventDetectionSettings<>(maxCheck, threshold, maxIter), handler);
  106.         this.rawDetector  = rawDetector;
  107.         this.filter       = filter;
  108.         this.transformers = new Transformer[HISTORY_SIZE];
  109.         this.updates      = (FieldAbsoluteDate<T>[]) Array.newInstance(FieldAbsoluteDate.class, HISTORY_SIZE);
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     protected FieldEventSlopeFilter<D, T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
  114.                                                  final int newMaxIter, final FieldEventHandler<T> newHandler) {
  115.         return new FieldEventSlopeFilter<>(newMaxCheck, newThreshold, newMaxIter, newHandler, rawDetector, filter);
  116.     }

  117.     /**
  118.      * Get the wrapped raw detector.
  119.      * @return the wrapped raw detector
  120.      */
  121.     public D getDetector() {
  122.         return rawDetector;
  123.     }

  124.     /**  {@inheritDoc} */
  125.     @Override
  126.     public void init(final FieldSpacecraftState<T> s0,
  127.                      final FieldAbsoluteDate<T> t) {
  128.         super.init(s0, t);

  129.         // delegate to raw detector
  130.         rawDetector.init(s0, t);

  131.         // initialize events triggering logic
  132.         forward  = t.compareTo(s0.getDate()) >= 0;
  133.         extremeT = forward ?
  134.                    FieldAbsoluteDate.getPastInfinity(t.getField()) :
  135.                    FieldAbsoluteDate.getFutureInfinity(t.getField());
  136.         Arrays.fill(transformers, Transformer.UNINITIALIZED);
  137.         Arrays.fill(updates, extremeT);

  138.     }

  139.     /**  {@inheritDoc} */
  140.     public T g(final FieldSpacecraftState<T> s) {

  141.         final T rawG = rawDetector.g(s);

  142.         // search which transformer should be applied to g
  143.         if (forward) {
  144.             final int last = transformers.length - 1;
  145.             if (extremeT.compareTo(s.getDate()) < 0) {
  146.                 // we are at the forward end of the history

  147.                 // check if a new rough root has been crossed
  148.                 final Transformer previous = transformers[last];
  149.                 final Transformer next     = filter.selectTransformer(previous, rawG.getReal(), forward);
  150.                 if (next != previous) {
  151.                     // there is a root somewhere between extremeT and t.
  152.                     // the new transformer is valid for t (this is how we have just computed
  153.                     // it above), but it is in fact valid on both sides of the root, so
  154.                     // it was already valid before t and even up to previous time. We store
  155.                     // the switch at extremeT for safety, to ensure the previous transformer
  156.                     // is not applied too close of the root
  157.                     System.arraycopy(updates,      1, updates,      0, last);
  158.                     System.arraycopy(transformers, 1, transformers, 0, last);
  159.                     updates[last]      = extremeT;
  160.                     transformers[last] = next;
  161.                 }

  162.                 extremeT = s.getDate();

  163.                 // apply the transform
  164.                 return next.transformed(rawG);

  165.             } else {
  166.                 // we are in the middle of the history

  167.                 // select the transformer
  168.                 for (int i = last; i > 0; --i) {
  169.                     if (updates[i].compareTo(s.getDate()) <= 0) {
  170.                         // apply the transform
  171.                         return transformers[i].transformed(rawG);
  172.                     }
  173.                 }

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

  175.             }
  176.         } else {
  177.             if (s.getDate().compareTo(extremeT) < 0) {
  178.                 // we are at the backward end of the history

  179.                 // check if a new rough root has been crossed
  180.                 final Transformer previous = transformers[0];
  181.                 final Transformer next     = filter.selectTransformer(previous, rawG.getReal(), forward);
  182.                 if (next != previous) {
  183.                     // there is a root somewhere between extremeT and t.
  184.                     // the new transformer is valid for t (this is how we have just computed
  185.                     // it above), but it is in fact valid on both sides of the root, so
  186.                     // it was already valid before t and even up to previous time. We store
  187.                     // the switch at extremeT for safety, to ensure the previous transformer
  188.                     // is not applied too close of the root
  189.                     System.arraycopy(updates,      0, updates,      1, updates.length - 1);
  190.                     System.arraycopy(transformers, 0, transformers, 1, transformers.length - 1);
  191.                     updates[0]      = extremeT;
  192.                     transformers[0] = next;
  193.                 }

  194.                 extremeT = s.getDate();

  195.                 // apply the transform
  196.                 return next.transformed(rawG);

  197.             } else {
  198.                 // we are in the middle of the history

  199.                 // select the transformer
  200.                 for (int i = 0; i < updates.length - 1; ++i) {
  201.                     if (s.getDate().compareTo(updates[i]) <= 0) {
  202.                         // apply the transform
  203.                         return transformers[i].transformed(rawG);
  204.                     }
  205.                 }

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

  207.             }
  208.         }

  209.     }

  210.     /** Local handler. */
  211.     private static class LocalHandler<D extends FieldEventDetector<T>, T extends CalculusFieldElement<T>> implements FieldEventHandler<T> {

  212.         /** {@inheritDoc} */
  213.         @SuppressWarnings("unchecked")
  214.         public Action eventOccurred(final FieldSpacecraftState<T> s, final FieldEventDetector<T> detector, final boolean increasing) {
  215.             final FieldEventSlopeFilter<D, T> esf = (FieldEventSlopeFilter<D, T>) detector;
  216.             return esf.rawDetector.getHandler().eventOccurred(s, esf.rawDetector, esf.filter.getTriggeredIncreasing());
  217.         }

  218.         /** {@inheritDoc} */
  219.         @Override
  220.         @SuppressWarnings("unchecked")
  221.         public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> detector, final FieldSpacecraftState<T> oldState) {
  222.             final FieldEventSlopeFilter<D, T> esf = (FieldEventSlopeFilter<D, T>) detector;
  223.             return esf.rawDetector.getHandler().resetState(esf.rawDetector, oldState);
  224.         }

  225.     }

  226. }