Flow123d  release_3.0.0-1193-g9220a69
local_system.hh
Go to the documentation of this file.
1 
2 #ifndef LOCAL_SYSTEM_HH_
3 #define LOCAL_SYSTEM_HH_
4 
5 #include <armadillo>
6 #include "mesh/long_idx.hh"
7 
8 class LinSys;
9 
10 /** Local system class is meant to be used for local assembly and then pass to global linear system.
11  * The key idea is to take care of known solution values (Dirichlet boundary conditions) in a common way.
12  *
13  * Usage of the class consists of 3 steps:
14  * 1) create local system, set global DoFs.
15  * 2) set all known values (Dirichlet BC)
16  * 3) set matrix and RHS entries
17  * (if the entry is on dirichlet row or column, it is now taken care of)
18  * 4) eliminate known solution and possibly fix the diagonal entries of the local system, where Dirichlet BC is set
19  *
20  * TODO:
21  * - set dofs as references
22  * - use arma::vec<int> to keep dofs (efficient, can be constructed from raw arrays)
23  * - use local dof indeces to set solution
24  * - rename set_solution to set_constraint
25  * -
26  */
28 {
29 public:
30  typedef arma::Col<LongIdx> DofVec;
31 
32  /**
33  * Global row and col indices. Are public and can be freely set.
34  * Nevertheless one can also provide reference to already existing arrays through
35  * specific constructor or reset function.
36  */
37  DofVec row_dofs, col_dofs;
38 
39  /**
40  * @brief Default constructor.
41  *
42  * Object must be initialized by subsequent call of reset(nrows, ncols).
43  */
44  LocalSystem();
45 
46  /** @brief Constructor.
47  *
48  * @p nrows is number of rows of local system
49  * @p ncols is number of columns of local system
50  */
51  LocalSystem(unsigned int nrows, unsigned int ncols);
52 
53  /// Resets the matrix, RHS, dofs to zero and clears solution settings
54  void reset();
55 
56  /// Resize and reset.
57  void reset(arma::uword nrows, arma::uword ncols);
58 
59  /**
60  * Resize and reset. Set dofs vectors to reuse arrays provided by given vectors.
61  * Given vectors can not be changed until next call to of any reset function.
62  */
63  void reset(const DofVec &row_dofs, const DofVec &col_dofs);
64 
65  const arma::mat& get_matrix() {return matrix;}
66  const arma::vec& get_rhs() {return rhs;}
67 
68  /** @brief Set the position and value of known solution. E.g. Dirichlet boundary condition.
69  *
70  * @p loc_dofs is local row index in solution vector
71  * @p solution is the values of the solution
72  * @p diag_val is preferred diagonal value on the solution row
73  */
74  void set_solution(unsigned int loc_dof, double solution, double diag=0.0);
75 
76  void set_solution_row(uint loc_row, double solution, double diag=0.0);
77 
78  void set_solution_col(uint loc_col, double solution);
79 
80  //void set_solution(DofVec & loc_rows, const arma::vec &solution, const arma::vec &diag);
81  //void set_solution_rows(DofVec & loc_rows, const arma::vec &solution, const arma::vec &diag);
82  //void set_solution_cols(DofVec & loc_cols, const arma::vec &solution);
83 
84 
85  /**
86  * When finished with assembly of the local system,
87  * this function eliminates all the known dofs.
88  *
89  * It is skipped if there is not any solution dof set.
90  *
91  * During elimination, the (global) diagonal entries on the rows, where the solution is set, might be zero.
92  * Therefore it is necessary to set a proper value to the diagonal entry
93  * and respective RHS entry, such that the given solution holds.
94  * If preferred diagonal value has been set by @p set_solution then it is used.
95  *
96  * Calling this function after the assembly of local system is finished is users's responsibility.
97  */
98  void eliminate_solution();
99 
100  /** @brief Adds a single entry into the local system.
101  *
102  * @p row is local row index of local system
103  * @p col is local column index of local system
104  * @p mat_val is matrix entry value
105  * @p rhs_val is RHS entry value
106  */
107  void add_value(unsigned int row, unsigned int col,
108  double mat_val, double rhs_val);
109 
110  /** @brief Matrix entry.
111  * Adds a single entry into the local system matrix.
112  *
113  * @p row is local row index of local system
114  * @p col is local column index of local system
115  * @p mat_val is matrix entry value
116  */
117  void add_value(unsigned int row, unsigned int col,
118  double mat_val);
119 
120  /** @brief RHS entry.
121  * Adds a single entry into the local system RHS.
122  *
123  * @p row is local row index of local system
124  * @p rhs_val is RHS entry value
125  */
126  void add_value(unsigned int row, double rhs_val);
127 
129  void set_rhs(arma::vec rhs);
130 
131  /// Sets the sparsity pattern for the local system.
132  /** Due to petsc options: MatSetOption(matrix_, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE)
133  * all zeros will be thrown away from the system.
134  * If we do not want some zero entries in the system matrix to be thrown away,
135  * we can set these entries with this almost zero value.
136  *
137  * Almost_zero values will be set in all entries: sp(i,j) != 0
138  */
139  void set_sparsity(const arma::umat & sp);
140 
141 protected:
142  void set_size(unsigned int nrows, unsigned int ncols);
143 
144  arma::mat matrix; ///< local system matrix
145  arma::vec rhs; ///< local system RHS
146 
147  arma::mat sparsity; ///< sparsity pattern
148 
149  unsigned int n_elim_rows, n_elim_cols;
150  DofVec elim_rows;
151  DofVec elim_cols;
155 
156  /// Due to petsc options: MatSetOption(matrix_, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE)
157  /// all zeros will be thrown away from the system.
158  /// If we do not want some zero entries in the system matrix to be thrown away,
159  /// we can set these entries with this almost zero value.
160  ///
161  /// This is done for example when BC values are eliminated and later the BC changes to different type (e.g. seepage).
162  /// Another case is keeping the structure of matrix unchanged for the schur complements -
163  /// for that we fill the whole diagonal (escpecially block C in darcy flow) with artificial zeros.
164  static constexpr double almost_zero = std::numeric_limits<double>::min();
165 
166  /// vector of global row indices where the solution is set (dirichlet BC)
167  //std::vector<unsigned int> loc_solution_dofs;
168  /// vector of solution values at @p global_solution_rows indices (dirichlet BC)
169  //std::vector<double> solution;
170  /// diagonal values for dirichlet BC rows (set in set_solution)
171  //std::vector<double> preferred_diag_values;
172 
173 
174  /**
175  * Optimization. Is false if solution (at least one entry) is known.
176  */
177  //bool solution_not_set;
178 
179 
180  friend class LinSys;
181 
182 
183 };
184 
185 #endif // LOCAL_SYSTEM_HH_
void set_solution_col(uint loc_col, double solution)
Definition: local_system.cc:91
const arma::mat & get_matrix()
Definition: local_system.hh:65
const arma::vec & get_rhs()
Definition: local_system.hh:66
Mat< double, N, M > mat
Definition: armor.hh:214
DofVec elim_cols
unsigned int uint
void set_matrix(arma::mat matrix)
void set_sparsity(const arma::umat &sp)
Sets the sparsity pattern for the local system.
static constexpr double almost_zero
unsigned int n_elim_cols
void set_size(unsigned int nrows, unsigned int ncols)
Definition: local_system.cc:27
Mat< double, N, 1 > vec
Definition: armor.hh:211
DofVec col_dofs
Definition: local_system.hh:37
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:76
arma::Col< LongIdx > DofVec
Definition: local_system.hh:30
arma::vec rhs
local system RHS
arma::vec solution_rows
unsigned int n_elim_rows
arma::mat sparsity
sparsity pattern
void reset()
Resets the matrix, RHS, dofs to zero and clears solution settings.
Definition: local_system.cc:43
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:84
void set_rhs(arma::vec rhs)
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:37