How to use the petsc4py.PETSc.InsertMode.INSERT_VALUES 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 MiroK / fenics_ii / xii / assembler / nonconforming_trace_matrix.py View on Github external
# Set the dof cell                
                V_basis_function.cell = c
                    
                Vdofs = np.array(Vdm.cell_dofs(c), dtype='int32')  # These are columns
                    
                # Fill column
                for local_V, Vdof in enumerate(Vdofs):
                    # Set as basis_function
                    V_basis_function.dof = local_V

                    # Evaluate trace dof at basis function
                    dof_value = T_degree_of_freedom.eval(V_basis_function)

                    col_values[local_V] = dof_value

                mat.setValues([Tdof], Vdofs, col_values, PETSc.InsertMode.INSERT_VALUES)
    return mat
github MiroK / fenics_ii / fenics_ii / trace_tools / pcstr.py View on Github external
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)))

    return map(lambda x: PETScMatrix(x), mats),\
           map(lambda x: PETScMatrix(x), matsT),\
           PETScVector(rhs)
github MiroK / fenics_ii / xii / assembler / point_trace_matrix.py View on Github external
with petsc_serial_matrix(TV, V) as mat:

        # Scalar gets all
        if value_size == 1:
            component_dofs = lambda component: V.dofmap().cell_dofs(cell)
        # Slices
        else:
            component_dofs = lambda component: V.sub(component).dofmap().cell_dofs(cell)
        
        for row in map(int, TV.dofmap().cell_dofs(cell)):  # R^n components
            sub_dofs = component_dofs(row)
            sub_dofs_local = [all_dofs.index(dof) for dof in sub_dofs]
            print row, sub_dofs, sub_dofs_local, basis_values[sub_dofs_local]
            
            mat.setValues([row], sub_dofs, basis_values[sub_dofs_local],
                          PETSc.InsertMode.INSERT_VALUES)
    return mat
github MiroK / fenics_ii / fenics_ii / trace_tools / pipe_surf_avg.py View on Github external
# Add
                for col, value in zip(cols_k, values_k):
                    if col in cols:
                        cols[col] += value
                    else:
                        cols[col] = value
            # Time to assign
            column_indices, column_values = [], []
            for c, v in cols.items(): 
                column_indices.append(c)
                column_values.append(v)
            # Convert
            column_indices = np.array(column_indices, dtype='int32')
            column_values = np.array(column_values, dtype='double')
            row_indices = [row]
            mat.setValues(row_indices, column_indices, column_values, PETSc.InsertMode.INSERT_VALUES)
    mat.assemblyEnd()
    info('\tDone in %g' % timer.stop())

    return PETScMatrix(mat)
github MiroK / fenics_ii / xii / assembler / interpolation_matrix.py View on Github external
cell_rows = Q_dm.cell_dofs(cell)
            column_indices = np.array(V_dm.cell_dofs(cell), dtype='int32')
            for local_row, row in enumerate(cell_rows):
                if visited_rows[row]: continue
                
                visited_rows[row] = True
                # Define dof
                Q_dof.dof = local_row

                # Eval at V basis functions
                for local_col, col in enumerate(column_indices):
                    # Set which basis foo
                    V_basis_f.dof = local_col
                    column_values[local_col] = Q_dof.eval(V_basis_f)
                # Can fill the matrix now
                mat.setValues([row], column_indices, column_values, PETSc.InsertMode.INSERT_VALUES)
    return mat
github MiroK / fenics_ii / xii / assembler / normal_average_matrix.py View on Github external
Vel.evaluate_basis_all(basis_values, ip, vertex_coordinates, cell_orientation)

                    cols_ip = V_dm.cell_dofs(c)
                    values_ip = basis_values*wq[index]
                    # Add
                    for col, value in zip(cols_ip, values_ip.reshape((-1, value_size))):
                        if col in data:
                            data[col] += value.dot(normal_ip)/curve_measure
                        else:
                            data[col] = value.dot(normal_ip)/curve_measure
                            
                # The thing now that with data we can assign to several
                # rows of the matrix
                column_indices = np.array(data.keys(), dtype='int32')
                column_values = np.array([TV_dof.eval(Constant(data[col])) for col in column_indices])
                mat.setValues([row], column_indices, column_values, PETSc.InsertMode.INSERT_VALUES)
            # On to next avg point
        # On to next cell
    return PETScMatrix(mat)
github MiroK / fenics_ii / xii / assembler / restriction_matrix.py View on Github external
visited_dofs[dof_T] = True

                # Define trace dof
                TV_dof.dof = local_T
                
                # Eval at V basis functions
                for local, dof in enumerate(dofs):
                    # Set which basis foo
                    V_basis_f.dof = local
                    
                    dof_values[local] = TV_dof.eval(V_basis_f)

                # Can fill the matrix now
                col_indices = np.array(dofs, dtype='int32')
                # Insert
                mat.setValues([dof_T], col_indices, dof_values, PETSc.InsertMode.INSERT_VALUES)
    return mat
github MiroK / fenics_ii / xii / assembler / trace_matrix.py View on Github external
# Two sweeps to set the values in the row
                ADD_VALUES = False
                for modify, cell in zip(modifiers, facet_cells):
                    V_basis_f.cell = cell
                    dofs = dmap.cell_dofs(cell)

                    # Eval at V basis functions
                    for local, dof in enumerate(dofs):
                        # Set which basis foo
                        V_basis_f.dof = local
                        dof_values[local] = modify(TV_dof.eval(V_basis_f))
                    # Can fill the matrix now
                    col_indices = np.array(dofs, dtype='int32')

                    if not ADD_VALUES:
                        mat.setValues([dof_T], col_indices, dof_values, PETSc.InsertMode.INSERT_VALUES)
                        ADD_VALUES = True
                        #print 'setting', dof_T, col_indices, dof_values
                    else:
                        mat.setValues([dof_T], col_indices, dof_values, PETSc.InsertMode.ADD_VALUES)
                        #print 'adding', dof_T, col_indices, dof_values
    return mat
github OP2 / PyOP2 / pyop2 / petsc_base.py View on Github external
def set_values(self, rows, cols, values):
        """Set a block of values in the :class:`Mat`."""
        self.change_assembly_state(Mat.INSERT_VALUES)
        if len(values) > 0:
            self.handle.setValuesBlockedLocal(rows, cols, values,
                                              addv=PETSc.InsertMode.INSERT_VALUES)
github MiroK / fenics_ii / xii / linalg / convert.py View on Github external
row_offsets = np.cumsum([0] + list(row_sizes))
    col_offsets = np.cumsum([0] + list(col_sizes))

    with petsc_serial_matrix(row_offsets[-1], col_offsets[-1]) as mat:
        row = 0
        for row_blocks in bmat.blocks:
            # Zip the row iterators of the matrices together
            for indices_values in itertools.izip(*map(iter_rows, row_blocks)):
                indices, values = zip(*indices_values)

                indices = [list(index+offset) for index, offset in zip(indices, col_offsets)]
                indices = sum(indices, [])
            
                row_values = np.hstack(values)

                mat.setValues([row], indices, row_values, PETSc.InsertMode.INSERT_VALUES)

                row += 1
    return PETScMatrix(mat)