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