Esempio n. 1
0
    def compute_partials(self, inputs, partials):
        """
        Use complex step method to update the given Jacobian.

        Parameters
        ----------
        inputs : `VecWrapper`
            `VecWrapper` containing parameters. (p)

        partials : `Jacobian`
            Contains sub-jacobians.
        """
        # our complex step
        step = self.complex_stepsize * 1j
        out_names = self._var_allprocs_prom2abs_list['output']

        for param in inputs:

            pwrap = _TmpDict(inputs)

            pval = inputs[param]
            if isinstance(pval, ndarray):
                # replace the param array with a complex copy
                pwrap[param] = np.asarray(pval, npcomplex)
                idx_iter = array_idx_iter(pwrap[param].shape)
                psize = pval.size
            else:
                pwrap[param] = npcomplex(pval)
                idx_iter = (None, )
                psize = 1

            for i, idx in enumerate(idx_iter):
                # set a complex param value
                if idx is None:
                    pwrap[param] += step
                else:
                    pwrap[param][idx] += step

                uwrap = _TmpDict(self._outputs, return_complex=True)

                # solve with complex param value
                self._residuals.set_const(0.0)
                self.compute(pwrap, uwrap)

                for u in out_names:
                    jval = imag(uwrap[u] / self.complex_stepsize)
                    if (u, param) not in partials:  # create the dict entry
                        partials[(u, param)] = np.zeros((jval.size, psize))

                    # set the column in the Jacobian entry
                    partials[(u, param)][:, i] = jval.flat

                # restore old param value
                if idx is None:
                    pwrap[param] -= step
                else:
                    pwrap[param][idx] -= step
Esempio n. 2
0
    def compute_partials(self, inputs, partials):
        """
        Use complex step method to update the given Jacobian.

        Parameters
        ----------
        inputs : `VecWrapper`
            `VecWrapper` containing parameters. (p)

        partials : `Jacobian`
            Contains sub-jacobians.
        """
        # our complex step
        step = self.complex_stepsize * 1j
        out_names = self._var_allprocs_prom2abs_list['output']

        for param in inputs:

            pwrap = _TmpDict(inputs)

            pval = inputs[param]
            if isinstance(pval, ndarray):
                # replace the param array with a complex copy
                pwrap[param] = np.asarray(pval, npcomplex)
                idx_iter = array_idx_iter(pwrap[param].shape)
                psize = pval.size
            else:
                pwrap[param] = npcomplex(pval)
                idx_iter = (None,)
                psize = 1

            for i, idx in enumerate(idx_iter):
                # set a complex param value
                if idx is None:
                    pwrap[param] += step
                else:
                    pwrap[param][idx] += step

                uwrap = _TmpDict(self._outputs, return_complex=True)

                # solve with complex param value
                self._residuals.set_const(0.0)
                self.compute(pwrap, uwrap)

                for u in out_names:
                    jval = imag(uwrap[u] / self.complex_stepsize)
                    if (u, param) not in partials:  # create the dict entry
                        partials[(u, param)] = np.zeros((jval.size, psize))

                    # set the column in the Jacobian entry
                    partials[(u, param)][:, i] = jval.flat

                # restore old param value
                if idx is None:
                    pwrap[param] -= step
                else:
                    pwrap[param][idx] -= step
Esempio n. 3
0
 def __getitem__(self, name):
     if name in self._changed:
         return self._changed[name]
     elif self._complex:
         val = self._inner[name]
         if isinstance(val, ndarray):
             self._changed[name] = np.asarray(val, dtype=npcomplex)
         else:
             self._changed[name] = npcomplex(val)
         return self._changed[name]
     else:
         return self._inner[name]
Esempio n. 4
0
 def __getitem__(self, name):
     if name in self._changed:
         return self._changed[name]
     elif self._complex:
         val = self._inner[name]
         if isinstance(val, ndarray):
             self._changed[name] = np.asarray(val, dtype=npcomplex)
         else:
             self._changed[name] = npcomplex(val)
         return self._changed[name]
     else:
         return self._inner[name]
Esempio n. 5
0
    def linearize(self, params, unknowns, resids):
        """
        Uses complex step method to calculate a Jacobian dict.

        Args
        ----
        params : `VecWrapper`
            `VecWrapper` containing parameters. (p)

        unknowns : `VecWrapper`
            `VecWrapper` containing outputs and states. (u)

        resids : `VecWrapper`
            `VecWrapper` containing residuals. (r)

        Returns
        -------
        dict
            Dictionary whose keys are tuples of the form ('unknown', 'param')
            and whose values are ndarrays.
        """

        # our complex step
        step = self.complex_stepsize * 1j

        J = OrderedDict()
        non_pbo_outputs = self._non_pbo_outputs

        for param in params:

            pwrap = _TmpDict(params)

            pval = params[param]
            if isinstance(pval, ndarray):
                # replace the param array with a complex copy
                pwrap[param] = numpy.asarray(pval, npcomplex)
                idx_iter = array_idx_iter(pwrap[param].shape)
                psize = pval.size
            else:
                pwrap[param] = npcomplex(pval)
                idx_iter = (None, )
                psize = 1

            for i, idx in enumerate(idx_iter):
                # set a complex param value
                if idx is None:
                    pwrap[param] += step
                else:
                    pwrap[param][idx] += step

                uwrap = _TmpDict(unknowns, return_complex=True)

                # solve with complex param value
                self._residuals.set_val(0.0)
                self.compute(pwrap, uwrap)

                for u in non_pbo_outputs:
                    jval = imag(uwrap[u] / self.complex_stepsize)
                    if (u, param) not in J:  # create the dict entry
                        J[(u, param)] = numpy.zeros((jval.size, psize))

                    # set the column in the Jacobian entry
                    J[(u, param)][:, i] = jval.flat

                # restore old param value
                if idx is None:
                    pwrap[param] -= step
                else:
                    pwrap[param][idx] -= step

        return J