EventShifter.java

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

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

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

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

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

  45.     /** Event detection settings. */
  46.     private final EventDetectionSettings detectionSettings;

  47.     /** Specialized event handler. */
  48.     private final LocalHandler handler;

  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 EventHandler#eventOccurred(SpacecraftState, EventDetector, 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.      * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred} method of
  60.      * the associated {@code handler} 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 EventDetector detector, final boolean useShiftedStates,
  66.                         final double increasingTimeShift, final double decreasingTimeShift) {
  67.         this(detector.getDetectionSettings(), detector, useShiftedStates, increasingTimeShift, decreasingTimeShift);
  68.     }

  69.     /** Protected constructor with full parameters.
  70.      * <p>
  71.      * This constructor is not public 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 detectionSettings event detection settings
  76.      * @param detector event detector for the raw unshifted event
  77.      * @param useShiftedStates if true, the state provided to {@link
  78.      * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred} method of
  79.      * the <code>detector</code> will remain shifted, otherwise it will
  80.      * be <i>unshifted</i> to correspond to the underlying raw event.
  81.      * @param increasingTimeShift increasing events time shift.
  82.      * @param decreasingTimeShift decreasing events time shift.
  83.      * @since 13.0
  84.      */
  85.     public EventShifter(final EventDetectionSettings detectionSettings,
  86.                         final EventDetector detector, final boolean useShiftedStates,
  87.                         final double increasingTimeShift, final double decreasingTimeShift) {
  88.         this.detectionSettings = detectionSettings;
  89.         this.handler = new LocalHandler();
  90.         this.detector         = detector;
  91.         this.useShiftedStates = useShiftedStates;
  92.         this.increasingOffset = -increasingTimeShift;
  93.         this.decreasingOffset = -decreasingTimeShift;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public EventHandler getHandler() {
  98.         return handler;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public EventDetectionSettings getDetectionSettings() {
  103.         return detectionSettings;
  104.     }

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

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

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

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

  133.     /**
  134.      * Builds a new instance from the input detection settings.
  135.      * @param settings event detection settings to be used
  136.      * @return a new detector
  137.      */
  138.     public EventShifter withDetectionSettings(final EventDetectionSettings settings) {
  139.         return new EventShifter(settings, detector, useShiftedStates, getIncreasingTimeShift(), getDecreasingTimeShift());
  140.     }

  141.     /** Local class for handling events. */
  142.     private static class LocalHandler implements EventHandler {

  143.         /** Shifted state at even occurrence. */
  144.         private SpacecraftState shiftedState;

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

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

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

  157.         }

  158.         /** {@inheritDoc} */
  159.         @Override
  160.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  161.             final EventShifter shifter = (EventShifter) detector;
  162.             return shifter.detector.getHandler().resetState(shifter.detector, shiftedState);
  163.         }

  164.     }

  165. }