Пример #1
0
def get_coll_params(nlp_options, V, P, model):

    n_k = nlp_options['n_k']
    d = nlp_options['collocation']['d']
    N_coll = n_k * d  # collocation points

    parameters = model.parameters

    use_vortex_linearization = 'lin' in parameters.keys()
    if use_vortex_linearization:
        Xdot = construct_Xdot_struct(nlp_options, model.variables_dict)(0.)

        coll_params = []
        for kdx in range(n_k):
            for ddx in range(d):
                loc_params = get_parameters_at_time(nlp_options, P, V, Xdot,
                                                    model.variables,
                                                    model.parameters, kdx, ddx)
                coll_params = cas.horzcat(coll_params, loc_params)

    else:
        coll_params = cas.repmat(
            parameters(cas.vertcat(P['theta0'], V['phi'])), 1, N_coll)

    return coll_params
Пример #2
0
def get_ms_params(nlp_options, V, P, Xdot, model):
    n_k = nlp_options['n_k']
    N_ms = n_k  # collocation points

    parameters = model.parameters

    use_vortex_linearization = 'lin' in parameters.keys()
    if use_vortex_linearization:
        message = 'vortex induction model not yet supported for multiple shooting problems.'
        awelogger.logger.error(message)

    ms_params = cas.repmat(parameters(cas.vertcat(P['theta0'], V['phi'])), 1,
                           N_ms)

    return ms_params
Пример #3
0
    def __ms_nlp_vars(self, options, model, V, P):
        """Rearrange decision variables to dae-compatible form,
        allowing for parallel function evaluations

        @param model awebox model
        @param V nlp decision variables
        @param P nlp parameters
        """

        # interval parameters
        param_at_time = model.parameters(cas.vertcat(P['theta0'], V['phi']))
        ms_params = cas.repmat(param_at_time, 1, self.__n_k)

        if options['parallelization']['include']:
            # use function map for rootfinder parallellization
            G_map = self.__dae.rootfinder.map(
                'G_map', options['parallelization']['type'], self.__n_k, [],
                [])
            x_root = []
            z_root = []
            p_root = []

        else:
            # compute implicit vars in for loop
            z_implicit = []

        # compute explicit values of implicit variables
        ms_vars0 = []
        for kdx in range(self.__n_k):
            # get vars at time
            var_at_time = struct_op.get_variables_at_time(
                options, V, None, model.variables, kdx)
            ms_vars0 += [var_at_time]
            # get dae vars at time
            x, z, p = self.__dae.fill_in_dae_variables(var_at_time,
                                                       param_at_time)

            if not options['parallelization']['include']:
                # compute implicit vars in for loop
                z_at_time = self.__dae.z(self.__dae.rootfinder(z, x, p))
                z_implicit = cas.horzcat(z_implicit, z_at_time)
            else:
                # store vars for parallelization
                x_root = cas.horzcat(x_root, x)
                z_root = cas.horzcat(z_root, z)
                p_root = cas.horzcat(p_root, p)

        if options['parallelization']['include']:
            # compute implicit vars in parallel fashion
            z_implicit = G_map(z_root, x_root, p_root)

        # construct list of all interval variables
        ms_vars = []
        ms_x = []
        ms_z = []
        ms_p = []

        for kdx in range(self.__n_k):
            # fill in non-lifted vars
            var_at_time = self.__set_implicit_variables(
                options, ms_vars0[kdx], param_at_time,
                self.__dae.z(z_implicit[:, kdx]))
            # update dae vars at time
            x, z, p = self.__dae.fill_in_dae_variables(var_at_time,
                                                       param_at_time)

            # store result
            ms_vars = cas.horzcat(ms_vars, var_at_time)
            ms_x = cas.horzcat(ms_x, x)
            ms_z = cas.horzcat(ms_z, z)
            ms_p = cas.horzcat(ms_p, p)

        self.__ms_params = ms_params
        self.__ms_vars = ms_vars
        self.__ms_x = ms_x
        self.__ms_z = ms_z
        self.__ms_z0 = z_implicit
        self.__ms_p = ms_p

        return None
Пример #4
0
    def collocate_constraints(self, options, model, formulation, V, P, Xdot):
        """ Generate collocation and path constraints on all nodes, provide integral outputs and
            integral constraints on all nodes
        """
        # extract discretization information
        N_coll = self.__n_k * self.__d  # collocation points

        # extract model information
        variables = model.variables
        parameters = model.parameters

        # construct list of all collocation node variables and parameters
        coll_vars = []
        for kdx in range(self.__n_k):
            for ddx in range(self.__d):
                var_at_time = struct_op.get_variables_at_time(
                    options, V, Xdot, model, kdx, ddx)
                coll_vars = cas.horzcat(coll_vars, var_at_time)

        coll_params = cas.repmat(
            parameters(cas.vertcat(P['theta0'], V['phi'])), 1, N_coll)

        # evaluate dynamics and constraint functions on all intervals
        if options['parallelization']['include']:

            # use function map for parallellization
            parallellization = options['parallelization']['type']
            dynamics = model.dynamics.map('dynamics_map', parallellization,
                                          N_coll, [], [])
            path_constraints_fun = model.constraints_fun.map(
                'constraints_map', parallellization, N_coll, [], [])
            integral_outputs_fun = model.integral_outputs_fun.map(
                'integral_outputs_map', parallellization, N_coll, [], [])
            outputs_fun = model.outputs_fun.map('outputs_fun',
                                                parallellization, N_coll, [],
                                                [])

            # extract formulation information
            constraints_fun_ineq = formulation.constraints_fun['integral'][
                'inequality'].map('integral_constraints_map_ineq', 'serial',
                                  N_coll, [], [])
            constraints_fun_eq = formulation.constraints_fun['integral'][
                'equality'].map('integral_constraints_map_eq', 'serial',
                                N_coll, [], [])

            # evaluate functions
            coll_dynamics = dynamics(coll_vars, coll_params)
            coll_constraints = path_constraints_fun(coll_vars, coll_params)
            coll_outputs = outputs_fun(coll_vars, coll_params)
            integral_outputs_deriv = integral_outputs_fun(
                coll_vars, coll_params)
            integral_constraints = OrderedDict()
            integral_constraints['inequality'] = constraints_fun_ineq(
                coll_vars, coll_params)
            integral_constraints['equality'] = constraints_fun_eq(
                coll_vars, coll_params)

        else:

            # initialize function evaluations
            coll_dynamics = []
            coll_constraints = []
            coll_outputs = []
            integral_outputs_deriv = []
            integral_constraints = OrderedDict()
            integral_constraints['inequality'] = []
            integral_constraints['equality'] = []

            # evaluate functions in for loop
            for i in range(N_coll):
                coll_dynamics = cas.horzcat(
                    coll_dynamics,
                    model.dynamics(coll_vars[:, i], coll_params[:, i]))
                coll_constraints = cas.horzcat(
                    coll_constraints,
                    model.constraints_fun(coll_vars[:, i], coll_params[:, i]))
                coll_outputs = cas.horzcat(
                    coll_outputs,
                    model.outputs_fun(coll_vars[:, i], coll_params[:, i]))
                integral_outputs_deriv = cas.horzcat(
                    integral_outputs_deriv,
                    model.integral_outputs_fun(coll_vars[:, i],
                                               coll_params[:, i]))
                integral_constraints['inequality'] = cas.horzcat(
                    integral_constraints['inequality'],
                    formulation.constraints_fun['integral']['inequality'](
                        coll_vars[:, i], coll_params[:, i]))
                integral_constraints['equality'] = cas.horzcat(
                    integral_constraints['equality'],
                    formulation.constraints_fun['integral']['equality'](
                        coll_vars[:, i], coll_params[:, i]))

        # integrate integral outputs
        Integral_outputs_list = [np.zeros(model.integral_outputs.cat.shape[0])]
        Integral_constraints_list = []
        for kdx in range(self.__n_k):
            tf = struct_op.calculate_tf(options, V, kdx)
            Integral_outputs_list = self.__integrate_integral_outputs(
                Integral_outputs_list,
                integral_outputs_deriv[:, kdx * self.__d:(kdx + 1) * self.__d],
                model, tf)
            Integral_constraints_list += [
                self.__integrate_integral_constraints(integral_constraints,
                                                      kdx, tf)
            ]

        return coll_dynamics, coll_constraints, coll_outputs, Integral_outputs_list, Integral_constraints_list