Exemple #1
0
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 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()
ball = sk.NewtonEulerDS(x, v, m, inertia)

# set external forces
weight = [-m * g, 0, 0]
ball.setFExtPtr(weight)

#
# Interactions
#

# ball-floor
e = 0.9  # restitution coeficient
nslaw = sk.NewtonImpactNSL(e)
relation = BouncingBallR(r)

inter = sk.Interaction(nslaw, relation)

#
# Model
#
t0 = 0  # start time
T = 10.0  # end time
bouncingBall = sk.NonSmoothDynamicalSystem(t0, T)

# add the dynamical system to the non smooth dynamical system
bouncingBall.insertDynamicalSystem(ball)

# link the interaction and the dynamical system
bouncingBall.link(inter, ball)

#
Exemple #7
0
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)
Exemple #8
0
#
# Interactions
#

C = [[-1., 0.]]

D = [[Rvalue]]

B = [[-1. / Cvalue], [0.]]

LTIRCircuitRLCD = sk.FirstOrderLinearTIR(C, B)
LTIRCircuitRLCD.setDPtr(D)

nslaw = sk.ComplementarityConditionNSL(1)
InterCircuitRLCD = sk.Interaction(nslaw, LTIRCircuitRLCD)

#
# Model
#
CircuitRLCD = sk.NonSmoothDynamicalSystem(t0, T)
CircuitRLCD.setTitle("CircuitRLCD")

#   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
A = np.zeros((2, 2))
x0 = np.array([Vinit, -Vinit])
B = 2.0 * np.eye(2)
C = np.eye(2)
D = np.zeros((2, 2))

process = sk.FirstOrderLinearDS(x0, A)
process.setComputebFunction('plugins', 'computeB')

# Interactions
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
Exemple #10
0
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)

# 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
# =======================================
#
# Interactions
#

C = [[0., 0.], [0, 0.], [-1., 0.], [1., 0.]]

D = [[1. / Rvalue, 1. / Rvalue, -1., 0.], [1. / Rvalue, 1. / Rvalue, 0., -1.],
     [1., 0., 0., 0.], [0., 1., 0., 0.]]

B = [[0., 0., -1. / Cvalue, 1. / Cvalue], [0., 0., 0., 0.]]

LTIRDiodeBridge = sk.FirstOrderLinearTIR(C, B)
LTIRDiodeBridge.setDPtr(D)

nslaw = sk.ComplementarityConditionNSL(4)
InterDiodeBridge = sk.Interaction(nslaw, LTIRDiodeBridge)

#
# Model
#
DiodeBridge = sk.Model(t0, T, Modeltitle)

#   add the dynamical system in the non smooth dynamical system
DiodeBridge.nonSmoothDynamicalSystem().insertDynamicalSystem(LSDiodeBridge)

#   link the interaction and the dynamical system
DiodeBridge.nonSmoothDynamicalSystem().link(InterDiodeBridge, LSDiodeBridge)

#
# Simulation
#
Exemple #12
0
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()
Exemple #13
0
B = np.zeros((ndof, ninter), dtype=np.float64)
B[0, 0] = -alpha * 0.5
B[0, 1] = 1 + alpha * 0.5
B[1, 0] = -B[0, 1]
B[1, 1] = B[0, 0]
B[2, 0] = gamma * 0.5
B[2, 1] = gamma * 0.5

C = np.zeros((ninter, ndof), dtype=np.float64)
C[0, 0] = C[1, 1] = -1.

particle_relation = sk.FirstOrderLinearR(C, B)

nslaw = sk.RelayNSL(ninter)

particle_interaction = sk.Interaction(nslaw, particle_relation)

# -- The Model --
filippov = sk.Model(t0, T)
nsds = filippov.nonSmoothDynamicalSystem()
nsds.insertDynamicalSystem(particle)
nsds.link(particle_interaction, particle)

# -- Simulation --
td = sk.TimeDiscretisation(t0, h)
simu = sk.TimeStepping(td)
# osi
theta = 0.5
myIntegrator = sk.EulerMoreauOSI(theta)
simu.insertIntegrator(myIntegrator)
Exemple #14
0
ninter = number_of_cars
B = np.zeros((ndof, ninter), dtype=np.float64)
np.fill_diagonal(B[number_of_cars:, :number_of_cars], -1. / D)

M = np.zeros((ninter, ninter), dtype=np.float64)
np.fill_diagonal(M[:number_of_cars, :number_of_cars], S)

N = np.zeros((ninter, ndof), dtype=np.float64)
np.fill_diagonal(N[:number_of_cars, number_of_cars:], -1.)

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)