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.handlers;
18  
19  import java.util.List;
20  
21  import org.hipparchus.ode.events.Action;
22  import org.hipparchus.util.Decimal64;
23  import org.hipparchus.util.Decimal64Field;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.FramesFactory;
28  import org.orekit.orbits.FieldKeplerianOrbit;
29  import org.orekit.orbits.FieldOrbit;
30  import org.orekit.orbits.PositionAngle;
31  import org.orekit.propagation.FieldSpacecraftState;
32  import org.orekit.propagation.events.FieldDateDetector;
33  import org.orekit.propagation.events.handlers.FieldRecordAndContinue.Event;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.utils.Constants;
37  
38  /**
39   * Unit tests for {@link FieldRecordAndContinue}.
40   *
41   * @author Evan Ward
42   */
43  public class FieldRecordAndContinueTest {
44  
45      /** Field. */
46      private static final Decimal64Field field = Decimal64Field.getInstance();
47  
48      /** check add and clear behavior. */
49      @Test
50      public void testGetEvents() {
51          // setup
52          FieldRecordAndContinue<FieldDateDetector<Decimal64>, Decimal64> handler =
53                  new FieldRecordAndContinue<>();
54          FieldAbsoluteDate<Decimal64> date =
55                  new FieldAbsoluteDate<>(field, AbsoluteDate.J2000_EPOCH);
56          Decimal64 zero = date.getField().getZero();
57          FieldDateDetector<Decimal64> detector = new FieldDateDetector<>(date);
58          Frame eci = FramesFactory.getGCRF();
59          FieldOrbit<Decimal64> orbit = new FieldKeplerianOrbit<>(
60                  v(6378137 + 500e3), v(0), v(0), v(0), v(0), v(0),
61                  PositionAngle.TRUE, eci, date, zero.add(Constants.EIGEN5C_EARTH_MU));
62          FieldSpacecraftState<Decimal64> s1 = new FieldSpacecraftState<>(orbit);
63          FieldSpacecraftState<Decimal64> s2 = s1.shiftedBy(-10);
64          FieldSpacecraftState<Decimal64> s3 = s2.shiftedBy(1);
65          FieldSpacecraftState<Decimal64> s4 = s3.shiftedBy(1);
66  
67          // actions
68          Assert.assertEquals(Action.CONTINUE, handler.eventOccurred(s1, detector, true));
69          Assert.assertEquals(Action.CONTINUE, handler.eventOccurred(s2, detector, true));
70          Assert.assertEquals(Action.CONTINUE, handler.eventOccurred(s3, detector, false));
71  
72          // verify
73          List<Event<FieldDateDetector<Decimal64>, Decimal64>> events = handler.getEvents();
74          Assert.assertEquals(3, events.size());
75          Assert.assertEquals(s1, events.get(0).getState());
76          Assert.assertEquals(s2, events.get(1).getState());
77          Assert.assertEquals(s3, events.get(2).getState());
78          Assert.assertEquals(true, events.get(0).isIncreasing());
79          Assert.assertEquals(true, events.get(1).isIncreasing());
80          Assert.assertEquals(false, events.get(2).isIncreasing());
81          for (Event<FieldDateDetector<Decimal64>, Decimal64> event : events) {
82              Assert.assertEquals(detector, event.getDetector());
83          }
84  
85          // action: clear
86          handler.clear();
87  
88          // verify is empty
89          Assert.assertEquals(0, handler.getEvents().size());
90  
91          // action add more
92          Assert.assertEquals(Action.CONTINUE, handler.eventOccurred(s4, detector, false));
93  
94          // verify new events
95          events = handler.getEvents();
96          Assert.assertEquals(1, events.size());
97          Assert.assertEquals(s4, events.get(0).getState());
98          Assert.assertEquals(false, events.get(0).isIncreasing());
99          Assert.assertEquals(detector, events.get(0).getDetector());
100     }
101 
102     /**
103      * Box a double value.
104      *
105      * @param value to copy.
106      * @return boxed {@code value}.
107      */
108     private Decimal64 v(double value) {
109         return new Decimal64(value);
110     }
111 
112 }