FunctionalDetector.java

  1. /* Contributed in the public domain.
  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 java.util.function.ToDoubleFunction;

  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  21. import org.orekit.propagation.events.handlers.EventHandler;

  22. /**
  23.  * A detector that implements the {@link #g(SpacecraftState)} function using a lambda that
  24.  * can be set using {@link #withFunction(ToDoubleFunction)}.
  25.  *
  26.  * <p>For example, to create a simple date detector use:
  27.  *
  28.  * <pre>
  29.  * FunctionalDetector d = new FunctionalDetector()
  30.  *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
  31.  *     .withMaxCheck(1e10);
  32.  * </pre>
  33.  *
  34.  * @author Evan Ward
  35.  * @since 9.2
  36.  */
  37. public class FunctionalDetector extends AbstractDetector<FunctionalDetector> {

  38.     /** The g function. */
  39.     private final ToDoubleFunction<SpacecraftState> function;

  40.     /**
  41.      * Create an event detector with the default values. These are {@link
  42.      * #DEFAULT_MAXCHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
  43.      * ContinueOnEvent}, and a g function that is identically unity.
  44.      */
  45.     public FunctionalDetector() {
  46.         this(AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  47.              new ContinueOnEvent(),
  48.              (ToDoubleFunction<SpacecraftState>) value -> 1.0);
  49.     }

  50.     /**
  51.      * Private constructor.
  52.      *
  53.      * @param maxCheck  maximum checking interval
  54.      * @param threshold convergence threshold (s)
  55.      * @param maxIter   maximum number of iterations in the event time search
  56.      * @param handler   event handler to call at event occurrences
  57.      * @param function  the switching function.
  58.      */
  59.     protected FunctionalDetector(final AdaptableInterval maxCheck,
  60.                                  final double threshold,
  61.                                  final int maxIter,
  62.                                  final EventHandler handler,
  63.                                  final ToDoubleFunction<SpacecraftState> function) {
  64.         super(new EventDetectionSettings(maxCheck, threshold, maxIter), handler);
  65.         this.function = function;
  66.     }


  67.     @Override
  68.     public double g(final SpacecraftState s) {
  69.         return function.applyAsDouble(s);
  70.     }

  71.     @Override
  72.     protected FunctionalDetector create(
  73.             final AdaptableInterval newMaxCheck,
  74.             final double newThreshold,
  75.             final int newMaxIter,
  76.             final EventHandler newHandler) {

  77.         return new FunctionalDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  78.                                       function);
  79.     }

  80.     /**
  81.      * Create a new event detector with a new g function, keeping all other attributes the
  82.      * same. It is recommended to use {@link #withMaxCheck(AdaptableInterval)} and {@link
  83.      * #withThreshold(double)} to set appropriate values for this g function.
  84.      *
  85.      * @param newGFunction the new g function.
  86.      * @return a new detector with the new g function.
  87.      */
  88.     public FunctionalDetector withFunction(
  89.             final ToDoubleFunction<SpacecraftState> newGFunction) {
  90.         return new FunctionalDetector(getMaxCheckInterval(), getThreshold(),
  91.                                       getMaxIterationCount(), getHandler(), newGFunction);
  92.     }

  93.     /**
  94.      * Get the switching function.
  95.      *
  96.      * @return the function used in {@link #g(SpacecraftState)}.
  97.      */
  98.     public ToDoubleFunction<SpacecraftState> getFunction() {
  99.         return function;
  100.     }

  101. }