FieldExtremumApproachDetector.java

  1. /* Copyright 2002-2025 CS GROUP
  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 org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.hipparchus.ode.events.Action;
  22. import org.orekit.propagation.FieldSpacecraftState;
  23. import org.orekit.propagation.PropagatorsParallelizer;
  24. import org.orekit.propagation.events.handlers.EventHandler;
  25. import org.orekit.propagation.events.handlers.FieldEventHandler;
  26. import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
  27. import org.orekit.utils.FieldPVCoordinates;
  28. import org.orekit.utils.FieldPVCoordinatesProvider;
  29. import org.orekit.utils.PVCoordinatesProvider;
  30. import org.orekit.utils.TimeStampedFieldPVCoordinates;
  31. import org.orekit.utils.TimeStampedPVCoordinates;

  32. /**
  33.  * Finder for extremum approach events.
  34.  * <p>
  35.  * This class finds extremum approach events (i.e. closest or farthest approach).
  36.  * </p>
  37.  * <p>
  38.  * The default implementation behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and to
  39.  * {@link Action#STOP stop} propagation at closest approach. This can be changed by calling
  40.  * {@link FieldAbstractDetector#withHandler(FieldEventHandler)} after construction (go to the end of the documentation to see
  41.  * an example).
  42.  * </p>
  43.  * <p>
  44.  * As this detector needs two objects (moving relative to each other), it embeds one
  45.  * {@link FieldPVCoordinatesProvider fielded coordinates provider} for the secondary object and is registered as an event
  46.  * detector in the propagator of the primary object. The secondary object
  47.  * {@link FieldPVCoordinatesProvider fielded coordinates provider} will therefore be driven by this detector (and hence by
  48.  * the propagator in which this detector is registered). Note that you can also create this detector using a standard
  49.  * {@link PVCoordinatesProvider coordinates provider}
  50.  * </p>
  51.  * <p><b>
  52.  * In order to avoid infinite recursion, care must be taken to have the secondary object provider being <em>completely
  53.  * independent</em> from anything else. In particular, if the provider is a propagator, it should <em>not</em> be run
  54.  * together in a {@link PropagatorsParallelizer propagators parallelizer} with the propagator this detector is registered in.
  55.  * It is fine however to configure two separate propagators PsA and PsB with similar settings for the secondary object and
  56.  * one propagator Pm for the primary object and then use Psa in this detector registered within Pm while Pm and Psb are run
  57.  * in the context of a {@link PropagatorsParallelizer propagators parallelizer}.
  58.  * </b></p>
  59.  * <p>
  60.  * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
  61.  * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be computationally
  62.  * costly.
  63.  * </p>
  64.  * <p>
  65.  * Also, it is possible to detect solely one type of event using an {@link EventSlopeFilter event slope filter}. For example
  66.  * in order to only detect closest approach, one should type the following :
  67.  * </p>
  68.  * <pre>{@code
  69.  * FieldExtremumApproachDetector<Type> extremumApproachDetector = new FieldExtremumApproachDetector<>(field, secondaryPVProvider);
  70.  * FieldEventDetector<Type> closeApproachDetector = new FieldEventSlopeFilter<>(extremumApproachDetector, FilterType.TRIGGER_ONLY_INCREASING_EVENTS);
  71.  *  }
  72.  * </pre>
  73.  *
  74.  * @author Vincent Cucchietti
  75.  * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
  76.  * @see FieldEventSlopeFilter
  77.  * @see FilterType
  78.  * @since 11.3
  79.  */
  80. public class FieldExtremumApproachDetector<T extends CalculusFieldElement<T>>
  81.         extends FieldAbstractDetector<FieldExtremumApproachDetector<T>, T> {

  82.     /**
  83.      * PVCoordinates provider of the other object with which we want to find out the extremum approach.
  84.      */
  85.     private final FieldPVCoordinatesProvider<T> secondaryPVProvider;

  86.     /**
  87.      * Constructor with default values.
  88.      * <p>
  89.      * By default, the implemented behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and to
  90.      * {@link Action#STOP stop} propagation at closest approach.
  91.      * <p>
  92.      * <b>BEWARE : This constructor will "fieldify" given secondary PV coordinates provider.</b>
  93.      *
  94.      * @param field field the type of number to use
  95.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  96.      * approach.
  97.      */
  98.     public FieldExtremumApproachDetector(final Field<T> field, final PVCoordinatesProvider secondaryPVProvider) {
  99.         this(field, (FieldPVCoordinatesProvider<T>) (date, frame) -> {
  100.             final TimeStampedPVCoordinates timeStampedPV =
  101.                     secondaryPVProvider.getPVCoordinates(date.toAbsoluteDate(), frame);
  102.             return new TimeStampedFieldPVCoordinates<>(field, timeStampedPV);
  103.         });
  104.     }

  105.     /**
  106.      * Constructor with default values.
  107.      * <p>
  108.      * By default, the implemented behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and to
  109.      * {@link Action#STOP stop} propagation at closest approach.
  110.      * </p>
  111.      *
  112.      * @param field field the type of number to use
  113.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  114.      * approach.
  115.      */
  116.     public FieldExtremumApproachDetector(final Field<T> field, final FieldPVCoordinatesProvider<T> secondaryPVProvider) {
  117.         this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
  118.              new FieldStopOnIncreasing<>(), secondaryPVProvider);
  119.     }

  120.     /**
  121.      * Constructor.
  122.      * <p>
  123.      * This constructor is to be used if the user wants to change the default behavior of the detector.
  124.      * </p>
  125.      *
  126.      * @param detectionSettings Event detection settings.
  127.      * @param handler Event handler to call at event occurrences.
  128.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  129.      * approach.
  130.      * @since 13.0
  131.      * @see EventHandler
  132.      */
  133.     protected FieldExtremumApproachDetector(final FieldEventDetectionSettings<T> detectionSettings,
  134.                                             final FieldEventHandler<T> handler,
  135.                                             final FieldPVCoordinatesProvider<T> secondaryPVProvider) {
  136.         super(detectionSettings, handler);
  137.         this.secondaryPVProvider = secondaryPVProvider;
  138.     }

  139.     /**
  140.      * Compute the relative PV between primary and secondary objects.
  141.      *
  142.      * @param s Spacecraft state.
  143.      *
  144.      * @return Relative position between primary (=s) and secondaryPVProvider.
  145.      */
  146.     public FieldPVCoordinates<T> computeDeltaPV(final FieldSpacecraftState<T> s) {
  147.         final FieldVector3D<T> primaryPos = s.getPosition();
  148.         final FieldVector3D<T> primaryVel = s.getPVCoordinates().getVelocity();

  149.         final FieldPVCoordinates<T> secondaryPV  = secondaryPVProvider.getPVCoordinates(s.getDate(), s.getFrame());
  150.         final FieldVector3D<T>      secondaryPos = secondaryPV.getPosition();
  151.         final FieldVector3D<T>      secondaryVel = secondaryPV.getVelocity();

  152.         final FieldVector3D<T> relativePos = secondaryPos.subtract(primaryPos);
  153.         final FieldVector3D<T> relativeVel = secondaryVel.subtract(primaryVel);

  154.         return new FieldPVCoordinates<>(relativePos, relativeVel);
  155.     }

  156.     /**
  157.      * Get the secondary position-velocity provider stored in this instance.
  158.      *
  159.      * @return the secondary position-velocity provider stored in this instance
  160.      */
  161.     public FieldPVCoordinatesProvider<T> getSecondaryPVProvider() {
  162.         return secondaryPVProvider;
  163.     }

  164.     /**
  165.      * The {@code g} is positive when the primary object is getting further away from the secondary object and is negative
  166.      * when it is getting closer to it.
  167.      *
  168.      * @param s the current state information: date, kinematics, attitude
  169.      *
  170.      * @return value of the switching function
  171.      */
  172.     @Override
  173.     public T g(final FieldSpacecraftState<T> s) {
  174.         final FieldPVCoordinates<T> deltaPV = computeDeltaPV(s);
  175.         return FieldVector3D.dotProduct(deltaPV.getPosition(), deltaPV.getVelocity());
  176.     }

  177.     /** {@inheritDoc} */
  178.     @Override
  179.     protected FieldExtremumApproachDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  180.                                                       final FieldEventHandler<T> newHandler) {
  181.         return new FieldExtremumApproachDetector<>(detectionSettings, newHandler, secondaryPVProvider);
  182.     }
  183. }