Debian Codegraph @

dune/istl/paamg/amg.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_AMG_AMG_HH
6 #define DUNE_AMG_AMG_HH
7
8 #include <memory>
9 #include <sstream>
10 #include <dune/common/exceptions.hh>
11 #include <dune/istl/paamg/smoother.hh>
12 #include <dune/istl/paamg/transfer.hh>
13 #include <dune/istl/paamg/matrixhierarchy.hh>
14 #include <dune/istl/solvers.hh>
15 #include <dune/istl/scalarproducts.hh>
16 #include <dune/istl/superlu.hh>
17 #include <dune/istl/umfpack.hh>
18 #include <dune/istl/solvertype.hh>
19 #include <dune/istl/solverregistry.hh>
20 #include <dune/common/typetraits.hh>
21 #include <dune/common/exceptions.hh>
22 #include <dune/common/scalarvectorview.hh>
23 #include <dune/common/scalarmatrixview.hh>
24 #include <dune/common/parametertree.hh>
25
26 namespace Dune
27 {
28 namespace Amg
29 {
30 /**
31 * @defgroup ISTL_PAAMG Parallel Algebraic Multigrid
32 * @ingroup ISTL_Prec
33 * @brief A Parallel Algebraic Multigrid based on Agglomeration.
34 */
35
36 /**
37 * @addtogroup ISTL_PAAMG
38 *
39 * @{
40 */
41
42 /** @file
43 * @author Markus Blatt
44 * @brief The AMG preconditioner.
45 */
46
47 template<class M, class X, class S, class P, class K, class A>
48 class KAMG;
49
50 template<class T>
51 class KAmgTwoGrid;
52
53 /**
54 * @brief Parallel algebraic multigrid based on agglomeration.
55 *
56 * \tparam M The LinearOperator type which represents the matrix
57 * \tparam X The vector type
58 * \tparam S The smoother type
59 * \tparam A An allocator for X
60 *
61 * \todo drop the smoother template parameter and replace with dynamic construction
62 */
63 template<class M, class X, class S, class PI=SequentialInformation,
64 class A=std::allocator<X> >
65 class AMG : public Preconditioner<X,X>
66 {
67 template<class M1, class X1, class S1, class P1, class K1, class A1>
68 friend class KAMG;
69
70 friend class KAmgTwoGrid<AMG>;
71
72 public:
73 /** @brief The matrix operator type. */
74 typedef M Operator;
75 /**
76 * @brief The type of the parallel information.
77 * Either OwnerOverlapCommunication or another type
78 * describing the parallel data distribution and
79 * providing communication methods.
80 */
81 typedef PI ParallelInformation;
82 /** @brief The operator hierarchy type. */
83 typedef MatrixHierarchy<M, ParallelInformation, A> OperatorHierarchy;
84 /** @brief The parallal data distribution hierarchy type. */
85 typedef typename OperatorHierarchy::ParallelInformationHierarchy ParallelInformationHierarchy;
86
87 /** @brief The domain type. */
88 typedef X Domain;
89 /** @brief The range type. */
90 typedef X Range;
91 /** @brief the type of the coarse solver. */
92 typedef InverseOperator<X,X> CoarseSolver;
93 /**
94 * @brief The type of the smoother.
95 *
96 * One of the preconditioners implementing the Preconditioner interface.
97 * Note that the smoother has to fit the ParallelInformation.*/
98 typedef S Smoother;
99
100 /** @brief The argument type for the construction of the smoother. */
101 typedef typename SmootherTraits<Smoother>::Arguments SmootherArgs;
102
103 /**
104 * @brief Construct a new amg with a specific coarse solver.
105 * @param matrices The already set-up matrix hierarchy.
106 * @param coarseSolver The set up solver to use on the coarse
107 * grid, must match the coarse matrix in the matrix hierarchy.
108 * @param smootherArgs The arguments needed for thesmoother to use
109 * for pre and post smoothing.
110 * @param parms The parameters for the AMG.
111 */
112 AMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver,
113 const SmootherArgs& smootherArgs, const Parameters& parms);
114
115 /**
116 * @brief Construct an AMG with an inexact coarse solver based on the smoother.
117 *
118 * As coarse solver a preconditioned CG method with the smoother as preconditioner
119 * will be used. The matrix hierarchy is built automatically.
120 * @param fineOperator The operator on the fine level.
121 * @param criterion The criterion describing the coarsening strategy. E. g. SymmetricCriterion
122 * or UnsymmetricCriterion, and providing the parameters.
123 * @param smootherArgs The arguments for constructing the smoothers.
124 * @param pinfo The information about the parallel distribution of the data.
125 */
126 template<class C>
127 AMG(const Operator& fineOperator, const C& criterion,
128 const SmootherArgs& smootherArgs=SmootherArgs(),
129 const ParallelInformation& pinfo=ParallelInformation());
130
131 /*!
132 \brief Constructor an AMG via ParameterTree.
133
134 \param fineOperator The operator on the fine level.
135 \param configuration ParameterTree containing AMG parameters.
136 \param pinfo Optionally, specify ParallelInformation
137
138 ParameterTree Key | Meaning
139 --------------------------|------------
140 smootherIterations | The number of iterations to perform.
141 smootherRelaxation | The relaxation factor
142 maxLevel | Maximum number of levels allowed in the hierarchy.
143 coarsenTarget | Maximum number of unknowns on the coarsest level.
144 minCoarseningRate | Coarsening will stop if the rate is below this threshold.
145 accumulationMode | If and how data is agglomerated on coarser level to
146 | fewer processors. ("atOnce": do agglomeration once and
147 | to one process; "successive": Multiple agglomerations to
148 | fewer processes until all data is on one process;
149 | "none": Do no agglomeration at all and solve coarse level
150 | iteratively).
151 prolongationDampingFactor | Damping factor for the prolongation.
152 alpha | Scaling value for marking connections as strong.
153 beta | Threshold for marking nodes as isolated.
154 additive | Whether to use additive multigrid.
155 gamma | 1 for V-cycle, 2 for W-cycle.
156 preSteps | Number of presmoothing steps.
157 postSteps | Number of postsmoothing steps.
158 verbosity | Output verbosity. default=2.
159 criterionSymmetric | If true use SymmetricCriterion (default), else UnSymmetricCriterion
160 strengthMeasure | What conversion to use to convert a matrix block
161 | to a scalar when determining strength of connection:
162 | diagonal (use a diagonal of row diagonalRowIndex, class Diagonal, default);
163 | rowSum (rowSum norm), frobenius (Frobenius norm);
164 | one (use always one and neglect the actual entries).
165 diagonalRowIndex | The index to use for the diagonal strength (default 0)
166 | if this is i and strengthMeasure is "diagonal", then
167 | block[i][i] will be used when determining strength of
168 | connection.
169 defaultAggregationSizeMode | Whether to set default values depending on isotropy of
170 | problem uses parameters "defaultAggregationDimension" and
171 | "maxAggregateDistance" (isotropic: For and isotropic problem;
172 | anisotropic: for an anisotropic problem).
173 defaultAggregationDimension | Dimension of the problem (used for setting default aggregate size).
174 maxAggregateDistance | Maximum distance in an aggregte (in term of minimum edges needed to travel.
175 | one vertex to another within the aggregate).
176 minAggregateSize | Minimum number of vertices an aggregate should consist of.
177 maxAggregateSize | Maximum number of vertices an aggregate should consist of.
178
179 See \ref ISTL_Factory for the ParameterTree layout and examples.
180 */
181 AMG(std::shared_ptr<const Operator> fineOperator, const ParameterTree& configuration, const ParallelInformation& pinfo=ParallelInformation());
182
183 /**
184 * @brief Copy constructor.
185 */
186 AMG(const AMG& amg);
187
188 /** \copydoc Preconditioner::pre */
189 void pre(Domain& x, Range& b);
190
191 /** \copydoc Preconditioner::apply */
192 void apply(Domain& v, const Range& d);
193
194 //! Category of the preconditioner (see SolverCategory::Category)
195 virtual SolverCategory::Category category() const
196 {
197 return category_;
198 }
199
200 /** \copydoc Preconditioner::post */
201 void post(Domain& x);
202
203 /**
204 * @brief Get the aggregate number of each unknown on the coarsest level.
205 * @param cont The random access container to store the numbers in.
206 */
207 template<class A1>
208 void getCoarsestAggregateNumbers(std::vector<std::size_t,A1>& cont);
209
210 std::size_t levels();
211
212 std::size_t maxlevels();
213
214 /**
215 * @brief Recalculate the matrix hierarchy.
216 *
217 * It is assumed that the coarsening for the changed fine level
218 * matrix would yield the same aggregates. In this case it suffices
219 * to recalculate all the Galerkin products for the matrices of the
220 * coarser levels.
221 */
222 void recalculateHierarchy()
223 {
224 matrices_->recalculateGalerkin(NegateSet<typename PI::OwnerSet>());
225 }
226
227 /**
228 * @brief Check whether the coarse solver used is a direct solver.
229 * @return True if the coarse level solver is a direct solver.
230 */
231 bool usesDirectCoarseLevelSolver() const;
232
233 private:
234 /*
235 * @brief Helper function to create hierarchies with parameter tree.
236 *
237 * Will create the coarsen criterion with the norm and create the
238 * Hierarchies
239 * \tparam Norm Type of the norm to use.
240 */
241 template<class Norm>
242 void createCriterionAndHierarchies(std::shared_ptr<const Operator> matrixptr,
243 const PI& pinfo, const Norm&,
244 const ParameterTree& configuration,
245 std::true_type compiles = std::true_type());
246 template<class Norm>
247 void createCriterionAndHierarchies(std::shared_ptr<const Operator> matrixptr,
248 const PI& pinfo, const Norm&,
249 const ParameterTree& configuration,
250 std::false_type);
251 /**
252 * @brief Helper function to create hierarchies with settings from parameter tree.
253 * @param criterion Coarsen criterion to configure and use
254 */
255 template<class C>
256 void createHierarchies(C& criterion, std::shared_ptr<const Operator> matrixptr,
257 const PI& pinfo, const ParameterTree& configuration);
258 /**
259 * @brief Create matrix and smoother hierarchies.
260 * @param criterion The coarsening criterion.
261 * @param matrix The fine level matrix operator.
262 * @param pinfo The fine level parallel information.
263 */
264 template<class C>
265 void createHierarchies(C& criterion,
266 const std::shared_ptr<const Operator>& matrixptr,
267 const PI& pinfo);
268 /**
269 * @brief A struct that holds the context of the current level.
270 *
271 * These are the iterators to the smoother, matrix, parallel information,
272 * and so on needed for the computations on the current level.
273 */
274 struct LevelContext
275 {
276 typedef Smoother SmootherType;
277 /**
278 * @brief The iterator over the smoothers.
279 */
280 typename Hierarchy<Smoother,A>::Iterator smoother;
281 /**
282 * @brief The iterator over the matrices.
283 */
284 typename OperatorHierarchy::ParallelMatrixHierarchy::ConstIterator matrix;
285 /**
286 * @brief The iterator over the parallel information.
287 */
288 typename ParallelInformationHierarchy::Iterator pinfo;
289 /**
290 * @brief The iterator over the redistribution information.
291 */
292 typename OperatorHierarchy::RedistributeInfoList::const_iterator redist;
293 /**
294 * @brief The iterator over the aggregates maps.
295 */
296 typename OperatorHierarchy::AggregatesMapList::const_iterator aggregates;
297 /**
298 * @brief The iterator over the left hand side.
299 */
300 typename Hierarchy<Domain,A>::Iterator lhs;
301 /**
302 * @brief The iterator over the updates.
303 */
304 typename Hierarchy<Domain,A>::Iterator update;
305 /**
306 * @brief The iterator over the right hand sided.
307 */
308 typename Hierarchy<Range,A>::Iterator rhs;
309 /**
310 * @brief The level index.
311 */
312 std::size_t level;
313 };
314
315
316 /**
317 * @brief Multigrid cycle on a level.
318 * @param levelContext the iterators of the current level.
319 */
320 void mgc(LevelContext& levelContext);
321
322 void additiveMgc();
323
324 /**
325 * @brief Move the iterators to the finer level
326 * @param levelContext the iterators of the current level.
327 * @param processedFineLevel Whether the process computed on
328 * fine level or not.
329 */
330 void moveToFineLevel(LevelContext& levelContext,bool processedFineLevel);
331
332 /**
333 * @brief Move the iterators to the coarser level.
334 * @param levelContext the iterators of the current level
335 */
336 bool moveToCoarseLevel(LevelContext& levelContext);
337
338 /**
339 * @brief Initialize iterators over levels with fine level.
340 * @param levelContext the iterators of the current level
341 */
342 void initIteratorsWithFineLevel(LevelContext& levelContext);
343
344 /** @brief The matrix we solve. */
345 std::shared_ptr<OperatorHierarchy> matrices_;
346 /** @brief The arguments to construct the smoother */
347 SmootherArgs smootherArgs_;
348 /** @brief The hierarchy of the smoothers. */
349 std::shared_ptr<Hierarchy<Smoother,A> > smoothers_;
350 /** @brief The solver of the coarsest level. */
351 std::shared_ptr<CoarseSolver> solver_;
352 /** @brief The right hand side of our problem. */
353 std::shared_ptr<Hierarchy<Range,A>> rhs_;
354 /** @brief The left approximate solution of our problem. */
355 std::shared_ptr<Hierarchy<Domain,A>> lhs_;
356 /** @brief The total update for the outer solver. */
357 std::shared_ptr<Hierarchy<Domain,A>> update_;
358 /** @brief The type of the scalar product for the coarse solver. */
359 using ScalarProduct = Dune::ScalarProduct<X>;
360 /** @brief Scalar product on the coarse level. */
361 std::shared_ptr<ScalarProduct> scalarProduct_;
362 /** @brief Gamma, 1 for V-cycle and 2 for W-cycle. */
363 std::size_t gamma_;
364 /** @brief The number of pre and postsmoothing steps. */
365 std::size_t preSteps_;
366 /** @brief The number of postsmoothing steps. */
367 std::size_t postSteps_;
368 bool buildHierarchy_;
369 bool additive;
370 bool coarsesolverconverged;
371 std::shared_ptr<Smoother> coarseSmoother_;
372 /** @brief The solver category. */
373 SolverCategory::Category category_;
374 /** @brief The verbosity level. */
375 std::size_t verbosity_;
376
377 struct ToLower
378 {
379 std::string operator()(const std::string& str)
380 {
381 std::stringstream retval;
382 std::ostream_iterator<char> out(retval);
383 std::transform(str.begin(), str.end(), out,
384 [](char c){
385 return std::tolower(c, std::locale::classic());
386 });
387 return retval.str();
388 }
389 };
390 };
391
392 template<class M, class X, class S, class PI, class A>
393 inline AMG<M,X,S,PI,A>::AMG(const AMG& amg)
394 : matrices_(amg.matrices_), smootherArgs_(amg.smootherArgs_),
395 smoothers_(amg.smoothers_), solver_(amg.solver_),
396 rhs_(), lhs_(), update_(),
397 scalarProduct_(amg.scalarProduct_), gamma_(amg.gamma_),
398 preSteps_(amg.preSteps_), postSteps_(amg.postSteps_),
399 buildHierarchy_(amg.buildHierarchy_),
400 additive(amg.additive), coarsesolverconverged(amg.coarsesolverconverged),
401 coarseSmoother_(amg.coarseSmoother_),
402 category_(amg.category_),
403 verbosity_(amg.verbosity_)
404 {}
405
406 template<class M, class X, class S, class PI, class A>
407 AMG<M,X,S,PI,A>::AMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver,
408 const SmootherArgs& smootherArgs,
409 const Parameters& parms)
410 : matrices_(stackobject_to_shared_ptr(matrices)), smootherArgs_(smootherArgs),
411 smoothers_(new Hierarchy<Smoother,A>), solver_(&coarseSolver),
412 rhs_(), lhs_(), update_(), scalarProduct_(0),
413 gamma_(parms.getGamma()), preSteps_(parms.getNoPreSmoothSteps()),
414 postSteps_(parms.getNoPostSmoothSteps()), buildHierarchy_(false),
415 additive(parms.getAdditive()), coarsesolverconverged(true),
416 coarseSmoother_(),
417 // #warning should category be retrieved from matrices?
418 category_(SolverCategory::category(*smoothers_->coarsest())),
419 verbosity_(parms.debugLevel())
420 {
421 assert(matrices_->isBuilt());
422
423 // build the necessary smoother hierarchies
424 matrices_->coarsenSmoother(*smoothers_, smootherArgs_);
425 }
426
427 template<class M, class X, class S, class PI, class A>
428 template<class C>
429 AMG<M,X,S,PI,A>::AMG(const Operator& matrix,
430 const C& criterion,
431 const SmootherArgs& smootherArgs,
432 const PI& pinfo)
433 : smootherArgs_(smootherArgs),
434 smoothers_(new Hierarchy<Smoother,A>), solver_(),
435 rhs_(), lhs_(), update_(), scalarProduct_(),
436 gamma_(criterion.getGamma()), preSteps_(criterion.getNoPreSmoothSteps()),
437 postSteps_(criterion.getNoPostSmoothSteps()), buildHierarchy_(true),
438 additive(criterion.getAdditive()), coarsesolverconverged(true),
439 coarseSmoother_(),
440 category_(SolverCategory::category(pinfo)),
441 verbosity_(criterion.debugLevel())
442 {
443 if(SolverCategory::category(matrix) != SolverCategory::category(pinfo))
444 DUNE_THROW(InvalidSolverCategory, "Matrix and Communication must have the same SolverCategory!");
445 // TODO: reestablish compile time checks.
446 //static_assert(static_cast<int>(PI::category)==static_cast<int>(S::category),
447 // "Matrix and Solver must match in terms of category!");
448 auto matrixptr = stackobject_to_shared_ptr(matrix);
449 createHierarchies(criterion, matrixptr, pinfo);
450 }
451
452 template<class M, class X, class S, class PI, class A>
453 AMG<M,X,S,PI,A>::AMG(std::shared_ptr<const Operator> matrixptr,
454 const ParameterTree& configuration,
455 const ParallelInformation& pinfo) :
456 smoothers_(new Hierarchy<Smoother,A>),
457 solver_(), rhs_(), lhs_(), update_(), scalarProduct_(), buildHierarchy_(true),
458 coarsesolverconverged(true), coarseSmoother_(),
459 category_(SolverCategory::category(pinfo))
460 {
461
462 if (configuration.hasKey ("smootherIterations"))
463 smootherArgs_.iterations = configuration.get<int>("smootherIterations");
464
465 if (configuration.hasKey ("smootherRelaxation"))
466 smootherArgs_.relaxationFactor = configuration.get<typename SmootherArgs::RelaxationFactor>("smootherRelaxation");
467
468 auto normName = ToLower()(configuration.get("strengthMeasure", "diagonal"));
469 auto index = configuration.get<int>("diagonalRowIndex", 0);
470
471 if ( normName == "diagonal")
472 {
473 using field_type = typename M::field_type;
474 using real_type = typename FieldTraits<field_type>::real_type;
475 std::is_convertible<field_type, real_type> compiles;
476
477 switch (index)
478 {
479 case 0:
480 createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<0>(), configuration, compiles);
481 break;
482 case 1:
483 createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<1>(), configuration, compiles);
484 break;
485 case 2:
486 createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<2>(), configuration, compiles);
487 break;
488 case 3:
489 createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<3>(), configuration, compiles);
490 break;
491 case 4:
492 createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<4>(), configuration, compiles);
493 break;
494 default:
495 DUNE_THROW(InvalidStateException, "Currently strengthIndex>4 is not supported.");
496 }
497 }
498 else if (normName == "rowsum")
499 createCriterionAndHierarchies(matrixptr, pinfo, RowSum(), configuration);
500 else if (normName == "frobenius")
501 createCriterionAndHierarchies(matrixptr, pinfo, FrobeniusNorm(), configuration);
502 else if (normName == "one")
503 createCriterionAndHierarchies(matrixptr, pinfo, AlwaysOneNorm(), configuration);
504 else
505 DUNE_THROW(Dune::NotImplemented, "Wrong config file: strengthMeasure "<<normName<<" is not supported by AMG");
506 }
507
508 template<class M, class X, class S, class PI, class A>
509 template<class Norm>
510 void AMG<M,X,S,PI,A>::createCriterionAndHierarchies(std::shared_ptr<const Operator> matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::false_type)
511 {
512 DUNE_THROW(InvalidStateException, "Strength of connection measure does not support this type ("
513 << className<typename M::field_type>() << ") as it is lacking a conversion to"
514 << className<typename FieldTraits<typename M::field_type>::real_type>() << ".");
515 }
516
517 template<class M, class X, class S, class PI, class A>
518 template<class Norm>
519 void AMG<M,X,S,PI,A>::createCriterionAndHierarchies(std::shared_ptr<const Operator> matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::true_type)
520 {
521 if (configuration.get<bool>("criterionSymmetric", true))
522 {
523 using Criterion = Dune::Amg::CoarsenCriterion<
524 Dune::Amg::SymmetricCriterion<typename M::matrix_type,Norm> >;
525 Criterion criterion;
526 createHierarchies(criterion, matrixptr, pinfo, configuration);
527 }
528 else
529 {
530 using Criterion = Dune::Amg::CoarsenCriterion<
531 Dune::Amg::UnSymmetricCriterion<typename M::matrix_type,Norm> >;
532 Criterion criterion;
533 createHierarchies(criterion, matrixptr, pinfo, configuration);
534 }
535 }
536
537 template<class M, class X, class S, class PI, class A>
538 template<class C>
539 void AMG<M,X,S,PI,A>::createHierarchies(C& criterion, std::shared_ptr<const Operator> matrixptr, const PI& pinfo, const ParameterTree& configuration)
540 {
541 if (configuration.hasKey ("maxLevel"))
542 criterion.setMaxLevel(configuration.get<int>("maxLevel"));
543
544 if (configuration.hasKey ("minCoarseningRate"))
545 criterion.setMinCoarsenRate(configuration.get<int>("minCoarseningRate"));
546
547 if (configuration.hasKey ("coarsenTarget"))
548 criterion.setCoarsenTarget (configuration.get<int>("coarsenTarget"));
549
550 if (configuration.hasKey ("accumulationMode"))
551 {
552 std::string mode = ToLower()(configuration.get<std::string>("accumulationMode"));
553 if ( mode == "none")
554 criterion.setAccumulate(AccumulationMode::noAccu);
555 else if ( mode == "atonce" )
556 criterion.setAccumulate(AccumulationMode::atOnceAccu);
557 else if ( mode == "successive")
558 criterion.setCoarsenTarget (AccumulationMode::successiveAccu);
559 else
560 DUNE_THROW(InvalidSolverFactoryConfiguration, "Parameter accumulationMode does not allow value "
561 << mode <<".");
562 }
563
564 if (configuration.hasKey ("prolongationDampingFactor"))
565 criterion.setProlongationDampingFactor (configuration.get<double>("prolongationDampingFactor"));
566
567 if (configuration.hasKey("defaultAggregationSizeMode"))
568 {
569 auto mode = ToLower()(configuration.get<std::string>("defaultAggregationSizeMode"));
570 auto dim = configuration.get<std::size_t>("defaultAggregationDimension");
571 std::size_t maxDistance = 2;
572 if (configuration.hasKey("MaxAggregateDistance"))
573 maxDistance = configuration.get<std::size_t>("maxAggregateDistance");
574 if (mode == "isotropic")
575 criterion.setDefaultValuesIsotropic(dim, maxDistance);
576 else if(mode == "anisotropic")
577 criterion.setDefaultValuesAnisotropic(dim, maxDistance);
578 else
579 DUNE_THROW(InvalidSolverFactoryConfiguration, "Parameter accumulationMode does not allow value "
580 << mode <<".");
581 }
582
583 if (configuration.hasKey("maxAggregateDistance"))
584 criterion.setMaxDistance(configuration.get<std::size_t>("maxAggregateDistance"));
585
586 if (configuration.hasKey("minAggregateSize"))
587 criterion.setMinAggregateSize(configuration.get<std::size_t>("minAggregateSize"));
588
589 if (configuration.hasKey("maxAggregateSize"))
590 criterion.setMaxAggregateSize(configuration.get<std::size_t>("maxAggregateSize"));
591
592 if (configuration.hasKey("maxAggregateConnectivity"))
593 criterion.setMaxConnectivity(configuration.get<std::size_t>("maxAggregateConnectivity"));
594
595 if (configuration.hasKey ("alpha"))
596 criterion.setAlpha (configuration.get<double> ("alpha"));
597
598 if (configuration.hasKey ("beta"))
599 criterion.setBeta (configuration.get<double> ("beta"));
600
601 if (configuration.hasKey ("gamma"))
602 criterion.setGamma (configuration.get<std::size_t> ("gamma"));
603 gamma_ = criterion.getGamma();
604
605 if (configuration.hasKey ("additive"))
606 criterion.setAdditive (configuration.get<bool>("additive"));
607 additive = criterion.getAdditive();
608
609 if (configuration.hasKey ("preSteps"))
610 criterion.setNoPreSmoothSteps (configuration.get<std::size_t> ("preSteps"));
611 preSteps_ = criterion.getNoPreSmoothSteps ();
612
613 if (configuration.hasKey ("postSteps"))
614 criterion.setNoPostSmoothSteps (configuration.get<std::size_t> ("postSteps"));
615 postSteps_ = criterion.getNoPostSmoothSteps ();
616
617 verbosity_ = configuration.get("verbosity", 0);
618 criterion.setDebugLevel (verbosity_);
619
620 createHierarchies(criterion, matrixptr, pinfo);
621 }
622
623 template <class Matrix,
624 class Vector>
625 struct DirectSolverSelector
626 {
627 typedef typename Matrix :: field_type field_type;
628 enum SolverType { umfpack, superlu, none };
629
630 static constexpr SolverType solver =
631 #if DISABLE_AMG_DIRECTSOLVER
632 none;
633 #elif HAVE_SUITESPARSE_UMFPACK
634 UMFPackMethodChooser< field_type > :: valid ? umfpack : none ;
635 #elif HAVE_SUPERLU
636 superlu ;
637 #else
638 none;
639 #endif
640
641 template <class M, SolverType>
642 struct Solver
643 {
644 typedef InverseOperator<Vector,Vector> type;
645 static type* create(const M& mat, bool verbose, bool reusevector )
646 {
647 DUNE_THROW(NotImplemented,"DirectSolver not selected");
648 return nullptr;
649 }
650 static std::string name () { return "None"; }
651 };
652 #if HAVE_SUITESPARSE_UMFPACK
653 template <class M>
654 struct Solver< M, umfpack >
655 {
656 typedef UMFPack< M > type;
657 static type* create(const M& mat, bool verbose, bool reusevector )
658 {
659 return new type(mat, verbose, reusevector );
660 }
661 static std::string name () { return "UMFPack"; }
662 };
663 #endif
664 #if HAVE_SUPERLU
665 template <class M>
666 struct Solver< M, superlu >
667 {
668 typedef SuperLU< M > type;
669 static type* create(const M& mat, bool verbose, bool reusevector )
670 {
671 return new type(mat, verbose, reusevector );
672 }
673 static std::string name () { return "SuperLU"; }
674 };
675 #endif
676
677 // define direct solver type to be used
678 typedef Solver< Matrix, solver > SelectedSolver ;
679 typedef typename SelectedSolver :: type DirectSolver;
680 static constexpr bool isDirectSolver = solver != none;
681 static std::string name() { return SelectedSolver :: name (); }
682 static DirectSolver* create(const Matrix& mat, bool verbose, bool reusevector )
683 {
684 return SelectedSolver :: create( mat, verbose, reusevector );
685 }
686 };
687
688 template<class M, class X, class S, class PI, class A>
689 template<class C>
690 void AMG<M,X,S,PI,A>::createHierarchies(C& criterion,
691 const std::shared_ptr<const Operator>& matrixptr,
692 const PI& pinfo)
693 {
694 Timer watch;
695 matrices_ = std::make_shared<OperatorHierarchy>(
696 std::const_pointer_cast<Operator>(matrixptr),
697 stackobject_to_shared_ptr(const_cast<PI&>(pinfo)));
698
699 matrices_->template build<NegateSet<typename PI::OwnerSet> >(criterion);
700
701 // build the necessary smoother hierarchies
702 matrices_->coarsenSmoother(*smoothers_, smootherArgs_);
703
704 // test whether we should solve on the coarse level. That is the case if we
705 // have that level and if there was a redistribution on this level then our
706 // communicator has to be valid (size()>0) as the smoother might try to communicate
707 // in the constructor.
708 if(buildHierarchy_ && matrices_->levels()==matrices_->maxlevels()
709 && ( ! matrices_->redistributeInformation().back().isSetup() ||
710 matrices_->parallelInformation().coarsest().getRedistributed().communicator().size() ) )
711 {
712 // We have the carsest level. Create the coarse Solver
713 SmootherArgs sargs(smootherArgs_);
714 sargs.iterations = 1;
715
716 typename ConstructionTraits<Smoother>::Arguments cargs;
717 cargs.setArgs(sargs);
718 if(matrices_->redistributeInformation().back().isSetup()) {
719 // Solve on the redistributed partitioning
720 cargs.setMatrix(matrices_->matrices().coarsest().getRedistributed().getmat());
721 cargs.setComm(matrices_->parallelInformation().coarsest().getRedistributed());
722 }else{
723 cargs.setMatrix(matrices_->matrices().coarsest()->getmat());
724 cargs.setComm(*matrices_->parallelInformation().coarsest());
725 }
726
727 coarseSmoother_ = ConstructionTraits<Smoother>::construct(cargs);
728 scalarProduct_ = createScalarProduct<X>(cargs.getComm(),category());
729
730 typedef DirectSolverSelector< typename M::matrix_type, X > SolverSelector;
731
732 // Use superlu if we are purely sequential or with only one processor on the coarsest level.
733 if( SolverSelector::isDirectSolver &&
734 (std::is_same<ParallelInformation,SequentialInformation>::value // sequential mode
735 || matrices_->parallelInformation().coarsest()->communicator().size()==1 //parallel mode and only one processor
736 || (matrices_->parallelInformation().coarsest().isRedistributed()
737 && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()==1
738 && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()>0) )
739 )
740 { // redistribute and 1 proc
741 if(matrices_->parallelInformation().coarsest().isRedistributed())
742 {
743 if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0)
744 {
745 // We are still participating on this level
746 solver_.reset(SolverSelector::create(matrices_->matrices().coarsest().getRedistributed().getmat(), false, false));
747 }
748 else
749 solver_.reset();
750 }
751 else
752 {
753 solver_.reset(SolverSelector::create(matrices_->matrices().coarsest()->getmat(), false, false));
754 }
755 if(verbosity_>0 && matrices_->parallelInformation().coarsest()->communicator().rank()==0)
756 std::cout<< "Using a direct coarse solver (" << SolverSelector::name() << ")" << std::endl;
757 }
758 else
759 {
760 if(matrices_->parallelInformation().coarsest().isRedistributed())
761 {
762 if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0)
763 // We are still participating on this level
764
765 // we have to allocate these types using the rebound allocator
766 // in order to ensure that we fulfill the alignment requirements
767 solver_.reset(new BiCGSTABSolver<X>(const_cast<M&>(matrices_->matrices().coarsest().getRedistributed()),
768 *scalarProduct_,
769 *coarseSmoother_, 1E-2, 1000, 0));
770 else
771 solver_.reset();
772 }else
773 {
774 solver_.reset(new BiCGSTABSolver<X>(const_cast<M&>(*matrices_->matrices().coarsest()),
775 *scalarProduct_,
776 *coarseSmoother_, 1E-2, 1000, 0));
777 // // we have to allocate these types using the rebound allocator
778 // // in order to ensure that we fulfill the alignment requirements
779 // using Alloc = typename std::allocator_traits<A>::template rebind_alloc<BiCGSTABSolver<X>>;
780 // Alloc alloc;
781 // auto p = alloc.allocate(1);
782 // std::allocator_traits<Alloc>::construct(alloc, p,
783 // const_cast<M&>(*matrices_->matrices().coarsest()),
784 // *scalarProduct_,
785 // *coarseSmoother_, 1E-2, 1000, 0);
786 // solver_.reset(p,[](BiCGSTABSolver<X>* p){
787 // Alloc alloc;
788 // std::allocator_traits<Alloc>::destroy(alloc, p);
789 // alloc.deallocate(p,1);
790 // });
791 }
792 }
793 }
794
795 if(verbosity_>0 && matrices_->parallelInformation().finest()->communicator().rank()==0)
796 std::cout<<"Building hierarchy of "<<matrices_->maxlevels()<<" levels "
797 <<"(including coarse solver) took "<<watch.elapsed()<<" seconds."<<std::endl;
798 }
799
800
801 template<class M, class X, class S, class PI, class A>
802 void AMG<M,X,S,PI,A>::pre(Domain& x, Range& b)
803 {
804 // Detect Matrix rows where all offdiagonal entries are
805 // zero and set x such that A_dd*x_d=b_d
806 // Thus users can be more careless when setting up their linear
807 // systems.
808 typedef typename M::matrix_type Matrix;
809 typedef typename Matrix::ConstRowIterator RowIter;
810 typedef typename Matrix::ConstColIterator ColIter;
811 typedef typename Matrix::block_type Block;
812 Block zero;
813 zero=typename Matrix::field_type();
814
815 const Matrix& mat=matrices_->matrices().finest()->getmat();
816 for(RowIter row=mat.begin(); row!=mat.end(); ++row) {
817 bool isDirichlet = true;
818 bool hasDiagonal = false;
819 Block diagonal{};
820 for(ColIter col=row->begin(); col!=row->end(); ++col) {
821 if(row.index()==col.index()) {
822 diagonal = *col;
823 hasDiagonal = true;
824 }else{
825 if(*col!=zero)
826 isDirichlet = false;
827 }
828 }
829 if(isDirichlet && hasDiagonal)
830 {
831 auto&& xEntry = Impl::asVector(x[row.index()]);
832 auto&& bEntry = Impl::asVector(b[row.index()]);
833 Impl::asMatrix(diagonal).solve(xEntry, bEntry);
834 }
835 }
836
837 if(smoothers_->levels()>0)
838 smoothers_->finest()->pre(x,b);
839 else
840 // No smoother to make x consistent! Do it by hand
841 matrices_->parallelInformation().coarsest()->copyOwnerToAll(x,x);
842 rhs_ = std::make_shared<Hierarchy<Range,A>>(std::make_shared<Range>(b));
843 lhs_ = std::make_shared<Hierarchy<Domain,A>>(std::make_shared<Domain>(x));
844 update_ = std::make_shared<Hierarchy<Domain,A>>(std::make_shared<Domain>(x));
845 matrices_->coarsenVector(*rhs_);
846 matrices_->coarsenVector(*lhs_);
847 matrices_->coarsenVector(*update_);
848
849 // Preprocess all smoothers
850 typedef typename Hierarchy<Smoother,A>::Iterator Iterator;
851 typedef typename Hierarchy<Range,A>::Iterator RIterator;
852 typedef typename Hierarchy<Domain,A>::Iterator DIterator;
853 Iterator coarsest = smoothers_->coarsest();
854 Iterator smoother = smoothers_->finest();
855 RIterator rhs = rhs_->finest();
856 DIterator lhs = lhs_->finest();
857 if(smoothers_->levels()>1) {
858
859 assert(lhs_->levels()==rhs_->levels());
860 assert(smoothers_->levels()==lhs_->levels() || matrices_->levels()==matrices_->maxlevels());
861 assert(smoothers_->levels()+1==lhs_->levels() || matrices_->levels()<matrices_->maxlevels());
862
863 if(smoother!=coarsest)
864 for(++smoother, ++lhs, ++rhs; smoother != coarsest; ++smoother, ++lhs, ++rhs)
865 smoother->pre(*lhs,*rhs);
866 smoother->pre(*lhs,*rhs);
867 }
868
869
870 // The preconditioner might change x and b. So we have to
871 // copy the changes to the original vectors.
872 x = *lhs_->finest();
873 b = *rhs_->finest();
874
875 }
876 template<class M, class X, class S, class PI, class A>
877 std::size_t AMG<M,X,S,PI,A>::levels()
878 {
879 return matrices_->levels();
880 }
881 template<class M, class X, class S, class PI, class A>
882 std::size_t AMG<M,X,S,PI,A>::maxlevels()
883 {
884 return matrices_->maxlevels();
885 }
886
887 /** \copydoc Preconditioner::apply */
888 template<class M, class X, class S, class PI, class A>
889 void AMG<M,X,S,PI,A>::apply(Domain& v, const Range& d)
890 {
891 LevelContext levelContext;
892
893 if(additive) {
894 *(rhs_->finest())=d;
895 additiveMgc();
896 v=*lhs_->finest();
897 }else{
898 // Init all iterators for the current level
899 initIteratorsWithFineLevel(levelContext);
900
901
902 *levelContext.lhs = v;
903 *levelContext.rhs = d;
904 *levelContext.update=0;
905 levelContext.level=0;
906
907 mgc(levelContext);
908
909 if(postSteps_==0||matrices_->maxlevels()==1)
910 levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update);
911
912 v=*levelContext.update;
913 }
914
915 }
916
917 template<class M, class X, class S, class PI, class A>
918 void AMG<M,X,S,PI,A>::initIteratorsWithFineLevel(LevelContext& levelContext)
919 {
920 levelContext.smoother = smoothers_->finest();
921 levelContext.matrix = matrices_->matrices().finest();
922 levelContext.pinfo = matrices_->parallelInformation().finest();
923 levelContext.redist =
924 matrices_->redistributeInformation().begin();
925 levelContext.aggregates = matrices_->aggregatesMaps().begin();
926 levelContext.lhs = lhs_->finest();
927 levelContext.update = update_->finest();
928 levelContext.rhs = rhs_->finest();
929 }
930
931 template<class M, class X, class S, class PI, class A>
932 bool AMG<M,X,S,PI,A>
933 ::moveToCoarseLevel(LevelContext& levelContext)
934 {
935
936 bool processNextLevel=true;
937
938 if(levelContext.redist->isSetup()) {
939 levelContext.redist->redistribute(static_cast<const Range&>(*levelContext.rhs),
940 levelContext.rhs.getRedistributed());
941 processNextLevel = levelContext.rhs.getRedistributed().size()>0;
942 if(processNextLevel) {
943 //restrict defect to coarse level right hand side.
944 typename Hierarchy<Range,A>::Iterator fineRhs = levelContext.rhs++;
945 ++levelContext.pinfo;
946 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
947 ::restrictVector(*(*levelContext.aggregates), *levelContext.rhs,
948 static_cast<const Range&>(fineRhs.getRedistributed()),
949 *levelContext.pinfo);
950 }
951 }else{
952 //restrict defect to coarse level right hand side.
953 typename Hierarchy<Range,A>::Iterator fineRhs = levelContext.rhs++;
954 ++levelContext.pinfo;
955 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
956 ::restrictVector(*(*levelContext.aggregates),
957 *levelContext.rhs, static_cast<const Range&>(*fineRhs),
958 *levelContext.pinfo);
959 }
960
961 if(processNextLevel) {
962 // prepare coarse system
963 ++levelContext.lhs;
964 ++levelContext.update;
965 ++levelContext.matrix;
966 ++levelContext.level;
967 ++levelContext.redist;
968
969 if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()<matrices_->maxlevels()) {
970 // next level is not the globally coarsest one
971 ++levelContext.smoother;
972 ++levelContext.aggregates;
973 }
974 // prepare the update on the next level
975 *levelContext.update=0;
976 }
977 return processNextLevel;
978 }
979
980 template<class M, class X, class S, class PI, class A>
981 void AMG<M,X,S,PI,A>
982 ::moveToFineLevel(LevelContext& levelContext, bool processNextLevel)
983 {
984 if(processNextLevel) {
985 if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()<matrices_->maxlevels()) {
986 // previous level is not the globally coarsest one
987 --levelContext.smoother;
988 --levelContext.aggregates;
989 }
990 --levelContext.redist;
991 --levelContext.level;
992 //prolongate and add the correction (update is in coarse left hand side)
993 --levelContext.matrix;
994
995 //typename Hierarchy<Domain,A>::Iterator coarseLhs = lhs--;
996 --levelContext.lhs;
997 --levelContext.pinfo;
998 }
999 if(levelContext.redist->isSetup()) {
1000 // Need to redistribute during prolongateVector
1001 levelContext.lhs.getRedistributed()=0;
1002 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
1003 ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs,
1004 levelContext.lhs.getRedistributed(),
1005 matrices_->getProlongationDampingFactor(),
1006 *levelContext.pinfo, *levelContext.redist);
1007 }else{
1008 *levelContext.lhs=0;
1009 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
1010 ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs,
1011 matrices_->getProlongationDampingFactor(),
1012 *levelContext.pinfo);
1013 }
1014
1015
1016 if(processNextLevel) {
1017 --levelContext.update;
1018 --levelContext.rhs;
1019 }
1020
1021 *levelContext.update += *levelContext.lhs;
1022 }
1023
1024 template<class M, class X, class S, class PI, class A>
1025 bool AMG<M,X,S,PI,A>::usesDirectCoarseLevelSolver() const
1026 {
1027 return IsDirectSolver< CoarseSolver>::value;
1028 }
1029
1030 template<class M, class X, class S, class PI, class A>
1031 void AMG<M,X,S,PI,A>::mgc(LevelContext& levelContext){
1032 if(levelContext.matrix == matrices_->matrices().coarsest() && levels()==maxlevels()) {
1033 // Solve directly
1034 InverseOperatorResult res;
1035 res.converged=true; // If we do not compute this flag will not get updated
1036 if(levelContext.redist->isSetup()) {
1037 levelContext.redist->redistribute(*levelContext.rhs, levelContext.rhs.getRedistributed());
1038 if(levelContext.rhs.getRedistributed().size()>0) {
1039 // We are still participating in the computation
1040 levelContext.pinfo.getRedistributed().copyOwnerToAll(levelContext.rhs.getRedistributed(),
1041 levelContext.rhs.getRedistributed());
1042 solver_->apply(levelContext.update.getRedistributed(),
1043 levelContext.rhs.getRedistributed(), res);
1044 }
1045 levelContext.redist->redistributeBackward(*levelContext.update, levelContext.update.getRedistributed());
1046 levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update);
1047 }else{
1048 levelContext.pinfo->copyOwnerToAll(*levelContext.rhs, *levelContext.rhs);
1049 solver_->apply(*levelContext.update, *levelContext.rhs, res);
1050 }
1051
1052 if (!res.converged)
1053 coarsesolverconverged = false;
1054 }else{
1055 // presmoothing
1056 presmooth(levelContext, preSteps_);
1057
1058 #ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION
1059 bool processNextLevel = moveToCoarseLevel(levelContext);
1060
1061 if(processNextLevel) {
1062 // next level
1063 for(std::size_t i=0; i<gamma_; i++){
1064 mgc(levelContext);
1065 if (levelContext.matrix == matrices_->matrices().coarsest() && levels()==maxlevels())
1066 break;
1067 if(i+1 < gamma_){
1068 levelContext.matrix->applyscaleadd(-1., *levelContext.lhs, *levelContext.rhs);
1069 }
1070 }
1071 }
1072
1073 moveToFineLevel(levelContext, processNextLevel);
1074 #else
1075 *lhs=0;
1076 #endif
1077
1078 if(levelContext.matrix == matrices_->matrices().finest()) {
1079 coarsesolverconverged = matrices_->parallelInformation().finest()->communicator().prod(coarsesolverconverged);
1080 if(!coarsesolverconverged)
1081 DUNE_THROW(MathError, "Coarse solver did not converge");
1082 }
1083 // postsmoothing
1084 postsmooth(levelContext, postSteps_);
1085
1086 }
1087 }
1088
1089 template<class M, class X, class S, class PI, class A>
1090 void AMG<M,X,S,PI,A>::additiveMgc(){
1091
1092 // restrict residual to all levels
1093 typename ParallelInformationHierarchy::Iterator pinfo=matrices_->parallelInformation().finest();
1094 typename Hierarchy<Range,A>::Iterator rhs=rhs_->finest();
1095 typename Hierarchy<Domain,A>::Iterator lhs = lhs_->finest();
1096 typename OperatorHierarchy::AggregatesMapList::const_iterator aggregates=matrices_->aggregatesMaps().begin();
1097
1098 for(typename Hierarchy<Range,A>::Iterator fineRhs=rhs++; fineRhs != rhs_->coarsest(); fineRhs=rhs++, ++aggregates) {
1099 ++pinfo;
1100 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
1101 ::restrictVector(*(*aggregates), *rhs, static_cast<const Range&>(*fineRhs), *pinfo);
1102 }
1103
1104 // pinfo is invalid, set to coarsest level
1105 //pinfo = matrices_->parallelInformation().coarsest
1106 // calculate correction for all levels
1107 lhs = lhs_->finest();
1108 typename Hierarchy<Smoother,A>::Iterator smoother = smoothers_->finest();
1109
1110 for(rhs=rhs_->finest(); rhs != rhs_->coarsest(); ++lhs, ++rhs, ++smoother) {
1111 // presmoothing
1112 *lhs=0;
1113 smoother->apply(*lhs, *rhs);
1114 }
1115
1116 // Coarse level solve
1117 #ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION
1118 InverseOperatorResult res;
1119 pinfo->copyOwnerToAll(*rhs, *rhs);
1120 solver_->apply(*lhs, *rhs, res);
1121
1122 if(!res.converged)
1123 DUNE_THROW(MathError, "Coarse solver did not converge");
1124 #else
1125 *lhs=0;
1126 #endif
1127 // Prologate and add up corrections from all levels
1128 --pinfo;
1129 --aggregates;
1130
1131 for(typename Hierarchy<Domain,A>::Iterator coarseLhs = lhs--; coarseLhs != lhs_->finest(); coarseLhs = lhs--, --aggregates, --pinfo) {
1132 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
1133 ::prolongateVector(*(*aggregates), *coarseLhs, *lhs, 1.0, *pinfo);
1134 }
1135 }
1136
1137
1138 /** \copydoc Preconditioner::post */
1139 template<class M, class X, class S, class PI, class A>
1140 void AMG<M,X,S,PI,A>::post([[maybe_unused]] Domain& x)
1141 {
1142 // Postprocess all smoothers
1143 typedef typename Hierarchy<Smoother,A>::Iterator Iterator;
1144 typedef typename Hierarchy<Domain,A>::Iterator DIterator;
1145 Iterator coarsest = smoothers_->coarsest();
1146 Iterator smoother = smoothers_->finest();
1147 DIterator lhs = lhs_->finest();
1148 if(smoothers_->levels()>0) {
1149 if(smoother != coarsest || matrices_->levels()<matrices_->maxlevels())
1150 smoother->post(*lhs);
1151 if(smoother!=coarsest)
1152 for(++smoother, ++lhs; smoother != coarsest; ++smoother, ++lhs)
1153 smoother->post(*lhs);
1154 smoother->post(*lhs);
1155 }
1156 lhs_ = nullptr;
1157 update_ = nullptr;
1158 rhs_ = nullptr;
1159 }
1160
1161 template<class M, class X, class S, class PI, class A>
1162 template<class A1>
1163 void AMG<M,X,S,PI,A>::getCoarsestAggregateNumbers(std::vector<std::size_t,A1>& cont)
1164 {
1165 matrices_->getCoarsestAggregatesOnFinest(cont);
1166 }
1167
1168 } // end namespace Amg
1169
1170 struct AMGCreator{
1171 template<class> struct isValidMatrix : std::false_type{};
1172 template<class T, int n, int m, class A> struct isValidMatrix<BCRSMatrix<FieldMatrix<T,n,m>, A>> : std::true_type{};
1173
1174 template<class OP>
1175 std::shared_ptr<Dune::Preconditioner<typename OP::element_type::domain_type, typename OP::element_type::range_type> >
1176 makeAMG(const OP& op, const std::string& smoother, const Dune::ParameterTree& config) const
1177 {
1178 DUNE_THROW(Dune::Exception, "Operator type not supported by AMG");
1179 }
1180
1181 template<class M, class X, class Y>
1182 std::shared_ptr<Dune::Preconditioner<X,Y> >
1183 makeAMG(const std::shared_ptr<MatrixAdapter<M,X,Y>>& op, const std::string& smoother,
1184 const Dune::ParameterTree& config) const
1185 {
1186 using OP = MatrixAdapter<M,X,Y>;
1187
1188 if(smoother == "ssor")
1189 return std::make_shared<Amg::AMG<OP, X, SeqSSOR<M,X,Y>>>(op, config);
1190 if(smoother == "sor")
1191 return std::make_shared<Amg::AMG<OP, X, SeqSOR<M,X,Y>>>(op, config);
1192 if(smoother == "jac")
1193 return std::make_shared<Amg::AMG<OP, X, SeqJac<M,X,Y>>>(op, config);
1194 if(smoother == "gs")
1195 return std::make_shared<Amg::AMG<OP, X, SeqGS<M,X,Y>>>(op, config);
1196 if(smoother == "ilu")
1197 return std::make_shared<Amg::AMG<OP, X, SeqILU<M,X,Y>>>(op, config);
1198 else
1199 DUNE_THROW(Dune::Exception, "Unknown smoother for AMG");
1200 }
1201
1202 template<class M, class X, class Y, class C>
1203 std::shared_ptr<Dune::Preconditioner<X,Y> >
1204 makeAMG(const std::shared_ptr<OverlappingSchwarzOperator<M,X,Y,C>>& op, const std::string& smoother,
1205 const Dune::ParameterTree& config) const
1206 {
1207 using OP = OverlappingSchwarzOperator<M,X,Y,C>;
1208
1209 auto cop = std::static_pointer_cast<const OP>(op);
1210
1211 if(smoother == "ssor")
1212 return std::make_shared<Amg::AMG<OP, X, BlockPreconditioner<X,Y,C,SeqSSOR<M,X,Y>>,C>>(cop, config, op->getCommunication());
1213 if(smoother == "sor")
1214 return std::make_shared<Amg::AMG<OP, X, BlockPreconditioner<X,Y,C,SeqSOR<M,X,Y>>,C>>(cop, config, op->getCommunication());
1215 if(smoother == "jac")
1216 return std::make_shared<Amg::AMG<OP, X, BlockPreconditioner<X,Y,C,SeqJac<M,X,Y>>,C>>(cop, config, op->getCommunication());
1217 if(smoother == "gs")
1218 return std::make_shared<Amg::AMG<OP, X, BlockPreconditioner<X,Y,C,SeqGS<M,X,Y>>,C>>(cop, config, op->getCommunication());
1219 if(smoother == "ilu")
1220 return std::make_shared<Amg::AMG<OP, X, BlockPreconditioner<X,Y,C,SeqILU<M,X,Y>>,C>>(cop, config, op->getCommunication());
1221 else
1222 DUNE_THROW(Dune::Exception, "Unknown smoother for AMG");
1223 }
1224
1225 template<class M, class X, class Y, class C>
1226 std::shared_ptr<Dune::Preconditioner<X,Y> >
1227 makeAMG(const std::shared_ptr<NonoverlappingSchwarzOperator<M,X,Y,C>>& op, const std::string& smoother,
1228 const Dune::ParameterTree& config) const
1229 {
1230 using OP = NonoverlappingSchwarzOperator<M,X,Y,C>;
1231
1232 if(smoother == "ssor")
1233 return std::make_shared<Amg::AMG<OP, X, NonoverlappingBlockPreconditioner<C,SeqSSOR<M,X,Y>>,C>>(op, config, op->getCommunication());
1234 if(smoother == "sor")
1235 return std::make_shared<Amg::AMG<OP, X, NonoverlappingBlockPreconditioner<C,SeqSOR<M,X,Y>>,C>>(op, config, op->getCommunication());
1236 if(smoother == "jac")
1237 return std::make_shared<Amg::AMG<OP, X, NonoverlappingBlockPreconditioner<C,SeqJac<M,X,Y>>,C>>(op, config, op->getCommunication());
1238 if(smoother == "gs")
1239 return std::make_shared<Amg::AMG<OP, X, NonoverlappingBlockPreconditioner<C,SeqGS<M,X,Y>>,C>>(op, config, op->getCommunication());
1240 if(smoother == "ilu")
1241 return std::make_shared<Amg::AMG<OP, X, NonoverlappingBlockPreconditioner<C,SeqILU<M,X,Y>>,C>>(op, config, op->getCommunication());
1242 else
1243 DUNE_THROW(Dune::Exception, "Unknown smoother for AMG");
1244 }
1245
1246 template<typename OpTraits, typename OP>
1247 std::shared_ptr<Dune::Preconditioner<typename OpTraits::domain_type,
1248 typename OpTraits::range_type>>
1249 operator() (OpTraits opTraits, const std::shared_ptr<OP>& op, const Dune::ParameterTree& config,
1250 std::enable_if_t<isValidMatrix<typename OpTraits::matrix_type>::value,int> = 0) const
1251 {
1252 using field_type = typename OpTraits::matrix_type::field_type;
1253 using real_type = typename FieldTraits<field_type>::real_type;
1254 if (!std::is_convertible<field_type, real_type>())
1255 DUNE_THROW(UnsupportedType, "AMG needs field_type(" <<
1256 className<field_type>() <<
1257 ") to be convertible to its real_type (" <<
1258 className<real_type>() <<
1259 ").");
1260 std::string smoother = config.get("smoother", "ssor");
1261 // we can irgnore the OpTraits here. As the AMG can only work
1262 // with actual matrices, the operator op must be of type
1263 // MatrixAdapter or *SchwarzOperator. In any case these
1264 // operators provide all necessary information about matrix,
1265 // domain and range type
1266 return makeAMG(op, smoother, config);
1267 }
1268
1269 template<typename OpTraits, typename OP>
1270 std::shared_ptr<Dune::Preconditioner<typename OpTraits::domain_type,
1271 typename OpTraits::range_type>>
1272 operator() (OpTraits opTraits, const std::shared_ptr<OP>& op, const Dune::ParameterTree& config,
1273 std::enable_if_t<!isValidMatrix<typename OpTraits::matrix_type>::value,int> = 0) const
1274 {
1275 DUNE_THROW(UnsupportedType, "AMG needs a FieldMatrix as Matrix block_type");
1276 }
1277 };
1278
1279 DUNE_REGISTER_PRECONDITIONER("amg", AMGCreator());
1280 } // end namespace Dune
1281
1282 #endif