FieldBetaAngleDetector.java

  1. /* Copyright 2002-2025 Joseph Reed
  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.  * Joseph Reed 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.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.hipparchus.ode.events.Action;
  22. import org.hipparchus.util.MathUtils;
  23. import org.orekit.annotation.DefaultDataContext;
  24. import org.orekit.bodies.CelestialBodyFactory;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.frames.FramesFactory;
  27. import org.orekit.propagation.FieldSpacecraftState;
  28. import org.orekit.propagation.events.handlers.FieldEventHandler;
  29. import org.orekit.propagation.events.handlers.FieldStopOnEvent;
  30. import org.orekit.utils.FieldPVCoordinatesProvider;
  31. import org.orekit.utils.TimeStampedFieldPVCoordinates;

  32. /** Finder for beta angle crossing events.
  33.  * <p>Locate events when the beta angle (the angle between the orbit plane and the celestial body)
  34.  * crosses a threshold. The {@link #g(FieldSpacecraftState)} function is negative when the beta angle
  35.  * is above the threshold and positive when the beta angle is below the threshold.</p>
  36.  * <p>The inertial frame provided must have it's origin centered at the satellite's orbit plane. The
  37.  * beta angle is computed as the angle between the celestial body's position in this frame with the
  38.  * satellite's orbital momentum vector.</p>
  39.  * <p>The default implementation behavior is to {@link Action#STOP stop}
  40.  * propagation at the first event date occurrence. This can be changed by calling
  41.  * {@link #withHandler(FieldEventHandler)} after construction.</p>
  42.  * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
  43.  * @param <T> The field type
  44.  * @author Joe Reed
  45.  * @since 12.1
  46.  */
  47. public class FieldBetaAngleDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldBetaAngleDetector<T>, T> {
  48.     /** Beta angle crossing threshold. */
  49.     private final T betaAngleThreshold;
  50.     /** Coordinate provider for the celestial body. */
  51.     private final FieldPVCoordinatesProvider<T> celestialBodyProvider;
  52.     /** Inertial frame in which beta angle is calculated. */
  53.     private final Frame inertialFrame;

  54.     /**Solar beta angle constructor.
  55.      * <p>This method uses the default data context, assigns the sun as the celestial
  56.      * body and uses GCRF as the inertial frame.</p>
  57.      * @param betaAngleThreshold beta angle threshold (radians)
  58.      */
  59.     @DefaultDataContext
  60.     public FieldBetaAngleDetector(final T betaAngleThreshold) {
  61.         this(betaAngleThreshold.getField(), betaAngleThreshold,
  62.              CelestialBodyFactory.getSun().toFieldPVCoordinatesProvider(betaAngleThreshold.getField()),
  63.              FramesFactory.getGCRF());
  64.     }

  65.     /** Class constructor.
  66.      * @param field the field instance
  67.      * @param betaAngleThreshold beta angle threshold (radians)
  68.      * @param celestialBodyProvider coordinate provider for the celestial provider
  69.      * @param inertialFrame inertial frame in which to compute the beta angle
  70.      */
  71.     public FieldBetaAngleDetector(final Field<T> field, final T betaAngleThreshold,
  72.             final FieldPVCoordinatesProvider<T> celestialBodyProvider,
  73.             final Frame inertialFrame) {
  74.         this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
  75.                 new FieldStopOnEvent<>(), betaAngleThreshold, celestialBodyProvider, inertialFrame);
  76.     }

  77.     /** Protected constructor with full parameters.
  78.      * <p>This constructor is not public 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.</p>
  81.      * @param detectionSettings event detection settings
  82.      * @param handler event handler to call at event occurrences
  83.      * @param betaAngleThreshold beta angle threshold (radians)
  84.      * @param celestialBodyProvider coordinate provider for the celestial provider
  85.      * @param inertialFrame inertial frame in which to compute the beta angle
  86.      * @since 13.0
  87.      */
  88.     protected FieldBetaAngleDetector(final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
  89.                              final T betaAngleThreshold, final FieldPVCoordinatesProvider<T> celestialBodyProvider,
  90.                              final Frame inertialFrame) {
  91.         super(detectionSettings, handler);
  92.         this.betaAngleThreshold = betaAngleThreshold;
  93.         this.celestialBodyProvider = celestialBodyProvider;
  94.         this.inertialFrame = inertialFrame;
  95.     }

  96.     /** Coordinate provider for the celestial body.
  97.      * @return celestial body's coordinate provider
  98.      */
  99.     public FieldPVCoordinatesProvider<T> getCelestialBodyProvider() {
  100.         return this.celestialBodyProvider;
  101.     }

  102.     /** The inertial frame in which beta angle is computed.
  103.      * @return the inertial frame
  104.      */
  105.     public Frame getInertialFrame() {
  106.         return this.inertialFrame;
  107.     }

  108.     /** The beta angle threshold (radians).
  109.      * @return the beta angle threshold (radians)
  110.      */
  111.     public T getBetaAngleThreshold() {
  112.         return this.betaAngleThreshold;
  113.     }

  114.     /** Create a new instance with the provided coordinate provider.
  115.      * <p>This method does not change the current instance.</p>
  116.      * @param newProvider the new coordinate provider
  117.      * @return the new detector instance
  118.      */
  119.     public FieldBetaAngleDetector<T> withCelestialProvider(final FieldPVCoordinatesProvider<T> newProvider) {
  120.         return new FieldBetaAngleDetector<>(getDetectionSettings(),
  121.                 getHandler(), getBetaAngleThreshold(), newProvider, getInertialFrame());
  122.     }

  123.     /** Create a new instance with the provided beta angle threshold.
  124.      * <p>This method does not change the current instance.</p>
  125.      * @param newBetaAngleThreshold the beta angle threshold
  126.      * @return the new detector instance
  127.      */
  128.     public FieldBetaAngleDetector<T> withBetaThreshold(final T newBetaAngleThreshold) {
  129.         return new FieldBetaAngleDetector<>(getDetectionSettings(),
  130.                 getHandler(), newBetaAngleThreshold, getCelestialBodyProvider(), getInertialFrame());
  131.     }

  132.     /** Create a new instance with the provided inertial frame.
  133.      * <p>This method does not change the current instance.</p>
  134.      * @param newFrame the inertial frame
  135.      * @return the new detector instance
  136.      */
  137.     public FieldBetaAngleDetector<T> withInertialFrame(final Frame newFrame) {
  138.         return new FieldBetaAngleDetector<>(getDetectionSettings(),
  139.                 getHandler(), getBetaAngleThreshold(), getCelestialBodyProvider(), newFrame);
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public T g(final FieldSpacecraftState<T> s) {
  144.         final T beta = calculateBetaAngle(s, celestialBodyProvider, inertialFrame);
  145.         return betaAngleThreshold.subtract(beta);
  146.     }

  147.     /**Calculate the beta angle between the orbit plane and the celestial body.
  148.      * <p>This method computes the beta angle using the frame from the spacecraft state.</p>
  149.      * @param state spacecraft state
  150.      * @param celestialBodyProvider celestial body coordinate provider
  151.      * @param <T> The field type
  152.      * @return the beta angle (radians)
  153.      */
  154.     public static <T extends CalculusFieldElement<T>> T calculateBetaAngle(final FieldSpacecraftState<T> state,
  155.             final FieldPVCoordinatesProvider<T> celestialBodyProvider) {
  156.         return calculateBetaAngle(state, celestialBodyProvider, state.getFrame());
  157.     }

  158.     /**Calculate the beta angle between the orbit plane and the celestial body.
  159.      * @param state spacecraft state
  160.      * @param celestialBodyProvider celestial body coordinate provider
  161.      * @param frame inertial frame in which beta angle will be computed
  162.      * @param <T> The field type
  163.      * @return the beta angle (radians)
  164.      */
  165.     public static <T extends CalculusFieldElement<T>> T calculateBetaAngle(final FieldSpacecraftState<T> state,
  166.             final FieldPVCoordinatesProvider<T> celestialBodyProvider, final Frame frame) {
  167.         final FieldVector3D<T> celestialP = celestialBodyProvider.getPosition(state.getDate(), frame);
  168.         final TimeStampedFieldPVCoordinates<T> pv = state.getPVCoordinates(frame);
  169.         return FieldVector3D.angle(celestialP, pv.getMomentum()).negate().add(MathUtils.SEMI_PI);
  170.     }

  171.     /** {@inheritDoc} */
  172.     @Override
  173.     protected FieldBetaAngleDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
  174.                                                final FieldEventHandler<T> newHandler) {
  175.         return new FieldBetaAngleDetector<>(detectionSettings, newHandler,
  176.                 getBetaAngleThreshold(), getCelestialBodyProvider(), getInertialFrame());
  177.     }
  178. }