12 #include "../general/array.hpp"
24 nRowBlocks(offsets.Size()-1),
25 nColBlocks(offsets.Size()-1),
26 row_offsets(const_cast<
Array<int>& >(offsets).GetData(), offsets.Size()),
27 col_offsets(const_cast<
Array<int>& >(offsets).GetData(), offsets.Size()),
28 Aij(nRowBlocks, nColBlocks)
37 nRowBlocks(row_offsets_.Size()-1),
38 nColBlocks(col_offsets_.Size()-1),
39 row_offsets(const_cast<
Array<int>& >(row_offsets_).GetData(),
41 col_offsets(const_cast<
Array<int>& >(col_offsets_).GetData(),
43 Aij(nRowBlocks, nColBlocks)
53 it != Aij.GetRow(0)+(Aij.NumRows()*Aij.NumCols()); ++it)
62 if (nRowBlocks <= i || nColBlocks <= j)
67 if (mat->
Height() != row_offsets[i+1] - row_offsets[i])
72 if (mat->
Width() != col_offsets[j+1] - col_offsets[j])
83 if (nRowBlocks <= i || nColBlocks <= j)
99 if (nRowBlocks <= i || nColBlocks <= j)
117 for (
int jcol = 0; jcol != nColBlocks; ++jcol)
118 for (
int irow = 0; irow != nRowBlocks; ++irow)
122 nnz_elem+= Aij(irow,jcol)->NumNonZeroElems();
134 findGlobalRow(i, iblock, iloc);
135 findGlobalCol(j, jblock, jloc);
142 return Aij(iblock, jblock)->Elem(iloc, jloc);
150 findGlobalRow(i, iblock, iloc);
151 findGlobalCol(j, jblock, jloc);
158 return Aij(iblock, jblock)->Elem(iloc, jloc);
166 findGlobalRow(i, iblock, iloc);
168 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
169 if (Aij(iblock,jblock) != NULL)
171 rowsize += Aij(iblock,jblock)->RowSize(iloc);
179 int iblock, iloc, rowsize;
180 findGlobalRow(row, iblock, iloc);
188 int * it_cols = cols.
GetData();
189 double *it_srow = srow.
GetData();
191 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
192 if (Aij(iblock,jblock) != NULL)
194 Aij(iblock,jblock)->GetRow(iloc, bcols, bsrow);
195 for (
int i = 0; i < bcols.
Size(); ++i)
197 *(it_cols++) = bcols[i] + col_offsets[jblock];
198 *(it_srow++) = bsrow(i);
208 if (nRowBlocks != nColBlocks)
210 mfem_error(
"BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
213 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
214 if (row_offsets[iiblock] != col_offsets[iiblock])
216 std::cout <<
"BlockMatrix::EliminateRowCol: row_offests["
217 << iiblock <<
"] != col_offsets["<<iiblock<<
"]\n";
223 Vector block_sol, block_rhs;
225 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
227 int dsize = row_offsets[iiblock+1] - row_offsets[iiblock];
228 block_dofs.
MakeRef(ess_bc_dofs.
GetData()+row_offsets[iiblock], dsize);
232 if (Aij(iiblock, iiblock))
234 for (
int i = 0; i < block_dofs.
Size(); ++i)
237 Aij(iiblock, iiblock)->EliminateRowCol(i,block_sol(i), block_rhs);
242 for (
int i = 0; i < block_dofs.
Size(); ++i)
245 mfem_error(
"BlockMatrix::EliminateRowCol: Null diagonal block \n");
249 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
251 if (jjblock != iiblock && Aij(iiblock, jjblock))
253 for (
int i = 0; i < block_dofs.
Size(); ++i)
256 Aij(iiblock, jjblock)->EliminateRow(i);
259 if (jjblock != iiblock && Aij(jjblock, iiblock))
262 row_offsets[jjblock+1] - row_offsets[jjblock]);
263 Aij(jjblock, iiblock)->EliminateCols(block_dofs, &block_sol, &block_rhs);
271 if (nRowBlocks != nColBlocks)
273 mfem_error(
"BlockMatrix::EliminateZeroRows() #1");
276 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
278 if (Aij(iblock,iblock))
281 for (
int i = 0; i < Aij(iblock, iblock)->NumRows(); ++i)
284 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
285 if (Aij(iblock,jblock))
287 norm += Aij(iblock,jblock)->GetRowNorml1(i);
292 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
293 if (Aij(iblock,jblock))
295 Aij(iblock,jblock)->EliminateRow(i, iblock==jblock);
303 for (
int i = 0; i < row_offsets[iblock+1] - row_offsets[iblock]; ++i)
306 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
307 if (Aij(iblock,jblock))
309 norm += Aij(iblock,jblock)->GetRowNorml1(i);
314 std::cout<<
"i = " << i <<
"\n";
315 std::cout<<
"norm = " << norm <<
"\n";
316 mfem_error(
"BlockMatrix::EliminateZeroRows() #2");
327 mfem_error(
"Error: x and y can't point to the same datas \n");
338 mfem_error(
"Error: x and y can't point to the same datas \n");
341 Vector xblockview, yblockview;
343 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
346 row_offsets[iblock+1] - row_offsets[iblock]);
348 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
350 if (Aij(iblock, jblock) != NULL)
353 x.
GetData() + col_offsets[jblock],
354 col_offsets[jblock+1] - col_offsets[jblock]);
356 Aij(iblock, jblock)->AddMult(xblockview, yblockview, val);
366 mfem_error(
"Error: x and y can't point to the same datas \n");
374 const double val)
const
378 mfem_error(
"Error: x and y can't point to the same datas \n");
381 Vector xblockview, yblockview;
383 for (
int iblock = 0; iblock != nColBlocks; ++iblock)
386 col_offsets[iblock+1] - col_offsets[iblock]);
388 for (
int jblock = 0; jblock != nRowBlocks; ++jblock)
390 if (Aij(jblock, iblock) != NULL)
393 x.
GetData() + row_offsets[jblock],
394 row_offsets[jblock+1] - row_offsets[jblock]);
396 Aij(jblock, iblock)->AddMultTranspose(xblockview, yblockview, val);
406 int * i_amono =
new int[ row_offsets[nRowBlocks]+2 ];
407 int * j_amono =
new int[ nnz ];
408 double * data =
new double[ nnz ];
410 for (
int i = 0; i < row_offsets[nRowBlocks]+2; i++)
415 int * i_amono_construction = i_amono+1;
417 int * i_it(i_amono_construction);
419 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
421 for (
int irow(row_offsets[iblock]); irow < row_offsets[iblock+1]; ++irow)
423 int local_row = irow - row_offsets[iblock];
424 int ind = i_amono_construction[irow];
425 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
427 if (Aij(iblock,jblock) != NULL)
428 ind += Aij(iblock, jblock)->GetI()[local_row+1]
429 - Aij(iblock, jblock)->GetI()[local_row];
431 i_amono_construction[irow+1] = ind;
436 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
438 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
440 if (Aij(iblock,jblock) != NULL)
442 int nrow = row_offsets[iblock+1]-row_offsets[iblock];
443 int * i_aij = Aij(iblock, jblock)->GetI();
444 int * j_aij = Aij(iblock, jblock)->GetJ();
445 double * data_aij = Aij(iblock, jblock)->GetData();
446 i_it = i_amono_construction+row_offsets[iblock];
448 int loc_start_index = 0;
449 int loc_end_index = 0;
450 int glob_start_index = 0;
452 int shift(col_offsets[jblock]);
453 for (
int * i_it_aij(i_aij+1); i_it_aij != i_aij+nrow+1; ++i_it_aij)
455 glob_start_index = *i_it;
458 if (glob_start_index > nnz)
460 std::cout<<
"glob_start_index = " << glob_start_index <<
"\n";
461 std::cout<<
"Block:" << iblock <<
" " << jblock <<
"\n";
462 std::cout<<std::endl;
465 loc_end_index = *(i_it_aij);
466 for (
int cnt = 0; cnt < loc_end_index-loc_start_index; cnt++)
468 data[glob_start_index+cnt] = data_aij[loc_start_index+cnt];
469 j_amono[glob_start_index+cnt] = j_aij[loc_start_index+cnt] + shift;
472 *i_it += loc_end_index-loc_start_index;
474 loc_start_index = loc_end_index;
480 return new SparseMatrix(i_amono, j_amono, data, row_offsets[nRowBlocks],
481 col_offsets[nColBlocks]);
490 os<<
"% size " << row_offsets.
Last() <<
" " << col_offsets.
Last() <<
"\n";
491 os<<
"% Non Zeros " << nnz_elem <<
"\n";
493 std::ios::fmtflags old_fmt = os.flags();
494 os.setf(std::ios::scientific);
495 std::streamsize old_prec = os.precision(14);
496 for (i = 0; i < row_offsets.
Last(); i++)
498 GetRow(i, row_ind, row_data);
499 for (j = 0; j < row_ind.
Size(); j++)
501 os << i+1 <<
" " << row_ind[j]+1 <<
" " << row_data[j] << std::endl;
505 os.precision(old_prec);
514 for (
int irowAt = 0; irowAt < At->
NumRowBlocks(); ++irowAt)
515 for (
int jcolAt = 0; jcolAt < At->
NumColBlocks(); ++jcolAt)
532 CijPieces.
SetSize(0, static_cast<SparseMatrix *>(NULL));
539 if (CijPieces.Size() > 1)
543 it != CijPieces.GetData()+CijPieces.Size(); ++it)
548 else if (CijPieces.Size() == 1)
550 C->
SetBlock(irowC, jcolC, CijPieces[0]);
int Size() const
Logical size of the array.
void PrintMatlab(std::ostream &os=std::cout) const
Export the monolithic matrix to file.
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 if the new size is different.
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.
BlockMatrix(const Array< int > &offsets)
Constructor for square block matrices.
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
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 EliminateZeroRows()
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.
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.
Array< int > & RowOffsets()
Return the row offsets for block starts.
void EliminateRowCol(Array< int > &ess_bc_dofs, Vector &sol, Vector &rhs)
Symmetric elimination of the marked degree of freedom.
virtual int NumNonZeroElems() const
Returns the total number of non zeros in the matrix.
virtual ~BlockMatrix()
Destructor.
double * GetData() const
Return element data.
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 mfem_error(const char *msg)
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
int NumColBlocks() const
Return the number of column blocks.
void SetDataAndSize(double *d, int s)
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.