Debian Codegraph @

include/deal.II/lac/precondition_block.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1999 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_precondition_block_templates_h
16 #define dealii_precondition_block_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/memory_consumption.h>
23
24 #include <deal.II/lac/full_matrix.h>
25 #include <deal.II/lac/householder.h>
26 #include <deal.II/lac/precondition_block.h>
27 #include <deal.II/lac/vector.h>
28
29 DEAL_II_NAMESPACE_OPEN
30
31
32 template <typename MatrixType, typename inverse_type>
33 PreconditionBlock<MatrixType, inverse_type>::AdditionalData::AdditionalData(
34 const size_type block_size,
35 const double relaxation,
36 const bool invert_diagonal,
37 const bool same_diagonal)
38 : relaxation(relaxation)
39 , block_size(block_size)
40 , invert_diagonal(invert_diagonal)
41 , same_diagonal(same_diagonal)
42 , inversion(PreconditionBlockBase<inverse_type>::gauss_jordan)
43 , threshold(0.)
44 {}
45
46
47
48 template <typename MatrixType, typename inverse_type>
49 PreconditionBlock<MatrixType, inverse_type>::PreconditionBlock(bool store)
50 : PreconditionBlockBase<inverse_type>(store)
51 , blocksize(0)
52 , A(nullptr, typeid(*this).name())
53 , relaxation(1.0)
54 {}
55
56
57
58 template <typename MatrixType, typename inverse_type>
59 void
60 PreconditionBlock<MatrixType, inverse_type>::clear()
61 {
62 PreconditionBlockBase<inverse_type>::clear();
63 blocksize = 0;
64 A = nullptr;
65 }
66
67
68 template <typename MatrixType, typename inverse_type>
69 void
70 PreconditionBlock<MatrixType, inverse_type>::initialize(
71 const MatrixType &M,
72 const AdditionalData parameters)
73 {
74 const size_type bsize = parameters.block_size;
75
76 clear();
77 Assert(M.m() == M.n(), ExcNotQuadratic());
78 A = &M;
79 Assert(bsize > 0, ExcIndexRange(bsize, 1, M.m()));
80 Assert(A->m() % bsize == 0, ExcWrongBlockSize(bsize, A->m()));
81 blocksize = bsize;
82 relaxation = parameters.relaxation;
83 const unsigned int nblocks = A->m() / bsize;
84 this->reinit(nblocks,
85 blocksize,
86 parameters.same_diagonal,
87 parameters.inversion);
88
89 if (parameters.invert_diagonal)
90 {
91 if (permutation.size() == M.m())
92 invert_permuted_diagblocks();
93 else
94 invert_diagblocks();
95 }
96 }
97
98
99 template <typename MatrixType, typename inverse_type>
100 void
101 PreconditionBlock<MatrixType, inverse_type>::initialize(
102 const MatrixType &M,
103 const std::vector<size_type> &new_permutation,
104 const std::vector<size_type> &new_inverse_permutation,
105 const AdditionalData parameters)
106 {
107 set_permutation(new_permutation, new_inverse_permutation);
108 initialize(M, parameters);
109 }
110
111 template <typename MatrixType, typename inverse_type>
112 void
113 PreconditionBlock<MatrixType, inverse_type>::invert_permuted_diagblocks()
114 {
115 Assert(A != nullptr, ExcNotInitialized());
116 Assert(blocksize != 0, ExcNotInitialized());
117
118 const MatrixType &M = *A;
119 Assert(this->inverses_ready() == 0, ExcInverseMatricesAlreadyExist());
120 AssertDimension(permutation.size(), M.m());
121 AssertDimension(inverse_permutation.size(), M.m());
122
123 FullMatrix<inverse_type> M_cell(blocksize);
124
125 if (this->same_diagonal())
126 {
127 for (size_type row_cell = 0; row_cell < blocksize; ++row_cell)
128 {
129 typename MatrixType::const_iterator entry = M.begin(row_cell);
130 const typename MatrixType::const_iterator row_end = M.end(row_cell);
131 while (entry != row_end)
132 {
133 if (entry->column() < blocksize)
134 M_cell(row_cell, entry->column()) = entry->value();
135 ++entry;
136 }
137 }
138 if (this->store_diagonals())
139 this->diagonal(0) = M_cell;
140 switch (this->inversion)
141 {
142 case PreconditionBlockBase<inverse_type>::gauss_jordan:
143 this->inverse(0).invert(M_cell);
144 break;
145 case PreconditionBlockBase<inverse_type>::householder:
146 this->inverse_householder(0).initialize(M_cell);
147 break;
148 case PreconditionBlockBase<inverse_type>::svd:
149 this->inverse_svd(0) = M_cell;
150 this->inverse_svd(0).compute_inverse_svd(0.);
151 break;
152 default:
153 DEAL_II_NOT_IMPLEMENTED();
154 }
155 }
156 else
157 {
158 // cell_row, cell_column are the
159 // numbering of the blocks (cells).
160 // row_cell, column_cell are the local
161 // numbering of the unknowns in the
162 // blocks.
163 // row, column are the global numbering
164 // of the unknowns.
165 M_cell = 0;
166
167 for (unsigned int cell = 0; cell < this->size(); ++cell)
168 {
169 const size_type cell_start = cell * blocksize;
170 for (size_type row_cell = 0; row_cell < blocksize; ++row_cell)
171 {
172 const size_type urow = row_cell + cell_start;
173
174 const size_type row = permutation[urow];
175
176 typename MatrixType::const_iterator entry = M.begin(row);
177 const typename MatrixType::const_iterator row_end = M.end(row);
178
179 for (; entry != row_end; ++entry)
180 {
181 // if (entry->column()<cell_start)
182 if (inverse_permutation[entry->column()] < cell_start)
183 continue;
184
185 const size_type column_cell =
186 inverse_permutation[entry->column()] - cell_start;
187 if (column_cell >= blocksize)
188 continue;
189 M_cell(row_cell, column_cell) = entry->value();
190 }
191 }
192
193 if (this->store_diagonals())
194 this->diagonal(cell) = M_cell;
195 switch (this->inversion)
196 {
197 case PreconditionBlockBase<inverse_type>::gauss_jordan:
198 this->inverse(cell).invert(M_cell);
199 break;
200 case PreconditionBlockBase<inverse_type>::householder:
201 this->inverse_householder(cell).initialize(M_cell);
202 break;
203 case PreconditionBlockBase<inverse_type>::svd:
204 this->inverse_svd(cell) = M_cell;
205 this->inverse_svd(cell).compute_inverse_svd(0.);
206 break;
207 default:
208 DEAL_II_NOT_IMPLEMENTED();
209 }
210 }
211 }
212 this->inverses_computed(true);
213 }
214
215
216
217 template <typename MatrixType, typename inverse_type>
218 template <typename number2>
219 void
220 PreconditionBlock<MatrixType, inverse_type>::forward_step(
221 Vector<number2> &dst,
222 const Vector<number2> &prev,
223 const Vector<number2> &src,
224 const bool transpose_diagonal) const
225 {
226 Assert(this->A != nullptr, ExcNotInitialized());
227
228 const MatrixType &M = *this->A;
229
230 if (permutation.size() != 0)
231 Assert(
232 permutation.size() == M.m() || permutation.size() == this->size(),
233 ExcMessage(
234 "Permutation vector size must be equal to either the number of blocks or the dimension of the system"));
235
236 const bool permuted = (permutation.size() == M.m());
237 const bool cell_permuted = (permutation.size() == this->size());
238
239 Vector<number2> b_cell(this->blocksize), x_cell(this->blocksize);
240
241 // cell_row, cell_column are the
242 // numbering of the blocks (cells).
243 // row_cell, column_cell are the local
244 // numbering of the unknowns in the
245 // blocks.
246 // row, column are the global numbering
247 // of the unknowns.
248 size_type row, row_cell;
249 number2 b_cell_row;
250 // The diagonal block if the
251 // inverses were not precomputed
252 FullMatrix<number> M_cell(this->blocksize);
253
254 // Loop over all blocks
255 for (unsigned int rawcell = 0; rawcell < this->size(); ++rawcell)
256 {
257 const unsigned int cell = cell_permuted ? permutation[rawcell] : rawcell;
258 const size_type block_start = cell * this->blocksize;
259 const size_type permuted_block_start =
260 permuted ? permutation[block_start] : block_start;
261
262 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
263 ++row_cell, ++row)
264 {
265 const typename MatrixType::const_iterator row_end = M.end(row);
266 typename MatrixType::const_iterator entry = M.begin(row);
267
268 b_cell_row = src(row);
269 for (; entry != row_end; ++entry)
270 {
271 const size_type column = entry->column();
272 const size_type inverse_permuted_column =
273 permuted ? inverse_permutation[column] : column;
274 b_cell_row -= entry->value() * prev(column);
275 // TODO:[GK] Find out if this is really once column and once
276 // permuted
277 if (!this->inverses_ready() &&
278 inverse_permuted_column >= block_start &&
279 inverse_permuted_column < block_start + this->blocksize)
280 {
281 const size_type column_cell = column - block_start;
282 if (transpose_diagonal)
283 M_cell(column_cell, row_cell) = entry->value();
284 else
285 M_cell(row_cell, column_cell) = entry->value();
286 }
287 }
288 b_cell(row_cell) = b_cell_row;
289 }
290 if (this->inverses_ready())
291 {
292 if (transpose_diagonal)
293 this->inverse_Tvmult(cell, x_cell, b_cell);
294 else
295 this->inverse_vmult(cell, x_cell, b_cell);
296 }
297 else
298 {
299 Householder<number> house(M_cell);
300 house.least_squares(x_cell, b_cell);
301 }
302
303 // distribute x_cell to dst
304 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
305 ++row_cell, ++row)
306 dst(row) = prev(row) + this->relaxation * x_cell(row_cell);
307 }
308 }
309
310
311 template <typename MatrixType, typename inverse_type>
312 template <typename number2>
313 void
314 PreconditionBlock<MatrixType, inverse_type>::backward_step(
315 Vector<number2> &dst,
316 const Vector<number2> &prev,
317 const Vector<number2> &src,
318 const bool transpose_diagonal) const
319 {
320 Assert(this->A != nullptr, ExcNotInitialized());
321
322 const MatrixType &M = *this->A;
323
324 if (permutation.size() != 0)
325 Assert(
326 permutation.size() == M.m() || permutation.size() == this->size(),
327 ExcMessage(
328 "Permutation vector size must be equal to either the number of blocks or the dimension of the system"));
329
330 const bool permuted = (permutation.size() == M.m());
331 const bool cell_permuted = (permutation.size() == this->size());
332
333 Vector<number2> b_cell(this->blocksize), x_cell(this->blocksize);
334
335 // cell_row, cell_column are the
336 // numbering of the blocks (cells).
337 // row_cell, column_cell are the local
338 // numbering of the unknowns in the
339 // blocks.
340 // row, column are the global numbering
341 // of the unknowns.
342 size_type row, row_cell;
343 number2 b_cell_row;
344
345 FullMatrix<number> M_cell(this->blocksize);
346 for (unsigned int rawcell = this->size(); rawcell != 0;)
347 {
348 --rawcell;
349 const unsigned int cell = cell_permuted ? permutation[rawcell] : rawcell;
350 const size_type block_start = cell * this->blocksize;
351 const size_type block_end = block_start + this->blocksize;
352 const size_type permuted_block_start =
353 permuted ? permutation[block_start] : block_start;
354 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
355 ++row_cell, ++row)
356 {
357 const typename MatrixType::const_iterator row_end = M.end(row);
358 typename MatrixType::const_iterator entry = M.begin(row);
359
360 b_cell_row = src(row);
361 for (; entry != row_end; ++entry)
362 {
363 const size_type column = entry->column();
364 const size_type inverse_permuted_column =
365 permuted ? inverse_permutation[column] : column;
366 b_cell_row -= entry->value() * prev(column);
367 if (!this->inverses_ready() &&
368 inverse_permuted_column < block_end && column >= block_start)
369 {
370 const size_type column_cell = column - block_start;
371 // We need the
372 // transpose of the
373 // diagonal block,
374 // so we switch row
375 // and column
376 // indices
377 if (transpose_diagonal)
378 M_cell(column_cell, row_cell) = entry->value();
379 else
380 M_cell(row_cell, column_cell) = entry->value();
381 }
382 }
383 b_cell(row_cell) = b_cell_row;
384 }
385 if (this->inverses_ready())
386 {
387 if (transpose_diagonal)
388 this->inverse_Tvmult(cell, x_cell, b_cell);
389 else
390 this->inverse_vmult(cell, x_cell, b_cell);
391 }
392 else
393 {
394 Householder<number> house(M_cell);
395 house.least_squares(x_cell, b_cell);
396 }
397
398
399 // distribute x_cell to dst
400 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
401 ++row_cell, ++row)
402 dst(row) = prev(row) + this->relaxation * x_cell(row_cell);
403 }
404 }
405
406
407 template <typename MatrixType, typename inverse_type>
408 typename PreconditionBlock<MatrixType, inverse_type>::size_type
409 PreconditionBlock<MatrixType, inverse_type>::block_size() const
410 {
411 return blocksize;
412 }
413
414
415 template <typename MatrixType, typename inverse_type>
416 void
417 PreconditionBlock<MatrixType, inverse_type>::invert_diagblocks()
418 {
419 Assert(A != nullptr, ExcNotInitialized());
420 Assert(blocksize != 0, ExcNotInitialized());
421
422 const MatrixType &M = *A;
423 Assert(this->inverses_ready() == 0, ExcInverseMatricesAlreadyExist());
424
425 FullMatrix<inverse_type> M_cell(blocksize);
426
427 if (this->same_diagonal())
428 {
429 for (size_type row_cell = 0; row_cell < blocksize; ++row_cell)
430 {
431 typename MatrixType::const_iterator entry = M.begin(row_cell);
432 const typename MatrixType::const_iterator row_end = M.end(row_cell);
433 while (entry != row_end)
434 {
435 if (entry->column() < blocksize)
436 M_cell(row_cell, entry->column()) = entry->value();
437 ++entry;
438 }
439 }
440 if (this->store_diagonals())
441 this->diagonal(0) = M_cell;
442 switch (this->inversion)
443 {
444 case PreconditionBlockBase<inverse_type>::gauss_jordan:
445 this->inverse(0).invert(M_cell);
446 break;
447 case PreconditionBlockBase<inverse_type>::householder:
448 this->inverse_householder(0).initialize(M_cell);
449 break;
450 case PreconditionBlockBase<inverse_type>::svd:
451 this->inverse_svd(0) = M_cell;
452 this->inverse_svd(0).compute_inverse_svd(1.e-12);
453 break;
454 default:
455 DEAL_II_NOT_IMPLEMENTED();
456 }
457 }
458 else
459 {
460 M_cell = 0;
461
462 for (unsigned int cell = 0; cell < this->size(); ++cell)
463 {
464 const size_type cell_start = cell * blocksize;
465 for (size_type row_cell = 0; row_cell < blocksize; ++row_cell)
466 {
467 const size_type row = row_cell + cell_start;
468 typename MatrixType::const_iterator entry = M.begin(row);
469 const typename MatrixType::const_iterator row_end = M.end(row);
470
471 for (; entry != row_end; ++entry)
472 {
473 if (entry->column() < cell_start)
474 continue;
475
476 const size_type column_cell = entry->column() - cell_start;
477 if (column_cell >= blocksize)
478 continue;
479 M_cell(row_cell, column_cell) = entry->value();
480 }
481 }
482
483 if (this->store_diagonals())
484 this->diagonal(cell) = M_cell;
485 switch (this->inversion)
486 {
487 case PreconditionBlockBase<inverse_type>::gauss_jordan:
488 this->inverse(cell).invert(M_cell);
489 break;
490 case PreconditionBlockBase<inverse_type>::householder:
491 this->inverse_householder(cell).initialize(M_cell);
492 break;
493 case PreconditionBlockBase<inverse_type>::svd:
494 this->inverse_svd(cell) = M_cell;
495 this->inverse_svd(cell).compute_inverse_svd(1.e-12);
496 break;
497 default:
498 DEAL_II_NOT_IMPLEMENTED();
499 }
500 }
501 }
502 this->inverses_computed(true);
503 }
504
505
506
507 template <typename MatrixType, typename inverse_type>
508 void
509 PreconditionBlock<MatrixType, inverse_type>::set_permutation(
510 const std::vector<size_type> &new_permutation,
511 const std::vector<size_type> &new_inverse_permutation)
512 {
513 AssertDimension(new_permutation.size(), new_inverse_permutation.size());
514
515 if (this->inverses_ready())
516 {
517 AssertDimension(new_permutation.size(), this->size());
518 }
519
520 permutation = new_permutation;
521 inverse_permutation = new_inverse_permutation;
522 }
523
524
525 template <typename MatrixType, typename inverse_type>
526 std::size_t
527 PreconditionBlock<MatrixType, inverse_type>::memory_consumption() const
528 {
529 return (sizeof(*this) - sizeof(PreconditionBlockBase<inverse_type>) +
530 PreconditionBlockBase<inverse_type>::memory_consumption());
531 }
532
533
534
535 /*--------------------- PreconditionBlockJacobi -----------------------*/
536
537
538 template <typename MatrixType, typename inverse_type>
539 template <typename number2>
540 void
541 PreconditionBlockJacobi<MatrixType, inverse_type>::do_vmult(
542 Vector<number2> &dst,
543 const Vector<number2> &src,
544 bool adding) const
545 {
546 Assert(this->A != nullptr, ExcNotInitialized());
547
548 const MatrixType &M = *this->A;
549
550 Vector<number2> b_cell(this->blocksize), x_cell(this->blocksize);
551
552 // cell_row, cell_column are the
553 // numbering of the blocks (cells).
554 // row_cell, column_cell are the local
555 // numbering of the unknowns in the
556 // blocks.
557 // row, column are the global numbering
558 // of the unknowns.
559 size_type row, row_cell, begin_diag_block = 0;
560
561 if (!this->inverses_ready())
562 {
563 FullMatrix<number> M_cell(this->blocksize);
564 for (unsigned int cell = 0; cell < this->size(); ++cell)
565 {
566 for (row = cell * this->blocksize, row_cell = 0;
567 row_cell < this->blocksize;
568 ++row_cell, ++row)
569 {
570 b_cell(row_cell) = src(row);
571 for (size_type column_cell = 0, column = cell * this->blocksize;
572 column_cell < this->blocksize;
573 ++column_cell, ++column)
574 M_cell(row_cell, column_cell) = M(row, column);
575 }
576 Householder<number> house(M_cell);
577 house.least_squares(x_cell, b_cell);
578 // distribute x_cell to dst
579 for (row = cell * this->blocksize, row_cell = 0;
580 row_cell < this->blocksize;
581 ++row_cell, ++row)
582 if (adding)
583 dst(row) += x_cell(row_cell);
584 else
585 dst(row) = x_cell(row_cell);
586
587 begin_diag_block += this->blocksize;
588 }
589 }
590 else
591 for (unsigned int cell = 0; cell < this->size(); ++cell)
592 {
593 for (row = cell * this->blocksize, row_cell = 0;
594 row_cell < this->blocksize;
595 ++row_cell, ++row)
596 {
597 b_cell(row_cell) = src(row);
598 }
599 this->inverse_vmult(cell, x_cell, b_cell);
600 // distribute x_cell to dst
601 for (row = cell * this->blocksize, row_cell = 0;
602 row_cell < this->blocksize;
603 ++row_cell, ++row)
604 if (adding)
605 dst(row) += x_cell(row_cell);
606 else
607 dst(row) = x_cell(row_cell);
608
609 begin_diag_block += this->blocksize;
610 }
611 dst *= this->relaxation;
612 }
613
614
615 template <typename MatrixType, typename inverse_type>
616 template <typename number2>
617 void
618 PreconditionBlockJacobi<MatrixType, inverse_type>::vmult(
619 Vector<number2> &dst,
620 const Vector<number2> &src) const
621 {
622 do_vmult(dst, src, false);
623 }
624
625
626 template <typename MatrixType, typename inverse_type>
627 template <typename number2>
628 void
629 PreconditionBlockJacobi<MatrixType, inverse_type>::Tvmult(
630 Vector<number2> &dst,
631 const Vector<number2> &src) const
632 {
633 do_vmult(dst, src, false);
634 }
635
636
637 template <typename MatrixType, typename inverse_type>
638 template <typename number2>
639 void
640 PreconditionBlockJacobi<MatrixType, inverse_type>::vmult_add(
641 Vector<number2> &dst,
642 const Vector<number2> &src) const
643 {
644 do_vmult(dst, src, true);
645 }
646
647
648 template <typename MatrixType, typename inverse_type>
649 template <typename number2>
650 void
651 PreconditionBlockJacobi<MatrixType, inverse_type>::Tvmult_add(
652 Vector<number2> &dst,
653 const Vector<number2> &src) const
654 {
655 do_vmult(dst, src, true);
656 }
657
658
659 template <typename MatrixType, typename inverse_type>
660 template <typename number2>
661 void
662 PreconditionBlockJacobi<MatrixType, inverse_type>::step(
663 Vector<number2> &dst,
664 const Vector<number2> &src) const
665 {
666 GrowingVectorMemory<Vector<number2>> mem;
667 typename VectorMemory<Vector<number2>>::Pointer aux(mem);
668 aux->reinit(dst);
669
670 this->forward_step(*aux, dst, src, false);
671 dst = *aux;
672 }
673
674
675 template <typename MatrixType, typename inverse_type>
676 template <typename number2>
677 void
678 PreconditionBlockJacobi<MatrixType, inverse_type>::Tstep(
679 Vector<number2> &dst,
680 const Vector<number2> &src) const
681 {
682 GrowingVectorMemory<Vector<number2>> mem;
683 typename VectorMemory<Vector<number2>>::Pointer aux(mem);
684 aux->reinit(dst);
685
686 this->backward_step(*aux, dst, src, true);
687 dst = *aux;
688 }
689
690
691
692 /*--------------------- PreconditionBlockSOR -----------------------*/
693
694
695 template <typename MatrixType, typename inverse_type>
696 PreconditionBlockSOR<MatrixType, inverse_type>::PreconditionBlockSOR()
697 : PreconditionBlock<MatrixType, inverse_type>(false)
698
699 {}
700
701 template <typename MatrixType, typename inverse_type>
702 PreconditionBlockSOR<MatrixType, inverse_type>::PreconditionBlockSOR(bool store)
703 : PreconditionBlock<MatrixType, inverse_type>(store)
704
705 {}
706
707 template <typename MatrixType, typename inverse_type>
708 template <typename number2>
709 void
710 PreconditionBlockSOR<MatrixType, inverse_type>::forward(
711 Vector<number2> &dst,
712 const Vector<number2> &src,
713 const bool transpose_diagonal,
714 const bool) const
715 {
716 Assert(this->A != nullptr, ExcNotInitialized());
717
718 const MatrixType &M = *this->A;
719 const bool permuted = (this->permutation.size() != 0);
720 if (permuted)
721 {
722 Assert(this->permutation.size() == M.m(),
723 ExcDimensionMismatch(this->permutation.size(), M.m()));
724 }
725
726 Vector<number2> b_cell(this->blocksize), x_cell(this->blocksize);
727
728 // cell_row, cell_column are the
729 // numbering of the blocks (cells).
730 // row_cell, column_cell are the local
731 // numbering of the unknowns in the
732 // blocks.
733 // row, column are the global numbering
734 // of the unknowns.
735 size_type row, row_cell, block_start = 0;
736 number2 b_cell_row;
737 // The diagonal block if the
738 // inverses were not precomputed
739 FullMatrix<number> M_cell(this->blocksize);
740
741 for (unsigned int cell = 0; cell < this->size(); ++cell)
742 {
743 const size_type permuted_block_start =
744 permuted ? this->permutation[block_start] : block_start;
745
746 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
747 ++row_cell, ++row)
748 {
749 const typename MatrixType::const_iterator row_end = M.end(row);
750 typename MatrixType::const_iterator entry = M.begin(row);
751
752 b_cell_row = src(row);
753 for (; entry != row_end; ++entry)
754 {
755 const size_type column = entry->column();
756 const size_type inverse_permuted_column =
757 permuted ? this->inverse_permutation[column] : column;
758
759 if (inverse_permuted_column < block_start)
760 b_cell_row -= entry->value() * dst(column);
761 else if (!this->inverses_ready() &&
762 column < block_start + this->blocksize)
763 {
764 const size_type column_cell = column - block_start;
765 if (transpose_diagonal)
766 M_cell(column_cell, row_cell) = entry->value();
767 else
768 M_cell(row_cell, column_cell) = entry->value();
769 }
770 }
771 b_cell(row_cell) = b_cell_row;
772 }
773 if (this->inverses_ready())
774 {
775 if (transpose_diagonal)
776 this->inverse_Tvmult(cell, x_cell, b_cell);
777 else
778 this->inverse_vmult(cell, x_cell, b_cell);
779 }
780 else
781 {
782 Householder<number> house(M_cell);
783 house.least_squares(x_cell, b_cell);
784 }
785
786 // distribute x_cell to dst
787 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
788 ++row_cell, ++row)
789 dst(row) = this->relaxation * x_cell(row_cell);
790
791 block_start += this->blocksize;
792 }
793 }
794
795
796 template <typename MatrixType, typename inverse_type>
797 template <typename number2>
798 void
799 PreconditionBlockSOR<MatrixType, inverse_type>::backward(
800 Vector<number2> &dst,
801 const Vector<number2> &src,
802 const bool transpose_diagonal,
803 const bool) const
804 {
805 Assert(this->A != nullptr, ExcNotInitialized());
806
807 const MatrixType &M = *this->A;
808 const bool permuted = (this->permutation.size() != 0);
809 if (permuted)
810 {
811 Assert(this->permutation.size() == M.m(),
812 ExcDimensionMismatch(this->permutation.size(), M.m()));
813 }
814
815 Vector<number2> b_cell(this->blocksize), x_cell(this->blocksize);
816
817 // cell_row, cell_column are the
818 // numbering of the blocks (cells).
819 // row_cell, column_cell are the local
820 // numbering of the unknowns in the
821 // blocks.
822 // row, column are the global numbering
823 // of the unknowns.
824 size_type row, row_cell;
825 size_type block_end = this->blocksize * this->size();
826 number2 b_cell_row;
827
828 FullMatrix<number> M_cell(this->blocksize);
829 for (unsigned int cell = this->size(); cell != 0;)
830 {
831 --cell;
832 const size_type block_start = block_end - this->blocksize;
833 // Collect upper triangle
834 const size_type permuted_block_start = (this->permutation.size() != 0) ?
835 this->permutation[block_start] :
836 block_start;
837 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
838 ++row_cell, ++row)
839 {
840 const typename MatrixType::const_iterator row_end = M.end(row);
841 typename MatrixType::const_iterator entry = M.begin(row);
842
843 b_cell_row = src(row);
844 for (; entry != row_end; ++entry)
845 {
846 const size_type column = entry->column();
847 const size_type inverse_permuted_column =
848 permuted ? this->inverse_permutation[column] : column;
849 if (inverse_permuted_column >= block_end)
850 b_cell_row -= entry->value() * dst(column);
851 else if (!this->inverses_ready() && column >= block_start)
852 {
853 const size_type column_cell = column - block_start;
854 // We need the
855 // transpose of the
856 // diagonal block,
857 // so we switch row
858 // and column
859 // indices
860 if (transpose_diagonal)
861 M_cell(column_cell, row_cell) = entry->value();
862 else
863 M_cell(row_cell, column_cell) = entry->value();
864 }
865 }
866 b_cell(row_cell) = b_cell_row;
867 }
868 if (this->inverses_ready())
869 {
870 if (transpose_diagonal)
871 this->inverse_Tvmult(cell, x_cell, b_cell);
872 else
873 this->inverse_vmult(cell, x_cell, b_cell);
874 }
875 else
876 {
877 Householder<number> house(M_cell);
878 house.least_squares(x_cell, b_cell);
879 }
880
881
882 // distribute x_cell to dst
883 for (row = permuted_block_start, row_cell = 0; row_cell < this->blocksize;
884 ++row_cell, ++row)
885 dst(row) = this->relaxation * x_cell(row_cell);
886 block_end = block_start;
887 }
888 }
889
890
891 template <typename MatrixType, typename inverse_type>
892 template <typename number2>
893 void
894 PreconditionBlockSOR<MatrixType, inverse_type>::vmult(
895 Vector<number2> &dst,
896 const Vector<number2> &src) const
897 {
898 forward(dst, src, false, false);
899 }
900
901
902 template <typename MatrixType, typename inverse_type>
903 template <typename number2>
904 void
905 PreconditionBlockSOR<MatrixType, inverse_type>::vmult_add(
906 Vector<number2> &dst,
907 const Vector<number2> &src) const
908 {
909 forward(dst, src, false, true);
910 }
911
912
913 template <typename MatrixType, typename inverse_type>
914 template <typename number2>
915 void
916 PreconditionBlockSOR<MatrixType, inverse_type>::Tvmult(
917 Vector<number2> &dst,
918 const Vector<number2> &src) const
919 {
920 backward(dst, src, true, false);
921 }
922
923
924 template <typename MatrixType, typename inverse_type>
925 template <typename number2>
926 void
927 PreconditionBlockSOR<MatrixType, inverse_type>::Tvmult_add(
928 Vector<number2> &dst,
929 const Vector<number2> &src) const
930 {
931 backward(dst, src, true, true);
932 }
933
934
935
936 template <typename MatrixType, typename inverse_type>
937 template <typename number2>
938 void
939 PreconditionBlockSOR<MatrixType, inverse_type>::step(
940 Vector<number2> &dst,
941 const Vector<number2> &src) const
942 {
943 this->forward_step(dst, dst, src, false);
944 }
945
946
947 template <typename MatrixType, typename inverse_type>
948 template <typename number2>
949 void
950 PreconditionBlockSOR<MatrixType, inverse_type>::Tstep(
951 Vector<number2> &dst,
952 const Vector<number2> &src) const
953 {
954 this->backward_step(dst, dst, src, true);
955 }
956
957
958
959 //---------------------------------------------------------------------------
960
961
962 template <typename MatrixType, typename inverse_type>
963 PreconditionBlockSSOR<MatrixType, inverse_type>::PreconditionBlockSSOR()
964 : PreconditionBlockSOR<MatrixType, inverse_type>(true)
965
966 {}
967
968
969 template <typename MatrixType, typename inverse_type>
970 template <typename number2>
971 void
972 PreconditionBlockSSOR<MatrixType, inverse_type>::vmult(
973 Vector<number2> &dst,
974 const Vector<number2> &src) const
975 {
976 Vector<number2> help;
977 help.reinit(dst);
978
979 this->forward(help, src, false, false);
980
981 Vector<inverse_type> cell_src(this->blocksize);
982 Vector<inverse_type> cell_dst(this->blocksize);
983 const double scaling = (2. - this->relaxation) / this->relaxation;
984
985 // Multiply with diagonal blocks
986 for (unsigned int cell = 0; cell < this->size(); ++cell)
987 {
988 size_type row = cell * this->blocksize;
989
990 for (size_type row_cell = 0; row_cell < this->blocksize; ++row_cell)
991 cell_src(row_cell) = help(row + row_cell);
992
993 this->diagonal(cell).vmult(cell_dst, cell_src);
994
995 for (size_type row_cell = 0; row_cell < this->blocksize; ++row_cell)
996 help(row + row_cell) = scaling * cell_dst(row_cell);
997 }
998
999 this->backward(dst, help, false, false);
1000 }
1001
1002 template <typename MatrixType, typename inverse_type>
1003 template <typename number2>
1004 void
1005 PreconditionBlockSSOR<MatrixType, inverse_type>::Tvmult(
1006 Vector<number2> &dst,
1007 const Vector<number2> &src) const
1008 {
1009 Vector<number2> help;
1010 help.reinit(dst);
1011
1012 this->backward(help, src, true, false);
1013
1014 Vector<inverse_type> cell_src(this->blocksize);
1015 Vector<inverse_type> cell_dst(this->blocksize);
1016 const double scaling = (2. - this->relaxation) / this->relaxation;
1017
1018 // Multiply with diagonal blocks
1019 for (unsigned int cell = 0; cell < this->size(); ++cell)
1020 {
1021 size_type row = cell * this->blocksize;
1022
1023 for (size_type row_cell = 0; row_cell < this->blocksize; ++row_cell)
1024 cell_src(row_cell) = help(row + row_cell);
1025
1026 this->diagonal(cell).Tvmult(cell_dst, cell_src);
1027
1028 for (size_type row_cell = 0; row_cell < this->blocksize; ++row_cell)
1029 help(row + row_cell) = scaling * cell_dst(row_cell);
1030 }
1031
1032 this->forward(dst, help, true, false);
1033 }
1034
1035
1036 template <typename MatrixType, typename inverse_type>
1037 template <typename number2>
1038 void
1039 PreconditionBlockSSOR<MatrixType, inverse_type>::step(
1040 Vector<number2> &dst,
1041 const Vector<number2> &src) const
1042 {
1043 this->forward_step(dst, dst, src, false);
1044 this->backward_step(dst, dst, src, false);
1045 }
1046
1047
1048 template <typename MatrixType, typename inverse_type>
1049 template <typename number2>
1050 void
1051 PreconditionBlockSSOR<MatrixType, inverse_type>::Tstep(
1052 Vector<number2> &dst,
1053 const Vector<number2> &src) const
1054 {
1055 this->backward_step(dst, dst, src, true);
1056 this->forward_step(dst, dst, src, true);
1057 }
1058
1059
1060
1061 DEAL_II_NAMESPACE_CLOSE
1062
1063 #endif