1   /* Copyright 2002-2025 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.forces.maneuvers.trigger;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  import org.hipparchus.util.Binary64Field;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  import org.orekit.propagation.events.DateDetector;
28  import org.orekit.propagation.events.FieldEventDetector;
29  import org.orekit.propagation.events.intervals.FieldAdaptableInterval;
30  import org.orekit.propagation.events.FieldDateDetector;
31  import org.orekit.propagation.events.handlers.StopOnEvent;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.time.FieldAbsoluteDate;
34  import org.orekit.time.TimeStamped;
35  import org.orekit.utils.ParameterDriver;
36  
37  class IntervalEventTriggerTest extends AbstractManeuverTriggersTest<IntervalEventTrigger<DateDetector>> {
38  
39      public static class IntervalDates extends IntervalEventTrigger<DateDetector> {
40  
41          public IntervalDates(final AbsoluteDate start, final AbsoluteDate stop) {
42              super(new DateDetector(start, stop).
43                    withMaxCheck(0.5 * stop.durationFrom(start)).
44                    withThreshold(1.0e-10).
45                    withHandler(new StopOnEvent()));
46          }
47  
48          @Override
49          protected <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
50              D convertIntervalDetector(Field<S> field, DateDetector detector) {
51              final FieldAdaptableInterval<S> maxCheck  = (s, isForward) -> detector.getMaxCheckInterval().currentInterval(s.toSpacecraftState(), isForward);
52              final S                    threshold = field.getZero().newInstance(detector.getThreshold());
53              final FieldAbsoluteDate<S> d0 = new FieldAbsoluteDate<>(field, detector.getDates().get(0).getDate());
54              final FieldAbsoluteDate<S> d1 = new FieldAbsoluteDate<>(field, detector.getDates().get(1).getDate());
55              @SuppressWarnings("unchecked")
56              final D converted =
57                  (D) new FieldDateDetector<>(field, d0, d1).
58                                                withMaxCheck(maxCheck).
59                                                withThreshold(threshold);
60              return converted;
61          }
62  
63          @Override
64          public List<ParameterDriver> getParametersDrivers() {
65              return Collections.emptyList();
66          }
67  
68      }
69  
70      protected IntervalDates createTrigger(final AbsoluteDate start, final AbsoluteDate stop) {
71          return new IntervalDates(start, stop);
72      }
73  
74      @Test
75      void testComponents() {
76          IntervalDates trigger = createTrigger(AbsoluteDate.J2000_EPOCH,
77                                                AbsoluteDate.J2000_EPOCH.shiftedBy(100.0));
78          final List<TimeStamped>    dates = trigger.getFiringIntervalDetector().getDates();
79          Assertions.assertEquals(1,     trigger.getEventDetectors().count());
80          Assertions.assertEquals(1,     trigger.getFieldEventDetectors(Binary64Field.getInstance()).count());
81          Assertions.assertEquals(2,     dates.size());
82          Assertions.assertEquals(  0.0, dates.get(0).getDate().durationFrom(AbsoluteDate.J2000_EPOCH), 1.0e-10);
83          Assertions.assertEquals(100.0, dates.get(1).getDate().durationFrom(AbsoluteDate.J2000_EPOCH), 1.0e-10);
84      }
85  
86  }