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.sampling;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  
22  import java.util.ArrayDeque;
23  import java.util.Arrays;
24  import java.util.Queue;
25  import java.util.concurrent.Callable;
26  import java.util.concurrent.ExecutionException;
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Executors;
29  import java.util.concurrent.Future;
30  import java.util.concurrent.TimeoutException;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.hipparchus.ode.nonstiff.ClassicalRungeKuttaIntegrator;
34  import org.hipparchus.util.FastMath;
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.orekit.Utils;
38  import org.orekit.bodies.CelestialBodyFactory;
39  import org.orekit.frames.FactoryManagedFrame;
40  import org.orekit.frames.Frame;
41  import org.orekit.frames.FramesFactory;
42  import org.orekit.orbits.KeplerianOrbit;
43  import org.orekit.orbits.OrbitType;
44  import org.orekit.orbits.PositionAngle;
45  import org.orekit.propagation.Propagator;
46  import org.orekit.propagation.SpacecraftState;
47  import org.orekit.propagation.analytical.KeplerianPropagator;
48  import org.orekit.propagation.events.DateDetector;
49  import org.orekit.propagation.events.handlers.ContinueOnEvent;
50  import org.orekit.propagation.numerical.NumericalPropagator;
51  import org.orekit.time.AbsoluteDate;
52  import org.orekit.time.TimeScalesFactory;
53  import org.orekit.utils.Constants;
54  
55  public class OrekitStepHandlerTest {
56  
57      @Test
58      public void testForwardBackwardStep()
59          throws InterruptedException, ExecutionException, TimeoutException {
60          final AbsoluteDate initialDate = new AbsoluteDate(2014, 01, 01, 00, 00,
61                                                            00.000,
62                                                            TimeScalesFactory
63                                                                .getUTC());
64          final double mu = CelestialBodyFactory.getEarth().getGM();
65          FactoryManagedFrame inertialFrame = FramesFactory.getEME2000();
66  
67          final double propagationTime = 7200.0;// seconds
68          final double fixedStepSize = 3600; // seconds
69  
70          final double semimajorAxis = 8000e3; // meters
71          final double eccentricity = 0.001; // unitless
72          final double inclination = FastMath.toRadians(15.0);
73          final double argPerigee = FastMath.toRadians(10.0);
74          final double raan = FastMath.toRadians(45.0);
75          final double trueAnomaly = FastMath.toRadians(10.0);
76  
77          KeplerianOrbit initialOrbit = new KeplerianOrbit(semimajorAxis,
78                                                           eccentricity,
79                                                           inclination,
80                                                           argPerigee, raan,
81                                                           trueAnomaly,
82                                                           PositionAngle.TRUE,
83                                                           inertialFrame,
84                                                           initialDate, mu);
85  
86          final Propagator kepler = new KeplerianPropagator(initialOrbit);
87  
88          kepler.setStepHandler(fixedStepSize, currentState -> {});
89  
90          kepler.propagate(initialDate.shiftedBy(propagationTime));
91  
92          final double stepSizeInSeconds = 120;
93          ExecutorService service = Executors.newSingleThreadExecutor();
94          for (double elapsedTime = 0; elapsedTime <= propagationTime; elapsedTime += stepSizeInSeconds) {
95              final double dt = elapsedTime;
96              Future<SpacecraftState> stateFuture = service
97                  .submit(new Callable<SpacecraftState>() {
98  
99                      public SpacecraftState call()
100                         {
101                         return kepler.propagate(initialDate.shiftedBy(dt));
102                     }
103                 });
104 
105             SpacecraftState finalState = stateFuture.get(5, TimeUnit.SECONDS);
106             assertNotNull(finalState);
107         }
108     }
109 
110     /**
111      * Check {@link OrekitStepInterpolator#isPreviousStateInterpolated()} and {@link
112      * OrekitStepInterpolator#isCurrentStateInterpolated()}.
113      */
114     @Test
115     public void testIsInterpolated() {
116         // setup
117         NumericalPropagator propagator =
118                 new NumericalPropagator(new ClassicalRungeKuttaIntegrator(60));
119         AbsoluteDate date = AbsoluteDate.J2000_EPOCH;
120         Frame eci = FramesFactory.getGCRF();
121         SpacecraftState ic = new SpacecraftState(new KeplerianOrbit(
122                 6378137 + 500e3, 1e-3, 0, 0, 0, 0,
123                 PositionAngle.TRUE, eci, date, Constants.EIGEN5C_EARTH_MU));
124         propagator.setInitialState(ic);
125         propagator.setOrbitType(OrbitType.CARTESIAN);
126         // detector triggers half way through second step
127         DateDetector detector =
128                 new DateDetector(date.shiftedBy(90)).withHandler(new ContinueOnEvent<>());
129         propagator.addEventDetector(detector);
130 
131         // action and verify
132         Queue<Boolean> expected =
133                 new ArrayDeque<>(Arrays.asList(false, false, false, true, true, false));
134         propagator.setStepHandler(interpolator -> {
135             assertEquals(expected.poll(), interpolator.isPreviousStateInterpolated());
136             assertEquals(expected.poll(), interpolator.isCurrentStateInterpolated());
137         });
138         final AbsoluteDate end = date.shiftedBy(120);
139         assertEquals(end, propagator.propagate(end).getDate());
140     }
141 
142     @Before
143     public void setUp() {
144         Utils.setDataRoot("regular-data");
145     }
146 
147 }