FieldBetaAngleDetector.java

  1. /* Copyright 2002-2024 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(FieldAdaptableInterval.of(DEFAULT_MAXCHECK), field.getZero().newInstance(DEFAULT_THRESHOLD), DEFAULT_MAX_ITER,
  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 maxCheck maximum checking interval
  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 betaAngleThreshold beta angle threshold (radians)
  86.      * @param celestialBodyProvider coordinate provider for the celestial provider
  87.      * @param inertialFrame inertial frame in which to compute the beta angle
  88.      */
  89.     protected FieldBetaAngleDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold,
  90.                              final int maxIter, final FieldEventHandler<T> handler,
  91.                              final T betaAngleThreshold, final FieldPVCoordinatesProvider<T> celestialBodyProvider,
  92.                              final Frame inertialFrame) {
  93.         super(maxCheck, threshold, maxIter, handler);
  94.         this.betaAngleThreshold = betaAngleThreshold;
  95.         this.celestialBodyProvider = celestialBodyProvider;
  96.         this.inertialFrame = inertialFrame;
  97.     }

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

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

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

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

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

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

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

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

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

  173.     /** {@inheritDoc} */
  174.     @Override
  175.     protected FieldBetaAngleDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
  176.             final int newMaxIter, final FieldEventHandler<T> newHandler) {
  177.         return new FieldBetaAngleDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler,
  178.                 getBetaAngleThreshold(), getCelestialBodyProvider(), getInertialFrame());
  179.     }
  180. }