EventShifter.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.util.FastMath;
  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 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.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20131118L;

  41.     /** Event detector for the raw unshifted event. */
  42.     private final T detector;

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

  45.     /** Offset to apply to find increasing events. */
  46.     private final double increasingOffset;

  47.     /** Offset to apply to find decreasing events. */
  48.     private final double decreasingOffset;

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

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

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

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

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

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

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

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

  134.         /** Shifted state at even occurrence. */
  135.         private SpacecraftState shiftedState;

  136.         /** {@inheritDoc} */
  137.         public Action eventOccurred(final SpacecraftState s, final EventShifter<T> shifter, final boolean increasing)
  138.             throws OrekitException {

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

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

  148.         }

  149.         /** {@inheritDoc} */
  150.         @Override
  151.         public SpacecraftState resetState(final EventShifter<T> shifter, final SpacecraftState oldState)
  152.             throws OrekitException {
  153.             return shifter.detector.resetState(shiftedState);
  154.         }

  155.     }

  156. }