ug4
cluster_element_stacks.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
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_cluster_element_stacks
34 #define __H__UG_cluster_element_stacks
35 
36 #include <stack>
37 #include "../partitioner.h"
40 
41 namespace ug{
42 
43 template <class elem_t, class vector_t>
45 {
46  public:
48  {
50  }
51 
53  const Attachment<vector_t>& aPos,
54  const vector_t& stackingDir) :
55  m_aPos(aPos),
56  m_stackingDir(stackingDir)
57  {}
58 
60  {}
61 
63  {
64  m_aPos = aPos;
65  }
66 
67  void set_stacking_direction(const vector_t& stackingDir)
68  {
69  m_stackingDir = stackingDir;
70  }
71 
73  {
74  m_mg = mg;
75  m_partitions = partitions;
76  mg->attach_to<elem_t>(m_aProcessed);
77  }
78 
79  void post_process(int partitionLvl)
80  {
81  using namespace std;
82 
84  "Please specify a valid stacking direction. "
85  "Current stacking direction has length 0.");
86  UG_COND_THROW(!m_mg, "no valid grid was specified during 'init_post_processing'");
87  UG_COND_THROW(!m_partitions, "no valid partition map was specified during "
88  "'init_post_processing'");
89  UG_COND_THROW(!m_mg->has_vertex_attachment(m_aPos), "Please specify "
90  "a valid position attachment (attached to the underlying grid) "
91  "before performing partitioning through "
92  "'ClusterElementStacks::set_position_attachment'.");
93 
94  MultiGrid& mg = *m_mg;
96 
99 
100  // set all processed flags to false in this level
101  SetAttachmentValues(aaProcessed, mg.begin<elem_t>(partitionLvl),
102  mg.end<elem_t>(partitionLvl), false);
103 
104  vector_t stackingDir;
105  VecNormalize(stackingDir, m_stackingDir);
106 
107  // iterate over all elements. If one was already processed, ignore it.
108  // For each unprocessed element: Init the current stack with this element.
109  // Then traverse all elements which are neighbored to a stack element
110  // and which are connected by a side which is not orthogonal to the
111  // stacking direction. Add those elements to the stack.
112 
113  stack<elem_t*> stack;
116 
117  lg_for_each_in_lvl_template(elem_t, rootElem, mg, partitionLvl){
118  if(aaProcessed[rootElem])
119  continue;
120 
121  int newSi = sh.get_subset_index(rootElem);
122 
123  stack.push(rootElem);
124  aaProcessed[rootElem] = true;
125 
126  while(!stack.empty()){
127  elem_t* elem = stack.top();
128  stack.pop();
129 
130  mg.associated_elements(sides, elem);
131  for_each_in_vec(side_t* s, sides){
132  vector_t n = CalculateNormal(s, aaPos);
133  if(fabs(VecDot(n, stackingDir)) > SMALL){
134  // cluster neighbors of that side
135  mg.associated_elements(elems, s);
136  for_each_in_vec(elem_t* nbr, elems){
137  if(!aaProcessed[nbr]){
138  stack.push(nbr);
139  aaProcessed[nbr] = true;
140  sh.assign_subset(nbr, newSi);
141  }
142  }end_for;
143  }
144  }end_for;
145  }
146  }lg_end_for;
147  }
148 
150  {
151  m_mg->detach_from<elem_t>(m_aProcessed);
152  }
153 
154  private:
155  typedef typename elem_t::side side_t;
157 
162  vector_t m_stackingDir;
163 };
164 
165 
166 }// end of namespace
167 
168 #endif //__H__UG_cluster_element_stacks
parameterString s
Definition: cluster_element_stacks.h:45
void partitioning_done()
Definition: cluster_element_stacks.h:149
SubsetHandler * m_partitions
Definition: cluster_element_stacks.h:159
void set_stacking_direction(const vector_t &stackingDir)
Definition: cluster_element_stacks.h:67
void post_process(int partitionLvl)
Definition: cluster_element_stacks.h:79
ClusterElementStacks()
Definition: cluster_element_stacks.h:47
void set_position_attachment(const Attachment< vector_t > &aPos)
Definition: cluster_element_stacks.h:62
Attachment< vector_t > a_position_t
Definition: cluster_element_stacks.h:156
virtual ~ClusterElementStacks()
Definition: cluster_element_stacks.h:59
vector_t m_stackingDir
Definition: cluster_element_stacks.h:162
elem_t::side side_t
Definition: cluster_element_stacks.h:155
void init_post_processing(MultiGrid *mg, SubsetHandler *partitions)
Definition: cluster_element_stacks.h:72
ABool m_aProcessed
Definition: cluster_element_stacks.h:160
ClusterElementStacks(const Attachment< vector_t > &aPos, const vector_t &stackingDir)
Definition: cluster_element_stacks.h:52
a_position_t m_aPos
Definition: cluster_element_stacks.h:161
MultiGrid * m_mg
Definition: cluster_element_stacks.h:158
the generic attachment-accessor for access to grids attachment pipes.
Definition: grid.h:182
void detach_from(IAttachment &attachment)
Definition: grid_impl.hpp:369
void attach_to(IAttachment &attachment, bool passOnValues)
attach with custom pass-on-behaviour and unspecified default value.
Definition: grid_impl.hpp:296
bool has_vertex_attachment(IAttachment &attachment)
Definition: grid.h:798
void associated_elements(traits< Vertex >::secure_container &elemsOut, TElem *e)
Puts all elements of type TAss which are contained in 'e' or which contain 'e' into elemsOut.
Definition: grid_impl.hpp:466
Partitions elements of a grid into several subsets.
Definition: subset_handler_grid.h:53
void assign_subset(Vertex *elem, int subsetIndex)
assigns a vertex to a subset.
Definition: subset_handler_grid.cpp:204
allows to post-process partitions
Definition: partitioner.h:146
int get_subset_index(GridObject *elem) const
Definition: subset_handler_interface.cpp:560
Definition: multi_grid.h:72
geometry_traits< TElem >::iterator end(int level)
Definition: multi_grid.h:168
geometry_traits< TElem >::iterator begin(int level)
Definition: multi_grid.h:158
Container which holds an array of pointers.
Definition: pointer_const_array.h:84
void SetAttachmentValues(TAttachmentAccessor &aaVal, TIter elemsBegin, TIter elemsEnd, const TVal &val)
sets attachment-values for elements between elemsBegin and elemsEnd.
Definition: attachment_util_impl.hpp:44
int CalculateNormal(vector3 &vNormOut, Grid &grid, Edge *e, Grid::AttachmentAccessor< Vertex, APosition > &aaPos, Grid::AttachmentAccessor< Face, ANormal > *paaNormFACE)
Calculates the normal of the given edge.
Definition: edge_util.cpp:314
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
void VecSet(vector_t &vInOut, typename vector_t::value_type s)
Set each vector component to scalar (componentwise)
Definition: math_vector_functions_common_impl.hpp:539
void VecNormalize(vector_t &vOut, const vector_t &v)
scales a vector_t to unit length
Definition: math_vector_functions_common_impl.hpp:501
vector_t::value_type VecLengthSq(const vector_t &v)
returns the squared length of v. Faster than VecLength.
Definition: math_vector_functions_common_impl.hpp:324
vector_t::value_type VecDot(const vector_t &v1, const vector_t &v2)
returns the dot-product of two vector_ts
Definition: math_vector_functions_common_impl.hpp:385
#define lg_for_each_in_lvl_template(_feType, _feVar, _feCon, _feLvl)
Definition: lg_for_each.h:59
#define lg_end_for
Definition: lg_for_each.h:88
Definition: smart_pointer.h:814
the ug namespace
const number SMALL
Definition: math_constants.h:41
#define for_each_in_vec(_vfeDecl, _vfeVec)
Allows iteration over all members of an std::vector compatible type.
Definition: vec_for_each.h:52
#define end_for
Allows iteration over all members of an std::vector compatible type.
Definition: vec_for_each.h:56