16 #include <deal.II/lac/petsc_sparse_matrix.h> 18 #ifdef DEAL_II_WITH_PETSC 20 # include <deal.II/lac/petsc_vector.h> 21 # include <deal.II/lac/sparsity_pattern.h> 22 # include <deal.II/lac/dynamic_sparsity_pattern.h> 24 DEAL_II_NAMESPACE_OPEN
31 const int m=0,
n=0, n_nonzero_per_row=0;
33 = MatCreateSeqAIJ(PETSC_COMM_SELF, m,
n, n_nonzero_per_row,
45 do_reinit (m, n, n_nonzero_per_row, is_symmetric);
52 const std::vector<size_type> &row_lengths,
55 do_reinit (m, n, row_lengths, is_symmetric);
60 template <
typename SparsityPatternType>
63 const bool preset_nonzero_locations)
65 do_reinit (sparsity_pattern, preset_nonzero_locations);
87 #if DEAL_II_PETSC_VERSION_LT(3,2,0) 88 const int ierr = MatDestroy (
matrix);
90 const int ierr = MatDestroy (&
matrix);
94 do_reinit (m, n, n_nonzero_per_row, is_symmetric);
102 const std::vector<size_type> &row_lengths,
107 #if DEAL_II_PETSC_VERSION_LT(3,2,0) 108 const int ierr = MatDestroy (
matrix);
110 const int ierr = MatDestroy (&
matrix);
114 do_reinit (m, n, row_lengths, is_symmetric);
119 template <
typename SparsityPatternType>
122 reinit (
const SparsityPatternType &sparsity_pattern,
123 const bool preset_nonzero_locations)
127 #if DEAL_II_PETSC_VERSION_LT(3,2,0) 128 const int ierr = MatDestroy (
matrix);
130 const int ierr = MatDestroy (&
matrix);
134 do_reinit (sparsity_pattern, preset_nonzero_locations);
142 static MPI_Comm comm;
143 PetscObjectGetComm((PetscObject)
matrix, &comm);
159 = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, n_nonzero_per_row,
164 if (is_symmetric ==
true)
166 #if DEAL_II_PETSC_VERSION_LT(3,0,0) 168 = MatSetOption (
matrix, MAT_SYMMETRIC);
171 = MatSetOption (
matrix, MAT_SYMMETRIC, PETSC_TRUE);
183 const std::vector<size_type> &row_lengths,
186 Assert (row_lengths.size() ==
m,
187 ExcDimensionMismatch (row_lengths.size(),
m));
197 const std::vector<PetscInt>
198 int_row_lengths (row_lengths.begin(), row_lengths.end());
201 = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, 0,
202 &int_row_lengths[0], &
matrix);
206 if (is_symmetric ==
true)
208 #if DEAL_II_PETSC_VERSION_LT(3,0,0) 210 = MatSetOption (
matrix, MAT_SYMMETRIC);
213 = MatSetOption (
matrix, MAT_SYMMETRIC, PETSC_TRUE);
222 template <
typename SparsityPatternType>
225 const bool preset_nonzero_locations)
227 std::vector<size_type> row_lengths (sparsity_pattern.n_rows());
228 for (
size_type i=0; i<sparsity_pattern.n_rows(); ++i)
229 row_lengths[i] = sparsity_pattern.row_length (i);
232 sparsity_pattern.n_cols(),
248 if (preset_nonzero_locations ==
true)
250 std::vector<PetscInt> row_entries;
251 std::vector<PetscScalar> row_values;
252 for (
size_type i=0; i<sparsity_pattern.n_rows(); ++i)
254 row_entries.resize (row_lengths[i]);
255 row_values.resize (row_lengths[i], 0.0);
256 for (
size_type j=0; j<row_lengths[i]; ++j)
257 row_entries[j] = sparsity_pattern.column_number (i,j);
259 const PetscInt int_row = i;
260 MatSetValues (
matrix, 1, &int_row,
261 row_lengths[i], &row_entries[0],
262 &row_values[0], INSERT_VALUES);
272 #if DEAL_II_PETSC_VERSION_LT(3,0,0) 274 ierr = MatSetOption (
matrix, MAT_NEW_NONZERO_LOCATION_ERR);
277 ierr = MatSetOption (
matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
282 ierr = MatSetOption (
matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
285 ierr = MatSetOption (
matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
297 #if DEAL_II_PETSC_VERSION_LT(3,0,0) 298 ierr = MatSetOption (
matrix, MAT_KEEP_ZEROED_ROWS);
300 #elif DEAL_II_PETSC_VERSION_LT(3,1,0) 301 ierr = MatSetOption (
matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
304 ierr = MatSetOption (
matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
354 DEAL_II_NAMESPACE_CLOSE
356 #endif // DEAL_II_WITH_PETSC
PetscScalar matrix_scalar_product(const VectorBase &u, const VectorBase &v) const
void vmult(VectorBase &dst, const VectorBase &src) const
#define AssertThrow(cond, exc)
PetscScalar matrix_norm_square(const VectorBase &v) const
PetscBool is_symmetric(const double tolerance=1.e-12)
void compress(const VectorOperation::values operation)
SparseMatrix & operator=(const double d)
void reinit(const size_type m, const size_type n, const size_type n_nonzero_per_row, const bool is_symmetric=false)
virtual const MPI_Comm & get_mpi_communicator() const
#define Assert(cond, exc)
MatrixBase & operator=(const value_type d)
types::global_dof_index size_type
void do_reinit(const size_type m, const size_type n, const size_type n_nonzero_per_row, const bool is_symmetric=false)