Esempio n. 1
0
    def __init__(self, radius=1, refinement_level=0, reorder=None):
        """
        :arg radius: the radius of the sphere to approximate.
             For a radius R the edge length of the underlying
             icosahedron will be.

             .. math::

                a = \\frac{R}{\\sin(2 \\pi / 5)}

        :arg refinement_level: how many levels of refinement, zero
                               corresponds to an icosahedron.
        :arg reorder: Should the mesh be reordered?
        """

        self.name = "icosahedralspheremesh_%d_%g" % (refinement_level, radius)

        self._R = radius
        self._refinement = refinement_level

        self._vertices = np.empty_like(IcosahedralSphereMesh._base_vertices)
        self._faces = np.copy(IcosahedralSphereMesh._base_faces)
        # Rescale so that vertices live on sphere of specified radius
        for i, vtx in enumerate(IcosahedralSphereMesh._base_vertices):
            self._vertices[i] = self._force_to_sphere(vtx)

        for i in range(refinement_level):
            self._refine()

        plex = dmplex._from_cell_list(2, self._faces, self._vertices)
        super(IcosahedralSphereMesh, self).__init__(self.name, plex=plex, dim=3, reorder=reorder)
Esempio n. 2
0
    def __init__(self, ncells, length):
        self.name = "interval"
        dx = length / ncells
        # This ensures the rightmost point is actually present.
        coords = np.arange(0, length + 0.01 * dx, dx).reshape(-1, 1)
        cells = np.dstack((np.arange(0, len(coords) - 1, dtype=np.int32),
                           np.arange(1, len(coords), dtype=np.int32))).reshape(-1, 2)
        plex = dmplex._from_cell_list(1, cells, coords)
        # Apply boundary IDs
        plex.createLabel("boundary_ids")
        coordinates = plex.getCoordinates()
        coord_sec = plex.getCoordinateSection()
        vStart, vEnd = plex.getDepthStratum(0)  # vertices
        for v in range(vStart, vEnd):
            vcoord = plex.vecGetClosure(coord_sec, coordinates, v)
            if vcoord[0] == coords[0]:
                plex.setLabelValue("boundary_ids", v, 1)
            if vcoord[0] == coords[-1]:
                plex.setLabelValue("boundary_ids", v, 2)

        super(IntervalMesh, self).__init__(self.name, plex=plex, reorder=False)
Esempio n. 3
0
 def __init__(self):
     self.name = "unittri"
     coords = [[0., 0.], [1., 0.], [0., 1.]]
     cells = [[1, 2, 0]]
     plex = dmplex._from_cell_list(2, cells, coords)
     super(UnitTriangleMesh, self).__init__(self.name, plex=plex)
Esempio n. 4
0
 def __init__(self):
     self.name = "unittetra"
     coords = [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]
     cells = [[1, 0, 3, 2]]
     plex = dmplex._from_cell_list(3, cells, coords)
     super(UnitTetrahedronMesh, self).__init__(self.name, plex=plex)
Esempio n. 5
0
    def _from_triangle(self, filename, dim=0, periodic_coords=None, reorder=None):
        """Read a set of triangle mesh files from `filename`"""
        self.name = filename
        basename, ext = os.path.splitext(filename)

        if op2.MPI.comm.rank == 0:
            try:
                facetfile = open(basename+".face")
                tdim = 3
            except:
                try:
                    facetfile = open(basename+".edge")
                    tdim = 2
                except:
                    facetfile = None
                    tdim = 1
            if dim == 0:
                dim = tdim
            op2.MPI.comm.bcast(tdim, root=0)

            with open(basename+".node") as nodefile:
                header = np.fromfile(nodefile, dtype=np.int32, count=2, sep=' ')
                nodecount = header[0]
                nodedim = header[1]
                assert nodedim == dim
                coordinates = np.loadtxt(nodefile, usecols=range(1, dim+1), skiprows=1, delimiter=' ')
                assert nodecount == coordinates.shape[0]

            with open(basename+".ele") as elefile:
                header = np.fromfile(elefile, dtype=np.int32, count=2, sep=' ')
                elecount = header[0]
                eledim = header[1]
                eles = np.loadtxt(elefile, usecols=range(1, eledim+1), dtype=np.int32, skiprows=1, delimiter=' ')
                assert elecount == eles.shape[0]

            cells = map(lambda c: c-1, eles)
        else:
            tdim = op2.MPI.comm.bcast(None, root=0)
            cells = None
            coordinates = None
        plex = dmplex._from_cell_list(tdim, cells, coordinates, comm=op2.MPI.comm)

        # Apply boundary IDs
        if op2.MPI.comm.rank == 0:
            facets = None
            try:
                header = np.fromfile(facetfile, dtype=np.int32, count=2, sep=' ')
                edgecount = header[0]
                facets = np.loadtxt(facetfile, usecols=range(1, tdim+2), dtype=np.int32, skiprows=0, delimiter=' ')
                assert edgecount == facets.shape[0]
            finally:
                facetfile.close()

            if facets is not None:
                vStart, vEnd = plex.getDepthStratum(0)   # vertices
                for facet in facets:
                    bid = facet[-1]
                    vertices = map(lambda v: v + vStart - 1, facet[:-1])
                    join = plex.getJoin(vertices)
                    plex.setLabelValue("boundary_ids", join[0], bid)

        self._from_dmplex(plex, dim, periodic_coords, reorder=None)