NodeDetector.java

  1. /* Copyright 2002-2018 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.errors.OrekitException;
  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
  33.  * org.orekit.propagation.events.handlers.EventHandler.Action#CONTINUE continue}
  34.  * propagation at descending node crossing and to {@link
  35.  * org.orekit.propagation.events.handlers.EventHandler.Action#STOP stop} propagation
  36.  * at ascending node crossing. This can be changed by calling
  37.  * {@link #withHandler(EventHandler)} 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.Propagator#addEventDetector(EventDetector)
  45.  * @author Luc Maisonobe
  46.  */
  47. public class NodeDetector extends AbstractDetector<NodeDetector> {

  48.     /** Serializable UID. */
  49.     private static final long serialVersionUID = 20131118L;

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

  77.     /** Private constructor with full parameters.
  78.      * <p>
  79.      * This constructor is private as users are expected to use the builder
  80.      * API with the various {@code withXxx()} methods to set up the instance
  81.      * in a readable manner without using a huge amount of parameters.
  82.      * </p>
  83.      * @param maxCheck maximum checking interval (s)
  84.      * @param threshold convergence threshold (s)
  85.      * @param maxIter maximum number of iterations in the event time search
  86.      * @param handler event handler to call at event occurrences
  87.      * @param frame frame in which the equator is defined (typical
  88.      * values are {@link org.orekit.frames.FramesFactory#getEME2000() EME<sub>2000</sub>} or
  89.      * {@link org.orekit.frames.FramesFactory#getITRF(org.orekit.utils.IERSConventions, boolean) ITRF})
  90.      * @since 6.1
  91.      */
  92.     private NodeDetector(final double maxCheck, final double threshold,
  93.                          final int maxIter, final EventHandler<? super NodeDetector> handler,
  94.                          final Frame frame) {
  95.         super(maxCheck, threshold, maxIter, handler);
  96.         this.frame = frame;
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     protected NodeDetector create(final double newMaxCheck, final double newThreshold,
  101.                                   final int newMaxIter, final EventHandler<? super NodeDetector> newHandler) {
  102.         return new NodeDetector(newMaxCheck, newThreshold, newMaxIter, newHandler, frame);
  103.     }

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

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

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

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

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

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

  136.     }

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

  143.     /** Compute the value of the switching function.
  144.      * This function computes the Z position in the defined frame.
  145.      * @param s the current state information: date, kinematics, attitude
  146.      * @return value of the switching function
  147.      * @exception OrekitException if some specific error occurs
  148.      */
  149.     public double g(final SpacecraftState s) throws OrekitException {
  150.         return s.getPVCoordinates(frame).getPosition().getZ();
  151.     }

  152. }