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.     /** {@inheritDoc} */
  165.     @Override
  166.     public boolean dependsOnTimeOnly() {
  167.         return true;
  168.     }

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

  183.     /** Get the current event date according to the propagator.
  184.      * @return event date
  185.      */
  186.     public AbsoluteDate getDate() {
  187.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  188.     }

  189.     /** Get the minimum gap between added dates.
  190.      * @return the minimum gap between added dates (s)
  191.      */
  192.     public double getMinGap() {
  193.         return minGap;
  194.     }

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

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

  261.     /** Event date specification. */
  262.     private static class EventDate implements TimeStamped {

  263.         /** Event date. */
  264.         private final AbsoluteDate eventDate;

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

  267.         /** Simple constructor.
  268.          * @param date date
  269.          * @param increase if true, g function increases around event date
  270.          */
  271.         EventDate(final AbsoluteDate date, final boolean increase) {
  272.             this.eventDate = date;
  273.             this.gIncrease = increase;
  274.         }

  275.         /** Getter for event date.
  276.          * @return event date
  277.          */
  278.         public AbsoluteDate getDate() {
  279.             return eventDate;
  280.         }

  281.         /** Getter for g function way at event date.
  282.          * @return g function increasing flag
  283.          */
  284.         public boolean isgIncrease() {
  285.             return gIncrease;
  286.         }

  287.     }

  288. }