示例#1
0
    def make_solvars(self, solsets, g=None):
        from petram.mesh.mesh_utils import (get_extended_connectivity,
                                            get_reverse_connectivity)
        from petram.helper.variables import Variable
        from petram.helper.variables import add_scalar

        solvars = [None] * len(solsets)
        if g is None:
            g = {}
        for k, v in enumerate(solsets):
            mesh, soldict = v

            get_extended_connectivity(mesh[0])
            get_reverse_connectivity(mesh[0])

            solvar = g.copy()
            for phys in self.iter_enabled():
                phys.soldict_to_solvars(soldict, solvar)

            for var in solvar.values():
                if isinstance(var, Variable):
                    var.add_topological_info(mesh[0])

            solvars[k] = solvar

            if 'minSJac_' + str(k) in soldict:
                add_scalar(solvar, "minSJac", "", ['x', 'y', 'z'],
                           soldict['minSJac_' + str(k)][0], None)
        # print "solvars", solvars
        return solvars
示例#2
0
def populate_plotdata(mesh, table, cells, cell_data):
    from petram.mesh.mesh_utils import get_extended_connectivity
    if not hasattr(mesh, 'extended_connectivity'):
        get_extended_connectivity(mesh)

    ndim = mesh.Dimension()
    ec = mesh.extended_connectivity

    cell_data['line'] = {}
    cell_data['vertex'] = {}
    l2e = ec['line2edge']
    v2v = ec['vert2vert']

    kedge = []

    if mesh.Dimension() >= 2:
        method = mesh.GetEdgeVertices
    else:
        method = mesh.GetElementVertices

    if len(l2e) > 0:
        #kedge = np.array(sum([[key]*len(l2e[key]) for key in l2e], [])).astype(int)
        kedge = np.hstack([[key] * len(l2e[key])
                           for key in l2e]).astype(int, copy=False)
        iverts = np.vstack([method(ie) for key in l2e for ie in l2e[key]])
    else:
        iverts = np.atleast_1d([]).astype(int)
    cells['line'] = table[iverts]
    cell_data['line']['physical'] = np.array(kedge)

    kvert = np.array([key for key in v2v]).astype(int, copy=False)
    iverts = np.array([v2v[key] for key in v2v]).astype(int, copy=False)

    cells['vertex'] = table[iverts]
    cell_data['vertex']['physical'] = kvert
    if ndim == 3:
        l_s_loop = [ec['surf2line'], ec['vol2surf']]
    elif ndim == 2:
        l_s_loop = [ec['surf2line'], None]
    else:
        l_s_loop = [None, None]

    iedge2bb = None  # is it used?
    return l_s_loop
示例#3
0
def extract_mesh_data_1D(mesh):
    nv = mesh.GetNV()
    X = np.vstack([mesh.GetVertexArray(k) for k in range(nv)])
    if X.shape[1] == 2:
        X = np.hstack((X, np.zeros((X.shape[0], 1))))
    elif X.shape[1] == 1:
        X = np.hstack((X, np.zeros((X.shape[0], 2))))

    cells = {}
    cell_data = {}

    attrs = mesh.GetAttributeArray()
    lverts = []
    lattr = []
    for attr in np.unique(attrs):
        idx = np.where(attrs == attr)[0]
        for i in idx:
            lverts.append(mesh.GetElementVertices(i))
        lattr.extend([attr] * len(idx))

    cells['line'] = np.array(lverts)
    cell_data['line'] = {}
    cell_data['line']['physical'] = np.array(lattr).flatten()

    vverts = []
    vattr = []
    for ibe in range(mesh.GetNBE()):
        battr = mesh.GetBdrAttribute(ibe)
        vattr.append(battr)
        vverts.append(mesh.GetBdrElementVertices(ibe))
    cells['vertex'] = np.array(vverts)
    cell_data['vertex'] = {}
    cell_data['vertex']['physical'] = np.array(vattr).flatten()

    from petram.mesh.mesh_utils import get_extended_connectivity

    if not hasattr(mesh, 'extended_connectivity'):
        get_extended_connectivity(mesh)

    return X, cells, cell_data, [None, None], None
示例#4
0
    def postprocess(self, caller, gf=None, edges=None):

        from petram.helper.mpi_recipes import safe_flatstack
        from mfem.common.mpi_debug import nicePrint
        if edges is None: return

        print("postprocess is called")
        gfr, gfi = gf
        print(caller, gfr)
        try:
            fes = gfr.ParFESpace()
            mesh = fes.GetParMesh()
        except:
            fes = gfr.FESpace()
            mesh = fes.GetMesh()
        from petram.mesh.mesh_utils import get_extended_connectivity
        if not hasattr(mesh, 'extended_connectivity'):
            get_extended_connectivity(mesh)
        l2e = mesh.extended_connectivity['line2edge']
        idx = safe_flatstack([l2e[e] for e in edges])
        if len(idx) > 0:
            dofs = safe_flatstack([fes.GetEdgeDofs(i) for i in idx])
            size = dofs.size // idx.size

            w = []
            for i in idx:
                # don't put this Tr outside the loop....
                Tr = mesh.GetEdgeTransformation(i)
                w.extend([Tr.Weight()] * size)
            w = np.array(w)
            data = gfr.GetDataArray()[dofs] + 1j * gfi.GetDataArray()[dofs]
            field = data / w
        else:
            w = np.array([])
            field = np.array([])
        nicePrint(w)
        nicePrint(field)
    def preprocess_geometry(self, attrs, emesh_idx=0):
        self.vertices = None

        self.knowns = WKD()
        mesh = self.mesh()[emesh_idx]
        self.iverts = []
        self.attrs = attrs

        if attrs[0] == 'all':
            eattrs = 'all'
        else:
            eattrs = attrs

        if mesh.Dimension() == 3:
            '''
            from petram.mesh.find_edges import find_edges
            edges, bb_edges = find_edges(mesh)
            bb_bdrs = bb_edges.keys()
            iverts = []
            for bb_bdr in bb_bdrs:
                if eattrs != 'all':
                    check = [sorted(tuple(eattr)) ==  sorted(bb_bdr) for eattr in eattrs]
                    if not any(check): continue
                iedges = bb_edges[bb_bdr]
                iverts.extend([mesh.GetEdgeVertices(ie) for ie in iedges])
            print iverts
            '''
            from petram.mesh.mesh_utils import get_extended_connectivity
            if not hasattr(mesh, 'extended_connectivity'):
                get_extended_connectivity(mesh)

            l2e = mesh.extended_connectivity['line2edge']
            keys = l2e.keys() if eattrs == 'all' else list(
                np.atleast_1d(eattrs).flatten())
            iedges = list(set(sum([l2e[k] for k in keys if k in l2e], [])))
            iverts = [mesh.GetEdgeVertices(ie) for ie in iedges]
            self.ibeles = None  # can not use boundary variable in this evaulator

        elif mesh.Dimension() == 2:
            kbdr = mesh.GetBdrAttributeArray()
            if eattrs == 'all': eattrs = np.unique(kbdr)
            iverts = []
            #d = defaultdict(list)
            for i in range(mesh.GetNBE()):
                attr = mesh.GetBdrAttribute(i)
                if attr in eattrs:
                    iverts.append(
                        list(mesh.GetBdrElement(i).GetVerticesArray()))

            x = np.unique([mesh.GetBdrArray(e)
                           for e in eattrs]).astype(int, copy=False)
            self.ibeles = x
            #d[attr].extend(mesh.GetBdrElement(i).GetVerticesArray())
        elif mesh.Dimension() == 1:
            kbdr = mesh.GetAttributeArray()
            if eattrs == 'all': eattrs = np.unique(kbdr)
            iverts = []
            #d = defaultdict(list)
            for i in range(mesh.GetNE()):
                attr = mesh.GetAttribute(i)
                if attr in eattrs:
                    iverts.append(list(mesh.GetElement(i).GetVerticesArray()))
                    #d[attr].extend(mesh.GetBdrElement(i).GetVerticesArray())
            x = np.unique([mesh.GetDomainArray(e)
                           for e in eattrs]).astype(int, copy=False)
            self.ibeles = x

        else:
            assert False, "Unsupported dim"

        self.emesh_idx = emesh_idx

        if len(iverts) == 0: return

        iverts = np.stack(iverts)
        self.iverts = iverts
        if len(self.iverts) == 0: return

        data = process_iverts2nodals(mesh, iverts)
        for k in six.iterkeys(data):
            setattr(self, k, data[k])
import sys
import petram

use_parallel = False
filename = 'circle.mesh'
for param in sys.argv[1:]:
    if param == '-p':
        use_parallel = True
    else:
        filename = param

from petram.helper.load_mfem import load

mfem, MPI = load(use_parallel)

print("use_parallel", use_parallel)
from petram.mesh.partial_mesh import edge
from petram.mesh.mesh_utils import get_extended_connectivity

mesh0 = mfem.Mesh(filename)

if use_parallel:
    mesh = mfem.ParMesh(MPI.COMM_WORLD, mesh0)
else:
    mesh = mesh0

get_extended_connectivity(mesh)

out_file = filename.split('.')[0] + "_edge.mesh"
edge(mesh, [1, 2, 3, 4, 5], filename=out_file, precision=8)
示例#7
0
def merge_domain_3d(mesh, domain_list, reorder_dom=True):

    if not hasattr(mesh, 'extended_connectivity'):
        from petram.mesh.mesh_utils import get_extended_connectivity
        get_extended_connectivity(mesh)
    ec = mesh.extended_connectivity

    v2s = ec['vol2surf']
    s2l = ec['surf2line']

    from petram.mesh.mesh_utils import vol2line, line2surf, line2vol

    v2l = vol2line(v2s, s2l)
    l2v = line2vol(v2l)
    l2s = line2surf(s2l)

    # find surfaces to remove
    # find edge to remove
    remove_surf = []
    remove_edge = Set()
    merge_vol = {}
    for d in domain_list:
        merge_vol[d] = 0
    touched_dom = []
    for i, j in combinations(domain_list, 2):
        iscts = np.intersect1d(v2s[i], v2s[j])
        if len(iscts) != 0: touched_dom.append((i, j))
        remove_surf.append(iscts)
    remove_surf = list(np.hstack(remove_surf).astype(int))

    for s in remove_surf:
        for l in s2l[s]:
            for v in v2l:
                if l in v2l[v] and not v in domain_list:
                    break
            else:
                remove_edge.add(l)
        for l in s2l[s]:
            connected_surf = [s for s in l2s[l] if not s in remove_surf]
            if len(connected_surf) != 2: continue
            v1 = [
                v for v in v2s
                if not v in domain_list and connected_surf[0] in v2s[v]
            ]
            v2 = [
                v for v in v2s
                if not v in domain_list and connected_surf[1] in v2s[v]
            ]
            if len(Set(v1 + v2)) == 1:
                remove_edge.add(l)

    remove_edge = list(remove_edge)

    # check merging to one domain
    if len(touched_dom) == 0:
        assert False, "domains are not connected"
    c = list(touched_dom[0])
    for i in range(len(touched_dom)):
        for j in range(i, len(touched_dom)):
            if touched_dom[j][0] in c and not touched_dom[j][1] in c:
                c.append(touched_dom[j][1])
            if touched_dom[j][1] in c and not touched_dom[j][0] in c:
                c.append(touched_dom[j][0])
    print("touched_domain", c)
    if len(c) != len(domain_list):
        assert False, "domains are not connected"

    # collect faces to merge
    merge_face0 = []
    for k, l in enumerate(remove_edge):
        for f in merge_face0:
            if np.intersect1d(list(f), l2s[l]).size != 0:
                for s in l2s[l]:
                    if not s in remove_surf: f.add(s)
                break
        else:
            sss = [s for s in l2s[l] if not s in remove_surf]
            merge_face0.append(Set(sss))
    print(merge_face0)
    merge_face = {}
    for k, ss in enumerate(merge_face0):
        for s in ss:
            merge_face[s] = k

    # mapping of bdr (face) attributes
    idx = 1
    bdrattr_map = {}
    for s in s2l:
        if s in remove_surf: continue
        if not s in merge_face:
            bdrattr_map[s] = idx
            idx = idx + 1
    for s, k in merge_face.items():
        if s in remove_surf: continue
        bdrattr_map[s] = k + idx

    # mapping of domaon attributes
    if reorder_dom:
        idx = 1
        domattr_map = {}
        for v in v2s:
            if not v in merge_vol:
                domattr_map[v] = idx
                idx = idx + 1
        for v, k in merge_vol.items():
            domattr_map[v] = k + idx
    else:
        domattr_map = {}
        for v in v2s:
            if not v in merge_vol:
                domattr_map[v] = v
        idx = max(v2s.keys()) + 1
        for v, k in merge_vol.tems():
            domattr_map[v] = k + idx

    print("merge_vol", merge_vol)
    print("merge_surf", merge_face)
    print("remove_edge", remove_edge)
    print("remove_surf", remove_surf)

    print("domattr_map", domattr_map)
    print("bdrattr_map", bdrattr_map)

    # check if edge can be removed...

    NV = mesh.GetNV()
    NE = mesh.GetNE()
    NBE = mesh.GetNBE()

    bdrattr = mesh.GetBdrAttributeArray()
    domattr = mesh.GetAttributeArray()

    Nbelem = np.sum([x in bdrattr_map for x in bdrattr])

    print("NV, NE, NBE", NV, NE, Nbelem)

    import sys
    if 'mfem.par' in sys.modules:
        import mfem.par as mfem
    else:
        import mfem.par as mfem
    omesh = mfem.Mesh(3, NV, NE, Nbelem, 3)

    for k in range(NV):
        v = mesh.GetVertexArray(k)
        omesh.AddVertex(list(v))

    for k in range(NE):
        iv = mesh.GetElementVertices(k)
        a = domattr_map[domattr[k]]
        if len(iv) == 4:
            omesh.AddTet(list(iv), a)
        if len(iv) == 8:
            omesh.AddHex(list(iv), a)

    for k in range(NBE):
        iv = mesh.GetBdrElementVertices(k)
        if not bdrattr[k] in bdrattr_map: continue
        a = bdrattr_map[bdrattr[k]]

        if len(iv) == 3:
            omesh.AddBdrTriangle(list(iv), a)
        if len(iv) == 4:
            omesh.AddBdrQuad(list(iv), a)

    omesh.FinalizeTopology()
    omesh.Finalize(refine=True, fix_orientation=True)

    return omesh