FieldNodeDetector.java

  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. 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. /** 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(FieldEventHandler)} 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.FieldPropagator#addEventDetector(FieldEventDetector)
  45.  * @author Luc Maisonobe
  46.  * @param <T> type of the field elements
  47.  */
  48. public class FieldNodeDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldNodeDetector<T>, T> {

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

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

  62.     /** Build a new instance.
  63.      * <p>The orbit is used only to set an upper bound for the max check interval
  64.      * to period/3.</p>
  65.      * @param threshold convergence threshold (s)
  66.      * @param orbit initial orbit
  67.      * @param frame frame in which the equator is defined (typical
  68.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  69.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  70.      */
  71.     public FieldNodeDetector(final T threshold, final FieldOrbit<T> orbit, final Frame frame) {
  72.         this(s -> orbit.getA().getField().getZero().add(2 * estimateNodesTimeSeparation(orbit.toOrbit()) / 3).getReal(), threshold,
  73.              DEFAULT_MAX_ITER, new FieldStopOnIncreasing<>(),
  74.              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 maxCheck maximum checking interval
  83.      * @param threshold convergence threshold (s)
  84.      * @param maxIter maximum number of iterations in the event time search
  85.      * @param handler event handler to call at event occurrences
  86.      * @param frame frame in which the equator is defined (typical
  87.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  88.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  89.      * @since 6.1
  90.      */
  91.     protected FieldNodeDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold,
  92.                                 final int maxIter, final FieldEventHandler<T> handler,
  93.                                 final Frame frame) {
  94.         super(maxCheck, threshold, maxIter, handler);
  95.         this.frame = frame;
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     protected FieldNodeDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
  100.                                           final int newMaxIter, final FieldEventHandler<T> newHandler) {
  101.         return new FieldNodeDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler, frame);
  102.     }

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

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

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

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

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

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

  135.     }

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

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

  150. //    public NodeDetector toNoField() {
  151. //        return new NodeDetector(getThreshold().getReal(), orbit.toOrbit(), frame);
  152. //    }

  153. }