AbstractDetector.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 org.hipparchus.ode.events.Action;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.OrekitMessages;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.handlers.EventHandler;
  23. import org.orekit.time.AbsoluteDate;

  24. /** Common parts shared by several orbital events finders.
  25.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  26.  * @author Luc Maisonobe
  27.  */
  28. public abstract class AbstractDetector<T extends AbstractDetector<T>> implements EventDetector {

  29.     /** Default maximum checking interval (s). */
  30.     public static final double DEFAULT_MAXCHECK = 600;

  31.     /** Default convergence threshold (s). */
  32.     public static final double DEFAULT_THRESHOLD = 1.e-6;

  33.     /** Default maximum number of iterations in the event time search. */
  34.     public static final int DEFAULT_MAX_ITER = 100;

  35.     /** Max check interval. */
  36.     private final double maxCheck;

  37.     /** Convergence threshold. */
  38.     private final double threshold;

  39.     /** Maximum number of iterations in the event time search. */
  40.     private final int maxIter;

  41.     /** Default handler for event overrides. */
  42.     private final EventHandler<? super T> handler;

  43.     /** Propagation direction. */
  44.     private boolean forward;

  45.     /** Build a new instance.
  46.      * @param maxCheck maximum checking interval, must be strictly positive (s)
  47.      * @param threshold convergence threshold (s)
  48.      * @param maxIter maximum number of iterations in the event time search
  49.      * @param handler event handler to call at event occurrences
  50.      */
  51.     protected AbstractDetector(final double maxCheck, final double threshold, final int maxIter,
  52.                                final EventHandler<? super T> handler) {
  53.         checkStrictlyPositive(maxCheck);
  54.         checkStrictlyPositive(threshold);
  55.         this.maxCheck  = maxCheck;
  56.         this.threshold = threshold;
  57.         this.maxIter   = maxIter;
  58.         this.handler   = handler;
  59.         this.forward   = true;
  60.     }

  61.     /** Check value is strictly positive.
  62.      * @param value value to check
  63.      * @exception OrekitException if value is not strictly positive
  64.      * @since 11.2
  65.      */
  66.     private void checkStrictlyPositive(final double value) throws OrekitException {
  67.         if (value <= 0.0) {
  68.             throw new OrekitException(OrekitMessages.NOT_STRICTLY_POSITIVE, value);
  69.         }
  70.     }

  71.     /**
  72.      * {@inheritDoc}
  73.      *
  74.      * <p> This implementation sets the direction of propagation and initializes the event
  75.      * handler. If a subclass overrides this method it should call {@code
  76.      * super.init(s0, t)}.
  77.      */
  78.     @SuppressWarnings("unchecked")
  79.     public void init(final SpacecraftState s0,
  80.                      final AbsoluteDate t) {
  81.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  82.         getHandler().init(s0, t, (T) this);
  83.     }

  84.     /** {@inheritDoc} */
  85.     public abstract double g(SpacecraftState s);

  86.     /** {@inheritDoc} */
  87.     public double getMaxCheckInterval() {
  88.         return maxCheck;
  89.     }

  90.     /** {@inheritDoc} */
  91.     public int getMaxIterationCount() {
  92.         return maxIter;
  93.     }

  94.     /** {@inheritDoc} */
  95.     public double getThreshold() {
  96.         return threshold;
  97.     }

  98.     /**
  99.      * Setup the maximum checking interval.
  100.      * <p>
  101.      * This will override a maximum checking interval if it has been configured previously.
  102.      * </p>
  103.      * @param newMaxCheck maximum checking interval (s)
  104.      * @return a new detector with updated configuration (the instance is not changed)
  105.      * @since 6.1
  106.      */
  107.     public T withMaxCheck(final double newMaxCheck) {
  108.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  109.     }

  110.     /**
  111.      * Setup the maximum number of iterations in the event time search.
  112.      * <p>
  113.      * This will override a number of iterations if it has been configured previously.
  114.      * </p>
  115.      * @param newMaxIter maximum number of iterations in the event time search
  116.      * @return a new detector with updated configuration (the instance is not changed)
  117.      * @since 6.1
  118.      */
  119.     public T withMaxIter(final int newMaxIter) {
  120.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  121.     }

  122.     /**
  123.      * Setup the convergence threshold.
  124.      * <p>
  125.      * This will override a convergence threshold if it has been configured previously.
  126.      * </p>
  127.      * @param newThreshold convergence threshold (s)
  128.      * @return a new detector with updated configuration (the instance is not changed)
  129.      * @since 6.1
  130.      */
  131.     public T withThreshold(final double newThreshold) {
  132.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  133.     }

  134.     /**
  135.      * Setup the event handler to call at event occurrences.
  136.      * <p>
  137.      * This will override a handler if it has been configured previously.
  138.      * </p>
  139.      * @param newHandler event handler to call at event occurrences
  140.      * @return a new detector with updated configuration (the instance is not changed)
  141.      * @since 6.1
  142.      */
  143.     public T withHandler(final EventHandler<? super T> newHandler) {
  144.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  145.     }

  146.     /** Get the handler.
  147.      * @return event handler to call at event occurrences
  148.      */
  149.     public EventHandler<? super T> getHandler() {
  150.         return handler;
  151.     }

  152.     /** {@inheritDoc} */
  153.     public Action eventOccurred(final SpacecraftState s, final boolean increasing) {
  154.         @SuppressWarnings("unchecked")
  155.         final Action whatNext = getHandler().eventOccurred(s, (T) this, increasing);
  156.         return whatNext;
  157.     }

  158.     /** {@inheritDoc} */
  159.     public SpacecraftState resetState(final SpacecraftState oldState) {
  160.         @SuppressWarnings("unchecked")
  161.         final SpacecraftState newState = getHandler().resetState((T) this, oldState);
  162.         return newState;
  163.     }

  164.     /** Build a new instance.
  165.      * @param newMaxCheck maximum checking interval (s)
  166.      * @param newThreshold convergence threshold (s)
  167.      * @param newMaxIter maximum number of iterations in the event time search
  168.      * @param newHandler event handler to call at event occurrences
  169.      * @return a new instance of the appropriate sub-type
  170.      */
  171.     protected abstract T create(double newMaxCheck, double newThreshold,
  172.                                 int newMaxIter, EventHandler<? super T> newHandler);

  173.     /** Check if the current propagation is forward or backward.
  174.      * @return true if the current propagation is forward
  175.      * @since 7.2
  176.      */
  177.     public boolean isForward() {
  178.         return forward;
  179.     }

  180. }