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(s -> 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.      */
  91.     protected DateDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  92.                            final EventHandler handler, final double minGap, final TimeStamped... dates) {
  93.         super(maxCheck, threshold, maxIter, handler);
  94.         this.currentIndex  = -1;
  95.         this.gDate         = null;
  96.         this.eventDateList = new ArrayList<DateDetector.EventDate>(dates.length);
  97.         for (final TimeStamped ts : dates) {
  98.             addEventDate(ts.getDate());
  99.         }
  100.         this.minGap        = minGap;
  101.     }

  102.     /**
  103.      * Setup minimum gap between added dates.
  104.      * @param newMinGap new minimum gap between added dates
  105.      * @return a new detector with updated configuration (the instance is not changed)
  106.      * @since 12.0
  107.      */
  108.     public DateDetector withMinGap(final double newMinGap) {
  109.         return new DateDetector(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(),
  110.                                 getHandler(), newMinGap,
  111.                                 eventDateList.toArray(new EventDate[eventDateList.size()]));
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     protected DateDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
  116.                                   final int newMaxIter, final EventHandler newHandler) {
  117.         return new DateDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, minGap,
  118.                                 eventDateList.toArray(new EventDate[eventDateList.size()]));
  119.     }

  120.     /** Get all event dates currently managed, in chronological order.
  121.      * @return all event dates currently managed, in chronological order
  122.      * @since 11.1
  123.      */
  124.     public List<TimeStamped> getDates() {
  125.         return Collections.unmodifiableList(eventDateList);
  126.     }

  127.     /** Compute the value of the switching function.
  128.      * This function measures the difference between the current and the target date.
  129.      * @param s the current state information: date, kinematics, attitude
  130.      * @return value of the switching function
  131.      */
  132.     public double g(final SpacecraftState s) {
  133.         gDate = s.getDate();
  134.         if (currentIndex < 0) {
  135.             return -1.0;
  136.         } else {
  137.             final EventDate event = getClosest(gDate);
  138.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  139.         }
  140.     }

  141.     /** Get the current event date according to the propagator.
  142.      * @return event date
  143.      */
  144.     public AbsoluteDate getDate() {
  145.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  146.     }

  147.     /** Add an event date.
  148.      * <p>The date to add must be:</p>
  149.      * <ul>
  150.      *   <li>less than the smallest already registered event date minus the maxCheck</li>
  151.      *   <li>or more than the largest already registered event date plus the maxCheck</li>
  152.      * </ul>
  153.      * @param target target date
  154.      * @throws IllegalArgumentException if the date is too close from already defined interval
  155.      * @see #DateDetector(TimeStamped...)
  156.      */
  157.     public void addEventDate(final AbsoluteDate target) throws IllegalArgumentException {
  158.         final boolean increasing;
  159.         if (currentIndex < 0) {
  160.             increasing = (gDate == null) ? true : target.durationFrom(gDate) > 0.0;
  161.             currentIndex = 0;
  162.             eventDateList.add(new EventDate(target, increasing));
  163.         } else {
  164.             final int          lastIndex = eventDateList.size() - 1;
  165.             final AbsoluteDate firstDate = eventDateList.get(0).getDate();
  166.             final AbsoluteDate lastDate  = eventDateList.get(lastIndex).getDate();
  167.             if (firstDate.durationFrom(target) > minGap) {
  168.                 increasing = !eventDateList.get(0).isgIncrease();
  169.                 eventDateList.add(0, new EventDate(target, increasing));
  170.                 currentIndex++;
  171.             } else if (target.durationFrom(lastDate) > minGap) {
  172.                 increasing = !eventDateList.get(lastIndex).isgIncrease();
  173.                 eventDateList.add(new EventDate(target, increasing));
  174.             } else {
  175.                 throw new OrekitIllegalArgumentException(OrekitMessages.EVENT_DATE_TOO_CLOSE,
  176.                                                          target,
  177.                                                          firstDate,
  178.                                                          lastDate,
  179.                                                          minGap,
  180.                                                          firstDate.durationFrom(target),
  181.                                                          target.durationFrom(lastDate));
  182.             }
  183.         }
  184.     }

  185.     /** Get the closest EventDate to the target date.
  186.      * @param target target date
  187.      * @return current EventDate
  188.      */
  189.     private EventDate getClosest(final AbsoluteDate target) {
  190.         final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  191.         if (dt < 0.0 && currentIndex > 0) {
  192.             boolean found = false;
  193.             while (currentIndex > 0 && !found) {
  194.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  195.                     currentIndex--;
  196.                 } else {
  197.                     found = true;
  198.                 }
  199.             }
  200.         } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
  201.             final int maxIndex = eventDateList.size() - 1;
  202.             boolean found = false;
  203.             while (currentIndex < maxIndex && !found) {
  204.                 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  205.                     currentIndex++;
  206.                 } else {
  207.                     found = true;
  208.                 }
  209.             }
  210.         }
  211.         return eventDateList.get(currentIndex);
  212.     }

  213.     /** Event date specification. */
  214.     private static class EventDate implements TimeStamped {

  215.         /** Event date. */
  216.         private final AbsoluteDate eventDate;

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

  219.         /** Simple constructor.
  220.          * @param date date
  221.          * @param increase if true, g function increases around event date
  222.          */
  223.         EventDate(final AbsoluteDate date, final boolean increase) {
  224.             this.eventDate = date;
  225.             this.gIncrease = increase;
  226.         }

  227.         /** Getter for event date.
  228.          * @return event date
  229.          */
  230.         public AbsoluteDate getDate() {
  231.             return eventDate;
  232.         }

  233.         /** Getter for g function way at event date.
  234.          * @return g function increasing flag
  235.          */
  236.         public boolean isgIncrease() {
  237.             return gIncrease;
  238.         }

  239.     }

  240. }