FieldAbstractDetector.java

  1. /* Copyright 2002-2022 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.hipparchus.ode.events.Action;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.propagation.FieldSpacecraftState;
  23. import org.orekit.propagation.events.handlers.FieldEventHandler;
  24. import org.orekit.time.FieldAbsoluteDate;

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

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

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

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

  37.     /** Max check interval. */
  38.     private final T maxCheck;

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

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

  43.     /** Default handler for event overrides. */
  44.     private final FieldEventHandler<? super D, T> handler;

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

  47.     /** Build a new instance.
  48.      * @param maxCheck maximum checking interval (s)
  49.      * @param threshold convergence threshold (s)
  50.      * @param maxIter maximum number of iterations in the event time search
  51.      * @param handler event handler to call at event occurrences
  52.      */
  53.     protected FieldAbstractDetector(final T maxCheck, final T threshold, final int maxIter,
  54.                                     final FieldEventHandler<? super D, T> handler) {
  55.         checkStrictlyPositive(maxCheck.getReal());
  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.     @SuppressWarnings("unchecked")
  75.     public void init(final FieldSpacecraftState<T> s0,
  76.                      final FieldAbsoluteDate<T> t) {
  77.         forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
  78.         getHandler().init(s0, t, (D) this);
  79.     }

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

  82.     /** {@inheritDoc} */
  83.     public T getMaxCheckInterval() {
  84.         return maxCheck;
  85.     }

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

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

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

  106.     /**
  107.      * Setup the maximum number of iterations in the event time search.
  108.      * <p>
  109.      * This will override a number of iterations if it has been configured previously.
  110.      * </p>
  111.      * @param newMaxIter maximum number of iterations in the event time search
  112.      * @return a new detector with updated configuration (the instance is not changed)
  113.      * @since 6.1
  114.      */
  115.     public D withMaxIter(final int newMaxIter) {
  116.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  117.     }

  118.     /**
  119.      * Setup the convergence threshold.
  120.      * <p>
  121.      * This will override a convergence threshold if it has been configured previously.
  122.      * </p>
  123.      * @param newThreshold convergence threshold (s)
  124.      * @return a new detector with updated configuration (the instance is not changed)
  125.      * @since 6.1
  126.      */
  127.     public D withThreshold(final T newThreshold) {
  128.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  129.     }

  130.     /**
  131.      * Setup the event handler to call at event occurrences.
  132.      * <p>
  133.      * This will override a handler if it has been configured previously.
  134.      * </p>
  135.      * @param newHandler event handler to call at event occurrences
  136.      * @return a new detector with updated configuration (the instance is not changed)
  137.      * @since 6.1
  138.      */
  139.     public D withHandler(final FieldEventHandler<? super D, T> newHandler) {
  140.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  141.     }

  142.     /** Get the handler.
  143.      * @return event handler to call at event occurrences
  144.      */
  145.     public FieldEventHandler<? super D, T> getHandler() {
  146.         return handler;
  147.     }

  148.     /** {@inheritDoc} */
  149.     public Action eventOccurred(final FieldSpacecraftState<T> s, final boolean increasing) {
  150.         @SuppressWarnings("unchecked")
  151.         final Action whatNext = getHandler().eventOccurred(s, (D) this, increasing);
  152.         return whatNext;
  153.     }

  154.     /** {@inheritDoc} */
  155.     public FieldSpacecraftState<T> resetState(final FieldSpacecraftState<T> oldState) {
  156.         @SuppressWarnings("unchecked")
  157.         final FieldSpacecraftState<T> newState = getHandler().resetState((D) this, oldState);
  158.         return newState;
  159.     }

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

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

  176. }