dune-fem 2.12-git
Loading...
Searching...
No Matches
istlpreconditioner.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_ISTLPRECONDITIONERWRAPPER_HH
2#define DUNE_FEM_ISTLPRECONDITIONERWRAPPER_HH
3
4#include <memory>
5#include <type_traits>
6
7// standard diagonal preconditioner
9
10#if HAVE_DUNE_ISTL
12
15
18#endif
19
20#include <dune/fem/solver/parameter.hh> // SolverParameter
24
25namespace Dune
26{
27
28 namespace Fem
29 {
30
32 {
34 return std::vector< int > ({ SolverParameter::none, // no preconditioner
35 SolverParameter::ssor, // SSOR preconditioner
36 SolverParameter::sor , // SOR preconditioner
37 SolverParameter::ilu , // ILU preconditioner
38 SolverParameter::gauss_seidel,// Gauss-Seidel preconditioner
39 SolverParameter::jacobi, // Jacobi preconditioner
40 SolverParameter::amg_ilu, // AMG with ILU-0 smoother (deprecated)
41 SolverParameter::amg_jacobi, // AMG with Jacobi smoother
42 SolverParameter::ildl // ILDL from istl
43 });
44 }
45 };
46
47
48 template <class MatrixImp>
50
51 template <class MatrixImp>
53
54 template <class MatrixImp>
56
57 struct ISTLSolverParameter : public LocalParameter< SolverParameter, ISTLSolverParameter >
58 {
60 public:
63
67
71
75
76 virtual double overflowFraction () const
77 {
78 return parameter().getValue< double >( keyPrefix() + "matrix.overflowfraction", 1.0 );
79 }
80
81 virtual bool fastILUStorage () const
82 {
83 return parameter().getValue< bool >( keyPrefix() + "preconditioning.fastilustorage", true );
84 }
85 };
86
87#if HAVE_DUNE_ISTL
96 template< class MatrixObject, class X, class Y, int method, int l=1 >
97 class ParallelIterative : public Preconditioner<X,Y>
98 {
99 public:
100 typedef typename MatrixObject :: ColumnDiscreteFunctionType DiscreteFunctionType ;
101
102 protected:
103 typedef typename X::field_type field_type;
104 typedef typename MatrixObject::MatrixType MatrixType;
105 typedef MatrixType M;
106 typedef typename M::block_type MatrixBlockType;
107
108 typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType ;
109 typedef typename DiscreteFunctionSpaceType:: DomainFieldType DomainFieldType;
110
111 static const bool isNumber = (M::block_type::rows == M::block_type::cols) && (M::block_type::rows == 1);
112
114 // BlockVector from dune-istl
115 typedef BlockVector< FlatMatrixBlock > DiagonalType;
116
117 typedef FieldMatrixConverter< FlatMatrixBlock, MatrixBlockType > MatrixConverterType;
118
119 // Implementation of SOR and Jacobi's method
120 // Note: for SOR we have xnew == xold
121 template <class rowiterator, bool forward>
122 void iterate (const M& A, X& xnew, const X& xold, const Y& b, const field_type& w,
123 const DiagonalType& diagInv,
124 rowiterator i,
125 const rowiterator endi,
127 {
128 typedef typename M::ConstColIterator coliterator;
129
130 typedef typename Y::block_type bblock;
131 typedef typename X::block_type xblock;
132
133 bblock rhs;
134 xblock v;
135
136 // Initialize nested data structure if there are entries
137 if( i != endi )
138 v=xnew[0];
139
140 const bool hasDiagInv = diagInv.size() > 0 ;
141
142 const auto nextRow = [&i]()
143 {
144 // increment/decrement iterator (for new iterator we only need ++
145 if constexpr ( forward or requires{ i.isNewIterator();} )
146 ++i;
147 else
148 --i;
149 };
150
151 for (; i!=endi; nextRow() )
152 {
153 const coliterator endj=(*i).end(); // iterate over a_ij with j < i
154 coliterator j=(*i).begin();
155 const auto rowIdx = i.index();
156
157 // treat missing rows as unit rows
158 if( j == endj )
159 {
160 xnew[rowIdx] += w*b[rowIdx];
161 continue ;
162 }
163
164 rhs = b[rowIdx]; // rhs = b_i
165 if constexpr (isNumber)
166 {
167 // note that for SOR xnew == xold
168 for (; j.index()<rowIdx; ++j)
169 rhs -= (*j)[0][0] * xold[j.index()]; // rhs -= sum_{j<i} a_ij * x_j
170
171 // not needed, since we store the diagonal separately
172 coliterator diag=j; // *diag = a_ii
173 for (; j!=endj; ++j)
174 rhs -= (*j)[0][0] * xold[j.index()]; // rhs -= sum_{j<i} a_ij * x_j
175
176 // v = rhs / diag
177 if( hasDiagInv )
178 v = rhs * diagInv[ rowIdx ];
179 else
180 v = rhs / (*diag)[0][0];
181
182 xnew[ rowIdx ] += w*v; // x_i = w / a_ii * (b_i - sum_{j<i} a_ij * xnew_j - sum_{j>=i} a_ij * xold_j)
183 }
184 else
185 {
186 // note that for SOR xnew == xold
187 for (; j.index()<rowIdx; ++j)
188 (*j).mmv(xold[j.index()],rhs); // rhs -= sum_{j<i} a_ij * x_j
189 coliterator diag=j; // *diag = a_ii
190 for (; j!=endj; ++j)
191 (*j).mmv(xold[j.index()],rhs); // rhs -= sum_{j<i} a_ij * x_j
192
193 // v = rhs / diag
194 if( hasDiagInv )
195 {
196 MatrixConverterType m( diagInv[ rowIdx ] );
197 m.solve( v, rhs );
198 }
199 else
200 (*diag).solve(v, rhs);
201 xnew[rowIdx].axpy(w,v); // x_i = w / a_ii * (b_i - sum_{j<i} a_ij * xnew_j - sum_{j>=i} a_ij * xold_j)
202 }
203 }
204 }
205
206 // Implementation of SOR and Jacobi's method
207 // Note: for SOR we have xnew == xold
208 void forwardIterate (const M& A, X& xnew, const X& xold, const Y& b, const field_type& w,
209 const DiagonalType& diagInv ) const
210 {
211 bool runParallel = threading_ && (&xnew != &xold); // Jacobi only
212
213 if( runParallel )
214 {
215 auto doIterate = [this, &A, &xnew, &xold, &b, &w, &diagInv] ()
216 {
217 const auto slice = A.sliceBeginEnd( MPIManager::thread(), MPIManager::numThreads() );
218 // standard forwards iteration
219 iterate( A, xnew, xold, b, w, diagInv,
220 A.slicedBegin( slice.first ), A.slicedEnd( slice.second ), std::true_type() );
221 };
222
223 try {
224 // execute in parallel
225 MPIManager :: run ( doIterate );
226 }
227 catch ( const SingleThreadModeError& e )
228 {
229 runParallel = false;
230 }
231 }
232
233 if( ! runParallel )
234 {
235 // serial version
236 iterate( A, xnew, xold, b, w, diagInv, A.begin(), A.end(), std::true_type() );
237 }
238 }
239
240 // Implementation of SOR and Jacobi's method
241 // Note: for SOR we have xnew == xold
242 void backwardIterate (const M& A, X& xnew, const X& xold, const Y& b, const field_type& w,
243 const DiagonalType& diagInv ) const
244 {
245 // backwards iteration (use r-iterators)
246 iterate( A, xnew, xold, b, w, diagInv, A.beforeEnd(), A.beforeBegin(), std::false_type() );
247 }
248
249 protected:
250 const MatrixObject& mObj_;
251 const MatrixType& _A_;
252 const int _n;
253 const double _w;
254
255 DiagonalType diagonalInv_;
256
257 const bool threading_;
258
259 public:
260 ParallelIterative( const MatrixObject& mObj, int n=1, double relax=1.0, const bool verbose=false )
261 : mObj_( mObj ),
262 _A_( mObj.exportMatrix() ),
263 _n( n ), _w(relax),
264 threading_( mObj.threading() )
265 {
266 if ( verbose )
267 {
268 std::string name = SolverParameter::preconditionMethodTable( method );
269 std::cout << "Using ParallelIterative(" << name << "): it = " << n << ", w = "<< relax << std::endl;
270 }
271
272 if( _n <= 0 )
273 DUNE_THROW(InvalidStateException,"ParallelIterative: number of iterations `" << _n << "` is invalid!" );
274
276
277 const auto& space = mObj_.domainSpace();
278 if( space.continuous() )
279 {
280 // create BlockVectorWrapper for BlockVector
281 ISTLBlockVector< FlatMatrixBlock > diagonalInv( &diagonalInv_ );
282
283 diagonalInv.resize(space.blockMapper().size());
284 assert( space.blockMapper().size() == _A_.N() );
285 diagonalInv.clear();
286
287 // extract diagonal elements from matrix object
288 {
289 const auto end = _A_.end();
290 for( auto row = _A_.begin(); row != end; ++row )
291 {
292 // get diagonal entry of matrix if existent
293 auto diag = (*row).find( row.index() );
294 if( diag != (*row).end() )
295 {
296 MatrixConverterType m( diagonalInv[ row.index() ] );
297 m = (*diag);
298 }
299 }
300 }
301
302 // make diagonal consistent (communicate at border dofs)
303 // get default communication operation type
304 typename DiscreteFunctionSpaceType :: template
305 CommDataHandle< DiscreteFunctionType > :: OperationType operation;
306 space.communicate( diagonalInv, operation );
307
308 // In general: store 1/diag if diagonal is number
309 //
310 // note: We set near-zero entries to 1 to avoid NaNs. Such entries occur
311 // if DoFs are excluded from matrix setup
312 if constexpr( isNumber )
313 {
314 const double eps = 16.*std::numeric_limits< double >::epsilon();
315 for( auto& diag : diagonalInv )
316 {
317 diag = (std::abs( diag ) < eps ? 1. : 1. / diag );
318 }
319 }
320 }
321 }
322
323 ParallelIterative( const ParallelIterative& org )
324 : mObj_( org.mObj_ ),
325 _A_( org._A_ ),
326 _n( org._n ), _w( org._w ),
327 diagonalInv_( org.diagonalInv_ )
328 {
329 }
330
332 void pre (X& x, Y& b) override {}
333
335 void apply (X& v, const Y& d) override
336 {
337 const auto& space = mObj_.domainSpace();
338 const bool continuous = space.continuous();
339
340 // for communication
341 DiscreteFunctionType tmp("ParIt::apply", space, v );
342
343 static constexpr bool jacobi = (method == SolverParameter::jacobi);
344
346 if constexpr ( jacobi )
347 xTmp.reset( new X(v) );
348
349 X& x = (jacobi) ? (*xTmp) : v;
350
351 for (int i=0; i<_n; ++i)
352 {
353 forwardIterate(_A_, v, x, d, _w, diagonalInv_ );
354
355 if constexpr ( method == SolverParameter::ssor )
356 {
357 // seems not to be necessary
358 // communicate border unknowns
359 //tmp.communicate();
360
361 // create symmetry by iterating backwards
362 backwardIterate(_A_, v, x, d, _w, diagonalInv_ );
363 }
364
365 // communicate border unknowns
366 tmp.communicate();
367
368 // for Jacobi now update the result
369 if constexpr ( jacobi )
370 {
371 x = v;
372 // for Jacobi skip last communication since this
373 // is already in consistent state at this point
374 if( continuous && i == _n-1)
375 continue ;
376 }
377 }
378 }
379
381 void post (X& x) override {}
382
384 SolverCategory::Category category () const override { return SolverCategory::sequential; }
385 };
386
387 template< class MatrixObject, class X, class Y >
388 using ParallelSOR = ParallelIterative< MatrixObject, X, Y, SolverParameter::sor>;
389
390 template< class MatrixObject, class X, class Y >
391 using ParallelSSOR = ParallelIterative< MatrixObject, X, Y, SolverParameter::ssor>;
392
393 template< class MatrixObject, class X, class Y >
394 using ParallelJacobi = ParallelIterative< MatrixObject, X, Y, SolverParameter::jacobi>;
395
396 template< class X, class Y >
397 class IdentityPreconditionerWrapper : public Preconditioner<X,Y>
398 {
399 public:
401 typedef X domain_type;
403 typedef Y range_type;
405 typedef typename X::field_type field_type;
406
408 IdentityPreconditionerWrapper(){}
409
411 void pre (X& x, Y& b) override {}
412
414 void apply (X& v, const Y& d) override
415 {
416 if constexpr ( std::is_same< X, Y> :: value )
417 {
418 v = d;
419 }
420 }
421
423 void post (X& x) override {}
424
425 SolverCategory::Category category () const override { return SolverCategory::sequential; }
426 };
427
428
432 template<class MatrixImp>
433 class PreconditionerWrapper
434 : public Preconditioner<typename MatrixImp :: RowBlockVectorType,
435 typename MatrixImp :: ColBlockVectorType>
436 {
437 typedef MatrixImp MatrixType;
438 typedef typename MatrixType :: RowBlockVectorType X;
439 typedef typename MatrixType :: ColBlockVectorType Y;
440
441 // use BCRSMatrix type because of specializations in dune-istl
442 typedef typename MatrixType :: BaseType ISTLMatrixType ;
443
444 typedef typename MatrixType :: CommunicationType
445 CommunicationType;
446
447 // matrix adapter for AMG
448 //#if HAVE_MPI
449 // typedef Dune::OverlappingSchwarzOperator<
450 // ISTLMatrixType, X, Y, CommunicationType> OperatorType ;
451 //#else
452 typedef MatrixAdapter< ISTLMatrixType, X, Y > OperatorType;
453 //#endif
455
456 // auto pointer to preconditioning object
457 typedef Preconditioner<X,Y> PreconditionerInterfaceType;
459
460 // flag whether we have preconditioning, and if yes if it is AMG
461 const int preEx_;
462 const bool verbose_;
463
464 template <class XImp, class YImp>
465 struct Apply
466 {
467 inline static void apply(PreconditionerInterfaceType& preconder,
468 XImp& v, const YImp& d)
469 {
470 }
471
472 inline static void copy(XImp& v, const YImp& d)
473 {
474 }
475 };
476
477 template <class XImp>
478 struct Apply<XImp,XImp>
479 {
480 inline static void apply(PreconditionerInterfaceType& preconder,
481 XImp& v, const XImp& d)
482 {
483 preconder.apply( v ,d );
484 }
485
486 inline static void copy(X& v, const Y& d)
487 {
488 v = d;
489 }
490 };
491 public:
493 typedef X domain_type;
495 typedef Y range_type;
497 typedef typename X::field_type field_type;
498
500 PreconditionerWrapper (const PreconditionerWrapper& org, bool verbose)
501 : op_( org.op_ )
502 , preconder_(org.preconder_)
503 , preEx_(org.preEx_)
504 , verbose_( verbose )
505 {
506 }
507
509 PreconditionerWrapper(bool verbose)
510 : op_()
511 , preconder_()
512 , preEx_( 0 )
513 , verbose_( verbose )
514 {}
515
517 template <class PreconditionerType>
518 PreconditionerWrapper(MatrixType & matrix,
519 int iter,
520 field_type relax,
521 bool verbose,
522 const PreconditionerType* p)
523 : op_()
524 , preconder_( new PreconditionerType( matrix, iter, relax ) )
525 , preEx_( 1 )
526 , verbose_( verbose )
527 {
528 }
529
530
533 template <class PreconditionerType>
534 PreconditionerWrapper(MatrixType & matrix, bool verbose,
535 PreconditionerType* p)
536 : op_()
537 , preconder_( p )
538 , preEx_( 1 )
539 , verbose_( verbose )
540 {
541 }
542
544 template <class PreconditionerType>
545 PreconditionerWrapper(MatrixType & matrix,
546 int iter,
547 field_type relax,
548 bool verbose,
549 const PreconditionerType* p ,
550 const CommunicationType& comm)
551 //#if HAVE_MPI
552 // : op_( new OperatorType( matrix, comm ) )
553 //#else
554 : op_( new OperatorType( matrix ) )
555 //#endif
556 , preconder_( createAMGPreconditioner(comm, iter, relax, p) )
557 , preEx_( 2 )
558 , verbose_( verbose )
559 {
560 }
561
563 void pre (X& x, Y& b) override
564 {
565 // all the sequentiel implemented Preconditioners do nothing in pre and post
566 // apply preconditioner
567 if( preEx_ > 1 )
568 {
569 //X tmp (x);
570 preconder_->pre(x,b);
571 //assert( std::abs( x.two_norm() - tmp.two_norm() ) < 1e-15);
572 }
573 }
574
576 void apply (X& v, const Y& d) override
577 {
578 if( preEx_ )
579 {
580 // apply preconditioner
581 Apply<X,Y>::apply( *preconder_ , v, d );
582 }
583 else
584 {
585 Apply<X,Y>::copy( v, d );
586 }
587 }
588
590 void post (X& x) override
591 {
592 // all the implemented Preconditioners do nothing in pre and post
593 // apply preconditioner
594 if( preEx_ > 1 )
595 {
596 preconder_->post(x);
597 }
598 }
599
601 SolverCategory::Category category () const override
602 {
603 return (preconder_ ? preconder_->category() : SolverCategory::sequential);
604 }
605
606 protected:
607 template <class Smoother>
608 PreconditionerInterfaceType*
609 createAMGPreconditioner(const CommunicationType& comm,
610 int iter, field_type relax, const Smoother* )
611 {
612 typedef typename Dune::FieldTraits< field_type>::real_type real_type;
617
618 typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs;
619
620 SmootherArgs smootherArgs;
621
622 smootherArgs.iterations = iter;
623 smootherArgs.relaxationFactor = relax ;
624
625 int coarsenTarget=1200;
626 Criterion criterion(15,coarsenTarget);
627 criterion.setDefaultValuesIsotropic(2);
628 criterion.setAlpha(.67);
629 criterion.setBeta(1.0e-8);
630 criterion.setMaxLevel(10);
631 if( verbose_ && Parameter :: verbose() )
632 criterion.setDebugLevel( 1 );
633 else
634 criterion.setDebugLevel( 0 );
635
636 /*
637 if( comm.size() > 1 )
638 {
639 typedef Dune::OwnerOverlapCopyCommunication<int> ParallelInformation;
640 ParallelInformation pinfo(MPI_COMM_WORLD);
641 typedef Dune::Amg::AMG<OperatorType, X, Smoother, ParallelInformation> AMG;
642 return new AMG(*op_, criterion, smootherArgs, pinfo);
643 }
644 else
645 */
646 {
647 // X == Y is needed for AMG
649 return new AMG(*op_, criterion, smootherArgs);
650 //return new AMG(*op_, criterion, smootherArgs, 1, 1, 1, false);
651 }
652 }
653 };
654
655
656 // ISTLPreconditionerFactory
657 // -------------------------
658
659 template < class MatrixObject >
660 class ISTLMatrixAdapterFactory ;
661
662 template < class DomainSpace, class RangeSpace, class DomainBlock, class RangeBlock,
663 template <class, class, class, class> class MatrixObject >
664 class ISTLMatrixAdapterFactory< MatrixObject< DomainSpace, RangeSpace, DomainBlock, RangeBlock > >
665 {
666 typedef DomainSpace DomainSpaceType ;
667 typedef RangeSpace RangeSpaceType;
668
669 public:
670 typedef MatrixObject< DomainSpaceType, RangeSpaceType, DomainBlock, RangeBlock > MatrixObjectType;
671 typedef typename MatrixObjectType :: MatrixType MatrixType;
672 typedef ISTLParallelMatrixAdapterInterface< MatrixType > MatrixAdapterInterfaceType;
673
676 matrixAdapter( const MatrixObjectType& matrixObj,
677 const ISTLSolverParameter& param)
678 {
680 if( matrixObj.domainSpace().continuous() )
681 {
682 typedef LagrangeParallelMatrixAdapter< MatrixType > MatrixAdapterImplementation;
683 ptr.reset( matrixAdapterObject( matrixObj, (MatrixAdapterImplementation *) nullptr, param ) );
684 }
685 else
686 {
687 typedef DGParallelMatrixAdapter< MatrixType > MatrixAdapterImplementation;
688 ptr.reset( matrixAdapterObject( matrixObj, (MatrixAdapterImplementation *) nullptr, param ) );
689 }
690 return ptr;
691 }
692
693 protected:
694 template <class MatrixAdapterType>
695 static MatrixAdapterType*
696 matrixAdapterObject( const MatrixObjectType& matrixObj,
697 const MatrixAdapterType*,
698 const ISTLSolverParameter& param )
699 {
700 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
701 return new MatrixAdapterType( matrixObj.exportMatrix(),
702 matrixObj.domainSpace(), matrixObj.rangeSpace(), PreConType(param.verbose()),
703 matrixObj.threading() );
704 }
705
706 };
707
708#ifndef DISABLE_ISTL_PRECONDITIONING
710 template < class Space, class DomainBlock, class RangeBlock,
711 template <class, class, class, class> class MatrixObject >
712 class ISTLMatrixAdapterFactory< MatrixObject< Space, Space, DomainBlock, RangeBlock > >
713 {
714 public:
715 typedef Space DomainSpaceType ;
716 typedef Space RangeSpaceType;
717
718 typedef MatrixObject< DomainSpaceType, RangeSpaceType, DomainBlock, RangeBlock > MatrixObjectType;
719 typedef typename MatrixObjectType :: MatrixType MatrixType;
720 typedef typename MatrixType :: BaseType ISTLMatrixType ;
721 typedef Fem::PreconditionerWrapper< MatrixType > PreconditionAdapterType;
722
723 protected:
724 template <class MatrixAdapterType, class PreconditionerType>
725 static MatrixAdapterType*
726 createMatrixAdapter(const MatrixAdapterType*,
727 const PreconditionerType* preconditioning,
728 MatrixType& matrix,
729 const DomainSpaceType& domainSpace,
730 const RangeSpaceType& rangeSpace,
731 const double relaxFactor,
732 std::size_t numIterations,
733 bool verbose)
734 {
735 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
736 PreConType preconAdapter(matrix, numIterations, relaxFactor, verbose, preconditioning );
737 return new MatrixAdapterType(matrix, domainSpace, rangeSpace, preconAdapter );
738 }
739
740 template <class MatrixAdapterType, class PreconditionerType>
741 static MatrixAdapterType*
742 createAMGMatrixAdapter(const MatrixAdapterType*,
743 const PreconditionerType* preconditioning,
744 MatrixType& matrix,
745 const DomainSpaceType& domainSpace,
746 const RangeSpaceType& rangeSpace,
747 const double relaxFactor,
748 std::size_t numIterations,
749 bool solverVerbose)
750 {
751 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
752 PreConType preconAdapter(matrix, numIterations, relaxFactor, solverVerbose,
753 preconditioning, domainSpace.gridPart().comm() );
754 return new MatrixAdapterType(matrix, domainSpace, rangeSpace, preconAdapter );
755 }
756
757 template <class MatrixAdapterType>
758 static MatrixAdapterType*
759 matrixAdapterObject( const MatrixObjectType& matrixObj,
760 const MatrixAdapterType*,
761 const ISTLSolverParameter& param )
762 {
763 int preconditioning = param.preconditionMethod(
764 ISTLPreconditionMethods::supportedPreconditionMethods() );
765 const double relaxFactor = param.relaxation();
766 const size_t numIterations = param.preconditionerIteration();
767
768 const DomainSpaceType& domainSpace = matrixObj.domainSpace();
769 const RangeSpaceType& rangeSpace = matrixObj.rangeSpace();
770
771 MatrixType& matrix = matrixObj.exportMatrix();
772 const auto procs = domainSpace.gridPart().comm().size();
773
774 const bool verbose = param.verbose();// && Parameter::verbose();
775
776 typedef typename MatrixType :: BaseType ISTLMatrixType;
777 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
778
779 typedef typename MatrixObjectType :: RowBlockVectorType RowBlockVectorType;
780 typedef typename MatrixObjectType :: ColumnBlockVectorType ColumnBlockVectorType;
781
782 // no preconditioner
783 if( preconditioning == SolverParameter::none )
784 {
785 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, PreConType(param.verbose()) );
786 }
787 // SSOR
788 else if( preconditioning == SolverParameter::ssor )
789 {
790 typedef ParallelSSOR<MatrixObjectType, RowBlockVectorType,ColumnBlockVectorType> PreconditionerType;
791 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
792 PreConType preconAdapter( matrix, verbose, new PreconditionerType( matrixObj, numIterations, relaxFactor, verbose ) );
793 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, preconAdapter );
794 }
795 // SOR and Gauss-Seidel
796 else if(preconditioning == SolverParameter::sor || preconditioning == SolverParameter::gauss_seidel)
797 {
798 typedef ParallelSOR<MatrixObjectType, RowBlockVectorType,ColumnBlockVectorType> PreconditionerType;
799 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
800 PreConType preconAdapter( matrix, verbose, new PreconditionerType( matrixObj, numIterations,
801 (preconditioning == SolverParameter::gauss_seidel) ? 1.0 : relaxFactor, verbose ) );
802 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, preconAdapter );
803 }
804 // ILU
805 else if(preconditioning == SolverParameter::ilu)
806 {
807 if( procs > 1 )
808 DUNE_THROW(InvalidStateException,"ISTL::SeqILU not working in parallel computations");
809
810 typedef SeqILU<ISTLMatrixType,RowBlockVectorType,ColumnBlockVectorType> PreconditionerType;
811 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
812 // might need to set verbosity here?
813 PreConType preconAdapter( matrix, verbose, new PreconditionerType( matrix, numIterations, relaxFactor, param.fastILUStorage()) );
814 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, preconAdapter );
815 }
816 // Jacobi
817 else if(preconditioning == SolverParameter::jacobi)
818 {
819 typedef ParallelJacobi<MatrixObjectType, RowBlockVectorType,ColumnBlockVectorType> PreconditionerType;
820 typedef typename MatrixAdapterType :: PreconditionAdapterType PreConType;
821 PreConType preconAdapter( matrix, verbose, new PreconditionerType( matrixObj, numIterations, relaxFactor, verbose ) );
822 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, preconAdapter );
823 }
824 // AMG ILU-0
825 else if(preconditioning == SolverParameter::amg_ilu)
826 {
827 // use original SeqILU because of some AMG traits classes.
828 typedef SeqILU<ISTLMatrixType,RowBlockVectorType,ColumnBlockVectorType> PreconditionerType;
829 return createAMGMatrixAdapter( (MatrixAdapterType *)nullptr,
830 (PreconditionerType*)nullptr,
831 matrix, domainSpace, rangeSpace, relaxFactor, numIterations,
832 verbose);
833 }
834 // AMG Jacobi
835 else if(preconditioning == SolverParameter::amg_jacobi)
836 {
837 typedef SeqJac<ISTLMatrixType,RowBlockVectorType,ColumnBlockVectorType,1> PreconditionerType;
838 return createAMGMatrixAdapter( (MatrixAdapterType *)nullptr,
839 (PreconditionerType*)nullptr,
840 matrix, domainSpace, rangeSpace, relaxFactor, numIterations,
841 verbose);
842 }
843#ifndef USE_IBCRS_MATRIX
844 // ILDL
845 else if(preconditioning == SolverParameter::ildl)
846 {
847 if( procs > 1 )
848 DUNE_THROW(InvalidStateException,"ISTL::SeqILDL not working in parallel computations");
849
850 PreConType preconAdapter( matrix, verbose, new SeqILDL<ISTLMatrixType,RowBlockVectorType,ColumnBlockVectorType>( matrix , relaxFactor ) );
851 return new MatrixAdapterType( matrix, domainSpace, rangeSpace, preconAdapter );
852 }
853#endif
854 else
855 {
856 preConErrorMsg(preconditioning);
857 }
858 return new MatrixAdapterType(matrix, domainSpace, rangeSpace, PreConType(verbose) );
859 }
860
861 typedef ISTLParallelMatrixAdapterInterface< MatrixType > MatrixAdapterInterfaceType;
862
863 static void preConErrorMsg(int preCon)
864 {
865 // TODO: re-write
866 std::cerr << "ERROR: Wrong precoditioning number (p = " << preCon;
867 std::cerr <<") in ISTLMatrixObject! \n";
868 std::cerr <<"Valid values are: \n";
869 std::cerr <<"0 == no \n";
870 std::cerr <<"1 == SSOR \n";
871 std::cerr <<"2 == SOR \n";
872 std::cerr <<"3 == ILU-0 \n";
873 std::cerr <<"4 == ILU-n \n";
874 std::cerr <<"5 == Gauss-Seidel \n";
875 std::cerr <<"6 == Jacobi \n";
876
877 assert( false );
878 DUNE_THROW(NotImplemented,"Wrong precoditioning selected");
879 }
880
881 public:
884 matrixAdapter( const MatrixObjectType& matrixObj,
885 const ISTLSolverParameter& param)
886 {
887 const ISTLSolverParameter* parameter = dynamic_cast< const ISTLSolverParameter* > (&param);
889 if( ! parameter )
890 {
891 paramPtr.reset( new ISTLSolverParameter( param ) );
892 parameter = paramPtr.operator->();
893 }
894
896 if( matrixObj.domainSpace().continuous() )
897 {
898 typedef LagrangeParallelMatrixAdapter< MatrixType > MatrixAdapterImplementation;
899 ptr.reset( matrixAdapterObject( matrixObj, (MatrixAdapterImplementation *) nullptr, *parameter ) );
900 }
901 else
902 {
903 typedef DGParallelMatrixAdapter< MatrixType > MatrixAdapterImplementation;
904 ptr.reset( matrixAdapterObject( matrixObj, (MatrixAdapterImplementation *) nullptr, *parameter ) );
905 }
906 return ptr;
907 }
908
909 }; // end specialization of ISTLMatrixAdapterFactor for domainSpace == rangeSpace
910#endif
911
912#endif // end HAVE_DUNE_ISTL
913
914 } // namespace Fem
915
916} // namespace Dune
917
918#endif // #ifndef DUNE_FEM_PRECONDITIONERWRAPPER_HH
SolverCategory::Category category() const override
const char * name()
void pre(Domain &x, Range &b)
AMG(OperatorHierarchy &matrices, CoarseSolver &coarseSolver, const SmootherArgs &smootherArgs, const Parameters &parms)
int coarsenTarget() const
OperatorHierarchy::ParallelMatrixHierarchy::ConstIterator matrix
void post(Domain &x)
Y & rhs()
iterator end()
static Type apply(T &t)
static constexpr size_type M()
#define DUNE_THROW(E,...)
const Communication & comm() const
Dune::Fem::Double abs(const Dune::Fem::Double &a)
Definition double.hh:942
static void check(const Matrix &mat)
static ParameterContainer & container()
Definition io/parameter.hh:199
Definition io/parameter.hh:576
T getValue(const std::string &key) const
get mandatory parameter
Definition reader.hh:161
Definition istlpreconditioner.hh:32
static std::vector< int > supportedPreconditionMethods()
Definition istlpreconditioner.hh:33
Definition istlpreconditioner.hh:49
Definition istlpreconditioner.hh:55
Definition istlpreconditioner.hh:58
virtual bool fastILUStorage() const
Definition istlpreconditioner.hh:81
LocalParameter< SolverParameter, ISTLSolverParameter > BaseType
Definition istlpreconditioner.hh:59
ISTLSolverParameter(const std::string &keyPrefix, const ParameterReader &parameter=Parameter::container())
Definition istlpreconditioner.hh:68
virtual double overflowFraction() const
Definition istlpreconditioner.hh:76
ISTLSolverParameter(const ParameterReader &parameter=Parameter::container())
Definition istlpreconditioner.hh:64
ISTLSolverParameter(const SolverParameter &sp)
Definition istlpreconditioner.hh:72
Definition solver/parameter.hh:25
const ParameterReader & parameter() const
Definition solver/parameter.hh:81
const std::string & keyPrefix() const
Definition solver/parameter.hh:79
T copy(T... args)
T endl(T... args)
T epsilon(T... args)
T reset(T... args)