AbstractDetector.java

  1. /* Copyright 2002-2017 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.     /** {@inheritDoc} */
  60.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  61.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  62.     }

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

  65.     /** {@inheritDoc} */
  66.     public double getMaxCheckInterval() {
  67.         return maxCheck;
  68.     }

  69.     /** {@inheritDoc} */
  70.     public int getMaxIterationCount() {
  71.         return maxIter;
  72.     }

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

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

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

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

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

  125.     /** Get the handler.
  126.      * @return event handler to call at event occurrences
  127.      */
  128.     public EventHandler<? super T> getHandler() {
  129.         return handler;
  130.     }

  131.     /** {@inheritDoc} */
  132.     public EventHandler.Action eventOccurred(final SpacecraftState s, final boolean increasing)
  133.         throws OrekitException {
  134.         @SuppressWarnings("unchecked")
  135.         final EventHandler.Action whatNext = getHandler().eventOccurred(s, (T) this, increasing);
  136.         return whatNext;
  137.     }

  138.     /** {@inheritDoc} */
  139.     public SpacecraftState resetState(final SpacecraftState oldState) throws OrekitException {
  140.         @SuppressWarnings("unchecked")
  141.         final SpacecraftState newState = getHandler().resetState((T) this, oldState);
  142.         return newState;
  143.     }

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

  153.     /** Check if the current propagation is forward or backward.
  154.      * @return true if the current propagation is forward
  155.      * @since 7.2
  156.      */
  157.     public boolean isForward() {
  158.         return forward;
  159.     }

  160. }