| 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 |
#include <dune/common/fmatrix.hh> |
| 6 |
#include <dune/common/diagonalmatrix.hh> |
| 7 |
#include <dune/istl/scaledidmatrix.hh> |
| 8 |
#include <dune/istl/bcrsmatrix.hh> |
| 9 |
#include <dune/istl/matrix.hh> |
| 10 |
#include <dune/istl/io.hh> |
| 11 |
#include "laplacian.hh" |
| 12 |
|
| 13 |
/* "tests" the writeMatrixToMatlabHelper and writeSVGMatrix methods by calling |
| 14 |
* them for a Laplacian with a given BlockType and writing to cout. |
| 15 |
* Actual functionality is not tested. |
| 16 |
*/ |
| 17 |
template <class BlockType> |
| 18 |
void testWriteMatrix(BlockType b=BlockType(0.0)) |
| 19 |
{ |
| 20 |
typedef Dune::BCRSMatrix<BlockType> Matrix; |
| 21 |
|
| 22 |
Matrix A; |
| 23 |
setupLaplacian(A, 3); |
| 24 |
|
| 25 |
A[0][0] += b; |
| 26 |
writeMatrixToMatlabHelper(A, 0, 0, std::cout); |
| 27 |
writeSVGMatrix(std::cout, A); |
| 28 |
} |
| 29 |
|
| 30 |
/* uses the writeVectorToMatlab method, filled with dummy data */ |
| 31 |
template <class VectorType> |
| 32 |
void testWriteVectorToMatlab() |
| 33 |
{ |
| 34 |
VectorType v; |
| 35 |
for (unsigned int i = 0; i < v.size(); ++i) |
| 36 |
{ |
| 37 |
v[i] = i; |
| 38 |
} |
| 39 |
|
| 40 |
Dune::writeVectorToMatlabHelper(v, std::cout); |
| 41 |
} |
| 42 |
|
| 43 |
int main(int argc, char** argv) |
| 44 |
{ |
| 45 |
/* testing the writeMatrixToMatlabHelper method for BlockType=FieldMatrix with different field_types */ |
| 46 |
testWriteMatrix<Dune::FieldMatrix<double,1,1> >(); |
| 47 |
testWriteMatrix<Dune::FieldMatrix<double,1,2> >(); |
| 48 |
// testWriteMatrix<Dune::FieldMatrix<double,2,1> >(); // commented because setUpLaplacian cannot handle block_types with more rows than cols |
| 49 |
testWriteMatrix<Dune::FieldMatrix<double,4,7> >(); |
| 50 |
// testWriteMatrix<Dune::FieldMatrix<double,7,4> >(); // commented because setUpLaplacian cannot handle block_types with more rows than cols |
| 51 |
testWriteMatrix<Dune::FieldMatrix<double,2,2> >(); |
| 52 |
|
| 53 |
testWriteMatrix<Dune::FieldMatrix<std::complex<double>,1,1> >(Dune::FieldMatrix<std::complex<double>,1,1>(std::complex<double>(0, 1))); |
| 54 |
testWriteMatrix<Dune::FieldMatrix<std::complex<double>,1,2> >(Dune::FieldMatrix<std::complex<double>,1,2>(std::complex<double>(0, 1))); |
| 55 |
// testWriteMatrix<Dune::FieldMatrix<std::complex<double>,2,1> >(); // commented because setUpLaplacian cannot handle block_types with more rows than cols |
| 56 |
testWriteMatrix<Dune::FieldMatrix<std::complex<double>,4,7> >(Dune::FieldMatrix<std::complex<double>,4,7>(std::complex<double>(0, 1))); |
| 57 |
// testWriteMatrix<Dune::FieldMatrix<std::complex<double>,7,4> >(); // commented because setUpLaplacian cannot handle block_types with more rows than cols |
| 58 |
testWriteMatrix<Dune::FieldMatrix<std::complex<double>,2,2> >(Dune::FieldMatrix<std::complex<double>,2,2>(std::complex<double>(0, 1))); |
| 59 |
|
| 60 |
testWriteMatrix<double>(); |
| 61 |
testWriteMatrix<std::complex<double> >(); |
| 62 |
|
| 63 |
/* testing the writeMatrixToMatlabHelper method for BlockType=[Diagonal|ScaledIdentity]Matrix with different field_types */ |
| 64 |
testWriteMatrix<Dune::DiagonalMatrix<double,1> >(); |
| 65 |
testWriteMatrix<Dune::ScaledIdentityMatrix<double,1> >(); |
| 66 |
testWriteMatrix<Dune::DiagonalMatrix<double,2> >(); |
| 67 |
testWriteMatrix<Dune::ScaledIdentityMatrix<double,2> >(); |
| 68 |
|
| 69 |
testWriteMatrix<Dune::DiagonalMatrix<std::complex<double>,1> >(Dune::DiagonalMatrix<std::complex<double>,1>(std::complex<double>(0,1))); |
| 70 |
testWriteMatrix<Dune::ScaledIdentityMatrix<std::complex<double>,1> >(Dune::ScaledIdentityMatrix<std::complex<double>,1>(std::complex<double>(0,1))); |
| 71 |
testWriteMatrix<Dune::DiagonalMatrix<std::complex<double>,2> >(Dune::DiagonalMatrix<std::complex<double>,2>(std::complex<double>(0,1))); |
| 72 |
testWriteMatrix<Dune::ScaledIdentityMatrix<std::complex<double>,2> >(Dune::ScaledIdentityMatrix<std::complex<double>,2>(std::complex<double>(0,1))); |
| 73 |
|
| 74 |
|
| 75 |
/* testing the writeVectorToMatlabHelper method for FieldVector */ |
| 76 |
testWriteVectorToMatlab<Dune::FieldVector<double,1> >(); |
| 77 |
testWriteVectorToMatlab<Dune::FieldVector<float,5> >(); |
| 78 |
testWriteVectorToMatlab<Dune::FieldVector<std::complex<double>,5> >(); |
| 79 |
/* testing the writeVectorToMatlabHelper method for BlockVector */ |
| 80 |
Dune::BlockVector<Dune::FieldVector<double,3> > v1 = {{1.0, 2.0, 3.0}}; |
| 81 |
Dune::writeVectorToMatlabHelper(v1, std::cout); |
| 82 |
Dune::BlockVector<Dune::BlockVector<Dune::FieldVector<double,1> > > v2 = {{1.0, 2.0}, {3.0, 4.0, 5.0}, {6.0}}; |
| 83 |
Dune::writeVectorToMatlabHelper(v2, std::cout); |
| 84 |
/* testing the writeVectorToMatlabHelper method for STL containers */ |
| 85 |
testWriteVectorToMatlab<std::array<double,5> >(); |
| 86 |
std::vector<double> v3; |
| 87 |
v3.push_back(1); |
| 88 |
v3.push_back(2); |
| 89 |
Dune::writeVectorToMatlabHelper(v3, std::cout); |
| 90 |
|
| 91 |
// Test the printmatrix and printSparseMatrix methods |
| 92 |
// BCRSMatrix |
| 93 |
{ |
| 94 |
Dune::BCRSMatrix<double> matrix; |
| 95 |
setupLaplacian(matrix, 3); |
| 96 |
Dune::printmatrix(std::cout, matrix, "BCRSMatrix<double>", "--"); |
| 97 |
Dune::printSparseMatrix(std::cout, matrix, "BCRSMatrix<double>", "--"); |
| 98 |
} |
| 99 |
{ |
| 100 |
Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > matrix; |
| 101 |
setupLaplacian(matrix, 3); |
| 102 |
Dune::printmatrix(std::cout, matrix, "BCRSMatrix<FieldMatrix<double,1,1> >", "--"); |
| 103 |
Dune::printSparseMatrix(std::cout, matrix, "BCRSMatrix<FieldMatrix<double,1,1> >", "--"); |
| 104 |
} |
| 105 |
{ |
| 106 |
Dune::BCRSMatrix<Dune::ScaledIdentityMatrix<double,1> > matrix; |
| 107 |
setupLaplacian(matrix, 3); |
| 108 |
Dune::printSparseMatrix(std::cout, matrix, "BCRSMatrix<ScaledIdentityMatrix<double,1> >", "--"); |
| 109 |
} |
| 110 |
{ |
| 111 |
Dune::BCRSMatrix<Dune::FieldMatrix<double,2,3> > matrix; |
| 112 |
setupLaplacian(matrix, 3); |
| 113 |
Dune::printmatrix(std::cout, matrix, "BCRSMatrix<FieldMatrix<double,2,3> >", "--"); |
| 114 |
Dune::printSparseMatrix(std::cout, matrix, "BCRSMatrix<FieldMatrix<double,2,3> >", "--"); |
| 115 |
} |
| 116 |
|
| 117 |
// Matrix |
| 118 |
{ |
| 119 |
Dune::Matrix<double> matrix(3,3); |
| 120 |
matrix = 0; |
| 121 |
Dune::printmatrix(std::cout, matrix, "Matrix<double>", "--"); |
| 122 |
} |
| 123 |
{ |
| 124 |
Dune::Matrix<Dune::FieldMatrix<double,1,1> > matrix(3,3); |
| 125 |
matrix = 0; |
| 126 |
Dune::printmatrix(std::cout, matrix, "Matrix<FieldMatrix<double,1,1> >", "--"); |
| 127 |
} |
| 128 |
{ |
| 129 |
Dune::Matrix<Dune::FieldMatrix<double,2,3> > matrix(3,3); |
| 130 |
matrix = 0; |
| 131 |
Dune::printmatrix(std::cout, matrix, "Matrix<FieldMatrix<double,2,3> >", "--"); |
| 132 |
} |
| 133 |
} |