Exemplo n.º 1
0
def test_xml3():
    ''' DryFriction '''
    # --- buildModelXML loading from xml file ---
    oscillator = buildModelXML("./data/DryFriction.xml")

    # --- Get the simulation ---
    s = oscillator.simulation()
    k = 0
    T = oscillator.finalT()
    t0 = oscillator.t0()
    h = s.timeStep()
    N = np.ceil((T - t0) / h)
    # --- Get the values to be plotted ---
    # . saved in a matrix dataPlot
    dataPlot = np.zeros((N + 1, 5))

    print("Prepare data for plotting ... ")
    # For the initial time step:
    # time
    dataPlot[k, 0] = t0
    # state q for the first dynamical system (ball)
    dsN = SK.dynamicalSystems(oscillator.nonSmoothDynamicalSystem().topology().dSG(0))[0].number()
    oscillo = oscillator.nonSmoothDynamicalSystem().dynamicalSystem(dsN)
    inter = SK.interactions(oscillator.nonSmoothDynamicalSystem().topology().indexSet(0))[0]

    dataPlot[k, 1] = oscillo.q()[0]
    # velocity for the oscillo
    dataPlot[k, 2] = oscillo.velocity()[0]
    dataPlot[k, 3] = inter.lambda_(1)[0]
    dataPlot[k, 4] = inter.lambda_(1)[1]

    # --- Compute elapsed time ---
    print("Computation ... ")
    # --- Time loop  ---
    while k < N:
        # get current time step
        k += 1
        #  print( " Pas " << k
        # solve ...
        s.computeOneStep()
        # --- Get values to be plotted ---
        # time
        dataPlot[k, 0] = s.nextTime()
        # Oscillo: state q
        dataPlot[k, 1] = oscillo.q()[0]
        # Oscillo: velocity
        dataPlot[k, 2] = oscillo.velocity()[0]

        dataPlot[k, 3] = inter.lambda_(1)[0]
        dataPlot[k, 4] = inter.lambda_(1)[1]
        # transfer of state i+1 into state i and time incrementation
        s.nextStep()

    # Number of time iterations
    print("Number of iterations done: {:}".format(k))

    # dataPlot (ascii) output
    np.savetxt("DryFriction.dat",  dataPlot)
Exemplo n.º 2
0
def test_xml1():
    ''' the BouncingBall '''

    bouncingBall = buildModelXML('./data/BBallTS.xml')
    # --- Get the simulation ---
    s = bouncingBall.simulation()

    dsN = SK.dynamicalSystems(bouncingBall.nonSmoothDynamicalSystem().topology().dSG(0))[0].number()
    ball = bouncingBall.nonSmoothDynamicalSystem().dynamicalSystem(dsN)

    N = 2000  # Number of time steps
    # saved in a matrix dataPlot

    outputSize = 4
    dataPlot = np.zeros((N + 1, outputSize))

    q = ball.q()
    v = ball.velocity()
    p = ball.p(1)

    dataPlot[0, 0] = bouncingBall.t0()
    dataPlot[0, 1] = q[0]
    dataPlot[0, 2] = v[0]
    dataPlot[0, 3] = p[0]

    print("====> Start computation ...")
    # --- Time loop  ---
    k = 1
    while s.hasNextEvent():
        s.computeOneStep()
        # --- Get values to be plotted ---
        dataPlot[k, 0] = s.nextTime()
        dataPlot[k, 1] = q[0]
        dataPlot[k, 2] = v[0]
        dataPlot[k, 3] = p[0]
        s.nextStep()
        k += 1

    print("End of computation - Number of iterations done: {:}".format(k))
    print("====> Output file writing ...")
    dataPlot.resize(k, outputSize)
    np.savetxt("BBallTS.dat", dataPlot)

    # Comparison with a reference file
    dataPlotRef = SK.getMatrix(SK.SimpleMatrix('./data/BBallTSXML.ref'))
    if np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf) > 1e-12:
        print(dataPlot - dataPlotRef)
        print("ERROR: The result is rather different from the reference file.")
Exemplo n.º 3
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:
        processDS = SK.FirstOrderLinearDS(x0, A)
    else:
        processDS = SK.FirstOrderLinearTIDS(x0, A)
    # Model
    process = SK.Model(t0, T)
    process.nonSmoothDynamicalSystem().insertDynamicalSystem(processDS)
    # time discretisation
    processTD = SK.TimeDiscretisation(t0, h)
    # Creation of the Simulation
    processSimulation = SK.TimeStepping(processTD, 0)
    processSimulation.setName("plant simulation")
    # Declaration of the integrator
    processIntegrator = SK.ZeroOrderHoldOSI(processDS)
    processSimulation.insertIntegrator(processIntegrator)

    rel = SK.FirstOrderLinearTIR(Csurface, B)
    nslaw = SK.RelayNSL(m)
    inter = SK.Interaction(m, nslaw, rel)

    # process.nonSmoothDynamicalSystem().insertInteraction(inter, True)
    process.nonSmoothDynamicalSystem().link(inter, processDS)
    process.nonSmoothDynamicalSystem().setControlProperty(inter, True)
    # Initialization
    process.initialize(processSimulation)

    # Main loop
    processSimulation.computeOneStep()
    Ad = SK.getMatrix(processIntegrator.Ad(processDS)).copy()
    Bd = SK.getMatrix(processIntegrator.Bd(processDS)).copy()

    return (Ad, Bd)
Exemplo n.º 4
0
def test_xml6():
    ''' BeadPlan '''
    # --- buildModelXML loading from xml file ---
    oscillator = buildModelXML("./data/BeadPlan.xml")

    # --- Get and initialize the simulation ---
    s = oscillator.simulation()

    k = 0
    t0 = oscillator.t0()
    T = oscillator.finalT()
    h = s.timeStep()
    N = np.ceil((T - t0) / h)  # Number of time steps

    # --- Get the values to be plotted ---
    # . saved in a matrix dataPlot
    dataPlot = np.zeros((N, 3))

    print("Prepare data for plotting ... ")
    # For the initi)al time step:
    # XXX fix this crap
    dsN = SK.dynamicalSystems(oscillator.nonSmoothDynamicalSystem().topology().dSG(0))[0].number()
    oscillo = oscillator.nonSmoothDynamicalSystem().dynamicalSystem(dsN)
    q = oscillo.q()
    v = oscillo.velocity()
    p = oscillo.p(1)

    dataPlot[k, 0] = t0
    dataPlot[0, 1] = q[0]
    # velocity for the oscillo
    dataPlot[0, 2] = v[0]

    print("Computation ... ")
    # --- Time loop  ---
    # while (s.hasNextEvent()
    # {
    #   // solve ...
    #   s.computeOneStep()
    #   // --- Get values to be plotted ---
    #   dataPlot[k, 0] = s.nextTime()
    #   dataPlot[k, 1] = oscillo.q()[0]
    #   dataPlot[k, 2] = oscillo.velocity()[0]
    #   // transfer of state i+1 into state i and time incrementation
    #   s.nextStep()
    #   k += 1
    #   }

    # Number of time iterations
    print("Number of iterations done: {:}".format(k))

    # dataPlot (ascii) output
    np.savetxt("BeadPlan.dat", dataPlot)
Exemplo n.º 5
0
def test_xml2():
    ''' BallInBowl '''
    # --- buildModelXML loading from xml file ---
    bouncingBall = buildModelXML("./data/BallInBowl.xml")

    # --- Get the simulation ---
    s = bouncingBall.simulation()
    k = 0
    T = bouncingBall.finalT()
    t0 = bouncingBall.t0()
    h = s.timeStep()
    N = np.ceil((T - t0) / h)

    # --- Get the values to be plotted ---
    # . saved in a matrix dataPlot
    dataPlot = np.zeros((N + 1, 6))

    print("Prepare data for plotting ... ")
    # For the initial time step:
    # time
    dataPlot[k, 0] = bouncingBall.t0()
    # state q for the first dynamical system (ball)
    dsN = SK.dynamicalSystems(bouncingBall.nonSmoothDynamicalSystem().topology().dSG(0))[0].number()
    ball = bouncingBall.nonSmoothDynamicalSystem().dynamicalSystem(dsN)

    q = ball.q()
    v = ball.velocity()
    p = ball.p(1)

    dataPlot[k, 1] = q[0]
    dataPlot[k, 2] = v[0]
    dataPlot[k, 3] = q[1]
    dataPlot[k, 4] = v[1]
    dataPlot[k, 5] = p[0]

    # --- Compute elapsed time ---
    print("Computation ... ")
    # --- Time loop  ---
    while s.hasNextEvent():
        # solve ...
        s.computeOneStep()

        # --- Get values to be plotted ---
        # time
        dataPlot[k, 0] = s.nextTime()
        # Ball: state q
        dataPlot[k, 1] = q[0]
        # Ball: velocity
        dataPlot[k, 2] = v[0]
        # Ground: q
        dataPlot[k, 3] = q[1]
        # Ground: velocity
        dataPlot[k, 4] = v[1]
        # Reaction
        dataPlot[k, 5] = p[0]
        #  dataPlot[k, 6] = osi.computeResidu()
        # transfer of state i+1 into state i and time incrementation
        s.nextStep()
        k += 1

    # Number of time iterations
    print("Number of iterations done: ")

    # dataPlot (ascii) output
    # ioMatrix::write(dataPlot,"noDim")
    np.savetxt("BallInBowl.dat", dataPlot)
Exemplo n.º 6
0
def test_xml5():
    ''' Bouncing Ball ED '''
    # --- buildModelXML loading from xml file ---
    bouncingBall = buildModelXML("./data/BBallED.xml")

    # --- Get and initialize the simulation ---
    s = bouncingBall.simulation()
    dsN = SK.dynamicalSystems(bouncingBall.nonSmoothDynamicalSystem().topology().dSG(0))[0].number()
    ball = bouncingBall.nonSmoothDynamicalSystem().dynamicalSystem(dsN)

    # --- Get the values to be plotted ---
    # . saved in a matrix dataPlot

    N = 12368  # Number of saved points: depends on the number of events ...
    outputSize = 5
    dataPlot = np.zeros((N + 1, outputSize))

    q = ball.q()
    v = ball.velocity()
    p = ball.p(1)
    f = ball.p(2)

    dataPlot[0, 0] = bouncingBall.t0()
    dataPlot[0, 1] = q[0]
    dataPlot[0, 2] = v[0]
    dataPlot[0, 3] = p[0]
    dataPlot[0, 4] = f[0]

    print("====> Start computation ... ")
    # --- Time loop  ---
    eventsManager = s.eventsManager()
    numberOfEvent = 0
    k = 0
    nonSmooth = False

    while s.hasNextEvent():
        k += 1

        s.advanceToEvent()
        if eventsManager.nextEvent().getType() == 2:
            nonSmooth = True

        s.processEvents()
        # If the treated event is non smooth, the pre-impact state has been solved in memory vectors during process.
        if nonSmooth:
            dataPlot[k, 0] = s.startingTime()
            dataPlot[k, 1] = ball.qMemory().getSiconosVector(1)[0]
            dataPlot[k, 2] = ball.velocityMemory().getSiconosVector(1)[0]
            k += 1
            nonSmooth = False
        dataPlot[k, 0] = s.startingTime()
        dataPlot[k, 1] = q[0]
        dataPlot[k, 2] = v[0]
        dataPlot[k, 3] = p[0]
        dataPlot[k, 4] = f[0]
        numberOfEvent += 1

    # --- Output files ---
    dataPlot.resize(k, outputSize)
    np.savetxt("BBallED.dat",  dataPlot)
    # Comparison with a reference file
    dataPlotRef = SK.getMatrix(SK.SimpleMatrix("./data/BouncingBallEDXml.ref"))

    if np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf) > 1e-11:
        print("Warning. The results is rather different from the reference file.")
        print(np.linalg.norm(dataPlot - dataPlotRef, ord=np.inf))
        exit(1)