EventShifter.java

  1. /* Copyright 2002-2020 CS GROUP
  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;

  18. import org.hipparchus.ode.events.Action;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.time.AbsoluteDate;

  23. /** Wrapper shifting events occurrences times.
  24.  * <p>This class wraps an {@link EventDetector event detector} to slightly
  25.  * shift the events occurrences times. A typical use case is for handling
  26.  * operational delays before or after some physical event really occurs.</p>
  27.  * <p>For example, the satellite attitude mode may be switched from sun pointed
  28.  * to spin-stabilized a few minutes before eclipse entry, and switched back
  29.  * to sun pointed a few minutes after eclipse exit. This behavior is handled
  30.  * by wrapping an {@link EclipseDetector eclipse detector} into an instance
  31.  * of this class with a positive times shift for increasing events (eclipse exit)
  32.  * and a negative times shift for decreasing events (eclipse entry).</p>
  33.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  34.  * @see EventDetector
  35.  * @param <T> class type for the generic version
  36.  * @author Luc Maisonobe
  37.  */
  38. public class EventShifter<T extends EventDetector> extends AbstractDetector<EventShifter<T>> {

  39.     /** Event detector for the raw unshifted event. */
  40.     private final T detector;

  41.     /** Indicator for using shifted or unshifted states at event occurrence. */
  42.     private final boolean useShiftedStates;

  43.     /** Offset to apply to find increasing events. */
  44.     private final double increasingOffset;

  45.     /** Offset to apply to find decreasing events. */
  46.     private final double decreasingOffset;

  47.     /** Build a new instance.
  48.      * <p>The {@link #getMaxCheckInterval() max check interval}, the
  49.      * {@link #getThreshold() convergence threshold} of the raw unshifted
  50.      * events will be used for the shifted event. When an event occurs,
  51.      * the {@link #eventOccurred(SpacecraftState, boolean) eventOccurred}
  52.      * method of the raw unshifted events will be called (with spacecraft
  53.      * state at either the shifted or the unshifted event date depending
  54.      * on the <code>useShiftedStates</code> parameter).</p>
  55.      * @param detector event detector for the raw unshifted event
  56.      * @param useShiftedStates if true, the state provided to {@link
  57.      * #eventOccurred(SpacecraftState, boolean) eventOccurred} method of
  58.      * the <code>detector</code> will remain shifted, otherwise it will
  59.      * be <i>unshifted</i> to correspond to the underlying raw event.
  60.      * @param increasingTimeShift increasing events time shift.
  61.      * @param decreasingTimeShift decreasing events time shift.
  62.      */
  63.     public EventShifter(final T detector, final boolean useShiftedStates,
  64.                         final double increasingTimeShift, final double decreasingTimeShift) {
  65.         this(detector.getMaxCheckInterval(), detector.getThreshold(),
  66.              detector.getMaxIterationCount(), new LocalHandler<T>(),
  67.              detector, useShiftedStates, increasingTimeShift, decreasingTimeShift);
  68.     }

  69.     /** Private constructor with full parameters.
  70.      * <p>
  71.      * This constructor is private as users are expected to use the builder
  72.      * API with the various {@code withXxx()} methods to set up the instance
  73.      * in a readable manner without using a huge amount of parameters.
  74.      * </p>
  75.      * @param maxCheck maximum checking interval (s)
  76.      * @param threshold convergence threshold (s)
  77.      * @param maxIter maximum number of iterations in the event time search
  78.      * @param handler event handler to call at event occurrences
  79.      * @param detector event detector for the raw unshifted event
  80.      * @param useShiftedStates if true, the state provided to {@link
  81.      * #eventOccurred(SpacecraftState, boolean) eventOccurred} method of
  82.      * the <code>detector</code> will remain shifted, otherwise it will
  83.      * be <i>unshifted</i> to correspond to the underlying raw event.
  84.      * @param increasingTimeShift increasing events time shift.
  85.      * @param decreasingTimeShift decreasing events time shift.
  86.      * @since 6.1
  87.      */
  88.     private EventShifter(final double maxCheck, final double threshold,
  89.                          final int maxIter, final EventHandler<? super EventShifter<T>> handler,
  90.                          final T detector, final boolean useShiftedStates,
  91.                          final double increasingTimeShift, final double decreasingTimeShift) {
  92.         super(maxCheck, threshold, maxIter, handler);
  93.         this.detector         = detector;
  94.         this.useShiftedStates = useShiftedStates;
  95.         this.increasingOffset = -increasingTimeShift;
  96.         this.decreasingOffset = -decreasingTimeShift;
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     protected EventShifter<T> create(final double newMaxCheck, final double newThreshold,
  101.                                      final int newMaxIter, final EventHandler<? super EventShifter<T>> newHandler) {
  102.         return new EventShifter<T>(newMaxCheck, newThreshold, newMaxIter, newHandler,
  103.                                    detector, useShiftedStates, -increasingOffset, -decreasingOffset);
  104.     }

  105.     /** Get the increasing events time shift.
  106.      * @return increasing events time shift
  107.      */
  108.     public double getIncreasingTimeShift() {
  109.         return -increasingOffset;
  110.     }

  111.     /** Get the decreasing events time shift.
  112.      * @return decreasing events time shift
  113.      */
  114.     public double getDecreasingTimeShift() {
  115.         return -decreasingOffset;
  116.     }

  117.     /** {@inheritDoc} */
  118.     public void init(final SpacecraftState s0,
  119.                      final AbsoluteDate t) {
  120.         super.init(s0, t);
  121.         detector.init(s0, t);
  122.     }

  123.     /** {@inheritDoc} */
  124.     public double g(final SpacecraftState s) {
  125.         final double incShiftedG = detector.g(s.shiftedBy(increasingOffset));
  126.         final double decShiftedG = detector.g(s.shiftedBy(decreasingOffset));
  127.         return (increasingOffset >= decreasingOffset) ?
  128.                FastMath.max(incShiftedG, decShiftedG) : FastMath.min(incShiftedG, decShiftedG);
  129.     }

  130.     /** Local class for handling events. */
  131.     private static class LocalHandler<T extends EventDetector> implements EventHandler<EventShifter<T>> {

  132.         /** Shifted state at even occurrence. */
  133.         private SpacecraftState shiftedState;

  134.         /** {@inheritDoc} */
  135.         public Action eventOccurred(final SpacecraftState s, final EventShifter<T> shifter, final boolean increasing) {

  136.             if (shifter.useShiftedStates) {
  137.                 // the state provided by the caller already includes the time shift
  138.                 shiftedState = s;
  139.             } else {
  140.                 // we need to "unshift" the state
  141.                 final double offset = increasing ? shifter.increasingOffset : shifter.decreasingOffset;
  142.                 shiftedState = s.shiftedBy(offset);
  143.             }

  144.             return shifter.detector.eventOccurred(shiftedState, increasing);

  145.         }

  146.         /** {@inheritDoc} */
  147.         @Override
  148.         public SpacecraftState resetState(final EventShifter<T> shifter, final SpacecraftState oldState) {
  149.             return shifter.detector.resetState(shiftedState);
  150.         }

  151.     }

  152. }