Exemple #1
0
    def setup(self,
              parent_params_vec,
              params_dict,
              srcvec,
              my_params,
              connections,
              relevance=None,
              var_of_interest=None,
              store_byobjs=False,
              shared_vec=None,
              alloc_complex=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.

        relevance : `Relevance` object
            Object that knows what vars are relevant for each var_of_interest.

        var_of_interest : str or None
            Name of the current variable of interest.

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

        shared_vec : ndarray, optional
            If not None, create vec as a subslice of this array.

        alloc_complex : bool, optional
            If True, allocate space for the imaginary part of the vector and
            configure all functions to support complex computation.
        """
        # dparams vector has some additional behavior
        if not store_byobjs:
            self.deriv_units = True

        src_to_prom_name = srcvec._sysdata.to_prom_name
        scoped_name = self._sysdata._scoped_abs_name
        vec_size = 0
        missing = []  # names of our params that we don't 'own'
        syspath = self._sysdata.pathname + '.'

        for meta in itervalues(params_dict):
            if relevance is None or relevance.is_relevant(
                    var_of_interest, meta['top_promoted_name']):
                pathname = meta['pathname']
                if pathname in my_params:
                    # if connected, get metadata from the source
                    try:
                        src = connections[pathname]
                    except KeyError:
                        raise RuntimeError("Parameter '%s' is not connected" %
                                           pathname)
                    src_pathname, idxs = src
                    src_rel_name = src_to_prom_name[src_pathname]
                    src_acc = srcvec._dat[src_rel_name]

                    slc, val = self._setup_var_meta(pathname, meta, vec_size,
                                                    src_acc, store_byobjs)

                    if 'remote' not in meta or not meta['remote']:
                        vec_size += meta['size']

                    self._dat[scoped_name(pathname)] = Accessor(
                        self, slc, val, meta, self._probdata, alloc_complex)

                elif parent_params_vec is not None and pathname in connections:
                    src, _ = connections[pathname]
                    common = get_common_ancestor(src, pathname)
                    if (common == self._sysdata.pathname
                            or syspath not in common):
                        missing.append(pathname)

        if shared_vec is not None:
            self.vec = shared_vec[:vec_size]
        else:
            self.alloc_complex = alloc_complex
            self.vec = numpy.zeros(vec_size)
            if alloc_complex:
                self.imag_vec = numpy.zeros(vec_size)

        # map slices to the array
        for acc in itervalues(self._dat):
            if not (acc.pbo or acc.remote):
                start, end = acc.slice
                acc.val = self.vec[start:end]
                if alloc_complex:
                    acc.imag_val = self.imag_vec[start:end]

        # fill entries for missing params with views from the parent
        if parent_params_vec is not None:
            parent_scoped_name = parent_params_vec._sysdata._scoped_abs_name

        for pathname in missing:
            parent_acc = parent_params_vec._dat[parent_scoped_name(pathname)]
            newmeta = parent_acc.meta
            if newmeta['pathname'] == pathname:

                if alloc_complex is True and not newmeta.get('pass_by_obj'):
                    imag_val = parent_acc.imag_val
                else:
                    imag_val = None

                # mark this param as not 'owned' by this VW
                self._dat[scoped_name(pathname)] = Accessor(self,
                                                            None,
                                                            parent_acc.val,
                                                            newmeta,
                                                            self._probdata,
                                                            alloc_complex,
                                                            owned=False,
                                                            imag_val=imag_val)

        if self.deriv_units:
            self._cache_units()
Exemple #2
0
    def setup(self, parent_params_vec, params_dict, srcvec, my_params,
              connections, relevance=None, var_of_interest=None,
              store_byobjs=False, shared_vec=None, alloc_complex=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.

        relevance : `Relevance` object
            Object that knows what vars are relevant for each var_of_interest.

        var_of_interest : str or None
            Name of the current variable of interest.

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

        shared_vec : ndarray, optional
            If not None, create vec as a subslice of this array.

        alloc_complex : bool, optional
            If True, allocate space for the imaginary part of the vector and
            configure all functions to support complex computation.
        """
        # dparams vector has some additional behavior
        if not store_byobjs:
            self.deriv_units = True

        src_to_prom_name = srcvec._sysdata.to_prom_name
        scoped_name = self._sysdata._scoped_abs_name
        vec_size = 0
        missing = []  # names of our params that we don't 'own'
        syspath = self._sysdata.pathname + '.'

        for meta in itervalues(params_dict):
            if relevance is None or relevance.is_relevant(var_of_interest,
                                                          meta['top_promoted_name']):
                pathname = meta['pathname']
                if pathname in my_params:
                    # if connected, get metadata from the source
                    try:
                        src = connections[pathname]
                    except KeyError:
                        raise RuntimeError("Parameter '%s' is not connected" % pathname)
                    src_pathname, idxs = src
                    src_rel_name = src_to_prom_name[src_pathname]
                    src_acc = srcvec._dat[src_rel_name]

                    slc, val = self._setup_var_meta(pathname, meta, vec_size,
                                                    src_acc, store_byobjs)

                    if 'remote' not in meta or not meta['remote']:
                        vec_size += meta['size']

                    self._dat[scoped_name(pathname)] = Accessor(self, slc, val,
                                                                meta,
                                                                self._probdata,
                                                                alloc_complex)

                elif parent_params_vec is not None and pathname in connections:
                    src, _ = connections[pathname]
                    common = get_common_ancestor(src, pathname)
                    if (common == self._sysdata.pathname or
                                                syspath not in common):
                        missing.append(pathname)

        if shared_vec is not None:
            self.vec = shared_vec[:vec_size]
        else:
            self.alloc_complex = alloc_complex
            self.vec = numpy.zeros(vec_size)
            if alloc_complex:
                self.imag_vec = numpy.zeros(vec_size)

        # map slices to the array
        for acc in itervalues(self._dat):
            if not (acc.pbo or acc.remote):
                start, end = acc.slice
                acc.val = self.vec[start:end]
                if alloc_complex:
                    acc.imag_val = self.imag_vec[start:end]

        # fill entries for missing params with views from the parent
        if parent_params_vec is not None:
            parent_scoped_name = parent_params_vec._sysdata._scoped_abs_name

        for pathname in missing:
            parent_acc = parent_params_vec._dat[parent_scoped_name(pathname)]
            newmeta = parent_acc.meta
            if newmeta['pathname'] == pathname:

                if alloc_complex is True and not newmeta.get('pass_by_obj'):
                    imag_val = parent_acc.imag_val
                else:
                    imag_val = None

                # mark this param as not 'owned' by this VW
                self._dat[scoped_name(pathname)] = Accessor(self, None,
                                                            parent_acc.val,
                                                            newmeta, self._probdata,
                                                            alloc_complex,
                                                            owned=False,
                                                            imag_val=imag_val)

        if self.deriv_units:
            self._cache_units()
    def setup(self, parent_params_vec, params_dict, srcvec, my_params,
              connections, relevance=None, var_of_interest=None,
              store_byobjs=False, shared_vec=None):
        """
        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.

        relevance : `Relevance` object
            Object that knows what vars are relevant for each var_of_interest.

        var_of_interest : str or None
            Name of the current variable of interest.

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

        shared_vec : ndarray, optional
            If not None, create vec as a subslice of this array.
        """
        # dparams vector has some additional behavior
        if not store_byobjs:
            self.deriv_units = True

        src_to_prom_name = srcvec._sysdata.to_prom_name
        vec_size = 0
        missing = []  # names of our params that we don't 'own'
        for meta in itervalues(params_dict):
            pathname = meta['pathname']
            if relevance is None or relevance.is_relevant(var_of_interest,
                                                          meta['top_promoted_name']):
                if pathname in my_params:
                    # if connected, get metadata from the source
                    src = connections.get(pathname)
                    if src is None:
                        raise RuntimeError("Parameter '%s' is not connected" % pathname)
                    src_pathname, idxs = src
                    src_rel_name = src_to_prom_name[src_pathname]
                    src_acc = srcvec._dat[src_rel_name]

                    slc, val = self._setup_var_meta(pathname, meta, vec_size,
                                                    src_acc, store_byobjs)

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

                    my_abs = self._scoped_abs_name(pathname)
                    self._dat[my_abs] = Accessor(self, slc, val, meta)
                else:
                    if parent_params_vec is not None:
                        src = connections.get(pathname)
                        if src:
                            src, idxs = src
                            common = get_common_ancestor(src, pathname)
                            if (common == self._sysdata.pathname or
                                 (self._sysdata.pathname+'.') not in common):
                                missing.append(meta)

        if shared_vec is not None:
            self.vec = shared_vec[:vec_size]
        else:
            self.vec = numpy.zeros(vec_size)

        # map slices to the array
        for name, acc in iteritems(self._dat):
            meta = acc.meta
            if meta.get('pass_by_obj'):
                self._has_pbos = True
            elif not meta.get('remote'):
                start, end = self._dat[name].slice
                acc.val = self.vec[start:end]

        # fill entries for missing params with views from the parent
        for meta in missing:
            pathname = meta['pathname']
            parent_acc = parent_params_vec._dat[parent_params_vec._scoped_abs_name(pathname)]
            newmeta = parent_acc.meta
            if newmeta['pathname'] == pathname:
                my_abs = self._scoped_abs_name(pathname)
                # mark this param as not 'owned' by this VW
                self._dat[my_abs] = Accessor(self, None, parent_acc.val,
                                             newmeta, owned=False)
                if self._dat[my_abs].meta.get('pass_by_obj'):
                    self._has_pbos = True

        # Finally, set up unit conversions, if any exist.
        for meta in itervalues(params_dict):
            pathname = meta['pathname']
            if pathname in my_params and (relevance is None or
                                          relevance.is_relevant(var_of_interest,
                                                                pathname)):
                unitconv = meta.get('unit_conv')
                if unitconv:
                    scale, offset = unitconv
                    scoped_abs = self._scoped_abs_name(pathname)
                    self._dat[scoped_abs].meta['unit_conv'] = (scale, offset)
Exemple #4
0
    def setup(self, parent_params_vec, params_dict, srcvec, my_params,
              connections, relevance=None, var_of_interest=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.

        relevance : `Relevance` object
            Object that knows what vars are relevant for each var_of_interest.

        var_of_interest : str or None
            Name of the current 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 itervalues(params_dict):
            pathname = meta['pathname']
            if relevance is None or relevance.is_relevant(var_of_interest,
                                                          meta['top_promoted_name']):
                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 iteritems(self._vardict):
            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 itervalues(params_dict):
            pathname = meta['pathname']
            if pathname in my_params and (relevance is None or
                                          relevance.is_relevant(var_of_interest,
                                                                pathname)):
                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)

        self._setup_prom_map()