DateDetector.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.io.Serializable;
  19. import java.util.ArrayList;

  20. import org.orekit.errors.OrekitIllegalArgumentException;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.propagation.events.handlers.StopOnEvent;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.time.TimeStamped;

  28. /** Finder for date events.
  29.  * <p>This class finds date events (i.e. occurrence of some predefined dates).</p>
  30.  * <p>As of version 5.1, it is an enhanced date detector:</p>
  31.  * <ul>
  32.  *   <li>it can be defined without prior date ({@link #DateDetector(double, double, TimeStamped...)})</li>
  33.  *   <li>several dates can be added ({@link #addEventDate(AbsoluteDate)})</li>
  34.  * </ul>
  35.  * <p>The gap between the added dates must be more than the maxCheck.</p>
  36.  * <p>The default implementation behavior is to {@link
  37.  * org.orekit.propagation.events.handlers.EventHandler.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.     /** Serializable UID. */
  46.     private static final long serialVersionUID = 20131118L;

  47.     /** Last date for g computation. */
  48.     private AbsoluteDate gDate;

  49.     /** List of event dates. */
  50.     private final ArrayList<EventDate> eventDateList;

  51.     /** Current event date. */
  52.     private int currentIndex;

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

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

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

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

  105.     /** Compute the value of the switching function.
  106.      * This function measures the difference between the current and the target date.
  107.      * @param s the current state information: date, kinematics, attitude
  108.      * @return value of the switching function
  109.      * @exception OrekitException if some specific error occurs
  110.      */
  111.     public double g(final SpacecraftState s) throws OrekitException {
  112.         gDate = s.getDate();
  113.         if (currentIndex < 0) {
  114.             return -1.0;
  115.         } else {
  116.             final EventDate event = getClosest(gDate);
  117.             return event.isgIncrease() ? gDate.durationFrom(event.getDate()) : event.getDate().durationFrom(gDate);
  118.         }
  119.     }

  120.     /** Get the current event date according to the propagator.
  121.      * @return event date
  122.      */
  123.     public AbsoluteDate getDate() {
  124.         return currentIndex < 0 ? null : eventDateList.get(currentIndex).getDate();
  125.     }

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

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

  188.     /** Event date specification. */
  189.     private static class EventDate implements Serializable, TimeStamped {

  190.         /** Serializable UID. */
  191.         private static final long serialVersionUID = -7641032576122527149L;

  192.         /** Event date. */
  193.         private final AbsoluteDate eventDate;

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

  196.         /** Simple constructor.
  197.          * @param date date
  198.          * @param increase if true, g function increases around event date
  199.          */
  200.         EventDate(final AbsoluteDate date, final boolean increase) {
  201.             this.eventDate = date;
  202.             this.gIncrease = increase;
  203.         }

  204.         /** Getter for event date.
  205.          * @return event date
  206.          */
  207.         public AbsoluteDate getDate() {
  208.             return eventDate;
  209.         }

  210.         /** Getter for g function way at event date.
  211.          * @return g function increasing flag
  212.          */
  213.         public boolean isgIncrease() {
  214.             return gIncrease;
  215.         }

  216.     }

  217. }