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 java.util.function.Function;
20  
21  import org.hamcrest.CoreMatchers;
22  import org.hamcrest.MatcherAssert;
23  import org.hipparchus.Field;
24  import org.hipparchus.CalculusFieldElement;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.hipparchus.ode.events.Action;
28  import org.hipparchus.util.Decimal64Field;
29  import org.junit.Test;
30  import org.orekit.frames.FramesFactory;
31  import org.orekit.orbits.FieldCartesianOrbit;
32  import org.orekit.propagation.FieldSpacecraftState;
33  import org.orekit.propagation.events.handlers.FieldEventHandler;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.utils.FieldPVCoordinates;
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(Decimal64Field.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<FieldEventDetector<T>, T> handler =
59                  (s, detector, increasing) -> Action.STOP;
60  
61          // action
62          FieldFunctionalDetector<T> detector = new FieldFunctionalDetector<>(field)
63                  .withMaxIter(1)
64                  .withThreshold(zero.add(2))
65                  .withMaxCheck(zero.add(3))
66                  .withHandler(handler)
67                  .withFunction(g);
68  
69          // verify
70          MatcherAssert.assertThat(detector.getMaxIterationCount(), CoreMatchers.is(1));
71          MatcherAssert.assertThat(detector.getThreshold().getReal(), CoreMatchers.is(2.0));
72          MatcherAssert.assertThat(detector.getMaxCheckInterval().getReal(), CoreMatchers.is(3.0));
73          MatcherAssert.assertThat(detector.getHandler(), CoreMatchers.is(handler));
74          FieldSpacecraftState<T> state = new FieldSpacecraftState<>(
75                  new FieldCartesianOrbit<>(
76                          new FieldPVCoordinates<>(
77                                  new FieldVector3D<>(one, new Vector3D(1, 2, 3)),
78                                  new FieldVector3D<>(one, new Vector3D(4, 5, 6))),
79                          FramesFactory.getGCRF(),
80                          new FieldAbsoluteDate<>(field, AbsoluteDate.CCSDS_EPOCH),
81                          zero.add(4)),
82                  zero.add(5));
83          MatcherAssert.assertThat(detector.g(state).getReal(), CoreMatchers.is(5.0));
84          MatcherAssert.assertThat(detector.eventOccurred(null, false),
85                  CoreMatchers.is(Action.STOP));
86          MatcherAssert.assertThat(detector.getFunction(), CoreMatchers.is(g));
87      }
88  
89  }