This file is indexed.

/usr/include/dune/istl/paamg/smoother.hh is in libdune-istl-dev 2.5.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_AMGSMOOTHER_HH
#define DUNE_AMGSMOOTHER_HH

#include <dune/istl/paamg/construction.hh>
#include <dune/istl/paamg/aggregates.hh>
#include <dune/istl/preconditioners.hh>
#include <dune/istl/schwarz.hh>
#include <dune/istl/novlpschwarz.hh>
#include <dune/common/propertymap.hh>
#include <dune/common/unused.hh>

namespace Dune
{
  namespace Amg
  {

    /**
     * @addtogroup ISTL_PAAMG
     *
     * @{
     */

    /** @file
     * @author Markus Blatt
     * @brief Classes for the generic construction and application
     * of the smoothers.
     */

    /**
     * @brief The default class for the smoother arguments.
     */
    template<class T>
    struct DefaultSmootherArgs
    {
      /**
       * @brief The type of the relaxation factor.
       */
      typedef T RelaxationFactor;

      /**
       * @brief The numbe of iterations to perform.
       */
      int iterations;
      /**
       * @brief The relaxation factor to use.
       */
      RelaxationFactor relaxationFactor;

      /**
       * @brief Default constructor.
       */
      DefaultSmootherArgs()
        : iterations(1), relaxationFactor(1.0)
      {}
    };

    /**
     * @brief Traits class for getting the attribute class of a smoother.
     */
    template<class T>
    struct SmootherTraits
    {
      typedef DefaultSmootherArgs<typename T::matrix_type::field_type> Arguments;

    };

    template<class X, class Y, class C, class T>
    struct SmootherTraits<BlockPreconditioner<X,Y,C,T> >
    {
      typedef DefaultSmootherArgs<typename T::matrix_type::field_type> Arguments;

    };

    template<class C, class T>
    struct SmootherTraits<NonoverlappingBlockPreconditioner<C,T> >
    {
      typedef DefaultSmootherArgs<typename T::matrix_type::field_type> Arguments;

    };

    /**
     * @brief Construction Arguments for the default smoothers
     */
    template<class T>
    class DefaultConstructionArgs
    {
      typedef typename T::matrix_type Matrix;

      typedef typename SmootherTraits<T>::Arguments SmootherArgs;

      typedef Dune::Amg::AggregatesMap<typename MatrixGraph<Matrix>::VertexDescriptor> AggregatesMap;

    public:
      virtual ~DefaultConstructionArgs()
      {}

      void setMatrix(const Matrix& matrix)
      {
        matrix_=&matrix;
      }
      virtual void setMatrix(const Matrix& matrix, const AggregatesMap& amap)
      {
        DUNE_UNUSED_PARAMETER(amap);
        setMatrix(matrix);
      }


      const Matrix& getMatrix() const
      {
        return *matrix_;
      }

      void setArgs(const SmootherArgs& args)
      {
        args_=&args;
      }

      template<class T1>
      void setComm(T1& comm)
      {
        DUNE_UNUSED_PARAMETER(comm);
      }

      const SequentialInformation& getComm()
      {
        return comm_;
      }

      const SmootherArgs getArgs() const
      {
        return *args_;
      }

    protected:
      const Matrix* matrix_;
    private:
      const SmootherArgs* args_;
      SequentialInformation comm_;
    };

    template<class T>
    struct ConstructionArgs
      : public DefaultConstructionArgs<T>
    {};

    template<class T, class C=SequentialInformation>
    class DefaultParallelConstructionArgs
      : public ConstructionArgs<T>
    {
    public:
      virtual ~DefaultParallelConstructionArgs()
      {}

      void setComm(const C& comm)
      {
        comm_ = &comm;
      }

      const C& getComm() const
      {
        return *comm_;
      }
    private:
      const C* comm_;
    };


    template<class T>
    class ConstructionTraits;

    /**
     * @brief Policy for the construction of the SeqSSOR smoother
     */
    template<class M, class X, class Y, int l>
    struct ConstructionTraits<SeqSSOR<M,X,Y,l> >
    {
      typedef DefaultConstructionArgs<SeqSSOR<M,X,Y,l> > Arguments;

      static inline SeqSSOR<M,X,Y,l>* construct(Arguments& args)
      {
        return new SeqSSOR<M,X,Y,l>(args.getMatrix(), args.getArgs().iterations,
                                    args.getArgs().relaxationFactor);
      }

      static inline void deconstruct(SeqSSOR<M,X,Y,l>* ssor)
      {
        delete ssor;
      }

    };


    /**
     * @brief Policy for the construction of the SeqSOR smoother
     */
    template<class M, class X, class Y, int l>
    struct ConstructionTraits<SeqSOR<M,X,Y,l> >
    {
      typedef DefaultConstructionArgs<SeqSOR<M,X,Y,l> > Arguments;

      static inline SeqSOR<M,X,Y,l>* construct(Arguments& args)
      {
        return new SeqSOR<M,X,Y,l>(args.getMatrix(), args.getArgs().iterations,
                                   args.getArgs().relaxationFactor);
      }

      static inline void deconstruct(SeqSOR<M,X,Y,l>* sor)
      {
        delete sor;
      }

    };
    /**
     * @brief Policy for the construction of the SeqJac smoother
     */
    template<class M, class X, class Y, int l>
    struct ConstructionTraits<SeqJac<M,X,Y,l> >
    {
      typedef DefaultConstructionArgs<SeqJac<M,X,Y,l> > Arguments;

      static inline SeqJac<M,X,Y,l>* construct(Arguments& args)
      {
        return new SeqJac<M,X,Y,l>(args.getMatrix(), args.getArgs().iterations,
                                   args.getArgs().relaxationFactor);
      }

      static void deconstruct(SeqJac<M,X,Y,l>* jac)
      {
        delete jac;
      }

    };


    /**
     * @brief Policy for the construction of the SeqILUn smoother
     */
    template<class M, class X, class Y>
    struct ConstructionTraits<SeqILU0<M,X,Y> >
    {
      typedef DefaultConstructionArgs<SeqILU0<M,X,Y> > Arguments;

      static inline SeqILU0<M,X,Y>* construct(Arguments& args)
      {
        return new SeqILU0<M,X,Y>(args.getMatrix(),
                                  args.getArgs().relaxationFactor);
      }

      static void deconstruct(SeqILU0<M,X,Y>* ilu)
      {
        delete ilu;
      }

    };

    template<class M, class X, class Y>
    class ConstructionArgs<SeqILUn<M,X,Y> >
      : public DefaultConstructionArgs<SeqILUn<M,X,Y> >
    {
    public:
      ConstructionArgs(int n=1)
        : n_(n)
      {}

      void setN(int n)
      {
        n_ = n;
      }
      int getN()
      {
        return n_;
      }

    private:
      int n_;
    };


    /**
     * @brief Policy for the construction of the SeqJac smoother
     */
    template<class M, class X, class Y>
    struct ConstructionTraits<SeqILUn<M,X,Y> >
    {
      typedef ConstructionArgs<SeqILUn<M,X,Y> > Arguments;

      static inline SeqILUn<M,X,Y>* construct(Arguments& args)
      {
        return new SeqILUn<M,X,Y>(args.getMatrix(), args.getN(),
                                  args.getArgs().relaxationFactor);
      }

      static void deconstruct(SeqILUn<M,X,Y>* ilu)
      {
        delete ilu;
      }

    };

    /**
     * @brief Policy for the construction of the ParSSOR smoother
     */
    template<class M, class X, class Y, class C>
    struct ConstructionTraits<ParSSOR<M,X,Y,C> >
    {
      typedef DefaultParallelConstructionArgs<M,C> Arguments;

      static inline ParSSOR<M,X,Y,C>* construct(Arguments& args)
      {
        return new ParSSOR<M,X,Y,C>(args.getMatrix(), args.getArgs().iterations,
                                    args.getArgs().relaxationFactor,
                                    args.getComm());
      }
      static inline void deconstruct(ParSSOR<M,X,Y,C>* ssor)
      {
        delete ssor;
      }
    };

    template<class X, class Y, class C, class T>
    struct ConstructionTraits<BlockPreconditioner<X,Y,C,T> >
    {
      typedef DefaultParallelConstructionArgs<T,C> Arguments;
      typedef ConstructionTraits<T> SeqConstructionTraits;
      static inline BlockPreconditioner<X,Y,C,T>* construct(Arguments& args)
      {
        return new BlockPreconditioner<X,Y,C,T>(*SeqConstructionTraits::construct(args),
                                                args.getComm());
      }

      static inline void deconstruct(BlockPreconditioner<X,Y,C,T>* bp)
      {
        SeqConstructionTraits::deconstruct(static_cast<T*>(&bp->preconditioner));
        delete bp;
      }

    };

    template<class C, class T>
    struct ConstructionTraits<NonoverlappingBlockPreconditioner<C,T> >
    {
      typedef DefaultParallelConstructionArgs<T,C> Arguments;
      typedef ConstructionTraits<T> SeqConstructionTraits;
      static inline NonoverlappingBlockPreconditioner<C,T>* construct(Arguments& args)
      {
        return new NonoverlappingBlockPreconditioner<C,T>(*SeqConstructionTraits::construct(args),
                                                          args.getComm());
      }

      static inline void deconstruct(NonoverlappingBlockPreconditioner<C,T>* bp)
      {
        SeqConstructionTraits::deconstruct(static_cast<T*>(&bp->preconditioner));
        delete bp;
      }

    };

    /**
     * @brief Helper class for applying the smoothers.
     *
     * The goal of this class is to get a symmetric AMG method
     * whenever possible.
     *
     * The specializations for SOR and SeqOverlappingSchwarz in
     * MultiplicativeSchwarzMode will apply
     * the smoother forward when pre and backward when post smoothing.
     */
    template<class T>
    struct SmootherApplier
    {
      typedef T Smoother;
      typedef typename Smoother::range_type Range;
      typedef typename Smoother::domain_type Domain;

      /**
       * @brief apply pre smoothing in forward direction
       *
       * @param smoother The smoother to use.
       * @param d The current defect.
       * @param v handle to store the update in.
       */
      static void preSmooth(Smoother& smoother, Domain& v, const Range& d)
      {
        smoother.apply(v,d);
      }

      /**
       * @brief apply post smoothing in forward direction
       *
       * @param smoother The smoother to use.
       * @param d The current defect.
       * @param v handle to store the update in.
       */
      static void postSmooth(Smoother& smoother, Domain& v, const Range& d)
      {
        smoother.apply(v,d);
      }
    };

    /**
     * @brief Apply pre smoothing on the current level.
     * @param levelContext the iterators of the current level.
     * @param steps The number of smoothing steps to apply.
     */
    template<typename LevelContext>
    void presmooth(LevelContext& levelContext, size_t steps)
    {
        for(std::size_t i=0; i < steps; ++i) {
          *levelContext.lhs=0;
          SmootherApplier<typename LevelContext::SmootherType>
            ::preSmooth(*levelContext.smoother, *levelContext.lhs,
                        *levelContext.rhs);
          // Accumulate update
          *levelContext.update += *levelContext.lhs;

          // update defect
          levelContext.matrix->applyscaleadd(-1, *levelContext.lhs, *levelContext.rhs);
          levelContext.pinfo->project(*levelContext.rhs);
        }
    }

    /**
     * @brief Apply post smoothing on the current level.
     * @param levelContext the iterators of the current level.
     * @param steps The number of smoothing steps to apply.
     */
    template<typename LevelContext>
    void postsmooth(LevelContext& levelContext, size_t steps)
    {
        for(std::size_t i=0; i < steps; ++i) {
          // update defect
          levelContext.matrix->applyscaleadd(-1, *levelContext.lhs,
                                             *levelContext.rhs);
          *levelContext.lhs=0;
          levelContext.pinfo->project(*levelContext.rhs);
          SmootherApplier<typename LevelContext::SmootherType>
            ::postSmooth(*levelContext.smoother, *levelContext.lhs, *levelContext.rhs);
          // Accumulate update
          *levelContext.update += *levelContext.lhs;
        }
    }

    template<class M, class X, class Y, int l>
    struct SmootherApplier<SeqSOR<M,X,Y,l> >
    {
      typedef SeqSOR<M,X,Y,l> Smoother;
      typedef typename Smoother::range_type Range;
      typedef typename Smoother::domain_type Domain;

      static void preSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<true>(v,d);
      }


      static void postSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<false>(v,d);
      }
    };

    template<class M, class X, class Y, class C, int l>
    struct SmootherApplier<BlockPreconditioner<X,Y,C,SeqSOR<M,X,Y,l> > >
    {
      typedef BlockPreconditioner<X,Y,C,SeqSOR<M,X,Y,l> > Smoother;
      typedef typename Smoother::range_type Range;
      typedef typename Smoother::domain_type Domain;

      static void preSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<true>(v,d);
      }


      static void postSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<false>(v,d);
      }
    };

    template<class M, class X, class Y, class C, int l>
    struct SmootherApplier<NonoverlappingBlockPreconditioner<C,SeqSOR<M,X,Y,l> > >
    {
      typedef NonoverlappingBlockPreconditioner<C,SeqSOR<M,X,Y,l> > Smoother;
      typedef typename Smoother::range_type Range;
      typedef typename Smoother::domain_type Domain;

      static void preSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<true>(v,d);
      }


      static void postSmooth(Smoother& smoother, Domain& v, Range& d)
      {
        smoother.template apply<false>(v,d);
      }
    };

  } // end namespace Amg

  // forward declarations
  template<class M, class X, class MO, class MS, class A>
  class SeqOverlappingSchwarz;

  struct MultiplicativeSchwarzMode;

  namespace Amg
  {
    template<class M, class X, class MS, class TA>
    struct SmootherApplier<SeqOverlappingSchwarz<M,X,MultiplicativeSchwarzMode,
            MS,TA> >
    {
      typedef SeqOverlappingSchwarz<M,X,MultiplicativeSchwarzMode,MS,TA> Smoother;
      typedef typename Smoother::range_type Range;
      typedef typename Smoother::domain_type Domain;

      static void preSmooth(Smoother& smoother, Domain& v, const Range& d)
      {
        smoother.template apply<true>(v,d);
      }


      static void postSmooth(Smoother& smoother, Domain& v, const Range& d)
      {
        smoother.template apply<false>(v,d);

      }
    };

    //    template<class M, class X, class TM, class TA>
    //    class SeqOverlappingSchwarz;

    template<class T>
    struct SeqOverlappingSchwarzSmootherArgs
      : public DefaultSmootherArgs<T>
    {
      enum Overlap {vertex, aggregate, pairwise, none};

      Overlap overlap;
      bool onthefly;

      SeqOverlappingSchwarzSmootherArgs(Overlap overlap_=vertex,
                                        bool onthefly_=false)
        : overlap(overlap_), onthefly(onthefly_)
      {}
    };

    template<class M, class X, class TM, class TS, class TA>
    struct SmootherTraits<SeqOverlappingSchwarz<M,X,TM,TS,TA> >
    {
      typedef  SeqOverlappingSchwarzSmootherArgs<typename M::field_type> Arguments;
    };

    template<class M, class X, class TM, class TS, class TA>
    class ConstructionArgs<SeqOverlappingSchwarz<M,X,TM,TS,TA> >
      : public DefaultConstructionArgs<SeqOverlappingSchwarz<M,X,TM,TS,TA> >
    {
      typedef DefaultConstructionArgs<SeqOverlappingSchwarz<M,X,TM,TS,TA> > Father;

    public:
      typedef typename MatrixGraph<M>::VertexDescriptor VertexDescriptor;
      typedef Dune::Amg::AggregatesMap<VertexDescriptor> AggregatesMap;
      typedef typename AggregatesMap::AggregateDescriptor AggregateDescriptor;
      typedef typename SeqOverlappingSchwarz<M,X,TM,TS,TA>::subdomain_vector Vector;
      typedef typename Vector::value_type Subdomain;

      virtual void setMatrix(const M& matrix, const AggregatesMap& amap)
      {
        Father::setMatrix(matrix);

        std::vector<bool> visited(amap.noVertices(), false);
        typedef IteratorPropertyMap<std::vector<bool>::iterator,IdentityMap> VisitedMapType;
        VisitedMapType visitedMap(visited.begin());

        MatrixGraph<const M> graph(matrix);

        typedef SeqOverlappingSchwarzSmootherArgs<typename M::field_type> SmootherArgs;

        switch(Father::getArgs().overlap) {
        case SmootherArgs::vertex :
        {
          VertexAdder visitor(subdomains, amap);
          createSubdomains(matrix, graph, amap, visitor,  visitedMap);
        }
        break;
        case SmootherArgs::pairwise :
        {
          createPairDomains(graph);
        }
        break;
        case SmootherArgs::aggregate :
        {
          AggregateAdder<VisitedMapType> visitor(subdomains, amap, graph, visitedMap);
          createSubdomains(matrix, graph, amap, visitor, visitedMap);
        }
        break;
        case SmootherArgs::none :
          NoneAdder visitor;
          createSubdomains(matrix, graph, amap, visitor, visitedMap);
          break;
        default :
          DUNE_THROW(NotImplemented, "This overlapping scheme is not supported!");
        }
      }
      void setMatrix(const M& matrix)
      {
        Father::setMatrix(matrix);

        /* Create aggregates map where each aggregate is just one vertex. */
        AggregatesMap amap(matrix.N());
        VertexDescriptor v=0;
        for(typename AggregatesMap::iterator iter=amap.begin();
            iter!=amap.end(); ++iter)
          *iter=v++;

        std::vector<bool> visited(amap.noVertices(), false);
        typedef IteratorPropertyMap<std::vector<bool>::iterator,IdentityMap> VisitedMapType;
        VisitedMapType visitedMap(visited.begin());

        MatrixGraph<const M> graph(matrix);

        typedef SeqOverlappingSchwarzSmootherArgs<typename M::field_type> SmootherArgs;

        switch(Father::getArgs().overlap) {
        case SmootherArgs::vertex :
        {
          VertexAdder visitor(subdomains, amap);
          createSubdomains(matrix, graph, amap, visitor,  visitedMap);
        }
        break;
        case SmootherArgs::aggregate :
        {
          DUNE_THROW(NotImplemented, "Aggregate overlap is not supported yet");
          /*
             AggregateAdder<VisitedMapType> visitor(subdomains, amap, graph, visitedMap);
             createSubdomains(matrix, graph, amap, visitor, visitedMap);
           */
        }
        break;
        case SmootherArgs::pairwise :
        {
          createPairDomains(graph);
        }
        break;
        case SmootherArgs::none :
          NoneAdder visitor;
          createSubdomains(matrix, graph, amap, visitor, visitedMap);

        }
      }

      const Vector& getSubDomains()
      {
        return subdomains;
      }

    private:
      struct VertexAdder
      {
        VertexAdder(Vector& subdomains_, const AggregatesMap& aggregates_)
          : subdomains(subdomains_), max(-1), subdomain(-1), aggregates(aggregates_)
        {}
        template<class T>
        void operator()(const T& edge)
        {
          if(aggregates[edge.target()]!=AggregatesMap::ISOLATED)
            subdomains[subdomain].insert(edge.target());
        }
        int setAggregate(const AggregateDescriptor& aggregate_)
        {
          subdomain=aggregate_;
          max = std::max(subdomain, aggregate_);
          return subdomain;
        }
        int noSubdomains() const
        {
          return max+1;
        }
      private:
        Vector& subdomains;
        AggregateDescriptor max;
        AggregateDescriptor subdomain;
        const AggregatesMap& aggregates;
      };
      struct NoneAdder
      {
        template<class T>
        void operator()(const T& edge)
        {}
        int setAggregate(const AggregateDescriptor& aggregate_)
        {
          return -1;
        }
        int noSubdomains() const
        {
          return -1;
        }
      };

      template<class VM>
      struct AggregateAdder
      {
        AggregateAdder(Vector& subdomains_, const AggregatesMap& aggregates_,
                       const MatrixGraph<const M>& graph_, VM& visitedMap_)
          : subdomains(subdomains_), subdomain(-1), aggregates(aggregates_),
            adder(subdomains_, aggregates_), graph(graph_), visitedMap(visitedMap_)
        {}
        template<class T>
        void operator()(const T& edge)
        {
          subdomains[subdomain].insert(edge.target());
          // If we (the neighbouring vertex of the aggregate)
          // are not isolated, add the aggregate we belong to
          // to the same subdomain using the OneOverlapAdder
          if(aggregates[edge.target()]!=AggregatesMap::ISOLATED) {
            assert(aggregates[edge.target()]!=aggregate);
            typename AggregatesMap::VertexList vlist;
            aggregates.template breadthFirstSearch<true,false>(edge.target(), aggregate,
                                                               graph, vlist, adder, adder,
                                                               visitedMap);
          }
        }

        int setAggregate(const AggregateDescriptor& aggregate_)
        {
          adder.setAggregate(aggregate_);
          aggregate=aggregate_;
          return ++subdomain;
        }
        int noSubdomains() const
        {
          return subdomain+1;
        }

      private:
        AggregateDescriptor aggregate;
        Vector& subdomains;
        int subdomain;
        const AggregatesMap& aggregates;
        VertexAdder adder;
        const MatrixGraph<const M>& graph;
        VM& visitedMap;
      };

      void createPairDomains(const MatrixGraph<const M>& graph)
      {
        typedef typename MatrixGraph<const M>::ConstVertexIterator VIter;
        typedef typename MatrixGraph<const M>::ConstEdgeIterator EIter;
        typedef typename M::size_type size_type;

        std::set<std::pair<size_type,size_type> > pairs;
        int total=0;
        for(VIter v=graph.begin(), ve=graph.end(); ve != v; ++v)
          for(EIter e = v.begin(), ee=v.end(); ee!=e; ++e)
          {
            ++total;
            if(e.source()<e.target())
              pairs.insert(std::make_pair(e.source(),e.target()));
            else
              pairs.insert(std::make_pair(e.target(),e.source()));
          }


        subdomains.resize(pairs.size());
        Dune::dinfo <<std::endl<< "Created "<<pairs.size()<<" ("<<total<<") pair domains"<<std::endl<<std::endl;
        typedef typename std::set<std::pair<size_type,size_type> >::const_iterator SIter;
        typename Vector::iterator subdomain=subdomains.begin();

        for(SIter s=pairs.begin(), se =pairs.end(); se!=s; ++s)
        {
          subdomain->insert(s->first);
          subdomain->insert(s->second);
          ++subdomain;
        }
        std::size_t minsize=10000;
        std::size_t maxsize=0;
        int sum=0;
        for(typename Vector::size_type i=0; i < subdomains.size(); ++i) {
          sum+=subdomains[i].size();
          minsize=std::min(minsize, subdomains[i].size());
          maxsize=std::max(maxsize, subdomains[i].size());
        }
        Dune::dinfo<<"Subdomain size: min="<<minsize<<" max="<<maxsize<<" avg="<<(sum/subdomains.size())
                   <<" no="<<subdomains.size()<<std::endl;
      }

      template<class Visitor>
      void createSubdomains(const M& matrix, const MatrixGraph<const M>& graph,
                            const AggregatesMap& amap, Visitor& overlapVisitor,
                            IteratorPropertyMap<std::vector<bool>::iterator,IdentityMap>& visitedMap )
      {
        // count  number ag aggregates. We assume that the
        // aggregates are numbered consecutively from 0 except
        // for the isolated ones. All isolated vertices form
        // one aggregate, here.
        int isolated=0;
        AggregateDescriptor maxAggregate=0;

        for(std::size_t i=0; i < amap.noVertices(); ++i)
          if(amap[i]==AggregatesMap::ISOLATED)
            isolated++;
          else
            maxAggregate = std::max(maxAggregate, amap[i]);

        subdomains.resize(maxAggregate+1+isolated);

        // reset the subdomains
        for(typename Vector::size_type i=0; i < subdomains.size(); ++i)
          subdomains[i].clear();

        // Create the subdomains from the aggregates mapping.
        // For each aggregate we mark all entries and the
        // neighbouring vertices as belonging to the same subdomain
        VertexAdder aggregateVisitor(subdomains, amap);

        for(VertexDescriptor i=0; i < amap.noVertices(); ++i)
          if(!get(visitedMap, i)) {
            AggregateDescriptor aggregate=amap[i];

            if(amap[i]==AggregatesMap::ISOLATED) {
              // isolated vertex gets its own aggregate
              subdomains.push_back(Subdomain());
              aggregate=subdomains.size()-1;
            }
            overlapVisitor.setAggregate(aggregate);
            aggregateVisitor.setAggregate(aggregate);
            subdomains[aggregate].insert(i);
            typename AggregatesMap::VertexList vlist;
            amap.template breadthFirstSearch<false,false>(i, aggregate, graph, vlist, aggregateVisitor,
                                                          overlapVisitor, visitedMap);
          }

        std::size_t minsize=10000;
        std::size_t maxsize=0;
        int sum=0;
        for(typename Vector::size_type i=0; i < subdomains.size(); ++i) {
          sum+=subdomains[i].size();
          minsize=std::min(minsize, subdomains[i].size());
          maxsize=std::max(maxsize, subdomains[i].size());
        }
        Dune::dinfo<<"Subdomain size: min="<<minsize<<" max="<<maxsize<<" avg="<<(sum/subdomains.size())
                   <<" no="<<subdomains.size()<<" isolated="<<isolated<<std::endl;



      }
      Vector subdomains;
    };


    template<class M, class X, class TM, class TS, class TA>
    struct ConstructionTraits<SeqOverlappingSchwarz<M,X,TM,TS,TA> >
    {
      typedef ConstructionArgs<SeqOverlappingSchwarz<M,X,TM,TS,TA> > Arguments;

      static inline SeqOverlappingSchwarz<M,X,TM,TS,TA>* construct(Arguments& args)
      {
        return new SeqOverlappingSchwarz<M,X,TM,TS,TA>(args.getMatrix(),
                                                       args.getSubDomains(),
                                                       args.getArgs().relaxationFactor,
                                                       args.getArgs().onthefly);
      }

      static void deconstruct(SeqOverlappingSchwarz<M,X,TM,TS,TA>* schwarz)
      {
        delete schwarz;
      }
    };


  } // namespace Amg
} // namespace Dune



#endif