def __init__(self, string, position=None, restitution_coeff=1.): """" """ # siconos relation self.position = position[1] * npw.ones(1) self.contact_index = position[0] if string.modal_form: coeff = 1. # string.length / string._N if string.use_sparse: hmat = sk.SimpleMatrix(1, string.ndof, sk.SPARSE, string.ndof) for i in xrange(string.ndof): val = coeff * string.s_mat[self.contact_index, i] hmat.setValue(0, i, val) else: hmat = npw.zeros((1, string.ndof)) hmat[...] = string.s_mat[self.contact_index, :] hmat *= coeff else: hmat = npw.zeros((1, string.ndof)) hmat[0, self.contact_index] = 1. e = restitution_coeff nslaw = sk.NewtonImpactNSL(e) relation = sk.LagrangianLinearTIR(hmat, -self.position) super(Fret, self).__init__(nslaw, relation)
def __init__(self, string, contact_positions, restitution_coeff): """ Parameters ---------- string : :class:`StringDS` the dynamical system linked to this interaction contact_positons : tuple contact_positions[0] = horizontal index position of the contact point contact_positions[1] = vertical position of the fret restitution_coeff : double coefficient of restitution """ # vertical positions of the contact points hmat = npw.zeros((1, string.n_modes)) dx = string.space_step hmat[0, :] = string.s_mat[contact_positions[0], :] # compute contact point horizontal position self.contact_pos = dx * contact_positions[0] # set contact index (mind that boundary points # are not included in fret/ds) self.contact_index = contact_positions[0] # Build nslaw, relation and interaction e = restitution_coeff nslaw = sk.NewtonImpactNSL(e) dist = -contact_positions[1] relation = sk.LagrangianLinearTIR(hmat, [dist]) super(Fret, self).__init__(nslaw, relation)
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()
nslaw = kernel.NewtonImpactNSL(e) diminter = 1 hh = np.zeros((diminter, sico.nbdof)) b = np.zeros(diminter) b[0] = 1.0 k = 0 relation = [] inter = [] hh = np.zeros((diminter, sico.nbdof)) nbInter = pidbot.shape[0] for i in range(nbInter): # hh is a submatrix of sico.H with 1 row. hh[:, :] = sico.H[k, :] k += 3 relation.append(kernel.LagrangianLinearTIR(hh, b)) inter.append(kernel.Interaction(diminter, nslaw, relation[i])) nbInter = len(inter) # ======================================= # The Model # ======================================= blockModel = kernel.Model(t0, T) # 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)
block = kernel.LagrangianLinearTIDS(sico.pos,v0,sico.Mass.full()) F = sico.RHS + sico.K0 block.setFExtPtr(F) block.setKPtr(sico.Stiff.full()) # ======================================= # The interaction # ======================================= dist = 3.0 dimH = sico.H.shape[0] if(with_friction): diminter = dimH nslaw = kernel.NewtonImpactFrictionNSL(e,e,mu,3) relation = kernel.LagrangianLinearTIR(sico.H) else: diminter = dimH/3 Hnofric = np.zeros((diminter, sico.nbdof)) b = np.repeat([dist], diminter) Hnofric = sico.H[0:dimH-1:3,:] nslaw = kernel.NewtonImpactNSL(e) relation = kernel.LagrangianLinearTIR(Hnofric,b) inter = kernel.Interaction(diminter, nslaw, relation) # ======================================= # The Model # ======================================= blockModel = kernel.Model(t0,T)
def test_LagrangianLinearTIR(): H = np.array([[1, 0, 0]]) b = np.zeros(1) relation = K.LagrangianLinearTIR(H, b) assert np.allclose(relation.jachq(), H, rtol=tol, atol=tol)
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()
weight[0:ndof - 1:3] = -m * g block = kernel.LagrangianLinearTIDS(q0, v0, Mass) block.setFExtPtr(weight) block.setKPtr(K) diminter = 4 H = np.zeros((diminter, ndof)) H[0, 2] = 1.0 H[1, 5] = 1.0 H[2, 8] = 1.0 H[3, 11] = 1.0 dist = 3.0 b = np.repeat([dist], diminter) nslaw = kernel.NewtonImpactNSL(e) relation = kernel.LagrangianLinearTIR(H, b) inter = kernel.Interaction(diminter, nslaw, relation) # ======================================= # The Model # ======================================= blockModel = kernel.Model(t0, T) # add the dynamical system to the non smooth dynamical system blockModel.nonSmoothDynamicalSystem().insertDynamicalSystem(block) # link the interaction and the dynamical system blockModel.nonSmoothDynamicalSystem().link(inter, block) # ======================================= # The Simulation