EventShifter.java

  1. /* Copyright 2002-2024 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.  * @author Luc Maisonobe
  36.  */
  37. public class EventShifter extends AbstractDetector<EventShifter> {

  38.     /** Event detector for the raw unshifted event. */
  39.     private final EventDetector detector;

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

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

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

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

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

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

  104.     /**
  105.      * Get the detector for the raw unshifted event.
  106.      * @return the detector for the raw unshifted event
  107.      * @since 11.1
  108.      */
  109.     public EventDetector getDetector() {
  110.         return detector;
  111.     }

  112.     /** Get the increasing events time shift.
  113.      * @return increasing events time shift
  114.      */
  115.     public double getIncreasingTimeShift() {
  116.         return -increasingOffset;
  117.     }

  118.     /** Get the decreasing events time shift.
  119.      * @return decreasing events time shift
  120.      */
  121.     public double getDecreasingTimeShift() {
  122.         return -decreasingOffset;
  123.     }

  124.     /** {@inheritDoc} */
  125.     public void init(final SpacecraftState s0,
  126.                      final AbsoluteDate t) {
  127.         super.init(s0, t);
  128.         detector.init(s0, t);
  129.     }

  130.     /** {@inheritDoc} */
  131.     public double g(final SpacecraftState s) {
  132.         final double incShiftedG = detector.g(s.shiftedBy(increasingOffset));
  133.         final double decShiftedG = detector.g(s.shiftedBy(decreasingOffset));
  134.         return (increasingOffset >= decreasingOffset) ?
  135.                FastMath.max(incShiftedG, decShiftedG) : FastMath.min(incShiftedG, decShiftedG);
  136.     }

  137.     /** Local class for handling events. */
  138.     private static class LocalHandler implements EventHandler {

  139.         /** Shifted state at even occurrence. */
  140.         private SpacecraftState shiftedState;

  141.         /** {@inheritDoc} */
  142.         public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {

  143.             final EventShifter shifter = (EventShifter) detector;
  144.             if (shifter.useShiftedStates) {
  145.                 // the state provided by the caller already includes the time shift
  146.                 shiftedState = s;
  147.             } else {
  148.                 // we need to "unshift" the state
  149.                 final double offset = increasing ? shifter.increasingOffset : shifter.decreasingOffset;
  150.                 shiftedState = s.shiftedBy(offset);
  151.             }

  152.             return shifter.detector.getHandler().eventOccurred(shiftedState, shifter.detector, increasing);

  153.         }

  154.         /** {@inheritDoc} */
  155.         @Override
  156.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  157.             final EventShifter shifter = (EventShifter) detector;
  158.             return shifter.detector.getHandler().resetState(shifter.detector, shiftedState);
  159.         }

  160.     }

  161. }