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

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.hipparchus.ode.events.Action;
  22. import org.orekit.errors.OrekitIllegalArgumentException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.propagation.SpacecraftState;
  25. import org.orekit.propagation.events.handlers.EventHandler;
  26. import org.orekit.propagation.events.handlers.StopOnEvent;
  27. import org.orekit.propagation.events.intervals.DateDetectionAdaptableIntervalFactory;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.TimeStamped;

  30. /** Finder for date events.
  31.  * <p>This class finds date events (i.e. occurrence of some predefined dates).</p>
  32.  * <p>As of version 5.1, it is an enhanced date detector:</p>
  33.  * <ul>
  34.  *   <li>it can be defined without prior date ({@link #DateDetector(TimeStamped...)})</li>
  35.  *   <li>several dates can be added ({@link #addEventDate(AbsoluteDate)})</li>
  36.  * </ul>
  37.  * <p>The gap between the added dates must be more than the minGap.</p>
  38.  * <p>The default implementation behavior is to {@link Action#STOP stop}
  39.  * propagation at the first event date occurrence. This can be changed by calling
  40.  * {@link #withHandler(EventHandler)} after construction.</p>
  41.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  42.  * @author Luc Maisonobe
  43.  * @author Pascal Parraud
  44.  */
  45. public class DateDetector extends AbstractDetector<DateDetector> implements TimeStamped {

  46.     /** Default value for max check.
  47.      * @since 12.0
  48.      */
  49.     public static final double DEFAULT_MAX_CHECK = DateDetectionAdaptableIntervalFactory.DEFAULT_MAX_CHECK;

  50.     /** Default value for minimum gap between added dates.
  51.      * @since 12.0
  52.      */
  53.     public static final double DEFAULT_MIN_GAP = 1.0;

  54.     /** Default value for convergence threshold.
  55.      * @since 12.0
  56.      */
  57.     public static final double DEFAULT_THRESHOLD = 1.0e-10;

  58.     /** Minimum gap between added dates.
  59.      * @since 12.0
  60.      */
  61.     private final double minGap;

  62.     /** Last date for g computation. */
  63.     private AbsoluteDate gDate;

  64.     /** List of event dates. */
  65.     private final ArrayList<EventDate> eventDateList;

  66.     /** Current event date. */
  67.     private int currentIndex;

  68.     /** Build a new instance.
  69.      * <p>First event dates are set here, but others can be
  70.      * added later with {@link #addEventDate(AbsoluteDate)}, although the max. check should probably be changed then.</p>
  71.      * @param dates list of event dates
  72.      * @see #addEventDate(AbsoluteDate)
  73.      * @since 12.0
  74.      */
  75.     public DateDetector(final TimeStamped... dates) {
  76.         this(new EventDetectionSettings(DateDetectionAdaptableIntervalFactory.getDatesDetectionConstantInterval(dates),
  77.                         DEFAULT_THRESHOLD, EventDetectionSettings.DEFAULT_MAX_ITER),
  78.                 new StopOnEvent(), DEFAULT_MIN_GAP, dates);
  79.     }

  80.     /** Build a new instance from a single time.
  81.      * <p>First event dates are set here, but others can be
  82.      * added later with {@link #addEventDate(AbsoluteDate)}, although the max. check should probably be changed then.</p>
  83.      * @param date event date
  84.      * @see #addEventDate(AbsoluteDate)
  85.      * @since 13.0
  86.      */
  87.     public DateDetector(final AbsoluteDate date) {
  88.         this((TimeStamped) date);
  89.     }

  90.     /** Build a new instance.
  91.      * <p>First event dates are set here, but others can be
  92.      * added later with {@link #addEventDate(AbsoluteDate)}, although the max. check should probably be changed then.</p>
  93.      * @param minGap minimum gap between added dates (s)
  94.      * @param dates list of event dates
  95.      * @see #addEventDate(AbsoluteDate)
  96.      * @since 13.0
  97.      */
  98.     public DateDetector(final double minGap, final TimeStamped... dates) {
  99.         this(new EventDetectionSettings(DateDetectionAdaptableIntervalFactory.getDatesDetectionConstantInterval(dates),
  100.                                         DEFAULT_THRESHOLD, EventDetectionSettings.DEFAULT_MAX_ITER),
  101.              new StopOnEvent(), minGap, dates);
  102.     }

  103.     /** Build a new instance from a single time.
  104.      * <p>First event dates are set here, but others can be
  105.      * added later with {@link #addEventDate(AbsoluteDate)}, although the max. check should probably be changed then.</p>
  106.      * @param minGap minimum gap between added dates (s)
  107.      * @param date event date
  108.      * @see #addEventDate(AbsoluteDate)
  109.      * @since 13.0
  110.      */
  111.     public DateDetector(final double minGap, final AbsoluteDate date) {
  112.         this(minGap, (TimeStamped) date);
  113.     }

  114.     /** Protected constructor with full parameters.
  115.      * <p>
  116.      * This constructor is not public as users are expected to use the builder
  117.      * API with the various {@code withXxx()} methods to set up the instance
  118.      * in a readable manner without using a huge amount of parameters.
  119.      * </p>
  120.      * @param detectionSettings detection settings
  121.      * @param handler event handler to call at event occurrences
  122.      * @param minGap minimum gap between added dates (s)
  123.      * @param dates list of event dates
  124.      * @since 12.2
  125.      */
  126.     protected DateDetector(final EventDetectionSettings detectionSettings,
  127.                            final EventHandler handler, final double minGap, final TimeStamped... dates) {
  128.         super(detectionSettings, handler);
  129.         this.currentIndex  = -1;
  130.         this.gDate         = null;
  131.         this.eventDateList = new ArrayList<>();
  132.         this.minGap        = minGap;
  133.         for (final TimeStamped ts : dates) {
  134.             final AbsoluteDate date = ts.getDate();
  135.             final boolean notPresentYet = eventDateList.stream().noneMatch(d -> d.getDate().isEqualTo(date));
  136.             if (notPresentYet) {
  137.                 addEventDate(date);
  138.             }
  139.         }
  140.     }

  141.     /**
  142.      * Setup minimum gap between added dates.
  143.      * @param newMinGap new minimum gap between added dates
  144.      * @return a new detector with updated configuration (the instance is not changed)
  145.      * @since 12.0
  146.      */
  147.     public DateDetector withMinGap(final double newMinGap) {
  148.         return new DateDetector(getDetectionSettings(), getHandler(), newMinGap,
  149.                                 eventDateList.toArray(new EventDate[0]));
  150.     }

  151.     /** {@inheritDoc} */
  152.     @Override
  153.     protected DateDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
  154.         return new DateDetector(detectionSettings, newHandler, minGap,
  155.                                 eventDateList.toArray(new EventDate[0]));
  156.     }

  157.     /** Get all event dates currently managed, in chronological order.
  158.      * @return all event dates currently managed, in chronological order
  159.      * @since 11.1
  160.      */
  161.     public List<TimeStamped> getDates() {
  162.         return Collections.unmodifiableList(eventDateList);
  163.     }

  164.     /** Compute the value of the switching function.
  165.      * This function measures the difference between the current and the target date.
  166.      * @param s the current state information: date, kinematics, attitude
  167.      * @return value of the switching function
  168.      */
  169.     public double g(final SpacecraftState s) {
  170.         gDate = s.getDate();
  171.         if (currentIndex < 0) {
  172.             return -1.0;
  173.         } else {
  174.             final EventDate event = getClosest(gDate);
  175.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  176.         }
  177.     }

  178.     /** Get the current event date according to the propagator.
  179.      * @return event date
  180.      */
  181.     public AbsoluteDate getDate() {
  182.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  183.     }

  184.     /** Get the minimum gap between added dates.
  185.      * @return the minimum gap between added dates (s)
  186.      */
  187.     public double getMinGap() {
  188.         return minGap;
  189.     }

  190.     /** Add an event date.
  191.      * <p>The date to add must be:</p>
  192.      * <ul>
  193.      *   <li>less than the smallest already registered event date minus the maxCheck</li>
  194.      *   <li>or more than the largest already registered event date plus the maxCheck</li>
  195.      * </ul>
  196.      * @param target target date
  197.      * @throws IllegalArgumentException if the date is too close from already defined interval
  198.      * @see #DateDetector(TimeStamped...)
  199.      */
  200.     public void addEventDate(final AbsoluteDate target) throws IllegalArgumentException {
  201.         final boolean increasing;
  202.         if (currentIndex < 0) {
  203.             increasing = (gDate == null) ? true : target.durationFrom(gDate) > 0.0;
  204.             currentIndex = 0;
  205.             eventDateList.add(new EventDate(target, increasing));
  206.         } else {
  207.             final int          lastIndex = eventDateList.size() - 1;
  208.             final AbsoluteDate firstDate = eventDateList.get(0).getDate();
  209.             final AbsoluteDate lastDate  = eventDateList.get(lastIndex).getDate();
  210.             if (firstDate.durationFrom(target) > minGap) {
  211.                 increasing = !eventDateList.get(0).isgIncrease();
  212.                 eventDateList.add(0, new EventDate(target, increasing));
  213.                 currentIndex++;
  214.             } else if (target.durationFrom(lastDate) > minGap) {
  215.                 increasing = !eventDateList.get(lastIndex).isgIncrease();
  216.                 eventDateList.add(new EventDate(target, increasing));
  217.             } else {
  218.                 throw new OrekitIllegalArgumentException(OrekitMessages.EVENT_DATE_TOO_CLOSE,
  219.                                                          target,
  220.                                                          firstDate,
  221.                                                          lastDate,
  222.                                                          minGap,
  223.                                                          firstDate.durationFrom(target),
  224.                                                          target.durationFrom(lastDate));
  225.             }
  226.         }
  227.     }

  228.     /** Get the closest EventDate to the target date.
  229.      * @param target target date
  230.      * @return current EventDate
  231.      */
  232.     private EventDate getClosest(final AbsoluteDate target) {
  233.         final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  234.         if (dt < 0.0 && currentIndex > 0) {
  235.             boolean found = false;
  236.             while (currentIndex > 0 && !found) {
  237.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  238.                     currentIndex--;
  239.                 } else {
  240.                     found = true;
  241.                 }
  242.             }
  243.         } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
  244.             final int maxIndex = eventDateList.size() - 1;
  245.             boolean found = false;
  246.             while (currentIndex < maxIndex && !found) {
  247.                 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  248.                     currentIndex++;
  249.                 } else {
  250.                     found = true;
  251.                 }
  252.             }
  253.         }
  254.         return eventDateList.get(currentIndex);
  255.     }

  256.     /** Event date specification. */
  257.     private static class EventDate implements TimeStamped {

  258.         /** Event date. */
  259.         private final AbsoluteDate eventDate;

  260.         /** Flag for g function way around event date. */
  261.         private final boolean gIncrease;

  262.         /** Simple constructor.
  263.          * @param date date
  264.          * @param increase if true, g function increases around event date
  265.          */
  266.         EventDate(final AbsoluteDate date, final boolean increase) {
  267.             this.eventDate = date;
  268.             this.gIncrease = increase;
  269.         }

  270.         /** Getter for event date.
  271.          * @return event date
  272.          */
  273.         public AbsoluteDate getDate() {
  274.             return eventDate;
  275.         }

  276.         /** Getter for g function way at event date.
  277.          * @return g function increasing flag
  278.          */
  279.         public boolean isgIncrease() {
  280.             return gIncrease;
  281.         }

  282.     }

  283. }