How to use the petsc4py.PETSc.LGMap function in petsc4py

To help you get started, we’ve selected a few petsc4py examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OP2 / PyOP2 / pyop2 / petsc_base.py View on Github external
def scalar_lgmap(self):
        if self.cdim == 1:
            return self.lgmap
        indices = self.lgmap.block_indices
        return PETSc.LGMap().create(indices=indices, bsize=1, comm=self.comm)
github MiroK / fenics_ii / fenics_ii / trace_tools / trace_matrices.py View on Github external
Vdofmap = V.dofmap()
    Vfirst, Vlast = Vdofmap.ownership_range()
    Vel = V.element()

    comm = Vmesh.mpi_comm().tompi4py()
    mat = PETSc.Mat()
    mat.create(comm)
    mat.setSizes([[Qdofmap.index_map().size(IndexMap.MapSize_OWNED),
                   Qdofmap.index_map().size(IndexMap.MapSize_GLOBAL)],
                  [Vdofmap.index_map().size(IndexMap.MapSize_OWNED),
                   Vdofmap.index_map().size(IndexMap.MapSize_GLOBAL)]])
    mat.setType('aij')
    mat.setUp()
    # Local to global
    row_lgmap = PETSc.LGMap().create(map(int, Qdofmap.tabulate_local_to_global_dofs()), comm=comm)
    col_lgmap = PETSc.LGMap().create(map(int, Vdofmap.tabulate_local_to_global_dofs()), comm=comm)
    mat.setLGMap(row_lgmap, col_lgmap)
    # Qdof -> Qcell -> Vcell -> Vdofs: evaluate basis functions Vdofs at Qdof => a col

    mat.assemblyBegin()
    # FIXME: REALLY ONLY SERIAL
    for comp in range(nsubs):
        V_comp_map = V.sub(comp).dofmap()

        Vel = V.sub(comp).element()
        space_dim = Vel.space_dimension()
        basis_values = np.zeros(space_dim)

        for Qdof in Q.sub(comp).dofmap().dofs():

            # Dofs are point evaluations
            x = Qdofsx[Qdof]
github MiroK / fenics_ii / fenics_ii / trace_tools / trace_assembler.py View on Github external
restriction=analysis['restriction'],
                     emesh=embedded_mesh)
    # Combine based on whether V -> Q or Q -> V (its transpose was requested)
    A = PETSc.Mat()
    M.matMult(T, A)

    if analysis['transpose']: 
        A = A.transpose(PETSc.Mat())
        # NOTE: without setting the dofmaps again we get PETSc error code 73.
        # rows are from space, cols aren't from trace space but from the space
        # due to the second argument
        from_space, to_space = analysis['space'], analysis['range']
        from_map, to_map = from_space.dofmap(), to_space.dofmap()
        comm = from_space.mesh().mpi_comm().tompi4py()
        # Local to global
        row_lgmap = PETSc.LGMap().create(map(int, from_map.tabulate_local_to_global_dofs()), comm=comm)
        col_lgmap = PETSc.LGMap().create(map(int, to_map.tabulate_local_to_global_dofs()), comm=comm)
        A.setLGMap(row_lgmap, col_lgmap)
    
    return PETScMatrix(A)
github OP2 / PyOP2 / pyop2 / petsc_base.py View on Github external
def lgmap(self):
        """A PETSc LGMap mapping process-local indices to global
        indices for this :class:`DataSet`.
        """
        lgmap = PETSc.LGMap()
        lgmap.create(indices=np.arange(1, dtype=IntType),
                     bsize=self.cdim, comm=self.comm)
        return lgmap
github MiroK / fenics_ii / fenics_ii / trace_tools / pcstr.py View on Github external
mats, matsT = [], []
    # We now build matrix for EACH space
    for index in entries:
        space = spaces[index]
        # Build the matrixG
        comm = space.mesh().mpi_comm().tompi4py()
        mat = PETSc.Mat()
        mat.create(comm)
        mat.setSizes([len(relations), space.dim()])
        mat.setType('aij')
        mat.setUp()
        # Local to global
        row_lgmap = PETSc.LGMap().create(map(int, np.arange(len(relations))), comm=comm)

        col_lgmap = space.dofmap().tabulate_local_to_global_dofs().tolist()
        col_lgmap = PETSc.LGMap().create(map(int, col_lgmap), comm=comm)
        mat.setLGMap(row_lgmap, col_lgmap)

        mat.assemblyBegin()
        for row, columns, values in entries[index]:
            mat.setValues([row], columns, values, PETSc.InsertMode.INSERT_VALUES)
        mat.assemblyEnd()

        matT = PETSc.Mat()
        mat.transpose(matT)
        matT.setLGMap(col_lgmap, row_lgmap)

        mats.append(mat)
        matsT.append(matT)

    rhs = PETSc.Vec().createWithArray(np.zeros(len(relations)))
github erdc / proteus / src / LinearAlgebraTools.py View on Github external
self.setSizes([[par_n*blockSize,par_N*blockSize],[par_n*blockSize,par_N*blockSize]],bsize=1)
            if blockSize > 1: #have to build in block dofs
                subdomain2globalTotal = numpy.zeros((blockSize*subdomain2global.shape[0],),'i')
                for j in range(blockSize):
                    subdomain2globalTotal[j::blockSize]=subdomain2global*blockSize+j
                self.subdomain2global=subdomain2globalTotal
            else:
                self.subdomain2global=subdomain2global
        import Comm
        comm = Comm.get()
        logEvent("ParMat_petsc4py comm.rank= %s blockSize = %s par_n= %s par_N=%s par_nghost=%s par_jacobian.getSizes()= %s "
                 % (comm.rank(),blockSize,par_n,par_N,par_nghost,self.getSizes()))
        self.csr_rep = ghosted_csr_mat.getCSRrepresentation()
        blockOwned = blockSize*par_n
        self.csr_rep_owned = ghosted_csr_mat.getSubMatCSRrepresentation(0,blockOwned)
        self.petsc_l2g = p4pyPETSc.LGMap()
        self.petsc_l2g.create(self.subdomain2global)
        self.colind_global = self.petsc_l2g.apply(self.csr_rep_owned[1]) #prealloc needs global indices
        self.setPreallocationCSR([self.csr_rep_owned[0],self.colind_global,self.csr_rep_owned[2]])
        self.setUp()
        self.setLGMap(self.petsc_l2g)
        self.setFromOptions()
github erdc / proteus / proteus / LinearAlgebraTools.py View on Github external
self.subdomain2global=subdomain2globalTotal
            else:
                self.subdomain2global=subdomain2global
        from proteus import Comm
        comm = Comm.get()
        logEvent("ParMat_petsc4py comm.rank= %s blockSize = %s par_n= %s par_N=%s par_nghost=%s par_jacobian.getSizes()= %s "
                 % (comm.rank(),self.blockSize,par_n,par_N,par_nghost,self.getSizes()))
        self.csr_rep = ghosted_csr_mat.getCSRrepresentation()
        if self.proteus_jacobian != None:
            self.proteus_csr_rep = self.proteus_jacobian.getCSRrepresentation()
        if self.blockSize > 1:
            blockOwned = self.blockSize*par_n
            self.csr_rep_local = ghosted_csr_mat.getSubMatCSRrepresentation(0,blockOwned)
        else:
            self.csr_rep_local = ghosted_csr_mat.getSubMatCSRrepresentation(0,par_n)
        self.petsc_l2g = p4pyPETSc.LGMap()
        self.petsc_l2g.create(self.subdomain2global)
        self.setUp()
        self.setLGMap(self.petsc_l2g)
        #
        self.colind_global = self.petsc_l2g.apply(self.csr_rep_local[1]) #prealloc needs global indices
        self.setPreallocationCSR([self.csr_rep_local[0],self.colind_global,self.csr_rep_local[2]])
        self.setFromOptions()
github MiroK / fenics_ii / xii / linalg / matrix_utils.py View on Github external
    lgmap = lambda indices: (PETSc.LGMap().create(indices, comm=comm)
                             if isinstance(indices, list)
                             else
                             PETSc.LGMap().createIS(indices))
github MiroK / fenics_ii / fenics_ii / trace_tools / pipe_surf_avg.py View on Github external
Qdofm = Q.dofmap()                                       # rows
    Qdofs_x = Q.tabulate_dof_coordinates().reshape((-1, 3))  # determines L
    Qmesh_x = Qmesh.coordinates().reshape((-1, 3))           # for normal

    # Define the mat object ---------------------------------------------------
    comm = Vmesh.mpi_comm().tompi4py()
    mat = PETSc.Mat()
    mat.create(comm)
    mat.setSizes([[Qdofm.index_map().size(IndexMap.MapSize_OWNED),
                   Qdofm.index_map().size(IndexMap.MapSize_GLOBAL)],
                  [Vdofm.index_map().size(IndexMap.MapSize_OWNED),
                   Vdofm.index_map().size(IndexMap.MapSize_GLOBAL)]])
    mat.setType('aij')
    mat.setUp()
    # Local to global
    row_lgmap = PETSc.LGMap().create(map(int, Qdofm.tabulate_local_to_global_dofs()), comm=comm)
    col_lgmap = PETSc.LGMap().create(map(int, Vdofm.tabulate_local_to_global_dofs()), comm=comm)
    mat.setLGMap(row_lgmap, col_lgmap)
    # -------------------------------------------------------------------------
    if isinstance(R, (int, float)): R = lambda x, R=R: R

    xq, wq = leggauss(deg)
    xq *= pi    # pi*(-1, 1)
    wq /= 2.    # And the end we want 0.5*\int{}...
    mat.assemblyBegin()
    for cell in cells(Qmesh):
        # Up to the shift x, L can be defined for entire segment
        v0, v1 = Qmesh_x[cell.entities(0)]
        n = v0 - v1

        t1 = np.array([n[1]-n[2], n[2]-n[0], n[0]-n[1]])
github OP2 / PyOP2 / pyop2 / petsc_base.py View on Github external
def masked_lgmap(lgmap, mask, block=True):
    if block:
        indices = lgmap.block_indices.copy()
        bsize = lgmap.getBlockSize()
    else:
        indices = lgmap.indices.copy()
        bsize = 1
    indices[mask] = -1
    return PETSc.LGMap().create(indices=indices, bsize=bsize, comm=lgmap.comm)