AbstractDetector.java

  1. /* Copyright 2002-2019 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 org.orekit.propagation.SpacecraftState;
  19. import org.orekit.propagation.events.handlers.EventHandler;
  20. import org.orekit.time.AbsoluteDate;

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

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

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

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

  32.     /** Serializable UID. */
  33.     private static final long serialVersionUID = 20131202l;

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

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

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

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

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

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

  58.     /**
  59.      * {@inheritDoc}
  60.      *
  61.      * <p> This implementation sets the direction of propagation and initializes the event
  62.      * handler. If a subclass overrides this method it should call {@code
  63.      * super.init(s0, t)}.
  64.      */
  65.     public void init(final SpacecraftState s0,
  66.                      final AbsoluteDate t) {
  67.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  68.         getHandler().init(s0, t);
  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 EventHandler.Action eventOccurred(final SpacecraftState s, final boolean increasing) {
  140.         @SuppressWarnings("unchecked")
  141.         final EventHandler.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. }