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.CalculusFieldElement;
  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. import org.orekit.propagation.events.intervals.FieldAdaptableInterval;

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

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

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

  57.     /**
  58.      * Protected constructor.
  59.      *
  60.      * @param detectionSettings event detection settings
  61.      * @param handler   event handler to call at event occurrences
  62.      * @param function  the switching function.
  63.      * @since 13.0
  64.      */
  65.     protected FieldFunctionalDetector(
  66.             final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
  67.             final Function<FieldSpacecraftState<T>, T> function) {
  68.         super(detectionSettings, handler);
  69.         this.function = function;
  70.     }


  71.     @Override
  72.     public T g(final FieldSpacecraftState<T> s) {
  73.         return function.apply(s);
  74.     }

  75.     @Override
  76.     protected FieldFunctionalDetector<T> create(
  77.             final FieldEventDetectionSettings<T> detectionSettings,
  78.             final FieldEventHandler<T> newHandler) {

  79.         return new FieldFunctionalDetector<>(detectionSettings, newHandler, function);
  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(FieldAdaptableInterval)} and {@link
  84.      * #withThreshold(CalculusFieldElement)} 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 FieldFunctionalDetector<T> withFunction(
  90.             final Function<FieldSpacecraftState<T>, T> newGFunction) {
  91.         return new FieldFunctionalDetector<>(getDetectionSettings(), getHandler(), newGFunction);
  92.     }

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

  101. }