Debian Codegraph @

packages/seacas/applications/ejoin/EJ_match_xyz.C

raw
1 // Copyright(C) 1999-2020, 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 #include "EJ_CodeTypes.h"
7 #include "EJ_index_sort.h" // for index_coord_sort
8 #include "EJ_mapping.h" // for eliminate_omitted_nodes
9 #include "EJ_vector3d.h" // for vector3d
10 #include "Ioss_NodeBlock.h" // for NodeBlock
11 #include "Ioss_Property.h" // for Property
12 #include "Ioss_Region.h" // for Region, NodeBlockContainer
13 #include "Ioss_SmartAssert.h"
14 #include <algorithm> // for max, min
15 #include <cfloat> // for FLT_MAX
16 #include <cstddef> // for size_t
17 #include <fmt/format.h>
18
19 namespace {
20 template <typename INT>
21 void do_matching(std::vector<INT> &i_inrange, const RealVector &i_coord, size_t i_offset,
22 std::vector<INT> &j_inrange, const RealVector &j_coord, size_t j_offset,
23 double epsilon, int XYZ, std::vector<INT> &local_node_map);
24
25 double max3(double x, double y, double z)
26 {
27 double max = x;
28 if (y > max) {
29 max = y;
30 }
31 if (z > max) {
32 max = z;
33 }
34 return max;
35 }
36
37 void find_range(const std::vector<double> &coord, vector3d &min, vector3d &max)
38 {
39 if (!coord.empty()) {
40 min.set(coord[0], coord[1], coord[2]);
41 max = min;
42 for (size_t i = 3; i < coord.size(); i += 3) {
43 if (min.x > coord[i + 0]) {
44 min.x = coord[i + 0];
45 }
46 if (min.y > coord[i + 1]) {
47 min.y = coord[i + 1];
48 }
49 if (min.z > coord[i + 2]) {
50 min.z = coord[i + 2];
51 }
52
53 if (max.x < coord[i + 0]) {
54 max.x = coord[i + 0];
55 }
56 if (max.y < coord[i + 1]) {
57 max.y = coord[i + 1];
58 }
59 if (max.z < coord[i + 2]) {
60 max.z = coord[i + 2];
61 }
62 }
63 }
64 else {
65 min.set(0, 0, 0);
66 max = min;
67 }
68 }
69
70 template <typename INT>
71 void find_in_range(const std::vector<double> &coord, const vector3d &min, const vector3d &max,
72 std::vector<INT> &in_range)
73 {
74 if (!coord.empty()) {
75 for (size_t i = 0; i < coord.size(); i += 3) {
76 if (coord[i + 0] > min.x && coord[i + 0] < max.x && coord[i + 1] > min.y &&
77 coord[i + 1] < max.y && coord[i + 2] > min.z && coord[i + 2] < max.z) {
78 in_range.push_back(i / 3);
79 }
80 }
81 }
82 }
83 } // namespace
84
85 template <typename INT>
86 void match_node_xyz(RegionVector &part_mesh, double tolerance, std::vector<INT> &global_node_map,
87 std::vector<INT> &local_node_map)
88 {
89 // See if any omitted element blocks...
90 bool has_omissions = false;
91 for (auto &elem : part_mesh) {
92 if (elem->get_property("block_omission_count").get_int() > 0) {
93 has_omissions = true;
94 break;
95 }
96 }
97
98 if (!has_omissions) {
99 for (size_t i = 0; i < local_node_map.size(); i++) {
100 local_node_map[i] = i;
101 }
102 }
103 else {
104 std::vector<INT> dummy;
105 bool fill_global = false;
106 eliminate_omitted_nodes(part_mesh, dummy, local_node_map, fill_global);
107 SMART_ASSERT(dummy.empty());
108
109 // The local_node_map is not quite in the correct format after the
110 // call to 'eliminate_omitted_nodes'. We need all non-omitted
111 // nodes to have local_node_map[i] == i.
112 for (size_t i = 0; i < local_node_map.size(); i++) {
113 if (local_node_map[i] >= 0) {
114 local_node_map[i] = i;
115 }
116 }
117 }
118
119 size_t part_count = part_mesh.size();
120 enum { X = 0, Y = 1, Z = 2 };
121
122 for (size_t ip = 0; ip < part_count; ip++) {
123 vector3d i_max;
124 vector3d i_min;
125 std::vector<double> i_coord;
126 Ioss::NodeBlock *inb = part_mesh[ip]->get_node_blocks()[0];
127 inb->get_field_data("mesh_model_coordinates", i_coord);
128 find_range(i_coord, i_min, i_max);
129
130 size_t i_offset = part_mesh[ip]->get_property("node_offset").get_int();
131
132 for (size_t jp = ip + 1; jp < part_count; jp++) {
133 vector3d j_max;
134 vector3d j_min;
135 std::vector<double> j_coord;
136 Ioss::NodeBlock *jnb = part_mesh[jp]->get_node_blocks()[0];
137 jnb->get_field_data("mesh_model_coordinates", j_coord);
138 find_range(j_coord, j_min, j_max);
139
140 size_t j_offset = part_mesh[jp]->get_property("node_offset").get_int();
141
142 // See if the ranges overlap...
143 vector3d max;
144 vector3d min;
145 max.x = std::min(i_max.x, j_max.x);
146 max.y = std::min(i_max.y, j_max.y);
147 max.z = std::min(i_max.z, j_max.z);
148
149 min.x = std::max(i_min.x, j_min.x);
150 min.y = std::max(i_min.y, j_min.y);
151 min.z = std::max(i_min.z, j_min.z);
152
153 double delta[3];
154 int XYZ = X;
155 delta[XYZ] = max.x - min.x;
156 delta[Y] = max.y - min.y;
157 if (delta[Y] > delta[XYZ]) {
158 XYZ = Y;
159 }
160 delta[Z] = max.z - min.z;
161 if (delta[Z] > delta[XYZ]) {
162 XYZ = Z;
163 }
164
165 double epsilon = (delta[X] + delta[Y] + delta[Z]) / 1.0e3;
166 if (epsilon < 0.0) {
167 fmt::print("Parts {} and {} do not overlap.\n", ip, jp);
168 continue;
169 }
170
171 min -= epsilon;
172 max += epsilon;
173
174 if (tolerance >= 0.0) {
175 epsilon = tolerance;
176 }
177
178 std::vector<INT> j_inrange;
179 std::vector<INT> i_inrange;
180
181 find_in_range(j_coord, min, max, j_inrange);
182 find_in_range(i_coord, min, max, i_inrange);
183
184 // Index sort all nodes on the coordinate range with the maximum delta.
185 index_coord_sort(i_coord, i_inrange, XYZ);
186 index_coord_sort(j_coord, j_inrange, XYZ);
187
188 if (i_inrange.size() < j_inrange.size()) {
189 do_matching(i_inrange, i_coord, i_offset, j_inrange, j_coord, j_offset, epsilon, XYZ,
190 local_node_map);
191 }
192 else {
193 do_matching(j_inrange, j_coord, j_offset, i_inrange, i_coord, i_offset, epsilon, XYZ,
194 local_node_map);
195 }
196 }
197 }
198
199 // Build the global and local maps...
200 size_t j = 1;
201 for (size_t i = 0; i < local_node_map.size(); i++) {
202 if (local_node_map[i] == (INT)i) {
203 global_node_map.push_back(j);
204 local_node_map[i] = j - 1;
205 j++;
206 }
207 else if (local_node_map[i] >= 0) {
208 local_node_map[i] = local_node_map[local_node_map[i]];
209 }
210 }
211 }
212 template void match_node_xyz(RegionVector &part_mesh, double tolerance,
213 std::vector<int> &global_node_map, std::vector<int> &local_node_map);
214 template void match_node_xyz(RegionVector &part_mesh, double tolerance,
215 std::vector<int64_t> &global_node_map,
216 std::vector<int64_t> &local_node_map);
217
218 namespace {
219 template <typename INT>
220 void do_matching(std::vector<INT> &i_inrange, const RealVector &i_coord, size_t i_offset,
221 std::vector<INT> &j_inrange, const RealVector &j_coord, size_t j_offset,
222 double epsilon, int XYZ, std::vector<INT> &local_node_map)
223 {
224 INT j2beg = 0;
225
226 INT match = 0;
227 INT compare = 0;
228
229 double g_dismin = FLT_MAX;
230 double dismax = -FLT_MAX;
231
232 for (auto ii : i_inrange) {
233
234 if (local_node_map[ii + i_offset] < 0) {
235 continue;
236 }
237
238 double dismin = FLT_MAX;
239 double dmin = FLT_MAX;
240 INT node_dmin = -1;
241
242 for (size_t j = j2beg; j < j_inrange.size(); j++) {
243 compare++;
244 INT jj = j_inrange[j];
245 if (jj < 0 || local_node_map[jj + j_offset] < 0) {
246 continue;
247 }
248
249 if (i_coord[3 * ii + XYZ] - epsilon > j_coord[3 * jj + XYZ]) {
250 j2beg = j;
251 continue;
252 }
253
254 //... Since we are sorted on coordinate X|Y|Z,
255 // if set 'j' X|Y|Z greater than set 'i' X|Y|Z+eps, go to next 'i' X1|Y1|Z1 coord.
256 if (j_coord[3 * jj + XYZ] - epsilon > i_coord[3 * ii + XYZ]) {
257 break;
258 }
259
260 double distance = max3(std::fabs(j_coord[3 * jj + 0] - i_coord[3 * ii + 0]),
261 std::fabs(j_coord[3 * jj + 1] - i_coord[3 * ii + 1]),
262 std::fabs(j_coord[3 * jj + 2] - i_coord[3 * ii + 2]));
263
264 if (float(distance) <= float(epsilon)) {
265 if (distance < dmin) {
266 dmin = distance;
267 node_dmin = j;
268 }
269 }
270 else {
271 if (distance < dismin) {
272 dismin = distance;
273 }
274 }
275 if (distance == 0.0) {
276 break;
277 }
278 }
279
280 if (dmin <= epsilon && node_dmin >= 0) {
281 INT jnod = j_inrange[node_dmin] + j_offset;
282 INT inod = ii + i_offset;
283 match++;
284 if (dmin > dismax) {
285 dismax = dmin;
286 }
287 j_inrange[node_dmin] *= -1;
288 SMART_ASSERT(jnod < (INT)local_node_map.size());
289 if (inod < jnod) {
290 local_node_map[jnod] = inod;
291 }
292 else {
293 local_node_map[inod] = jnod;
294 }
295 }
296 else {
297 if (dismin < g_dismin) {
298 g_dismin = dismin;
299 }
300 }
301 }
302 fmt::print("\nNumber of nodes matched = {}\n", match);
303 fmt::print("Number of comparisons = {}\n", compare);
304 fmt::print("Tolerance used for matching = {}\n", epsilon);
305 if (dismax > double(-FLT_MAX)) {
306 fmt::print("Maximum distance between matched nodes = {}\n", dismax);
307 }
308 if (g_dismin < double(FLT_MAX)) {
309 fmt::print("Minimum distance between nonmatched nodes = {}\n", g_dismin);
310 }
311 fmt::print("\n");
312 }
313 } // namespace