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.FieldEventHandler;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.time.FieldAbsoluteDate;

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

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

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

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

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

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

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

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

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

  108.     }

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

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

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

  123.         return Stream.of(fd);

  124.     }

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

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

  169.     /** Local handler for both start and stop triggers. */
  170.     private class Handler extends TriggerHandler {

  171.         /** {@inheritDoc} */
  172.         @Override
  173.         public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
  174.             if (isForward()) {
  175.                 getFirings().addValidAfter(increasing, s.getDate(), false);
  176.             } else {
  177.                 getFirings().addValidBefore(!increasing, s.getDate(), false);
  178.             }
  179.             notifyResetters(s, increasing);
  180.             return determineAction(detector, s);
  181.         }

  182.     }

  183.     /** Local handler for both start and stop triggers.
  184.      * @param <S> type of the field elements
  185.      */
  186.     private class FieldHandler<S extends CalculusFieldElement<S>> extends FieldTriggerHandler<S> {

  187.         /** {@inheritDoc} */
  188.         @Override
  189.         public Action eventOccurred(final FieldSpacecraftState<S> s, final FieldEventDetector<S> detector, final boolean increasing) {
  190.             if (isForward()) {
  191.                 getFirings().addValidAfter(increasing, s.getDate().toAbsoluteDate(), false);
  192.             } else {
  193.                 getFirings().addValidBefore(!increasing, s.getDate().toAbsoluteDate(), false);
  194.             }
  195.             notifyResetters(s, increasing);
  196.             return determineAction(detector, s);
  197.         }
  198.     }

  199. }