Example #1
0
def linearize_dae(model):
    """ 
    Linearize a DAE represented by a pyjmi.jmi.JMUModel object. The DAE is 
    represented by

      F(dx,x,u,w,t) = 0

    and the linearized model is given by

      E*dx = A*x + B*u + F*w + g

    where E, A, B, F and g are constant coefficient matrices. The linearization 
    is done around the current values of the dx, x, u, w, and t values in the 
    JMUModel object.

    The matrices are computed by evaluating Jacobians with the AD package CppAD 
    which provides derivatives with machine precision. (That is, no numerical 
    finite differences are used in the linearization.)
    
    Parameters::
    
        model --
            The jmi.JMUModel object representing the model.

    Returns::
    
        E -- 
            n_eq_F x n_dx matrix corresponding to dF/ddx.
            
        A -- 
            n_eq_F x n_x matrix corresponding to -dF/dx.
            
        B -- 
            n_eq_F x n_u matrix corresponding to -dF/du.
            
        F -- 
            n_eq_F x n_w matrix corresponding to -dF/dw.
            
        g -- 
            n_eq_F x 1 matrix corresponding to F(dx0,x0,u0,w0,t0)
            
        state_names -- 
            Names of the differential variables.
            
        input_names -- 
            Names of the input variables.
            
        algebraic_names -- 
            Names of the algebraic variables.
            
        dx0 -- 
            Derivative variable vector around which the linearization is done.
            
        x0 -- 
            Differential variable vector around which the linearization is done.
            
        u0 -- 
            Input variable vector around which the linearization is done.
            
        w0 -- 
            Algebraic variable vector around which the linearization is done.
            
        t0 -- 
            Time for which the linearization is done.  

    Limitations::
    
        Currently only dense matrix format supported. Sparse format to be added.
    """
    n_x = model._n_real_x.value
    n_u = model._n_real_u.value
    n_w = model._n_real_w.value
    n_z = model._n_z.value
    n_eq = n_x+n_w
    sc = model.jmimodel.get_variable_scaling_factors()
    
    if model.jmimodel.get_scaling_method() == jmi.JMI_SCALING_VARIABLES:
        sc_dx = []
        for var in model.get_dx_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_dx.append(var[0])
        sc_dx = N.diag([1.0/sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set(sc_dx))])
        sc_x = []
        for var in model.get_x_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_x.append(var[0])
        sc_x = N.diag([1.0/sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set(sc_x))])
        sc_w = []
        for var in model.get_w_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_w.append(var[0])
        sc_w = N.diag([1.0/sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set(sc_w))])
        sc_u = []
        for var in model.get_u_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_u.append(var[0])
        sc_u = N.diag([1.0/sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set(sc_u))])
    else:
        sc_dx = N.diag([1.0]*n_x)
        sc_x = N.diag([1.0]*n_x)
        sc_w = N.diag([1.0]*n_w)
        sc_u = N.diag([1.0]*n_u)
    #sc_dx = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_dx_variable_names()]))])
    #sc_x  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_x_variable_names()]))])
    #sc_w  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_w_variable_names()]))])
    #sc_u  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_u_variable_names()]))])
    
    E = N.zeros((n_eq*n_x))

    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD,
                          jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_DX,
                          N.ones(n_z,dtype=int),
                          E)
    
    E = N.dot(N.reshape(E,(n_eq,n_x)),sc_dx)
    
    A = N.zeros((n_eq*n_x))
    
    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD,
                          jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_X,
                          N.ones(n_z,dtype=int),
                          A)
    
    A = N.dot(-N.reshape(A,(n_eq,n_x)),sc_x)
    
    B = N.zeros((n_eq*n_u))
    
    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD,
                          jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_U,
                          N.ones(n_z,dtype=int),
                          B)
    
    B = N.dot(-N.reshape(B,(n_eq,n_u)),sc_u)
    
    F = N.zeros((n_eq*n_w))
    
    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD,
                          jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_W,
                          N.ones(n_z,dtype=int),
                          F)
    
    F = N.dot(-N.reshape(F,(n_eq,n_w)),sc_w)
    
    g = N.zeros(n_eq)
    
    model.jmimodel.dae_F(g)
    
    g = -N.transpose(N.matrix(g))

    state_names = model.get_x_variable_names(include_alias=False)
    #state_names = sorted([(v,k) for k,v in state_names.items()])
    state_names = sorted(state_names)
    state_names = [state_names[i][1] for i in range(len(state_names))]
    algebraic_names = model.get_w_variable_names(include_alias=False)
    #algebraic_names = sorted([(v,k) for k,v in algebraic_names.items()])
    algebraic_names = sorted(algebraic_names)
    algebraic_names = [algebraic_names[i][1] for i in range(len(algebraic_names))]
    input_names = model.get_u_variable_names(include_alias=False)
    input_names = sorted(input_names)
    #input_names = sorted([(v,k) for k,v in input_names.items()])
    input_names = [input_names[i][1] for i in range(len(input_names))]

    dx0 = N.zeros(n_x)
    x0 = N.zeros(n_x)
    u0 = N.zeros(n_u)
    w0 = N.zeros(n_w)
    t0 = N.zeros(1)

    dx0[:] = model.jmimodel.get_real_dx()
    x0[:] = model.jmimodel.get_real_x()
    u0[:] = model.jmimodel.get_real_u()
    w0[:] = model.jmimodel.get_real_w()
    t0[:] = model.jmimodel.get_t()
    t0 = t0[0]
    
    return E,A,B,F,g,state_names,input_names,algebraic_names,dx0,x0,u0,w0,t0
Example #2
0
    def __init__(self, model, stat = 0):
        """ 
        Constructor where main data structure is created. 
        
        Initial guesses, lower and upper bounds and linearity information is set 
        for optimized parameters, derivatives, states, inputs and algebraic 
        variables. These values are taken from the XML files created at 
        compilation.
        
        Parameters::
        
            model -- 
                The pyjmi.jmi.JMUModel object.
        """
        self._jmi_init_opt = ct.c_voidp() 
        self._model = model
        self._jmi_model = model.jmimodel

        self._n_p_free = 0
        self._p_free_indices = N.ones(self._n_p_free,dtype=int)

        self._n_p_opt=model.jmimodel.opt_get_n_p_opt()

        # Initialization
        _p_opt_start = N.zeros(self._n_p_opt) 
        _p_free_start = N.zeros(self._n_p_free) # Not supported
        _dx_start = N.zeros(model._n_real_dx.value)
        _x_start = N.zeros(model._n_real_x.value)
        _u_start = N.zeros(model._n_real_u.value)
        _w_start = N.zeros(model._n_real_w.value)
    
        # Bounds
        _p_opt_lb = N.zeros(self._n_p_opt) 
        _p_free_lb = -1.0e20*N.ones(self._n_p_free) # Not supported
        _dx_lb = -1.0e20*N.ones(model._n_real_dx.value)
        _x_lb = -1.0e20*N.ones(model._n_real_x.value)
        _u_lb = -1.0e20*N.ones(model._n_real_u.value)
        _w_lb = -1.0e20*N.ones(model._n_real_w.value)

        _p_opt_ub = N.zeros(self._n_p_opt) 
        _p_free_ub = 1.0e20*N.ones(self._n_p_free)
        _dx_ub = 1.0e20*N.ones(model._n_real_dx.value)
        _x_ub = 1.0e20*N.ones(model._n_real_x.value)
        _u_ub = 1.0e20*N.ones(model._n_real_u.value)
        _w_ub = 1.0e20*N.ones(model._n_real_w.value)


        # Bounds
        _p_opt_lb = -1.0e20*N.ones(self._n_p_opt)
        _dx_lb = -1.0e20*N.ones(model._n_real_dx.value)
        _x_lb = -1.0e20*N.ones(model._n_real_x.value)
        _u_lb = -1.0e20*N.ones(model._n_real_u.value)
        _w_lb = -1.0e20*N.ones(model._n_real_w.value)
        
        _p_opt_ub = 1.0e20*N.ones(self._n_p_opt)
        _dx_ub = 1.0e20*N.ones(model._n_real_dx.value)
        _x_ub = 1.0e20*N.ones(model._n_real_x.value)
        _u_ub = -1.0e20*N.ones(model._n_real_u.value)
        _w_ub = 1.0e20*N.ones(model._n_real_w.value)

        if stat==0:
            self._model._set_xml_start_values(
                _p_opt_start, _dx_start, _x_start, _u_start, _w_start)
        else:
            self._model._set_initial_values(
                _p_opt_start, _dx_start, _x_start, _u_start, _w_start)
        self._model._set_lb_values(_p_opt_lb, _dx_lb, _x_lb, _u_lb, _w_lb)
        self._model._set_ub_values(_p_opt_ub, _dx_ub, _x_ub, _u_ub, _w_ub)

        # Overwrite input values with the ones set in the model: the
        # user may have set these. Should be done for all variables?
        for i in range(len(_u_start)):
            _u_start[i] = self._model.jmimodel.get_real_u()[i]
                    
        _linearity_information_provided = 1;
        _p_opt_lin = N.ones(model._n_p_opt,dtype=int)
        _p_free_lin = N.ones(0,dtype=int)
        _dx_lin = N.ones(model._n_real_dx.value,dtype=int)
        _x_lin = N.ones(model._n_real_x.value,dtype=int)
        _u_lin = N.ones(model._n_real_u.value,dtype=int)        
        _w_lin = N.ones(model._n_real_w.value,dtype=int)
        _dx_tp_lin = N.ones(model._n_real_dx.value*model._n_tp.value,dtype=int)
        _x_tp_lin = N.ones(model._n_real_x.value*model._n_tp.value,dtype=int)
        _u_tp_lin = N.ones(model._n_real_u.value*model._n_tp.value,dtype=int)        
        _w_tp_lin = N.ones(model._n_real_w.value*model._n_tp.value,dtype=int)

        self._model._set_lin_values(_p_opt_lin, _dx_lin, _x_lin, _u_lin, _w_lin, 
                                    _dx_tp_lin, _x_tp_lin, _u_tp_lin, _w_tp_lin)

        # Modify the linearity information in the case of initialization.
        # All states and algebraics with fixed=false end up in the
        # quadratic cost function in the initialization problem and
        # are thus non-linear.

        if stat==0:
            # x: differentiated variables
            fixeds = self._model._xmldoc.get_x_fixed(include_alias=False)
            for fix in fixeds:
                if fix[1] == False:
                    (z_i, ptype) = _translate_value_ref(fix[0])
                    i_x = z_i - self._model._offs_real_x.value
                    _x_lin[i_x] = 0
                            
            # w: algebraic
            fixeds = self._model._xmldoc.get_w_fixed(include_alias=False)
            for fix in fixeds:
                if fix[1] == False:
                    (z_i, ptype) = _translate_value_ref(fix[0])
                    i_w = z_i - self._model._offs_real_w.value
                    _w_lin[i_w] = 0
        
        self._set_typedef_init_opt_new()
#        try:       
        assert model.jmimodel._dll.jmi_init_opt_new(
            byref(self._jmi_init_opt), 
            model.jmimodel._jmi,
            self._n_p_free,self._p_free_indices,
            _p_opt_start, 
            _p_free_start, 
            _dx_start, 
            _x_start,
            _w_start,
            _p_opt_lb, 
            _p_free_lb, 
            _dx_lb, 
            _x_lb,
            _w_lb,
            _p_opt_ub, 
            _p_free_ub, 
            _dx_ub, 
            _x_ub, 
            _w_ub, 
            _linearity_information_provided, 
            _p_opt_lin, 
            _p_free_lin, 
            _dx_lin, 
            _x_lin, 
            _w_lin, 
            JMI_DER_CPPAD,stat) is 0, \
                " jmi_opt_lp_new returned non-zero."
        #        except AttributeError as e:
#             raise JMIException("Can not create NLPInitialization object.")
        assert self._jmi_init_opt.value is not None, \
            "jmi_init_opt struct has not returned correctly."
            
        self._set_nlpInit_typedefs()
Example #3
0
def linearize_dae(model):
    """ 
    Linearize a DAE represented by a pyjmi.jmi.JMUModel object. The DAE is 
    represented by

      F(dx,x,u,w,t) = 0

    and the linearized model is given by

      E*dx = A*x + B*u + F*w + g

    where E, A, B, F and g are constant coefficient matrices. The linearization 
    is done around the current values of the dx, x, u, w, and t values in the 
    JMUModel object.

    The matrices are computed by evaluating Jacobians with the AD package CppAD 
    which provides derivatives with machine precision. (That is, no numerical 
    finite differences are used in the linearization.)
    
    Parameters::
    
        model --
            The jmi.JMUModel object representing the model.

    Returns::
    
        E -- 
            n_eq_F x n_dx matrix corresponding to dF/ddx.
            
        A -- 
            n_eq_F x n_x matrix corresponding to -dF/dx.
            
        B -- 
            n_eq_F x n_u matrix corresponding to -dF/du.
            
        F -- 
            n_eq_F x n_w matrix corresponding to -dF/dw.
            
        g -- 
            n_eq_F x 1 matrix corresponding to F(dx0,x0,u0,w0,t0)
            
        state_names -- 
            Names of the differential variables.
            
        input_names -- 
            Names of the input variables.
            
        algebraic_names -- 
            Names of the algebraic variables.
            
        dx0 -- 
            Derivative variable vector around which the linearization is done.
            
        x0 -- 
            Differential variable vector around which the linearization is done.
            
        u0 -- 
            Input variable vector around which the linearization is done.
            
        w0 -- 
            Algebraic variable vector around which the linearization is done.
            
        t0 -- 
            Time for which the linearization is done.  

    Limitations::
    
        Currently only dense matrix format supported. Sparse format to be added.
    """
    n_x = model._n_real_x.value
    n_u = model._n_real_u.value
    n_w = model._n_real_w.value
    n_z = model._n_z.value
    n_eq = n_x + n_w
    sc = model.jmimodel.get_variable_scaling_factors()

    if model.jmimodel.get_scaling_method() == jmi.JMI_SCALING_VARIABLES:
        sc_dx = []
        for var in model.get_dx_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_dx.append(var[0])
        sc_dx = N.diag([
            1.0 / sc[jmi._translate_value_ref(ref)[0]]
            for ref in sorted(set(sc_dx))
        ])
        sc_x = []
        for var in model.get_x_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_x.append(var[0])
        sc_x = N.diag([
            1.0 / sc[jmi._translate_value_ref(ref)[0]]
            for ref in sorted(set(sc_x))
        ])
        sc_w = []
        for var in model.get_w_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_w.append(var[0])
        sc_w = N.diag([
            1.0 / sc[jmi._translate_value_ref(ref)[0]]
            for ref in sorted(set(sc_w))
        ])
        sc_u = []
        for var in model.get_u_variable_names():
            if not model._get_XMLDoc().is_alias(var[1]):
                sc_u.append(var[0])
        sc_u = N.diag([
            1.0 / sc[jmi._translate_value_ref(ref)[0]]
            for ref in sorted(set(sc_u))
        ])
    else:
        sc_dx = N.diag([1.0] * n_x)
        sc_x = N.diag([1.0] * n_x)
        sc_w = N.diag([1.0] * n_w)
        sc_u = N.diag([1.0] * n_u)
    #sc_dx = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_dx_variable_names()]))])
    #sc_x  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_x_variable_names()]))])
    #sc_w  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_w_variable_names()]))])
    #sc_u  = N.diag([sc[jmi._translate_value_ref(ref)[0]] for ref in sorted(set([var[0] for var in model.get_u_variable_names()]))])

    E = N.zeros((n_eq * n_x))

    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD, jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_DX, N.ones(n_z, dtype=int), E)

    E = N.dot(N.reshape(E, (n_eq, n_x)), sc_dx)

    A = N.zeros((n_eq * n_x))

    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD, jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_X, N.ones(n_z, dtype=int), A)

    A = N.dot(-N.reshape(A, (n_eq, n_x)), sc_x)

    B = N.zeros((n_eq * n_u))

    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD, jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_U, N.ones(n_z, dtype=int), B)

    B = N.dot(-N.reshape(B, (n_eq, n_u)), sc_u)

    F = N.zeros((n_eq * n_w))

    model.jmimodel.dae_dF(jmi.JMI_DER_CPPAD, jmi.JMI_DER_DENSE_ROW_MAJOR,
                          jmi.JMI_DER_W, N.ones(n_z, dtype=int), F)

    F = N.dot(-N.reshape(F, (n_eq, n_w)), sc_w)

    g = N.zeros(n_eq)

    model.jmimodel.dae_F(g)

    g = -N.transpose(N.matrix(g))

    state_names = model.get_x_variable_names(include_alias=False)
    #state_names = sorted([(v,k) for k,v in state_names.items()])
    state_names = sorted(state_names)
    state_names = [state_names[i][1] for i in range(len(state_names))]
    algebraic_names = model.get_w_variable_names(include_alias=False)
    #algebraic_names = sorted([(v,k) for k,v in algebraic_names.items()])
    algebraic_names = sorted(algebraic_names)
    algebraic_names = [
        algebraic_names[i][1] for i in range(len(algebraic_names))
    ]
    input_names = model.get_u_variable_names(include_alias=False)
    input_names = sorted(input_names)
    #input_names = sorted([(v,k) for k,v in input_names.items()])
    input_names = [input_names[i][1] for i in range(len(input_names))]

    dx0 = N.zeros(n_x)
    x0 = N.zeros(n_x)
    u0 = N.zeros(n_u)
    w0 = N.zeros(n_w)
    t0 = N.zeros(1)

    dx0[:] = model.jmimodel.get_real_dx()
    x0[:] = model.jmimodel.get_real_x()
    u0[:] = model.jmimodel.get_real_u()
    w0[:] = model.jmimodel.get_real_w()
    t0[:] = model.jmimodel.get_t()
    t0 = t0[0]

    return E, A, B, F, g, state_names, input_names, algebraic_names, dx0, x0, u0, w0, t0
Example #4
0
    def __init__(self, model, input=None, result_file_name='', start_time=0.0):
        """
        Sets the initial values.
        """
        if input != None and not isinstance(input[0],list):
            input = ([input[0]], input[1])
        
        self._model = model
        self.input = input
        
        #Set start time to the model
        self._model.t = start_time
        
        self.t0 = start_time
        self.y0 = N.append(self._model.real_x,self._model.real_w)
        self.yd0 = N.append(self._model.real_dx,[0]*len(self._model.real_w))
        #Sets the algebraic components of the model
        self.algvar = [1.0]*len(self._model.real_x) + [0.0]*len(self._model.real_w) 
        
        #Used for determine if there are discontinuities
        [f_nbr, g_nbr] = self._model.jmimodel.dae_get_sizes() 
        [f0_nbr, f1_nbr, fp_nbr, g0_nbr] = self._model.jmimodel.init_get_sizes()
        
        if g_nbr > 0:
            raise JMIModel_Exception("Hybrid models with event functions are currently not supported.")
        
        if f_nbr == 0:
            raise Exception("The JMI framework does not support 'simulation' of 0-dimension systems. Use the FMI framework.")

        
        #Construct the input nominal vector
        if self.input != None:
            self._input_nominal = N.array([1.0]*len(self.input[0]))
            if (self._model.get_scaling_method() == jmi.JMI_SCALING_VARIABLES):
                for i in range(len(self.input[0])):
                    val_ref = self._model._xmldoc.get_value_reference(self.input[0][i])
                    for j, nom in enumerate(self._model._xmldoc.get_u_nominal()):
                        if val_ref == nom[0]:
                            if nom[1] == None:
                                self._input_nominal[i] = 1.0
                            else:
                                self._input_nominal[i] = nom[1]
        
        if self._model.has_cppad_derivatives():
            self.jac = self.j #Activates the jacobian
        
        #Default values
        self.export = ResultWriterDymolaSensitivity(model)
        
        #Determine the result file name
        if result_file_name == '':
            self.result_file_name = model.get_name()+'_result.txt'
        else:
            self.result_file_name = result_file_name
        
        #Internal values
        self._parameter_names = [
            name[1] for name in self._model.get_p_opt_variable_names()]
        self._parameter_valref = [
            name[0] for name in self._model.get_p_opt_variable_names()]
        self._parameter_pos = [jmi._translate_value_ref(x)[0] for x in self._parameter_valref]
        self._sens_matrix = [] #Sensitivity matrix
        self._f_nbr = f_nbr #Number of equations
        self._f0_nbr = f0_nbr #Number of initial equations
        self._g_nbr = g_nbr #Number of event indicatiors
        self._x_nbr = len(self._model.real_x) #Number of differentiated
        self._w_nbr = len(self._model.real_w) #Number of algebraic
        self._dx_nbr = len(self._model.real_dx) #Number of derivatives
        self._p_nbr = len(self._parameter_names) #Number of parameters
        self._write_header = True
        self._input_names = [k[1] for k in sorted(model.get_u_variable_names())]

        #Used for logging
        self._logLevel = logging.CRITICAL
        self._log = createLogger(model, self._logLevel)
        
        #Set the start values to the parameters.
        if self._parameter_names:
            self.p0 = N.array([])
            for i,n in enumerate(self._parameter_names):
                self.p0 = N.append(self.p0, self._model.z[self._parameter_pos[i]])
                self._sens_matrix += [[]] 
            self.pbar = N.array([N.abs(x) if N.abs(x) > 0 else 1.0 for x in self.p0])
            self._p_nbr = len(self.p0) #Number of parameters
        else:
            self._p_nbr = 0
            
        #Initial values for sensitivity
        if self._p_nbr > 0:
            self.yS0 = self._sens_init()

        self._log.debug('Number of parameters: ' +str(self._p_nbr))
Example #5
0
    def __init__(self, model, stat=0):
        """ 
        Constructor where main data structure is created. 
        
        Initial guesses, lower and upper bounds and linearity information is set 
        for optimized parameters, derivatives, states, inputs and algebraic 
        variables. These values are taken from the XML files created at 
        compilation.
        
        Parameters::
        
            model -- 
                The pyjmi.jmi.JMUModel object.
        """
        self._jmi_init_opt = ct.c_voidp()
        self._model = model
        self._jmi_model = model.jmimodel

        self._n_p_free = 0
        self._p_free_indices = N.ones(self._n_p_free, dtype=int)

        self._n_p_opt = model.jmimodel.opt_get_n_p_opt()

        # Initialization
        _p_opt_start = N.zeros(self._n_p_opt)
        _p_free_start = N.zeros(self._n_p_free)  # Not supported
        _dx_start = N.zeros(model._n_real_dx.value)
        _x_start = N.zeros(model._n_real_x.value)
        _u_start = N.zeros(model._n_real_u.value)
        _w_start = N.zeros(model._n_real_w.value)

        # Bounds
        _p_opt_lb = N.zeros(self._n_p_opt)
        _p_free_lb = -1.0e20 * N.ones(self._n_p_free)  # Not supported
        _dx_lb = -1.0e20 * N.ones(model._n_real_dx.value)
        _x_lb = -1.0e20 * N.ones(model._n_real_x.value)
        _u_lb = -1.0e20 * N.ones(model._n_real_u.value)
        _w_lb = -1.0e20 * N.ones(model._n_real_w.value)

        _p_opt_ub = N.zeros(self._n_p_opt)
        _p_free_ub = 1.0e20 * N.ones(self._n_p_free)
        _dx_ub = 1.0e20 * N.ones(model._n_real_dx.value)
        _x_ub = 1.0e20 * N.ones(model._n_real_x.value)
        _u_ub = 1.0e20 * N.ones(model._n_real_u.value)
        _w_ub = 1.0e20 * N.ones(model._n_real_w.value)

        # Bounds
        _p_opt_lb = -1.0e20 * N.ones(self._n_p_opt)
        _dx_lb = -1.0e20 * N.ones(model._n_real_dx.value)
        _x_lb = -1.0e20 * N.ones(model._n_real_x.value)
        _u_lb = -1.0e20 * N.ones(model._n_real_u.value)
        _w_lb = -1.0e20 * N.ones(model._n_real_w.value)

        _p_opt_ub = 1.0e20 * N.ones(self._n_p_opt)
        _dx_ub = 1.0e20 * N.ones(model._n_real_dx.value)
        _x_ub = 1.0e20 * N.ones(model._n_real_x.value)
        _u_ub = -1.0e20 * N.ones(model._n_real_u.value)
        _w_ub = 1.0e20 * N.ones(model._n_real_w.value)

        if stat == 0:
            self._model._set_xml_start_values(_p_opt_start, _dx_start,
                                              _x_start, _u_start, _w_start)
        else:
            self._model._set_initial_values(_p_opt_start, _dx_start, _x_start,
                                            _u_start, _w_start)
        self._model._set_lb_values(_p_opt_lb, _dx_lb, _x_lb, _u_lb, _w_lb)
        self._model._set_ub_values(_p_opt_ub, _dx_ub, _x_ub, _u_ub, _w_ub)

        # Overwrite input values with the ones set in the model: the
        # user may have set these. Should be done for all variables?
        for i in range(len(_u_start)):
            _u_start[i] = self._model.jmimodel.get_real_u()[i]

        _linearity_information_provided = 1
        _p_opt_lin = N.ones(model._n_p_opt, dtype=int)
        _p_free_lin = N.ones(0, dtype=int)
        _dx_lin = N.ones(model._n_real_dx.value, dtype=int)
        _x_lin = N.ones(model._n_real_x.value, dtype=int)
        _u_lin = N.ones(model._n_real_u.value, dtype=int)
        _w_lin = N.ones(model._n_real_w.value, dtype=int)
        _dx_tp_lin = N.ones(model._n_real_dx.value * model._n_tp.value,
                            dtype=int)
        _x_tp_lin = N.ones(model._n_real_x.value * model._n_tp.value,
                           dtype=int)
        _u_tp_lin = N.ones(model._n_real_u.value * model._n_tp.value,
                           dtype=int)
        _w_tp_lin = N.ones(model._n_real_w.value * model._n_tp.value,
                           dtype=int)

        self._model._set_lin_values(_p_opt_lin, _dx_lin, _x_lin, _u_lin,
                                    _w_lin, _dx_tp_lin, _x_tp_lin, _u_tp_lin,
                                    _w_tp_lin)

        # Modify the linearity information in the case of initialization.
        # All states and algebraics with fixed=false end up in the
        # quadratic cost function in the initialization problem and
        # are thus non-linear.

        if stat == 0:
            # x: differentiated variables
            fixeds = self._model._xmldoc.get_x_fixed(include_alias=False)
            for fix in fixeds:
                if fix[1] == False:
                    (z_i, ptype) = _translate_value_ref(fix[0])
                    i_x = z_i - self._model._offs_real_x.value
                    _x_lin[i_x] = 0

            # w: algebraic
            fixeds = self._model._xmldoc.get_w_fixed(include_alias=False)
            for fix in fixeds:
                if fix[1] == False:
                    (z_i, ptype) = _translate_value_ref(fix[0])
                    i_w = z_i - self._model._offs_real_w.value
                    _w_lin[i_w] = 0

        self._set_typedef_init_opt_new()
        #        try:
        assert model.jmimodel._dll.jmi_init_opt_new(
            byref(self._jmi_init_opt),
            model.jmimodel._jmi,
            self._n_p_free,self._p_free_indices,
            _p_opt_start,
            _p_free_start,
            _dx_start,
            _x_start,
            _w_start,
            _p_opt_lb,
            _p_free_lb,
            _dx_lb,
            _x_lb,
            _w_lb,
            _p_opt_ub,
            _p_free_ub,
            _dx_ub,
            _x_ub,
            _w_ub,
            _linearity_information_provided,
            _p_opt_lin,
            _p_free_lin,
            _dx_lin,
            _x_lin,
            _w_lin,
            JMI_DER_CPPAD,stat) is 0, \
                " jmi_opt_lp_new returned non-zero."
        #        except AttributeError as e:
        #             raise JMIException("Can not create NLPInitialization object.")
        assert self._jmi_init_opt.value is not None, \
            "jmi_init_opt struct has not returned correctly."

        self._set_nlpInit_typedefs()