| 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 <dune/istl/io.hh> |
| 7 |
#include <dune/istl/bvector.hh> |
| 8 |
#include <dune/istl/operators.hh> |
| 9 |
#include <dune/common/fmatrix.hh> |
| 10 |
#include <dune/common/fvector.hh> |
| 11 |
#include <dune/common/test/testsuite.hh> |
| 12 |
#include "laplacian.hh" |
| 13 |
#include <dune/common/timer.hh> |
| 14 |
#include <dune/common/sllist.hh> |
| 15 |
#include <dune/istl/overlappingschwarz.hh> |
| 16 |
#include <dune/istl/solvers.hh> |
| 17 |
#include<dune/istl/superlu.hh> |
| 18 |
#include<dune/istl/umfpack.hh> |
| 19 |
|
| 20 |
#include <iterator> |
| 21 |
|
| 22 |
template<class MatrixBlock, class VectorBlock> |
| 23 |
Dune::TestSuite test(int argc, char** argv) |
| 24 |
{ |
| 25 |
Dune::TestSuite suite; |
| 26 |
#if HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK |
| 27 |
int N=4; |
| 28 |
|
| 29 |
if(argc>1) |
| 30 |
N = atoi(argv[1]); |
| 31 |
std::cout<<"testing for N="<<N<<std::endl; |
| 32 |
|
| 33 |
|
| 34 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 35 |
typedef Dune::BlockVector<VectorBlock> BVector; |
| 36 |
typedef Dune::MatrixAdapter<BCRSMat,BVector,BVector> Operator; |
| 37 |
|
| 38 |
BCRSMat mat; |
| 39 |
Operator fop(mat); |
| 40 |
BVector b(N*N), x(N*N); |
| 41 |
|
| 42 |
setupLaplacian(mat,N); |
| 43 |
b=0; |
| 44 |
x=100; |
| 45 |
//setBoundary(x,b,N); |
| 46 |
/* |
| 47 |
for (BCRSMat::RowIterator row = mat.begin(); row != mat.end(); ++row) |
| 48 |
(*row)[row.index()]+=row.index(); |
| 49 |
*/ |
| 50 |
// create the subdomains |
| 51 |
int domainSize=2; |
| 52 |
if(argc>2) |
| 53 |
domainSize = atoi(argv[2]); |
| 54 |
int overlap = 0; |
| 55 |
|
| 56 |
int domainsPerDim=(N+domainSize-1)/domainSize; |
| 57 |
|
| 58 |
// set up the overlapping domains |
| 59 |
typedef Dune::SeqOverlappingSchwarz<BCRSMat,BVector> Schwarz; |
| 60 |
typedef typename Schwarz::subdomain_vector subdomain_vector; |
| 61 |
|
| 62 |
subdomain_vector domains(domainsPerDim*domainsPerDim); |
| 63 |
|
| 64 |
// set up the rowToDomain vector |
| 65 |
typedef typename Schwarz::rowtodomain_vector rowtodomain_vector; |
| 66 |
rowtodomain_vector rowToDomain(N*N); |
| 67 |
|
| 68 |
for(int j=0; j < N; ++j) |
| 69 |
for(int i=0; i < N; ++i) |
| 70 |
{ |
| 71 |
int xdomain = i/domainSize; |
| 72 |
int ydomain = j/domainSize; |
| 73 |
int mainDomain=ydomain*domainsPerDim+xdomain; |
| 74 |
int id=j*N+i; |
| 75 |
domains[mainDomain].insert(id); |
| 76 |
rowToDomain[id].push_back(mainDomain); |
| 77 |
|
| 78 |
// check left domain |
| 79 |
int domain = (i-overlap)/domainSize; |
| 80 |
int neighbourDomain=ydomain*domainsPerDim+domain; |
| 81 |
if(domain>=0 && domain<domainsPerDim && neighbourDomain!=mainDomain) |
| 82 |
{ |
| 83 |
domains[neighbourDomain].insert(id); |
| 84 |
rowToDomain[id].push_back(neighbourDomain); |
| 85 |
} |
| 86 |
|
| 87 |
//check right domain |
| 88 |
domain = (i+overlap)/domainSize; |
| 89 |
neighbourDomain=ydomain*domainsPerDim+domain; |
| 90 |
if(domain>=0 && domain<domainsPerDim && neighbourDomain!=mainDomain) |
| 91 |
{ |
| 92 |
domains[neighbourDomain].insert(id); |
| 93 |
rowToDomain[id].push_back(neighbourDomain); |
| 94 |
} |
| 95 |
|
| 96 |
// check lower domain |
| 97 |
domain = (j-overlap)/domainSize; |
| 98 |
neighbourDomain=domain*domainsPerDim+xdomain; |
| 99 |
if(domain>=0 && domain<domainsPerDim && neighbourDomain!=mainDomain) |
| 100 |
{ |
| 101 |
domains[neighbourDomain].insert(id); |
| 102 |
rowToDomain[id].push_back(neighbourDomain); |
| 103 |
} |
| 104 |
|
| 105 |
//check upper domain |
| 106 |
domain = (j+overlap)/domainSize; |
| 107 |
neighbourDomain=domain*domainsPerDim+xdomain; |
| 108 |
if(domain>=0 && domain<domainsPerDim && neighbourDomain!=mainDomain) |
| 109 |
{ |
| 110 |
domains[neighbourDomain].insert(id); |
| 111 |
rowToDomain[id].push_back(domain*domainsPerDim+xdomain); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
typedef typename subdomain_vector::const_iterator iterator; |
| 116 |
|
| 117 |
if(N<10) { |
| 118 |
int i=0; |
| 119 |
for(iterator iter=domains.begin(); iter != domains.end(); ++iter) { |
| 120 |
typedef typename std::iterator_traits<iterator>::value_type |
| 121 |
::const_iterator entry_iterator; |
| 122 |
std::cout<<"domain "<<i++<<":"; |
| 123 |
for(entry_iterator entry = iter->begin(); entry != iter->end(); ++entry) { |
| 124 |
std::cout<<" "<<*entry; |
| 125 |
} |
| 126 |
std::cout<<std::endl; |
| 127 |
} |
| 128 |
Dune::printmatrix(std::cout, mat, std::string("A"), std::string("A")); |
| 129 |
Dune::printvector(std::cout, b, std::string("B"), std::string("B")); |
| 130 |
Dune::printvector(std::cout, x, std::string("X"), std::string("X")); |
| 131 |
} |
| 132 |
|
| 133 |
Dune::Timer watch; |
| 134 |
|
| 135 |
watch.reset(); |
| 136 |
|
| 137 |
Dune::InverseOperatorResult res; |
| 138 |
|
| 139 |
std::cout<<"Additive Schwarz (domains vector)"<<std::endl; |
| 140 |
|
| 141 |
b=0; |
| 142 |
x=100; |
| 143 |
// setBoundary(x,b,N); |
| 144 |
#if HAVE_SUITESPARSE_UMFPACK |
| 145 |
std::cout << "Do testing with UMFPack" << std::endl; |
| 146 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode, |
| 147 |
Dune::UMFPack<BCRSMat> > prec0(mat, domains, 1); |
| 148 |
Dune::LoopSolver<BVector> solver0(fop, prec0, 1e-2,100,2); |
| 149 |
solver0.apply(x,b, res); |
| 150 |
suite.check(res.converged) << "solver0 did not converge"; |
| 151 |
|
| 152 |
b=0; |
| 153 |
x=100; |
| 154 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode, |
| 155 |
Dune::UMFPack<BCRSMat> > |
| 156 |
prec1(mat, domains, 1, false); |
| 157 |
Dune::LoopSolver<BVector> solver1(fop, prec1, 1e-2,100,2); |
| 158 |
solver1.apply(x,b, res); |
| 159 |
suite.check(res.converged) << "solver1 did not converge"; |
| 160 |
|
| 161 |
#endif // HAVE_SUITESPARSE_UMFPACK |
| 162 |
#if HAVE_SUPERLU |
| 163 |
std::cout << "Do testing with SuperLU" << std::endl; |
| 164 |
x=100; |
| 165 |
b=0; |
| 166 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode, |
| 167 |
Dune::SuperLU<BCRSMat> > slu_prec0(mat, domains, 1); |
| 168 |
Dune::LoopSolver<BVector> slu_solver(fop, slu_prec0, 1e-2,100,2); |
| 169 |
slu_solver.apply(x,b, res); |
| 170 |
suite.check(res.converged) << "slu_solver did not converge"; |
| 171 |
|
| 172 |
x=100; |
| 173 |
b=0; |
| 174 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode, |
| 175 |
Dune::SuperLU<BCRSMat> > slu_prec1(mat, domains, 1, false); |
| 176 |
Dune::LoopSolver<BVector> slu_solver1(fop, slu_prec1, 1e-2,100,2); |
| 177 |
slu_solver1.apply(x,b, res); |
| 178 |
suite.check(res.converged) << "slu_solver1 did not converge"; |
| 179 |
|
| 180 |
#endif |
| 181 |
x=100; |
| 182 |
b=0; |
| 183 |
|
| 184 |
std::cout << "Do testing with DynamicMatrixSubdomainSolver" << std::endl; |
| 185 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode, |
| 186 |
Dune::DynamicMatrixSubdomainSolver<BCRSMat,BVector,BVector> > dyn_prec0(mat, domains, 1); |
| 187 |
Dune::LoopSolver<BVector> dyn_solver(fop, dyn_prec0, 1e-2,100,2); |
| 188 |
dyn_solver.apply(x,b, res); |
| 189 |
suite.check(res.converged) << "dyn_solver did not converge"; |
| 190 |
|
| 191 |
std::cout<<"Additive Schwarz not on the fly (domains vector)"<<std::endl; |
| 192 |
|
| 193 |
b=0; |
| 194 |
x=100; |
| 195 |
// setBoundary(x,b,N); |
| 196 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::AdditiveSchwarzMode> prec0o(mat, domains, 1, false); |
| 197 |
Dune::LoopSolver<BVector> solver0o(fop, prec0o, 1e-2,100,2); |
| 198 |
solver0o.apply(x,b, res); |
| 199 |
suite.check(res.converged) << "solver0o did not converge"; |
| 200 |
|
| 201 |
std::cout << "Multiplicative Schwarz (domains vector)"<<std::endl; |
| 202 |
|
| 203 |
b=0; |
| 204 |
x=100; |
| 205 |
//setBoundary(x,b,N); |
| 206 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::MultiplicativeSchwarzMode> prec1m(mat, domains, 1); |
| 207 |
Dune::LoopSolver<BVector> solver1m(fop, prec1m, 1e-2,100,2); |
| 208 |
solver1m.apply(x,b, res); |
| 209 |
suite.check(res.converged) << "solver1m did not converge"; |
| 210 |
|
| 211 |
std::cout<<"Additive Schwarz (rowToDomain vector)"<<std::endl; |
| 212 |
|
| 213 |
b=0; |
| 214 |
x=100; |
| 215 |
// setBoundary(x,b,N); |
| 216 |
if(N<10) { |
| 217 |
typedef typename rowtodomain_vector::const_iterator rt_iter; |
| 218 |
int row=0; |
| 219 |
std::cout<<" row to domain"<<std::endl; |
| 220 |
for(rt_iter i= rowToDomain.begin(); i!= rowToDomain.end(); ++i, ++row) { |
| 221 |
std::cout<<"row="<<row<<": "; |
| 222 |
typedef typename rowtodomain_vector::value_type::const_iterator diter; |
| 223 |
for(diter d=i->begin(); d!=i->end(); ++d) |
| 224 |
std::cout<<*d<<" "; |
| 225 |
std::cout<<std::endl; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector> prec2(mat, rowToDomain, 1); |
| 230 |
Dune::LoopSolver<BVector> solver2(fop, prec2, 1e-2,100,2); |
| 231 |
solver2.apply(x,b, res); |
| 232 |
suite.check(res.converged) << "solver2 did not converge"; |
| 233 |
|
| 234 |
std::cout << "Multiplicative Schwarz (rowToDomain vector)"<<std::endl; |
| 235 |
|
| 236 |
b=0; |
| 237 |
x=100; |
| 238 |
//setBoundary(x,b,N); |
| 239 |
Dune::SeqOverlappingSchwarz<BCRSMat,BVector,Dune::MultiplicativeSchwarzMode> prec3(mat, rowToDomain, 1); |
| 240 |
Dune::LoopSolver<BVector> solver3(fop, prec3, 1e-2,100,2); |
| 241 |
solver3.apply(x,b, res); |
| 242 |
suite.check(res.converged) << "solver3 did not converge"; |
| 243 |
|
| 244 |
std::cout << "SOR"<<std::endl; |
| 245 |
|
| 246 |
b=0; |
| 247 |
x=100; |
| 248 |
//setBoundary(x,b,N); |
| 249 |
Dune::SeqSOR<BCRSMat,BVector,BVector> sor(mat, 1,1); |
| 250 |
Dune::LoopSolver<BVector> solver4(fop, sor, 1e-2,100,2); |
| 251 |
solver4.apply(x,b, res); |
| 252 |
suite.check(res.converged) << "solver4 did not converge"; |
| 253 |
|
| 254 |
#endif // HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK |
| 255 |
return suite; |
| 256 |
} |
| 257 |
|
| 258 |
int main(int argc, char** argv){ |
| 259 |
Dune::TestSuite suite; |
| 260 |
suite.subTest(test<Dune::FieldMatrix<double, 1, 1>, Dune::FieldVector<double, 1>>(argc, argv)); |
| 261 |
suite.subTest(test<double, double>(argc, argv)); |
| 262 |
return suite.exit(); |
| 263 |
} |