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

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

Introduction

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

Prototype

double getCurrentTime();

Source Link

Document

Get the current grid point time.

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// www  .ja  v a  2s  . 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:de.uni_erlangen.lstm.modelaccess.Model.java

/**
 * Run the model using set parameters/*  ww w .  ja va2s. com*/
 */
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: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 ww  .j  a v a2  s . c  om
            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);
            }
        });
    }
}