Esempio n. 1
0
def _assign_parameters(connections):
    """Map absolute system names to the absolute names of the
    parameters they transfer data to.
    """
    param_owners = {}

    for par, unk in connections.items():
        param_owners.setdefault(get_common_ancestor(par, unk), []).append(par)

    return param_owners
Esempio n. 2
0
    def setup(self, parent_params_vec, params_dict, srcvec, my_params,
              connections, relevant_vars=None, store_byobjs=False):
        """
        Configure this vector to store a flattened array of the variables
        in params_dict. Variable shape and value are retrieved from srcvec.

        Args
        ----
        parent_params_vec : `VecWrapper` or None
            `VecWrapper` of parameters from the parent `System`.

        params_dict : `OrderedDict`
            Dictionary of parameter absolute name mapped to metadata dict.

        srcvec : `VecWrapper`
            Source `VecWrapper` corresponding to the target `VecWrapper` we're building.

        my_params : list of str
            A list of absolute names of parameters that the `VecWrapper` we're building
            will 'own'.

        connections : dict of str : str
            A dict of absolute target names mapped to the absolute name of their
            source variable.

        relevant_vars : iter of str
            Names of variables that are relevant a particular variable of
            interest.

        store_byobjs : bool, optional
            If True, store 'pass by object' variables in the `VecWrapper` we're building.
        """

        # dparams vector has some additional behavior
        if not store_byobjs:
            self.deriv_units = True

        vec_size = 0
        missing = []  # names of our params that we don't 'own'
        for meta in params_dict.values():
            pathname = meta['pathname']
            if relevant_vars is None or meta['top_promoted_name'] in relevant_vars:
                if pathname in my_params:
                    # if connected, get metadata from the source
                    src_pathname = connections.get(pathname)
                    if src_pathname is None:
                        raise RuntimeError("Parameter '%s' is not connected" % pathname)
                    src_rel_name = srcvec.get_promoted_varname(src_pathname)
                    src_meta = srcvec.metadata(src_rel_name)

                    vmeta = self._setup_var_meta(pathname, meta, vec_size,
                                                 src_meta, store_byobjs)
                    vmeta['owned'] = True

                    if not meta.get('remote'):
                        vec_size += vmeta['size']

                    self._vardict[self._scoped_abs_name(pathname)] = vmeta
                else:
                    if parent_params_vec is not None:
                        src = connections.get(pathname)
                        if src:
                            common = get_common_ancestor(src, pathname)
                            if common == self.pathname or (self.pathname+'.') not in common:
                                missing.append(meta)
        self.vec = numpy.zeros(vec_size)

        # map slices to the array
        for name, meta in self._vardict.items():
            if not meta.get('pass_by_obj') and not meta.get('remote'):
                start, end = self._slices[name]
                meta['val'] = self.vec[start:end]

        # fill entries for missing params with views from the parent
        for meta in missing:
            pathname = meta['pathname']
            newmeta = parent_params_vec._vardict[parent_params_vec._scoped_abs_name(pathname)]
            if newmeta['pathname'] == pathname:
                newmeta = newmeta.copy()
                newmeta['promoted_name'] = meta['promoted_name']
                newmeta['owned'] = False # mark this param as not 'owned' by this VW
                self._vardict[self._scoped_abs_name(pathname)] = newmeta

        # Finally, set up unit conversions, if any exist.
        for meta in params_dict.values():
            pathname = meta['pathname']
            if pathname in my_params and (relevant_vars is None or pathname in relevant_vars):
                unitconv = meta.get('unit_conv')
                if unitconv:
                    scale, offset = unitconv
                    if self.deriv_units:
                        offset = 0.0
                    self._vardict[self._scoped_abs_name(pathname)]['unit_conv'] = (scale, offset)