FieldAbstractDetector.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.orekit.propagation.FieldSpacecraftState;
  20. import org.orekit.propagation.events.handlers.FieldEventHandler;
  21. import org.orekit.time.FieldAbsoluteDate;

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

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

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

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

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

  36.     /** Convergence threshold. */
  37.     private final T threshold;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  160. }