IntervalEventTrigger.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.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.AbstractDetector;
  27. import org.orekit.propagation.events.EventDetector;
  28. import org.orekit.propagation.events.FieldAbstractDetector;
  29. import org.orekit.propagation.events.FieldAdaptableInterval;
  30. import org.orekit.propagation.events.FieldEventDetector;
  31. import org.orekit.propagation.events.handlers.EventHandler;
  32. import org.orekit.propagation.events.handlers.FieldEventHandler;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.time.FieldAbsoluteDate;

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

  47.     /** Intervals detector. */
  48.     private final T firingIntervalDetector;

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

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

  72.     /**
  73.      * Getter for the firing interval detector.
  74.      * @return firing interval detector
  75.      */
  76.     public T getFiringIntervalDetector() {
  77.         return firingIntervalDetector;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     protected boolean isFiringOnInitialState(final SpacecraftState initialState, final boolean isForward) {

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

  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public Stream<EventDetector> getEventDetectors() {
  104.         return Stream.of(firingIntervalDetector);
  105.     }

  106.     /** {@inheritDoc} */
  107.     public <S extends CalculusFieldElement<S>> Stream<FieldEventDetector<S>> getFieldEventDetectors(final Field<S> field) {

  108.         @SuppressWarnings("unchecked")
  109.         FieldEventDetector<S> fd = (FieldEventDetector<S>) cached.get(field);
  110.         if (fd == null) {
  111.             fd = convertAndSetUpHandler(field);
  112.             cached.put(field, fd);
  113.         }

  114.         return Stream.of(fd);

  115.     }

  116.     /** Convert a detector and set up check interval, threshold and new handler.
  117.      * <p>
  118.      * This method is not inlined in {@link #getFieldEventDetectors(Field)} because the
  119.      * parameterized types confuses the Java compiler.
  120.      * </p>
  121.      * @param field field to which the state belongs
  122.      * @param <D> type of the event detector
  123.      * @param <S> type of the field elements
  124.      * @return converted firing intervals detector
  125.      */
  126.     private <D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>> D convertAndSetUpHandler(final Field<S> field) {
  127.         final FieldAbstractDetector<D, S> converted = convertIntervalDetector(field, firingIntervalDetector);
  128.         final FieldAdaptableInterval<S>   maxCheck  = s -> firingIntervalDetector.getMaxCheckInterval().currentInterval(s.toSpacecraftState());
  129.         return converted.
  130.                withMaxCheck(maxCheck).
  131.                withThreshold(field.getZero().newInstance(firingIntervalDetector.getThreshold())).
  132.                withHandler(new FieldHandler<>());
  133.     }

  134.     /** Convert a primitive firing intervals detector into a field firing intervals detector.
  135.      * <p>
  136.      * There is not need to set up {@link FieldAbstractDetector#withMaxCheck(FieldAdaptableInterval) withMaxCheck},
  137.      * {@link FieldAbstractDetector#withThreshold(CalculusFieldElement) withThreshold}, or
  138.      * {@link FieldAbstractDetector#withHandler(org.orekit.propagation.events.handlers.FieldEventHandler) withHandler}
  139.      * in the converted detector, this will be done by caller.
  140.      * </p>
  141.      * <p>
  142.      * A skeleton implementation of this method to convert some {@code XyzDetector} into {@code FieldXyzDetector},
  143.      * considering these detectors are created from a date and a number parameter is:
  144.      * </p>
  145.      * <pre>{@code
  146.      *     protected <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
  147.      *         FieldAbstractDetector<D, S> convertIntervalDetector(final Field<S> field, final XyzDetector detector) {
  148.      *
  149.      *         final FieldAbsoluteDate<S> date  = new FieldAbsoluteDate<>(field, detector.getDate());
  150.      *         final S                    param = field.getZero().newInstance(detector.getParam());
  151.      *
  152.      *         final FieldAbstractDetector<D, S> converted = (FieldAbstractDetector<D, S>) new FieldXyzDetector<>(date, param);
  153.      *         return converted;
  154.      *
  155.      *     }
  156.      * }
  157.      * </pre>
  158.      * @param field field to which the state belongs
  159.      * @param detector primitive firing intervals detector to convert
  160.      * @param <D> type of the event detector
  161.      * @param <S> type of the field elements
  162.      * @return converted firing intervals detector
  163.      */
  164.     protected abstract <D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>>
  165.         FieldAbstractDetector<D, S> convertIntervalDetector(Field<S> field, T detector);

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

  168.         /** Propagation direction. */
  169.         private boolean forward;

  170.         /** {@inheritDoc} */
  171.         @Override
  172.         public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
  173.             forward = target.isAfterOrEqualTo(initialState);
  174.             initializeResetters(initialState, target);
  175.         }

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

  187.         /** {@inheritDoc} */
  188.         @Override
  189.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  190.             return applyResetters(oldState);
  191.         }

  192.     }

  193.     /** Local handler for both start and stop triggers.
  194.      * @param <S> type of the field elements
  195.      */
  196.     private class FieldHandler<D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>> implements FieldEventHandler<S> {

  197.         /** Propagation direction. */
  198.         private boolean forward;

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

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

  218.         /** {@inheritDoc} */
  219.         @Override
  220.         public FieldSpacecraftState<S> resetState(final FieldEventDetector<S> detector, final FieldSpacecraftState<S> oldState) {
  221.             return applyResetters(oldState);
  222.         }

  223.     }

  224. }