| 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 |
|
| 6 |
#include "anisotropic.hh" |
| 7 |
#include <dune/common/timer.hh> |
| 8 |
#include <dune/common/parallel/indexset.hh> |
| 9 |
#include <dune/common/parallel/communication.hh> |
| 10 |
#include <dune/istl/paamg/fastamg.hh> |
| 11 |
#include <dune/istl/paamg/pinfo.hh> |
| 12 |
#include <dune/istl/solvers.hh> |
| 13 |
#include <cstdlib> |
| 14 |
#include <ctime> |
| 15 |
|
| 16 |
namespace Dune |
| 17 |
{ |
| 18 |
using Mat = BCRSMatrix<FieldMatrix<double,1,1>>; |
| 19 |
using Vec = BlockVector<FieldVector<double,1>>; |
| 20 |
|
| 21 |
// explicit template instantiation of FastAMG preconditioner |
| 22 |
template class Amg::FastAMG<MatrixAdapter<Mat,Vec,Vec>, Vec, Amg::SequentialInformation>; |
| 23 |
|
| 24 |
} // end namespace Dune |
| 25 |
|
| 26 |
|
| 27 |
template<class M, class V> |
| 28 |
void randomize(const M& mat, V& b) |
| 29 |
{ |
| 30 |
V x=b; |
| 31 |
|
| 32 |
srand(20030317); |
| 33 |
|
| 34 |
typedef typename V::iterator iterator; |
| 35 |
for(iterator i=x.begin(); i != x.end(); ++i) |
| 36 |
*i=(rand() / (RAND_MAX + 1.0)); |
| 37 |
|
| 38 |
mat.mv(static_cast<const V&>(x), b); |
| 39 |
} |
| 40 |
|
| 41 |
template <class MatrixBlock, class VectorBlock> |
| 42 |
void testAMG(int N, int coarsenTarget, int ml) |
| 43 |
{ |
| 44 |
std::cout<<"N="<<N<<" coarsenTarget="<<coarsenTarget<<" maxlevel="<<ml<<std::endl; |
| 45 |
|
| 46 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 47 |
|
| 48 |
ParallelIndexSet indices; |
| 49 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 50 |
typedef Dune::BlockVector<VectorBlock> Vector; |
| 51 |
typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator; |
| 52 |
typedef Dune::Communication<void*> Comm; |
| 53 |
int n; |
| 54 |
|
| 55 |
Comm c; |
| 56 |
BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, c, &n, 1); |
| 57 |
|
| 58 |
Vector b(mat.N()), x(mat.M()); |
| 59 |
|
| 60 |
b=0; |
| 61 |
x=100; |
| 62 |
|
| 63 |
setBoundary(x, b, N); |
| 64 |
|
| 65 |
x=0; |
| 66 |
randomize(mat, b); |
| 67 |
|
| 68 |
if(N<6) { |
| 69 |
Dune::printmatrix(std::cout, mat, "A", "row"); |
| 70 |
Dune::printvector(std::cout, x, "x", "row"); |
| 71 |
} |
| 72 |
|
| 73 |
Dune::Timer watch; |
| 74 |
|
| 75 |
watch.reset(); |
| 76 |
Operator fop(mat); |
| 77 |
|
| 78 |
typedef Dune::Amg::AggregationCriterion<Dune::Amg::SymmetricMatrixDependency<BCRSMat,Dune::Amg::FirstDiagonal> > CriterionBase; |
| 79 |
typedef Dune::Amg::CoarsenCriterion<CriterionBase> Criterion; |
| 80 |
|
| 81 |
Criterion criterion(15,coarsenTarget); |
| 82 |
criterion.setDefaultValuesIsotropic(2); |
| 83 |
criterion.setAlpha(.67); |
| 84 |
criterion.setBeta(1.0e-4); |
| 85 |
criterion.setMaxLevel(ml); |
| 86 |
criterion.setSkipIsolated(false); |
| 87 |
|
| 88 |
typedef Dune::Amg::FastAMG<Operator,Vector> AMG; |
| 89 |
Dune::Amg::Parameters params; |
| 90 |
|
| 91 |
AMG amg(fop, criterion, params); |
| 92 |
|
| 93 |
// check if recalculation of matrix hierarchy works |
| 94 |
amg.recalculateHierarchy(); |
| 95 |
|
| 96 |
double buildtime = watch.elapsed(); |
| 97 |
|
| 98 |
std::cout<<"Building hierarchy took "<<buildtime<<" seconds"<<std::endl; |
| 99 |
|
| 100 |
Dune::GeneralizedPCGSolver<Vector> amgCG(fop,amg,1e-6,80,2); |
| 101 |
//Dune::LoopSolver<Vector> amgCG(fop, amg, 1e-4, 10000, 2); |
| 102 |
watch.reset(); |
| 103 |
Dune::InverseOperatorResult r; |
| 104 |
amgCG.apply(x,b,r); |
| 105 |
|
| 106 |
double solvetime = watch.elapsed(); |
| 107 |
|
| 108 |
std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl; |
| 109 |
|
| 110 |
std::cout<<"AMG building took "<<(buildtime/r.elapsed*r.iterations)<<" iterations"<<std::endl; |
| 111 |
std::cout<<"AMG building together with solving took "<<buildtime+solvetime<<std::endl; |
| 112 |
|
| 113 |
/* |
| 114 |
watch.reset(); |
| 115 |
cg.apply(x,b,r); |
| 116 |
|
| 117 |
std::cout<<"CG solving took "<<watch.elapsed()<<" seconds"<<std::endl; |
| 118 |
*/ |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
int main(int argc, char** argv) |
| 123 |
try |
| 124 |
{ |
| 125 |
int N=100; |
| 126 |
int coarsenTarget=1200; |
| 127 |
int ml=10; |
| 128 |
|
| 129 |
if(argc>1) |
| 130 |
N = atoi(argv[1]); |
| 131 |
|
| 132 |
if(argc>2) |
| 133 |
coarsenTarget = atoi(argv[2]); |
| 134 |
|
| 135 |
if(argc>3) |
| 136 |
ml = atoi(argv[3]); |
| 137 |
|
| 138 |
{ |
| 139 |
using MB = double; |
| 140 |
using VB = double; |
| 141 |
testAMG<MB, VB>(N, coarsenTarget, ml); |
| 142 |
} |
| 143 |
|
| 144 |
{ |
| 145 |
using MB = Dune::FieldMatrix<double,1,1>; |
| 146 |
using VB = Dune::FieldVector<double,1>; |
| 147 |
testAMG<MB, VB>(N, coarsenTarget, ml); |
| 148 |
} |
| 149 |
|
| 150 |
{ |
| 151 |
using MB = Dune::FieldMatrix<double,2,2>; |
| 152 |
using VB = Dune::FieldVector<double,2>; |
| 153 |
testAMG<MB, VB>(N, coarsenTarget, ml); |
| 154 |
} |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
catch (std::exception &e) |
| 159 |
{ |
| 160 |
std::cout << "ERROR: " << e.what() << std::endl; |
| 161 |
return 1; |
| 162 |
} |
| 163 |
catch (...) |
| 164 |
{ |
| 165 |
std::cerr << "Dune reported an unknown error." << std::endl; |
| 166 |
exit(1); |
| 167 |
} |