FieldEventShifter.java

  1. /* Copyright 2022-2025 Romain Serra
  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.CalculusFieldElement;
  19. import org.hipparchus.ode.events.Action;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.EventHandler;
  24. import org.orekit.propagation.events.handlers.FieldEventHandler;

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

  43.     /** Event detector for the raw unshifted event. */
  44.     private final FieldEventDetector<T> detector;

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

  47.     /** Offset to apply to find increasing events. */
  48.     private final T increasingOffset;

  49.     /** Offset to apply to find decreasing events. */
  50.     private final T decreasingOffset;

  51.     /** Specialized event handler. */
  52.     private final LocalHandler<T> handler;

  53.     /** Event detection settings. */
  54.     private final FieldEventDetectionSettings<T> detectionSettings;

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

  75.     /** Constructor with full parameters.
  76.      * @param detectionSettings event detection settings
  77.      * @param detector event detector for the raw unshifted event
  78.      * @param useShiftedStates if true, the state provided to {@link
  79.      * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred} method of
  80.      * the <code>detector</code> will remain shifted, otherwise it will
  81.      * be <i>unshifted</i> to correspond to the underlying raw event.
  82.      * @param increasingTimeShift increasing events time shift.
  83.      * @param decreasingTimeShift decreasing events time shift.
  84.      * @since 13.0
  85.      */
  86.     public FieldEventShifter(final FieldEventDetectionSettings<T> detectionSettings,
  87.                              final FieldEventDetector<T> detector, final boolean useShiftedStates,
  88.                              final T increasingTimeShift, final T decreasingTimeShift) {
  89.         this.detectionSettings = detectionSettings;
  90.         this.handler          = new LocalHandler<>();
  91.         this.detector         = detector;
  92.         this.useShiftedStates = useShiftedStates;
  93.         this.increasingOffset = increasingTimeShift.negate();
  94.         this.decreasingOffset = decreasingTimeShift.negate();
  95.     }

  96.     @Override
  97.     public FieldEventHandler<T> getHandler() {
  98.         return handler;
  99.     }

  100.     @Override
  101.     public FieldEventDetectionSettings<T> getDetectionSettings() {
  102.         return detectionSettings;
  103.     }

  104.     /**
  105.      * Get the detector for the raw unshifted event.
  106.      * @return the detector for the raw unshifted event
  107.      */
  108.     public FieldEventDetector<T> getDetector() {
  109.         return detector;
  110.     }

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

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

  123.     /**
  124.      * Builds a new instance from the input detection settings.
  125.      * @param settings event detection settings to be used
  126.      * @return a new detector
  127.      */
  128.     public FieldEventShifter<T> withDetectionSettings(final FieldEventDetectionSettings<T> settings) {
  129.         return new FieldEventShifter<>(settings, detector, useShiftedStates, getIncreasingTimeShift(), getDecreasingTimeShift());
  130.     }

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     public T g(final FieldSpacecraftState<T> s) {
  134.         final T incShiftedG = detector.g(s.shiftedBy(increasingOffset));
  135.         final T decShiftedG = detector.g(s.shiftedBy(decreasingOffset));
  136.         return (increasingOffset.getReal() >= decreasingOffset.getReal()) ?
  137.                FastMath.max(incShiftedG, decShiftedG) : FastMath.min(incShiftedG, decShiftedG);
  138.     }

  139.     /** Local class for handling events. */
  140.     private static class LocalHandler<W extends CalculusFieldElement<W>> implements FieldEventHandler<W> {

  141.         /** Shifted state at even occurrence. */
  142.         private FieldSpacecraftState<W> shiftedState;

  143.         /** {@inheritDoc} */
  144.         public Action eventOccurred(final FieldSpacecraftState<W> s, final FieldEventDetector<W> detector,
  145.                                     final boolean increasing) {

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

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

  156.         }

  157.         /** {@inheritDoc} */
  158.         @Override
  159.         public FieldSpacecraftState<W> resetState(final FieldEventDetector<W> detector,
  160.                                                   final FieldSpacecraftState<W> oldState) {
  161.             final FieldEventShifter<W> shifter = (FieldEventShifter<W>) detector;
  162.             return shifter.detector.getHandler().resetState(shifter.detector, shiftedState);
  163.         }

  164.     }

  165. }