NodeDetector.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.util.FastMath;
  19. import org.hipparchus.util.MathUtils;
  20. import org.orekit.frames.Frame;
  21. import org.orekit.orbits.KeplerianOrbit;
  22. import org.orekit.orbits.Orbit;
  23. import org.orekit.orbits.OrbitType;
  24. import org.orekit.orbits.PositionAngle;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.events.handlers.EventHandler;
  27. import org.orekit.propagation.events.handlers.StopOnIncreasing;

  28. /** Finder for node crossing events.
  29.  * <p>This class finds equator crossing events (i.e. ascending
  30.  * or descending node crossing).</p>
  31.  * <p>The default implementation behavior is to {@link
  32.  * org.orekit.propagation.events.handlers.EventHandler.Action#CONTINUE continue}
  33.  * propagation at descending node crossing and to {@link
  34.  * org.orekit.propagation.events.handlers.EventHandler.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.     /** Serializable UID. */
  48.     private static final long serialVersionUID = 20131118L;

  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 NodeDetector(final Orbit orbit, final Frame frame) {
  60.         this(1.0e-13 * orbit.getKeplerianPeriod(), 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 NodeDetector(final double threshold, final Orbit orbit, final Frame frame) {
  72.         this(2 * estimateNodesTimeSeparation(orbit) / 3, threshold,
  73.              DEFAULT_MAX_ITER, new StopOnIncreasing<NodeDetector>(),
  74.              frame);
  75.     }

  76.     /** Private constructor with full parameters.
  77.      * <p>
  78.      * This constructor is private 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 (s)
  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.     private NodeDetector(final double maxCheck, final double threshold,
  92.                          final int maxIter, final EventHandler<? super NodeDetector> handler,
  93.                          final Frame frame) {
  94.         super(maxCheck, threshold, maxIter, handler);
  95.         this.frame = frame;
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     protected NodeDetector create(final double newMaxCheck, final double newThreshold,
  100.                                   final int newMaxIter, final EventHandler<? super NodeDetector> newHandler) {
  101.         return new NodeDetector(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(), PositionAngle.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(), PositionAngle.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 double g(final SpacecraftState s) {
  148.         return s.getPVCoordinates(frame).getPosition().getZ();
  149.     }

  150. }