Ejemplo n.º 1
0
    def _add_variable(self, name, val, var_type, **kwargs):
        """ Code common to all add functions goes here."""
        shape = kwargs.get('shape')
        self._check_val(name, var_type, val, shape)
        self._check_name(name)
        args = kwargs.copy()

        args['promoted_name'] = name
        args['val'] = val = self._get_initial_val(val, shape)

        if is_differentiable(val) and not args.get('pass_by_obj'):
            if isinstance(val, np.ndarray):
                args['size'] = val.size
                args['shape'] = val.shape
            else:
                args['size'] = 1
                args['shape'] = 1
        else:
            args['size'] = 0
            args['pass_by_obj'] = True

        if isinstance(shape, int) and shape > 1:
            args['shape'] = (shape,)

        return args
Ejemplo n.º 2
0
    def _add_variable(self, name, val, var_type, **kwargs):
        """ Contruct metadata for new variable.

        Args
        ----
        name : string
            Name of the variable.

        val : float or ndarray or object
            Initial value for the variable.

        var_type : 'param' or 'output'
            Type of variable.

        **kwargs
            Arbitrary keyword arguments to be added to metadata.

        Raises
        ------
        RuntimeError
            If name is already in use or if setup has already been performed.

        NameError
            If name is not valid.

        ValueError
            If a valid value or shape is not specified.
        """
        shape = kwargs.get('shape')
        self._check_val(name, var_type, val, shape)
        self._check_name(name)
        meta = kwargs.copy()

        meta['promoted_name'] = name
        meta['val'] = val = self._get_initial_val(val, shape)

        if is_differentiable(val) and not meta.get('pass_by_obj'):
            if isinstance(val, np.ndarray):
                meta['size'] = val.size
                meta['shape'] = val.shape
            else:
                meta['size'] = 1
                meta['shape'] = 1
        else:
            meta['size'] = 0
            meta['pass_by_obj'] = True

        if isinstance(shape, int) and shape > 1:
            meta['shape'] = (shape,)

        return meta
Ejemplo n.º 3
0
    def _setup_var_meta(self, name, meta):
        """
        Populate the metadata dict for the named variable.

        Args
        ----
        name : str
           The name of the variable to add.

        meta : dict
            Starting metadata for the variable, collected from components
            in an earlier stage of setup.

        """
        vmeta = meta.copy()
        val = meta['val']
        if not is_differentiable(val) or meta.get('pass_by_obj'):
            vmeta['val'] = _ByObjWrapper(val)

        return vmeta