13#ifndef MATRIX_BLOCKS_HH
14#define MATRIX_BLOCKS_HH
21#include <dune/istl/bvector.hh>
22#include <dune/istl/bcrsmatrix.hh>
23#include <dune/istl/matrixindexset.hh>
45 for (Index i=0; i<localSizes.size(); ++i) {
46 assert(localSizes[i]>=0);
47 offset_[i+1] = offset_[i] + localSizes[i];
58 return offset_[block];
94 return std::make_pair(block,
offset(block));
102 return offset_.back();
111 return -
offset(block) + offset_[block+1];
119 return offset_.size()-1;
124 std::vector<Index> offset_ {0};
130template <
class Entry,
class Index>
138 if(globalCreator.
cols() < startCol+matrixBlock.
M()) {
139 std::ostringstream message;
140 message <<
"Block (" << matrixBlock.
M() <<
" columns and starting at column " << startCol <<
") does not fit in global matrix (creator) (" << globalCreator.
cols() <<
" columns). ";
143 if(matrixBlock.
getPattern()->isSymmetric() && (startCol != startRow)) {
144 throw LinearAlgebraException(
"Symmetric stored blocks can only be inserted on diagonal.",__FILE__,__LINE__);
147 throw LinearAlgebraException(
"Symmetric stored blocks can only be inserted into symmetric stored Matrices.",__FILE__,__LINE__);
149 for(Index i=0; i<matrixBlock.
N(); ++i) {
150 auto const& rowMb = matrixBlock[i];
151 std::vector<Index> colIdxs(rowMb.size());
152 std::transform(rowMb.getindexptr(), rowMb.getindexptr()+rowMb.size(), colIdxs.begin(), [&](Index
const& colIdx) {
153 return startCol+colIdx;
155 Index rowIdx = startRow+i;
156 globalCreator.
addElements(&rowIdx, (&rowIdx)+1, colIdxs.begin(), colIdxs.end());
163template <
class Entry>
164void insertMatrixBlockIndices(Dune::MatrixIndexSet & globalIndexSet, Dune::BCRSMatrix<Entry>
const& matrixBlock, Dune::MatrixIndexSet::size_type startRow, Dune::MatrixIndexSet::size_type startCol) {
165 if(globalIndexSet.rows() < startRow+matrixBlock.N()) {
166 std::ostringstream message;
167 message <<
"Block (" << matrixBlock.N() <<
" rows and starting at row " << startRow <<
") does not fit in global matrix (index set)(" << globalIndexSet.rows() <<
" rows). ";
175 globalIndexSet.import(matrixBlock, startRow, startCol);
181template <
class BMatrix>
182void insertMatrixBlockEntries(BMatrix & globalMatrix, BMatrix
const& matrixBlock,
typename BMatrix::size_type startRow,
typename BMatrix::size_type startCol) {
183 if(globalMatrix.N() < startRow+matrixBlock.N()) {
184 std::ostringstream message;
185 message <<
"Block (" << matrixBlock.N() <<
" rows and starting at row " << startRow <<
") does not fit in global matrix (" << globalMatrix.N() <<
" rows). ";
188 if(globalMatrix.M() < startCol+matrixBlock.M()) {
189 std::ostringstream message;
190 message <<
"Block (" << matrixBlock.M() <<
" columns and starting at column " << startCol <<
") does not fit in global matrix (" << globalMatrix.M() <<
" columns). ";
193 for(
typename BMatrix::size_type i=0; i<matrixBlock.N(); ++i) {
194 auto const& rowMb = matrixBlock[i];
195 if(rowMb.size() > 0) std::copy(rowMb.getptr(), rowMb.getptr()+rowMb.size(), &(globalMatrix[startRow+i][startCol+rowMb.begin().index()]));
202template <
class BVector>
203void insertVectorBlock(BVector & globalVector, BVector
const& vectorBlock,
typename BVector::size_type startIdx) {
204 if(globalVector.N() < startIdx+vectorBlock.N()) {
205 std::ostringstream message;
206 message <<
"Block (of size " << vectorBlock.N() <<
" and starting at index " << startIdx <<
") does not fit in global vector (of size " << globalVector.N() <<
"). ";
209 for(
typename BVector::size_type i=0; i<vectorBlock.N(); ++i) {
210 globalVector[startIdx+i] = vectorBlock[i];
The GlobalLocalIndexRelationship class provides relationships between local indices of (e....
std::pair< Index, Index > findOffset(Index globalIndex) const
findOffset
Index globalIndex(Index block, Index localIndex) const
globalIndex
Index localSize(Index block) const
localSize
Index numberBlocks() const
numberBlocks
Index globalSize() const
globalSize
GlobalLocalIndexRelationship()
GlobalLocalIndexRelationship(std::vector< Index > const &localSizes)
GlobalLocalIndexRelationship.
std::pair< Index, Index > localIndex(Index globalIndex) const
localIndex
Index offset(Index block) const
offset
A base class for linear algebra exceptions.
std::shared_ptr< NumaCRSPattern< Index > > getPattern() const
Returns a pointer to the sparsity pattern.
Index M() const
The number of columns.
Index N() const
The number of rows.
A NUMA-aware creator for matrix sparsity patterns.
Index cols() const
The number of columns.
void addElements(IterRow const fromRow, IterRow const toRow, IterCol const fromCol, IterCol const toCol, bool colIsSorted=false)
Enters entries into the sparsity pattern.
bool isSymmetric() const
Returns the symmetry status of the pattern.
void insertMatrixBlockIndices(NumaCRSPatternCreator< Index > &globalCreator, NumaBCRSMatrix< Entry, Index > const &matrixBlock, typename NumaBCRSMatrix< Entry, Index >::size_type startRow, typename NumaBCRSMatrix< Entry, Index >::size_type startCol)
void insertVectorBlock(BVector &globalVector, BVector const &vectorBlock, typename BVector::size_type startIdx)
void insertMatrixBlockEntries(BMatrix &globalMatrix, BMatrix const &matrixBlock, typename BMatrix::size_type startRow, typename BMatrix::size_type startCol)