Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_ElementPermutation.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_CodeTypes.h" // for IntVector
8 #include "Ioss_ElementPermutation.h"
9 #include "Ioss_Utils.h"
10 #include <assert.h>
11 #include <fmt/ostream.h>
12 #include <ostream> // for basic_ostream, etc
13 #include <string> // for string, char_traits, etc
14 #include <vector> // for vector
15
16 namespace Ioss {
17 void EPRegistry::insert(const Ioss::EPM_VP &value, bool delete_me)
18 {
19 m_registry.insert(value);
20 if (delete_me) {
21 m_deleteThese.push_back(value.second);
22 }
23 }
24
25 EPRegistry::~EPRegistry()
26 {
27 for (const auto &entry : m_deleteThese) {
28 delete entry;
29 }
30 }
31
32 //=========================================================================================
33 ElementPermutation::ElementPermutation(std::string type, bool delete_me) : m_type(std::move(type))
34 {
35 registry().insert(EPM_VP(Ioss::Utils::lowercase(m_type), this), delete_me);
36 }
37
38 EPRegistry &ElementPermutation::registry()
39 {
40 static EPRegistry registry_;
41 return registry_;
42 }
43
44 ElementPermutation *Ioss::ElementPermutation::factory(const std::string &type)
45 {
46 std::string ltype = Ioss::Utils::lowercase(type);
47
48 Ioss::ElementPermutation *inst = nullptr;
49 auto iter = registry().find(ltype);
50
51 if (iter == registry().end()) {
52 std::string base1{"super"};
53 if (ltype.compare(0, base1.length(), base1) == 0) {
54 // A ring permutation can have a varying number of nodes. Create
55 // a permutation type for this ring permutation. The node count
56 // should be encoded in the 'type' as '[base1]42' for a 42-node
57 // ring permutation.
58
59 Ioss::SuperPermutation::make_super(ltype);
60 iter = registry().find(ltype);
61 }
62 }
63
64 if (iter == registry().end()) {
65 std::ostringstream errmsg;
66 fmt::print(errmsg, "ERROR: The permutation type '{}' is not supported.", type);
67 IOSS_ERROR(errmsg);
68 }
69 else {
70 inst = (*iter).second;
71 }
72 return inst;
73 }
74
75 NameList ElementPermutation::describe()
76 {
77 Ioss::NameList names;
78 describe(&names);
79 return names;
80 }
81
82 int ElementPermutation::describe(NameList *names)
83 {
84 int count = 0;
85 for (auto &entry : registry()) {
86 names->push_back(entry.first);
87 count++;
88 }
89 return count;
90 }
91
92 const std::string &ElementPermutation::type() const { return m_type; }
93
94 unsigned ElementPermutation::num_permutations() const { return m_numPermutations; }
95
96 unsigned ElementPermutation::num_positive_permutations() const
97 {
98 return m_numPositivePermutations;
99 }
100
101 bool ElementPermutation::is_positive_polarity(Permutation permutation) const
102 {
103 return permutation < m_numPositivePermutations;
104 }
105
106 bool ElementPermutation::valid_permutation(Permutation permutation) const
107 {
108 return permutation < m_numPermutations;
109 }
110
111 bool ElementPermutation::fill_permutation_indices(Permutation permutation,
112 std::vector<Ordinal> &nodeOrdinalVector) const
113 {
114 if (!valid_permutation(permutation)) {
115 return false;
116 }
117
118 nodeOrdinalVector.resize(num_permutation_nodes());
119 const auto &ordinals = m_permutationNodeOrdinals[permutation];
120 for (unsigned i = 0; i < num_permutation_nodes(); i++) {
121 nodeOrdinalVector[i] = ordinals[i];
122 }
123
124 return true;
125 }
126
127 std::vector<Ordinal> ElementPermutation::permutation_indices(Permutation permutation) const
128 {
129 std::vector<Ordinal> nodeOrdinalVector;
130 fill_permutation_indices(permutation, nodeOrdinalVector);
131 return nodeOrdinalVector;
132 }
133
134 Permutation ElementPermutation::num_permutation_nodes() const { return m_numPermutationNodes; }
135
136 void ElementPermutation::set_permutation(
137 Permutation numPermutationNodes_, Permutation numPermutations_,
138 Permutation numPositivePermutations_,
139 const std::vector<std::vector<Permutation>> &permutationNodeOrdinals_)
140 {
141 assert(permutationNodeOrdinals_.size() == numPermutations_);
142 assert(numPositivePermutations_ <= numPermutations_);
143
144 m_numPermutations = numPermutations_;
145 m_numPositivePermutations = numPositivePermutations_;
146 m_numPermutationNodes = numPermutationNodes_;
147
148 for (const auto &ordinals : permutationNodeOrdinals_) {
149 if (ordinals.size() != numPermutationNodes_) {
150 std::ostringstream errmsg;
151 fmt::print(errmsg,
152 "ERROR: Number of low order permutation ordinals: {} for permutation: {} does "
153 "not match permutation value: {}",
154 ordinals.size(), type(), numPermutationNodes_);
155 IOSS_ERROR(errmsg);
156 }
157
158 for (const auto ordinal : ordinals) {
159 if (ordinal >= numPermutationNodes_) {
160 std::ostringstream errmsg;
161 fmt::print(errmsg, "ERROR: Invalid value of ordinal: {} for permutation: {}", ordinal,
162 numPermutationNodes_);
163 IOSS_ERROR(errmsg);
164 }
165 }
166 }
167
168 m_permutationNodeOrdinals = permutationNodeOrdinals_;
169 }
170
171 bool ElementPermutation::equal_(const ElementPermutation &rhs, bool quiet) const
172 {
173 if (this->m_type != rhs.m_type) {
174 if (!quiet) {
175 fmt::print(Ioss::OUTPUT(), "Element Permutation: NAME mismatch ({} vs. {})\n",
176 this->m_type.c_str(), rhs.m_type.c_str());
177 }
178 return false;
179 }
180
181 if (this->m_numPermutations != rhs.m_numPermutations) {
182 if (!quiet) {
183 fmt::print(Ioss::OUTPUT(), "Element Permutation: NUM PERMUTATION mismatch ({} vs. {})\n",
184 this->m_numPermutations, rhs.m_numPermutations);
185 }
186 return false;
187 }
188
189 if (this->m_numPositivePermutations != rhs.m_numPositivePermutations) {
190 if (!quiet) {
191 fmt::print(Ioss::OUTPUT(),
192 "Element Permutation: NUM POSITIVE PERMUTATION mismatch ({} vs. {})\n",
193 this->m_numPositivePermutations, rhs.m_numPositivePermutations);
194 }
195 return false;
196 }
197
198 if (this->m_numPermutationNodes != rhs.m_numPermutationNodes) {
199 if (!quiet) {
200 fmt::print(Ioss::OUTPUT(),
201 "Element Permutation: NUM PERMUTATION NODES mismatch ({} vs. {})\n",
202 this->m_numPermutationNodes, rhs.m_numPermutationNodes);
203 }
204 return false;
205 }
206
207 if (this->m_permutationNodeOrdinals != rhs.m_permutationNodeOrdinals) {
208 if (!quiet) {
209 fmt::print(Ioss::OUTPUT(), "Element Permutation: PERMUTATION NODE ORDINALS mismatch\n");
210 }
211 return false;
212 }
213
214 return true;
215 }
216
217 bool ElementPermutation::operator==(const ElementPermutation &rhs) const
218 {
219 return equal_(rhs, true);
220 }
221
222 bool ElementPermutation::operator!=(const ElementPermutation &rhs) const
223 {
224 return !(*this == rhs);
225 }
226
227 bool ElementPermutation::equal(const ElementPermutation &rhs) const { return equal_(rhs, false); }
228
229 //====================================================================================================
230 void NullPermutation::factory() { static NullPermutation registerThis; }
231
232 NullPermutation::NullPermutation() : ElementPermutation("none") { set_permutation(0, 0, 0, {}); }
233
234 //====================================================================================================
235 void SpherePermutation::factory() { static SpherePermutation registerThis; }
236
237 SpherePermutation::SpherePermutation() : ElementPermutation("sphere")
238 {
239 set_permutation(1, 1, 1, {{0}});
240 }
241
242 //====================================================================================================
243 void LinePermutation::factory() { static LinePermutation registerThis; }
244
245 LinePermutation::LinePermutation() : ElementPermutation("line")
246 {
247 set_permutation(2, 2, 1, {{0, 1}, {1, 0}});
248 }
249
250 //====================================================================================================
251 void SpringPermutation::factory() { static SpringPermutation registerThis; }
252
253 SpringPermutation::SpringPermutation() : ElementPermutation("spring")
254 {
255 set_permutation(2, 2, 2, {{0, 1}, {1, 0}});
256 }
257
258 //====================================================================================================
259 void TriPermutation::factory() { static TriPermutation registerThis; }
260
261 TriPermutation::TriPermutation() : ElementPermutation("tri")
262 {
263 set_permutation(3, 6, 3, {{0, 1, 2}, {2, 0, 1}, {1, 2, 0}, {0, 2, 1}, {2, 1, 0}, {1, 0, 2}});
264 }
265
266 //====================================================================================================
267 void QuadPermutation::factory() { static QuadPermutation registerThis; }
268
269 QuadPermutation::QuadPermutation() : ElementPermutation("quad")
270 {
271 set_permutation(4, 8, 4,
272 {{0, 1, 2, 3},
273 {3, 0, 1, 2},
274 {2, 3, 0, 1},
275 {1, 2, 3, 0},
276 {0, 3, 2, 1},
277 {3, 2, 1, 0},
278 {2, 1, 0, 3},
279 {1, 0, 3, 2}});
280 }
281
282 //====================================================================================================
283 void TetPermutation::factory() { static TetPermutation registerThis; }
284
285 TetPermutation::TetPermutation() : ElementPermutation("tet")
286 {
287 set_permutation(4, 12, 12,
288 {{0, 1, 2, 3},
289 {1, 2, 0, 3},
290 {2, 0, 1, 3},
291 {0, 3, 1, 2},
292 {3, 1, 0, 2},
293 {1, 0, 3, 2},
294 {0, 2, 3, 1},
295 {2, 3, 0, 1},
296 {3, 0, 2, 1},
297 {1, 3, 2, 0},
298 {3, 2, 1, 0},
299 {2, 1, 3, 0}});
300 }
301
302 //====================================================================================================
303 void PyramidPermutation::factory() { static PyramidPermutation registerThis; }
304
305 PyramidPermutation::PyramidPermutation() : ElementPermutation("pyramid")
306 {
307 set_permutation(5, 4, 4, {{0, 1, 2, 3, 4}, {1, 2, 3, 0, 4}, {2, 3, 0, 1, 4}, {3, 0, 1, 2, 4}});
308 }
309
310 //====================================================================================================
311 void WedgePermutation::factory() { static WedgePermutation registerThis; }
312
313 WedgePermutation::WedgePermutation() : ElementPermutation("wedge")
314 {
315 set_permutation(6, 6, 6,
316 {{0, 1, 2, 3, 4, 5},
317 {1, 2, 0, 4, 5, 3},
318 {2, 0, 1, 5, 3, 4},
319 {3, 5, 4, 0, 2, 1},
320 {5, 4, 3, 2, 1, 0},
321 {4, 3, 5, 1, 0, 2}});
322 }
323
324 //====================================================================================================
325 void HexPermutation::factory() { static HexPermutation registerThis; }
326
327 HexPermutation::HexPermutation() : ElementPermutation("hex")
328 {
329 set_permutation(8, 24, 24,
330 {{0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 5, 4, 3, 2, 6, 7}, {0, 4, 7, 3, 1, 5, 6, 2},
331 {1, 2, 3, 0, 5, 6, 7, 4}, {1, 2, 6, 5, 0, 3, 7, 4}, {1, 5, 4, 0, 2, 6, 7, 3},
332 {2, 3, 0, 1, 6, 7, 4, 5}, {2, 3, 7, 6, 1, 0, 4, 5}, {2, 6, 5, 1, 3, 7, 4, 0},
333 {3, 0, 1, 2, 7, 4, 5, 6}, {3, 0, 4, 7, 2, 1, 5, 6}, {3, 7, 6, 2, 0, 4, 5, 1},
334 {4, 0, 1, 5, 7, 3, 2, 6}, {4, 7, 3, 0, 5, 6, 2, 1}, {4, 7, 6, 5, 0, 3, 2, 1},
335 {5, 1, 2, 6, 4, 0, 3, 7}, {5, 4, 0, 1, 6, 7, 3, 2}, {5, 4, 7, 6, 1, 0, 3, 2},
336 {6, 2, 3, 7, 5, 1, 0, 4}, {6, 5, 1, 2, 7, 4, 0, 3}, {6, 5, 4, 7, 2, 1, 0, 3},
337 {7, 3, 0, 4, 6, 2, 1, 5}, {7, 6, 2, 3, 4, 5, 1, 0}, {7, 6, 5, 4, 3, 2, 1, 0}});
338 }
339
340 //====================================================================================================
341 // Permutation based on round-robin labeling i,e "ring" permutation
342 // Super permutation with 4 nodes will have the following positive permutations
343 // {0, 1, 2, 3}, {1, 2, 3, 0}, {2, 3, 0, 1}, {3, 0, 1, 2}
344 // and the following negative permutations
345 // {0, 3, 2, 1}, {3, 2, 1, 0}, {2, 1, 0, 3}, {1, 0, 3, 2}
346 std::string SuperPermutation::get_name(unsigned n) { return "super" + std::to_string(n); }
347
348 void SuperPermutation::make_super(const std::string &type)
349 {
350 // Decode name to determine number of nodes...
351 // Assume that digits at end specify number of nodes.
352 std::string node_count_str = Ioss::Utils::get_trailing_digits(type);
353 if (!node_count_str.empty()) {
354 int node_count = std::stoi(node_count_str);
355 SuperPermutation::factory(node_count);
356 }
357 }
358
359 void SuperPermutation::factory() {}
360
361 void SuperPermutation::factory(unsigned n)
362 {
363 auto iter = registry().find(get_name(n));
364 if (iter == registry().end()) {
365 new SuperPermutation(n);
366 }
367 }
368
369 // Make sure the permutation is deleted ... boolean arg is true
370 SuperPermutation::SuperPermutation() : ElementPermutation(get_name(0), true)
371 {
372 set_permutation(0, 0, 0, {});
373 }
374
375 // Make sure the permutation is deleted... boolean arg is true
376 SuperPermutation::SuperPermutation(unsigned n) : ElementPermutation(get_name(n), true)
377 {
378 set_permutation(n, 2 * n, n, get_super_permutations(n));
379 }
380
381 std::vector<std::vector<Permutation>> SuperPermutation::get_super_permutations(unsigned n)
382 {
383 std::vector<std::vector<Permutation>> superPerms;
384
385 // Positive permutations
386 for (unsigned i = 0; i < n; i++) {
387 std::vector<Permutation> perm;
388
389 for (unsigned j = 0; j < n; j++) {
390 perm.push_back((i + j) % n);
391 }
392
393 superPerms.push_back(perm);
394 }
395
396 // Negative permutations
397 for (unsigned i = 0; i < n; i++) {
398 std::vector<Permutation> perm;
399
400 for (unsigned j = 0; j < n; j++) {
401 perm.push_back(((i + n) - j) % n);
402 }
403
404 superPerms.push_back(perm);
405 }
406
407 return superPerms;
408 }
409
410 } // namespace Ioss