Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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
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)
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
# 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)
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
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)
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
# 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
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)
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)