Flow123d  JS_before_hm-1879-g0c4ed93bc
local_system.cc
Go to the documentation of this file.
1 
2 #include "local_system.hh"
3 
4 #include <armadillo>
5 #include "system/asserts.hh"
6 #include "system/index_types.hh"
7 
8 
10 {}
11 
12 
14 : row_dofs(nrows),
15  col_dofs(ncols),
16  matrix(nrows, ncols),
17  rhs(nrows),
18  sparsity(nrows,ncols),
19  elim_rows(nrows),
20  elim_cols(ncols),
21  solution_rows(nrows),
22  solution_cols(ncols),
23  diag_rows(nrows)
24 {
25  reset();
26 }
27 
28 void LocalSystem::set_size(uint nrows, uint ncols)
29 {
30  row_dofs.set_size(nrows);
31  col_dofs.set_size(ncols);
32  matrix.set_size(nrows, ncols);
33  rhs.set_size(nrows);
34  elim_rows.set_size(nrows);
35  elim_cols.set_size(ncols);
36  solution_rows.set_size(nrows);
37  solution_cols.set_size(ncols);
38  diag_rows.set_size(nrows);
39  // destroy previous sparsity pattern
40  sparsity.set_size(nrows,ncols);
41  sparsity.fill(almost_zero);
42 }
43 
44 
46 {
47  // zeros in local system
48  matrix.zeros();
49  rhs.zeros();
50  // drop all dirichlet values
52 }
53 
54 
55 void LocalSystem::reset(uint nrows, uint ncols)
56 {
57  if(matrix.n_rows != nrows || matrix.n_cols != ncols)
58  {
59  set_size(nrows, ncols);
60  }
61 
62  reset();
63 }
64 
65 
66 
67 void LocalSystem::reset(const LocDofVec &rdofs, const LocDofVec &cdofs)
68 {
69  if(matrix.n_rows != rdofs.n_rows || matrix.n_cols != cdofs.n_rows)
70  {
71  set_size(rdofs.n_rows, cdofs.n_rows);
72  }
73 
74  reset();
75  row_dofs = rdofs;
76  col_dofs = cdofs;
77 }
78 
79 
80 
81 void LocalSystem::set_solution(uint loc_dof, double solution, double diag)
82 {
83  // check that dofs are same
84  //ASSERT_DBG( arma::all(row_dofs == col_dofs) );
85  set_solution_row(loc_dof, solution, diag);
86  set_solution_col(loc_dof, solution);
87 }
88 
89 void LocalSystem::set_solution_row(uint loc_row, double solution, double diag) {
90  elim_rows[n_elim_rows]=loc_row;
91  solution_rows[n_elim_rows] = solution;
92  diag_rows[n_elim_rows] = diag;
93  n_elim_rows++;
94 }
95 
96 void LocalSystem::set_solution_col(uint loc_col, double solution) {
97  elim_cols[n_elim_cols]=loc_col;
98  solution_cols[n_elim_cols] = solution;
99  n_elim_cols++;
100 }
101 
103 {
104  //if there is solution set, eliminate:
105  if (n_elim_rows || n_elim_cols )
106  {
107  //DebugOut().fmt("elim rows: {} elim_cols: {}", n_elim_rows, n_elim_cols);
108 
109  arma::mat tmp_mat = matrix;
110  arma::vec tmp_rhs = rhs;
111 
112  uint ic, ir, row, col;
113 
114  // eliminate columns
115  for(ic=0; ic < n_elim_cols; ic++) {
116  col = elim_cols[ic];
117  tmp_rhs -= solution_cols[ic] * tmp_mat.col( col );
118  tmp_mat.col( col ).zeros();
119  }
120 
121  // eliminate rows
122  for(ir=0; ir < n_elim_rows; ir++) {
123  row = elim_rows[ir];
124  tmp_rhs( row ) = 0.0;
125  tmp_mat.row( row ).zeros();
126 
127  // fix global diagonal
128  for(ic=0; ic < n_elim_cols; ic++) {
129  col = elim_cols[ic];
130  if (row_dofs[row] == col_dofs[col]) {
131  ASSERT_DBG(fabs(solution_rows[ir] - solution_cols[ic]) <1e-12 );
132  // if preferred value is not set, then try using matrix value
133  double new_diagonal = matrix(row, col);
134 
135  if (diag_rows[ir] != 0.0) // if preferred value is set
136  new_diagonal = diag_rows[ir];
137  else if(new_diagonal == 0.0) // if an assembled value is not available
138  new_diagonal = 1.0;
139  // double new_diagonal = fabs(matrix(sol_row,col));
140  // if (new_diagonal == 0.0) {
141  // if (matrix.is_square()) {
142  // new_diagonal = arma::sum( abs(matrix.diag())) / matrix.n_rows;
143  // } else {
144  // new_diagonal = arma::accu( abs(matrix) ) / matrix.n_elem;
145  // }
146  // }
147  tmp_mat(row,col) = new_diagonal;
148  tmp_rhs(row) = new_diagonal * solution_rows[ir];
149 
150  }
151  }
152  }
153 
154  matrix = tmp_mat;
155  rhs = tmp_rhs;
157  }
158 
159  // filling almost_zero according to sparsity pattern
160  ASSERT_EQ_DBG(matrix.n_rows, sparsity.n_rows);
161  ASSERT_EQ_DBG(matrix.n_cols, sparsity.n_cols);
162  matrix = matrix + sparsity;
163 
164  //DebugOut() << matrix;
165  //DebugOut() << rhs;
166 }
167 
168 
169 void LocalSystem::add_value(uint row, uint col, double mat_val, double rhs_val)
170 {
171  ASSERT_DBG(row < matrix.n_rows);
172  ASSERT_DBG(col < matrix.n_cols);
173  ASSERT_DBG(sparsity(row,col))(row)(col).error("Violation of sparsity pattern.");
174 
175  matrix(row, col) += mat_val;
176  rhs(row) += rhs_val;
177 }
178 
179 void LocalSystem::add_value(uint row, uint col, double mat_val)
180 {
181  ASSERT_DBG(row < matrix.n_rows);
182  ASSERT_DBG(col < matrix.n_cols);
183  ASSERT_DBG(sparsity(row,col))(row)(col).error("Violation of sparsity pattern.");
184 
185  matrix(row, col) += mat_val;
186 }
187 
188 void LocalSystem::add_value(uint row, double rhs_val)
189 {
190  ASSERT_DBG(row < matrix.n_rows);
191 
192  rhs(row) += rhs_val;
193 }
194 
195 
197  ASSERT_EQ_DBG(matrix.n_rows, m.n_rows);
198  ASSERT_EQ_DBG(matrix.n_cols, m.n_cols);
199  matrix = m;
200 }
201 
203  ASSERT_EQ_DBG(matrix.n_rows, r.n_rows);
204  rhs = r;
205 }
206 
207 void LocalSystem::set_sparsity(const arma::umat & sp)
208 {
209  ASSERT_EQ_DBG(sparsity.n_rows, sp.n_rows);
210  ASSERT_EQ_DBG(sparsity.n_cols, sp.n_cols);
211 
212  sparsity.zeros();
213  for(uint i=0; i < sp.n_rows; i++)
214  for(uint j=0; j < sp.n_cols; j++)
215  if( sp(i,j) != 0 )
216  sparsity(i,j) = almost_zero;
217 // sparsity.print("sparsity");
218 }
219 
220 void LocalSystem::compute_schur_complement(uint offset, LocalSystem& schur, bool negative) const
221 {
222  // only for square matrix
223  ASSERT_EQ_DBG(matrix.n_rows, matrix.n_cols)("Cannot compute Schur complement for non-square matrix.");
224  arma::uword n = matrix.n_rows - 1;
225  ASSERT_LT_DBG(offset, n)("Schur complement (offset) dimension mismatch.");
226 
227  // B * invA
228  arma::mat BinvA = matrix.submat(offset, 0, n, offset-1) * matrix.submat(0, 0, offset-1, offset-1).i();
229 
230  // Schur complement S = C - B * invA * Bt
231  schur.matrix = matrix.submat(offset, offset, n, n) - BinvA * matrix.submat(0, offset, offset-1, n);
232  schur.rhs = rhs.subvec(offset, n) - BinvA * rhs.subvec(0, offset-1);
233 
234  if(negative)
235  {
236  schur.matrix = -1.0 * schur.matrix;
237  schur.rhs = -1.0 * schur.rhs;
238  }
239 }
240 
241 void LocalSystem::reconstruct_solution_schur(uint offset, const arma::vec &schur_solution, arma::vec& reconstructed_solution) const
242 {
243  // only for square matrix
244  ASSERT_EQ_DBG(matrix.n_rows, matrix.n_cols)("Cannot compute Schur complement for non-square matrix.");
245  arma::uword n = matrix.n_rows - 1;
246  ASSERT_LT_DBG(offset, n)("Schur complement (offset) dimension mismatch.");
247 
248  reconstructed_solution.set_size(offset);
249  // invA
250  arma::mat invA = matrix.submat(0, 0, offset-1, offset-1).i();
251 
252  // x = invA*b - invA * Bt * schur_solution
253  reconstructed_solution = invA * rhs.subvec(0,offset-1) - invA * matrix.submat(0, offset, offset-1, n) * schur_solution;
254 }
LocDofVec
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
LocalSystem
Definition: local_system.hh:45
LocalSystem::add_value
void add_value(uint row, uint col, double mat_val, double rhs_val)
Adds a single entry into the local system.
Definition: local_system.cc:169
LocalSystem::solution_rows
arma::vec solution_rows
Cols indices of cols to be eliminated.
Definition: local_system.hh:199
asserts.hh
Definitions of ASSERTS.
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
LocalSystem::reconstruct_solution_schur
void reconstruct_solution_schur(uint offset, const arma::vec &schur_solution, arma::vec &reconstructed_solution) const
Reconstructs the solution from the Schur complement solution: x = invA*b - invA * Bt * schur_solution...
Definition: local_system.cc:241
LocalSystem::set_solution
void set_solution(uint loc_dof, double solution, double diag=0.0)
Set the position and value of known solution. E.g. Dirichlet boundary condition.
Definition: local_system.cc:81
LocalSystem::set_matrix
void set_matrix(arma::mat matrix)
Definition: local_system.cc:196
LocalSystem::row_dofs
LocDofVec row_dofs
Definition: local_system.hh:53
ASSERT_EQ_DBG
#define ASSERT_EQ_DBG(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:332
LocalSystem::col_dofs
LocDofVec col_dofs
Definition: local_system.hh:53
LocalSystem::sparsity
arma::mat sparsity
sparsity pattern
Definition: local_system.hh:193
uint
unsigned int uint
Definition: mh_dofhandler.hh:101
LocalSystem::almost_zero
static constexpr double almost_zero
Definition: local_system.hh:184
index_types.hh
LocalSystem::compute_schur_complement
void compute_schur_complement(uint offset, LocalSystem &schur, bool negative=false) const
Computes Schur complement of the local system: S = C - B * invA * Bt Applicable for square matrices....
Definition: local_system.cc:220
Armor::mat
ArmaMat< double, N, M > mat
Definition: armor.hh:888
LocalSystem::matrix
arma::mat matrix
local system matrix
Definition: local_system.hh:190
LocalSystem::elim_cols
LocDofVec elim_cols
Rows indices of rows to be eliminated.
Definition: local_system.hh:198
LocalSystem::set_rhs
void set_rhs(arma::vec rhs)
Definition: local_system.cc:202
local_system.hh
LocalSystem::solution_cols
arma::vec solution_cols
Values of the known solution (for row dofs).
Definition: local_system.hh:200
LocalSystem::rhs
arma::vec rhs
local system RHS
Definition: local_system.hh:191
LocalSystem::eliminate_solution
void eliminate_solution()
Definition: local_system.cc:102
LocalSystem::n_elim_rows
uint n_elim_rows
Number of rows/cols to be eliminated due to known solution.
Definition: local_system.hh:196
LocalSystem::reset
void reset()
Resets the matrix, RHS, dofs to zero and clears solution settings.
Definition: local_system.cc:45
ASSERT_LT_DBG
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:300
IntersectionResult::negative
@ negative
LocalSystem::LocalSystem
LocalSystem()
Default constructor.
Definition: local_system.cc:9
LocalSystem::set_size
void set_size(uint nrows, uint ncols)
Definition: local_system.cc:28
LocalSystem::set_sparsity
void set_sparsity(const arma::umat &sp)
Sets the sparsity pattern for the local system.
Definition: local_system.cc:207
LocalSystem::set_solution_col
void set_solution_col(uint loc_col, double solution)
Definition: local_system.cc:96
LocalSystem::set_solution_row
void set_solution_row(uint loc_row, double solution, double diag=0.0)
Definition: local_system.cc:89
LocalSystem::n_elim_cols
uint n_elim_cols
Definition: local_system.hh:196
LocalSystem::diag_rows
arma::vec diag_rows
Values of the known solution (for col dofs).
Definition: local_system.hh:201
LocalSystem::elim_rows
LocDofVec elim_rows
Definition: local_system.hh:197