예제 #1
0
파일: vector.py 프로젝트: hwangjt/blue
    def __setitem__(self, name, value):
        """
        Set the unscaled variable value in true units.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.
        value : float or list or tuple or ndarray
            variable value to set (not scaled, not dimensionless)
        """
        abs_name = name2abs_name(self._system, name, self._names, self._typ)
        if abs_name is not None:
            if self._icol is None:
                slc = _full_slice
            else:
                slc = (_full_slice, self._icol)
            value, _ = ensure_compatible(name, value,
                                         self._views[abs_name][slc].shape)
            if self._vector_info._under_complex_step:

                # setitem overwrites anything you may have done with numpy indexing
                try:
                    del self._complex_view_cache[abs_name]
                except KeyError:
                    pass

                self._views[abs_name][slc] = value.real
                self._imag_views[abs_name][slc] = value.imag
            else:
                self._views[abs_name][slc] = value
        else:
            msg = 'Variable name "{}" not found.'
            raise KeyError(msg.format(name))
예제 #2
0
    def __setitem__(self, name, value):
        """
        Set the unscaled variable value in true units.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.
        value : float or list or tuple or ndarray
            variable value to set (not scaled, not dimensionless)
        """
        abs_name = name2abs_name(self._system, name, self._names, self._typ)
        if abs_name is not None:
            if self.read_only:
                msg = "Attempt to set value of '{}' in {} vector when it is read only."
                raise ValueError(msg.format(name, self._kind))

            if self._icol is None:
                slc = _full_slice
                oldval = self._views[abs_name]
            else:
                slc = (_full_slice, self._icol)
                oldval = self._views[abs_name][slc]

            value = np.asarray(value)
            if value.shape != () and value.shape != (1,) and oldval.shape != value.shape:
                raise ValueError("Incompatible shape for '%s': "
                                 "Expected %s but got %s." %
                                 (name, oldval.shape, value.shape))

            self._views[abs_name][slc] = value

        else:
            msg = 'Variable name "{}" not found.'
            raise KeyError(msg.format(name))
예제 #3
0
파일: vector.py 프로젝트: onodip/OpenMDAO
    def __contains__(self, name):
        """
        Check if the variable is involved in the current mat-vec product.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.

        Returns
        -------
        boolean
            True or False.
        """
        return name2abs_name(self._system, name, self._names, self._typ) is not None
예제 #4
0
파일: vector.py 프로젝트: hwangjt/blue
    def __getitem__(self, name):
        """
        Get the unscaled variable value in true units.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.

        Returns
        -------
        float or ndarray
            variable value (not scaled, not dimensionless).
        """
        abs_name = name2abs_name(self._system, name, self._names, self._typ)
        if abs_name is not None:
            if self._vector_info._under_complex_step:
                if self._typ == 'input':
                    if self._icol is None:
                        return self._views[
                            abs_name] + 1j * self._imag_views[abs_name]
                    else:
                        return self._views[abs_name][:, self._icol] + \
                            1j * self._imag_views[abs_name][:, self._icol]
                else:
                    if abs_name not in self._complex_view_cache:
                        if self._icol is None:
                            self._complex_view_cache[abs_name] = self._views[abs_name] + \
                                1j * self._imag_views[abs_name]
                        else:
                            self._complex_view_cache[abs_name][:, self._icol] = \
                                self._views[abs_name][:, self._icol] + \
                                1j * self._imag_views[abs_name][:, self._icol]
                            return self._complex_view_cache[abs_name][:, self.
                                                                      _icol]
                    return self._complex_view_cache[abs_name]

            if self._icol is None:
                return self._views[abs_name]
            else:
                return self._views[abs_name][:, self._icol]
        else:
            msg = 'Variable name "{}" not found.'
            raise KeyError(msg.format(name))
예제 #5
0
    def __getitem__(self, name):
        """
        Get the variable value.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.

        Returns
        -------
        float or ndarray
            variable value.
        """
        abs_name = name2abs_name(self._system(), name, self._names, self._typ)
        if abs_name is not None:
            if self._icol is None:
                return self._views[abs_name]
            else:
                return self._views[abs_name][:, self._icol]
        else:
            raise KeyError('Variable name "{}" not found.'.format(name))
예제 #6
0
파일: vector.py 프로젝트: hedongya/OpenMDAO
    def __getitem__(self, name):
        """
        Get the unscaled variable value in true units.

        Parameters
        ----------
        name : str
            Promoted or relative variable name in the owning system's namespace.

        Returns
        -------
        float or ndarray
            variable value (not scaled, not dimensionless).
        """
        abs_name = name2abs_name(self._system, name, self._names, self._typ)
        if abs_name is not None:
            if self._icol is None:
                return self._views[abs_name]
            else:
                return self._views[abs_name][:, self._icol]
        else:
            msg = 'Variable name "{}" not found.'
            raise KeyError(msg.format(name))