FieldFunctionalDetector.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.Function;

  19. import org.hipparchus.Field;
  20. import org.hipparchus.RealFieldElement;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  23. import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
  24. import org.orekit.propagation.events.handlers.FieldEventHandler;

  25. /**
  26.  * A detector that implements the {@link #g(FieldSpacecraftState)} function using a lambda
  27.  * that can be set using {@link #withFunction(Function)}.
  28.  *
  29.  * <p>For example, to create a simple date detector use:
  30.  *
  31.  * <pre>
  32.  * FieldFunctionalDetector&lt;T&gt; d = new FieldFunctionalDetector&lt;&gt;(field)
  33.  *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
  34.  *     .withMaxCheck(field.getZero().add(1e10));
  35.  * </pre>
  36.  *
  37.  * @param <T> the type of numbers this detector uses.
  38.  * @author Evan Ward
  39.  * @since 10.2
  40.  */
  41. public class FieldFunctionalDetector<T extends RealFieldElement<T>>
  42.         extends FieldAbstractDetector<FieldFunctionalDetector<T>, T> {

  43.     /** The g function. */
  44.     private final Function<FieldSpacecraftState<T>, T> function;

  45.     /**
  46.      * Create an event detector with the default values. These are {@link
  47.      * #DEFAULT_MAXCHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
  48.      * ContinueOnEvent}, and a g function that is identically unity.
  49.      *
  50.      * @param field on which this detector is defined.
  51.      */
  52.     public FieldFunctionalDetector(final Field<T> field) {
  53.         this(field.getZero().add(DEFAULT_MAXCHECK),
  54.                 field.getZero().add(DEFAULT_THRESHOLD),
  55.                 DEFAULT_MAX_ITER,
  56.                 new FieldContinueOnEvent<>(), value -> field.getOne());
  57.     }

  58.     /**
  59.      * Private constructor.
  60.      *
  61.      * @param maxCheck  maximum checking interval (s)
  62.      * @param threshold convergence threshold (s)
  63.      * @param maxIter   maximum number of iterations in the event time search
  64.      * @param handler   event handler to call at event occurrences
  65.      * @param function  the switching function.
  66.      */
  67.     private FieldFunctionalDetector(
  68.             final T maxCheck,
  69.             final T threshold,
  70.             final int maxIter,
  71.             final FieldEventHandler<? super FieldFunctionalDetector<T>, T> handler,
  72.             final Function<FieldSpacecraftState<T>, T> function) {
  73.         super(maxCheck, threshold, maxIter, handler);
  74.         this.function = function;
  75.     }


  76.     @Override
  77.     public T g(final FieldSpacecraftState<T> s) {
  78.         return function.apply(s);
  79.     }

  80.     @Override
  81.     protected FieldFunctionalDetector<T> create(
  82.             final T newMaxCheck,
  83.             final T newThreshold,
  84.             final int newMaxIter,
  85.             final FieldEventHandler<? super FieldFunctionalDetector<T>, T> newHandler) {

  86.         return new FieldFunctionalDetector<>(newMaxCheck, newThreshold, newMaxIter,
  87.                 newHandler, function);
  88.     }

  89.     /**
  90.      * Create a new event detector with a new g function, keeping all other attributes the
  91.      * same. It is recommended to use {@link #withMaxCheck(RealFieldElement)} and {@link
  92.      * #withThreshold(RealFieldElement)} to set appropriate values for this g function.
  93.      *
  94.      * @param newGFunction the new g function.
  95.      * @return a new detector with the new g function.
  96.      */
  97.     public FieldFunctionalDetector<T> withFunction(
  98.             final Function<FieldSpacecraftState<T>, T> newGFunction) {
  99.         return new FieldFunctionalDetector<>(getMaxCheckInterval(), getThreshold(),
  100.                 getMaxIterationCount(), getHandler(), newGFunction);
  101.     }

  102.     /**
  103.      * Get the switching function.
  104.      *
  105.      * @return the function used in {@link #g(FieldSpacecraftState)}.
  106.      */
  107.     public Function<FieldSpacecraftState<T>, T> getFunction() {
  108.         return function;
  109.     }

  110. }