Line data Source code
1 : /*
2 : * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3 : * Authors: Raphael Prohl, Andreas Vogel
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__LIB_DISC__SPATIAL_DISC__ASS_TUNER__
34 : #define __H__UG__LIB_DISC__SPATIAL_DISC__ASS_TUNER__
35 :
36 : #include "lib_grid/tools/bool_marker.h"
37 : #include "lib_grid/tools/selector_grid.h"
38 : #include "lib_disc/spatial_disc/local_to_global/local_to_global_mapper.h"
39 : #include "lib_disc/spatial_disc/elem_disc/elem_disc_interface.h"
40 :
41 : namespace ug{
42 :
43 : template <typename TDomain, typename TAlgebra>
44 : class IDomainConstraint;
45 :
46 : /// Types of constraint
47 : /**
48 : * These types control the order the constraints are applied in.
49 : * Constraints with a lower number will be applied first. The order of
50 : * constraints with equal number is undefined.
51 : */
52 : enum ConstraintType
53 : {
54 : CT_NONE = 0,
55 : CT_ASSEMBLED = 1,
56 : CT_MAY_DEPEND_ON_HANGING = 1 << 1, // constraints which may depend on hanging DoFs; but NOT vice-versa
57 : CT_HANGING = 1 << 2, // constraint defined for hanging DoFs
58 : CT_CONSTRAINTS = 1 << 3, // any other constraint which MUST NOT depend on a hanging DoF
59 : CT_DIRICHLET = 1 << 4, // Dirichlet constraints
60 : CT_ALL = CT_NONE | CT_ASSEMBLED | CT_MAY_DEPEND_ON_HANGING | CT_HANGING | CT_CONSTRAINTS | CT_DIRICHLET
61 : };
62 :
63 : template <typename TAlgebra>
64 : class LocalToGlobalMapper
65 : {
66 : public:
67 : /// Algebra type
68 : typedef TAlgebra algebra_type;
69 :
70 : /// Type of algebra matrix
71 : typedef typename algebra_type::matrix_type matrix_type;
72 :
73 : /// Type of algebra vector
74 : typedef typename algebra_type::vector_type vector_type;
75 :
76 : public:
77 :
78 : /// adds a local vector to the global one
79 : void add_local_vec_to_global(vector_type& vec, const LocalVector& lvec) const
80 0 : { AddLocalVector(vec, lvec);}
81 :
82 : /// adds a local matrix to the global one
83 : void add_local_mat_to_global(matrix_type& mat, const LocalMatrix& lmat) const
84 0 : { AddLocalMatrixToGlobal(mat, lmat);}
85 : };
86 :
87 : /// The AssemblingTuner class combines tools to adapt the assembling routine.
88 : template <typename TAlgebra>
89 : class AssemblingTuner
90 : {
91 : public:
92 : /// Algebra type
93 : typedef TAlgebra algebra_type;
94 :
95 : /// Type of algebra matrix
96 : typedef typename algebra_type::matrix_type matrix_type;
97 :
98 : /// Type of algebra vector
99 : typedef typename algebra_type::vector_type vector_type;
100 :
101 : /// Type of algebra value
102 : typedef typename vector_type::value_type value_type;
103 :
104 : public:
105 : /// constructor
106 0 : AssemblingTuner(): m_pMapper(nullptr), m_pBoolMarker(NULL), m_pSelector(NULL),
107 0 : m_bSingleAssIndex(false), m_SingleAssIndex(0),
108 0 : m_bForceRegGrid(false), m_bModifySolutionImplemented(false),
109 0 : m_ConstraintTypesEnabled(CT_ALL), m_ElemTypesEnabled(EDT_ALL),
110 0 : m_bMatrixIsConst(false), m_bMatrixStructureIsConst(false), m_bClearOnResize(true) {}
111 :
112 : /// destructor
113 0 : virtual ~AssemblingTuner() {}
114 :
115 : /// set local to global mapping
116 : void set_mapping(ILocalToGlobalMapper<TAlgebra>* pMapper = NULL)
117 : {
118 0 : m_pMapper = pMapper;
119 : }
120 :
121 : /// LocalToGlobalMapper-function calls
122 0 : void add_local_vec_to_global(vector_type& vec, const LocalVector& lvec,
123 : ConstSmartPtr<DoFDistribution> dd) const
124 : {
125 0 : if (m_pMapper)
126 0 : m_pMapper->add_local_vec_to_global(vec, lvec, dd);
127 : else
128 : m_defaultMapper.add_local_vec_to_global(vec, lvec);
129 0 : }
130 :
131 0 : void add_local_mat_to_global(matrix_type& mat, const LocalMatrix& lmat,
132 : ConstSmartPtr<DoFDistribution> dd) const
133 : {
134 0 : if (m_pMapper)
135 0 : m_pMapper->add_local_mat_to_global(mat, lmat, dd);
136 : else
137 : m_defaultMapper.add_local_mat_to_global(mat, lmat);
138 0 : }
139 :
140 0 : void modify_LocalSol(LocalVector& vecMod, const LocalVector& lvec,
141 : ConstSmartPtr<DoFDistribution> dd) const
142 : {
143 0 : if (m_pMapper)
144 0 : m_pMapper->modify_LocalSol(vecMod, lvec, dd);
145 0 : }
146 : /// sets a marker to exclude elements from assembling
147 : /**
148 : * This methods sets a marker. Only elements that are marked will be
149 : * assembled during assembling process. If no marker is set, this
150 : * corresponds to a marker where all elements have been marked.
151 : *
152 : * \param[in] mark BoolMarker
153 : */
154 : void set_marker(BoolMarker* mark = NULL){ m_pBoolMarker = mark; }
155 :
156 : /// sets a selector of elements for assembling
157 : /**
158 : * This methods sets an element list. Only elements of this list will be
159 : * assembled during assembling process. The list especially defines the begin
160 : * and end of the element-iterator in the element assembling-loop.
161 : * If no element list is set, this corresponds to an assembling where the loop is
162 : * carried out over all elements of a subset.
163 : *
164 : * \param[in] sel Selector
165 : */
166 0 : void set_selector(Selector* sel = NULL){ m_pSelector = sel; }
167 :
168 : /// sets an index for which the assembling should be carried out
169 : /**
170 : * This methods sets a boolean if an DoFindex-wise assemble routine should be used.
171 : * This proceeding is e.g. useful for a nonlinear Gauss-Seidel or nonlinear
172 : * Jacobi solver. The specific index is passed.
173 : *
174 : * \param[in] ind DoFIndex
175 : */
176 0 : void disable_single_index_assembling() {m_bSingleAssIndex = false;}
177 : void set_single_index_assembling(const size_t index)
178 : {
179 0 : m_SingleAssIndex = index; m_bSingleAssIndex = true;
180 : }
181 :
182 : /// checks whether the assemble DoFindex is set or not
183 0 : bool single_index_assembling_enabled() const {return m_bSingleAssIndex;}
184 :
185 :
186 : /// enables the usage of modify solution
187 : void enable_modify_solution(bool bEnable) {m_bModifySolutionImplemented = bEnable;}
188 :
189 : /// checks whether the assemble index is set or not
190 0 : bool modify_solution_enabled() const {return m_bModifySolutionImplemented;}
191 :
192 :
193 : /// forces the assembling to consider the grid as regular
194 0 : virtual void set_force_regular_grid(bool bForce) {m_bForceRegGrid = bForce;}
195 :
196 : /// returns if assembling is to considered as regular grid
197 0 : bool regular_grid_forced() const {return m_bForceRegGrid;}
198 :
199 :
200 : /// enables constraints
201 0 : void enable_constraints(int bEnableTypes) {m_ConstraintTypesEnabled = bEnableTypes;}
202 :
203 : /// returns flags of enabled constraints
204 0 : int enabled_constraints() const {return m_ConstraintTypesEnabled;}
205 :
206 : /// returns if constraint type enabled
207 0 : bool constraint_type_enabled(int type) const {return (type & m_ConstraintTypesEnabled);}
208 :
209 :
210 : /// enables elem discs
211 0 : void enable_elem_discs(int bEnableTypes) {m_ElemTypesEnabled = bEnableTypes;}
212 :
213 : /// returns flags of enabled elem discs
214 0 : int enabled_elem_discs() const {return m_ElemTypesEnabled;}
215 :
216 : /// returns if elem disc type enabled
217 0 : bool elem_disc_type_enabled(int type) const {return (type & m_ElemTypesEnabled);}
218 :
219 :
220 : /// resize functions used in assemble funcs
221 : void resize(ConstSmartPtr<DoFDistribution> dd, vector_type& vec) const;
222 : void resize(ConstSmartPtr<DoFDistribution> dd, matrix_type& mat) const;
223 :
224 : /// gets the element iterator from the Selector
225 : template <typename TElem>
226 : void collect_selected_elements(std::vector<TElem*>& vElem, ConstSmartPtr<DoFDistribution> dd, int si) const;
227 :
228 : /// returns if only selected elements used for assembling
229 0 : bool selected_elements_used() const {return (m_pSelector != NULL);}
230 :
231 : /// returns if element is to be used in assembling
232 : template <typename TElem>
233 : bool element_used(TElem* elem) const;
234 :
235 : /// only one index will be set to Dirichlet in case of index-wise assembling
236 : /// instead of setting a complete matrix row to Dirichlet
237 : void set_dirichlet_row(matrix_type& mat, const DoFIndex& ind) const;
238 : void set_dirichlet_val(vector_type& vec, const DoFIndex& ind, const double val) const;
239 :
240 : /// Disable clearing of matrix/vector when resizing.
241 : /// This is useful when an IAssemble object consists of more than one
242 : /// domain disc, e.g., CompositeTimeDisc.
243 0 : void disable_clear_on_resize() {m_bClearOnResize = false;}
244 :
245 : /**
246 : * specify whether matrix will be modified by assembling
247 : * disables matrix assembling if set to true
248 : *
249 : * @param bCh set true if matrix is not to be changed during assembling
250 : */
251 0 : void set_matrix_is_const(bool bCh) {m_bMatrixIsConst = bCh;}
252 :
253 0 : void set_matrix_structure_is_const(bool b) {m_bMatrixStructureIsConst = b;}
254 :
255 : /**
256 : * whether matrix is to be modified by assembling
257 : *
258 : * @return true iff matrix is not to be modified
259 : */
260 0 : bool matrix_is_const() const {return m_bMatrixIsConst;}
261 :
262 : protected:
263 : /// default LocalToGlobalMapper
264 : LocalToGlobalMapper<TAlgebra> m_defaultMapper;
265 :
266 : /// LocalToGlobalMapper
267 : ILocalToGlobalMapper<TAlgebra>* m_pMapper;
268 :
269 : /// marker used to skip elements
270 : BoolMarker* m_pBoolMarker;
271 :
272 : /// selector used to set a list of elements for the assembling
273 : Selector* m_pSelector;
274 :
275 : /// object for DoFindex-wise assemble routine
276 : bool m_bSingleAssIndex;
277 : size_t m_SingleAssIndex;
278 :
279 : /// forces the assembling to regard the grid as regular
280 : bool m_bForceRegGrid;
281 :
282 : /// calls the 'modify_solution()' method of constraints;
283 : /// gives the modified solution to the assembling methods
284 : bool m_bModifySolutionImplemented;
285 :
286 : /// enables the constraints
287 : int m_ConstraintTypesEnabled;
288 :
289 : /// enables the constraints
290 : int m_ElemTypesEnabled;
291 :
292 : /// disables matrix assembling if set to false
293 : bool m_bMatrixIsConst;
294 :
295 : /// keeps matrix structure from last call if set to true
296 : bool m_bMatrixStructureIsConst;
297 :
298 : /// disables clearing of vector/matrix on resize
299 : bool m_bClearOnResize;
300 : };
301 :
302 : } // end namespace ug
303 :
304 : #include "ass_tuner_impl.h"
305 :
306 : #endif /* __H__UG__LIB_DISC__SPATIAL_DISC__ASS_TUNER__ */
|