DateDetector.java

  1. /* Copyright 2002-2022 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(double, double, 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 maxCheck.</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.     /** Last date for g computation. */
  46.     private AbsoluteDate gDate;

  47.     /** List of event dates. */
  48.     private final ArrayList<EventDate> eventDateList;

  49.     /** Current event date. */
  50.     private int currentIndex;

  51.     /** Build a new instance.
  52.      * <p>First event dates are set here, but others can be
  53.      * added later with {@link #addEventDate(AbsoluteDate)}.</p>
  54.      * @param maxCheck maximum checking interval (s)
  55.      * @param threshold convergence threshold (s)
  56.      * @param dates list of event dates
  57.      * @see #addEventDate(AbsoluteDate)
  58.      */
  59.     public DateDetector(final double maxCheck, final double threshold, final TimeStamped... dates) {
  60.         this(maxCheck, threshold, DEFAULT_MAX_ITER, new StopOnEvent<DateDetector>(), dates);
  61.     }

  62.     /** Build a new instance.
  63.      * <p>This constructor is dedicated to single date detection.
  64.      * {@link #getMaxCheckInterval() max check interval} is set to 1.0e10, so almost
  65.      * no other date can be added. Tolerance is set to 1.0e-9.</p>
  66.      * @param target target date
  67.      * @see #addEventDate(AbsoluteDate)
  68.      */
  69.     public DateDetector(final AbsoluteDate target) {
  70.         this(1.0e10, 1.e-9, target);
  71.     }

  72.     /** Private constructor with full parameters.
  73.      * <p>
  74.      * This constructor is private as users are expected to use the builder
  75.      * API with the various {@code withXxx()} methods to set up the instance
  76.      * in a readable manner without using a huge amount of parameters.
  77.      * </p>
  78.      * @param maxCheck maximum checking interval (s)
  79.      * @param threshold convergence threshold (s)
  80.      * @param maxIter maximum number of iterations in the event time search
  81.      * @param handler event handler to call at event occurrences
  82.      * @param dates list of event dates
  83.      * @since 6.1
  84.      */
  85.     private DateDetector(final double maxCheck, final double threshold,
  86.                          final int maxIter, final EventHandler<? super DateDetector> handler,
  87.                          final TimeStamped... dates) {
  88.         super(maxCheck, threshold, maxIter, handler);
  89.         this.currentIndex  = -1;
  90.         this.gDate         = null;
  91.         this.eventDateList = new ArrayList<DateDetector.EventDate>(dates.length);
  92.         for (final TimeStamped ts : dates) {
  93.             addEventDate(ts.getDate());
  94.         }
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     protected DateDetector create(final double newMaxCheck, final double newThreshold,
  99.                                   final int newMaxIter, final EventHandler<? super DateDetector> newHandler) {
  100.         return new DateDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  101.                                 eventDateList.toArray(new EventDate[eventDateList.size()]));
  102.     }

  103.     /** Get all event dates currently managed, in chronological order.
  104.      * @return all event dates currently managed, in chronological order
  105.      * @since 11.1
  106.      */
  107.     public List<TimeStamped> getDates() {
  108.         return Collections.unmodifiableList(eventDateList);
  109.     }

  110.     /** Compute the value of the switching function.
  111.      * This function measures the difference between the current and the target date.
  112.      * @param s the current state information: date, kinematics, attitude
  113.      * @return value of the switching function
  114.      */
  115.     public double g(final SpacecraftState s) {
  116.         gDate = s.getDate();
  117.         if (currentIndex < 0) {
  118.             return -1.0;
  119.         } else {
  120.             final EventDate event = getClosest(gDate);
  121.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  122.         }
  123.     }

  124.     /** Get the current event date according to the propagator.
  125.      * @return event date
  126.      */
  127.     public AbsoluteDate getDate() {
  128.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  129.     }

  130.     /** Add an event date.
  131.      * <p>The date to add must be:</p>
  132.      * <ul>
  133.      *   <li>less than the smallest already registered event date minus the maxCheck</li>
  134.      *   <li>or more than the largest already registered event date plus the maxCheck</li>
  135.      * </ul>
  136.      * @param target target date
  137.      * @throws IllegalArgumentException if the date is too close from already defined interval
  138.      * @see #DateDetector(double, double, TimeStamped...)
  139.      */
  140.     public void addEventDate(final AbsoluteDate target) throws IllegalArgumentException {
  141.         final boolean increasing;
  142.         if (currentIndex < 0) {
  143.             increasing = (gDate == null) ? true : target.durationFrom(gDate) > 0.0;
  144.             currentIndex = 0;
  145.             eventDateList.add(new EventDate(target, increasing));
  146.         } else {
  147.             final int lastIndex = eventDateList.size() - 1;
  148.             final AbsoluteDate firstDate = eventDateList.get(0).getDate();
  149.             final AbsoluteDate lastDate = eventDateList.get(lastIndex).getDate();
  150.             if (firstDate.durationFrom(target) > getMaxCheckInterval()) {
  151.                 increasing = !eventDateList.get(0).isgIncrease();
  152.                 eventDateList.add(0, new EventDate(target, increasing));
  153.                 currentIndex++;
  154.             } else if (target.durationFrom(lastDate) > getMaxCheckInterval()) {
  155.                 increasing = !eventDateList.get(lastIndex).isgIncrease();
  156.                 eventDateList.add(new EventDate(target, increasing));
  157.             } else {
  158.                 throw new OrekitIllegalArgumentException(OrekitMessages.EVENT_DATE_TOO_CLOSE,
  159.                                                          target,
  160.                                                          firstDate,
  161.                                                          lastDate,
  162.                                                          getMaxCheckInterval(),
  163.                                                          firstDate.durationFrom(target),
  164.                                                          target.durationFrom(lastDate));
  165.             }
  166.         }
  167.     }

  168.     /** Get the closest EventDate to the target date.
  169.      * @param target target date
  170.      * @return current EventDate
  171.      */
  172.     private EventDate getClosest(final AbsoluteDate target) {
  173.         final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  174.         if (dt < 0.0 && currentIndex > 0) {
  175.             boolean found = false;
  176.             while (currentIndex > 0 && !found) {
  177.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  178.                     currentIndex--;
  179.                 } else {
  180.                     found = true;
  181.                 }
  182.             }
  183.         } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
  184.             final int maxIndex = eventDateList.size() - 1;
  185.             boolean found = false;
  186.             while (currentIndex < maxIndex && !found) {
  187.                 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  188.                     currentIndex++;
  189.                 } else {
  190.                     found = true;
  191.                 }
  192.             }
  193.         }
  194.         return eventDateList.get(currentIndex);
  195.     }

  196.     /** Event date specification. */
  197.     private static class EventDate implements TimeStamped {

  198.         /** Event date. */
  199.         private final AbsoluteDate eventDate;

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

  202.         /** Simple constructor.
  203.          * @param date date
  204.          * @param increase if true, g function increases around event date
  205.          */
  206.         EventDate(final AbsoluteDate date, final boolean increase) {
  207.             this.eventDate = date;
  208.             this.gIncrease = increase;
  209.         }

  210.         /** Getter for event date.
  211.          * @return event date
  212.          */
  213.         public AbsoluteDate getDate() {
  214.             return eventDate;
  215.         }

  216.         /** Getter for g function way at event date.
  217.          * @return g function increasing flag
  218.          */
  219.         public boolean isgIncrease() {
  220.             return gIncrease;
  221.         }

  222.     }

  223. }