OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
Adjacency Class Reference

Public Member Functions

void setNbVertices (const size_t &s)
void reserve (const size_t &s)
void addEdge (const int &a, const int &b)
Vint CuthillMckee () const
void printStats (const Vint &perm) const
void printPermutation (const vector< int > &perm) const

Private Member Functions

size_t degree (const size_t &i) const
size_t pickRoot (const Vint &mask, size_t &lower_bound) const

Private Attributes

vector< Vint_Adj
size_t _N

Detailed Description

Definition at line 84 of file cpp_reorder_elements.cpp.

Member Function Documentation

◆ addEdge()

void Adjacency::addEdge ( const int & a,
const int & b )
inline

Definition at line 181 of file cpp_reorder_elements.cpp.

182 {
183 _Adj[a].push_back(b);
184 }
vector< Vint > _Adj

◆ CuthillMckee()

Vint Adjacency::CuthillMckee ( ) const
inline

Definition at line 188 of file cpp_reorder_elements.cpp.

189 {
190 Vint dist(_N,-1);
191 Vint order(_N,-1);
192 size_t lowerBound = 0;
193 size_t nextIndex = 0;
194
195 while (nextIndex < _N)
196 {
197 const size_t root = pickRoot(order, lowerBound);
198 //assert(dist[root] == -1);
199 //assert(order[root] == -1);
200 dist[root] = 0;
201 order[root] = nextIndex++;
202 size_t currentDist = 0;
203 deque<int> currentVertices(1, root);
204 while(currentVertices.size() > 0)
205 {
206 const int current = currentVertices.front();
207 currentVertices.pop_front();
208 currentDist = dist[current] + 1;
209 multiset<pair<int, int>> nextVertices;
210 for (const auto &next : _Adj[current])
211 {
212 if (dist[next] == -1)
213 {
214 dist[next] = currentDist;
215 nextVertices.insert(make_pair(degree(next), next));
216 }
217 }
218 for(const auto &next : nextVertices)
219 {
220 order[next.second] = nextIndex++;
221 currentVertices.push_back(next.second);
222 }
223 }
224 }
225 // order["old"] = "new"
226 return order;
227 }
size_t pickRoot(const Vint &mask, size_t &lower_bound) const
size_t degree(const size_t &i) const
vector< int > Vint
char root[ROOTLEN]
Definition rad2rad_c.c:124

◆ degree()

size_t Adjacency::degree ( const size_t & i) const
inlineprivate

Definition at line 91 of file cpp_reorder_elements.cpp.

92 {
93 return _Adj[i].size();
94 }

◆ pickRoot()

size_t Adjacency::pickRoot ( const Vint & mask,
size_t & lower_bound ) const
inlineprivate

Definition at line 101 of file cpp_reorder_elements.cpp.

102 {
103 size_t root = 0;
104
105 // pick next available root
106 for (root = lower_bound; root < _N; root++)
107 {
108 if (mask[root] == -1)
109 {
110 break;
111 }
112 }
113
114 int maxLevel = -1;
115 bool continueLoop = true;
116 size_t iter = 0;
117
118 while(continueLoop && iter < _N)
119 {
120 iter++;
121 size_t currentDist = 0;
122 Vint level(_N, -1); // level[i] = distance to root or -1 if not visited yet
123 deque<int> currentVertices(1, root);
124 level[root] = 0;
125 while(currentVertices.size() > 0)
126 {
127
128 const int current = currentVertices.front();
129 if (level[current] > level[root] || (level[current] == level[root] && degree(current) > degree(root)))
130 {
131 //the new root is the vertex with the highest level
132 // with level > -1 (i.e. reachable from the current root)
133 root = current;
134 } else if(level[current] == level[root] && degree(current) == degree(root))
135 {
136 if(current < root)
137 {
138 root = current;
139 }
140 }
141
142 currentVertices.pop_front();
143 currentDist = level[current] + 1;
144 for(const auto &next : _Adj[current])
145 {
146 if (level[next] == -1)
147 {
148 level[next] = currentDist;
149 currentVertices.push_back(next);
150 }
151 }
152 }
153
154 if(maxLevel < level[root])
155 { // continue if we have found a new root
156 maxLevel = level[root];
157 }else
158 { // stop if we have not found a new root
159 continueLoop = false;
160 }
161 }
162
163 //assert(mask[root] == -1);
164 return root;
165 }

◆ printPermutation()

void Adjacency::printPermutation ( const vector< int > & perm) const
inline

Definition at line 258 of file cpp_reorder_elements.cpp.

259 {
260 ofstream outfile;
261 outfile.open("spy_matrices.m");
262 outfile << "clear all; \n";
263 outfile << " A = sparse(" << _N << "," << _N << "); \n";
264 outfile << " P = zeros(" << _N << ",1); \n";
265 for (size_t i = 0; i < _N; ++i)
266 {
267 outfile << "P(" << perm[i] + 1 << ")=" << i + 1 << "; \n";
268 }
269 for (size_t i = 0; i < _N; ++i)
270 {
271 for (const auto &j : _Adj[i])
272 {
273 outfile << "A(" << i + 1 << "," << j + 1 << ") = 1; \n";
274 }
275 }
276 outfile.close();
277 }
FILE * outfile[100]

◆ printStats()

void Adjacency::printStats ( const Vint & perm) const
inline

Definition at line 229 of file cpp_reorder_elements.cpp.

230 {
231 int newMaxBandwidth = 0;
232 long long int newSumBandwidth = 0;
233 int oldMaxBandwidth = 0;
234 long long int oldSumBandwidth = 0;
235 Vint iperm(perm.size());
236 for (size_t i = 0; i < perm.size(); ++i)
237 {
238 iperm[perm[i]] = i;
239 }
240 for (size_t i = 0; i < _N; ++i)
241 {
242 for (const auto &j : _Adj[i])
243 {
244 const int newBandwidth = abs(perm[i] - perm[j]);
245 newMaxBandwidth = max(newMaxBandwidth, newBandwidth);
246 newSumBandwidth += newBandwidth;
247 const int oldBandwidth = abs(((int)i) - j);
248 oldMaxBandwidth = max(oldMaxBandwidth, oldBandwidth);
249 oldSumBandwidth += oldBandwidth;
250 }
251 }
252 if (_N > 1)
253 {
254 cout << "n:" << _N << " Old/New Max: " << ((double)oldMaxBandwidth)<<"/"<<((double)newMaxBandwidth);
255 cout << " Sum: " << ((double)oldSumBandwidth)<<"/"<<((double)newSumBandwidth) << endl;
256 }
257 }
#define max(a, b)
Definition macros.h:21

◆ reserve()

void Adjacency::reserve ( const size_t & s)
inline

Definition at line 173 of file cpp_reorder_elements.cpp.

174 {
175 for (auto &a : _Adj)
176 {
177 a.reserve(s);
178 }
179 }

◆ setNbVertices()

void Adjacency::setNbVertices ( const size_t & s)
inline

Definition at line 168 of file cpp_reorder_elements.cpp.

169 {
170 _N = s;
171 _Adj.resize(_N);
172 }

Field Documentation

◆ _Adj

vector<Vint> Adjacency::_Adj
private

Definition at line 87 of file cpp_reorder_elements.cpp.

◆ _N

size_t Adjacency::_N
private

Definition at line 88 of file cpp_reorder_elements.cpp.


The documentation for this class was generated from the following file: