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  
19  import org.hipparchus.ode.events.Action;
20  import org.hipparchus.util.FastMath;
21  import org.hipparchus.util.MathUtils;
22  import org.orekit.frames.Frame;
23  import org.orekit.orbits.KeplerianOrbit;
24  import org.orekit.orbits.Orbit;
25  import org.orekit.orbits.OrbitType;
26  import org.orekit.orbits.PositionAngleType;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.propagation.events.handlers.EventHandler;
29  import org.orekit.propagation.events.handlers.StopOnIncreasing;
30  
31  /** Finder for node crossing events.
32   * <p>This class finds equator crossing events (i.e. ascending
33   * or descending node crossing).</p>
34   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
35   * propagation at descending node crossing and to {@link Action#STOP stop} propagation
36   * at ascending node crossing. This can be changed by calling
37   * {@link #withHandler(EventHandler)} after construction.</p>
38   * <p>Beware that node detection will fail for almost equatorial orbits. If
39   * for example a node detector is used to trigger an {@link
40   * org.orekit.forces.maneuvers.ImpulseManeuver ImpulseManeuver} and the maneuver
41   * turn the orbit plane to equator, then the detector may completely fail just
42   * after the maneuver has been performed! This is a real case that has been
43   * encountered during validation ...</p>
44   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
45   * @author Luc Maisonobe
46   */
47  public class NodeDetector extends AbstractDetector<NodeDetector> {
48  
49      /** Default max check interval. */
50      private static final double DEFAULT_MAX_CHECK = 1800.0;
51  
52      /** Default convergence threshold. */
53      private static final double DEFAULT_THRESHOLD = 1.0e-3;
54  
55      /** Frame in which the equator is defined. */
56      private final Frame frame;
57  
58      /** Build a new instance.
59       * <p>The default {@link #getMaxCheckInterval() max check interval}
60       * is set to 1800s, it can be changed using {@link #withMaxCheck(double)}
61       * in the fluent API. The default {@link #getThreshold() convergence threshold}
62       * is set to 1.0e-3s, it can be changed using {@link #withThreshold(double)}
63       * in the fluent API.</p>
64       * @param frame frame in which the equator is defined (typical
65       * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
66       * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
67       * @since 10.3
68       */
69      public NodeDetector(final Frame frame) {
70          this(AdaptableInterval.of(DEFAULT_MAX_CHECK), DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
71               new StopOnIncreasing(), frame);
72      }
73  
74      /** Build a new instance.
75       * <p>The orbit is used only to set an upper bound for the max check interval
76       * to a value related to nodes separation (as computed by a Keplerian model)
77       * and to set the convergence threshold according to orbit size.</p>
78       * @param orbit initial orbit
79       * @param frame frame in which the equator is defined (typical
80       * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
81       * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
82       */
83      public NodeDetector(final Orbit orbit, final Frame frame) {
84          this(1.0e-13 * orbit.getKeplerianPeriod(), orbit, frame);
85      }
86  
87      /** Build a new instance.
88       * <p>The orbit is used only to set an upper bound for the max check interval
89       * to a value related to nodes separation (as computed by a Keplerian model).</p>
90       * @param threshold convergence threshold (s)
91       * @param orbit initial orbit
92       * @param frame frame in which the equator is defined (typical
93       * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
94       * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
95       */
96      public NodeDetector(final double threshold, final Orbit orbit, final Frame frame) {
97          this(AdaptableInterval.of(2 * estimateNodesTimeSeparation(orbit) / 3), threshold,
98               DEFAULT_MAX_ITER, new StopOnIncreasing(),
99               frame);
100     }
101 
102     /** Protected constructor with full parameters.
103      * <p>
104      * This constructor is not public as users are expected to use the builder
105      * API with the various {@code withXxx()} methods to set up the instance
106      * in a readable manner without using a huge amount of parameters.
107      * </p>
108      * @param maxCheck maximum checking interval
109      * @param threshold convergence threshold (s)
110      * @param maxIter maximum number of iterations in the event time search
111      * @param handler event handler to call at event occurrences
112      * @param frame frame in which the equator is defined (typical
113      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
114      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
115      * @since 6.1
116      */
117     protected NodeDetector(final AdaptableInterval maxCheck, final double threshold,
118                            final int maxIter, final EventHandler handler,
119                            final Frame frame) {
120         super(maxCheck, threshold, maxIter, handler);
121         this.frame = frame;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     protected NodeDetector create(final AdaptableInterval newMaxCheck, final double newThreshold,
127                                   final int newMaxIter, final EventHandler newHandler) {
128         return new NodeDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, frame);
129     }
130 
131     /** Find time separation between nodes.
132      * <p>
133      * The estimation of time separation is based on Keplerian motion, it is only
134      * used as a rough guess for a safe setting of default max check interval for
135      * event detection.
136      * </p>
137      * @param orbit initial orbit
138      * @return minimum time separation between nodes
139      */
140     private static double estimateNodesTimeSeparation(final Orbit orbit) {
141 
142         final KeplerianOrbit keplerian = (KeplerianOrbit) OrbitType.KEPLERIAN.convertType(orbit);
143 
144         // mean anomaly of ascending node
145         final double ascendingM  =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
146                                                        keplerian.getI(),
147                                                        keplerian.getPerigeeArgument(),
148                                                        keplerian.getRightAscensionOfAscendingNode(),
149                                                        -keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
150                                                        keplerian.getFrame(), keplerian.getDate(),
151                                                        keplerian.getMu()).getMeanAnomaly();
152 
153         // mean anomaly of descending node
154         final double descendingM =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
155                                                        keplerian.getI(),
156                                                        keplerian.getPerigeeArgument(),
157                                                        keplerian.getRightAscensionOfAscendingNode(),
158                                                        FastMath.PI - keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
159                                                        keplerian.getFrame(), keplerian.getDate(),
160                                                        keplerian.getMu()).getMeanAnomaly();
161 
162         // differences between mean anomalies
163         final double delta1 = MathUtils.normalizeAngle(ascendingM, descendingM + FastMath.PI) - descendingM;
164         final double delta2 = 2 * FastMath.PI - delta1;
165 
166         // minimum time separation between the two nodes
167         return FastMath.min(delta1, delta2) / keplerian.getKeplerianMeanMotion();
168 
169     }
170 
171     /** Get the frame in which the equator is defined.
172      * @return the frame in which the equator is defined
173      */
174     public Frame getFrame() {
175         return frame;
176     }
177 
178     /** Compute the value of the switching function.
179      * This function computes the Z position in the defined frame.
180      * @param s the current state information: date, kinematics, attitude
181      * @return value of the switching function
182      */
183     public double g(final SpacecraftState s) {
184         return s.getPosition(frame).getZ();
185     }
186 
187 }