36 #include "PetscMatTools.hpp" 50 if (row >= lo && row < hi)
52 MatSetValue(matrix, row, col, value, INSERT_VALUES);
61 if (row >= lo && row < hi)
63 MatSetValue(matrix, row, col, value, ADD_VALUES);
69 MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY);
70 MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY);
75 MatAssemblyBegin(matrix, MAT_FLUSH_ASSEMBLY);
76 MatAssemblyEnd(matrix, MAT_FLUSH_ASSEMBLY);
82 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7+ 83 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
85 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
87 MatView(matrix,PETSC_VIEWER_STDOUT_WORLD);
88 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7+ 89 PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
98 if (row >= lo && row < hi)
101 MatGetSize(matrix, &rows, &cols);
122 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1) //PETSc 3.1 or later 123 MatSetOption(matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
124 #elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 0) //PETSc 3.0 125 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
127 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS);
131 for (
unsigned i=0; i<rRows.size(); i++)
135 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2 137 ISCreateGeneral(PETSC_COMM_WORLD, rRows.size(), rows, &is);
138 MatZeroRows(matrix, is, &diagonalValue);
182 #elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later 183 MatZeroRows(matrix, rRows.size(), rows, diagonalValue ,
nullptr,
nullptr);
185 MatZeroRows(matrix, rRows.size(), rows, diagonalValue);
196 std::sort(rowColIndices.begin(), rowColIndices.end());
200 unsigned size = hi-lo;
202 std::vector<unsigned>* cols_to_zero_per_row =
new std::vector<unsigned>[size];
206 for (
PetscInt row = lo; row < hi; row++)
211 MatGetRow(matrix, row, &num_cols, &cols, PETSC_NULL);
216 if (std::binary_search(rowColIndices.begin(), rowColIndices.end(), cols[i]))
218 cols_to_zero_per_row[row-lo].push_back(cols[i]);
223 MatRestoreRow(matrix, row, &num_cols, &cols, PETSC_NULL);
227 for (
PetscInt row = lo; row < hi; row++)
229 unsigned num_cols_to_zero_this_row = cols_to_zero_per_row[row-lo].size();
231 if (num_cols_to_zero_this_row>0)
234 double* zeros =
new double[num_cols_to_zero_this_row];
235 for (
unsigned i=0; i<num_cols_to_zero_this_row; i++)
237 cols_to_zero[i] = cols_to_zero_per_row[row-lo][i];
243 MatSetValues(matrix, 1, rows, num_cols_to_zero_this_row, cols_to_zero, zeros, INSERT_VALUES);
244 delete [] cols_to_zero;
249 delete [] cols_to_zero_per_row;
263 std::vector<unsigned> nonzero_rows;
264 for (
PetscInt row = lo; row < hi; row++)
268 nonzero_rows.push_back(row);
273 unsigned size = nonzero_rows.size();
276 double* zeros =
new double[size];
280 for (
unsigned i=0; i<size; i++)
282 rows[i] = nonzero_rows[i];
286 MatSetValues(matrix, size, rows, 1, cols, zeros, INSERT_VALUES);
293 MatZeroEntries(matrix);
300 MatGetSize(matrix, &rows, &cols);
301 assert(rows == cols);
302 return (
unsigned) rows;
307 MatGetOwnershipRange(matrix, &lo, &hi);
315 assert(lo <= row && row < hi);
317 row_as_array[0] = row;
319 col_as_array[0] = col;
323 MatGetValues(matrix, 1, row_as_array, 1, col_as_array, ret_array);
330 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x 331 MatSetOption(matrix, option, PETSC_TRUE);
333 MatSetOption(matrix, option);
352 const PetscScalar* values;
362 MatGetRow(matrix, rowIndex, &num_entries, &column_indices, &values);
363 VecSetValues(mat_ith_row, num_entries, column_indices, values, INSERT_VALUES);
366 VecAssemblyBegin(mat_ith_row);
367 VecAssemblyEnd(mat_ith_row);
371 MatRestoreRow(matrix, rowIndex, &num_entries, &column_indices, &values);
381 MatDuplicate(mat2, MAT_COPY_VALUES, &y);
383 double minus_one = -1.0;
384 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2 386 MatAYPX(&minus_one, mat1, y);
387 #elif (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 1) //PETSc 2.3.1 389 MatAYPX(y, minus_one, mat1);
392 MatAYPX(y, minus_one, mat1, DIFFERENT_NONZERO_PATTERN);
395 MatNorm(y, NORM_INFINITY, &norm);
404 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x 405 MatTranspose(matrix, MAT_INITIAL_MATRIX, &trans);
407 MatTranspose(matrix, &trans);
416 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later 417 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);