FieldAbstractDetector.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.hipparchus.CalculusFieldElement;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.OrekitMessages;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.events.handlers.FieldEventHandler;
  23. import org.orekit.time.FieldAbsoluteDate;

  24. /** Common parts shared by several orbital events finders.
  25.  * @param <D> type of the detector
  26.  * @param <T> type of the field element
  27.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  28.  * @author Luc Maisonobe
  29.  */
  30. public abstract class FieldAbstractDetector<D extends FieldAbstractDetector<D, T>, T extends CalculusFieldElement<T>>
  31.     implements FieldEventDetector<T> {

  32.     /** Default maximum checking interval (s). */
  33.     public static final double DEFAULT_MAXCHECK = FieldEventDetectionSettings.DEFAULT_MAXCHECK;

  34.     /** Default convergence threshold (s). */
  35.     public static final double DEFAULT_THRESHOLD = FieldEventDetectionSettings.DEFAULT_THRESHOLD;

  36.     /** Default maximum number of iterations in the event time search. */
  37.     public static final int DEFAULT_MAX_ITER = FieldEventDetectionSettings.DEFAULT_MAX_ITER;

  38.     /** Detection settings. */
  39.     private final FieldEventDetectionSettings<T> eventDetectionSettings;

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

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

  44.     /** Build a new instance.
  45.      * @param maxCheck maximum checking interval
  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.      * @deprecated as of 12.2
  50.      */
  51.     @Deprecated
  52.     protected FieldAbstractDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold, final int maxIter,
  53.                                     final FieldEventHandler<T> handler) {
  54.         this(new FieldEventDetectionSettings<>(maxCheck, threshold, maxIter), handler);
  55.     }

  56.     /** Build a new instance.
  57.      * @param detectionSettings event detection settings
  58.      * @param handler event handler to call at event occurrences
  59.      * @since 12.2
  60.      */
  61.     protected FieldAbstractDetector(final FieldEventDetectionSettings<T> detectionSettings,
  62.                                     final FieldEventHandler<T> handler) {
  63.         checkStrictlyPositive(detectionSettings.getThreshold().getReal());
  64.         this.eventDetectionSettings = detectionSettings;
  65.         this.handler   = handler;
  66.         this.forward   = true;
  67.     }

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

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public void init(final FieldSpacecraftState<T> s0,
  81.                      final FieldAbsoluteDate<T> t) {
  82.         forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
  83.         getHandler().init(s0, t, this);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public FieldEventDetectionSettings<T> getDetectionSettings() {
  88.         return eventDetectionSettings;
  89.     }

  90.     /** {@inheritDoc} */
  91.     public FieldAdaptableInterval<T> getMaxCheckInterval() {
  92.         return getDetectionSettings().getMaxCheckInterval();
  93.     }

  94.     /** {@inheritDoc} */
  95.     public int getMaxIterationCount() {
  96.         return getDetectionSettings().getMaxIterationCount();
  97.     }

  98.     /** {@inheritDoc} */
  99.     public T getThreshold() {
  100.         return getDetectionSettings().getThreshold();
  101.     }

  102.     /**
  103.      * Set up the maximum checking interval.
  104.      * <p>
  105.      * This will override a maximum checking interval if it has been configured previously.
  106.      * </p>
  107.      * @param newMaxCheck maximum checking interval (s)
  108.      * @return a new detector with updated configuration (the instance is not changed)
  109.      * @since 12.0
  110.      */
  111.     public D withMaxCheck(final double newMaxCheck) {
  112.         return withMaxCheck(FieldAdaptableInterval.of(newMaxCheck));
  113.     }

  114.     /**
  115.      * Set up the maximum checking interval.
  116.      * <p>
  117.      * This will override a maximum checking interval if it has been configured previously.
  118.      * </p>
  119.      * @param newMaxCheck maximum checking interval (s)
  120.      * @return a new detector with updated configuration (the instance is not changed)
  121.      * @since 12.0
  122.      */
  123.     public D withMaxCheck(final FieldAdaptableInterval<T> newMaxCheck) {
  124.         return create(new FieldEventDetectionSettings<>(newMaxCheck, getThreshold(), getMaxIterationCount()), getHandler());
  125.     }

  126.     /**
  127.      * Set up the maximum number of iterations in the event time search.
  128.      * <p>
  129.      * This will override a number of iterations if it has been configured previously.
  130.      * </p>
  131.      * @param newMaxIter maximum number of iterations in the event time search
  132.      * @return a new detector with updated configuration (the instance is not changed)
  133.      * @since 6.1
  134.      */
  135.     public D withMaxIter(final int newMaxIter) {
  136.         return create(new FieldEventDetectionSettings<>(getMaxCheckInterval(), getThreshold(), newMaxIter), getHandler());
  137.     }

  138.     /**
  139.      * Set up the convergence threshold.
  140.      * <p>
  141.      * This will override a convergence threshold if it has been configured previously.
  142.      * </p>
  143.      * @param newThreshold convergence threshold (s)
  144.      * @return a new detector with updated configuration (the instance is not changed)
  145.      * @since 6.1
  146.      */
  147.     public D withThreshold(final T newThreshold) {
  148.         return create(new FieldEventDetectionSettings<>(getMaxCheckInterval(), newThreshold, getMaxIterationCount()), getHandler());
  149.     }

  150.     /**
  151.      * Set up the event detection settings.
  152.      * <p>
  153.      * This will override settings previously configured.
  154.      * </p>
  155.      * @param newSettings new event detection settings
  156.      * @return a new detector with updated configuration (the instance is not changed)
  157.      * @since 12.2
  158.      */
  159.     public D withDetectionSettings(final FieldEventDetectionSettings<T> newSettings) {
  160.         return create(newSettings, getHandler());
  161.     }

  162.     /**
  163.      * Set up the event handler to call at event occurrences.
  164.      * <p>
  165.      * This will override a handler if it has been configured previously.
  166.      * </p>
  167.      * @param newHandler event handler to call at event occurrences
  168.      * @return a new detector with updated configuration (the instance is not changed)
  169.      * @since 6.1
  170.      */
  171.     public D withHandler(final FieldEventHandler<T> newHandler) {
  172.         return create(getDetectionSettings(), newHandler);
  173.     }

  174.     /** {@inheritDoc} */
  175.     public FieldEventHandler<T> getHandler() {
  176.         return handler;
  177.     }

  178.     /** Build a new instance.
  179.      * @param newMaxCheck maximum checking interval
  180.      * @param newThreshold convergence threshold (s)
  181.      * @param newMaxIter maximum number of iterations in the event time search
  182.      * @param newHandler event handler to call at event occurrences
  183.      * @return a new instance of the appropriate sub-type
  184.      * @deprecated as of 12.2
  185.      */
  186.     @Deprecated
  187.     protected abstract D create(FieldAdaptableInterval<T> newMaxCheck, T newThreshold,
  188.                                 int newMaxIter, FieldEventHandler<T> newHandler);

  189.     /** Build a new instance.
  190.      * @param detectionSettings detection settings
  191.      * @param newHandler event handler to call at event occurrences
  192.      * @return a new instance of the appropriate sub-type
  193.      * @since 12.2
  194.      */
  195.     protected D create(final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> newHandler) {
  196.         return create(detectionSettings.getMaxCheckInterval(), detectionSettings.getThreshold(),
  197.             detectionSettings.getMaxIterationCount(), newHandler);
  198.     }

  199.     /** Check if the current propagation is forward or backward.
  200.      * @return true if the current propagation is forward
  201.      * @since 7.2
  202.      */
  203.     public boolean isForward() {
  204.         return forward;
  205.     }

  206. }