Exemplo n.º 1
0
bool ode_solver::contain_NaN(C0Rect2Set const & s) {
    return contain_NaN(IVector(s));
}
Exemplo n.º 2
0
ode_solver::ODE_result ode_solver::compute_forward(vector<pair<interval, IVector>> & bucket) {
    ODE_result ret = ODE_result::SAT;
    auto start = high_resolution_clock::now();
    bool invariantViolated = false;

    try {
        // Set up VectorField
        IMap vectorField(m_diff_sys_forward);
        for (Enode * par : m_pars) {
            double lb = get_lb(par);
            double ub = get_ub(par);
            string name = par->getCar()->getName();
            vectorField.setParameter(name, interval(lb, ub));
        }
        ITaylor solver(vectorField, m_config.nra_ODE_taylor_order, .001);
        ITimeMap timeMap(solver);
        C0Rect2Set s(m_X_0);
        timeMap.stopAfterStep(true);
        timeMap.turnOnStepControl();

        // TODO(soonhok): visualization
        if (m_config.nra_json) {
            m_trajectory.clear();
            m_trajectory.emplace_back(timeMap.getCurrentTime(), IVector(s));
        }

        interval prevTime(0.);
        do {
            // Handle Timeout
            if (m_config.nra_ODE_timeout > 0.0) {
                auto end = high_resolution_clock::now();
                if (duration_cast<milliseconds>(end - start).count() >= m_config.nra_ODE_timeout) {
                    return ODE_result::TIMEOUT;
                }
            }

            // Check Invariant
            invariantViolated = !check_invariant(s, m_inv);
            if (invariantViolated) {
                // TODO(soonhok): invariant
                if (timeMap.getCurrentTime().rightBound() < m_T.leftBound()) {
                    ret = ODE_result::UNSAT;
                } else {
                    ret = ODE_result::SAT;
                }
                break;
            }

            // Control TimeStep
            timeMap.turnOnStepControl();
            if (m_stepControl > 0 && solver.getStep() < m_stepControl) {
                timeMap.turnOffStepControl();
                solver.setStep(m_stepControl);
                timeMap.setStep(m_stepControl);
            }

            // Move s toward m_T.rightBound()
            timeMap(m_T.rightBound(), s);
            if (contain_NaN(s)) { return ODE_result::SAT; }
            if (m_T.leftBound() <= timeMap.getCurrentTime().rightBound()) {
                invariantViolated = inner_loop_forward(solver, prevTime, bucket);
                if (invariantViolated) {
                    // TODO(soonhok): invariant
                    ret = ODE_result::SAT;
                    break;
                }
            } else {
                if (m_config.nra_json) {
                    interval const stepMade = solver.getStep();
                    const ITaylor::CurveType& curve = solver.getCurve();
                    interval domain = interval(0, 1) * stepMade;
                    list<interval> intvs;
                    intvs = split(domain, m_config.nra_ODE_grid_size);
                    for (interval subsetOfDomain : intvs) {
                        interval dt = prevTime + subsetOfDomain;
                        IVector v = curve(subsetOfDomain);
                        m_trajectory.emplace_back(dt, v);
                    }
                }
            }
            prevTime = timeMap.getCurrentTime();
        } while (!invariantViolated && !timeMap.completed());
    } catch (exception& e) {
        ret = ODE_result::EXCEPTION;
    }
    if (m_config.nra_json) {
        prune_trajectory(m_T, m_X_t);
    }
    return ret;
}