FunctionalDetector.java

  1. /* Contributed in the public domain.
  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.orekit.errors.OrekitException;
  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
  24.  * {@link GFunction lambda} that can be set using {@link #withGFunction(GFunction)}.
  25.  *
  26.  * <p>For example, to create a simple date detector use:
  27.  *
  28.  * <code><pre>
  29.  * FunctionalDetector d = new FunctionalDetector()
  30.  *     .withGFunction((s)-> s.getDate().durationFrom(triggerDate))
  31.  *     .withMaxCheck(1e10);
  32.  * </pre></code>
  33.  *
  34.  * @author Evan Ward
  35.  * @since 9.2
  36.  */
  37. public class FunctionalDetector extends AbstractDetector<FunctionalDetector> {

  38.     /** Serializable UID. */
  39.     private static final long serialVersionUID = 20180525L;

  40.     /** The g function. */
  41.     private final GFunction gFunction;

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

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

  68.     @Override
  69.     public double g(final SpacecraftState s) throws OrekitException {
  70.         return gFunction.apply(s);
  71.     }

  72.     @Override
  73.     protected FunctionalDetector create(
  74.             final double newMaxCheck,
  75.             final double newThreshold,
  76.             final int newMaxIter,
  77.             final EventHandler<? super FunctionalDetector> newHandler) {

  78.         return new FunctionalDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  79.                 gFunction);
  80.     }

  81.     /**
  82.      * Create a new event detector with a new g function, keeping all other attributes the
  83.      * same. It is recommended to use {@link #withMaxCheck(double)} and
  84.      * {@link #withThreshold(double)} to set appropriate values for this g function.
  85.      *
  86.      * @param newGFunction the new g function.
  87.      * @return a new detector with the new g function.
  88.      */
  89.     public FunctionalDetector withGFunction(final GFunction 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 GFunction getGFunction() {
  99.         return gFunction;
  100.     }

  101.     /** A functional interface for the {@link #g(SpacecraftState)} function. */
  102.     @FunctionalInterface
  103.     public interface GFunction {

  104.         /**
  105.          * Applies this function to the given argument.
  106.          *
  107.          * @param value the function argument
  108.          * @return the function result
  109.          * @throws OrekitException if one is thrown while computing the result.
  110.          */
  111.         double apply(SpacecraftState value) throws OrekitException;

  112.     }

  113. }