ExtremumApproachDetector.java

  1. /* Copyright 2002-2024 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 maxCheck            Maximum checking interval.
  96.      * @param threshold           Convergence threshold (s).
  97.      * @param maxIter             Maximum number of iterations in the event time search.
  98.      * @param handler             Event handler to call at event occurrences.
  99.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  100.      *                            approach.
  101.      * @see EventHandler
  102.      */
  103.     protected ExtremumApproachDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  104.                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider) {
  105.         this(new EventDetectionSettings(maxCheck, threshold, maxIter), handler, secondaryPVProvider);
  106.     }

  107.     /**
  108.      * Constructor.
  109.      * <p>
  110.      * This constructor is to be used if the user wants to change the default behavior of the detector.
  111.      * </p>
  112.      *
  113.      * @param detectionSettings   Detection settings.
  114.      * @param handler             Event handler to call at event occurrences.
  115.      * @param secondaryPVProvider PVCoordinates provider of the other object with which we want to find out the extremum
  116.      *                            approach.
  117.      * @see EventHandler
  118.      * @since 12.2
  119.      */
  120.     protected ExtremumApproachDetector(final EventDetectionSettings detectionSettings,
  121.                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider) {
  122.         super(detectionSettings, handler);
  123.         this.secondaryPVProvider = secondaryPVProvider;
  124.     }

  125.     /**
  126.      * The {@code g} is positive when the primary object is getting further away from the secondary object and is
  127.      * negative when it is getting closer to it.
  128.      *
  129.      * @param s the current state information: date, kinematics, attitude
  130.      * @return value of the switching function
  131.      */
  132.     public double g(final SpacecraftState s) {
  133.         final PVCoordinates deltaPV = computeDeltaPV(s);
  134.         return Vector3D.dotProduct(deltaPV.getPosition(), deltaPV.getVelocity());
  135.     }

  136.     /**
  137.      * Compute the relative PV between primary and secondary objects.
  138.      *
  139.      * @param s Spacecraft state.
  140.      *
  141.      * @return Relative position between primary (=s) and secondaryPVProvider.
  142.      *
  143.      * @deprecated The output type of this method shall be modified in the future to improve code efficiency (though it will
  144.      * still give access to the relative position and velocity)
  145.      */
  146.     @Deprecated
  147.     public PVCoordinates computeDeltaPV(final SpacecraftState s) {
  148.         final Vector3D primaryPos = s.getPosition();
  149.         final Vector3D primaryVel = s.getPVCoordinates().getVelocity();

  150.         final PVCoordinates secondaryPV  = secondaryPVProvider.getPVCoordinates(s.getDate(), s.getFrame());
  151.         final Vector3D      secondaryPos = secondaryPV.getPosition();
  152.         final Vector3D      secondaryVel = secondaryPV.getVelocity();

  153.         final Vector3D relativePos = secondaryPos.subtract(primaryPos);
  154.         final Vector3D relativeVel  = secondaryVel.subtract(primaryVel);

  155.         return new PVCoordinates(relativePos, relativeVel);
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     protected ExtremumApproachDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
  160.                                               final EventHandler newHandler) {
  161.         return new ExtremumApproachDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, secondaryPVProvider);
  162.     }

  163.     /**
  164.      * Get the secondary position-velocity provider stored in this instance.
  165.      *
  166.      * @return the secondary position-velocity provider stored in this instance
  167.      */
  168.     public PVCoordinatesProvider getSecondaryPVProvider() {
  169.         return secondaryPVProvider;
  170.     }
  171. }