FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
direct_stokes_solver.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/backend.hpp>
11#include <kernel/lafem/saddle_point_matrix.hpp>
12#include <kernel/lafem/sparse_matrix_bcsr.hpp>
13#include <kernel/lafem/sparse_matrix_csr.hpp>
14#include <kernel/lafem/dense_vector.hpp>
15#include <kernel/lafem/dense_vector_blocked.hpp>
16#include <kernel/lafem/tuple_vector.hpp>
17#include <kernel/lafem/mean_filter.hpp>
18#include <kernel/lafem/none_filter.hpp>
19#include <kernel/lafem/slip_filter.hpp>
20#include <kernel/lafem/unit_filter.hpp>
21#include <kernel/lafem/unit_filter_blocked.hpp>
22#include <kernel/lafem/tuple_filter.hpp>
23#include <kernel/lafem/filter_chain.hpp>
24#include <kernel/lafem/filter_sequence.hpp>
25#include <kernel/global/matrix.hpp>
26#include <kernel/global/vector.hpp>
27#include <kernel/global/filter.hpp>
28#include <kernel/global/mean_filter.hpp>
29#include <kernel/solver/base.hpp>
30#include <kernel/solver/cudss.hpp>
31#include <kernel/solver/mkl_dss.hpp>
32#include <kernel/solver/umfpack.hpp>
33
34// includes, system
35#include <deque>
36
37namespace FEAT
38{
39 namespace Solver
40 {
48 template<
49 typename SolverDT_, typename SolverIT_,
50 typename MatrixA_, typename MatrixB_, typename MatrixD_,
51 typename MatrixS_ = LAFEM::SparseMatrixCSR<typename MatrixA_::DataType, typename MatrixA_::IndexType>>
53
118 template<typename SolverDT_, typename SolverIT_, typename DT_, typename IT_, int dim_>
120 SolverDT_, SolverIT_,
121 LAFEM::SparseMatrixBCSR<DT_, IT_, dim_, dim_>,
122 LAFEM::SparseMatrixBCSR<DT_, IT_, dim_, 1>,
123 LAFEM::SparseMatrixBCSR<DT_, IT_, 1, dim_>,
124 LAFEM::SparseMatrixCSR<DT_, IT_>>
125 {
126 public:
135
140
147
150 //typedef LAFEM::MeanFilter<DT_, IT_> MeanFilterTypeP;
151 //typedef Global::MeanFilter<DT_, IT_> MeanFilterTypePG;
154
156 typedef SolverDT_ SolverDataType;
158 typedef SolverIT_ SolverIndexType;
159
166
167 protected:
176
181
183 std::deque<SlipFilterTypeV> _slip_filters_v;
185 std::deque<UnitFilterTypeV> _unit_filters_v;
187 std::deque<UnitFilterTypeP> _unit_filters_p;
190
192 bool _filters_already_set = false;
193
195 bool _have_mean_filter_p = false;
196
198 bool _need_main_diagonal = false;
199
201 std::vector<int> _unit_mask_v, _unit_mask_p;
202
204 std::vector<IT_> _slip_row_ptr, _slip_col_idx, _slip_fil_dqu, _slip_fil_idx;
205
207 typedef std::array<std::size_t, 3u> FilterUpdateCounter;
208
209 public:
223 explicit DirectStokesCore(const MatrixTypeA& matrix_a, const MatrixTypeB& matrix_b,
224 const MatrixTypeD& matrix_d, const MatrixTypeS* matrix_s = nullptr) :
225 _matrix_a(matrix_a),
226 _matrix_b(matrix_b),
227 _matrix_d(matrix_d),
228 _matrix_s(matrix_s)
229 {
230 }
231
234 {
235 }
236
241
249 void set_need_main_diagonal(bool need_it)
250 {
251 this->_need_main_diagonal = need_it;
252 }
253
258 {
259 return this->_solver_matrix;
260 }
261
266 {
267 return this->_solver_filter;
268 }
269
274 {
275 return this->_solver_matrix.create_vector_r();
276 }
277
287 virtual void upload_vector(SolverVectorType& vector, const VectorTypeV& vector_v, const VectorTypeP& vector_p) const
288 {
289 const IT_ nv = IT_(vector_v.size_raw());
290 const IT_ np = IT_(vector_p.size());
291 const IT_ nx = IT_(vector.size());
292
293 XASSERTM(nx == IT_(this->_solver_matrix.num_rows()), "invalid solver vector size");
294 XASSERTM(nv + np <= nx, "invalid velocity/pressure vector size");
295
296 Memory::TypedView<SolverDataType> vec = vector.elements_view_w();
297 const Memory::TypedView<DT_> vec_v = vector_v.elements_view_raw_r();
298 const Memory::TypedView<DT_> vec_p = vector_p.elements_view_r();
299 IT_ j(0u);
300 // copy velocity DOFs
301 for(IT_ i(0); i < nv; ++i, ++j)
302 vec[j] = SolverDataType(vec_v[i]);
303 // copy pressure DOFs
304 for(IT_ i(0); i < np; ++i, ++j)
305 vec[j] = SolverDataType(vec_p[i]);
306 // format Lagrange multipliers
307 for(; j < nx; ++j)
308 vec[j] = SolverDataType(0);
309 }
310
320 virtual void download_vector(const SolverVectorType& vector, VectorTypeV& vector_v, VectorTypeP& vector_p) const
321 {
322 const IT_ nv = IT_(vector_v.size_raw());
323 const IT_ np = IT_(vector_p.size());
324 const IT_ nx = IT_(vector.size());
325
326 XASSERTM(nx == IT_(this->_solver_matrix.num_rows()), "invalid solver vector size");
327 XASSERTM(nv + np <= nx, "invalid velocity/pressure vector size");
328
329 const Memory::TypedView<SolverDataType> vec = vector.elements_view_r();
330 Memory::TypedView<DT_> vec_v = vector_v.elements_view_raw_w();
331 Memory::TypedView<DT_> vec_p = vector_p.elements_view_w();
332 IT_ j(0u);
333 // copy velocity DOFs
334 for(IT_ i(0); i < nv; ++i, ++j)
335 vec_v[i] = DT_(vec[j]);
336 // copy pressure DOFs
337 for(IT_ i(0); i < np; ++i, ++j)
338 vec_p[i] = DT_(vec[j]);
339 // Lagrange multipliers are ignored
340 }
341
350 template<typename FilterV_, typename FilterP_>
352 {
353 XASSERTM(!this->_filters_already_set, "Filter have already been set");
354 this->_add_filter_velo(filter.template at<0>(), nullptr);
355 this->_add_filter_pres(filter.template at<1>(), nullptr);
356 this->_filters_already_set = true;
357 }
358
368 template<typename FilterV_, typename FilterP_>
370 {
371 XASSERTM(this->_filters_already_set, "Filters have not been set yet");
372 FilterUpdateCounter fuc{0u, 0u, 0u};
373 this->_add_filter_velo(filter.template at<0>(), &fuc);
374 this->_add_filter_pres(filter.template at<1>(), &fuc);
375 }
376
382 virtual void init_symbolic()
383 {
384 // get the number of DOFs
385 const IT_ idim = IT_(dim_);
386 const IT_ num_dofs_v = IT_(_matrix_a.num_rows());
387 const IT_ num_dofs_p = IT_(_matrix_d.num_rows());
388
389 XASSERT(_matrix_a.num_cols() == num_dofs_v);
390 XASSERT(_matrix_b.num_cols() == num_dofs_p);
391 XASSERT(_matrix_d.num_cols() == num_dofs_v);
392 XASSERT(_matrix_b.num_rows() == num_dofs_v);
393 XASSERT(_matrix_d.num_rows() == num_dofs_p);
394
395 if(_matrix_s != nullptr)
396 {
397 XASSERT(_matrix_s->num_rows() == num_dofs_p);
398 XASSERT(_matrix_s->num_cols() == num_dofs_p);
399 }
400
401 // is the mean filter dual vector non-empty?
402 this->_have_mean_filter_p = !this->_mean_filter_p_dual.empty();
403
404 // compute number of slip DOFs
405 const IT_ num_slip_dofs = this->_compute_slip_matrix_layout();
406
407 // mask velocity and pressure unit dofs
408 this->_compute_unit_mask_v();
409 this->_compute_unit_mask_p();
410
411 // compute total number of scalar DOFs
412 const IT_ num_total_dofs = idim*num_dofs_v + num_dofs_p + num_slip_dofs + IT_(_have_mean_filter_p ? 1 : 0);
413
414 // get number of matrix NZEs
415 const IT_ num_nze_a = IT_(_matrix_a.num_nzes());
416 const IT_ num_nze_b = IT_(_matrix_b.num_nzes());
417 const IT_ num_nze_d = IT_(_matrix_d.num_nzes());
418 const IT_ num_nze_s = IT_(_matrix_s != nullptr ? _matrix_s->num_nzes() : 0u);
419
420 // compute upper bound for non-zero entry count
421 IT_ max_nze = idim*idim*num_nze_a + idim*(num_nze_b + num_nze_d) + num_nze_s;
422
423 // Lagrange multiplier for velocity slip filters
424 max_nze += IT_(2)*num_slip_dofs*idim;
425
426 // Lagrange multiplier for pressure mean filter (when necessary)
427 if(this->_have_mean_filter_p)
428 max_nze += IT_(2)*num_dofs_p;
429
430 // need a main diagonal?
431 if(this->_need_main_diagonal)
432 max_nze += num_dofs_p + IT_(2)*num_slip_dofs*idim + IT_(1);
433
434 // allocate row-pointer and column index arrays
435 std::vector<IT_> row_ptr, col_idx;
436 row_ptr.resize(num_total_dofs + IT_(1u), ~IT_(0));
437 col_idx.resize(max_nze, ~IT_(0));
438 row_ptr[0u] = IT_(0u);
439
440 // the system has the following structure:
441 // | A B Q 0 |
442 // | D S 0 P |
443 // | Q^T 0 0 0 |
444 // | 0 P^T 0 0 |
445 //
446 // where:
447 // - A, B, D and S are the original matrices from the Stokes system
448 // - Q is the Lagrange multiplier for the velocity slip DOFs
449 // - P is the Lagrange multiplier for the pressure mean filter
450
451 // get all the matrix arrays
452 const Memory::TypedView<IT_> row_ptr_a = _matrix_a.row_ptr_view_r();
453 const Memory::TypedView<IT_> col_idx_a = _matrix_a.col_idx_view_r();
454 const Memory::TypedView<IT_> row_ptr_b = _matrix_b.row_ptr_view_r();
455 const Memory::TypedView<IT_> col_idx_b = _matrix_b.col_idx_view_r();
456 const Memory::TypedView<IT_> row_ptr_d = _matrix_d.row_ptr_view_r();
457 const Memory::TypedView<IT_> col_idx_d = _matrix_d.col_idx_view_r();
458 Memory::TypedView<IT_> row_ptr_s, col_idx_s;
459 if(_matrix_s != nullptr)
460 {
461 row_ptr_s = _matrix_s->row_ptr_view_r();
462 col_idx_s = _matrix_s->col_idx_view_r();
463 }
464
465 IT_ row(0u), op(0u);
466
467 // okay, loop over all rows of A/B/Q
468 for(IT_ i(0); i < num_dofs_v; ++i)
469 {
470 // is this a unit row?
471 if(_unit_mask_v[i] != 0)
472 {
473 // set the entire block to an identity block
474 for(IT_ k(0); k < idim; ++k)
475 {
476 col_idx[op] = i*idim + k;
477 row_ptr[++row] = ++op;
478 }
479 continue;
480 }
481
482 // not a unit row, so copy rows of A, B and Q
483 for(IT_ j(0); j < idim; ++j)
484 {
485 // copy row of A
486 for(IT_ ip(row_ptr_a[i]); ip < row_ptr_a[i+1]; ++ip)
487 for(IT_ k(0); k < idim; ++k, ++op)
488 col_idx[op] = idim* col_idx_a[ip] + k;
489 // copy row of B
490 for(IT_ ip(row_ptr_b[i]); ip < row_ptr_b[i+1]; ++ip, ++op)
491 col_idx[op] = idim*num_dofs_v + col_idx_b[ip];
492 // copy row of Q
493 for(IT_ ip(_slip_row_ptr[i]); ip < _slip_row_ptr[i+1]; ++ip, ++op)
494 col_idx[op] = idim*num_dofs_v + num_dofs_p + _slip_col_idx[ip];
495 // set row pointer
496 row_ptr[++row] = op;
497 }
498 }
499
500 XASSERT(row == idim*num_dofs_v);
501
502 // next, loop over all rows of D/S/P
503 for(IT_ i(0); i < num_dofs_p; ++i)
504 {
505 // is this a unit row?
506 if(_unit_mask_p[i] != 0)
507 {
508 col_idx[op] = idim*num_dofs_v + i;
509 row_ptr[++row] = ++op;
510 continue;
511 }
512
513 // not a unit row, so copy rows of D, S and P
514 // copy row of D
515 for(IT_ ip(row_ptr_d[i]); ip < row_ptr_d[i+1]; ++ip)
516 for(IT_ k(0); k < idim; ++k, ++op)
517 col_idx[op] = idim*col_idx_d[ip] + k;
518 // copy row of S (if it exists)
519 if(_matrix_s != nullptr)
520 {
521 if(this->_need_main_diagonal)
522 {
523 // copy row i of S and insert a main diagonal entry if it is not present in S
524 bool have_diag = false;
525 for(IT_ ip(row_ptr_s[i]); ip < row_ptr_s[i+1]; ++ip, ++op)
526 {
527 if(!have_diag && (col_idx_s[ip] > i))
528 {
529 col_idx[op++] = row;
530 have_diag = true;
531 }
532 col_idx[op] = idim*num_dofs_v + col_idx_s[ip];
533 have_diag = have_diag || (col_idx_s[ip] == i);
534 }
535 }
536 else
537 {
538 for(IT_ ip(row_ptr_s[i]); ip < row_ptr_s[i+1]; ++ip, ++op)
539 col_idx[op] = idim*num_dofs_v + col_idx_s[ip];
540 }
541 }
542 else if(this->_need_main_diagonal)
543 {
544 // insert a main diagonal entry
545 col_idx[op++] = row;
546 }
547 // copy row of P (if we have a pressure mean filter)
548 if(this->_have_mean_filter_p)
549 col_idx[op++] = idim*num_dofs_v + num_slip_dofs + num_dofs_p;
550 // set row pointer
551 row_ptr[++row] = op;
552 }
553
554 // next, add all slip filter entries Q^T
555 for(auto it(_slip_filters_v.begin()); it != _slip_filters_v.end(); ++it)
556 {
557 const IT_ nsx = IT_(it->num_nzes());
558 const Memory::TypedView<IT_> idx = it->get_filter_vector().indices_view_r();
559 for(IT_ j(0); j < nsx; ++j)
560 {
561 for(IT_ k(0); k < idim; ++k, ++op)
562 col_idx[op] = idim*idx[j] + k;
563 if(this->_need_main_diagonal)
564 col_idx[op++] = row;
565 // set row pointer
566 row_ptr[++row] = op;
567 }
568 }
569
570 // finally, add the pressure mean filter vector P^T (if it exists)
571 if(this->_have_mean_filter_p)
572 {
573 for(IT_ j(0); j < num_dofs_p; ++j, ++op)
574 col_idx[op] = idim*num_dofs_v + j;
575 if(this->_need_main_diagonal)
576 col_idx[op++] = row;
577 row_ptr[++row] = op;
578 }
579
580 // the number of rows must match, but we may have less non-zeros due to unit filtering
581 XASSERT(row == num_total_dofs);
582 XASSERT(op <= max_nze);
583
584 const Index num_nze = op;
585
586 // ok, allocate the matrix
587 this->_solver_matrix = SolverMatrixType(num_total_dofs, num_total_dofs, num_nze);
588
589 // copy row pointer
590 Memory::TypedView<SolverIndexType> row_ptr_x = this->_solver_matrix.row_ptr_view_w();
591 for(IT_ i(0); i <= num_total_dofs; ++i)
592 row_ptr_x[i] = SolverIndexType(row_ptr[i]);
593
594 // copy column indices
595 Memory::TypedView<SolverIndexType> col_idx_x = this->_solver_matrix.col_idx_view_w();
596 for(IT_ i(0); i < num_nze; ++i)
597 col_idx_x[i] = SolverIndexType(col_idx[i]);
598
599 this->_solver_matrix.format();
600 }
601
607 virtual void init_numeric()
608 {
609 // get the number of DOFs
610 const IT_ num_dofs_v = IT_(_matrix_a.num_rows());
611 const IT_ num_dofs_p = IT_(_matrix_d.num_rows());
612
613 // the system has the following structure:
614 // | A B Q 0 |
615 // | D S 0 P |
616 // | Q^T 0 0 0 |
617 // | 0 P^T 0 0 |
618 //
619 // where:
620 // - A, B, D and S are the original matrices from the Stokes system
621 // - Q is the Lagrange multiplier for the velocity slip DOFs
622 // - P is the Lagrange multiplier for the pressure mean filter
623
624 // get all the matrix arrays
625 const Memory::TypedView<IT_> row_ptr_a = _matrix_a.row_ptr_view_r();
626 const auto val_a = _matrix_a.val_view_r();
627 const Memory::TypedView<IT_> row_ptr_b = _matrix_b.row_ptr_view_r();
628 const auto val_b = _matrix_b.val_view_r();
629 const Memory::TypedView<IT_> row_ptr_d = _matrix_d.row_ptr_view_r();
630 const auto val_d = _matrix_d.val_view_r();
631 Memory::TypedView<IT_> row_ptr_s, col_idx_s;
633 if(_matrix_s != nullptr)
634 {
635 row_ptr_s = _matrix_s->row_ptr_view_r();
636 col_idx_s = _matrix_s->col_idx_view_r();
637 val_s = _matrix_s->val_view_r();
638 }
639 Memory::TypedView<SolverDataType> val_x = _solver_matrix.val_view_w();
640
641 IT_ op(0u);
642
643 // okay, loop over all rows of A/B/Q
644 for(IT_ i(0); i < num_dofs_v; ++i)
645 {
646 // is this a unit row?
647 if(_unit_mask_v[i] != 0)
648 {
649 // set the entire block to an identity block
650 for(int k(0); k < dim_; ++k, ++op)
651 val_x[op] = SolverDataType(1);
652 continue;
653 }
654
655 // not a unit row, so copy rows of A, B and Q
656 for(int j(0); j < dim_; ++j)
657 {
658 // copy row of A
659 for(IT_ ip(row_ptr_a[i]); ip < row_ptr_a[i+1]; ++ip)
660 for(int k(0); k < dim_; ++k, ++op)
661 val_x[op] = SolverDataType(val_a[ip][j][k]);
662 // copy row of B
663 for(IT_ ip(row_ptr_b[i]); ip < row_ptr_b[i+1]; ++ip, ++op)
664 val_x[op] = SolverDataType(val_b[ip][j][0]);
665 // copy row of Q
666 for(IT_ ip(_slip_row_ptr[i]); ip < _slip_row_ptr[i+1]; ++ip, ++op)
667 {
668 // get the normal vector of this slip filter entry
669 //const auto& nu = this->_slip_filters_v.at(_slip_fil_dqu[ip]).get_filter_vector().elements_view_r()[_slip_fil_idx[ip]];
670 // set the normal vector component
671 //val_x[op] = SolverDataType(nu[j]);
672 val_x[op] = SolverDataType(this->_slip_filters_v.at(_slip_fil_dqu[ip]).get_filter_vector().elements_view_r()[_slip_fil_idx[ip]][j]);
673 }
674 }
675 }
676
677 // next, loop over all rows of D/S/P
678 for(IT_ i(0); i < num_dofs_p; ++i)
679 {
680 // is this a unit row?
681 if(_unit_mask_p[i] != 0)
682 {
683 val_x[op++] = SolverDataType(1);
684 continue;
685 }
686
687 // not a unit row, so copy rows of D, S and P
688 // copy row of D
689 for(IT_ ip(row_ptr_d[i]); ip < row_ptr_d[i+1]; ++ip)
690 for(int k(0); k < dim_; ++k, ++op)
691 val_x[op] = SolverDataType(val_d[ip][0][k]);
692 // copy row of S (if it exists)
693 if(_matrix_s != nullptr)
694 {
695 if(this->_need_main_diagonal)
696 {
697 bool have_diag = false;
698 for(IT_ ip(row_ptr_s[i]); ip < row_ptr_s[i+1]; ++ip, ++op)
699 {
700 if(!have_diag && (col_idx_s[ip] > i))
701 {
702 val_x[op++] = SolverDataType(0);
703 have_diag = true;
704 }
705 val_x[op] = SolverDataType(val_s[ip]);
706 have_diag = have_diag || (col_idx_s[ip] == i);
707 }
708 }
709 else
710 {
711 for(IT_ ip(row_ptr_s[i]); ip < row_ptr_s[i+1]; ++ip, ++op)
712 val_x[op] = SolverDataType(val_s[ip]);
713 }
714 }
715 else if(this->_need_main_diagonal)
716 {
717 // insert a main diagonal entry
718 val_x[op++] = SolverDataType(0);
719 }
720
721 // copy row of P (if we have a pressure mean filter)
722 if(this->_have_mean_filter_p)
723 {
724 const auto v = _mean_filter_p_dual.elements_view_r();
725 val_x[op++] = SolverDataType(v[i]);
726 }
727 }
728
729 // next, add all slip filter entries Q^T
730 for(auto it(_slip_filters_v.begin()); it != _slip_filters_v.end(); ++it)
731 {
732 const IT_ nsx = IT_(it->num_nzes());
733 const auto nu = it->get_filter_vector().elements_view_r();
734 for(IT_ j(0); j < nsx; ++j)
735 {
736 for(int k(0); k < dim_; ++k, ++op)
737 val_x[op] = SolverDataType(nu[j][k]);
738 if(this->_need_main_diagonal)
739 val_x[op++] = SolverDataType(0);
740 }
741 }
742
743 // finally, add the pressure mean filter vector P^T (if it exists)
744 if(this->_have_mean_filter_p)
745 {
746 const auto v = _mean_filter_p_dual.elements_view_r();
747 for(IT_ j(0); j < num_dofs_p; ++j, ++op)
748 val_x[op] = SolverDataType(v[j]);
749 if(this->_need_main_diagonal)
750 val_x[op++] = SolverDataType(0);
751 }
752
753 // sanity check
754 XASSERT(op == _solver_matrix.num_nzes());
755 }
756
758 virtual void done_numeric()
759 {
760 // nothing to do here
761 }
762
770 virtual void done_symbolic()
771 {
772 _slip_row_ptr.clear();
773 _slip_col_idx.clear();
774 _slip_fil_dqu.clear();
775 _slip_fil_idx.clear();
776 _slip_filters_v.clear();
777 _unit_filters_v.clear();
778 _unit_filters_p.clear();
779 _mean_filter_p_dual.clear();
780 _unit_mask_v.clear();
781 _unit_mask_p.clear();
782 _have_mean_filter_p = false;
783 _filters_already_set = false;
784 }
785
786 protected:
789 {
790 if(fuc != nullptr)
791 {
792 // get the old unit filter
793 UnitFilterTypeV& old_filter = this->_unit_filters_v.at(fuc->at(0u)++);
794
795 // compare layout of old and new filter
796 if(!old_filter.get_filter_vector().same_layout(filter.get_filter_vector()))
797 XABORTM("velocity unit filter layout has changed");
798
799 // update the filter
800 old_filter.clone(filter, LAFEM::CloneMode::Shallow);
801 }
802 else
803 {
804 // push new filter
805 this->_unit_filters_v.push_back(filter.clone(LAFEM::CloneMode::Shallow));
806 }
807 }
808
811 {
812 if(fuc != nullptr)
813 {
814 // get the old slip filter
815 SlipFilterTypeV& old_filter = this->_slip_filters_v.at(fuc->at(1u)++);
816
817 // compare layout of old and new filter
818 if(!old_filter.get_filter_vector().same_layout(filter.get_filter_vector()))
819 XABORTM("velocity slip filter layout has changed");
820
821 // update the filter
822 old_filter.clone(filter, LAFEM::CloneMode::Shallow);
823 }
824 else
825 {
826 // push new filter
827 this->_slip_filters_v.push_back(filter.clone(LAFEM::CloneMode::Shallow));
828 }
829 }
830
833 {
834 // nothing to do here
835 }
836
838 template<typename First_>
840 {
841 this->_add_filter_velo(filter.first(), fuc);
842 }
843
845 template<typename First_, typename Second_, typename... Rest_>
847 {
848 this->_add_filter_velo(filter.first(), fuc);
849 this->_add_filter_velo(filter.rest(), fuc);
850 }
851
853 template<typename SubFilter_>
855 {
856 for(auto it = filter.begin(); it != filter.end(); ++it)
857 {
858 this->_add_filter_velo(it->second, fuc);
859 }
860 }
861
863 template<typename LocFilter_, typename Mirror_>
865 {
866 this->_add_filter_velo(filter.local(), fuc);
867 }
868
871 {
872 if(fuc != nullptr)
873 {
874 // get the old unit filter
875 UnitFilterTypeP& old_filter = this->_unit_filters_p.at(fuc->at(2u)++);
876
877 // compare layout of old and new filter
878 if(!old_filter.get_filter_vector().same_layout(filter.get_filter_vector()))
879 XABORTM("pressure unit filter layout has changed");
880
881 // update the filter
882 old_filter.clone(filter, LAFEM::CloneMode::Shallow);
883 }
884 else
885 {
886 this->_unit_filters_p.push_back(filter.clone(LAFEM::CloneMode::Shallow));
887 }
888 }
889
892 {
893 if(fuc != nullptr)
894 {
895 XASSERTM(this->_mean_filter_p_dual.empty() == filter.get_vec_dual().empty(), "pressure mean filter has changed");
896 }
897 this->_mean_filter_p_dual.clone(filter.get_vec_dual(), LAFEM::CloneMode::Shallow);
898 }
899
902 {
903 if(fuc != nullptr)
904 {
905 XASSERTM(this->_mean_filter_p_dual.empty() == filter.get_vec_dual().empty(), "pressure mean filter has changed");
906 }
907 this->_mean_filter_p_dual.clone(filter.get_vec_dual(), LAFEM::CloneMode::Shallow);
908 }
909
912 {
913 // nothing to do here
914 }
915
917 template<typename First_>
919 {
920 this->_add_filter_pres(filter.first(), fuc);
921 }
922
924 template<typename First_, typename Second_, typename... Rest_>
926 {
927 this->_add_filter_pres(filter.first(), fuc);
928 this->_add_filter_pres(filter.rest(), fuc);
929 }
930
932 template<typename SubFilter_>
934 {
935 for(auto it = filter.begin(); it != filter.end(); ++it)
936 {
937 this->_add_filter_pres(it->second, fuc);
938 }
939 }
940
942 template<typename LocFilter_, typename Mirror_>
944 {
945 this->_add_filter_pres(filter.local(), fuc);
946 }
947
950 {
951 const IT_ nv = IT_(_matrix_a.num_rows());
952
953 _slip_row_ptr.clear();
954 _slip_col_idx.clear();
955 _slip_fil_dqu.clear();
956 _slip_fil_idx.clear();
957 _slip_row_ptr.resize(nv+1u, IT_(0));
958
959 if(this->_slip_filters_v.empty())
960 return IT_(0);
961
962 // count the total number of slip filter entries
963 IT_ count(0u);
964 for(std::size_t it(0u); it < this->_slip_filters_v.size(); ++it)
965 {
966 const IT_ ni = IT_(this->_slip_filters_v.at(it).num_nzes());
967 const Memory::TypedView<IT_> idx = this->_slip_filters_v.at(it).get_filter_vector().indices_view_r();
968 for(IT_ i(0); i < ni; ++i)
969 ++_slip_row_ptr.at(idx[i]+1);
970 count += ni;
971 }
972
973 // only empty filters?
974 if(count == IT_(0))
975 return count;
976
977 // compute row pointer array from aux
978 for(IT_ i(0); i < nv; ++i)
979 {
980 _slip_row_ptr[i+1u] += _slip_row_ptr[i];
981 }
982
983 // allocate vectors
984 _slip_col_idx.resize(count);
985 _slip_fil_dqu.resize(count);
986 _slip_fil_idx.resize(count);
987
988 // copy row pointer
989 std::vector<IT_> aux(_slip_row_ptr);
990
991 // loop over all slip filters
992 IT_ offset(0u);
993 for(std::size_t it(0u); it < this->_slip_filters_v.size(); ++it)
994 {
995 const IT_ ni = IT_(this->_slip_filters_v.at(it).num_nzes());
996 const Memory::TypedView<IT_> idx = this->_slip_filters_v.at(it).get_filter_vector().indices_view_r();
997
998 // and emplace them
999 for(IT_ i(0); i < ni; ++i)
1000 {
1001 IT_& px = aux[idx[i]];
1002 _slip_col_idx[px] = offset + i;
1003 _slip_fil_dqu[px] = IT_(it);
1004 _slip_fil_idx[px] = i;
1005 ++px;
1006 }
1007 offset += ni;
1008 }
1009
1010 XASSERT(count == offset);
1011 return count;
1012 }
1013
1016 {
1017 // reset mask to 0
1018 _unit_mask_v.clear();
1019 _unit_mask_v.resize(_matrix_a.num_rows(), 0);
1020
1021 for(const auto& it : this->_unit_filters_v)
1022 {
1023 const IT_ n = IT_(it.num_nzes());
1024 const Memory::TypedView<IT_> idx = it.indices_view_r();
1025 for(IT_ k(0); k < n; ++k)
1026 _unit_mask_v.at(idx[k]) = 1;
1027 }
1028 }
1029
1032 {
1033 // reset mask to 0
1034 _unit_mask_p.clear();
1035 _unit_mask_p.resize(_matrix_d.num_rows(), 0);
1036
1037 for(const auto& it : this->_unit_filters_p)
1038 {
1039 const IT_ n = IT_(it.num_nzes());
1040 const Memory::TypedView<IT_> idx = it.indices_view_r();
1041 for(IT_ k(0); k < n; ++k)
1042 _unit_mask_p.at(idx[k]) = 1;
1043 }
1044 }
1045 }; // class DirectStokesCore
1046
1048 static constexpr bool direct_stokes_solver_available =
1049#ifdef FEAT_HAVE_CUDSS
1050 true ||
1051#endif
1052#ifdef FEAT_HAVE_MKL
1053 true ||
1054#endif
1055#ifdef FEAT_HAVE_UMFPACK
1056 true ||
1057#endif
1058 false;
1059
1067 template<typename Matrix_, typename Filter_>
1069
1078 template<typename MatrixA_, typename MatrixB_, typename MatrixD_, typename FilterV_, typename FilterP_>
1079 class DirectStokesSolver<LAFEM::SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>, LAFEM::TupleFilter<FilterV_, FilterP_>> :
1080 public Solver::SolverBase<LAFEM::TupleVector<typename MatrixB_::VectorTypeL, typename MatrixD_::VectorTypeL>>
1081 {
1082 public:
1089
1092
1095
1097 typedef typename StokesCoreType::SolverMatrixType SolverMatrixType;
1099 typedef typename StokesCoreType::SolverVectorType SolverVectorType;
1101 typedef typename StokesCoreType::SolverFilterType SolverFilterType;
1102
1104#if defined(FEAT_HAVE_CUDSS) || defined(DOXYGEN)
1105 static constexpr bool have_backend_cudss = true;
1106#else
1107 static constexpr bool have_backend_cudss = false;
1108#endif // FEAT_HAVE_CUDSS
1109
1111#if defined(FEAT_HAVE_MKL) || defined(DOXYGEN)
1112 static constexpr bool have_backend_mkldss = true;
1113#else
1114 static constexpr bool have_backend_mkldss = false;
1115#endif // FEAT_HAVE_MKL
1116
1118#if defined(FEAT_HAVE_UMFPACK) || defined(DOXYGEN)
1119 static constexpr bool have_backend_umfpack = true;
1120#else
1121 static constexpr bool have_backend_umfpack = false;
1122#endif // FEAT_HAVE_UMFPACK
1123
1124 protected:
1129
1132
1135
1137 std::shared_ptr<Solver::SolverBase<SolverVectorType>> _direct_solver;
1138
1139#if defined(FEAT_HAVE_CUDSS) || defined(DOXYGEN)
1141 std::shared_ptr<Solver::CUDSS> _cudss_solver;
1142#endif // FEAT_HAVE_CUDSS
1143#if defined(FEAT_HAVE_MKL) || defined(DOXYGEN)
1145 std::shared_ptr<Solver::MKLDSS> _mkldss_solver;
1146#endif // FEAT_HAVE_MKL
1147#if defined(FEAT_HAVE_UMFPACK) || defined(DOXYGEN)
1149 std::shared_ptr<Solver::Umfpack> _umfpack_solver;
1150#endif // FEAT_HAVE_UMFPACK
1151
1152 public:
1174 explicit DirectStokesSolver(const SystemMatrixType& matrix_sys, const SystemFilterType& filter_sys, PreferredBackend backend) :
1175 _matrix_sys(matrix_sys),
1176 _filter_sys(filter_sys),
1177 _stokes_core(_matrix_sys.block_a(), _matrix_sys.block_b(), _matrix_sys.block_d()),
1178 _direct_solver()
1179 {
1180#ifdef FEAT_HAVE_CUDSS
1181 // create a CUDSS solver if this is the desired backend or if the desired backend is not available
1182 if((backend == PreferredBackend::cuda) ||
1183 ((backend == PreferredBackend::mkl) && !have_backend_mkldss) ||
1184 ((backend == PreferredBackend::generic) && !have_backend_umfpack))
1185 {
1186 _cudss_solver = Solver::new_cudss(_stokes_core.get_solver_matrix());
1187 _direct_solver = _cudss_solver;
1188 // main diagonal is not absolutely necessary for cuDSS, but it seems to improve precision a bit
1189 _stokes_core.set_need_main_diagonal(true);
1190 }
1191 else
1192#endif // FEAT_HAVE_CUDSS
1193#ifdef FEAT_HAVE_MKL
1194 // create a MKL DSS solver if this is the desired backend or if the desired backend is not available
1195 if((backend == PreferredBackend::mkl) ||
1196 ((backend == PreferredBackend::generic) && !have_backend_umfpack))
1197 {
1198 _mkldss_solver = Solver::new_mkl_dss(_stokes_core.get_solver_matrix());
1199 _direct_solver = _mkldss_solver;
1200 // MKL emphasizes that main diagonal is absolutely mandatory
1201 _stokes_core.set_need_main_diagonal(true);
1202 }
1203 else
1204#endif // FEAT_HAVE_MKL
1205#ifdef FEAT_HAVE_UMFPACK
1206 // fallback or generic: create an UMFPACK solver
1207 {
1208 // create an UMFPACK solver
1209 _umfpack_solver = Solver::new_umfpack(_stokes_core.get_solver_matrix());
1210 _direct_solver = _umfpack_solver;
1211 }
1212#else
1213 {
1214 XABORTM("DirectStokesSolver can only be used if at least one of {cuDSS, MKL DSS, UMFPACK} is available");
1215 }
1216#endif
1217 // ensure that compilers don't warn about unused parameters
1218 (void)backend;
1219 }
1220
1232 explicit DirectStokesSolver(const SystemMatrixType& matrix_sys, const SystemFilterType& filter_sys) :
1233 DirectStokesSolver(matrix_sys, filter_sys, Backend::get_preferred_backend())
1234 {
1235 }
1236
1238 virtual String name() const override
1239 {
1240#ifdef FEAT_HAVE_CUDSS
1241 if(_cudss_solver)
1242 return "DirectStokesSolver<SaddlePointMatrix>[CUDSS]";
1243#endif
1244#ifdef FEAT_HAVE_MKL
1245 if(_mkldss_solver)
1246 return "DirectStokesSolver<SaddlePointMatrix>[MKLDSS]";
1247#endif
1248#ifdef FEAT_HAVE_UMFPACK
1249 if(_umfpack_solver)
1250 return "DirectStokesSolver<SaddlePointMatrix>[UMFPACK]";
1251#endif
1252 return "DirectStokesSolver<SaddlePointMatrix>[???]";
1253 }
1254
1256 virtual void init_symbolic() override
1257 {
1258 BaseClass::init_symbolic();
1259
1260 // add the filter
1261 _stokes_core.set_filters(_filter_sys);
1262
1263 // initialize
1264 _stokes_core.init_symbolic();
1265 if(_direct_solver)
1266 _direct_solver->init_symbolic();
1267
1268 // create vectors
1269 _vec_sol = _stokes_core.create_solver_vector();
1270 _vec_rhs = _stokes_core.create_solver_vector();
1271 }
1272
1274 virtual void init_numeric() override
1275 {
1276 BaseClass::init_numeric();
1277
1278 // update filter
1279 _stokes_core.update_filters(_filter_sys);
1280
1281 // initialize
1282 _stokes_core.init_numeric();
1283 if(_direct_solver)
1284 _direct_solver->init_numeric();
1285 }
1286
1288 virtual void done_numeric() override
1289 {
1290 if(_direct_solver)
1291 _direct_solver->done_numeric();
1292 _stokes_core.done_numeric();
1293 BaseClass::done_numeric();
1294 }
1295
1297 virtual void done_symbolic() override
1298 {
1299 _vec_rhs.clear();
1300 _vec_sol.clear();
1301 if(_direct_solver)
1302 _direct_solver->done_symbolic();
1303 _stokes_core.done_symbolic();
1304 BaseClass::done_symbolic();
1305 }
1306
1322 virtual Solver::Status apply(SystemVectorType& vec_cor, const SystemVectorType& vec_def) override
1323 {
1324 // upload RHS vector
1325 _stokes_core.upload_vector(this->_vec_rhs, vec_def.template at<0>(), vec_def.template at<1>());
1326
1327 // apply the actual solver
1328 Solver::Status status = _direct_solver->apply(this->_vec_sol, this->_vec_rhs);
1329
1330 // download solution vector
1331 _stokes_core.download_vector(this->_vec_sol, vec_cor.template at<0>(), vec_cor.template at<1>());
1332
1333 // return status
1334 return status;
1335 }
1336 }; // class DirectStokesSolver<LAFEM::SaddlePointMatrix<...>, LAFEM::TupleFilter<...>>
1337
1351 template<typename LocalMatrix_, typename LocalFilter_, typename Mirror_>
1352 class DirectStokesSolver<Global::Matrix<LocalMatrix_, Mirror_, Mirror_>, Global::Filter<LocalFilter_, Mirror_>> :
1353 public Solver::SolverBase<Global::Vector<typename LocalMatrix_::VectorTypeR, Mirror_>>
1354 {
1355 public:
1362
1365
1366 protected:
1371
1374
1375 public:
1376 explicit DirectStokesSolver(const SystemMatrixType& matrix_sys, const SystemFilterType& filter_sys) :
1377 _matrix_sys(matrix_sys),
1378 _filter_sys(filter_sys),
1379 _local_solver(_matrix_sys.local(), _filter_sys.local())
1380 {
1381 }
1382
1384 virtual String name() const override
1385 {
1386 return "DirectStokesSolver<Global::Matrix>";
1387 }
1388
1390 virtual void init_symbolic() override
1391 {
1392 // ensure that we are on one process only...
1393 const Dist::Comm* comm = _matrix_sys.get_comm();
1394 if(comm != nullptr)
1395 {
1396 XASSERTM(comm->size() <= 1, "DirectStokesSolver can only work on one process");
1397 }
1398
1399 BaseClass::init_symbolic();
1400 _local_solver.init_symbolic();
1401 }
1402
1404 virtual void init_numeric() override
1405 {
1406 BaseClass::init_numeric();
1407 _local_solver.init_numeric();
1408 }
1409
1411 virtual void done_numeric() override
1412 {
1413 _local_solver.done_numeric();
1414 BaseClass::done_numeric();
1415 }
1416
1418 virtual void done_symbolic() override
1419 {
1420 _local_solver.done_symbolic();
1421 BaseClass::done_symbolic();
1422 }
1423
1439 virtual Solver::Status apply(SystemVectorType& vec_sol, const SystemVectorType& vec_rhs) override
1440 {
1441 return _local_solver.apply(vec_sol.local(), vec_rhs.local());
1442 }
1443 }; // class DirectStokesSolver<Global::Matrix<...>, Global::Filter<...>>
1444
1457 template<typename Matrix_, typename Filter_>
1458 inline std::shared_ptr<DirectStokesSolver<Matrix_, Filter_>> new_direct_stokes_solver(const Matrix_& matrix, const Filter_& filter)
1459 {
1460 return std::make_shared<DirectStokesSolver<Matrix_, Filter_>>(matrix, filter);
1461 }
1462 } // namespace Solver
1463} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Backend support class.
Definition: backend.hpp:38
Communicator class.
Definition: dist.hpp:1657
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
Global Filter wrapper class template.
Definition: filter.hpp:21
Global Matrix wrapper class template.
Definition: matrix.hpp:40
const Dist::Comm * get_comm() const
Returns a const pointer to the internal communicator of the gates of the matrix.
Definition: matrix.hpp:184
Mean Filter class template.
Definition: mean_filter.hpp:23
Global vector wrapper class template.
Definition: vector.hpp:68
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:122
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
virtual void clear()
Free all allocated arrays.
Definition: container.hpp:885
Blocked Dense data vector class template.
Index size_raw() const
Returns the size of this vector in scalar data type entries, i.e. its number of entries multiplied by...
Dense data vector class template.
bool empty() const
Checks whether the vector is empty, i.e. if it has size 0.
DenseVector clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Filter Chainclass template.
Sequence of filters of the same type.
Mean Filter class template.
Definition: mean_filter.hpp:22
Blocked None Filter class template.
None Filter class template.
Definition: none_filter.hpp:29
Saddle-Point matrix meta class template.
Slip Filter class template.
Definition: slip_filter.hpp:67
SlipFilter clone(CloneMode clone_mode=CloneMode::Deep) const
Creates a clone of itself.
CSR based blocked sparse matrix.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
Index num_cols() const
Retrieve matrix column count.
CSR based sparse matrix.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
TupleVector meta-filter class template.
Variadic TupleVector class template.
Unit Filter Blocked class template.
UnitFilterBlocked clone(CloneMode clone_mode=CloneMode::Deep) const
Creates a clone of itself.
Unit Filter class template.
Definition: unit_filter.hpp:31
UnitFilter clone(CloneMode clone_mode=CloneMode::Deep) const
Creates a clone of itself.
Typed memory view class.
virtual void upload_vector(SolverVectorType &vector, const VectorTypeV &vector_v, const VectorTypeP &vector_p) const
Uploads a velocity/pressure vector pair to a solver vector.
virtual void download_vector(const SolverVectorType &vector, VectorTypeV &vector_v, VectorTypeP &vector_p) const
Downloads a velocity/pressure vector pair from a solver vector.
Core class for conversion of Stokes systems to CSR format for direct solvers.
virtual Solver::Status apply(SystemVectorType &vec_sol, const SystemVectorType &vec_rhs) override
Applies the direct Stokes solver.
virtual Solver::Status apply(SystemVectorType &vec_cor, const SystemVectorType &vec_def) override
Applies the direct Stokes solver.
std::shared_ptr< Solver::Umfpack > _umfpack_solver
our UMFPACK solver object; used if no other direct solver was chosen
std::shared_ptr< Solver::CUDSS > _cudss_solver
our CUDSS solver object; used if preferred backend is PreferredBackend::cuda
std::shared_ptr< Solver::MKLDSS > _mkldss_solver
our MKLDSS solver object; used if preferred backend is PreferredBackend::mkl
DirectStokesSolver(const SystemMatrixType &matrix_sys, const SystemFilterType &filter_sys)
Constructor for preferred backend.
LAFEM::TupleVector< typename MatrixB_::VectorTypeL, typename MatrixD_::VectorTypeL > SystemVectorType
our system vector type
DirectStokesCore< double, Index, MatrixA_, MatrixB_, MatrixD_ > StokesCoreType
the direct Stokes core class type
std::shared_ptr< Solver::SolverBase< SolverVectorType > > _direct_solver
the actual direct solver; opaque SolverBase pointer
DirectStokesSolver(const SystemMatrixType &matrix_sys, const SystemFilterType &filter_sys, PreferredBackend backend)
Constructor.
Direct Stokes solver class template.
Polymorphic solver interface.
Definition: base.hpp:183
String class implementation.
Definition: string.hpp:47
std::shared_ptr< MKLDSS > new_mkl_dss(const LAFEM::SparseMatrixCSR< double, Index > &matrix)
Creates a new MKLDSS solver object.
Definition: mkl_dss.hpp:112
std::shared_ptr< DirectStokesSolver< Matrix_, Filter_ > > new_direct_stokes_solver(const Matrix_ &matrix, const Filter_ &filter)
Creates a new DirectStokesSolver object.
std::shared_ptr< CUDSS > new_cudss(const LAFEM::SparseMatrixCSR< double, Index > &matrix)
Creates a new CUDSS solver object.
Definition: cudss.hpp:107
Status
Solver status return codes enumeration.
Definition: base.hpp:47
static constexpr bool direct_stokes_solver_available
specifies whether at least one backend for the DirectStokesSolver class is available
std::shared_ptr< Umfpack > new_umfpack(const LAFEM::SparseMatrixCSR< double, Index > &matrix)
Creates a new Umfpack solver object.
Definition: umfpack.hpp:116
FEAT namespace.
Definition: adjactor.hpp:12
PreferredBackend
The backend that shall be used in all compute heavy calculations.
Definition: backend.hpp:18
std::uint64_t Index
Index data type.