RelativeDistanceDetector.java

  1. /* Copyright 2022-2024 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(AdaptableInterval.of(DEFAULT_MAXCHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER, 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 maxCheck            Maximum checking interval.
  83.      * @param threshold           Convergence threshold (s).
  84.      * @param maxIter             Maximum number of iterations in the event time search.
  85.      * @param handler             Event handler to call at event occurrences.
  86.      * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
  87.      * @param distanceThreshold Relative distance threshold for event detection
  88.      * @see EventHandler
  89.      */
  90.     protected RelativeDistanceDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
  91.                                        final EventHandler handler, final PVCoordinatesProvider secondaryPVProvider,
  92.                                        final double distanceThreshold) {
  93.         super(maxCheck, threshold, maxIter, handler);
  94.         this.secondaryPVProvider = secondaryPVProvider;
  95.         this.distanceThreshold = distanceThreshold;
  96.     }

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

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     protected RelativeDistanceDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
  112.                                               final int newMaxIter, final EventHandler newHandler) {
  113.         return new RelativeDistanceDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, secondaryPVProvider,
  114.                 distanceThreshold);
  115.     }

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

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