1   /* Copyright 2002-2022 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.analytical;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.ode.events.Action;
23  import org.hipparchus.util.FastMath;
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.orekit.Utils;
28  import org.orekit.bodies.CelestialBodyFactory;
29  import org.orekit.bodies.OneAxisEllipsoid;
30  import org.orekit.errors.OrekitException;
31  import org.orekit.frames.FramesFactory;
32  import org.orekit.orbits.KeplerianOrbit;
33  import org.orekit.orbits.Orbit;
34  import org.orekit.orbits.OrbitType;
35  import org.orekit.orbits.PositionAngle;
36  import org.orekit.propagation.BoundedPropagator;
37  import org.orekit.propagation.Propagator;
38  import org.orekit.propagation.SpacecraftState;
39  import org.orekit.propagation.events.EclipseDetector;
40  import org.orekit.propagation.events.handlers.EventHandler;
41  import org.orekit.time.AbsoluteDate;
42  import org.orekit.time.DateComponents;
43  import org.orekit.time.TimeComponents;
44  import org.orekit.time.TimeScalesFactory;
45  import org.orekit.utils.IERSConventions;
46  
47  public class EphemerisEventsTest {
48  
49      @Test
50      public void testEphemKeplerian() throws IllegalArgumentException, OrekitException {
51          checkEphem(OrbitType.KEPLERIAN);
52      }
53  
54      @Test
55      public void testEphemCircular() throws IllegalArgumentException, OrekitException {
56          checkEphem(OrbitType.CIRCULAR);
57      }
58  
59      @Test
60      public void testEphemEquinoctial() throws IllegalArgumentException, OrekitException {
61          checkEphem(OrbitType.EQUINOCTIAL);
62      }
63  
64      @Test
65      public void testEphemCartesian() throws IllegalArgumentException, OrekitException {
66          checkEphem(OrbitType.CARTESIAN);
67      }
68  
69      private Ephemeris buildEphem(OrbitType type)
70          throws IllegalArgumentException, OrekitException {
71  
72          double mass = 2500;
73          double a = 7187990.1979844316;
74          double e = 0.5e-4;
75          double i = 1.7105407051081795;
76          double omega = 1.9674147913622104;
77          double OMEGA = FastMath.toRadians(261);
78          double lv = 0;
79          double mu  = 3.9860047e14;
80          double ae  = 6.378137e6;
81          double c20 = -1.08263e-3;
82          double c30 = 2.54e-6;
83          double c40 = 1.62e-6;
84          double c50 = 2.3e-7;
85          double c60 = -5.5e-7;
86  
87          double deltaT = finalDate.durationFrom(initDate);
88  
89          Orbit transPar = new KeplerianOrbit(a, e, i, omega, OMEGA, lv, PositionAngle.TRUE,
90                                              FramesFactory.getEME2000(), initDate, mu);
91  
92          int nbIntervals = 720;
93          Propagator propagator =
94                  new EcksteinHechlerPropagator(transPar, mass, ae, mu, c20, c30, c40, c50, c60);
95  
96          List<SpacecraftState> tab = new ArrayList<SpacecraftState>(nbIntervals + 1);
97          for (int j = 0; j<= nbIntervals; j++) {
98              SpacecraftState state = propagator.propagate(initDate.shiftedBy((j * deltaT) / nbIntervals));
99              tab.add(new SpacecraftState(type.convertType(state.getOrbit()),
100                                         state.getAttitude(), state.getMass()));
101         }
102 
103         return new Ephemeris(tab, 2);
104     }
105 
106     private EclipseDetector buildEclipsDetector(final OrbitType type) {
107 
108         double sunRadius = 696000000.;
109         double earthRadius = 6400000.;
110 
111         EclipseDetector ecl = new EclipseDetector(CelestialBodyFactory.getSun(), sunRadius,
112                                                   new OneAxisEllipsoid(earthRadius,
113                                                                        0.0,
114                                                                        FramesFactory.getITRF(IERSConventions.IERS_2010, true))).
115                               withMaxCheck(60.0).
116                               withThreshold(1.0e-3).
117                               withHandler(new EventHandler<EclipseDetector>() {
118                                 public Action eventOccurred(SpacecraftState s, EclipseDetector detector,
119                                                             boolean increasing)
120                                     {
121                                     Assert.assertEquals(type, s.getOrbit().getType());
122                                     if (increasing) {
123                                         ++inEclipsecounter;
124                                     } else {
125                                         ++outEclipsecounter;
126                                     }
127                                     return Action.CONTINUE;
128                                 }
129                             });
130 
131         return ecl;
132     }
133 
134     private void checkEphem(OrbitType type)
135         throws IllegalArgumentException, OrekitException {
136 
137         initDate = new AbsoluteDate(new DateComponents(2004, 01, 01),
138                                     TimeComponents.H00,
139                                     TimeScalesFactory.getUTC());
140 
141         finalDate = new AbsoluteDate(new DateComponents(2004, 01, 02),
142                                      TimeComponents.H00,
143                                      TimeScalesFactory.getUTC());
144 
145 
146 
147         BoundedPropagator ephem = buildEphem(type);
148 
149         ephem.addEventDetector(buildEclipsDetector(type));
150 
151         AbsoluteDate computeEnd = new AbsoluteDate(finalDate, -1000.0);
152 
153         ephem.clearStepHandlers();
154         SpacecraftState state = ephem.propagate(computeEnd);
155         Assert.assertEquals(computeEnd, state.getDate());
156         Assert.assertEquals(14, inEclipsecounter);
157         Assert.assertEquals(14, outEclipsecounter);
158     }
159 
160     @Before
161     public void setUp() {
162         Utils.setDataRoot("regular-data");
163         inEclipsecounter = 0;
164         outEclipsecounter = 0;
165     }
166 
167     private AbsoluteDate initDate;
168     private AbsoluteDate finalDate;
169     private int inEclipsecounter;
170     private int outEclipsecounter;
171 
172 }