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.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.EventHandler;
  21. import org.orekit.time.AbsoluteDate;

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

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

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

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

  33.     /** Max check interval. */
  34.     private final double maxCheck;

  35.     /** Convergence threshold. */
  36.     private final double threshold;

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

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

  41.     /** Propagation direction. */
  42.     private boolean forward;

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

  57.     /**
  58.      * {@inheritDoc}
  59.      *
  60.      * <p> This implementation sets the direction of propagation and initializes the event
  61.      * handler. If a subclass overrides this method it should call {@code
  62.      * super.init(s0, t)}.
  63.      */
  64.     @SuppressWarnings("unchecked")
  65.     public void init(final SpacecraftState s0,
  66.                      final AbsoluteDate t) {
  67.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  68.         getHandler().init(s0, t, (T) this);
  69.     }

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

  72.     /** {@inheritDoc} */
  73.     public double getMaxCheckInterval() {
  74.         return maxCheck;
  75.     }

  76.     /** {@inheritDoc} */
  77.     public int getMaxIterationCount() {
  78.         return maxIter;
  79.     }

  80.     /** {@inheritDoc} */
  81.     public double getThreshold() {
  82.         return threshold;
  83.     }

  84.     /**
  85.      * Setup the maximum checking interval.
  86.      * <p>
  87.      * This will override a maximum checking interval if it has been configured previously.
  88.      * </p>
  89.      * @param newMaxCheck maximum checking interval (s)
  90.      * @return a new detector with updated configuration (the instance is not changed)
  91.      * @since 6.1
  92.      */
  93.     public T withMaxCheck(final double newMaxCheck) {
  94.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  95.     }

  96.     /**
  97.      * Setup the maximum number of iterations in the event time search.
  98.      * <p>
  99.      * This will override a number of iterations if it has been configured previously.
  100.      * </p>
  101.      * @param newMaxIter maximum number of iterations in the event time search
  102.      * @return a new detector with updated configuration (the instance is not changed)
  103.      * @since 6.1
  104.      */
  105.     public T withMaxIter(final int newMaxIter) {
  106.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  107.     }

  108.     /**
  109.      * Setup the convergence threshold.
  110.      * <p>
  111.      * This will override a convergence threshold if it has been configured previously.
  112.      * </p>
  113.      * @param newThreshold convergence threshold (s)
  114.      * @return a new detector with updated configuration (the instance is not changed)
  115.      * @since 6.1
  116.      */
  117.     public T withThreshold(final double newThreshold) {
  118.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  119.     }

  120.     /**
  121.      * Setup the event handler to call at event occurrences.
  122.      * <p>
  123.      * This will override a handler if it has been configured previously.
  124.      * </p>
  125.      * @param newHandler event handler to call at event occurrences
  126.      * @return a new detector with updated configuration (the instance is not changed)
  127.      * @since 6.1
  128.      */
  129.     public T withHandler(final EventHandler<? super T> newHandler) {
  130.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  131.     }

  132.     /** Get the handler.
  133.      * @return event handler to call at event occurrences
  134.      */
  135.     public EventHandler<? super T> getHandler() {
  136.         return handler;
  137.     }

  138.     /** {@inheritDoc} */
  139.     public Action eventOccurred(final SpacecraftState s, final boolean increasing) {
  140.         @SuppressWarnings("unchecked")
  141.         final Action whatNext = getHandler().eventOccurred(s, (T) this, increasing);
  142.         return whatNext;
  143.     }

  144.     /** {@inheritDoc} */
  145.     public SpacecraftState resetState(final SpacecraftState oldState) {
  146.         @SuppressWarnings("unchecked")
  147.         final SpacecraftState newState = getHandler().resetState((T) this, oldState);
  148.         return newState;
  149.     }

  150.     /** Build a new instance.
  151.      * @param newMaxCheck maximum checking interval (s)
  152.      * @param newThreshold convergence threshold (s)
  153.      * @param newMaxIter maximum number of iterations in the event time search
  154.      * @param newHandler event handler to call at event occurrences
  155.      * @return a new instance of the appropriate sub-type
  156.      */
  157.     protected abstract T create(double newMaxCheck, double newThreshold,
  158.                                 int newMaxIter, EventHandler<? super T> newHandler);

  159.     /** Check if the current propagation is forward or backward.
  160.      * @return true if the current propagation is forward
  161.      * @since 7.2
  162.      */
  163.     public boolean isForward() {
  164.         return forward;
  165.     }

  166. }