Search in sources :

Example 6 with EventHandler

use of org.orekit.propagation.events.handlers.EventHandler in project Orekit by CS-SI.

the class FunctionalDetectorTest method testFunctionalDetector.

/**
 * Check {@link FunctionalDetector}.
 *
 * @throws OrekitException on error.
 */
@Test
public void testFunctionalDetector() throws OrekitException {
    // setup
    GFunction g = SpacecraftState::getMass;
    EventHandler<EventDetector> handler = (s, detector, increasing) -> Action.STOP;
    // action
    FunctionalDetector detector = new FunctionalDetector().withMaxIter(1).withThreshold(2).withMaxCheck(3).withGFunction(g).withHandler(handler);
    // verify
    MatcherAssert.assertThat(detector.getMaxIterationCount(), CoreMatchers.is(1));
    MatcherAssert.assertThat(detector.getThreshold(), CoreMatchers.is(2.0));
    MatcherAssert.assertThat(detector.getMaxCheckInterval(), CoreMatchers.is(3.0));
    MatcherAssert.assertThat(detector.getHandler(), CoreMatchers.is(handler));
    MatcherAssert.assertThat(detector.getGFunction(), CoreMatchers.is(g));
    SpacecraftState state = new SpacecraftState(new CartesianOrbit(new PVCoordinates(new Vector3D(1, 2, 3), new Vector3D(4, 5, 6)), FramesFactory.getGCRF(), AbsoluteDate.CCSDS_EPOCH, 4), 5);
    MatcherAssert.assertThat(detector.g(state), CoreMatchers.is(5.0));
    MatcherAssert.assertThat(detector.eventOccurred(null, false), CoreMatchers.is(Action.STOP));
}
Also used : CartesianOrbit(org.orekit.orbits.CartesianOrbit) CoreMatchers(org.hamcrest.CoreMatchers) Vector3D(org.hipparchus.geometry.euclidean.threed.Vector3D) FramesFactory(org.orekit.frames.FramesFactory) Test(org.junit.Test) PVCoordinates(org.orekit.utils.PVCoordinates) MatcherAssert(org.hamcrest.MatcherAssert) EventHandler(org.orekit.propagation.events.handlers.EventHandler) OrekitException(org.orekit.errors.OrekitException) SpacecraftState(org.orekit.propagation.SpacecraftState) GFunction(org.orekit.propagation.events.FunctionalDetector.GFunction) Action(org.orekit.propagation.events.handlers.EventHandler.Action) AbsoluteDate(org.orekit.time.AbsoluteDate) SpacecraftState(org.orekit.propagation.SpacecraftState) CartesianOrbit(org.orekit.orbits.CartesianOrbit) Vector3D(org.hipparchus.geometry.euclidean.threed.Vector3D) PVCoordinates(org.orekit.utils.PVCoordinates) GFunction(org.orekit.propagation.events.FunctionalDetector.GFunction) Test(org.junit.Test)

Example 7 with EventHandler

use of org.orekit.propagation.events.handlers.EventHandler in project Orekit by CS-SI.

the class CloseEventsAbstractTest method testShortBracketingInterval.

/**
 * The root finder requires the start point to be in the interval (a, b) which is hard
 * when there aren't many numbers between a and b. This test uses a second event
 * detector to force a very small window for the first event detector.
 */
@Test
public void testShortBracketingInterval() throws OrekitException {
    // setup
    double maxCheck = 10;
    double tolerance = 1e-6;
    final double t1 = FastMath.nextUp(10.0), t2 = 10.5;
    // shared event list so we know the order in which they occurred
    List<Event<EventDetector>> events = new ArrayList<>();
    // never zero so there is no easy way out
    EventDetector detectorA = new AbstractDetector<EventDetector>(maxCheck, tolerance, 100, new RecordAndContinue<>(events)) {

        private static final long serialVersionUID = 1L;

        @Override
        public double g(SpacecraftState state) {
            final AbsoluteDate t = state.getDate();
            if (t.compareTo(epoch.shiftedBy(t1)) < 0) {
                return -1;
            } else if (t.compareTo(epoch.shiftedBy(t2)) < 0) {
                return 1;
            } else {
                return -1;
            }
        }

        @Override
        protected EventDetector create(double newMaxCheck, double newThreshold, int newMaxIter, EventHandler<? super EventDetector> newHandler) {
            return null;
        }
    };
    TimeDetector detectorB = new TimeDetector(t1).withHandler(new RecordAndContinue<>(events)).withMaxCheck(maxCheck).withThreshold(tolerance);
    Propagator propagator = getPropagator(10);
    propagator.addEventDetector(detectorA);
    propagator.addEventDetector(detectorB);
    // action
    propagator.propagate(epoch.shiftedBy(30.0));
    // verify
    Assert.assertEquals(3, events.size());
    Assert.assertEquals(t1, events.get(0).getState().getDate().durationFrom(epoch), tolerance);
    Assert.assertEquals(true, events.get(0).isIncreasing());
    Assert.assertSame(detectorA, events.get(0).getDetector());
    Assert.assertEquals(t1, events.get(1).getState().getDate().durationFrom(epoch), tolerance);
    Assert.assertEquals(true, events.get(1).isIncreasing());
    Assert.assertSame(detectorB, events.get(1).getDetector());
    Assert.assertEquals(t2, events.get(2).getState().getDate().durationFrom(epoch), tolerance);
    Assert.assertEquals(false, events.get(2).isIncreasing());
    Assert.assertSame(detectorA, events.get(2).getDetector());
}
Also used : SpacecraftState(org.orekit.propagation.SpacecraftState) Propagator(org.orekit.propagation.Propagator) ArrayList(java.util.ArrayList) Event(org.orekit.propagation.events.handlers.RecordAndContinue.Event) StopOnEvent(org.orekit.propagation.events.handlers.StopOnEvent) EventHandler(org.orekit.propagation.events.handlers.EventHandler) AbsoluteDate(org.orekit.time.AbsoluteDate) Test(org.junit.Test)

Example 8 with EventHandler

use of org.orekit.propagation.events.handlers.EventHandler in project Orekit by CS-SI.

the class DateDetectorTest method testGenericHandler.

/**
 * Check that a generic event handler can be used with an event detector.
 *
 * @throws OrekitException on error.
 */
@Test
public void testGenericHandler() throws OrekitException {
    // setup
    dateDetector = new DateDetector(maxCheck, threshold, iniDate.shiftedBy(dt));
    // generic event handler that works with all detectors.
    EventHandler<EventDetector> handler = new EventHandler<EventDetector>() {

        @Override
        public Action eventOccurred(SpacecraftState s, EventDetector detector, boolean increasing) throws OrekitException {
            Assert.assertSame(dateDetector, detector);
            return Action.STOP;
        }

        @Override
        public SpacecraftState resetState(EventDetector detector, SpacecraftState oldState) throws OrekitException {
            throw new RuntimeException("Should not be called");
        }
    };
    // action
    dateDetector = dateDetector.withHandler(handler);
    propagator.addEventDetector(dateDetector);
    SpacecraftState finalState = propagator.propagate(iniDate.shiftedBy(100 * dt));
    // verify
    Assert.assertEquals(dt, finalState.getDate().durationFrom(iniDate), threshold);
}
Also used : SpacecraftState(org.orekit.propagation.SpacecraftState) EventHandler(org.orekit.propagation.events.handlers.EventHandler) Test(org.junit.Test)

Aggregations

SpacecraftState (org.orekit.propagation.SpacecraftState)8 EventHandler (org.orekit.propagation.events.handlers.EventHandler)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Propagator (org.orekit.propagation.Propagator)4 Event (org.orekit.propagation.events.handlers.RecordAndContinue.Event)4 StopOnEvent (org.orekit.propagation.events.handlers.StopOnEvent)4 OrekitException (org.orekit.errors.OrekitException)3 Action (org.orekit.propagation.events.handlers.EventHandler.Action)3 AbsoluteDate (org.orekit.time.AbsoluteDate)3 OrekitExceptionWrapper (org.orekit.errors.OrekitExceptionWrapper)2 DateDetector (org.orekit.propagation.events.DateDetector)2 NumericalPropagator (org.orekit.propagation.numerical.NumericalPropagator)2 CoreMatchers (org.hamcrest.CoreMatchers)1 MatcherAssert (org.hamcrest.MatcherAssert)1 MultivariateVectorFunction (org.hipparchus.analysis.MultivariateVectorFunction)1 Vector3D (org.hipparchus.geometry.euclidean.threed.Vector3D)1 ArrayRealVector (org.hipparchus.linear.ArrayRealVector)1 RealMatrix (org.hipparchus.linear.RealMatrix)1 RealVector (org.hipparchus.linear.RealVector)1