FieldAbstractDetector.java

  1. /* Copyright 2002-2020 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.RealFieldElement;
  19. import org.hipparchus.ode.events.Action;
  20. import org.orekit.propagation.FieldSpacecraftState;
  21. import org.orekit.propagation.events.handlers.FieldEventHandler;
  22. import org.orekit.time.FieldAbsoluteDate;

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

  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 cmaximum number of iterations in the event time search. */
  34.     public static final int DEFAULT_MAX_ITER = 100;

  35.     /** Max check interval. */
  36.     private final T maxCheck;

  37.     /** Convergence threshold. */
  38.     private final T 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 FieldEventHandler<? super D, T> handler;

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

  45.     /** Build a new instance.
  46.      * @param maxCheck maximum checking interval (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 FieldAbstractDetector(final T maxCheck, final T threshold, final int maxIter,
  52.                                     final FieldEventHandler<? super D, T> handler) {
  53.         this.maxCheck  = maxCheck;
  54.         this.threshold = threshold;
  55.         this.maxIter   = maxIter;
  56.         this.handler   = handler;
  57.         this.forward   = true;
  58.     }

  59.     /** {@inheritDoc} */
  60.     public void init(final FieldSpacecraftState<T> s0,
  61.                      final FieldAbsoluteDate<T> t) {
  62.         forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
  63.         getHandler().init(s0, t);
  64.     }

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

  67.     /** {@inheritDoc} */
  68.     public T getMaxCheckInterval() {
  69.         return maxCheck;
  70.     }

  71.     /** {@inheritDoc} */
  72.     public int getMaxIterationCount() {
  73.         return maxIter;
  74.     }

  75.     /** {@inheritDoc} */
  76.     public T getThreshold() {
  77.         return threshold;
  78.     }

  79.     /**
  80.      * Setup the maximum checking interval.
  81.      * <p>
  82.      * This will override a maximum checking interval if it has been configured previously.
  83.      * </p>
  84.      * @param newMaxCheck maximum checking interval (s)
  85.      * @return a new detector with updated configuration (the instance is not changed)
  86.      * @since 6.1
  87.      */
  88.     public D withMaxCheck(final T newMaxCheck) {
  89.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  90.     }

  91.     /**
  92.      * Setup the maximum number of iterations in the event time search.
  93.      * <p>
  94.      * This will override a number of iterations if it has been configured previously.
  95.      * </p>
  96.      * @param newMaxIter maximum number of iterations in the event time search
  97.      * @return a new detector with updated configuration (the instance is not changed)
  98.      * @since 6.1
  99.      */
  100.     public D withMaxIter(final int newMaxIter) {
  101.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  102.     }

  103.     /**
  104.      * Setup the convergence threshold.
  105.      * <p>
  106.      * This will override a convergence threshold if it has been configured previously.
  107.      * </p>
  108.      * @param newThreshold convergence threshold (s)
  109.      * @return a new detector with updated configuration (the instance is not changed)
  110.      * @since 6.1
  111.      */
  112.     public D withThreshold(final T newThreshold) {
  113.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  114.     }

  115.     /**
  116.      * Setup the event handler to call at event occurrences.
  117.      * <p>
  118.      * This will override a handler if it has been configured previously.
  119.      * </p>
  120.      * @param newHandler event handler to call at event occurrences
  121.      * @return a new detector with updated configuration (the instance is not changed)
  122.      * @since 6.1
  123.      */
  124.     public D withHandler(final FieldEventHandler<? super D, T> newHandler) {
  125.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  126.     }

  127.     /** Get the handler.
  128.      * @return event handler to call at event occurrences
  129.      */
  130.     public FieldEventHandler<? super D, T> getHandler() {
  131.         return handler;
  132.     }

  133.     /** {@inheritDoc} */
  134.     public Action eventOccurred(final FieldSpacecraftState<T> s, final boolean increasing) {
  135.         @SuppressWarnings("unchecked")
  136.         final Action whatNext = getHandler().eventOccurred(s, (D) this, increasing);
  137.         return whatNext;
  138.     }

  139.     /** {@inheritDoc} */
  140.     public FieldSpacecraftState<T> resetState(final FieldSpacecraftState<T> oldState) {
  141.         @SuppressWarnings("unchecked")
  142.         final FieldSpacecraftState<T> newState = getHandler().resetState((D) this, oldState);
  143.         return newState;
  144.     }

  145.     /** Build a new instance.
  146.      * @param newMaxCheck maximum checking interval (s)
  147.      * @param newThreshold convergence threshold (s)
  148.      * @param newMaxIter maximum number of iterations in the event time search
  149.      * @param newHandler event handler to call at event occurrences
  150.      * @return a new instance of the appropriate sub-type
  151.      */
  152.     protected abstract D create(T newMaxCheck, T newThreshold,
  153.                                 int newMaxIter, FieldEventHandler<? super D, T> newHandler);

  154.     /** Check if the current propagation is forward or backward.
  155.      * @return true if the current propagation is forward
  156.      * @since 7.2
  157.      */
  158.     public boolean isForward() {
  159.         return forward;
  160.     }

  161. }