Ejemplo n.º 1
0
    def write_vtu(self, fname=None):
        if fname is None:
            fname = self.meshname.split('.')[0] + '.vtu'

        from vtk_writer import write_vtu
        vtk_id = {1: 3, 2: 5, 4: 10, 15: 1}
        Cells = {}
        cdata = {}
        k = 0.0
        for g_id, E in self.Elmts.iteritems():
            k += 1.0
            if g_id not in vtk_id:
                raise NotImplementedError('vtk ids not yet implemented')
            Cells[vtk_id[g_id]] = E[1]
            cdata[vtk_id[g_id]] = k * numpy.ones((E[1].shape[0], ))

        write_vtu(Verts=self.Verts, Cells=Cells, cdata=cdata, fname=fname)
Ejemplo n.º 2
0
    def write_vtu(self, fname=None):
        if fname is None:
            fname = self.meshname.split(".")[0] + ".vtu"

        from vtk_writer import write_vtu

        vtk_id = {1: 3, 2: 5, 4: 10, 15: 1}
        Cells = {}
        cdata = {}
        k = 0.0
        for g_id, E in self.Elmts.items():
            k += 1.0
            if g_id not in vtk_id:
                raise NotImplementedError("vtk ids not yet implemented")
            Cells[vtk_id[g_id]] = E[1]
            cdata[vtk_id[g_id]] = k * np.ones((E[1].shape[0],))

        write_vtu(Verts=self.Verts, Cells=Cells, cdata=cdata, fname=fname)
Ejemplo n.º 3
0
def vis_aggregate_groups(Verts, E2V, Agg, mesh_type, output='vtk', fname='output.vtu'):
    """
    Coarse grid visualization of aggregate groups.  Create .vtu files for use
    in Paraview or display with Matplotlib

    Parameters
    ----------
    Verts : {array}
        coordinate array (N x D)
    E2V : {array}
        element index array (Nel x Nelnodes)
    Agg : {csr_matrix}
        sparse matrix for the aggregate-vertex relationship (N x Nagg)  
    mesh_type : {string}
        type of elements: vertex, tri, quad, tet, hex (all 3d)
    fname : {string, file object}
        file to be written, e.g. 'output.vtu'
    output : {string}
        'vtk' or 'matplotlib'

    Returns
    -------
        - Writes data to .vtu file for use in paraview (xml 0.1 format) or
          displays to screen using matplotlib
    
    Notes
    -----
        - Works for both 2d and 3d elements.  Element groupings are colored
          with data equal to 2.0 and stringy edges in the aggregate are colored
          with 3.0

    Examples
    --------
    >>> from pyamg.aggregation import standard_aggregation
    >>> from pyamg.vis.vis_coarse import vis_aggregate_groups
    >>> from pyamg.gallery import load_example
    >>> data = load_example('unit_square')
    >>> A = data['A'].tocsr()
    >>> V = data['vertices']
    >>> E2V = data['elements']
    >>> Agg = standard_aggregation(A)[0]
    >>> vis_aggregate_groups(Verts=V, E2V=E2V, Agg=Agg, mesh_type='tri', output='vtk', fname='output.vtu')

    >>> from pyamg.aggregation import standard_aggregation
    >>> from pyamg.vis.vis_coarse import vis_aggregate_groups
    >>> from pyamg.gallery import load_example
    >>> data = load_example('unit_cube')
    >>> A = data['A'].tocsr()
    >>> V = data['vertices']
    >>> E2V = data['elements']
    >>> Agg = standard_aggregation(A)[0]
    >>> vis_aggregate_groups(Verts=V, E2V=E2V, Agg=Agg, mesh_type='tet', output='vtk', fname='output.vtu')

    """
    check_input(Verts=Verts,E2V=E2V,Agg=Agg,mesh_type=mesh_type)
    map_type_to_key = {'tri':5, 'quad':9, 'tet':10, 'hex':12}
    if mesh_type not in map_type_to_key:
        raise ValueError('unknown mesh_type=%s' % mesh_type)
    key = map_type_to_key[mesh_type]

    Agg = csr_matrix(Agg)

    # remove elements with dirichlet BCs
    if E2V.max() >= Agg.shape[0]:
        E2V = E2V[E2V.max(axis=1) < Agg.shape[0]]

    #####
    # 1 #
    # Find elements with all vertices in same aggregate

    # account for 0 rows.  Mark them as solitary aggregates
    # TODO: (Luke) full_aggs is not defined, I think its just a mask
    #       indicated with rows are not 0.
    if len(Agg.indices) != Agg.shape[0]:
        full_aggs = ((Agg.indptr[1:] - Agg.indptr[:-1]) == 0).nonzero()[0]
        new_aggs = numpy.array(Agg.sum(axis=1),dtype=int).ravel()
        new_aggs[full_aggs==1] = Agg.indices    # keep existing aggregate IDs
        new_aggs[full_aggs==0] = Agg.shape[1]   # fill in singletons maxID+1
        ElementAggs = new_aggs[E2V]
    else:
        ElementAggs = Agg.indices[E2V]

    #####
    # 2 #
    # find all aggregates encompassing full elements
    # mask[i] == True if all vertices in element i belong to the same aggregate
    mask = numpy.where( abs(numpy.diff(ElementAggs)).max(axis=1) == 0 )[0]
    #mask = (ElementAggs[:,:] == ElementAggs[:,0]).all(axis=1)
    E2V_a = E2V[mask,:]   # elements where element is full
    Nel_a = E2V_a.shape[0]

    #####
    # 3 #
    # find edges of elements in the same aggregate (brute force)
 
    # construct vertex to vertex graph
    col = E2V.ravel()
    row = numpy.kron(numpy.arange(0,E2V.shape[0]),numpy.ones((E2V.shape[1],),dtype=int))
    data = numpy.ones((len(col),))
    if len(row)!=len(col):
        raise ValueError('Problem constructing vertex-to-vertex map')
    V2V = coo_matrix((data,(row,col)),shape=(E2V.shape[0],E2V.max()+1))
    V2V = V2V.T * V2V
    V2V = triu(V2V,1).tocoo()

    # get all the edges
    edges = numpy.vstack((V2V.row,V2V.col)).T

    # all the edges in the same aggregate
    E2V_b = edges[Agg.indices[V2V.row] == Agg.indices[V2V.col]]
    Nel_b = E2V_b.shape[0]

    #######
    # 3.5 #
    # single node aggregates
    sums  = numpy.array(Agg.sum(axis=0)).ravel()
    E2V_c = numpy.where(sums==1)[0]
    Nel_c = len(E2V_c)

    #####
    # 4 #
    # now write out the elements and edges
    colors_a = 3*numpy.ones((Nel_a,))  # color triangles with threes
    colors_b = 2*numpy.ones((Nel_b,))  # color edges with twos
    colors_c = 1*numpy.ones((Nel_c,))  # color the vertices with ones

    Cells  =  {1:E2V_c, 3:E2V_b, key:E2V_a}
    cdata  =  {1:colors_c, 3:colors_b, key:colors_a} # make sure it's a tuple
    write_vtu(Verts=Verts, Cells=Cells, fname=fname, cdata=cdata)