Example usage for org.apache.commons.math3.ode.sampling StepHandler StepHandler

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

Introduction

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

Prototype

StepHandler

Source Link

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  ww  . j  a  va 2s . com*/
        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:de.uni_erlangen.lstm.modelaccess.Model.java

/**
 * Run the model using set parameters/* w  ww .  j  av  a2s  .co m*/
 */
public void simulate() {
    finished = false;
    /*
     * Integrator selection 
     */
    //FirstOrderIntegrator integrator = new HighamHall54Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
    //FirstOrderIntegrator integrator = new DormandPrince54Integrator(1.0e-12, 100.0, 1.0e-12, 1.0e-12);
    //FirstOrderIntegrator integrator = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
    //FirstOrderIntegrator integrator = new GraggBulirschStoerIntegrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
    FirstOrderIntegrator integrator = new AdamsBashforthIntegrator(2, 1.0e-14, 100.0, 1.0e-10, 1.0e-10);
    //FirstOrderIntegrator integrator = new AdamsMoultonIntegrator(2, 1.0e-8, 100.0, 1.0e-10, 1.0e-10);

    // influent values, digester parameters, S_H_ion, dae system
    final DAEModel ode = new DAEModel(u, param, S_H_ion, dae, fix_pH);
    //FirstOrderDifferentialEquations ode = model; 

    // Records progress
    StepHandler progHandler = new StepHandler() {
        public void init(double t0, double[] y0, double t) {
        }

        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            progress = interpolator.getCurrentTime();
        }
    };
    integrator.addStepHandler(progHandler);

    /*
     * Continuous model recorded in CSV
     */
    if (onlineRecord) {
        final CSVWriter writer = new CSVWriter();
        StepHandler stepHandler = new StepHandler() {
            double prevT = 0.0;

            public void init(double t0, double[] y0, double t) {
            }

            public void handleStep(StepInterpolator interpolator, boolean isLast) {
                double t = interpolator.getCurrentTime();
                if (t - prevT > resolution) {
                    // Add time to the beginning of the array
                    double[] timemodel = new double[ode.getDimensions().length + 1];
                    timemodel[0] = t;

                    // We need to pull variables (S_h2 and acid-base) directly from the model if using DAE
                    for (int i = 1; i < timemodel.length; i++) {
                        timemodel[i] = ode.getDimensions()[i - 1];
                    }

                    writer.WriteArray(output_file, timemodel, true);
                    prevT = t;
                }
            }
        };
        integrator.addStepHandler(stepHandler);
    }

    /*
     * Add event handlers for discrete events
     * maxCheck - maximal time interval between switching function checks (this interval prevents missing sign changes in case the integration steps becomes very large)
      * conv - convergence threshold in the event time search
      * maxIt - upper limit of the iteration count in the event time search
     */
    if (events.size() > 0) {
        for (DiscreteEvent event : events) {
            double maxCheck = Double.POSITIVE_INFINITY;
            double conv = 1.0e-20;
            int maxIt = 100;
            integrator.addEventHandler(event, maxCheck, conv, maxIt);
        }
    }

    integrator.integrate(ode, start, x, end, x);

    /*
     * Return the time that the discrete event occurred
     */
    if (events.size() > 0) {
        for (DiscreteEvent event : events) {
            if (event.getTime() < end) {
                end = event.getTime();
            }
        }
    }

    // We need to pull variables (S_h2 and acid-base) directly from the model
    x = ode.getDimensions();

    finished = true;
}

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 {//  w  ww . j a  v a2s . 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/* w  w w .j av  a  2s  .  com*/
            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);
            }
        });
    }
}