NodeDetector.java

  1. /* Copyright 2002-2020 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.PositionAngle;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.propagation.events.handlers.EventHandler;
  28. import org.orekit.propagation.events.handlers.StopOnIncreasing;

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

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

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

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

  52.     /** Build a new instance.
  53.      * <p>The default {@link #getMaxCheckInterval() max check interval}
  54.      * is set to 1800s, it can be changed using {@link #withMaxCheck(double)}
  55.      * in the fluent API. The default {@link #getThreshold() convergence threshold}
  56.      * is set to 1.0e-3s, it can be changed using {@link #withThreshold(double)}
  57.      * in the fluent API.</p>
  58.      * @param frame frame in which the equator is defined (typical
  59.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  60.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  61.      * @since 10.3
  62.      */
  63.     public NodeDetector(final Frame frame) {
  64.         this(DEFAULT_MAX_CHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  65.              new StopOnIncreasing<NodeDetector>(), frame);
  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 a value related to nodes separation (as computed by a Keplerian model)
  70.      * and to set the convergence threshold according to orbit size.</p>
  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 NodeDetector(final Orbit orbit, final Frame frame) {
  77.         this(1.0e-13 * orbit.getKeplerianPeriod(), orbit, frame);
  78.     }

  79.     /** Build a new instance.
  80.      * <p>The orbit is used only to set an upper bound for the max check interval
  81.      * to a value related to nodes separation (as computed by a Keplerian model).</p>
  82.      * @param threshold convergence threshold (s)
  83.      * @param orbit initial orbit
  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.      */
  88.     public NodeDetector(final double threshold, final Orbit orbit, final Frame frame) {
  89.         this(2 * estimateNodesTimeSeparation(orbit) / 3, threshold,
  90.              DEFAULT_MAX_ITER, new StopOnIncreasing<NodeDetector>(),
  91.              frame);
  92.     }

  93.     /** Private constructor with full parameters.
  94.      * <p>
  95.      * This constructor is private as users are expected to use the builder
  96.      * API with the various {@code withXxx()} methods to set up the instance
  97.      * in a readable manner without using a huge amount of parameters.
  98.      * </p>
  99.      * @param maxCheck maximum checking interval (s)
  100.      * @param threshold convergence threshold (s)
  101.      * @param maxIter maximum number of iterations in the event time search
  102.      * @param handler event handler to call at event occurrences
  103.      * @param frame frame in which the equator is defined (typical
  104.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  105.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  106.      * @since 6.1
  107.      */
  108.     private NodeDetector(final double maxCheck, final double threshold,
  109.                          final int maxIter, final EventHandler<? super NodeDetector> handler,
  110.                          final Frame frame) {
  111.         super(maxCheck, threshold, maxIter, handler);
  112.         this.frame = frame;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     protected NodeDetector create(final double newMaxCheck, final double newThreshold,
  117.                                   final int newMaxIter, final EventHandler<? super NodeDetector> newHandler) {
  118.         return new NodeDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, frame);
  119.     }

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

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

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

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

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

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

  152.     }

  153.     /** Get the frame in which the equator is defined.
  154.      * @return the frame in which the equator is defined
  155.      */
  156.     public Frame getFrame() {
  157.         return frame;
  158.     }

  159.     /** Compute the value of the switching function.
  160.      * This function computes the Z position in the defined frame.
  161.      * @param s the current state information: date, kinematics, attitude
  162.      * @return value of the switching function
  163.      */
  164.     public double g(final SpacecraftState s) {
  165.         return s.getPVCoordinates(frame).getPosition().getZ();
  166.     }

  167. }