NodeDetector.java

  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. import org.hipparchus.ode.events.Action;
  19. import org.hipparchus.util.FastMath;
  20. import org.hipparchus.util.MathUtils;
  21. import org.orekit.frames.Frame;
  22. import org.orekit.orbits.KeplerianOrbit;
  23. import org.orekit.orbits.Orbit;
  24. import org.orekit.orbits.OrbitType;
  25. import org.orekit.orbits.PositionAngleType;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.propagation.events.handlers.EventHandler;
  28. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  29. import org.orekit.propagation.events.intervals.AdaptableInterval;

  30. /** Finder for node crossing events.
  31.  * <p>This class finds equator crossing events (i.e. ascending
  32.  * or descending node crossing).</p>
  33.  * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
  34.  * propagation at descending node crossing and to {@link Action#STOP stop} propagation
  35.  * at ascending node crossing. This can be changed by calling
  36.  * {@link #withHandler(EventHandler)} after construction.</p>
  37.  * <p>Beware that node detection will fail for almost equatorial orbits. If
  38.  * for example a node detector is used to trigger an {@link
  39.  * org.orekit.forces.maneuvers.ImpulseManeuver ImpulseManeuver} and the maneuver
  40.  * turn the orbit plane to equator, then the detector may completely fail just
  41.  * after the maneuver has been performed! This is a real case that has been
  42.  * encountered during validation ...</p>
  43.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  44.  * @author Luc Maisonobe
  45.  */
  46. public class NodeDetector extends AbstractDetector<NodeDetector> {

  47.     /** Default max check interval. */
  48.     private static final double DEFAULT_MAX_CHECK = 1800.0;

  49.     /** Default convergence threshold. */
  50.     private static final double DEFAULT_THRESHOLD = 1.0e-3;

  51.     /** Frame in which the equator is defined. */
  52.     private final Frame frame;

  53.     /** Build a new instance.
  54.      * <p>The default {@link #getMaxCheckInterval() max check interval}
  55.      * is set to 1800s, it can be changed using {@link #withMaxCheck(double)}
  56.      * in the fluent API. The default {@link #getThreshold() convergence threshold}
  57.      * is set to 1.0e-3s, it can be changed using {@link #withThreshold(double)}
  58.      * in the fluent API.</p>
  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.      * @since 10.3
  63.      */
  64.     public NodeDetector(final Frame frame) {
  65.         this(new EventDetectionSettings(DEFAULT_MAX_CHECK, DEFAULT_THRESHOLD, EventDetectionSettings.DEFAULT_MAX_ITER),
  66.                 new StopOnIncreasing(), frame);
  67.     }

  68.     /** Build a new instance.
  69.      * <p>The orbit is used only to set an upper bound for the max check interval
  70.      * to a value related to nodes separation (as computed by a Keplerian model)
  71.      * and to set the convergence threshold according to orbit size.</p>
  72.      * @param orbit initial orbit
  73.      * @param frame frame in which the equator is defined (typical
  74.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  75.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  76.      */
  77.     public NodeDetector(final Orbit orbit, final Frame frame) {
  78.         this(1.0e-13 * orbit.getKeplerianPeriod(), orbit, frame);
  79.     }

  80.     /** Build a new instance.
  81.      * <p>The orbit is used only to set an upper bound for the max check interval
  82.      * to a value related to nodes separation (as computed by a Keplerian model).</p>
  83.      * @param threshold convergence threshold (s)
  84.      * @param orbit initial orbit
  85.      * @param frame frame in which the equator is defined (typical
  86.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  87.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  88.      */
  89.     public NodeDetector(final double threshold, final Orbit orbit, final Frame frame) {
  90.         this(new EventDetectionSettings(AdaptableInterval.of(2 * estimateNodesTimeSeparation(orbit) / 3), threshold,
  91.              DEFAULT_MAX_ITER), new StopOnIncreasing(),
  92.              frame);
  93.     }

  94.     /** Protected constructor with full parameters.
  95.      * <p>
  96.      * This constructor is not public as users are expected to use the builder
  97.      * API with the various {@code withXxx()} methods to set up the instance
  98.      * in a readable manner without using a huge amount of parameters.
  99.      * </p>
  100.      * @param detectionSettings detection settings
  101.      * @param handler event handler to call at event occurrences
  102.      * @param frame frame in which the equator is defined (typical
  103.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  104.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  105.      * @since 12.2
  106.      */
  107.     protected NodeDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
  108.                            final Frame frame) {
  109.         super(detectionSettings, handler);
  110.         this.frame = frame;
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     protected NodeDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
  115.         return new NodeDetector(detectionSettings, newHandler, frame);
  116.     }

  117.     /** Find time separation between nodes.
  118.      * <p>
  119.      * The estimation of time separation is based on Keplerian motion, it is only
  120.      * used as a rough guess for a safe setting of default max check interval for
  121.      * event detection.
  122.      * </p>
  123.      * @param orbit initial orbit
  124.      * @return minimum time separation between nodes
  125.      */
  126.     private static double estimateNodesTimeSeparation(final Orbit orbit) {

  127.         final KeplerianOrbit keplerian = (KeplerianOrbit) OrbitType.KEPLERIAN.convertType(orbit);

  128.         // mean anomaly of ascending node
  129.         final double ascendingM  =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
  130.                                                        keplerian.getI(),
  131.                                                        keplerian.getPerigeeArgument(),
  132.                                                        keplerian.getRightAscensionOfAscendingNode(),
  133.                                                        -keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
  134.                                                        keplerian.getFrame(), keplerian.getDate(),
  135.                                                        keplerian.getMu()).getMeanAnomaly();

  136.         // mean anomaly of descending node
  137.         final double descendingM =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
  138.                                                        keplerian.getI(),
  139.                                                        keplerian.getPerigeeArgument(),
  140.                                                        keplerian.getRightAscensionOfAscendingNode(),
  141.                                                        FastMath.PI - keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
  142.                                                        keplerian.getFrame(), keplerian.getDate(),
  143.                                                        keplerian.getMu()).getMeanAnomaly();

  144.         // differences between mean anomalies
  145.         final double delta1 = MathUtils.normalizeAngle(ascendingM, descendingM + FastMath.PI) - descendingM;
  146.         final double delta2 = 2 * FastMath.PI - delta1;

  147.         // minimum time separation between the two nodes
  148.         return FastMath.min(delta1, delta2) / keplerian.getKeplerianMeanMotion();

  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.     /** Compute the value of the switching function.
  157.      * This function computes the Z position in the defined frame.
  158.      * @param s the current state information: date, kinematics, attitude
  159.      * @return value of the switching function
  160.      */
  161.     public double g(final SpacecraftState s) {
  162.         return s.getPosition(frame).getZ();
  163.     }

  164. }