def help(args=None): import sys, shlex # program name try: prog = sys.argv[0] except Exception: prog = getattr(sys, 'executable', 'python') # arguments if args is None: args = sys.argv[1:] elif isinstance(args, str): args = shlex.split(args) else: args = [str(a) for a in args] # import and initialize import petsc4py petsc4py.init([prog, '-help'] + args) from petsc4py import PETSc # help dispatcher COMM = PETSc.COMM_SELF if 'vec' in args: vec = PETSc.Vec().create(comm=COMM) vec.setSizes(0) vec.setFromOptions() vec.destroy() if 'mat' in args: mat = PETSc.Mat().create(comm=COMM) mat.setSizes([0, 0]) mat.setFromOptions() mat.destroy() if 'pc' in args: pc = PETSc.PC().create(comm=COMM) pc.setFromOptions() pc.destroy() if 'ksp' in args: ksp = PETSc.KSP().create(comm=COMM) ksp.setFromOptions() ksp.destroy() if 'snes' in args: snes = PETSc.SNES().create(comm=COMM) snes.setFromOptions() snes.destroy() if 'ts' in args: ts = PETSc.TS().create(comm=COMM) ts.setFromOptions() ts.destroy() if 'tao' in args: tao = PETSc.TAO().create(comm=COMM) tao.setFromOptions() tao.destroy() if 'dmda' in args: dmda = PETSc.DMDA().create(comm=COMM) dmda.setFromOptions() dmda.destroy() if 'dmplex' in args: dmplex = PETSc.DMPlex().create(comm=COMM) dmplex.setFromOptions() dmplex.destroy()
def generateDMPlex(self, dh, dim=1): coords, cone, dl = self.generateBody(dh) self.__dl = dl self.__L = self.getLong() self.__dom = PETSc.DMPlex().createFromCellList(dim, cone, coords) self.setUpDimensions() points = self.getTotalNodes() ind = [ poi * 2 + dof for poi in range(points) for dof in range(len(self.__vel)) ] self.__velVec = PETSc.Vec().createMPI( ((points * len(self.__vel), None))) self.__velVec.setValues(ind, np.tile(self.__vel, points)) self.__velVec.assemble()
class TestPlex_2D_P3(BaseTestPlex_2D, unittest.TestCase): DOFS = [1, 2, 1] class TestPlex_3D_P3(BaseTestPlex_3D, unittest.TestCase): DOFS = [1, 2, 1, 0] class TestPlex_3D_P4(BaseTestPlex_3D, unittest.TestCase): DOFS = [1, 3, 3, 1] import sys try: raise PETSc.Error PETSc.DMPlex().createBoxMesh(1, comm=PETSc.COMM_SELF).destroy() except PETSc.Error: pass else: class TestPlex_2D_Box(BaseTestPlex_2D, unittest.TestCase): def setUp(self): self.plex = PETSc.DMPlex().createBoxMesh(self.DIM) class TestPlex_2D_Boundary(BaseTestPlex_2D, unittest.TestCase): def setUp(self): boundary = PETSc.DMPlex().create(self.COMM) boundary.createSquareBoundary([0., 0.], [1., 1.], [2, 2]) boundary.setDimension(self.DIM - 1) self.plex = PETSc.DMPlex().generate(boundary)
[1.0, 0.0], [0.0, 0.5], [0.5, 0.5], [1.0, 0.5], [0.0, 1.0], [0.5, 1.0], [1.0, 1.0]], dtype=float) cells = np.asarray([[0,1,4,3], [1,2,5,4], [3,4,7,6], [4,5,8,7]], dtype=PETSc.IntType) else: coords = np.zeros((0, 2), dtype=float) cells = np.zeros((0, 4), dtype=PETSc.IntType) plex = PETSc.DMPlex().createFromCellList(dim, cells, coords, comm=PETSc.COMM_WORLD) pStart, pEnd = plex.getChart() plex.view() print("pStart, pEnd: ", pStart, pEnd) # Create section with 1 field with 1 DoF per vertex, edge amd cell numComp = 1 # Start with an empty vector numDof = [0] * 3 # Field defined on vertexes numDof[0] = 1 # Field defined on edges numDof[1] = 1 # Field defined on cells numDof[2] = 1
def _create_DMPlex(self, dim, coords, cells, elev): """ Create a PETSc DMPlex object from the mesh attributes Args: dim: mesh dimensions coords: mesh coordinates cells: cell nodes indices elev: nodes elevation """ t0 = clock() self.dm = PETSc.DMPlex().createFromCellList(dim, cells, coords, comm=PETSc.COMM_WORLD) if MPIrank == 0 and self.verbose: print('Create DMPlex (%0.02f seconds)' % (clock() - t0)) # Create boundary labels t0 = clock() label = "boundary" self._set_DMPlex_boundary_points(label) # label coarse DM in case it is ever needed again self.dm.createLabel("coarse") pStart, pEnd = self.dm.getDepthStratum(0) for pt in range(pStart, pEnd): self.dm.setLabelValue("coarse", pt, 1) # Define one DoF on the nodes self.dm.setNumFields(1) origSect = self.dm.createSection(1, [1, 0, 0]) origSect.setFieldName(0, "points") origSect.setUp() self.dm.setDefaultSection(origSect) origVec = self.dm.createGlobalVector() # Distribute to other processors if any if MPIsize > 1: sf = self.dm.distribute(overlap=1) newSect, newVec = self.dm.distributeField(sf, origSect, origVec) self.dm.setDefaultSection(newSect) newSect.destroy() newVec.destroy() sf.destroy() origVec.destroy() origSect.destroy() self.hGlobal = self.dm.createGlobalVector() self.hLocal = self.dm.createLocalVector() self.sizes = self.hGlobal.getSizes(), self.hGlobal.getSizes() # Local/Global mapping self.lgmap_row = self.dm.getLGMap() l2g = self.lgmap_row.indices.copy() offproc = l2g < 0 l2g[offproc] = -(l2g[offproc] + 1) self.lgmap_col = PETSc.LGMap().create(l2g, comm=PETSc.COMM_WORLD) del l2g if MPIrank == 0 and self.verbose: print('Distribute DMPlex (%0.02f seconds)' % (clock() - t0)) # Get natural numbering t0 = clock() coords = MPI.COMM_WORLD.bcast(coords, root=0) elev = MPI.COMM_WORLD.bcast(elev, root=0) self._naturalNumbering(coords) self.hLocal.setArray(elev[self.natural2local]) self.dm.localToGlobal(self.hLocal, self.hGlobal) self.dm.globalToLocal(self.hGlobal, self.hLocal) if MPIrank == 0 and self.verbose: print('Distribute field to DMPlex (%0.02f seconds)' % (clock() - t0)) # Forcing event number self.rainNb = -1 self.tecNb = -1 return
return ret # Set metric parameters h_min = 1.0e-10 # Minimum tolerated metric magnitude ~ cell size h_max = 1.0e-01 # Maximum tolerated metric magnitude ~ cell size a_max = 1.0e+05 # Maximum tolerated anisotropy targetComplexity = 10000.0 # Analogous to number of vertices in adapted mesh p = 1.0 # Lᵖ normalization order # Create a uniform mesh OptDB = PETSc.Options() dim = OptDB.getInt('dim', 2) numEdges = 10 simplex = True plex = PETSc.DMPlex().createBoxMesh([numEdges] * dim, simplex=simplex) plex.distribute() plex.view() viewer = PETSc.Viewer().createVTK('base_mesh.vtk', 'w') viewer(plex) # Do four mesh adaptation iterations for i in range(4): vStart, vEnd = plex.getDepthStratum(0) # Create a P1 sensor function comm = plex.getComm() fe = PETSc.FE().createLagrange(dim, 1, simplex, 1, -1, comm=comm) plex.setField(0, fe) plex.createDS() f = plex.createLocalVector()
def regenerateDMPlex(self, dh, dim=1): coords, cone, _ = self.generateBody(dh) dm = PETSc.DMPlex().createFromCellList(dim, cone, coords) return dm