FieldDateDetector.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 org.hipparchus.ode.events.Action;
  20. import org.orekit.errors.OrekitIllegalArgumentException;
  21. import org.hipparchus.CalculusFieldElement;
  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(CalculusFieldElement, CalculusFieldElement, 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 Action#STOP stop}
  37.  * propagation at the first event date occurrence. This can be changed by calling
  38.  * {@link #withHandler(FieldEventHandler)} after construction.</p>
  39.  * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
  40.  * @author Luc Maisonobe
  41.  * @author Pascal Parraud
  42.  */
  43. public class FieldDateDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldDateDetector<T>, T>
  44.     implements FieldTimeStamped<T> {

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

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

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     protected FieldDateDetector<T> create(final T newMaxCheck, final T newThreshold,
  100.                                           final int newMaxIter, final FieldEventHandler<? super FieldDateDetector<T>, T> newHandler) {
  101.         @SuppressWarnings("unchecked")
  102.         final FieldTimeStamped<T>[] dates = eventDateList.toArray(new FieldEventDate[eventDateList.size()]);
  103.         return new FieldDateDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler, dates);
  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.      */
  110.     public T g(final FieldSpacecraftState<T> s) {
  111.         gDate = s.getDate();
  112.         if (currentIndex < 0) {
  113.             return s.getA().getField().getZero().add(-1);
  114.         } else {
  115.             final FieldEventDate<T> 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 FieldAbsoluteDate<T> 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 #FieldDateDetector(CalculusFieldElement, CalculusFieldElement, FieldTimeStamped...)
  134.      */
  135.     public void addEventDate(final FieldAbsoluteDate<T> target) throws IllegalArgumentException {
  136.         final boolean increasing;
  137.         if (currentIndex < 0) {
  138.             increasing = (gDate == null) ? true : target.durationFrom(gDate).getReal() > 0.0;
  139.             currentIndex = 0;
  140.             eventDateList.add(new FieldEventDate<>(target, increasing));
  141.         } else {
  142.             final int lastIndex = eventDateList.size() - 1;
  143.             if (eventDateList.get(0).getDate().durationFrom(target).getReal() > getMaxCheckInterval().getReal()) {
  144.                 increasing = !eventDateList.get(0).isgIncrease();
  145.                 eventDateList.add(0, new FieldEventDate<>(target, increasing));
  146.                 currentIndex++;
  147.             } else if (target.durationFrom(eventDateList.get(lastIndex).getDate()).getReal() > getMaxCheckInterval().getReal()) {
  148.                 increasing = !eventDateList.get(lastIndex).isgIncrease();
  149.                 eventDateList.add(new FieldEventDate<>(target, increasing));
  150.             } else {
  151.                 throw new OrekitIllegalArgumentException(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 FieldEventDate<T> getClosest(final FieldAbsoluteDate<T> target) {
  164.         final T dt = target.durationFrom(eventDateList.get(currentIndex).getDate());
  165.         if (dt.getReal() < 0.0 && currentIndex > 0) {
  166.             boolean found = false;
  167.             while (currentIndex > 0 && !found) {
  168.                 if (target.durationFrom(eventDateList.get(currentIndex - 1).getDate()).getReal() < eventDateList.get(currentIndex).getDate().durationFrom(target).getReal()) {
  169.                     currentIndex--;
  170.                 } else {
  171.                     found = true;
  172.                 }
  173.             }
  174.         } else if (dt.getReal() > 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()).getReal() > eventDateList.get(currentIndex).getDate().durationFrom(target).getReal()) {
  179.                     currentIndex++;
  180.                 } else {
  181.                     found = true;
  182.                 }
  183.             }
  184.         }
  185.         return eventDateList.get(currentIndex);
  186.     }

  187.     /** Event date specification. */
  188.     private static class FieldEventDate<T extends CalculusFieldElement<T>> implements FieldTimeStamped<T> {

  189.         /** Event date. */
  190.         private final FieldAbsoluteDate<T> eventDate;

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

  193.         /** Simple constructor.
  194.          * @param date date
  195.          * @param increase if true, g function increases around event date
  196.          */
  197.         FieldEventDate(final FieldAbsoluteDate<T> date, final boolean increase) {
  198.             this.eventDate = date;
  199.             this.gIncrease = increase;
  200.         }

  201.         /** Getter for event date.
  202.          * @return event date
  203.          */
  204.         public FieldAbsoluteDate<T> getDate() {
  205.             return eventDate;
  206.         }

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

  213.     }

  214. }