EclipseDetector.java

  1. /* Copyright 2002-2022 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.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.ode.events.Action;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.bodies.OneAxisEllipsoid;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.EventHandler;
  24. import org.orekit.propagation.events.handlers.StopOnIncreasing;
  25. import org.orekit.utils.PVCoordinatesProvider;

  26. /** Finder for satellite eclipse related events.
  27.  * <p>This class finds eclipse events, i.e. satellite within umbra (total
  28.  * eclipse) or penumbra (partial eclipse).</p>
  29.  * <p>The occulted body is given through a {@link PVCoordinatesProvider} and its radius in meters. It is modeled as a sphere.
  30.  * </p>
  31.  * <p>Since v10.0 the occulting body is a {@link OneAxisEllipsoid}, before it was modeled as a  sphere.
  32.  * <br>It was changed to precisely model Solar eclipses by the Earth, especially for Low Earth Orbits.
  33.  * <br>If you want eclipses by a spherical occulting body, set its flattening to 0. when defining its OneAxisEllipsoid model..
  34.  * </p>
  35.  * <p>The {@link #withUmbra} or {@link #withPenumbra} methods will tell you if the event is triggered when complete umbra/lighting
  36.  * is achieved or when entering/living the penumbra zone.
  37.  * <br>The default behavior is detecting complete umbra/lighting events.
  38.  * <br>If you want to have both, you'll need to set up two distinct detectors.
  39.  * </p>
  40.  * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
  41.  * propagation when entering the eclipse and to {@link Action#STOP stop} propagation
  42.  * when exiting the eclipse.
  43.  * <br>This can be changed by calling {@link #withHandler(EventHandler)} after construction.
  44.  * </p>
  45.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  46.  * @author Pascal Parraud
  47.  * @author Luc Maisonobe
  48.  */
  49. public class EclipseDetector extends AbstractDetector<EclipseDetector> {

  50.     /** Occulting body. */
  51.     private final OneAxisEllipsoid occulting;

  52.     /** Occulted body. */
  53.     private final PVCoordinatesProvider occulted;

  54.     /** Occulted body radius (m). */
  55.     private final double occultedRadius;

  56.     /** Umbra, if true, or penumbra, if false, detection flag. */
  57.     private final boolean totalEclipse;

  58.     /** Build a new eclipse detector.
  59.      * <p>The new instance is a total eclipse (umbra) detector with default
  60.      * values for maximal checking interval ({@link #DEFAULT_MAXCHECK})
  61.      * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
  62.      * @param occulted the body to be occulted
  63.      * @param occultedRadius the radius of the body to be occulted (m)
  64.      * @param occulting the occulting body
  65.      * @since 10.0
  66.      */
  67.     public EclipseDetector(final PVCoordinatesProvider occulted,  final double occultedRadius,
  68.                            final OneAxisEllipsoid occulting) {
  69.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  70.              new StopOnIncreasing<EclipseDetector>(),
  71.              occulted, occultedRadius, occulting, true);
  72.     }

  73.     /** Private constructor with full parameters.
  74.      * <p>
  75.      * This constructor is private as users are expected to use the builder
  76.      * API with the various {@code withXxx()} methods to set up the instance
  77.      * in a readable manner without using a huge amount of parameters.
  78.      * </p>
  79.      * @param maxCheck maximum checking interval (s)
  80.      * @param threshold convergence threshold (s)
  81.      * @param maxIter maximum number of iterations in the event time search
  82.      * @param handler event handler to call at event occurrences
  83.      * @param occulted the body to be occulted
  84.      * @param occultedRadius the radius of the body to be occulted in meters
  85.      * @param occulting the occulting body
  86.      * @param totalEclipse umbra (true) or penumbra (false) detection flag
  87.      * @since 10.0
  88.      */
  89.     private EclipseDetector(final double maxCheck, final double threshold,
  90.                             final int maxIter, final EventHandler<? super EclipseDetector> handler,
  91.                             final PVCoordinatesProvider occulted,  final double occultedRadius,
  92.                             final OneAxisEllipsoid occulting, final boolean totalEclipse) {
  93.         super(maxCheck, threshold, maxIter, handler);
  94.         this.occulted       = occulted;
  95.         this.occultedRadius = FastMath.abs(occultedRadius);
  96.         this.occulting      = occulting;
  97.         this.totalEclipse   = totalEclipse;
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     protected EclipseDetector create(final double newMaxCheck, final double newThreshold,
  102.                                      final int nawMaxIter, final EventHandler<? super EclipseDetector> newHandler) {
  103.         return new EclipseDetector(newMaxCheck, newThreshold, nawMaxIter, newHandler,
  104.                                    occulted, occultedRadius, occulting, totalEclipse);
  105.     }

  106.     /**
  107.      * Setup the detector to full umbra detection.
  108.      * <p>
  109.      * This will override a penumbra/umbra flag if it has been configured previously.
  110.      * </p>
  111.      * @return a new detector with updated configuration (the instance is not changed)
  112.      * @see #withPenumbra()
  113.      * @since 6.1
  114.      */
  115.     public EclipseDetector withUmbra() {
  116.         return new EclipseDetector(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
  117.                                    occulted, occultedRadius, occulting, true);
  118.     }

  119.     /**
  120.      * Setup the detector to penumbra detection.
  121.      * <p>
  122.      * This will override a penumbra/umbra flag if it has been configured previously.
  123.      * </p>
  124.      * @return a new detector with updated configuration (the instance is not changed)
  125.      * @see #withUmbra()
  126.      * @since 6.1
  127.      */
  128.     public EclipseDetector withPenumbra() {
  129.         return new EclipseDetector(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
  130.                                    occulted, occultedRadius, occulting, false);
  131.     }

  132.     /** Getter for the occulting body.
  133.      * @return the occulting body
  134.      */
  135.     public OneAxisEllipsoid getOcculting() {
  136.         return occulting;
  137.     }

  138.     /** Getter for the occulted body.
  139.      * @return the occulted body
  140.      */
  141.     public PVCoordinatesProvider getOcculted() {
  142.         return occulted;
  143.     }

  144.     /** Getter for the occultedRadius.
  145.      * @return the occultedRadius
  146.      */
  147.     public double getOccultedRadius() {
  148.         return occultedRadius;
  149.     }

  150.     /** Get the total eclipse detection flag.
  151.      * @return the total eclipse detection flag (true for umbra events detection,
  152.      * false for penumbra events detection)
  153.      */
  154.     public boolean getTotalEclipse() {
  155.         return totalEclipse;
  156.     }

  157.     /** Compute the value of the switching function.
  158.      * This function becomes negative when entering the region of shadow
  159.      * and positive when exiting.
  160.      * @param s the current state information: date, kinematics, attitude
  161.      * @return value of the switching function
  162.      */
  163.     public double g(final SpacecraftState s) {
  164.         final Vector3D pted  = occulted.getPVCoordinates(s.getDate(), occulting.getBodyFrame()).getPosition();
  165.         final Vector3D psat  = s.getPVCoordinates(occulting.getBodyFrame()).getPosition();
  166.         final Vector3D plimb = occulting.pointOnLimb(psat, pted);
  167.         final Vector3D ps    = psat.subtract(pted);
  168.         final Vector3D pi    = psat.subtract(plimb);
  169.         final double angle   = Vector3D.angle(ps, psat);
  170.         final double rs      = FastMath.asin(occultedRadius / ps.getNorm());
  171.         if (Double.isNaN(rs)) {
  172.             return FastMath.PI;
  173.         }
  174.         final double ro = Vector3D.angle(pi, psat);
  175.         return totalEclipse ? (angle - ro + rs) : (angle - ro - rs);
  176.     }
  177. }