| 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 |
/** \file |
| 6 |
\brief Unit tests for the different dynamic matrices provided by ISTL |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <fenv.h> |
| 10 |
|
| 11 |
#include <dune/common/fmatrix.hh> |
| 12 |
#include <dune/common/diagonalmatrix.hh> |
| 13 |
#include <dune/istl/blocklevel.hh> |
| 14 |
#include <dune/istl/bcrsmatrix.hh> |
| 15 |
#include <dune/istl/matrix.hh> |
| 16 |
#include <dune/istl/bdmatrix.hh> |
| 17 |
#include <dune/istl/btdmatrix.hh> |
| 18 |
#include <dune/istl/scaledidmatrix.hh> |
| 19 |
|
| 20 |
|
| 21 |
using namespace Dune; |
| 22 |
|
| 23 |
// forward decls |
| 24 |
|
| 25 |
template <class MatrixType> |
| 26 |
void testSuperMatrix(MatrixType& matrix); |
| 27 |
template <class MatrixType, class X, class Y> |
| 28 |
void testMatrix(MatrixType& matrix, X& x, Y& y); |
| 29 |
template <class MatrixType, class VectorType> |
| 30 |
void testSolve(const MatrixType& matrix); |
| 31 |
|
| 32 |
template <class MatrixType> |
| 33 |
void testSuperMatrix(MatrixType& matrix) |
| 34 |
{ |
| 35 |
// //////////////////////////////////////////////////////// |
| 36 |
// Check the types which are exported by the matrix |
| 37 |
// //////////////////////////////////////////////////////// |
| 38 |
|
| 39 |
typedef typename MatrixType::field_type field_type; |
| 40 |
|
| 41 |
typedef typename MatrixType::block_type block_type; |
| 42 |
|
| 43 |
typedef typename MatrixType::size_type size_type; |
| 44 |
|
| 45 |
using allocator_type [[maybe_unused]] = typename MatrixType::allocator_type; |
| 46 |
|
| 47 |
size_type n = matrix.N(); |
| 48 |
size_type m = matrix.M(); |
| 49 |
BlockVector<FieldVector<field_type, block_type::cols> > x(m); |
| 50 |
BlockVector<FieldVector<field_type, block_type::rows> > y(n); |
| 51 |
|
| 52 |
testMatrix(matrix, x, y); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
template <class MatrixType, class X, class Y> |
| 57 |
void testMatrix(MatrixType& matrix, X& x, Y& y) |
| 58 |
{ |
| 59 |
// //////////////////////////////////////////////////////// |
| 60 |
// Check the types which are exported by the matrix |
| 61 |
// //////////////////////////////////////////////////////// |
| 62 |
|
| 63 |
typedef typename MatrixType::field_type field_type; |
| 64 |
|
| 65 |
typedef typename FieldTraits<field_type>::real_type real_type; |
| 66 |
|
| 67 |
using block_type [[maybe_unused]] = typename MatrixType::block_type; |
| 68 |
|
| 69 |
using row_type [[maybe_unused]] = typename MatrixType::row_type; |
| 70 |
|
| 71 |
typedef typename MatrixType::size_type size_type; |
| 72 |
|
| 73 |
using RowIterator [[maybe_unused]] = typename MatrixType::RowIterator; |
| 74 |
|
| 75 |
using ConstRowIterator [[maybe_unused]] = typename MatrixType::ConstRowIterator; |
| 76 |
|
| 77 |
using ColIterator [[maybe_unused]] = typename MatrixType::ColIterator; |
| 78 |
|
| 79 |
using ConstColIterator [[maybe_unused]] = typename MatrixType::ConstColIterator; |
| 80 |
|
| 81 |
static_assert(maxBlockLevel<MatrixType>() >= 0, "Block level has to be at least 1 for a matrix!"); |
| 82 |
|
| 83 |
// //////////////////////////////////////////////////////// |
| 84 |
// Count number of rows, columns, and nonzero entries |
| 85 |
// //////////////////////////////////////////////////////// |
| 86 |
|
| 87 |
typename MatrixType::RowIterator rowIt = matrix.begin(); |
| 88 |
typename MatrixType::RowIterator rowEndIt = matrix.end(); |
| 89 |
|
| 90 |
typename MatrixType::size_type numRows = 0, numEntries = 0; |
| 91 |
|
| 92 |
for (; rowIt!=rowEndIt; ++rowIt) { |
| 93 |
|
| 94 |
typename MatrixType::ColIterator colIt = rowIt->begin(); |
| 95 |
typename MatrixType::ColIterator colEndIt = rowIt->end(); |
| 96 |
|
| 97 |
for (; colIt!=colEndIt; ++colIt) { |
| 98 |
assert(matrix.exists(rowIt.index(), colIt.index())); |
| 99 |
numEntries++; |
| 100 |
} |
| 101 |
|
| 102 |
numRows++; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
assert (numRows == matrix.N()); |
| 107 |
|
| 108 |
// /////////////////////////////////////////////////////////////// |
| 109 |
// Count number of rows, columns, and nonzero entries again. |
| 110 |
// This time use the const iterators |
| 111 |
// /////////////////////////////////////////////////////////////// |
| 112 |
|
| 113 |
typename MatrixType::ConstRowIterator constRowIt = matrix.begin(); |
| 114 |
typename MatrixType::ConstRowIterator constRowEndIt = matrix.end(); |
| 115 |
|
| 116 |
numRows = 0; |
| 117 |
numEntries = 0; |
| 118 |
|
| 119 |
for (; constRowIt!=constRowEndIt; ++constRowIt) { |
| 120 |
|
| 121 |
typename MatrixType::ConstColIterator constColIt = constRowIt->begin(); |
| 122 |
typename MatrixType::ConstColIterator constColEndIt = constRowIt->end(); |
| 123 |
|
| 124 |
for (; constColIt!=constColEndIt; ++constColIt) |
| 125 |
numEntries++; |
| 126 |
|
| 127 |
numRows++; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
assert (numRows == matrix.N()); |
| 132 |
|
| 133 |
// //////////////////////////////////////////////////////// |
| 134 |
// Count number of rows, columns, and nonzero entries |
| 135 |
// This time we're counting backwards |
| 136 |
// //////////////////////////////////////////////////////// |
| 137 |
|
| 138 |
rowIt = matrix.beforeEnd(); |
| 139 |
rowEndIt = matrix.beforeBegin(); |
| 140 |
|
| 141 |
numRows = 0; |
| 142 |
numEntries = 0; |
| 143 |
|
| 144 |
for (; rowIt!=rowEndIt; --rowIt) { |
| 145 |
|
| 146 |
typename MatrixType::ColIterator colIt = rowIt->beforeEnd(); |
| 147 |
typename MatrixType::ColIterator colEndIt = rowIt->beforeBegin(); |
| 148 |
|
| 149 |
for (; colIt!=colEndIt; --colIt) { |
| 150 |
assert(matrix.exists(rowIt.index(), colIt.index())); |
| 151 |
numEntries++; |
| 152 |
} |
| 153 |
|
| 154 |
numRows++; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
assert (numRows == matrix.N()); |
| 159 |
|
| 160 |
// /////////////////////////////////////////////////////////////// |
| 161 |
// Count number of rows, columns, and nonzero entries again. |
| 162 |
// This time use the const iterators and count backwards. |
| 163 |
// /////////////////////////////////////////////////////////////// |
| 164 |
|
| 165 |
constRowIt = matrix.beforeEnd(); |
| 166 |
constRowEndIt = matrix.beforeBegin(); |
| 167 |
|
| 168 |
numRows = 0; |
| 169 |
numEntries = 0; |
| 170 |
|
| 171 |
for (; constRowIt!=constRowEndIt; --constRowIt) { |
| 172 |
|
| 173 |
typename MatrixType::ConstColIterator constColIt = constRowIt->beforeEnd(); |
| 174 |
typename MatrixType::ConstColIterator constColEndIt = constRowIt->beforeBegin(); |
| 175 |
|
| 176 |
for (; constColIt!=constColEndIt; --constColIt) |
| 177 |
numEntries++; |
| 178 |
|
| 179 |
numRows++; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
assert (numRows == matrix.N()); |
| 184 |
|
| 185 |
// /////////////////////////////////////////////////////// |
| 186 |
// More dimension stuff |
| 187 |
// /////////////////////////////////////////////////////// |
| 188 |
|
| 189 |
size_type n = matrix.N(); ++n; |
| 190 |
size_type m = matrix.M(); ++m; |
| 191 |
|
| 192 |
// /////////////////////////////////////////////////////// |
| 193 |
// Test assignment operators and the copy constructor |
| 194 |
// /////////////////////////////////////////////////////// |
| 195 |
|
| 196 |
// assignment from other matrix |
| 197 |
MatrixType secondMatrix; |
| 198 |
secondMatrix = matrix; |
| 199 |
|
| 200 |
// assignment from scalar |
| 201 |
matrix = 0; |
| 202 |
|
| 203 |
// The copy constructor |
| 204 |
[[maybe_unused]] MatrixType thirdMatrix(matrix); |
| 205 |
|
| 206 |
// /////////////////////////////////////////////////////// |
| 207 |
// Test component-wise operations |
| 208 |
// /////////////////////////////////////////////////////// |
| 209 |
|
| 210 |
matrix *= real_type(1.0); |
| 211 |
matrix /= real_type(1.0); |
| 212 |
|
| 213 |
matrix += secondMatrix; |
| 214 |
matrix -= secondMatrix; |
| 215 |
|
| 216 |
// /////////////////////////////////////////////////////////// |
| 217 |
// Test the various matrix-vector multiplications |
| 218 |
// /////////////////////////////////////////////////////////// |
| 219 |
|
| 220 |
Y yy=y; |
| 221 |
|
| 222 |
matrix.mv(x,yy); |
| 223 |
|
| 224 |
matrix.mtv(x,yy); |
| 225 |
|
| 226 |
matrix.umv(x,y); |
| 227 |
|
| 228 |
matrix.umtv(x,y); |
| 229 |
|
| 230 |
matrix.umhv(x,y); |
| 231 |
|
| 232 |
matrix.mmv(x,y); |
| 233 |
|
| 234 |
matrix.mmtv(x,y); |
| 235 |
|
| 236 |
matrix.mmhv(x,y); |
| 237 |
|
| 238 |
matrix.usmv(field_type(1.0),x,y); |
| 239 |
|
| 240 |
matrix.usmtv(field_type(1.0),x,y); |
| 241 |
|
| 242 |
matrix.usmhv(field_type(1.0),x,y); |
| 243 |
|
| 244 |
// ////////////////////////////////////////////////////////////// |
| 245 |
// Test the matrix norms |
| 246 |
// ////////////////////////////////////////////////////////////// |
| 247 |
|
| 248 |
real_type frobenius_norm = matrix.frobenius_norm(); |
| 249 |
|
| 250 |
frobenius_norm += matrix.frobenius_norm2(); |
| 251 |
|
| 252 |
frobenius_norm += matrix.infinity_norm(); |
| 253 |
|
| 254 |
frobenius_norm += matrix.infinity_norm_real(); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
// /////////////////////////////////////////////////////////////////// |
| 259 |
// Test the solve()-method for those matrix classes that have it |
| 260 |
// /////////////////////////////////////////////////////////////////// |
| 261 |
template <class MatrixType, class VectorType> |
| 262 |
void testSolve(const MatrixType& matrix) |
| 263 |
{ |
| 264 |
typedef typename VectorType::size_type size_type; |
| 265 |
|
| 266 |
// create some right hand side |
| 267 |
VectorType b(matrix.N()); |
| 268 |
for (size_type i=0; i<b.size(); i++) |
| 269 |
b[i] = i; |
| 270 |
|
| 271 |
// solution vector |
| 272 |
VectorType x(matrix.M()); |
| 273 |
|
| 274 |
// Solve the system |
| 275 |
matrix.solve(x,b); |
| 276 |
|
| 277 |
// compute residual |
| 278 |
matrix.mmv(x,b); |
| 279 |
|
| 280 |
if (b.two_norm() > 1e-10) |
| 281 |
DUNE_THROW(ISTLError, "Solve() method doesn't appear to produce the solution!"); |
| 282 |
} |
| 283 |
|
| 284 |
// ////////////////////////////////////////////////////////////// |
| 285 |
// Test transposing the matrix |
| 286 |
// ////////////////////////////////////////////////////////////// |
| 287 |
template <class MatrixType> |
| 288 |
void testTranspose(const MatrixType& matrix) |
| 289 |
{ |
| 290 |
MatrixType transposedMatrix = matrix.transpose(); |
| 291 |
|
| 292 |
for(size_t i = 0; i < matrix.N(); i++) |
| 293 |
for(size_t j = 0; j < matrix.M(); j++) |
| 294 |
if(fabs(transposedMatrix[j][i] - matrix[i][j]) > 1e-10) |
| 295 |
DUNE_THROW(ISTLError, "transpose() method produces wrong result!"); |
| 296 |
} |
| 297 |
|
| 298 |
int main() |
| 299 |
{ |
| 300 |
|
| 301 |
|
| 302 |
#if defined( __APPLE__ ) or defined( __MINGW32__ ) or defined(_MSC_VER) |
| 303 |
feraiseexcept(FE_INVALID); |
| 304 |
#else |
| 305 |
feenableexcept(FE_INVALID); |
| 306 |
#endif |
| 307 |
|
| 308 |
|
| 309 |
// //////////////////////////////////////////////////////////// |
| 310 |
// Test the Matrix class -- a scalar dense dynamic matrix |
| 311 |
// //////////////////////////////////////////////////////////// |
| 312 |
|
| 313 |
{ |
| 314 |
Matrix<double> matrixScalar(10,10); |
| 315 |
for (int i=0; i<10; i++) |
| 316 |
for (int j=0; j<10; j++) |
| 317 |
matrixScalar[i][j] = (i+j)/((double)(i*j+1)); // just anything |
| 318 |
|
| 319 |
BlockVector<double> x(10), y(10); |
| 320 |
testMatrix(matrixScalar, x, y); |
| 321 |
} |
| 322 |
|
| 323 |
// //////////////////////////////////////////////////////////// |
| 324 |
// Test the Matrix class -- a block-valued dense dynamic matrix |
| 325 |
// //////////////////////////////////////////////////////////// |
| 326 |
|
| 327 |
Matrix<FieldMatrix<double,3,3> > matrix(10,10); |
| 328 |
for (int i=0; i<10; i++) |
| 329 |
for (int j=0; j<10; j++) |
| 330 |
for (int k=0; k<3; k++) |
| 331 |
for (int l=0; l<3; l++) |
| 332 |
matrix[i][j][k][l] = (i+j)/((double)(k*l+1)); // just anything |
| 333 |
|
| 334 |
testSuperMatrix(matrix); |
| 335 |
|
| 336 |
Matrix<FieldMatrix<double,1,1> > nonquadraticMatrix(1,2); |
| 337 |
{ |
| 338 |
size_t n = 1; |
| 339 |
for (size_t i=0; i<1; i++) |
| 340 |
for (size_t j=0; j<2; j++) |
| 341 |
nonquadraticMatrix[i][j] = n++; |
| 342 |
} |
| 343 |
|
| 344 |
testTranspose(nonquadraticMatrix); |
| 345 |
|
| 346 |
|
| 347 |
// //////////////////////////////////////////////////////////// |
| 348 |
// Test the BCRSMatrix class -- a sparse dynamic matrix |
| 349 |
// //////////////////////////////////////////////////////////// |
| 350 |
|
| 351 |
{ |
| 352 |
BCRSMatrix<double> bcrsMatrix(4,4, BCRSMatrix<double>::random); |
| 353 |
|
| 354 |
bcrsMatrix.setrowsize(0,2); |
| 355 |
bcrsMatrix.setrowsize(1,3); |
| 356 |
bcrsMatrix.setrowsize(2,3); |
| 357 |
bcrsMatrix.setrowsize(3,2); |
| 358 |
|
| 359 |
bcrsMatrix.endrowsizes(); |
| 360 |
|
| 361 |
bcrsMatrix.addindex(0, 0); |
| 362 |
bcrsMatrix.addindex(0, 1); |
| 363 |
|
| 364 |
bcrsMatrix.addindex(1, 0); |
| 365 |
bcrsMatrix.addindex(1, 1); |
| 366 |
bcrsMatrix.addindex(1, 2); |
| 367 |
|
| 368 |
bcrsMatrix.addindex(2, 1); |
| 369 |
bcrsMatrix.addindex(2, 2); |
| 370 |
bcrsMatrix.addindex(2, 3); |
| 371 |
|
| 372 |
bcrsMatrix.addindex(3, 2); |
| 373 |
bcrsMatrix.addindex(3, 3); |
| 374 |
|
| 375 |
bcrsMatrix.endindices(); |
| 376 |
|
| 377 |
typedef BCRSMatrix<double>::RowIterator RowIterator; |
| 378 |
typedef BCRSMatrix<double>::ColIterator ColIterator; |
| 379 |
|
| 380 |
for(RowIterator row = bcrsMatrix.begin(); row != bcrsMatrix.end(); ++row) |
| 381 |
for(ColIterator col = row->begin(); col != row->end(); ++col) |
| 382 |
*col = 1.0 + (double) row.index() * (double) col.index(); |
| 383 |
|
| 384 |
BlockVector<double> x(4), y(4); |
| 385 |
|
| 386 |
testMatrix(bcrsMatrix, x, y); |
| 387 |
|
| 388 |
// Test whether matrix resizing works |
| 389 |
int size = 3; |
| 390 |
bcrsMatrix.setSize(size,size,size); |
| 391 |
|
| 392 |
for (int i=0; i<size; i++) |
| 393 |
bcrsMatrix.setrowsize(i, 1); |
| 394 |
|
| 395 |
bcrsMatrix.endrowsizes(); |
| 396 |
|
| 397 |
for (int i=0; i<size; i++) |
| 398 |
bcrsMatrix.addindex(i, i); |
| 399 |
|
| 400 |
bcrsMatrix.endindices(); |
| 401 |
|
| 402 |
for (int i=0; i<size; i++) |
| 403 |
bcrsMatrix[i][i] = 1.0; |
| 404 |
|
| 405 |
x.resize(size); |
| 406 |
y.resize(size); |
| 407 |
testMatrix(bcrsMatrix, x, y); |
| 408 |
} |
| 409 |
|
| 410 |
// //////////////////////////////////////////////////////////// |
| 411 |
// Test the BCRSMatrix class with FieldMatrix entries |
| 412 |
// //////////////////////////////////////////////////////////// |
| 413 |
|
| 414 |
BCRSMatrix<FieldMatrix<double,2,2> > bcrsMatrix(4,4, BCRSMatrix<FieldMatrix<double,2,2> >::random); |
| 415 |
|
| 416 |
bcrsMatrix.setrowsize(0,2); |
| 417 |
bcrsMatrix.setrowsize(1,3); |
| 418 |
bcrsMatrix.setrowsize(2,3); |
| 419 |
bcrsMatrix.setrowsize(3,2); |
| 420 |
|
| 421 |
bcrsMatrix.endrowsizes(); |
| 422 |
|
| 423 |
bcrsMatrix.addindex(0, 0); |
| 424 |
bcrsMatrix.addindex(0, 1); |
| 425 |
|
| 426 |
bcrsMatrix.addindex(1, 0); |
| 427 |
bcrsMatrix.addindex(1, 1); |
| 428 |
bcrsMatrix.addindex(1, 2); |
| 429 |
|
| 430 |
bcrsMatrix.addindex(2, 1); |
| 431 |
bcrsMatrix.addindex(2, 2); |
| 432 |
bcrsMatrix.addindex(2, 3); |
| 433 |
|
| 434 |
bcrsMatrix.addindex(3, 2); |
| 435 |
bcrsMatrix.addindex(3, 3); |
| 436 |
|
| 437 |
bcrsMatrix.endindices(); |
| 438 |
|
| 439 |
typedef BCRSMatrix<FieldMatrix<double,2,2> >::RowIterator RowIterator; |
| 440 |
typedef BCRSMatrix<FieldMatrix<double,2,2> >::ColIterator ColIterator; |
| 441 |
|
| 442 |
for(RowIterator row = bcrsMatrix.begin(); row != bcrsMatrix.end(); ++row) |
| 443 |
for(ColIterator col = row->begin(); col != row->end(); ++col) |
| 444 |
*col = 1.0 + (double) row.index() * (double) col.index(); |
| 445 |
|
| 446 |
testSuperMatrix(bcrsMatrix); |
| 447 |
|
| 448 |
// Test whether matrix resizing works |
| 449 |
int size = 3; |
| 450 |
bcrsMatrix.setSize(size,size,size); |
| 451 |
|
| 452 |
for (int i=0; i<size; i++) |
| 453 |
bcrsMatrix.setrowsize(i, 1); |
| 454 |
|
| 455 |
bcrsMatrix.endrowsizes(); |
| 456 |
|
| 457 |
for (int i=0; i<size; i++) |
| 458 |
bcrsMatrix.addindex(i, i); |
| 459 |
|
| 460 |
bcrsMatrix.endindices(); |
| 461 |
|
| 462 |
for (int i=0; i<size; i++) |
| 463 |
bcrsMatrix[i][i] = 1.0; |
| 464 |
|
| 465 |
testSuperMatrix(bcrsMatrix); |
| 466 |
|
| 467 |
/////////////////////////////////////////////////////////////////////////// |
| 468 |
// Test the BDMatrix class -- a dynamic block-diagonal matrix |
| 469 |
/////////////////////////////////////////////////////////////////////////// |
| 470 |
|
| 471 |
{ |
| 472 |
BDMatrix<double> bdMatrix(3); |
| 473 |
bdMatrix = 4.0; |
| 474 |
|
| 475 |
BlockVector<double> x(3), y(3); |
| 476 |
testMatrix(bdMatrix, x, y); |
| 477 |
|
| 478 |
// Test construction from initializer list |
| 479 |
BDMatrix<double> bdMatrix2 = {1.0, 2.0, 3.0}; |
| 480 |
testMatrix(bdMatrix2, x, y); |
| 481 |
|
| 482 |
// Test equation-solving |
| 483 |
BDMatrix<double> bdMatrix3 = {1.0, 2.0, 3.0}; |
| 484 |
testSolve<BDMatrix<double>, BlockVector<double> >(bdMatrix3); |
| 485 |
|
| 486 |
// Test whether inversion works |
| 487 |
bdMatrix3.invert(); |
| 488 |
|
| 489 |
// test whether resizing works |
| 490 |
bdMatrix2.setSize(5); |
| 491 |
bdMatrix2 = 4.0; |
| 492 |
x.resize(5); |
| 493 |
y.resize(5); |
| 494 |
testMatrix(bdMatrix2, x, y); |
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
// //////////////////////////////////////////////////////////////////////// |
| 499 |
// Test the BDMatrix class with FieldMatrix entries |
| 500 |
// //////////////////////////////////////////////////////////////////////// |
| 501 |
|
| 502 |
BDMatrix<FieldMatrix<double,4,4> > bdMatrix(2); |
| 503 |
bdMatrix = 4.0; |
| 504 |
|
| 505 |
testSuperMatrix(bdMatrix); |
| 506 |
|
| 507 |
// Test construction from initializer list |
| 508 |
BDMatrix<FieldMatrix<double,2,2> > bdMatrix2 = { {{1,0},{0,1}}, {{0,1},{-1,0}}}; |
| 509 |
|
| 510 |
// Test equation-solving |
| 511 |
testSolve<BDMatrix<FieldMatrix<double,2,2> >, BlockVector<FieldVector<double,2> > >(bdMatrix2); |
| 512 |
|
| 513 |
// Test whether inversion works |
| 514 |
bdMatrix2.invert(); |
| 515 |
|
| 516 |
// Run matrix tests on this matrix |
| 517 |
testSuperMatrix(bdMatrix2); |
| 518 |
|
| 519 |
// test whether resizing works |
| 520 |
bdMatrix2.setSize(5); |
| 521 |
bdMatrix2 = 4.0; |
| 522 |
testSuperMatrix(bdMatrix2); |
| 523 |
|
| 524 |
// //////////////////////////////////////////////////////////////////////// |
| 525 |
// Test the BTDMatrix class -- a dynamic block-tridiagonal matrix |
| 526 |
// a) the scalar case |
| 527 |
// //////////////////////////////////////////////////////////////////////// |
| 528 |
|
| 529 |
{ |
| 530 |
BTDMatrix<double> btdMatrixScalar(4); |
| 531 |
using size_type = BTDMatrix<double>::size_type; |
| 532 |
|
| 533 |
btdMatrixScalar = 4.0; |
| 534 |
|
| 535 |
BlockVector<double> x(4), y(4); |
| 536 |
testMatrix(btdMatrixScalar, x, y); |
| 537 |
|
| 538 |
btdMatrixScalar = 0.0; |
| 539 |
for (size_type i=0; i<btdMatrixScalar.N(); i++) // diagonal |
| 540 |
btdMatrixScalar[i][i] = 1+i; |
| 541 |
|
| 542 |
for (size_type i=0; i<btdMatrixScalar.N()-1; i++) |
| 543 |
btdMatrixScalar[i][i+1] = 2+i; // first off-diagonal |
| 544 |
|
| 545 |
testSolve<BTDMatrix<double>, BlockVector<double> >(btdMatrixScalar); |
| 546 |
|
| 547 |
// test a 1x1 BTDMatrix, because that is a special case |
| 548 |
BTDMatrix<double> btdMatrixScalar_1x1(1); |
| 549 |
btdMatrixScalar_1x1 = 1.0; |
| 550 |
x.resize(1); |
| 551 |
y.resize(1); |
| 552 |
testMatrix(btdMatrixScalar_1x1, x, y); |
| 553 |
|
| 554 |
// test whether resizing works |
| 555 |
btdMatrixScalar_1x1.setSize(5); |
| 556 |
btdMatrixScalar_1x1 = 4.0; |
| 557 |
x.resize(5); |
| 558 |
y.resize(5); |
| 559 |
testMatrix(btdMatrixScalar_1x1, x, y); |
| 560 |
} |
| 561 |
|
| 562 |
/////////////////////////////////////////////////////////////////////////// |
| 563 |
// Test the BTDMatrix class -- a dynamic block-tridiagonal matrix |
| 564 |
// b) the scalar case with FieldMatrix entries |
| 565 |
/////////////////////////////////////////////////////////////////////////// |
| 566 |
|
| 567 |
BTDMatrix<FieldMatrix<double,1,1> > btdMatrixScalar(4); |
| 568 |
typedef BTDMatrix<FieldMatrix<double,1,1> >::size_type size_type; |
| 569 |
|
| 570 |
btdMatrixScalar = 4.0; |
| 571 |
|
| 572 |
testSuperMatrix(btdMatrixScalar); |
| 573 |
|
| 574 |
btdMatrixScalar = 0.0; |
| 575 |
for (size_type i=0; i<btdMatrixScalar.N(); i++) // diagonal |
| 576 |
btdMatrixScalar[i][i] = 1+i; |
| 577 |
|
| 578 |
for (size_type i=0; i<btdMatrixScalar.N()-1; i++) |
| 579 |
btdMatrixScalar[i][i+1] = 2+i; // first off-diagonal |
| 580 |
|
| 581 |
testSolve<BTDMatrix<FieldMatrix<double,1,1> >, BlockVector<FieldVector<double,1> > >(btdMatrixScalar); |
| 582 |
|
| 583 |
// test a 1x1 BTDMatrix, because that is a special case |
| 584 |
BTDMatrix<FieldMatrix<double,1,1> > btdMatrixScalar_1x1(1); |
| 585 |
btdMatrixScalar_1x1 = 1.0; |
| 586 |
testSuperMatrix(btdMatrixScalar_1x1); |
| 587 |
|
| 588 |
// test whether resizing works |
| 589 |
btdMatrixScalar_1x1.setSize(5); |
| 590 |
btdMatrixScalar_1x1 = 4.0; |
| 591 |
testSuperMatrix(btdMatrixScalar_1x1); |
| 592 |
|
| 593 |
// //////////////////////////////////////////////////////////////////////// |
| 594 |
// Test the BTDMatrix class -- a dynamic block-tridiagonal matrix |
| 595 |
// c) the block-valued case |
| 596 |
// //////////////////////////////////////////////////////////////////////// |
| 597 |
|
| 598 |
BTDMatrix<FieldMatrix<double,2,2> > btdMatrix(4); |
| 599 |
typedef BTDMatrix<FieldMatrix<double,2,2> >::size_type size_type; |
| 600 |
|
| 601 |
btdMatrix = 0.0; |
| 602 |
for (size_type i=0; i<btdMatrix.N(); i++) // diagonal |
| 603 |
btdMatrix[i][i] = ScaledIdentityMatrix<double,2>(1+i); |
| 604 |
|
| 605 |
for (size_type i=0; i<btdMatrix.N()-1; i++) |
| 606 |
btdMatrix[i][i+1] = ScaledIdentityMatrix<double,2>(2+i); // upper off-diagonal |
| 607 |
for (size_type i=1; i<btdMatrix.N(); i++) |
| 608 |
btdMatrix[i-1][i] = ScaledIdentityMatrix<double,2>(2+i); // lower off-diagonal |
| 609 |
|
| 610 |
// add some off diagonal stuff to the blocks in the matrix |
| 611 |
// diagonals |
| 612 |
btdMatrix[0][0][0][1] = 2; |
| 613 |
btdMatrix[0][0][1][0] = -1; |
| 614 |
|
| 615 |
btdMatrix[1][1][0][1] = 2; |
| 616 |
btdMatrix[1][1][1][0] = 3; |
| 617 |
|
| 618 |
btdMatrix[2][2][0][1] = 2; |
| 619 |
btdMatrix[2][2][0][0] += sqrt(2.); |
| 620 |
btdMatrix[2][2][1][0] = 3; |
| 621 |
|
| 622 |
btdMatrix[3][3][0][1] = -1; |
| 623 |
btdMatrix[3][3][0][0] -= 0.5; |
| 624 |
btdMatrix[3][3][1][0] = 2; |
| 625 |
|
| 626 |
// off diagonals |
| 627 |
btdMatrix[0][1][0][1] = std::sqrt(2); |
| 628 |
btdMatrix[1][0][0][1] = std::sqrt(2); |
| 629 |
|
| 630 |
btdMatrix[1][0][1][0] = -13./17.; |
| 631 |
btdMatrix[1][2][0][1] = -1./std::sqrt(2); |
| 632 |
btdMatrix[1][2][1][0] = -13./17.; |
| 633 |
|
| 634 |
btdMatrix[2][1][0][1] = -13./17.; |
| 635 |
btdMatrix[2][1][1][0] = -13./17.; |
| 636 |
btdMatrix[2][3][0][1] = -1./std::sqrt(2); |
| 637 |
btdMatrix[2][3][1][0] = -17.; |
| 638 |
|
| 639 |
btdMatrix[3][2][0][1] = 1.; |
| 640 |
btdMatrix[3][2][1][0] = 1.; |
| 641 |
|
| 642 |
|
| 643 |
BTDMatrix<FieldMatrix<double,2,2> > btdMatrixThrowAway = btdMatrix; // the test method overwrites the matrix |
| 644 |
testSuperMatrix(btdMatrixThrowAway); |
| 645 |
|
| 646 |
testSolve<BTDMatrix<FieldMatrix<double,2,2> >, BlockVector<FieldVector<double,2> > >(btdMatrix); |
| 647 |
|
| 648 |
// test a 1x1 BTDMatrix, because that is a special case |
| 649 |
BTDMatrix<FieldMatrix<double,2,2> > btdMatrix_1x1(1); |
| 650 |
btdMatrix_1x1 = 1.0; |
| 651 |
testSuperMatrix(btdMatrix_1x1); |
| 652 |
|
| 653 |
// test whether resizing works |
| 654 |
btdMatrix_1x1.setSize(5); |
| 655 |
btdMatrix_1x1 = 4.0; |
| 656 |
testSuperMatrix(btdMatrix_1x1); |
| 657 |
|
| 658 |
// //////////////////////////////////////////////////////////////////////// |
| 659 |
// Test the FieldMatrix class |
| 660 |
// //////////////////////////////////////////////////////////////////////// |
| 661 |
typedef FieldMatrix<double,4,4>::size_type size_type; |
| 662 |
FieldMatrix<double,4,4> fMatrix; |
| 663 |
|
| 664 |
for (size_type i=0; i<fMatrix.N(); i++) |
| 665 |
for (size_type j=0; j<fMatrix.M(); j++) |
| 666 |
fMatrix[i][j] = (i+j)/3; // just anything |
| 667 |
FieldVector<double,4> fvX; |
| 668 |
FieldVector<double,4> fvY; |
| 669 |
|
| 670 |
testMatrix(fMatrix, fvX, fvY); |
| 671 |
|
| 672 |
// //////////////////////////////////////////////////////////////////////// |
| 673 |
// Test the 1x1 specialization of the FieldMatrix class |
| 674 |
// //////////////////////////////////////////////////////////////////////// |
| 675 |
|
| 676 |
FieldMatrix<double,1,1> fMatrix1x1; |
| 677 |
fMatrix1x1[0][0] = 2.3; // just anything |
| 678 |
|
| 679 |
FieldVector<double,1> fvX1; |
| 680 |
FieldVector<double,1> fvY1; |
| 681 |
|
| 682 |
testMatrix(fMatrix1x1, fvX1, fvY1); |
| 683 |
|
| 684 |
// //////////////////////////////////////////////////////////////////////// |
| 685 |
// Test the DiagonalMatrix class |
| 686 |
// //////////////////////////////////////////////////////////////////////// |
| 687 |
|
| 688 |
FieldVector<double,1> dMatrixConstructFrom; |
| 689 |
dMatrixConstructFrom = 3.1459; |
| 690 |
|
| 691 |
DiagonalMatrix<double,4> dMatrix1; |
| 692 |
dMatrix1 = 3.1459; |
| 693 |
testMatrix(dMatrix1, fvX, fvY); |
| 694 |
|
| 695 |
DiagonalMatrix<double,4> dMatrix2(3.1459); |
| 696 |
testMatrix(dMatrix2, fvX, fvY); |
| 697 |
|
| 698 |
DiagonalMatrix<double,4> dMatrix3(dMatrixConstructFrom); |
| 699 |
testMatrix(dMatrix3, fvX, fvY); |
| 700 |
|
| 701 |
// //////////////////////////////////////////////////////////////////////// |
| 702 |
// Test the ScaledIdentityMatrix class |
| 703 |
// //////////////////////////////////////////////////////////////////////// |
| 704 |
|
| 705 |
ScaledIdentityMatrix<double,4> sIdMatrix; |
| 706 |
sIdMatrix = 3.1459; |
| 707 |
|
| 708 |
testMatrix(sIdMatrix, fvX, fvY); |
| 709 |
} |