FieldAbstractDetector.java

  1. /* Copyright 2002-2025 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.propagation.events.intervals.FieldAdaptableInterval;
  24. import org.orekit.time.FieldAbsoluteDate;

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

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

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

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

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

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

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

  45.     /** Build a new instance.
  46.      * @param detectionSettings event detection settings
  47.      * @param handler event handler to call at event occurrences
  48.      * @since 12.2
  49.      */
  50.     protected FieldAbstractDetector(final FieldEventDetectionSettings<T> detectionSettings,
  51.                                     final FieldEventHandler<T> handler) {
  52.         checkStrictlyPositive(detectionSettings.getThreshold().getReal());
  53.         this.eventDetectionSettings = detectionSettings;
  54.         this.handler   = handler;
  55.         this.forward   = true;
  56.     }

  57.     /**
  58.      * Check if propagation is forward or not.
  59.      * @param <W> field type
  60.      * @param state initial state
  61.      * @param targetDate target propagation date
  62.      * @return forward flag
  63.      * @since 13.0
  64.      */
  65.     public static <W extends CalculusFieldElement<W>> boolean checkIfForward(final FieldSpacecraftState<W> state,
  66.                                                                              final FieldAbsoluteDate<W> targetDate) {
  67.         return targetDate.durationFrom(state.getDate()).getReal() >= 0.0;
  68.     }

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

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
  82.         FieldEventDetector.super.init(s0, t);
  83.         forward = checkIfForward(s0, t);
  84.     }

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

  90.     /**
  91.      * Set up the maximum checking interval.
  92.      * <p>
  93.      * This will override a maximum checking interval if it has been configured previously.
  94.      * </p>
  95.      * @param newMaxCheck maximum checking interval (s)
  96.      * @return a new detector with updated configuration (the instance is not changed)
  97.      * @since 12.0
  98.      */
  99.     public D withMaxCheck(final double newMaxCheck) {
  100.         return withMaxCheck(FieldAdaptableInterval.of(newMaxCheck));
  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 FieldAdaptableInterval<T> newMaxCheck) {
  112.         return withDetectionSettings(new FieldEventDetectionSettings<>(newMaxCheck, getThreshold(), getMaxIterationCount()));
  113.     }

  114.     /**
  115.      * Set up the maximum number of iterations in the event time search.
  116.      * <p>
  117.      * This will override a number of iterations if it has been configured previously.
  118.      * </p>
  119.      * @param newMaxIter maximum number of iterations in the event time search
  120.      * @return a new detector with updated configuration (the instance is not changed)
  121.      * @since 6.1
  122.      */
  123.     public D withMaxIter(final int newMaxIter) {
  124.         return withDetectionSettings(new FieldEventDetectionSettings<>(getMaxCheckInterval(), getThreshold(), newMaxIter));
  125.     }

  126.     /**
  127.      * Set up the convergence threshold.
  128.      * <p>
  129.      * This will override a convergence threshold if it has been configured previously.
  130.      * </p>
  131.      * @param newThreshold convergence threshold (s)
  132.      * @return a new detector with updated configuration (the instance is not changed)
  133.      * @since 6.1
  134.      */
  135.     public D withThreshold(final T newThreshold) {
  136.         return withDetectionSettings(new FieldEventDetectionSettings<>(getMaxCheckInterval(), newThreshold, getMaxIterationCount()));
  137.     }

  138.     /**
  139.      * Set up the event detection settings.
  140.      * <p>
  141.      * This will override settings previously configured.
  142.      * </p>
  143.      * @param newSettings new event detection settings
  144.      * @return a new detector with updated configuration (the instance is not changed)
  145.      * @since 12.2
  146.      */
  147.     public D withDetectionSettings(final FieldEventDetectionSettings<T> newSettings) {
  148.         return create(newSettings, getHandler());
  149.     }

  150.     /**
  151.      * Set up the event handler to call at event occurrences.
  152.      * <p>
  153.      * This will override a handler if it has been configured previously.
  154.      * </p>
  155.      * @param newHandler event handler to call at event occurrences
  156.      * @return a new detector with updated configuration (the instance is not changed)
  157.      * @since 6.1
  158.      */
  159.     public D withHandler(final FieldEventHandler<T> newHandler) {
  160.         return create(getDetectionSettings(), newHandler);
  161.     }

  162.     /** {@inheritDoc} */
  163.     public FieldEventHandler<T> getHandler() {
  164.         return handler;
  165.     }

  166.     /** Build a new instance.
  167.      * @param detectionSettings detection settings
  168.      * @param newHandler event handler to call at event occurrences
  169.      * @return a new instance of the appropriate sub-type
  170.      * @since 12.2
  171.      */
  172.     protected abstract D create(FieldEventDetectionSettings<T> detectionSettings, FieldEventHandler<T> newHandler);

  173.     /** Check if the current propagation is forward or backward.
  174.      * @return true if the current propagation is forward
  175.      * @since 7.2
  176.      */
  177.     public boolean isForward() {
  178.         return forward;
  179.     }

  180. }