1   /* Copyright 2023-2025 Alberto Ferrero
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    * Alberto Ferrero 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.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.util.FastMath;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  import org.orekit.Utils;
25  import org.orekit.bodies.OneAxisEllipsoid;
26  import org.orekit.frames.FramesFactory;
27  import org.orekit.orbits.EquinoctialOrbit;
28  import org.orekit.orbits.Orbit;
29  import org.orekit.propagation.Propagator;
30  import org.orekit.propagation.SpacecraftState;
31  import org.orekit.propagation.analytical.EcksteinHechlerPropagator;
32  import org.orekit.propagation.events.EventsLogger.LoggedEvent;
33  import org.orekit.propagation.events.handlers.ContinueOnEvent;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.TimeScale;
36  import org.orekit.time.TimeScalesFactory;
37  import org.orekit.utils.Constants;
38  import org.orekit.utils.IERSConventions;
39  import org.orekit.utils.PVCoordinates;
40  
41  public class LatitudeRangeCrossingDetectorTest {
42  
43      @Test
44      public void testRegularCrossing() {
45  
46          final OneAxisEllipsoid earth = new OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
47                                                              Constants.WGS84_EARTH_FLATTENING,
48                                                              FramesFactory.getITRF(IERSConventions.IERS_2010, true));
49  
50          LatitudeRangeCrossingDetector d =
51                  new LatitudeRangeCrossingDetector(60.0, 1.e-6, earth,
52                      FastMath.toRadians(50.0), FastMath.toRadians(60.0)).
53                  withHandler(new ContinueOnEvent());
54  
55          Assertions.assertEquals(60.0, d.getMaxCheckInterval().currentInterval(null, true), 1.0e-15);
56          Assertions.assertEquals(1.0e-6, d.getThreshold(), 1.0e-15);
57          Assertions.assertEquals(50.0, FastMath.toDegrees(d.getFromLatitude()), 1.0e-14);
58          Assertions.assertEquals(60.0, FastMath.toDegrees(d.getToLatitude()), 1.0e-14);
59          Assertions.assertEquals(AbstractDetector.DEFAULT_MAX_ITER, d.getMaxIterationCount());
60  
61          final TimeScale utc = TimeScalesFactory.getUTC();
62          final Vector3D position = new Vector3D(-6142438.668, 3492467.56, -25767.257);
63          final Vector3D velocity = new Vector3D(505.848, 942.781, 7435.922);
64          final AbsoluteDate date = new AbsoluteDate(2003, 9, 16, utc);
65          final Orbit orbit = new EquinoctialOrbit(new PVCoordinates(position,  velocity),
66                                                   FramesFactory.getEME2000(), date,
67                                                   Constants.EIGEN5C_EARTH_MU);
68  
69          Propagator propagator =
70              new EcksteinHechlerPropagator(orbit,
71                                            Constants.EIGEN5C_EARTH_EQUATORIAL_RADIUS,
72                                            Constants.EIGEN5C_EARTH_MU,
73                                            Constants.EIGEN5C_EARTH_C20,
74                                            Constants.EIGEN5C_EARTH_C30,
75                                            Constants.EIGEN5C_EARTH_C40,
76                                            Constants.EIGEN5C_EARTH_C50,
77                                            Constants.EIGEN5C_EARTH_C60);
78  
79          EventsLogger logger = new EventsLogger();
80          propagator.addEventDetector(logger.monitorDetector(d));
81  
82          propagator.propagate(date.shiftedBy(Constants.JULIAN_DAY));
83          for (LoggedEvent e : logger.getLoggedEvents()) {
84              SpacecraftState state = e.getState();
85              double latitude = earth.transform(state.getPVCoordinates(earth.getBodyFrame()).getPosition(),
86                  earth.getBodyFrame(), null).getLatitude();
87              if (e.isIncreasing()) {
88                  if (state.getPVCoordinates().getVelocity().getZ() < 0) {
89                      // entering northward
90                      Assertions.assertEquals(60.0, FastMath.toDegrees(latitude), FastMath.toRadians(1e-4));
91                  } else {
92                      // entering southward
93                      Assertions.assertEquals(50.0, FastMath.toDegrees(latitude), FastMath.toRadians(1e-4));
94                  }
95              } else {
96                  if (state.getPVCoordinates().getVelocity().getZ() < 0) {
97                      // exiting southward
98                      Assertions.assertEquals(50.0, FastMath.toDegrees(latitude), FastMath.toRadians(1e-4));
99                  } else {
100                     // exiting northward
101                     Assertions.assertEquals(60.0, FastMath.toDegrees(latitude), FastMath.toRadians(1e-4));
102                 }
103             }
104         }
105         Assertions.assertEquals(30 * 2, logger.getLoggedEvents().size());
106 
107     }
108 
109     @Test
110     public void testNoCrossing() {
111 
112         final OneAxisEllipsoid earth = new OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
113                                                             Constants.WGS84_EARTH_FLATTENING,
114                                                             FramesFactory.getITRF(IERSConventions.IERS_2010, true));
115 
116         LatitudeRangeCrossingDetector d =
117                 new LatitudeRangeCrossingDetector(10.0, 1.e-6, earth,
118                     FastMath.toRadians(82.0),  FastMath.toRadians(87.0)).
119                 withHandler(new ContinueOnEvent());
120 
121         Assertions.assertEquals(10.0, d.getMaxCheckInterval().currentInterval(null, true), 1.0e-15);
122         Assertions.assertEquals(1.0e-6, d.getThreshold(), 1.0e-15);
123         Assertions.assertEquals(82.0, FastMath.toDegrees(d.getFromLatitude()), 1.0e-14);
124         Assertions.assertEquals(87.0, FastMath.toDegrees(d.getToLatitude()), 1.0e-14);
125         Assertions.assertEquals(AbstractDetector.DEFAULT_MAX_ITER, d.getMaxIterationCount());
126 
127         final TimeScale utc = TimeScalesFactory.getUTC();
128         final Vector3D position = new Vector3D(-6142438.668, 3492467.56, -25767.257);
129         final Vector3D velocity = new Vector3D(505.848, 942.781, 7435.922);
130         final AbsoluteDate date = new AbsoluteDate(2003, 9, 16, utc);
131         final Orbit orbit = new EquinoctialOrbit(new PVCoordinates(position,  velocity),
132                                                  FramesFactory.getEME2000(), date,
133                                                  Constants.EIGEN5C_EARTH_MU);
134 
135         Propagator propagator =
136             new EcksteinHechlerPropagator(orbit,
137                                           Constants.EIGEN5C_EARTH_EQUATORIAL_RADIUS,
138                                           Constants.EIGEN5C_EARTH_MU,
139                                           Constants.EIGEN5C_EARTH_C20,
140                                           Constants.EIGEN5C_EARTH_C30,
141                                           Constants.EIGEN5C_EARTH_C40,
142                                           Constants.EIGEN5C_EARTH_C50,
143                                           Constants.EIGEN5C_EARTH_C60);
144 
145         EventsLogger logger = new EventsLogger();
146         propagator.addEventDetector(logger.monitorDetector(d));
147 
148         propagator.propagate(date.shiftedBy(Constants.JULIAN_DAY));
149         Assertions.assertEquals(0, logger.getLoggedEvents().size());
150 
151     }
152 
153     @BeforeEach
154     public void setUp() {
155         Utils.setDataRoot("regular-data");
156     }
157 
158 }
159