| 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 |
#include "anisotropic.hh" |
| 6 |
#include <dune/common/timer.hh> |
| 7 |
#include <dune/common/parallel/indexset.hh> |
| 8 |
#include <dune/common/parallel/communication.hh> |
| 9 |
#include <dune/istl/paamg/kamg.hh> |
| 10 |
#include <dune/istl/paamg/pinfo.hh> |
| 11 |
#include <cstdlib> |
| 12 |
#include <ctime> |
| 13 |
|
| 14 |
namespace Dune |
| 15 |
{ |
| 16 |
using Mat = BCRSMatrix<FieldMatrix<double,1,1>>; |
| 17 |
using Vec = BlockVector<FieldVector<double,1>>; |
| 18 |
using LinOp = MatrixAdapter<Mat,Vec,Vec>; |
| 19 |
using Comm = Amg::SequentialInformation; |
| 20 |
|
| 21 |
// explicit template instantiation of FastAMG preconditioner |
| 22 |
template class Amg::KAMG<LinOp, Vec, Richardson<Vec,Vec>, Comm, GeneralizedPCGSolver<Vec>>; |
| 23 |
template class Amg::KAMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm, GeneralizedPCGSolver<Vec>>; |
| 24 |
template class Amg::KAMG<LinOp, Vec, SeqSOR<Mat,Vec,Vec>, Comm, GeneralizedPCGSolver<Vec>>; |
| 25 |
template class Amg::KAMG<LinOp, Vec, SeqSSOR<Mat,Vec,Vec>, Comm, GeneralizedPCGSolver<Vec>>; |
| 26 |
|
| 27 |
template class Amg::KAMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm, RestartedFCGSolver<Vec>>; |
| 28 |
template class Amg::KAMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm, CompleteFCGSolver<Vec>>; |
| 29 |
template class Amg::KAMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm, BiCGSTABSolver<Vec>>; |
| 30 |
template class Amg::KAMG<LinOp, Vec, SeqJac<Mat,Vec,Vec>, Comm, MINRESSolver<Vec>>; |
| 31 |
|
| 32 |
/** |
| 33 |
* Note: RestartedGMResSolver can not be used, since additional constructor argument required. |
| 34 |
* Needs something like ConstructionTraits for solvers. |
| 35 |
**/ |
| 36 |
// template class Amg::KAMG<LinOp, Vec, SeqJac<Matrix,Vec,Vec>, Comm, RestartedGMResSolver<Vec>>; |
| 37 |
|
| 38 |
} // end namespace Dune |
| 39 |
|
| 40 |
|
| 41 |
template<class M, class V> |
| 42 |
void randomize(const M& mat, V& b) |
| 43 |
{ |
| 44 |
V x=b; |
| 45 |
|
| 46 |
srand((unsigned)std::clock()); |
| 47 |
|
| 48 |
typedef typename V::iterator iterator; |
| 49 |
for(iterator i=x.begin(); i != x.end(); ++i) |
| 50 |
*i=(rand() / (RAND_MAX + 1.0)); |
| 51 |
|
| 52 |
mat.mv(static_cast<const V&>(x), b); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
template <int BS> |
| 57 |
void testAMG(int N, int coarsenTarget, int ml) |
| 58 |
{ |
| 59 |
|
| 60 |
std::cout<<"N="<<N<<" coarsenTarget="<<coarsenTarget<<" maxlevel="<<ml<<std::endl; |
| 61 |
|
| 62 |
|
| 63 |
typedef Dune::ParallelIndexSet<int,LocalIndex,512> ParallelIndexSet; |
| 64 |
|
| 65 |
ParallelIndexSet indices; |
| 66 |
typedef Dune::FieldMatrix<double,BS,BS> MatrixBlock; |
| 67 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 68 |
typedef Dune::FieldVector<double,BS> VectorBlock; |
| 69 |
typedef Dune::BlockVector<VectorBlock> Vector; |
| 70 |
typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator; |
| 71 |
typedef Dune::Communication<void*> Comm; |
| 72 |
int n; |
| 73 |
|
| 74 |
Comm c; |
| 75 |
BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, c, &n, .001); |
| 76 |
|
| 77 |
Vector b(mat.N()), x(mat.M()); |
| 78 |
|
| 79 |
b=0; |
| 80 |
x=100; |
| 81 |
|
| 82 |
setBoundary(x, b, N); |
| 83 |
|
| 84 |
x=0; |
| 85 |
randomize(mat, b); |
| 86 |
|
| 87 |
if(N<6) { |
| 88 |
Dune::printmatrix(std::cout, mat, "A", "row"); |
| 89 |
Dune::printvector(std::cout, x, "x", "row"); |
| 90 |
} |
| 91 |
|
| 92 |
Dune::Timer watch; |
| 93 |
|
| 94 |
watch.reset(); |
| 95 |
Operator fop(mat); |
| 96 |
|
| 97 |
typedef Dune::Amg::CoarsenCriterion<Dune::Amg::UnSymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> > |
| 98 |
Criterion; |
| 99 |
typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> Smoother; |
| 100 |
//typedef Dune::SeqSOR<BCRSMat,Vector,Vector> Smoother; |
| 101 |
//typedef Dune::SeqJac<BCRSMat,Vector,Vector> Smoother; |
| 102 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::MultiplicativeSchwarzMode> Smoother; |
| 103 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector,Dune::SymmetricMultiplicativeSchwarzMode> Smoother; |
| 104 |
//typedef Dune::SeqOverlappingSchwarz<BCRSMat,Vector> Smoother; |
| 105 |
typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs; |
| 106 |
|
| 107 |
SmootherArgs smootherArgs; |
| 108 |
|
| 109 |
smootherArgs.iterations = 1; |
| 110 |
|
| 111 |
//smootherArgs.overlap=SmootherArgs::vertex; |
| 112 |
//smootherArgs.overlap=SmootherArgs::none; |
| 113 |
//smootherArgs.overlap=SmootherArgs::aggregate; |
| 114 |
|
| 115 |
smootherArgs.relaxationFactor = 1; |
| 116 |
|
| 117 |
Criterion criterion(15,coarsenTarget); |
| 118 |
criterion.setDefaultValuesIsotropic(2); |
| 119 |
criterion.setAlpha(.67); |
| 120 |
criterion.setBeta(1.0e-4); |
| 121 |
criterion.setMaxLevel(ml); |
| 122 |
criterion.setSkipIsolated(false); |
| 123 |
// specify pre/post smoother steps |
| 124 |
criterion.setNoPreSmoothSteps(1); |
| 125 |
criterion.setNoPostSmoothSteps(1); |
| 126 |
|
| 127 |
Dune::SeqScalarProduct<Vector> sp; |
| 128 |
typedef Dune::Amg::KAMG<Operator,Vector,Smoother,Dune::Amg::SequentialInformation> AMG; |
| 129 |
|
| 130 |
AMG amg(fop, criterion, smootherArgs); |
| 131 |
|
| 132 |
|
| 133 |
double buildtime = watch.elapsed(); |
| 134 |
|
| 135 |
std::cout<<"Building hierarchy took "<<buildtime<<" seconds"<<std::endl; |
| 136 |
|
| 137 |
//Dune::BiCGSTABSolver<Vector> amgCG(fop,amg,1e-6,80,2); |
| 138 |
Dune::CGSolver<Vector> amgCG(fop,amg,1e-6,80,2); |
| 139 |
//Dune::LoopSolver<Vector> amgCG(fop, amg, 1e-4, 10000, 2); |
| 140 |
watch.reset(); |
| 141 |
Dune::InverseOperatorResult r; |
| 142 |
amgCG.apply(x,b,r); |
| 143 |
|
| 144 |
double solvetime = watch.elapsed(); |
| 145 |
|
| 146 |
std::cout<<"AMG solving took "<<solvetime<<" seconds"<<std::endl; |
| 147 |
|
| 148 |
std::cout<<"AMG building took "<<(buildtime/r.elapsed*r.iterations)<<" iterations"<<std::endl; |
| 149 |
std::cout<<"AMG building together with solving took "<<buildtime+solvetime<<std::endl; |
| 150 |
|
| 151 |
/* |
| 152 |
watch.reset(); |
| 153 |
cg.apply(x,b,r); |
| 154 |
|
| 155 |
std::cout<<"CG solving took "<<watch.elapsed()<<" seconds"<<std::endl; |
| 156 |
*/ |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
int main(int argc, char** argv) |
| 161 |
try |
| 162 |
{ |
| 163 |
int N=100; |
| 164 |
int coarsenTarget=1200; |
| 165 |
int ml=10; |
| 166 |
|
| 167 |
if(argc>1) |
| 168 |
N = atoi(argv[1]); |
| 169 |
|
| 170 |
if(argc>2) |
| 171 |
coarsenTarget = atoi(argv[2]); |
| 172 |
|
| 173 |
if(argc>3) |
| 174 |
ml = atoi(argv[3]); |
| 175 |
|
| 176 |
testAMG<1>(N, coarsenTarget, ml); |
| 177 |
testAMG<2>(N, coarsenTarget, ml); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
catch (std::exception &e) |
| 182 |
{ |
| 183 |
std::cout << "ERROR: " << e.what() << std::endl; |
| 184 |
return 1; |
| 185 |
} |
| 186 |
catch (...) |
| 187 |
{ |
| 188 |
std::cerr << "Dune reported an unknown error." << std::endl; |
| 189 |
exit(1); |
| 190 |
} |