void Simulation::initialize(SP::Model m, bool withOSI) { // === Connection with the model === assert(m && "Simulation::initialize(model) - model = NULL."); _model = std11::weak_ptr<Model>(m); _T = m->finalT(); // === Events manager initialization === _eventsManager->initialize(_T); _tinit = _eventsManager->startingTime(); //=== if (withOSI) { if (numberOfOSI() == 0) RuntimeException::selfThrow("Simulation::initialize No OSI !"); // === OneStepIntegrators initialization === for (OSIIterator itosi = _allOSI->begin(); itosi != _allOSI->end(); ++itosi) { for (DSIterator itds = (*itosi)->dynamicalSystems()->begin(); itds != (*itosi)->dynamicalSystems()->end(); ++itds) { (*itds)->initialize(model()->t0(), (*itosi)->getSizeMem()); addInOSIMap(*itds, *itosi); } (*itosi)->setSimulationPtr(shared_from_this()); (*itosi)->initialize(); } } // This is the default _levelMinForInput = LEVELMAX; _levelMaxForInput = 0; _levelMinForOutput = LEVELMAX; _levelMaxForOutput = 0; computeLevelsForInputAndOutput(); // Loop over all DS in the graph, to reset NS part of each DS. // Note FP : this was formerly done in inter->initialize call with local levels values // but I think it's ok (better?) to do it with the simulation levels values. DynamicalSystemsGraph::VIterator dsi, dsend; SP::DynamicalSystemsGraph DSG = model()->nonSmoothDynamicalSystem()->topology()->dSG(0); for (std11::tie(dsi, dsend) = DSG->vertices(); dsi != dsend; ++dsi) { //assert(_levelMinForInput <= _levelMaxForInput); for (unsigned int k = _levelMinForInput ; k < _levelMaxForInput + 1; k++) { DSG->bundle(*dsi)->initializeNonSmoothInput(k); } } InteractionsGraph::VIterator ui, uiend; SP::InteractionsGraph indexSet0 = model()->nonSmoothDynamicalSystem()->topology()->indexSet0(); for (std11::tie(ui, uiend) = indexSet0->vertices(); ui != uiend; ++ui) { Interaction& inter = *indexSet0->bundle(*ui); inter.initialize(_tinit, indexSet0->properties(*ui)); } // Initialize OneStepNSProblem(s). Depends on the type of simulation. // Warning FP : must be done in any case, even if the interactions set // is empty. initOSNS(); // Process events at time _tinit. Useful to save values in memories // for example. Warning: can not be called during // eventsManager->initialize, because it needs the initialization of // OSI, OSNS ... _eventsManager->preUpdate(*this); _tend = _eventsManager->nextTime(); // Set Model current time (warning: current time of the model // corresponds to the time of the next event to be treated). model()->setCurrentTime(nextTime()); // End of initialize: // - all OSI and OSNS (ie DS and Interactions) states are computed // - for time _tinit and saved into memories. // - Sensors or related objects are updated for t=_tinit. // - current time of the model is equal to t1, time of the first // - event after _tinit. // - currentEvent of the simu. corresponds to _tinit and nextEvent // - to _tend. // If _printStat is true, open output file. if (_printStat) { statOut.open("simulationStat.dat", std::ios::out | std::ios::trunc); if (!statOut.is_open()) SiconosVectorException::selfThrow("writing error : Fail to open file simulationStat.dat "); statOut << "============================================" <<std::endl; statOut << " Siconos Simulation of type " << Type::name(*this) << "." <<std::endl; statOut <<std::endl; statOut << "The tolerance parameter is equal to: " << _tolerance <<std::endl; statOut <<std::endl <<std::endl; } }
void KernelTest::t6() { SP::Model bouncingBall = Siconos::load("BouncingBall1.xml"); try { double T = bouncingBall->finalT(); double t0 = bouncingBall->t0(); double h = bouncingBall->simulation()->timeStep(); int N = (int)((T - t0) / h); // Number of time steps SP::DynamicalSystemsGraph dsg = bouncingBall->nonSmoothDynamicalSystem()->topology()->dSG(0); SP::LagrangianDS ball = std11::static_pointer_cast<LagrangianDS> (dsg->bundle(*(dsg->begin()))); SP::TimeStepping s = std11::static_pointer_cast<TimeStepping>(bouncingBall->simulation()); SP::Interaction inter; InteractionsGraph::VIterator ui, uiend; SP::InteractionsGraph indexSet0 = bouncingBall->nonSmoothDynamicalSystem()->topology()->indexSet(0); for (std11::tie(ui, uiend) = indexSet0->vertices(); ui != uiend; ++ui) inter = indexSet0->bundle(*ui); // --- Get the values to be plotted --- // -> saved in a matrix dataPlot unsigned int outputSize = 5; SimpleMatrix dataPlot(N + 1, outputSize); SP::SiconosVector q = ball->q(); SP::SiconosVector v = ball->velocity(); SP::SiconosVector p = ball->p(1); SP::SiconosVector lambda = inter->lambda(1); dataPlot(0, 0) = bouncingBall->t0(); dataPlot(0, 1) = (*q)(0); dataPlot(0, 2) = (*v)(0); dataPlot(0, 3) = (*p)(0); dataPlot(0, 4) = (*lambda)(0); // --- Time loop --- cout << "====> Start computation ... " << endl << endl; // ==== Simulation loop - Writing without explicit event handling ===== int k = 1; boost::progress_display show_progress(N); boost::timer time; time.restart(); while (s->hasNextEvent()) { s->computeOneStep(); // --- Get values to be plotted --- dataPlot(k, 0) = s->nextTime(); dataPlot(k, 1) = (*q)(0); dataPlot(k, 2) = (*v)(0); dataPlot(k, 3) = (*p)(0); dataPlot(k, 4) = (*lambda)(0); s->nextStep(); ++show_progress; k++; } cout << endl << "End of computation - Number of iterations done: " << k - 1 << endl; cout << "Computation Time " << time.elapsed() << endl; // --- Output files --- cout << "====> Output file writing ..." << endl; dataPlot.resize(k, outputSize); ioMatrix::write("result.dat", "ascii", dataPlot, "noDim"); // Comparison with a reference file SimpleMatrix dataPlotRef(dataPlot); dataPlotRef.zero(); ioMatrix::read("result.ref", "ascii", dataPlotRef); if ((dataPlot - dataPlotRef).normInf() > 1e-12) { std::cout << "Warning. The results is rather different from the reference file :" << (dataPlot - dataPlotRef).normInf() << std::endl; CPPUNIT_ASSERT(false); } } catch (SiconosException e) { cout << e.report() << endl; CPPUNIT_ASSERT(false); } catch (...) { cout << "Exception caught in BouncingBallTS.cpp" << endl; CPPUNIT_ASSERT(false); } }