def create_conn_graph(self, verbose=True): """ Create a graph of mesh connectivity. Returns ------- graph : csr_matrix The mesh connectivity graph as a SciPy CSR matrix. """ from extmods.fem import raw_graph shape = (self.n_nod, self.n_nod) output('graph shape:', shape, verbose=verbose) if nm.prod(shape) == 0: output('no graph (zero size)!', verbose=verbose) return None output('assembling mesh graph...', verbose=verbose) tt = time.clock() ret, prow, icol = raw_graph(int(shape[0]), int(shape[1]), len(self.conns), self.conns, self.conns ) output('...done in %.2f s' % (time.clock() - tt), verbose=verbose) nnz = prow[-1] output('graph nonzeros: %d (%.2e%% fill)' \ % (nnz, float(nnz) / nm.prod(shape))) data = nm.ones((nnz,), dtype=nm.bool) graph = sp.csr_matrix((data, icol, prow), shape) return graph
def create_matrix_graph( self, var_names = None, vvar_names = None ): """ Create tangent matrix graph. Order of dof connectivities is not important here... """ def _prepare_dc_lists( adof_conns, var_names = None ): if var_names is None: var_names = adof_conns.iterkeys() gdcs = {} for var_name in var_names: adcs = adof_conns[var_name] for ig, dc in adcs.volume_d_cs.iteritems(): ## print dc gdcs.setdefault( ig, [] ).append( dc ) return gdcs shape = (self.avdi.ptr[-1], self.adi.ptr[-1]) output( 'matrix shape:', shape ) if nm.prod( shape ) == 0: output( 'no matrix!' ) return None cgdcs = _prepare_dc_lists( self.adof_conns, var_names ) ## print cgdcs ## pause() if self.has_virtual_d_cs: rgdcs = _prepare_dc_lists( self.avdof_conns, vvar_names ) else: rgdcs = cgdcs ## # Make all permutations per element group. rdcs = [] cdcs = [] for ig in rgdcs.iterkeys(): rgdc, cgdc = rgdcs[ig], cgdcs[ig] for perm in la.cycle( [len( rgdc ), len( cgdc )] ): rdcs.append( rgdc[perm[0]] ) cdcs.append( cgdc[perm[1]] ) # print ' ', perm, '->', rdcs[-1].shape, cdcs[-1].shape output( 'assembling matrix graph...' ) tt = time.clock() # shape = nm.array( shape, dtype = nm.long ) ret, prow, icol = raw_graph( int( shape[0] ), int( shape[1] ), len( rdcs ), rdcs, cdcs ) output( '...done in %.2f s' % (time.clock() - tt) ) nnz = prow[-1] output( 'matrix structural nonzeros: %d (%.2e%% fill)' \ % (nnz, float( nnz ) / nm.prod( shape ) ) ) ## print ret, prow, icol, nnz data = nm.zeros( (nnz,), dtype = self.dtype ) matrix = sp.csr_matrix( (data, icol, prow), shape ) ## matrix.save( 'matrix', format = '%d %d %e\n' ) ## pause() return matrix
def create_matrix_graph(self, any_dof_conn=False, rdcs=None, cdcs=None, shape=None): """ Create tangent matrix graph, i.e. preallocate and initialize the sparse storage needed for the tangent matrix. Order of DOF connectivities is not important. Parameters ---------- any_dof_conn : bool By default, only volume DOF connectivities are used, with the exception of trace surface DOF connectivities. If True, any kind of DOF connectivities is allowed. rdcs, cdcs : arrays, optional Additional row and column DOF connectivities, corresponding to the variables used in the equations. shape : tuple, optional The required shape, if it is different from the shape determined by the equations variables. This may be needed if additional row and column DOF connectivities are passed in. Returns ------- matrix : csr_matrix The matrix graph in the form of a CSR matrix with preallocated structure and zero data. """ if not self.variables.has_virtuals(): output("no matrix (no test variables)!") return None shape = get_default(shape, self.variables.get_matrix_shape()) output("matrix shape:", shape) if nm.prod(shape) == 0: output("no matrix (zero size)!") return None rdcs, cdcs = self.get_graph_conns(any_dof_conn=any_dof_conn, rdcs=rdcs, cdcs=cdcs) if not len(rdcs): output("no matrix (empty dof connectivities)!") return None output("assembling matrix graph...") tt = time.clock() ret, prow, icol = raw_graph(int(shape[0]), int(shape[1]), len(rdcs), rdcs, cdcs) output("...done in %.2f s" % (time.clock() - tt)) nnz = prow[-1] output("matrix structural nonzeros: %d (%.2e%% fill)" % (nnz, float(nnz) / nm.prod(shape))) ## print ret, prow, icol, nnz data = nm.zeros((nnz,), dtype=self.variables.dtype) matrix = sp.csr_matrix((data, icol, prow), shape) ## matrix.save( 'matrix', format = '%d %d %e\n' ) ## pause() return matrix