AbstractDetector.java

  1. /* Copyright 2002-2024 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.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.time.AbsoluteDate;

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

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

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

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

  35.     /** Max check interval. */
  36.     private final AdaptableInterval 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 handler;

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

  45.     /** Build a new instance.
  46.      * @param maxCheck maximum checking interval, must be strictly positive (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 handler) {
  53.         this(s -> maxCheck, threshold, maxIter, handler);
  54.     }

  55.     /** Build a new instance.
  56.      * @param maxCheck maximum checking interval
  57.      * @param threshold convergence threshold (s)
  58.      * @param maxIter maximum number of iterations in the event time search
  59.      * @param handler event handler to call at event occurrences
  60.      * @since 12.0
  61.      */
  62.     protected AbstractDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  63.                                final EventHandler handler) {
  64.         checkStrictlyPositive(threshold);
  65.         this.maxCheck  = maxCheck;
  66.         this.threshold = threshold;
  67.         this.maxIter   = maxIter;
  68.         this.handler   = handler;
  69.         this.forward   = true;
  70.     }

  71.     /** Check value is strictly positive.
  72.      * @param value value to check
  73.      * @exception OrekitException if value is not strictly positive
  74.      * @since 11.2
  75.      */
  76.     private void checkStrictlyPositive(final double value) throws OrekitException {
  77.         if (value <= 0.0) {
  78.             throw new OrekitException(OrekitMessages.NOT_STRICTLY_POSITIVE, value);
  79.         }
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      *
  84.      * <p> This implementation sets the direction of propagation and initializes the event
  85.      * handler. If a subclass overrides this method it should call {@code
  86.      * super.init(s0, t)}.
  87.      */
  88.     public void init(final SpacecraftState s0,
  89.                      final AbsoluteDate t) {
  90.         forward = t.durationFrom(s0.getDate()) >= 0.0;
  91.         getHandler().init(s0, t, this);
  92.     }

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

  95.     /** {@inheritDoc} */
  96.     public AdaptableInterval getMaxCheckInterval() {
  97.         return maxCheck;
  98.     }

  99.     /** {@inheritDoc} */
  100.     public int getMaxIterationCount() {
  101.         return maxIter;
  102.     }

  103.     /** {@inheritDoc} */
  104.     public double getThreshold() {
  105.         return threshold;
  106.     }

  107.     /**
  108.      * Setup the maximum checking interval.
  109.      * <p>
  110.      * This will override a maximum checking interval if it has been configured previously.
  111.      * </p>
  112.      * @param newMaxCheck maximum checking interval (s)
  113.      * @return a new detector with updated configuration (the instance is not changed)
  114.      * @since 6.1
  115.      */
  116.     public T withMaxCheck(final double newMaxCheck) {
  117.         return withMaxCheck(s -> newMaxCheck);
  118.     }

  119.     /**
  120.      * Setup the maximum checking interval.
  121.      * <p>
  122.      * This will override a maximum checking interval if it has been configured previously.
  123.      * </p>
  124.      * @param newMaxCheck maximum checking interval (s)
  125.      * @return a new detector with updated configuration (the instance is not changed)
  126.      * @since 12.0
  127.      */
  128.     public T withMaxCheck(final AdaptableInterval newMaxCheck) {
  129.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  130.     }

  131.     /**
  132.      * Setup the maximum number of iterations in the event time search.
  133.      * <p>
  134.      * This will override a number of iterations if it has been configured previously.
  135.      * </p>
  136.      * @param newMaxIter maximum number of iterations in the event time search
  137.      * @return a new detector with updated configuration (the instance is not changed)
  138.      * @since 6.1
  139.      */
  140.     public T withMaxIter(final int newMaxIter) {
  141.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  142.     }

  143.     /**
  144.      * Setup the convergence threshold.
  145.      * <p>
  146.      * This will override a convergence threshold if it has been configured previously.
  147.      * </p>
  148.      * @param newThreshold convergence threshold (s)
  149.      * @return a new detector with updated configuration (the instance is not changed)
  150.      * @since 6.1
  151.      */
  152.     public T withThreshold(final double newThreshold) {
  153.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  154.     }

  155.     /**
  156.      * Setup the event handler to call at event occurrences.
  157.      * <p>
  158.      * This will override a handler if it has been configured previously.
  159.      * </p>
  160.      * @param newHandler event handler to call at event occurrences
  161.      * @return a new detector with updated configuration (the instance is not changed)
  162.      * @since 6.1
  163.      */
  164.     public T withHandler(final EventHandler newHandler) {
  165.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  166.     }

  167.     /** {@inheritDoc} */
  168.     @Override
  169.     public EventHandler getHandler() {
  170.         return handler;
  171.     }

  172.     /** Build a new instance.
  173.      * @param newMaxCheck maximum checking interval (s)
  174.      * @param newThreshold convergence threshold (s)
  175.      * @param newMaxIter maximum number of iterations in the event time search
  176.      * @param newHandler event handler to call at event occurrences
  177.      * @return a new instance of the appropriate sub-type
  178.      */
  179.     protected abstract T create(AdaptableInterval newMaxCheck, double newThreshold,
  180.                                 int newMaxIter, EventHandler newHandler);

  181.     /** Check if the current propagation is forward or backward.
  182.      * @return true if the current propagation is forward
  183.      * @since 7.2
  184.      */
  185.     public boolean isForward() {
  186.         return forward;
  187.     }

  188. }