Debian Codegraph @

packages/seacas/applications/zellij/Cell.C

raw
1 // Copyright(C) 2021, 2022, 2023, 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 <numeric>
8
9 #include "Cell.h"
10
11 #include <Ioss_NodeBlock.h>
12 #include <Ioss_SmartAssert.h>
13 #include <algorithm>
14 #include <fmt/format.h>
15 #include <fmt/ranges.h>
16 #include <string>
17
18 //! \file
19 extern unsigned int debug_level;
20
21 template <> struct fmt::formatter<Loc> : formatter<std::string>
22 {
23 // parse is inherited from formatter<std::string>.
24 template <typename FormatContext> auto format(Loc l, FormatContext &ctx) const
25 {
26 std::string name = "unknown";
27 switch (l) {
28 case Loc::C: name = "Center"; break;
29 case Loc::BL: name = "Bottom Left"; break;
30 case Loc::B: name = "Bottom"; break;
31 case Loc::BR: name = "Bottom Right"; break;
32 case Loc::L: name = "Left"; break;
33 case Loc::R: name = "Right"; break;
34 case Loc::TL: name = "Top Left"; break;
35 case Loc::T: name = "Top"; break;
36 case Loc::TR: name = "Top Right"; break;
37 }
38 return formatter<std::string>::format(name, ctx);
39 }
40 };
41
42 namespace {
43 // Iterate over the interior nodes on the specified face. Skips
44 // the corner nodes on the I-J intersections. Processes I-K and
45 // J-K corners.
46 template <typename INT>
47 void process_face_nodes(const std::vector<INT> &node_map, std::vector<INT> &nodes,
48 std::vector<INT> &procs, const std::vector<int64_t> &face_nodes,
49 size_t KK, int rank)
50 {
51 for (size_t i = KK; i < face_nodes.size() - KK; i++) {
52 auto idx = face_nodes[i];
53 nodes.push_back(node_map[idx + 1]);
54 procs.push_back(rank);
55 }
56 }
57
58 // Iterate over the specified corner nodes.
59 template <typename INT>
60 void process_corner_nodes(const std::vector<INT> &node_map, std::vector<INT> &nodes,
61 std::vector<INT> &procs, const std::vector<int64_t> &face_nodes,
62 size_t KK, int rank, Loc location)
63 {
64 if (location == Loc::BL || location == Loc::TL) {
65 for (size_t i = 0; i < KK; i++) {
66 auto idx = face_nodes[i];
67 nodes.push_back(node_map[idx + 1]);
68 procs.push_back(rank);
69 }
70 }
71 else {
72 for (size_t i = face_nodes.size() - KK; i < face_nodes.size(); i++) {
73 auto idx = face_nodes[i];
74 nodes.push_back(node_map[idx + 1]);
75 procs.push_back(rank);
76 }
77 }
78 }
79
80 // Return a vector (possibly empty) of the ranks of the cells that surround this
81 // cell. Includes the rank that this cell is on (cell_ranks[0]).
82 std::vector<int> get_shared_ranks(const std::array<int, 9> &cell_ranks)
83 {
84 std::vector<int> ranks(9);
85 std::copy(cell_ranks.begin(), cell_ranks.end(), ranks.begin());
86
87 // Set all `-1` (non-neighbor) values to match center rank...
88 for (auto &r : ranks) {
89 if (r == -1) {
90 r = cell_ranks[(int)Loc::C];
91 }
92 }
93 Ioss::Utils::uniquify(ranks);
94 return ranks;
95 }
96 } // namespace
97
98 void Cell::initialize(size_t i, size_t j, std::shared_ptr<UnitCell> unit_cell)
99 {
100 m_i = i;
101 m_j = j;
102 m_unitCell = unit_cell;
103
104 // These are not necessarily the correct ranks, but at this point can determine
105 // Whether this cell is surrounded by other cells, or is on the boundary.
106 set_rank(Loc::C, 0);
107 set_rank(Loc::L, m_i > 0 ? 0 : -1);
108 set_rank(Loc::B, m_j > 0 ? 0 : -1);
109 set_rank(Loc::BL, (m_i > 0 && m_j > 0) ? 0 : -1);
110 }
111
112 std::pair<double, double> Cell::get_coordinate_range(enum Axis axis) const
113 {
114 if (axis == Axis::X) {
115 return m_unitCell->minmax_x;
116 }
117 if (axis == Axis::Y) {
118 return m_unitCell->minmax_y;
119 }
120 return std::make_pair(0.0, 0.0);
121 }
122
123 // Number of nodes that will be added to global node count when this cell is added to
124 // grid -- accounts for coincident nodes if cell has neighbor(s)
125 size_t Cell::added_node_count(enum Mode mode, bool equivalence_nodes) const
126 {
127 // If no neighbors (to -I, -J), then all nodes would be added...
128 auto count = m_unitCell->m_region->get_property("node_count").get_int();
129
130 if (equivalence_nodes) {
131 if (mode == Mode::GLOBAL) {
132 if (has_neighbor_i()) {
133 count -= (m_unitCell->cell_JJ * m_unitCell->cell_KK);
134 }
135
136 if (has_neighbor_j()) {
137 count -= (m_unitCell->cell_II * m_unitCell->cell_KK);
138 }
139
140 if (has_neighbor_i() && has_neighbor_j()) {
141 count += m_unitCell->cell_KK;
142 }
143 }
144 else if (mode == Mode::PROCESSOR) {
145 if (has_neighbor_i() && !processor_boundary(Loc::L)) {
146 count -= (m_unitCell->cell_JJ * m_unitCell->cell_KK);
147 }
148
149 if (has_neighbor_j() && !processor_boundary(Loc::B)) {
150 count -= (m_unitCell->cell_II * m_unitCell->cell_KK);
151 }
152
153 if (has_neighbor_i() && has_neighbor_j() && !processor_boundary(Loc::L) &&
154 !processor_boundary(Loc::B)) {
155 count += m_unitCell->cell_KK;
156 }
157
158 // Now the "corner case" ;-) If there is a processor boundary below, but the cell to the BL is
159 // on the same rank as this cell, then we have already counted the IJ-line nodes, so need to
160 // subtract that count...
161 if (processor_boundary(Loc::B) && processor_boundary(Loc::L) &&
162 rank(Loc::BL) == rank(Loc::C)) {
163 count -= m_unitCell->cell_KK;
164 }
165
166 // Now the other "corner case"
167 if (processor_boundary(Loc::B) && rank(Loc::BR) == rank(Loc::C)) {
168 count -= m_unitCell->cell_KK;
169 }
170 }
171 }
172 return count;
173 }
174
175 std::array<int, 9> Cell::categorize_processor_boundary_nodes(int the_rank) const
176 {
177 // Create a "unit cell" to categorize processor boundary nodes...
178 std::array<int, 9> bnd_nodes{0};
179
180 // Bottom...
181 if (rank(Loc::B) == the_rank) {
182 bnd_nodes[(int)Loc::B] = 1;
183 if ((rank(Loc::BL) != rank(Loc::C)) && (rank(Loc::L) != rank(Loc::C))) {
184 bnd_nodes[(int)Loc::BL] = 1;
185 }
186 if (rank(Loc::BR) != rank(Loc::C)) {
187 bnd_nodes[(int)Loc::BR] = 1;
188 }
189 }
190
191 // Left
192 if (rank(Loc::L) == the_rank) {
193 bnd_nodes[(int)Loc::L] = 1;
194 bnd_nodes[(int)Loc::TL] = 1;
195 if ((rank(Loc::BL) != rank(Loc::C)) && (rank(Loc::B) != rank(Loc::C))) {
196 bnd_nodes[(int)Loc::BL] = 1;
197 }
198 }
199
200 // Top
201 if (rank(Loc::T) == the_rank) {
202 bnd_nodes[(int)Loc::T] = 1;
203 bnd_nodes[(int)Loc::TR] = 1;
204 if (rank(Loc::L) != rank(Loc::C)) {
205 bnd_nodes[(int)Loc::TL] = 1;
206 }
207 }
208
209 // Right
210 if (rank(Loc::R) == the_rank) {
211 bnd_nodes[(int)Loc::R] = 1;
212 bnd_nodes[(int)Loc::TR] = 1;
213 if ((rank(Loc::BR) != rank(Loc::C)) && (rank(Loc::B) != rank(Loc::C))) {
214 bnd_nodes[(int)Loc::BR] = 1;
215 }
216 }
217
218 // Bottom Left
219 if (rank(Loc::BL) == the_rank) {
220 // If left and bottom *don't* match the_rank, then need to add this node
221 if ((rank(Loc::L) != the_rank) && (rank(Loc::B) != the_rank) &&
222 (rank(Loc::L) != rank(Loc::C)) && (rank(Loc::B) != rank(Loc::C))) {
223 bnd_nodes[(int)Loc::BL] = 1;
224 }
225 }
226
227 // Bottom Right
228 if (rank(Loc::BR) == the_rank) {
229 // If bottom *doesn't* match the_rank, then need to add this node
230 if ((rank(Loc::B) != the_rank) && (rank(Loc::B) != rank(Loc::C))) {
231 bnd_nodes[(int)Loc::BR] = 1;
232 }
233 }
234
235 // Top Left
236 if (rank(Loc::TL) == the_rank) {
237 // If left *doesn't* match the_rank, then need to add this node
238 if ((rank(Loc::L) != the_rank) && (rank(Loc::L) != rank(Loc::C))) {
239 bnd_nodes[(int)Loc::TL] = 1;
240 }
241 }
242
243 // Top Right
244 if (rank(Loc::TR) == the_rank) {
245 bnd_nodes[(int)Loc::TR] = 1;
246 }
247
248 return bnd_nodes;
249 }
250
251 size_t Cell::processor_boundary_node_count() const
252 {
253 // Get list of ranks that this cell shares nodes with...
254 auto ranks = get_shared_ranks(m_ranks);
255 if (ranks.size() == 1) {
256 // `ranks` contains center node, so if size == 1, does not touch
257 // any other processor.
258 return 0;
259 }
260
261 // Iterate `ranks` and for each rank, "color" the `bnd_nodes` that that rank touches...
262 // Skip center.
263 size_t b_count = 0;
264 for (auto the_rank : ranks) {
265 if (the_rank == rank(Loc::C)) {
266 continue;
267 }
268
269 // a "unit cell" categorizing processor boundary nodes...
270 // Size is 9. Value is '1' if nodes at this location are shared with rank `rank`
271 auto bnd_nodes = categorize_processor_boundary_nodes(the_rank);
272
273 // Now count how many nodes we have added...
274 // Edges (B, T, L, R) without corners
275 b_count += (m_unitCell->cell_II - 2) * (bnd_nodes[(int)Loc::B] + bnd_nodes[(int)Loc::T]);
276 b_count += (m_unitCell->cell_JJ - 2) * (bnd_nodes[(int)Loc::L] + bnd_nodes[(int)Loc::R]);
277
278 // Now the corners (BL, BR, TL, TR)
279 b_count += bnd_nodes[(int)Loc::BL] + bnd_nodes[(int)Loc::BR] + bnd_nodes[(int)Loc::TL] +
280 bnd_nodes[(int)Loc::TR];
281 }
282
283 // The counts above only account for a single KK plane. Now multiply by `m_unitCell->KK` to get
284 // total count.
285 b_count *= m_unitCell->cell_KK;
286 m_communicationNodeCount = b_count;
287 return b_count;
288 }
289
290 template void Cell::populate_node_communication_map(const std::vector<int> &node_map,
291 std::vector<int> &nodes,
292 std::vector<int> &procs) const;
293 template void Cell::populate_node_communication_map(const std::vector<int64_t> &node_map,
294 std::vector<int64_t> &nodes,
295 std::vector<int64_t> &procs) const;
296
297 template <typename INT>
298 void Cell::populate_node_communication_map(const std::vector<INT> &node_map,
299 std::vector<INT> &nodes, std::vector<INT> &procs) const
300 {
301 if (m_communicationNodeCount == 0) {
302 return;
303 }
304
305 nodes.reserve(m_communicationNodeCount);
306 procs.reserve(m_communicationNodeCount);
307
308 // Get list of ranks that this cell shares nodes with...
309 auto ranks = get_shared_ranks(m_ranks);
310 SMART_ASSERT(ranks.size() > 1);
311
312 auto KK = m_unitCell->cell_KK;
313
314 for (auto shared_rank : ranks) {
315 if (shared_rank == rank(Loc::C)) {
316 continue;
317 }
318
319 // a "unit cell" categorizing processor boundary nodes...
320 // Size is 9. Value is '1' if nodes at this location are shared with rank `rank`
321 auto bnd_nodes = categorize_processor_boundary_nodes(shared_rank);
322
323 // Handle Edges, but skip nodes on corners. They are handled later.
324 if (bnd_nodes[(int)Loc::B] == 1) {
325 process_face_nodes(node_map, nodes, procs, m_unitCell->min_J_face, KK, shared_rank);
326 }
327
328 if (bnd_nodes[(int)Loc::T] == 1) {
329 process_face_nodes(node_map, nodes, procs, m_unitCell->max_J_face, KK, shared_rank);
330 }
331
332 if (bnd_nodes[(int)Loc::L] == 1) {
333 process_face_nodes(node_map, nodes, procs, m_unitCell->min_I_face, KK, shared_rank);
334 }
335
336 if (bnd_nodes[(int)Loc::R] == 1) {
337 process_face_nodes(node_map, nodes, procs, m_unitCell->max_I_face, KK, shared_rank);
338 }
339
340 // Now the corners...
341 if (bnd_nodes[(int)Loc::BL] == 1) {
342 process_corner_nodes(node_map, nodes, procs, m_unitCell->min_J_face, KK, shared_rank,
343 Loc::BL);
344 }
345
346 if (bnd_nodes[(int)Loc::BR] == 1) {
347 process_corner_nodes(node_map, nodes, procs, m_unitCell->min_J_face, KK, shared_rank,
348 Loc::BR);
349 }
350
351 if (bnd_nodes[(int)Loc::TL] == 1) {
352 process_corner_nodes(node_map, nodes, procs, m_unitCell->max_J_face, KK, shared_rank,
353 Loc::TL);
354 }
355
356 if (bnd_nodes[(int)Loc::TR] == 1) {
357 process_corner_nodes(node_map, nodes, procs, m_unitCell->max_J_face, KK, shared_rank,
358 Loc::TR);
359 }
360 }
361 SMART_ASSERT(nodes.size() == procs.size())(nodes.size())(procs.size());
362 SMART_ASSERT(nodes.size() == m_communicationNodeCount)(nodes.size())(m_communicationNodeCount);
363 }
364
365 std::vector<int> Cell::categorize_nodes(enum Mode mode) const
366 {
367 auto nodes = m_unitCell->categorize_nodes(has_neighbor_i(), has_neighbor_j());
368 if (mode == Mode::PROCESSOR) {
369 // If there is a processor boundary to the left, then need to change categorization of
370 // all nodes on the left to '0'
371 if (processor_boundary(Loc::L)) {
372 const auto &min_I_face = m_unitCell->min_I_face;
373 for (const auto &node : min_I_face) {
374 nodes[node] -= 1;
375 }
376 }
377 if (processor_boundary(Loc::B)) {
378 const auto &min_J_face = m_unitCell->min_J_face;
379 for (const auto &node : min_J_face) {
380 nodes[node] -= 2;
381 }
382 }
383
384 // Now the "corner case" ;-) If there is a processor boundary below, but the cell to the BL is
385 // on the same rank as this cell, then we have already counted the IJ-line nodes, so need to
386 // categorize those nodes as already accounted for in a previous map...
387 if (processor_boundary(Loc::B) && processor_boundary(Loc::L) && rank(Loc::BL) == rank(Loc::C)) {
388 // Want KK() nodes -- First KK of min_i and of min_j. But since they match, can "unzero"
389 // min_i[0..KK)
390 for (size_t i = 0; i < m_unitCell->cell_KK; i++) {
391 nodes[m_unitCell->min_I_face[i]] = -1;
392 }
393 }
394 // Now the other "corner case"
395 if (processor_boundary(Loc::B) && rank(Loc::BR) == rank(Loc::C)) {
396 // Want KK() nodes -- First KK of max_i and Last KK of min_j. But since they match, can
397 // "unzero" max_i[0..KK)
398 for (size_t i = 0; i < m_unitCell->cell_KK; i++) {
399 nodes[m_unitCell->max_I_face[i]] = -1;
400 }
401 }
402 }
403 return nodes;
404 }
405
406 template std::vector<int64_t> Cell::generate_node_map(Mode, bool, int64_t) const;
407 template std::vector<int> Cell::generate_node_map(Mode, bool, int) const;
408
409 template <typename INT>
410 std::vector<INT> Cell::generate_node_map(Mode mode, bool equivalence_nodes, INT /*dummy*/) const
411 {
412 // Size is node_count + 1 to handle the 1-based connectivity values.
413 size_t cell_node_count = m_unitCell->m_region->get_property("node_count").get_int();
414 std::vector<INT> map(cell_node_count + 1);
415
416 INT offset = mode == Mode::PROCESSOR ? m_localNodeIdOffset : m_globalNodeIdOffset;
417
418 if (!equivalence_nodes || !(has_neighbor_i() || has_neighbor_j())) {
419 std::iota(map.begin(), map.end(), offset);
420 }
421 else if (has_neighbor_i() || has_neighbor_j()) {
422 // At least one neighboring cell and the nodes are being equivalenced
423 // Generate map for the "non-neighbored" nodes (not contiguous with a neighbor cell)
424 auto categorized_nodes = categorize_nodes(mode);
425 SMART_ASSERT(categorized_nodes.size() == cell_node_count)
426 (categorized_nodes.size())(cell_node_count);
427 offset++; // To deal with 1-based node numbers.
428 for (size_t n = 0; n < cell_node_count; n++) {
429 if (categorized_nodes[n] == 0) {
430 map[n + 1] = offset++;
431 }
432 }
433 }
434
435 if (equivalence_nodes && has_neighbor_i() &&
436 (mode == Mode::GLOBAL || (mode == Mode::PROCESSOR && rank(Loc::C) == rank(Loc::L)))) {
437 // Get the neighbor cell...
438 // iterate my unit cell's min_I_face() nodes to get index into map
439 // At this index, set value to this cells min_I_nodes() node
440 // which was created by the neighbor when he was processed...
441 SMART_ASSERT(min_I_nodes.size() == m_unitCell->min_I_face.size())
442 (m_i)(m_j)(min_I_nodes.size())(m_unitCell->min_I_face.size());
443
444 for (size_t i = 0; i < m_unitCell->min_I_face.size(); i++) {
445 auto idx = m_unitCell->min_I_face[i] + 1;
446 auto val = min_I_nodes[i];
447 map[idx] = (INT)val;
448 }
449 }
450
451 if (equivalence_nodes && has_neighbor_j() &&
452 (mode == Mode::GLOBAL || (mode == Mode::PROCESSOR && rank(Loc::C) == rank(Loc::B)))) {
453 SMART_ASSERT(min_J_nodes.size() == m_unitCell->min_J_face.size())
454 (m_i)(m_j)(min_J_nodes.size())(m_unitCell->min_J_face.size());
455
456 for (size_t i = 0; i < m_unitCell->min_J_face.size(); i++) {
457 auto idx = m_unitCell->min_J_face[i] + 1;
458 auto val = min_J_nodes[i];
459 map[idx] = (INT)val;
460 }
461 }
462
463 if (mode == Mode::PROCESSOR) {
464 // Now the "corner case" ;-) If there is a processor boundary below, but the cell to the BL is
465 // on the same rank as this cell, then we have already counted the IJ-line nodes, so need to
466 // categorize those nodes as already accounted for in a previous map...
467 if (processor_boundary(Loc::B) && processor_boundary(Loc::L) && rank(Loc::BL) == rank(Loc::C)) {
468 // Want KK() nodes -- First KK of min_i and of min_j. But since they match, can "unzero"
469 // min_i[0..KK)
470 auto KK = m_unitCell->cell_KK;
471 for (size_t i = 0; i < KK; i++) {
472 auto idx = m_unitCell->min_J_face[i] + 1;
473 auto val = min_J_nodes[i];
474 map[idx] = (INT)val;
475 }
476 }
477 // Now the other "corner case"
478 if (processor_boundary(Loc::B) && rank(Loc::BR) == rank(Loc::C)) {
479 // Want KK() nodes -- First KK of max_i and Last KK of min_j. But since they match, can
480 // "unzero" max_i[0..KK)
481 auto KK = m_unitCell->cell_KK;
482 auto j_offset = min_J_nodes.size() - KK;
483 for (size_t i = 0; i < KK; i++) {
484 auto idx = m_unitCell->min_J_face[j_offset + i] + 1;
485 auto val = min_J_nodes[j_offset + i];
486 map[idx] = (INT)val;
487 }
488 }
489 }
490 // Can now clean out the `min_I_nodes` and `min_J_nodes` lists since the data will no longer be
491 // needed.
492 Ioss::Utils::clear(min_I_nodes);
493 Ioss::Utils::clear(min_J_nodes);
494
495 return map;
496 }
497
498 template void Cell::populate_neighbor(Loc location, const std::vector<int64_t> &map,
499 const Cell &neighbor) const;
500 template void Cell::populate_neighbor(Loc location, const std::vector<int> &map,
501 const Cell &neighbor) const;
502
503 template <typename INT>
504 void Cell::populate_neighbor(Loc location, const std::vector<INT> &map, const Cell &neighbor) const
505 {
506 switch (location) {
507 case Loc::L:
508 neighbor.min_I_nodes.resize(m_unitCell->max_I_face.size());
509 for (size_t i = 0; i < m_unitCell->max_I_face.size(); i++) {
510 auto idx = m_unitCell->max_I_face[i] + 1;
511 auto val = map[idx];
512 neighbor.min_I_nodes[i] = val;
513 }
514 if (debug_level & 8) {
515 fmt::print("\nCell {} {}\n", neighbor.m_i, neighbor.m_j);
516 fmt::print("min_I_nodes: {}\n", fmt::join(neighbor.min_I_nodes, " "));
517 }
518 break;
519
520 case Loc::B:
521 neighbor.min_J_nodes.resize(m_unitCell->max_J_face.size());
522 for (size_t i = 0; i < m_unitCell->max_J_face.size(); i++) {
523 auto idx = m_unitCell->max_J_face[i] + 1;
524 auto val = map[idx];
525 neighbor.min_J_nodes[i] = val;
526 }
527 if (debug_level & 8) {
528 fmt::print("min_J_nodes: {}\n", fmt::join(neighbor.min_J_nodes, " "));
529 }
530 break;
531
532 case Loc::BR: {
533 neighbor.min_J_nodes.resize(m_unitCell->max_J_face.size());
534 auto KK = m_unitCell->cell_KK;
535 auto j_offset = neighbor.min_J_nodes.size() - KK;
536 for (size_t i = 0; i < KK; i++) {
537 auto idx = m_unitCell->max_J_face[i] + 1;
538 auto val = map[idx];
539 neighbor.min_J_nodes[j_offset + i] = val;
540 }
541 } break;
542
543 case Loc::BL: {
544 neighbor.min_J_nodes.resize(m_unitCell->max_J_face.size());
545 auto KK = m_unitCell->cell_KK;
546 auto j_offset = neighbor.min_J_nodes.size() - KK;
547 for (size_t i = 0; i < KK; i++) {
548 auto idx = m_unitCell->max_J_face[j_offset + i] + 1;
549 auto val = map[idx];
550 neighbor.min_J_nodes[i] = val;
551 }
552 } break;
553
554 default:
555 fmt::print(stderr, "\nINTERNAL ERROR: Unhandled direction in populate_neighbor(): {}\n",
556 location);
557 exit(EXIT_FAILURE);
558 }
559 }