Example usage for org.apache.commons.math3.analysis.solvers BracketingNthOrderBrentSolver BracketingNthOrderBrentSolver

List of usage examples for org.apache.commons.math3.analysis.solvers BracketingNthOrderBrentSolver BracketingNthOrderBrentSolver

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.solvers BracketingNthOrderBrentSolver BracketingNthOrderBrentSolver.

Prototype

public BracketingNthOrderBrentSolver(final double absoluteAccuracy, final int maximalOrder)
        throws NumberIsTooSmallException 

Source Link

Document

Construct a solver.

Usage

From source file:fr.cs.examples.bodies.Phasing.java

/**
 * Find the state at which the reference latitude is crossed.
 * @param latitude latitude to search for
 * @param guessDate guess date for the crossing
 * @param endDate maximal date not to overtake
 * @param shift shift value used to evaluate the latitude function bracketing around the guess date
 * @param maxShift maximum value that the shift value can take
 * @param propagator propagator used/*from  w  w  w.j  a va  2 s  . c  om*/
 * @return state at latitude crossing time
 * @throws OrekitException if state cannot be propagated
 * @throws NoBracketingException if latitude cannot be bracketed in the search interval
 */
private SpacecraftState findLatitudeCrossing(final double latitude, final AbsoluteDate guessDate,
        final AbsoluteDate endDate, final double shift, final double maxShift, final Propagator propagator)
        throws OrekitException, NoBracketingException {

    // function evaluating to 0 at latitude crossings
    final UnivariateFunction latitudeFunction = new UnivariateFunction() {
        /** {@inheritDoc} */
        public double value(double x) {
            try {
                final SpacecraftState state = propagator.propagate(guessDate.shiftedBy(x));
                final Vector3D position = state.getPVCoordinates(earth.getBodyFrame()).getPosition();
                final GeodeticPoint point = earth.transform(position, earth.getBodyFrame(), state.getDate());
                return point.getLatitude() - latitude;
            } catch (OrekitException oe) {
                throw new RuntimeException(oe);
            }
        }
    };

    // try to bracket the encounter
    double span;
    if (guessDate.shiftedBy(shift).compareTo(endDate) > 0) {
        // Take a 1e-3 security margin
        span = endDate.durationFrom(guessDate) - 1e-3;
    } else {
        span = shift;
    }

    while (!UnivariateSolverUtils.isBracketing(latitudeFunction, -span, span)) {

        if (2 * span > maxShift) {
            // let the Apache Commons Math exception be thrown
            UnivariateSolverUtils.verifyBracketing(latitudeFunction, -span, span);
        } else if (guessDate.shiftedBy(2 * span).compareTo(endDate) > 0) {
            // Out of range :
            return null;
        }

        // expand the search interval
        span *= 2;

    }

    // find the encounter in the bracketed interval
    final BaseUnivariateSolver<UnivariateFunction> solver = new BracketingNthOrderBrentSolver(0.1, 5);
    final double dt = solver.solve(1000, latitudeFunction, -span, span);
    return propagator.propagate(guessDate.shiftedBy(dt));

}

From source file:org.orekit.propagation.events.EventState.java

/** Evaluate the impact of the proposed step on the event detector.
 * @param interpolator step interpolator for the proposed step
 * @return true if the event detector triggers an event before
 * the end of the proposed step (this implies the step should be
 * rejected)//from  ww w. ja v  a2  s.c o m
 * @exception OrekitException if the switching function
 * cannot be evaluated
 * @exception TooManyEvaluationsException if an event cannot be located
 * @exception NoBracketingException if bracketing cannot be performed
 */
public boolean evaluateStep(final OrekitStepInterpolator interpolator)
        throws OrekitException, TooManyEvaluationsException, NoBracketingException {

    try {

        final double convergence = detector.getThreshold();
        final int maxIterationcount = detector.getMaxIterationCount();
        if (forward ^ interpolator.isForward()) {
            forward = !forward;
            pendingEvent = false;
            pendingEventTime = null;
            previousEventTime = null;
        }
        final AbsoluteDate t1 = interpolator.getCurrentDate();
        final double dt = t1.durationFrom(t0);
        if (FastMath.abs(dt) < convergence) {
            // we cannot do anything on such a small step, don't trigger any events
            return false;
        }
        final int n = FastMath.max(1, (int) FastMath.ceil(FastMath.abs(dt) / detector.getMaxCheckInterval()));
        final double h = dt / n;

        final UnivariateFunction f = new UnivariateFunction() {
            public double value(final double t) throws LocalWrapperException {
                try {
                    interpolator.setInterpolatedDate(t0.shiftedBy(t));
                    return g(interpolator.getInterpolatedState());
                } catch (OrekitException oe) {
                    throw new LocalWrapperException(oe);
                }
            }
        };

        final BracketingNthOrderBrentSolver solver = new BracketingNthOrderBrentSolver(convergence, 5);

        AbsoluteDate ta = t0;
        double ga = g0;
        for (int i = 0; i < n; ++i) {

            // evaluate detector value at the end of the substep
            final AbsoluteDate tb = t0.shiftedBy((i + 1) * h);
            interpolator.setInterpolatedDate(tb);
            final double gb = g(interpolator.getInterpolatedState());

            // check events occurrence
            if (g0Positive ^ (gb >= 0)) {
                // there is a sign change: an event is expected during this step

                // variation direction, with respect to the integration direction
                increasing = gb >= ga;

                // find the event time making sure we select a solution just at or past the exact root
                final double dtA = ta.durationFrom(t0);
                final double dtB = tb.durationFrom(t0);
                final double dtRoot = forward
                        ? solver.solve(maxIterationcount, f, dtA, dtB, AllowedSolution.RIGHT_SIDE)
                        : solver.solve(maxIterationcount, f, dtB, dtA, AllowedSolution.LEFT_SIDE);
                final AbsoluteDate root = t0.shiftedBy(dtRoot);

                if ((previousEventTime != null) && (FastMath.abs(root.durationFrom(ta)) <= convergence)
                        && (FastMath.abs(root.durationFrom(previousEventTime)) <= convergence)) {
                    // we have either found nothing or found (again ?) a past event,
                    // retry the substep excluding this value, and taking care to have the
                    // required sign in case the g function is noisy around its zero and
                    // crosses the axis several times
                    do {
                        ta = forward ? ta.shiftedBy(convergence) : ta.shiftedBy(-convergence);
                        ga = f.value(ta.durationFrom(t0));
                    } while ((g0Positive ^ (ga >= 0)) && (forward ^ (ta.compareTo(tb) >= 0)));

                    if (forward ^ (ta.compareTo(tb) >= 0)) {
                        // we were able to skip this spurious root
                        --i;
                    } else {
                        // we can't avoid this root before the end of the step,
                        // we have to handle it despite it is close to the former one
                        // maybe we have two very close roots
                        pendingEventTime = root;
                        pendingEvent = true;
                        return true;
                    }

                } else if ((previousEventTime == null)
                        || (FastMath.abs(previousEventTime.durationFrom(root)) > convergence)) {
                    pendingEventTime = root;
                    pendingEvent = true;
                    return true;
                } else {
                    // no sign change: there is no event for now
                    ta = tb;
                    ga = gb;
                }

            } else {
                // no sign change: there is no event for now
                ta = tb;
                ga = gb;
            }

        }

        // no event during the whole step
        pendingEvent = false;
        pendingEventTime = null;
        return false;

    } catch (LocalWrapperException lwe) {
        throw lwe.getWrappedException();
    }

}

From source file:org.orekit.utils.SecularAndHarmonicTest.java

private SpacecraftState findLatitudeCrossing(final double latitude, final AbsoluteDate guessDate,
        final AbsoluteDate endDate, final double shift, final double maxShift, final Propagator propagator)
        throws OrekitException, NoBracketingException {

    // function evaluating to 0 at latitude crossings
    final UnivariateFunction latitudeFunction = new UnivariateFunction() {
        /** {@inheritDoc} */
        public double value(double x) {
            try {
                final SpacecraftState state = propagator.propagate(guessDate.shiftedBy(x));
                final Vector3D position = state.getPVCoordinates(earth.getBodyFrame()).getPosition();
                final GeodeticPoint point = earth.transform(position, earth.getBodyFrame(), state.getDate());
                return point.getLatitude() - latitude;
            } catch (OrekitException oe) {
                throw new RuntimeException(oe);
            }/*from   www  . j  a  v  a2s. c  o  m*/
        }
    };

    // try to bracket the encounter
    double span;
    if (guessDate.shiftedBy(shift).compareTo(endDate) > 0) {
        // Take a 1e-3 security margin
        span = endDate.durationFrom(guessDate) - 1e-3;
    } else {
        span = shift;
    }

    while (!UnivariateSolverUtils.isBracketing(latitudeFunction, -span, span)) {

        if (2 * span > maxShift) {
            // let the Apache Commons Math exception be thrown
            UnivariateSolverUtils.verifyBracketing(latitudeFunction, -span, span);
        } else if (guessDate.shiftedBy(2 * span).compareTo(endDate) > 0) {
            // Out of range :
            return null;
        }

        // expand the search interval
        span *= 2;

    }

    // find the encounter in the bracketed interval
    final BaseUnivariateSolver<UnivariateFunction> solver = new BracketingNthOrderBrentSolver(0.1, 5);
    final double dt = solver.solve(1000, latitudeFunction, -span, span);
    return propagator.propagate(guessDate.shiftedBy(dt));

}