1   /* Copyright 2002-2024 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  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.ode.events.Action;
22  import org.orekit.bodies.OneAxisEllipsoid;
23  import org.orekit.propagation.FieldSpacecraftState;
24  import org.orekit.propagation.events.handlers.FieldEventHandler;
25  import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
26  import org.orekit.utils.ExtendedPVCoordinatesProvider;
27  import org.orekit.utils.OccultationEngine;
28  
29  /** Finder for satellite eclipse related events.
30   * <p>This class finds eclipse events, i.e. satellite within umbra (total
31   * eclipse) or penumbra (partial eclipse).</p>
32   * <p>The default implementation behavior is to {@link Action#CONTINUE continue}
33   * propagation when entering the eclipse and to {@link Action#STOP stop} propagation
34   * when exiting the eclipse. This can be changed by calling {@link
35   * #withHandler(FieldEventHandler)} after construction.</p>
36   * @param <T> the type of the field elements
37   * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
38   * @author Pascal Parraud
39   */
40  public class FieldEclipseDetector<T extends CalculusFieldElement<T>> extends FieldAbstractDetector<FieldEclipseDetector<T>, T> {
41  
42      /** Occultation engine.
43       * @since 12.0
44       */
45      private final OccultationEngine occultationEngine;
46  
47      /** Umbra, if true, or penumbra, if false, detection flag. */
48      private boolean totalEclipse;
49  
50      /** Margin to apply to eclipse angle. */
51      private final T margin;
52  
53      /** Build a new eclipse detector.
54       * <p>The new instance is a total eclipse (umbra) detector with default
55       * values for maximal checking interval ({@link #DEFAULT_MAXCHECK})
56       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
57       * @param field field used by default
58       * @param occulted the body to be occulted
59       * @param occultedRadius the radius of the body to be occulted (m)
60       * @param occulting the occulting body
61       * @since 12.0
62       */
63      public FieldEclipseDetector(final Field<T> field,
64                                  final ExtendedPVCoordinatesProvider occulted, final double occultedRadius,
65                                  final OneAxisEllipsoid occulting) {
66          this(field, new OccultationEngine(occulted, occultedRadius, occulting));
67      }
68  
69      /** Build a new eclipse detector.
70       * <p>The new instance is a total eclipse (umbra) detector with default
71       * values for maximal checking interval ({@link #DEFAULT_MAXCHECK})
72       * and convergence threshold ({@link #DEFAULT_THRESHOLD}).</p>
73       * @param field field used by default
74       * @param occultationEngine occultation engine
75       * @since 12.0
76       */
77      public FieldEclipseDetector(final Field<T> field, final OccultationEngine occultationEngine) {
78          this(FieldAdaptableInterval.of(DEFAULT_MAXCHECK), field.getZero().newInstance(DEFAULT_THRESHOLD),
79               DEFAULT_MAX_ITER, new FieldStopOnIncreasing<>(),
80               occultationEngine, field.getZero(), true);
81      }
82  
83      /** Protected constructor with full parameters.
84       * <p>
85       * This constructor is not public as users are expected to use the builder
86       * API with the various {@code withXxx()} methods to set up the instance
87       * in a readable manner without using a huge amount of parameters.
88       * </p>
89       * @param maxCheck maximum checking interval
90       * @param threshold convergence threshold (s)
91       * @param maxIter maximum number of iterations in the event time search
92       * @param handler event handler to call at event occurrences
93       * @param occultationEngine occultation engine
94       * @param margin to apply to eclipse angle (rad)
95       * @param totalEclipse umbra (true) or penumbra (false) detection flag
96       * @since 12.0
97       */
98      protected FieldEclipseDetector(final FieldAdaptableInterval<T> maxCheck, final T threshold,
99                                     final int maxIter, final FieldEventHandler<T> handler,
100                                    final OccultationEngine occultationEngine, final T margin, final boolean totalEclipse) {
101         super(maxCheck, threshold, maxIter, handler);
102         this.occultationEngine = occultationEngine;
103         this.margin            = margin;
104         this.totalEclipse      = totalEclipse;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     protected FieldEclipseDetector<T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold, final int nawMaxIter,
110                                              final FieldEventHandler<T> newHandler) {
111         return new FieldEclipseDetector<>(newMaxCheck, newThreshold, nawMaxIter, newHandler,
112                                           occultationEngine, margin, totalEclipse);
113     }
114 
115     /**
116      * Setup the detector to full umbra detection.
117      * <p>
118      * This will override a penumbra/umbra flag if it has been configured previously.
119      * </p>
120      * @return a new detector with updated configuration (the instance is not changed)
121      * @see #withPenumbra()
122      * @since 6.1
123      */
124     public FieldEclipseDetector<T> withUmbra() {
125         return new FieldEclipseDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
126                                           occultationEngine, margin, true);
127     }
128 
129     /**
130      * Setup the detector to penumbra detection.
131      * <p>
132      * This will override a penumbra/umbra flag if it has been configured previously.
133      * </p>
134      * @return a new detector with updated configuration (the instance is not changed)
135      * @see #withUmbra()
136      * @since 6.1
137      */
138     public FieldEclipseDetector<T> withPenumbra() {
139         return new FieldEclipseDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
140                                           occultationEngine, margin, false);
141     }
142 
143     /**
144      * Setup a margin to angle detection.
145      * <p>
146      * A positive margin implies eclipses are "larger" hence entry occurs earlier and exit occurs later
147      * than a detector with 0 margin.
148      * </p>
149      * @param newMargin angular margin to apply to eclipse detection (rad)
150      * @return a new detector with updated configuration (the instance is not changed)
151      * @since 12.0
152      */
153     public FieldEclipseDetector<T> withMargin(final T newMargin) {
154         return new FieldEclipseDetector<>(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), getHandler(),
155                                           occultationEngine, newMargin, totalEclipse);
156     }
157 
158     /** Get the angular margin used for eclipse detection.
159      * @return angular margin used for eclipse detection (rad)
160      * @since 12.0
161      */
162     public T getMargin() {
163         return margin;
164     }
165 
166     /** Get the occultation engine.
167      * @return occultation engine
168      * @since 12.0
169      */
170     public OccultationEngine getOccultationEngine() {
171         return occultationEngine;
172     }
173 
174     /** Get the total eclipse detection flag.
175      * @return the total eclipse detection flag (true for umbra events detection,
176      * false for penumbra events detection)
177      */
178     public boolean getTotalEclipse() {
179         return totalEclipse;
180     }
181 
182     /** Compute the value of the switching function.
183      * This function becomes negative when entering the region of shadow
184      * and positive when exiting.
185      * @param s the current state information: date, kinematics, attitude
186      * @return value of the switching function
187      */
188     public T g(final FieldSpacecraftState<T> s) {
189         final OccultationEngine.FieldOccultationAngles<T> angles = occultationEngine.angles(s);
190         return totalEclipse ?
191                angles.getSeparation().subtract(angles.getLimbRadius()).add(angles.getOccultedApparentRadius().add(margin)) :
192                angles.getSeparation().subtract(angles.getLimbRadius()).subtract(angles.getOccultedApparentRadius().add(margin));
193     }
194 
195 }