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