FieldDateDetector.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.util.ArrayList;

  19. import org.orekit.errors.OrekitIllegalArgumentException;
  20. import org.hipparchus.RealFieldElement;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitMessages;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.propagation.events.handlers.FieldEventHandler;
  25. import org.orekit.propagation.events.handlers.FieldStopOnEvent;
  26. import org.orekit.time.FieldAbsoluteDate;
  27. import org.orekit.time.FieldTimeStamped;

  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 #FieldDateDetector(RealFieldElement, RealFieldElement, FieldTimeStamped...)})</li>
  33.  *   <li>several dates can be added ({@link #addEventDate(FieldAbsoluteDate)})</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.FieldEventHandler.Action#STOP stop}
  38.  * propagation at the first event date occurrence. This can be changed by calling
  39.  * {@link #withHandler(FieldEventHandler)} after construction.</p>
  40.  * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
  41.  * @author Luc Maisonobe
  42.  * @author Pascal Parraud
  43.  */
  44. public class FieldDateDetector<T extends RealFieldElement<T>> extends FieldAbstractDetector<FieldDateDetector<T>, T>
  45.     implements FieldTimeStamped<T> {

  46.     /** Last date for g computation. */
  47.     private FieldAbsoluteDate<T> gDate;

  48.     /** List of event dates. */
  49.     private final ArrayList<FieldEventDate<T>> 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(FieldAbsoluteDate)}.</p>
  55.      * @param maxCheck maximum checking interval (s)
  56.      * @param threshold convergence threshold (s)
  57.      * @param dates list of event dates
  58.      * @see #addEventDate(FieldAbsoluteDate)
  59.      */
  60.     @SafeVarargs
  61.     public FieldDateDetector(final T maxCheck, final T threshold, final FieldTimeStamped<T>... dates) {
  62.         this(maxCheck, threshold, DEFAULT_MAX_ITER, new FieldStopOnEvent<FieldDateDetector<T>, T>(), 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(FieldAbsoluteDate)
  70.      */
  71.     public FieldDateDetector(final FieldAbsoluteDate<T> target) {
  72.         this(target.getField().getZero().add(1.0e10), target.getField().getZero().add(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.      */
  86.     @SafeVarargs
  87.     private FieldDateDetector(final T maxCheck, final T threshold,
  88.                               final int maxIter, final FieldEventHandler<? super FieldDateDetector<T>, T> handler,
  89.                               final FieldTimeStamped<T>... dates) {
  90.         super(maxCheck, threshold, maxIter, handler);
  91.         this.currentIndex  = -1;
  92.         this.gDate         = null;
  93.         this.eventDateList = new ArrayList<FieldDateDetector.FieldEventDate<T>>(dates.length);
  94.         for (final FieldTimeStamped<T> ts : dates) {
  95.             addEventDate(ts.getDate());
  96.         }
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     protected FieldDateDetector<T> create(final T newMaxCheck, final T newThreshold,
  101.                                           final int newMaxIter, final FieldEventHandler<? super FieldDateDetector<T>, T> newHandler) {
  102.         @SuppressWarnings("unchecked")
  103.         final FieldTimeStamped<T>[] dates = eventDateList.toArray(new FieldEventDate[eventDateList.size()]);
  104.         return new FieldDateDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler, dates);
  105.     }

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

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

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

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

  189.     /** Event date specification. */
  190.     private static class FieldEventDate<T extends RealFieldElement<T>> implements FieldTimeStamped<T> {

  191.         /** Event date. */
  192.         private final FieldAbsoluteDate<T> 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.         FieldEventDate(final FieldAbsoluteDate<T> 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 FieldAbsoluteDate<T> 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. }