RelativeDistanceDetector.java

  1. /* Copyright 2022-2025 Romain Serra
  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.StopOnEvent;
  24. import org.orekit.utils.PVCoordinatesProvider;

  25. /**
  26.  * Detector of specific value for the distance relative to another trajectory (using the Euclidean norm).
  27.  * <p>
  28.  * The default implementation behavior is to {@link Action#STOP stop} propagation.
  29.  * This can be changed by calling {@link #withHandler(EventHandler)} after construction.
  30.  * </p>
  31.  * <p>
  32.  * As this detector needs two objects (moving relative to each other), it embeds one
  33.  * {@link PVCoordinatesProvider coordinates provider} for the secondary object and is registered as an event detector in
  34.  * the propagator of the primary object. The secondary object {@link PVCoordinatesProvider coordinates provider} will
  35.  * therefore be driven by this detector (and hence by the propagator in which this detector is registered).
  36.  * </p>
  37.  * <p><b>
  38.  * In order to avoid infinite recursion, care must be taken to have the secondary object provider being <em>completely
  39.  * independent</em> from anything else. In particular, if the provider is a propagator, it should <em>not</em> be run
  40.  * together in a {@link PropagatorsParallelizer propagators parallelizer} with the propagator this detector is
  41.  * registered in. It is fine however to configure two separate propagators PsA and PsB with similar settings for the
  42.  * secondary object and one propagator Pm for the primary object and then use Psa in this detector registered within Pm
  43.  * while Pm and Psb are run in the context of a {@link PropagatorsParallelizer propagators parallelizer}.
  44.  * </b></p>
  45.  * <p>
  46.  * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
  47.  * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be
  48.  * computationally costly.
  49.  * </p>
  50.  *
  51.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  52.  * @author Romain Serra
  53.  * @since 12.1
  54.  */
  55. public class RelativeDistanceDetector extends AbstractDetector<RelativeDistanceDetector> {

  56.     /**
  57.      * PVCoordinates provider of the other object used to define relative distance.
  58.      */
  59.     private final PVCoordinatesProvider secondaryPVProvider;

  60.     /** Relative distance value triggering detection. */
  61.     private final double distanceThreshold;

  62.     /**
  63.      * Constructor with default values.
  64.      * <p>
  65.      * By default, the implemented behavior is to {@link Action#STOP stop} propagation at detection.
  66.      * </p>
  67.      *
  68.      * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
  69.      * @param distanceThreshold Relative distance threshold for event detection
  70.      */
  71.     public RelativeDistanceDetector(final PVCoordinatesProvider secondaryPVProvider,
  72.                                     final double distanceThreshold) {
  73.         this(EventDetectionSettings.getDefaultEventDetectionSettings(), new StopOnEvent(), secondaryPVProvider,
  74.                 distanceThreshold);
  75.     }

  76.     /**
  77.      * Constructor.
  78.      * <p>
  79.      * This constructor is to be used if the user wants to change the default behavior of the detector.
  80.      * </p>
  81.      *
  82.      * @param detectionSettings   Detection settings
  83.      * @param handler             Event handler to call at event occurrences.
  84.      * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
  85.      * @param distanceThreshold Relative distance threshold for event detection
  86.      * @see EventHandler
  87.      * @since 12.2
  88.      */
  89.     protected RelativeDistanceDetector(final EventDetectionSettings detectionSettings,
  90.                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider,
  91.                                        final double distanceThreshold) {
  92.         super(detectionSettings, handler);
  93.         this.secondaryPVProvider = secondaryPVProvider;
  94.         this.distanceThreshold = distanceThreshold;
  95.     }

  96.     /**
  97.      * The {@code g} is positive when the relative distance is larger or equal than the threshold,
  98.      * non-positive otherwise.
  99.      *
  100.      * @param s the current state information: date, kinematics, attitude
  101.      * @return value of the switching function
  102.      */
  103.     public double g(final SpacecraftState s) {
  104.         final Vector3D secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
  105.         final double relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
  106.         return relativeDistance - distanceThreshold;
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     protected RelativeDistanceDetector create(final EventDetectionSettings detectionSettings,
  111.                                               final EventHandler newHandler) {
  112.         return new RelativeDistanceDetector(detectionSettings, newHandler, secondaryPVProvider, distanceThreshold);
  113.     }

  114.     /**
  115.      * Get the secondary position-velocity provider stored in this instance.
  116.      *
  117.      * @return the secondary position-velocity provider stored in this instance
  118.      */
  119.     public PVCoordinatesProvider getSecondaryPVProvider() {
  120.         return secondaryPVProvider;
  121.     }

  122.     /**
  123.      * Get the relative distance threshold.
  124.      *
  125.      * @return threshold triggering detection
  126.      */
  127.     public double getDistanceThreshold() {
  128.         return distanceThreshold;
  129.     }
  130. }