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  
18  package org.orekit.propagation.events;
19  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.orekit.propagation.FieldSpacecraftState;
24  import org.orekit.propagation.events.handlers.FieldEventHandler;
25  import org.orekit.propagation.events.handlers.FieldStopOnEvent;
26  import org.orekit.utils.FieldPVCoordinatesProvider;
27  
28  /**
29   * Detector of specific value for the distance relative to another trajectory (using the Euclidean norm).
30   * <p>
31   * The default implementation behavior is to {@link Action#STOP stop} propagation.
32   * This can be changed by calling {@link #withHandler(org.orekit.propagation.events.handlers.FieldEventHandler)} after construction.
33   * </p>
34   * <p>
35   * As this detector needs two objects (moving relative to each other), it embeds one
36   * {@link org.orekit.utils.FieldPVCoordinatesProvider coordinates provider} for the secondary object and is registered as an event detector in
37   * the propagator of the primary object. The secondary object {@link org.orekit.utils.FieldPVCoordinatesProvider coordinates provider} will
38   * therefore be driven by this detector (and hence by the propagator in which this detector is registered).
39   * </p>
40   * <p>
41   * For efficiency reason during the event search loop, it is recommended to have the secondary provider be an analytical
42   * propagator or an ephemeris. A numerical propagator as a secondary propagator works but is expected to be
43   * computationally costly.
44   * </p>
45   *
46   * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
47   * @author Romain Serra
48   * @since 12.1
49   */
50  public class FieldRelativeDistanceDetector<T extends CalculusFieldElement<T>>
51          extends FieldAbstractDetector<FieldRelativeDistanceDetector<T>, T> {
52  
53      /**
54       * PVCoordinates provider of the other object used to define relative distance.
55       */
56      private final FieldPVCoordinatesProvider<T> secondaryPVProvider;
57  
58      /** Relative distance value triggering detection. */
59      private final T distanceThreshold;
60  
61      /**
62       * Constructor with default values.
63       * <p>
64       * By default, the implemented behavior is to {@link Action#STOP stop} propagation at detection.
65       * </p>
66       *
67       * @param secondaryPVProvider PVCoordinates provider of the other object defining relative distance.
68       * @param distanceThreshold Relative distance threshold for event detection
69       */
70      public FieldRelativeDistanceDetector(final FieldPVCoordinatesProvider<T> secondaryPVProvider,
71                                           final T distanceThreshold) {
72          this(FieldAdaptableInterval.of(DEFAULT_MAXCHECK), distanceThreshold.getField().getZero().newInstance(DEFAULT_THRESHOLD),
73                  DEFAULT_MAX_ITER, new FieldStopOnEvent<>(), secondaryPVProvider, distanceThreshold);
74      }
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 FieldEventHandler
89       */
90      protected FieldRelativeDistanceDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold, final int maxIter,
91                                              final FieldEventHandler<T> handler, final FieldPVCoordinatesProvider<T> secondaryPVProvider,
92                                              final T distanceThreshold) {
93          super(maxCheck, threshold, maxIter, handler);
94          this.secondaryPVProvider = secondaryPVProvider;
95          this.distanceThreshold = distanceThreshold;
96      }
97  
98      /**
99       * The {@code g} is positive when the relative distance is larger or equal than the threshold,
100      * non-positive otherwise.
101      *
102      * @param s the current state information: date, kinematics, attitude
103      * @return value of the switching function
104      */
105     @Override
106     public T g(final FieldSpacecraftState<T> s) {
107         final FieldVector3D<T> secondaryPosition = getSecondaryPVProvider().getPosition(s.getDate(), s.getFrame());
108         final T relativeDistance = s.getPosition().subtract(secondaryPosition).getNorm();
109         return relativeDistance.subtract(distanceThreshold);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     protected FieldRelativeDistanceDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
115                                                       final int newMaxIter, final FieldEventHandler<T> newHandler) {
116         return new FieldRelativeDistanceDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler, secondaryPVProvider,
117                 distanceThreshold);
118     }
119 
120     /**
121      * Get the secondary position-velocity provider stored in this instance.
122      *
123      * @return the secondary position-velocity provider stored in this instance
124      */
125     public FieldPVCoordinatesProvider<T> getSecondaryPVProvider() {
126         return secondaryPVProvider;
127     }
128 
129     /**
130      * Get the relative distance threshold.
131      *
132      * @return threshold triggering detection
133      */
134     public T getDistanceThreshold() {
135         return distanceThreshold;
136     }
137 }