| 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 |
* \author: Marian Piatkowski, Steffen Müthing |
| 7 |
* \file |
| 8 |
* \brief Test MINRES and GMRes for complex matrices and complex rhs. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <complex> |
| 12 |
|
| 13 |
#include <dune/common/fvector.hh> |
| 14 |
#include <dune/common/fmatrix.hh> |
| 15 |
#include <dune/istl/operators.hh> |
| 16 |
#include <dune/istl/solvers.hh> |
| 17 |
#include <dune/istl/preconditioners.hh> |
| 18 |
#include "complexdata.hh" |
| 19 |
|
| 20 |
typedef std::complex<double> FIELD_TYPE; |
| 21 |
|
| 22 |
|
| 23 |
int main(int argc, char** argv) |
| 24 |
{ |
| 25 |
|
| 26 |
try { |
| 27 |
|
| 28 |
std::size_t N = 3; |
| 29 |
const int maxIter = int(N*N*N*N); |
| 30 |
const double reduction = 1e-16; |
| 31 |
|
| 32 |
std::cout << "============================================" << '\n' |
| 33 |
<< "starting solver tests with complex matrices and complex rhs... " |
| 34 |
<< std::endl |
| 35 |
<< std::endl; |
| 36 |
|
| 37 |
std::cout << "============================================" << '\n' |
| 38 |
<< "solving system with Hilbert matrix of size 10 times imaginary unit... " |
| 39 |
<< std::endl |
| 40 |
<< std::endl; |
| 41 |
|
| 42 |
Dune::FieldMatrix<std::complex<double>,10,10> hilbertmatrix; |
| 43 |
for(int i=0; i<10; i++) { |
| 44 |
for(int j=0; j<10; j++) { |
| 45 |
std::complex<double> temp(0.0,1./(i+j+1)); |
| 46 |
hilbertmatrix[i][j] = temp; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
Dune::FieldVector<std::complex<double>,10> hilbertsol(1.0); |
| 51 |
Dune::FieldVector<std::complex<double>,10> hilbertiter(0.0); |
| 52 |
Dune::MatrixAdapter<Dune::FieldMatrix<std::complex<double>,10,10>,Dune::FieldVector<std::complex<double>,10>,Dune::FieldVector<std::complex<double>,10> > hilbertadapter(hilbertmatrix); |
| 53 |
|
| 54 |
Dune::FieldVector<std::complex<double>,10> hilbertrhs(0.0); |
| 55 |
hilbertadapter.apply(hilbertsol,hilbertrhs); |
| 56 |
|
| 57 |
Dune::Richardson<Dune::FieldVector<std::complex<double>,10>,Dune::FieldVector<std::complex<double>,10> > noprec(1.0); |
| 58 |
|
| 59 |
Dune::RestartedGMResSolver<Dune::FieldVector<std::complex<double>,10> > realgmrestest(hilbertadapter,noprec,reduction,maxIter,maxIter,2); |
| 60 |
Dune::InverseOperatorResult stat; |
| 61 |
realgmrestest.apply(hilbertiter,hilbertrhs,stat); |
| 62 |
|
| 63 |
std::cout << hilbertiter << std::endl; |
| 64 |
// error of solution |
| 65 |
hilbertiter -= hilbertsol; |
| 66 |
std::cout << "error of solution with GMRes:" << std::endl; |
| 67 |
std::cout << hilbertiter.two_norm() << std::endl; |
| 68 |
|
| 69 |
std::cout << "============================================" << '\n' |
| 70 |
<< "solving system with complex matrix of size 10" << '\n' |
| 71 |
<< "randomly generated with the Eigen library... " |
| 72 |
<< std::endl << std::endl; |
| 73 |
|
| 74 |
Dune::FieldMatrix<std::complex<double>,10,10> complexmatrix(0.0), hermitianmatrix(0.0); |
| 75 |
Dune::FieldVector<std::complex<double>,10> complexsol, complexrhs(0.0), complexiter(0.0); |
| 76 |
|
| 77 |
// assemble randomly generated matrices from Eigen |
| 78 |
assemblecomplexmatrix(complexmatrix); |
| 79 |
assemblecomplexsol(complexsol); |
| 80 |
assemblehermitianmatrix(hermitianmatrix); |
| 81 |
|
| 82 |
Dune::MatrixAdapter<Dune::FieldMatrix<std::complex<double>,10,10>,Dune::FieldVector<std::complex<double>,10>,Dune::FieldVector<std::complex<double>,10> > complexadapter(complexmatrix), hermitianadapter(hermitianmatrix); |
| 83 |
|
| 84 |
Dune::SeqJac<Dune::FieldMatrix<std::complex<double>,10,10>,Dune::FieldVector<std::complex<double>,10>,Dune::FieldVector<std::complex<double>,10>,0> complexjacprec(complexmatrix,1,1.0); |
| 85 |
Dune::Richardson<Dune::FieldVector<std::complex<double>,10>,Dune::FieldVector<std::complex<double>,10> > complexnoprec(1.0); |
| 86 |
|
| 87 |
Dune::RestartedGMResSolver<Dune::FieldVector<std::complex<double>,10> > complexgmrestest(complexadapter,complexnoprec,1e-12,maxIter,maxIter*maxIter,2); |
| 88 |
|
| 89 |
complexadapter.apply(complexsol,complexrhs); |
| 90 |
complexgmrestest.apply(complexiter,complexrhs,stat); |
| 91 |
|
| 92 |
std::cout << complexiter << std::endl; |
| 93 |
// error of solution |
| 94 |
complexiter -= complexsol; |
| 95 |
std::cout << "error of solution with GMRes: " << complexiter.two_norm() << std::endl; |
| 96 |
|
| 97 |
std::cout << "============================================" << '\n' |
| 98 |
<< "solving system with hermitian matrix of size 10" << '\n' |
| 99 |
<< "randomly generated with the Eigen library... " |
| 100 |
<< std::endl << std::endl; |
| 101 |
|
| 102 |
Dune::RestartedGMResSolver<Dune::FieldVector<std::complex<double>,10> > hermitiangmrestest(hermitianadapter,complexnoprec,1e-12,maxIter,maxIter*maxIter,2); |
| 103 |
Dune::MINRESSolver<Dune::FieldVector<std::complex<double>,10> > complexminrestest(hermitianadapter,complexnoprec,1e-12,maxIter,2); |
| 104 |
|
| 105 |
complexiter = 0.0; |
| 106 |
hermitianadapter.apply(complexsol,complexrhs); |
| 107 |
hermitiangmrestest.apply(complexiter,complexrhs,stat); |
| 108 |
|
| 109 |
std::cout << complexiter << std::endl; |
| 110 |
// error of solution |
| 111 |
complexiter -= complexsol; |
| 112 |
std::cout << "error of solution with GMRes: " << complexiter.two_norm() << std::endl; |
| 113 |
|
| 114 |
complexiter = 0.0; |
| 115 |
hermitianadapter.apply(complexsol,complexrhs); |
| 116 |
complexminrestest.apply(complexiter,complexrhs,stat); |
| 117 |
|
| 118 |
std::cout << complexiter << std::endl; |
| 119 |
// error of solution |
| 120 |
complexiter-= complexsol; |
| 121 |
std::cout << "error of solution with MinRes: " << complexiter.two_norm() << std::endl; |
| 122 |
|
| 123 |
return 0; |
| 124 |
|
| 125 |
} catch (Dune::Exception& e) { |
| 126 |
std::cerr << "DUNE reported an exception: " << e << std::endl; |
| 127 |
return 1; |
| 128 |
} catch (std::exception& e) { |
| 129 |
std::cerr << "C++ reported an exception: " << e.what() << std::endl; |
| 130 |
return 2; |
| 131 |
} catch (...) { |
| 132 |
std::cerr << "Unknown exception encountered!" << std::endl; |
| 133 |
return 3; |
| 134 |
} |
| 135 |
} |