| 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 <complex> |
| 7 |
|
| 8 |
#include <dune/common/fmatrix.hh> |
| 9 |
#include <dune/common/fvector.hh> |
| 10 |
#include <dune/common/timer.hh> |
| 11 |
|
| 12 |
#include <dune/istl/bvector.hh> |
| 13 |
#include <dune/istl/operators.hh> |
| 14 |
#include <dune/istl/superlu.hh> |
| 15 |
|
| 16 |
#include "laplacian.hh" |
| 17 |
|
| 18 |
#if HAVE_SUPERLU |
| 19 |
template<typename Matrix, typename Vector> |
| 20 |
void runSuperLU(std::size_t N) |
| 21 |
{ |
| 22 |
using Operator = Dune::MatrixAdapter<Matrix,Vector,Vector>; |
| 23 |
|
| 24 |
Matrix mat; |
| 25 |
Operator fop(mat); |
| 26 |
Vector b(N*N), x(N*N), b1(N/2), x1(N/2); |
| 27 |
|
| 28 |
setupLaplacian(mat,N); |
| 29 |
b=1; |
| 30 |
b1=1; |
| 31 |
x=0; |
| 32 |
x1=0; |
| 33 |
|
| 34 |
Dune::Timer watch; |
| 35 |
|
| 36 |
watch.reset(); |
| 37 |
|
| 38 |
Dune::SuperLU<Matrix> solver(mat, true); |
| 39 |
|
| 40 |
Dune::InverseOperatorResult res; |
| 41 |
|
| 42 |
Dune::SuperLU<Matrix> solver1; |
| 43 |
|
| 44 |
solver.setVerbosity(true); |
| 45 |
solver.apply(x,b, res); |
| 46 |
|
| 47 |
std::set<typename Matrix::size_type> mrs; |
| 48 |
for(typename Matrix::size_type s=0; s < N/2; ++s) |
| 49 |
mrs.insert(s); |
| 50 |
|
| 51 |
solver1.setSubMatrix(mat,mrs); |
| 52 |
|
| 53 |
solver1.apply(x1,b1, res); |
| 54 |
solver1.apply(reinterpret_cast<typename Matrix::field_type*>(&x1[0]), |
| 55 |
reinterpret_cast<typename Matrix::field_type*>(&b1[0])); |
| 56 |
} |
| 57 |
|
| 58 |
// explicit template instantiation of SuperLU class |
| 59 |
|
| 60 |
#if __has_include("slu_sdefs.h") |
| 61 |
template class Dune::SuperLU<Dune::BCRSMatrix<float>>; |
| 62 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<float,1,1> >>; |
| 63 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<float,2,2> >>; |
| 64 |
#endif |
| 65 |
|
| 66 |
#if __has_include("slu_ddefs.h") |
| 67 |
template class Dune::SuperLU<Dune::BCRSMatrix<double>>; |
| 68 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> >>; |
| 69 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<double,2,2> >>; |
| 70 |
#endif |
| 71 |
|
| 72 |
#if __has_include("slu_cdefs.h") |
| 73 |
template class Dune::SuperLU<Dune::BCRSMatrix<std::complex<float> >>; |
| 74 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<float>,1,1> >>; |
| 75 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<float>,2,2> >>; |
| 76 |
#endif |
| 77 |
|
| 78 |
#if __has_include("slu_zdefs.h") |
| 79 |
template class Dune::SuperLU<Dune::BCRSMatrix<std::complex<double> >>; |
| 80 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<double>,1,1> >>; |
| 81 |
template class Dune::SuperLU<Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<double>,2,2> >>; |
| 82 |
#endif |
| 83 |
|
| 84 |
#endif |
| 85 |
|
| 86 |
|
| 87 |
int main(int argc, char** argv) |
| 88 |
try |
| 89 |
{ |
| 90 |
#if HAVE_SUPERLU |
| 91 |
std::size_t N=100; |
| 92 |
|
| 93 |
if(argc>1) |
| 94 |
N = atoi(argv[1]); |
| 95 |
|
| 96 |
// ------------------------------------------------------------------------------ |
| 97 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<T>"<<std::endl; |
| 98 |
#if __has_include("slu_sdefs.h") |
| 99 |
{ |
| 100 |
using Matrix = Dune::BCRSMatrix<float>; |
| 101 |
using Vector = Dune::BlockVector<float>; |
| 102 |
runSuperLU<Matrix,Vector>(N); |
| 103 |
} |
| 104 |
#endif |
| 105 |
#if __has_include("slu_ddefs.h") |
| 106 |
{ |
| 107 |
using Matrix = Dune::BCRSMatrix<double>; |
| 108 |
using Vector = Dune::BlockVector<double>; |
| 109 |
runSuperLU<Matrix,Vector>(N); |
| 110 |
} |
| 111 |
#endif |
| 112 |
#if __has_include("slu_cdefs.h") |
| 113 |
{ |
| 114 |
using Matrix = Dune::BCRSMatrix<std::complex<float> >; |
| 115 |
using Vector = Dune::BlockVector<std::complex<float> >; |
| 116 |
runSuperLU<Matrix,Vector>(N); |
| 117 |
} |
| 118 |
#endif |
| 119 |
#if __has_include("slu_zdefs.h") |
| 120 |
{ |
| 121 |
using Matrix = Dune::BCRSMatrix<std::complex<double> >; |
| 122 |
using Vector = Dune::BlockVector<std::complex<double> >; |
| 123 |
runSuperLU<Matrix,Vector>(N); |
| 124 |
} |
| 125 |
#endif |
| 126 |
|
| 127 |
// ------------------------------------------------------------------------------ |
| 128 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<FieldMatrix<T,1,1> >"<<std::endl; |
| 129 |
#if __has_include("slu_sdefs.h") |
| 130 |
{ |
| 131 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<float,1,1> >; |
| 132 |
using Vector = Dune::BlockVector<Dune::FieldVector<float,1> >; |
| 133 |
runSuperLU<Matrix,Vector>(N); |
| 134 |
} |
| 135 |
#endif |
| 136 |
#if __has_include("slu_ddefs.h") |
| 137 |
{ |
| 138 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> >; |
| 139 |
using Vector = Dune::BlockVector<Dune::FieldVector<double,1> >; |
| 140 |
runSuperLU<Matrix,Vector>(N); |
| 141 |
} |
| 142 |
#endif |
| 143 |
#if __has_include("slu_cdefs.h") |
| 144 |
{ |
| 145 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<float>,1,1> >; |
| 146 |
using Vector = Dune::BlockVector<Dune::FieldVector<std::complex<float>,1> >; |
| 147 |
runSuperLU<Matrix,Vector>(N); |
| 148 |
} |
| 149 |
#endif |
| 150 |
#if __has_include("slu_zdefs.h") |
| 151 |
{ |
| 152 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<double>,1,1> >; |
| 153 |
using Vector = Dune::BlockVector<Dune::FieldVector<std::complex<double>,1> >; |
| 154 |
runSuperLU<Matrix,Vector>(N); |
| 155 |
} |
| 156 |
#endif |
| 157 |
|
| 158 |
// ------------------------------------------------------------------------------ |
| 159 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<FieldMatrix<T,2,2> >"<<std::endl; |
| 160 |
#if __has_include("slu_sdefs.h") |
| 161 |
{ |
| 162 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<float,2,2> >; |
| 163 |
using Vector = Dune::BlockVector<Dune::FieldVector<float,2> >; |
| 164 |
runSuperLU<Matrix,Vector>(N); |
| 165 |
} |
| 166 |
#endif |
| 167 |
#if __has_include("slu_ddefs.h") |
| 168 |
{ |
| 169 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double,2,2> >; |
| 170 |
using Vector = Dune::BlockVector<Dune::FieldVector<double,2> >; |
| 171 |
runSuperLU<Matrix,Vector>(N); |
| 172 |
} |
| 173 |
#endif |
| 174 |
#if __has_include("slu_cdefs.h") |
| 175 |
{ |
| 176 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<float>,2,2> >; |
| 177 |
using Vector = Dune::BlockVector<Dune::FieldVector<std::complex<float>,2> >; |
| 178 |
runSuperLU<Matrix,Vector>(N); |
| 179 |
} |
| 180 |
#endif |
| 181 |
#if __has_include("slu_zdefs.h") |
| 182 |
{ |
| 183 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<std::complex<double>,2,2> >; |
| 184 |
using Vector = Dune::BlockVector<Dune::FieldVector<std::complex<double>,2> >; |
| 185 |
runSuperLU<Matrix,Vector>(N); |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
return 0; |
| 190 |
|
| 191 |
#else // HAVE_SUPERLU |
| 192 |
std::cerr << "You need SuperLU to run this test." << std::endl; |
| 193 |
return 77; |
| 194 |
#endif // HAVE_SUPERLU |
| 195 |
} |
| 196 |
catch (std::exception &e) |
| 197 |
{ |
| 198 |
std::cout << "ERROR: " << e.what() << std::endl; |
| 199 |
return 1; |
| 200 |
} |
| 201 |
catch (...) |
| 202 |
{ |
| 203 |
std::cerr << "Dune reported an unknown error." << std::endl; |
| 204 |
exit(1); |
| 205 |
} |