12 #include "../general/array.hpp"
13 #include "../general/globals.hpp"
25 nRowBlocks(offsets.Size()-1),
26 nColBlocks(offsets.Size()-1),
27 row_offsets(const_cast<
Array<int>& >(offsets).GetData(), offsets.Size()),
28 col_offsets(const_cast<
Array<int>& >(offsets).GetData(), offsets.Size()),
29 Aij(nRowBlocks, nColBlocks)
38 nRowBlocks(row_offsets_.Size()-1),
39 nColBlocks(col_offsets_.Size()-1),
40 row_offsets(const_cast<
Array<int>& >(row_offsets_).GetData(),
42 col_offsets(const_cast<
Array<int>& >(col_offsets_).GetData(),
44 Aij(nRowBlocks, nColBlocks)
54 it != Aij.GetRow(0)+(Aij.NumRows()*Aij.NumCols()); ++it)
63 if (nRowBlocks <= i || nColBlocks <= j)
68 if (mat->
Height() != row_offsets[i+1] - row_offsets[i])
73 if (mat->
Width() != col_offsets[j+1] - col_offsets[j])
84 if (nRowBlocks <= i || nColBlocks <= j)
100 if (nRowBlocks <= i || nColBlocks <= j)
118 for (
int jcol = 0; jcol != nColBlocks; ++jcol)
119 for (
int irow = 0; irow != nRowBlocks; ++irow)
123 nnz_elem+= Aij(irow,jcol)->NumNonZeroElems();
135 findGlobalRow(i, iblock, iloc);
136 findGlobalCol(j, jblock, jloc);
143 return Aij(iblock, jblock)->Elem(iloc, jloc);
151 findGlobalRow(i, iblock, iloc);
152 findGlobalCol(j, jblock, jloc);
159 return Aij(iblock, jblock)->Elem(iloc, jloc);
167 findGlobalRow(i, iblock, iloc);
169 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
170 if (Aij(iblock,jblock) != NULL)
172 rowsize += Aij(iblock,jblock)->RowSize(iloc);
180 int iblock, iloc, rowsize;
181 findGlobalRow(row, iblock, iloc);
189 int * it_cols = cols.
GetData();
190 double *it_srow = srow.
GetData();
192 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
193 if (Aij(iblock,jblock) != NULL)
195 Aij(iblock,jblock)->GetRow(iloc, bcols, bsrow);
196 for (
int i = 0; i < bcols.
Size(); ++i)
198 *(it_cols++) = bcols[i] + col_offsets[jblock];
199 *(it_srow++) = bsrow(i);
210 for (iiblock = 0; iiblock < nRowBlocks; ++iiblock)
212 idx = rc - row_offsets[iiblock];
213 if (idx < 0 ) {
break; }
216 idx = rc - row_offsets[iiblock];
219 MFEM_ASSERT(nRowBlocks == nColBlocks,
220 "BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
222 MFEM_ASSERT(row_offsets[iiblock] == col_offsets[iiblock],
223 "BlockMatrix::EliminateRowCol: row_offests["
224 << iiblock <<
"] != col_offsets["<<iiblock<<
"]");
226 MFEM_ASSERT(Aij(iiblock, iiblock),
227 "BlockMatrix::EliminateRowCol: Null diagonal block");
230 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
232 if (iiblock == jjblock) {
continue; }
233 if (Aij(iiblock,jjblock)) { Aij(iiblock,jjblock)->EliminateRow(idx); }
235 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
237 if (iiblock == jjblock) {
continue; }
238 if (Aij(jjblock,iiblock)) { Aij(jjblock,iiblock)->EliminateCol(idx); }
240 Aij(iiblock, iiblock)->EliminateRowCol(idx,dpolicy);
246 if (nRowBlocks != nColBlocks)
248 mfem_error(
"BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
251 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
252 if (row_offsets[iiblock] != col_offsets[iiblock])
254 mfem::out <<
"BlockMatrix::EliminateRowCol: row_offests["
255 << iiblock <<
"] != col_offsets["<<iiblock<<
"]\n";
261 Vector block_sol, block_rhs;
263 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
265 int dsize = row_offsets[iiblock+1] - row_offsets[iiblock];
266 block_dofs.
MakeRef(ess_bc_dofs.
GetData()+row_offsets[iiblock], dsize);
270 if (Aij(iiblock, iiblock))
272 for (
int i = 0; i < block_dofs.
Size(); ++i)
275 Aij(iiblock, iiblock)->EliminateRowCol(i,block_sol(i), block_rhs);
280 for (
int i = 0; i < block_dofs.
Size(); ++i)
283 mfem_error(
"BlockMatrix::EliminateRowCol: Null diagonal block \n");
287 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
289 if (jjblock != iiblock && Aij(iiblock, jjblock))
291 for (
int i = 0; i < block_dofs.
Size(); ++i)
294 Aij(iiblock, jjblock)->EliminateRow(i);
297 if (jjblock != iiblock && Aij(jjblock, iiblock))
300 row_offsets[jjblock+1] - row_offsets[jjblock]);
301 Aij(jjblock, iiblock)->EliminateCols(block_dofs, &block_sol, &block_rhs);
309 MFEM_VERIFY(nRowBlocks == nColBlocks,
"not a square matrix");
311 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
313 if (Aij(iblock,iblock))
316 for (
int i = 0; i < Aij(iblock, iblock)->NumRows(); ++i)
319 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
320 if (Aij(iblock,jblock))
322 norm += Aij(iblock,jblock)->GetRowNorml1(i);
325 if (norm <= threshold)
327 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
328 if (Aij(iblock,jblock))
330 Aij(iblock,jblock)->EliminateRow(
339 for (
int i = 0; i < row_offsets[iblock+1] - row_offsets[iblock]; ++i)
342 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
343 if (Aij(iblock,jblock))
345 norm += Aij(iblock,jblock)->GetRowNorml1(i);
348 MFEM_VERIFY(!(norm <= threshold),
"diagonal block is NULL:"
349 " iblock = " << iblock <<
", i = " << i <<
", norm = "
358 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
360 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
362 if (!Aij(iblock,jblock)) {
continue; }
363 if (!Aij(iblock,jblock)->Finalized())
365 Aij(iblock,jblock)->Finalize(skip_zeros, fix_empty_rows);
375 mfem_error(
"Error: x and y can't point to the same datas \n");
378 MFEM_ASSERT(
width == x.
Size(),
"Input vector size (" << x.
Size()
379 <<
") must match matrix width (" <<
width <<
")");
380 MFEM_ASSERT(
height == y.
Size(),
"Output vector size (" << y.
Size()
381 <<
") must match matrix height (" <<
height <<
")");
390 mfem_error(
"Error: x and y can't point to the same datas \n");
393 Vector xblockview, yblockview;
395 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
398 row_offsets[iblock+1] - row_offsets[iblock]);
400 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
402 if (Aij(iblock, jblock) != NULL)
405 x.
GetData() + col_offsets[jblock],
406 col_offsets[jblock+1] - col_offsets[jblock]);
408 Aij(iblock, jblock)->AddMult(xblockview, yblockview, val);
418 mfem_error(
"Error: x and y can't point to the same datas \n");
426 const double val)
const
430 mfem_error(
"Error: x and y can't point to the same datas \n");
433 Vector xblockview, yblockview;
435 for (
int iblock = 0; iblock != nColBlocks; ++iblock)
438 col_offsets[iblock+1] - col_offsets[iblock]);
440 for (
int jblock = 0; jblock != nRowBlocks; ++jblock)
442 if (Aij(jblock, iblock) != NULL)
445 x.
GetData() + row_offsets[jblock],
446 row_offsets[jblock+1] - row_offsets[jblock]);
448 Aij(jblock, iblock)->AddMultTranspose(xblockview, yblockview, val);
458 int * i_amono =
new int[ row_offsets[nRowBlocks]+2 ];
459 int * j_amono =
new int[ nnz ];
460 double * data =
new double[ nnz ];
462 for (
int i = 0; i < row_offsets[nRowBlocks]+2; i++)
467 int * i_amono_construction = i_amono+1;
469 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
471 for (
int irow(row_offsets[iblock]); irow < row_offsets[iblock+1]; ++irow)
473 int local_row = irow - row_offsets[iblock];
474 int ind = i_amono_construction[irow];
475 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
477 if (Aij(iblock,jblock) != NULL)
478 ind += Aij(iblock, jblock)->GetI()[local_row+1]
479 - Aij(iblock, jblock)->GetI()[local_row];
481 i_amono_construction[irow+1] = ind;
486 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
488 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
490 if (Aij(iblock,jblock) != NULL)
492 int nrow = row_offsets[iblock+1]-row_offsets[iblock];
493 int * i_aij = Aij(iblock, jblock)->GetI();
494 int * j_aij = Aij(iblock, jblock)->GetJ();
495 double * data_aij = Aij(iblock, jblock)->GetData();
496 int *i_it = i_amono_construction+row_offsets[iblock];
498 int loc_start_index = 0;
499 int loc_end_index = 0;
500 int glob_start_index = 0;
502 int shift(col_offsets[jblock]);
503 for (
int * i_it_aij(i_aij+1); i_it_aij != i_aij+nrow+1; ++i_it_aij)
505 glob_start_index = *i_it;
508 if (glob_start_index > nnz)
510 mfem::out<<
"glob_start_index = " << glob_start_index <<
"\n";
511 mfem::out<<
"Block:" << iblock <<
" " << jblock <<
"\n";
515 loc_end_index = *(i_it_aij);
516 for (
int cnt = 0; cnt < loc_end_index-loc_start_index; cnt++)
518 data[glob_start_index+cnt] = data_aij[loc_start_index+cnt];
519 j_amono[glob_start_index+cnt] = j_aij[loc_start_index+cnt] + shift;
522 *i_it += loc_end_index-loc_start_index;
524 loc_start_index = loc_end_index;
530 return new SparseMatrix(i_amono, j_amono, data, row_offsets[nRowBlocks],
531 col_offsets[nColBlocks]);
540 os<<
"% size " << row_offsets.
Last() <<
" " << col_offsets.
Last() <<
"\n";
541 os<<
"% Non Zeros " << nnz_elem <<
"\n";
543 std::ios::fmtflags old_fmt = os.flags();
544 os.setf(std::ios::scientific);
545 std::streamsize old_prec = os.precision(14);
546 for (i = 0; i < row_offsets.
Last(); i++)
548 GetRow(i, row_ind, row_data);
549 for (j = 0; j < row_ind.
Size(); j++)
551 os << i+1 <<
" " << row_ind[j]+1 <<
" " << row_data[j] << std::endl;
555 os.precision(old_prec);
564 for (
int irowAt = 0; irowAt < At->
NumRowBlocks(); ++irowAt)
565 for (
int jcolAt = 0; jcolAt < At->
NumColBlocks(); ++jcolAt)
582 CijPieces.
SetSize(0, static_cast<SparseMatrix *>(NULL));
589 if (CijPieces.Size() > 1)
593 it != CijPieces.GetData()+CijPieces.Size(); ++it)
598 else if (CijPieces.Size() == 1)
600 C->
SetBlock(irowC, jcolC, CijPieces[0]);
int Size() const
Logical size of the array.
SparseMatrix * CreateMonolithic() const
Returns a monolithic CSR matrix that represents this operator.
int owns_blocks
If owns_blocks the SparseMatrix objects Aij will be deallocated.
void SetSize(int s)
Resize the vector to size s.
Set the diagonal value to zero.
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
Abstract data type for sparse matrices.
T * GetData()
Returns the data.
int Size() const
Returns the size of the vector.
BlockMatrix(const Array< int > &offsets)
Constructor for square block matrices.
double * GetData() const
Return a pointer to the beginning of the Vector data.
virtual void AddMultTranspose(const Vector &x, Vector &y, const double val=1.) const
MatrixTranspose-Vector Multiplication y = y + val*A'*x.
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const
Gets the columns indexes and values for row row.
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
void SetBlock(int i, int j, SparseMatrix *mat)
Set A(i,j) = mat.
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
virtual void Finalize(int skip_zeros=1)
Finalize all the submatrices.
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
int IsZeroBlock(int i, int j) const
Check if block (i,j) is a zero block.
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
int NumRowBlocks() const
Return the number of row blocks.
Array< int > & ColOffsets()
Return the columns offsets for block starts.
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
void PrintMatlab(std::ostream &os=mfem::out) const
Export the monolithic matrix to file.
Array< int > & RowOffsets()
Return the row offsets for block starts.
virtual int NumNonZeroElems() const
Returns the total number of non zeros in the matrix.
virtual ~BlockMatrix()
Destructor.
double * GetData() const
Return element data, i.e. array A.
Set the diagonal value to one.
virtual void MultTranspose(const Vector &x, Vector &y) const
MatrixTranspose-Vector Multiplication y = A'*x.
virtual void Mult(const Vector &x, Vector &y) const
Matrix-Vector Multiplication y = A*x.
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
int NumColBlocks() const
Return the number of column blocks.
void EliminateRowCol(int rc, DiagonalPolicy dpolicy=DIAG_ONE)
Eliminate the row and column rc from the matrix.
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
int height
Dimension of the output / number of rows in the matrix.
virtual void AddMult(const Vector &x, Vector &y, const double val=1.) const
Matrix-Vector Multiplication y = y + val*A*x.
SparseMatrix & GetBlock(int i, int j)
Return a reference to block (i,j). Reference may be invalid if Aij(i,j) == NULL.
T & Last()
Return the last element in the array.
void MakeRef(T *, int)
Make this Array a reference to a pointer.
int RowSize(const int i) const
Return the number of non zeros in row i.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
virtual void EliminateZeroRows(const double threshold=1e-12)
If the matrix is square, this method will place 1 on the diagonal (i,i) if row i has "almost" zero l1...
int width
Dimension of the input / number of columns in the matrix.