1   /* Contributed in the public domain.
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.hamcrest.CoreMatchers;
20  import org.hamcrest.MatcherAssert;
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.Field;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.hipparchus.ode.events.Action;
26  import org.hipparchus.util.Binary64Field;
27  import org.junit.jupiter.api.Test;
28  import org.orekit.frames.FramesFactory;
29  import org.orekit.orbits.FieldCartesianOrbit;
30  import org.orekit.propagation.FieldSpacecraftState;
31  import org.orekit.propagation.events.handlers.FieldEventHandler;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.time.FieldAbsoluteDate;
34  import org.orekit.utils.FieldPVCoordinates;
35  
36  import java.util.function.Function;
37  
38  /**
39   * Unit tests for {@link FieldFunctionalDetector}
40   *
41   * @author Evan Ward
42   */
43  public class FieldFunctionalDetectorTest {
44  
45      /**
46       * Check {@link FieldFunctionalDetector}.
47       */
48      @Test
49      public void testFunctionalDetector() {
50          doTestFunctionalDetector(Binary64Field.getInstance());
51      }
52  
53      public <T extends CalculusFieldElement<T>> void doTestFunctionalDetector(Field<T> field) {
54          // setup
55          T zero = field.getZero();
56          T one = field.getOne();
57          Function<FieldSpacecraftState<T>, T> g = FieldSpacecraftState::getMass;
58          FieldEventHandler<T> handler = (s, detector, increasing) -> Action.STOP;
59  
60          // action
61          FieldFunctionalDetector<T> detector = new FieldFunctionalDetector<>(field)
62                  .withMaxIter(1)
63                  .withThreshold(zero.add(2))
64                  .withMaxCheck(3)
65                  .withHandler(handler)
66                  .withFunction(g);
67  
68          // verify
69          MatcherAssert.assertThat(detector.getMaxIterationCount(), CoreMatchers.is(1));
70          MatcherAssert.assertThat(detector.getThreshold().getReal(), CoreMatchers.is(2.0));
71          MatcherAssert.assertThat(detector.getMaxCheckInterval().currentInterval(null, true), CoreMatchers.is(3.0));
72          MatcherAssert.assertThat(detector.getHandler(), CoreMatchers.is(handler));
73          FieldSpacecraftState<T> state = new FieldSpacecraftState<>(
74                  new FieldCartesianOrbit<>(
75                          new FieldPVCoordinates<>(
76                                  new FieldVector3D<>(one, new Vector3D(1, 2, 3)),
77                                  new FieldVector3D<>(one, new Vector3D(4, 5, 6))),
78                          FramesFactory.getGCRF(),
79                          new FieldAbsoluteDate<>(field, AbsoluteDate.CCSDS_EPOCH),
80                          zero.add(4)),
81                  zero.add(5));
82          MatcherAssert.assertThat(detector.g(state).getReal(), CoreMatchers.is(5.0));
83          MatcherAssert.assertThat(detector.getHandler().eventOccurred(null, detector, false),
84                  CoreMatchers.is(Action.STOP));
85          MatcherAssert.assertThat(detector.getFunction(), CoreMatchers.is(g));
86      }
87  
88  }