| 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 |
#include "anisotropic.hh" |
| 4 |
#include <dune/common/timer.hh> |
| 5 |
#include <dune/common/shared_ptr.hh> |
| 6 |
#include <dune/common/parallel/indexset.hh> |
| 7 |
#include <dune/common/parallel/communication.hh> |
| 8 |
#include <dune/istl/paamg/twolevelmethod.hh> |
| 9 |
#include <dune/istl/overlappingschwarz.hh> |
| 10 |
#include <dune/istl/paamg/pinfo.hh> |
| 11 |
#include <dune/istl/solvers.hh> |
| 12 |
|
| 13 |
template<class M, class V> |
| 14 |
void randomize(const M& mat, V& b) |
| 15 |
{ |
| 16 |
V x=b; |
| 17 |
|
| 18 |
srand((unsigned)std::clock()); |
| 19 |
|
| 20 |
typedef typename V::iterator iterator; |
| 21 |
for(iterator i=x.begin(); i != x.end(); ++i) |
| 22 |
*i=(rand() / (RAND_MAX + 1.0)); |
| 23 |
|
| 24 |
mat.mv(static_cast<const V&>(x), b); |
| 25 |
} |
| 26 |
|
| 27 |
void testTwoLevelMethod() |
| 28 |
{ |
| 29 |
const int BS=1; |
| 30 |
int N=100; |
| 31 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 32 |
ParallelIndexSet indices; |
| 33 |
typedef Dune::FieldMatrix<double,BS,BS> MatrixBlock; |
| 34 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 35 |
typedef Dune::FieldVector<double,BS> VectorBlock; |
| 36 |
typedef Dune::BlockVector<VectorBlock> Vector; |
| 37 |
typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator; |
| 38 |
typedef Dune::Communication<void*> Comm; |
| 39 |
Comm c; |
| 40 |
int n; |
| 41 |
BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, c, &n, 1); |
| 42 |
Vector b(mat.N()), x(mat.M()); |
| 43 |
x=0; |
| 44 |
randomize(mat, b); |
| 45 |
#ifndef USE_OVERLAPPINGSCHWARZ |
| 46 |
// Smoother used for the finest level |
| 47 |
typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> FSmoother; |
| 48 |
FSmoother fineSmoother(mat,1,1.0); |
| 49 |
#else |
| 50 |
// Smoother used for the finest level. There is an additional |
| 51 |
// template parameter to provide the subdomain solver. |
| 52 |
typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector, |
| 53 |
Dune::SymmetricMultiplicativeSchwarzMode> FSmoother; |
| 54 |
// Type of the subdomain vector |
| 55 |
typedef FSmoother::subdomain_vector SubdomainVector; |
| 56 |
// Create subdomain. |
| 57 |
std::size_t stride=2; |
| 58 |
SubdomainVector subdomains((((N-1)/stride)+1)*(((N-1)/stride)+1)); |
| 59 |
|
| 60 |
for(int i=0; i<N; ++i) |
| 61 |
for(int j=0; j<N; ++j) |
| 62 |
{ |
| 63 |
int index=i/stride*(((N-1)/stride)+1)+j/stride; |
| 64 |
subdomains[index].insert(i*N+j); |
| 65 |
} |
| 66 |
//create smoother |
| 67 |
FSmoother fineSmoother(mat,subdomains, 1.0, false); |
| 68 |
#endif |
| 69 |
typedef Dune::SeqJac<BCRSMat,Vector,Vector> CSmoother; |
| 70 |
typedef Dune::Amg::SmootherTraits<CSmoother>::Arguments SmootherArgs; |
| 71 |
typedef Dune::Amg::CoarsenCriterion< |
| 72 |
Dune::Amg::UnSymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> > Criterion; |
| 73 |
typedef Dune::Amg::AggregationLevelTransferPolicy<Operator,Criterion> TransferPolicy; |
| 74 |
typedef Dune::Amg::OneStepAMGCoarseSolverPolicy<Operator,CSmoother, Criterion> CoarsePolicy; // Policy for coarse solver creation |
| 75 |
Criterion crit; |
| 76 |
CoarsePolicy coarsePolicy=CoarsePolicy(SmootherArgs(), crit); |
| 77 |
TransferPolicy transferPolicy(crit); |
| 78 |
Operator fop(mat); |
| 79 |
Dune::Amg::TwoLevelMethod<Operator,CoarsePolicy,FSmoother> preconditioner(fop, |
| 80 |
Dune::stackobject_to_shared_ptr(fineSmoother), |
| 81 |
transferPolicy, |
| 82 |
coarsePolicy); |
| 83 |
Dune::GeneralizedPCGSolver<Vector> amgCG(fop,preconditioner,.8,80,2); |
| 84 |
Dune::Amg::TwoLevelMethod<Operator,CoarsePolicy,FSmoother> preconditioner1(preconditioner); |
| 85 |
Dune::InverseOperatorResult res; |
| 86 |
amgCG.apply(x,b,res); |
| 87 |
} |
| 88 |
|
| 89 |
int main() |
| 90 |
{ |
| 91 |
testTwoLevelMethod(); |
| 92 |
return 0; |
| 93 |
} |