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 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
83 MatView(matrix,PETSC_VIEWER_STDOUT_WORLD);
91 if (row >= lo && row < hi)
94 MatGetSize(matrix, &rows, &cols);
115 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 1) //PETSc 3.1 or later
116 MatSetOption(matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
117 #elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 0) //PETSc 3.0
118 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
120 MatSetOption(matrix, MAT_KEEP_ZEROED_ROWS);
124 for (
unsigned i=0; i<rRows.size(); i++)
128 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
130 ISCreateGeneral(PETSC_COMM_WORLD, rRows.size(), rows, &is);
131 MatZeroRows(matrix, is, &diagonalValue);
175 #elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
176 MatZeroRows(matrix, rRows.size(), rows, diagonalValue , NULL, NULL);
178 MatZeroRows(matrix, rRows.size(), rows, diagonalValue);
189 std::sort(rowColIndices.begin(), rowColIndices.end());
193 unsigned size = hi-lo;
195 std::vector<unsigned>* cols_to_zero_per_row =
new std::vector<unsigned>[size];
199 for (
PetscInt row = lo; row < hi; row++)
204 MatGetRow(matrix, row, &num_cols, &cols, PETSC_NULL);
209 if(std::binary_search(rowColIndices.begin(), rowColIndices.end(), cols[i]))
211 cols_to_zero_per_row[row-lo].push_back(cols[i]);
216 MatRestoreRow(matrix, row, &num_cols, &cols, PETSC_NULL);
220 for (
PetscInt row = lo; row < hi; row++)
222 unsigned num_cols_to_zero_this_row = cols_to_zero_per_row[row-lo].size();
224 if(num_cols_to_zero_this_row>0)
227 double* zeros =
new double[num_cols_to_zero_this_row];
228 for(
unsigned i=0; i<num_cols_to_zero_this_row; i++)
230 cols_to_zero[i] = cols_to_zero_per_row[row-lo][i];
236 MatSetValues(matrix, 1, rows, num_cols_to_zero_this_row, cols_to_zero, zeros, INSERT_VALUES);
237 delete [] cols_to_zero;
242 delete [] cols_to_zero_per_row;
256 std::vector<unsigned> nonzero_rows;
257 for (
PetscInt row = lo; row < hi; row++)
261 nonzero_rows.push_back(row);
266 unsigned size = nonzero_rows.size();
269 double* zeros =
new double[size];
273 for (
unsigned i=0; i<size; i++)
275 rows[i] = nonzero_rows[i];
279 MatSetValues(matrix, size, rows, 1, cols, zeros, INSERT_VALUES);
286 MatZeroEntries(matrix);
293 MatGetSize(matrix, &rows, &cols);
294 assert(rows == cols);
295 return (
unsigned) rows;
300 MatGetOwnershipRange(matrix, &lo, &hi);
308 assert(lo <= row && row < hi);
310 row_as_array[0] = row;
312 col_as_array[0] = col;
316 MatGetValues(matrix, 1, row_as_array, 1, col_as_array, ret_array);
323 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
324 MatSetOption(matrix, option, PETSC_TRUE);
326 MatSetOption(matrix, option);
345 const PetscScalar* values;
355 MatGetRow(matrix, rowIndex, &num_entries, &column_indices, &values);
356 VecSetValues(mat_ith_row, num_entries, column_indices, values, INSERT_VALUES);
359 VecAssemblyBegin(mat_ith_row);
360 VecAssemblyEnd(mat_ith_row);
364 MatRestoreRow(matrix, rowIndex, &num_entries, &column_indices, &values);
374 MatDuplicate(mat2, MAT_COPY_VALUES, &y);
376 double minus_one = -1.0;
377 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
379 MatAYPX(&minus_one, mat1, y);
380 #elif (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 1) //PETSc 2.3.1
382 MatAYPX(y, minus_one, mat1);
385 MatAYPX(y, minus_one, mat1, DIFFERENT_NONZERO_PATTERN);
388 MatNorm(y, NORM_INFINITY, &norm);
397 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
398 MatTranspose(matrix, MAT_INITIAL_MATRIX, &trans);
400 MatTranspose(matrix, &trans);
409 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later
410 MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);