Debian Codegraph @

packages/seacas/applications/zellij/UnitCell.C

raw
1 // Copyright(C) 2021, 2023 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 "UnitCell.h"
8 #include <vector>
9
10 #include "Ioss_ElementBlock.h"
11 #include "Ioss_FaceGenerator.h"
12 #include "Ioss_NodeBlock.h"
13 #include "Ioss_Region.h"
14 #include "Ioss_SmartAssert.h"
15 #include "Ioss_Sort.h"
16 #include "fmt/format.h"
17 #include "fmt/ranges.h"
18
19 //! \file
20
21 extern unsigned int debug_level;
22
23 namespace {
24 bool on_boundary(Flg ijk, const Ioss::Face &face, std::vector<int> &categorized_nodes)
25 {
26 int result = (unsigned int)ijk & categorized_nodes[face.connectivity_[0] - 1] &
27 categorized_nodes[face.connectivity_[1] - 1] &
28 categorized_nodes[face.connectivity_[2] - 1] &
29 categorized_nodes[face.connectivity_[3] - 1];
30 return result != 0;
31 }
32
33 Ioss::ElementBlock *get_element_block(Ioss::ElementBlock *block, int64_t elem_id,
34 std::shared_ptr<Ioss::Region> &region)
35 {
36 auto *new_block = block;
37 if (block == nullptr || !block->contains(elem_id)) {
38 new_block = region->get_element_block(elem_id);
39 assert(new_block != nullptr);
40 }
41 return new_block;
42 }
43
44 bool approx_equal(double A, double B)
45 {
46 static double maxRelDiff = 1000.0 * std::numeric_limits<double>::epsilon();
47 static double maxDiff = 100.0 * maxRelDiff;
48
49 // Check if the numbers are really close -- needed
50 // when comparing numbers near zero.
51 double diff = std::abs(A - B);
52 if (diff <= maxDiff) {
53 return true;
54 }
55
56 A = std::abs(A);
57 B = std::abs(B);
58 double largest = std::max(B, A);
59
60 return (diff <= largest * maxRelDiff);
61 }
62
63 void gather_face_nodes(std::vector<double> &coord, std::pair<double, double> &minmax,
64 std::vector<int64_t> &min_face, std::vector<int64_t> &max_face)
65 {
66 for (size_t i = 0; i < coord.size(); i++) {
67 if (approx_equal(coord[i], minmax.first)) {
68 min_face.push_back(i);
69 }
70 if (approx_equal(coord[i], minmax.second)) {
71 max_face.push_back(i);
72 }
73 }
74 SMART_ASSERT(min_face.size() == max_face.size())(min_face.size())(max_face.size());
75 }
76
77 template <typename INT>
78 void sort_face_nodes(std::vector<INT> &face_nodes, const std::vector<double> &coord_j,
79 const std::vector<double> &coord_i)
80 {
81 // NOTE: The `!approx_equal(coord_j[a], coord_j[b])` portion of the less than comparison
82 // is meant to handle values close to but one or both are not exactly zero.
83 Ioss::sort(face_nodes.begin(), face_nodes.end(), [&coord_j](size_t a, size_t b) {
84 return !approx_equal(coord_j[a], coord_j[b]) && float(coord_j[a]) < float(coord_j[b]);
85 });
86 std::stable_sort(face_nodes.begin(), face_nodes.end(), [&coord_i](size_t a, size_t b) {
87 return !approx_equal(coord_i[a], coord_i[b]) && float(coord_i[a]) < float(coord_i[b]);
88 });
89 }
90 } // namespace
91
92 UnitCell::UnitCell(std::shared_ptr<Ioss::Region> region) : m_region(region)
93 {
94 std::vector<double> coord_x;
95 std::vector<double> coord_y;
96 std::vector<double> coord_z;
97
98 auto *nb = region->get_node_blocks()[0];
99 nb->get_field_data("mesh_model_coordinates_x", coord_x);
100 nb->get_field_data("mesh_model_coordinates_y", coord_y);
101 nb->get_field_data("mesh_model_coordinates_z", coord_z);
102
103 const auto x_min_max_it = std::minmax_element(coord_x.begin(), coord_x.end());
104 const auto y_min_max_it = std::minmax_element(coord_y.begin(), coord_y.end());
105
106 minmax_x = std::make_pair(*x_min_max_it.first, *x_min_max_it.second);
107 minmax_y = std::make_pair(*y_min_max_it.first, *y_min_max_it.second);
108
109 if (debug_level & 4) {
110 fmt::print("Min / Max X = {} ... {}\n", minmax_x.first, minmax_x.second);
111 fmt::print("Min / Max Y = {} ... {}\n", minmax_y.first, minmax_y.second);
112 }
113 // Now iterate all nodes and categorize if on a face -- minx, maxx, miny, maxy,
114 gather_face_nodes(coord_x, minmax_x, min_I_face, max_I_face);
115 gather_face_nodes(coord_y, minmax_y, min_J_face, max_J_face);
116 if (debug_level & 4) {
117 fmt::print("Nodes on X Face = {}\n", min_I_face.size());
118 fmt::print("Nodes on Y Face = {}\n", min_J_face.size());
119 }
120
121 sort_face_nodes(min_I_face, coord_z, coord_y);
122 sort_face_nodes(max_I_face, coord_z, coord_y);
123 sort_face_nodes(min_J_face, coord_z, coord_x);
124 sort_face_nodes(max_J_face, coord_z, coord_x);
125 if (debug_level & 4) {
126 // Output each set of nodes --
127 fmt::print("\nSORTED:\n");
128 fmt::print("\tMin/Max I Face:\n");
129 for (size_t i = 0; i < min_I_face.size(); i++) {
130 auto min_I = min_I_face[i];
131 auto max_I = max_I_face[i];
132 fmt::print("\t\t{:10}: {:12.4e} {:12.4e} {:12.4e}\t{:10}: {:12.4e} {:12.4e} {:12.4e}\n",
133 min_I, coord_x[min_I], coord_y[min_I], coord_z[min_I], max_I, coord_x[max_I],
134 coord_y[max_I], coord_z[max_I]);
135 }
136 fmt::print("\tMin/Max J Face:\n");
137 for (size_t i = 0; i < min_J_face.size(); i++) {
138 auto min_J = min_J_face[i];
139 auto max_J = max_J_face[i];
140 fmt::print("\t\t{:10}: {:12.4e} {:12.4e} {:12.4e}\t{:10}: {:12.4e} {:12.4e} {:12.4e}\n",
141 min_J, coord_x[min_J], coord_y[min_J], coord_z[min_J], max_J, coord_x[max_J],
142 coord_y[max_J], coord_z[max_J]);
143 }
144 }
145
146 #ifndef NDEBUG
147 for (size_t i = 0; i < min_I_face.size(); i++) {
148 auto minI = min_I_face[i];
149 auto maxI = max_I_face[i];
150 SMART_ASSERT((size_t)minI < coord_y.size());
151 SMART_ASSERT((size_t)maxI < coord_y.size());
152 SMART_ASSERT((size_t)minI < coord_z.size());
153 SMART_ASSERT((size_t)maxI < coord_z.size());
154 SMART_ASSERT(approx_equal(coord_y[minI], coord_y[maxI]))(coord_y[minI])(coord_y[maxI]);
155 SMART_ASSERT(approx_equal(coord_z[minI], coord_z[maxI]))(coord_z[minI])(coord_z[maxI]);
156 }
157 for (size_t i = 0; i < min_J_face.size(); i++) {
158 auto minJ = min_J_face[i];
159 auto maxJ = max_J_face[i];
160 SMART_ASSERT((size_t)minJ < coord_x.size());
161 SMART_ASSERT((size_t)maxJ < coord_x.size());
162 SMART_ASSERT((size_t)minJ < coord_z.size());
163 SMART_ASSERT((size_t)maxJ < coord_z.size());
164 SMART_ASSERT(approx_equal(coord_x[minJ], coord_x[maxJ]))(coord_x[minJ])(coord_z[maxJ]);
165 SMART_ASSERT(approx_equal(coord_z[minJ], coord_z[maxJ]))(coord_z[minJ])(coord_z[maxJ]);
166 }
167 #endif
168
169 // Determine 'K' -- size of minI_minJ corner list.
170 cell_KK = 0;
171 for (; cell_KK < min_I_face.size(); cell_KK++) {
172 if (min_I_face[cell_KK] != min_J_face[cell_KK]) {
173 break;
174 }
175
176 #ifndef NDEBUG
177 // Checking that we get the same on the maxI_maxJ corner...
178 size_t i_x = max_I_face.size() - 1 - cell_KK;
179 size_t i_y = max_J_face.size() - 1 - cell_KK;
180 SMART_ASSERT(max_I_face[i_x] == max_J_face[i_y])
181 (cell_KK)(i_x)(i_y)(max_I_face[i_x])(max_J_face[i_y]);
182 #endif
183 }
184
185 SMART_ASSERT(min_I_face.size() % cell_KK == 0)(min_I_face.size())(cell_KK);
186 SMART_ASSERT(min_J_face.size() % cell_KK == 0)(min_J_face.size())(cell_KK);
187
188 cell_II = min_J_face.size() / cell_KK;
189 cell_JJ = min_I_face.size() / cell_KK;
190
191 if (debug_level & 4) {
192 fmt::print("\nUnitCell {}:\n", m_region->name());
193 fmt::print("\tThe minI face contains {} nodes\n", min_I_face.size());
194 fmt::print("\tThe minJ face contains {} nodes\n", min_J_face.size());
195 fmt::print("\tThe calculated cell shape is {} x {} x {}\n", cell_II, cell_JJ, cell_KK);
196 }
197 }
198
199 std::vector<int> UnitCell::categorize_nodes(bool neighbor_i, bool neighbor_j, bool all_faces) const
200 {
201 // Create a vector of `node_count` length which has the following values:
202 // 0: Node that is not shared with any neighbors.
203 // 1: Node on min_I face
204 // 2: Node on min_J face
205 // 3: Node on min_I-min_J line
206
207 auto node_count = m_region->get_property("node_count").get_int();
208 std::vector<int> node_category(node_count);
209
210 if (neighbor_i || all_faces) {
211 for (auto node : min_I_face) {
212 node_category[node] = (int)Flg::MIN_I;
213 }
214 }
215
216 if (neighbor_j || all_faces) {
217 for (auto node : min_J_face) {
218 node_category[node] += (int)Flg::MIN_J;
219 }
220 }
221
222 if (all_faces) {
223 for (auto node : max_I_face) {
224 node_category[node] += (int)Flg::MAX_I;
225 }
226
227 for (auto node : max_J_face) {
228 node_category[node] += (int)Flg::MAX_J;
229 }
230 }
231 return node_category;
232 }
233
234 void UnitCell::categorize_z_nodes(std::vector<int> &categorized_nodes)
235 {
236 std::vector<double> coord_z;
237
238 auto *nb = m_region->get_node_blocks()[0];
239 nb->get_field_data("mesh_model_coordinates_z", coord_z);
240
241 const auto z_min_max_it = std::minmax_element(coord_z.begin(), coord_z.end());
242 auto minmax_z = std::make_pair(*z_min_max_it.first, *z_min_max_it.second);
243
244 std::vector<int64_t> min_K_face;
245 std::vector<int64_t> max_K_face;
246 gather_face_nodes(coord_z, minmax_z, min_K_face, max_K_face);
247
248 for (auto node : min_K_face) {
249 categorized_nodes[node] += (int)Flg::MIN_K;
250 }
251 for (auto node : max_K_face) {
252 categorized_nodes[node] += (int)Flg::MAX_K;
253 }
254 }
255
256 void UnitCell::generate_boundary_faces(unsigned int which_faces)
257 {
258 Ioss::FaceGenerator face_generator(*m_region);
259 bool block_by_block = false;
260 bool local_ids = true;
261 if (m_region->get_database()->int_byte_size_api() == 4) {
262 face_generator.generate_faces((int)0, block_by_block, local_ids);
263 }
264 else {
265 face_generator.generate_faces((int64_t)0, block_by_block, local_ids);
266 }
267
268 // Get vector of all boundary faces which need to be categorized into
269 // which face of the unit cell they are on
270 auto categorized_nodes = categorize_nodes(false, false, true);
271 categorize_z_nodes(categorized_nodes);
272
273 if (debug_level & 128) {
274 fmt::print("Node Category: {}\n", fmt::join(categorized_nodes, " "));
275 }
276
277 std::array<enum Flg, 6> boundary_flag{Flg::MIN_I, Flg::MAX_I, Flg::MIN_J,
278 Flg::MAX_J, Flg::MIN_K, Flg::MAX_K};
279 auto &faces = face_generator.faces("ALL");
280 Ioss::ElementBlock *block = nullptr;
281 for (const auto &face : faces) {
282 if (face.element_count() == 1) {
283 block = get_element_block(block, face.element[0] / 10, m_region);
284 auto block_offset = block->get_offset();
285 for (int i = 0; i < 6; i++) {
286 if ((which_faces & (unsigned)boundary_flag[i]) &&
287 on_boundary(boundary_flag[i], face, categorized_nodes)) {
288
289 // Complexity: The `face.element[0]` is 10 * local_element_id + face.
290 // We need to convert that to the offset within the element block
291 // For example, if we have two element blocks with 100 elements each, then
292 // local_element_id 128 would be the 28 element in the second element block
293 // and we would need to store '28 * 10 + face' instead of '128 * 10 + face'
294 // so when we process this element later, we can correctly calculate the
295 // local_element_id in the output file...
296 auto unit_local_element_id = face.element[0] / 10;
297 auto face_ordinal = face.element[0] % 10;
298 auto unit_block_location = (unit_local_element_id - block_offset) * 10 + face_ordinal;
299
300 if (debug_level & 128) {
301 fmt::print("Element {}, Side {} is on boundary {}, block {}\n",
302 unit_block_location / 10, unit_block_location % 10, i, block->name());
303 }
304 boundary_blocks[i].m_faces[block->name()].push_back(unit_block_location);
305 break;
306 }
307 }
308 }
309 }
310
311 if (which_faces & (unsigned)Flg::MIN_I) {
312 SMART_ASSERT(boundary_blocks[(int)Bnd::MIN_I].size() == (cell_JJ - 1) * (cell_KK - 1))
313 (boundary_blocks[(int)Bnd::MIN_I].size())(cell_JJ - 1)(cell_KK - 1);
314 }
315 if (which_faces & (unsigned)Flg::MAX_I) {
316 SMART_ASSERT(boundary_blocks[(int)Bnd::MAX_I].size() == (cell_JJ - 1) * (cell_KK - 1))
317 (boundary_blocks[(int)Bnd::MAX_I].size())(cell_JJ - 1)(cell_KK - 1);
318 }
319
320 if (which_faces & (unsigned)Flg::MIN_J) {
321 SMART_ASSERT(boundary_blocks[(int)Bnd::MIN_J].size() == (cell_II - 1) * (cell_KK - 1))
322 (boundary_blocks[(int)Bnd::MIN_J].size())(cell_II - 1)(cell_KK - 1);
323 }
324 if (which_faces & (unsigned)Flg::MAX_J) {
325 SMART_ASSERT(boundary_blocks[(int)Bnd::MAX_J].size() == (cell_II - 1) * (cell_KK - 1))
326 (boundary_blocks[(int)Bnd::MAX_J].size())(cell_II - 1)(cell_KK - 1);
327 }
328 }