AbstractDetector.java

  1. /* Copyright 2002-2020 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.     public void init(final SpacecraftState s0,
  65.                      final AbsoluteDate t) {
  66.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  67.         getHandler().init(s0, t);
  68.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

  165. }