Ejemplo n.º 1
0
int main () {
  try {
    PendulumSystem sys;
    sys.addEventHandler(new ZeroVelocityHandler(sys));
    sys.addEventHandler(PeriodicHandler::handler = new PeriodicHandler());
    sys.addEventHandler(new ZeroPositionHandler(sys));
    sys.addEventReporter(PeriodicReporter::reporter = new PeriodicReporter(sys));
    sys.addEventReporter(new OnceOnlyEventReporter());
    sys.addEventReporter(new DiscontinuousReporter());
    sys.realizeTopology();

    // Test with various intervals for the event handler and event reporter, ones that are either
    // large or small compared to the expected internal step size of the integrator.

    for (int i = 0; i < 4; ++i) {
        PeriodicHandler::handler->setEventInterval(i == 0 || i == 1 ? 0.01 : 2.0);
        PeriodicReporter::reporter->setEventInterval(i == 0 || i == 2 ? 0.015 : 1.5);
        
        // Test the integrator in both normal and single step modes.
        
        VerletIntegrator integ(sys);
        testIntegrator(integ, sys);
        integ.setReturnEveryInternalStep(true);
        testIntegrator(integ, sys);
    }
    cout << "Done" << endl;
    return 0;
  }
  catch (std::exception& e) {
    std::printf("FAILED: %s\n", e.what());
    return 1;
  }
}
Ejemplo n.º 2
0
int main () {
  try {
    PendulumSystem sys;
    sys.addEventHandler(new ZeroVelocityHandler(sys));
    sys.addEventHandler(new PeriodicHandler());
    sys.addEventReporter(new ZeroPositionReporter(sys));
    sys.addEventReporter(new PeriodicReporter());
    sys.realizeTopology();

    RungeKuttaMersonIntegrator integ(sys);

    const Real t0=0;
    const Real qi[] = {1,0}; // (x,y)=(1,0)
    const Real ui[] = {0,0}; // v=0
    const Vector q0(2, qi);
    const Vector u0(2, ui);

    sys.setDefaultMass(10);
    sys.setDefaultTimeAndState(t0, q0, u0);

    integ.setAccuracy(1e-2);
    integ.setConstraintTolerance(1e-4);

    const Real tFinal = 20.003;
    const Real hReport = 1.;

    integ.setFinalTime(tFinal);

    TimeStepper ts(sys);
    ts.setIntegrator(integ);
    ts.initialize(sys.getDefaultState());
    ASSERT(ts.getTime() == 0.0);
    ts.stepTo(10.0);
    ASSERT(ts.getTime() == 10.0);
    ts.stepTo(50.0);
    ASSERT(ts.getTime() == tFinal);
    ASSERT(integ.getTerminationReason() == Integrator::ReachedFinalTime);
    ASSERT(ZeroVelocityHandler::eventCount >= 10);
    ASSERT(PeriodicHandler::eventCount == (int) (ts.getTime()/1.5));
    ASSERT(ZeroPositionReporter::eventCount > 10);
    ASSERT(PeriodicReporter::eventCount == (int) (std::log(ts.getTime())/std::log(2.0))+1);
    cout << "Done" << endl;
    return 0;
  }
  catch (std::exception& e) {
    std::printf("FAILED: %s\n", e.what());
    return 1;
  }
}
Ejemplo n.º 3
0
int main () {
  try {
    PendulumSystem sys;
    sys.addEventHandler(new ZeroVelocityHandler(sys));
    sys.addEventHandler(PeriodicHandler::handler = new PeriodicHandler());
    sys.addEventHandler(new ZeroPositionHandler(sys));
    sys.addEventReporter(PeriodicReporter::reporter = new PeriodicReporter(sys));
    sys.addEventReporter(new OnceOnlyEventReporter());
    sys.addEventReporter(new DiscontinuousReporter());
    sys.realizeTopology();

    // Test with various intervals for the event handler and event reporter, ones that are either
    // large or small compared to the expected internal step size of the integrator.

    for (int i = 0; i < 4; ++i) {
        PeriodicHandler::handler->setEventInterval(i == 0 || i == 1 ? 0.01 : 2.0);
        PeriodicReporter::reporter->setEventInterval(i == 0 || i == 2 ? 0.015 : 1.5);
        
        // Test the BDF integrator in both normal and single step modes.
        
        CPodesIntegrator bdfInteg(sys, CPodes::BDF);
        testIntegrator(bdfInteg, sys);
        bdfInteg.setReturnEveryInternalStep(true);
        testIntegrator(bdfInteg, sys);
        
        // Test the Adams integrator in both normal and single step modes.
        
        CPodesIntegrator adamsInteg(sys, CPodes::Adams);
        testIntegrator(adamsInteg, sys);
        adamsInteg.setReturnEveryInternalStep(true);
        testIntegrator(adamsInteg, sys);
        
        // Calling setUseCPodesProjection() after the integrator has been initialized should
        // produce an exception.
        
        try {
            adamsInteg.setUseCPodesProjection();
            assert(false);
        }
        catch (...) {
        }
        
        // Try having CPODES do the projection instead of the System.
        
        CPodesIntegrator projInteg(sys, CPodes::BDF);
        projInteg.setUseCPodesProjection();
        testIntegrator(projInteg, sys);
    }
    cout << "Done" << endl;
    return 0;
  }
  catch (std::exception& e) {
    std::printf("FAILED: %s\n", e.what());
    return 1;
  }
}