| 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"xreal.h" |
| 7 |
|
| 8 |
namespace std |
| 9 |
{ |
| 10 |
|
| 11 |
HPA::xreal abs(const HPA::xreal& t) |
| 12 |
{ |
| 13 |
return t>=0?t:-t; |
| 14 |
} |
| 15 |
|
| 16 |
}; |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "anisotropic.hh" |
| 20 |
#include <dune/common/timer.hh> |
| 21 |
#include <dune/common/parallel/indexset.hh> |
| 22 |
#include <dune/common/parallel/communication.hh> |
| 23 |
#include <dune/istl/paamg/amg.hh> |
| 24 |
#include <dune/istl/paamg/pinfo.hh> |
| 25 |
#include <dune/istl/solvers.hh> |
| 26 |
#include <cstdlib> |
| 27 |
#include <ctime> |
| 28 |
#include <complex> |
| 29 |
|
| 30 |
typedef double XREAL; |
| 31 |
// typedef std::complex<double> XREAL; |
| 32 |
|
| 33 |
/* |
| 34 |
typedef HPA::xreal XREAL; |
| 35 |
|
| 36 |
namespace Dune |
| 37 |
{ |
| 38 |
template<> |
| 39 |
struct DoubleConverter<HPA::xreal> |
| 40 |
{ |
| 41 |
static double toDouble(const HPA::xreal& t) |
| 42 |
{ |
| 43 |
return t._2double(); |
| 44 |
} |
| 45 |
}; |
| 46 |
} |
| 47 |
*/ |
| 48 |
|
| 49 |
namespace Dune |
| 50 |
{ |
| 51 |
using Mat = BCRSMatrix<FieldMatrix<double,1,1>>; |
| 52 |
using Vec = BlockVector<FieldVector<double,1>>; |
| 53 |
using LinOp = MatrixAdapter<Mat,Vec,Vec>; |
| 54 |
using Comm = Amg::SequentialInformation; |
| 55 |
|
| 56 |
// explicit template instantiation of FastAMG preconditioner |
| 57 |
template class Amg::AMG<LinOp, Vec, Richardson<Vec,Vec>, Comm>; |
| 58 |
template class Amg::AMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm>; |
| 59 |
template class Amg::AMG<LinOp, Vec, SeqSOR<Mat,Vec,Vec>, Comm>; |
| 60 |
template class Amg::AMG<LinOp, Vec, SeqSSOR<Mat,Vec,Vec>, Comm>; |
| 61 |
|
| 62 |
} // end namespace Dune |
| 63 |
|
| 64 |
|
| 65 |
template<class M, class V> |
| 66 |
void randomize(const M& mat, V& b) |
| 67 |
{ |
| 68 |
V x=b; |
| 69 |
|
| 70 |
srand((unsigned)std::clock()); |
| 71 |
|
| 72 |
typedef typename V::iterator iterator; |
| 73 |
for(iterator i=x.begin(); i != x.end(); ++i) |
| 74 |
*i=(rand() / (RAND_MAX + 1.0)); |
| 75 |
|
| 76 |
mat.mv(static_cast<const V&>(x), b); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
template <class Matrix, class Vector> |
| 81 |
Dune::InverseOperatorResult testAMG(int N, int coarsenTarget, int ml, int gamma = 1) |
| 82 |
{ |
| 83 |
|
| 84 |
std::cout<<"N="<<N<<" coarsenTarget="<<coarsenTarget<<" maxlevel="<<ml<<std::endl; |
| 85 |
|
| 86 |
|
| 87 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 88 |
|
| 89 |
ParallelIndexSet indices; |
| 90 |
typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Operator; |
| 91 |
typedef Dune::Communication<void*> Comm; |
| 92 |
int n; |
| 93 |
|
| 94 |
Comm c; |
| 95 |
Matrix mat = setupAnisotropic2d<typename Matrix::block_type>(N, indices, c, &n, 1); |
| 96 |
|
| 97 |
Vector b(mat.N()), x(mat.M()); |
| 98 |
|
| 99 |
b=0; |
| 100 |
x=100; |
| 101 |
|
| 102 |
setBoundary(x, b, N); |
| 103 |
|
| 104 |
x=0; |
| 105 |
randomize(mat, b); |
| 106 |
|
| 107 |
if(N<6) { |
| 108 |
Dune::printmatrix(std::cout, mat, "A", "row"); |
| 109 |
Dune::printvector(std::cout, x, "x", "row"); |
| 110 |
} |
| 111 |
|
| 112 |
Dune::Timer watch; |
| 113 |
|
| 114 |
watch.reset(); |
| 115 |
Operator fop(mat); |
| 116 |
|
| 117 |
typedef typename std::conditional< std::is_convertible<XREAL, typename Dune::FieldTraits<XREAL>::real_type>::value, |
| 118 |
Dune::Amg::FirstDiagonal, Dune::Amg::RowSum >::type Norm; |
| 119 |
typedef Dune::Amg::CoarsenCriterion<Dune::Amg::UnSymmetricCriterion<Matrix,Norm> > |
| 120 |
Criterion; |
| 121 |
typedef Dune::SeqSSOR<Matrix,Vector,Vector> Smoother; |
| 122 |
//typedef Dune::SeqSOR<BCRSMat,Vector,Vector> Smoother; |
| 123 |
//typedef Dune::SeqJac<BCRSMat,Vector,Vector> Smoother; |
| 124 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::MultiplicativeSchwarzMode> Smoother; |
| 125 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::SymmetricMultiplicativeSchwarzMode> Smoother; |
| 126 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector> Smoother; |
| 127 |
typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs; |
| 128 |
|
| 129 |
SmootherArgs smootherArgs; |
| 130 |
|
| 131 |
smootherArgs.iterations = 1; |
| 132 |
|
| 133 |
//smootherArgs.overlap=SmootherArgs::vertex; |
| 134 |
//smootherArgs.overlap=SmootherArgs::none; |
| 135 |
//smootherArgs.overlap=SmootherArgs::aggregate; |
| 136 |
|
| 137 |
smootherArgs.relaxationFactor = 1; |
| 138 |
|
| 139 |
Criterion criterion(15,coarsenTarget); |
| 140 |
criterion.setDefaultValuesIsotropic(2); |
| 141 |
criterion.setAlpha(.67); |
| 142 |
criterion.setBeta(1.0e-4); |
| 143 |
criterion.setGamma(gamma); |
| 144 |
criterion.setMaxLevel(ml); |
| 145 |
criterion.setSkipIsolated(false); |
| 146 |
// specify pre/post smoother steps |
| 147 |
criterion.setNoPreSmoothSteps(1); |
| 148 |
criterion.setNoPostSmoothSteps(1); |
| 149 |
|
| 150 |
Dune::SeqScalarProduct<Vector> sp; |
| 151 |
typedef Dune::Amg::AMG<Operator,Vector,Smoother> AMG; |
| 152 |
|
| 153 |
Smoother smoother(mat,1,1); |
| 154 |
|
| 155 |
AMG amg(fop, criterion, smootherArgs); |
| 156 |
|
| 157 |
|
| 158 |
double buildtime = watch.elapsed(); |
| 159 |
|
| 160 |
std::cout<<"Building hierarchy took "<<buildtime<<" seconds"<<std::endl; |
| 161 |
|
| 162 |
Dune::GeneralizedPCGSolver<Vector> amgCG(fop,amg,1e-6,80,2); |
| 163 |
//Dune::LoopSolver<Vector> amgCG(fop, amg, 1e-4, 10000, 2); |
| 164 |
watch.reset(); |
| 165 |
Dune::InverseOperatorResult r; |
| 166 |
amgCG.apply(x,b,r); |
| 167 |
|
| 168 |
XREAL solvetime = watch.elapsed(); |
| 169 |
|
| 170 |
std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl; |
| 171 |
|
| 172 |
std::cout<<"AMG building took the same time as "<<(buildtime/r.elapsed*r.iterations)<<" iterations"<<std::endl; |
| 173 |
std::cout<<"AMG building together with solving took "<<buildtime+solvetime<<std::endl; |
| 174 |
|
| 175 |
/* |
| 176 |
watch.reset(); |
| 177 |
cg.apply(x,b,r); |
| 178 |
|
| 179 |
std::cout<<"CG solving took "<<watch.elapsed()<<" seconds"<<std::endl; |
| 180 |
*/ |
| 181 |
return r; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
int main(int argc, char** argv) |
| 186 |
try |
| 187 |
{ |
| 188 |
int N=100; |
| 189 |
int coarsenTarget=1200; |
| 190 |
int ml=10; |
| 191 |
|
| 192 |
if(argc>1) |
| 193 |
N = atoi(argv[1]); |
| 194 |
|
| 195 |
if(argc>2) |
| 196 |
coarsenTarget = atoi(argv[2]); |
| 197 |
|
| 198 |
if(argc>3) |
| 199 |
ml = atoi(argv[3]); |
| 200 |
|
| 201 |
Dune::InverseOperatorResult gamma1_res; |
| 202 |
for(int gamma = 1; gamma<=2;++gamma){ |
| 203 |
{ |
| 204 |
using Matrix = Dune::BCRSMatrix<XREAL>; |
| 205 |
using Vector = Dune::BlockVector<XREAL>; |
| 206 |
|
| 207 |
Dune::InverseOperatorResult res = testAMG<Matrix,Vector>(N, coarsenTarget, ml, gamma); |
| 208 |
if(gamma==1){ |
| 209 |
gamma1_res = res; |
| 210 |
}else{ |
| 211 |
assert(res.conv_rate < gamma1_res.conv_rate); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
{ |
| 217 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<XREAL,1,1> >; |
| 218 |
using Vector = Dune::BlockVector<Dune::FieldVector<XREAL,1> >; |
| 219 |
|
| 220 |
testAMG<Matrix,Vector>(N, coarsenTarget, ml); |
| 221 |
} |
| 222 |
|
| 223 |
{ |
| 224 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<XREAL,2,2> >; |
| 225 |
using Vector = Dune::BlockVector<Dune::FieldVector<XREAL,2> >; |
| 226 |
|
| 227 |
testAMG<Matrix,Vector>(N, coarsenTarget, ml); |
| 228 |
} |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
catch (std::exception &e) |
| 233 |
{ |
| 234 |
std::cout << "ERROR: " << e.what() << std::endl; |
| 235 |
return 1; |
| 236 |
} |
| 237 |
catch (...) |
| 238 |
{ |
| 239 |
std::cerr << "Dune reported an unknown error." << std::endl; |
| 240 |
exit(1); |
| 241 |
} |