1   /* Copyright 2002-2022 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.geometry.euclidean.threed.Vector3D;
20  import org.junit.Assert;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.orekit.Utils;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  import org.orekit.frames.FramesFactory;
27  import org.orekit.orbits.CartesianOrbit;
28  import org.orekit.orbits.Orbit;
29  import org.orekit.propagation.Propagator;
30  import org.orekit.propagation.analytical.EcksteinHechlerPropagator;
31  import org.orekit.propagation.events.handlers.ContinueOnEvent;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.time.TimeScale;
34  import org.orekit.time.TimeScalesFactory;
35  import org.orekit.utils.Constants;
36  import org.orekit.utils.PVCoordinates;
37  import org.orekit.utils.ParameterDriver;
38  
39  public class ParameterDrivenDateIntervalDetectorTest {
40  
41      private Propagator propagator;
42  
43      @Test
44      public void testNoShift() {
45          final AbsoluteDate t0    = propagator.getInitialState().getDate();
46          final AbsoluteDate start = t0.shiftedBy( 120);
47          final AbsoluteDate stop  = t0.shiftedBy(1120);
48          ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("no-shift", start, stop).
49                                                         withMaxCheck(10.0).
50                                                         withThreshold(1.0e-12).
51                                                         withHandler(new ContinueOnEvent<>());
52  
53          Assert.assertEquals(10.0, detector.getMaxCheckInterval(), 1.0e-15);
54          Assert.assertEquals(1.0e-12, detector.getThreshold(), 1.0e-15);
55          Assert.assertEquals(AbstractDetector.DEFAULT_MAX_ITER, detector.getMaxIterationCount());
56          Assert.assertEquals("no-shift_START", detector.getStartDriver().getName());
57          Assert.assertEquals("no-shift_STOP", detector.getStopDriver().getName());
58  
59  
60          EventsLogger logger = new EventsLogger();
61          propagator.addEventDetector(logger.monitorDetector(detector));
62  
63          propagator.propagate(propagator.getInitialState().getOrbit().getDate().shiftedBy(3600.0));
64  
65          Assert.assertEquals(2, logger.getLoggedEvents().size());
66          Assert.assertEquals(0.0, logger.getLoggedEvents().get(0).getDate().durationFrom(start), 1.0e-10);
67          Assert.assertEquals(0.0, logger.getLoggedEvents().get(1).getDate().durationFrom(stop),  1.0e-10);
68      }
69  
70      @Test
71      public void testSmallShift() {
72          final AbsoluteDate t0    = propagator.getInitialState().getDate();
73          final AbsoluteDate start = t0.shiftedBy( 120);
74          final AbsoluteDate stop  = t0.shiftedBy(1120);
75          ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("small-shift", start, stop).
76                                                         withMaxCheck(10.0).
77                                                         withThreshold(1.0e-12).
78                                                         withHandler(new ContinueOnEvent<>());
79  
80          Assert.assertEquals(10.0, detector.getMaxCheckInterval(), 1.0e-15);
81          Assert.assertEquals(1.0e-12, detector.getThreshold(), 1.0e-15);
82          Assert.assertEquals(AbstractDetector.DEFAULT_MAX_ITER, detector.getMaxIterationCount());
83          Assert.assertEquals("small-shift_START", detector.getStartDriver().getName());
84          Assert.assertEquals("small-shift_STOP", detector.getStopDriver().getName());
85  
86          EventsLogger logger = new EventsLogger();
87          propagator.addEventDetector(logger.monitorDetector(detector));
88  
89          final double startShift = 5.5;
90          detector.getStartDriver().setValue(startShift);
91          final double stopShift  = -0.5;
92          detector.getStopDriver().setValue(stopShift);
93          propagator.propagate(propagator.getInitialState().getOrbit().getDate().shiftedBy(3600.0));
94  
95          Assert.assertEquals(2, logger.getLoggedEvents().size());
96          Assert.assertEquals(startShift, logger.getLoggedEvents().get(0).getDate().durationFrom(start), 1.0e-10);
97          Assert.assertEquals(stopShift,  logger.getLoggedEvents().get(1).getDate().durationFrom(stop),  1.0e-10);
98      }
99  
100     @Test
101     public void testLargeShift() {
102         final AbsoluteDate t0       = propagator.getInitialState().getDate();
103         final AbsoluteDate start    = t0.shiftedBy( 120);
104         final AbsoluteDate stop     = t0.shiftedBy(1120);
105         final double       duration = stop.durationFrom(start);
106         final AbsoluteDate median   = start.shiftedBy(0.5 * duration);
107         ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("large-shift", median, duration).
108                                                        withMaxCheck(10.0).
109                                                        withThreshold(1.0e-12).
110                                                        withHandler(new ContinueOnEvent<>());
111 
112         Assert.assertEquals(10.0, detector.getMaxCheckInterval(), 1.0e-15);
113         Assert.assertEquals(1.0e-12, detector.getThreshold(), 1.0e-15);
114         Assert.assertEquals(AbstractDetector.DEFAULT_MAX_ITER, detector.getMaxIterationCount());
115         Assert.assertEquals("large-shift_START", detector.getStartDriver().getName());
116         Assert.assertEquals("large-shift_STOP", detector.getStopDriver().getName());
117 
118         EventsLogger logger = new EventsLogger();
119         propagator.addEventDetector(logger.monitorDetector(detector));
120 
121         final double startShift =  500.5;
122         detector.getStartDriver().setValue(startShift);
123         final double stopShift  = -500.5;
124         detector.getStopDriver().setValue(stopShift);
125         propagator.propagate(propagator.getInitialState().getOrbit().getDate().shiftedBy(3600.0));
126 
127         Assert.assertEquals(0, logger.getLoggedEvents().size());
128 
129     }
130 
131     @Test
132     public void testSelection() {
133         final AbsoluteDate t0       = propagator.getInitialState().getDate();
134         final AbsoluteDate start    = t0.shiftedBy( 120);
135         final AbsoluteDate stop     = t0.shiftedBy(1120);
136         ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("large-shift", start, stop).
137                                                        withMaxCheck(10.0).
138                                                        withThreshold(1.0e-12).
139                                                        withHandler(new ContinueOnEvent<>());
140 
141         Assert.assertFalse(detector.getStartDriver().isSelected());
142         Assert.assertFalse(detector.getStopDriver().isSelected());
143         Assert.assertFalse(detector.getMedianDriver().isSelected());
144         Assert.assertFalse(detector.getDurationDriver().isSelected());
145 
146         // check all 16 possible configurations, changing one selection at a time
147         checkSelection(detector, detector.getDurationDriver(), true,  false, false, false, true,  false);
148         checkSelection(detector, detector.getMedianDriver(),   true,  false, false, true,  true,  false);
149         checkSelection(detector, detector.getDurationDriver(), false, false, false, true,  false, false);
150         checkSelection(detector, detector.getStopDriver(),     true,  false, true,  true,  false, true);
151         checkSelection(detector, detector.getDurationDriver(), true,  false, true,  true,  true,  true);
152         checkSelection(detector, detector.getMedianDriver(),   false, false, true,  false, true,  true);
153         checkSelection(detector, detector.getDurationDriver(), false, false, true,  false, false, false);
154         checkSelection(detector, detector.getStartDriver(),    true,  true,  true,  false, false, false);
155         checkSelection(detector, detector.getDurationDriver(), true,  true,  true,  false, true,  true);
156         checkSelection(detector, detector.getMedianDriver(),   true,  true,  true,  true,  true,  true);
157         checkSelection(detector, detector.getDurationDriver(), false, true,  true,  true,  false, true);
158         checkSelection(detector, detector.getStopDriver(),     false, true,  false, true,  false, true);
159         checkSelection(detector, detector.getDurationDriver(), true,  true,  false, true,  true,  true);
160         checkSelection(detector, detector.getMedianDriver(),   false, true,  false, false, true,  true);
161         checkSelection(detector, detector.getDurationDriver(), false, true,  false, false, false, false);
162         checkSelection(detector, detector.getStartDriver(),    false, false, false, false, false, false);
163         
164     }
165 
166     @Test
167     public void testStartStopToMedianDuration() {
168         final AbsoluteDate t0       = propagator.getInitialState().getDate();
169         final AbsoluteDate start    = t0.shiftedBy( 120);
170         final AbsoluteDate stop     = t0.shiftedBy(1120);
171         ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("large-shift", start, stop).
172                                                        withMaxCheck(10.0).
173                                                        withThreshold(1.0e-12).
174                                                        withHandler(new ContinueOnEvent<>());
175         Assert.assertEquals(   0.0, detector.getStartDriver().getValue(),    1.0e-15);
176         Assert.assertEquals(   0.0, detector.getStopDriver().getValue(),     1.0e-15);
177         Assert.assertEquals(   0.0, detector.getMedianDriver().getValue(),   1.0e-15);
178         Assert.assertEquals(1000.0, detector.getDurationDriver().getValue(), 1.0e-15);
179         detector.getStartDriver().setSelected(true);
180         detector.getStartDriver().setValue(1.0);
181         Assert.assertEquals(   1.0, detector.getStartDriver().getValue(),    1.0e-15);
182         Assert.assertEquals(   0.0, detector.getStopDriver().getValue(),     1.0e-15);
183         Assert.assertEquals(   0.5, detector.getMedianDriver().getValue(),   1.0e-15);
184         Assert.assertEquals( 999.0, detector.getDurationDriver().getValue(), 1.0e-15);
185         detector.getStopDriver().setSelected(true);
186         detector.getStopDriver().setValue(10.0);
187         Assert.assertEquals(   1.0, detector.getStartDriver().getValue(),    1.0e-15);
188         Assert.assertEquals(  10.0, detector.getStopDriver().getValue(),     1.0e-15);
189         Assert.assertEquals(   5.5, detector.getMedianDriver().getValue(),   1.0e-15);
190         Assert.assertEquals(1009.0, detector.getDurationDriver().getValue(), 1.0e-15);
191     }
192 
193     @Test
194     public void testMedianDurationToStartStop() {
195         final AbsoluteDate t0       = propagator.getInitialState().getDate();
196         final AbsoluteDate start    = t0.shiftedBy( 120);
197         final AbsoluteDate stop     = t0.shiftedBy(1120);
198         ParameterDrivenDateIntervalDetector detector = new ParameterDrivenDateIntervalDetector("large-shift", start, stop).
199                                                        withMaxCheck(10.0).
200                                                        withThreshold(1.0e-12).
201                                                        withHandler(new ContinueOnEvent<>());
202         Assert.assertEquals(   0.0, detector.getStartDriver().getValue(),    1.0e-15);
203         Assert.assertEquals(   0.0, detector.getStopDriver().getValue(),     1.0e-15);
204         Assert.assertEquals(   0.0, detector.getMedianDriver().getValue(),   1.0e-15);
205         Assert.assertEquals(1000.0, detector.getDurationDriver().getValue(), 1.0e-15);
206         detector.getMedianDriver().setSelected(true);
207         detector.getMedianDriver().setValue(1.0);
208         Assert.assertEquals(   1.0, detector.getStartDriver().getValue(),    1.0e-15);
209         Assert.assertEquals(   1.0, detector.getStopDriver().getValue(),     1.0e-15);
210         Assert.assertEquals(   1.0, detector.getMedianDriver().getValue(),   1.0e-15);
211         Assert.assertEquals(1000.0, detector.getDurationDriver().getValue(), 1.0e-15);
212         detector.getDurationDriver().setSelected(true);
213         detector.getDurationDriver().setValue(900.0);
214         Assert.assertEquals(  51.0, detector.getStartDriver().getValue(),    1.0e-15);
215         Assert.assertEquals( -49.0, detector.getStopDriver().getValue(),     1.0e-15);
216         Assert.assertEquals(   1.0, detector.getMedianDriver().getValue(),   1.0e-15);
217         Assert.assertEquals( 900.0, detector.getDurationDriver().getValue(), 1.0e-15);
218     }
219 
220     private void checkSelection(final ParameterDrivenDateIntervalDetector detector,
221                                 final ParameterDriver driver, final boolean selection, final boolean expectedStart,
222                                 final boolean expectedStop, final boolean expectedMedian,
223                                 final boolean expectedDuration, final boolean shouldFail) {
224         try {
225             driver.setSelected(selection);
226             if (shouldFail) {
227                 Assert.fail("an exception should have been thrown");
228             }
229         } catch (OrekitException oe) {
230             Assert.assertEquals(OrekitMessages.INCONSISTENT_SELECTION, oe.getSpecifier());
231         }
232         Assert.assertEquals(selection,        driver.isSelected());
233         Assert.assertEquals(expectedStart,    detector.getStartDriver().isSelected());
234         Assert.assertEquals(expectedStop,     detector.getStopDriver().isSelected());
235         Assert.assertEquals(expectedMedian,   detector.getMedianDriver().isSelected());
236         Assert.assertEquals(expectedDuration, detector.getDurationDriver().isSelected());
237     }
238 
239     @Before
240     public void setUp() {
241         Utils.setDataRoot("regular-data");
242         final TimeScale utc = TimeScalesFactory.getUTC();
243         final Vector3D position = new Vector3D(-6142438.668, 3492467.56, -25767.257);
244         final Vector3D velocity = new Vector3D(506.0, 943.0, 7450);
245         final AbsoluteDate date = new AbsoluteDate(2003, 9, 16, utc);
246         final Orbit orbit = new CartesianOrbit(new PVCoordinates(position,  velocity),
247                                                FramesFactory.getEME2000(), date,
248                                                Constants.EIGEN5C_EARTH_MU);
249 
250         propagator =
251             new EcksteinHechlerPropagator(orbit,
252                                           Constants.EIGEN5C_EARTH_EQUATORIAL_RADIUS,
253                                           Constants.EIGEN5C_EARTH_MU,
254                                           Constants.EIGEN5C_EARTH_C20,
255                                           Constants.EIGEN5C_EARTH_C30,
256                                           Constants.EIGEN5C_EARTH_C40,
257                                           Constants.EIGEN5C_EARTH_C50,
258                                           Constants.EIGEN5C_EARTH_C60);
259     }
260 
261 }
262