void ZOHTest::testMatrixIntegration1() { std::cout << "===========================================" <<std::endl; std::cout << " ===== ZOH tests start ... ===== " <<std::endl; std::cout << "===========================================" <<std::endl; std::cout << "------- Integrate an oscillator -------" <<std::endl; _A->zero(); (*_A)(0, 1) = 1; (*_A)(1, 0) = -1; _x0->zero(); (*_x0)(0) = 1; init(); SimpleMatrix dataPlot((unsigned)ceil((_T - _t0) / _h) + 10, 3); SiconosVector& xProc = *_DS->x(); unsigned int k = 0; dataPlot(0, 0) = _t0; dataPlot(0, 1) = (*_x0)(0); dataPlot(0, 2) = (*_x0)(1); while (_sim->hasNextEvent()) { _sim->computeOneStep(); k++; dataPlot(k, 0) = _sim->nextTime(); dataPlot(k, 1) = xProc(0); dataPlot(k, 2) = xProc(1); _sim->nextStep(); _sim->eventsManager()->display(); } dataPlot.display(); std::cout <<std::endl <<std::endl; dataPlot.resize(k, 3); ioMatrix::write("testMatrixIntegration1.dat", "ascii", dataPlot, "noDim"); // Reference Matrix SimpleMatrix dataPlotRef(dataPlot); dataPlotRef.zero(); //magic line to compute the following: // python -c "import numpy as np; t = np.linspace(0, 9.9, 100); np.savetxt('testMatrixIntegration1.ref', np.transpose([t, np.cos(t), -np.sin(t)]))" && sed -i "1i100 3" testMatrixIntegration1.ref ioMatrix::read("testMatrixIntegration1.ref", "ascii", dataPlotRef); std::cout << "------- Integration Ok, error = " << (dataPlot - dataPlotRef).normInf() << " -------" <<std::endl; CPPUNIT_ASSERT_EQUAL_MESSAGE("testMatrixExp1 : ", (dataPlot - dataPlotRef).normInf() < _tol, true); }
void ZOHTest::testMatrixIntegration4() { std::cout << "===========================================" <<std::endl; std::cout << " ===== ZOH tests start ... ===== " <<std::endl; std::cout << "===========================================" <<std::endl; std::cout << "------- Integrate Orlov's controller -------" <<std::endl; _h = .001; _A->zero(); (*_A)(0, 1) = 1; _x0->zero(); (*_x0)(0) = 1; (*_x0)(1) = 1; SP::SimpleMatrix B(new SimpleMatrix(_n, _n, 0)); SP::SimpleMatrix C(new SimpleMatrix(_n, _n)); (*B)(1, 0) = 2; (*B)(1, 1) = 1; C->eye(); SP::FirstOrderLinearTIR rel(new FirstOrderLinearTIR(C, B)); SP::SimpleMatrix D(new SimpleMatrix(_n, _n, 0)); rel->setDPtr(D); SP::NonSmoothLaw nslaw(new RelayNSL(_n)); _DS.reset(new FirstOrderLinearDS(_x0, _A, _b)); _TD.reset(new TimeDiscretisation(_t0, _h)); _model.reset(new Model(_t0, _T)); SP::Interaction inter(new Interaction(_n, nslaw, rel)); _ZOH.reset(new ZeroOrderHoldOSI()); _model->nonSmoothDynamicalSystem()->insertDynamicalSystem(_DS); _model->nonSmoothDynamicalSystem()->topology()->setOSI(_DS, _ZOH); _model->nonSmoothDynamicalSystem()->link(inter, _DS); _model->nonSmoothDynamicalSystem()->setControlProperty(inter, true); _sim.reset(new TimeStepping(_TD, 1)); _sim->insertIntegrator(_ZOH); SP::Relay osnspb(new Relay()); _sim->insertNonSmoothProblem(osnspb); _model->setSimulation(_sim); _model->initialize(); SimpleMatrix dataPlot((unsigned)ceil((_T - _t0) / _h) + 10, 7); SiconosVector& xProc = *_DS->x(); SiconosVector& lambda = *inter->lambda(0); SiconosVector sampledControl(_n); unsigned int k = 0; dataPlot(0, 0) = _t0; dataPlot(0, 1) = (*_x0)(0); dataPlot(0, 2) = (*_x0)(1); dataPlot(0, 3) = 0; dataPlot(0, 4) = 0; dataPlot(0, 5) = 0; dataPlot(0, 6) = 0; while (_sim->hasNextEvent()) { _sim->computeOneStep(); prod(*B, lambda, sampledControl, true); k++; dataPlot(k, 0) = _sim->nextTime(); dataPlot(k, 1) = xProc(0); dataPlot(k, 2) = xProc(1); dataPlot(k, 3) = sampledControl(0); dataPlot(k, 4) = sampledControl(1); dataPlot(k, 5) = lambda(0); dataPlot(k, 6) = lambda(1); _sim->nextStep(); } dataPlot.resize(k, 7); dataPlot.display(); std::cout <<std::endl <<std::endl; ioMatrix::write("testMatrixIntegration4.dat", "ascii", dataPlot, "noDim"); // Reference Matrix SimpleMatrix dataPlotRef(dataPlot); dataPlotRef.zero(); ioMatrix::read("testMatrixIntegration4.ref", "ascii", dataPlotRef); std::cout << "------- Integration Ok, error = " << (dataPlot - dataPlotRef).normInf() << " -------" <<std::endl; CPPUNIT_ASSERT_EQUAL_MESSAGE("testMatrixExp4 : ", (dataPlot - dataPlotRef).normInf() < _tol, true); }
void OSNSPTest::testAVI() { std::cout << "===========================================" <<std::endl; std::cout << " ===== OSNSP tests start ... ===== " <<std::endl; std::cout << "===========================================" <<std::endl; std::cout << "------- implicit Twisting relation -------" <<std::endl; _h = 1e-1; _T = 20.0; double G = 10.0; double beta = .3; _A->zero(); (*_A)(0, 1) = 1.0; _x0->zero(); (*_x0)(0) = 10.0; (*_x0)(1) = 10.0; SP::SimpleMatrix B(new SimpleMatrix(_n, _n, 0)); SP::SimpleMatrix C(new SimpleMatrix(_n, _n)); (*B)(1, 0) = G; (*B)(1, 1) = G*beta; C->eye(); SP::FirstOrderLinearTIR rel(new FirstOrderLinearTIR(C, B)); SP::SimpleMatrix H(new SimpleMatrix(4, 2)); (*H)(0, 0) = 1.0; (*H)(1, 0) = -_h/2.0; (*H)(2, 0) = -1.0; (*H)(3, 0) = _h/2.0; (*H)(1, 1) = 1.0; (*H)(3, 1) = -1.0; SP::SiconosVector K(new SiconosVector(4)); (*K)(0) = -1.0; (*K)(1) = -1.0; (*K)(2) = -1.0; (*K)(3) = -1.0; SP::NonSmoothLaw nslaw(new NormalConeNSL(_n, H, K)); _DS.reset(new FirstOrderLinearTIDS(_x0, _A, _b)); _TD.reset(new TimeDiscretisation(_t0, _h)); _model.reset(new Model(_t0, _T)); SP::Interaction inter(new Interaction(_n, nslaw, rel)); _osi.reset(new EulerMoreauOSI(_theta)); _model->nonSmoothDynamicalSystem()->insertDynamicalSystem(_DS); _model->nonSmoothDynamicalSystem()->setOSI(_DS, _osi); _model->nonSmoothDynamicalSystem()->link(inter, _DS); _sim.reset(new TimeStepping(_TD)); _sim->insertIntegrator(_osi); SP::AVI osnspb(new AVI()); _sim->insertNonSmoothProblem(osnspb); _model->initialize(_sim); SimpleMatrix dataPlot((unsigned)ceil((_T - _t0) / _h) + 10, 5); SiconosVector& xProc = *_DS->x(); SiconosVector& lambda = *inter->lambda(0); unsigned int k = 0; dataPlot(0, 0) = _t0; dataPlot(0, 1) = (*_x0)(0); dataPlot(0, 2) = (*_x0)(1); dataPlot(0, 3) = -1.0; dataPlot(0, 4) = -1.0; while (_sim->hasNextEvent()) { _sim->computeOneStep(); k++; dataPlot(k, 0) = _sim->nextTime(); dataPlot(k, 1) = xProc(0); dataPlot(k, 2) = xProc(1); dataPlot(k, 3) = lambda(0); dataPlot(k, 4) = lambda(1); _sim->nextStep(); } std::cout <<std::endl <<std::endl; dataPlot.resize(k, dataPlot.size(1)); ioMatrix::write("testAVI.dat", "ascii", dataPlot, "noDim"); // Reference Matrix SimpleMatrix dataPlotRef(dataPlot); dataPlotRef.zero(); ioMatrix::read("testAVI.ref", "ascii", dataPlotRef); std::cout << "------- Integration Ok, error = " << (dataPlot - dataPlotRef).normInf() << " -------" <<std::endl; CPPUNIT_ASSERT_EQUAL_MESSAGE("testAVI : ", (dataPlot - dataPlotRef).normInf() < _tol, true); }