Debian Codegraph @

packages/seacas/applications/exodiff/create_file.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 "ED_SystemInterface.h" // for SystemInterface, interFace
8 #include "Tolerance.h" // for Tolerance, etc
9 #include "exo_entity.h" // for Exo_Entity, EXOTYPE
10 #include "exodusII.h"
11 #include "fmt/color.h"
12 #include "fmt/ostream.h"
13 #include "smart_assert.h" // for SMART_ASSERT
14 #include "stringx.h" // for find_string, etc
15 #include "util.h"
16 #include <cstddef> // for size_t
17 #include <cstdio> // for nullptr
18 #include <string> // for string, char_traits, etc
19 #include <vector> // for vector
20 template <typename INT> class Exo_Read;
21
22 namespace {
23 void build_variable_names(const char *type, NameList &names, std::vector<Tolerance> &tols,
24 const Tolerance &default_tol, bool do_all_flag,
25 const NameList &var_names1, const NameList &var_names2,
26 bool *diff_found);
27
28 template <typename INT>
29 void build_truth_table(EXOTYPE type, const char *label, NameList &names, size_t num_entity,
30 Exo_Read<INT> &file1, Exo_Read<INT> &file2, const NameList &var_names1,
31 const NameList &var_names2, std::vector<int> &truth_tab, bool quiet_flag,
32 bool *diff_found);
33
34 void output_exodus_names(int file_id, EXOTYPE type, const NameList &names);
35 void output_diff_names(const char *type, const NameList &names);
36 void output_compare_names(const char *type, const NameList &names,
37 const std::vector<Tolerance> &tol, int num_vars1, int num_vars2);
38 } // namespace
39
40 template <typename INT>
41 void Build_Variable_Names(Exo_Read<INT> &file1, Exo_Read<INT> &file2, bool *diff_found)
42 {
43 // Build (and compare) global variable names.
44 build_variable_names("global", interFace.glob_var_names, interFace.glob_var,
45 interFace.glob_var_default, interFace.glob_var_do_all_flag,
46 file1.Global_Var_Names(), file2.Global_Var_Names(), diff_found);
47
48 // Build (and compare) nodal variable names.
49 build_variable_names("nodal", interFace.node_var_names, interFace.node_var,
50 interFace.node_var_default, interFace.node_var_do_all_flag,
51 file1.Nodal_Var_Names(), file2.Nodal_Var_Names(), diff_found);
52
53 // Build (and compare) element variable names.
54 build_variable_names("element", interFace.elmt_var_names, interFace.elmt_var,
55 interFace.elmt_var_default, interFace.elmt_var_do_all_flag,
56 file1.Element_Var_Names(), file2.Element_Var_Names(), diff_found);
57
58 // Build (and compare) element variable names.
59 if (!interFace.ignore_attributes) {
60 build_variable_names("element attribute", interFace.elmt_att_names, interFace.elmt_att,
61 interFace.elmt_att_default, interFace.elmt_att_do_all_flag,
62 file1.Element_Att_Names(), file2.Element_Att_Names(), diff_found);
63 }
64
65 // Build (and compare) nodeset variable names.
66 build_variable_names("nodeset", interFace.ns_var_names, interFace.ns_var,
67 interFace.ns_var_default, interFace.ns_var_do_all_flag, file1.NS_Var_Names(),
68 file2.NS_Var_Names(), diff_found);
69
70 // Build (and compare) sideset variable names.
71 build_variable_names("sideset", interFace.ss_var_names, interFace.ss_var,
72 interFace.ss_var_default, interFace.ss_var_do_all_flag, file1.SS_Var_Names(),
73 file2.SS_Var_Names(), diff_found);
74
75 // Build (and compare) edgeblock variable names.
76 build_variable_names("edgeblock", interFace.eb_var_names, interFace.eb_var,
77 interFace.eb_var_default, interFace.eb_var_do_all_flag, file1.EB_Var_Names(),
78 file2.EB_Var_Names(), diff_found);
79
80 // Build (and compare) faceblock variable names.
81 build_variable_names("faceblock", interFace.fb_var_names, interFace.fb_var,
82 interFace.fb_var_default, interFace.fb_var_do_all_flag, file1.FB_Var_Names(),
83 file2.FB_Var_Names(), diff_found);
84 }
85
86 template <typename INT>
87 int Create_File(Exo_Read<INT> &file1, Exo_Read<INT> &file2, const std::string &diffile_name,
88 bool *diff_found)
89 {
90 // Multiple modes:
91 // summary_flag == true --> Single file, output summary and variable names, return
92 // diffile_name == "" --> Dual file, output summary, variable names, check compatibility,
93 // diffile_name != "" --> Three files (2 in, 1 out)
94 // create output file which is diff of input.
95 // output summary, variable names, check compatibility
96 // quiet_flag == true --> don't output summary information
97
98 SMART_ASSERT(!interFace.summary_flag);
99 //========================================================================
100 // From here on down, have two input files and possibly 1 output file...
101 // Create output file.
102
103 int out_file_id = -1;
104 if (!diffile_name.empty()) {
105
106 // Take minimum word size for output file.
107 int iows =
108 file1.IO_Word_Size() < file2.IO_Word_Size() ? file1.IO_Word_Size() : file2.IO_Word_Size();
109 int compws = sizeof(double);
110
111 int mode = EX_CLOBBER;
112 if (sizeof(INT) == 8) {
113 mode |= EX_ALL_INT64_DB;
114 mode |= EX_ALL_INT64_API;
115 }
116 out_file_id = ex_create(diffile_name.c_str(), mode, &compws, &iows);
117 if (out_file_id < 0) {
118 Error(fmt::format("Couldn't create output file \"{}\".\n", diffile_name));
119 }
120 ex_copy(file1.File_ID(), out_file_id);
121 }
122
123 if (!interFace.quiet_flag) {
124 if (out_file_id >= 0) { // The files are to be differenced .. just list names.
125 if (interFace.coord_tol.type != ToleranceMode::IGNORE_) {
126 fmt::print("Coordinates: tol: {:8g} {}, floor: {:8g}\n", interFace.coord_tol.value,
127 interFace.coord_tol.typestr(), interFace.coord_tol.floor);
128 }
129 else {
130 fmt::print("Locations of nodes will not be considered.\n");
131 }
132 if (interFace.time_tol.type != ToleranceMode::IGNORE_) {
133 fmt::print("Time step values: tol: {:8g} {}, floor: {:8g}\n", interFace.time_tol.value,
134 interFace.time_tol.typestr(), interFace.time_tol.floor);
135 }
136 else {
137 fmt::print("Time step time values will not be differenced.\n");
138 }
139 output_diff_names("Global", interFace.glob_var_names);
140 output_diff_names("Nodal", interFace.node_var_names);
141 output_diff_names("Element", interFace.elmt_var_names);
142 output_diff_names("Element Attribute", interFace.elmt_att_names);
143 output_diff_names("Nodeset", interFace.ns_var_names);
144 output_diff_names("Sideset", interFace.ss_var_names);
145 output_diff_names("Edgeblock", interFace.eb_var_names);
146 output_diff_names("Faceblock", interFace.fb_var_names);
147 }
148 else { // The files are to be compared .. echo additional info.
149 if (Tolerance::use_old_floor) {
150 std::ostringstream info;
151 fmt::print(info, "INFO: Using old definition of floor tolerance. |a-b|<floor.\n\n");
152 DIFF_OUT(info, fmt::color::yellow);
153 }
154 if (interFace.coord_tol.type != ToleranceMode::IGNORE_) {
155 fmt::print(
156 "\nNodal coordinates will be compared:\n {:<{}} tol: {:8g} ({}), floor: {:8g}\n",
157 "...", name_length(), interFace.coord_tol.value, interFace.coord_tol.typestr(),
158 interFace.coord_tol.floor);
159 }
160 else {
161 std::ostringstream info;
162 fmt::print(info, "\nNodal coordinates will not be compared.\n");
163 DIFF_OUT(info, fmt::color::yellow);
164 }
165
166 if (interFace.time_tol.type != ToleranceMode::IGNORE_) {
167 fmt::print("Time step values will be compared:\n {:<{}} tol: {:8g} ({}), floor: {:8g}\n",
168 "...", name_length(), interFace.time_tol.value, interFace.time_tol.typestr(),
169 interFace.time_tol.floor);
170 }
171 else {
172 std::ostringstream info;
173 fmt::print(info, "Time step time values will not be compared.\n");
174 DIFF_OUT(info, fmt::color::yellow);
175 }
176
177 output_compare_names("Global", interFace.glob_var_names, interFace.glob_var,
178 file1.Num_Global_Vars(), file2.Num_Global_Vars());
179
180 output_compare_names("Nodal", interFace.node_var_names, interFace.node_var,
181 file1.Num_Nodal_Vars(), file2.Num_Nodal_Vars());
182
183 output_compare_names("Element", interFace.elmt_var_names, interFace.elmt_var,
184 file1.Num_Element_Vars(), file2.Num_Element_Vars());
185
186 output_compare_names("Element Attribute", interFace.elmt_att_names, interFace.elmt_att,
187 file1.Num_Element_Atts(), file2.Num_Element_Atts());
188
189 output_compare_names("Nodeset", interFace.ns_var_names, interFace.ns_var, file1.Num_NS_Vars(),
190 file2.Num_NS_Vars());
191
192 output_compare_names("Sideset", interFace.ss_var_names, interFace.ss_var, file1.Num_SS_Vars(),
193 file2.Num_SS_Vars());
194 if (!interFace.ignore_sideset_df && interFace.ss_df_tol.type != ToleranceMode::IGNORE_ &&
195 file1.Num_Side_Sets() > 0 && file2.Num_Side_Sets() > 0) {
196 fmt::print("Sideset Distribution Factors will be compared:\n {:<{}} tol: {:8g} ({}), "
197 "floor: {:8g}\n",
198 "...", name_length(), interFace.ss_df_tol.value, interFace.ss_df_tol.typestr(),
199 interFace.ss_df_tol.floor);
200 }
201 else {
202 if (interFace.ignore_sideset_df || interFace.ss_df_tol.type == ToleranceMode::IGNORE_) {
203 std::ostringstream info;
204 fmt::print(info, "Sideset Distribution Factors will not be compared.\n");
205 DIFF_OUT(info, fmt::color::yellow);
206 }
207 else {
208 fmt::print("No Sideset Distribution Factors on either file.\n");
209 }
210 }
211 output_compare_names("Edgeblock", interFace.eb_var_names, interFace.eb_var,
212 file1.Num_EB_Vars(), file2.Num_EB_Vars());
213 output_compare_names("Faceblock", interFace.fb_var_names, interFace.fb_var,
214 file1.Num_FB_Vars(), file2.Num_FB_Vars());
215 }
216 }
217
218 std::vector<int> truth_tab;
219 build_truth_table(EX_ELEM_BLOCK, "Element Block", interFace.elmt_var_names,
220 file1.Num_Element_Blocks(), file1, file2, file1.Element_Var_Names(),
221 file2.Element_Var_Names(), truth_tab, interFace.quiet_flag, diff_found);
222
223 std::vector<int> ns_truth_tab;
224 build_truth_table(EX_NODE_SET, "Nodeset", interFace.ns_var_names, file1.Num_Node_Sets(), file1,
225 file2, file1.NS_Var_Names(), file2.NS_Var_Names(), ns_truth_tab,
226 interFace.quiet_flag, diff_found);
227
228 std::vector<int> ss_truth_tab;
229 build_truth_table(EX_SIDE_SET, "Sideset", interFace.ss_var_names, file1.Num_Side_Sets(), file1,
230 file2, file1.SS_Var_Names(), file2.SS_Var_Names(), ss_truth_tab,
231 interFace.quiet_flag, diff_found);
232
233 std::vector<int> eb_truth_tab;
234 build_truth_table(EX_EDGE_BLOCK, "Edgeblock", interFace.eb_var_names, file1.Num_Edge_Blocks(),
235 file1, file2, file1.EB_Var_Names(), file2.EB_Var_Names(), eb_truth_tab,
236 interFace.quiet_flag, diff_found);
237
238 std::vector<int> fb_truth_tab;
239 build_truth_table(EX_FACE_BLOCK, "Faceblock", interFace.fb_var_names, file1.Num_Face_Blocks(),
240 file1, file2, file1.FB_Var_Names(), file2.FB_Var_Names(), fb_truth_tab,
241 interFace.quiet_flag, diff_found);
242
243 // Put out the concatenated variable parameters here and then
244 // put out the names....
245 if (out_file_id >= 0) {
246 ex_put_all_var_param(out_file_id, interFace.glob_var_names.size(),
247 interFace.node_var_names.size(), interFace.elmt_var_names.size(),
248 Data(truth_tab), interFace.ns_var_names.size(), Data(ns_truth_tab),
249 interFace.ss_var_names.size(), Data(ss_truth_tab));
250
251 output_exodus_names(out_file_id, EX_GLOBAL, interFace.glob_var_names);
252 output_exodus_names(out_file_id, EX_NODAL, interFace.node_var_names);
253 output_exodus_names(out_file_id, EX_ELEM_BLOCK, interFace.elmt_var_names);
254 output_exodus_names(out_file_id, EX_NODE_SET, interFace.ns_var_names);
255 output_exodus_names(out_file_id, EX_SIDE_SET, interFace.ss_var_names);
256 output_exodus_names(out_file_id, EX_EDGE_BLOCK, interFace.eb_var_names);
257 output_exodus_names(out_file_id, EX_FACE_BLOCK, interFace.fb_var_names);
258 }
259 return out_file_id;
260 }
261
262 namespace {
263 void output_exodus_names(int file_id, EXOTYPE type, const NameList &names)
264 {
265 if (!names.empty()) {
266 std::vector<char *> vars(names.size());
267 for (unsigned i = 0; i < names.size(); ++i) {
268 vars[i] = const_cast<char *>(names[i].c_str());
269 }
270 ex_put_variable_names(file_id, type, names.size(), Data(vars));
271 }
272 }
273
274 void output_compare_names(const char *type, const NameList &names,
275 const std::vector<Tolerance> &tol, int num_vars1, int num_vars2)
276 {
277 if (!names.empty()) {
278 fmt::print("{} variables to be compared:\n", type);
279 for (unsigned v = 0; v < names.size(); ++v) {
280 if (v == 0) {
281 fmt::print(" {:<{}} tol: {:8g} ({}), floor: {:8g}\n", names[v], name_length(),
282 tol[v].value, tol[v].typestr(), tol[v].floor);
283 }
284 else {
285 fmt::print(" {:<{}} {:8g} ({}), {:8g}\n", names[v], name_length(),
286 tol[v].value, tol[v].typestr(), tol[v].floor);
287 }
288 }
289 }
290 else if (num_vars1 == 0 && num_vars2 == 0) {
291 fmt::print("No {} variables on either file.\n", type);
292 }
293 else {
294 fmt::print("{} variables will not be compared.\n", type);
295 }
296 }
297
298 void output_diff_names(const char *type, const NameList &names)
299 {
300 if (!names.empty()) {
301 fmt::print("{} variables to be differenced:\n", type);
302 for (const auto &name : names) {
303 fmt::print("\t{}\n", name);
304 }
305 }
306 else {
307 fmt::print("No {} variables will be differenced.\n", type);
308 }
309 }
310
311 void build_variable_names(const char *type, NameList &names, std::vector<Tolerance> &tols,
312 const Tolerance &default_tol, bool do_all_flag,
313 const NameList &var_names1, const NameList &var_names2,
314 bool *diff_found)
315 {
316 NameList x_list; // exclusion list
317 for (auto name : names) {
318 chop_whitespace(name);
319 SMART_ASSERT(!name.empty());
320 if (name[0] == '!') {
321 x_list.push_back(extract_token(name, "!")); // remove "!" & add
322 }
323 }
324
325 if (do_all_flag) {
326 auto length_name = var_names1.size();
327 for (size_t n = 0; n < length_name; ++n) {
328 const std::string &name = var_names1[n];
329 if (!interFace.summary_flag &&
330 find_string(var_names2, name, interFace.nocase_var_names) < 0) {
331 if (find_string(x_list, name, interFace.nocase_var_names) < 0) {
332 if (interFace.allowNameMismatch) {
333 x_list.push_back(name);
334 }
335 else {
336 *diff_found = true;
337 if (!interFace.quiet_flag) {
338 std::ostringstream diff;
339 fmt::print(diff,
340 "exodiff: DIFFERENCE .. The {} variable \"{}\" is in the first file but "
341 "not the second.\n",
342 type, name);
343 DIFF_OUT(diff);
344 }
345 continue;
346 }
347 }
348 }
349 if (find_string(names, name, interFace.nocase_var_names) < 0 &&
350 find_string(x_list, name, interFace.nocase_var_names) < 0) {
351 names.push_back(name);
352 tols.push_back(default_tol);
353 SMART_ASSERT(names.size() == tols.size())(names.size())(tols.size());
354 }
355 }
356
357 if (!interFace.noSymmetricNameCheck) {
358 length_name = var_names2.size();
359 for (size_t n = 0; n < length_name; ++n) {
360 const std::string &name = var_names2[n];
361 if (!interFace.summary_flag &&
362 find_string(var_names1, name, interFace.nocase_var_names) < 0) {
363 if (find_string(x_list, name, interFace.nocase_var_names) < 0) {
364 *diff_found = true;
365 if (!interFace.quiet_flag) {
366 std::ostringstream diff;
367 fmt::print(diff,
368 "exodiff: DIFFERENCE .. The {} variable \"{}\" is in the second file "
369 "but not the first.\n",
370 type, name);
371 DIFF_OUT(diff);
372 }
373 continue;
374 }
375 }
376 SMART_ASSERT(find_string(names, name, interFace.nocase_var_names) >= 0 ||
377 find_string(x_list, name, interFace.nocase_var_names) >= 0);
378 }
379 }
380 }
381
382 NameList tmp_list;
383 std::vector<Tolerance> tmp_tols;
384 for (size_t n = 0; n < names.size(); ++n) {
385 std::string name = names[n];
386 chop_whitespace(name);
387 if (name[0] == '!') {
388 continue;
389 }
390
391 int idx = find_string(var_names1, name, interFace.nocase_var_names);
392 if (idx >= 0) {
393 if (interFace.summary_flag ||
394 find_string(var_names2, name, interFace.nocase_var_names) >= 0) {
395 tmp_tols.push_back(tols[n]);
396 tmp_list.push_back(var_names1[idx]);
397 }
398 else {
399 *diff_found = true;
400 if (!interFace.quiet_flag) {
401 std::ostringstream diff;
402 fmt::print(diff,
403 "exodiff: DIFFERENCE .. The {} variable \"{}\" is in the first file, but "
404 "not the second file.\n",
405 type, name);
406 DIFF_OUT(diff);
407 }
408 }
409 }
410 else {
411 // Variable is in `names`, but not in `var_names1`. This is a difference unless the file
412 // has changesets in which case the variable might exist in a different change set. In this
413 // case, we check that if the variable does not exist in in either `var_names1` or
414 // `var_names2` and there are change sets, there is no diff.
415 if (interFace.has_change_sets &&
416 (interFace.summary_flag ||
417 find_string(var_names2, name, interFace.nocase_var_names) < 0)) {
418 // OK that variable not found in file1 and file2
419 }
420 else {
421 *diff_found = true;
422 if (!interFace.quiet_flag) {
423 std::ostringstream diff;
424 fmt::print(
425 diff,
426 "exodiff: DIFFERENCE .. Specified {} variable \"{}\" is not in the first file.\n",
427 type, name);
428 DIFF_OUT(diff);
429 }
430 }
431 }
432 }
433 names = tmp_list;
434 tols = tmp_tols;
435 SMART_ASSERT(names.size() == tols.size())(names.size())(tols.size());
436 }
437
438 template <typename INT>
439 void build_truth_table(EXOTYPE type, const char *label, NameList &names, size_t num_entity,
440 Exo_Read<INT> &file1, Exo_Read<INT> &file2, const NameList &var_names1,
441 const NameList &var_names2, std::vector<int> &truth_tab, bool quiet_flag,
442 bool *diff_found)
443 {
444 if (!names.empty()) {
445 int num_vars = names.size();
446
447 truth_tab.resize(num_vars * num_entity);
448 for (int i = num_vars * num_entity - 1; i >= 0; --i) {
449 truth_tab[i] = 0;
450 }
451
452 for (size_t b = 0; b < num_entity; ++b) {
453 Exo_Entity *set1 = file1.Get_Entity_by_Index(type, b);
454 Exo_Entity *set2 = nullptr;
455 if (interFace.by_name) {
456 set2 = file2.Get_Entity_by_Name(type, set1->Name());
457 }
458 else {
459 set2 = file2.Get_Entity_by_Id(type, set1->Id());
460 }
461
462 if (set2 == nullptr) {
463 if (interFace.map_flag != MapType::PARTIAL) {
464 *diff_found = true;
465 std::ostringstream diff;
466 fmt::print(diff,
467 "exodiff: DIFFERENCE {} id {} exists in first file but not the second...\n",
468 label, set1->Id());
469 DIFF_OUT(diff);
470 }
471 continue;
472 }
473
474 for (int out_idx = 0; out_idx < num_vars; ++out_idx) {
475 const std::string &name = names[out_idx];
476 int idx1 = find_string(var_names1, name, interFace.nocase_var_names);
477 int idx2 = find_string(var_names2, name, interFace.nocase_var_names);
478 if (idx1 < 0 || idx2 < 0) {
479 Error(fmt::format("Unable to find variable named '{}' on database.\n", name));
480 }
481
482 if (set1->is_valid_var(idx1)) {
483 if (set2->is_valid_var(idx2)) {
484 truth_tab[b * num_vars + out_idx] = 1;
485 }
486 else if (!quiet_flag) {
487 std::ostringstream diff;
488 fmt::print(diff,
489 "exodiff: INFO {0} variable \"{1}\" is not saved for {0}"
490 " id {2} in the second file but is in the first (by virtue of the truth "
491 "tables). "
492 "This variable won't be considered for this {0}.\n",
493 label, name, set1->Id());
494 DIFF_OUT(diff, fmt::color::yellow);
495 }
496 }
497 else if (set2->is_valid_var(idx2) && !quiet_flag) {
498 std::ostringstream diff;
499 fmt::print(
500 diff,
501 "exodiff: INFO {0} variable \"{1}\" is not saved for {0}"
502 " id {2} in the first file but is in the second (by virtue of the truth tables). "
503 "This variable won't be considered for this {0}.\n",
504 label, name, set1->Id());
505 DIFF_OUT(diff, fmt::color::yellow);
506 }
507 }
508 }
509 }
510 }
511 } // End of namespace
512
513 template int Create_File(Exo_Read<int> &file1, Exo_Read<int> &file2,
514 const std::string &diffile_name, bool *diff_found);
515 template void Build_Variable_Names(Exo_Read<int> &file1, Exo_Read<int> &file2, bool *diff_found);
516
517 template int Create_File(Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2,
518 const std::string &diffile_name, bool *diff_found);
519 template void Build_Variable_Names(Exo_Read<int64_t> &file1, Exo_Read<int64_t> &file2,
520 bool *diff_found);