MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
blockmatrix.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #include "../general/array.hpp"
13 #include "../general/globals.hpp"
14 #include "matrix.hpp"
15 #include "sparsemat.hpp"
16 #include "blockvector.hpp"
17 #include "blockmatrix.hpp"
18 
19 namespace mfem
20 {
21 
23  AbstractSparseMatrix(offsets.Last()),
24  owns_blocks(false),
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)
30 {
31  Aij = (SparseMatrix *)NULL;
32 }
33 
34 BlockMatrix::BlockMatrix(const Array<int> & row_offsets_,
35  const Array<int> & col_offsets_):
36  AbstractSparseMatrix(row_offsets_.Last(), col_offsets_.Last()),
37  owns_blocks(false),
38  nRowBlocks(row_offsets_.Size()-1),
39  nColBlocks(col_offsets_.Size()-1),
40  row_offsets(const_cast< Array<int>& >(row_offsets_).GetData(),
41  row_offsets_.Size()),
42  col_offsets(const_cast< Array<int>& >(col_offsets_).GetData(),
43  col_offsets_.Size()),
44  Aij(nRowBlocks, nColBlocks)
45 {
46  Aij = (SparseMatrix *)NULL;
47 }
48 
49 
51 {
52  if (owns_blocks)
53  for (SparseMatrix ** it = Aij.GetRow(0);
54  it != Aij.GetRow(0)+(Aij.NumRows()*Aij.NumCols()); ++it)
55  {
56  delete *it;
57  }
58 }
59 
60 void BlockMatrix::SetBlock(int i, int j, SparseMatrix * mat)
61 {
62 #ifdef MFEM_DEBUG
63  if (nRowBlocks <= i || nColBlocks <= j)
64  {
65  mfem_error("BlockMatrix::SetBlock #0");
66  }
67 
68  if (mat->Height() != row_offsets[i+1] - row_offsets[i])
69  {
70  mfem_error("BlockMatrix::SetBlock #1");
71  }
72 
73  if (mat->Width() != col_offsets[j+1] - col_offsets[j])
74  {
75  mfem_error("BlockMatrix::SetBlock #2");
76  }
77 #endif
78  Aij(i,j) = mat;
79 }
80 
82 {
83 #ifdef MFEM_DEBUG
84  if (nRowBlocks <= i || nColBlocks <= j)
85  {
86  mfem_error("BlockMatrix::Block #0");
87  }
88 
89  if (IsZeroBlock(i,j))
90  {
91  mfem_error("BlockMatrix::Block #1");
92  }
93 #endif
94  return *Aij(i,j);
95 }
96 
97 const SparseMatrix & BlockMatrix::GetBlock(int i, int j) const
98 {
99 #ifdef MFEM_DEBUG
100  if (nRowBlocks <= i || nColBlocks <= j)
101  {
102  mfem_error("BlockMatrix::Block const #0");
103  }
104 
105  if (IsZeroBlock(i,j))
106  {
107  mfem_error("BlockMatrix::Block const #1");
108  }
109 #endif
110 
111  return *Aij(i,j);
112 }
113 
114 
116 {
117  int nnz_elem = 0;
118  for (int jcol = 0; jcol != nColBlocks; ++jcol)
119  for (int irow = 0; irow != nRowBlocks; ++irow)
120  {
121  if (Aij(irow,jcol))
122  {
123  nnz_elem+= Aij(irow,jcol)->NumNonZeroElems();
124  }
125  }
126  return nnz_elem;
127 }
128 
129 
130 double& BlockMatrix::Elem (int i, int j)
131 {
132  int iloc, iblock;
133  int jloc, jblock;
134 
135  findGlobalRow(i, iblock, iloc);
136  findGlobalCol(j, jblock, jloc);
137 
138  if (IsZeroBlock(i, j))
139  {
140  mfem_error("BlockMatrix::Elem");
141  }
142 
143  return Aij(iblock, jblock)->Elem(iloc, jloc);
144 }
145 
146 const double& BlockMatrix::Elem (int i, int j) const
147 {
148  int iloc, iblock;
149  int jloc, jblock;
150 
151  findGlobalRow(i, iblock, iloc);
152  findGlobalCol(j, jblock, jloc);
153 
154  if (IsZeroBlock(i, j))
155  {
156  mfem_error("BlockMatrix::Elem");
157  }
158 
159  return Aij(iblock, jblock)->Elem(iloc, jloc);
160 }
161 
162 int BlockMatrix::RowSize(const int i) const
163 {
164  int rowsize = 0;
165 
166  int iblock, iloc;
167  findGlobalRow(i, iblock, iloc);
168 
169  for (int jblock = 0; jblock < nColBlocks; ++jblock)
170  if (Aij(iblock,jblock) != NULL)
171  {
172  rowsize += Aij(iblock,jblock)->RowSize(iloc);
173  }
174 
175  return rowsize;
176 }
177 
178 int BlockMatrix::GetRow(const int row, Array<int> &cols, Vector &srow) const
179 {
180  int iblock, iloc, rowsize;
181  findGlobalRow(row, iblock, iloc);
182  rowsize = RowSize(row);
183  cols.SetSize(rowsize);
184  srow.SetSize(rowsize);
185 
186  Array<int> bcols;
187  Vector bsrow;
188 
189  int * it_cols = cols.GetData();
190  double *it_srow = srow.GetData();
191 
192  for (int jblock = 0; jblock < nColBlocks; ++jblock)
193  if (Aij(iblock,jblock) != NULL)
194  {
195  Aij(iblock,jblock)->GetRow(iloc, bcols, bsrow);
196  for (int i = 0; i < bcols.Size(); ++i)
197  {
198  *(it_cols++) = bcols[i] + col_offsets[jblock];
199  *(it_srow++) = bsrow(i);
200  }
201  }
202 
203  return 0;
204 }
205 
207  Vector & rhs)
208 {
209  if (nRowBlocks != nColBlocks)
210  {
211  mfem_error("BlockMatrix::EliminateRowCol: nRowBlocks != nColBlocks");
212  }
213 
214  for (int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
215  if (row_offsets[iiblock] != col_offsets[iiblock])
216  {
217  mfem::out << "BlockMatrix::EliminateRowCol: row_offests["
218  << iiblock << "] != col_offsets["<<iiblock<<"]\n";
219  mfem_error();
220  }
221 
222  // We also have to do the same for each Aij
223  Array<int> block_dofs;
224  Vector block_sol, block_rhs;
225 
226  for (int iiblock = 0; iiblock < nRowBlocks; ++iiblock)
227  {
228  int dsize = row_offsets[iiblock+1] - row_offsets[iiblock];
229  block_dofs.MakeRef(ess_bc_dofs.GetData()+row_offsets[iiblock], dsize);
230  block_sol.SetDataAndSize(sol.GetData()+row_offsets[iiblock], dsize);
231  block_rhs.SetDataAndSize(rhs.GetData()+row_offsets[iiblock], dsize);
232 
233  if (Aij(iiblock, iiblock))
234  {
235  for (int i = 0; i < block_dofs.Size(); ++i)
236  if (block_dofs[i])
237  {
238  Aij(iiblock, iiblock)->EliminateRowCol(i,block_sol(i), block_rhs);
239  }
240  }
241  else
242  {
243  for (int i = 0; i < block_dofs.Size(); ++i)
244  if (block_dofs[i])
245  {
246  mfem_error("BlockMatrix::EliminateRowCol: Null diagonal block \n");
247  }
248  }
249 
250  for (int jjblock = 0; jjblock < nRowBlocks; ++jjblock)
251  {
252  if (jjblock != iiblock && Aij(iiblock, jjblock))
253  {
254  for (int i = 0; i < block_dofs.Size(); ++i)
255  if (block_dofs[i])
256  {
257  Aij(iiblock, jjblock)->EliminateRow(i);
258  }
259  }
260  if (jjblock != iiblock && Aij(jjblock, iiblock))
261  {
262  block_rhs.SetDataAndSize(rhs.GetData()+row_offsets[jjblock],
263  row_offsets[jjblock+1] - row_offsets[jjblock]);
264  Aij(jjblock, iiblock)->EliminateCols(block_dofs, &block_sol, &block_rhs);
265  }
266  }
267  }
268 }
269 
271 {
272  if (nRowBlocks != nColBlocks)
273  {
274  mfem_error("BlockMatrix::EliminateZeroRows() #1");
275  }
276 
277  for (int iblock = 0; iblock < nRowBlocks; ++iblock)
278  {
279  if (Aij(iblock,iblock))
280  {
281  double norm;
282  for (int i = 0; i < Aij(iblock, iblock)->NumRows(); ++i)
283  {
284  norm = 0.;
285  for (int jblock = 0; jblock < nColBlocks; ++jblock)
286  if (Aij(iblock,jblock))
287  {
288  norm += Aij(iblock,jblock)->GetRowNorml1(i);
289  }
290 
291  if (norm < 1e-12)
292  {
293  for (int jblock = 0; jblock < nColBlocks; ++jblock)
294  if (Aij(iblock,jblock))
295  {
296  Aij(iblock,jblock)->EliminateRow(i, iblock==jblock);
297  }
298  }
299  }
300  }
301  else
302  {
303  double norm;
304  for (int i = 0; i < row_offsets[iblock+1] - row_offsets[iblock]; ++i)
305  {
306  norm = 0.;
307  for (int jblock = 0; jblock < nColBlocks; ++jblock)
308  if (Aij(iblock,jblock))
309  {
310  norm += Aij(iblock,jblock)->GetRowNorml1(i);
311  }
312 
313  if (norm < 1e-12)
314  {
315  mfem::out<<"i = " << i << "\n";
316  mfem::out<<"norm = " << norm << "\n";
317  mfem_error("BlockMatrix::EliminateZeroRows() #2");
318  }
319  }
320  }
321  }
322 }
323 
324 void BlockMatrix::Mult(const Vector & x, Vector & y) const
325 {
326  if (x.GetData() == y.GetData())
327  {
328  mfem_error("Error: x and y can't point to the same datas \n");
329  }
330 
331  y = 0.;
332  AddMult(x, y, 1.0);
333 }
334 
335 void BlockMatrix::AddMult(const Vector & x, Vector & y, const double val) const
336 {
337  if (x.GetData() == y.GetData())
338  {
339  mfem_error("Error: x and y can't point to the same datas \n");
340  }
341 
342  Vector xblockview, yblockview;
343 
344  for (int iblock = 0; iblock != nRowBlocks; ++iblock)
345  {
346  yblockview.SetDataAndSize(y.GetData() + row_offsets[iblock],
347  row_offsets[iblock+1] - row_offsets[iblock]);
348 
349  for (int jblock = 0; jblock != nColBlocks; ++jblock)
350  {
351  if (Aij(iblock, jblock) != NULL)
352  {
353  xblockview.SetDataAndSize(
354  x.GetData() + col_offsets[jblock],
355  col_offsets[jblock+1] - col_offsets[jblock]);
356 
357  Aij(iblock, jblock)->AddMult(xblockview, yblockview, val);
358  }
359  }
360  }
361 }
362 
363 void BlockMatrix::MultTranspose(const Vector & x, Vector & y) const
364 {
365  if (x.GetData() == y.GetData())
366  {
367  mfem_error("Error: x and y can't point to the same datas \n");
368  }
369 
370  y = 0.;
371  AddMultTranspose(x, y, 1.0);
372 }
373 
375  const double val) const
376 {
377  if (x.GetData() == y.GetData())
378  {
379  mfem_error("Error: x and y can't point to the same datas \n");
380  }
381 
382  Vector xblockview, yblockview;
383 
384  for (int iblock = 0; iblock != nColBlocks; ++iblock)
385  {
386  yblockview.SetDataAndSize(y.GetData() + col_offsets[iblock],
387  col_offsets[iblock+1] - col_offsets[iblock]);
388 
389  for (int jblock = 0; jblock != nRowBlocks; ++jblock)
390  {
391  if (Aij(jblock, iblock) != NULL)
392  {
393  xblockview.SetDataAndSize(
394  x.GetData() + row_offsets[jblock],
395  row_offsets[jblock+1] - row_offsets[jblock]);
396 
397  Aij(jblock, iblock)->AddMultTranspose(xblockview, yblockview, val);
398  }
399  }
400  }
401 }
402 
404 {
405  int nnz = NumNonZeroElems();
406 
407  int * i_amono = new int[ row_offsets[nRowBlocks]+2 ];
408  int * j_amono = new int[ nnz ];
409  double * data = new double[ nnz ];
410 
411  for (int i = 0; i < row_offsets[nRowBlocks]+2; i++)
412  {
413  i_amono[i] = 0;
414  }
415 
416  int * i_amono_construction = i_amono+1;
417 
418  int * i_it(i_amono_construction);
419 
420  for (int iblock = 0; iblock != nRowBlocks; ++iblock)
421  {
422  for (int irow(row_offsets[iblock]); irow < row_offsets[iblock+1]; ++irow)
423  {
424  int local_row = irow - row_offsets[iblock];
425  int ind = i_amono_construction[irow];
426  for (int jblock = 0; jblock < nColBlocks; ++jblock)
427  {
428  if (Aij(iblock,jblock) != NULL)
429  ind += Aij(iblock, jblock)->GetI()[local_row+1]
430  - Aij(iblock, jblock)->GetI()[local_row];
431  }
432  i_amono_construction[irow+1] = ind;
433  }
434  }
435 
436  // Fill in the jarray and copy the data
437  for (int iblock = 0; iblock != nRowBlocks; ++iblock)
438  {
439  for (int jblock = 0; jblock != nColBlocks; ++jblock)
440  {
441  if (Aij(iblock,jblock) != NULL)
442  {
443  int nrow = row_offsets[iblock+1]-row_offsets[iblock];
444  int * i_aij = Aij(iblock, jblock)->GetI();
445  int * j_aij = Aij(iblock, jblock)->GetJ();
446  double * data_aij = Aij(iblock, jblock)->GetData();
447  i_it = i_amono_construction+row_offsets[iblock];
448 
449  int loc_start_index = 0;
450  int loc_end_index = 0;
451  int glob_start_index = 0;
452 
453  int shift(col_offsets[jblock]);
454  for (int * i_it_aij(i_aij+1); i_it_aij != i_aij+nrow+1; ++i_it_aij)
455  {
456  glob_start_index = *i_it;
457 
458 #ifdef MFEM_DEBUG
459  if (glob_start_index > nnz)
460  {
461  mfem::out<<"glob_start_index = " << glob_start_index << "\n";
462  mfem::out<<"Block:" << iblock << " " << jblock << "\n";
463  mfem::out<<std::endl;
464  }
465 #endif
466  loc_end_index = *(i_it_aij);
467  for (int cnt = 0; cnt < loc_end_index-loc_start_index; cnt++)
468  {
469  data[glob_start_index+cnt] = data_aij[loc_start_index+cnt];
470  j_amono[glob_start_index+cnt] = j_aij[loc_start_index+cnt] + shift;
471  }
472 
473  *i_it += loc_end_index-loc_start_index;
474  ++i_it;
475  loc_start_index = loc_end_index;
476  }
477  }
478  }
479  }
480 
481  return new SparseMatrix(i_amono, j_amono, data, row_offsets[nRowBlocks],
482  col_offsets[nColBlocks]);
483 }
484 
485 void BlockMatrix::PrintMatlab(std::ostream & os) const
486 {
487 
488  Vector row_data;
489  Array<int> row_ind;
490  int nnz_elem = NumNonZeroElems();
491  os<<"% size " << row_offsets.Last() << " " << col_offsets.Last() << "\n";
492  os<<"% Non Zeros " << nnz_elem << "\n";
493  int i, j;
494  std::ios::fmtflags old_fmt = os.flags();
495  os.setf(std::ios::scientific);
496  std::streamsize old_prec = os.precision(14);
497  for (i = 0; i < row_offsets.Last(); i++)
498  {
499  GetRow(i, row_ind, row_data);
500  for (j = 0; j < row_ind.Size(); j++)
501  {
502  os << i+1 << " " << row_ind[j]+1 << " " << row_data[j] << std::endl;
503  }
504  }
505 
506  os.precision(old_prec);
507  os.flags(old_fmt);
508 }
509 
511 {
512  BlockMatrix * At = new BlockMatrix(A.ColOffsets(), A.RowOffsets());
513  At->owns_blocks = 1;
514 
515  for (int irowAt = 0; irowAt < At->NumRowBlocks(); ++irowAt)
516  for (int jcolAt = 0; jcolAt < At->NumColBlocks(); ++jcolAt)
517  if (!A.IsZeroBlock(jcolAt, irowAt))
518  {
519  At->SetBlock(irowAt, jcolAt, Transpose(A.GetBlock(jcolAt, irowAt)));
520  }
521  return At;
522 }
523 
524 BlockMatrix * Mult(const BlockMatrix & A, const BlockMatrix & B)
525 {
526  BlockMatrix * C= new BlockMatrix(A.RowOffsets(), B.ColOffsets());
527  C->owns_blocks = 1;
528  Array<SparseMatrix *> CijPieces(A.NumColBlocks());
529 
530  for (int irowC = 0; irowC < A.NumRowBlocks(); ++irowC)
531  for (int jcolC = 0; jcolC < B.NumColBlocks(); ++jcolC)
532  {
533  CijPieces.SetSize(0, static_cast<SparseMatrix *>(NULL));
534  for (int k = 0; k < A.NumColBlocks(); ++k)
535  if (!A.IsZeroBlock(irowC, k) && !B.IsZeroBlock(k, jcolC))
536  {
537  CijPieces.Append(Mult(A.GetBlock(irowC, k), B.GetBlock(k, jcolC)));
538  }
539 
540  if (CijPieces.Size() > 1)
541  {
542  C->SetBlock(irowC, jcolC, Add(CijPieces));
543  for (SparseMatrix ** it = CijPieces.GetData();
544  it != CijPieces.GetData()+CijPieces.Size(); ++it)
545  {
546  delete *it;
547  }
548  }
549  else if (CijPieces.Size() == 1)
550  {
551  C->SetBlock(irowC, jcolC, CijPieces[0]);
552  }
553  }
554 
555  return C;
556 }
557 
558 }
int Size() const
Logical size of the array.
Definition: array.hpp:110
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.
Definition: vector.hpp:320
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
Definition: operator.hpp:42
Abstract data type for sparse matrices.
Definition: matrix.hpp:69
T * GetData()
Returns the data.
Definition: array.hpp:92
BlockMatrix(const Array< int > &offsets)
Constructor for square block matrices.
Definition: blockmatrix.cpp:22
double * GetData() const
Definition: vector.hpp:121
virtual void AddMultTranspose(const Vector &x, Vector &y, const double val=1.) const
MatrixTranspose-Vector Multiplication y = y + val*A&#39;*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.
Definition: densemat.cpp:2942
void SetBlock(int i, int j, SparseMatrix *mat)
Set A(i,j) = mat.
Definition: blockmatrix.cpp:60
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
virtual void EliminateZeroRows()
Data type sparse matrix.
Definition: sparsemat.hpp:38
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition: operator.hpp:36
int IsZeroBlock(int i, int j) const
Check if block (i,j) is a zero block.
Definition: blockmatrix.hpp:52
int NumRowBlocks() const
Return the number of row blocks.
Definition: blockmatrix.hpp:43
Array< int > & ColOffsets()
Return the columns offsets for block starts.
Definition: blockmatrix.hpp:56
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:408
void PrintMatlab(std::ostream &os=mfem::out) const
Export the monolithic matrix to file.
Array< int > & RowOffsets()
Return the row offsets for block starts.
Definition: blockmatrix.hpp:54
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.
Definition: blockmatrix.cpp:50
double * GetData() const
Return element data, i.e. array A.
Definition: sparsemat.hpp:135
virtual void MultTranspose(const Vector &x, Vector &y) const
MatrixTranspose-Vector Multiplication y = A&#39;*x.
virtual void Mult(const Vector &x, Vector &y) const
Matrix-Vector Multiplication y = A*x.
void mfem_error(const char *msg)
Definition: error.cpp:107
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
Definition: array.hpp:503
int NumColBlocks() const
Return the number of column blocks.
Definition: blockmatrix.hpp:45
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
Definition: vector.hpp:94
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.
Definition: blockmatrix.cpp:81
T & Last()
Return the last element in the array.
Definition: array.hpp:581
void MakeRef(T *, int)
Make this Array a reference to a pointer.
Definition: array.hpp:644
Vector data type.
Definition: vector.hpp:41
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...
Definition: globals.hpp:64