FieldNodeDetector.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.CalculusFieldElement;
  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.FieldOrbit;
  24. import org.orekit.orbits.KeplerianOrbit;
  25. import org.orekit.orbits.Orbit;
  26. import org.orekit.orbits.OrbitType;
  27. import org.orekit.orbits.PositionAngleType;
  28. import org.orekit.propagation.FieldSpacecraftState;
  29. import org.orekit.propagation.events.handlers.FieldEventHandler;
  30. import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
  31. import org.orekit.propagation.events.intervals.FieldAdaptableInterval;

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

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

  52.     /** Build a new instance.
  53.      * <p>The orbit is used only to set an upper bound for the max check interval
  54.      * to period/3 and to set the convergence threshold according to orbit size.</p>
  55.      * @param orbit initial orbit
  56.      * @param frame frame in which the equator is defined (typical
  57.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  58.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  59.      */
  60.     public FieldNodeDetector(final FieldOrbit<T> orbit, final Frame frame) {
  61.         this(orbit.getKeplerianPeriod().multiply(1.0e-13), orbit, frame);
  62.     }

  63.     /** Build a new instance.
  64.      * <p>The orbit is used only to set an upper bound for the max check interval
  65.      * to period/3.</p>
  66.      * @param threshold convergence threshold (s)
  67.      * @param orbit initial orbit
  68.      * @param frame frame in which the equator is defined (typical
  69.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  70.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  71.      */
  72.     public FieldNodeDetector(final T threshold, final FieldOrbit<T> orbit, final Frame frame) {
  73.         this(new FieldEventDetectionSettings<>(FieldAdaptableInterval.of(orbit.getA().getField().getZero().newInstance(2 * estimateNodesTimeSeparation(orbit.toOrbit()) / 3).getReal()),
  74.             threshold, DEFAULT_MAX_ITER), new FieldStopOnIncreasing<>(), frame);
  75.     }

  76.     /** Protected constructor with full parameters.
  77.      * <p>
  78.      * This constructor is not public as users are expected to use the builder
  79.      * API with the various {@code withXxx()} methods to set up the instance
  80.      * in a readable manner without using a huge amount of parameters.
  81.      * </p>
  82.      * @param detectionSettings event detection settings
  83.      * @param handler event handler to call at event occurrences
  84.      * @param frame frame in which the equator is defined (typical
  85.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  86.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  87.      * @since 13.0
  88.      */
  89.     protected FieldNodeDetector(final FieldEventDetectionSettings<T> detectionSettings,
  90.                                 final FieldEventHandler<T> handler, final Frame frame) {
  91.         super(detectionSettings, handler);
  92.         this.frame = frame;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     protected FieldNodeDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  97.                                           final FieldEventHandler<T> newHandler) {
  98.         return new FieldNodeDetector<>(detectionSettings, newHandler, frame);
  99.     }

  100.     /** Find time separation between nodes.
  101.      * <p>
  102.      * The estimation of time separation is based on Keplerian motion, it is only
  103.      * used as a rough guess for a safe setting of default max check interval for
  104.      * event detection.
  105.      * </p>
  106.      * @param orbit initial orbit
  107.      * @return minimum time separation between nodes
  108.      */
  109.     private static double estimateNodesTimeSeparation(final Orbit orbit) {

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

  111.         // mean anomaly of ascending node
  112.         final double ascendingM  =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
  113.                                                        keplerian.getI(),
  114.                                                        keplerian.getPerigeeArgument(),
  115.                                                        keplerian.getRightAscensionOfAscendingNode(),
  116.                                                        -keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
  117.                                                        keplerian.getFrame(), keplerian.getDate(),
  118.                                                        keplerian.getMu()).getMeanAnomaly();

  119.         // mean anomaly of descending node
  120.         final double descendingM =  new KeplerianOrbit(keplerian.getA(), keplerian.getE(),
  121.                                                        keplerian.getI(),
  122.                                                        keplerian.getPerigeeArgument(),
  123.                                                        keplerian.getRightAscensionOfAscendingNode(),
  124.                                                        FastMath.PI - keplerian.getPerigeeArgument(), PositionAngleType.TRUE,
  125.                                                        keplerian.getFrame(), keplerian.getDate(),
  126.                                                        keplerian.getMu()).getMeanAnomaly();

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

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

  132.     }

  133.     /** Get the frame in which the equator is defined.
  134.      * @return the frame in which the equator is defined
  135.      */
  136.     public Frame getFrame() {
  137.         return frame;
  138.     }

  139.     /** Compute the value of the switching function.
  140.      * This function computes the Z position in the defined frame.
  141.      * @param s the current state information: date, kinematics, attitude
  142.      * @return value of the switching function
  143.      */
  144.     public T g(final FieldSpacecraftState<T> s) {
  145.         return s.getPosition(frame).getZ();
  146.     }

  147. //    public NodeDetector toNoField() {
  148. //        return new NodeDetector(getThreshold().getReal(), orbit.toOrbit(), frame);
  149. //    }

  150. }