def _compute_sparsity(self, ordered_of_info, ordered_wrt_info, num_full_jacs, tol, orders): """ Compute a dense sparsity matrix for this jacobian using saved absolute summations. The sparsity matrix will contain only those columns that match the wrt variables in wrt_matches, but will contain rows for all outputs in the given system. Parameters ---------- ordered_of_info : list of (name, offset, end, idxs) Name, offset, etc. of row variables in the order that they appear in the jacobian. ordered_wrt_info : list of (name, offset, end, idxs) Name, offset, etc. of column variables in the order that they appear in the jacobian. num_full_jacs : int Number of times to compute partial jacobian when computing sparsity. tol : float Tolerance used to determine if an array entry is zero or nonzero. orders : int Number of orders +/- for the tolerance sweep. Returns ------- ndarray Boolean sparsity matrix. """ from openmdao.utils.coloring import _tol_sweep subjacs = self._subjacs_info summ = self._jac_summ rend = ordered_of_info[-1][2] cend = ordered_wrt_info[-1][2] J = np.zeros((rend, cend)) for of, roffset, rend, _ in ordered_of_info: for wrt, coffset, cend, _ in ordered_wrt_info: key = (of, wrt) if key in subjacs: meta = subjacs[key] if meta['rows'] is not None: rows = meta['rows'] + roffset cols = meta['cols'] + coffset J[rows, cols] = summ[key] elif issparse(summ[key]): raise NotImplementedError( "{}: scipy sparse arrays are not " "supported yet.".format(self.msginfo)) else: J[roffset:rend, coffset:cend] = summ[key] J *= (1.0 / np.max(J)) tol_info = _tol_sweep(J, tol, orders) boolJ = np.zeros(J.shape, dtype=bool) boolJ[J > tol_info['good_tol']] = True return boolJ, tol_info
def _compute_sparsity(self, ordered_of_info, ordered_wrt_info, tol, orders): """ Compute a dense sparsity matrix for this jacobian using saved absolute summations. The sparsity matrix will contain only those columns that match the wrt variables in wrt_matches, but will contain rows for all outputs in the given system. Parameters ---------- ordered_of_info : list of (name, offset, end, idxs) Name, offset, etc. of row variables in the order that they appear in the jacobian. ordered_wrt_info : list of (name, offset, end, idxs) Name, offset, etc. of column variables in the order that they appear in the jacobian. tol : float Tolerance used to determine if an array entry is zero or nonzero. orders : int Number of orders +/- for the tolerance sweep. Returns ------- coo_matrix Boolean sparsity matrix. """ from openmdao.utils.coloring import _tol_sweep subjacs = self._subjacs_info summ = self._jac_summ Jrows = [] Jcols = [] Jdata = [] for of, roffset, rend, _ in ordered_of_info: for wrt, coffset, cend, _, _ in ordered_wrt_info: key = (of, wrt) if key in summ: subsum = summ[key] meta = subjacs[key] if meta['rows'] is not None: Jrows.append(meta['rows'] + roffset) Jcols.append(meta['cols'] + coffset) Jdata.append(subsum) elif issparse(subsum): raise NotImplementedError( "{}: scipy sparse arrays are not " "supported yet.".format(self.msginfo)) else: # dense for i, r in enumerate(range(roffset, rend)): Jrows.append([r] * (cend - coffset)) Jcols.append(np.arange(coffset, cend)) Jdata.append(subsum[i, :]) summ = self._jac_summ = None # free up some memory Jrows = np.hstack(Jrows) Jcols = np.hstack(Jcols) Jdata = np.hstack([d.flat for d in Jdata]) shape = (rend, cend) Jdata *= (1.0 / np.max(Jdata)) tol_info = _tol_sweep(Jdata, tol, orders) mask = Jdata > tol_info['good_tol'] size = np.count_nonzero(mask) boolJ = coo_matrix( (np.ones(size, dtype=bool), (Jrows[mask], Jcols[mask])), shape=shape) return boolJ, tol_info