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 = 600;

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

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

  38.     /** Max check interval. */
  39.     private final FieldAdaptableInterval<T> maxCheck;

  40.     /** Convergence threshold. */
  41.     private final T threshold;

  42.     /** Maximum number of iterations in the event time search. */
  43.     private final int maxIter;

  44.     /** Default handler for event overrides. */
  45.     private final FieldEventHandler<T> handler;

  46.     /** Propagation direction. */
  47.     private boolean forward;

  48.     /** Build a new instance.
  49.      * @param maxCheck maximum checking interval
  50.      * @param threshold convergence threshold (s)
  51.      * @param maxIter maximum number of iterations in the event time search
  52.      * @param handler event handler to call at event occurrences
  53.      */
  54.     protected FieldAbstractDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold, final int maxIter,
  55.                                     final FieldEventHandler<T> handler) {
  56.         checkStrictlyPositive(threshold.getReal());
  57.         this.maxCheck  = maxCheck;
  58.         this.threshold = threshold;
  59.         this.maxIter   = maxIter;
  60.         this.handler   = handler;
  61.         this.forward   = true;
  62.     }

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

  73.     /** {@inheritDoc} */
  74.     public void init(final FieldSpacecraftState<T> s0,
  75.                      final FieldAbsoluteDate<T> t) {
  76.         forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
  77.         getHandler().init(s0, t, this);
  78.     }

  79.     /** {@inheritDoc} */
  80.     public abstract T g(FieldSpacecraftState<T> s);

  81.     /** {@inheritDoc} */
  82.     public FieldAdaptableInterval<T> getMaxCheckInterval() {
  83.         return maxCheck;
  84.     }

  85.     /** {@inheritDoc} */
  86.     public int getMaxIterationCount() {
  87.         return maxIter;
  88.     }

  89.     /** {@inheritDoc} */
  90.     public T getThreshold() {
  91.         return threshold;
  92.     }

  93.     /**
  94.      * Setup the maximum checking interval.
  95.      * <p>
  96.      * This will override a maximum checking interval if it has been configured previously.
  97.      * </p>
  98.      * @param newMaxCheck maximum checking interval (s)
  99.      * @return a new detector with updated configuration (the instance is not changed)
  100.      * @since 12.0
  101.      */
  102.     public D withMaxCheck(final double newMaxCheck) {
  103.         return withMaxCheck(s -> newMaxCheck);
  104.     }

  105.     /**
  106.      * Setup the maximum checking interval.
  107.      * <p>
  108.      * This will override a maximum checking interval if it has been configured previously.
  109.      * </p>
  110.      * @param newMaxCheck maximum checking interval (s)
  111.      * @return a new detector with updated configuration (the instance is not changed)
  112.      * @since 12.0
  113.      */
  114.     public D withMaxCheck(final FieldAdaptableInterval<T> newMaxCheck) {
  115.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  116.     }

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

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

  141.     /**
  142.      * Setup the event handler to call at event occurrences.
  143.      * <p>
  144.      * This will override a handler if it has been configured previously.
  145.      * </p>
  146.      * @param newHandler event handler to call at event occurrences
  147.      * @return a new detector with updated configuration (the instance is not changed)
  148.      * @since 6.1
  149.      */
  150.     public D withHandler(final FieldEventHandler<T> newHandler) {
  151.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  152.     }

  153.     /** {@inheritDoc} */
  154.     public FieldEventHandler<T> getHandler() {
  155.         return handler;
  156.     }

  157.     /** Build a new instance.
  158.      * @param newMaxCheck maximum checking interval
  159.      * @param newThreshold convergence threshold (s)
  160.      * @param newMaxIter maximum number of iterations in the event time search
  161.      * @param newHandler event handler to call at event occurrences
  162.      * @return a new instance of the appropriate sub-type
  163.      */
  164.     protected abstract D create(FieldAdaptableInterval<T> newMaxCheck, T newThreshold,
  165.                                 int newMaxIter, FieldEventHandler<T> newHandler);

  166.     /** Check if the current propagation is forward or backward.
  167.      * @return true if the current propagation is forward
  168.      * @since 7.2
  169.      */
  170.     public boolean isForward() {
  171.         return forward;
  172.     }

  173. }