FieldNodeDetector.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.RealFieldElement;
  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.PositionAngle;
  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.  */
  47. public class FieldNodeDetector<T extends RealFieldElement<T>> extends FieldAbstractDetector<FieldNodeDetector<T>, T> {

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

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

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

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

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

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

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

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

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

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

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

  134.     }

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

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

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

  152. }