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)
55 it != Aij.GetRow(0)+(Aij.NumRows()*Aij.NumCols()); ++it)
65 if (nRowBlocks <= i || nColBlocks <= j)
70 if (mat->
Height() != row_offsets[i+1] - row_offsets[i])
75 if (mat->
Width() != col_offsets[j+1] - col_offsets[j])
86 if (nRowBlocks <= i || nColBlocks <= j)
102 if (nRowBlocks <= i || nColBlocks <= j)
120 for (
int jcol = 0; jcol != nColBlocks; ++jcol)
122 for (
int irow = 0; irow != nRowBlocks; ++irow)
126 nnz_elem+= Aij(irow,jcol)->NumNonZeroElems();
139 findGlobalRow(i, iblock, iloc);
140 findGlobalCol(j, jblock, jloc);
147 return Aij(iblock, jblock)->Elem(iloc, jloc);
155 findGlobalRow(i, iblock, iloc);
156 findGlobalCol(j, jblock, jloc);
163 return Aij(iblock, jblock)->Elem(iloc, jloc);
171 findGlobalRow(i, iblock, iloc);
173 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
175 if (Aij(iblock,jblock) != NULL)
177 rowsize += Aij(iblock,jblock)->RowSize(iloc);
186 int iblock, iloc, rowsize;
187 findGlobalRow(row, iblock, iloc);
195 int * it_cols = cols.
GetData();
196 double *it_srow = srow.
GetData();
198 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
200 if (Aij(iblock,jblock) != NULL)
202 Aij(iblock,jblock)->GetRow(iloc, bcols, bsrow);
203 for (
int i = 0; i < bcols.
Size(); ++i)
205 *(it_cols++) = bcols[i] + col_offsets[jblock];
206 *(it_srow++) = bsrow(i);
218 for (iiblock = 0; iiblock < nRowBlocks; ++iiblock)
220 idx = rc - row_offsets[iiblock];
221 if (idx < 0 ) {
break; }
224 idx = rc - row_offsets[iiblock];
227 MFEM_ASSERT(nRowBlocks == nColBlocks,
228 "BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
230 MFEM_ASSERT(row_offsets[iiblock] == col_offsets[iiblock],
231 "BlockMatrix::EliminateRowCol: row_offests["
232 << iiblock <<
"] != col_offsets["<<iiblock<<
"]");
234 MFEM_ASSERT(Aij(iiblock, iiblock),
235 "BlockMatrix::EliminateRowCol: Null diagonal block");
238 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
240 if (iiblock == jjblock) {
continue; }
241 if (Aij(iiblock,jjblock)) { Aij(iiblock,jjblock)->EliminateRow(idx); }
243 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
245 if (iiblock == jjblock) {
continue; }
246 if (Aij(jjblock,iiblock)) { Aij(jjblock,iiblock)->EliminateCol(idx); }
248 Aij(iiblock, iiblock)->EliminateRowCol(idx,dpolicy);
254 if (nRowBlocks != nColBlocks)
256 mfem_error(
"BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
259 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
261 if (row_offsets[iiblock] != col_offsets[iiblock])
263 mfem::out <<
"BlockMatrix::EliminateRowCol: row_offests["
264 << iiblock <<
"] != col_offsets["<<iiblock<<
"]\n";
271 Vector block_sol, block_rhs;
273 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
275 int dsize = row_offsets[iiblock+1] - row_offsets[iiblock];
276 block_dofs.
MakeRef(ess_bc_dofs.
GetData()+row_offsets[iiblock], dsize);
280 if (Aij(iiblock, iiblock))
282 for (
int i = 0; i < block_dofs.
Size(); ++i)
286 Aij(iiblock, iiblock)->EliminateRowCol(i,block_sol(i), block_rhs);
292 for (
int i = 0; i < block_dofs.
Size(); ++i)
296 mfem_error(
"BlockMatrix::EliminateRowCol: Null diagonal block \n");
301 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
303 if (jjblock != iiblock && Aij(iiblock, jjblock))
305 for (
int i = 0; i < block_dofs.
Size(); ++i)
309 Aij(iiblock, jjblock)->EliminateRow(i);
313 if (jjblock != iiblock && Aij(jjblock, iiblock))
316 row_offsets[jjblock+1] - row_offsets[jjblock]);
317 Aij(jjblock, iiblock)->EliminateCols(block_dofs, &block_sol, &block_rhs);
325 MFEM_VERIFY(nRowBlocks == nColBlocks,
"not a square matrix");
327 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
329 if (Aij(iblock,iblock))
332 for (
int i = 0; i < Aij(iblock, iblock)->NumRows(); ++i)
335 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
336 if (Aij(iblock,jblock))
338 norm += Aij(iblock,jblock)->GetRowNorml1(i);
341 if (norm <= threshold)
343 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
345 if (Aij(iblock,jblock))
347 Aij(iblock,jblock)->EliminateRow(
357 for (
int i = 0; i < row_offsets[iblock+1] - row_offsets[iblock]; ++i)
360 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
362 if (Aij(iblock,jblock))
364 norm += Aij(iblock,jblock)->GetRowNorml1(i);
368 MFEM_VERIFY(!(norm <= threshold),
"diagonal block is NULL:"
369 " iblock = " << iblock <<
", i = " << i <<
", norm = "
378 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
380 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
382 if (!Aij(iblock,jblock)) {
continue; }
383 if (!Aij(iblock,jblock)->Finalized())
385 Aij(iblock,jblock)->Finalize(skip_zeros, fix_empty_rows);
395 mfem_error(
"Error: x and y can't point to the same datas \n");
398 MFEM_ASSERT(
width == x.
Size(),
"Input vector size (" << x.
Size()
399 <<
") must match matrix width (" <<
width <<
")");
400 MFEM_ASSERT(
height == y.
Size(),
"Output vector size (" << y.
Size()
401 <<
") must match matrix height (" <<
height <<
")");
410 mfem_error(
"Error: x and y can't point to the same datas \n");
413 Vector xblockview, yblockview;
415 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
418 row_offsets[iblock+1] - row_offsets[iblock]);
420 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
422 if (Aij(iblock, jblock) != NULL)
425 x.
GetData() + col_offsets[jblock],
426 col_offsets[jblock+1] - col_offsets[jblock]);
428 Aij(iblock, jblock)->AddMult(xblockview, yblockview, val);
438 mfem_error(
"Error: x and y can't point to the same datas \n");
446 const double val)
const
450 mfem_error(
"Error: x and y can't point to the same datas \n");
453 Vector xblockview, yblockview;
455 for (
int iblock = 0; iblock != nColBlocks; ++iblock)
458 col_offsets[iblock+1] - col_offsets[iblock]);
460 for (
int jblock = 0; jblock != nRowBlocks; ++jblock)
462 if (Aij(jblock, iblock) != NULL)
465 x.
GetData() + row_offsets[jblock],
466 row_offsets[jblock+1] - row_offsets[jblock]);
468 Aij(jblock, iblock)->AddMultTranspose(xblockview, yblockview, val);
478 int * i_amono =
Memory<int>(row_offsets[nRowBlocks]+2);
482 for (
int i = 0; i < row_offsets[nRowBlocks]+2; i++)
487 int * i_amono_construction = i_amono+1;
489 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
491 for (
int irow(row_offsets[iblock]); irow < row_offsets[iblock+1]; ++irow)
493 int local_row = irow - row_offsets[iblock];
494 int ind = i_amono_construction[irow];
495 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
497 if (Aij(iblock,jblock) != NULL)
498 ind += Aij(iblock, jblock)->GetI()[local_row+1]
499 - Aij(iblock, jblock)->GetI()[local_row];
501 i_amono_construction[irow+1] = ind;
506 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
508 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
510 if (Aij(iblock,jblock) != NULL)
512 int nrow = row_offsets[iblock+1]-row_offsets[iblock];
513 int * i_aij = Aij(iblock, jblock)->GetI();
514 int * j_aij = Aij(iblock, jblock)->GetJ();
515 double * data_aij = Aij(iblock, jblock)->GetData();
516 int *i_it = i_amono_construction+row_offsets[iblock];
518 int loc_start_index = 0;
519 int loc_end_index = 0;
520 int glob_start_index = 0;
522 int shift(col_offsets[jblock]);
523 for (
int * i_it_aij(i_aij+1); i_it_aij != i_aij+nrow+1; ++i_it_aij)
525 glob_start_index = *i_it;
528 if (glob_start_index > nnz)
530 mfem::out<<
"glob_start_index = " << glob_start_index <<
"\n";
531 mfem::out<<
"Block:" << iblock <<
" " << jblock <<
"\n";
535 loc_end_index = *(i_it_aij);
536 for (
int cnt = 0; cnt < loc_end_index-loc_start_index; cnt++)
538 data[glob_start_index+cnt] = data_aij[loc_start_index+cnt];
539 j_amono[glob_start_index+cnt] = j_aij[loc_start_index+cnt] + shift;
542 *i_it += loc_end_index-loc_start_index;
544 loc_start_index = loc_end_index;
550 return new SparseMatrix(i_amono, j_amono, data, row_offsets[nRowBlocks],
551 col_offsets[nColBlocks]);
560 os<<
"% size " << row_offsets.
Last() <<
" " << col_offsets.
Last() <<
"\n";
561 os<<
"% Non Zeros " << nnz_elem <<
"\n";
563 std::ios::fmtflags old_fmt = os.flags();
564 os.setf(std::ios::scientific);
565 std::streamsize old_prec = os.precision(14);
566 for (i = 0; i < row_offsets.
Last(); i++)
568 GetRow(i, row_ind, row_data);
569 for (j = 0; j < row_ind.
Size(); j++)
571 os << i+1 <<
" " << row_ind[j]+1 <<
" " << row_data[j] << std::endl;
575 os.precision(old_prec);
584 for (
int irowAt = 0; irowAt < At->
NumRowBlocks(); ++irowAt)
586 for (
int jcolAt = 0; jcolAt < At->
NumColBlocks(); ++jcolAt)
607 CijPieces.
SetSize(0, static_cast<SparseMatrix *>(NULL));
616 if (CijPieces.Size() > 1)
620 it != CijPieces.GetData()+CijPieces.Size(); ++it)
625 else if (CijPieces.Size() == 1)
627 C->
SetBlock(irowC, jcolC, CijPieces[0]);
int Size() const
Return the 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.
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.
double * GetData()
Return the element data, i.e. the array A.
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.
Set the diagonal value to one.
virtual int NumNonZeroElems() const
Returns the total number of non zeros in the matrix.
virtual ~BlockMatrix()
Destructor.
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 the 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.
DiagonalPolicy
Defines operator diagonal policy upon elimination of rows and/or columns.
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...
Set the diagonal value to zero.
int width
Dimension of the input / number of columns in the matrix.