def compute_dt_matrices(A, B, h, TV=False): # variable declaration t0 = 0.0 # start time T = 1 # end time n, m = B.shape # Matrix declaration x0 = np.random.random(n) Csurface = np.random.random((m, n)) # Declaration of the Dynamical System if TV: processDS = SK.FirstOrderLinearDS(x0, A) else: processDS = SK.FirstOrderLinearTIDS(x0, A) # Model process = SK.Model(t0, T) process.nonSmoothDynamicalSystem().insertDynamicalSystem(processDS) # time discretisation processTD = SK.TimeDiscretisation(t0, h) # Creation of the Simulation processSimulation = SK.TimeStepping(processTD, 0) processSimulation.setName("plant simulation") # Declaration of the integrator processIntegrator = SK.ZeroOrderHoldOSI(processDS) processSimulation.insertIntegrator(processIntegrator) rel = SK.FirstOrderLinearTIR(Csurface, B) nslaw = SK.RelayNSL(m) inter = SK.Interaction(m, nslaw, rel) # process.nonSmoothDynamicalSystem().insertInteraction(inter, True) process.nonSmoothDynamicalSystem().link(inter, processDS) process.nonSmoothDynamicalSystem().setControlProperty(inter, True) # Initialization process.initialize(processSimulation) # Main loop processSimulation.computeOneStep() Ad = SK.getMatrix(processIntegrator.Ad(processDS)).copy() Bd = SK.getMatrix(processIntegrator.Bd(processDS)).copy() return (Ad, Bd)
def test_xml1(): ''' the BouncingBall ''' bouncingBall = buildModelXML('./data/BBallTS.xml') # --- Get the simulation --- s = bouncingBall.simulation() dsN = SK.dynamicalSystems(bouncingBall.nonSmoothDynamicalSystem().topology().dSG(0))[0].number() ball = bouncingBall.nonSmoothDynamicalSystem().dynamicalSystem(dsN) N = 2000 # Number of time steps # saved in a matrix dataPlot outputSize = 4 dataPlot = np.zeros((N + 1, outputSize)) q = ball.q() v = ball.velocity() p = ball.p(1) dataPlot[0, 0] = bouncingBall.t0() dataPlot[0, 1] = q[0] dataPlot[0, 2] = v[0] dataPlot[0, 3] = p[0] print("====> Start computation ...") # --- Time loop --- k = 1 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] s.nextStep() k += 1 print("End of computation - Number of iterations done: {:}".format(k)) print("====> Output file writing ...") dataPlot.resize(k, outputSize) np.savetxt("BBallTS.dat", dataPlot) # Comparison with a reference file dataPlotRef = SK.getMatrix(SK.SimpleMatrix('./data/BBallTSXML.ref')) if np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf) > 1e-12: print(dataPlot - dataPlotRef) print("ERROR: The result is rather different from the reference file.")
def test_xml5(): ''' Bouncing Ball ED ''' # --- buildModelXML loading from xml file --- bouncingBall = buildModelXML("./data/BBallED.xml") # --- Get and initialize the simulation --- s = bouncingBall.simulation() dsN = SK.dynamicalSystems(bouncingBall.nonSmoothDynamicalSystem().topology().dSG(0))[0].number() ball = bouncingBall.nonSmoothDynamicalSystem().dynamicalSystem(dsN) # --- Get the values to be plotted --- # . saved in a matrix dataPlot N = 12368 # Number of saved points: depends on the number of events ... outputSize = 5 dataPlot = np.zeros((N + 1, outputSize)) q = ball.q() v = ball.velocity() p = ball.p(1) f = ball.p(2) dataPlot[0, 0] = bouncingBall.t0() dataPlot[0, 1] = q[0] dataPlot[0, 2] = v[0] dataPlot[0, 3] = p[0] dataPlot[0, 4] = f[0] print("====> Start computation ... ") # --- Time loop --- eventsManager = s.eventsManager() numberOfEvent = 0 k = 0 nonSmooth = False while s.hasNextEvent(): k += 1 s.advanceToEvent() if eventsManager.nextEvent().getType() == 2: nonSmooth = True s.processEvents() # If the treated event is non smooth, the pre-impact state has been solved in memory vectors during process. if nonSmooth: dataPlot[k, 0] = s.startingTime() dataPlot[k, 1] = ball.qMemory().getSiconosVector(1)[0] dataPlot[k, 2] = ball.velocityMemory().getSiconosVector(1)[0] k += 1 nonSmooth = False dataPlot[k, 0] = s.startingTime() dataPlot[k, 1] = q[0] dataPlot[k, 2] = v[0] dataPlot[k, 3] = p[0] dataPlot[k, 4] = f[0] numberOfEvent += 1 # --- Output files --- dataPlot.resize(k, outputSize) np.savetxt("BBallED.dat", dataPlot) # Comparison with a reference file dataPlotRef = SK.getMatrix(SK.SimpleMatrix("./data/BouncingBallEDXml.ref")) if np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf) > 1e-11: print("Warning. The results is rather different from the reference file.") print(np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf)) exit(1)