示例#1
0
def complex_mesh(r, filename, n):
    import meshio
    mesh = meshio.read(filename)
    node = mesh.points
    node = node[:,0:2]*r
    cell = mesh.cells
    mesh.node = node
    mesh.cell = cell
    cell = cell['triangle']
    isUsingNode = np.zeros(node.shape[0], dtype=np.bool)
    isUsingNode[cell] = True
    NN = isUsingNode.sum()
    idxmap = np.zeros(node.shape[0], dtype=np.int32)
    idxmap[isUsingNode] = range(NN)
    cell = idxmap[cell]
    node = node[isUsingNode]
    #cell = cell[:,::-1]
    mesh = TriangleMesh(node,cell)
    nmesh = TriangleMeshWithInfinityNode(mesh)
    ppoint, pcell, pcellLocation =  nmesh.to_polygonmesh()
    pmesh = PolygonMesh(ppoint, pcell, pcellLocation)
    hmesh = HalfEdgeMesh2d.from_mesh(pmesh)
    hmesh.init_level_info()
    hmesh.convexity()
    hmesh.uniform_refine(n=n)
    mesh = PolygonMesh.from_halfedgemesh(hmesh)
    return mesh
示例#2
0
 def uniform_boundary_meshing_test(self, plot=True):
     vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)],
                         dtype=np.float)
     facets = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
     subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)], dtype=np.int)
     dof = np.array([1, 1, 1, 1], dtype=np.bool)
     mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
     uniform_boundary_mesh = CVTPMesher(mesh)
     uniform_boundary_mesh.uniform_boundary_meshing(n=2)
     bnode = uniform_boundary_mesh.bnode
     vor = Voronoi(bnode)
     if plot:
         fig = plt.figure()
         axes = fig.gca()
         mesh.add_plot(axes)
         mesh.find_node(axes, color='k', showindex=True)
         mesh.find_node(axes, node=bnode, showindex=True)
         voronoi_plot_2d(vor, ax=axes)
         '''
         cs = [axes.add_artist( plt.Circle(x, r, facecolor='none',
             edgecolor='r')) for x, r in zip(center, radius)]
         '''
         fig = plt.figure()
         axes = fig.gca()
         mesh.add_halfedge_plot(axes, showindex=True)
         mesh.find_node(axes, showindex=True)
         plt.show()
示例#3
0
    def quad_refine():
        cell = np.array([[0, 1, 2, 3], [1, 4, 5, 2]], dtype=np.int)
        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)
        mesh = QuadrangleMesh(node, cell)

        mesh = HalfEdgeMesh2d.from_mesh(mesh)
示例#4
0
    def cell_to_node(self):
        node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0),
                         (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0),
                         (2.0, 2.0)],
                        dtype=np.float)
        cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5],
                        dtype=np.int)
        cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

        print('node:', node)
        print('cell:', cell)
        print('cellLocation:', cellLocation)
        mesh = PolygonMesh(node, cell, cellLocation)

        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes)
        mesh.find_node(axes, showindex=True)
        mesh.find_cell(axes, showindex=True)

        mesh = HalfEdgeMesh2d.from_mesh(mesh)
        mesh.print()
        cell, cellLocation = mesh.entity('cell')
        print('cell:', cell)
        print('cellLocation:', cellLocation)
        plt.show()
示例#5
0
    def interpolation(self, n=2, plot=True):
        from fealpy.pde.poisson_2d import CosCosData
        from fealpy.functionspace import ConformingVirtualElementSpace2d

        pde = CosCosData()
        node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float64)
        cell = np.array([(0, 1, 2, 3)], dtype=np.int_)
        mesh = QuadrangleMesh(node, cell)
        #mesh = PolygonMesh.from_mesh(mesh)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)
        mesh.uniform_refine(n=n)

        #mesh.print()

        space = ConformingVirtualElementSpace2d(mesh, p=1)
        uI = space.interpolation(pde.solution)
        up = space.project_to_smspace(uI)
        error = space.integralalg.L2_error(pde.solution, up)
        print(error)

        if plot:
            fig = plt.figure()
            axes = fig.gca()
            #mesh.add_halfedge_plot(axes, showindex=True)
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.find_edge(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            plt.show()
示例#6
0
    def run(self, estimator='mix'):
        problem = self.problem
        options = self.optoptions
        model = problem['objective']

        optalg = SteepestDescentAlg(problem, options)
        x, f, g, diff = optalg.run(maxit=1)
        while True:
            mesh = problem['mesh']
            hmesh = HalfEdgeMesh2d.from_mesh(mesh)
            cell, cellLocation = hmesh.entity('cell')
            print('2', cellLocation)
            aopts = hmesh.adaptive_options(method='mean',
                                           maxcoarsen=3,
                                           HB=True)
            print('NN', mesh.number_of_nodes())
            mu = problem['x0']
            if estimator == 'mix':
                eta = model.mix_estimate(mu, w=1)
            if estimator == 'grad':
                eta = model.estimate(q)

            aopts['data'] = {'mu': mu}
            S0 = model.vemspace.project_to_smspace(aopts['data']['mu'][:, 0])
            S1 = model.vemspace.project_to_smspace(aopts['data']['mu'][:, 1])

            hmesh.adaptive(eta, aopts)
            #fig = plt.figure()
            #axes = fig.gca()
            #hmesh.add_plot(axes)
            #hmesh.find_cell(axes, showindex=True)
            #plt.show()

            cell, cellLocation = hmesh.entity('cell')
            print('3', cellLocation)
            mesh = PolygonMesh.from_halfedgemesh(hmesh)

            model.reinit(mesh)
            aopts['data']['mu'] = np.zeros((model.gdof, 2))
            aopts['data']['mu'][:, 0] = model.vemspace.interpolation(
                S0, aopts['HB'])
            aopts['data']['mu'][:, 1] = model.vemspace.interpolation(
                S1, aopts['HB'])
            problem['x0'] = aopts['data']['mu']

            optalg = SteepestDescentAlg(problem, options)
            x, f, g, diff = optalg.run(maxit=1)
            problem['mesh'] = mesh
            problem['x0'] = x
            problem['rho'] = model.rho
            self.problem = problem

            if diff < options['FunValDiff']:
                if (np.max(problem['rho'][:, 0]) < 1) and (np.min(
                        problem['rho'][:, 0]) > 0):
                    break
            pass
示例#7
0
    def coarsen_quad(self, plot=True):
        cell = np.array([[0, 1, 2, 3], [1, 4, 5, 2]], dtype=np.int)
        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)
        mesh = QuadrangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)
        mesh.init_level_info()
        halfedge = mesh.ds.halfedge
        NE = mesh.ds.NE
        color = 3 * np.ones(NE * 2, dtype=np.int_)
        color[1] = 1
        while (color == 3).any():
            red = color == 1
            gre = color == 0
            color[halfedge[red][:, [2, 3, 4]]] = 0
            color[halfedge[gre][:, [2, 3, 4]]] = 1
        colorlevel = ((color == 1) | (color == 2)).astype(np.int_)
        mesh.hedgecolor = {'color': color, 'level': colorlevel}

        for i in range(2):
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.ones(NC, dtype=np.bool_)
            mesh.refine_quad(isMarkedCell)
        NC = mesh.number_of_all_cells()
        isMarkedCell = np.zeros(NC, dtype=np.bool_)
        isMarkedCell[[
            1, 3, 12, 18, 20, 17, 16, 9, 7, 2, 4, 6, 7, 8, 9, 27, 15, 8, 23,
            32, 5, 24, 16, 29, 4, 7, 28, 11, 14, 22, 31, 19
        ]] = True
        mesh.coarsen_quad(isMarkedCell)
        if 1:
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[
                20, 21, 23, 22, 18, 24, 26, 11, 1, 7, 13, 3, 8, 15, 12, 17
            ]] = True
            mesh.coarsen_quad(isMarkedCell)

        for i in range(2):
            print(i, '*************************************************')
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.ones(NC, dtype=np.bool_)
            mesh.refine_quad(isMarkedCell)
        print(np.where(mesh.hedgecolor['color'] == 2))
        print(np.c_[np.arange(mesh.ds.NE * 2), mesh.hedgecolor['color'],
                    mesh.hedgecolor['level']])
        print(np.c_[np.arange(mesh.ds.NE * 2), mesh.halfedgedata['level']])
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_node(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            plt.show()
示例#8
0
 def refine_quad(self, l, plot=True):
     cell = np.array([[0, 1, 2, 3], [1, 4, 5, 2]], dtype=np.int)
     node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                     dtype=np.float)
     mesh = QuadrangleMesh(node, cell)
     mesh = HalfEdgeMesh2d.from_mesh(mesh)
     mesh.init_level_info()
     halfedge = mesh.ds.halfedge
     NE = mesh.ds.NE
     color = 3 * np.ones(NE * 2, dtype=np.int_)
     color[1] = 1
     while (color == 3).any():
         red = color == 1
         gre = color == 0
         color[halfedge[red][:, [2, 3, 4]]] = 0
         color[halfedge[gre][:, [2, 3, 4]]] = 1
     colorlevel = ((color == 1) | (color == 2)).astype(np.int_)
     mesh.hedgecolor = {'color': color, 'level': colorlevel}
     c = np.array([1, 10000.5])
     r = 10000
     h = 1e-2
     k = 0
     NB = 0
     while k < l:
         halfedge = mesh.ds.halfedge
         halfedge1 = halfedge[:, 3]
         node = mesh.node
         flag = node - c
         flag = flag[:, 0]**2 + flag[:, 1]**2
         flag = flag <= r**2
         flag1 = flag[halfedge[:, 0]].astype(int)
         flag2 = flag[halfedge[halfedge1, 0]].astype(int)
         markedge = flag1 + flag2 == 1
         markedcell = halfedge[markedge, 1]
         markedcell = np.unique(markedcell)
         cell = np.unique(halfedge[:, 1])
         nc = cell.shape[0]
         markedcell1 = np.zeros(nc)
         markedcell1[markedcell] = 1
         print('makee', markedcell)
         mesh.refine_quad(markedcell1.astype(np.bool_))
         k += 1
         print('循环', k, '次***************************')
         print(np.c_[np.arange(mesh.ds.NE * 2), mesh.hedgecolor['color'],
                     mesh.hedgecolor['level']])
         if plot:
             plt.close()
             fig = plt.figure()
             axes = fig.gca()
             nindex = mesh.nodedata['level']
             mesh.add_plot(axes)
             #mesh.add_halfedge_plot(axes, showindex=True)
             #mesh.find_node(axes, showindex=True, multiindex=nindex)
             plt.show()
示例#9
0
def halfedgemesh(n=4, h=4):
    """
    半边数据结构的网格
    """
    node = np.array([(0, 0), (h, 0), (h, h), (0, h)], dtype=np.float64)
    cell = np.array([(0, 1, 2, 3)], dtype=np.int_)

    mesh = QuadrangleMesh(node, cell)
    mesh = HalfEdgeMesh2d.from_mesh(mesh)
    mesh.uniform_refine(n)
    return mesh
示例#10
0
    def data_structure(self, plot=True):
        node = np.array([[0,0],[1,0],[1,1],[0,1],[2,0],[2,1]], dtype = np.float)
        cell = np.array([[0,1,2],[0,2,3],[1,4,5],[2,1,5]],dtype = np.int)
        mesh = TriangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)
        mesh.print()

        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_node(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            plt.show()
示例#11
0
    def from_edges(self, plot=True):

        node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float)
        edge = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
        edge2subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)],
                                  dtype=np.int)

        mesh = HalfEdgeMesh2d.from_edges(node, edge, edge2subdomain)
        mesh.print()
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_halfedge_plot(axes, showindex=True)
            plt.show()
示例#12
0
    def harmonic_map(self, fanme):

        reader = CCGMeshReader(fname)
        tmesh = reader.read()

        mesh = HalfEdgeMesh2d.from_mesh(tmesh, NV=3)
        mesh.nodedata['rgb'] = tmesh.nodedata['rgb']

        cmesh = self.ccgalg.harmonic_map(mesh)
        cmesh.nodedata['rgb'] = tmesh.nodedata['rgb']
        writer = MeshWriter(mesh)
        writer.write(fname='face3.vtu')

        writer = MeshWriter(cmesh)
        writer.write(fname='face2.vtu')
示例#13
0
    def tri_cut_graph(self, fname):
        data = sio.loadmat(fname)
        node = np.array(data['node'], dtype=np.float64)
        cell = np.array(data['elem'] - 1, dtype=np.int_)

        mesh = TriangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh, closed=True)
        mesh.ds.NV = 3

        gamma = self.ccgalg.tri_cut_graph(mesh)

        writer = MeshWriter(mesh)
        writer.write(fname='test0.vtu')
        for i, index in enumerate(gamma):
            writer = MeshWriter(mesh, etype='edge', index=index)
            writer.write(fname='test' + str(i + 1) + '.vtu')
示例#14
0
def halfedgemesh(h=4, n=4):
    """
    半边数据结构的网格
    """
    node = h * np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0),
                         (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0),
                         (2.0, 2.0)],
                        dtype=np.float)
    cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5],
                    dtype=np.int)
    cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

    mesh = PolygonMesh(node, cell, cellLocation)
    mesh = HalfEdgeMesh2d.from_mesh(mesh)
    #mesh.uniform_refine(n)
    return mesh
示例#15
0
    def refine_halfedge(self, plot=True):
        node = np.array([[0,0],[1,0],[1,1],[0,1],[2,0],[2,1]], dtype = np.float)
        cell = np.array([[0,1,2],[0,2,3],[1,4,5],[2,1,5]],dtype = np.int)
        mesh = TriangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)

        isMarkedCell = np.array([0, 1, 0, 0, 0], dtype=np.bool_)
        isMarkedHEdge = mesh.mark_halfedge(isMarkedCell)
        mesh.refine_halfedge(isMarkedHEdge)
        mesh.print()
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_node(axes, showindex=True)
            #mesh.find_cell(axes, showindex=True)
            plt.show()
示例#16
0
    def convexity(self):
        node = np.array([[0, 0], [2, 0], [2, 1], [1, 1], [1, 2], [1, 3],
                         [1, 4], [2, 4], [2, 5], [0, 5], [0, 2]],
                        dtype=np.float)
        cell = np.array([0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9, 10, 4],
                        dtype=np.int_)
        cellLocation = np.array([0, 6, 13], dtype=np.int)

        mesh = PolygonMesh(node, cell, cellLocation)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)

        mesh.print()
        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes)
        mesh.find_node(axes, showindex=True)
        mesh.find_cell(axes, showindex=True)
        mesh.add_halfedge_plot(axes, showindex=True)
        plt.show()
示例#17
0
    def coarsen_poly(self, plot=True):

        node = np.array([[0,0],[1,0],[1,1],[0,1],[2,0],[2,1]], dtype = np.float)
        cell = np.array([[0,1,2],[0,2,3],[1,4,5],[2,1,5]],dtype = np.int)
        mesh = TriangleMesh(node, cell)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)

        isMarkedCell = np.array([0,0,0,1,0], dtype=np.bool_)
        mesh.refine_poly(isMarkedCell)


        isMarkedCell = np.array([0,0,0,0,1,1,1], dtype=np.bool_)
        mesh.coarsen_poly(isMarkedCell)
        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes)
        mesh.find_node(axes, showindex=True)
        mesh.find_cell(axes, showindex=True)
        plt.show()
示例#18
0
    def square_domain(self, plot=True):

        node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float)
        edge = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
        edge2subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)],
                                  dtype=np.int)

        mesh = HalfEdgeMesh2d.from_edges(node, edge, edge2subdomain)
        alg = TriAdvancingFrontAlg(mesh)
        alg.run()
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_halfedge_plot(axes, showindex=True)

            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes)
            plt.show()
示例#19
0
    def square_domain(self, plot=True):

        node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float)
        edge = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
        edge2subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)],
                                  dtype=np.int)

        mesh = HalfEdgeMesh2d.from_edges(node, edge, edge2subdomain)
        alg = TriAdvancingFrontAlg(mesh)
        tri, fnode = alg.run()
        node = mesh.entity('node')
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes)

            node = np.r_['0', node, fnode]
            fig = plt.figure()
            axes = fig.gca()
            axes.triplot(node[:, 0], node[:, 1], tri.simplices)
            axes.set_axis_off()
            axes.set_aspect('equal')
            plt.show()
示例#20
0
import numpy as np
import matplotlib.pyplot as plt
from fealpy.mesh import PolygonMesh, HalfEdgeMesh2d

node3 = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0), (1.0, 1.0),
                  (1.0, 2.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0)],
                 dtype=np.float64)
cell3 = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5],
                 dtype=np.int)
cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

mesh3 = PolygonMesh(node3, cell3, cellLocation)
mesh3 = HalfEdgeMesh2d.from_mesh(mesh3)
mesh3.uniform_refine(2)  # 这里一致加密的时候会报错
node3 = mesh3.entity("node")
edge3 = mesh3.entity("edge")
cell3, cellLocation = mesh3.entity("cell")
halfedge = mesh3.entity("halfedge")

fig3 = plt.figure()
axes3 = fig3.gca()
mesh3.add_plot(axes3)
mesh3.find_node(axes3, showindex=True)
mesh3.find_edge(axes3)
plt.show()
示例#21
0
    def Lloyd_test(self, domain='square', plot=True):
        if domain == 'square':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_meshing(refine=2)

        elif domain == 'LShape':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0),
                                 (-1.0, -1.0), (0.0, -1.0)],
                                dtype=np.float)
            fixed = np.array([1, 1, 1, 0, 1, 0, 1, 1], dtype=np.bool)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0)],
                              dtype=np.int)
            subdomain = np.array([
                (1, 0),
                (1, 0),
                (1, 0),
                (1, 0),
                (1, 0),
                (1, 0),
                (1, 0),
                (1, 0),
            ],
                                 dtype=np.int)

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain,
                                             fixed)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_meshing(refine=2)
        elif domain == 'circle':
            n = 20
            h = 2 * np.pi / n
            theta = np.arange(0, 2 * np.pi, h)
            vertices = np.zeros((n, 2), dtype=np.float)
            vertices[:, 0] = np.cos(theta)
            vertices[:, 1] = np.sin(theta)
            fixed = np.ones(n, dtype=np.bool)
            facets = np.zeros((n, 2), dtype=np.int)
            facets[:, 0] = range(0, n)
            facets[:-1, 1] = range(1, n)
            subdomain = np.zeros((n, 2), dtype=np.int)
            subdomain[:, 0] = 1

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain,
                                             fixed)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_meshing(refine=0)
        elif domain == 'partition1':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0), (0.5, 0.5)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 0), (0, 4), (4, 3),
                               (4, 1), (4, 2)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (2, 0), (3, 0), (4, 0), (4, 1),
                                  (4, 3), (2, 1), (3, 2)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_meshing(refine=3)

        elif domain == 'partition2':
            vertices = np.array([(0.0, 0.0), (0.5, 0.0), (1.0, 0.0),
                                 (1.0, 0.5), (1.0, 1.0), (0.5, 1.0),
                                 (0.0, 1.0), (0.0, 0.5), (0.5, 0.5)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0), (1, 8), (8, 7), (8, 3), (8, 5)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (2, 0), (2, 0), (3, 0), (3, 0),
                                  (4, 0), (4, 0), (1, 0), (1, 2), (1, 4),
                                  (3, 2), (4, 3)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mgesh)
            uniform_mesh.uniform_meshing(refine=2)
        elif domain == 'hole1':

            vertices = np.array([(0.0, 0.0), (0.5, 0.0), (1.0, 0.0),
                                 (1.0, 0.5), (1.0, 1.0), (0.5, 1.0),
                                 (0.0, 1.0), (0.0, 0.5), (0.4, 0.4),
                                 (0.7, 0.4), (0.7, 0.7), (0.4, 0.7)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0), (8, 9), (9, 10), (10, 11),
                               (11, 8)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0), (1, 0),
                                  (1, 0), (1, 0), (1, 0), (1, -1), (1, -1),
                                  (1, -1), (1, -1)],
                                 dtype=np.int)
            """ 
            vertices = np.array([
                ( 0.0, 0.0),( 1.0, 0.0),( 1.0, 1.0),( 0.0, 1.0),
                ( 0.4, 0.4),( 0.7, 0.4),( 0.7, 0.7),( 0.4, 0.7)],dtype=np.float)
            facets = np.array([
                (0, 1),(1, 2),( 2, 3),( 3, 0),
                (4, 5),(5, 6),( 6, 7),( 7, 4)], dtype=np.int)
            subdomain = np.array([
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1,-1),(1,-1),(1,-1),(1,-1)], dtype=np.int)
            """

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_meshing(refine=1)
        mesh.print()
        vor, start = uniform_mesh.voronoi()
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, color='k', showindex=True)
            mesh.find_node(axes, node=vor.points, showindex=True)
            voronoi_plot_2d(vor, ax=axes)
            plt.show()
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_node(axes, showindex=True)
            plt.show()

        i = 0
        while i < 100:
            vor = uniform_mesh.Lloyd(vor, start)
            i += 1

        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, color='k', showindex=True)
            mesh.find_node(axes, node=vor.points, showindex=True)
            voronoi_plot_2d(vor, ax=axes)
            plt.show()
示例#22
0
    def uniform_meshing_test(self,
                             domain='square',
                             plot=True,
                             interior_nodes=True,
                             add_cnode=False):

        if domain == 'square':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 0)], dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=2)
            bnode = uniform_mesh.bnode

        elif domain == 'LShape':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0), (-1.0, 1.0), (-1.0, 0.0),
                                 (-1.0, -1.0), (0.0, -1.0)],
                                dtype=np.float)
            fixed = np.array([0, 0, 0, 1, 0, 1, 0, 0], dtype=np.bool)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0), (1, 0),
                                  (1, 0), (1, 0), (1, 0)],
                                 dtype=np.int)

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain,
                                             fixed)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=2)
            bnode = uniform_mesh.bnode

        elif domain == 'circle':
            n = 20
            h = 2 * np.pi / n
            theta = np.arange(0, 2 * np.pi, h)
            vertices = np.zeros((n, 2), dtype=np.float)
            vertices[:, 0] = np.cos(theta)
            vertices[:, 1] = np.sin(theta)
            fixed = np.zeros(n, dtype=np.bool)
            facets = np.zeros((n, 2), dtype=np.int)
            facets[:, 0] = range(0, n)
            facets[:-1, 1] = range(1, n)
            subdomain = np.zeros((n, 2), dtype=np.int)
            subdomain[:, 0] = 1

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain,
                                             fixed)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=0)
            bnode = uniform_mesh.bnode
        elif domain == 'partition1':
            vertices = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                 (0.0, 1.0), (0.5, 0.5)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 0), (0, 4), (4, 3),
                               (4, 1), (4, 2)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (2, 0), (3, 0), (4, 0), (4, 1),
                                  (4, 3), (2, 1), (3, 2)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=3)
            bnode = uniform_mesh.bnode

        elif domain == 'partition2':
            vertices = np.array([(0.0, 0.0), (0.5, 0.0), (1.0, 0.0),
                                 (1.0, 0.5), (1.0, 1.0), (0.5, 1.0),
                                 (0.0, 1.0), (0.0, 0.5), (0.5, 0.5)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0), (1, 8), (8, 7), (8, 3), (8, 5)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (2, 0), (2, 0), (3, 0), (3, 0),
                                  (4, 0), (4, 0), (1, 0), (1, 2), (1, 4),
                                  (3, 2), (4, 3)],
                                 dtype=np.int)
            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=2)
            bnode = uniform_mesh.bnode
        elif domain == 'hole1':

            vertices = np.array([(0.0, 0.0), (0.5, 0.0), (1.0, 0.0),
                                 (1.0, 0.5), (1.0, 1.0), (0.5, 1.0),
                                 (0.0, 1.0), (0.0, 0.5), (0.4, 0.4),
                                 (0.7, 0.4), (0.7, 0.7), (0.4, 0.7)],
                                dtype=np.float)
            facets = np.array([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 0), (8, 9), (9, 10), (10, 11),
                               (11, 8)],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0), (1, 0),
                                  (1, 0), (1, 0), (1, 0), (1, -1), (1, -1),
                                  (1, -1), (1, -1)],
                                 dtype=np.int)
            """ 
            vertices = np.array([
                ( 0.0, 0.0),( 1.0, 0.0),( 1.0, 1.0),( 0.0, 1.0),
                ( 0.4, 0.4),( 0.7, 0.4),( 0.7, 0.7),( 0.4, 0.7)],dtype=np.float)
            facets = np.array([
                (0, 1),(1, 2),( 2, 3),( 3, 0),
                (4, 5),(5, 6),( 6, 7),( 7, 4)], dtype=np.int)
            subdomain = np.array([
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1,-1),(1,-1),(1,-1),(1,-1)], dtype=np.int)
            """

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=1)
            bnode = uniform_mesh.bnode
        elif domain == 'hole2':
            """
            vertices = np.array([
                ( 0.0, 0.0),( 0.5, 1.0),( 1.0, 0.0),( 1.5, 0.0),
                ( 2.0, 0.0),( 2.0, 0.5),( 2.0, 1.0),( 2.0, 1.5),
                ( 2.0, 2.0),( 1.5, 2.0),( 1.0, 2.0),( 0.5, 2.0),
                ( 0.0, 2.0),( 0.0, 1.5),( 0.0, 1.0),( 0.0, 0.5),
                ( 0.4, 0.4),( 0.4, 0.7),( 0.7, 0.7),( 0.7, 0.4),
                ( 1.2, 1.2),( 1.2, 1.5),( 1.5, 1.5),( 1.5, 1.2)],dtype=np.float)
            facets = np.array([
                ( 0, 1),( 1, 2),( 2, 3),( 3, 4),
                ( 4, 5),( 5, 6),( 6, 7),( 7, 8),
                ( 8, 9),( 9,10),(10,11),(11,12),
                (12,13),(13,14),(14,15),(15, 0),
                (16,17),(17,18),(18,19),(19,16),
                (20,21),(21,22),(22,23),(23,20)], dtype=np.int)
            subdomain = np.array([
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1, 0),(1, 0),(1, 0),(1, 0),
                (1,-1),(1,-1),(1,-1),(1,-1),
                (1,-2),(1,-2),(1,-2),(1,-2)], dtype=np.int)
            """
            #"""
            vertices = np.array(
                [(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0),
                 (1.0, 2.0), (0.0, 2.0), (0.0, 1.0), (0.4, 0.4), (0.4, 0.7),
                 (0.7, 0.7), (0.7, 0.4), (1.2, 1.2), (1.2, 1.5), (1.5, 1.5),
                 (1.5, 1.2)],
                dtype=np.float)
            facets = np.array([
                (0, 1),
                (1, 2),
                (2, 3),
                (3, 4),
                (4, 5),
                (5, 6),
                (6, 7),
                (7, 0),
                (8, 9),
                (9, 10),
                (10, 11),
                (11, 8),
                (12, 13),
                (13, 14),
                (14, 15),
                (15, 12),
            ],
                              dtype=np.int)
            subdomain = np.array([(1, 0), (1, 0), (1, 0), (1, 0), (1, 0),
                                  (1, 0), (1, 0), (1, 0), (1, -1), (1, -1),
                                  (1, -1), (1, -1), (1, -2), (1, -2), (1, -2),
                                  (1, -2)],
                                 dtype=np.int)

            #"""

            mesh = HalfEdgeMesh2d.from_edges(vertices, facets, subdomain)
            uniform_mesh = CVTPMesher(mesh)
            uniform_mesh.uniform_boundary_meshing(n=1)
            bnode = uniform_mesh.bnode

        if add_cnode == True:
            cnode = uniform_mesh.cnode
            bnode = np.append(bnode, cnode, axis=0)

        vor = Voronoi(bnode, incremental=True)
        if interior_nodes:
            uniform_mesh.uniform_init_interior_nodes()
            newnode = uniform_mesh.inode
            for k in newnode:
                vor.add_points(newnode[k])

        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, color='k', showindex=True)
            mesh.find_node(axes, node=bnode, showindex=True)
            mesh.print()
            voronoi_plot_2d(vor, ax=axes)
            plt.show()
示例#23
0
    def run(self, estimator='mix'):
        problem = self.problem
        options = self.optoptions
        moptions = self.moptions
        model = problem['objective']

        optalg = SteepestDescentAlg(problem, options)
        x, f, g, diff = optalg.run(maxit=10)

        q = np.zeros(model.rho.shape)
        q[:, 0] = model.q0[:, -1]
        q[:, 1] = model.q1[:, -1]
        problem['x0'] = x
        while True:
            print('chiN', moptions['chiN'])
            while True:
                mesh = problem['mesh']  ##多边形网格
                hmesh = HalfEdgeMesh2d.from_mesh(mesh)  ##半边网格
                aopts = hmesh.adaptive_options(method='mean',
                                               maxcoarsen=3,
                                               HB=True)
                print('NN', mesh.number_of_nodes())

                mu = problem['x0']
                if estimator == 'mix':
                    eta = model.mix_estimate(q, w=1)
                if estimator == 'grad':
                    eta = model.estimate(q)

                aopts['data'] = {'mu': mu}
                S0 = model.vemspace.project_to_smspace(aopts['data']['mu'][:,
                                                                           0])
                S1 = model.vemspace.project_to_smspace(aopts['data']['mu'][:,
                                                                           1])

                hmesh.adaptive(eta, aopts)  ###半边网格做自适应
                mesh = PolygonMesh.from_halfedgemesh(hmesh)  ###多边形网格

                model.reinit(mesh)  ###多边形网格给进空间
                aopts['data']['mu'] = np.zeros((model.gdof, 2))
                aopts['data']['mu'][:, 0] = model.vemspace.interpolation(
                    S0, aopts['HB'])
                aopts['data']['mu'][:, 1] = model.vemspace.interpolation(
                    S1, aopts['HB'])
                problem['x0'] = aopts['data']['mu']

                optalg = SteepestDescentAlg(problem, options)
                x, f, g, diff = optalg.run(maxit=200)
                problem['mesh'] = mesh  ###多边形网格
                problem['x0'] = x
                problem['rho'] = model.rho
                self.problem = problem
                q = np.zeros(model.rho.shape)

                q[:, 0] = model.q0[:, -1]
                q[:, 1] = model.q1[:, -1]

                if diff < options['FunValDiff']:
                    if (np.max(problem['rho'][:, 0]) < 1) and (np.min(
                            problem['rho'][:, 0]) > 0):
                        break

            myfile = open(
                moptions['rdir'] + '/' + str(int(moptions['chiN'])) +
                'mesh.bin', 'wb')
            import pickle
            pickle.dump(problem['mesh'], myfile)
            myfile.close()
            model.save_data(moptions['rdir'] + '/' +
                            str(int(moptions['chiN'])) + '.mat')

            moptions['chiN'] += 5
            if moptions['chiN'] > 60:
                break
示例#24
0
    def adaptive_poly(self, plot=True):
        """"
        initial mesh
        """
        node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0),
                         (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0),
                         (2.0, 2.0)],
                        dtype=np.float)
        cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5],
                        dtype=np.int)
        cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

        mesh = PolygonMesh(node, cell, cellLocation)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)

        #fig = plt.figure()
        #axes = fig.gca()
        #mesh.add_plot(axes)
        #mesh.find_node(axes, showindex=True)
        #mesh.find_cell(axes, showindex=True)

        NE = mesh.number_of_edges()
        nC = mesh.number_of_cells()
        """
        refined mesh
        """
        aopts = mesh.adaptive_options(method='numrefine',
                                      maxcoarsen=3,
                                      HB=True)
        eta = [0, 0, 1, 1, 1]

        mesh.adaptive(eta, aopts)
        print('r', aopts['HB'])

        #fig = plt.figure()
        #axes = fig.gca()
        #mesh.add_plot(axes)
        #mesh.find_node(axes, showindex=True)
        #mesh.find_cell(axes, showindex=True)
        #plt.show()

        mesh.from_mesh(mesh)
        """
        coarsened mesh
        """
        eta = [0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1]
        #eta = [0,0,0,0,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2]

        mesh.adaptive(eta, aopts)
        print('c', aopts['HB'])
        print(mesh.ds.hcell)
        #fig = plt.figure()
        #axes = fig.gca()
        #mesh.add_plot(axes)
        #mesh.find_node(axes, showindex=True)
        #mesh.find_cell(axes, showindex=True)
        #plt.show()

        if plot:

            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_node(axes, showindex=True)
            #mesh.find_cell(axes, showindex=True)
            plt.show()
        if 0:
            NAC = mesh.number_of_all_cells()  # 包括外部区域和洞
            cindex = range(mesh.ds.cellstart, NAC)
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.find_cell(axes, showindex=True, multiindex=cindex)

            NN = mesh.number_of_nodes()
            nindex = np.zeros(NN, dtype=np.int)
            halfedge = mesh.ds.halfedge
            nindex[halfedge[:, 0]] = mesh.get_data('halfedge', 'level')
            cindex = mesh.get_data('cell', 'level')
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True, multiindex=nindex)
            mesh.find_cell(axes, showindex=True, multiindex=cindex)
            plt.show()
        else:
            return mesh
示例#25
0
    def refine_poly(self, plot=True):
        node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0),
                         (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0),
                         (2.0, 2.0)],
                        dtype=np.float)
        cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5],
                        dtype=np.int)
        cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int)

        mesh = PolygonMesh(node, cell, cellLocation)
        mesh = HalfEdgeMesh2d.from_mesh(mesh)

        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes)
        mesh.find_node(axes, showindex=True)
        mesh.find_cell(axes, showindex=True)

        NE = mesh.number_of_edges()
        NC = mesh.number_of_cells()

        if 1:
            isMarkedCell = mesh.mark_helper([2])
            mesh.refine_poly(isMarkedCell)

        if False:
            isMarkedCell = mesh.mark_helper([6])
            mesh.refine_poly(isMarkedCell, dflag=False)

        if False:
            isMarkedCell = mesh.mark_helper([3])
            mesh.refine_poly(isMarkedCell, dflag=False)

        if 1:
            isMarkedCell = mesh.mark_helper([1, 5])
            mesh.refine_poly(isMarkedCell)

        if False:
            isMarkedCell = mesh.mark_helper([1, 12])
            mesh.refine_poly(isMarkedCell, dflag=False)

        if False:
            isMarkedCell = mesh.mark_helper([0, 21])
            mesh.refine_poly(isMarkedCell, dflag=False)

        clevel = mesh.celldata['level']

        print("halfedge level:\n")
        for i, val in enumerate(mesh.halfedgedata['level']):
            print(i, ':', val, mesh.ds.halfedge[i, 0:2])

        print("cell level:\n")
        for i, val in enumerate(mesh.celldata['level']):
            print(i, ':', val)

        mesh.print()
        if plot:

            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.add_halfedge_plot(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)

            if 0:
                NAC = mesh.number_of_all_cells()  # 包括外部区域和洞
                cindex = range(mesh.ds.cellstart, NAC)
                fig = plt.figure()
                axes = fig.gca()
                #mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True, multiindex=cindex)

                NN = mesh.number_of_nodes()
                nindex = np.zeros(NN, dtype=np.int)
                halfedge = mesh.ds.halfedge
                nindex[halfedge[:, 0]] = mesh.get_data('halfedge', 'level')
                cindex = mesh.get_data('cell', 'level')
                fig = plt.figure()
                axes = fig.gca()
                #mesh.add_plot(axes)
                mesh.find_node(axes, showindex=True, multiindex=nindex)
                #mesh.find_cell(axes, showindex=True, multiindex=cindex)
            plt.show()
        else:
            return mesh
示例#26
0
    def run(self, estimator='grad'):
        problem = self.problem
        options = self.optoptions
        moptions = self.moptions
        model = problem['objective']
        mesh = problem['mesh']
        fig = plt.figure()
        axes = fig.gca()
        mesh.add_plot(axes, cellcolor='w')
        plt.show()
        #plt.savefig('flower15.png')
        #plt.savefig('flower15.pdf')
        #plt.close()

        optalg = SteepestDescentAlg(problem, options)
        x, f, g, diff = optalg.run(maxit=500)

        q = np.zeros(model.rho.shape)
        while True:
            print('chiN', moptions['chiN'])
            if moptions['chiN'] > 15:
                optalg = SteepestDescentAlg(problem, options)
                x, f, g, diff = optalg.run(maxit=10, eta_ref='etamaxmin')
            q = np.zeros(model.rho.shape)
            q[:, 0] = model.q0[:, -1]
            q[:, 1] = model.q1[:, -1]
            problem['x0'] = x
            while True:
                mesh = problem['mesh']
                hmesh = HalfEdgeMesh2d.from_mesh(mesh)
                aopts = hmesh.adaptive_options(method='mean',
                                               maxcoarsen=3,
                                               HB=True)
                print('NN', mesh.number_of_nodes())

                mu = problem['x0']
                if estimator == 'mix':
                    eta = model.mix_estimate(q, w=1)
                if estimator == 'grad':
                    eta = model.estimate(q)

                aopts['data'] = {'mu': mu}
                S0 = model.vemspace.project_to_smspace(aopts['data']['mu'][:,
                                                                           0])
                S1 = model.vemspace.project_to_smspace(aopts['data']['mu'][:,
                                                                           1])

                hmesh.adaptive(eta, aopts)
                mesh = PolygonMesh.from_halfedgemesh(hmesh)

                model.reinit(mesh)
                aopts['data']['mu'] = np.zeros((model.gdof, 2))
                aopts['data']['mu'][:, 0] = model.vemspace.interpolation(
                    S0, aopts['HB'])
                aopts['data']['mu'][:, 1] = model.vemspace.interpolation(
                    S1, aopts['HB'])
                problem['x0'] = aopts['data']['mu']

                optalg = SteepestDescentAlg(problem, options)
                x, f, g, diff = optalg.run(maxit=100)
                problem['mesh'] = mesh
                problem['x0'] = x
                problem['rho'] = model.rho
                q = np.zeros(model.rho.shape)

                q[:, 0] = model.q0[:, -1]
                q[:, 1] = model.q1[:, -1]

                if diff < options['FunValDiff']:
                    if (np.max(problem['rho'][:, 0]) < 1) and (np.min(
                            problem['rho'][:, 0]) > 0):
                        break
                myfile = open(
                    moptions['rdir'] + '/' + str(int(moptions['chiN'])) +
                    'mesh.bin', 'wb')
                import pickle
                pickle.dump(problem['mesh'], myfile)
                myfile.close()
                model.save_data(moptions['rdir'] + '/' +
                                str(int(moptions['chiN'])) + '.mat')

            moptions['chiN'] += 1
            if moptions['chiN'] > 60:
                break
示例#27
0
from HHOStokesSpace2d import HHOStokesSpace2d
from Stokes2DData import Stokes2DData_0


# --- begin --- #
n = 2
p = 1
nu = 1.0
pde = Stokes2DData_0(nu)  # create pde model

# --- mesh setting --- #
box = [0, 1, 0, 1]  # [0, 1]^2 domain
mf = MeshFactory()
meshtype = 'quad'
mesh = mf.boxmesh2d(box, nx=n, ny=n, meshtype=meshtype)
mesh = HalfEdgeMesh2d.from_mesh(mesh)
mesh.init_level_info()
mesh.uniform_refine(n-2)  # refine the mesh at beginning

# --- plot the mesh --- #
# fig = plt.figure()
# axes = fig.gca()
# mesh.add_plot(axes, cellcolor='w')
# find_entity(axes, mesh, entity='cell', showindex=True, color='b', markersize=10, fontsize=8)
# find_entity(axes, mesh, entity='edge', showindex=True, color='r', markersize=10, fontsize=8)
# find_entity(axes, mesh, entity='node', showindex=True, color='y', markersize=10, fontsize=8)
# plt.show()

cell2edge, cellLocation = mesh.ds.cell_to_edge()
# #
# #
示例#28
0
    def refine_tri(self, maxit=2, method='rg', plot=True, rb=True):
        cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                        dtype=np.int)
        node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                        dtype=np.float)

        if False:
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh.from_mesh(mesh)
            mesh.ds.cell2hedge = np.array([0, 3, 2, 11, 10])
            isMarkedCell = np.array([0, 1, 0, 0, 1], dtype=np.bool_)
            #mesh.refine_triangle_rbg(isMarkedCell)

            mesh.ds.NV = 3
            cell = mesh.ds.cell_to_node()
            node = mesh.entity('node')

            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            if False:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()

            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            if method == 'rg':
                color[[4, 13, 17, 28]] = 1
                color[[23, 27]] = 2
                color[[22, 26]] = 3
                mesh.hedgecolor = color
                isMarkedCell = np.array(
                    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool_)
                mesh.refine_triangle_rg(isMarkedCell)
            else:
                color[[2, 3, 10, 11]] = 1
                mesh.hedgecolor = color
                isMarkedCell = np.array([0, 1, 1, 0, 0], dtype=np.bool_)
                #isMarkedCell = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ,0],
                #       dtype=np.bool_)
                mesh.refine_triangle_nvb(isMarkedCell)
                mesh.print()
            if plot:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                mesh.add_halfedge_plot(axes, showindex=True)
                mesh.find_node(axes, showindex=True)
                mesh.find_cell(axes, showindex=True)
                plt.show()
        if True:
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            if method == 'nvb':
                color[[2, 3, 10, 11]] = 1
            mesh.hedgecolor = color
            c = np.array([0.8, 0.8])
            r = 0.9
            h = 1e-2
            k = 0
            NB = 0
            start = time.time()
            while k < maxit:
                halfedge = mesh.ds.halfedge
                halfedge1 = halfedge[:, 3]
                node = mesh.node
                flag = node - c
                flag = flag[:, 0]**2 + flag[:, 1]**2
                flag = flag <= r**2
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[halfedge1, 0]].astype(int)
                markedge = flag1 + flag2 == 1
                markedcell = halfedge[markedge, 1]
                markedcell = np.unique(markedcell)
                cell = np.unique(halfedge[:, 1])
                nc = cell.shape[0]
                markedcell1 = np.zeros(nc)
                markedcell1[markedcell] = 1
                if method == 'rg':
                    mesh.refine_triangle_rg(markedcell1.astype(np.bool_))
                else:
                    mesh.refine_triangle_nvb(markedcell1.astype(np.bool_))
                k += 1
                print('循环', k, '次***************************')
            end = time.time()
            print('用时', end - start)
            if plot:
                fig = plt.figure()
                axes = fig.gca()
                nindex = mesh.nodedata['level']
                mesh.add_plot(axes)
                #mesh.add_halfedge_plot(axes, showindex=True)
                #mesh.find_node(axes, showindex=True, multiindex=nindex)
                #mesh.find_cell(axes, showindex=True)
                #print(np.c_[np.arange(len(mesh.hedgecolor)), mesh.hedgecolor])
                plt.show()