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)
36 nRowBlocks(row_offsets_.Size()-1),
37 nColBlocks(col_offsets_.Size()-1),
38 row_offsets(const_cast<
Array<int>& >(row_offsets_).GetData(), row_offsets_.Size()),
39 col_offsets(const_cast<
Array<int>& >(col_offsets_).GetData(), col_offsets_.Size()),
40 Aij(nRowBlocks, nColBlocks)
50 it != Aij.GetRow(0)+(Aij.NumRows()*Aij.NumCols()); ++it)
57 if (nRowBlocks <= i || nColBlocks <= j)
60 if (mat->
Height() != row_offsets[i+1] - row_offsets[i])
63 if (mat->
Width() != col_offsets[j+1] - col_offsets[j])
72 if (nRowBlocks <= i || nColBlocks <= j)
84 if (nRowBlocks <= i || nColBlocks <= j)
98 for (
int jcol = 0; jcol != nColBlocks; ++jcol)
99 for (
int irow = 0; irow != nRowBlocks; ++irow)
102 nnz_elem+= Aij(irow,jcol)->NumNonZeroElems();
113 findGlobalRow(i, iblock, iloc);
114 findGlobalCol(j, jblock, jloc);
119 return Aij(iblock, jblock)->Elem(iloc, jloc);
127 findGlobalRow(i, iblock, iloc);
128 findGlobalCol(j, jblock, jloc);
133 return Aij(iblock, jblock)->Elem(iloc, jloc);
141 findGlobalRow(i, iblock, iloc);
143 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
144 if (Aij(iblock,jblock) != NULL)
145 rowsize += Aij(iblock,jblock)->RowSize(iloc);
152 int iblock, iloc, rowsize;
153 findGlobalRow(row, iblock, iloc);
161 int * it_cols = cols.
GetData();
162 double *it_srow = srow.
GetData();
164 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
165 if (Aij(iblock,jblock) != NULL)
167 Aij(iblock,jblock)->GetRow(iloc, bcols, bsrow);
168 for (
int i = 0; i < bcols.
Size(); ++i)
170 *(it_cols++) = bcols[i] + col_offsets[jblock];
171 *(it_srow++) = bsrow(i);
180 if (nRowBlocks != nColBlocks)
181 mfem_error(
"BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
183 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
184 if (row_offsets[iiblock] != col_offsets[iiblock])
186 std::cout <<
"BlockMatrix::EliminateRowCol: row_offests["
187 << iiblock <<
"] != col_offsets["<<iiblock<<
"]\n";
193 Vector block_sol, block_rhs;
195 for (
int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
197 int dsize = row_offsets[iiblock+1] - row_offsets[iiblock];
198 block_dofs.
MakeRef(ess_bc_dofs.
GetData()+row_offsets[iiblock], dsize);
202 if (Aij(iiblock, iiblock))
204 for (
int i = 0; i < block_dofs.
Size(); ++i)
206 Aij(iiblock, iiblock)->EliminateRowCol(i,block_sol(i), block_rhs);
210 for (
int i = 0; i < block_dofs.
Size(); ++i)
212 mfem_error(
"BlockMatrix::EliminateRowCol: Null diagonal block \n");
215 for (
int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
217 if (jjblock != iiblock && Aij(iiblock, jjblock))
219 for (
int i = 0; i < block_dofs.
Size(); ++i)
221 Aij(iiblock, jjblock)->EliminateRow(i);
223 if (jjblock != iiblock && Aij(jjblock, iiblock))
226 row_offsets[jjblock+1] - row_offsets[jjblock]);
227 Aij(jjblock, iiblock)->EliminateCols(block_dofs, &block_sol, &block_rhs);
235 if (nRowBlocks != nColBlocks)
236 mfem_error(
"BlockMatrix::EliminateZeroRows() #1");
238 for (
int iblock = 0; iblock < nRowBlocks; ++iblock)
240 if (Aij(iblock,iblock))
243 for (
int i = 0; i < Aij(iblock, iblock)->NumRows(); ++i)
246 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
247 if (Aij(iblock,jblock))
248 norm += Aij(iblock,jblock)->GetRowNorml1(i);
252 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
253 if (Aij(iblock,jblock))
254 Aij(iblock,jblock)->EliminateRow(i, iblock==jblock);
261 for (
int i = 0; i < row_offsets[iblock+1] - row_offsets[iblock]; ++i)
264 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
265 if (Aij(iblock,jblock))
266 norm += Aij(iblock,jblock)->GetRowNorml1(i);
270 std::cout<<
"i = " << i <<
"\n";
271 std::cout<<
"norm = " << norm <<
"\n";
272 mfem_error(
"BlockMatrix::EliminateZeroRows() #2");
282 mfem_error(
"Error: x and y can't point to the same datas \n");
291 mfem_error(
"Error: x and y can't point to the same datas \n");
293 Vector xblockview, yblockview;
295 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
298 row_offsets[iblock+1] - row_offsets[iblock]);
300 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
302 if (Aij(iblock, jblock) != NULL)
305 x.
GetData() + col_offsets[jblock],
306 col_offsets[jblock+1] - col_offsets[jblock]);
308 Aij(iblock, jblock)->AddMult(xblockview, yblockview, val);
317 mfem_error(
"Error: x and y can't point to the same datas \n");
324 const double val)
const
327 mfem_error(
"Error: x and y can't point to the same datas \n");
329 Vector xblockview, yblockview;
331 for (
int iblock = 0; iblock != nColBlocks; ++iblock)
334 col_offsets[iblock+1] - col_offsets[iblock]);
336 for (
int jblock = 0; jblock != nRowBlocks; ++jblock)
338 if (Aij(jblock, iblock) != NULL)
341 x.
GetData() + row_offsets[jblock],
342 row_offsets[jblock+1] - row_offsets[jblock]);
344 Aij(jblock, iblock)->AddMultTranspose(xblockview, yblockview, val);
354 int * i_amono =
new int[ row_offsets[nRowBlocks]+2 ];
355 int * j_amono =
new int[ nnz ];
356 double * data =
new double[ nnz ];
358 for (
int i = 0; i < row_offsets[nRowBlocks]+2; i++)
361 int * i_amono_construction = i_amono+1;
363 int * i_it(i_amono_construction);
365 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
367 for (
int irow(row_offsets[iblock]); irow < row_offsets[iblock+1]; ++irow)
369 int local_row = irow - row_offsets[iblock];
370 int ind = i_amono_construction[irow];
371 for (
int jblock = 0; jblock < nColBlocks; ++jblock)
373 if (Aij(iblock,jblock) != NULL)
374 ind += Aij(iblock, jblock)->GetI()[local_row+1]
375 - Aij(iblock, jblock)->GetI()[local_row];
377 i_amono_construction[irow+1] = ind;
382 for (
int iblock = 0; iblock != nRowBlocks; ++iblock)
384 for (
int jblock = 0; jblock != nColBlocks; ++jblock)
386 if (Aij(iblock,jblock) != NULL)
388 int nrow = row_offsets[iblock+1]-row_offsets[iblock];
389 int * i_aij = Aij(iblock, jblock)->GetI();
390 int * j_aij = Aij(iblock, jblock)->GetJ();
391 double * data_aij = Aij(iblock, jblock)->GetData();
392 i_it = i_amono_construction+row_offsets[iblock];
394 int loc_start_index = 0;
395 int loc_end_index = 0;
396 int glob_start_index = 0;
398 int shift(col_offsets[jblock]);
399 for (
int * i_it_aij(i_aij+1); i_it_aij != i_aij+nrow+1; ++i_it_aij)
401 glob_start_index = *i_it;
404 if (glob_start_index > nnz)
406 std::cout<<
"glob_start_index = " << glob_start_index <<
"\n";
407 std::cout<<
"Block:" << iblock <<
" " << jblock <<
"\n";
408 std::cout<<std::endl;
411 loc_end_index = *(i_it_aij);
412 for (
int cnt = 0; cnt < loc_end_index-loc_start_index; cnt++) {
413 data[glob_start_index+cnt] = data_aij[loc_start_index+cnt];
414 j_amono[glob_start_index+cnt] = j_aij[loc_start_index+cnt] + shift;
417 *i_it += loc_end_index-loc_start_index;
419 loc_start_index = loc_end_index;
425 return new SparseMatrix(i_amono, j_amono, data, row_offsets[nRowBlocks], col_offsets[nColBlocks]);
434 os<<
"% size " << row_offsets.
Last() <<
" " << col_offsets.
Last() <<
"\n";
435 os<<
"% Non Zeros " << nnz_elem <<
"\n";
437 std::ios::fmtflags old_fmt = os.flags();
438 os.setf(std::ios::scientific);
439 std::streamsize old_prec = os.precision(14);
440 for (i = 0; i < row_offsets.
Last(); i++)
442 GetRow(i, row_ind, row_data);
443 for (j = 0; j < row_ind.
Size(); j++)
444 os << i+1 <<
" " << row_ind[j]+1 <<
" " << row_data[j] << std::endl;
447 os.precision(old_prec);
456 for (
int irowAt = 0; irowAt < At->
NumRowBlocks(); ++irowAt)
457 for (
int jcolAt = 0; jcolAt < At->
NumColBlocks(); ++jcolAt)
472 CijPieces.
SetSize(0, static_cast<SparseMatrix *>(NULL));
477 if (CijPieces.Size() > 1)
481 it != CijPieces.GetData()+CijPieces.Size(); ++it)
484 else if (CijPieces.Size() == 1)
486 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)
Resizes 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 poiter.
int RowSize(const int i) const
Return the number of non zeros in row i.