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: process_ds = SK.FirstOrderLinearDS(x0, A) else: process_ds = SK.FirstOrderLinearTIDS(x0, A) # Model process = SK.Model(t0, T) process.nonSmoothDynamicalSystem().insertDynamicalSystem(process_ds) # time discretisation process_time_discretisation = SK.TimeDiscretisation(t0, h) # Creation of the Simulation process_simu = SK.TimeStepping(process_time_discretisation, 0) process_simu.setName("plant simulation") # Declaration of the integrator process_integrator = SK.ZeroOrderHoldOSI() process_integrator.insertDynamicalSystem(process_ds) process_simu.insertIntegrator(process_integrator) rel = SK.FirstOrderLinearTIR(Csurface, B) nslaw = SK.RelayNSL(m) inter = SK.Interaction(m, nslaw, rel) #process.nonSmoothDynamicalSystem().insertInteraction(inter, True) process.nonSmoothDynamicalSystem().link(inter, process_ds) process.nonSmoothDynamicalSystem().setControlProperty(inter, True) # Initialization process.initialize(process_simu) # Main loop process_simu.computeOneStep() Ad = SK.getMatrix(process_integrator.Ad(process_ds)).copy() Bd = SK.getMatrix(process_integrator.Bd(process_ds)).copy() return (Ad, Bd)
def __init__(self, strings, frets, time_range, fs): """ """ super(Guitar, self).__init__(time_range[0], time_range[1]) if not isinstance(strings, list): strings = [strings] if not isinstance(frets, list): frets = [frets] self.modal_form = strings[0].modal_form self.frets = frets self.strings = strings for string in strings: self.nonSmoothDynamicalSystem().insertDynamicalSystem(string) # link the interaction(s) and the dynamical system(s) for fret in frets: self.nonSmoothDynamicalSystem().link(fret, string) # -- Simulation -- # (1) OneStepIntegrators theta = 0.5001 osi = sk.MoreauJeanOSI(theta) t0 = time_range[0] tend = time_range[1] # (2) Time discretisation -- self.fs = fs # 1960 self.time_step = 1. / fs t = sk.TimeDiscretisation(t0, self.time_step) self.nb_time_steps = (int)((tend - t0) / self.time_step) # (3) one step non smooth problem osnspb = sk.LCP() # (4) Simulation setup with (1) (2) (3) self.simu = sk.TimeStepping(t, osi, osnspb) # simulation initialization self.setSimulation(self.simu) self.initialize() ndof = strings[0].ndof self.fret_position = frets[0].contact_index self.data = npw.zeros((self.nb_time_steps + 1, 3 + ndof))
def test_bouncing_ball1(): """Run a complete simulation (Bouncing ball example) LagrangianLinearTIDS, no plugins. """ t0 = 0. # start time tend = 10. # end time h = 0.005 # time step r = 0.1 # ball radius g = 9.81 # gravity m = 1 # ball mass e = 0.9 # restitution coeficient theta = 0.5 # theta scheme # # dynamical system # x = np.zeros(3, dtype=np.float64) x[0] = 1. v = np.zeros_like(x) # mass matrix mass = np.eye(3, dtype=np.float64) mass[2, 2] = 3. / 5 * r * r # the dynamical system ball = sk.LagrangianLinearTIDS(x, v, mass) # set external forces weight = np.zeros_like(x) weight[0] = -m * g ball.setFExtPtr(weight) # # Interactions # # ball-floor H = np.zeros((1, 3), dtype=np.float64) H[0, 0] = 1. nslaw = sk.NewtonImpactNSL(e) relation = sk.LagrangianLinearTIR(H) inter = sk.Interaction(nslaw, relation) # # NSDS # bouncing_ball = sk.NonSmoothDynamicalSystem(t0, tend) # add the dynamical system to the non smooth dynamical system bouncing_ball.insertDynamicalSystem(ball) # link the interaction and the dynamical system bouncing_ball.link(inter, ball) # # Simulation # # (1) OneStepIntegrators OSI = sk.MoreauJeanOSI(theta) # (2) Time discretisation -- t = sk.TimeDiscretisation(t0, h) # (3) one step non smooth problem osnspb = sk.LCP() # (4) Simulation setup with (1) (2) (3) s = sk.TimeStepping(bouncing_ball,t, OSI, osnspb) # end of model definition # # computation # # # save and load data from xml and .dat # try: from siconos.io import save save(bouncing_ball, "bouncingBall.xml") save(bouncing_ball, "bouncingBall.bin") except: print("Warning : could not import save from siconos.io") # the number of time steps nb_time_steps = int((tend - t0) / h + 1) # Get the values to be plotted # ->saved in a matrix dataPlot data = np.empty((nb_time_steps, 5)) # # numpy pointers on dense Siconos vectors # q = ball.q() v = ball.velocity() p = ball.p(1) lambda_ = inter.lambda_(1) # # initial data # data[0, 0] = t0 data[0, 1] = q[0] data[0, 2] = v[0] data[0, 3] = p[0] data[0, 4] = lambda_[0] k = 1 # time loop while(s.hasNextEvent()): s.computeOneStep() data[k, 0] = s.nextTime() data[k, 1] = q[0] data[k, 2] = v[0] data[k, 3] = p[0] data[k, 4] = lambda_[0] k += 1 #print(s.nextTime()) s.nextStep() # # comparison with the reference file # ref = sk.getMatrix(sk.SimpleMatrix( os.path.join(working_dir, "data/result.ref"))) assert (np.linalg.norm(data - ref) < 1e-12)
def test_bouncing_ball2(): """Run a complete simulation (Bouncing ball example) LagrangianLinearTIDS, plugged Fext. """ t0 = 0 # start time T = 5 # end time h = 0.005 # time step r = 0.1 # ball radius g = 9.81 # gravity m = 1 # ball mass e = 0.9 # restitution coeficient theta = 0.5 # theta scheme # # dynamical system # x = np.zeros(3, dtype=np.float64) x[0] = 1. v = np.zeros_like(x) # mass matrix mass = np.eye(3, dtype=np.float64) mass[2, 2] = 3. / 5 * r * r # the dynamical system ball = sk.LagrangianLinearTIDS(x, v, mass) weight = np.zeros(ball.dimension()) weight[0] = -m * g ball.setFExtPtr(weight) # a ball with its own computeFExt class Ball(sk.LagrangianLinearTIDS): def computeFExt(self, t): """External forces operator computation """ print("computing FExt at t=", t) #self._fExt[0] = -m * g weight = np.zeros(self.dimension()) weight[0] = -m * g self.setFExtPtr(weight) ball_d = Ball(x.copy(), v.copy(), mass) ball_d.computeFExt(t0) # Interactions # # ball-floor H = np.zeros((1, 3), dtype=np.float64) H[0, 0] = 1. nslaw = sk.NewtonImpactNSL(e) nslaw_d = sk.NewtonImpactNSL(e) relation = sk.LagrangianLinearTIR(H) relation_d = sk.LagrangianLinearTIR(H) inter = sk.Interaction(nslaw, relation) inter_d = sk.Interaction(nslaw_d, relation_d) # # NSDS # bouncing_ball = sk.NonSmoothDynamicalSystem(t0, T) bouncing_ball_d = sk.NonSmoothDynamicalSystem(t0, T) # add the dynamical system to the non smooth dynamical system bouncing_ball.insertDynamicalSystem(ball) bouncing_ball_d.insertDynamicalSystem(ball_d) # link the interaction and the dynamical system bouncing_ball.link(inter, ball) bouncing_ball_d.link(inter_d, ball_d) # # Simulation # # (1) OneStepIntegrators OSI = sk.MoreauJeanOSI(theta) OSI_d = sk.MoreauJeanOSI(theta) # (2) Time discretisation -- t = sk.TimeDiscretisation(t0, h) t_d = sk.TimeDiscretisation(t0, h) # (3) one step non smooth problem osnspb = sk.LCP() osnspb_d = sk.LCP() # (4) Simulation setup with (1) (2) (3) s = sk.TimeStepping(bouncing_ball,t, OSI, osnspb) s_d = sk.TimeStepping(bouncing_ball_d,t_d, OSI_d, osnspb_d) # end of model definition # # computation # # the number of time steps nb_time_steps = int((T - t0) / h + 1) # Get the values to be plotted # ->saved in a matrix data s_d.computeOneStep() data = np.empty((nb_time_steps + 1, 5)) data_d = np.empty((nb_time_steps + 1, 5)) data[0, 0] = t0 data[0, 1] = ball.q()[0] data[0, 2] = ball.velocity()[0] data[0, 3] = ball.p(1)[0] data[0, 4] = inter.lambda_(1) data_d[0, 0] = t0 data_d[0, 1] = ball_d.q()[0] data_d[0, 2] = ball_d.velocity()[0] data_d[0, 3] = ball_d.p(1)[0] data_d[0, 4] = inter_d.lambda_(1) k = 1 # time loop while(s.hasNextEvent()): s.computeOneStep() s_d.computeOneStep() data[k, 0] = s.nextTime() data[k, 1] = ball.q()[0] data[k, 2] = ball.velocity()[0] data[k, 3] = ball.p(1)[0] data[k, 4] = inter.lambda_(1)[0] data_d[k, 0] = s_d.nextTime() data_d[k, 1] = ball_d.q()[0] data_d[k, 2] = ball_d.velocity()[0] data_d[k, 3] = ball_d.p(1)[0] data_d[k, 4] = inter_d.lambda_(1)[0] assert np.allclose(data[k, 1], data_d[k, 1]) #print(s.nextTime()) k += 1 s.nextStep() s_d.nextStep()
def test_lagrangian_and_osis(): """Tests osi/lagrangian combinations """ # --- The dynamical systems --- # dimension ndof = 3 # initial conditons q0 = np.zeros(ndof, dtype=np.float64) v0 = np.zeros_like(q0) q0[0] = 1. # mass, stiffness and damping matrices # (diagonal values only) mass_diag = np.ones(ndof, dtype=np.float64) k_diag = np.zeros_like(mass_diag) c_diag = np.zeros_like(mass_diag) sigma = 0.2 omega2 = 1.2 k_diag[...] = omega2 c_diag[...] = sigma ds_list = {} # - LagrangianLinearTIDS - ds_list['LTIDS+MJ'] = sk.LagrangianLinearTIDS(q0, v0, np.diag(mass_diag), np.diag(k_diag), np.diag(c_diag)) # - LagrangianLinearDiagonalDS - ds_list['LLDDS+MJ'] = sk.LagrangianLinearDiagonalDS( q0, v0, k_diag, c_diag, mass_diag) ds_list['LLDDS+MJB'] = sk.LagrangianLinearDiagonalDS( q0, v0, k_diag, c_diag, mass_diag) # no mass (i.e. implicitely equal to Id) ds_list['LLDDS+MJB2'] = sk.LagrangianLinearDiagonalDS( q0, v0, k_diag, c_diag) nb_ds = len(ds_list) for ds in ds_list.values(): ds.computeForces(0., q0, v0) assert np.allclose(ds_list['LTIDS+MJ'].forces(), ds_list['LTIDS+MJ'].forces()) # --- Interactions --- cor = 0.9 nslaw = sk.NewtonImpactNSL(cor) hmat = np.zeros((1, ndof), dtype=np.float64) hmat[0, 0] = 1. relation = sk.LagrangianLinearTIR(hmat) interactions = [] for k in range(nb_ds): interactions.append(sk.Interaction(nslaw, relation)) # --- NSDS --- tinit = 0. tend = 3. nsds = sk.NonSmoothDynamicalSystem(tinit, tend) # - insert ds into the model and link them with their interaction - ninter = 0 for ds in ds_list.values(): nsds.insertDynamicalSystem(ds) nsds.link(interactions[ninter], ds) ninter += 1 # --- Simulation --- # - (1) OneStepIntegrators -- theta = 0.5000001 standard = sk.MoreauJeanOSI(theta) bilbao = sk.MoreauJeanBilbaoOSI() #-- (2) Time discretisation -- time_step = 1e-4 td = sk.TimeDiscretisation(tinit, time_step) # -- (3) one step non smooth problem lcp = sk.LCP() # -- (4) Simulation setup with (1) (2) (3) simu = sk.TimeStepping(nsds, td, standard, lcp) # extra osi must be explicitely inserted into simu and linked to ds simu.associate(standard, ds_list['LTIDS+MJ']) simu.associate(standard, ds_list['LLDDS+MJ']) simu.associate(bilbao, ds_list['LLDDS+MJB']) simu.associate(bilbao, ds_list['LLDDS+MJB2']) positions = [] velocities = [] for ds in ds_list.values(): positions.append(ds.q()) velocities.append(ds.velocity()) # number of time steps and buffer used to save results nb_steps = int((tend - tinit) / time_step) + 1 data = np.zeros((nb_steps, 1 + 2 * len(ds_list)), dtype=np.float64) data[0, 0] = tinit for k in range(nb_ds): data[0, k * 2 + 1] = positions[k][0] data[0, k * 2 + 2] = velocities[k][0] current_iter = 1 # --- Time loop --- while simu.hasNextEvent(): simu.computeOneStep() data[current_iter, 0] = simu.nextTime() for k in range(nb_ds): data[current_iter, k * 2 + 1] = positions[k][0] data[current_iter, k * 2 + 2] = velocities[k][0] simu.nextStep() current_iter += 1 # -- check results ref_pos = data[:, 1] ref_vel = data[:, 2] for k in range(1, nb_ds): assert np.allclose(ref_pos, data[:, k * 2 + 1], atol=time_step) assert np.allclose(ref_vel, data[:, k * 2 + 2], atol=time_step)
def test_bouncing_ball2(): import siconos.kernel as K from numpy import array, eye, empty t0 = 0 # start time T = 5 # end time h = 0.005 # time step r = 0.1 # ball radius g = 9.81 # gravity m = 1 # ball mass e = 0.9 # restitution coeficient theta = 0.5 # theta scheme # # dynamical system # x = array([1, 0, 0]) # initial position v = array([0, 0, 0]) # initial velocity mass = eye(3) # mass matrix mass[2, 2] = 3./5 * r * r # the dynamical system ball = K.LagrangianLinearTIDS(x, v, mass) # set external forces weight = array([-m * g, 0, 0]) ball.setFExtPtr(weight) # a ball with its own computeFExt class Ball(K.LagrangianLinearTIDS): def computeFExt(self, t): #print("computing FExt at t=", t) weight = array([-m * g, 0, 0]) self.setFExtPtr(weight) ball_d = Ball(array([1, 0, 0]), array([0, 0, 0]), mass) ball_d.setFExtPtr(array([0, 0, 0])) # # Interactions # # ball-floor H = array([[1, 0, 0]]) nslaw = K.NewtonImpactNSL(e) nslaw_d = K.NewtonImpactNSL(e) relation = K.LagrangianLinearTIR(H) relation_d = K.LagrangianLinearTIR(H) inter = K.Interaction(1, nslaw, relation) inter_d = K.Interaction(1, nslaw_d, relation_d) # # Model # bouncingBall = K.Model(t0, T) bouncingBall_d = K.Model(t0, T) # add the dynamical system to the non smooth dynamical system bouncingBall.nonSmoothDynamicalSystem().insertDynamicalSystem(ball) bouncingBall_d.nonSmoothDynamicalSystem().insertDynamicalSystem(ball_d) # link the interaction and the dynamical system bouncingBall.nonSmoothDynamicalSystem().link(inter, ball) bouncingBall_d.nonSmoothDynamicalSystem().link(inter_d, ball_d) # # Simulation # # (1) OneStepIntegrators OSI = K.MoreauJeanOSI(theta) OSI.insertDynamicalSystem(ball) OSI_d = K.MoreauJeanOSI(theta) OSI_d.insertDynamicalSystem(ball_d) # (2) Time discretisation -- t = K.TimeDiscretisation(t0, h) t_d = K.TimeDiscretisation(t0, h) # (3) one step non smooth problem osnspb = K.LCP() osnspb_d = K.LCP() # (4) Simulation setup with (1) (2) (3) s = K.TimeStepping(t) s.insertIntegrator(OSI) s.insertNonSmoothProblem(osnspb) s_d = K.TimeStepping(t_d) s_d.insertIntegrator(OSI_d) s_d.insertNonSmoothProblem(osnspb_d) # end of model definition # # computation # # simulation initialization bouncingBall.initialize(s) bouncingBall_d.initialize(s_d) # the number of time steps N = (T-t0)/h+1 # Get the values to be plotted # ->saved in a matrix dataPlot s_d.computeOneStep() dataPlot = empty((N+1, 5)) dataPlot_d = empty((N+1, 5)) dataPlot[0, 0] = t0 dataPlot[0, 1] = ball.q()[0] dataPlot[0, 2] = ball.velocity()[0] dataPlot[0, 3] = ball.p(1)[0] dataPlot[0, 4] = inter.lambda_(1) dataPlot_d[0, 0] = t0 dataPlot_d[0, 1] = ball_d.q()[0] dataPlot_d[0, 2] = ball_d.velocity()[0] dataPlot_d[0, 3] = ball_d.p(1)[0] dataPlot_d[0, 4] = inter_d.lambda_(1) k = 1 # time loop while(s.hasNextEvent()): s.computeOneStep() s_d.computeOneStep() dataPlot[k, 0] = s.nextTime() dataPlot[k, 1] = ball.q()[0] dataPlot[k, 2] = ball.velocity()[0] dataPlot[k, 3] = ball.p(1)[0] dataPlot[k, 4] = inter.lambda_(1)[0] dataPlot_d[k, 0] = s_d.nextTime() dataPlot_d[k, 1] = ball_d.q()[0] dataPlot_d[k, 2] = ball_d.velocity()[0] dataPlot_d[k, 3] = ball_d.p(1)[0] dataPlot_d[k, 4] = inter_d.lambda_(1)[0] assert dataPlot[k, 1] == dataPlot_d[k, 1] #print(s.nextTime()) k += 1 s.nextStep() s_d.nextStep()
A = zeros((2, 2)) A[0, 1] = 1 B = [[0], [1]] x0 = [10., 10.] C = [[1., 0]] # we have to specify ndmin=2, so it's understood as K = [.25, .125, 2] # Declaration of the Dynamical System doubleIntegrator = sk.FirstOrderLinearTIDS(x0, A) # Model process = sk.Model(t0, T) process.nonSmoothDynamicalSystem().insertDynamicalSystem(doubleIntegrator) # Declaration of the integrator OSI = sk.EulerMoreauOSI(theta) # time discretisation t = sk.TimeDiscretisation(t0, h) tSensor = sk.TimeDiscretisation(t0, h) tActuator = sk.TimeDiscretisation(t0, h) s = sk.TimeStepping(t, 0) s.insertIntegrator(OSI) # Actuator, Sensor & ControlManager control = ControlManager(s) sens = LinearSensor(doubleIntegrator, C) control.addSensorPtr(sens, tSensor) act = PID(sens) act.setB(B) control.addActuatorPtr(act, tActuator) # Initialization process.setSimulation(s)
# add the dynamical system to the non smooth dynamical system blockModel.nonSmoothDynamicalSystem().insertDynamicalSystem(block) # link the interactions and the dynamical system for i in range(nbInter): blockModel.nonSmoothDynamicalSystem().link(inter[i], block) # ======================================= # The Simulation # ======================================= # (1) OneStepIntegrators OSI = kernel.Moreau(theta) # (2) Time discretisation -- t = kernel.TimeDiscretisation(t0, h) osnspb = kernel.LCP() # (4) Simulation setup with (1) (2) (3) s = kernel.TimeStepping(t) s.insertIntegrator(OSI) s.insertNonSmoothProblem(osnspb) blockModel.setSimulation(s) # simulation initialization blockModel.initialize() # the number of time steps N = (T - t0) / h
bouncingBall.insertDynamicalSystem(ball) # link the interaction and the dynamical system bouncingBall.link(inter, ball) # # Simulation # h = 0.005 # time step theta = 0.5 # theta scheme # (1) OneStepIntegrators OSI = sk.MoreauJeanOSI(theta) # (2) Time discretisation -- t = sk.TimeDiscretisation(t0, h) # (3) one step non smooth problem osnspb = sk.LCP() # (4) Simulation setup with (1) (2) (3) s = sk.TimeStepping(bouncingBall, t, OSI, osnspb) s.setNewtonTolerance(1e-10) s.setNewtonMaxIteration(10) # end of model definition # # computation #
myNslaw = sk.RelayNSL(2) myProcessRelation = sk.FirstOrderLinearR(C, B) myProcessRelation.setComputeEFunction('plugins', 'computeE') # myProcessRelation.setDPtr(D) myProcessInteraction = sk.Interaction(myNslaw, myProcessRelation) # NSDS simplerelay = sk.NonSmoothDynamicalSystem(t0, T) simplerelay.insertDynamicalSystem(process) simplerelay.link(myProcessInteraction, process) # myProcessRelation.computeJachx(0, x0, x0 , x0, C) # Simulation s = sk.TimeStepping(simplerelay, sk.TimeDiscretisation(t0, h), sk.EulerMoreauOSI(theta), sk.Relay()) # s.setComputeResiduY(True) # s.setComputeResiduR(True) # matrix to save data dataPlot = np.empty((N + 1, 7)) k = 0 dataPlot[k, 0] = t0 dataPlot[k, 1:3] = process.x() dataPlot[k, 3] = myProcessInteraction.lambda_(0)[0] dataPlot[k, 4] = myProcessInteraction.lambda_(0)[1] dataPlot[k, 5] = myProcessInteraction.y(0)[0] dataPlot[k, 6] = myProcessInteraction.y(0)[1] # time loop k = 1
# add the dynamical system in the non smooth dynamical system CircuitRLCD.insertDynamicalSystem(LSCircuitRLCD) # link the interaction and the dynamical system CircuitRLCD.link(InterCircuitRLCD, LSCircuitRLCD) # # Simulation # # (1) OneStepIntegrators theta = 0.500000001 aOSI = sk.EulerMoreauOSI(theta) # (2) Time discretisation aTiDisc = sk.TimeDiscretisation(t0, h_step) # (3) Non smooth problem aLCP = sk.LCP() # (4) Simulation setup with (1) (2) (3) aTS = sk.TimeStepping(CircuitRLCD, aTiDisc, aOSI, aLCP) # end of model definition # # computation # k = 0 h = aTS.timeStep()
def run_simulation_with_two_ds(ball, ball_d, t0): T = 5 # end time h = 0.005 # time step e = 0.9 # restitution coeficient theta = 0.5 # theta scheme # ball-floor H = np.zeros((1, 3), dtype=np.float64) H[0, 0] = 1. nslaw = sk.NewtonImpactNSL(e) nslaw_d = sk.NewtonImpactNSL(e) relation = sk.LagrangianLinearTIR(H) relation_d = sk.LagrangianLinearTIR(H) inter = sk.Interaction(nslaw, relation) inter_d = sk.Interaction(nslaw_d, relation_d) # # NSDS # bouncing_ball = sk.NonSmoothDynamicalSystem(t0, T) bouncing_ball_d = sk.NonSmoothDynamicalSystem(t0, T) # add the dynamical system to the non smooth dynamical system bouncing_ball.insertDynamicalSystem(ball) bouncing_ball_d.insertDynamicalSystem(ball_d) # link the interaction and the dynamical system bouncing_ball.link(inter, ball) bouncing_ball_d.link(inter_d, ball_d) # # Simulation # # (1) OneStepIntegrators OSI = sk.MoreauJeanOSI(theta) OSI_d = sk.MoreauJeanOSI(theta) # (2) Time discretisation -- t = sk.TimeDiscretisation(t0, h) t_d = sk.TimeDiscretisation(t0, h) # (3) one step non smooth problem osnspb = sk.LCP() osnspb_d = sk.LCP() # (4) Simulation setup with (1) (2) (3) s = sk.TimeStepping(bouncing_ball, t, OSI, osnspb) s_d = sk.TimeStepping(bouncing_ball_d, t_d, OSI_d, osnspb_d) # end of model definition # # computation # # the number of time steps nb_time_steps = int((T - t0) / h + 1) # Get the values to be plotted # ->saved in a matrix data s_d.computeOneStep() data = np.empty((nb_time_steps + 1, 5)) data_d = np.empty((nb_time_steps + 1, 5)) data[0, 0] = t0 data[0, 1] = ball.q()[0] data[0, 2] = ball.velocity()[0] data[0, 3] = ball.p(1)[0] data[0, 4] = inter.lambda_(1) data_d[0, 0] = t0 data_d[0, 1] = ball_d.q()[0] data_d[0, 2] = ball_d.velocity()[0] data_d[0, 3] = ball_d.p(1)[0] data_d[0, 4] = inter_d.lambda_(1) k = 1 # time loop while (s.hasNextEvent()): s.computeOneStep() s_d.computeOneStep() data[k, 0] = s.nextTime() data[k, 1] = ball.q()[0] data[k, 2] = ball.velocity()[0] data[k, 3] = ball.p(1)[0] data[k, 4] = inter.lambda_(1)[0] data_d[k, 0] = s_d.nextTime() data_d[k, 1] = ball_d.q()[0] data_d[k, 2] = ball_d.velocity()[0] data_d[k, 3] = ball_d.p(1)[0] data_d[k, 4] = inter_d.lambda_(1)[0] assert np.allclose(data[k, 1], data_d[k, 1]) #print(s.nextTime()) k += 1 s.nextStep() s_d.nextStep() data.resize(k, 5) view = False if view: import matplotlib.pyplot as plt fig_size = [14, 14] plt.rcParams["figure.figsize"] = fig_size plt.subplot(411) plt.title('displacement') plt.plot(data[:, 0], data[:, 1]) plt.grid() plt.subplot(412) plt.title('velocity') plt.plot(data[:, 0], data[:, 2]) plt.grid() plt.subplot(413) plt.plot(data[:, 0], data[:, 3]) plt.title('reaction') plt.grid() plt.subplot(414) plt.plot(data[:, 0], data[:, 4]) plt.title('lambda') plt.grid() plt.show()
def __init__(self, strings_and_frets, time_range, fs, output_freq=1, interactions_output=0, integrators=None, default_integrator=None): """ Parameters ---------- strings_and_frets : python dictionnary keys = interactions (Fret). See notes below. values = dynamical systems (StringDS) time_range : list time range for the simulation. fs : double sampling frequency integrators : dictionnary, optional integrators[some_ds] = osi class name (MoreauJeanOSI or MoreauJeanBilbaoOSI). Default: Bilbao for all ds. default_integrator: sk.OneStepIntegrator default osi type (if integrators is not set or for ds not present in integrators). Default = Bilbao. output_freq: int, optional output frequency for times steps interactions_output: int, optional 0: save nothing, 1: only y, 2: y + lambda(vel) 3: y + ydot + lambda default = 0 Notes ----- * strings_and_frets.keys() : interactions * strings_and_frets.values() : dynamical systems * so, strings_and_frets[some_interaction] returns the ds linked to some_interaction. * For integrators: - to use Bilbao for all ds : do not set integrators and default_integrator params - to choose the integrator: set default_integrator=OSI OSI being some siconos OneStepIntegrator class. - to explicitely set the integrator for each ds: set integrators dictionnary, with integrators[some_ds] = OSI (OneStepIntegrator class) all ds not set in integrators will be integrated with default_integrator. """ # iterate through ds and interactions in the input dictionnary # - insert ds into the nonsmooth dynamical system # - link each ds to its interaction self.nsds = sk.NonSmoothDynamicalSystem(time_range[0], time_range[1]) if None in strings_and_frets: # No interactions in the model ds = strings_and_frets[None] assert isinstance(ds, StringDS) self.nsds.insertDynamicalSystem(ds) else: #self.nsds.insertDynamicalSystem(list(strings_and_frets.values())[0]) for interaction in strings_and_frets: ds = strings_and_frets[interaction] assert isinstance(interaction, Fret) assert isinstance(ds, StringDS) self.nsds.insertDynamicalSystem(ds) # link the interaction and the dynamical system self.nsds.link(interaction, ds) self.strings_and_frets = strings_and_frets # -- Simulation -- moreau_bilbao = sk.MoreauJeanBilbaoOSI() moreau_jean = sk.MoreauJeanOSI(0.500001) default_integrator = moreau_bilbao if default_integrator == 'MoreauJean': default_integrator = moreau_jean # (2) Time discretisation -- t0 = time_range[0] tend = time_range[1] self.fs = fs self.time_step = 1. / fs t = sk.TimeDiscretisation(t0, self.time_step) self.nb_time_steps = (int)((tend - t0) * fs) self.output_freq = output_freq self.nb_time_steps_output = (int)(self.nb_time_steps / output_freq) # (3) one step non smooth problem self.osnspb = sk.LCP() # (4) Simulation setup with (1) (2) (3) self.default_integrator = default_integrator self.simulation = sk.TimeStepping(self.nsds, t, default_integrator, self.osnspb) #self.simulation = sk.TimeStepping(self.nsds, t, moreau_bilbao, self.osnspb) if integrators is not None: for ds in integrators: self.simulation.associate(integrators[ds], ds) # internal buffers, used to save data for each time step. # A dict of buffers to save ds variables for all time steps self.data_ds = {} for ds in self.strings_and_frets.values(): ndof = ds.dimension() self.data_ds[ds] = npw.zeros((ndof, self.nb_time_steps_output + 1)) if None in self.strings_and_frets: self.strings_and_frets = {} # A dict of buffers to save interactions variables for all time steps self.data_interactions = {} self.save_interactions = interactions_output > 0 self.interactions_output = interactions_output if self.save_interactions: for interaction in self.strings_and_frets: self.data_interactions[interaction] = [] for i in range(interactions_output): self.data_interactions[interaction].append( npw.zeros(self.nb_time_steps_output + 1)) # time instants self.time = npw.zeros(self.nb_time_steps_output + 1) # saved data state (modal or nodal) self.modal_values = True
relation = sk.FirstOrderLinearTIR(N, B) relation.setDPtr(M) nslaw = sk.ComplementarityConditionNSL(ninter) interaction = sk.Interaction(ninter, nslaw, relation) # -- The Model -- circuit = sk.Model(t0, T, 'train') nsds = circuit.nonSmoothDynamicalSystem() nsds.insertDynamicalSystem(RC) nsds.link(interaction, RC) # -- Simulation -- td = sk.TimeDiscretisation(t0, tau) simu = sk.TimeStepping(td) # osi theta = 0.50000000000001 osi = sk.EulerMoreauOSI(theta) osi.insertDynamicalSystem(RC) simu.insertIntegrator(osi) # osns osnspb = sk.LCP() simu.insertNonSmoothProblem(osnspb) circuit.initialize(simu) # -- Get the values to be plotted -- output_size = 2 * number_of_cars + 1
def __init__(self, strings_and_frets, time_range, fs, integrators=None, default_integrator=None): """ Parameters ---------- strings_and_frets : python dictionnary keys = interactions (Fret). See notes below. values = dynamical systems (StringDS) time_range : list time range for the simulation. fs : double sampling frequency integrators : dictionnary, optional integrators[some_ds] = osi class name (MoreauJeanOSI or MoreauJeanBilbaoOSI). Default: Bilbao for all ds. default_integrator: sk.OneStepIntegrator default osi type (if integrators is not set or for ds not present in integrators). Default = Bilbao. Notes ----- * strings_and_frets.keys() : interactions * strings_and_frets.values() : dynamical systems * so, strings_and_frets[some_interaction] returns the ds linked to some_interaction. * For integrators: - to use Bilbao for all ds : do not set integrators and default_integrator params - to choose the integrator: set default_integrator=OSI OSI being some siconos OneStepIntegrator class. - to explicitely set the integrator for each ds: set integrators dictionnary, with integrators[some_ds] = OSI (OneStepIntegrator class) all ds not set in integrators will be integrated with default_integrator. """ # Build siconos model object (input = time range) super(Guitar, self).__init__(time_range[0], time_range[1]) # iterate through ds and interactions in the input dictionnary # - insert ds into the nonsmooth dynamical system # - link each ds to its interaction for interaction in strings_and_frets: ds = strings_and_frets[interaction] assert isinstance(interaction, Fret) assert isinstance(ds, StringDS) self.insertDynamicalSystem(ds) # link the interaction and the dynamical system self.link(interaction, ds) self.strings_and_frets = strings_and_frets # -- Simulation -- moreau_bilbao = sk.MoreauJeanBilbaoOSI() moreau_jean = sk.MoreauJeanOSI(0.500001) default_integrator = moreau_bilbao if default_integrator is 'MoreauJean': default_integrator = moreau_jean # (2) Time discretisation -- t0 = time_range[0] tend = time_range[1] self.fs = fs self.time_step = 1. / fs t = sk.TimeDiscretisation(t0, self.time_step) self.nb_time_steps = (int)((tend - t0) / self.time_step) + 1 # (3) one step non smooth problem osnspb = sk.LCP() # (4) Simulation setup with (1) (2) (3) self.simu = sk.TimeStepping(self, t, default_integrator, osnspb) if integrators is not None: for ds in integrators: self.simu.associate(integrators[ds], ds) # internal buffers, used to save data for each time step. # A dict of buffers to save ds variables for all time steps self.data_ds = {} for ds in self.strings_and_frets.values(): ndof = ds.dimension() self.data_ds[ds] = npw.zeros((self.nb_time_steps + 1, ndof)) # A dict of buffers to save interactions variables for all time steps self.data_interactions = {} for interaction in self.strings_and_frets: nbc = interaction.getSizeOfY() self.data_interactions[interaction] = \ npw.zeros((self.nb_time_steps + 1, 3 * nbc)) # time instants self.time = npw.zeros(self.nb_time_steps + 1)