Debian Codegraph @

packages/seacas/applications/exodiff/map.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 <algorithm>
8 #include <cfloat>
9 #include <cstdlib>
10 #include <iomanip>
11 #include <numeric>
12
13 #include "ED_SystemInterface.h"
14 #include "Tolerance.h"
15 #include "exo_block.h"
16 #include "exo_read.h"
17 #include "fmt/ostream.h"
18 #include "iqsort.h"
19 #include "smart_assert.h"
20 #include "util.h"
21
22 namespace {
23 double find_range(const double *x, size_t num_nodes);
24
25 template <typename INT>
26 int64_t Find(double x0, double y0, double z0, const std::vector<double> &x,
27 const std::vector<double> &y, const std::vector<double> &z,
28 const std::vector<INT> &id, int dim, bool ignore_dups);
29
30 template <typename INT>
31 void Compute_Node_Map(std::vector<INT> &node_map, Exo_Read<INT> &file1, Exo_Read<INT> &file2);
32 } // namespace
33
34 template <typename INT>
35 void Compute_Maps(std::vector<INT> &node_map, std::vector<INT> &elmt_map, Exo_Read<INT> &file1,
36 Exo_Read<INT> &file2)
37 {
38 SMART_ASSERT(file1.Open());
39 SMART_ASSERT(file2.Open());
40
41 size_t num_nodes = file1.Num_Nodes();
42 size_t num_elmts = file1.Num_Elements();
43 int dim = file1.Dimension();
44
45 // ******************** elements ******************** //
46
47 // Load global ids (0-offset) into id array.
48 std::vector<INT> id(num_elmts);
49 std::iota(id.begin(), id.end(), 0);
50
51 // Get map storage.
52 node_map.resize(num_nodes);
53 std::fill(node_map.begin(), node_map.end(), -1);
54
55 elmt_map.resize(num_elmts);
56 std::fill(elmt_map.begin(), elmt_map.end(), -1);
57
58 // Create storage for midpoints.
59 std::vector<double> x2;
60 x2.reserve(num_elmts);
61 std::vector<double> y2;
62 y2.reserve(dim > 1 ? num_elmts : 0);
63 std::vector<double> z2;
64 z2.reserve(dim > 2 ? num_elmts : 0);
65
66 // Load coordinates for file 2 and get pointers to them.
67 file2.Load_Nodal_Coordinates();
68 const auto *x2_f = file2.X_Coords();
69 const auto *y2_f = file2.Y_Coords();
70 const auto *z2_f = file2.Z_Coords();
71
72 // Load connectivities for all blocks in second file.
73 file2.Load_Element_Block_Descriptions();
74
75 {
76 // Compute midpoints of each element and place into x,y,z arrays.
77 size_t num_blocks = file2.Num_Element_Blocks();
78 for (size_t b = 0; b < num_blocks; ++b) {
79 const Exo_Block<INT> *block = file2.Get_Element_Block_by_Index(b);
80 size_t num_elmts_in_block = block->Size();
81 size_t num_nodes_per_elmt = block->Num_Nodes_per_Element();
82 for (size_t i = 0; i < num_elmts_in_block; ++i) {
83 const INT *conn = block->Connectivity(i); // Connectivity for element i.
84 double sum_x = 0.0;
85 double sum_y = 0.0;
86 double sum_z = 0.0;
87 for (size_t j = 0; j < num_nodes_per_elmt; ++j) {
88 sum_x += x2_f[conn[j] - 1];
89 if (dim > 1) {
90 sum_y += y2_f[conn[j] - 1];
91 }
92 if (dim > 2) {
93 sum_z += z2_f[conn[j] - 1];
94 }
95 }
96 x2.push_back(sum_x / static_cast<double>(num_nodes_per_elmt));
97 if (dim > 1) {
98 y2.push_back(sum_y / static_cast<double>(num_nodes_per_elmt));
99 }
100 if (dim > 2) {
101 z2.push_back(sum_z / static_cast<double>(num_nodes_per_elmt));
102 }
103 }
104 }
105 }
106
107 // Sort by x value.
108 index_qsort(Data(x2), Data(id), num_elmts);
109
110 #if 0
111 fmt::print("****************** elmts ******************** \n");
112 {for (size_t i = 0; i < num_elmts; ++i)
113 fmt::print("{})\t{}\t{}\t{}\t{}\n"
114 i, x2[id[i]], y2[id[i]], z2[id[i]], id[i]);}
115 fmt::print("****************** elmts ******************** \n");
116 #endif
117 // Load and get nodal coordinates for first file.
118 file1.Load_Nodal_Coordinates();
119 const auto *x1_f = file1.X_Coords();
120 const auto *y1_f = file1.Y_Coords();
121 const auto *z1_f = file1.Z_Coords();
122
123 // Cannot ignore the comparisons, so make sure the coord_tol_type
124 // is not -1 which is "ignore"
125 ToleranceMode save_tolerance_type = interFace.coord_tol.type;
126 if (save_tolerance_type == ToleranceMode::IGNORE_) {
127 interFace.coord_tol.type = ToleranceMode::ABSOLUTE_;
128 }
129
130 // Match elmts in first file to their corresponding elmts in second.
131 size_t num_blocks = file1.Num_Element_Blocks();
132 size_t e1 = 0;
133
134 for (size_t b = 0; b < num_blocks; ++b) {
135 const Exo_Block<INT> *block1 = file1.Get_Element_Block_by_Index(b);
136 file1.Load_Element_Block_Description(b);
137 size_t num_elmts_in_block = block1->Size();
138 size_t num_nodes_per_elmt = block1->Num_Nodes_per_Element();
139 for (size_t i = 0; i < num_elmts_in_block; ++i) {
140 // Connectivity for element i.
141 const INT *conn1 = block1->Connectivity(i);
142
143 // Compute midpoint.
144 double mid_x = 0.0;
145 double mid_y = 0.0;
146 double mid_z = 0.0;
147
148 for (size_t j = 0; j < num_nodes_per_elmt; ++j) {
149 SMART_ASSERT(conn1[j] >= 1 && conn1[j] <= (INT)num_nodes);
150 mid_x += x1_f[conn1[j] - 1];
151 if (dim > 1) {
152 mid_y += y1_f[conn1[j] - 1];
153 }
154 if (dim > 2) {
155 mid_z += z1_f[conn1[j] - 1];
156 }
157 }
158 mid_x /= static_cast<double>(num_nodes_per_elmt);
159 if (dim > 1) {
160 mid_y /= static_cast<double>(num_nodes_per_elmt);
161 }
162 if (dim > 2) {
163 mid_z /= static_cast<double>(num_nodes_per_elmt);
164 }
165
166 // Locate midpoint in sorted array.
167 int64_t sort_idx = Find(mid_x, mid_y, mid_z, x2, y2, z2, id, dim, interFace.ignore_dups);
168
169 if (sort_idx < 0) {
170 Error(fmt::format("Files are different (couldn't match element {} from block {} from first "
171 "file to second)\n",
172 i + 1, file1.Block_Id(b)));
173 }
174 size_t e2 = id[sort_idx];
175
176 // Assign element map for this element.
177 elmt_map[e1] = e2;
178
179 {
180 // Determine the block and elmt index of matched element.
181 auto bl_idx = file2.Global_to_Block_Local(e2 + 1);
182
183 const Exo_Block<INT> *block2 = file2.Get_Element_Block_by_Index(bl_idx.first);
184 SMART_ASSERT(block2 != nullptr);
185
186 // Check that the element types are the same.
187 if (num_nodes_per_elmt != block2->Num_Nodes_per_Element()) {
188 Error(fmt::format("Files are different.\n"
189 " In File 1: Element {} in Block {} has {} and\n"
190 " In File 2: Element {} in Block {} has {}\n",
191 fmt::group_digits(i + 1), file1.Block_Id(b), num_nodes_per_elmt,
192 fmt::group_digits(bl_idx.second + 1), file2.Block_Id(bl_idx.first),
193 block2->Num_Nodes_per_Element()));
194 }
195
196 // Get connectivity for file2 element.
197 const INT *conn2 = block2->Connectivity(bl_idx.second);
198
199 // Match each node in the first elmt with a node in the second
200 // and assign node_map.
201 for (size_t ln1 = 0; ln1 < num_nodes_per_elmt; ++ln1) {
202 // Grab coordinate of node in first file.
203 double x1_val = x1_f[conn1[ln1] - 1];
204 double y1_val = dim > 1 ? y1_f[conn1[ln1] - 1] : 0.0;
205 double z1_val = dim > 2 ? z1_f[conn1[ln1] - 1] : 0.0;
206
207 size_t found = 0;
208 for (size_t ln2 = 0; ln2 < num_nodes_per_elmt; ++ln2) {
209 // Grab coordinate of node in second file.
210 double x2_val = x2_f[conn2[ln2] - 1];
211 double y2_val = dim > 1 ? y2_f[conn2[ln2] - 1] : 0.0;
212 double z2_val = dim > 2 ? z2_f[conn2[ln2] - 1] : 0.0;
213
214 if (!interFace.coord_tol.Diff(x1_val, x2_val) &&
215 !interFace.coord_tol.Diff(y1_val, y2_val) &&
216 !interFace.coord_tol.Diff(z1_val, z2_val)) {
217 // assert that if this node has been given a map
218 // previously, that it agrees with the latest
219 // assignment.
220 if (node_map[conn1[ln1] - 1] >= 0 && node_map[conn1[ln1] - 1] != conn2[ln2] - 1) {
221
222 if (!interFace.ignore_dups) {
223 // Node in file 1.
224 INT node1 = conn1[ln1];
225 double x1a = x1_f[node1 - 1];
226 double y1a = dim >= 2 ? y1_f[node1 - 1] : 0.0;
227 double z1a = dim >= 3 ? z1_f[node1 - 1] : 0.0;
228
229 // Node in file 2 that was already mapped to node 1 in file 1
230 INT n1 = node_map[conn1[ln1] - 1] + 1;
231 double x2a = x2_f[n1 - 1];
232 double y2a = dim >= 2 ? y2_f[n1 - 1] : 0.0;
233 double z2a = dim >= 3 ? z2_f[n1 - 1] : 0.0;
234
235 // Node in file 2 that is now being mapped to node 1 in file 1
236 INT n2 = conn2[ln2];
237 double x2b = x2_f[n2 - 1];
238 double y2b = dim >= 2 ? y2_f[n2 - 1] : 0.0;
239 double z2b = dim >= 3 ? z2_f[n2 - 1] : 0.0;
240
241 SMART_ASSERT(!interFace.coord_tol.Diff(x2a, x2b) &&
242 !interFace.coord_tol.Diff(y2a, y2b) &&
243 !interFace.coord_tol.Diff(z2a, z2b));
244 Error(fmt::format("No unique node mapping possible.\n"
245 "\tFile 1, Node {} at ({}, {}, {}) maps to both:\n"
246 "\tFile 2, Node {} at ({}, {}, {}) and\n"
247 "\tFile 2, Node {} at ({}, {}, {})\n\n",
248 fmt::group_digits(node1), x1a, y1a, z1a, fmt::group_digits(n1),
249 x2a, y2a, z2a, fmt::group_digits(n2), x2b, y2b, z2b));
250 }
251 found = 1;
252 break;
253 }
254 node_map[conn1[ln1] - 1] = conn2[ln2] - 1;
255 found = 1;
256 break;
257 }
258 }
259 if (!found) {
260 std::ostringstream out;
261 fmt::print(out,
262 "\nCannot find a match for node at position {} in first element.\n"
263 "\tFile 1: Element {} in Block {} nodes:\n",
264 ln1 + 1, fmt::group_digits(i + 1), file1.Block_Id(b));
265 for (size_t l1 = 0; l1 < num_nodes_per_elmt; ++l1) {
266 double x_val = x1_f[conn1[l1] - 1];
267 double y_val = dim > 1 ? y1_f[conn1[l1] - 1] : 0.0;
268 double z_val = dim > 2 ? z1_f[conn1[l1] - 1] : 0.0;
269 fmt::print(out, "\t({})\t{}\t{:.9e}\t{:.9e}\t{:.9e}\n", l1 + 1,
270 fmt::group_digits(conn1[l1]), x_val, y_val, z_val);
271 }
272 fmt::print(out, "\tFile 2: Element {} in Block {} nodes:\n",
273 fmt::group_digits(bl_idx.second + 1), file1.Block_Id(b));
274 for (size_t l3 = 0; l3 < num_nodes_per_elmt; ++l3) {
275 double x_val = x2_f[conn2[l3] - 1];
276 double y_val = dim > 1 ? y2_f[conn2[l3] - 1] : 0.0;
277 double z_val = dim > 2 ? z2_f[conn2[l3] - 1] : 0.0;
278 fmt::print(out, "\t({})\t{}\t{:.9e}\t{:.9e}\t{:.9e}\n", l3 + 1,
279 fmt::group_digits(conn2[l3]), x_val, y_val, z_val);
280 }
281 fmt::print(out, "Coordinates compared using tolerance: {} ({}), floor: {}\n",
282 interFace.coord_tol.value, interFace.coord_tol.typestr(),
283 interFace.coord_tol.floor);
284 Error(out);
285 }
286 } // End of local node loop on file1's element.
287 } // End of local node search block.
288
289 ++e1;
290
291 } // End of loop on elements in file1 element block.
292
293 file1.Free_Element_Block(b);
294
295 } // End of loop on file1 blocks.
296
297 // Check that all nodes in the file have been matched... If any
298 // unmatched nodes are found, then perform a node-based matching
299 // algorithm...
300 for (size_t i = 0; i < num_nodes; i++) {
301 if (node_map[i] < 0) {
302 Compute_Node_Map(node_map, file1, file2);
303 break;
304 }
305 }
306
307 file1.Free_Nodal_Coordinates();
308 file2.Free_Nodal_Coordinates();
309 file2.Free_Element_Blocks();
310
311 interFace.coord_tol.type = save_tolerance_type;
312 }
313
314 template <typename INT>
315 void Compute_Partial_Maps(std::vector<INT> &node_map, std::vector<INT> &elmt_map,
316 Exo_Read<INT> &file1, Exo_Read<INT> &file2)
317 {
318 SMART_ASSERT(file1.Open());
319 SMART_ASSERT(file2.Open());
320
321 size_t num_nodes1 = file1.Num_Nodes();
322 size_t num_elmts1 = file1.Num_Elements();
323
324 size_t num_nodes2 = file2.Num_Nodes();
325 size_t num_elmts2 = file2.Num_Elements();
326 int dim = file1.Dimension();
327 SMART_ASSERT(dim == file2.Dimension());
328
329 // ******************** elements ******************** //
330
331 // Load global ids (0-offset) into id array.
332 std::vector<INT> id2(num_elmts2);
333 std::iota(id2.begin(), id2.end(), 0);
334
335 // Get map storage.
336 node_map.resize(num_nodes1);
337 std::fill(node_map.begin(), node_map.end(), -1);
338
339 elmt_map.resize(num_elmts1);
340 std::fill(elmt_map.begin(), elmt_map.end(), -1);
341
342 // Create storage for midpoints.
343 std::vector<double> x2;
344 std::vector<double> y2;
345 std::vector<double> z2;
346 x2.reserve(num_elmts2);
347 if (dim > 1) {
348 y2.reserve(num_elmts2);
349 }
350 if (dim > 2) {
351 z2.reserve(num_elmts2);
352 }
353
354 // Load coordinates for file 2 and get pointers to them.
355 file2.Load_Nodal_Coordinates();
356 const auto *x2_f = file2.X_Coords();
357 const auto *y2_f = file2.Y_Coords();
358 const auto *z2_f = file2.Z_Coords();
359
360 // Load connectivities for all blocks in second file.
361 file2.Load_Element_Block_Descriptions();
362
363 {
364 // Compute midpoints of each element and place into x,y,z arrays.
365 size_t num_blocks2 = file2.Num_Element_Blocks();
366 double sum_x;
367 double sum_y;
368 double sum_z;
369 for (size_t b = 0; b < num_blocks2; ++b) {
370 const Exo_Block<INT> *block = file2.Get_Element_Block_by_Index(b);
371 size_t num_elmts_in_block = block->Size();
372 size_t num_nodes_per_elmt = block->Num_Nodes_per_Element();
373 for (size_t i = 0; i < num_elmts_in_block; ++i) {
374 const INT *conn = block->Connectivity(i); // Connectivity for element i.
375 sum_x = 0.0;
376 sum_y = 0.0;
377 sum_z = 0.0;
378 for (size_t j = 0; j < num_nodes_per_elmt; ++j) {
379 sum_x += x2_f[conn[j] - 1];
380 if (dim > 1) {
381 sum_y += y2_f[conn[j] - 1];
382 }
383 if (dim > 2) {
384 sum_z += z2_f[conn[j] - 1];
385 }
386 }
387 x2.push_back(sum_x / static_cast<double>(num_nodes_per_elmt));
388 if (dim > 1) {
389 y2.push_back(sum_y / static_cast<double>(num_nodes_per_elmt));
390 }
391 if (dim > 2) {
392 z2.push_back(sum_z / static_cast<double>(num_nodes_per_elmt));
393 }
394 }
395 }
396 }
397
398 // Sort by x value.
399 index_qsort(Data(x2), Data(id2), num_elmts2);
400
401 #if 0
402 fmt::print("****************** elmts ******************** \n");
403 {for (size_t i = 0; i < num_elmts; ++i)
404 fmt::print("{})\t{}\t{}\t{}\t{}\n"
405 i, x2[id[i]], y2[id[i]], z2[id[i]], id[i]);}
406 fmt::print("****************** elmts ******************** \n");
407 #endif
408 // Load and get nodal coordinates for first file.
409 file1.Load_Nodal_Coordinates();
410 const auto *x1_f = file1.X_Coords();
411 const auto *y1_f = file1.Y_Coords();
412 const auto *z1_f = file1.Z_Coords();
413
414 // Cannot ignore the comparisons, so make sure the coord_tol_type
415 // is not -1 which is "ignore"
416 ToleranceMode save_tolerance_type = interFace.coord_tol.type;
417 if (save_tolerance_type == ToleranceMode::IGNORE_) {
418 interFace.coord_tol.type = ToleranceMode::ABSOLUTE_;
419 }
420
421 // Match elmts in first file to their corresponding elmts in second.
422 size_t num_blocks1 = file1.Num_Element_Blocks();
423 size_t e1 = 0;
424
425 bool first = true;
426 size_t unmatched = 0;
427 for (size_t b = 0; b < num_blocks1; ++b) {
428 const Exo_Block<INT> *block1 = file1.Get_Element_Block_by_Index(b);
429 file1.Load_Element_Block_Description(b);
430 size_t num_elmts_in_block = block1->Size();
431 size_t num_nodes_per_elmt = block1->Num_Nodes_per_Element();
432 for (size_t i = 0; i < num_elmts_in_block; ++i) {
433 // Connectivity for element i.
434 const INT *conn1 = block1->Connectivity(i);
435
436 // Compute midpoint.
437 double mid_x = 0.0;
438 double mid_y = 0.0;
439 double mid_z = 0.0;
440
441 for (size_t j = 0; j < num_nodes_per_elmt; ++j) {
442 SMART_ASSERT(conn1[j] >= 1 && conn1[j] <= (INT)num_nodes1);
443 mid_x += x1_f[conn1[j] - 1];
444 if (dim > 1) {
445 mid_y += y1_f[conn1[j] - 1];
446 }
447 if (dim > 2) {
448 mid_z += z1_f[conn1[j] - 1];
449 }
450 }
451 mid_x /= static_cast<double>(num_nodes_per_elmt);
452 if (dim > 1) {
453 mid_y /= static_cast<double>(num_nodes_per_elmt);
454 }
455 if (dim > 2) {
456 mid_z /= static_cast<double>(num_nodes_per_elmt);
457 }
458
459 // Locate midpoint in sorted array.
460 int64_t sort_idx = Find(mid_x, mid_y, mid_z, x2, y2, z2, id2, dim, interFace.ignore_dups);
461 if (sort_idx < 0) {
462 unmatched++;
463 if (first && interFace.show_unmatched) {
464 fmt::print("exodiff: Doing Partial Comparison: No Match for (b.e):\n");
465 }
466 first = false;
467 if (interFace.show_unmatched) {
468 fmt::print("{}.{}, ", file1.Block_Id(b), (i + 1));
469 }
470 }
471 else {
472 size_t e2 = id2[sort_idx];
473 elmt_map[e1] = e2;
474
475 // Assign element map for this element.
476
477 // Determine the block and elmt index of matched element.
478 auto bl_idx = file2.Global_to_Block_Local(e2 + 1);
479
480 const Exo_Block<INT> *block2 = file2.Get_Element_Block_by_Index(bl_idx.first);
481 SMART_ASSERT(block2 != nullptr);
482
483 // Check that the element types are the same.
484 if (num_nodes_per_elmt != block2->Num_Nodes_per_Element()) {
485 Error(fmt::format("Files are different.\n"
486 " In File 1: Element {} in Block {} has {} and\n"
487 " In File 2: Element {} in Block {} has {}\n",
488 fmt::group_digits(i + 1), file1.Block_Id(b), num_nodes_per_elmt,
489 fmt::group_digits(bl_idx.second + 1), file2.Block_Id(bl_idx.first),
490 block2->Num_Nodes_per_Element()));
491 }
492
493 // Get connectivity for file2 element.
494 const INT *conn2 = block2->Connectivity(bl_idx.second);
495
496 // Match each node in the first elmt with a node in the second
497 // and assign node_map.
498 for (size_t ln1 = 0; ln1 < num_nodes_per_elmt; ++ln1) {
499 // Grab coordinate of node in first file.
500 double x1_val = x1_f[conn1[ln1] - 1];
501 double y1_val = dim > 1 ? y1_f[conn1[ln1] - 1] : 0.0;
502 double z1_val = dim > 2 ? z1_f[conn1[ln1] - 1] : 0.0;
503 size_t found = 0;
504 for (size_t ln2 = 0; ln2 < num_nodes_per_elmt; ++ln2) {
505 // Grab coordinate of node in second file.
506 double x2_val = x2_f[conn2[ln2] - 1];
507 double y2_val = dim > 1 ? y2_f[conn2[ln2] - 1] : 0.0;
508 double z2_val = dim > 2 ? z2_f[conn2[ln2] - 1] : 0.0;
509
510 if (!interFace.coord_tol.Diff(x1_val, x2_val) &&
511 !interFace.coord_tol.Diff(y1_val, y2_val) &&
512 !interFace.coord_tol.Diff(z1_val, z2_val)) {
513 node_map[conn1[ln1] - 1] = conn2[ln2] - 1;
514 found = 1;
515 break;
516 }
517 }
518 if (!found) {
519 std::ostringstream out;
520 fmt::print(out,
521 "\nCannot find a match for node at position {} in first element.\n"
522 "\tFile 1: Element {} in Block {} nodes:\n",
523 ln1 + 1, fmt::group_digits(i + 1), file1.Block_Id(b));
524 for (size_t l1 = 0; l1 < num_nodes_per_elmt; ++l1) {
525 double x_val = x1_f[conn1[l1] - 1];
526 double y_val = dim > 1 ? y1_f[conn1[l1] - 1] : 0.0;
527 double z_val = dim > 2 ? z1_f[conn1[l1] - 1] : 0.0;
528 fmt::print(out, "\t({})\t{}\t{:.9e}\t{:.9e}\t{:.9e}\n", l1 + 1,
529 fmt::group_digits(conn1[l1]), x_val, y_val, z_val);
530 }
531 fmt::print(out, "\tFile 2: Element {} in Block {} nodes:\n",
532 fmt::group_digits(bl_idx.second + 1), file1.Block_Id(b));
533 for (size_t l3 = 0; l3 < num_nodes_per_elmt; ++l3) {
534 double x_val = x2_f[conn2[l3] - 1];
535 double y_val = dim > 1 ? y2_f[conn2[l3] - 1] : 0.0;
536 double z_val = dim > 2 ? z2_f[conn2[l3] - 1] : 0.0;
537 fmt::print(out, "\t({})\t{}\t{:.9e}\t{:.9e}\t{:.9e}\n", l3 + 1,
538 fmt::group_digits(conn2[l3]), x_val, y_val, z_val);
539 }
540 fmt::print(out, "Coordinates compared using tolerance: {} ({}), floor: {}\n",
541 interFace.coord_tol.value, interFace.coord_tol.typestr(),
542 interFace.coord_tol.floor);
543 Error(out);
544 }
545 } // End of local node loop on file1's element.
546 } // End of local node search block.
547
548 ++e1;
549
550 } // End of loop on elements in file1 element block.
551 file1.Free_Element_Block(b);
552
553 } // End of loop on file1 blocks.
554 if (!first) {
555 fmt::print("\nPartial Map selected -- {} elements unmatched\n", fmt::group_digits(unmatched));
556 }
557 else {
558 if (num_elmts1 == num_elmts2 && num_nodes1 == num_nodes2) {
559 fmt::print(
560 "exodiff: INFO .. Partial Map was specified, but not needed. All elements matched.\n");
561 }
562 }
563
564 // Check that all nodes in the file have been matched... If any
565 // unmatched nodes are found, then perform a node-based matching
566 // algorithm...
567 // for (size_t i=0; i < num_nodes; i++) {
568 // if (node_map[i] < 0) {
569 // Compute_Node_Map(node_map, file1, file2);
570 // break;
571 // }
572 // }
573
574 file1.Free_Nodal_Coordinates();
575 file2.Free_Nodal_Coordinates();
576 file2.Free_Element_Blocks();
577
578 interFace.coord_tol.type = save_tolerance_type;
579 }
580
581 namespace {
582 template <typename INT> bool check_sort(const INT *map, size_t count)
583 {
584 for (size_t i = 1; i < count; i++) {
585 if (map[i - 1] > map[i]) {
586 return true;
587 }
588 }
589 return false;
590 }
591
592 template <typename INT>
593 bool internal_compute_maps(std::vector<INT> &map, const INT *file1_id_map,
594 const INT *file2_id_map, size_t count, const char *type)
595 {
596 std::vector<INT> id1;
597 id1.reserve(count);
598 std::vector<INT> id2;
599 id2.reserve(count);
600 for (size_t i = 0; i < count; i++) {
601 id1.push_back(i);
602 id2.push_back(i);
603 }
604
605 // Check whether sorting needed...
606 bool sort1_needed = check_sort(file1_id_map, count);
607 if (sort1_needed) {
608 index_qsort(file1_id_map, Data(id1), count);
609 }
610
611 bool sort2_needed = check_sort(file2_id_map, count);
612 if (sort2_needed) {
613 index_qsort(file2_id_map, Data(id2), count);
614 }
615
616 for (size_t i = 0; i < count; i++) {
617 if (file1_id_map[id1[i]] == file2_id_map[id2[i]]) {
618 map[id1[i]] = id2[i];
619 }
620 else {
621 Error(fmt::format("Unable to match {0} {1} in first file with {0} in second file.\n", type,
622 fmt::group_digits(file1_id_map[id1[i]])));
623 }
624 }
625
626 // See if there is any mapping happening...
627 bool mapped = false;
628 for (INT i = 0; i < (INT)count; i++) {
629 if (i != map[i]) {
630 mapped = true;
631 break;
632 }
633 }
634 return mapped;
635 }
636 } // namespace
637
638 template <typename INT>
639 void Compute_FileId_Maps(std::vector<INT> &node_map, std::vector<INT> &elmt_map,
640 Exo_Read<INT> &file1, Exo_Read<INT> &file2)
641 {
642 // Compute map of nodes and elements in file1 to nodes and elements in file2
643 // Use the internal exodus node and element number maps in file1 and file2 to
644 // do the matching. Currently assume (and verify) that number of nodes and
645 // elements match in the two files.
646
647 SMART_ASSERT(file1.Open());
648 SMART_ASSERT(file2.Open());
649
650 {
651 size_t num_nodes = file1.Num_Nodes();
652 SMART_ASSERT(num_nodes == file2.Num_Nodes());
653
654 node_map.resize(num_nodes);
655 file1.Load_Node_Map();
656 file2.Load_Node_Map();
657 const INT *node_id_map1 = file1.Get_Node_Map();
658 const INT *node_id_map2 = file2.Get_Node_Map();
659
660 if (!internal_compute_maps(node_map, node_id_map1, node_id_map2, num_nodes, "node")) {
661 node_map.clear();
662 }
663 }
664
665 {
666 size_t num_elmts = file1.Num_Elements();
667 SMART_ASSERT(num_elmts == file2.Num_Elements());
668 elmt_map.resize(num_elmts);
669 file1.Load_Element_Map();
670 file2.Load_Element_Map();
671 const INT *elem_id_map1 = file1.Get_Element_Map();
672 const INT *elem_id_map2 = file2.Get_Element_Map();
673
674 if (!internal_compute_maps(elmt_map, elem_id_map1, elem_id_map2, num_elmts, "element")) {
675 elmt_map.clear();
676 }
677 }
678 }
679
680 template <typename INT>
681 void Dump_Maps(const std::vector<INT> &node_map, const std::vector<INT> &elmt_map,
682 Exo_Read<INT> &file1)
683 {
684 size_t ijk;
685 fmt::print("\n=== node number map (file1 -> file2) local ids\n");
686 bool one_to_one = true;
687 if (!node_map.empty()) {
688 for (ijk = 0; ijk < file1.Num_Nodes(); ++ijk) {
689 if ((INT)ijk != node_map[ijk]) {
690 one_to_one = false;
691 break;
692 }
693 }
694 }
695 if (!one_to_one) {
696 for (ijk = 0; ijk < file1.Num_Nodes(); ++ijk) {
697 fmt::print("{} -> {}\n", (ijk + 1), (node_map[ijk] + 1));
698 }
699 }
700 else {
701 fmt::print(" *** Node map is one-to-one\n");
702 }
703
704 fmt::print("\n=== element number map (file1 -> file2) local ids\n");
705 one_to_one = true;
706 if (!elmt_map.empty()) {
707 for (ijk = 0; ijk < file1.Num_Elements(); ++ijk) {
708 if ((INT)ijk != elmt_map[ijk]) {
709 one_to_one = false;
710 break;
711 }
712 }
713 }
714 if (!one_to_one) {
715 for (ijk = 0; ijk < file1.Num_Elements(); ++ijk) {
716 fmt::print("{} -> {}\n", (ijk + 1), (elmt_map[ijk] + 1));
717 }
718 }
719 else {
720 fmt::print(" *** Element map is one-to-one\n");
721 }
722 fmt::print("===\n");
723 }
724
725 namespace {
726 template <typename INT>
727 void Compute_Node_Map(std::vector<INT> &node_map, Exo_Read<INT> &file1, Exo_Read<INT> &file2)
728 {
729 // This function is called if and only if there are nodes that were
730 // not matched in the Compute_Map function. This is typically the
731 // case if there are 'free' nodes which are not connected to any
732 // elements.
733
734 size_t num_nodes = file1.Num_Nodes();
735 std::vector<INT> mapped_2(num_nodes, -1);
736
737 // Cannot ignore the comparisons, so make sure the coord_tol_type
738 // is not -1 which is "ignore"
739 ToleranceMode save_tolerance_type = interFace.coord_tol.type;
740 if (save_tolerance_type == ToleranceMode::IGNORE_) {
741 interFace.coord_tol.type = ToleranceMode::ABSOLUTE_;
742 }
743
744 // Find unmapped nodes in file2; count the unmapped nodes in file_1.
745 // The code below sets the 'mapped_2' entry to 1 for each node in
746 // file2 which has been mapped to a node in file1
747 size_t count_1 = 0;
748 for (size_t i = 0; i < num_nodes; i++) {
749 if (node_map[i] != -1) {
750 mapped_2[node_map[i]] = 1;
751 }
752 else {
753 count_1++;
754 }
755 }
756
757 // Get list of all unmapped nodes in file1 and file2. A file1
758 // unmapped node will have a '-1' entry in 'node_map' and a file2
759 // unmapped node will have a '-1' entry in 'mapped_2'. Reuse the
760 // 'mapped_2' array to hold the list.
761 std::vector<INT> mapped_1(count_1);
762 size_t count_2 = 0;
763 count_1 = 0;
764 for (size_t i = 0; i < num_nodes; i++) {
765 if (node_map[i] == -1) {
766 mapped_1[count_1++] = i;
767 }
768 if (mapped_2[i] == -1) {
769 mapped_2[count_2++] = i;
770 }
771 }
772
773 // check that umnapped node counts are equal. If not, output
774 // message and exit.
775 if (count_1 != count_2) {
776 Error(fmt::format("Files are different (free node count in file1 is "
777 "{} but file2 free node count is {})\n",
778 fmt::group_digits(count_1), fmt::group_digits(count_2)));
779 }
780
781 // Now, need to match all nodes in 'mapped_1' with nodes in
782 // 'mapped_2'
783 // Get pointer to coordinates...
784 const auto *x1_f = file1.X_Coords();
785 const auto *y1_f = file1.Y_Coords();
786 const auto *z1_f = file1.Z_Coords();
787
788 const auto *x2_f = file2.X_Coords();
789 const auto *y2_f = file2.Y_Coords();
790 const auto *z2_f = file2.Z_Coords();
791
792 // For now, we will try a brute force matching with the hopes that
793 // the number of unmatched nodes is 'small'. If this proves to be a
794 // bottleneck, replace with a better algorithm; perhaps the sorted
795 // matching process used in gjoin...
796 size_t matched = 0;
797 int dim = file1.Dimension();
798 size_t j;
799 for (size_t i = 0; i < count_1; i++) {
800 size_t id_1 = mapped_1[i];
801 for (j = 0; j < count_2; j++) {
802 if (mapped_2[j] >= 0) {
803 size_t id_2 = mapped_2[j];
804 if ((dim == 1 && !interFace.coord_tol.Diff(x1_f[id_1], x2_f[id_2])) ||
805 (dim == 2 && !interFace.coord_tol.Diff(x1_f[id_1], x2_f[id_2]) &&
806 !interFace.coord_tol.Diff(y1_f[id_1], y2_f[id_2])) ||
807 (dim == 3 && !interFace.coord_tol.Diff(x1_f[id_1], x2_f[id_2]) &&
808 !interFace.coord_tol.Diff(y1_f[id_1], y2_f[id_2]) &&
809 !interFace.coord_tol.Diff(z1_f[id_1], z2_f[id_2]))) {
810 node_map[id_1] = id_2;
811 mapped_2[j] = -1;
812 matched++;
813 break;
814 }
815 }
816 }
817 }
818
819 // Check that all nodes were matched.
820 if (matched != count_1) {
821 Error(fmt::format("Unable to match all free nodes in the model. There are {}"
822 " unmatched nodes remaining.\n",
823 fmt::group_digits(count_1 - matched)));
824 }
825 interFace.coord_tol.type = save_tolerance_type;
826 }
827
828 template <typename INT>
829 int64_t Find(double x0, double y0, double z0, const std::vector<double> &x,
830 const std::vector<double> &y, const std::vector<double> &z,
831 const std::vector<INT> &id, int dim, bool ignore_dups)
832 {
833 if (x.empty()) {
834 return -1;
835 }
836
837 // Cannot ignore the comparisons, so make sure the coord_tol_type
838 // is not -1 which is "ignore"
839 ToleranceMode save_tolerance_type = interFace.coord_tol.type;
840 if (save_tolerance_type == ToleranceMode::IGNORE_) {
841 interFace.coord_tol.type = ToleranceMode::ABSOLUTE_;
842 }
843
844 // Find the index such that x0 > x[0,1,...,low-1] and x0 >= x[low]
845 // where x[N] is infinity.
846 auto N = x.size();
847 size_t low = 0;
848 size_t high = N;
849 while (low < high) {
850 size_t mid = (low + high) / 2;
851 if (x[id[mid]] < x0) {
852 low = mid + 1;
853 }
854 else {
855 high = mid;
856 }
857 }
858
859 int64_t i = low == N ? N - 1 : low; // Make sure index falls within array bounds.
860
861 if (i == 0 && interFace.coord_tol.Diff(x[id[i]], x0)) {
862 // Could not find an index within tolerance on x coordinate.
863 return -1;
864 }
865
866 // Drop to first index before which the tolerance fails.
867 while (i > 0 && !interFace.coord_tol.Diff(x[id[i - 1]], x0)) {
868 --i;
869 }
870
871 // Search until tolerance between the x coordinate fails or a match is found.
872 // If a match is found, the loop continues in order to check for dups.
873
874 int64_t index = -1;
875 do {
876 if (dim == 1 || (dim == 2 && !interFace.coord_tol.Diff(y[id[i]], y0)) ||
877 (dim == 3 && !interFace.coord_tol.Diff(y[id[i]], y0) &&
878 !interFace.coord_tol.Diff(z[id[i]], z0))) {
879 if (index >= 0) {
880 if (ignore_dups) {
881 return index;
882 }
883
884 double x1 = x[id[i]];
885 double y1 = dim > 1 ? y[id[i]] : 0.0;
886 double z1 = dim > 2 ? z[id[i]] : 0.0;
887
888 double x2 = x[id[index]];
889 double y2 = dim > 1 ? y[id[index]] : 0.0;
890 double z2 = dim > 2 ? z[id[index]] : 0.0;
891
892 Warning(fmt::format("Two elements in file 2 have the same midpoint (within tolerance).\n"
893 "\tLocal element {} at ({}, {}, {}) and\n"
894 "\tLocal element {} at ({}, {}, {})\n"
895 "\tNo unique element mapping possible.\n",
896 fmt::group_digits(id[i] + 1), x1, y1, z1,
897 fmt::group_digits(id[index] + 1), x2, y2, z2));
898 return -1;
899 }
900
901 index = i;
902 }
903 } while (++i < (int64_t)N && !interFace.coord_tol.Diff(x[id[i]], x0));
904
905 interFace.coord_tol.type = save_tolerance_type;
906 return index;
907 }
908
909 inline double dist_sqrd(double x1, double x2) { return (x2 - x1) * (x2 - x1); }
910
911 inline double dist_sqrd(double x1, double y1, double x2, double y2)
912 {
913 double d1 = x2 - x1;
914 d1 *= d1;
915 double d2 = y2 - y1;
916 d2 *= d2;
917 return (d1 + d2);
918 }
919
920 inline double dist_sqrd(double x1, double y1, double z1, double x2, double y2, double z2)
921 {
922 double d1 = x2 - x1;
923 d1 *= d1;
924 double d2 = y2 - y1;
925 d2 *= d2;
926 double d3 = z2 - z1;
927 d3 *= d3;
928 return (d1 + d2 + d3);
929 }
930
931 double find_range(const double *x, size_t num_nodes)
932 {
933 auto range = std::minmax_element(x, x + num_nodes);
934 return *range.second - *range.first;
935 }
936 } // namespace
937
938 template <typename INT> double Find_Min_Coord_Sep(Exo_Read<INT> &file)
939 {
940 size_t num_nodes = file.Num_Nodes();
941 if (num_nodes < 2) {
942 return 0.0;
943 }
944
945 file.Load_Nodal_Coordinates();
946 const auto *x = file.X_Coords();
947 const auto *y = file.Y_Coords();
948 const auto *z = file.Z_Coords();
949
950 std::vector<INT> indx(num_nodes);
951 std::iota(indx.begin(), indx.end(), 0);
952
953 // Find coordinate with largest range...
954 const double *r = x;
955 double range = find_range(x, num_nodes);
956 if (file.Dimension() > 1) {
957 double yrange = find_range(y, num_nodes);
958 if (yrange > range) {
959 range = yrange;
960 r = y;
961 }
962 }
963
964 if (file.Dimension() > 2) {
965 double zrange = find_range(z, num_nodes);
966 if (zrange > range) {
967 range = zrange;
968 r = z;
969 }
970 }
971
972 // Sort based on coordinate with largest range...
973 index_qsort(r, Data(indx), num_nodes);
974
975 double min = DBL_MAX;
976 switch (file.Dimension()) {
977 case 1: {
978 for (size_t i = 0; i < num_nodes; i++) {
979 for (size_t j = i + 1; j < num_nodes; j++) {
980 double tmp = dist_sqrd(x[indx[i]], x[indx[j]]);
981 if (tmp >= min) {
982 break;
983 }
984 min = tmp;
985 }
986 }
987 break;
988 }
989
990 case 2: {
991 for (size_t i = 0; i < num_nodes; i++) {
992 for (size_t j = i + 1; j < num_nodes; j++) {
993 double delr = dist_sqrd(r[indx[i]], r[indx[j]]);
994 if (delr > min) {
995 break;
996 }
997 double tmp = dist_sqrd(x[indx[i]], y[indx[i]], x[indx[j]], y[indx[j]]);
998 min = min < tmp ? min : tmp;
999 }
1000 }
1001 break;
1002 }
1003
1004 case 3: {
1005 for (size_t i = 0; i < num_nodes; i++) {
1006 for (size_t j = i + 1; j < num_nodes; j++) {
1007 double delr = dist_sqrd(r[indx[i]], r[indx[j]]);
1008 if (delr > min) {
1009 break;
1010 }
1011 double tmp =
1012 dist_sqrd(x[indx[i]], y[indx[i]], z[indx[i]], x[indx[j]], y[indx[j]], z[indx[j]]);
1013 min = min < tmp ? min : tmp;
1014 }
1015 }
1016 break;
1017 }
1018 }
1019 return sqrt(min);
1020 }
1021
1022 template <typename INT>
1023 bool Compare_Maps_Internal(const std::vector<INT> &entity_map, bool partial_flag,
1024 const INT *entity_id_map1, const INT *entity_id_map2,
1025 size_t num_entities1, size_t num_entities2, const char *type)
1026 {
1027 bool diff = false;
1028 int warn_count = 0;
1029
1030 if (!entity_map.empty()) {
1031 if (!interFace.dump_mapping) {
1032 // There is a map between file1 and file2, but all entities are
1033 // used in both files.
1034 for (size_t i = 0; i < num_entities1; i++) {
1035 size_t idx = entity_map[i];
1036 if (idx >= num_entities2) {
1037 continue;
1038 }
1039 if (entity_id_map1[i] != entity_id_map2[idx]) {
1040 if (!(entity_id_map2[idx] == 0 &&
1041 partial_flag)) { // Don't output diff if non-matched and partial
1042 fmt::print(stderr,
1043 "exodiff: WARNING .. The local {} {} with global id {} in file1 has the "
1044 "global id "
1045 "{} in file2.\n",
1046 type, i + 1, entity_id_map1[i], entity_id_map2[idx]);
1047 diff = true;
1048 warn_count++;
1049 if (warn_count >= interFace.max_warnings) {
1050 fmt::print(stderr, "exodiff: WARNING .. Too many warnings, skipping remainder...\n");
1051 break;
1052 }
1053 }
1054 }
1055 }
1056 }
1057 }
1058 else {
1059 // No entity mapping between file1 and file2 -- do a straight compare.
1060 for (size_t i = 0; i < num_entities1; i++) {
1061 if (i >= num_entities2) {
1062 break;
1063 }
1064 if (entity_id_map1[i] != entity_id_map2[i]) {
1065 if (!(entity_id_map2[i] == 0 &&
1066 partial_flag)) { // Don't output diff if non-matched and partial
1067 fmt::print(
1068 stderr,
1069 "exodiff: WARNING .. The local {} {} with global id {} in file1 has the global id "
1070 "{} in file2.\n",
1071 type, i + 1, entity_id_map1[i], entity_id_map2[i]);
1072 diff = true;
1073 warn_count++;
1074 if (warn_count >= interFace.max_warnings) {
1075 fmt::print(stderr, "exodiff: WARNING .. Too many warnings, skipping remainder...\n");
1076 break;
1077 }
1078 }
1079 }
1080 }
1081 }
1082 return diff;
1083 }
1084
1085 template <typename INT>
1086 bool Compare_Maps(Exo_Read<INT> &file1, Exo_Read<INT> &file2, const std::vector<INT> &node_map,
1087 const std::vector<INT> &elmt_map, bool partial_flag)
1088 {
1089 // Check whether the node and element number maps from both file1
1090 // and file2 match which indicates that we are comparing the same
1091 // element and node in each file.
1092
1093 size_t num_nodes1 = file1.Num_Nodes();
1094 size_t num_nodes2 = file2.Num_Nodes();
1095
1096 // NOTE: file1 maps are already loaded...
1097 file2.Load_Node_Map();
1098
1099 const INT *node_id_map1 = file1.Get_Node_Map();
1100 const INT *node_id_map2 = file2.Get_Node_Map();
1101
1102 bool diff_nodes = Compare_Maps_Internal(node_map, partial_flag, node_id_map1, node_id_map2,
1103 num_nodes1, num_nodes2, "node");
1104 file2.Free_Node_Map();
1105
1106 size_t num_elmts1 = file1.Num_Elements();
1107 size_t num_elmts2 = file2.Num_Elements();
1108
1109 // NOTE: file1 maps are already loaded...
1110 file2.Load_Element_Map();
1111
1112 const INT *elem_id_map1 = file1.Get_Element_Map();
1113 const INT *elem_id_map2 = file2.Get_Element_Map();
1114
1115 bool diff_elems = Compare_Maps_Internal(elmt_map, partial_flag, elem_id_map1, elem_id_map2,
1116 num_elmts1, num_elmts2, "element");
1117
1118 file2.Free_Element_Map();
1119
1120 if (diff_nodes || diff_elems) {
1121 fmt::print("\n");
1122 }
1123 return diff_nodes || diff_elems;
1124 }
1125
1126 template void Compute_Maps(std::vector<int> &node_map, std::vector<int> &elmt_map,
1127 Exo_Read<int> &file1, Exo_Read<int> &file2);
1128 template bool Compare_Maps(Exo_Read<int> &file1, Exo_Read<int> &file2,
1129 const std::vector<int> &node_map, const std::vector<int> &elmt_map,
1130 bool partial_flag);
1131
1132 template void Compute_Partial_Maps(std::vector<int> &node_map, std::vector<int> &elmt_map,
1133 Exo_Read<int> &file1, Exo_Read<int> &file2);
1134 template void Compute_FileId_Maps(std::vector<int> &node_map, std::vector<int> &elmt_map,
1135 Exo_Read<int> &file1, Exo_Read<int> &file2);
1136 template void Dump_Maps(const std::vector<int> &node_map, const std::vector<int> &elmt_map,
1137 Exo_Read<int> &file1);
1138 template double Find_Min_Coord_Sep(Exo_Read<int> &file);
1139
1140 template void Compute_Maps(std::vector<int64_t> &node_map, std::vector<int64_t> &elmt_map,
1141 Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2);
1142 template bool Compare_Maps(Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2,
1143 const std::vector<int64_t> &node_map,
1144 const std::vector<int64_t> &elmt_map, bool partial_flag);
1145
1146 template void Compute_Partial_Maps(std::vector<int64_t> &node_map, std::vector<int64_t> &elmt_map,
1147 Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2);
1148 template void Compute_FileId_Maps(std::vector<int64_t> &node_map, std::vector<int64_t> &elmt_map,
1149 Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2);
1150 template void Dump_Maps(const std::vector<int64_t> &node_map, const std::vector<int64_t> &elmt_map,
1151 Exo_Read<int64_t> &file1);
1152 template double Find_Min_Coord_Sep(Exo_Read<int64_t> &file);