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