DateDetector.java

  1. /* Copyright 2002-2013 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.OrekitException;
  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
  36.  * org.orekit.propagation.events.handlers.EventHandler.Action#STOP stop}
  37.  * propagation at the first event date occurrence. This can be changed by calling
  38.  * {@link #withHandler(EventHandler)} after construction.</p>
  39.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  40.  * @author Luc Maisonobe
  41.  * @author Pascal Parraud
  42.  */
  43. public class DateDetector extends AbstractReconfigurableDetector<DateDetector> implements TimeStamped {

  44.     /** Serializable UID. */
  45.     private static final long serialVersionUID = 20131118L;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  215.     }

  216. }