16 #include <deal.II/base/function.h> 17 #include <deal.II/base/quadrature.h> 18 #include <deal.II/base/work_stream.h> 19 #include <deal.II/base/geometry_info.h> 20 #include <deal.II/base/quadrature.h> 21 #include <deal.II/dofs/dof_handler.h> 22 #include <deal.II/dofs/dof_accessor.h> 23 #include <deal.II/dofs/dof_tools.h> 24 #include <deal.II/fe/fe.h> 25 #include <deal.II/fe/fe_values.h> 26 #include <deal.II/fe/mapping_q1.h> 27 #include <deal.II/grid/tria_iterator.h> 28 #include <deal.II/hp/fe_values.h> 29 #include <deal.II/hp/mapping_collection.h> 30 #include <deal.II/numerics/matrix_tools.h> 31 #include <deal.II/lac/vector.h> 32 #include <deal.II/lac/block_vector.h> 33 #include <deal.II/lac/full_matrix.h> 34 #include <deal.II/lac/sparse_matrix.h> 35 #include <deal.II/lac/block_sparse_matrix.h> 37 #ifdef DEAL_II_WITH_PETSC 38 # include <deal.II/lac/petsc_parallel_sparse_matrix.h> 39 # include <deal.II/lac/petsc_sparse_matrix.h> 40 # include <deal.II/lac/petsc_parallel_vector.h> 41 # include <deal.II/lac/petsc_parallel_block_sparse_matrix.h> 44 #ifdef DEAL_II_WITH_TRILINOS 45 # include <deal.II/lac/trilinos_sparse_matrix.h> 46 # include <deal.II/lac/trilinos_vector.h> 47 # include <deal.II/lac/trilinos_block_sparse_matrix.h> 48 # include <deal.II/lac/trilinos_parallel_block_vector.h> 59 DEAL_II_NAMESPACE_OPEN
68 template <
typename Iterator>
69 bool column_less_than(
const typename Iterator::value_type p,
70 const unsigned int column)
72 return (p.column() < column);
77 template <
typename number>
83 const bool eliminate_columns)
89 Assert (matrix.n() == matrix.m(),
94 if (boundary_values.size() == 0)
106 number first_nonzero_diagonal_entry = 1;
107 for (
unsigned int i=0; i<n_dofs; ++i)
108 if (matrix.diag_element(i) != number())
110 first_nonzero_diagonal_entry = matrix.diag_element(i);
115 typename std::map<types::global_dof_index,number>::const_iterator dof = boundary_values.begin(),
116 endd = boundary_values.end();
117 for (; dof != endd; ++dof)
127 p = matrix.begin(dof_number);
128 p != matrix.end(dof_number); ++p)
129 if (p->column() != dof_number)
146 if (matrix.diag_element(dof_number) != number())
148 new_rhs = dof->second * matrix.diag_element(dof_number);
149 right_hand_side(dof_number) = new_rhs;
153 matrix.set (dof_number, dof_number,
154 first_nonzero_diagonal_entry);
155 new_rhs = dof->second * first_nonzero_diagonal_entry;
156 right_hand_side(dof_number) = new_rhs;
167 if (eliminate_columns)
172 const number diagonal_entry = matrix.diag_element(dof_number);
183 q = matrix.begin(dof_number)+1;
184 q != matrix.end(dof_number); ++q)
192 const unsigned int column)
210 Assert ((p != matrix.end(row))
212 (p->column() == dof_number),
213 ExcMessage(
"This function is trying to access an element of the " 214 "matrix that doesn't seem to exist. Are you using a " 215 "nonsymmetric sparsity pattern? If so, you are not " 216 "allowed to set the eliminate_column argument of this " 217 "function, see the documentation."));
220 right_hand_side(row) -=
static_cast<number
>(p->value()) /
221 diagonal_entry * new_rhs;
229 solution(dof_number) = dof->second;
235 template <
typename number>
241 const bool eliminate_columns)
243 const unsigned int blocks = matrix.n_block_rows();
249 Assert (matrix.n_block_rows() == matrix.n_block_cols(),
251 Assert (matrix.get_sparsity_pattern().get_row_indices() ==
252 matrix.get_sparsity_pattern().get_column_indices(),
254 Assert (matrix.get_sparsity_pattern().get_column_indices() ==
257 Assert (matrix.get_sparsity_pattern().get_row_indices() ==
263 if (boundary_values.size() == 0)
275 number first_nonzero_diagonal_entry = 0;
276 for (
unsigned int diag_block=0; diag_block<blocks; ++diag_block)
278 for (
unsigned int i=0; i<matrix.block(diag_block,diag_block).n(); ++i)
279 if (matrix.block(diag_block,diag_block).diag_element(i) != 0)
281 first_nonzero_diagonal_entry
282 = matrix.block(diag_block,diag_block).diag_element(i);
288 if (first_nonzero_diagonal_entry != 0)
293 if (first_nonzero_diagonal_entry == 0)
294 first_nonzero_diagonal_entry = 1;
297 typename std::map<types::global_dof_index,number>::const_iterator dof = boundary_values.begin(),
298 endd = boundary_values.end();
300 sparsity_pattern = matrix.get_sparsity_pattern();
311 for (; dof != endd; ++dof)
320 const std::pair<unsigned int,types::global_dof_index>
330 for (
unsigned int block_col=0; block_col<blocks; ++block_col)
332 p = (block_col == block_index.first ?
333 matrix.block(block_index.first,block_col).begin(block_index.second) + 1 :
334 matrix.block(block_index.first,block_col).begin(block_index.second));
335 p != matrix.block(block_index.first,block_col).end(block_index.second);
353 if (matrix.block(block_index.first, block_index.first)
354 .diag_element(block_index.second) != 0.0)
355 new_rhs = dof->second *
356 matrix.block(block_index.first, block_index.first)
357 .diag_element(block_index.second);
360 matrix.block(block_index.first, block_index.first)
361 .diag_element(block_index.second)
362 = first_nonzero_diagonal_entry;
363 new_rhs = dof->second * first_nonzero_diagonal_entry;
365 right_hand_side.
block(block_index.first)(block_index.second)
378 if (eliminate_columns)
383 const number diagonal_entry
384 = matrix.block(block_index.first,block_index.first)
385 .diag_element(block_index.second);
408 for (
unsigned int block_row=0; block_row<blocks; ++block_row)
413 = sparsity_pattern.
block (block_row, block_index.first);
416 = matrix.block(block_row, block_index.first);
418 = matrix.block(block_index.first, block_row);
424 q = (block_index.first == block_row ?
425 transpose_matrix.
begin(block_index.second)+1 :
426 transpose_matrix.
begin(block_index.second));
427 q != transpose_matrix.
end(block_index.second);
439 const unsigned int column)
446 if (this_matrix.
begin(row)->column()
449 p = this_matrix.
begin(row);
452 this_matrix.
end(row),
458 this_matrix.
end(row),
472 Assert ((p->column() == block_index.second) &&
473 (p != this_matrix.
end(row)),
477 right_hand_side.
block(block_row)(row)
479 diagonal_entry * new_rhs;
488 solution.
block(block_index.first)(block_index.second) = dof->second;
496 template <
typename number>
499 const std::vector<types::global_dof_index> &local_dof_indices,
502 const bool eliminate_columns)
504 Assert (local_dof_indices.size() == local_matrix.
m(),
507 Assert (local_dof_indices.size() == local_matrix.
n(),
510 Assert (local_dof_indices.size() == local_rhs.
size(),
516 if (boundary_values.size() == 0)
542 number average_diagonal = 0;
543 const unsigned int n_local_dofs = local_dof_indices.size();
544 for (
unsigned int i=0; i<n_local_dofs; ++i)
546 const typename std::map<types::global_dof_index, number>::const_iterator
547 boundary_value = boundary_values.find (local_dof_indices[i]);
548 if (boundary_value != boundary_values.end())
552 for (
unsigned int j=0; j<n_local_dofs; ++j)
554 local_matrix(i,j) = 0;
561 if (local_matrix(i,i) == 0.)
565 if (average_diagonal == 0.)
567 unsigned int nonzero_diagonals = 0;
568 for (
unsigned int k=0; k<n_local_dofs; ++k)
569 if (local_matrix(k,k) != 0.)
571 average_diagonal += std::fabs(local_matrix(k,k));
574 if (nonzero_diagonals != 0)
575 average_diagonal /= nonzero_diagonals;
577 average_diagonal = 0;
583 if (average_diagonal == 0.)
584 average_diagonal = 1.;
586 local_matrix(i,i) = average_diagonal;
589 local_matrix(i,i) = std::fabs(local_matrix(i,i));
593 local_rhs(i) = local_matrix(i,i) * boundary_value->second;
597 if (eliminate_columns ==
true)
599 for (
unsigned int row=0; row<n_local_dofs; ++row)
602 local_rhs(row) -= local_matrix(row,i) * boundary_value->second;
603 local_matrix(row,i) = 0;
614 #include "matrix_tools.inst" 617 DEAL_II_NAMESPACE_CLOSE
Iterator lower_bound(Iterator first, Iterator last, const T &val)
const_iterator end() const
SparsityPatternType & block(const size_type row, const size_type column)
static ::ExceptionBase & ExcMessage(std::string arg1)
unsigned int global_dof_index
const BlockIndices & get_block_indices() const
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
const Accessor< number, Constness > & value_type
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcBlocksDontMatch()
const_iterator begin() const
BlockType & block(const unsigned int i)
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
const BlockIndices & get_column_indices() const
static ::ExceptionBase & ExcInternalError()