AbstractDetector.java

  1. /* Copyright 2002-2018 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.errors.OrekitException;
  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 EventDetector> 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 cmaximum number of iterations in the event time search. */
  32.     public static final int DEFAULT_MAX_ITER = 100;

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

  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 (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.         this.maxCheck  = maxCheck;
  54.         this.threshold = threshold;
  55.         this.maxIter   = maxIter;
  56.         this.handler   = handler;
  57.         this.forward   = true;
  58.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  168. }