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  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.hipparchus.util.MathUtils;
24  import org.orekit.annotation.DefaultDataContext;
25  import org.orekit.bodies.CelestialBodyFactory;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.FramesFactory;
28  import org.orekit.propagation.FieldSpacecraftState;
29  import org.orekit.propagation.events.handlers.FieldEventHandler;
30  import org.orekit.propagation.events.handlers.FieldStopOnEvent;
31  import org.orekit.utils.FieldPVCoordinatesProvider;
32  import org.orekit.utils.TimeStampedFieldPVCoordinates;
33  
34  /** Finder for beta angle crossing events.
35   * <p>Locate events when the beta angle (the angle between the orbit plane and the celestial body)
36   * crosses a threshold. The {@link #g(FieldSpacecraftState)} function is negative when the beta angle
37   * is above the threshold and positive when the beta angle is below the threshold.</p>
38   * <p>The inertial frame provided must have it's origin centered at the satellite's orbit plane. The
39   * beta angle is computed as the angle between the celestial body's position in this frame with the
40   * satellite's orbital momentum vector.</p>
41   * <p>The default implementation behavior is to {@link Action#STOP stop}
42   * propagation at the first event date occurrence. This can be changed by calling
43   * {@link #withHandler(FieldEventHandler)} after construction.</p>
44   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
45   * @param <T> The field type
46   * @author Joe Reed
47   * @since 12.1
48   */
49  public class FieldBetaAngleDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldBetaAngleDetector<T>, T> {
50      /** Beta angle crossing threshold. */
51      private final T betaAngleThreshold;
52      /** Coordinate provider for the celestial body. */
53      private final FieldPVCoordinatesProvider<T> celestialBodyProvider;
54      /** Inertial frame in which beta angle is calculated. */
55      private final Frame inertialFrame;
56  
57      /**Solar beta angle constructor.
58       * <p>This method uses the default data context, assigns the sun as the celestial
59       * body and uses GCRF as the inertial frame.</p>
60       * @param betaAngleThreshold beta angle threshold (radians)
61       */
62      @DefaultDataContext
63      public FieldBetaAngleDetector(final T betaAngleThreshold) {
64          this(betaAngleThreshold.getField(), betaAngleThreshold,
65               CelestialBodyFactory.getSun().toFieldPVCoordinatesProvider(betaAngleThreshold.getField()),
66               FramesFactory.getGCRF());
67      }
68  
69      /** Class constructor.
70       * @param field the field instance
71       * @param betaAngleThreshold beta angle threshold (radians)
72       * @param celestialBodyProvider coordinate provider for the celestial provider
73       * @param inertialFrame inertial frame in which to compute the beta angle
74       */
75      public FieldBetaAngleDetector(final Field<T> field, final T betaAngleThreshold,
76              final FieldPVCoordinatesProvider<T> celestialBodyProvider,
77              final Frame inertialFrame) {
78          this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
79                  new FieldStopOnEvent<>(), betaAngleThreshold, celestialBodyProvider, inertialFrame);
80      }
81  
82      /** Protected constructor with full parameters.
83       * <p>This constructor is not public as users are expected to use the builder
84       * API with the various {@code withXxx()} methods to set up the instance
85       * in a readable manner without using a huge amount of parameters.</p>
86       * @param detectionSettings event detection settings
87       * @param handler event handler to call at event occurrences
88       * @param betaAngleThreshold beta angle threshold (radians)
89       * @param celestialBodyProvider coordinate provider for the celestial provider
90       * @param inertialFrame inertial frame in which to compute the beta angle
91       * @since 13.0
92       */
93      protected FieldBetaAngleDetector(final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
94                               final T betaAngleThreshold, final FieldPVCoordinatesProvider<T> celestialBodyProvider,
95                               final Frame inertialFrame) {
96          super(detectionSettings, handler);
97          this.betaAngleThreshold = betaAngleThreshold;
98          this.celestialBodyProvider = celestialBodyProvider;
99          this.inertialFrame = inertialFrame;
100     }
101 
102     /** Coordinate provider for the celestial body.
103      * @return celestial body's coordinate provider
104      */
105     public FieldPVCoordinatesProvider<T> getCelestialBodyProvider() {
106         return this.celestialBodyProvider;
107     }
108 
109     /** The inertial frame in which beta angle is computed.
110      * @return the inertial frame
111      */
112     public Frame getInertialFrame() {
113         return this.inertialFrame;
114     }
115 
116     /** The beta angle threshold (radians).
117      * @return the beta angle threshold (radians)
118      */
119     public T getBetaAngleThreshold() {
120         return this.betaAngleThreshold;
121     }
122 
123     /** Create a new instance with the provided coordinate provider.
124      * <p>This method does not change the current instance.</p>
125      * @param newProvider the new coordinate provider
126      * @return the new detector instance
127      */
128     public FieldBetaAngleDetector<T> withCelestialProvider(final FieldPVCoordinatesProvider<T> newProvider) {
129         return new FieldBetaAngleDetector<>(getDetectionSettings(),
130                 getHandler(), getBetaAngleThreshold(), newProvider, getInertialFrame());
131     }
132 
133     /** Create a new instance with the provided beta angle threshold.
134      * <p>This method does not change the current instance.</p>
135      * @param newBetaAngleThreshold the beta angle threshold
136      * @return the new detector instance
137      */
138     public FieldBetaAngleDetector<T> withBetaThreshold(final T newBetaAngleThreshold) {
139         return new FieldBetaAngleDetector<>(getDetectionSettings(),
140                 getHandler(), newBetaAngleThreshold, getCelestialBodyProvider(), getInertialFrame());
141     }
142 
143     /** Create a new instance with the provided inertial frame.
144      * <p>This method does not change the current instance.</p>
145      * @param newFrame the inertial frame
146      * @return the new detector instance
147      */
148     public FieldBetaAngleDetector<T> withInertialFrame(final Frame newFrame) {
149         return new FieldBetaAngleDetector<>(getDetectionSettings(),
150                 getHandler(), getBetaAngleThreshold(), getCelestialBodyProvider(), newFrame);
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public T g(final FieldSpacecraftState<T> s) {
156         final T beta = calculateBetaAngle(s, celestialBodyProvider, inertialFrame);
157         return betaAngleThreshold.subtract(beta);
158     }
159 
160     /**Calculate the beta angle between the orbit plane and the celestial body.
161      * <p>This method computes the beta angle using the frame from the spacecraft state.</p>
162      * @param state spacecraft state
163      * @param celestialBodyProvider celestial body coordinate provider
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) {
169         return calculateBetaAngle(state, celestialBodyProvider, state.getFrame());
170     }
171 
172     /**Calculate the beta angle between the orbit plane and the celestial body.
173      * @param state spacecraft state
174      * @param celestialBodyProvider celestial body coordinate provider
175      * @param frame inertial frame in which beta angle will be computed
176      * @param <T> The field type
177      * @return the beta angle (radians)
178      */
179     public static <T extends CalculusFieldElement<T>> T calculateBetaAngle(final FieldSpacecraftState<T> state,
180             final FieldPVCoordinatesProvider<T> celestialBodyProvider, final Frame frame) {
181         final FieldVector3D<T> celestialP = celestialBodyProvider.getPosition(state.getDate(), frame);
182         final TimeStampedFieldPVCoordinates<T> pv = state.getPVCoordinates(frame);
183         return FieldVector3D.angle(celestialP, pv.getMomentum()).negate().add(MathUtils.SEMI_PI);
184     }
185 
186     /** {@inheritDoc} */
187     @Override
188     protected FieldBetaAngleDetector<T> create(final FieldEventDetectionSettings<T> detectionSettings,
189                                                final FieldEventHandler<T> newHandler) {
190         return new FieldBetaAngleDetector<>(detectionSettings, newHandler,
191                 getBetaAngleThreshold(), getCelestialBodyProvider(), getInertialFrame());
192     }
193 }