How to use the dymos.utils.rk_methods.rk_methods function in dymos

To help you get started, we’ve selected a few dymos examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenMDAO / dymos / dymos / phases / explicit / solvers / nl_rk_solver.py View on Github external
Parameters
    ----------
    ode_interface
    h
    init_time
    init_states
    controls
    control_rates
    control_rates2

    Returns
    -------
    """
    num_stages = rk_methods['rk4']['num_stages']
    c = rk_methods['rk4']['c']
    A = rk_methods['rk4']['A']
    b = rk_methods['rk4']['b']

    k_stage = {}
    y_stage = {}
    ydot = {}
    yf = {}
    for name, val in iteritems(init_states):
        k_stage[name] = np.zeros([num_stages] + (list(val.shape)))
        y_stage[name] = np.zeros([num_stages] + (list(val.shape)))
        ydot[name] = np.zeros([num_stages] + (list(val.shape)))
        yf[name] = np.zeros_like(val)

    for istage in range(num_stages):

        # Set time
        t = init_time + h * c[istage]
github OpenMDAO / dymos / dymos / phases / explicit / explicit_phase.py View on Github external
def _get_parameter_connections(self, name):
        """
        Returns a list containing tuples of each path and related indices to which the
        given design variable name is to be connected.

        Returns
        -------
        connection_info : list of (paths, indices)
            A list containing a tuple of target paths and corresponding src_indices to which the
            given design variable is to be connected.
        """
        connection_info = []
        template = 'seg_{0}.stage_ode.{1}'
        num_stages = rk_methods[self.options['method']]['num_stages']

        if name in self.ode_options._parameters:
            ode_tgts = self.ode_options._parameters[name]['targets']

            for i in range(self.grid_data.num_segments):
                num_steps = self.grid_data.num_steps_per_segment[i]
                num_nodes = num_stages * num_steps
                src_idxs = [0] * num_nodes
                connection_info.append(([template.format(i, t) for t in ode_tgts], src_idxs))

        return connection_info
github OpenMDAO / dymos / dymos / phases / explicit / components / segment / stage_time_comp.py View on Github external
def setup(self):
        num_steps = self.options['num_steps']
        time_options = self.options['time_options']
        method = self.options['method']
        num_stages = rk_methods[method]['num_stages']
        c = rk_methods[self.options['method']]['c']

        self.add_input(name='seg_t0_tf',
                       val=np.array([0.0, 1.0]),
                       desc='initial and final time in the segment',
                       units=time_options['units'])

        self.add_input(name='t_initial_phase',
                       val=0.0,
                       desc='initial time of the phase',
                       units=time_options['units'])

        self.add_output(name='h',
                        val=np.ones((num_steps,)),
                        desc='size of the steps within the segment',
                        units=time_options['units'])
github OpenMDAO / dymos / dymos / transcriptions / runge_kutta / components / runge_kutta_state_predict_comp.py View on Github external
def setup(self):

        self._var_names = {}
        num_seg = self.options['num_segments']

        rk_data = rk_methods[self.options['method']]
        self._A = block_diag(rk_data['A'])
        self._num_stages = rk_data['num_stages']

        for name, options in self.options['state_options'].items():
            shape = options['shape']
            units = options['units']

            self._var_names[name] = {}
            self._var_names[name]['initial'] = 'initial_states_per_seg:{0}'.format(name)
            self._var_names[name]['k'] = 'k:{0}'.format(name)
            self._var_names[name]['predicted'] = 'predicted_states:{0}'.format(name)

            self.add_input(self._var_names[name]['initial'], shape=(num_seg,) + shape, units=units,
                           desc='The initial value of the state at the start of the segment.')

            self.add_input(self._var_names[name]['k'],
github OpenMDAO / dymos / dymos / transcriptions / runge_kutta / components / runge_kutta_state_advance_comp.py View on Github external
def setup(self):

        self._var_names = {}

        num_segs = self.options['num_segments']
        rk_data = rk_methods[self.options['method']]
        num_stages = rk_data['num_stages']

        for name, options in self.options['state_options'].items():
            shape = options['shape']
            size = np.prod(shape)
            units = options['units']

            self._var_names[name] = {}
            self._var_names[name]['initial'] = 'initial_states_per_seg:{0}'.format(name)
            self._var_names[name]['k'] = 'k:{0}'.format(name)
            self._var_names[name]['final'] = 'final_states:{0}'.format(name)
            self._var_names[name]['integral'] = 'state_integrals:{0}'.format(name)

            self.add_input(self._var_names[name]['initial'], shape=(num_segs,) + shape, units=units,
                           desc='The initial value of the state at the start of each segment.')
github OpenMDAO / dymos / dymos / transcriptions / runge_kutta / runge_kutta.py View on Github external
Parameters
        ----------
        name : str
            The name of the parameter for which connection info is desired.
        phase : Phase
            The Phase instance with which this transcription is associated.

        Returns
        -------
        connection_info : list of (paths, indices)
            A list containing a tuple of target paths and corresponding src_indices to which the
            given design variable is to be connected.
        """
        connection_info = []
        num_seg = self.grid_data.num_segments
        num_stages = rk_methods[self.options['method']]['num_stages']
        num_iter_ode_nodes = num_seg * num_stages
        num_final_ode_nodes = 2 * num_seg

        parameter_options = phase.design_parameter_options.copy()
        parameter_options.update(phase.input_parameter_options)
        parameter_options.update(phase.control_options)

        if name in parameter_options:
            ode_tgts = parameter_options[name]['targets']
            dynamic = parameter_options[name]['dynamic']
            shape = parameter_options[name]['shape']

            if dynamic:
                src_idxs_raw = np.zeros(num_final_ode_nodes, dtype=int)
                src_idxs = get_src_indices_by_row(src_idxs_raw, shape)
                if shape == (1,):
github OpenMDAO / dymos / dymos / phases / explicit / components / segment / stage_control_comp.py View on Github external
def _setup_controls(self):
        control_options = self.options['control_options']
        idx = self.options['index']
        gd = self.options['grid_data']
        num_control_disc_nodes = gd.subset_num_nodes_per_segment['control_disc'][idx]
        time_units = self.options['time_units']
        num_steps = self.options['num_steps']
        method = self.options['method']
        num_stages = rk_methods[method]['num_stages']
        num_nodes = num_steps * num_stages

        for name, options in iteritems(control_options):
            self._input_names[name] = 'disc_controls:{0}'.format(name)
            self._output_val_names[name] = 'stage_control_values:{0}'.format(name)
            self._output_rate_names[name] = 'stage_control_rates:{0}_rate'.format(name)
            self._output_rate2_names[name] = 'stage_control_rates:{0}_rate2'.format(name)
            shape = options['shape']
            input_shape = (num_control_disc_nodes,) + shape
            output_shape = (num_steps, num_stages) + shape

            units = options['units']
            rate_units = get_rate_units(units, time_units)
            rate2_units = get_rate_units(units, time_units, deriv=2)

            self._dynamic_names.append(name)
github OpenMDAO / dymos / dymos / phases / explicit / components / segment / explicit_segment.py View on Github external
def setup(self):
        idx = self.options['index']
        num_steps = self.options['num_steps']
        state_options = self.options['state_options']
        time_options = self.options['time_options']
        control_options = self.options['control_options']
        ode_class = self.options['ode_class']
        ode_init_kwargs = self.options['ode_init_kwargs']
        method = self.options['method']
        grid_data = self.options['grid_data']
        num_stages = rk_methods[method]['num_stages']

        self.add_subsystem('stage_time_comp',
                           subsys=StageTimeComp(num_steps=num_steps, method=method,
                                                time_options=time_options),
                           promotes_inputs=['*'],
                           promotes_outputs=['*'])

        self.connect('t_stage',
                     ['stage_ode.{0}'.format(t) for t in time_options['targets']],
                     src_indices=np.arange(num_steps * num_stages, dtype=int),
                     flat_src_indices=True)

        self.connect('t_phase_stage',
                     ['stage_ode.{0}'.format(t) for t in time_options['time_phase_targets']],
                     src_indices=np.arange(num_steps * num_stages, dtype=int),
                     flat_src_indices=True)
github OpenMDAO / dymos / dymos / transcriptions / grid_data.py View on Github external
'segment_ends' gives the indices of the nodes at the start (even) and end (odd) of a segment
        'step' gives the indices of the nodes at step boundaries
        'all' gives all node indices
    nodes : np.ndarray
        The location of all nodes on the interval -1, 1.

    Notes
    -----
    (subsets, nodes)
    Subset 'state_input' is the same as subset 'state_disc'.

    """
    # transform c onto [-1, 1]
    nodes = 2.0 * np.asarray(rk_methods[method]['c']) - 1.0
    n = len(nodes)
    control_disc_idxs = np.asarray(rk_methods[method]['control_disc_indices'])

    subsets = {
        'control_disc': control_disc_idxs,
        'control_input': control_disc_idxs if not compressed or seg_idx == 0
        else control_disc_idxs[1:],
        'segment_ends': np.array([0, n-1], dtype=int),
        'all': np.arange(n, dtype=int)
    }
    subsets['state_disc'] = subsets['segment_ends']
    subsets['state_input'] = subsets['state_disc'] if seg_idx == 0 else subsets['state_disc'][-1:]

    return subsets, nodes
github OpenMDAO / dymos / dymos / transcriptions / runge_kutta / components / runge_kutta_k_comp.py View on Github external
def setup(self):

        self._var_names = {}

        num_seg = self.options['num_segments']
        rk_data = rk_methods[self.options['method']]
        num_stages = rk_data['num_stages']

        self.add_input('h', val=np.ones(num_seg), units=self.options['time_units'],
                       desc='step size for current Runge-Kutta segment.')

        for name, options in self.options['state_options'].items():
            shape = options['shape']
            units = options['units']
            rate_units = get_rate_units(units, self.options['time_units'])

            self._var_names[name] = {}
            self._var_names[name]['f'] = 'f:{0}'.format(name)
            self._var_names[name]['k'] = 'k:{0}'.format(name)

            self.add_input(self._var_names[name]['f'], shape=(num_seg, num_stages) + shape,
                           units=rate_units,