ug4
lexorder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: 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__ORDERING_STRATEGIES_ALGORITHMS__LEXORDER__
34 #define __H__UG__LIB_DISC__ORDERING_STRATEGIES_ALGORITHMS__LEXORDER__
35 
36 #include <vector>
37 #include <utility> // for pair
38 #include <cstring> //strlen
39 
40 #include "lib_disc/domain.h"
42 
46 
47 #include "common/error.h"
48 
49 namespace ug{
50 
51 template<int dim>
52 void ComputeLexicographicOrder(std::vector<size_t>& vNewIndex,
53  std::vector<std::pair<MathVector<dim>, size_t> >& vPos,
54  size_t orderDim = 0, bool increasing = true);
55 
57 template <typename TDomain>
59  size_t orderDim = 0, bool increasing = true);
60 
62 template <typename TDomain>
63 void OrderLex(ApproximationSpace<TDomain>& approxSpace, const char *order);
64 
65 
66 template <typename TAlgebra, typename TDomain, typename O_t>
67 class LexOrdering : public IOrderingAlgorithm<TAlgebra, O_t>
68 {
69 public:
70  typedef typename TAlgebra::matrix_type M_t;
71  typedef typename TAlgebra::vector_type V_t;
73 
76 
78  typedef typename std::pair<MathVector<TDomain::dim>, size_t> Position_t;
79 
81 
84  : baseclass(){}
85 
87  {
89  }
90 
91  void parse(){
92  if(sizeof(m_dir) == 0){
93  UG_THROW(name() << "::parse: empty string\n");
94  }
95 
96  std::cout << name() << "::parse()" << std::endl;
97  std::cout << "length: " << strlen(m_dir) << std::endl;
98  }
99 
100  void compute(){
101  size_t len = strlen(m_dir);
102 
103  if(len == 0){
104  UG_THROW(name() << "::compute: Empty direction!");
105  }
106 
107  size_t pos = 0;
108  bool increasing = true;
109  char sign;
110  while(pos < len){
111  if(increasing){
112  sign = '+';
113  }
114  else{
115  sign = '-';
116  }
117 
118  switch(m_dir[pos]){
119  case '+':
120  //UG_LOG(name() << "::compute: LexOrdering in + direction.\n")
121  ++pos;
122  increasing = true;
123  break;
124  case '-':
125  //UG_LOG(name() << "::compute: LexOrdering in - direction.\n")
126  ++pos;
127  increasing = false;
128  break;
129  case 'x':
130  UG_LOG(name() << "::compute: LexOrdering in " << sign << "x direction.\n")
131  ComputeLexicographicOrder<TDomain::dim>(o, m_vPositions, 0, increasing);
132  ++pos;
133  increasing = true;
134  break;
135  case 'y':
136  UG_LOG(name() << "::compute: LexOrdering in " << sign << "y direction.\n")
137  ComputeLexicographicOrder<TDomain::dim>(o, m_vPositions, 1, increasing);
138  ++pos;
139  increasing = true;
140  break;
141  case 'z':
142  UG_LOG(name() << "::compute: LexOrdering in " << sign << "z direction.\n")
143  ComputeLexicographicOrder<TDomain::dim>(o, m_vPositions, 2, increasing);
144  ++pos;
145  increasing = true;
146  break;
147  default:
148  UG_THROW(name() << "::compute: Invalid token in direction string, valid tokens: +, -, x, y, z");
149  }
150  }
151 
152  mat = NULL;
153  }
154 
155  void check(){
156  if(!is_permutation(o)){
157  UG_THROW(name() << "::check: Not a permutation!");
158  }
159  }
160 
161  O_t& ordering(){
162  return o;
163  }
164 
165  void init(M_t* A, const V_t& V){
166  if(strcmp(m_dir, "") == 0){
167  UG_THROW(name() << "::init: no direction chosen!");
168  }
169 
170  try{
171  const GridFunc_t* pGridF;
172  if((pGridF = dynamic_cast<const GridFunc_t*>(&V)) == 0){
173  UG_THROW(name() << "::init: No DoFDistribution specified.");
174  }
175 
176  SmartPtr<DoFDistribution> dd = ((GridFunc_t*) pGridF)->dof_distribution();
177 
178  size_t indN = dd->num_indices();
179 
180  if(indN != A->num_rows ()){
181  UG_THROW(name() << "::init: #indices != #rows");
182  }
183 
184  o.resize(indN);
185  ExtractPositions(pGridF->domain(), dd, m_vPositions);
186  }
187  catch(...){
188  throw;
189  }
190 
191  #ifdef UG_ENABLE_DEBUG_LOGS
192  UG_LOG("Using " << name() << " in " << m_dir << " direction\n");
193  #endif
194 
195  mat = A;
196  }
197 
198  void init(M_t*){
199  UG_THROW(name() << "::init: Cannot initialize smoother without a geometry. Specify the 2nd argument for init!");
200  }
201 
202  void init(M_t*, const V_t&, const O_t&){
203  UG_THROW(name() << "::init: induced subgraph version not implemented yet!");
204  }
205 
206  void init(M_t*, const O_t&){
207  UG_THROW(name() << "::init: induced subgraph version not implemented yet!");
208  }
209 
210  virtual const char* name() const {return "LexOrdering";}
211 
212  void set_direction(const char *dir){
213  m_dir = dir;
214  }
215 
216 private:
217  O_t o;
219 
220  const char *m_dir;
221  std::vector<Position_t> m_vPositions;
222 };
223 
224 } // end namespace ug
225 
226 #endif
Definition: smart_pointer.h:108
represents numerical solutions on a grid using an algebraic vector
Definition: grid_function.h:121
SmartPtr< TDomain > domain()
returns domain
Definition: grid_function.h:342
Definition: IOrderingAlgorithm.h:52
Definition: lexorder.h:68
TAlgebra::vector_type V_t
Definition: lexorder.h:71
const char * m_dir
Definition: lexorder.h:220
virtual const char * name() const
Definition: lexorder.h:210
void parse()
Definition: lexorder.h:91
GridFunction< TDomain, TAlgebra > GridFunc_t
Grid function type for the solution.
Definition: lexorder.h:75
void set_direction(const char *dir)
Definition: lexorder.h:212
IOrderingAlgorithm< TAlgebra, O_t > baseclass
Definition: lexorder.h:72
void compute()
Definition: lexorder.h:100
M_t * mat
Definition: lexorder.h:218
LexOrdering()
Definition: lexorder.h:80
std::pair< MathVector< TDomain::dim >, size_t > Position_t
Position attachment type.
Definition: lexorder.h:78
std::vector< Position_t > m_vPositions
Definition: lexorder.h:221
SmartPtr< IOrderingAlgorithm< TAlgebra, O_t > > clone()
Definition: lexorder.h:86
LexOrdering(const LexOrdering< TAlgebra, TDomain, O_t > &parent)
clone constructor
Definition: lexorder.h:83
void init(M_t *)
Definition: lexorder.h:198
O_t o
Definition: lexorder.h:217
TAlgebra::matrix_type M_t
Definition: lexorder.h:70
void check()
Definition: lexorder.h:155
O_t & ordering()
Definition: lexorder.h:161
void init(M_t *, const V_t &, const O_t &)
Definition: lexorder.h:202
void init(M_t *, const O_t &)
Definition: lexorder.h:206
void init(M_t *A, const V_t &V)
Definition: lexorder.h:165
#define UG_THROW(msg)
Definition: error.h:57
#define UG_LOG(msg)
Definition: log.h:367
the ug namespace
void ExtractPositions(ConstSmartPtr< TDomain > domain, ConstSmartPtr< DoFDistribution > dd, std::vector< MathVector< TDomain::dim > > &vPos)
Definition: dof_position_util.cpp:424
void OrderLexForDofDist(SmartPtr< DoFDistribution > dd, ConstSmartPtr< TDomain > domain, size_t orderDim, bool increasing)
orders the dof distribution using Cuthill-McKee
Definition: lexorder.cpp:117
bool is_permutation(O_t &o)
Definition: permutation_util.h:135
void ComputeLexicographicOrder(std::vector< size_t > &vNewIndex, std::vector< std::pair< MathVector< dim >, size_t > > &vPos, size_t orderDim, bool increasing)
Definition: lexorder.cpp:50
void OrderLex(ApproximationSpace< TDomain > &approxSpace, const char *order)
orders the all DofDistributions of the ApproximationSpace using lexicographic order
Definition: lexorder.cpp:236
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836