IntervalEventTrigger.java

  1. /* Copyright 2002-2022 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.FieldEventDetector;
  30. import org.orekit.propagation.events.handlers.EventHandler;
  31. import org.orekit.propagation.events.handlers.FieldEventHandler;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.FieldAbsoluteDate;

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

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

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

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

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

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

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

  99.     }

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

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

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

  113.         return Stream.of(fd);

  114.     }

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

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

  164.     /** Local handler for both start and stop triggers. */
  165.     private class Handler implements EventHandler<T> {

  166.         /** Propagation direction. */
  167.         private boolean forward;

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

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

  185.         /** {@inheritDoc} */
  186.         @Override
  187.         public SpacecraftState resetState(final T detector, final SpacecraftState oldState) {
  188.             return applyResetters(oldState);
  189.         }

  190.     }

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

  195.         /** Propagation direction. */
  196.         private boolean forward;

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

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

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

  221.     }

  222. }