ExtremumApproachDetector.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.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.ode.events.Action;
  20. import org.orekit.propagation.PropagatorsParallelizer;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.handlers.EventHandler;
  23. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  24. import org.orekit.utils.PVCoordinates;
  25. import org.orekit.utils.PVCoordinatesProvider;

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

  72.     /**
  73.      * PVCoordinates provider of the other object with which we want to find out the extremum approach.
  74.      */
  75.     private final PVCoordinatesProvider secondaryPVProvider;

  76.     /**
  77.      * Constructor with default values.
  78.      * <p>
  79.      * By default, the implemented behavior is to {@link Action#CONTINUE continue} propagation at farthest approach and
  80.      * to {@link Action#STOP stop} propagation at closest approach.
  81.      * </p>
  82.      *
  83.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  84.      *                            approach.
  85.      */
  86.     public ExtremumApproachDetector(final PVCoordinatesProvider secondaryPVProvider) {
  87.         this(EventDetectionSettings.getDefaultEventDetectionSettings(), new StopOnIncreasing(), secondaryPVProvider);
  88.     }

  89.     /**
  90.      * Constructor.
  91.      * <p>
  92.      * This constructor is to be used if the user wants to change the default behavior of the detector.
  93.      * </p>
  94.      *
  95.      * @param detectionSettings   Detection settings.
  96.      * @param handler             Event handler to call at event occurrences.
  97.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  98.      *                            approach.
  99.      * @see EventHandler
  100.      * @since 13.0
  101.      */
  102.     protected ExtremumApproachDetector(final EventDetectionSettings detectionSettings,
  103.                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider) {
  104.         super(detectionSettings, handler);
  105.         this.secondaryPVProvider = secondaryPVProvider;
  106.     }

  107.     /**
  108.      * The {@code g} is positive when the primary object is getting further away from the secondary object and is
  109.      * negative when it is getting closer to it.
  110.      *
  111.      * @param s the current state information: date, kinematics, attitude
  112.      * @return value of the switching function
  113.      */
  114.     public double g(final SpacecraftState s) {
  115.         final PVCoordinates deltaPV = computeDeltaPV(s);
  116.         return Vector3D.dotProduct(deltaPV.getPosition(), deltaPV.getVelocity());
  117.     }

  118.     /**
  119.      * Compute the relative PV between primary and secondary objects.
  120.      *
  121.      * @param s Spacecraft state.
  122.      *
  123.      * @return Relative position between primary (=s) and secondaryPVProvider.
  124.      */
  125.     public PVCoordinates computeDeltaPV(final SpacecraftState s) {
  126.         final Vector3D primaryPos = s.getPosition();
  127.         final Vector3D primaryVel = s.getPVCoordinates().getVelocity();

  128.         final PVCoordinates secondaryPV  = secondaryPVProvider.getPVCoordinates(s.getDate(), s.getFrame());
  129.         final Vector3D      secondaryPos = secondaryPV.getPosition();
  130.         final Vector3D      secondaryVel = secondaryPV.getVelocity();

  131.         final Vector3D relativePos = secondaryPos.subtract(primaryPos);
  132.         final Vector3D relativeVel  = secondaryVel.subtract(primaryVel);

  133.         return new PVCoordinates(relativePos, relativeVel);
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     protected ExtremumApproachDetector create(final EventDetectionSettings detectionSettings,
  138.                                               final EventHandler newHandler) {
  139.         return new ExtremumApproachDetector(detectionSettings, newHandler, secondaryPVProvider);
  140.     }

  141.     /**
  142.      * Get the secondary position-velocity provider stored in this instance.
  143.      *
  144.      * @return the secondary position-velocity provider stored in this instance
  145.      */
  146.     public PVCoordinatesProvider getSecondaryPVProvider() {
  147.         return secondaryPVProvider;
  148.     }
  149. }