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  
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(FieldAdaptableInterval.of(DEFAULT_MAXCHECK), field.getZero().newInstance(DEFAULT_THRESHOLD), DEFAULT_MAX_ITER,
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 maxCheck maximum checking interval
87       * @param threshold convergence threshold (s)
88       * @param maxIter maximum number of iterations in the event time search
89       * @param handler event handler to call at event occurrences
90       * @param betaAngleThreshold beta angle threshold (radians)
91       * @param celestialBodyProvider coordinate provider for the celestial provider
92       * @param inertialFrame inertial frame in which to compute the beta angle
93       */
94      protected FieldBetaAngleDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold,
95                               final int maxIter, final FieldEventHandler<T> handler,
96                               final T betaAngleThreshold, final FieldPVCoordinatesProvider<T> celestialBodyProvider,
97                               final Frame inertialFrame) {
98          super(maxCheck, threshold, maxIter, handler);
99          this.betaAngleThreshold = betaAngleThreshold;
100         this.celestialBodyProvider = celestialBodyProvider;
101         this.inertialFrame = inertialFrame;
102     }
103 
104     /** Coordinate provider for the celestial body.
105      * @return celestial body's coordinate provider
106      */
107     public FieldPVCoordinatesProvider<T> getCelestialBodyProvider() {
108         return this.celestialBodyProvider;
109     }
110 
111     /** The inertial frame in which beta angle is computed.
112      * @return the inertial frame
113      */
114     public Frame getInertialFrame() {
115         return this.inertialFrame;
116     }
117 
118     /** The beta angle threshold (radians).
119      * @return the beta angle threshold (radians)
120      */
121     public T getBetaAngleThreshold() {
122         return this.betaAngleThreshold;
123     }
124 
125     /** Create a new instance with the provided coordinate provider.
126      * <p>This method does not change the current instance.</p>
127      * @param newProvider the new coordinate provider
128      * @return the new detector instance
129      */
130     public FieldBetaAngleDetector<T> withCelestialProvider(final FieldPVCoordinatesProvider<T> newProvider) {
131         return new FieldBetaAngleDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(),
132                 getHandler(), getBetaAngleThreshold(), newProvider, getInertialFrame());
133     }
134 
135     /** Create a new instance with the provided beta angle threshold.
136      * <p>This method does not change the current instance.</p>
137      * @param newBetaAngleThreshold the beta angle threshold
138      * @return the new detector instance
139      */
140     public FieldBetaAngleDetector<T> withBetaThreshold(final T newBetaAngleThreshold) {
141         return new FieldBetaAngleDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(),
142                 getHandler(), newBetaAngleThreshold, getCelestialBodyProvider(), getInertialFrame());
143     }
144 
145     /** Create a new instance with the provided inertial frame.
146      * <p>This method does not change the current instance.</p>
147      * @param newFrame the inertial frame
148      * @return the new detector instance
149      */
150     public FieldBetaAngleDetector<T> withInertialFrame(final Frame newFrame) {
151         return new FieldBetaAngleDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(),
152                 getHandler(), getBetaAngleThreshold(), getCelestialBodyProvider(), newFrame);
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public T g(final FieldSpacecraftState<T> s) {
158         final T beta = calculateBetaAngle(s, celestialBodyProvider, inertialFrame);
159         return betaAngleThreshold.subtract(beta);
160     }
161 
162     /**Calculate the beta angle between the orbit plane and the celestial body.
163      * <p>This method computes the beta angle using the frame from the spacecraft state.</p>
164      * @param state spacecraft state
165      * @param celestialBodyProvider celestial body coordinate provider
166      * @param <T> The field type
167      * @return the beta angle (radians)
168      */
169     public static <T extends CalculusFieldElement<T>> T calculateBetaAngle(final FieldSpacecraftState<T> state,
170             final FieldPVCoordinatesProvider<T> celestialBodyProvider) {
171         return calculateBetaAngle(state, celestialBodyProvider, state.getFrame());
172     }
173 
174     /**Calculate the beta angle between the orbit plane and the celestial body.
175      * @param state spacecraft state
176      * @param celestialBodyProvider celestial body coordinate provider
177      * @param frame inertial frame in which beta angle will be computed
178      * @param <T> The field type
179      * @return the beta angle (radians)
180      */
181     public static <T extends CalculusFieldElement<T>> T calculateBetaAngle(final FieldSpacecraftState<T> state,
182             final FieldPVCoordinatesProvider<T> celestialBodyProvider, final Frame frame) {
183         final FieldVector3D<T> celestialP = celestialBodyProvider.getPosition(state.getDate(), frame);
184         final TimeStampedFieldPVCoordinates<T> pv = state.getPVCoordinates(frame);
185         return FieldVector3D.angle(celestialP, pv.getMomentum()).negate().add(MathUtils.SEMI_PI);
186     }
187 
188     /** {@inheritDoc} */
189     @Override
190     protected FieldBetaAngleDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
191             final int newMaxIter, final FieldEventHandler<T> newHandler) {
192         return new FieldBetaAngleDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler,
193                 getBetaAngleThreshold(), getCelestialBodyProvider(), getInertialFrame());
194     }
195 }