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.events;
18  
19  import java.util.concurrent.atomic.AtomicInteger;
20  
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.ode.events.Action;
23  import org.hipparchus.ode.nonstiff.AdaptiveStepsizeIntegrator;
24  import org.hipparchus.ode.nonstiff.DormandPrince853Integrator;
25  import org.junit.After;
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.orekit.Utils;
30  import org.orekit.errors.OrekitException;
31  import org.orekit.frames.FramesFactory;
32  import org.orekit.orbits.EquinoctialOrbit;
33  import org.orekit.orbits.Orbit;
34  import org.orekit.propagation.SpacecraftState;
35  import org.orekit.propagation.numerical.NumericalPropagator;
36  import org.orekit.time.AbsoluteDate;
37  import org.orekit.time.TimeScalesFactory;
38  import org.orekit.utils.PVCoordinates;
39  
40  public class AdapterDetectorTest {
41  
42      private double maxCheck;
43      private double threshold;
44      private double dt;
45      private Orbit iniOrbit;
46      private AbsoluteDate iniDate;
47      private NumericalPropagator propagator;
48  
49      @Test
50      public void testSimpleTimer() {
51          DateDetector dateDetector = new DateDetector(maxCheck, threshold, iniDate.shiftedBy(2.0*dt));
52          AdapterDetector adapter = new AdapterDetector(dateDetector);
53          Assert.assertSame(dateDetector, adapter.getDetector());
54          Assert.assertEquals(2 * dt, dateDetector.getDate().durationFrom(iniDate), 1.0e-10);
55          propagator.addEventDetector(adapter);
56          final SpacecraftState finalState = propagator.propagate(iniDate.shiftedBy(100.*dt));
57  
58          Assert.assertEquals(2.0*dt, finalState.getDate().durationFrom(iniDate), threshold);
59      }
60  
61      @Test
62      public void testOverrideAction() {
63          AtomicInteger count = new AtomicInteger(0);
64          DateDetector dateDetector = new DateDetector(maxCheck, threshold, iniDate.shiftedBy(2.0*dt));
65          AdapterDetector adapter = new AdapterDetector(dateDetector) {
66              /** {@inheritDoc} */
67              @Override
68              public Action eventOccurred(final SpacecraftState s, final boolean increasing) {
69                  count.incrementAndGet();
70                  return Action.RESET_STATE;
71              }
72          };
73          Assert.assertSame(dateDetector, adapter.getDetector());
74          Assert.assertEquals(2 * dt, dateDetector.getDate().durationFrom(iniDate), 1.0e-10);
75          propagator.addEventDetector(adapter);
76          Assert.assertEquals(0, count.get());
77          final SpacecraftState finalState = propagator.propagate(iniDate.shiftedBy(100.*dt));
78          Assert.assertEquals(1, count.get());
79  
80          Assert.assertEquals(100.0*dt, finalState.getDate().durationFrom(iniDate), threshold);
81      }
82  
83      @Before
84      public void setUp() {
85          try {
86              Utils.setDataRoot("regular-data");
87              final double mu = 3.9860047e14;
88              final Vector3D position  = new Vector3D(-6142438.668, 3492467.560, -25767.25680);
89              final Vector3D velocity  = new Vector3D(505.8479685, 942.7809215, 7435.922231);
90              iniDate  = new AbsoluteDate(1969, 7, 28, 4, 0, 0.0, TimeScalesFactory.getTT());
91              iniOrbit = new EquinoctialOrbit(new PVCoordinates(position, velocity),
92                                              FramesFactory.getEME2000(), iniDate, mu);
93              SpacecraftState initialState = new SpacecraftState(iniOrbit);
94              double[] absTolerance = {
95                  0.001, 1.0e-9, 1.0e-9, 1.0e-6, 1.0e-6, 1.0e-6, 0.001
96              };
97              double[] relTolerance = {
98                  1.0e-7, 1.0e-4, 1.0e-4, 1.0e-7, 1.0e-7, 1.0e-7, 1.0e-7
99              };
100             AdaptiveStepsizeIntegrator integrator =
101                 new DormandPrince853Integrator(0.001, 1000, absTolerance, relTolerance);
102             integrator.setInitialStepSize(60);
103             propagator = new NumericalPropagator(integrator);
104             propagator.setInitialState(initialState);
105             dt = 60.;
106             maxCheck  = 10.;
107             threshold = 10.e-10;
108         } catch (OrekitException oe) {
109             Assert.fail(oe.getLocalizedMessage());
110         }
111     }
112 
113     @After
114     public void tearDown() {
115         iniDate = null;
116         propagator = null;
117     }
118 
119 }