| 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 <iostream> |
| 7 |
|
| 8 |
#include <dune/common/fmatrix.hh> |
| 9 |
#include <dune/common/fvector.hh> |
| 10 |
#include <dune/common/bitsetvector.hh> |
| 11 |
#include <dune/common/tuplevector.hh> |
| 12 |
|
| 13 |
#include <dune/istl/bvector.hh> |
| 14 |
#include <dune/istl/io.hh> |
| 15 |
#include <dune/istl/operators.hh> |
| 16 |
#include<array> |
| 17 |
#include<vector> |
| 18 |
|
| 19 |
#include <dune/istl/cholmod.hh> |
| 20 |
#include <dune/istl/multitypeblockmatrix.hh> |
| 21 |
#include <dune/istl/multitypeblockvector.hh> |
| 22 |
|
| 23 |
|
| 24 |
#include "laplacian.hh" |
| 25 |
|
| 26 |
using namespace Dune; |
| 27 |
|
| 28 |
/** \brief Do the actual tests |
| 29 |
* |
| 30 |
* \tparam Index Type used by CHOLMOD for indices (int or long int) |
| 31 |
*/ |
| 32 |
template <typename Index> |
| 33 |
bool test() |
| 34 |
{ |
| 35 |
bool passed = true; |
| 36 |
#if HAVE_SUITESPARSE_UMFPACK |
| 37 |
int N = 30; // number of nodes |
| 38 |
const int bs = 2; // block size |
| 39 |
|
| 40 |
// fill matrix with external method |
| 41 |
BCRSMatrix<FieldMatrix<double,bs,bs>> A; |
| 42 |
// no regularization, A may be singular! |
| 43 |
setupLaplacian(A, N); |
| 44 |
|
| 45 |
BlockVector<FieldVector<double,bs>> b,x; |
| 46 |
b.resize(A.N()); |
| 47 |
x.resize(A.N()); |
| 48 |
b = 1; |
| 49 |
|
| 50 |
InverseOperatorResult res; |
| 51 |
|
| 52 |
// test without ignore nodes |
| 53 |
Cholmod<BlockVector<FieldVector<double,bs>>> cholmod; |
| 54 |
cholmod.setMatrix(A); |
| 55 |
cholmod.apply(x,b,res); |
| 56 |
|
| 57 |
// test |
| 58 |
A.mmv(x,b); |
| 59 |
|
| 60 |
if ( b.two_norm() > 1e-9 ) |
| 61 |
{ |
| 62 |
std::cerr << " Error in CHOLMOD, residual is too large: " << b.two_norm() << std::endl; |
| 63 |
passed = false; |
| 64 |
} |
| 65 |
|
| 66 |
x = 0; |
| 67 |
b = 1; |
| 68 |
|
| 69 |
// test with ignore nodes |
| 70 |
std::vector<std::array<bool,bs>> ignore; |
| 71 |
ignore.resize(A.N()); |
| 72 |
// ignore one random entry in x and b |
| 73 |
ignore[12][0] = true; |
| 74 |
b[12][0] = 666; |
| 75 |
x[12][0] = 123; |
| 76 |
|
| 77 |
|
| 78 |
Cholmod<BlockVector<FieldVector<double,bs>>> cholmod2; |
| 79 |
cholmod2.setMatrix(A,&ignore); |
| 80 |
cholmod2.apply(x,b,res); |
| 81 |
|
| 82 |
// check that x[12][0] is untouched |
| 83 |
if ( std::abs(x[12][0] - 123) > 1e-15 ) |
| 84 |
{ |
| 85 |
std::cerr << " Error in CHOLMOD, x was NOT ignored!"<< std::endl; |
| 86 |
passed = false; |
| 87 |
} |
| 88 |
|
| 89 |
// reset the x value |
| 90 |
x[12][0] = 0; |
| 91 |
// test -> this should result in zero in every line except entry [12][1] |
| 92 |
A.mmv(x,b); |
| 93 |
auto b_12_0 = b[12][0]; |
| 94 |
|
| 95 |
// check that error is completely caused by this entry |
| 96 |
if ( std::abs( b.two_norm() - std::abs(b_12_0) ) > 1e-15 ) |
| 97 |
{ |
| 98 |
std::cerr << " Error in CHOLMOD, b was NOT ignored correctly: " << std::abs( b.two_norm() - std::abs(b_12_0) ) << std::endl; |
| 99 |
passed = false; |
| 100 |
} |
| 101 |
|
| 102 |
using BCRS = BCRSMatrix<FieldMatrix<double,bs,bs>>; |
| 103 |
using MTRow = MultiTypeBlockVector<BCRS,BCRS>; |
| 104 |
using MTBM = MultiTypeBlockMatrix<MTRow,MTRow>; |
| 105 |
|
| 106 |
MTBM A2; |
| 107 |
|
| 108 |
using namespace Indices; |
| 109 |
A2[_0][_0] = A; |
| 110 |
A2[_1][_0] = A; |
| 111 |
A2[_0][_1] = A; |
| 112 |
A2[_1][_1] = A; |
| 113 |
|
| 114 |
// this makes it regular |
| 115 |
A2[_0][_0] *= 2.; |
| 116 |
|
| 117 |
using MTBV = MultiTypeBlockVector<BlockVector<FieldVector<double,bs>>,BlockVector<FieldVector<double,bs>>>; |
| 118 |
MTBV b2,x2; |
| 119 |
|
| 120 |
b = 1; |
| 121 |
x = 0; |
| 122 |
|
| 123 |
|
| 124 |
x2[_0] = x; |
| 125 |
x2[_1] = x; |
| 126 |
|
| 127 |
b2[_0] = b; |
| 128 |
b2[_1] = b; |
| 129 |
|
| 130 |
Cholmod<MTBV> cholmodMT; |
| 131 |
cholmodMT.setMatrix(A2); |
| 132 |
|
| 133 |
cholmodMT.apply(x2,b2,res); |
| 134 |
|
| 135 |
// test |
| 136 |
A2.mmv(x2,b2); |
| 137 |
|
| 138 |
if ( b2.two_norm() > 1e-9 ) |
| 139 |
{ |
| 140 |
std::cerr << " Error in CHOLMOD, residual is too large: " << b2.two_norm() << std::endl; |
| 141 |
passed = false; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
x2 = 0; |
| 146 |
b2 = 1; |
| 147 |
|
| 148 |
// test with ignore nodes |
| 149 |
TupleVector<std::vector<std::array<bool,bs>>,std::vector<std::array<bool,bs>>> ignoreMT; |
| 150 |
ignoreMT[_0].resize(A.N()); |
| 151 |
ignoreMT[_1].resize(A.N()); |
| 152 |
// ignore one random entry in x and b |
| 153 |
ignoreMT[_0][12][0] = true; |
| 154 |
b2[_0][12][0] = 666; |
| 155 |
x2[_0][12][0] = 123; |
| 156 |
|
| 157 |
Cholmod<MTBV> cholmodMT2; |
| 158 |
cholmodMT2.setMatrix(A2,&ignoreMT); |
| 159 |
|
| 160 |
cholmodMT2.apply(x2,b2,res); |
| 161 |
|
| 162 |
// check that x was ignored |
| 163 |
if ( std::abs( x2[_0][12][0] - 123 ) > 1e-15 ) |
| 164 |
{ |
| 165 |
std::cerr << " Error in CHOLMOD, x was NOT ignored correctly: " << std::abs( x2[_0][12][0] - 123 ) << std::endl; |
| 166 |
passed = false; |
| 167 |
} |
| 168 |
|
| 169 |
x2[_0][12][0] = 0; |
| 170 |
|
| 171 |
// test |
| 172 |
A2.mmv(x2,b2); |
| 173 |
|
| 174 |
auto b_0_12_0 = b2[_0][12][0]; |
| 175 |
|
| 176 |
// check that error is completely caused by this entry |
| 177 |
if ( std::abs( b2.two_norm() - std::abs(b_0_12_0) ) > 1e-15 ) |
| 178 |
{ |
| 179 |
std::cerr << " Error in CHOLMOD, b was NOT ignored correctly: " << std::abs( b2.two_norm() - std::abs(b_0_12_0) ) << std::endl; |
| 180 |
passed = false; |
| 181 |
} |
| 182 |
|
| 183 |
#else // HAVE_SUITESPARSE_UMFPACK |
| 184 |
std::cerr << "You need SuiteSparse to run the CHOLMOD test." << std::endl; |
| 185 |
passed = false; |
| 186 |
#endif // HAVE_SUITESPARSE_UMFPACK |
| 187 |
return passed; |
| 188 |
} |
| 189 |
|
| 190 |
int main(int argc, char** argv) try |
| 191 |
{ |
| 192 |
bool passed = true; |
| 193 |
|
| 194 |
std::cout << "testing int" << std::endl; |
| 195 |
passed &= test<int>(); |
| 196 |
std::cout << "testing SuiteSparse_long (i.e., long int)" << std::endl; |
| 197 |
passed &= test<SuiteSparse_long>(); |
| 198 |
|
| 199 |
return (passed) ? 0 : 1; |
| 200 |
} |
| 201 |
catch (std::exception &e) |
| 202 |
{ |
| 203 |
std::cerr << "ERROR: " << e.what() << std::endl; |
| 204 |
return 1; |
| 205 |
} |
| 206 |
catch (...) |
| 207 |
{ |
| 208 |
std::cerr << "Dune reported an unknown error." << std::endl; |
| 209 |
exit(1); |
| 210 |
} |