ParameterDrivenDateIntervalDetector.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.propagation.events;

  18. import java.util.List;
  19. import java.util.stream.Collectors;

  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.propagation.events.handlers.StopOnDecreasing;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.utils.DateDriver;
  28. import org.orekit.utils.ParameterDriver;
  29. import org.orekit.utils.ParameterObserver;
  30. import org.orekit.utils.TimeSpanMap;
  31. import org.orekit.utils.TimeSpanMap.Span;

  32. /** Detector for date intervals that may be offset thanks to parameter drivers.
  33.  * <p>
  34.  * Two dual views can be used for date intervals: either start date/stop date or
  35.  * median date/duration. {@link #getStartDriver() start}/{@link #getStopDriver() stop}
  36.  * drivers and {@link #getMedianDriver() median}/{@link #getDurationDriver() duration}
  37.  * drivers work in pair. Both drivers in one pair can be selected and their changes will
  38.  * be propagated to the other pair, but attempting to select drivers in both
  39.  * pairs at the same time will trigger an exception. Changing the value of a driver
  40.  * that is not selected should be avoided as it leads to inconsistencies between the pairs.
  41.  * </p>. Warning, startDate driver, stopDate driver, duration driver and medianDate driver
  42.  * must all have the same number of values to estimate (same number of span in valueSpanMap), that is is to
  43.  * say that the {@link org.orekit.utils.ParameterDriver#addSpans(AbsoluteDate, AbsoluteDate, double)}
  44.  * should be called with same arguments.
  45.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  46.  * @author Luc Maisonobe
  47.  * @since 11.1
  48.  */
  49. public class ParameterDrivenDateIntervalDetector extends AbstractDetector<ParameterDrivenDateIntervalDetector> {

  50.     /** Default suffix for start driver. */
  51.     public static final String START_SUFFIX = "_START";

  52.     /** Default suffix for stop driver. */
  53.     public static final String STOP_SUFFIX = "_STOP";

  54.     /** Default suffix for median driver. */
  55.     public static final String MEDIAN_SUFFIX = "_MEDIAN";

  56.     /** Default suffix for duration driver. */
  57.     public static final String DURATION_SUFFIX = "_DURATION";

  58.     /** Detection threshold. */
  59.     private static final double THRESHOLD = 1.0e-10;

  60.     /** Reference interval start driver. */
  61.     private DateDriver start;

  62.     /** Reference interval stop driver. */
  63.     private DateDriver stop;

  64.     /** Median date driver. */
  65.     private DateDriver median;

  66.     /** Duration driver. */
  67.     private ParameterDriver duration;

  68.     /** Build a new instance.
  69.      * @param prefix prefix to use for parameter drivers names
  70.      * @param refMedian reference interval median date
  71.      * @param refDuration reference duration
  72.      */
  73.     public ParameterDrivenDateIntervalDetector(final String prefix,
  74.                                                final AbsoluteDate refMedian, final double refDuration) {
  75.         this(prefix,
  76.              refMedian.shiftedBy(-0.5 * refDuration),
  77.              refMedian.shiftedBy(+0.5 * refDuration));
  78.     }

  79.     /** Build a new instance.
  80.      * @param prefix prefix to use for parameter drivers names
  81.      * @param refStart reference interval start date
  82.      * @param refStop reference interval stop date
  83.      */
  84.     public ParameterDrivenDateIntervalDetector(final String prefix,
  85.                                                final AbsoluteDate refStart, final AbsoluteDate refStop) {
  86.         this(s -> FastMath.max(0.5 * refStop.durationFrom(refStart), THRESHOLD),
  87.              THRESHOLD, DEFAULT_MAX_ITER,
  88.              new StopOnDecreasing(),
  89.              new DateDriver(refStart, prefix + START_SUFFIX, true),
  90.              new DateDriver(refStop, prefix + STOP_SUFFIX, false),
  91.              new DateDriver(refStart.shiftedBy(0.5 * refStop.durationFrom(refStart)), prefix + MEDIAN_SUFFIX, true),
  92.              new ParameterDriver(prefix + DURATION_SUFFIX, refStop.durationFrom(refStart), 1.0, 0.0, Double.POSITIVE_INFINITY));
  93.     }

  94.     /** Protected constructor with full parameters.
  95.      * <p>
  96.      * This constructor is not public as users are expected to use the builder
  97.      * API with the various {@code withXxx()} methods to set up the instance
  98.      * in a readable manner without using a huge amount of parameters.
  99.      * </p>
  100.      * @param maxCheck maximum checking interval
  101.      * @param threshold convergence threshold (s)
  102.      * @param maxIter maximum number of iterations in the event time search
  103.      * @param handler event handler to call at event occurrences
  104.      * @param start reference interval start driver
  105.      * @param stop reference interval stop driver
  106.      * @param median median date driver
  107.      * @param duration duration driver
  108.      */
  109.     protected ParameterDrivenDateIntervalDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  110.                                                   final EventHandler handler,
  111.                                                   final DateDriver start, final DateDriver stop,
  112.                                                   final DateDriver median, final ParameterDriver duration) {
  113.         super(new EventDetectionSettings(maxCheck, threshold, maxIter), handler);
  114.         this.start    = start;
  115.         this.stop     = stop;
  116.         this.median   = median;
  117.         this.duration = duration;

  118.         // set up delegation between drivers
  119.         replaceBindingObserver(start,    new StartObserver());
  120.         replaceBindingObserver(stop,     new StopObserver());
  121.         replaceBindingObserver(median,   new MedianObserver());
  122.         replaceBindingObserver(duration, new DurationObserver());

  123.     }

  124.     /** Replace binding observers.
  125.      * @param driver driver for whose binding observers should be replaced
  126.      * @param bindingObserver new binding observer
  127.      */
  128.     private void replaceBindingObserver(final ParameterDriver driver, final BindingObserver bindingObserver) {

  129.         // remove the previous binding observers
  130.         final List<ParameterObserver> original = driver.
  131.                                                  getObservers().
  132.                                                  stream().
  133.                                                  filter(observer -> observer instanceof ParameterDrivenDateIntervalDetector.BindingObserver).
  134.                                                  collect(Collectors.toList());
  135.         original.forEach(driver::removeObserver);

  136.         driver.addObserver(bindingObserver);

  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     protected ParameterDrivenDateIntervalDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
  141.                                                          final EventHandler newHandler) {
  142.         return new ParameterDrivenDateIntervalDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  143.                                                        start, stop, median, duration);
  144.     }

  145.     /** Get the driver for start date.
  146.      * <p>
  147.      * Note that the start date is automatically adjusted if either
  148.      * {@link #getMedianDriver() median date} or {@link #getDurationDriver() duration}
  149.      * are {@link ParameterDriver#isSelected() selected} and changed.
  150.      * </p>
  151.      * @return driver for start date
  152.      */
  153.     public DateDriver getStartDriver() {
  154.         return start;
  155.     }

  156.     /** Get the driver for stop date.
  157.      * <p>
  158.      * Note that the stop date is automatically adjusted if either
  159.      * {@link #getMedianDriver() median date} or {@link #getDurationDriver() duration}
  160.      * are {@link ParameterDriver#isSelected() selected} changed.
  161.      * </p>
  162.      * @return driver for stop date
  163.      */
  164.     public DateDriver getStopDriver() {
  165.         return stop;
  166.     }

  167.     /** Get the driver for median date.
  168.      * <p>
  169.      * Note that the median date is automatically adjusted if either
  170.      * {@link #getStartDriver()} start date or {@link #getStopDriver() stop date}
  171.      * are {@link ParameterDriver#isSelected() selected} changed.
  172.      * </p>
  173.      * @return driver for median date
  174.      */
  175.     public DateDriver getMedianDriver() {
  176.         return median;
  177.     }

  178.     /** Get the driver for duration.
  179.      * <p>
  180.      * Note that the duration is automatically adjusted if either
  181.      * {@link #getStartDriver()} start date or {@link #getStopDriver() stop date}
  182.      * are {@link ParameterDriver#isSelected() selected} changed.
  183.      * </p>
  184.      * @return driver for duration
  185.      */
  186.     public ParameterDriver getDurationDriver() {
  187.         return duration;
  188.     }

  189.     /** Compute the value of the switching function.
  190.      * <p>
  191.      * The function is positive for dates within the interval defined
  192.      * by applying the parameter drivers shifts to reference dates,
  193.      * and negative for dates outside of this interval. Note that
  194.      * if Δt_start - Δt_stop is less than ref_stop.durationFrom(ref_start),
  195.      * then the interval degenerates to empty and the function never
  196.      * reaches positive values.
  197.      * </p>
  198.      * @param s the current state information: date, kinematics, attitude
  199.      * @return value of the switching function
  200.      */
  201.     public double g(final SpacecraftState s) {
  202.         return FastMath.min(s.getDate().durationFrom(start.getDate()),
  203.                             stop.getDate().durationFrom(s.getDate()));
  204.     }

  205.     /** Base observer. */
  206.     private abstract class BindingObserver implements ParameterObserver {

  207.         /** {@inheritDoc} */
  208.         @Override
  209.         public void valueChanged(final double previousValue, final ParameterDriver driver, final AbsoluteDate date) {
  210.             if (driver.isSelected()) {
  211.                 setDelta(driver.getValue(date) - previousValue, date);
  212.             }
  213.         }
  214.         /** {@inheritDoc} */
  215.         @Override
  216.         public void valueSpanMapChanged(final TimeSpanMap<Double> previousValue, final ParameterDriver driver) {
  217.             if (driver.isSelected()) {
  218.                 for (Span<Double> span = driver.getValueSpanMap().getFirstSpan(); span != null; span = span.next()) {
  219.                     setDelta(span.getData() - previousValue.get(span.getStart()), span.getStart());
  220.                 }
  221.             }
  222.         }
  223.         /** {@inheritDoc} */
  224.         @Override
  225.         public void selectionChanged(final boolean previousSelection, final ParameterDriver driver) {
  226.             if ((start.isSelected()  || stop.isSelected()) &&
  227.                 (median.isSelected() || duration.isSelected())) {
  228.                 throw new OrekitException(OrekitMessages.INCONSISTENT_SELECTION,
  229.                                           start.getName(), stop.getName(),
  230.                                           median.getName(), duration.getName());
  231.             }
  232.         }

  233.         /** Change a value.
  234.          * @param delta change of value
  235.          * @param date date at which the delta wants to be set
  236.          */
  237.         protected abstract void setDelta(double delta, AbsoluteDate date);

  238.     }

  239.     /** Observer for start date. */
  240.     private class StartObserver extends BindingObserver {
  241.         /** {@inheritDoc} */
  242.         @Override
  243.         protected void setDelta(final double delta, final AbsoluteDate date) {
  244.             // date driver has no validity period, only 1 value is estimated
  245.             // over the all interval so there is no problem for calling getValue with null argument
  246.             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
  247.             // of the driver
  248.             median.setValue(median.getValue(date) + 0.5 * delta, date);
  249.             duration.setValue(duration.getValue(date) - delta, date);
  250.         }
  251.     }

  252.     /** Observer for stop date. */
  253.     private class StopObserver extends BindingObserver {
  254.         /** {@inheritDoc} */
  255.         @Override
  256.         protected void setDelta(final double delta, final AbsoluteDate date) {
  257.             // date driver has no validity period, only 1 value is estimated
  258.             // over the all interval so there is no problem for calling getValue with null argument
  259.             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
  260.             // of the driver
  261.             median.setValue(median.getValue(date) + 0.5 * delta, date);
  262.             duration.setValue(duration.getValue(date) + delta, date);
  263.         }
  264.     }

  265.     /** Observer for median date. */
  266.     private class MedianObserver extends BindingObserver {
  267.         /** {@inheritDoc} */
  268.         @Override
  269.         protected void setDelta(final double delta, final AbsoluteDate date) {
  270.             // date driver has no validity period, only 1 value is estimated
  271.             // over the all interval so there is no problem for calling getValue with null argument
  272.             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
  273.             // of the driver
  274.             start.setValue(start.getValue(date) + delta, date);
  275.             stop.setValue(stop.getValue(date) + delta, date);
  276.         }
  277.     }

  278.     /** Observer for duration. */
  279.     private class DurationObserver extends BindingObserver {
  280.         /** {@inheritDoc} */
  281.         @Override
  282.         protected void setDelta(final double delta, final AbsoluteDate date) {
  283.             // date driver has no validity period, only 1 value is estimated
  284.             // over the all interval so there is no problem for calling getValue with null argument
  285.             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
  286.             // of the driver
  287.             start.setValue(start.getValue(date) - 0.5 * delta, date);
  288.             stop.setValue(stop.getValue(date) + 0.5 * delta, date);
  289.         }
  290.     }

  291. }