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

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

Introduction

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

Prototype

double getInterpolatedTime();

Source Link

Document

Get the time of the interpolated point.

Usage

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  ww  .j  a  v  a  2 s.co  m
            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);
        }
    });
}