Exemplo n.º 1
0
def run_example(with_plots=True):
    global exp_mod, exp_sim

    #Define the rhs
    def f(t, y):
        ydot = -y[0]
        return N.array([ydot])

    #Define an Assimulo problem
    exp_mod = Explicit_Problem(f, y0=4)
    exp_mod.name = 'Simple CVode Example'

    #Define an explicit solver
    exp_sim = CVode(exp_mod)  #Create a CVode solver

    #Sets the parameters
    exp_sim.iter = 'Newton'  #Default 'FixedPoint'
    exp_sim.discr = 'BDF'  #Default 'Adams'
    exp_sim.atol = [1e-4]  #Default 1e-6
    exp_sim.rtol = 1e-4  #Default 1e-6

    #Simulate
    t1, y1 = exp_sim.simulate(5, 100)  #Simulate 5 seconds
    t2, y2 = exp_sim.simulate(7)  #Simulate 2 seconds more

    #Basic test
    nose.tools.assert_almost_equal(y2[-1], 0.00347746, 5)
Exemplo n.º 2
0
    def _solve_cvode(f, t, y0, args, console=False, max_steps=1000, terminate=False):
        """Wrapper for Assimulo's CVODE-Sundials routine
        """
        def rhs(t, u):
            return f(u, t, *args)

        prob = Explicit_Problem(rhs, y0)
        sim = CVode(prob)
        sim.rtol = 1e-15
        sim.atol = 1e-12
        sim.discr = 'BDF'
        sim.maxord = 5 
        sim.maxsteps = max_steps

        if not console:
            sim.verbosity = 50
        else:
            sim.report_continuously = True


        t_end = t[-1]
        steps = len(t)

        try:
            print t_end, steps
            t, x = sim.simulate(t_end, steps)
        except CVodeError, e:
            raise ValueError("Something broke in CVode: %r" % e)
            return None, False
Exemplo n.º 3
0
def run_example(with_plots=True):
    global exp_mod, exp_sim
    
    #Define the rhs
    def f(t,y):
        ydot = -y[0]
        return N.array([ydot])
    
    #Define an Assimulo problem
    exp_mod = Explicit_Problem(f, y0=4)
    exp_mod.name = 'Simple CVode Example'
    
    #Define an explicit solver
    exp_sim = CVode(exp_mod) #Create a CVode solver
    
    #Sets the parameters
    exp_sim.iter  = 'Newton' #Default 'FixedPoint'
    exp_sim.discr = 'BDF' #Default 'Adams'
    exp_sim.atol = [1e-4] #Default 1e-6
    exp_sim.rtol = 1e-4 #Default 1e-6

    #Simulate
    t1, y1 = exp_sim.simulate(5,100) #Simulate 5 seconds
    t2, y2 = exp_sim.simulate(7) #Simulate 2 seconds more
    
    #Basic test
    nose.tools.assert_almost_equal(y2[-1], 0.00347746, 5)
Exemplo n.º 4
0
def run_example(with_plots=True):
    """
    This is the same example from the Sundials package (cvsRoberts_FSA_dns.c)

    This simple example problem for CVode, due to Robertson, 
    is from chemical kinetics, and consists of the following three 
    equations::
    
       dy1/dt = -p1*y1 + p2*y2*y3
       dy2/dt = p1*y1 - p2*y2*y3 - p3*y2**2
       dy3/dt = p3*(y2)^2
    
    """
    
    def f(t, y, p):
        
        yd_0 = -p[0]*y[0]+p[1]*y[1]*y[2]
        yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2
        yd_2 = p[2]*y[1]**2
        
        return N.array([yd_0,yd_1,yd_2])
    
    #The initial conditions
    y0 = [1.0,0.0,0.0]          #Initial conditions for y
    
    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f,y0)
    
    #Sets the options to the problem
    exp_mod.p0 = [0.040, 1.0e4, 3.0e7]  #Initial conditions for parameters
    exp_mod.pbar = [0.040, 1.0e4, 3.0e7]

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)
    
    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1.e-4
    exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
    exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used
    exp_sim.suppress_sens = False       #Dont suppress the sensitivity variables in the error test.
    exp_sim.continuous_output = True

    #Simulate
    t, y = exp_sim.simulate(4,400) #Simulate 4 seconds with 400 communication points
    
    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4)
    nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4)
    nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4)
    nose.tools.assert_almost_equal(exp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials
    nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8)
    nose.tools.assert_almost_equal(exp_sim.p_sol[2][-1][0], -4.9334e-10, 12)
    
    #Plot
    if with_plots:
        P.plot(t, y)
        P.show()  
Exemplo n.º 5
0
    def _setup_sim(self, **kwargs):
        """ Create a simulation interface to Assimulo using CVODE, given
        a problem definition

        """

        # Create Assimulo interface
        sim = CVode(self.prob)
        sim.discr = 'BDF'
        sim.maxord = 5

        # Setup some default arguments for the ODE solver, or override
        # if available. This is very hackish, but it's fine for now while
        # the number of anticipated tuning knobs is small.
        if 'maxh' in kwargs:
            sim.maxh = kwargs['maxh']
        else:
            sim.maxh = np.min([0.1, self.output_dt])

        if 'minh' in kwargs:
            sim.minh = kwargs['minh']
        # else: sim.minh = 0.001

        if "iter" in kwargs:
            sim.iter = kwargs['iter']
        else:
            sim.iter = 'Newton'

        if "linear_solver" in kwargs:
            sim.linear_solver = kwargs['linear_solver']

        if "max_steps" in kwargs:  # DIFFERENT NAME!!!!
            sim.maxsteps = kwargs['max_steps']
        else:
            sim.maxsteps = 1000

        if "time_limit" in kwargs:
            sim.time_limit = kwargs['time_limit']
            sim.report_continuously = True
        else:
            sim.time_limit = 0.0

        # Don't save the [t_-, t_+] around events
        sim.store_event_points = False

        # Setup tolerances
        nr = self.args[0]
        sim.rtol = state_rtol
        sim.atol = state_atol + [1e-12]*nr

        if not self.console:
            sim.verbosity = 50
        else:
            sim.verbosity = 40
        # sim.report_continuously = False

        # Save the Assimulo interface
        return sim
Exemplo n.º 6
0
    def _setup_sim(self, **kwargs):
        """ Create a simulation interface to Assimulo using CVODE, given
        a problem definition

        """

        # Create Assimulo interface
        sim = CVode(self.prob)
        sim.discr = 'BDF'
        sim.maxord = 5

        # Setup some default arguments for the ODE solver, or override
        # if available. This is very hackish, but it's fine for now while
        # the number of anticipated tuning knobs is small.
        if 'maxh' in kwargs:
            sim.maxh = kwargs['maxh']
        else:
            sim.maxh = np.min([0.1, self.output_dt])

        if 'minh' in kwargs:
            sim.minh = kwargs['minh']
        # else: sim.minh = 0.001

        if "iter" in kwargs:
            sim.iter = kwargs['iter']
        else:
            sim.iter = 'Newton'

        if "linear_solver" in kwargs:
            sim.linear_solver = kwargs['linear_solver']

        if "max_steps" in kwargs:  # DIFFERENT NAME!!!!
            sim.maxsteps = kwargs['max_steps']
        else:
            sim.maxsteps = 1000

        if "time_limit" in kwargs:
            sim.time_limit = kwargs['time_limit']
            sim.report_continuously = True
        else:
            sim.time_limit = 0.0

        # Don't save the [t_-, t_+] around events
        sim.store_event_points = False

        # Setup tolerances
        nr = self.args[0]
        sim.rtol = state_rtol
        sim.atol = state_atol + [1e-12] * nr

        if not self.console:
            sim.verbosity = 50
        else:
            sim.verbosity = 40
        # sim.report_continuously = False

        # Save the Assimulo interface
        return sim
Exemplo n.º 7
0
def run_example(with_plots=True):

    #Defines the rhs
    def f(t, y):
        yd_0 = y[1]
        yd_1 = -9.82

        return N.array([yd_0, yd_1])

    #Defines the jacobian*vector product
    def jacv(t, y, fy, v):
        j = N.array([[0, 1.], [0, 0]])
        return N.dot(j, v)

    y0 = [1.0, 0.0]  #Initial conditions

    #Defines an Assimulo explicit problem
    exp_mod = Explicit_Problem(f, y0)

    exp_mod.jacv = jacv  #Sets the jacobian
    exp_mod.name = 'Example using the Jacobian Vector product'

    exp_sim = CVode(exp_mod)  #Create a CVode solver

    #Set the parameters
    exp_sim.iter = 'Newton'  #Default 'FixedPoint'
    exp_sim.discr = 'BDF'  #Default 'Adams'
    exp_sim.atol = 1e-5  #Default 1e-6
    exp_sim.rtol = 1e-5  #Default 1e-6
    exp_sim.linear_solver = 'SPGMR'  #Change linear solver
    #exp_sim.options["usejac"] = False

    #Simulate
    t, y = exp_sim.simulate(
        5, 1000)  #Simulate 5 seconds with 1000 communication points

    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0], -121.75000000, 4)
    nose.tools.assert_almost_equal(y[-1][1], -49.100000000)

    #Plot
    if with_plots:
        P.plot(t, y)
        P.show()
Exemplo n.º 8
0
def run_example(with_plots=True):
    global t, y

    #Defines the rhs
    def f(t, y):
        yd_0 = y[1]
        yd_1 = -9.82
        #print y, yd_0, yd_1
        return N.array([yd_0, yd_1])

    #Defines the jacobian
    def jac(t, y):
        j = N.array([[0, 1.], [0, 0]])
        return j

    #Defines an Assimulo explicit problem
    y0 = [1.0, 0.0]  #Initial conditions

    exp_mod = Explicit_Problem(f, y0)

    exp_mod.jac = jac  #Sets the jacobian
    exp_mod.name = 'Example using Jacobian'

    exp_sim = CVode(exp_mod)  #Create a CVode solver

    #Set the parameters
    exp_sim.iter = 'Newton'  #Default 'FixedPoint'
    exp_sim.discr = 'BDF'  #Default 'Adams'
    exp_sim.atol = 1e-5  #Default 1e-6
    exp_sim.rtol = 1e-5  #Default 1e-6

    #Simulate
    t, y = exp_sim.simulate(
        5, 1000)  #Simulate 5 seconds with 1000 communication points

    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0], -121.75000000, 4)
    nose.tools.assert_almost_equal(y[-1][1], -49.100000000)

    #Plot
    if with_plots:
        P.plot(t, y, linestyle="dashed", marker="o")  #Plot the solution
        P.show()
Exemplo n.º 9
0
def run_example(with_plots=True):
    
    #Defines the rhs
    def f(t,y):
        yd_0 = y[1]
        yd_1 = -9.82

        return N.array([yd_0,yd_1])
    
    #Defines the jacobian*vector product
    def jacv(t,y,fy,v):
        j = N.array([[0,1.],[0,0]])
        return N.dot(j,v)
    
    y0 = [1.0,0.0] #Initial conditions
    
    #Defines an Assimulo explicit problem
    exp_mod = Explicit_Problem(f,y0)
    
    exp_mod.jacv = jacv #Sets the jacobian
    exp_mod.name = 'Example using the Jacobian Vector product'
    
    exp_sim = CVode(exp_mod) #Create a CVode solver
    
    #Set the parameters
    exp_sim.iter = 'Newton' #Default 'FixedPoint'
    exp_sim.discr = 'BDF' #Default 'Adams'
    exp_sim.atol = 1e-5 #Default 1e-6
    exp_sim.rtol = 1e-5 #Default 1e-6
    exp_sim.linear_solver = 'SPGMR' #Change linear solver
    #exp_sim.options["usejac"] = False
    
    #Simulate
    t, y = exp_sim.simulate(5, 1000) #Simulate 5 seconds with 1000 communication points
    
    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0],-121.75000000,4)
    nose.tools.assert_almost_equal(y[-1][1],-49.100000000)
    
    #Plot
    if with_plots:
        P.plot(t,y)
        P.show()
Exemplo n.º 10
0
def run_example(with_plots=True):
    global t,y
    
    #Defines the rhs
    def f(t,y):
        yd_0 = y[1]
        yd_1 = -9.82
        #print y, yd_0, yd_1
        return N.array([yd_0,yd_1])
    
    #Defines the jacobian
    def jac(t,y):
        j = N.array([[0,1.],[0,0]])
        return j
    
    #Defines an Assimulo explicit problem
    y0 = [1.0,0.0] #Initial conditions

    exp_mod = Explicit_Problem(f,y0)
    
    exp_mod.jac = jac #Sets the jacobian
    exp_mod.name = 'Example using Jacobian'

    
    exp_sim = CVode(exp_mod) #Create a CVode solver
    
    #Set the parameters
    exp_sim.iter = 'Newton' #Default 'FixedPoint'
    exp_sim.discr = 'BDF' #Default 'Adams'
    exp_sim.atol = 1e-5 #Default 1e-6
    exp_sim.rtol = 1e-5 #Default 1e-6
    
    #Simulate
    t, y = exp_sim.simulate(5, 1000) #Simulate 5 seconds with 1000 communication points
    
    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0],-121.75000000,4)
    nose.tools.assert_almost_equal(y[-1][1],-49.100000000)
        
    #Plot
    if with_plots:
        P.plot(t,y,linestyle="dashed",marker="o") #Plot the solution
        P.show()
Exemplo n.º 11
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.
        """

        #X = X.copy()
        g = 9.81
        x = X[0]
        vx = X[2]
        vy = X[3]

        return np.array([vx, vy, -(np.pi**2)*x, -g, 1.0])

    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.
        """
        x = X[0]
        y = X[1]

        return [x - y]
        #return np.array([x - y])

    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]: #If the switch is True the pendulum bounces
                X = solver.y
                X[1] = X[0] + 1e-3
                X[3] = 0.9*(X[2] - X[3])

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

    #Initial values
    phi = 1.0241592; Y0 = -13.0666666; A = 2.0003417; w = np.pi
    y0 = [A*np.sin(phi), 1+A*np.sin(phi), A*w*np.cos(phi), Y0 - A*w*np.cos(phi)-1, 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 on Sonusoidal Platform'   #Sets the name of the problem

    #Create an Assimulo solver (CVode)
    sim = CVode(mod)
    #sim = LSODAR(mod)
    sim.options['verbosity'] = 40

    #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
def run_example(with_plots=True):
    """
    This example show how to use Assimulo and CVode for simulating sensitivities
    for initial conditions.::
    
        dy1/dt = -(k01+k21+k31)*y1 + k12*y2 + k13*y3 + b1
        dy2/dt = k21*y1 - (k02+k12)*y2
        dy3/dt = k31*y1 - k13*y3
     
        y1(0) = p1, y2(0) = p2, y3(0) = p3
        p1=p2=p3 = 0 
    
    See http://sundials.2283335.n4.nabble.com/Forward-sensitivities-for-initial-conditions-td3239724.html
    """
    def f(t, y, p):
        y1,y2,y3 = y
        k01 = 0.0211
        k02 = 0.0162
        k21 = 0.0111
        k12 = 0.0124
        k31 = 0.0039
        k13 = 0.000035
        b1 = 49.3
        
        yd_0 = -(k01+k21+k31)*y1+k12*y2+k13*y3+b1
        yd_1 = k21*y1-(k02+k12)*y2
        yd_2 = k31*y1-k13*y3
        
        return N.array([yd_0,yd_1,yd_2])
    
    #The initial conditions
    y0 = [0.0,0.0,0.0]          #Initial conditions for y
    p0 = [0.0, 0.0, 0.0]  #Initial conditions for parameters
    yS0 = N.array([[1,0,0],[0,1,0],[0,0,1.]])
    
    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f, y0, p0=p0)
    
    #Sets the options to the problem
    exp_mod.yS0 = yS0
    
    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)
    
    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1e-7
    exp_sim.atol = 1e-6
    exp_sim.pbar = [1,1,1] #pbar is used to estimate the tolerances for the parameters
    exp_sim.continuous_output = True #Need to be able to store the result using the interpolate methods
    exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used
    exp_sim.suppress_sens = False            #Dont suppress the sensitivity variables in the error test.
    
    #Simulate
    t, y = exp_sim.simulate(400) #Simulate 400 seconds
    
    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 1577.6552477, 5)
    nose.tools.assert_almost_equal(y[-1][1], 611.9574565, 5)
    nose.tools.assert_almost_equal(y[-1][2], 2215.88563217, 5)
    nose.tools.assert_almost_equal(exp_sim.p_sol[0][1][0], 1.0)

    #Plot
    if with_plots:
        P.figure(1)
        P.subplot(221)
        P.plot(t, N.array(exp_sim.p_sol[0])[:,0],
               t, N.array(exp_sim.p_sol[0])[:,1],
               t, N.array(exp_sim.p_sol[0])[:,2])
        P.title("Parameter p1")
        P.legend(("p1/dy1","p1/dy2","p1/dy3"))
        P.subplot(222)
        P.plot(t, N.array(exp_sim.p_sol[1])[:,0],
               t, N.array(exp_sim.p_sol[1])[:,1],
               t, N.array(exp_sim.p_sol[1])[:,2])
        P.title("Parameter p2")
        P.legend(("p2/dy1","p2/dy2","p2/dy3"))
        P.subplot(223)
        P.plot(t, N.array(exp_sim.p_sol[2])[:,0],
               t, N.array(exp_sim.p_sol[2])[:,1],
               t, N.array(exp_sim.p_sol[2])[:,2])
        P.title("Parameter p3")
        P.legend(("p3/dy1","p3/dy2","p3/dy3"))
        P.subplot(224)
        P.plot(t, y)
        P.show()
Exemplo n.º 13
0
def run_example():

    def pendulum(t,y,sw):
        """
        The ODE to be simulated. The parameter sw should be fixed during 
        the simulation and only be changed during the event handling.
        """
        l=1.0
        g=9.81
        yd_0 = y[1]
        yd_1 = -g/l*N.sin(y[0])
            
        return N.array([yd_0, yd_1])

    def state_events(t,y,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.
        """
        if sw[0]:
            e_0 = y[0]+N.pi/4.
        else:
            e_0 = y[0]

        return N.array([e_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]: #If the switch is True the pendulum bounces
                solver.y[1] = -0.9*solver.y[1] #Change the velocity and lose energy
                
            solver.sw[0] = not solver.sw[0] #Change event function

    #Initial values
    y0 = [N.pi/2.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 = 'Pendulum with events'   #Sets the name of the problem

    #Create an Assimulo solver (CVode)
    sim = CVode(mod)
    
    #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
    
    #Simulation
    ncp = 200     #Number of communication points
    tfinal = 10.0 #Final time
    
    t,y = sim.simulate(tfinal, ncp) #Simulate
    
    #Print event information
    sim.print_event_data()
    
    #Plot
    P.plot(t,y)
    P.show()
Exemplo n.º 14
0
def run_example(with_plots=True):
    def curl(v):
        return array([[0, v[2], -v[1]], [-v[2], 0, v[0]], [v[1], -v[0], 0]])

    #Defines the rhs
    def f(t, u):
        """
        Simulations for the Gyro (Heavy Top) example in Celledoni/Safstrom: 
        Journal of Physics A, Vol 39, 5463-5478, 2006
        """
        I1 = 1000.
        I2 = 5000.
        I3 = 6000.
        u0 = [0, 0, 1.]
        pi = u[0:3]
        Q = (u[3:12]).reshape((3, 3))
        Qu0 = dot(Q, u0)
        f = array([Qu0[1], -Qu0[0], 0.])
        f = 0
        omega = array([pi[0] / I1, pi[1] / I2, pi[2] / I3])
        pid = dot(curl(omega), pi) + f
        Qd = dot(curl(omega), Q)
        return hstack([pid, Qd.reshape((9, ))])

    def energi(state):
        energi = []
        for st in state:
            Q = (st[3:12]).reshape((3, 3))
            pi = st[0:3]
            u0 = [0, 0, 1.]
            Qu0 = dot(Q, u0)
            V = Qu0[2]  # potential energy
            T = 0.5 * (pi[0]**2 / 1000. + pi[1]**2 / 5000. + pi[2]**2 / 6000.)
            energi.append([T])
        return energi

    #Initial conditions
    y0 = hstack([[1000. * 10, 5000. * 10, 6000 * 10], eye(3).reshape((9, ))])

    #Create an Assimulo explicit problem
    gyro_mod = Explicit_Problem(f, y0)

    #Create an Assimulo explicit solver (CVode)
    gyro_sim = CVode(gyro_mod)

    #Sets the parameters
    gyro_sim.discr = 'BDF'
    gyro_sim.iter = 'Newton'
    gyro_sim.maxord = 2  #Sets the maxorder
    gyro_sim.atol = 1.e-10
    gyro_sim.rtol = 1.e-10

    #Simulate
    t, y = gyro_sim.simulate(0.1)

    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0], 692.800241862)
    nose.tools.assert_almost_equal(y[-1][8], 7.08468221e-1)

    #Plot
    if with_plots:
        P.plot(t, y)
        P.show()
Exemplo n.º 15
0
def run_example(with_plots=True):
    """
    This is the same example from the Sundials package (cvsRoberts_FSA_dns.c)

    This simple example problem for CVode, due to Robertson, 
    is from chemical kinetics, and consists of the following three 
    equations::
    
       dy1/dt = -p1*y1 + p2*y2*y3
       dy2/dt = p1*y1 - p2*y2*y3 - p3*y2**2
       dy3/dt = p3*(y2)^2
    
    """
    def f(t, y, p):

        yd_0 = -p[0] * y[0] + p[1] * y[1] * y[2]
        yd_1 = p[0] * y[0] - p[1] * y[1] * y[2] - p[2] * y[1]**2
        yd_2 = p[2] * y[1]**2

        return N.array([yd_0, yd_1, yd_2])

    #The initial conditions
    y0 = [1.0, 0.0, 0.0]  #Initial conditions for y

    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f, y0)

    #Sets the options to the problem
    exp_mod.p0 = [0.040, 1.0e4, 3.0e7]  #Initial conditions for parameters
    exp_mod.pbar = [0.040, 1.0e4, 3.0e7]

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)

    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1.e-4
    exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
    exp_sim.sensmethod = 'SIMULTANEOUS'  #Defines the sensitvity method used
    exp_sim.suppress_sens = False  #Dont suppress the sensitivity variables in the error test.
    exp_sim.continuous_output = True

    #Simulate
    t, y = exp_sim.simulate(
        4, 400)  #Simulate 4 seconds with 400 communication points

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4)
    nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4)
    nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4)
    nose.tools.assert_almost_equal(
        exp_sim.p_sol[0][-1][0], -1.8761,
        2)  #Values taken from the example in Sundials
    nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8)
    nose.tools.assert_almost_equal(exp_sim.p_sol[2][-1][0], -4.9334e-10, 12)

    #Plot
    if with_plots:
        P.plot(t, y)
        P.show()
Exemplo n.º 16
0
    def simulate(self, t:numpy.ndarray, parameters:dict=None, verbosity:int=30, reset_afterwards:bool=False, suppress_stdout:bool=True) -> List[TimeSeries]:
        """
        Runs a forward simulation for the fully specified model and its observation functions (if specified).

        Arguments
        ---------
            t : numpy.ndarray or float
                The time points for integration. In case a single time point is provided, 
                the solver will treat this as final integration time and chooses the intermediate steps on its own.

        Keyword arguments
        -----------------
            parameters : dict
                In case a simulation for specific parameter values is wanted. 
                Default is None.
            verbosity : int
                Prints solver statistics (quiet = 50, whisper = 40, normal = 30, loud = 20, scream = 10). 
                Default is 30.
            reset_afterwards : bool
                After simulation, reset the Simulator instance to its state directly after instantiation. 
                Useful, if parameters argument is used.
            suppress_stdout : bool
                No printouts of integrator warnings, which are directed to stdout by the assimulo package.
                Set to False for model debugging purposes.
                Default is True.

        Returns
        -------
            simulations : List[TimeSeries]
                The collection of simulations results as `ModelState` objects
                and `Observation` objects (if at least one `ObservationFunction` has been specified)

        Raises
        ------
            ValueError 
                Not all parameters have values.
        """

        if parameters is not None:
            self.set_parameters(parameters)

        # Create the solver from problem instance
        exp_sim = CVode(self.bioprocess_model)
        exp_sim.verbosity = verbosity # QUIET = 50 WHISPER = 40 NORMAL = 30 LOUD = 20 SCREAM = 10
        exp_sim.store_event_points = False
        exp_sim.discr = 'BDF'
        exp_sim.stablimit = True

        if self.integrator_kwargs is not None:
            for key in list(self.integrator_kwargs.keys()):
                exec(f'exp_sim.{key} = self.integrator_kwargs["{key}"]')

        # Do the actual forward simulation
        _t = numpy.array(t, dtype=numpy.float64).flatten()
        if len(_t) == 1:
            tfinal = float(_t)
            ncp_list = None
        elif len(_t) > 1:
            ncp_list = _t
            tfinal = numpy.max(_t)
        try:
            if suppress_stdout:
                f = io.StringIO()
                with redirect_stdout(f):
                    t, y = exp_sim.simulate(tfinal=tfinal, ncp_list=ncp_list)
            else:
                t, y = exp_sim.simulate(tfinal=tfinal, ncp_list=ncp_list)
        except CVodeError as e:
            print(f'CVodeError occured with flag {e.value}. CVodeError message was: {e}.')
            raise e

        # clean double entry artifacts due to event detection
        unq, unq_idx = numpy.unique(t, return_index=True)

        # build model prediction dictionary
        model_predictions = [
            ModelState(
                name=_name, 
                timepoints=numpy.array(t)[unq_idx], 
                values=numpy.array(_y)[unq_idx], 
                replicate_id=self.replicate_id,
                ) 
            for _name, _y in zip(self.bioprocess_model.states, y.T)
        ]

        if self.observer is not None:
            observations = self.observer.get_observations(model_predictions)
        else:
            observations = []

        if reset_afterwards:
            self.reset()

        simulations = []
        simulations.extend(model_predictions)
        simulations.extend(observations)

        return simulations
Exemplo n.º 17
0
def run_example(with_plots=True):
    """
    This example show how to use Assimulo and CVode for simulating sensitivities
    for initial conditions.::
    
        dy1/dt = -(k01+k21+k31)*y1 + k12*y2 + k13*y3 + b1
        dy2/dt = k21*y1 - (k02+k12)*y2
        dy3/dt = k31*y1 - k13*y3
     
        y1(0) = p1, y2(0) = p2, y3(0) = p3
        p1=p2=p3 = 0 
    
    See http://sundials.2283335.n4.nabble.com/Forward-sensitivities-for-initial-conditions-td3239724.html
    """
    def f(t, y, p):
        y1, y2, y3 = y
        k01 = 0.0211
        k02 = 0.0162
        k21 = 0.0111
        k12 = 0.0124
        k31 = 0.0039
        k13 = 0.000035
        b1 = 49.3

        yd_0 = -(k01 + k21 + k31) * y1 + k12 * y2 + k13 * y3 + b1
        yd_1 = k21 * y1 - (k02 + k12) * y2
        yd_2 = k31 * y1 - k13 * y3

        return N.array([yd_0, yd_1, yd_2])

    #The initial conditions
    y0 = [0.0, 0.0, 0.0]  #Initial conditions for y
    p0 = [0.0, 0.0, 0.0]  #Initial conditions for parameters
    yS0 = N.array([[1, 0, 0], [0, 1, 0], [0, 0, 1.]])

    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f, y0, p0=p0)

    #Sets the options to the problem
    exp_mod.yS0 = yS0

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)

    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1e-7
    exp_sim.atol = 1e-6
    exp_sim.pbar = [
        1, 1, 1
    ]  #pbar is used to estimate the tolerances for the parameters
    exp_sim.continuous_output = True  #Need to be able to store the result using the interpolate methods
    exp_sim.sensmethod = 'SIMULTANEOUS'  #Defines the sensitvity method used
    exp_sim.suppress_sens = False  #Dont suppress the sensitivity variables in the error test.

    #Simulate
    t, y = exp_sim.simulate(400)  #Simulate 400 seconds

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 1577.6552477, 5)
    nose.tools.assert_almost_equal(y[-1][1], 611.9574565, 5)
    nose.tools.assert_almost_equal(y[-1][2], 2215.88563217, 5)
    nose.tools.assert_almost_equal(exp_sim.p_sol[0][1][0], 1.0)

    #Plot
    if with_plots:
        P.figure(1)
        P.subplot(221)
        P.plot(t,
               N.array(exp_sim.p_sol[0])[:, 0], t,
               N.array(exp_sim.p_sol[0])[:, 1], t,
               N.array(exp_sim.p_sol[0])[:, 2])
        P.title("Parameter p1")
        P.legend(("p1/dy1", "p1/dy2", "p1/dy3"))
        P.subplot(222)
        P.plot(t,
               N.array(exp_sim.p_sol[1])[:, 0], t,
               N.array(exp_sim.p_sol[1])[:, 1], t,
               N.array(exp_sim.p_sol[1])[:, 2])
        P.title("Parameter p2")
        P.legend(("p2/dy1", "p2/dy2", "p2/dy3"))
        P.subplot(223)
        P.plot(t,
               N.array(exp_sim.p_sol[2])[:, 0], t,
               N.array(exp_sim.p_sol[2])[:, 1], t,
               N.array(exp_sim.p_sol[2])[:, 2])
        P.title("Parameter p3")
        P.legend(("p3/dy1", "p3/dy2", "p3/dy3"))
        P.subplot(224)
        P.plot(t, y)
        P.show()
Exemplo n.º 18
0
def run_example(with_plots=True):
    
    def curl(v):
        return array([[0,v[2],-v[1]],[-v[2],0,v[0]],[v[1],-v[0],0]])

    #Defines the rhs
    def f(t,u):
        """
        Simulations for the Gyro (Heavy Top) example in Celledoni/Safstrom: 
        Journal of Physics A, Vol 39, 5463-5478, 2006
        """
        I1=1000.
        I2=5000.
        I3=6000.
        u0=[0,0,1.]
        pi=u[0:3]
        Q=(u[3:12]).reshape((3,3))
        Qu0=dot(Q,u0)
        f=array([Qu0[1],-Qu0[0],0.])
        f=0
        omega=array([pi[0]/I1,pi[1]/I2,pi[2]/I3])
        pid=dot(curl(omega),pi)+f
        Qd=dot(curl(omega),Q)
        return hstack([pid,Qd.reshape((9,))])

    def energi(state):
        energi=[]
        for st in state:
            Q=(st[3:12]).reshape((3,3))
            pi=st[0:3]
            u0=[0,0,1.]
            Qu0=dot(Q,u0)
            V=Qu0[2]  # potential energy
            T=0.5*(pi[0]**2/1000.+pi[1]**2/5000.+pi[2]**2/6000.)
            energi.append([T])
        return energi

    #Initial conditions
    y0=hstack([[1000.*10,5000.*10,6000*10],eye(3).reshape((9,))])
    
    #Create an Assimulo explicit problem
    gyro_mod = Explicit_Problem(f,y0)
    
    #Create an Assimulo explicit solver (CVode)
    gyro_sim=CVode(gyro_mod)
    
    #Sets the parameters
    gyro_sim.discr='BDF'
    gyro_sim.iter='Newton'
    gyro_sim.maxord=2 #Sets the maxorder
    gyro_sim.atol=1.e-10
    gyro_sim.rtol=1.e-10
    
    #Simulate
    t, y = gyro_sim.simulate(0.1)
    
    #Basic tests
    nose.tools.assert_almost_equal(y[-1][0],692.800241862)
    nose.tools.assert_almost_equal(y[-1][8],7.08468221e-1)
    
    #Plot
    if with_plots:
        P.plot(t,y)
        P.show()
Exemplo n.º 19
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.
        """

        X = X.copy()
        X[0] = X[1]
        X[1] = -1 + 0.04 * (X[1]**2) * np.sin(X[1])
        X[2] = 1

        return X

    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[0] - np.sin(X[2])]

    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]: #If the switch is True the pendulum bounces
                X = solver.y
                if X[1] - np.cos(X[2]) < 0:
                    X[1] = -0.9*X[1] + 1.9*np.cos(X[2])

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

    #Initial values
    y0 = [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 on Sonusoidal Platform'   #Sets the name of the problem

    #Create an Assimulo solver (CVode)
    sim = CVode(mod)
    #sim = LSODAR(mod)
    #sim.options['verbosity'] = 20 #LOUD
    sim.options['verbosity'] = 40 #WHISPER
    #sim.options['minh'] = 1e-4
    #sim.options['rtol'] = 1e-3

    #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
Exemplo n.º 20
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.
        """

        #X = X.copy()
        g = 9.81
        x = X[0]
        vx = X[2]
        vy = X[3]

        return np.array([vx, vy, -(np.pi**2) * x, -g, 1.0])

    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.
        """
        x = X[0]
        y = X[1]

        return [x - y]
        #return np.array([x - y])

    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]:  #If the switch is True the pendulum bounces
                X = solver.y
                X[1] = X[0] + 1e-3
                X[3] = 0.9 * (X[2] - X[3])

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

    #Initial values
    phi = 1.0241592
    Y0 = -13.0666666
    A = 2.0003417
    w = np.pi
    y0 = [
        A * np.sin(phi), 1 + A * np.sin(phi), A * w * np.cos(phi),
        Y0 - A * w * np.cos(phi) - 1, 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 on Sonusoidal Platform'  #Sets the name of the problem

    #Create an Assimulo solver (CVode)
    sim = CVode(mod)
    #sim = LSODAR(mod)
    sim.options['verbosity'] = 40

    #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
Exemplo n.º 21
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.
        """

        X = X.copy()
        X[0] = X[1]
        X[1] = -1 + 0.04 * (X[1]**2) * np.sin(X[1])
        X[2] = 1

        return X

    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[0] - np.sin(X[2])]

    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]:  #If the switch is True the pendulum bounces
                X = solver.y
                if X[1] - np.cos(X[2]) < 0:
                    X[1] = -0.9 * X[1] + 1.9 * np.cos(X[2])

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

    #Initial values
    y0 = [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 on Sonusoidal Platform'  #Sets the name of the problem

    #Create an Assimulo solver (CVode)
    sim = CVode(mod)
    #sim = LSODAR(mod)
    #sim.options['verbosity'] = 20 #LOUD
    sim.options['verbosity'] = 40  #WHISPER
    #sim.options['minh'] = 1e-4
    #sim.options['rtol'] = 1e-3

    #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