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.propagation.events;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.Utils;
24  import org.orekit.bodies.CelestialBodyFactory;
25  import org.orekit.bodies.OneAxisEllipsoid;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.FramesFactory;
28  import org.orekit.orbits.KeplerianOrbit;
29  import org.orekit.orbits.Orbit;
30  import org.orekit.orbits.PositionAngleType;
31  import org.orekit.propagation.SpacecraftState;
32  import org.orekit.propagation.analytical.KeplerianPropagator;
33  import org.orekit.propagation.events.handlers.StopOnEvent;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.TimeScalesFactory;
36  
37  public class AltitudeDetectorTest {
38  
39      @Test
40      public void testBackAndForth() {
41  
42          final Frame EME2000 = FramesFactory.getEME2000();
43          final AbsoluteDate initialDate = new AbsoluteDate(2009, 1, 1, TimeScalesFactory.getUTC());
44          final double a = 8000000;
45          final double e = 0.1;
46          final double earthRadius = 6378137.0;
47          final double earthF = 1.0 / 298.257223563;
48          final double apogee = a*(1+e);
49          final double alt = apogee - earthRadius - 500;
50  
51  
52  
53          // initial state is at apogee
54          final Orbit initialOrbit = new KeplerianOrbit(a, e, 0, 0, 0, FastMath.PI, PositionAngleType.MEAN, EME2000,
55                                                        initialDate, CelestialBodyFactory.getEarth().getGM());
56          final SpacecraftState initialState = new SpacecraftState(initialOrbit);
57          final KeplerianPropagator kepPropagator = new KeplerianPropagator(initialOrbit);
58          final OneAxisEllipsoid earth = new OneAxisEllipsoid(earthRadius, earthF, EME2000);
59          final AltitudeDetector altDetector = new AltitudeDetector(alt, earth).
60                                               withHandler(new StopOnEvent());
61          Assertions.assertEquals(alt, altDetector.getAltitude(), 1.0e-15);
62          Assertions.assertSame(earth, altDetector.getBodyShape());
63  
64          // altitudeDetector should stop propagation upon reaching required altitude
65          kepPropagator.addEventDetector(altDetector);
66  
67          // propagation to the future
68          SpacecraftState finalState = kepPropagator.propagate(initialDate.shiftedBy(1000));
69          Assertions.assertEquals(finalState.getPosition().getNorm()-earthRadius, alt, 1e-5);
70          Assertions.assertEquals(44.079, finalState.getDate().durationFrom(initialDate), 1.0e-3);
71  
72          // propagation to the past
73          kepPropagator.resetInitialState(initialState);
74          finalState = kepPropagator.propagate(initialDate.shiftedBy(-1000));
75          Assertions.assertEquals(finalState.getPosition().getNorm()-earthRadius, alt, 1e-5);
76          Assertions.assertEquals(-44.079, finalState.getDate().durationFrom(initialDate), 1.0e-3);
77  
78      }
79  
80      @BeforeEach
81      public void setUp() {
82          Utils.setDataRoot("regular-data");
83      }
84  
85  }
86