def daeOut(ode = None, alg = None):

    if alg is None:

        return ca.daeOut(ode = ode)

    else:

        return ca.daeOut(ode = ode, alg = alg)
Beispiel #2
0
    def solve_ode(self):
        """ Solve the ODE using casadi's CVODES wrapper to ensure that the
        collocated dynamics match the error-controlled dynamics of the ODE """

        f_integrator = cs.SXFunction(
            "ode",
            cs.daeIn(t=self.model.inputExpr(0), x=self.model.inputExpr(1), p=self.model.inputExpr(2)),
            cs.daeOut(ode=self.model.outputExpr(0)),
        )

        integrator = cs.Integrator("int", "cvodes", f_integrator)
        simulator = cs.Simulator("sim", integrator, self._tgrid)
        simulator.setInput(self._output["x_opt"][0], "x0")
        simulator.setInput(self._output["p_opt"], "p")
        simulator.evaluate()
        x_sim = self._output["x_sim"] = np.array(simulator.getOutput()).T

        err = ((self._output["x_opt"] - x_sim).mean(0) / (self._output["x_opt"].mean(0))).mean()

        if err > 1e-3:
            warn(
                "Collocation does not match ODE Solution: \
                {:.2f}% Error".format(
                    100 * err
                )
            )
Beispiel #3
0
def model():

    # Time Variable
    t = cs.ssym("t")

    # Variable Assignments
    X = cs.ssym("X")
    Y = cs.ssym("Y")

    y = cs.vertcat([X, Y])

    # Parameter Assignments
    u = cs.ssym("u")

    ode = [[]] * NEQ
    ode[0] = u * (X - (X**3 / 3.) - Y)
    ode[1] = X / u
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=cs.vertcat([
        u,
    ])), cs.daeOut(ode=ode))

    fn.setOption("name", "Van der Pol oscillator")

    return fn
Beispiel #4
0
def model():
    """ Create an ODE casadi SXFunction """

    # State Variables
    Y1 = cs.ssym("Y1")
    Y2 = cs.ssym("Y2")

    y = cs.vertcat([Y1, Y2])

    # Parameters
    c1x1 = cs.ssym("c1x1")
    c2x2 = cs.ssym("c2x2")
    c3 = cs.ssym("c3")
    c4 = cs.ssym("c4")

    symparamset = cs.vertcat([c1x1, c2x2, c3, c4])

    # Time
    t = cs.ssym("t")

    # ODES
    ode = [[]] * NEQ
    ode[0] = c1x1 - c2x2 * Y1 + (c3 / 2.) * (Y1**2) * Y2 - c4 * Y1
    ode[1] = c2x2 * Y1 - (c3 / 2.) * (Y1**2) * Y2
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset), cs.daeOut(ode=ode))

    fn.setOption("name", "Brusselator")

    return fn
Beispiel #5
0
    def solve_ode(self):
        """ Solve the ODE using casadi's CVODES wrapper to ensure that the
        collocated dynamics match the error-controlled dynamics of the ODE """


        self.ts.sort() # Assert ts is increasing

        f_integrator = cs.SXFunction('ode',
                                     cs.daeIn(
                                         t = self.dxdt.inputExpr(0),
                                         x = self.dxdt.inputExpr(1),
                                         p = self.dxdt.inputExpr(2)),
                                     cs.daeOut(
                                         ode = self.dxdt.outputExpr(0)))

        integrator = cs.Integrator('int', 'cvodes', f_integrator)
        simulator = cs.Simulator('sim', integrator, self.ts)
        simulator.setInput(self.sol[0], 'x0')
        simulator.setInput(self.var.p_op, 'p')
        simulator.evaluate()
        x_sim = self.sol_sim = np.array(simulator.getOutput()).T

        err = ((self.sol - x_sim).mean(0) /
               (self.sol.mean(0))).mean()

        if err > 1E-3: warn(
                'Collocation does not match ODE Solution: \
                {:.2%} Error'.format(err))
def kiss_model_ode():
    
    # For two oscillators
    e1 = cs.ssym('e1')
    tht1 = cs.ssym('tht1')

    e2 = cs.ssym('e2')
    tht2 = cs.ssym('tht2')

    state_set = cs.vertcat([e1, tht1, e2, tht2])

    # Parameter Assignments
    A1Rind1 = cs.ssym('A1Rind1')
    A2Rind2 = cs.ssym('A2Rind2')
    V       = cs.ssym('V')
    Ch      = cs.ssym('Ch')
    a       = cs.ssym('a')
    b       = cs.ssym('b')
    c       = cs.ssym('c')
    gam1    = cs.ssym('gam1')
    gam2    = cs.ssym('gam2')
    AR      = cs.ssym('AR') 
    
    # AR is coupling strength, = 50.5 for model in paper

    param_set = cs.vertcat([A1Rind1, A2Rind2, V, Ch, a, b, c,
                            gam1, gam2, AR])

    # Time
    t = cs.ssym('t')

    ode = [[]]*neq
    
    # oscillator 1
    ode[0] = (V-e1)/A1Rind1 - \
       ( Ch*cs.exp(0.5*e1)/(1+Ch*cs.exp(e1)) + a*cs.exp(e1) )*(1-tht1) -\
       (e1-e2-np.sin(freq/(2*np.pi)*t))/AR
    ode[1] = (1/gam1)*(\
            (cs.exp(0.5*e1)/(1+Ch*cs.exp(e1)))*(1-tht1) -\
            b*Ch*cs.exp(2*e1)*tht1/(c*Ch+cs.exp(e1))\
            )
    
    
    # oscillator 2
    ode[2] = (V-e2-np.sin(freq/(2*np.pi)*t))/A2Rind2 - \
       ( Ch*cs.exp(0.5*e2-np.sin(freq/(2*np.pi)*t))/(1+Ch*cs.exp(e2-np.sin(freq/(2*np.pi)*t))) + a*cs.exp(e2-np.sin(freq/(2*np.pi)*t)) )*(1-tht2) -\
       (e2+np.sin(freq/(2*np.pi)*t)-e1)/AR
    ode[3] = (1/gam2)*(\
            (cs.exp(0.5*e2-np.sin(freq/(2*np.pi)*t))/(1+Ch*cs.exp(e2-np.sin(freq/(2*np.pi)*t))))*(1-tht2) -\
            b*Ch*cs.exp(2*e2-np.sin(freq/(2*np.pi)*t))*tht2/(c*Ch+cs.exp(e2-np.sin(freq/(2*np.pi)*t)))\
            )

    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t,x=state_set,p=param_set), 
            cs.daeOut(ode=ode))

    fn.setOption("name","kiss_oscillator_2011")

    return fn
    def _create_ARC_model(self, numstates=1):
        """ Create model with quadrature for amplitude sensitivities
        numstates might allow us to calculate entire sARC at once, but
        now will use seed method. """

        # Allocate symbolic vectors for the model
        dphidx = cs.SX.sym('dphidx', numstates)
        t      = self.model.inputExpr(cs.DAE_T)    # time
        xd     = self.model.inputExpr(cs.DAE_X)    # differential state
        s      = cs.SX.sym("s", self.neq, numstates) # sensitivities
        p      = cs.vertcat([self.model.inputExpr(2), dphidx]) # parameters

        # Symbolic function (from input model)
        ode_rhs = self.model.outputExpr()[0]
        f_tilde = self.T*ode_rhs/(2*np.pi)

        # symbolic jacobians
        jac_x = self.model.jac(cs.DAE_X, cs.DAE_X)
        sens_rhs = jac_x.mul(s)

        quad = cs.SX.sym('q', self.neq, numstates)
        for i in xrange(numstates):
            quad[:,i] = 2*(s[:,i] - dphidx[i]*f_tilde)*(xd - self.avg)

        shape = (self.neq*numstates, 1)

        x = cs.vertcat([xd, s.reshape(shape)])
        ode = cs.vertcat([ode_rhs, sens_rhs.reshape(shape)])
        ffcn = cs.SXFunction(cs.daeIn(t=t, x=x, p=p),
                             cs.daeOut(ode=ode, quad=quad))
        return ffcn
    def phase_of_point(self, point, error=False, tol=1E-3):
        """ Finds the phase at which the distance from the point to the
        limit cycle is minimized. phi=0 corresponds to the definition of
        y0, returns the phase and the minimum distance to the limit
        cycle """

        point = np.asarray(point)

        #set up integrator so we only have to once...
        intr = cs.Integrator('cvodes',self.model)
        intr.setOption("abstol", self.intoptions['bvp_abstol'])
        intr.setOption("reltol", self.intoptions['bvp_reltol'])
        intr.setOption("tf", self.T)
        intr.setOption("max_num_steps",
                       self.intoptions['transmaxnumsteps'])
        intr.setOption("disable_internal_warnings", True)
        intr.init()
        for i in xrange(100):
            dist = cs.SX.sym("dist")
            x = self.model.inputExpr(cs.DAE_X)
            ode = self.model.outputExpr()[0]
            dist_ode = cs.sumAll(2.*(x - point)*ode)

            cat_x   = cs.vertcat([x, dist])
            cat_ode = cs.vertcat([ode, dist_ode])

            dist_model = cs.SXFunction(
                cs.daeIn(t=self.model.inputExpr(cs.DAE_T), x=cat_x,
                         p=self.model.inputExpr(cs.DAE_P)),
                cs.daeOut(ode=cat_ode))

            dist_model.setOption("name","distance model")

            dist_0 = ((self.y0 - point)**2).sum()
            if dist_0 < tol:
                # catch the case where we start at 0
                return 0.
            cat_y0 = np.hstack([self.y0, dist_0])

            roots_class = Oscillator(dist_model, self.param, cat_y0)
            roots_class.intoptions = self.intoptions
            #return roots_class
            roots_class.solve_bvp()
            roots_class.limit_cycle()
            roots_class.roots()

            phases = self._t_to_phi(roots_class.tmin[-1])
            distances = roots_class.ymin[-1]
            distance = np.min(distances)

            if distance < tol:
                phase_ind = np.argmin(distances) # for multiple minima
                return phases[phase_ind]#, roots_class

            intr.setInput(point, cs.INTEGRATOR_X0)
            intr.setInput(self.param, cs.INTEGRATOR_P)
            intr.evaluate()
            point = intr.output().toArray().flatten() #advance by one cycle

        raise RuntimeError("Point failed to converge to limit cycle")
Beispiel #9
0
    def computeJacobians(self, x, u, measurmentTags):

        ocp = self.ocp

        sysIn = C.daeIn(x=ocp.x, z=ocp.z, p=ocp.u, t=ocp.t)
        sysOut = C.daeOut(ode=ocp.ode(ocp.x), alg=ocp.alg)
        odeF = C.SXFunction(sysIn, sysOut)
        odeF.init()

        mSX = C.vertcat([
            ocp.variable(measurmentTags[k]).beq
            for k in range(len(measurmentTags))
        ])
        mSXF = C.SXFunction(sysIn, [mSX])
        mSXF.init()

        AS = odeF.jac('x', 'ode')
        BS = odeF.jac('p', 'ode')
        CS = mSXF.jac('x', 0)
        DS = mSXF.jac('p', 0)

        funcJacs = C.SXFunction(sysIn, [AS, BS, CS, DS])
        funcJacs.init()

        funcJacs.setInput(x, 'x')
        funcJacs.setInput(u, 'p')

        funcJacs.evaluate()

        a = funcJacs.getOutput(0)
        b = funcJacs.getOutput(1)
        c = funcJacs.getOutput(2)
        d = funcJacs.getOutput(3)

        return a, b, c, d
Beispiel #10
0
def model():
    """ Create an ODE casadi SXFunction """

    # State Variables
    Y1 = cs.SX.sym("Y1")
    Y2 = cs.SX.sym("Y2")
    Y3 = cs.SX.sym("Y3")

    y = cs.vertcat([Y1, Y2, Y3])

    # Parameters
    c1x1 = cs.SX.sym("c1x1")
    c2 = cs.SX.sym("c2")
    c3x2 = cs.SX.sym("c3x2")
    c4 = cs.SX.sym("c4")
    c5x3 = cs.SX.sym("c5x3")

    symparamset = cs.vertcat([c1x1, c2, c3x2, c4, c5x3])

    # Time
    t = cs.SX.sym("t")

    # ODES
    ode = [[]] * EqCount
    ode[0] = c1x1 * Y2 - c2 * Y1 * Y2 + c3x2 * Y1 - c4 * Y1**2
    ode[1] = -c1x1 * Y2 - c2 * Y1 * Y2 + c5x3 * Y3
    ode[2] = c3x2 * Y1 - c5x3 * Y3
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset), cs.daeOut(ode=ode))

    fn.setOption("name", "Oregonator")

    return fn
Beispiel #11
0
def model():
    #======================================================================
    # Variable Assignments
    #======================================================================
    m1 = cs.ssym("m1")
    m2 = cs.ssym("m2")
    m3 = cs.ssym("m3")

    y = cs.vertcat([m1, m2, m3])

    #======================================================================
    # Parameter Assignments
    #======================================================================
    alpha1 = cs.ssym("alpha1")
    alpha2 = cs.ssym("alpha2")
    alpha3 = cs.ssym("alpha3")
    n = cs.ssym("n")

    symparamset = cs.vertcat([alpha1, alpha2, alpha3, n])

    # Time Variable
    t = cs.ssym("t")

    ode = [[]] * NEQ
    ode[0] = alpha1 / (1. + m2**n) - m1
    ode[1] = alpha2 / (1. + m3**n) - m2
    ode[2] = alpha3 / (1. + m1**n) - m3
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset), cs.daeOut(ode=ode))

    fn.setOption("name", "Small repressilator")

    return fn
Beispiel #12
0
    def solve_ode(self):
        """ Solve the ODE using casadi's CVODES wrapper to ensure that the
        collocated dynamics match the error-controlled dynamics of the ODE """

        self.ts.sort()  # Assert ts is increasing

        f_integrator = cs.SXFunction(
            'ode',
            cs.daeIn(t=self.dxdt.inputExpr(0),
                     x=self.dxdt.inputExpr(1),
                     p=self.dxdt.inputExpr(2)),
            cs.daeOut(ode=self.dxdt.outputExpr(0)))

        integrator = cs.Integrator('int', 'cvodes', f_integrator)
        simulator = cs.Simulator('sim', integrator, self.ts)
        simulator.setInput(self.sol[0], 'x0')
        simulator.setInput(self.var.p_op, 'p')
        simulator.evaluate()
        x_sim = self.sol_sim = np.array(simulator.getOutput()).T

        err = ((self.sol - x_sim).mean(0) / (self.sol.mean(0))).mean()

        if err > 1E-3:
            warn('Collocation does not match ODE Solution: \
                {:.2%} Error'.format(err))
Beispiel #13
0
 def initialize(self, tau=1.0, scale=1.0):
     '''
     Build the dae system of transient CSTR model
     :param tau: space time of CSTR model
     '''
     Ptot = cas.sumRows(self._partP_in)            # Total Pressure
     for i in range(self.ngas):
         self._partP[i] = self._flow[i]/self._Flowtot * Ptot
     self.build_kinetic()
     self.build_rate(scale=1)
     for j in range(self.ngas):
         self._d_flow[j] = self._partP_in[j] / Ptot * self._Flowtot \
                         - self._flow[j]
         for i in range(self.nrxn):
             self._d_flow[j] += self.stoimat[i][j] * self._rate[i]
         # Scale the flow rate
         self._d_flow[j] /= tau
     for j in range(self.nsurf):
         self._d_cover[j] = 0
         for i in range(self.nrxn):
             self._d_cover[j] += self.stoimat[i][j + self.ngas] * self._rate[i]
     self._x = cas.vertcat([self._flow, self._cover])
     # ???
     self._p = cas.vertcat([self._dEa, self._dBE])
     self._u = cas.vertcat([self._partP_in, self._Tem, self._Flowtot])
     self._xdot = cas.vertcat([self._d_flow, self._d_cover])
     #self._dae_ = dict(x=self._x, p=self._p, ode=self._xdot)
     self._dae_ = cas.SXFunction('dae', cas.controldaeIn(x=self._x, p=self._p, u=self._u, t=self._t),
                                cas.daeOut(ode=self._xdot))
Beispiel #14
0
 def initialize(self,
                scale=1.0,
                pump_level=1e5,
                A_V_ratio=1e-3,
                des_scale=1e-3,
                constTem=None):
     '''
     Build the dae system of transient CSTR model
     :param tau: space time of CSTR model
     '''
     self.pump_ratio = pump_level / float(A_V_ratio)
     self._Tem = self._T0 + self._beta * self._t
     self.build_kinetic(constTem=constTem)
     self.build_rate(scale=1, des_scale=des_scale)
     for j in range(self.ngas):
         self._d_partP[j] = -pump_level * self._partP[j]
         for i in range(self.nrxn):
             self._d_partP[
                 j] += A_V_ratio * self.stoimat[i][j] * self._rate[i]
     for j in range(self.nsurf):
         self._d_cover[j] = 0
         for i in range(self.nrxn):
             self._d_cover[j] += self.stoimat[i][j +
                                                 self.ngas] * self._rate[i]
     self._x = cas.vertcat([self._partP, self._cover])
     self._p = cas.vertcat([self._dEa, self._dBE, self._T0, self._beta])
     self._xdot = cas.vertcat([self._d_partP, self._d_cover])
     self._dae_ = cas.SXFunction("dae",
                                 cas.daeIn(x=self._x, p=self._p, t=self._t),
                                 cas.daeOut(ode=self._xdot))
Beispiel #15
0
def model():
    # Variable Assignments
    X = cs.ssym("X")
    Y = cs.ssym("Y")

    y = cs.vertcat([X,Y]) # vector version of y
    
    # Parameter Assignments
    P  = cs.ssym("P")
    kt = cs.ssym("kt")
    kd = cs.ssym("kd")
    a0 = cs.ssym("a0")
    a1 = cs.ssym("a1")
    a2 = cs.ssym("a2")
        
    symparamset = cs.vertcat([P, kt, kd, a0, a1, a2])
    
    # Time Variable (typically unused for autonomous odes)
    t = cs.ssym("t")
    
    
    ode = [[]]*NEQ
    ode[0] = 1 / (1 + Y**P) - X
    ode[1] = kt*X - kd*Y - Y/(a0 + a1*Y + a2*Y**2)
    ode = cs.vertcat(ode)
    
    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset),
                       cs.daeOut(ode=ode))

    fn.setOption("name","Simple Hysteresis model")
    
    return fn
    def average(self):
        """
        integrate the solution with quadrature to find the average
        species concentration. outputs to self.avg
        """

        ffcn_in = self.model.inputExpr()
        ode = self.model.outputExpr()
        quad = cs.vertcat([ffcn_in[cs.DAE_X], ffcn_in[cs.DAE_X]**2])

        quadmodel = cs.SXFunction(ffcn_in, cs.daeOut(ode=ode[0], quad=quad))

        qint = cs.Integrator('cvodes',quadmodel)
        qint.setOption("abstol"        , self.intoptions['lc_abstol'])
        qint.setOption("reltol"        , self.intoptions['lc_reltol'])
        qint.setOption("max_num_steps" , self.intoptions['lc_maxnumsteps'])
        qint.setOption("tf",self.T)
        qint.init()
        qint.setInput(self.y0, cs.INTEGRATOR_X0)
        qint.setInput(self.param, cs.INTEGRATOR_P)
        qint.evaluate()
        quad_out = qint.output(cs.INTEGRATOR_QF).toArray().squeeze()
        self.avg = quad_out[:self.neq]/self.T
        self.rms = np.sqrt(quad_out[self.neq:]/self.T)
        self.std = np.sqrt(self.rms**2 - self.avg**2)
Beispiel #17
0
def ODEmodel():
    #==================================================================
    #State variable definitions
    #==================================================================
    S1a   = cs.ssym("S1a")
    S2a   = cs.ssym("S2a")
    S1b   = cs.ssym("S1b")
    S2b   = cs.ssym("S2b")
    S1c   = cs.ssym("S1c")
    S2c   = cs.ssym("S2c")
    S1d   = cs.ssym("S1d")
    S2d   = cs.ssym("S2d")
    
    #for Casadi
    y = cs.vertcat([S1a,S2a,S1b,S2b,S1c,S2c,S1d,S2d])
    
    # Time Variable
    t = cs.ssym("t")
    
    
    #===================================================================
    #Parameter definitions
    #===================================================================
    
    p1 = cs.ssym('p1')
    p2 = cs.ssym('p2')
    p3 = cs.ssym('p3')  
    p4 = cs.ssym('p4')
    
    paramset = cs.vertcat([p1,p2,p3,p4])
                        
    
    #===================================================================
    # Model Equations
    #===================================================================
    

    ode = [[]]*EqCount
    
    #Rxns
    ode[0] = (1/p1)*(S1a - (1/3)*S1a**3 - S2a) + couplingstr*(S1b-S1a)
    ode[1] = S1a + p1*S2a
    ode[2] = (1/p1)*(S1b - (1/3)*S1b**3 - S2b) + couplingstr*(S1a-S1b)  + couplingstr*(S1c-S1b)
    ode[3] = S1b + p1*S2b
    ode[4] = (1/p1)*(S1c - (1/3)*S1c**3 - S2c) + couplingstr*(S1b-S1c) + couplingstr*(S1d-S1c)
    ode[5] = S1c + p1*S2c
    ode[6] = (1/p1)*(S1d - (1/3)*S1d**3 - S2d) + couplingstr*(S1c-S1d)
    ode[7] = S1d + p1*S2d
    
    ode = cs.vertcat(ode)
    
    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=paramset),
                       cs.daeOut(ode=ode))
    
    fn.setOption("name","simple")
    
    return fn
Beispiel #18
0
    def _initialize_polynomial_coefs(self):
        """ Setup radau polynomials and initialize the weight factor matricies
        """
        self.col_vars['tau_root'] = cs.collocationPoints(self.d, "radau")

        # Dimensionless time inside one control interval
        tau = cs.SX.sym("tau")

        # For all collocation points
        L = [[]]*(self.d+1)
        for j in range(self.d+1):
            # Construct Lagrange polynomials to get the polynomial basis at the
            # collocation point
            L[j] = 1
            for r in range(self.d+1):
                if r != j:
                    L[j] *= (
                        (tau - self.col_vars['tau_root'][r]) / 
                        (self.col_vars['tau_root'][j] -
                         self.col_vars['tau_root'][r]))

        self.col_vars['lfcn'] = lfcn = cs.SXFunction(
            'lfcn', [tau], [cs.vertcat(L)])

        # Evaluate the polynomial at the final time to get the coefficients of
        # the continuity equation
        # Coefficients of the continuity equation
        self.col_vars['D'] = lfcn([1.0])[0].toArray().squeeze()

        # Evaluate the time derivative of the polynomial at all collocation
        # points to get the coefficients of the continuity equation
        tfcn = lfcn.tangent()

        # Coefficients of the collocation equation
        self.col_vars['C'] = np.zeros((self.d+1, self.d+1))
        for r in range(self.d+1):
            self.col_vars['C'][:,r] = tfcn([self.col_vars['tau_root'][r]]
                                           )[0].toArray().squeeze()

        # Find weights for gaussian quadrature: approximate int_0^1 f(x) by
        # Sum(
        xtau = cs.SX.sym("xtau")

        Phi = [[]] * (self.d+1)

        for j in range(self.d+1):
            tau_f_integrator = cs.SXFunction('ode', cs.daeIn(t=tau, x=xtau),
                                             cs.daeOut(ode=L[j]))
            tau_integrator = cs.Integrator(
                "integrator", "cvodes", tau_f_integrator, {'t0':0., 'tf':1})
            Phi[j] = np.asarray(tau_integrator({'x0' : 0})['xf'])[0][0]

        self.col_vars['Phi'] = np.array(Phi)
        self.col_vars['alpha'] = cs.SX.sym('alpha')
Beispiel #19
0
    def _initialize_polynomial_coefs(self):
        """ Setup radau polynomials and initialize the weight factor matricies
        """
        self.col_vars['tau_root'] = cs.collocationPoints(self.d, "radau")

        # Dimensionless time inside one control interval
        tau = cs.SX.sym("tau")

        # For all collocation points
        L = [[]]*(self.d+1)
        for j in range(self.d+1):
            # Construct Lagrange polynomials to get the polynomial basis at the
            # collocation point
            L[j] = 1
            for r in range(self.d+1):
                if r != j:
                    L[j] *= (
                        (tau - self.col_vars['tau_root'][r]) / 
                        (self.col_vars['tau_root'][j] -
                         self.col_vars['tau_root'][r]))

        self.col_vars['lfcn'] = lfcn = cs.SXFunction(
            'lfcn', [tau], [cs.vertcat(L)])

        # Evaluate the polynomial at the final time to get the coefficients of
        # the continuity equation
        # Coefficients of the continuity equation
        self.col_vars['D'] = lfcn([1.0])[0].toArray().squeeze()

        # Evaluate the time derivative of the polynomial at all collocation
        # points to get the coefficients of the continuity equation
        tfcn = lfcn.tangent()

        # Coefficients of the collocation equation
        self.col_vars['C'] = np.zeros((self.d+1, self.d+1))
        for r in range(self.d+1):
            self.col_vars['C'][:,r] = tfcn([self.col_vars['tau_root'][r]]
                                           )[0].toArray().squeeze()

        # Find weights for gaussian quadrature: approximate int_0^1 f(x) by
        # Sum(
        xtau = cs.SX.sym("xtau")

        Phi = [[]] * (self.d+1)

        for j in range(self.d+1):
            tau_f_integrator = cs.SXFunction('ode', cs.daeIn(t=tau, x=xtau),
                                             cs.daeOut(ode=L[j]))
            tau_integrator = cs.Integrator(
                "integrator", "cvodes", tau_f_integrator, {'t0':0., 'tf':1})
            Phi[j] = np.asarray(tau_integrator({'x0' : 0})['xf'])[0][0]

        self.col_vars['Phi'] = np.array(Phi)
Beispiel #20
0
def model():
    """
    Function for the L-P process model of human circadian rhythms. Calculation
    of phase shifts must be done via the
    """
    #==================================================================
    #setup of symbolics
    #==================================================================
    x = cs.SX.sym("x")
    xc = cs.SX.sym("xc")

    sys = cs.vertcat([x, xc])

    #===================================================================
    #Parameter definitions
    #===================================================================

    mu = cs.SX.sym("mu")
    k = cs.SX.sym("k")
    q = cs.SX.sym("q")
    taux = cs.SX.sym("taux")

    alpha0 = cs.SX.sym("alpha0")
    beta = cs.SX.sym("beta")
    G = cs.SX.sym("G")
    p = cs.SX.sym("p")
    I0 = cs.SX.sym("I0")

    Bhat = cs.SX.sym("B")
    paramset = cs.vertcat([mu, k, q, taux, Bhat])

    # Time
    t = cs.SX.sym("t")

    #===================================================================
    # set up the ode system
    #===================================================================
    def B(bhat):
        return (1 - 0.4 * x) * (1 - 0.4 * xc) * bhat

    ode = [[]] * EqCount  #initializes vector
    ode[0] = (cs.pi / 12) * (xc + mu * (x / 3. + (4 / 3.) * x**3 -
                                        (256 / 105.) * x**7) + B(Bhat))
    ode[1] = (cs.pi / 12) * (q * B(Bhat) * xc -
                             ((24 / (0.99729 * taux))**2 + k * B(Bhat)) * x)
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=sys, p=paramset), cs.daeOut(ode=ode))

    fn.setOption("name", "kronauer_P")

    return fn
Beispiel #21
0
    def sxFun(self):
        self._freeze('sxFun()')
        algRes = None
        if hasattr(self,'_algRes'):
            algRes = self._algRes
        odeRes = self._odeRes

        if isinstance(odeRes,list):
            odeRes = C.veccat(odeRes)
        if isinstance(algRes,list):
            algRes = C.veccat(algRes)
            
        return C.SXFunction( C.daeIn( x=self.xVec(), z=self.zVec(), p=C.veccat([self.uVec(),self.pVec()]), xdot=self.stateDotDummy ),
                             C.daeOut( alg=algRes, ode=odeRes) )
Beispiel #22
0
    def phase_of_point(self, point, error=False, tol=1E-3):
        """ Finds the phase at which the distance from the point to the
        limit cycle is minimized. phi=0 corresponds to the definition of
        y0, returns the phase and the minimum distance to the limit
        cycle """

        point = np.asarray(point)
        for i in xrange(100):
            dist = cs.ssym("dist")
            x = self.model.inputSX(cs.DAE_X)
            ode = self.model.outputSX()
            dist_ode = cs.sumAll(2*(x - point)*ode)

            cat_x   = cs.vertcat([x, dist])
            cat_ode = cs.vertcat([ode, dist_ode])

            dist_model = cs.SXFunction(
                cs.daeIn(t=self.model.inputSX(cs.DAE_T), x=cat_x,
                         p=self.model.inputSX(cs.DAE_P)),
                cs.daeOut(ode=cat_ode))

            dist_model.setOption("name","distance model")

            dist_0 = ((self.y0[:-1] - point)**2).sum()
            cat_y0 = np.hstack([self.y0[:-1], dist_0, self.y0[-1]])

            roots_class = pBase(dist_model, self.paramset, cat_y0)
            # roots_class.solveBVP()
            roots_class.roots()

            phase = self._t_to_phi(roots_class.Tmin[-1])
            distance = roots_class.Ymin[-1]

            if distance < tol: return phase, distance

            intr = cs.CVodesIntegrator(self.model)
            intr.setOption("abstol", self.intoptions['transabstol'])
            intr.setOption("reltol", self.intoptions['transreltol'])
            intr.setOption("tf", self.y0[-1])
            intr.setOption("max_num_steps",
                           self.intoptions['transmaxnumsteps'])
            intr.setOption("disable_internal_warnings", True)
            intr.init()
            intr.setInput(point, cs.INTEGRATOR_X0)
            intr.setInput(self.paramset, cs.INTEGRATOR_P)
            intr.evaluate()
            point = intr.output().toArray().flatten()

        raise RuntimeError("Point failed to converge to limit cycle")
Beispiel #23
0
    def build_model(self):
        """
        Takes inputs from self.states, self.params and self.odes to
        build a casadi SXFunction model in self.model. Also calculates
        self.NP and self.NEQ
        """

        x = cs.vertcat(self.states)
        p = cs.vertcat(self.params)
        ode = cs.vertcat(self.odes)

        t = cs.ssym('t')
        fn = cs.SXFunction(cs.daeIn(t=t, x=x, p=p), cs.daeOut(ode=ode))

        self.model = fn

        self.NP = len(self.params)
        self.NEQ = len(self.states)
Beispiel #24
0
def model():
    #==================================================================
    #setup of symbolics
    #==================================================================
    x = cs.SX.sym("x")
    y = cs.SX.sym("y")
    
    sys = cs.vertcat([x,y])
    
    #===================================================================
    #Parameter definitions
    #===================================================================
    
    k1  = cs.SX.sym("k1")
    Kd  = cs.SX.sym("Kd")
    P   = cs.SX.sym("P")
    kdx = cs.SX.sym("kdx")
    ksy = cs.SX.sym("ksy")
    kdy = cs.SX.sym("kdy")
    k2  = cs.SX.sym("k2")
    Km  = cs.SX.sym("Km")
    KI  = cs.SX.sym("KI")
    
    paramset = cs.vertcat([k1,Kd,P,kdx,ksy,kdy,k2,Km,KI])
    
    # Time
    t = cs.SX.sym("t")
    
    
    #===================================================================
    # set up the ode system
    #===================================================================
    
    ode = [[]]*EqCount #initializes vector
    ode[0] = k1*(Kd**P)/((Kd**P) + (y**P)) - kdx*x
    ode[1] = ksy*x - kdy*y - k2*y/(Km + y + KI*y**2)
    ode = cs.vertcat(ode)
    
    fn = cs.SXFunction(cs.daeIn(t=t, x=sys, p=paramset),
                        cs.daeOut(ode=ode))
                        
    fn.setOption("name","2state")
    
    return fn
Beispiel #25
0
    def modifiedModel(self):
        """
        Creates a new casadi model with period as a parameter, such that
        the model has an oscillatory period of 1. Necessary for the
        exact determinination of the period and initial conditions
        through the BVP method. (see Wilkins et. al. 2009 SIAM J of Sci
        Comp)
        """

        pSX = self.model.inputSX(cs.DAE_P)
        T = cs.ssym("T")
        pTSX = cs.vertcat([pSX, T])
        
        self.modlT = cs.SXFunction(
            cs.daeIn(t=self.model.inputSX(cs.DAE_T),
                     x=self.model.inputSX(cs.DAE_X), p=pTSX),
            cs.daeOut(ode=cs.vertcat([self.model.outputSX()*T])))

        self.modlT.setOption("name","T-shifted model")
Beispiel #26
0
    def casadiDae(self):
#       I have:
#       0 = fg(xdot,x,z)
#
#       I need dot(x') = f(x',z')
#                    0 = g(x',z')
#
#       let x' = x
#           z' = [z,xdot]
#           dot(x') = xdot
#           0 = g(x',z') = g(x,[z,xdot]) = fg(xdot,x,z)
        self._freezeXzup('casadiDae()')
        f = self.getResidual()

        xdot = C.veccat([self.ddt(name) for name in self.xNames()])
        return C.SXFunction( C.daeIn( x=self.xVec(),
                                      z=C.veccat([self.zVec(),xdot]),
                                      p=C.veccat([self.uVec(),self.pVec()])
                                      ),
                             C.daeOut( alg=f, ode=xdot) )
Beispiel #27
0
def model():
    #======================================================================
    # Variable Assignments
    #======================================================================
    m1 = cs.ssym("m1")
    p1 = cs.ssym("p1")
    m2 = cs.ssym("m2")
    p2 = cs.ssym("p2")
    m3 = cs.ssym("m3")
    p3 = cs.ssym("p3")

    y = cs.vertcat([m1, p1, m2, p2, m3, p3])

    #======================================================================
    # Parameter Assignments
    #======================================================================
    alpha = cs.ssym("alpha")
    alpha0 = cs.ssym("alpha0")
    n = cs.ssym("n")
    beta = cs.ssym("beta")

    symparamset = cs.vertcat([alpha, alpha0, n, beta])

    # Time Variable
    t = cs.ssym("t")

    ode = [[]] * NEQ
    ode[0] = alpha0 + alpha / (1 + p3**n) - m1
    ode[1] = beta * (m1 - p1)
    ode[2] = alpha0 + alpha / (1 + p1**n) - m2
    ode[3] = beta * (m2 - p2)
    ode[4] = alpha0 + alpha / (1 + p2**n) - m3
    ode[5] = beta * (m3 - p3)
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset), cs.daeOut(ode=ode))

    fn.setOption("name", "Tyson 2 State Model")

    return fn
Beispiel #28
0
def model():
    #======================================================================
    # Variable Assignments
    #======================================================================
    X = cs.ssym("X")
    Y = cs.ssym("Y")

    y = cs.vertcat([X,Y])
    
    #======================================================================
    # Parameter Assignments
    #======================================================================
    k1  = cs.ssym("k1")
    Kd  = cs.ssym("Kd")
    P   = cs.ssym("P")
    kdx = cs.ssym("kdx")
    ksy = cs.ssym("ksy")
    kdy = cs.ssym("kdy")
    k2  = cs.ssym("k2")
    Km  = cs.ssym("Km")
    KI  = cs.ssym("KI")
        
    symparamset = cs.vertcat([k1,Kd,P,kdx,ksy,kdy,k2,Km,KI])
    # Time Variable
    t = cs.ssym("t")
    
    
    ode = [[]]*NEQ
    ode[0] = k1*(Kd**P)/((Kd**P) + (Y**P)) - kdx*X
    ode[1] = ksy*X - kdy*Y - k2*Y/(Km + Y + KI*Y**2)
    ode = cs.vertcat(ode)
    
    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset),
                       cs.daeOut(ode=ode))

    fn.setOption("name","Tyson 2 State Model")
    
    return fn
Beispiel #29
0
def ODEModel():
    # Variable Assignments
    X = cs.ssym("X")
    Y = cs.ssym("Y")

    sys = cs.vertcat([X, Y])  # vector version of y

    # Parameter Assignments
    P = cs.ssym("P")
    kt = cs.ssym("kt")
    kd = cs.ssym("kd")
    a0 = cs.ssym("a0")
    a1 = cs.ssym("a1")
    a2 = cs.ssym("a2")
    kdx = cs.ssym("kdx")

    paramset = cs.vertcat([P, kt, kd, a0, a1, a2, kdx])

    #================================================================================
    # Model Equations
    #================================================================================

    ode = [[]] * EqCount

    # MRNA Species
    t = cs.ssym("t")

    ode = [[]] * EqCount
    ode[0] = 1 / (1 + Y**P) - kdx * X
    ode[1] = kt * X - kd * Y - Y / (a0 + a1 * Y + a2 * Y**2)

    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=sys, p=paramset), cs.daeOut(ode=ode))

    fn.setOption("name", "2state")
    return fn
Beispiel #30
0
def model():
    """ Deterministic model """

    # State Variables
    X1 = cs.ssym('X1')
    X2 = cs.ssym('X2')
    X3 = cs.ssym('X3')

    y = cs.vertcat([X1, X2, X3])


    # Parameters
    h = cs.ssym('h')
    a = cs.ssym('a')
    i = cs.ssym('i')
    r = cs.ssym('r')

    symparamset = cs.vertcat([h, a, i, r])


    # Time
    t = cs.ssym('t')


    # ODES
    ode = [[]]*NEQ
    ode[0] = 1./(1. + X3**h) - a*X1
    ode[1] = X1 - i*X2
    ode[2] = X2 - r*X3
    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=symparamset),
                       cs.daeOut(ode=ode))

    fn.setOption("name", "Goodwin")
    
    return fn
Beispiel #31
0
    def checkStability(self, x, u):

        stateScaling = self.stateScaling
        algStateScaling = self.algStateScaling
        controlScaling = self.controlScaling
        ocp = self.ocp

        odeS = C.substitute(
            ocp.ode(ocp.x), C.vertcat([ocp.x, ocp.z, ocp.u]),
            C.vertcat([
                stateScaling * ocp.x, algStateScaling * ocp.z,
                controlScaling * ocp.u
            ])) / stateScaling
        algS = C.substitute(
            ocp.alg, C.vertcat([ocp.x, ocp.z, ocp.u]),
            C.vertcat([
                stateScaling * ocp.x, algStateScaling * ocp.z,
                controlScaling * ocp.u
            ]))

        sysIn = C.daeIn(x=ocp.x, z=ocp.z, p=ocp.u, t=ocp.t)
        sysOut = C.daeOut(ode=odeS, alg=algS)
        odeF = C.SXFunction(sysIn, sysOut)
        odeF.init()

        dG = odeF.jacobian('x', 'ode')
        dG.init()
        dG.setInput(x / stateScaling, 'x')
        dG.setInput(u / controlScaling, 'p')

        dG.evaluate()

        dGdx = dG.getOutput()

        eigVals, eigVecs = scipy.linalg.eig(dGdx)

        return eigVals, dGdx, odeS
Beispiel #32
0
def model():
    # Variable Assignments
    x = cs.ssym("x")
    y = cs.ssym("y")
    z = cs.ssym("z")

    symy = cs.vertcat([x, y, z])

    # Parameter Assignments
    b = cs.ssym("b")
    d1 = cs.ssym("d1")
    d2 = cs.ssym("d2")
    d3 = cs.ssym("d3")
    t1 = cs.ssym("t1")
    h = cs.ssym("h")
    V = cs.ssym("V")
    K = cs.ssym("K")

    symparamset = cs.vertcat([b, d1, d2, d3, t1, h, V, K])
    # Time Variable
    t = cs.ssym("t")

    ode = [[]] * NEQ

    #    /*  mRNA of per */
    ode[0] = V / (1 + (z / K)**h) - d1 * x
    ode[1] = b * x - (d2 + t1) * y
    ode[2] = t1 * y - d3 * z

    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=symy, p=symparamset),
                       cs.daeOut(ode=ode))

    fn.setOption("name", "Reischl Model")

    return fn
Beispiel #33
0
    def solve_ode(self):
        """ Solve the ODE using casadi's CVODES wrapper to ensure that the
        collocated dynamics match the error-controlled dynamics of the ODE """

        f_integrator = cs.SXFunction(
            'ode',
            cs.daeIn(t=self.model.inputExpr(0),
                     x=self.model.inputExpr(1),
                     p=self.model.inputExpr(2)),
            cs.daeOut(ode=self.model.outputExpr(0)))

        integrator = cs.Integrator('int', 'cvodes', f_integrator)
        simulator = cs.Simulator('sim', integrator, self._tgrid)
        simulator.setInput(self._output['x_opt'][0], 'x0')
        simulator.setInput(self._output['p_opt'], 'p')
        simulator.evaluate()
        x_sim = self._output['x_sim'] = np.array(simulator.getOutput()).T

        err = ((self._output['x_opt'] - x_sim).mean(0) /
               (self._output['x_opt'].mean(0))).mean()

        if err > 1E-3:
            warn('Collocation does not match ODE Solution: \
                {:.2f}% Error'.format(100 * err))
def ODEmodel():
    #==================================================================
    #State variable definitions
    #==================================================================
    M    = cs.ssym("M")
    Pc   = cs.ssym("Pc")
    Pn   = cs.ssym("Pn")
    
    #for Casadi
    y = cs.vertcat([M, Pc, Pn])
    
    # Time Variable
    t = cs.ssym("t")
    
    
    #===================================================================
    #Parameter definitions
    #===================================================================
    
    vs0 = cs.ssym('vs0')
    light = cs.ssym('light')
    alocal = cs.ssym('alocal')
    couplingStrength = cs.ssym('couplingStrength')
    n = cs.ssym('n')
    vm = cs.ssym('vm')
    k1 = cs.ssym('k1')
    km = cs.ssym('km')
    ks = cs.ssym('ks')
    vd = cs.ssym('vd')
    kd = cs.ssym('kd')
    k1_ = cs.ssym('k1_')
    k2_ = cs.ssym('k2_')    

    paramset = cs.vertcat([vs0, light, alocal, couplingStrength,
                           n,   vm,    k1,     km, 
                           ks,  vd,    kd,     k1_, 
                           k2_])
                        
    
    #===================================================================
    # Model Equations
    #===================================================================
    
    ode = [[]]*EqCount

    def pm_prod(Pn, K, v0, n, light, a, M, Mi):
        vs = v0+light+a*(M-Mi)
        return (vs*K**n)/(K**n + Pn**n)
    
    def pm_deg(M, Km, vm):
        return vm*M/(Km+M)
    
    def Pc_prod(ks, M):
        return ks*M
    
    def Pc_deg(Pc, K, v):
        return v*Pc/(K+Pc)
    
    def Pc_comp(k1,k2,Pc,Pn):
        return -k1*Pc + k2*Pn
    
    def Pn_comp(k1,k2,Pc,Pn):
        return k1*Pc - k1*Pn

    #Rxns
    ode[0] = (pm_prod(Pn, k1, vs0, n, light, alocal, M, M) - pm_deg(M,km,vm))
    ode[1] = Pc_prod(ks,M) - Pc_deg(Pc,kd,vd) + Pc_comp(k1_,k2_,Pc,Pn)
    ode[2] = Pn_comp(k1_,k2_,Pc,Pn)

    ode = cs.vertcat(ode)
    
    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=paramset),
                       cs.daeOut(ode=ode))
    
    fn.setOption("name","SDS16")
    
    return fn
                                                   consList=['net.w1sim.z1', 'net.w2sim.z1', 'net.p.z'])

uOpt, xOpt, zOpt, yOpt = daeModel.findOptimalPoint(u0, x0=x0, z0=z0, simCount=0,
                                                   consList=['net.w1.z1', 'net.w2.z1', 'net.p.z'])
uOptSim, xOptSim, zOptSim, yOptSim = daeModelSim.findOptimalPoint(u0Sim, x0=x0Sim, z0=z0Sim, simCount=0,
                                                                  consList=['net.w1sim.z1', 'net.w2sim.z1', 'net.p.z'])

print 'uOpt:', uOpt
print 'yOpt:', yOpt

sys.stdout.flush()

## jacobian calculation Test

sysIn = ca.daeIn(x=ocp.x, z=ocp.z, p=ocp.u, t=ocp.t)
sysOut = ca.daeOut(ode=ocp.ode(ocp.x), alg=ocp.alg)
odeF = ca.SXFunction(sysIn, sysOut)
odeF.init()

measTags = ['Obj']
mSX = ca.vertcat([ocp.variable(measTags[k]).beq for k in range(len(measTags))])
mSXF = ca.SXFunction(sysIn, [mSX])
mSXF.init()

AS = odeF.jac('x', 'ode')
BS = odeF.jac('p', 'ode')
CS = mSXF.jac('x', 0)
DS = mSXF.jac('p', 0)

funcJacs = ca.SXFunction(sysIn, [AS, BS, CS, DS])
funcJacs.init()
Beispiel #36
0
    def run_simulation(self, \
        x0 = None, tsim = None, usim = None, psim = None, method = "rk"):

        r'''
        :param x0: initial value for the states
                   :math:`x_0 \in \mathbb{R}^{n_x}`
        :type x0: list, numpy,ndarray, casadi.DMatrix

        :param tsim: optional, switching time points for the controls
                    :math:`t_{sim} \in \mathbb{R}^{L}` to be used for the
                    simulation
        :type tsim: list, numpy,ndarray, casadi.DMatrix        

        :param usim: optional, control values 
                     :math:`u_{sim} \in \mathbb{R}^{n_u \times L}`
                     to be used for the simulation
        :type usim: list, numpy,ndarray, casadi.DMatrix   

        :param psim: optional, parameter set 
                     :math:`p_{sim} \in \mathbb{R}^{n_p}`
                     to be used for the simulation
        :type psim: list, numpy,ndarray, casadi.DMatrix 

        :param method: optional, CasADi integrator to be used for the
                       simulation
        :type method: str

        This function performs a simulation of the system for a given
        parameter set :math:`p_{sim}`, starting from a user-provided initial
        value for the states :math:`x_0`. If the argument ``psim`` is not
        specified, the estimated parameter set :math:`\hat{p}` is used.
        For this, a parameter
        estimation using :func:`run_parameter_estimation()` has to be
        done beforehand, of course.

        By default, the switching time points for
        the controls :math:`t_u` and the corresponding controls 
        :math:`u_N` will be used for simulation. If desired, other time points
        :math:`t_{sim}` and corresponding controls :math:`u_{sim}`
        can be passed to the function.

        For the moment, the function can only be used for systems of type
        :class:`pecas.systems.ExplODE`.

        '''

        intro.pecas_intro()
        print('\n' + 27 * '-' + \
            ' PECas system simulation ' + 26 * '-')
        print('\nPerforming system simulation, this might take some time ...') 

        if not type(self.pesetup.system) is systems.ExplODE:

            raise NotImplementedError("Until now, this function can only " + \
                "be used for systems of type ExplODE.")


        if x0 == None:

            raise ValueError("You have to provide an initial value x0 " + \
                "to run the simulation.")


        x0 = np.squeeze(np.asarray(x0))

        if np.atleast_1d(x0).shape[0] != self.pesetup.nx:

            raise ValueError("Wrong dimension for initial value x0.")


        if tsim == None:

            tsim = self.pesetup.tu


        if usim == None:

            usim = self.pesetup.uN


        if psim == None:

            try:

                psim = self.phat

            except AttributeError:

                errmsg = '''
You have to either perform a parameter estimation beforehand to obtain a
parameter set that can be used for simulation, or you have to provide a
parameter set in the argument psim.
'''
                raise AttributeError(errmsg)

        else:

            if not np.atleast_1d(np.squeeze(psim)).shape[0] == self.pesetup.np:

                raise ValueError("Wrong dimension for parameter set psim.")


        fp = ca.MXFunction("fp", \
            [self.pesetup.system.t, self.pesetup.system.u, \
            self.pesetup.system.x, self.pesetup.system.eps_e, \
            self.pesetup.system.eps_u, self.pesetup.system.p], \
            [self.pesetup.system.f])

        fpeval = fp([\
            self.pesetup.system.t, self.pesetup.system.u, \
            self.pesetup.system.x, np.zeros(self.pesetup.neps_e), \
            np.zeros(self.pesetup.neps_u), psim])[0]

        fsim = ca.MXFunction("fsim", \
            ca.daeIn(t = self.pesetup.system.t, \
                x = self.pesetup.system.x, \
                p = self.pesetup.system.u), \
            ca.daeOut(ode = fpeval))


        Xsim = []
        Xsim.append(x0)

        u0 = ca.DMatrix()

        for k,e in enumerate(tsim[:-1]):

            try:

                integrator = ca.Integrator("integrator", method, \
                    fsim, {"t0": e, "tf": tsim[k+1]})

            except RuntimeError as err:

                errmsg = '''
It seems like you want to use an integration method that is not currently
supported by CasADi. Please refer to the CasADi documentation for a list
of supported integrators, or use the default RK4-method by not setting the
method-argument of the function.
'''
                raise RuntimeError(errmsg)


            if not self.pesetup.nu == 0:

                u0 = usim[:,k]


            Xk_end = itemgetter('xf')(integrator({'x0':x0,'p':u0}))

            Xsim.append(Xk_end)
            x0 = Xk_end


        self.Xsim = ca.horzcat(Xsim)

        print( \
'''System simulation finished.''')
Beispiel #37
0
def model():

    #==================================================================
    #setup of symbolics
    #==================================================================
    p   = cs.SX.sym("p")
    c1  = cs.SX.sym("c1")
    c2  = cs.SX.sym("c2")
    P   = cs.SX.sym("P")
    C1  = cs.SX.sym("C1")
    C2  = cs.SX.sym("C2")
    C1N = cs.SX.sym("C1N")
    C2N = cs.SX.sym("C2N")
    rev = cs.SX.sym("rev")
    ror = cs.SX.sym("ror")
    REV = cs.SX.sym("REV")
    ROR = cs.SX.sym("ROR")
    b = cs.SX.sym("b")
    B = cs.SX.sym("B")
    
    y = cs.vertcat([p, c1, c2, ror, rev, P, C1, C2, ROR, REV, b, B, C1N, C2N])
    
    # Time Variable
    t = cs.SX.sym("t")
    
    
    #===================================================================
    #Parameter definitions
    #===================================================================
    vtp    = cs.SX.sym("vtp")
    ktp = cs.SX.sym("ktp")
    kb = cs.SX.sym("kb")
    vdp = cs.SX.sym("vdp")
    kdp = cs.SX.sym("kdp")    
    
    vtc1   = cs.SX.sym("vtc1")
    vtc2   = cs.SX.sym("vtc2")
    ktc = cs.SX.sym("ktc")
    vdc1 = cs.SX.sym("vdc1")
    vdc2 = cs.SX.sym("vdc2")    
    kdc = cs.SX.sym("kdc")
    
    vtror = cs.SX.sym("vtror")
    ktror = cs.SX.sym("ktror")
    vdror = cs.SX.sym("vdror")
    kdror = cs.SX.sym("kdror")
    
    vtrev = cs.SX.sym("vtrev")
    ktrev = cs.SX.sym("ktrev")
    vdrev = cs.SX.sym("vdrev")
    kdrev = cs.SX.sym("kdrev")

    klp = cs.SX.sym("klp")
    vdP = cs.SX.sym("vdP")
    kdP = cs.SX.sym("kdP")
    vaCP = cs.SX.sym("vaCP")
    vdCP = cs.SX.sym("vdCP")

    klc = cs.SX.sym("klc") #do we need this or are we assuming removal here for simplicity
    vdC1 = cs.SX.sym("vdC1")
    kdC = cs.SX.sym("kdC")    
    vdC2 = cs.SX.sym("vdC2")
    
    klror = cs.SX.sym("klror")
    vdROR = cs.SX.sym("vdROR")
    kdROR = cs.SX.sym("kdROR") 
    
    klrev = cs.SX.sym("klrev")
    vdREV = cs.SX.sym("vdREV")
    kdREV = cs.SX.sym("kdREV") 
    
    vxROR = cs.SX.sym("vxROR")
    vxREV = cs.SX.sym("vxREV")
    kxREV = cs.SX.sym("kxREV")
    kxROR = cs.SX.sym("kxROR")
    vdb = cs.SX.sym("vdb")
    kdb = cs.SX.sym("kdb")

    klb = cs.SX.sym("klb")
    vdB = cs.SX.sym("vdB")
    kdB = cs.SX.sym("kdB")

    vdC1N = cs.SX.sym("vdC1N")
    kdCP = cs.SX.sym("kdCP")
    vdC2N = cs.SX.sym("vdC2N")    
    
    paramset = cs.vertcat([vtp, ktp, kb, vdp, kdp, #0-4
                           vtc1, vtc2, ktc, vdc1, vdc2, kdc, #5-10
                           vtror, ktror, vdror, kdror, #11-14
                           vtrev, ktrev, vdrev, kdrev, #15-18
                           klp, vdP, kdP, vaCP, vdCP, #19-23
                           vdC1, kdC, vdC2, #klc removed from front, #24-26
                           klror, vdROR, kdROR, #27-29
                           klrev, vdREV, kdREV, #30-32
                           vxROR, vxREV, kxREV, kxROR, vdb, kdb, #33-38
                           klb, vdB, kdB, #39-41
                           vdC1N, kdCP, vdC2N]) #42-44
    
    # Model Equations
    ode = [[]]*EqCount
      
    
    def txnE(vmax,km, kbc, dact1, dact2, Bc):
        return vmax/(km + (kbc/Bc) + dact1 + dact2)
        #return vmax/(km + (dact1 + dact2)**3)
    
    def txl(mrna,kt):
        return kt*mrna
    
    def MMdeg(species,vmax,km):
        return -vmax*(species)/(km+species)
        
    def cxMMdeg(species1,species2,vmax,km):
        return -vmax*(species1)/(km + species1 + species2)
        
    def cnrate(s1,s2,cmplx,ka,kd):
        # positive for reacting species, negative for complex
        return -ka*s1*s2 + kd*cmplx
        
    def txnb(vmax1, vmax2, k1, k2, species1, species2):
        return (vmax1*species1+vmax2)/(1+k1*species1+k2*species2)
    
    ode[0]  = txnE(vtp, ktp, kb, C1N, C2N, B) + MMdeg(p, vdp, kdp)
    ode[1] = txnE(vtc1, ktc, kb, C1N, C2N, B) + MMdeg(c1, vdc1, kdc)
    ode[2] = txnE(vtc2, ktc, kb, C1N, C2N, B) + MMdeg(c2, vdc2, kdc)
    ode[3] = txnE(vtror, ktror, kb, C1N, C2N, B) + MMdeg(ror, vdror, kdror)
    ode[4] = txnE(vtrev, ktrev, kb, C1N, C2N, B) + MMdeg(rev, vdrev, kdrev)
    ode[5]= txl(p, klp)+MMdeg(P, vdP, kdP)+cnrate(P, C1, C1N, vaCP, vdCP)+cnrate(P, C2, C2N, vaCP, vdCP)
    ode[6] = txl(c1, 1)+MMdeg(C1, vdC1, kdC)+cnrate(P, C1, C1N, vaCP, vdCP) #klc replaced with 1
    ode[7] = txl(c2, 1)+MMdeg(C2, vdC2, kdC)+cnrate(P, C2, C2N, vaCP, vdCP) #klc replaced with 1
    ode[8] = txl(ror, klror)+MMdeg(ROR, vdROR, kdROR)
    ode[9] = txl(rev, klrev)+MMdeg(REV, vdREV, kdREV)
    ode[10] = txnb(vxROR, vxREV, kxREV, kxROR, REV, ROR)+MMdeg(b, vdb, kdb)
    ode[11] = txl(b, klb)+MMdeg(B, vdB, kdB)
    ode[12] = cxMMdeg(C1N, C2N, vdC1N, kdCP)-cnrate(C1, P, C1N, vaCP, vdCP)
    ode[13] = cxMMdeg(C2N, C1N, vdC2N, kdCP)-cnrate(C2, P, C2N, vaCP, vdCP)

    ode = cs.vertcat(ode)

    fn = cs.SXFunction(cs.daeIn(t=t, x=y, p=paramset),
                       cs.daeOut(ode=ode))
    
    fn.setOption("name","degmodel")
    
    return fn
Beispiel #38
0
denom = l * ( 4. / 3. - mp * cost*cost / (mc + mp) )
ddtheta = num / denom
ddx = (-u + mp * l * ( x[0] * x[0] * sint - ddtheta * cost ) - fric) / (mc + mp)

x_err = x-x_target
cost_mat = np.diag( [1,1,1,1] )

ode = ca.vertcat([	ddtheta, \
					x[0], \
					ddx, \
					x[2]
				] )

#					ca.mul( [ x_err.T, cost_mat, x_err ] )

dae = ca.SXFunction( 'dae', ca.daeIn( x=x, p=u, t=t ), ca.daeOut( ode=ode ) )

# Create an integrator
opts = { 'tf': tf/nk } # final time
if coll:
  opts[ 'number_of_finite_elements' ] = 5
  opts['interpolation_order'] = 5
  opts['collocation_scheme'] = 'legendre'
  opts['implicit_solver'] = 'kinsol'
  opts['implicit_solver_options'] =  {'linear_solver' : 'csparse'}
  opts['expand_f'] = True
  integrator = ca.Integrator('integrator', 'oldcollocation', dae, opts)
else:
  opts['abstol'] = 1e-1 # tolerance
  opts['reltol'] = 1e-1 # tolerance
#  opts['steps_per_checkpoint'] = 1000
    sigma_y = pl.array([0.01, 0.01, 0.01, 0.01])

    udata = u0 * pl.sin(2 * pl.pi*time_points[:-1])
    udata_noise = udata + sigma_u * pl.randn(*udata.shape)

    simulation_true_parameters.run_system_simulation( \
        x0 = x0, time_points = time_points, udata = udata_noise)

    ydata = simulation_true_parameters.simulation_results.T

    ydata_noise = ydata + sigma_y * pl.randn(*ydata.shape)


    ffcn = ca.MXFunction("ffcn", \
        ca.daeIn(x = x, p = ca.vertcat([u, eps_u, p])), \
        ca.daeOut(ode = f))

    ffcn = ffcn.expand()

    rk4 = ca.Integrator("rk4", "rk", ffcn, {"t0": 0, "tf": dt, \
        "number_of_finite_elements": 1})

    P = ca.MX.sym("P", 3)
    EPS_U = ca.MX.sym("EPS_U", int(N))
    X0 = ca.MX.sym("X0", 4)

    V = ca.vertcat([P, EPS_U, X0])

    x_end = X0
    obj = [x_end - ydata[0,:].T]
Beispiel #40
0
def freeModel(endTimeSteps=None):
    stateNames = [ "x"   # state 0
                 , "y"   # state 1
                 , "z"   # state 2
                 , "e11" # state 3
                 , "e12" # state 4
                 , "e13" # state 5
                 , "e21" # state 6
                 , "e22" # state 7
                 , "e23" # state 8
                 , "e31" # state 9
                 , "e32" # state 10
                 , "e33" # state 11
                 , "dx"  # state 12
                 , "dy"  # state 13
                 , "dz"  # state 14
                 , "w1"  # state 15
                 , "w2"  # state 16
                 , "w3"  # state 17
                 ]
    
    uNames = [ "tc"
             , "aileron"
             , "elevator"
             , "rudder"
#             , 'ddr'
             ]

    pNames = [ "wind_x"
             ]
    
    uSyms = [C.ssym(name) for name in uNames]
    uDict = dict(zip(uNames,uSyms))
    uVec = C.veccat( uSyms )

    pSyms = [C.ssym(name) for name in pNames]
    pDict = dict(zip(pNames,pSyms))
    pVec = C.veccat( pSyms )

    stateSyms = [C.ssym(name) for name in stateNames]
    stateDict = dict(zip(stateNames,stateSyms))
    stateVec = C.veccat( stateSyms )

    dx = stateDict['dx']
    dy = stateDict['dy']
    dz = stateDict['dz']

    outputs = {}
    outputs['aileron(deg)']=uDict['aileron']*180/C.pi
    outputs['elevator(deg)']=uDict['elevator']*180/C.pi
    outputs['rudder(deg)']=uDict['rudder']*180/C.pi
    (massMatrix, rhs, dRexp) = modelInteg(stateDict, uDict, pDict, outputs)
    
    zVec = C.solve(massMatrix, rhs)
    
    ddX = zVec[0:3]
    dw = zVec[3:6]

    ode = C.veccat( [ C.veccat([dx,dy,dz])
                    , dRexp.trans().reshape([9,1])
                    , ddX
                    , dw
                    ] )

    
    if endTimeSteps is not None:
        endTime,nSteps = endTimeSteps
        pNames.append("endTime")
        pDict["endTime"] = endTime
        pVec = C.veccat([pVec,endTime])
        ode = ode#/(endTime/(nSteps-1))

    dae = C.SXFunction( C.daeIn( x=stateVec, p=C.veccat([uVec,pVec])),
                        C.daeOut( ode=ode))
    return (dae, {'xVec':stateVec,'xDict':stateDict,'xNames':stateNames,
                  'uVec':uVec,'uNames':uNames,
                  'pVec':pVec,'pNames':pNames}, outputs)
Beispiel #41
0
import matplotlib.pyplot as plt
from operator import itemgetter

nk = 20    # Control discretization
tf = 10.0  # End time

# Declare variables (use scalar graph)
u  = ca.SX.sym("u")    # control
x  = ca.SX.sym("x",2)  # states

# ODE right hand side and quadratures
xdot = ca.vertcat( [(1 - x[1]*x[1])*x[0] - x[1] + u, x[0]] )
qdot = x[0]*x[0] + x[1]*x[1] + u*u

# DAE residual function
dae = ca.SXFunction("dae", ca.daeIn(x=x, p=u), ca.daeOut(ode=xdot, quad=qdot))

# Create an integrator
integrator = ca.Integrator("integrator", "cvodes", dae, {"tf":tf/nk})

# All controls (use matrix graph)
x = ca.MX.sym("x",nk) # nk-by-1 symbolic variable
U = ca.vertsplit(x) # cheaper than x[0], x[1], ...

# The initial state (x_0=0, x_1=1)
X  = ca.MX([0,1])

# Objective function
f = 0

# Build a graph of integrator calls
    def setUp(self):

        self.x = ca.MX.sym("x")
        self.f = ca.MX.sym("f")

        self.dae = ca.MXFunction("dae", ca.daeIn(x=self.x), ca.daeOut(ode=self.f))