Debian Codegraph @

dune/istl/paamg/twolevelmethod.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_ISTL_TWOLEVELMETHOD_HH
6 #define DUNE_ISTL_TWOLEVELMETHOD_HH
7
8 #include <tuple>
9
10 #include<dune/istl/operators.hh>
11 #include"amg.hh"
12 #include"galerkin.hh"
13 #include<dune/istl/solver.hh>
14
15 /**
16 * @addtogroup ISTL_PAAMG
17 * @{
18 * @file
19 * @author Markus Blatt
20 * @brief Algebraic twolevel methods.
21 */
22 namespace Dune
23 {
24 namespace Amg
25 {
26
27 /**
28 * @brief Abstract base class for transfer between levels and creation
29 * of the coarse level system.
30 *
31 * @tparam FO The type of the linear operator of the finel level system. Has to be
32 * derived from AssembledLinearOperator.
33 * @tparam CO The type of the linear operator of the coarse level system. Has to be
34 * derived from AssembledLinearOperator.
35 */
36 template<class FO, class CO>
37 class LevelTransferPolicy
38 {
39 public:
40 /**
41 * @brief The linear operator of the finel level system. Has to be
42 * derived from AssembledLinearOperator.
43 */
44 typedef FO FineOperatorType;
45 /**
46 * @brief The type of the range of the fine level operator.
47 */
48 typedef typename FineOperatorType::range_type FineRangeType;
49 /**
50 * @brief The type of the domain of the fine level operator.
51 */
52 typedef typename FineOperatorType::domain_type FineDomainType;
53 /**
54 * @brief The linear operator of the finel level system. Has to be
55 * derived from AssembledLinearOperator.
56 */
57 typedef CO CoarseOperatorType;
58 /**
59 * @brief The type of the range of the coarse level operator.
60 */
61 typedef typename CoarseOperatorType::range_type CoarseRangeType;
62 /**
63 * @brief The type of the domain of the coarse level operator.
64 */
65 typedef typename CoarseOperatorType::domain_type CoarseDomainType;
66 /**
67 * @brief Get the coarse level operator.
68 * @return A shared pointer to the coarse level system.
69 */
70 std::shared_ptr<CoarseOperatorType>& getCoarseLevelOperator()
71 {
72 return operator_;
73 }
74 /**
75 * @brief Get the coarse level right hand side.
76 * @return The coarse level right hand side.
77 */
78 CoarseRangeType& getCoarseLevelRhs()
79 {
80 return rhs_;
81 }
82
83 /**
84 * @brief Get the coarse level left hand side.
85 * @return The coarse level leftt hand side.
86 */
87 CoarseDomainType& getCoarseLevelLhs()
88 {
89 return lhs_;
90 }
91 /**
92 * @brief Transfers the data to the coarse level.
93 *
94 * Restricts the residual to the right hand side of the
95 * coarse level system and initializes the left hand side
96 * of the coarse level system. These can afterwards be accessed
97 * using getCoarseLevelRhs() and getCoarseLevelLhs().
98 * @param fineRhs The current right hand side of the fine level system.
99 */
100 virtual void moveToCoarseLevel(const FineRangeType& fineRhs)=0;
101 /**
102 * @brief Updates the fine level linear system after the correction
103 * of the coarse levels system.
104 *
105 * After returning from this function the coarse level correction
106 * will have been added to fine level system.
107 * @param[in,out] fineLhs The left hand side of the fine level to update
108 * with the coarse level correction.
109 */
110 virtual void moveToFineLevel(FineDomainType& fineLhs)=0;
111 /**
112 * @brief Algebraically creates the coarse level system.
113 *
114 * After returning from this function the coarse level operator
115 * can be accessed using getCoarseLevelOperator().
116 * @param fineOperator The operator of the fine level system.
117 */
118 virtual void createCoarseLevelSystem(const FineOperatorType& fineOperator)=0;
119
120 /** @brief Clone the current object. */
121 virtual LevelTransferPolicy* clone() const =0;
122
123 /** @brief Destructor. */
124 virtual ~LevelTransferPolicy(){}
125
126 protected:
127 /** @brief The coarse level rhs. */
128 CoarseRangeType rhs_;
129 /** @brief The coarse level lhs. */
130 CoarseDomainType lhs_;
131 /** @brief the coarse level linear operator. */
132 std::shared_ptr<CoarseOperatorType> operator_;
133 };
134
135 /**
136 * @brief A LeveTransferPolicy that used aggregation to construct the coarse level system.
137 * @tparam O The type of the fine and coarse level operator.
138 * @tparam C The criterion that describes the aggregation procedure.
139 */
140 template<class O, class C>
141 class AggregationLevelTransferPolicy
142 : public LevelTransferPolicy<O,O>
143 {
144 typedef Dune::Amg::AggregatesMap<typename O::matrix_type::size_type> AggregatesMap;
145 public:
146 typedef LevelTransferPolicy<O,O> FatherType;
147 typedef C Criterion;
148 typedef SequentialInformation ParallelInformation;
149
150 AggregationLevelTransferPolicy(const Criterion& crit)
151 : criterion_(crit)
152 {}
153
154 void createCoarseLevelSystem(const O& fineOperator)
155 {
156 prolongDamp_ = criterion_.getProlongationDampingFactor();
157 GalerkinProduct<ParallelInformation> productBuilder;
158 typedef typename Dune::Amg::MatrixGraph<const typename O::matrix_type> MatrixGraph;
159 typedef typename Dune::Amg::PropertiesGraph<MatrixGraph,Dune::Amg::VertexProperties,
160 Dune::Amg::EdgeProperties,Dune::IdentityMap,Dune::IdentityMap> PropertiesGraph;
161 MatrixGraph mg(fineOperator.getmat());
162 PropertiesGraph pg(mg,Dune::IdentityMap(),Dune::IdentityMap());
163 typedef NegateSet<typename ParallelInformation::OwnerSet> OverlapFlags;
164
165 aggregatesMap_ = std::make_shared<AggregatesMap>(pg.maxVertex()+1);
166
167 int noAggregates, isoAggregates, oneAggregates, skippedAggregates;
168
169 std::tie(noAggregates, isoAggregates, oneAggregates, skippedAggregates) =
170 aggregatesMap_->buildAggregates(fineOperator.getmat(), pg, criterion_, true);
171 std::cout<<"no aggregates="<<noAggregates<<" iso="<<isoAggregates<<" one="<<oneAggregates<<" skipped="<<skippedAggregates<<std::endl;
172 // misuse coarsener to renumber aggregates
173 Dune::Amg::IndicesCoarsener<Dune::Amg::SequentialInformation,int> renumberer;
174 typedef std::vector<bool>::iterator Iterator;
175 typedef Dune::IteratorPropertyMap<Iterator, Dune::IdentityMap> VisitedMap;
176 std::vector<bool> excluded(fineOperator.getmat().N(), false);
177 VisitedMap vm(excluded.begin(), Dune::IdentityMap());
178 ParallelInformation pinfo;
179 std::size_t aggregates = renumberer.coarsen(pinfo, pg, vm,
180 *aggregatesMap_, pinfo,
181 noAggregates);
182 std::vector<bool>& visited=excluded;
183
184 typedef std::vector<bool>::iterator Iterator;
185
186 for(Iterator iter= visited.begin(), end=visited.end();
187 iter != end; ++iter)
188 *iter=false;
189 matrix_.reset(productBuilder.build(mg, vm,
190 SequentialInformation(),
191 *aggregatesMap_,
192 aggregates,
193 OverlapFlags()));
194 productBuilder.calculate(fineOperator.getmat(), *aggregatesMap_, *matrix_, pinfo, OverlapFlags());
195 this->lhs_.resize(this->matrix_->M());
196 this->rhs_.resize(this->matrix_->N());
197 this->operator_ = std::make_shared<O>(*matrix_);
198 }
199
200 void moveToCoarseLevel(const typename FatherType::FineRangeType& fineRhs)
201 {
202 Transfer<typename AggregatesMap::VertexDescriptor,typename FatherType::FineRangeType,ParallelInformation>
203 ::restrictVector(*aggregatesMap_, this->rhs_, fineRhs, ParallelInformation());
204 this->lhs_=0;
205 }
206
207 void moveToFineLevel(typename FatherType::FineDomainType& fineLhs)
208 {
209 Transfer<typename AggregatesMap::VertexDescriptor,typename FatherType::FineRangeType,ParallelInformation>
210 ::prolongateVector(*aggregatesMap_, this->lhs_, fineLhs,
211 prolongDamp_, ParallelInformation());
212 }
213
214 AggregationLevelTransferPolicy* clone() const
215 {
216 return new AggregationLevelTransferPolicy(*this);
217 }
218
219 private:
220 typename O::matrix_type::field_type prolongDamp_;
221 std::shared_ptr<AggregatesMap> aggregatesMap_;
222 Criterion criterion_;
223 std::shared_ptr<typename O::matrix_type> matrix_;
224 };
225
226 /**
227 * @brief A policy class for solving the coarse level system using one step of AMG.
228 * @tparam O The type of the linear operator used.
229 * @tparam S The type of the smoother used in AMG.
230 * @tparam C The type of the crition used for the aggregation within AMG.
231 */
232 template<class O, class S, class C>
233 class OneStepAMGCoarseSolverPolicy
234 {
235 public:
236 /** @brief The type of the linear operator used. */
237 typedef O Operator;
238 /** @brief The type of the range and domain of the operator. */
239 typedef typename O::range_type X;
240 /** @brief The type of the crition used for the aggregation within AMG.*/
241 typedef C Criterion;
242 /** @brief The type of the smoother used in AMG. */
243 typedef S Smoother;
244 /** @brief The type of the arguments used for constructing the smoother. */
245 typedef typename Dune::Amg::SmootherTraits<S>::Arguments SmootherArgs;
246 /** @brief The type of the AMG construct on the coarse level.*/
247 typedef AMG<Operator,X,Smoother> AMGType;
248 /**
249 * @brief Constructs the coarse solver policy.
250 * @param args The arguments used for constructing the smoother.
251 * @param c The crition used for the aggregation within AMG.
252 */
253 OneStepAMGCoarseSolverPolicy(const SmootherArgs& args, const Criterion& c)
254 : smootherArgs_(args), criterion_(c)
255 {}
256 /** @brief Copy constructor. */
257 OneStepAMGCoarseSolverPolicy(const OneStepAMGCoarseSolverPolicy& other)
258 : coarseOperator_(other.coarseOperator_), smootherArgs_(other.smootherArgs_),
259 criterion_(other.criterion_)
260 {}
261 private:
262 /**
263 * @brief A wrapper that makes an inverse operator out of AMG.
264 *
265 * The operator will use one step of AMG to approximately solve
266 * the coarse level system.
267 */
268 struct AMGInverseOperator : public InverseOperator<X,X>
269 {
270 AMGInverseOperator(const typename AMGType::Operator& op,
271 const Criterion& crit,
272 const typename AMGType::SmootherArgs& args)
273 : amg_(op, crit,args), first_(true)
274 {}
275
276 void apply(X& x, X& b, [[maybe_unused]] double reduction, [[maybe_unused]] InverseOperatorResult& res) override
277 {
278 if(first_)
279 {
280 amg_.pre(x,b);
281 first_=false;
282 x_=x;
283 }
284 amg_.apply(x,b);
285 }
286
287 void apply(X& x, X& b, InverseOperatorResult& res) override
288 {
289 return apply(x,b,1e-8,res);
290 }
291
292 //! Category of the solver (see SolverCategory::Category)
293 SolverCategory::Category category() const override
294 {
295 return amg_.category();
296 }
297
298 ~AMGInverseOperator()
299 {
300 if(!first_)
301 amg_.post(x_);
302 }
303 AMGInverseOperator(const AMGInverseOperator& other)
304 : x_(other.x_), amg_(other.amg_), first_(other.first_)
305 {
306 }
307 private:
308 X x_;
309 AMGType amg_;
310 bool first_;
311 };
312
313 public:
314 /** @brief The type of solver constructed for the coarse level. */
315 typedef AMGInverseOperator CoarseLevelSolver;
316
317 /**
318 * @brief Constructs a coarse level solver.
319 *
320 * @param transferPolicy The policy describing the transfer between levels.
321 * @return A pointer to the constructed coarse level solver.
322 * @tparam P The type of the level transfer policy.
323 */
324 template<class P>
325 CoarseLevelSolver* createCoarseLevelSolver(P& transferPolicy)
326 {
327 coarseOperator_=transferPolicy.getCoarseLevelOperator();
328 AMGInverseOperator* inv = new AMGInverseOperator(*coarseOperator_,
329 criterion_,
330 smootherArgs_);
331
332 return inv; //std::shared_ptr<InverseOperator<X,X> >(inv);
333
334 }
335
336 private:
337 /** @brief The coarse level operator. */
338 std::shared_ptr<Operator> coarseOperator_;
339 /** @brief The arguments used to construct the smoother. */
340 SmootherArgs smootherArgs_;
341 /** @brief The coarsening criterion. */
342 Criterion criterion_;
343 };
344
345 /**
346 * @tparam FO The type of the fine level linear operator.
347 * @tparam CSP The type of the coarse level solver policy.
348 * @tparam S The type of the fine level smoother used.
349 */
350 template<class FO, class CSP, class S>
351 class TwoLevelMethod :
352 public Preconditioner<typename FO::domain_type, typename FO::range_type>
353 {
354 public:
355 /** @brief The type of the policy for constructing the coarse level solver. */
356 typedef CSP CoarseLevelSolverPolicy;
357 /** @brief The type of the coarse level solver. */
358 typedef typename CoarseLevelSolverPolicy::CoarseLevelSolver CoarseLevelSolver;
359 /**
360 * @brief The linear operator of the finel level system. Has to be
361 * derived from AssembledLinearOperator.
362 */
363 typedef FO FineOperatorType;
364 /**
365 * @brief The type of the range of the fine level operator.
366 */
367 typedef typename FineOperatorType::range_type FineRangeType;
368 /**
369 * @brief The type of the domain of the fine level operator.
370 */
371 typedef typename FineOperatorType::domain_type FineDomainType;
372 /**
373 * @brief The linear operator of the finel level system. Has to be
374 * derived from AssembledLinearOperator.
375 */
376 typedef typename CSP::Operator CoarseOperatorType;
377 /**
378 * @brief The type of the range of the coarse level operator.
379 */
380 typedef typename CoarseOperatorType::range_type CoarseRangeType;
381 /**
382 * @brief The type of the domain of the coarse level operator.
383 */
384 typedef typename CoarseOperatorType::domain_type CoarseDomainType;
385 /**
386 * @brief The type of the fine level smoother.
387 */
388 typedef S SmootherType;
389
390 /**
391 * @brief Constructs a two level method.
392 *
393 * @tparam CoarseSolverPolicy The policy for constructing the coarse
394 * solver, e.g. OneStepAMGCoarseSolverPolicy
395 * @param op The fine level operator.
396 * @param smoother The fine level smoother.
397 * @param policy The level transfer policy.
398 * @param coarsePolicy The policy for constructing the coarse level solver.
399 * @param preSteps The number of smoothing steps to apply before the coarse
400 * level correction.
401 * @param postSteps The number of smoothing steps to apply after the coarse
402 * level correction.
403 */
404 TwoLevelMethod(const FineOperatorType& op,
405 std::shared_ptr<SmootherType> smoother,
406 const LevelTransferPolicy<FineOperatorType,
407 CoarseOperatorType>& policy,
408 CoarseLevelSolverPolicy& coarsePolicy,
409 std::size_t preSteps=1, std::size_t postSteps=1)
410 : operator_(&op), smoother_(smoother),
411 preSteps_(preSteps), postSteps_(postSteps)
412 {
413 policy_ = policy.clone();
414 policy_->createCoarseLevelSystem(*operator_);
415 coarseSolver_=coarsePolicy.createCoarseLevelSolver(*policy_);
416 }
417
418 TwoLevelMethod(const TwoLevelMethod& other)
419 : operator_(other.operator_), coarseSolver_(new CoarseLevelSolver(*other.coarseSolver_)),
420 smoother_(other.smoother_), policy_(other.policy_->clone()),
421 preSteps_(other.preSteps_), postSteps_(other.postSteps_)
422 {}
423
424 ~TwoLevelMethod()
425 {
426 // Each instance has its own policy.
427 delete policy_;
428 delete coarseSolver_;
429 }
430
431 void pre(FineDomainType& x, FineRangeType& b) override
432 {
433 smoother_->pre(x,b);
434 }
435
436 void post([[maybe_unused]] FineDomainType& x) override
437 {}
438
439 void apply(FineDomainType& v, const FineRangeType& d) override
440 {
441 FineDomainType u(v);
442 FineRangeType rhs(d);
443 LevelContext context;
444 SequentialInformation info;
445 context.pinfo=&info;
446 context.lhs=&u;
447 context.update=&v;
448 context.smoother=smoother_;
449 context.rhs=&rhs;
450 context.matrix=operator_;
451 // Presmoothing
452 presmooth(context, preSteps_);
453 //Coarse grid correction
454 policy_->moveToCoarseLevel(*context.rhs);
455 InverseOperatorResult res;
456 coarseSolver_->apply(policy_->getCoarseLevelLhs(), policy_->getCoarseLevelRhs(), res);
457 *context.lhs=0;
458 policy_->moveToFineLevel(*context.lhs);
459 *context.update += *context.lhs;
460 // Postsmoothing
461 postsmooth(context, postSteps_);
462
463 }
464
465 //! Category of the preconditioner (see SolverCategory::Category)
466 SolverCategory::Category category() const override
467 {
468 return SolverCategory::sequential;
469 }
470
471 private:
472 /**
473 * @brief Struct containing the level information.
474 */
475 struct LevelContext
476 {
477 /** @brief The type of the smoother used. */
478 typedef S SmootherType;
479 /** @brief A pointer to the smoother. */
480 std::shared_ptr<SmootherType> smoother;
481 /** @brief The left hand side passed to the and returned by the smoother. */
482 FineDomainType* lhs;
483 /*
484 * @brief The right hand side holding the current residual.
485 *
486 * This is passed to the smoother as the right hand side.
487 */
488 FineRangeType* rhs;
489 /**
490 * @brief The total update calculated by the preconditioner.
491 *
492 * I.e. all update from smoothing and coarse grid correction summed up.
493 */
494 FineDomainType* update;
495 /** @parallel information */
496 SequentialInformation* pinfo;
497 /**
498 * @brief The matrix that we are solving.
499 *
500 * Needed to update the residual.
501 */
502 const FineOperatorType* matrix;
503 };
504 const FineOperatorType* operator_;
505 /** @brief The coarse level solver. */
506 CoarseLevelSolver* coarseSolver_;
507 /** @brief The fine level smoother. */
508 std::shared_ptr<S> smoother_;
509 /** @brief Policy for prolongation, restriction, and coarse level system creation. */
510 LevelTransferPolicy<FO,typename CSP::Operator>* policy_;
511 /** @brief The number of presmoothing steps to apply. */
512 std::size_t preSteps_;
513 /** @brief The number of postsmoothing steps to apply. */
514 std::size_t postSteps_;
515 };
516 }// end namespace Amg
517 }// end namespace Dune
518
519 /** @} */
520 #endif