Example usage for org.apache.commons.math3.ode.sampling StepInterpolator getInterpolatedState

List of usage examples for org.apache.commons.math3.ode.sampling StepInterpolator getInterpolatedState

Introduction

In this page you can find the example usage for org.apache.commons.math3.ode.sampling StepInterpolator getInterpolatedState.

Prototype

double[] getInterpolatedState() throws MaxCountExceededException;

Source Link

Document

Get the state vector of the interpolated point.

Usage

From source file:edu.gcsc.vrl.commons.math.ode.ODESolver.java

public Trajectory solve(@ParamInfo(name = "Label", options = "value=\"Label 1\"") String label,
        @ParamInfo(name = "x0", options = "value=0.0D") double t0,
        @ParamInfo(name = "xn", options = "value=3.0D") double tn,
        @ParamInfo(name = "y0", options = "value=0.0") double y0,
        @ParamInfo(name = "Min Step", options = "value=1e-6D") double minStep,
        @ParamInfo(name = "Max Step", options = "value=1e-2D") double maxStep,
        @ParamInfo(name = "Abs.Tol.", options = "value=1e-10") double absTol,
        @ParamInfo(name = "Rel.Tol.", options = "value=1e-10") double relTol,
        @ParamInfo(name = "RHS") FirstOrderDifferentialEquations rhs) {

    FirstOrderIntegrator integrator = new DormandPrince853Integrator(minStep, maxStep, absTol, relTol);

    final Trajectory result = new Trajectory(label);

    StepHandler stepHandler = new StepHandler() {
        @Override/*  w  w  w .j  av a  2  s.c o  m*/
        public void init(double t0, double[] y0, double t) {
            result.add(t, y0[0]);
        }

        @Override
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            double t = interpolator.getCurrentTime();
            double[] y = interpolator.getInterpolatedState();
            result.add(t, y[0]);
        }
    };

    integrator.addStepHandler(stepHandler);

    double[] y = new double[] { y0 }; // initial state
    integrator.integrate(rhs, t0, y, tn, y);

    return result;
}

From source file:nl.rivm.cib.episim.model.disease.infection.MSEIRSTest.java

public static Observable<Map.Entry<Double, double[]>> deterministic(final SIRConfig config,
        final Supplier<FirstOrderIntegrator> integrators) {
    return Observable.create(sub -> {
        final double gamma = 1. / config.recovery();
        final double beta = gamma * config.reproduction();
        final double[] y0 = Arrays.stream(config.population()).mapToDouble(n -> n).toArray();
        final double[] t = config.t();

        try {//from w  w w .java  2  s .  com
            final FirstOrderIntegrator integrator = integrators.get();

            integrator.addStepHandler(new StepHandler() {
                @Override
                public void init(final double t0, final double[] y0, final double t) {
                    publishCopy(sub, t0, y0);
                }

                @Override
                public void handleStep(final StepInterpolator interpolator, final boolean isLast)
                        throws MaxCountExceededException {
                    publishCopy(sub, interpolator.getInterpolatedTime(), interpolator.getInterpolatedState());
                    if (isLast)
                        sub.onComplete();
                }
            });

            integrator.integrate(new FirstOrderDifferentialEquations() {
                @Override
                public int getDimension() {
                    return y0.length;
                }

                @Override
                public void computeDerivatives(final double t, final double[] y, final double[] yp) {
                    // SIR terms (flow rates)
                    final double n = y[0] + y[1] + y[2], flow_si = beta * y[0] * y[1] / n,
                            flow_ir = gamma * y[1];

                    yp[0] = -flow_si;
                    yp[1] = flow_si - flow_ir;
                    yp[2] = flow_ir;
                }
            }, t[0], y0, t[1], y0);
        } catch (final Exception e) {
            sub.onError(e);
        }
    });
}

From source file:ummisco.gaml.extensions.maths.ode.utils.solver.Solver.java

Solver(final double step, final FirstOrderIntegrator integrator,
        final GamaMap<String, IList<Double>> integrated_val) {
    this.step = step;
    this.integrator = integrator;
    if (integrated_val != null) {
        integrator.addStepHandler(new StepHandler() {

            @Override/*from  w w w .j  ava2s.c  o  m*/
            public void init(final double t0, final double[] y0, final double t) {
            }

            @Override
            public void handleStep(final StepInterpolator interpolator, final boolean isLast) {
                final double time = interpolator.getCurrentTime();
                final double[] y = interpolator.getInterpolatedState();
                count++;
                storeValues(time, y, integrated_val);
            }
        });
    }
}