DateDetector.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.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.time.AbsoluteDate;
  28. import org.orekit.time.TimeStamped;

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

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

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

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

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

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

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

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

  67.     /** Build a new instance.
  68.      * <p>First event dates are set here, but others can be
  69.      * added later with {@link #addEventDate(AbsoluteDate)}.</p>
  70.      * @param dates list of event dates
  71.      * @see #addEventDate(AbsoluteDate)
  72.      * @since 12.0
  73.      */
  74.     public DateDetector(final TimeStamped... dates) {
  75.         this(new EventDetectionSettings(AdaptableInterval.of(DEFAULT_MAX_CHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER), new StopOnEvent(), DEFAULT_MIN_GAP, dates);
  76.     }

  77.     /** Protected constructor with full parameters.
  78.      * <p>
  79.      * This constructor is not public as users are expected to use the builder
  80.      * API with the various {@code withXxx()} methods to set up the instance
  81.      * in a readable manner without using a huge amount of parameters.
  82.      * </p>
  83.      * @param maxCheck maximum checking interval
  84.      * @param threshold convergence threshold (s)
  85.      * @param maxIter maximum number of iterations in the event time search
  86.      * @param handler event handler to call at event occurrences
  87.      * @param minGap minimum gap between added dates (s)
  88.      * @param dates list of event dates
  89.      * @since 12.0
  90.      * @deprecated as of 12.2
  91.      */
  92.     @Deprecated
  93.     protected DateDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  94.                            final EventHandler handler, final double minGap, final TimeStamped... dates) {
  95.         this(new EventDetectionSettings(maxCheck, threshold, maxIter), handler, minGap, dates);
  96.     }

  97.     /** Protected constructor with full parameters.
  98.      * <p>
  99.      * This constructor is not public as users are expected to use the builder
  100.      * API with the various {@code withXxx()} methods to set up the instance
  101.      * in a readable manner without using a huge amount of parameters.
  102.      * </p>
  103.      * @param detectionSettings detection settings
  104.      * @param handler event handler to call at event occurrences
  105.      * @param minGap minimum gap between added dates (s)
  106.      * @param dates list of event dates
  107.      * @since 12.2
  108.      */
  109.     protected DateDetector(final EventDetectionSettings detectionSettings,
  110.                            final EventHandler handler, final double minGap, final TimeStamped... dates) {
  111.         super(detectionSettings, handler);
  112.         this.currentIndex  = -1;
  113.         this.gDate         = null;
  114.         this.eventDateList = new ArrayList<>(dates.length);
  115.         for (final TimeStamped ts : dates) {
  116.             addEventDate(ts.getDate());
  117.         }
  118.         this.minGap        = minGap;
  119.     }

  120.     /**
  121.      * Setup minimum gap between added dates.
  122.      * @param newMinGap new minimum gap between added dates
  123.      * @return a new detector with updated configuration (the instance is not changed)
  124.      * @since 12.0
  125.      */
  126.     public DateDetector withMinGap(final double newMinGap) {
  127.         return new DateDetector(getDetectionSettings(), getHandler(), newMinGap,
  128.                                 eventDateList.toArray(new EventDate[eventDateList.size()]));
  129.     }

  130.     /** {@inheritDoc} */
  131.     @Override
  132.     protected DateDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
  133.                                   final int newMaxIter, final EventHandler newHandler) {
  134.         return new DateDetector(new EventDetectionSettings(newMaxCheck, newThreshold, newMaxIter), newHandler, minGap,
  135.                                 eventDateList.toArray(new EventDate[eventDateList.size()]));
  136.     }

  137.     /** Get all event dates currently managed, in chronological order.
  138.      * @return all event dates currently managed, in chronological order
  139.      * @since 11.1
  140.      */
  141.     public List<TimeStamped> getDates() {
  142.         return Collections.unmodifiableList(eventDateList);
  143.     }

  144.     /** Compute the value of the switching function.
  145.      * This function measures the difference between the current and the target date.
  146.      * @param s the current state information: date, kinematics, attitude
  147.      * @return value of the switching function
  148.      */
  149.     public double g(final SpacecraftState s) {
  150.         gDate = s.getDate();
  151.         if (currentIndex < 0) {
  152.             return -1.0;
  153.         } else {
  154.             final EventDate event = getClosest(gDate);
  155.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  156.         }
  157.     }

  158.     /** Get the current event date according to the propagator.
  159.      * @return event date
  160.      */
  161.     public AbsoluteDate getDate() {
  162.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  163.     }

  164.     /** Add an event date.
  165.      * <p>The date to add must be:</p>
  166.      * <ul>
  167.      *   <li>less than the smallest already registered event date minus the maxCheck</li>
  168.      *   <li>or more than the largest already registered event date plus the maxCheck</li>
  169.      * </ul>
  170.      * @param target target date
  171.      * @throws IllegalArgumentException if the date is too close from already defined interval
  172.      * @see #DateDetector(TimeStamped...)
  173.      */
  174.     public void addEventDate(final AbsoluteDate target) throws IllegalArgumentException {
  175.         final boolean increasing;
  176.         if (currentIndex < 0) {
  177.             increasing = (gDate == null) ? true : target.durationFrom(gDate) > 0.0;
  178.             currentIndex = 0;
  179.             eventDateList.add(new EventDate(target, increasing));
  180.         } else {
  181.             final int          lastIndex = eventDateList.size() - 1;
  182.             final AbsoluteDate firstDate = eventDateList.get(0).getDate();
  183.             final AbsoluteDate lastDate  = eventDateList.get(lastIndex).getDate();
  184.             if (firstDate.durationFrom(target) > minGap) {
  185.                 increasing = !eventDateList.get(0).isgIncrease();
  186.                 eventDateList.add(0, new EventDate(target, increasing));
  187.                 currentIndex++;
  188.             } else if (target.durationFrom(lastDate) > minGap) {
  189.                 increasing = !eventDateList.get(lastIndex).isgIncrease();
  190.                 eventDateList.add(new EventDate(target, increasing));
  191.             } else {
  192.                 throw new OrekitIllegalArgumentException(OrekitMessages.EVENT_DATE_TOO_CLOSE,
  193.                                                          target,
  194.                                                          firstDate,
  195.                                                          lastDate,
  196.                                                          minGap,
  197.                                                          firstDate.durationFrom(target),
  198.                                                          target.durationFrom(lastDate));
  199.             }
  200.         }
  201.     }

  202.     /** Get the closest EventDate to the target date.
  203.      * @param target target date
  204.      * @return current EventDate
  205.      */
  206.     private EventDate getClosest(final AbsoluteDate target) {
  207.         final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  208.         if (dt < 0.0 && currentIndex > 0) {
  209.             boolean found = false;
  210.             while (currentIndex > 0 && !found) {
  211.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  212.                     currentIndex--;
  213.                 } else {
  214.                     found = true;
  215.                 }
  216.             }
  217.         } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
  218.             final int maxIndex = eventDateList.size() - 1;
  219.             boolean found = false;
  220.             while (currentIndex < maxIndex && !found) {
  221.                 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  222.                     currentIndex++;
  223.                 } else {
  224.                     found = true;
  225.                 }
  226.             }
  227.         }
  228.         return eventDateList.get(currentIndex);
  229.     }

  230.     /** Event date specification. */
  231.     private static class EventDate implements TimeStamped {

  232.         /** Event date. */
  233.         private final AbsoluteDate eventDate;

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

  236.         /** Simple constructor.
  237.          * @param date date
  238.          * @param increase if true, g function increases around event date
  239.          */
  240.         EventDate(final AbsoluteDate date, final boolean increase) {
  241.             this.eventDate = date;
  242.             this.gIncrease = increase;
  243.         }

  244.         /** Getter for event date.
  245.          * @return event date
  246.          */
  247.         public AbsoluteDate getDate() {
  248.             return eventDate;
  249.         }

  250.         /** Getter for g function way at event date.
  251.          * @return g function increasing flag
  252.          */
  253.         public boolean isgIncrease() {
  254.             return gIncrease;
  255.         }

  256.     }

  257. }