Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_local_diagonal_entries(self, rows, diag_val=1.0, idx=None):
rows = np.asarray(rows, dtype=IntType)
rbs, _ = self.dims[0][0]
if rbs > 1:
if idx is not None:
rows = rbs * rows + idx
else:
rows = np.dstack([rbs*rows + i for i in range(rbs)]).flatten()
rows = rows.reshape(-1, 1)
self.change_assembly_state(Mat.INSERT_VALUES)
if len(rows) > 0:
values = np.full(rows.shape, diag_val, dtype=ScalarType)
self.handle.setValuesLocalRCV(rows, rows, values,
addv=PETSc.InsertMode.INSERT_VALUES)
def scatter_reverse_add(self):
self.ghostUpdateBegin(p4pyPETSc.InsertMode.ADD_VALUES,p4pyPETSc.ScatterMode.REVERSE)
self.ghostUpdateEnd(p4pyPETSc.InsertMode.ADD_VALUES,p4pyPETSc.ScatterMode.REVERSE)
def scatter_reverse_add(self):
if self.proteus2petsc_subdomain is not None:
self.proteus_array[:] = self.proteus_array[self.petsc2proteus_subdomain]
self.ghostUpdateBegin(p4pyPETSc.InsertMode.ADD_VALUES,p4pyPETSc.ScatterMode.REVERSE)
self.ghostUpdateEnd(p4pyPETSc.InsertMode.ADD_VALUES,p4pyPETSc.ScatterMode.REVERSE)
if self.proteus2petsc_subdomain is not None:
self.proteus_array[:] = self.proteus_array[self.proteus2petsc_subdomain]
out_index = vec.getValue(i)
vec_i_processor = mpi_comm.rank
vec_i_processor = mpi_comm.allreduce(vec_i_processor, op=MAX)
assert vec_i_processor >= 0
out_reduced_i_processor = -1
if reduced_i >= out_row_start and reduced_i < out_row_end:
out_reduced_i_processor = mpi_comm.rank
out_reduced_i_processor = mpi_comm.allreduce(vec_i_processor, op=MAX)
assert out_reduced_i_processor >= 0
if vec_i_processor != out_reduced_i_processor:
if mpi_comm.rank == vec_i_processor:
mpi_comm.send(out_index, dest=out_reduced_i_processor)
if mpi_comm.rank == out_reduced_i_processor:
out_index = mpi_comm.recv(source=vec_i_processor)
if mpi_comm.rank == out_reduced_i_processor:
out.setValues(reduced_i, out_index, addv=PETSc.InsertMode.INSERT)
out.assemble()
out.ghostUpdate()
def gather_to_zero(pvec):
"""
Return the global PETSc vector, corresponding to `pvec`, on the task of
rank zero. The vector is reused between calls!
"""
g20.scatter(pvec, pvec_full, PETSc.InsertMode.INSERT,
PETSc.ScatterMode.FORWARD)
return pvec_full
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
m = len(I)
I_perp = set(map(long, range(0, n))).difference(set(I))
k = len(I_perp)
assert m + k == n
E_perp = PETSc.Mat()
E_perp.createAIJ(size=(n, k), nnz=1)
E_perp.setUp()
for col, row in enumerate(I_perp):
rows = [row]
cols = [col]
values = [1.]
E_perp.setValues(rows, cols, values, PETSc.InsertMode.INSERT_VALUES)
E_perp.assemble()
# Reduce as Ep.T.(A.Ep)
R0, RA = PETSc.Mat(), PETSc.Mat()
A.matMult(E_perp, R0)
E_perp.transposeMatMult(R0, RA)
RA = PETScMatrix(RA)
if M is not None:
RM = PETSc.Mat()
M.matMult(E_perp, R0)
E_perp.transposeMatMult(R0, RM)
RM = PETScMatrix(RM)
# Solve the reduced eigenvalue problem
info('Solving reduced GEVP of size %d' % RA.size(0))
fine_dofs = fdmap.cell_dofs(f_cell)
for local, dof in enumerate(fine_dofs):
if visited_rows[dof]:
continue
else:
visited_rows[dof] = True
Vf_dof.dof = local
# Evaluete coarse basis foos here
for local_c, dof_c in enumerate(coarse_dofs):
Vc_basis_f.dof = local_c
row_values[local_c] = Vf_dof.eval(Vc_basis_f)
# Insert
mat.setValues([dof], coarse_dofs, row_values, PETSc.InsertMode.INSERT_VALUES)
return PETScMatrix(mat)