Search in sources :

Example 1 with EventHandler

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

the class JacobianPropagatorConverter method getObjectiveFunction.

/**
 * {@inheritDoc}
 */
protected MultivariateVectorFunction getObjectiveFunction() {
    return new MultivariateVectorFunction() {

        /**
         * {@inheritDoc}
         */
        public double[] value(final double[] arg) throws IllegalArgumentException, OrekitExceptionWrapper {
            try {
                final double[] value = new double[getTargetSize()];
                final NumericalPropagator prop = builder.buildPropagator(arg);
                final int stateSize = isOnlyPosition() ? 3 : 6;
                final List<SpacecraftState> sample = getSample();
                for (int i = 0; i < sample.size(); ++i) {
                    final int row = i * stateSize;
                    if (prop.getInitialState().getDate().equals(sample.get(i).getDate())) {
                        // use initial state
                        fillRows(value, row, prop.getInitialState());
                    } else {
                        // use a date detector to pick up states
                        prop.addEventDetector(new DateDetector(sample.get(i).getDate()).withHandler(new EventHandler<DateDetector>() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public Action eventOccurred(final SpacecraftState state, final DateDetector detector, final boolean increasing) throws OrekitException {
                                fillRows(value, row, state);
                                return row + stateSize >= getTargetSize() ? Action.STOP : Action.CONTINUE;
                            }
                        }));
                    }
                }
                prop.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
                return value;
            } catch (OrekitException ex) {
                throw new OrekitExceptionWrapper(ex);
            }
        }
    };
}
Also used : DateDetector(org.orekit.propagation.events.DateDetector) SpacecraftState(org.orekit.propagation.SpacecraftState) OrekitExceptionWrapper(org.orekit.errors.OrekitExceptionWrapper) NumericalPropagator(org.orekit.propagation.numerical.NumericalPropagator) EventHandler(org.orekit.propagation.events.handlers.EventHandler) OrekitException(org.orekit.errors.OrekitException) MultivariateVectorFunction(org.hipparchus.analysis.MultivariateVectorFunction)

Example 2 with EventHandler

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

the class JacobianPropagatorConverter method getModel.

/**
 * {@inheritDoc}
 */
protected MultivariateJacobianFunction getModel() {
    return new MultivariateJacobianFunction() {

        /**
         * {@inheritDoc}
         */
        public Pair<RealVector, RealMatrix> value(final RealVector point) throws IllegalArgumentException, OrekitExceptionWrapper {
            try {
                final RealVector value = new ArrayRealVector(getTargetSize());
                final RealMatrix jacobian = MatrixUtils.createRealMatrix(getTargetSize(), point.getDimension());
                final NumericalPropagator prop = builder.buildPropagator(point.toArray());
                final int stateSize = isOnlyPosition() ? 3 : 6;
                final ParameterDriversList orbitalParameters = builder.getOrbitalParametersDrivers();
                final PartialDerivativesEquations pde = new PartialDerivativesEquations("pde", prop);
                final ParameterDriversList propagationParameters = pde.getSelectedParameters();
                prop.setInitialState(pde.setInitialJacobians(prop.getInitialState()));
                final JacobiansMapper mapper = pde.getMapper();
                final List<SpacecraftState> sample = getSample();
                for (int i = 0; i < sample.size(); ++i) {
                    final int row = i * stateSize;
                    if (prop.getInitialState().getDate().equals(sample.get(i).getDate())) {
                        // use initial state and Jacobians
                        fillRows(value, jacobian, row, prop.getInitialState(), stateSize, orbitalParameters, propagationParameters, mapper);
                    } else {
                        // use a date detector to pick up state and Jacobians
                        prop.addEventDetector(new DateDetector(sample.get(i).getDate()).withHandler(new EventHandler<DateDetector>() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public Action eventOccurred(final SpacecraftState state, final DateDetector detector, final boolean increasing) throws OrekitException {
                                fillRows(value, jacobian, row, state, stateSize, orbitalParameters, propagationParameters, mapper);
                                return row + stateSize >= getTargetSize() ? Action.STOP : Action.CONTINUE;
                            }
                        }));
                    }
                }
                prop.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
                return new Pair<RealVector, RealMatrix>(value, jacobian);
            } catch (OrekitException ex) {
                throw new OrekitExceptionWrapper(ex);
            }
        }
    };
}
Also used : DateDetector(org.orekit.propagation.events.DateDetector) OrekitExceptionWrapper(org.orekit.errors.OrekitExceptionWrapper) ArrayRealVector(org.hipparchus.linear.ArrayRealVector) EventHandler(org.orekit.propagation.events.handlers.EventHandler) MultivariateJacobianFunction(org.hipparchus.optim.nonlinear.vector.leastsquares.MultivariateJacobianFunction) SpacecraftState(org.orekit.propagation.SpacecraftState) RealMatrix(org.hipparchus.linear.RealMatrix) NumericalPropagator(org.orekit.propagation.numerical.NumericalPropagator) ParameterDriversList(org.orekit.utils.ParameterDriversList) PartialDerivativesEquations(org.orekit.propagation.numerical.PartialDerivativesEquations) ArrayRealVector(org.hipparchus.linear.ArrayRealVector) RealVector(org.hipparchus.linear.RealVector) OrekitException(org.orekit.errors.OrekitException) JacobiansMapper(org.orekit.propagation.numerical.JacobiansMapper) Pair(org.hipparchus.util.Pair)

Example 3 with EventHandler

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

the class CloseEventsAbstractTest method testShortBracketingIntervalReverse.

/**
 * 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 testShortBracketingIntervalReverse() throws OrekitException {
    // setup
    double maxCheck = 10;
    double tolerance = 1e-6;
    final double t1 = FastMath.nextDown(-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(false, 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(true, 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 4 with EventHandler

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

the class CloseEventsAbstractTest method testEventChangesGFunctionDefinition.

/**
 * test when one event detector changes the definition of another's g function before
 * the end of the step as a result of a continue action. Not sure if this should be
 * officially supported, but it is used in Orekit's DateDetector, it's useful, and not
 * too hard to implement.
 */
@Test
public void testEventChangesGFunctionDefinition() throws OrekitException {
    // setup
    double maxCheck = 5;
    double tolerance = 1e-6;
    double t1 = 11, t2 = 19;
    // shared event list so we know the order in which they occurred
    List<Event<EventDetector>> events = new ArrayList<>();
    // mutable boolean
    boolean[] swap = new boolean[1];
    final ContinuousDetector detectorA = new ContinuousDetector(t1).withThreshold(maxCheck).withThreshold(tolerance).withHandler(new RecordAndContinue<EventDetector>(events) {

        @Override
        public Action eventOccurred(SpacecraftState s, EventDetector detector, boolean increasing) {
            swap[0] = true;
            return super.eventOccurred(s, detector, increasing);
        }
    });
    ContinuousDetector detectorB = new ContinuousDetector(t2);
    EventDetector detectorC = new AbstractDetector<EventDetector>(maxCheck, tolerance, 100, new RecordAndContinue<>(events)) {

        private static final long serialVersionUID = 1L;

        @Override
        public double g(SpacecraftState s) throws OrekitException {
            if (swap[0]) {
                return detectorB.g(s);
            } else {
                return -1;
            }
        }

        @Override
        protected EventDetector create(double newMaxCheck, double newThreshold, int newMaxIter, EventHandler<? super EventDetector> newHandler) {
            return null;
        }
    };
    Propagator propagator = getPropagator(10);
    propagator.addEventDetector(detectorA);
    propagator.addEventDetector(detectorC);
    // action
    propagator.propagate(epoch.shiftedBy(30));
    // verify
    Assert.assertEquals(2, 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(t2, events.get(1).getState().getDate().durationFrom(epoch), tolerance);
    Assert.assertEquals(true, events.get(1).isIncreasing());
    Assert.assertSame(detectorC, events.get(1).getDetector());
}
Also used : Action(org.orekit.propagation.events.handlers.EventHandler.Action) ArrayList(java.util.ArrayList) EventHandler(org.orekit.propagation.events.handlers.EventHandler) SpacecraftState(org.orekit.propagation.SpacecraftState) Propagator(org.orekit.propagation.Propagator) Event(org.orekit.propagation.events.handlers.RecordAndContinue.Event) StopOnEvent(org.orekit.propagation.events.handlers.StopOnEvent) Test(org.junit.Test)

Example 5 with EventHandler

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

the class CloseEventsAbstractTest method testEventChangesGFunctionDefinitionReverse.

/**
 * test when one event detector changes the definition of another's g function before
 * the end of the step as a result of a continue action. Not sure if this should be
 * officially supported, but it is used in Orekit's DateDetector, it's useful, and not
 * too hard to implement.
 */
@Test
public void testEventChangesGFunctionDefinitionReverse() throws OrekitException {
    // setup
    double maxCheck = 5;
    double tolerance = 1e-6;
    double t1 = -11, t2 = -19;
    // shared event list so we know the order in which they occurred
    List<Event<EventDetector>> events = new ArrayList<>();
    // mutable boolean
    boolean[] swap = new boolean[1];
    ContinuousDetector detectorA = new ContinuousDetector(t1).withMaxCheck(maxCheck).withThreshold(tolerance).withHandler(new RecordAndContinue<EventDetector>(events) {

        @Override
        public Action eventOccurred(SpacecraftState s, EventDetector detector, boolean increasing) {
            swap[0] = true;
            return super.eventOccurred(s, detector, increasing);
        }
    });
    ContinuousDetector detectorB = new ContinuousDetector(t2);
    EventDetector detectorC = new AbstractDetector<EventDetector>(maxCheck, tolerance, 100, new RecordAndContinue<>(events)) {

        private static final long serialVersionUID = 1L;

        @Override
        public double g(SpacecraftState state) throws OrekitException {
            if (swap[0]) {
                return detectorB.g(state);
            } else {
                return 1;
            }
        }

        @Override
        protected EventDetector create(double newMaxCheck, double newThreshold, int newMaxIter, EventHandler<? super EventDetector> newHandler) {
            return null;
        }
    };
    Propagator propagator = getPropagator(10);
    propagator.addEventDetector(detectorA);
    propagator.addEventDetector(detectorC);
    // action
    propagator.propagate(epoch.shiftedBy(-30.0));
    // verify
    Assert.assertEquals(2, 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(t2, events.get(1).getState().getDate().durationFrom(epoch), tolerance);
    Assert.assertEquals(true, events.get(1).isIncreasing());
    Assert.assertSame(detectorC, events.get(1).getDetector());
}
Also used : Action(org.orekit.propagation.events.handlers.EventHandler.Action) ArrayList(java.util.ArrayList) EventHandler(org.orekit.propagation.events.handlers.EventHandler) SpacecraftState(org.orekit.propagation.SpacecraftState) Propagator(org.orekit.propagation.Propagator) Event(org.orekit.propagation.events.handlers.RecordAndContinue.Event) StopOnEvent(org.orekit.propagation.events.handlers.StopOnEvent) 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