Flow123d  intersections_paper-476-gbe68821
local_system.cc
Go to the documentation of this file.
1 
2 #include "local_system.hh"
3 
4 #include <armadillo>
5 #include "system/sys_vector.hh"
6 
7 
9 {}
10 
11 
12 LocalSystem::LocalSystem(unsigned int nrows, unsigned int ncols)
13 : row_dofs(nrows),
14  col_dofs(ncols),
15  matrix(nrows, ncols),
16  rhs(nrows),
17  elim_rows(nrows),
18  elim_cols(ncols),
19  solution_rows(nrows),
20  solution_cols(ncols),
21  diag_rows(nrows)
22 {
23  reset();
24 }
25 
26 void LocalSystem::set_size(unsigned int nrows, unsigned int ncols) {
27  row_dofs.set_size(nrows);
28  col_dofs.set_size(ncols);
29  matrix.set_size(nrows, ncols);
30  rhs.set_size(nrows);
31  elim_rows.set_size(nrows);
32  elim_cols.set_size(ncols);
33  solution_rows.set_size(nrows);
34  solution_cols.set_size(ncols);
35  diag_rows.set_size(nrows);
36 }
37 
38 
40 {
41  // zeros in local system
42  matrix.zeros();
43  rhs.zeros();
44  // drop all dirichlet values
46 }
47 
48 
49 void LocalSystem::reset(unsigned int nrows, unsigned int ncols)
50 {
51  matrix.set_size(nrows, ncols);
52  rhs.set_size(nrows);
53  row_dofs.resize(matrix.n_rows);
54  col_dofs.resize(matrix.n_cols);
55  reset();
56 }
57 
58 
59 
60 void LocalSystem::reset(const DofVec &rdofs, const DofVec &cdofs)
61 {
62  set_size(rdofs.n_rows, cdofs.n_rows);
63  reset();
64  row_dofs = rdofs;
65  col_dofs = cdofs;
66 }
67 
68 
69 
70 void LocalSystem::set_solution(unsigned int loc_dof, double solution, double diag)
71 {
72  // check that dofs are same
73  //ASSERT_DBG( arma::all(row_dofs == col_dofs) );
74  set_solution_row(loc_dof, solution, diag);
75  set_solution_col(loc_dof, solution);
76 }
77 
78 void LocalSystem::set_solution_row(uint loc_row, double solution, double diag) {
79  elim_rows[n_elim_rows]=loc_row;
80  solution_rows[n_elim_rows] = solution;
81  diag_rows[n_elim_rows] = diag;
82  n_elim_rows++;
83 }
84 
85 void LocalSystem::set_solution_col(uint loc_col, double solution) {
86  elim_cols[n_elim_cols]=loc_col;
87  solution_cols[n_elim_cols] = solution;
88  n_elim_cols++;
89 }
90 
91 /*
92 
93 void LocalSystem::set_solution(const DofVec & loc_rows, const arma::vec &solution, const arma::vec &diag) {
94  ASSERT_EQ_DBG(loc_rows.n_rows(), solution)
95  set_solution_rows(loc_rows, solution, diag);
96  set_solution_cols()
97 }
98 void LocalSystem::set_solution_rows(DofVec & loc_rows, const arma::vec &solution, const arma::vec &diag);
99 void LocalSystem::set_solution_cols(DofVec & loc_cols, const arma::vec &solution);
100 
101 */
102 
104 {
105  if (! n_elim_rows && ! n_elim_cols ) return;
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  unsigned int 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;
156  n_elim_cols=n_elim_rows=0;
157 
158  //DebugOut() << matrix;
159  //DebugOut() << rhs;
160 
161 
162 }
163 
164 
165 void LocalSystem::add_value(unsigned int row, unsigned int col, double mat_val, double rhs_val)
166 {
167  ASSERT_DBG(row < matrix.n_rows);
168  ASSERT_DBG(col < matrix.n_cols);
169 
170  matrix(row, col) += mat_val;
171  rhs(row) += rhs_val;
172 }
173 
174 void LocalSystem::add_value(unsigned int row, unsigned int col, double mat_val)
175 {
176  ASSERT_DBG(row < matrix.n_rows);
177  ASSERT_DBG(col < matrix.n_cols);
178 
179  matrix(row, col) += mat_val;
180 }
181 
182 void LocalSystem::add_value(unsigned int row, double rhs_val)
183 {
184  ASSERT_DBG(row < matrix.n_rows);
185 
186  rhs(row) += rhs_val;
187 }
188 
189 
190 void LocalSystem::set_matrix(arma::mat m) {
191  ASSERT_EQ_DBG(matrix.n_rows, m.n_rows);
192  ASSERT_EQ_DBG(matrix.n_cols, m.n_cols);
193  matrix = m;
194 }
195 
196 void LocalSystem::set_rhs(arma::vec r) {
197  ASSERT_EQ_DBG(matrix.n_rows, r.n_rows);
198  rhs = r;
199 }
void set_solution_col(uint loc_col, double solution)
Definition: local_system.cc:85
#define ASSERT_EQ_DBG(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:332
DofVec elim_cols
unsigned int uint
void set_matrix(arma::mat matrix)
unsigned int n_elim_cols
arma::uvec DofVec
Definition: local_system.hh:29
void set_size(unsigned int nrows, unsigned int ncols)
Definition: local_system.cc:26
DofVec col_dofs
Definition: local_system.hh:36
void set_solution(unsigned int 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:70
arma::vec rhs
local system RHS
arma::vec solution_rows
unsigned int n_elim_rows
void reset()
Resets the matrix, RHS, dofs to zero and clears solution settings.
Definition: local_system.cc:39
arma::vec diag_rows
LocalSystem()
Default constructor.
Definition: local_system.cc:8
arma::mat matrix
local system matrix
void set_solution_row(uint loc_row, double solution, double diag=0.0)
Definition: local_system.cc:78
Vector classes to support both Iterator, index and Id access and creating co-located vectors...
void set_rhs(arma::vec rhs)
#define ASSERT_DBG(expr)
Definition: asserts.hh:350
arma::vec solution_cols
DofVec elim_rows
void add_value(unsigned int row, unsigned int col, double mat_val, double rhs_val)
Adds a single entry into the local system.
void eliminate_solution()
DofVec row_dofs
Definition: local_system.hh:36