PositionAngleDetector.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 java.util.function.Function;

  19. import org.hipparchus.analysis.UnivariateFunction;
  20. import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathUtils;
  23. import org.orekit.errors.OrekitIllegalArgumentException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.orbits.CircularOrbit;
  26. import org.orekit.orbits.EquinoctialOrbit;
  27. import org.orekit.orbits.KeplerianOrbit;
  28. import org.orekit.orbits.Orbit;
  29. import org.orekit.orbits.OrbitType;
  30. import org.orekit.orbits.PositionAngle;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.propagation.events.handlers.EventHandler;
  33. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.utils.TimeSpanMap;

  36. /** Detector for in-orbit position angle.
  37.  * <p>
  38.  * The detector is based on anomaly for {@link OrbitType#KEPLERIAN Keplerian}
  39.  * orbits, latitude argument for {@link OrbitType#CIRCULAR circular} orbits,
  40.  * or longitude argument for {@link OrbitType#EQUINOCTIAL equinoctial} orbits.
  41.  * It does not support {@link OrbitType#CARTESIAN Cartesian} orbits. The
  42.  * angles can be either {@link PositionAngle#TRUE true}, {link {@link PositionAngle#MEAN
  43.  * mean} or {@link PositionAngle#ECCENTRIC eccentric} angles.
  44.  * </p>
  45.  * @author Luc Maisonobe
  46.  * @since 7.1
  47.  */
  48. public class PositionAngleDetector extends AbstractDetector<PositionAngleDetector> {

  49.     /** Serializable UID. */
  50.     private static final long serialVersionUID = 20180919L;

  51.     /** Orbit type defining the angle type. */
  52.     private final OrbitType orbitType;

  53.     /** Type of position angle. */
  54.     private final PositionAngle positionAngle;

  55.     /** Fixed angle to be crossed. */
  56.     private final double angle;

  57.     /** Position angle extraction function. */
  58.     private final Function<Orbit, Double> positionAngleExtractor;

  59.     /** Estimators for the offset angle, taking care of 2π wrapping and g function continuity. */
  60.     private TimeSpanMap<OffsetEstimator> offsetEstimators;

  61.     /** Build a new detector.
  62.      * <p>The new instance uses default values for maximal checking interval
  63.      * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
  64.      * #DEFAULT_THRESHOLD}).</p>
  65.      * @param orbitType orbit type defining the angle type
  66.      * @param positionAngle type of position angle
  67.      * @param angle fixed angle to be crossed
  68.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  69.      */
  70.     public PositionAngleDetector(final OrbitType orbitType, final PositionAngle positionAngle,
  71.                                  final double angle)
  72.         throws OrekitIllegalArgumentException {
  73.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, orbitType, positionAngle, angle);
  74.     }

  75.     /** Build a detector.
  76.      * @param maxCheck maximal checking interval (s)
  77.      * @param threshold convergence threshold (s)
  78.      * @param orbitType orbit type defining the angle type
  79.      * @param positionAngle type of position angle
  80.      * @param angle fixed angle to be crossed
  81.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  82.      */
  83.     public PositionAngleDetector(final double maxCheck, final double threshold,
  84.                                  final OrbitType orbitType, final PositionAngle positionAngle,
  85.                                  final double angle)
  86.         throws OrekitIllegalArgumentException {
  87.         this(maxCheck, threshold, DEFAULT_MAX_ITER, new StopOnIncreasing<PositionAngleDetector>(),
  88.              orbitType, positionAngle, angle);
  89.     }

  90.     /** Private constructor with full parameters.
  91.      * <p>
  92.      * This constructor is private as users are expected to use the builder
  93.      * API with the various {@code withXxx()} methods to set up the instance
  94.      * in a readable manner without using a huge amount of parameters.
  95.      * </p>
  96.      * @param maxCheck maximum checking interval (s)
  97.      * @param threshold convergence threshold (s)
  98.      * @param maxIter maximum number of iterations in the event time search
  99.      * @param handler event handler to call at event occurrences
  100.      * @param orbitType orbit type defining the angle type
  101.      * @param positionAngle type of position angle
  102.      * @param angle fixed angle to be crossed
  103.      * @exception OrekitIllegalArgumentException if orbit type is {@link OrbitType#CARTESIAN}
  104.      */
  105.     private PositionAngleDetector(final double maxCheck, final double threshold,
  106.                                      final int maxIter, final EventHandler<? super PositionAngleDetector> handler,
  107.                                      final OrbitType orbitType, final PositionAngle positionAngle,
  108.                                      final double angle)
  109.         throws OrekitIllegalArgumentException {

  110.         super(maxCheck, threshold, maxIter, handler);

  111.         this.orbitType        = orbitType;
  112.         this.positionAngle    = positionAngle;
  113.         this.angle            = angle;
  114.         this.offsetEstimators = null;

  115.         switch (orbitType) {
  116.             case KEPLERIAN:
  117.                 positionAngleExtractor = o -> ((KeplerianOrbit) orbitType.convertType(o)).getAnomaly(positionAngle);
  118.                 break;
  119.             case CIRCULAR:
  120.                 positionAngleExtractor = o -> ((CircularOrbit) orbitType.convertType(o)).getAlpha(positionAngle);
  121.                 break;
  122.             case EQUINOCTIAL:
  123.                 positionAngleExtractor = o -> ((EquinoctialOrbit) orbitType.convertType(o)).getL(positionAngle);
  124.                 break;
  125.             default:
  126.                 final String sep = ", ";
  127.                 throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_TYPE_NOT_ALLOWED,
  128.                                                          orbitType,
  129.                                                          OrbitType.KEPLERIAN   + sep +
  130.                                                          OrbitType.CIRCULAR    + sep +
  131.                                                          OrbitType.EQUINOCTIAL);
  132.         }

  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     protected PositionAngleDetector create(final double newMaxCheck, final double newThreshold,
  137.                                               final int newMaxIter,
  138.                                               final EventHandler<? super PositionAngleDetector> newHandler) {
  139.         return new PositionAngleDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  140.                                          orbitType, positionAngle, angle);
  141.     }

  142.     /** Get the orbit type defining the angle type.
  143.      * @return orbit type defining the angle type
  144.      */
  145.     public OrbitType getOrbitType() {
  146.         return orbitType;
  147.     }

  148.     /** Get the type of position angle.
  149.      * @return type of position angle
  150.      */
  151.     public PositionAngle getPositionAngle() {
  152.         return positionAngle;
  153.     }

  154.     /** Get the fixed angle to be crossed (radians).
  155.      * @return fixed angle to be crossed (radians)
  156.      */
  157.     public double getAngle() {
  158.         return angle;
  159.     }

  160.     /** {@inheritDoc} */
  161.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  162.         super.init(s0, t);
  163.         offsetEstimators = new TimeSpanMap<>(new OffsetEstimator(s0.getOrbit(), +1.0));
  164.     }

  165.     /** Compute the value of the detection function.
  166.      * <p>
  167.      * The value is the angle difference between the spacecraft and the fixed
  168.      * angle to be crossed, with some sign tweaks to ensure continuity.
  169.      * These tweaks imply the {@code increasing} flag in events detection becomes
  170.      * irrelevant here! As an example, the angle always increase in a Keplerian
  171.      * orbit, but this g function will increase and decrease so it
  172.      * will cross the zero value once per orbit, in increasing and decreasing
  173.      * directions on alternate orbits..
  174.      * </p>
  175.      * @param s the current state information: date, kinematics, attitude
  176.      * @return angle difference between the spacecraft and the fixed
  177.      * angle, with some sign tweaks to ensure continuity
  178.      */
  179.     public double g(final SpacecraftState s) {

  180.         final Orbit orbit = s.getOrbit();

  181.         // angle difference
  182.         OffsetEstimator estimator = offsetEstimators.get(s.getDate());
  183.         double          delta     = estimator.delta(orbit);

  184.         // we use a value greater than π for handover in order to avoid
  185.         // several switches to be estimated as the calling propagator
  186.         // and Orbit.shiftedBy have different accuracy. It is sufficient
  187.         // to have a handover roughly opposite to the detected position angle
  188.         while (FastMath.abs(delta) >= 3.5) {
  189.             // we are too far away from the current estimator, we need to set up a new one
  190.             // ensuring that we do have a crossing event in the current orbit
  191.             // and we ensure sign continuity with the current estimator

  192.             // find when the previous estimator becomes invalid
  193.             final AbsoluteDate handover = estimator.dateForOffset(FastMath.copySign(FastMath.PI, delta), orbit);

  194.             // perform handover to a new estimator at this date
  195.             estimator = new OffsetEstimator(orbit, delta);
  196.             delta     = estimator.delta(orbit);
  197.             if (isForward()) {
  198.                 offsetEstimators.addValidAfter(estimator, handover.getDate());
  199.             } else {
  200.                 offsetEstimators.addValidBefore(estimator, handover.getDate());
  201.             }

  202.         }

  203.         return delta;

  204.     }

  205.     /** Local class for estimating offset angle, handling 2π wrap-up and sign continuity. */
  206.     private class OffsetEstimator {

  207.         /** Target angle. */
  208.         private final double target;

  209.         /** Sign correction to offset. */
  210.         private final double sign;

  211.         /** Reference angle. */
  212.         private final double r0;

  213.         /** Slope of the linearized model. */
  214.         private final double r1;

  215.         /** Reference date. */
  216.         private final AbsoluteDate t0;

  217.         /** Simple constructor.
  218.          * @param orbit current orbit
  219.          * @param currentSign desired sign of the offset at current orbit time (magnitude is ignored)
  220.          */
  221.         OffsetEstimator(final Orbit orbit, final double currentSign) {
  222.             r0     = positionAngleExtractor.apply(orbit);
  223.             target = MathUtils.normalizeAngle(angle, r0);
  224.             sign   = FastMath.copySign(1.0, (r0 - target) * currentSign);
  225.             r1     = orbit.getKeplerianMeanMotion();
  226.             t0     = orbit.getDate();
  227.         }

  228.         /** Compute offset from reference angle.
  229.          * @param orbit current orbit
  230.          * @return offset between current angle and reference angle
  231.          */
  232.         public double delta(final Orbit orbit) {
  233.             final double rawAngle        = positionAngleExtractor.apply(orbit);
  234.             final double linearReference = r0 + r1 * orbit.getDate().durationFrom(t0);
  235.             final double linearizedAngle = MathUtils.normalizeAngle(rawAngle, linearReference);
  236.             return sign * (linearizedAngle - target);
  237.         }

  238.         /** Find date at which offset reaches specified value.
  239.          * <p>
  240.          * This computation is an approximation because it relies on
  241.          * {@link Orbit#shiftedBy(double)} only.
  242.          * </p>
  243.          * @param offset target value for offset angle
  244.          * @param orbit current orbit
  245.          * @return approximate date at which offset reached specified value
  246.          */
  247.         public AbsoluteDate dateForOffset(final double offset, final Orbit orbit) {

  248.             // bracket the search
  249.             final double period = orbit.getKeplerianPeriod();
  250.             final double delta0 = delta(orbit);
  251.             final double searchInf;
  252.             final double searchSup;
  253.             if ((delta0 - offset) * sign >= 0) {
  254.                 // the date is before current orbit
  255.                 searchInf = -period;
  256.                 searchSup = 0;
  257.             } else {
  258.                 // the date is after current orbit
  259.                 searchInf = 0;
  260.                 searchSup = +period;
  261.             }

  262.             // find the date as an offset from current orbit
  263.             final BracketingNthOrderBrentSolver solver = new BracketingNthOrderBrentSolver(getThreshold(), 5);
  264.             final UnivariateFunction            f      = dt -> delta(orbit.shiftedBy(dt)) - offset;
  265.             final double                        root   = solver.solve(getMaxIterationCount(), f, searchInf, searchSup);

  266.             return orbit.getDate().shiftedBy(root);

  267.         }

  268.     }

  269. }