Beispiel #1
0
def test_LagrangianLinearTIDS():
    ball = K.LagrangianLinearTIDS(q, v, mass)
    assert np.allclose(ball.q(), q, rtol=tol, atol=tol)
    assert np.allclose(ball.velocity(), v, rtol=tol, atol=tol)
    assert np.allclose(ball.mass(), mass, rtol=tol, atol=tol)
    ball.setFExtPtr(weight)
    assert np.allclose(ball.fExt(), weight, rtol=tol, atol=tol)
Beispiel #2
0
def test_lagrangian_tids():
    """Build and test lagrangian linear and time-invariant ds
    """
    q0[...] = [1, 2, 3]
    v0[...] = [4, 5, 6]

    mass = np.asarray(np.diag([1, 2, 3]), dtype=np.float64)
    stiffness = np.zeros((ndof, ndof), dtype=np.float64)
    stiffness.flat[...] = np.arange(9)
    damping = np.zeros_like(stiffness)
    damping.flat[...] = np.arange(9, 18)
    ds = sk.LagrangianLinearTIDS(q0, v0, mass, stiffness, damping)
    ec = ds.computeKineticEnergy()
    assert ec == 87.
    assert ds.dimension() == ndof
    assert np.allclose(ds.mass(), mass)
    assert np.allclose(ds.K(), stiffness)
    assert np.allclose(ds.C(), damping)
    q = ds.q()
    v = ds.velocity()
    fref = -np.dot(stiffness, q)
    fref -= np.dot(damping, v)
    time = 0.3
    ds.computeForces(time, q, v)
    assert np.allclose(fref, ds.forces())
    ds.computeJacobianqForces(time)
    assert np.allclose(stiffness, ds.jacobianqForces())
    ds.computeJacobianqDotForces(time)
    assert np.allclose(damping, ds.jacobianqDotForces())
Beispiel #3
0
def test_bouncing_ball4():
    """Run a complete simulation (Bouncing ball example)
    LagrangianDS,  plugged Fext.
    """

    t0 = 0  # start time
    r = 0.1  # ball radius
    g = 9.81  # gravity
    m = 1  # ball mass
    #
    # 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)

    stiffness = np.eye(3, dtype=np.float64)
    ball.setKPtr(stiffness)
    weight = np.zeros(ball.dimension())
    weight[0] = -m * g

    #ball.setFExtPtr(weight)

    # a ball with its own computeFExt
    class Ball(sk.LagrangianLinearTIDS):
        def __init__(self, x, v, mass, stiffness):
            sk.LagrangianLinearTIDS.__init__(self, x, v, mass)
            self.setKPtr(stiffness)

        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, stiffness)
    ball_d.computeFExt(t0)
    run_simulation_with_two_ds(ball, ball_d, t0)
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()
Beispiel #8
0
# From getfem, we have Mass, Stiffness and RHS
# saved in object sico.

# H-Matrix
fillH(pidbot, sico, mfu.nbdof(), BOTTOM)

# =======================================
# Create the siconos Dynamical System
#
# Mass.ddot q + Kq = fExt
#
# q: dof vector (displacements)
# =======================================
# Initial displacement and velocity
v0 = np.zeros(sico.nbdof)
block = kernel.LagrangianLinearTIDS(sico.initial_displacement, v0, sico.Mass)
# set fExt and K
block.setFExtPtr(sico.RHS)
block.setKPtr(sico.Stiff)

# =======================================
# The interactions
# A contact is defined for each node at
# the bottom of the block
# =======================================
# Create one relation/interaction for each point
# in the bottom surface
# Each interaction is of size three with a
# relation between local coordinates at contact and global coordinates given by:
# y = Hq + b
# y = [ normal component, first tangent component, second tangent component]
Beispiel #9
0
e = 0.9     # restitution coeficient
mu=0.3 # Friction coefficient
theta = 0.5 # theta scheme

with_friction = False

sico = gts.SiconosFem()
fem_model = gts.import_fem(sico)

# =======================================
# Create the siconos Dynamical System
# =======================================
# Initial position and velocity
v0 = np.zeros(sico.nbdof)

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)
Beispiel #10
0
mu = 0.3  # Friction coefficient
theta = 0.5  # theta scheme
m = 1

# =======================================
# Create the siconos Dynamical System
# =======================================
# Initial position and velocity
v0 = np.zeros(ndof)
q0 = np.zeros(ndof)
Mass = np.eye(ndof)
K = np.eye(ndof)
weight = np.zeros(ndof)
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)