DateDetector.java

  1. /* Copyright 2002-2020 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 org.hipparchus.ode.events.Action;
  20. import org.orekit.errors.OrekitIllegalArgumentException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.EventHandler;
  24. import org.orekit.propagation.events.handlers.StopOnEvent;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.time.TimeStamped;

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

  43.     /** Last date for g computation. */
  44.     private AbsoluteDate gDate;

  45.     /** List of event dates. */
  46.     private final ArrayList<EventDate> eventDateList;

  47.     /** Current event date. */
  48.     private int currentIndex;

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

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

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

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

  101.     /** Compute the value of the switching function.
  102.      * This function measures the difference between the current and the target date.
  103.      * @param s the current state information: date, kinematics, attitude
  104.      * @return value of the switching function
  105.      */
  106.     public double g(final SpacecraftState s) {
  107.         gDate = s.getDate();
  108.         if (currentIndex < 0) {
  109.             return -1.0;
  110.         } else {
  111.             final EventDate event = getClosest(gDate);
  112.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  113.         }
  114.     }

  115.     /** Get the current event date according to the propagator.
  116.      * @return event date
  117.      */
  118.     public AbsoluteDate getDate() {
  119.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  120.     }

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

  155.     /** Get the closest EventDate to the target date.
  156.      * @param target target date
  157.      * @return current EventDate
  158.      */
  159.     private EventDate getClosest(final AbsoluteDate target) {
  160.         final double dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  161.         if (dt < 0.0 && currentIndex > 0) {
  162.             boolean found = false;
  163.             while (currentIndex > 0 && !found) {
  164.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()) < eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  165.                     currentIndex--;
  166.                 } else {
  167.                     found = true;
  168.                 }
  169.             }
  170.         } else if (dt > 0.0 && currentIndex < eventDateList.size() - 1) {
  171.             final int maxIndex = eventDateList.size() - 1;
  172.             boolean found = false;
  173.             while (currentIndex < maxIndex && !found) {
  174.                 if (target.durationFrom(eventDateList.get(currentIndex + 1).getDate()) > eventDateList.get(currentIndex).getDate().durationFrom(target)) {
  175.                     currentIndex++;
  176.                 } else {
  177.                     found = true;
  178.                 }
  179.             }
  180.         }
  181.         return eventDateList.get(currentIndex);
  182.     }

  183.     /** Event date specification. */
  184.     private static class EventDate implements TimeStamped {

  185.         /** Event date. */
  186.         private final AbsoluteDate eventDate;

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

  189.         /** Simple constructor.
  190.          * @param date date
  191.          * @param increase if true, g function increases around event date
  192.          */
  193.         EventDate(final AbsoluteDate date, final boolean increase) {
  194.             this.eventDate = date;
  195.             this.gIncrease = increase;
  196.         }

  197.         /** Getter for event date.
  198.          * @return event date
  199.          */
  200.         public AbsoluteDate getDate() {
  201.             return eventDate;
  202.         }

  203.         /** Getter for g function way at event date.
  204.          * @return g function increasing flag
  205.          */
  206.         public boolean isgIncrease() {
  207.             return gIncrease;
  208.         }

  209.     }

  210. }