Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
directional_ordering.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022: G-CSC, Goethe University Frankfurt
3 * Author: Lukas Larisch
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 __UG__LIB_DISC__ORDERING_STRATEGIES_ALGORITHMS_DIRECTIONAL_ORDERING__
34#define __UG__LIB_DISC__ORDERING_STRATEGIES_ALGORITHMS_DIRECTIONAL_ORDERING__
35
36#include <boost/graph/adjacency_list.hpp>
37#include <boost/graph/graph_traits.hpp>
38#include <boost/graph/properties.hpp>
39
40#include <boost/graph/cuthill_mckee_ordering.hpp>
41
42#include <set>
43#include <algorithm> //reverse
44#include <utility> //pair
45
46#include "lib_disc/domain.h"
49
52
53#include <assert.h>
54#include "common/error.h"
55
56
57namespace ug{
58
59bool CompareScalar(const std::pair<number, size_t> &p1,
60 const std::pair<number, size_t> &p2)
61{
62 return p1.first<p2.first;
63}
64
65
66template <typename TAlgebra, typename TDomain, typename O_t>
67class DirectionalOrdering : public IOrderingAlgorithm<TAlgebra, O_t>
68{
69public:
70 typedef typename TAlgebra::matrix_type M_t;
71 typedef typename TAlgebra::vector_type V_t;
73
74 typedef typename std::pair<MathVector<TDomain::dim>, size_t> Position_t;
75 typedef typename std::pair<number, size_t> Scalar_t;
78
80
82
86
91
92 void compute(){
93 m_vScalars.resize(m_vPositions.size());
94 small_vec_t pos;
95 for(size_t i = 0; i < m_vPositions.size(); ++i){
96 pos = m_vPositions[i].first;
97 m_vScalars[i].first = pos*(*m_dir); //scalar product
98 m_vScalars[i].second = m_vPositions[i].second;
99 }
100
101 std::sort(m_vScalars.begin(), m_vScalars.end(), CompareScalar);
102
103 for(size_t i = 0; i < m_vScalars.size(); ++i){
104 o[m_vScalars[i].second] = i;
105 }
106
107 #ifdef UG_DEBUG
108 check();
109 #endif
110 }
111
112 void init(M_t* A, const V_t& V){
113 const GridFunc_t* pGridF;
114 unsigned n;
115
116 try{
117 if((pGridF = dynamic_cast<const GridFunc_t*>(&V)) == 0){
118 UG_THROW(name() << "::init: No DoFDistribution specified.");
119 }
120
121 SmartPtr<DoFDistribution> dd = ((GridFunc_t*) pGridF)->dof_distribution();
122
123 n = dd->num_indices();
124
125 if(n != A->num_rows ()){
126 UG_THROW(name() << "::init: #indices != #rows");
127 }
128
129 o.resize(n);
130 ExtractPositions(pGridF->domain(), dd, m_vPositions);
131 }
132 catch(...){
133 throw;
134 }
135
136 #ifdef UG_ENABLE_DEBUG_LOGS
137 UG_LOG("Using " << name() << " (direction " << *m_dir << ")\n");
138 #endif
139 }
140
141 void init(M_t* A){
142 UG_THROW(name() << "::init: Cannot initialize smoother without a geometry. Specify the 2nd argument for init!");
143 }
144
145 void init(M_t* A, const V_t&, const O_t& inv_map){
146 UG_THROW(name() << "::init: induced subgraph version not implemented yet!");
147 }
148
149 void init(M_t* A, const O_t& inv_map){
150 UG_THROW(name() << "::init: induced subgraph version not implemented yet!");
151 }
152
153 void check(){
154 if(!is_permutation(o)){
155 UG_THROW(name() << "::check: Not a permutation!");
156 }
157 }
158
159 O_t& ordering(){
160 return o;
161 }
162
164 m_dir = dir;
165 }
166
167 virtual const char* name() const {return "DirectionalOrdering";}
168
169private:
170 O_t o;
171
173
174 std::vector<Position_t> m_vPositions;
175 std::vector<Scalar_t> m_vScalars;
176};
177
178} //namespace
179
180
181#endif //guard
Definition smart_pointer.h:108
Definition directional_ordering.cpp:68
IOrderingAlgorithm< TAlgebra, O_t > baseclass
Definition directional_ordering.cpp:72
O_t & ordering()
Definition directional_ordering.cpp:159
void init(M_t *A, const V_t &V)
Definition directional_ordering.cpp:112
void init(M_t *A, const V_t &, const O_t &inv_map)
Definition directional_ordering.cpp:145
MathVector< TDomain::dim > small_vec_t
Definition directional_ordering.cpp:76
DirectionalOrdering(const DirectionalOrdering< TAlgebra, TDomain, O_t > &parent)
clone constructor
Definition directional_ordering.cpp:84
void init(M_t *A)
Definition directional_ordering.cpp:141
void set_direction(small_vec_t *dir)
Definition directional_ordering.cpp:163
std::vector< Scalar_t > m_vScalars
Definition directional_ordering.cpp:175
std::pair< number, size_t > Scalar_t
Definition directional_ordering.cpp:75
void compute()
Definition directional_ordering.cpp:92
DirectionalOrdering()
Definition directional_ordering.cpp:81
void check()
Definition directional_ordering.cpp:153
small_vec_t * m_dir
Definition directional_ordering.cpp:172
std::pair< MathVector< TDomain::dim >, size_t > Position_t
Definition directional_ordering.cpp:74
TAlgebra::matrix_type M_t
Definition directional_ordering.cpp:70
TAlgebra::vector_type V_t
Definition directional_ordering.cpp:71
O_t o
Definition directional_ordering.cpp:170
GridFunction< TDomain, TAlgebra > GridFunc_t
Definition directional_ordering.cpp:77
virtual const char * name() const
Definition directional_ordering.cpp:167
SmartPtr< IOrderingAlgorithm< TAlgebra, O_t > > clone()
Definition directional_ordering.cpp:87
std::vector< Position_t > m_vPositions
Definition directional_ordering.cpp:174
void init(M_t *A, const O_t &inv_map)
Definition directional_ordering.cpp:149
SmartPtr< UserData< MathVector< TDomain::dim >, TDomain::dim > > TSpUserData
Definition directional_ordering.cpp:79
represents numerical solutions on a grid using an algebraic vector
Definition grid_function.h:121
Definition IOrderingAlgorithm.h:52
a mathematical Vector with N entries.
Definition math_vector.h:97
#define UG_THROW(msg)
Definition error.h:57
#define UG_LOG(msg)
Definition log.h:367
the ug namespace
bool CompareScalar(const std::pair< number, size_t > &p1, const std::pair< number, size_t > &p2)
Definition directional_ordering.cpp:59
void ExtractPositions(ConstSmartPtr< TDomain > domain, ConstSmartPtr< DoFDistribution > dd, std::vector< MathVector< TDomain::dim > > &vPos)
Definition dof_position_util.cpp:424
bool is_permutation(O_t &o)
Definition permutation_util.h:135
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition smart_pointer.h:836