Example #1
0
def run_example(with_plots=True):
    """
    Example of the use of DOPRI5 for a differential equation
    with a discontinuity (state event) and the need for an event iteration.
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    """
    #Create an instance of the problem
    exp_mod = Extended_Problem()  #Create the problem

    exp_sim = Dopri5(exp_mod)  #Create the solver

    exp_sim.verbosity = 0
    exp_sim.report_continuously = True

    #Simulate
    t, y = exp_sim.simulate(
        10.0, 1000)  #Simulate 10 seconds with 1000 communications points

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 8.0)
    nose.tools.assert_almost_equal(y[-1][1], 3.0)
    nose.tools.assert_almost_equal(y[-1][2], 2.0)

    #Plot
    if with_plots:
        P.plot(t, y)
        P.title(exp_mod.name)
        P.ylabel('States')
        P.xlabel('Time')
        P.show()

    return exp_mod, exp_sim

    return exp_mod, exp_sim
Example #2
0
def run_example(with_plots=True):
    r"""
    Example to demonstrate the use of the Runge-Kutta solver DOPRI5
    for the linear test equation :math:`\dot y = - y`
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    
    """    
    #Defines the rhs
    def f(t,y):
        ydot = -y[0]
        return N.array([ydot])

    #Define an Assimulo problem
    exp_mod = Explicit_Problem(f, 4.0,
              name = 'DOPRI5 Example: $\dot y = - y$')
    
    exp_sim = Dopri5(exp_mod) #Create a Dopri5 solver

    #Simulate
    t, y = exp_sim.simulate(5) #Simulate 5 seconds
    
    #Plot
    if with_plots:
        import pylab as P
        P.plot(t,y)
        P.title(exp_mod.name)
        P.xlabel('Time')
        P.ylabel('State')
        P.show()
    
    #Basic test
    nose.tools.assert_almost_equal(float(y[-1]),0.02695199,5)
    
    return exp_mod, exp_sim
Example #3
0
def create_model():
    def pendulum(t, X, sw):
        """
        The ODE to be simulated. The parameter sw should be fixed during
        the simulation and only be changed during the event handling.
        """
        g = 1

        Y = X.copy()
        Y[0] = X[2]  #x_dot
        Y[1] = X[3]  #y_dot
        Y[2] = 0  #vx_dot
        Y[3] = -g  #vy_dot
        return Y

    def state_events(t, X, sw):
        """
        This is our function that keep track of our events, when the sign
        of any of the events has changed, we have an event.
        """
        return [X[1]]  # y == 0

    def handle_event(solver, event_info):
        """
        Event handling. This functions is called when Assimulo finds an event as
        specified by the event functions.
        """
        state_info = event_info[
            0]  #We are only interested in state events info

        if state_info[
                0] != 0:  #Check if the first event function have been triggered
            if solver.sw[0]:
                X = solver.y
                if X[3] < 0:  # if the ball is falling (vy < 0)
                    # bounce!
                    #X[1] = 1e-5 # used with CVode
                    X[1] = 1e-3  # gives better results with Dopri
                    X[3] = -0.75 * X[3]

            #solver.sw[0] = not solver.sw[0] #Change event function

    #Initial values
    y0 = [0., 0., 0., 0.]  #Initial states
    t0 = 0.0  #Initial time
    switches0 = [True]  #Initial switches

    #Create an Assimulo Problem
    mod = Explicit_Problem(pendulum, y0, t0, sw0=switches0)

    mod.state_events = state_events  #Sets the state events to the problem
    mod.handle_event = handle_event  #Sets the event handling to the problem
    mod.name = 'Bouncing Ball in X-Y'  #Sets the name of the problem

    #Create an Assimulo solver (CVode)

    # leaks memory!!
    #sim = CVode(mod)

    # hands and possibly leaks memory
    #sim = LSODAR(mod)

    #sim = RungeKutta34(mod)
    sim = Dopri5(mod)

    #sim.options['verbosity'] = 20 #LOUD
    sim.verbosity = 40  #WHISPER
    #sim.display_progress = False
    #sim.options['minh'] = 1e-4
    #sim.options['rtol'] = 1e-3
    # What is time_limit?
    sim.time_limit = 1

    #     #Specifies options
    #     sim.discr = 'Adams'     #Sets the discretization method
    #     sim.iter = 'FixedPoint' #Sets the iteration method
    #     sim.rtol = 1.e-8        #Sets the relative tolerance
    #     sim.atol = 1.e-6        #Sets the absolute tolerance

    return sim
    lam = x[7:13]

    # insert in a) and b)
    #    lamdot = (lam-y[14:20])/2 # 1st order approx ??
    yd = N.hstack((v, w, lam))

    return yd


#
t0 = 0.0
y0 = init_y()

model = Explicit_Problem(rhs, y0, t0)

sim = Dopri5(model)
#sim.algvar = [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#sim.algvar = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
#sim.suppress_alg = True
#sim = CVode(model)

tfinal = 0.03  #Specify the final time
t, y = sim.simulate(tfinal)

#sim.show()
P.figure()
P.plot(t, (y[:, 0:7] + N.pi) % (2 * N.pi) - N.pi)
P.axis([t0, tfinal, -0.8, 0.8])  # zoom in to cf. w/ ref. (optional)
#figure
#plot(t,y[:,14:24])
lamint = y[:, 14:21]