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 java.util.List;
20  import java.util.stream.Collectors;
21  
22  import org.hipparchus.util.FastMath;
23  import org.orekit.errors.OrekitException;
24  import org.orekit.errors.OrekitMessages;
25  import org.orekit.propagation.SpacecraftState;
26  import org.orekit.propagation.events.handlers.EventHandler;
27  import org.orekit.propagation.events.handlers.StopOnDecreasing;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.DateDriver;
30  import org.orekit.utils.ParameterDriver;
31  import org.orekit.utils.ParameterObserver;
32  import org.orekit.utils.TimeSpanMap;
33  import org.orekit.utils.TimeSpanMap.Span;
34  
35  /** Detector for date intervals that may be offset thanks to parameter drivers.
36   * <p>
37   * Two dual views can be used for date intervals: either start date/stop date or
38   * median date/duration. {@link #getStartDriver() start}/{@link #getStopDriver() stop}
39   * drivers and {@link #getMedianDriver() median}/{@link #getDurationDriver() duration}
40   * drivers work in pair. Both drivers in one pair can be selected and their changes will
41   * be propagated to the other pair, but attempting to select drivers in both
42   * pairs at the same time will trigger an exception. Changing the value of a driver
43   * that is not selected should be avoided as it leads to inconsistencies between the pairs.
44   * </p>. Warning, startDate driver, stopDate driver, duration driver and medianDate driver
45   * must all have the same number of values to estimate (same number of span in valueSpanMap), that is is to
46   * say that the {@link org.orekit.utils.ParameterDriver#addSpans(AbsoluteDate, AbsoluteDate, double)}
47   * should be called with same arguments.
48   * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
49   * @author Luc Maisonobe
50   * @since 11.1
51   */
52  public class ParameterDrivenDateIntervalDetector extends AbstractDetector<ParameterDrivenDateIntervalDetector> {
53  
54      /** Default suffix for start driver. */
55      public static final String START_SUFFIX = "_START";
56  
57      /** Default suffix for stop driver. */
58      public static final String STOP_SUFFIX = "_STOP";
59  
60      /** Default suffix for median driver. */
61      public static final String MEDIAN_SUFFIX = "_MEDIAN";
62  
63      /** Default suffix for duration driver. */
64      public static final String DURATION_SUFFIX = "_DURATION";
65  
66      /** Detection threshold. */
67      private static final double THRESHOLD = 1.0e-10;
68  
69      /** Reference interval start driver. */
70      private DateDriver start;
71  
72      /** Reference interval stop driver. */
73      private DateDriver stop;
74  
75      /** Median date driver. */
76      private DateDriver median;
77  
78      /** Duration driver. */
79      private ParameterDriver duration;
80  
81      /** Build a new instance.
82       * @param prefix prefix to use for parameter drivers names
83       * @param refMedian reference interval median date
84       * @param refDuration reference duration
85       */
86      public ParameterDrivenDateIntervalDetector(final String prefix,
87                                                 final AbsoluteDate refMedian, final double refDuration) {
88          this(prefix,
89               refMedian.shiftedBy(-0.5 * refDuration),
90               refMedian.shiftedBy(+0.5 * refDuration));
91      }
92  
93      /** Build a new instance.
94       * @param prefix prefix to use for parameter drivers names
95       * @param refStart reference interval start date
96       * @param refStop reference interval stop date
97       */
98      public ParameterDrivenDateIntervalDetector(final String prefix,
99                                                 final AbsoluteDate refStart, final AbsoluteDate refStop) {
100         this(s -> FastMath.max(0.5 * refStop.durationFrom(refStart), THRESHOLD),
101              THRESHOLD, DEFAULT_MAX_ITER,
102              new StopOnDecreasing(),
103              new DateDriver(refStart, prefix + START_SUFFIX, true),
104              new DateDriver(refStop, prefix + STOP_SUFFIX, false),
105              new DateDriver(refStart.shiftedBy(0.5 * refStop.durationFrom(refStart)), prefix + MEDIAN_SUFFIX, true),
106              new ParameterDriver(prefix + DURATION_SUFFIX, refStop.durationFrom(refStart), 1.0, 0.0, Double.POSITIVE_INFINITY));
107     }
108 
109     /** Protected constructor with full parameters.
110      * <p>
111      * This constructor is not public as users are expected to use the builder
112      * API with the various {@code withXxx()} methods to set up the instance
113      * in a readable manner without using a huge amount of parameters.
114      * </p>
115      * @param maxCheck maximum checking interval
116      * @param threshold convergence threshold (s)
117      * @param maxIter maximum number of iterations in the event time search
118      * @param handler event handler to call at event occurrences
119      * @param start reference interval start driver
120      * @param stop reference interval stop driver
121      * @param median median date driver
122      * @param duration duration driver
123      */
124     protected ParameterDrivenDateIntervalDetector(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
125                                                   final EventHandler handler,
126                                                   final DateDriver start, final DateDriver stop,
127                                                   final DateDriver median, final ParameterDriver duration) {
128         super(maxCheck, threshold, maxIter, handler);
129         this.start    = start;
130         this.stop     = stop;
131         this.median   = median;
132         this.duration = duration;
133 
134         // set up delegation between drivers
135         replaceBindingObserver(start,    new StartObserver());
136         replaceBindingObserver(stop,     new StopObserver());
137         replaceBindingObserver(median,   new MedianObserver());
138         replaceBindingObserver(duration, new DurationObserver());
139 
140     }
141 
142     /** Replace binding observers.
143      * @param driver driver for whose binding observers should be replaced
144      * @param bindingObserver new binding observer
145      */
146     private void replaceBindingObserver(final ParameterDriver driver, final BindingObserver bindingObserver) {
147 
148         // remove the previous binding observers
149         final List<ParameterObserver> original = driver.
150                                                  getObservers().
151                                                  stream().
152                                                  filter(observer -> observer instanceof ParameterDrivenDateIntervalDetector.BindingObserver).
153                                                  collect(Collectors.toList());
154         original.forEach(observer -> driver.removeObserver(observer));
155 
156         driver.addObserver(bindingObserver);
157 
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     protected ParameterDrivenDateIntervalDetector create(final AdaptableInterval newMaxCheck, final double newThreshold, final int newMaxIter,
163                                                          final EventHandler newHandler) {
164         return new ParameterDrivenDateIntervalDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
165                                                        start, stop, median, duration);
166     }
167 
168     /** Get the driver for start date.
169      * <p>
170      * Note that the start date is automatically adjusted if either
171      * {@link #getMedianDriver() median date} or {@link #getDurationDriver() duration}
172      * are {@link ParameterDriver#isSelected() selected} and changed.
173      * </p>
174      * @return driver for start date
175      */
176     public DateDriver getStartDriver() {
177         return start;
178     }
179 
180     /** Get the driver for stop date.
181      * <p>
182      * Note that the stop date is automatically adjusted if either
183      * {@link #getMedianDriver() median date} or {@link #getDurationDriver() duration}
184      * are {@link ParameterDriver#isSelected() selected} changed.
185      * </p>
186      * @return driver for stop date
187      */
188     public DateDriver getStopDriver() {
189         return stop;
190     }
191 
192     /** Get the driver for median date.
193      * <p>
194      * Note that the median date is automatically adjusted if either
195      * {@link #getStartDriver()} start date or {@link #getStopDriver() stop date}
196      * are {@link ParameterDriver#isSelected() selected} changed.
197      * </p>
198      * @return driver for median date
199      */
200     public DateDriver getMedianDriver() {
201         return median;
202     }
203 
204     /** Get the driver for duration.
205      * <p>
206      * Note that the duration is automatically adjusted if either
207      * {@link #getStartDriver()} start date or {@link #getStopDriver() stop date}
208      * are {@link ParameterDriver#isSelected() selected} changed.
209      * </p>
210      * @return driver for duration
211      */
212     public ParameterDriver getDurationDriver() {
213         return duration;
214     }
215 
216     /** Compute the value of the switching function.
217      * <p>
218      * The function is positive for dates within the interval defined
219      * by applying the parameter drivers shifts to reference dates,
220      * and negative for dates outside of this interval. Note that
221      * if Δt_start - Δt_stop is less than ref_stop.durationFrom(ref_start),
222      * then the interval degenerates to empty and the function never
223      * reaches positive values.
224      * </p>
225      * @param s the current state information: date, kinematics, attitude
226      * @return value of the switching function
227      */
228     public double g(final SpacecraftState s) {
229         return FastMath.min(s.getDate().durationFrom(start.getDate()),
230                             stop.getDate().durationFrom(s.getDate()));
231     }
232 
233     /** Base observer. */
234     private abstract class BindingObserver implements ParameterObserver {
235 
236         /** {@inheritDoc} */
237         @Override
238         public void valueChanged(final double previousValue, final ParameterDriver driver, final AbsoluteDate date) {
239             if (driver.isSelected()) {
240                 setDelta(driver.getValue(date) - previousValue, date);
241             }
242         }
243         /** {@inheritDoc} */
244         @Override
245         public void valueSpanMapChanged(final TimeSpanMap<Double> previousValue, final ParameterDriver driver) {
246             if (driver.isSelected()) {
247                 for (Span<Double> span = driver.getValueSpanMap().getFirstSpan(); span != null; span = span.next()) {
248                     setDelta(span.getData() - previousValue.get(span.getStart()), span.getStart());
249                 }
250             }
251         }
252         /** {@inheritDoc} */
253         @Override
254         public void selectionChanged(final boolean previousSelection, final ParameterDriver driver) {
255             if ((start.isSelected()  || stop.isSelected()) &&
256                 (median.isSelected() || duration.isSelected())) {
257                 throw new OrekitException(OrekitMessages.INCONSISTENT_SELECTION,
258                                           start.getName(), stop.getName(),
259                                           median.getName(), duration.getName());
260             }
261         }
262 
263         /** Change a value.
264          * @param delta change of value
265          * @param date date at which the delta wants to be set
266          */
267         protected abstract void setDelta(double delta, AbsoluteDate date);
268 
269     }
270 
271     /** Observer for start date. */
272     private class StartObserver extends BindingObserver {
273         /** {@inheritDoc} */
274         @Override
275         protected void setDelta(final double delta, final AbsoluteDate date) {
276             // date driver has no validity period, only 1 value is estimated
277             // over the all interval so there is no problem for calling getValue with null argument
278             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
279             // of the driver
280             median.setValue(median.getValue(date) + 0.5 * delta, date);
281             duration.setValue(duration.getValue(date) - delta, date);
282         }
283     }
284 
285     /** Observer for stop date. */
286     private class StopObserver extends BindingObserver {
287         /** {@inheritDoc} */
288         @Override
289         protected void setDelta(final double delta, final AbsoluteDate date) {
290             // date driver has no validity period, only 1 value is estimated
291             // over the all interval so there is no problem for calling getValue with null argument
292             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
293             // of the driver
294             median.setValue(median.getValue(date) + 0.5 * delta, date);
295             duration.setValue(duration.getValue(date) + delta, date);
296         }
297     }
298 
299     /** Observer for median date. */
300     private class MedianObserver extends BindingObserver {
301         /** {@inheritDoc} */
302         @Override
303         protected void setDelta(final double delta, final AbsoluteDate date) {
304             // date driver has no validity period, only 1 value is estimated
305             // over the all interval so there is no problem for calling getValue with null argument
306             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
307             // of the driver
308             start.setValue(start.getValue(date) + delta, date);
309             stop.setValue(stop.getValue(date) + delta, date);
310         }
311     }
312 
313     /** Observer for duration. */
314     private class DurationObserver extends BindingObserver {
315         /** {@inheritDoc} */
316         @Override
317         protected void setDelta(final double delta, final AbsoluteDate date) {
318             // date driver has no validity period, only 1 value is estimated
319             // over the all interval so there is no problem for calling getValue with null argument
320             // or any date, it would give the same result as there is only 1 span on the valueSpanMap
321             // of the driver
322             start.setValue(start.getValue(date) - 0.5 * delta, date);
323             stop.setValue(stop.getValue(date) + 0.5 * delta, date);
324         }
325     }
326 
327 }