24#ifndef DUMUX_MATRIX_CONVERTER
25#define DUMUX_MATRIX_CONVERTER
29#include <dune/common/indices.hh>
30#include <dune/common/hybridutilities.hh>
31#include <dune/istl/bvector.hh>
32#include <dune/istl/bcrsmatrix.hh>
33#include <dune/istl/matrixindexset.hh>
45template <
class MultiTypeBlockMatrix,
class Scalar=
double>
48 using MatrixBlock =
typename Dune::FieldMatrix<Scalar, 1, 1>;
61 const auto numRows = getNumRows_(A);
64 auto M = BCRSMatrix(numRows, numRows, BCRSMatrix::random);
67 setOccupationPattern_(M, A);
81 static void setOccupationPattern_(BCRSMatrix& M,
const MultiTypeBlockMatrix& A)
84 const auto numRows = M.N();
85 Dune::MatrixIndexSet occupationPattern;
86 occupationPattern.resize(numRows, numRows);
89 auto addIndices = [&](
const auto& subMatrix,
const std::size_t startRow,
const std::size_t startCol)
92 static const Scalar eps = getParam<Scalar>(
"MatrixConverter.DeletePatternEntriesBelowAbsThreshold", -1.0);
94 using BlockType =
typename std::decay_t<
decltype(subMatrix)>::block_type;
95 const auto blockSizeI = BlockType::rows;
96 const auto blockSizeJ = BlockType::cols;
97 for(
auto row = subMatrix.begin(); row != subMatrix.end(); ++row)
98 for(
auto col = row->begin(); col != row->end(); ++col)
99 for(std::size_t i = 0; i < blockSizeI; ++i)
100 for(std::size_t j = 0; j < blockSizeJ; ++j)
101 if(abs(subMatrix[row.index()][col.index()][i][j]) > eps)
102 occupationPattern.add(startRow + row.index()*blockSizeI + i, startCol + col.index()*blockSizeJ + j);
106 using namespace Dune::Hybrid;
107 std::size_t rowIndex = 0;
108 forEach(std::make_index_sequence<MultiTypeBlockMatrix::N()>(), [&A, &addIndices, &rowIndex, numRows](
const auto i)
110 std::size_t colIndex = 0;
111 forEach(A[i], [&](
const auto& subMatrix)
113 addIndices(subMatrix, rowIndex, colIndex);
115 using SubBlockType =
typename std::decay_t<
decltype(subMatrix)>::block_type;
117 colIndex += SubBlockType::cols * subMatrix.M();
120 if(colIndex == numRows)
121 rowIndex += SubBlockType::rows * subMatrix.N();
125 occupationPattern.exportIdx(M);
134 static void copyValues_(BCRSMatrix& M,
const MultiTypeBlockMatrix& A)
137 const auto numRows = M.N();
140 auto copyValues = [&](
const auto& subMatrix,
const std::size_t startRow,
const std::size_t startCol)
143 static const Scalar eps = getParam<Scalar>(
"MatrixConverter.DeletePatternEntriesBelowAbsThreshold", -1.0);
145 using BlockType =
typename std::decay_t<
decltype(subMatrix)>::block_type;
146 const auto blockSizeI = BlockType::rows;
147 const auto blockSizeJ = BlockType::cols;
148 for (
auto row = subMatrix.begin(); row != subMatrix.end(); ++row)
149 for (
auto col = row->begin(); col != row->end(); ++col)
150 for (std::size_t i = 0; i < blockSizeI; ++i)
151 for (std::size_t j = 0; j < blockSizeJ; ++j)
152 if(abs(subMatrix[row.index()][col.index()][i][j]) > eps)
153 M[startRow + row.index()*blockSizeI + i][startCol + col.index()*blockSizeJ + j] = subMatrix[row.index()][col.index()][i][j];
157 using namespace Dune::Hybrid;
158 std::size_t rowIndex = 0;
159 forEach(std::make_index_sequence<MultiTypeBlockMatrix::N()>(), [&A, ©Values, &rowIndex, numRows](
const auto i)
161 std::size_t colIndex = 0;
162 forEach(A[i], [&](
const auto& subMatrix)
164 copyValues(subMatrix, rowIndex, colIndex);
166 using SubBlockType =
typename std::decay_t<
decltype(subMatrix)>::block_type;
168 colIndex += SubBlockType::cols * subMatrix.M();
171 if(colIndex == numRows)
172 rowIndex += SubBlockType::rows * subMatrix.N();
182 static std::size_t getNumRows_(
const MultiTypeBlockMatrix& A)
185 std::size_t numRows = 0;
186 Dune::Hybrid::forEach(Dune::Hybrid::elementAt(A, Dune::Indices::_0), [&numRows](
const auto& subMatrixInFirstRow)
189 const auto numEq = std::decay_t<
decltype(subMatrixInFirstRow)>::block_type::cols;
190 numRows += numEq * subMatrixInFirstRow.M();
202template<
class MultiTypeBlockVector,
class Scalar=
double>
205 using VectorBlock =
typename Dune::FieldVector<Scalar, 1>;
206 using BlockVector =
typename Dune::BlockVector<VectorBlock>;
218 bTmp.resize(b.dim());
220 std::size_t startIndex = 0;
221 Dune::Hybrid::forEach(b, [&](
const auto& subVector)
223 const auto numEq = std::decay_t<
decltype(subVector)>::block_type::size();
225 for(std::size_t i = 0; i < subVector.size(); ++i)
226 for(std::size_t j = 0; j < numEq; ++j)
227 bTmp[startIndex + i*numEq + j] = subVector[i][j];
229 startIndex += numEq*subVector.size();
243 std::size_t startIndex = 0;
244 Dune::Hybrid::forEach(x, [&](
auto& subVector)
246 const auto numEq = std::decay_t<
decltype(subVector)>::block_type::size();
248 for(std::size_t i = 0; i < subVector.size(); ++i)
249 for(std::size_t j = 0; j < numEq; ++j)
250 subVector[i][j] = y[startIndex + i*numEq + j];
252 startIndex += numEq*subVector.size();
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
typename BlockTypeHelper< SolutionVector, Dune::IsNumber< SolutionVector >::value >::type BlockType
Definition: nonlinear/newtonsolver.hh:198
A helper class that converts a Dune::MultiTypeBlockMatrix into a plain Dune::BCRSMatrix TODO: allow b...
Definition: matrixconverter.hh:47
static auto multiTypeToBCRSMatrix(const MultiTypeBlockMatrix &A)
Converts the matrix to a type the IterativeSolverBackend can handle.
Definition: matrixconverter.hh:58
A helper class that converts a Dune::MultiTypeBlockVector into a plain Dune::BlockVector and transfer...
Definition: matrixconverter.hh:204
static void retrieveValues(MultiTypeBlockVector &x, const BlockVector &y)
Copies the entries of a Dune::BlockVector to a Dune::MultiTypeBlockVector.
Definition: matrixconverter.hh:241
static auto multiTypeToBlockVector(const MultiTypeBlockVector &b)
Converts a Dune::MultiTypeBlockVector to a plain 1x1 Dune::BlockVector.
Definition: matrixconverter.hh:215