IntervalEventTrigger.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.forces.maneuvers.trigger;

  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.stream.Stream;

  21. import org.hipparchus.CalculusFieldElement;
  22. import org.hipparchus.Field;
  23. import org.hipparchus.ode.events.Action;
  24. import org.orekit.propagation.FieldSpacecraftState;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.events.EventDetector;
  27. import org.orekit.propagation.events.FieldEventDetector;
  28. import org.orekit.propagation.events.handlers.EventHandler;
  29. import org.orekit.propagation.events.handlers.FieldEventHandler;
  30. import org.orekit.time.AbsoluteDate;
  31. import org.orekit.time.FieldAbsoluteDate;

  32. /**
  33.  * Maneuver triggers based on a single event detector that defines firing intervals.
  34.  * <p>
  35.  * Firing intervals correspond to time spans with positive value of the event detector
  36.  * {@link EventDetector#g(SpacecraftState) g} function.
  37.  * </p>
  38.  * @param <T> type of the interval detector
  39.  * @see StartStopEventsTrigger
  40.  * @author Luc Maisonobe
  41.  * @since 11.1
  42.  */
  43. public abstract class IntervalEventTrigger<T extends EventDetector> extends AbstractManeuverTriggers {

  44.     /** Intervals detector. */
  45.     private final ManeuverTriggerDetector<T> firingIntervalDetector;

  46.     /** Cached field-based detectors. */
  47.     private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldEventDetector<? extends CalculusFieldElement<?>>> cached;

  48.     /** Simple constructor.
  49.      * <p>
  50.      * Note that the {@code intervalDetector} passed as an argument is used only
  51.      * as a <em>prototype</em> from which a new detector will be built using
  52.      * {@link ManeuverTriggerDetector}. The original event handler from the prototype
  53.      * will be <em>ignored</em> and never called.
  54.      * </p>
  55.      * <p>
  56.      * If the trigger is used in a {@link org.orekit.propagation.FieldPropagator field-based propagation},
  57.      * the detector will be automatically converted to a field equivalent. Beware however that the
  58.      * {@link FieldEventHandler#eventOccurred(FieldSpacecraftState, FieldEventDetector, boolean) eventOccurred}
  59.      * of the converted propagator <em>will</em> call the method with the same name in the prototype
  60.      * detector, in order to get the correct return value.
  61.      * </p>
  62.      * @param prototypeFiringIntervalDetector prototype detector for firing interval
  63.      */
  64.     protected IntervalEventTrigger(final T prototypeFiringIntervalDetector) {
  65.         this.firingIntervalDetector = new ManeuverTriggerDetector<>(prototypeFiringIntervalDetector, new Handler());
  66.         this.cached                 = new HashMap<>();
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  71.         this.firingIntervalDetector.init(initialState, target);
  72.         super.init(initialState, target);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public <D extends CalculusFieldElement<D>> void init(final FieldSpacecraftState<D> initialState,
  77.                                                          final FieldAbsoluteDate<D> target) {
  78.         this.firingIntervalDetector.init(initialState.toSpacecraftState(), target.toAbsoluteDate());
  79.         super.init(initialState, target);
  80.     }

  81.     /**
  82.      * Getter for the firing interval detector.
  83.      * @return firing interval detector
  84.      */
  85.     public T getFiringIntervalDetector() {
  86.         return firingIntervalDetector.getDetector();
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     protected boolean isFiringOnInitialState(final SpacecraftState initialState, final boolean isForward) {

  91.         // set the initial value of firing
  92.         final double insideThrustArcG = firingIntervalDetector.g(initialState);
  93.         if (insideThrustArcG == 0) {
  94.             // bound of arc
  95.             // check state for the upcoming times
  96.             final double shift = (isForward ? 2 : -2) * firingIntervalDetector.getThreshold();
  97.             if (firingIntervalDetector.g(initialState.shiftedBy(shift)) > 0) {
  98.                 // we are entering the firing interval, from start if forward, from end if backward
  99.                 notifyResetters(initialState, isForward);
  100.                 return true;
  101.             } else {
  102.                 // we are leaving the firing interval, from end if forward, from start if backward
  103.                 notifyResetters(initialState, !isForward);
  104.                 return false;
  105.             }
  106.         } else {
  107.             return insideThrustArcG > 0;
  108.         }

  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public Stream<EventDetector> getEventDetectors() {
  113.         return Stream.of(firingIntervalDetector);
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     public <S extends CalculusFieldElement<S>> Stream<FieldEventDetector<S>> getFieldEventDetectors(final Field<S> field) {

  118.         @SuppressWarnings("unchecked")
  119.         FieldEventDetector<S> fd = (FieldEventDetector<S>) cached.get(field);
  120.         if (fd == null) {
  121.             fd = convertAndSetUpHandler(field);
  122.             cached.put(field, fd);
  123.         }

  124.         return Stream.of(fd);

  125.     }

  126.     /** Convert a detector and set up check interval, threshold and new handler.
  127.      * <p>
  128.      * This method is not inlined in {@link #getFieldEventDetectors(Field)} because the
  129.      * parameterized types confuses the Java compiler.
  130.      * </p>
  131.      * @param field field to which the state belongs
  132.      * @param <D> type of the event detector
  133.      * @param <S> type of the field elements
  134.      * @return converted firing intervals detector
  135.      */
  136.     private <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>> FieldManeuverTriggerDetector<S, D> convertAndSetUpHandler(final Field<S> field) {
  137.         final D converted = convertIntervalDetector(field, firingIntervalDetector.getDetector());
  138.         return new FieldManeuverTriggerDetector<>(converted, new FieldHandler<>());
  139.     }

  140.     /** Convert a primitive firing intervals detector into a field firing intervals detector.
  141.      * <p>
  142.      * The {@link org.orekit.propagation.events.FieldEventDetectionSettings} must be set up in conformance with the
  143.      * non-field detector.
  144.      * </p>
  145.      * <p>
  146.      * A skeleton implementation of this method to convert some {@code XyzDetector} into {@code FieldXyzDetector},
  147.      * considering these detectors are created from a date and a number parameter is:
  148.      * </p>
  149.      * <pre>{@code
  150.      *     protected <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
  151.      *         D convertIntervalDetector(final Field<S> field, final XyzDetector detector) {
  152.      *
  153.      *         final FieldAbsoluteDate<S> date  = new FieldAbsoluteDate<>(field, detector.getDate());
  154.      *         final S                    param = field.getZero().newInstance(detector.getParam());
  155.      *
  156.      *         D converted = (D) new FieldXyzDetector<>(date, param).withDetectionSettings(field, detector.getDetectionSettings());
  157.      *         return converted;
  158.      *
  159.      *     }
  160.      * }
  161.      * </pre>
  162.      * @param field field to which the state belongs
  163.      * @param detector primitive firing intervals detector to convert
  164.      * @param <D> type of the event detector
  165.      * @param <S> type of the field elements
  166.      * @return converted firing intervals detector
  167.      */
  168.     protected abstract <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
  169.         D convertIntervalDetector(Field<S> field, T detector);

  170.     /** Local handler for both start and stop triggers. */
  171.     private class Handler implements EventHandler {

  172.         /** Propagation direction. */
  173.         private boolean forward;

  174.         /** {@inheritDoc} */
  175.         @Override
  176.         public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
  177.             forward = target.isAfterOrEqualTo(initialState);
  178.             initializeResetters(initialState, target);
  179.         }

  180.         /** {@inheritDoc} */
  181.         @Override
  182.         public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
  183.             if (forward) {
  184.                 getFirings().addValidAfter(increasing, s.getDate(), false);
  185.             } else {
  186.                 getFirings().addValidBefore(!increasing, s.getDate(), false);
  187.             }
  188.             notifyResetters(s, increasing);
  189.             return Action.RESET_STATE;
  190.         }

  191.         /** {@inheritDoc} */
  192.         @Override
  193.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  194.             return applyResetters(oldState);
  195.         }

  196.     }

  197.     /** Local handler for both start and stop triggers.
  198.      * @param <S> type of the field elements
  199.      */
  200.     private class FieldHandler<S extends CalculusFieldElement<S>> implements FieldEventHandler<S> {

  201.         /** Propagation direction. */
  202.         private boolean forward;

  203.         /** {@inheritDoc} */
  204.         @Override
  205.         public void init(final FieldSpacecraftState<S> initialState,
  206.                          final FieldAbsoluteDate<S> target,
  207.                          final FieldEventDetector<S> detector) {
  208.             forward = target.isAfterOrEqualTo(initialState);
  209.             initializeResetters(initialState, target);
  210.         }

  211.         /** {@inheritDoc} */
  212.         @Override
  213.         public Action eventOccurred(final FieldSpacecraftState<S> s, final FieldEventDetector<S> detector, final boolean increasing) {
  214.             if (forward) {
  215.                 getFirings().addValidAfter(increasing, s.getDate().toAbsoluteDate(), false);
  216.             } else {
  217.                 getFirings().addValidBefore(!increasing, s.getDate().toAbsoluteDate(), false);
  218.             }
  219.             notifyResetters(s, increasing);
  220.             return Action.RESET_STATE;
  221.         }

  222.         /** {@inheritDoc} */
  223.         @Override
  224.         public FieldSpacecraftState<S> resetState(final FieldEventDetector<S> detector, final FieldSpacecraftState<S> oldState) {
  225.             return applyResetters(oldState);
  226.         }

  227.     }

  228. }