Debian Codegraph @

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

raw
1 // Copyright(C) 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 <array>
8 #include <assert.h>
9 #include <fmt/core.h>
10 #include <numeric>
11 #include <stddef.h>
12 #include <string>
13
14 #include "Ioss_ChainGenerator.h"
15 #include "Ioss_ElementBlock.h"
16 #include "Ioss_ElementTopology.h"
17 #include "Ioss_FaceGenerator.h"
18 #include "Ioss_Property.h"
19 #include "Ioss_Region.h"
20 #include "Ioss_SideBlock.h"
21 #include "Ioss_SideSet.h"
22 #include "Ioss_Utils.h"
23 #include "ioss_export.h"
24 #include "robin_hash.h"
25 #include "tokenize.h"
26
27 // ========================================================================
28
29 namespace {
30 template <typename INT> using front_t = std::vector<std::pair<INT, int>>;
31 using connectivity_t = std::vector<std::array<const Ioss::Face *, 6>>;
32
33 int debug = 0;
34
35 // 0-based face
36 int hex_opposite_side(int side)
37 {
38 switch (side) {
39 case 0: return 2;
40 case 1: return 3;
41 case 2: return 0;
42 case 3: return 1;
43 case 4: return 5;
44 case 5: return 4;
45 }
46 return -1;
47 }
48
49 Ioss::NameList get_adjacent_blocks(Ioss::Region &region, const std::string &surface_list)
50 {
51 Ioss::NameList adjacent_blocks;
52 if (surface_list == "ALL") {
53 const Ioss::SideSetContainer &fss = region.get_sidesets();
54 for (const auto &fs : fss) {
55 // Save a list of all blocks that are adjacent to the surfaces...
56 Ioss::NameList blocks;
57 fs->block_membership(blocks);
58 for (const auto &block : blocks) {
59 adjacent_blocks.push_back(block); // May have duplicates at this point.
60 }
61 }
62 }
63 else {
64 auto selected_surfaces = Ioss::tokenize(surface_list, ",");
65 for (const auto &surface : selected_surfaces) {
66 auto *sset = region.get_sideset(surface);
67 if (sset != nullptr) {
68 // Save a list of all blocks that are adjacent to the surfaces...
69 Ioss::NameList blocks;
70 sset->block_membership(blocks);
71 for (const auto &block : blocks) {
72 adjacent_blocks.push_back(block); // May have duplicates at this point.
73 }
74 }
75 else {
76 fmt::print("\nWARNING: Surface '{}' does not exist in this model.\n", surface);
77 }
78 }
79 }
80
81 Ioss::Utils::uniquify(adjacent_blocks);
82 return adjacent_blocks;
83 }
84
85 template <typename INT>
86 void get_line_front(Ioss::SideSet *fs, const Ioss::ElementBlock *block,
87 Ioss::chain_t<INT> &element_chains, front_t<INT> &front)
88 {
89 const auto &adj_block_name = block->name();
90 Ioss::NameList blocks;
91 fs->block_membership(blocks);
92 for (const auto &fs_block : blocks) {
93 if (fs_block == adj_block_name) {
94 // This faceset has some elements that are in `adj_block_name` -- put those in the `front`
95 // list. Get list of "sides" in this faceset...
96 std::vector<INT> element_side;
97 assert(fs->side_block_count() == 1);
98 const auto *fb = fs->get_block(0);
99 fb->get_field_data("element_side_raw", element_side);
100
101 // Mark each element so we know it is on the sideset(s)
102 for (size_t i = 0; i < element_side.size(); i += 2) {
103 auto element = element_side[i];
104 if (block->contains(element)) {
105 if (element_chains[element - 1] == Ioss::chain_entry_t<INT>()) {
106 int side = static_cast<int>(element_side[i + 1]); // 1-based sides
107 element_chains[element - 1] = Ioss::chain_entry_t<INT>{element, 0};
108 front.push_back(std::make_pair(element, side));
109 if (debug & 16) {
110 fmt::print("Putting element {}, side {} in front.\n", element, side);
111 }
112 }
113 }
114 }
115 }
116 }
117 }
118
119 template <typename INT>
120 front_t<INT> get_line_front(Ioss::Region &region, const Ioss::ElementBlock *block,
121 Ioss::chain_t<INT> &element_chains, const std::string &surface_list)
122 {
123 front_t<INT> front;
124
125 // Since lines can not cross element blocks, we can process everything a block at a time.
126 assert(block != nullptr);
127 assert(block->topology()->shape() == Ioss::ElementShape::HEX);
128
129 if (surface_list == "ALL") {
130 const Ioss::SideSetContainer &fss = region.get_sidesets();
131 for (const auto &fs : fss) {
132 get_line_front(fs, block, element_chains, front);
133 }
134 }
135 else {
136 auto selected_surfaces = Ioss::tokenize(surface_list, ",");
137 for (const auto &surface : selected_surfaces) {
138 auto *sset = region.get_sideset(surface);
139 if (sset != nullptr) {
140 get_line_front(sset, block, element_chains, front);
141 }
142 }
143 }
144 return front;
145 }
146
147 void generate_face_connectivity(const Ioss::FaceUnorderedSet &faces, int offset,
148 connectivity_t &face_connectivity)
149 {
150 for (const auto &face : faces) {
151 for (int i = 0; i < face.element_count(); i++) {
152 auto element = face.element[i] / 10 - offset;
153 auto side = face.element[i] % 10; // 0-based side
154 face_connectivity[element][side] = &face;
155 }
156 }
157
158 if (debug & 16) {
159 fmt::print("\n-----------------------------\n");
160 int l = 1;
161 for (size_t i = 0; i < face_connectivity.size(); i++) {
162 for (size_t j = 0; j < 6; j++) {
163 const auto *face = face_connectivity[i][j];
164 assert(face != nullptr);
165 int k = (face->element_count() > 1 && face->element[0] / 10 - offset != i) ? 1 : 0;
166 auto element = face->element[k] / 10;
167 auto side = face->element[k] % 10;
168 assert(side == j);
169 if (face->element_count() > 1) {
170 fmt::print(
171 "[{:3}] Element {}, Side {}/{} is Face {}.\tAdjacent to Element {}, Side {}.\n",
172 l++, element, side, j, face->hashId_, face->element[1 - k] / 10,
173 face->element[1 - k] % 10);
174 }
175 else {
176 fmt::print("[{:3}] Element {}, Side {}/{} is Face {}.\n", l++, element, side, j,
177 face->hashId_);
178 }
179 }
180 }
181 }
182 }
183 } // namespace
184
185 namespace Ioss {
186
187 template IOSS_EXPORT Ioss::chain_t<int> generate_element_chains(Ioss::Region &region,
188 const std::string &, int, int);
189 template IOSS_EXPORT Ioss::chain_t<int64_t>
190 generate_element_chains(Ioss::Region &region, const std::string &, int, int64_t);
191
192 template <typename INT>
193 Ioss::chain_t<INT> generate_element_chains(Ioss::Region &region, const std::string &surface_list,
194 int debug_level, INT /*dummy*/)
195 {
196 region.get_database()->progress(__func__);
197
198 debug = debug_level;
199 size_t numel = region.get_property("element_count").get_int();
200
201 // Determine which element block(s) are adjacent to the faceset specifying "lines"
202 // The `adjacent_blocks` contains the names of all element blocks that are adjacent to the
203 // surface(s) that specify the faces at the 'root' of the lines...
204 Ioss::NameList adjacent_block_names = get_adjacent_blocks(region, surface_list);
205 if (adjacent_block_names.empty()) {
206 fmt::print("WARNING: No surfaces in the model matched the input surface list ({}).\n\tNo "
207 "chains will be generated.\n",
208 surface_list);
209 }
210
211 // Get the EB* corresponding to the EB names...
212 Ioss::ElementBlockContainer adjacent_blocks;
213 adjacent_blocks.reserve(adjacent_block_names.size());
214 for (const auto &blk_name : adjacent_block_names) {
215 auto *eb = region.get_element_block(blk_name);
216 assert(eb != nullptr);
217 if (eb->topology()->shape() != Ioss::ElementShape::HEX) {
218 fmt::print("Skipping Element Block {}; it does not contain HEX elements.\n", blk_name);
219 }
220 else {
221 adjacent_blocks.push_back(eb);
222 }
223 }
224
225 // Generate the faces for use later... (only generate on the blocks touching the front)
226 Ioss::FaceGenerator face_generator(region);
227 face_generator.generate_block_faces(adjacent_blocks, (INT)0, true);
228 region.get_database()->progress("\tAfter generate_block_faces");
229
230 Ioss::chain_t<INT> element_chains(numel);
231 for (const auto *block : adjacent_blocks) {
232 // Get the offset into the element_chains vector...
233 auto offset = block->get_offset() + 1;
234 auto count = block->entity_count();
235
236 auto front = get_line_front(region, block, element_chains, surface_list);
237 if (front.empty()) {
238 continue;
239 }
240
241 // We want a vector giving us the Face for each face of each element in the block...
242 connectivity_t face_connectivity(count);
243 generate_face_connectivity(face_generator.faces(block), static_cast<int>(offset),
244 face_connectivity);
245 face_generator.clear(block);
246
247 // For each face on the "front" (at the beginning the boundary sideset faces)
248 // Set `element_chains` to the `face` "ID"
249 // We are only working on the elements that are in the current block...
250 front_t<INT> next_front;
251 while (!front.empty()) {
252 if (debug & 16) {
253 fmt::print("\n----------------------\n");
254 }
255 next_front.reserve(front.size());
256 for (auto &element_side : front) {
257 auto element = element_side.first;
258 auto side = element_side.second - 1;
259
260 auto opp_side = hex_opposite_side(side);
261 assert(opp_side >= 0);
262 auto *opp_face = face_connectivity[element - offset][opp_side];
263 // See if there is an element attached to the `opp_side`
264 if (opp_face->element_count() > 1) {
265 // Determine which is current element and which is adjacent element...
266 int index = (opp_face->element[0] / 10 ==
267 static_cast<typename decltype(opp_face->element)::value_type>(element))
268 ? 1
269 : 0;
270 auto nxt_element = opp_face->element[index] / 10;
271 auto nxt_side = opp_face->element[index] % 10;
272 if (element_chains[nxt_element - 1] == Ioss::chain_entry_t<INT>()) {
273 element_chains[nxt_element - 1] = element_chains[element - 1];
274 element_chains[nxt_element - 1].link++;
275 if (debug & 16) {
276 fmt::print("At element {}, side {} -- Next in chain is element {}, side {}\n",
277 element, side, nxt_element, nxt_side);
278 }
279 next_front.push_back(
280 std::make_pair(static_cast<INT>(nxt_element), static_cast<int>(nxt_side + 1)));
281 }
282 else {
283 if (debug & 16) {
284 fmt::print("At element {}, side {} -- Termination of chain {} of size {}.\n",
285 element, side, element_chains[element - 1].element,
286 element_chains[element - 1].link + 1);
287 }
288 }
289 }
290 else {
291 if (debug & 16) {
292 fmt::print("At element {}, side {} -- Termination of chain {} of size {}.\n", element,
293 side, element_chains[element - 1].element,
294 element_chains[element - 1].link + 1);
295 }
296 }
297 }
298 std::swap(front, next_front);
299 next_front.clear();
300 }
301 } // End of block loop
302 region.get_database()->progress("\tAfter generating chains");
303 return element_chains;
304 }
305 } // namespace Ioss