1   package org.orekit.propagation.events;
2   
3   import org.hipparchus.complex.Complex;
4   import org.hipparchus.complex.ComplexField;
5   import org.hipparchus.util.Binary64;
6   import org.hipparchus.util.Binary64Field;
7   import org.junit.jupiter.api.Assertions;
8   import org.junit.jupiter.api.Test;
9   import org.mockito.Mockito;
10  import org.orekit.frames.FramesFactory;
11  import org.orekit.propagation.FieldSpacecraftState;
12  import org.orekit.propagation.SpacecraftState;
13  import org.orekit.propagation.events.intervals.AdaptableInterval;
14  import org.orekit.propagation.events.intervals.FieldAdaptableInterval;
15  import org.orekit.time.AbsoluteDate;
16  import org.orekit.utils.AbsolutePVCoordinates;
17  import org.orekit.utils.PVCoordinates;
18  
19  class FieldEventDetectionSettingsTest {
20  
21      @Test
22      void testConstructor() {
23          // GIVEN
24          final double maxCheck = 100.;
25          // WHEN
26          final FieldEventDetectionSettings<Complex> fieldEventDetectionSettings = new FieldEventDetectionSettings<>(maxCheck,
27                  new Complex(20.), 10);
28          // THEN
29          Assertions.assertEquals(maxCheck, fieldEventDetectionSettings.getMaxCheckInterval().currentInterval(null, true));
30      }
31  
32      @Test
33      void testConstructorFromNonField() {
34          // GIVEN
35          final EventDetectionSettings settings = new EventDetectionSettings(AdaptableInterval.of(10), 100., 1000);
36          // WHEN
37          final FieldEventDetectionSettings<Complex> fieldEventDetectionSettings = new FieldEventDetectionSettings<>(ComplexField.getInstance(),
38                  settings);
39          // THEN
40          Assertions.assertEquals(settings.getMaxIterationCount(), fieldEventDetectionSettings.getMaxIterationCount());
41          Assertions.assertEquals(settings.getThreshold(), fieldEventDetectionSettings.getThreshold().getReal());
42          final SpacecraftState state = new SpacecraftState(new AbsolutePVCoordinates(FramesFactory.getGCRF(),
43                  AbsoluteDate.ARBITRARY_EPOCH, new PVCoordinates()));
44          Assertions.assertEquals(fieldEventDetectionSettings.getMaxCheckInterval().currentInterval(new FieldSpacecraftState<>(ComplexField.getInstance(), state), true),
45                  settings.getMaxCheckInterval().currentInterval(state, true));
46      }
47  
48      @Test
49      void testToEventDetectionSettings() {
50          // GIVEN
51          final FieldAdaptableInterval<Complex> interval = FieldAdaptableInterval.of(1.);
52          final FieldEventDetectionSettings<Complex> fieldEventDetectionSettings = new FieldEventDetectionSettings<>(interval,
53                  new Complex(20.), 10);
54          // WHEN
55          final EventDetectionSettings eventDetectionSettings = fieldEventDetectionSettings.toEventDetectionSettings();
56          // THEN
57          Assertions.assertEquals(fieldEventDetectionSettings.getMaxIterationCount(), eventDetectionSettings.getMaxIterationCount());
58          Assertions.assertEquals(fieldEventDetectionSettings.getThreshold().getReal(), eventDetectionSettings.getThreshold());
59          final SpacecraftState state = new SpacecraftState(new AbsolutePVCoordinates(FramesFactory.getGCRF(),
60                  AbsoluteDate.ARBITRARY_EPOCH, new PVCoordinates()));
61          Assertions.assertEquals(fieldEventDetectionSettings.getMaxCheckInterval().currentInterval(new FieldSpacecraftState<>(ComplexField.getInstance(), state), true),
62                  eventDetectionSettings.getMaxCheckInterval().currentInterval(state, true));
63      }
64  
65      @Test
66      void testGetDefaultEventDetectionSettings() {
67          // GIVEN
68          final Binary64Field field = Binary64Field.getInstance();
69          // WHEN
70          final FieldEventDetectionSettings<Binary64> fieldEventDetectionSettings = FieldEventDetectionSettings
71                  .getDefaultEventDetectionSettings(field);
72          // THEN
73          final FieldEventDetectionSettings<Binary64> expectedDetectionSettings = new FieldEventDetectionSettings<>(field,
74                  EventDetectionSettings.getDefaultEventDetectionSettings());
75          Assertions.assertEquals(fieldEventDetectionSettings.getMaxIterationCount(), expectedDetectionSettings.getMaxIterationCount());
76          Assertions.assertEquals(fieldEventDetectionSettings.getThreshold(), expectedDetectionSettings.getThreshold());
77          final SpacecraftState state = new SpacecraftState(new AbsolutePVCoordinates(FramesFactory.getGCRF(),
78                  AbsoluteDate.ARBITRARY_EPOCH, new PVCoordinates()));
79          final FieldSpacecraftState<Binary64> fieldState = new FieldSpacecraftState<>(field, state);
80          Assertions.assertEquals(fieldEventDetectionSettings.getMaxCheckInterval().currentInterval(fieldState, true),
81                  expectedDetectionSettings.getMaxCheckInterval().currentInterval(fieldState, true));
82      }
83  
84      @Test
85      void testWithThreshold() {
86          // GIVEN
87          final FieldEventDetectionSettings<Binary64> defaultSettings = FieldEventDetectionSettings
88                  .getDefaultEventDetectionSettings(Binary64Field.getInstance());
89          final Binary64 expectedThreshold = new Binary64(123);
90          // WHEN
91          final FieldEventDetectionSettings<Binary64> detectionSettings = defaultSettings.withThreshold(expectedThreshold);
92          // THEN
93          Assertions.assertEquals(expectedThreshold, detectionSettings.getThreshold());
94      }
95  
96      @Test
97      void testWithMaxIter() {
98          // GIVEN
99          final FieldEventDetectionSettings<Binary64> defaultSettings = FieldEventDetectionSettings
100                 .getDefaultEventDetectionSettings(Binary64Field.getInstance());
101         final int expectedCount = 123;
102         // WHEN
103         final FieldEventDetectionSettings<Binary64> detectionSettings = defaultSettings.withMaxIter(expectedCount);
104         // THEN
105         Assertions.assertEquals(expectedCount, detectionSettings.getMaxIterationCount());
106     }
107 
108     @Test
109     @SuppressWarnings("unchecked")
110     void testWithMaxCheckInterval() {
111         // GIVEN
112         final FieldEventDetectionSettings<Binary64> defaultSettings = FieldEventDetectionSettings
113                 .getDefaultEventDetectionSettings(Binary64Field.getInstance());
114         final FieldAdaptableInterval<Binary64> expectedInterval = Mockito.mock();
115         // WHEN
116         final FieldEventDetectionSettings<Binary64> detectionSettings = defaultSettings.withMaxCheckInterval(expectedInterval);
117         // THEN
118         Assertions.assertEquals(expectedInterval, detectionSettings.getMaxCheckInterval());
119     }
120 }