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.     /** {@inheritDoc} */
  73.     @Override
  74.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  75.         this.firingIntervalDetector.init(initialState, target);
  76.         super.init(initialState, target);
  77.     }

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

  85.     /**
  86.      * Getter for the firing interval detector.
  87.      * @return firing interval detector
  88.      */
  89.     public T getFiringIntervalDetector() {
  90.         return firingIntervalDetector;
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     protected boolean isFiringOnInitialState(final SpacecraftState initialState, final boolean isForward) {

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

  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public Stream<EventDetector> getEventDetectors() {
  117.         return Stream.of(firingIntervalDetector);
  118.     }

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

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

  127.         return Stream.of(fd);

  128.     }

  129.     /** Convert a detector and set up check interval, threshold and new handler.
  130.      * <p>
  131.      * This method is not inlined in {@link #getFieldEventDetectors(Field)} because the
  132.      * parameterized types confuses the Java compiler.
  133.      * </p>
  134.      * @param field field to which the state belongs
  135.      * @param <D> type of the event detector
  136.      * @param <S> type of the field elements
  137.      * @return converted firing intervals detector
  138.      */
  139.     private <D extends FieldAbstractDetector<D, S>, S extends CalculusFieldElement<S>> D convertAndSetUpHandler(final Field<S> field) {
  140.         final FieldAbstractDetector<D, S> converted = convertIntervalDetector(field, firingIntervalDetector);
  141.         final FieldAdaptableInterval<S>   maxCheck  = s -> firingIntervalDetector.getMaxCheckInterval().currentInterval(s.toSpacecraftState());
  142.         return converted.
  143.                withMaxCheck(maxCheck).
  144.                withThreshold(field.getZero().newInstance(firingIntervalDetector.getThreshold())).
  145.                withHandler(new FieldHandler<>());
  146.     }

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

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

  181.         /** Propagation direction. */
  182.         private boolean forward;

  183.         /** {@inheritDoc} */
  184.         @Override
  185.         public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
  186.             forward = target.isAfterOrEqualTo(initialState);
  187.             initializeResetters(initialState, target);
  188.         }

  189.         /** {@inheritDoc} */
  190.         @Override
  191.         public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
  192.             if (forward) {
  193.                 getFirings().addValidAfter(increasing, s.getDate(), false);
  194.             } else {
  195.                 getFirings().addValidBefore(!increasing, s.getDate(), false);
  196.             }
  197.             notifyResetters(s, increasing);
  198.             return Action.RESET_STATE;
  199.         }

  200.         /** {@inheritDoc} */
  201.         @Override
  202.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  203.             return applyResetters(oldState);
  204.         }

  205.     }

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

  210.         /** Propagation direction. */
  211.         private boolean forward;

  212.         /** {@inheritDoc} */
  213.         @Override
  214.         public void init(final FieldSpacecraftState<S> initialState,
  215.                          final FieldAbsoluteDate<S> target,
  216.                          final FieldEventDetector<S> detector) {
  217.             forward = target.isAfterOrEqualTo(initialState);
  218.             initializeResetters(initialState, target);
  219.         }

  220.         /** {@inheritDoc} */
  221.         @Override
  222.         public Action eventOccurred(final FieldSpacecraftState<S> s, final FieldEventDetector<S> detector, final boolean increasing) {
  223.             if (forward) {
  224.                 getFirings().addValidAfter(increasing, s.getDate().toAbsoluteDate(), false);
  225.             } else {
  226.                 getFirings().addValidBefore(!increasing, s.getDate().toAbsoluteDate(), false);
  227.             }
  228.             notifyResetters(s, increasing);
  229.             return Action.RESET_STATE;
  230.         }

  231.         /** {@inheritDoc} */
  232.         @Override
  233.         public FieldSpacecraftState<S> resetState(final FieldEventDetector<S> detector, final FieldSpacecraftState<S> oldState) {
  234.             return applyResetters(oldState);
  235.         }

  236.     }

  237. }