Debian Codegraph @

packages/seacas/applications/conjoin/Conjoin.C

raw
1 // Copyright(C) 1999-2025 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 <algorithm>
7 #include <exception>
8 #include <fmt/chrono.h>
9 #include <fmt/ostream.h>
10 #include <iomanip>
11 #include <iostream>
12 #include <iterator>
13 #include <numeric>
14 #include <set>
15 #include <stdexcept>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 #include <cfloat>
21 #include <cmath>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <limits>
25 #include <unistd.h>
26
27 #include <cctype>
28 #include <cstring>
29 #include <ctime>
30
31 #include "add_to_log.h"
32 #include "adler.h"
33 #include "copy_string_cpp.h"
34 #include "format_time.h"
35 #include "sys_info.h"
36 #include "vector_data.h"
37 #if !USE_STD_SORT
38 #include "pdqsort.h"
39 #endif
40 #include "smart_assert.h"
41 #include "time_stamp.h"
42 #include <exodusII.h>
43
44 #if EX_API_VERS_NODOT <= 467
45 #error "Requires exodusII version 4.68 or later"
46 #endif
47
48 #include "CJ_ExodusEntity.h"
49 #include "CJ_ExodusFile.h"
50 #include "CJ_Internals.h"
51 #include "CJ_ObjectType.h"
52 #include "CJ_SystemInterface.h"
53 #include "CJ_Variables.h"
54 #include "CJ_Version.h"
55
56 namespace {
57 bool check_variable_params(size_t p, const Excn::Variables &vars);
58 template <typename T> void clear(std::vector<T> &vec)
59 {
60 vec.clear();
61 vec.shrink_to_fit();
62 SMART_ASSERT(vec.capacity() == 0);
63 }
64
65 template <typename T> bool approx_equal(T v1, T v2)
66 {
67 #if 0
68 static const T tolerance = 100.0 * std::numeric_limits<T>::epsilon();
69 return std::fabs(v1 - v2) <= std::fabs(v1+v2)*tolerance;
70 #else
71 return (float)v1 == (float)v2;
72 #endif
73 }
74 } // namespace
75
76 struct NodeInfo
77 {
78 NodeInfo() = default;
79 NodeInfo(size_t id_, double x_, double y_, double z_) : id(id_), x(x_), y(y_), z(z_) {}
80 size_t id{0};
81 double x{0.0};
82 double y{0.0};
83 double z{0.0};
84
85 bool operator==(const NodeInfo &other) const
86 {
87 return id == other.id && approx_equal(x, other.x) && approx_equal(y, other.y) &&
88 approx_equal(z, other.z);
89 }
90
91 bool operator!=(const NodeInfo &other) const { return !(*this == other); }
92
93 bool operator<(const NodeInfo &other) const
94 {
95 if (id < other.id) {
96 return true;
97 }
98 if (id > other.id) {
99 return false;
100 }
101 SMART_ASSERT(id == other.id);
102 if (!approx_equal(x, other.x) && x < other.x) {
103 return true;
104 }
105 if (!approx_equal(x, other.x) && x > other.x) {
106 return false;
107 }
108 SMART_ASSERT(approx_equal(x, other.x));
109 if (!approx_equal(y, other.y) && y < other.y) {
110 return true;
111 }
112 if (!approx_equal(y, other.y) && y > other.y) {
113 return false;
114 }
115 SMART_ASSERT(approx_equal(y, other.y));
116 if (!approx_equal(z, other.z) && z < other.z) {
117 return true;
118 }
119 return false;
120 }
121 };
122
123 using GlobalMap = std::vector<NodeInfo>;
124 using GMapIter = GlobalMap::iterator;
125
126 extern double seacas_timer();
127
128 namespace {
129 template <class T> struct TimeStepMap
130 {
131 TimeStepMap(size_t part, int step, T time)
132 : partNumber(part), localStepNumber(step), timeValue(time)
133 {
134 }
135 size_t partNumber;
136 int localStepNumber;
137 T timeValue;
138 };
139
140 int get_width(size_t max_value);
141
142 ex_entity_type exodus_object_type(const Excn::ObjectType &conjoin_type)
143 {
144 switch (conjoin_type) {
145 case Excn::ObjectType::EBLK: return EX_ELEM_BLOCK;
146 case Excn::ObjectType::SSET: return EX_SIDE_SET;
147 case Excn::ObjectType::NSET: return EX_NODE_SET;
148 default:
149 throw std::runtime_error("Invalid Object Type in exodus_object_type: " +
150 std::to_string(static_cast<int>(conjoin_type)));
151 }
152 }
153
154 char **get_name_array(int size, size_t length)
155 {
156 char **names = nullptr;
157 if (size > 0) {
158 names = new char *[size];
159 for (int i = 0; i < size; i++) {
160 names[i] = new char[length + 1];
161 std::memset(names[i], '\0', length + 1);
162 }
163 }
164 return names;
165 }
166
167 void free_name_array(char **names, int size)
168 {
169 for (int i = 0; i < size; i++) {
170 delete[] names[i];
171 }
172 delete[] names;
173 names = nullptr;
174 }
175 } // namespace
176
177 std::string tsFormat = "[{:%H:%M:%S}] ";
178
179 // prototypes
180
181 // The main program templated to permit float/double transfer.
182 template <typename T, typename INT>
183 int conjoin(Excn::SystemInterface &interFace, T /* dummy */, INT /* dummy int */);
184
185 namespace {
186 void sort_file_times(StringVector &input_files);
187
188 void compress_white_space(char *str);
189 void add_info_record(char *info_record, int size);
190 template <typename INT> void put_mesh_summary(const Excn::Mesh<INT> &mesh);
191
192 template <typename T>
193 void get_put_qa(int id, int id_out, const std::vector<TimeStepMap<T>> &global_times,
194 Excn::SystemInterface &interFace);
195 int get_put_coordinate_names(int in, int out, int dimensionality);
196
197 template <typename T, typename INT>
198 int get_put_coordinates(Excn::Mesh<INT> &global, size_t part_count,
199 std::vector<Excn::Mesh<INT>> &local_mesh, T dummy);
200
201 template <typename T, typename INT>
202 int get_coordinates(int id, int dimensionality, size_t num_nodes,
203 std::vector<INT> local_node_to_global, size_t part, std::vector<T> &x,
204 std::vector<T> &y, std::vector<T> &z);
205
206 StringVector get_exodus_variable_names(int id, ex_entity_type elType, size_t var_count);
207
208 template <typename T, typename INT>
209 void filter_truth_table(int id, Excn::Mesh<INT> &global, std::vector<T> &glob_blocks,
210 Excn::Variables &vars, const StringIdVector &variable_names);
211
212 template <typename T, typename INT>
213 void get_truth_table(Excn::Mesh<INT> &global, std::vector<std::vector<T>> &blocks,
214 std::vector<T> &glob_blocks, Excn::Variables &vars, int debug);
215
216 template <typename U>
217 void create_output_truth_table(std::vector<U> &global_sets, Excn::Variables &vars,
218 std::vector<int> &truth_table);
219
220 template <typename T, typename U, typename INT>
221 int read_write_master_values(Excn::Variables &vars, const Excn::Mesh<INT> &global,
222 std::vector<U> &global_sets,
223 std::vector<Excn::Mesh<INT>> &local_mesh,
224 std::vector<std::vector<U>> &local_sets, std::vector<T> &values,
225 size_t p, Excn::ExodusFile &id, int time_step, int time_step_out);
226
227 void get_variable_params(int id, Excn::Variables &vars, const StringIdVector &variable_list);
228
229 void get_put_variable_names(int id, int out, Excn::Variables &vars, Excn::SystemInterface &si,
230 int *combined_status_variable_index = nullptr);
231
232 template <typename INT>
233 void build_reverse_node_map(std::vector<Excn::Mesh<INT>> &local_mesh, Excn::Mesh<INT> *global,
234 size_t part_count, GlobalMap &global_node_map);
235
236 template <typename INT>
237 void build_reverse_node_map(std::vector<Excn::Mesh<INT>> &local_mesh, Excn::Mesh<INT> *global,
238 size_t part_count, std::vector<INT> &global_node_map);
239
240 template <typename INT>
241 void build_reverse_element_map(std::vector<Excn::Mesh<INT>> &local_mesh,
242 std::vector<std::vector<Excn::Block>> &blocks,
243 std::vector<Excn::Block> &glob_blocks, Excn::Mesh<INT> *global,
244 size_t part_count,
245 std::vector<std::pair<INT, size_t>> &global_element_map);
246
247 template <typename INT>
248 void get_nodesets(size_t total_node_count, std::vector<Excn::Mesh<INT>> &local_mesh,
249 std::vector<std::vector<Excn::NodeSet<INT>>> &nodesets,
250 std::vector<Excn::NodeSet<INT>> &glob_sets);
251
252 template <typename INT>
253 void get_element_blocks(const std::vector<Excn::Mesh<INT>> &local_mesh,
254 const Excn::Mesh<INT> &global,
255 std::vector<std::vector<Excn::Block>> &blocks,
256 std::vector<Excn::Block> &glob_blocks);
257 template <typename T, typename INT>
258 void put_element_blocks(std::vector<Excn::Mesh<INT>> &local_mesh,
259 std::vector<std::vector<Excn::Block>> &blocks,
260 std::vector<Excn::Block> &glob_blocks, T single_or_double);
261
262 template <typename INT> void put_nodesets(std::vector<Excn::NodeSet<INT>> &glob_sets);
263
264 template <typename INT>
265 void get_sideset_metadata(std::vector<Excn::Mesh<INT>> &local_mesh,
266 std::vector<std::vector<Excn::SideSet<INT>>> &sets,
267 std::vector<Excn::SideSet<INT>> &glob_ssets);
268 template <typename INT> void get_put_sidesets(std::vector<Excn::SideSet<INT>> &glob_ssets);
269
270 template <typename T, typename INT>
271 void add_status_variable(int id_out, const Excn::Mesh<INT> &global,
272 const std::vector<Excn::Block> &blocks,
273 const std::vector<Excn::Block> &glob_blocks,
274 const std::vector<INT> &local_element_to_global, int step, int variable,
275 T alive, int combined_variable_index);
276
277 template <typename INT>
278 size_t find_max_entity_count(size_t part_count, std::vector<Excn::Mesh<INT>> &local_mesh,
279 const Excn::Mesh<INT> &global,
280 std::vector<std::vector<Excn::Block>> &blocks,
281 std::vector<std::vector<Excn::NodeSet<INT>>> &nodesets,
282 std::vector<std::vector<Excn::SideSet<INT>>> &sidesets);
283
284 bool case_compare(const std::string &s1, const std::string &s2);
285
286 template <typename T>
287 void verify_set_position_mapping(const std::string &type, size_t part_count,
288 const std::vector<T> &global_sets,
289 const std::vector<std::vector<T>> &sets)
290 {
291 bool problem = false;
292 for (size_t p = 0; p < part_count; p++) {
293 for (size_t i = 0; i < sets[p].size(); i++) {
294 if (sets[p][i].id == 0) {
295 continue;
296 }
297 auto glob_pos = sets[p][i].position_;
298 if (global_sets[glob_pos].id != sets[p][i].id ||
299 !case_compare(global_sets[glob_pos].name_, sets[p][i].name_)) {
300 problem = true;
301 fmt::print(stderr,
302 "\nERROR: {0} Mismatch on part {1}:\n"
303 "\tpart {0} at position {2} has id {3} and name {4}\n"
304 "\tglobal {0} at position {5} has id {6} and name {7}\n",
305 type, p + 1, i, sets[p][i].id, sets[p][i].name_, glob_pos,
306 global_sets[glob_pos].id, global_sets[glob_pos].name_);
307 global_sets[glob_pos].dump();
308 sets[p][i].dump();
309 }
310 }
311 }
312
313 if (problem) {
314 throw std::runtime_error(type + " mismatch");
315 }
316 }
317
318 // SEE: http://lemire.me/blog/2017/04/10/removing-duplicates-from-lists-quickly
319 template <typename T> size_t unique(std::vector<T> &out)
320 {
321 if (out.empty()) {
322 return 0;
323 }
324 size_t pos = 1;
325 T oldv = out[0];
326 for (size_t i = 1; i < out.size(); ++i) {
327 T newv = out[i];
328 out[pos] = newv;
329 pos += (newv != oldv);
330 oldv = newv;
331 }
332 return pos;
333 }
334
335 template <typename T> void uniquify(std::vector<T> &vec)
336 {
337 #if USE_STD_SORT
338 std::sort(vec.begin(), vec.end());
339 #else
340 pdqsort(vec.begin(), vec.end());
341 #endif
342 vec.resize(unique(vec));
343 vec.shrink_to_fit();
344 }
345 } // namespace
346
347 unsigned int debug_level = 0;
348 int main(int argc, char *argv[])
349 {
350 try {
351 time_t begin_time = time(nullptr);
352 Excn::SystemInterface::show_version();
353
354 Excn::SystemInterface interFace;
355 bool ok = interFace.parse_options(argc, argv);
356
357 if (!ok) {
358 fmt::print(stderr, "\nERROR: Problems parsing command line arguments.\n\n");
359 exit(EXIT_FAILURE);
360 }
361
362 debug_level = interFace.debug();
363
364 if ((debug_level & 64) != 0U) {
365 ex_opts(EX_VERBOSE | EX_DEBUG);
366 }
367 else {
368 ex_opts(0);
369 }
370
371 int error = 0;
372
373 if (interFace.sort_times()) {
374 sort_file_times(interFace.inputFiles_);
375 }
376
377 if (!Excn::ExodusFile::initialize(interFace)) {
378 fmt::print(stderr, "ERROR: Problem initializing input and/or output files.\n");
379 exit(EXIT_FAILURE);
380 }
381
382 int int_byte_size = Excn::ExodusFile::ints_64_bit() ? 8 : 4;
383
384 if (Excn::ExodusFile::io_word_size() == 4) {
385 if (int_byte_size == 4) {
386 error = conjoin(interFace, static_cast<float>(0.0), 0);
387 }
388 else {
389 error = conjoin(interFace, static_cast<float>(0.0), static_cast<int64_t>(0));
390 }
391 }
392 else {
393 if (int_byte_size == 4) {
394 error = conjoin(interFace, 0.0, 0);
395 }
396 else {
397 error = conjoin(interFace, 0.0, static_cast<int64_t>(0));
398 }
399 }
400
401 Excn::ExodusFile::close_all();
402
403 time_t end_time = time(nullptr);
404 add_to_log(argv[0], end_time - begin_time);
405 return error;
406 }
407 catch (std::exception &e) {
408 fmt::print(stderr, "ERROR: Standard exception: {}\n", e.what());
409 }
410 }
411
412 template <typename T, typename INT>
413 int conjoin(Excn::SystemInterface &interFace, T /* dummy */, INT /* dummy int */)
414 {
415 SMART_ASSERT(sizeof(T) == Excn::ExodusFile::io_word_size());
416
417 const T alive = interFace.alive_value();
418 size_t part_count = Excn::ExodusFile::part_count();
419
420 std::array<char, MAX_LINE_LENGTH + 1> mytitle{};
421
422 Excn::Mesh<INT> global;
423
424 std::vector<Excn::Mesh<INT>> local_mesh(part_count);
425
426 // ******************************************************************
427 // 1. Read global info
428
429 int error = 0;
430
431 if (debug_level & 1) {
432 fmt::print("{}", time_stamp(tsFormat));
433 }
434
435 std::string title0;
436
437 for (size_t p = 0; p < part_count; p++) {
438 ex_init_params info{};
439 error += ex_get_init_ext(Excn::ExodusFile(p), &info);
440
441 local_mesh[p].title = info.title;
442 local_mesh[p].dimensionality = info.num_dim;
443 local_mesh[p].nodeCount = info.num_nodes;
444 local_mesh[p].elementCount = info.num_elem;
445 local_mesh[p].blockCount = info.num_elem_blk;
446 local_mesh[p].nodesetCount = info.num_node_sets;
447 local_mesh[p].sidesetCount = info.num_side_sets;
448
449 if (p == 0) {
450 global.title = mytitle.data();
451 global.dimensionality = local_mesh[p].count(Excn::ObjectType::DIM);
452 global.blockCount = local_mesh[p].count(Excn::ObjectType::EBLK);
453 }
454 else {
455 SMART_ASSERT(global.count(Excn::ObjectType::DIM) ==
456 local_mesh[p].count(Excn::ObjectType::DIM))
457 (global.count(Excn::ObjectType::DIM))(local_mesh[p].count(Excn::ObjectType::DIM))(p);
458 SMART_ASSERT(global.count(Excn::ObjectType::EBLK) ==
459 local_mesh[p].count(Excn::ObjectType::EBLK))
460 (global.count(Excn::ObjectType::EBLK))(local_mesh[p].count(Excn::ObjectType::EBLK))(p);
461 }
462
463 local_mesh[p].localNodeToGlobal.resize(local_mesh[p].count(Excn::ObjectType::NODE));
464 local_mesh[p].localElementToGlobal.resize(local_mesh[p].count(Excn::ObjectType::ELEM));
465
466 } // end for (p=0..part_count)
467
468 if (interFace.omit_nodesets()) {
469 global.nodesetCount = 0;
470 }
471
472 if (interFace.omit_sidesets()) {
473 global.sidesetCount = 0;
474 }
475
476 // Get database times... Save mapping from time step to part providing that time step.
477 std::vector<TimeStepMap<T>> global_times;
478
479 double t_min = FLT_MAX;
480 for (size_t p = part_count; p > 0; p--) {
481
482 Excn::ExodusFile id(p - 1);
483 bool used = false;
484 int nts = ex_inquire_int(id, EX_INQ_TIME);
485 if (nts == 0) {
486 std::string part = "Part " + std::to_string(p) + ": ";
487 part += interFace.inputFiles_[p - 1];
488 fmt::print(stderr,
489 "\nWARNING: '{}'\n\tdoes not contain any time steps so it will not be in the "
490 "conjoined file.\n",
491 part);
492 local_mesh[p - 1].isActive = false;
493 }
494 else {
495 std::vector<T> times(nts);
496 ex_get_all_times(id, Data(times));
497
498 // A database will include all times from step 0 up to the
499 // last time that is less than t_min on the following database.
500 // Note that we are iterating through the databases from last to first...
501
502 // Check delta between the last used timestep on this database
503 // and the first time on the previous (later in time) database
504 // (which is t_min). It must be > user-specified minimum delta.
505 // Set by -interpart_minimum_time_delta {delta} command line option.
506
507 // NOTE that this is not the delta between individual timesteps
508 // within a database, it is the delta between the last timestep
509 // on one database and the first on the next database.
510
511 int i = 0;
512 for (i = nts; i > 0; i--) {
513 if (times[i - 1] < t_min) {
514 if (used || t_min - times[i - 1] >= interFace.interpart_minimum_time_delta()) {
515 used = true;
516 global_times.push_back(TimeStepMap<T>(p - 1, i - 1, times[i - 1]));
517 }
518 }
519 }
520 local_mesh[p - 1].timestepCount = i;
521 t_min = t_min < times[0] ? t_min : times[0];
522
523 if (!used) {
524 std::string part = "Part " + std::to_string(p) + ": ";
525 part += interFace.inputFiles_[p - 1];
526 fmt::print(stderr,
527 "\nWARNING: '{}'\n\tdoes not contain any time steps which will be used in "
528 "conjoined file.\n"
529 "\tCurrent minimum time = {}, timestep range on this part is {} to {}\n",
530 part, t_min, times[0], times[nts - 1]);
531 local_mesh[p - 1].isActive = false;
532 }
533 }
534 }
535 global.timestepCount = global_times.size();
536 std::reverse(global_times.begin(), global_times.end());
537
538 // Need these throughout run, so declare outside of this block...
539 // TODO(gdsjaar): Add these to the "Mesh" class.
540 std::vector<Excn::Block> glob_blocks(global.count(Excn::ObjectType::EBLK));
541 std::vector<std::vector<Excn::Block>> blocks(part_count);
542
543 std::vector<Excn::SideSet<INT>> glob_ssets;
544 std::vector<std::vector<Excn::SideSet<INT>>> sidesets(part_count);
545
546 std::vector<Excn::NodeSet<INT>> glob_nsets;
547 std::vector<std::vector<Excn::NodeSet<INT>>> nodesets(part_count);
548 {
549 // Now, build the reverse global node map which permits access of the
550 // local id given the global id.
551 std::vector<INT> global_node_map;
552 if (interFace.ignore_coordinates()) {
553 build_reverse_node_map(local_mesh, &global, part_count, global_node_map);
554 }
555 else {
556 GlobalMap global_node_coord_map;
557 build_reverse_node_map(local_mesh, &global, part_count, global_node_coord_map);
558 global_node_map.reserve(global_node_coord_map.size());
559 for (const auto &ni : global_node_coord_map) {
560 global_node_map.push_back(ni.id);
561 }
562 }
563
564 if (debug_level & 1) {
565 fmt::print("{}", time_stamp(tsFormat));
566 }
567
568 // ****************************************************************************
569 // 5. Get Block information including element attributes
570 // must check for zero length blocks
571 get_element_blocks(local_mesh, global, blocks, glob_blocks);
572
573 std::vector<std::pair<INT, size_t>> global_element_map(global.count(Excn::ObjectType::ELEM));
574 build_reverse_element_map(local_mesh, blocks, glob_blocks, &global, part_count,
575 global_element_map);
576
577 //
578 // NOTE: Node set/side set information can be different for each part
579 /************************************************************************/
580 // 7. Get Side sets
581 if (!interFace.omit_sidesets()) {
582 if (debug_level & 1) {
583 fmt::print("{}", time_stamp(tsFormat));
584 }
585 get_sideset_metadata(local_mesh, sidesets, glob_ssets);
586 if (global.count(Excn::ObjectType::SSET) != glob_ssets.size()) {
587 global.sidesetCount = glob_ssets.size();
588 }
589 }
590
591 /************************************************************************/
592 // 6. Get Node sets
593 if (!interFace.omit_nodesets()) {
594 if (debug_level & 1) {
595 fmt::print("{}", time_stamp(tsFormat));
596 }
597 get_nodesets(global.count(Excn::ObjectType::NODE), local_mesh, nodesets, glob_nsets);
598 if (global.count(Excn::ObjectType::NSET) != glob_nsets.size()) {
599 global.nodesetCount = glob_nsets.size();
600 }
601 }
602
603 /************************************************************************/
604 // Start writing the output file...
605
606 if (debug_level & 1) {
607 fmt::print("{}", time_stamp(tsFormat));
608 }
609 Excn::CommunicationMetaData comm_data;
610
611 // Create the output file...
612 Excn::ExodusFile::create_output(interFace);
613
614 put_mesh_summary(global);
615
616 get_put_qa(Excn::ExodusFile(0), Excn::ExodusFile::output(), global_times, interFace);
617
618 Excn::Internals exodus(Excn::ExodusFile::output(), Excn::ExodusFile::max_name_length());
619
620 exodus.write_meta_data(global, glob_blocks, glob_nsets, glob_ssets, comm_data);
621
622 // Output bulk mesh data....
623 put_nodesets(glob_nsets);
624
625 // c.2. Write Global Node Number Map
626 if (debug_level & 1) {
627 fmt::print("{}", time_stamp(tsFormat));
628 }
629
630 error = ex_put_id_map(Excn::ExodusFile::output(), EX_NODE_MAP, Data(global_node_map));
631
632 if (debug_level & 1) {
633 fmt::print("{}", time_stamp(tsFormat));
634 }
635 if (!global_element_map.empty()) {
636 std::vector<INT> global_map(global.count(Excn::ObjectType::ELEM));
637 for (size_t i = 0; i < global.count(Excn::ObjectType::ELEM); i++) {
638 global_map[i] = global_element_map[i].first;
639 }
640 ex_put_id_map(Excn::ExodusFile::output(), EX_ELEM_MAP, Data(global_map));
641 }
642
643 T dummy = 0;
644 put_element_blocks(local_mesh, blocks, glob_blocks, dummy);
645 get_put_sidesets(glob_ssets);
646 }
647 // ************************************************************************
648 // 2. Get Coordinate Info.
649 {
650 if (debug_level & 1) {
651 fmt::print("{}", time_stamp(tsFormat));
652 }
653
654 error += get_put_coordinates(global, part_count, local_mesh, (T)0);
655
656 if (debug_level & 1) {
657 fmt::print("{}", time_stamp(tsFormat));
658 }
659 fmt::print("Wrote coordinate information...\n");
660 }
661 // ####################TRANSIENT DATA SECTION###########################
662 // ***********************************************************************
663 // 9. Get Variable Information and names
664
665 if (debug_level & 1) {
666 fmt::print("{}", time_stamp(tsFormat));
667 }
668
669 // I. read number of variables for each type. Note that exodusII does not
670 // provide for history variables
671 // NOTE: it is assumed that every part has the same global, nodal,
672 // and element lists
673
674 bool add_n_status = interFace.nodal_status_variable() != "NONE";
675 bool add_e_status = interFace.element_status_variable() != "NONE";
676 Excn::Variables global_vars(Excn::ObjectType::GLOBAL);
677 Excn::Variables nodal_vars(Excn::ObjectType::NODE, add_n_status);
678 Excn::Variables element_vars(Excn::ObjectType::EBLK, add_e_status);
679 Excn::Variables nodeset_vars(Excn::ObjectType::NSET);
680 Excn::Variables sideset_vars(Excn::ObjectType::SSET);
681
682 {
683 Excn::ExodusFile id(0);
684
685 get_variable_params(id, global_vars, interFace.global_var_names());
686 get_variable_params(id, nodal_vars, interFace.node_var_names());
687 get_variable_params(id, element_vars, interFace.elem_var_names());
688 get_variable_params(id, nodeset_vars, interFace.nset_var_names());
689 get_variable_params(id, sideset_vars, interFace.sset_var_names());
690
691 get_truth_table(global, blocks, glob_blocks, element_vars, 4);
692 filter_truth_table(id, global, glob_blocks, element_vars, interFace.elem_var_names());
693
694 get_truth_table(global, nodesets, glob_nsets, nodeset_vars, 32);
695 filter_truth_table(id, global, glob_nsets, nodeset_vars, interFace.nset_var_names());
696
697 get_truth_table(global, sidesets, glob_ssets, sideset_vars, 16);
698 filter_truth_table(id, global, glob_ssets, sideset_vars, interFace.sset_var_names());
699 }
700
701 // Check that the variable counts are the same on the subsequent files...
702 // Error out if there is a difference...
703 bool found_error = false;
704 for (size_t p = 1; p < part_count; p++) {
705 found_error |= check_variable_params(p, global_vars);
706 found_error |= check_variable_params(p, nodal_vars);
707 found_error |= check_variable_params(p, element_vars);
708 found_error |= check_variable_params(p, nodeset_vars);
709 found_error |= check_variable_params(p, sideset_vars);
710 }
711 if (found_error) {
712 return 1;
713 }
714
715 // There is a slightly tricky situation here. The truthTable block order
716 // is based on the ordering of the blocks on the input databases.
717 // These blocks may have been reordered on output to make the 'offset'
718 // variables line up correctly when the element ids are mapped back to
719 // the "overall global" order. There is not much problem since most
720 // calls outputting block-related items pass the id and don't rely
721 // on ordering. However, the truth table is one of the exceptions
722 // and we need to reorder the truth table to match the output block
723 // order. After this call, we can use the original ordering, so just
724 // need a temporary vector here...
725 if (global_vars.count(Excn::InOut::OUT_) + nodal_vars.count(Excn::InOut::OUT_) +
726 element_vars.count(Excn::InOut::OUT_) + nodeset_vars.count(Excn::InOut::OUT_) +
727 sideset_vars.count(Excn::InOut::OUT_) >
728 0) {
729
730 std::vector<int> elem_truth_table;
731 std::vector<int> nset_truth_table;
732 std::vector<int> sset_truth_table;
733 create_output_truth_table(glob_blocks, element_vars, elem_truth_table);
734 create_output_truth_table(glob_nsets, nodeset_vars, nset_truth_table);
735 create_output_truth_table(glob_ssets, sideset_vars, sset_truth_table);
736
737 ex_put_all_var_param(Excn::ExodusFile::output(), global_vars.count(Excn::InOut::OUT_),
738 nodal_vars.count(Excn::InOut::OUT_), element_vars.count(Excn::InOut::OUT_),
739 Data(elem_truth_table), nodeset_vars.count(Excn::InOut::OUT_),
740 Data(nset_truth_table), sideset_vars.count(Excn::InOut::OUT_),
741 Data(sset_truth_table));
742 }
743
744 // II. read/write the variable names
745 int combined_status_variable_index = 0;
746 {
747 Excn::ExodusFile id(0);
748 get_put_variable_names(id, Excn::ExodusFile::output(), global_vars, interFace);
749 get_put_variable_names(id, Excn::ExodusFile::output(), nodal_vars, interFace);
750 get_put_variable_names(id, Excn::ExodusFile::output(), element_vars, interFace,
751 &combined_status_variable_index);
752 get_put_variable_names(id, Excn::ExodusFile::output(), nodeset_vars, interFace);
753 get_put_variable_names(id, Excn::ExodusFile::output(), sideset_vars, interFace);
754 }
755 ex_update(Excn::ExodusFile::output());
756
757 /**********************************************************************/
758 // 10. Get Transient Data
759 // This routine reads in a time dump from an EXODUSII file
760
761 size_t num_time_steps = global.count(Excn::ObjectType::TIME);
762
763 if (debug_level & 1) {
764 fmt::print("{}", time_stamp(tsFormat));
765 }
766
767 std::vector<T> global_values(global_vars.count(Excn::InOut::IN_));
768 std::vector<T> output_global_values(global_vars.count(Excn::InOut::OUT_));
769
770 // Determine maximum number of entities on any part
771 auto max_ent = find_max_entity_count(part_count, local_mesh, global, blocks, nodesets, sidesets);
772 std::vector<T> values(max_ent);
773
774 // Stage II. Extracting transient variable data.
775 // loop over time steps
776
777 // Determine if user wants a subset of timesteps transferred to the output file.
778 // Time steps for output file
779 double start_time = seacas_timer();
780
781 // Used for output formatting at end of loop.
782 int percent_width = 0;
783 int field_width = 3;
784 if (num_time_steps > 100) {
785 percent_width = 1;
786 field_width = 5;
787 }
788
789 int element_width = get_width(global.count(Excn::ObjectType::ELEM));
790 int step_width = get_width(num_time_steps);
791 step_width += step_width / 3; // For commas -- 1,234,456
792
793 int part_width = get_width(part_count + 1);
794 int loc_step_width = 0;
795 for (auto &global_time : global_times) {
796 if (loc_step_width < global_time.localStepNumber + 1) {
797 loc_step_width = global_time.localStepNumber + 1;
798 }
799 }
800 loc_step_width = get_width(loc_step_width);
801 loc_step_width += loc_step_width / 3;
802
803 size_t time_step_out = 0;
804 for (size_t time_step = 0; time_step < num_time_steps; time_step++) {
805 time_step_out++;
806
807 T time_val = 0;
808 size_t p = global_times[time_step].partNumber;
809 Excn::ExodusFile id(p);
810
811 // read in and write out the time step information
812 error += ex_get_time(id, global_times[time_step].localStepNumber + 1, &time_val);
813 SMART_ASSERT(time_val == global_times[time_step].timeValue)
814 (time_step)(time_val)(global_times[time_step].timeValue);
815 error += ex_put_time(Excn::ExodusFile::output(), time_step_out, &time_val);
816
817 if (global_vars.count(Excn::InOut::OUT_) > 0) {
818 if (debug_level & 1) {
819 fmt::print("{}Global Variables...\n", time_stamp(tsFormat));
820 }
821 error += ex_get_var(id, global_times[time_step].localStepNumber + 1, EX_GLOBAL, 0, 0,
822 global_vars.count(), (void *)Data(global_values));
823 // Map ...
824 for (int ig = 0; ig < global_vars.count(Excn::InOut::IN_); ig++) {
825 if (global_vars.index_[ig] > 0) {
826 SMART_ASSERT(ig < (int)global_values.size());
827 output_global_values[global_vars.index_[ig] - 1] = global_values[ig];
828 }
829 }
830 error += ex_put_var(Excn::ExodusFile::output(), time_step_out, EX_GLOBAL, 1, 0,
831 global_vars.count(Excn::InOut::OUT_), Data(output_global_values));
832 }
833
834 // ========================================================================
835 // Nodal Values...
836 if (debug_level & 1) {
837 fmt::print("{}Nodal Variables...\n", time_stamp(tsFormat));
838 }
839
840 if (nodal_vars.count(Excn::InOut::OUT_) > 0) {
841 size_t node_count = local_mesh[p].count(Excn::ObjectType::NODE);
842 std::vector<T> master_nodal_values(global.count(Excn::ObjectType::NODE));
843
844 int offset = nodal_vars.addStatus ? 1 : 0;
845 for (int i = 0; i < nodal_vars.count(Excn::InOut::OUT_) - offset;
846 i++) { // Last output variable may be status
847 for (int j = 0; j < nodal_vars.count(Excn::InOut::IN_); j++) {
848 if (nodal_vars.index_[j] - 1 == i) {
849 std::fill(master_nodal_values.begin(), master_nodal_values.end(), T(0.0));
850
851 error += ex_get_var(id, global_times[time_step].localStepNumber + 1, EX_NODAL, j + 1, 0,
852 node_count, Data(values));
853
854 // copy values to master nodal value information
855 for (size_t jj = 0; jj < node_count; jj++) {
856 // Map local nodal value to global location...
857 size_t nodal_value = local_mesh[p].localNodeToGlobal[jj];
858 master_nodal_values[nodal_value] = values[jj];
859 }
860 error += ex_put_var(Excn::ExodusFile::output(), time_step_out, EX_NODAL, i + 1, 0,
861 global.count(Excn::ObjectType::NODE), Data(master_nodal_values));
862 break;
863 }
864 }
865
866 // Fill "node_status" variable -- 'alive' for alive; 1-alive for dead.
867 // It is the last output variable...
868 if (nodal_vars.addStatus) {
869 SMART_ASSERT(alive == 0.0 || alive == 1.0)(alive);
870 std::fill(master_nodal_values.begin(), master_nodal_values.end(), T((1.0 - alive)));
871 for (size_t j = 0; j < node_count; j++) {
872 // Map local nodal value to global location...
873 size_t nodal_value = local_mesh[p].localNodeToGlobal[j];
874 master_nodal_values[nodal_value] = alive;
875 }
876
877 error += ex_put_var(Excn::ExodusFile::output(), time_step_out, EX_NODAL,
878 nodal_vars.count(Excn::InOut::OUT_), 0,
879 global.count(Excn::ObjectType::NODE), Data(master_nodal_values));
880 }
881 }
882 }
883
884 // ========================================================================
885 // Extracting element transient variable data
886 if (debug_level & 1) {
887 fmt::print("{}Element Variables...\n", time_stamp(tsFormat));
888 }
889 if (element_vars.count(Excn::InOut::IN_) > 0) {
890 read_write_master_values(element_vars, global, glob_blocks, local_mesh, blocks, values, p, id,
891 global_times[time_step].localStepNumber, time_step_out);
892 }
893
894 // Add element status variable...
895 // Use the output time step for writing data
896 if (interFace.element_status_variable() != "NONE") {
897 add_status_variable(Excn::ExodusFile::output(), global, blocks[p], glob_blocks,
898 local_mesh[p].localElementToGlobal, time_step_out,
899 element_vars.index_[element_vars.count(Excn::InOut::IN_)], alive,
900 combined_status_variable_index);
901 }
902
903 // ========================================================================
904 // Extracting sideset transient variable data
905 if (debug_level & 1) {
906 fmt::print("{}Sideset Variables...\n", time_stamp(tsFormat));
907 }
908 if (sideset_vars.count(Excn::InOut::IN_) > 0) {
909 read_write_master_values(sideset_vars, global, glob_ssets, local_mesh, sidesets, values, p,
910 id, global_times[time_step].localStepNumber, time_step_out);
911 }
912
913 // ========================================================================
914 // Extracting nodeset transient variable data
915 if (debug_level & 1) {
916 fmt::print("{}Nodeset Variables...\n", time_stamp(tsFormat));
917 }
918 if (nodeset_vars.count(Excn::InOut::IN_) > 0) {
919 read_write_master_values(nodeset_vars, global, glob_nsets, local_mesh, nodesets, values, p,
920 id, global_times[time_step].localStepNumber, time_step_out);
921 }
922
923 // ========================================================================
924 ex_update(Excn::ExodusFile::output());
925
926 if (debug_level & 1) {
927 fmt::print("{}", time_stamp(tsFormat));
928 }
929
930 fmt::print("Step {:{}}/{}, time {:.4e} (Part {:{}}/{}, step {:{}}) Active Elem: {:{}}",
931 fmt::group_digits(time_step + 1), step_width, fmt::group_digits(num_time_steps),
932 time_val, p + 1, part_width, part_count,
933 fmt::group_digits(global_times[time_step].localStepNumber + 1), loc_step_width,
934 fmt::group_digits(local_mesh[p].count(Excn::ObjectType::ELEM)), element_width);
935
936 double cur_time = seacas_timer();
937 double elapsed = cur_time - start_time;
938 double time_per_step = elapsed / time_step_out;
939 double percentage_done = (time_step_out * 100.0) / global.count(Excn::ObjectType::TIME);
940 double estimated_remaining =
941 time_per_step * (global.count(Excn::ObjectType::TIME) - time_step_out);
942
943 fmt::print(" [{:{}.{}f}%, Elapsed={}, ETA={}]\n", percentage_done, field_width, percent_width,
944 format_time(elapsed), format_time(estimated_remaining));
945 if (debug_level & 1) {
946 fmt::print("\n");
947 }
948 }
949
950 /*************************************************************************/
951 // EXIT program
952 if (debug_level & 1) {
953 fmt::print("{}", time_stamp(tsFormat));
954 }
955 fmt::print("******* END *******\n");
956 return error;
957 }
958
959 namespace {
960 template <typename T>
961 void get_put_qa(int id, int id_out, const std::vector<TimeStepMap<T>> & /*global_times*/,
962 Excn::SystemInterface & /*interFace*/)
963 {
964 // NOTE: Assuming info and QA records for all parts
965 int error = 0;
966
967 int info_string_len = MAX_LINE_LENGTH;
968
969 size_t num_info_records = ex_inquire_int(id, EX_INQ_INFO);
970 // size_t extra_info = global_times.size() + 2 + 1;
971 size_t extra_info = 2 + 1;
972
973 char **info_records = get_name_array(num_info_records + extra_info, info_string_len);
974
975 if (num_info_records > 0) {
976 error += ex_get_info(id, info_records);
977 }
978
979 // Add an info record for CONJOIN
980 add_info_record(info_records[num_info_records], MAX_LINE_LENGTH);
981
982 #if 0
983 // Add time/part mapping...
984 for (size_t i=0; i < global_times.size(); i++) {
985 std::ostringstream os;
986 fmt::print(os, "Step {:2}, time {:.4e} (Part {}, step {}) File: {}",
987 i+1, global_times[i].timeValue,
988 global_times[i].partNumber+1,
989 global_times[i].localStepNumber+1,
990 interFace.inputFiles_[global_times[i].partNumber]);
991
992 copy_string(info_records[num_info_records+1+i], os.str(), MAX_LINE_LENGTH + 1);
993 }
994 #endif
995
996 error += ex_put_info(id_out, num_info_records + extra_info, info_records);
997
998 free_name_array(info_records, num_info_records + extra_info);
999
1000 // II. Get and store QA records, if they exist
1001 struct qa_element
1002 {
1003 char *qa_record[1][4];
1004 };
1005
1006 int num_qa_records = ex_inquire_int(id, EX_INQ_QA);
1007 std::vector<qa_element> qaRecord(num_qa_records + 1);
1008 for (int i = 0; i < num_qa_records + 1; i++) {
1009 for (int j = 0; j < 4; j++) {
1010 qaRecord[i].qa_record[0][j] = new char[MAX_STR_LENGTH + 1];
1011 qaRecord[i].qa_record[0][j][0] = '\0';
1012 }
1013 }
1014 if (num_qa_records) {
1015 error += ex_get_qa(id, qaRecord[0].qa_record);
1016 }
1017
1018 copy_string(qaRecord[num_qa_records].qa_record[0][0], qainfo[0], MAX_STR_LENGTH + 1); // Code
1019 copy_string(qaRecord[num_qa_records].qa_record[0][1], qainfo[2], MAX_STR_LENGTH + 1); // Version
1020
1021 std::time_t date_time = std::time(nullptr);
1022
1023 auto date = fmt::format("{:%Y/%m/%d}", fmt::localtime(date_time));
1024 copy_string(qaRecord[num_qa_records].qa_record[0][2], date.c_str(), MAX_STR_LENGTH + 1);
1025
1026 auto time = fmt::format("{:%T}", fmt::localtime(date_time));
1027 copy_string(qaRecord[num_qa_records].qa_record[0][3], time.c_str(), MAX_STR_LENGTH + 1);
1028
1029 error += ex_put_qa(id_out, num_qa_records + 1, qaRecord[0].qa_record);
1030
1031 for (int i = 0; i < num_qa_records + 1; i++) {
1032 for (int j = 0; j < 4; j++) {
1033 delete[] qaRecord[i].qa_record[0][j];
1034 }
1035 }
1036 }
1037
1038 template <typename T, typename INT>
1039 int get_put_coordinates(Excn::Mesh<INT> &global, size_t part_count,
1040 std::vector<Excn::Mesh<INT>> &local_mesh, T /* dummy */)
1041 {
1042 SMART_ASSERT(sizeof(T) == Excn::ExodusFile::io_word_size());
1043 std::vector<T> x(global.count(Excn::ObjectType::NODE));
1044 std::vector<T> y(global.count(Excn::ObjectType::NODE));
1045 std::vector<T> z(global.count(Excn::ObjectType::NODE));
1046
1047 if (debug_level & 8) {
1048 const T FILL_VALUE = FLT_MAX;
1049 std::fill(x.begin(), x.end(), FILL_VALUE);
1050 std::fill(y.begin(), y.end(), FILL_VALUE);
1051 std::fill(z.begin(), z.end(), FILL_VALUE);
1052 }
1053
1054 int error = 0;
1055 for (size_t p = 0; p < part_count; p++) {
1056 error += get_coordinates(Excn::ExodusFile(p), global.count(Excn::ObjectType::DIM),
1057 local_mesh[p].count(Excn::ObjectType::NODE),
1058 local_mesh[p].localNodeToGlobal, p, x, y, z);
1059 } // end for p=0..part_count
1060
1061 // Get Coordinate Names
1062 // NOTE: Assuming coordinate names should be
1063 // the same for all files/parts therefore, only one
1064 // file/part needs to be loaded
1065 error += get_put_coordinate_names(Excn::ExodusFile(0), Excn::ExodusFile::output(),
1066 global.count(Excn::ObjectType::DIM));
1067 // Write out coordinate information
1068 error += ex_put_coord(Excn::ExodusFile::output(), Data(x), Data(y), Data(z));
1069 return error;
1070 }
1071
1072 int get_put_coordinate_names(int in, int out, int dimensionality)
1073 {
1074 int error = 0;
1075 char **coordinate_names = get_name_array(dimensionality, Excn::ExodusFile::max_name_length());
1076
1077 error += ex_get_coord_names(in, coordinate_names);
1078 error += ex_put_coord_names(out, coordinate_names);
1079 fmt::print("Wrote coordinate names...\n");
1080
1081 free_name_array(coordinate_names, dimensionality);
1082 return error;
1083 }
1084
1085 template <typename T, typename INT>
1086 int get_coordinates(int id, int dimensionality, size_t num_nodes,
1087 std::vector<INT> local_node_to_global, size_t part, std::vector<T> &x,
1088 std::vector<T> &y, std::vector<T> &z)
1089 {
1090 SMART_ASSERT(sizeof(T) == Excn::ExodusFile::io_word_size());
1091 SMART_ASSERT(local_node_to_global.size() == num_nodes);
1092 int error = 0;
1093 std::vector<T> local_x(num_nodes);
1094 std::vector<T> local_y(num_nodes);
1095 std::vector<T> local_z(num_nodes);
1096
1097 error += ex_get_coord(id, Data(local_x), Data(local_y), Data(local_z));
1098
1099 // Check for 2D or 3D coordinates
1100 if (dimensionality == 3) {
1101 if (debug_level & 8) {
1102 for (size_t i = 0; i < num_nodes; i++) {
1103 INT node = local_node_to_global[i];
1104 const T FILL_VALUE = FLT_MAX;
1105 if (x[node] != FILL_VALUE && y[node] != FILL_VALUE && z[node] != FILL_VALUE) {
1106 if (!approx_equal(x[node], local_x[i]) || !approx_equal(y[node], local_y[i]) ||
1107 !approx_equal(z[node], local_z[i])) {
1108 fmt::print(
1109 stderr,
1110 "\nWARNING: Node {} has different coordinates in at least two parts.\n"
1111 " this may indicate that this id has been reused in the current part.\n"
1112 " cur value = {:14.6e} {:14.6e} {:14.6e}\n"
1113 " new value = {:14.6e} {:14.6e} {:14.6e} from part {}\n",
1114 node + 1, x[node], y[node], z[node], local_x[i], local_y[i], local_z[i], part);
1115 }
1116 }
1117 }
1118 }
1119
1120 for (size_t i = 0; i < num_nodes; i++) {
1121 // The following WILL overwrite x[node],y[node],z[node] if node is the
1122 // same for different parts
1123 INT node = local_node_to_global[i];
1124 x[node] = local_x[i];
1125 y[node] = local_y[i];
1126 z[node] = local_z[i];
1127 }
1128 }
1129 else {
1130 if (debug_level & 8) {
1131 for (size_t i = 0; i < num_nodes; i++) {
1132 INT node = local_node_to_global[i];
1133 const T FILL_VALUE = FLT_MAX;
1134 if (x[node] != FILL_VALUE && y[node] != FILL_VALUE) {
1135 if (!approx_equal(x[node], local_x[i]) || !approx_equal(y[node], local_y[i])) {
1136 fmt::print(
1137 stderr,
1138 "\nWARNING: Node {} has different coordinates in at least two parts.\n"
1139 " this may indicate that this id has been reused in the current part.\n"
1140 " cur value = {:14.6e} {:14.6e}\n"
1141 " new value = {:14.6e} {:14.6e} from part {}\n",
1142 node + 1, x[node], y[node], local_x[i], local_y[i], part);
1143 }
1144 }
1145 }
1146 }
1147
1148 for (size_t i = 0; i < num_nodes; i++) {
1149 // The following WILL overwrite x[node],y[node] if node is the same for
1150 // different parts
1151 INT node = local_node_to_global[i];
1152 x[node] = local_x[i];
1153 y[node] = local_y[i];
1154 }
1155 }
1156 return error;
1157 }
1158
1159 template <typename INT>
1160 void get_element_blocks(const std::vector<Excn::Mesh<INT>> &local_mesh,
1161 const Excn::Mesh<INT> &global,
1162 std::vector<std::vector<Excn::Block>> &blocks,
1163 std::vector<Excn::Block> &glob_blocks)
1164 {
1165 size_t part_count = local_mesh.size();
1166 for (size_t ip = 0; ip < part_count; ip++) {
1167 blocks[ip].resize(local_mesh[ip].count(Excn::ObjectType::EBLK));
1168 }
1169
1170 std::vector<INT> block_id(global.count(Excn::ObjectType::EBLK));
1171
1172 int error = 0;
1173 for (size_t p = 0; p < part_count; p++) {
1174 Excn::ExodusFile id(p);
1175
1176 error += ex_get_ids(id, EX_ELEM_BLOCK, Data(block_id));
1177
1178 if (p == 0) {
1179 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
1180 glob_blocks[b].id = block_id[b];
1181 }
1182 }
1183
1184 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
1185 ex_block block_param{};
1186 block_param.id = glob_blocks[b].id;
1187 block_param.type = EX_ELEM_BLOCK;
1188
1189 error += ex_get_block_param(id, &block_param);
1190
1191 std::vector<char> name(Excn::ExodusFile::max_name_length() + 1);
1192 ex_get_name(id, EX_ELEM_BLOCK, glob_blocks[b].id, Data(name));
1193
1194 blocks[p][b].id = glob_blocks[b].id;
1195 if (name[0] != '\0') {
1196 blocks[p][b].name_ = Data(name);
1197 if (p == 0) {
1198 glob_blocks[b].name_ = Data(name);
1199 }
1200 }
1201
1202 // Find position of this block id in the local mesh
1203 for (size_t lb = 0; lb < global.count(Excn::ObjectType::EBLK); lb++) {
1204 if (block_id[lb] == glob_blocks[b].id) {
1205 blocks[p][b].position_ = lb;
1206 break;
1207 }
1208 }
1209
1210 if (block_param.num_entry > 0) {
1211 blocks[p][b].elementCount = block_param.num_entry;
1212 blocks[p][b].nodesPerElement = block_param.num_nodes_per_entry;
1213 blocks[p][b].attributeCount = block_param.num_attribute;
1214 blocks[p][b].offset_ = block_param.num_entry;
1215 blocks[p][b].elType = block_param.topology;
1216
1217 // NOTE: This is not correct, but fixed below.
1218 glob_blocks[b].elementCount += block_param.num_entry;
1219
1220 if (glob_blocks[b].nodesPerElement == 0) {
1221 glob_blocks[b].nodesPerElement = block_param.num_nodes_per_entry;
1222 }
1223
1224 if (glob_blocks[b].attributeCount == 0) {
1225 glob_blocks[b].attributeCount = block_param.num_attribute;
1226 }
1227
1228 glob_blocks[b].position_ = b;
1229 glob_blocks[b].elType = block_param.topology;
1230 }
1231
1232 if (block_param.num_attribute > 0 && glob_blocks[b].attributeNames.empty()) {
1233 // Get attribute names. Assume the same on all parts
1234 // on which the block exists.
1235 char **names =
1236 get_name_array(block_param.num_attribute, Excn::ExodusFile::max_name_length());
1237
1238 ex_get_attr_names(id, EX_ELEM_BLOCK, block_id[b], names);
1239 for (int i = 0; i < block_param.num_attribute; i++) {
1240 glob_blocks[b].attributeNames.emplace_back(names[i]);
1241 }
1242 free_name_array(names, block_param.num_attribute);
1243 }
1244 }
1245 } // end for p=0..part_count
1246
1247 // Convert block_offset from elements/block/part to true offset
1248 for (size_t p = 0; p < part_count; p++) {
1249 if (debug_level & 4) {
1250 fmt::print("\nElement block info for part {}...\n", p);
1251 }
1252 // Number of elements per block in local block order...
1253 std::vector<size_t> local_order_entity_count(global.count(Excn::ObjectType::EBLK));
1254 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
1255 int local_order = blocks[p][b].position_;
1256 local_order_entity_count[local_order] = blocks[p][b].entity_count();
1257 }
1258 size_t sum = 0;
1259 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
1260 size_t save = local_order_entity_count[b];
1261 local_order_entity_count[b] = sum;
1262 sum += save;
1263 }
1264 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
1265 int local_order = blocks[p][b].position_;
1266 blocks[p][b].offset_ = local_order_entity_count[local_order];
1267
1268 if (debug_level & 4) {
1269 fmt::print("\tBlock {}, Id = {}, Name = '{}', Elements = {:12}, Nodes/element = {}, "
1270 "Attributes = {}, Position = {}, Offset = {}\n",
1271 b, glob_blocks[b].id, blocks[p][b].name_,
1272 fmt::group_digits(blocks[p][b].entity_count()), blocks[p][b].nodesPerElement,
1273 blocks[p][b].attributeCount, blocks[p][b].position_, blocks[p][b].offset_);
1274 }
1275 }
1276 }
1277 }
1278
1279 template <typename T, typename INT>
1280 void put_element_blocks(std::vector<Excn::Mesh<INT>> &local_mesh,
1281 std::vector<std::vector<Excn::Block>> &blocks,
1282 std::vector<Excn::Block> &glob_blocks, T /* dummy */)
1283 {
1284 SMART_ASSERT(sizeof(T) == Excn::ExodusFile::io_word_size());
1285 int global_num_blocks = glob_blocks.size();
1286
1287 if (debug_level & 1) {
1288 fmt::print("{}", time_stamp(tsFormat));
1289 }
1290 fmt::print("\nReading and Writing element connectivity & attributes\n");
1291
1292 for (int b = 0; b < global_num_blocks; b++) {
1293
1294 if (debug_level & 4) {
1295 fmt::print("\nOutput element block info for...\n"
1296 "Block {}, Id = {}, Name = '{}', Elements = {:12}, Nodes/element = {}, "
1297 "Attributes = {}\n",
1298 b, glob_blocks[b].id, glob_blocks[b].name_,
1299 fmt::group_digits(glob_blocks[b].entity_count()), glob_blocks[b].nodesPerElement,
1300 glob_blocks[b].attributeCount);
1301 }
1302
1303 if (debug_level & 4) {
1304 fmt::print("B{}:\t", b);
1305 }
1306
1307 size_t max_nodes = glob_blocks[b].entity_count();
1308 max_nodes *= glob_blocks[b].nodesPerElement;
1309 std::vector<INT> block_linkage(max_nodes);
1310
1311 // Initialize attributes list, if it exists
1312 std::vector<T> attributes(glob_blocks[b].attributeCount * glob_blocks[b].entity_count());
1313
1314 int error = 0;
1315 size_t part_count = local_mesh.size();
1316 for (size_t p = 0; p < part_count; p++) {
1317 Excn::ExodusFile id(p);
1318
1319 size_t global_pos;
1320 size_t global_block_pos;
1321
1322 if (blocks[p][b].entity_count() > 0) { // non-zero length block
1323
1324 if (debug_level & 4) {
1325 fmt::print("#");
1326 }
1327 size_t maximum_nodes = blocks[p][b].entity_count();
1328 maximum_nodes *= blocks[p][b].nodesPerElement;
1329 std::vector<INT> local_linkage(maximum_nodes);
1330
1331 INT bid = blocks[p][b].id;
1332 error = ex_get_conn(id, EX_ELEM_BLOCK, bid, Data(local_linkage), nullptr, nullptr);
1333 if (error < 0) {
1334 fmt::print(stderr,
1335 "ERROR: Cannot get element block connectivity for block {} on part {}.\n",
1336 bid, p);
1337 }
1338 size_t pos = 0;
1339 size_t goffset = glob_blocks[b].offset_;
1340 size_t element_count = blocks[p][b].entity_count();
1341 size_t boffset = blocks[p][b].offset_;
1342 size_t npe = blocks[p][b].nodesPerElement;
1343 INT *part_loc_elem_to_global = Data(local_mesh[p].localElementToGlobal);
1344 INT *part_loc_node_to_global = Data(local_mesh[p].localNodeToGlobal);
1345
1346 for (size_t e = 0; e < element_count; e++) {
1347 global_block_pos = part_loc_elem_to_global[(e + boffset)] - goffset;
1348 global_pos = global_block_pos * npe;
1349
1350 for (size_t n = 0; n < npe; n++) {
1351 size_t node = part_loc_node_to_global[local_linkage[pos++] - 1];
1352 if (debug_level & 4) {
1353 SMART_ASSERT(block_linkage[global_pos] == (int)node + 1 ||
1354 block_linkage[global_pos] == 0);
1355 }
1356 block_linkage[global_pos++] = node + 1;
1357 }
1358 }
1359
1360 // Get attributes list, if it exists
1361 if (blocks[p][b].attributeCount > 0) {
1362 SMART_ASSERT(blocks[p][b].attributeCount == glob_blocks[b].attributeCount)
1363 (p)(b)(blocks[p][b].attributeCount)(glob_blocks[b].attributeCount);
1364
1365 size_t max_attr = blocks[p][b].entity_count() * blocks[p][b].attributeCount;
1366 std::vector<T> local_attr(max_attr);
1367
1368 error += ex_get_attr(id, EX_ELEM_BLOCK, blocks[p][b].id, Data(local_attr));
1369
1370 pos = 0;
1371
1372 size_t att_count = blocks[p][b].attributeCount;
1373 for (size_t e = 0; e < element_count; e++) {
1374 // global_pos is global position within this element block...
1375 global_block_pos = local_mesh[p].localElementToGlobal[(e + boffset)] - goffset;
1376 global_pos = global_block_pos * att_count;
1377 for (size_t n = 0; n < att_count; n++) {
1378 attributes[global_pos++] = local_attr[pos++];
1379 }
1380 }
1381 }
1382 } // end if blocks[p][b].entity_count() (non-zero length block)
1383 else if (debug_level & 4) {
1384 fmt::print(".");
1385 }
1386 } // end for p=0..part_count-1
1387
1388 // Verify that connectivity has been set for all elements...
1389 for (size_t i = 0; i < block_linkage.size(); i++) {
1390 SMART_ASSERT(block_linkage[i] > 0)(block_linkage[i])(i)(b)(glob_blocks[b].id);
1391 }
1392
1393 // Write out block info
1394 int id_out = Excn::ExodusFile::output(); // output file identifier
1395
1396 if (!block_linkage.empty()) {
1397 error += ex_put_conn(id_out, EX_ELEM_BLOCK, glob_blocks[b].id, Data(block_linkage), nullptr,
1398 nullptr);
1399 }
1400
1401 // Write out attributes list if it exists
1402 if (glob_blocks[b].attributeCount > 0) {
1403 error += ex_put_attr(id_out, EX_ELEM_BLOCK, glob_blocks[b].id, Data(attributes));
1404 } // end for b=0..global_num_blocks-1
1405 if (debug_level & 4) {
1406 fmt::print("\n");
1407 }
1408 }
1409 fmt::print("\n");
1410 }
1411
1412 template <typename INT>
1413 void build_reverse_element_map(std::vector<Excn::Mesh<INT>> &local_mesh,
1414 std::vector<std::vector<Excn::Block>> &blocks,
1415 std::vector<Excn::Block> &glob_blocks, Excn::Mesh<INT> *global,
1416 size_t part_count,
1417 std::vector<std::pair<INT, size_t>> &global_element_map)
1418 {
1419 int error = 0;
1420 // Create the map that maps from a local part element to the
1421 // global map. This combines the mapping local part element to
1422 // 'global id' and then 'global id' to global position. The
1423 // mapping is now a direct lookup instead of a lookup followed by
1424 // a reverse map.
1425 //
1426
1427 // We iterate through each element block a part at a time to
1428 // determine which elements are in each block. Note that an
1429 // element will possibly exist in multiple parts, so we need to do
1430 // the sort/uniquify/shrink on each element block. The operations
1431 // for each element block will be:
1432 // 1. get elements for the block for each part into a vector.
1433 // 2. sort/uniquify/shrink that vector and copy into the
1434 // 'global_element_map' in the positions following the previous block.
1435 //
1436 // This is similar to what was done above in a global sense,
1437 // except it is done an element block at a time. The only need for
1438 // doing them both is that it lets us detect whether an element id
1439 // was reused and appears in both blocks. Currently, just error
1440 // out if that happens... In the future, change the id to an
1441 // unused value and continue....
1442
1443 size_t tot_size = 0;
1444 std::vector<std::vector<std::pair<INT, size_t>>> global_element_numbers(part_count);
1445 for (size_t p = 0; p < part_count; p++) {
1446 Excn::ExodusFile id(p);
1447 global_element_numbers[p].resize(local_mesh[p].count(Excn::ObjectType::ELEM));
1448 std::vector<INT> ids(local_mesh[p].count(Excn::ObjectType::ELEM));
1449 error += ex_get_id_map(id, EX_ELEM_MAP, Data(ids));
1450 for (size_t i = 0; i < local_mesh[p].count(Excn::ObjectType::ELEM); i++) {
1451 global_element_numbers[p][i] = std::make_pair(ids[i], size_t(0));
1452 }
1453 tot_size += local_mesh[p].count(Excn::ObjectType::ELEM);
1454 }
1455 global_element_map.resize(tot_size);
1456
1457 size_t goffset = 0;
1458 for (auto &glob_block : glob_blocks) {
1459
1460 size_t block_size = 0;
1461 for (size_t p = 0; p < part_count; p++) {
1462 size_t lb = 0;
1463 for (; lb < blocks[0].size(); lb++) {
1464 if (glob_block.id == blocks[p][lb].id) {
1465 break;
1466 }
1467 }
1468 SMART_ASSERT(glob_block.id == blocks[p][lb].id);
1469 block_size += blocks[p][lb].entity_count();
1470 }
1471 std::vector<std::pair<INT, size_t>> block_element_map(block_size);
1472
1473 size_t poffset = 0;
1474 for (size_t p = 0; p < part_count; p++) {
1475 Excn::ExodusFile id(p);
1476
1477 size_t lb = 0;
1478 for (; lb < blocks[0].size(); lb++) {
1479 if (glob_block.id == blocks[p][lb].id) {
1480 break;
1481 }
1482 }
1483 SMART_ASSERT(glob_block.id == blocks[p][lb].id);
1484
1485 // Get connectivity array for this element block...
1486 size_t element_count = blocks[p][lb].entity_count();
1487 size_t nodes_per_elem = blocks[p][lb].nodesPerElement;
1488 size_t maximum_nodes = element_count * nodes_per_elem;
1489
1490 std::vector<INT> local_linkage(maximum_nodes);
1491
1492 size_t bid = blocks[p][lb].id;
1493 error = ex_get_conn(id, EX_ELEM_BLOCK, bid, Data(local_linkage), nullptr, nullptr);
1494 if (error < 0) {
1495 fmt::print(stderr,
1496 "ERROR: Cannot get element block connectivity for block {} on part {}.\n", bid,
1497 p);
1498 }
1499
1500 // Convert connectivity to global node numbers.
1501 for (size_t i = 0; i < element_count * nodes_per_elem; i++) {
1502 INT local_node = local_linkage[i];
1503 INT global_node = local_mesh[p].localNodeToGlobal[local_node - 1] + 1;
1504 local_linkage[i] = global_node;
1505 }
1506
1507 // Have element global ids for all elements in this block,
1508 // and connectivity. Can now create out "eleminfo" for these elements.
1509 size_t con_offset = 0;
1510 size_t boffset = blocks[p][lb].offset_;
1511 for (size_t i = 0; i < element_count; i++) {
1512 size_t adler_crc = adler(0, &local_linkage[con_offset], nodes_per_elem * sizeof(int));
1513 global_element_numbers[p][boffset + i].second = adler_crc;
1514 block_element_map[poffset + i] = global_element_numbers[p][boffset + i];
1515 con_offset += nodes_per_elem;
1516 }
1517 poffset += element_count;
1518 }
1519
1520 // Sort, uniquify, shrink 'block_element_map' and the result
1521 // then contains the list of elements in this block...
1522 uniquify(block_element_map);
1523
1524 size_t block_total_num_elements = block_element_map.size();
1525 glob_block.elementCount = block_total_num_elements;
1526 glob_block.offset_ = goffset;
1527
1528 // Copy into the global_element_map...
1529 std::copy(block_element_map.begin(), block_element_map.end(), &global_element_map[goffset]);
1530 goffset += block_total_num_elements;
1531 }
1532
1533 global->elementCount = goffset;
1534 global_element_map.resize(goffset);
1535 if (goffset == 0) {
1536 return;
1537 }
1538
1539 size_t max_id = global_element_map[global->elementCount - 1].first;
1540 bool is_contiguous = max_id == global_element_map.size();
1541 // fmt::print("Element id map {}.\n", (is_contiguous ? "is" : "is not"));
1542
1543 // The global_element_map may or may not be globally sorted; however, each
1544 // block is sorted, so if we do the iteration by blocks, we can
1545 // use lower_bound instead of doing global searches...
1546 for (auto &glob_block : glob_blocks) {
1547
1548 auto gm_begin = global_element_map.begin() + glob_block.offset_;
1549 auto gm_end = gm_begin + glob_block.elementCount;
1550 auto cur_pos = gm_begin;
1551 for (size_t p = 0; p < part_count; p++) {
1552 size_t lb = 0;
1553 for (; lb < blocks[0].size(); lb++) {
1554 if (glob_block.id == blocks[p][lb].id) {
1555 break;
1556 }
1557 }
1558 SMART_ASSERT(glob_block.id == blocks[p][lb].id);
1559
1560 size_t element_count = blocks[p][lb].entity_count();
1561 size_t boffset = blocks[p][lb].offset_;
1562 for (size_t i = 0; i < element_count; i++) {
1563 std::pair<INT, size_t> global_element = global_element_numbers[p][boffset + i];
1564
1565 if (cur_pos == gm_end || *cur_pos != global_element) {
1566 auto iter = std::lower_bound(gm_begin, gm_end, global_element);
1567 SMART_ASSERT(iter != gm_end);
1568 cur_pos = iter;
1569 }
1570 size_t element_value = cur_pos - gm_begin;
1571 local_mesh[p].localElementToGlobal[i + boffset] = element_value + glob_block.offset_;
1572 ++cur_pos;
1573 }
1574 }
1575 }
1576
1577 // Update the element ids to give a unique, non-repeating set. If
1578 // contiguous, then there is nothing to do. If not contiguous,
1579 // then need to determine if there are any repeats (id reuse) and
1580 // if so, generate a new id for the repeated uses. Note that
1581 // there is a possibility that elements in two or more element
1582 // blocks will have the same element id, so we generate a vector
1583 // containing a pair<id, position_in_global_element_map>, sort it,
1584 // and then use it to detect duplicates and map them to a new id.
1585
1586 if (!is_contiguous) {
1587 std::vector<std::pair<size_t, size_t>> id_pos(global_element_map.size());
1588 for (size_t i = 0; i < global->elementCount; i++) {
1589 id_pos[i].first = global_element_map[i].first;
1590 id_pos[i].second = i;
1591 }
1592 #if USE_STD_SORT
1593 std::sort(id_pos.begin(), id_pos.end());
1594 #else
1595 pdqsort(id_pos.begin(), id_pos.end());
1596 #endif
1597 max_id = id_pos.back().first;
1598 // Check again for contiguous ids since we now have a sorted list...
1599 is_contiguous = max_id == global_element_map.size();
1600
1601 if (!is_contiguous) {
1602 int repeat_found = 0;
1603 size_t id_last = id_pos[0].first;
1604
1605 for (size_t i = 1; i < global->elementCount; i++) {
1606 if (id_pos[i].first == id_last) {
1607 global_element_map[id_pos[i].second].first = ++max_id;
1608 repeat_found++;
1609 }
1610 else {
1611 id_last = id_pos[i].first;
1612 }
1613 }
1614 if (repeat_found > 0) {
1615 fmt::print(stderr,
1616 "WARNING: {} duplicate element ids were found. Their ids have been "
1617 "renumbered to remove duplicates.\n",
1618 repeat_found);
1619 }
1620 }
1621 }
1622 }
1623
1624 template <typename INT>
1625 void build_reverse_node_map(std::vector<Excn::Mesh<INT>> &local_mesh, Excn::Mesh<INT> *global,
1626 size_t part_count, GlobalMap &global_node_map)
1627 {
1628 // Append all local node maps to the global node map.
1629 // Sort the global node map
1630 // Remove duplicates.
1631 // Position within map is now the map...
1632 // When building the local-part node to global id, use binary_search...
1633
1634 // Global node map and count.
1635 std::vector<std::vector<NodeInfo>> global_nodes(part_count);
1636
1637 size_t tot_size = 0;
1638 for (size_t p = 0; p < part_count; p++) {
1639 tot_size += local_mesh[p].count(Excn::ObjectType::NODE);
1640 global_nodes[p].resize(local_mesh[p].count(Excn::ObjectType::NODE));
1641 }
1642 global_node_map.resize(tot_size);
1643
1644 size_t offset = 0;
1645 for (size_t p = 0; p < part_count; p++) {
1646 std::vector<double> x(local_mesh[p].count(Excn::ObjectType::NODE));
1647 std::vector<double> y(local_mesh[p].count(Excn::ObjectType::NODE));
1648 std::vector<double> z(local_mesh[p].count(Excn::ObjectType::NODE));
1649 std::vector<INT> nid(local_mesh[p].count(Excn::ObjectType::NODE));
1650
1651 Excn::ExodusFile id(p);
1652 ex_get_id_map(id, EX_NODE_MAP, Data(nid));
1653 ex_get_coord(id, Data(x), Data(y), Data(z));
1654 for (size_t i = 0; i < local_mesh[p].count(Excn::ObjectType::NODE); i++) {
1655 global_nodes[p][i] = NodeInfo(nid[i], x[i], y[i], z[i]);
1656 }
1657 std::copy(global_nodes[p].begin(), global_nodes[p].end(), &global_node_map[offset]);
1658 offset += local_mesh[p].count(Excn::ObjectType::NODE);
1659 }
1660 // Now, sort the global_node_map array and remove duplicates...
1661 uniquify(global_node_map);
1662
1663 global->nodeCount = global_node_map.size();
1664
1665 // See whether the node numbers are contiguous. If so, we can map
1666 // the nodes back to their original location. Since the nodes are
1667 // sorted and there are no duplicates, we just need to see if the id
1668 // at global_node_map.size() == global_node_map.size();
1669 INT max_id = global_node_map[global->nodeCount - 1].id;
1670 bool is_contiguous = (int64_t)max_id == static_cast<int64_t>(global_node_map.size());
1671 fmt::print("Node map {} contiguous.\n", (is_contiguous ? "is" : "is not"));
1672
1673 // Create the map that maps from a local part node to the
1674 // global map. This combines the mapping local part node to
1675 // 'global id' and then 'global id' to global position. The
1676 // mapping is now a direct lookup instead of a lookup followed by
1677 // a reverse map.
1678 if (is_contiguous) {
1679 for (size_t p = 0; p < part_count; p++) {
1680 size_t node_count = local_mesh[p].count(Excn::ObjectType::NODE);
1681 for (size_t i = 0; i < node_count; i++) {
1682 const NodeInfo &global_node = global_nodes[p][i];
1683 local_mesh[p].localNodeToGlobal[i] = global_node.id - 1;
1684 }
1685 }
1686 }
1687 else {
1688 auto cur_pos = global_node_map.begin();
1689 for (size_t p = 0; p < part_count; p++) {
1690 size_t node_count = local_mesh[p].count(Excn::ObjectType::NODE);
1691 for (size_t i = 0; i < node_count; i++) {
1692 NodeInfo global_node = global_nodes[p][i];
1693
1694 if (cur_pos == global_node_map.end() || *cur_pos != global_node) {
1695 auto iter =
1696 std::lower_bound(global_node_map.begin(), global_node_map.end(), global_node);
1697 if (iter == global_node_map.end()) {
1698 NodeInfo n = global_node;
1699 fmt::print(stderr,
1700 "ERROR: Bad Node in build_reverse_node_map: {}\tat location: {}\t{}\t{}\n",
1701 n.id, n.x, n.y, n.z);
1702 exit(EXIT_FAILURE);
1703 }
1704 cur_pos = iter;
1705 }
1706 size_t nodal_value = cur_pos - global_node_map.begin();
1707 local_mesh[p].localNodeToGlobal[i] = nodal_value;
1708 ++cur_pos;
1709 }
1710 }
1711 }
1712
1713 // Update the nodal ids to give a unique, non-repeating set. If contiguous, then
1714 // there is nothing to do. If not contiguous, then need to determine if there are any
1715 // repeats (id reuse) and if so, generate a new id for the repeated uses.
1716 // A duplicate id would have the same id, but different x y z position.
1717 if (!is_contiguous) {
1718 bool repeat_found = false;
1719 size_t id_last = global_node_map[0].id;
1720 for (size_t i = 1; i < global->nodeCount; i++) {
1721 if (global_node_map[i].id == id_last) {
1722 global_node_map[i].id = ++max_id;
1723 repeat_found = true;
1724 }
1725 else {
1726 id_last = global_node_map[i].id;
1727 }
1728 }
1729 if (repeat_found) {
1730 fmt::print(
1731 stderr,
1732 "WARNING: Duplicate node ids were found. Their ids have been renumbered to remove "
1733 "duplicates.\n\tIf the part meshes should be identical, maybe use the "
1734 "--ignore_coordinate option.\n");
1735 }
1736 }
1737 }
1738
1739 template <typename INT>
1740 void build_reverse_node_map(std::vector<Excn::Mesh<INT>> &local_mesh, Excn::Mesh<INT> *global,
1741 size_t part_count, std::vector<INT> &global_node_map)
1742 {
1743 // Append all local node maps to the global node map.
1744 // Sort the global node map
1745 // Remove duplicates.
1746 // Position within map is now the map...
1747 // When building the local-part node to global id, use binary_search...
1748
1749 // Global node map and count.
1750 std::vector<std::vector<INT>> global_nodes(part_count);
1751
1752 size_t tot_size = 0;
1753 for (size_t p = 0; p < part_count; p++) {
1754 tot_size += local_mesh[p].count(Excn::ObjectType::NODE);
1755 global_nodes[p].resize(local_mesh[p].count(Excn::ObjectType::NODE));
1756 }
1757 global_node_map.resize(tot_size);
1758
1759 size_t offset = 0;
1760 for (size_t p = 0; p < part_count; p++) {
1761 Excn::ExodusFile id(p);
1762 ex_get_id_map(id, EX_NODE_MAP, Data(global_nodes[p]));
1763 std::copy(global_nodes[p].begin(), global_nodes[p].end(), &global_node_map[offset]);
1764 offset += local_mesh[p].count(Excn::ObjectType::NODE);
1765 }
1766
1767 // Now, sort the global_node_map array and remove duplicates...
1768 uniquify(global_node_map);
1769
1770 global->nodeCount = global_node_map.size();
1771
1772 // See whether the node numbers are contiguous. If so, we can map
1773 // the nodes back to their original location. Since the nodes are
1774 // sorted and there are no duplicates, we just need to see if the id
1775 // at global_node_map.size() == global_node_map.size();
1776 INT max_id = global_node_map[global->nodeCount - 1];
1777 bool is_contiguous = (int64_t)max_id == static_cast<int64_t>(global_node_map.size());
1778 fmt::print("Node map {} contiguous.\n", (is_contiguous ? "is" : "is not"));
1779
1780 // Create the map that maps from a local part node to the
1781 // global map. This combines the mapping local part node to
1782 // 'global id' and then 'global id' to global position. The
1783 // mapping is now a direct lookup instead of a lookup followed by
1784 // a reverse map.
1785 if (is_contiguous) {
1786 for (size_t p = 0; p < part_count; p++) {
1787 size_t node_count = local_mesh[p].count(Excn::ObjectType::NODE);
1788 for (size_t i = 0; i < node_count; i++) {
1789 INT global_node = global_nodes[p][i];
1790 local_mesh[p].localNodeToGlobal[i] = global_node - 1;
1791 }
1792 }
1793 }
1794 else {
1795 auto cur_pos = global_node_map.begin();
1796 for (size_t p = 0; p < part_count; p++) {
1797 size_t node_count = local_mesh[p].count(Excn::ObjectType::NODE);
1798 for (size_t i = 0; i < node_count; i++) {
1799 INT global_node = global_nodes[p][i];
1800
1801 if (cur_pos == global_node_map.end() || *cur_pos != global_node) {
1802 auto iter =
1803 std::lower_bound(global_node_map.begin(), global_node_map.end(), global_node);
1804 if (iter == global_node_map.end()) {
1805 INT n = global_node;
1806 fmt::print(stderr, "ERROR: Bad Node in build_reverse_node_map: {}\n", n);
1807 exit(EXIT_FAILURE);
1808 }
1809 cur_pos = iter;
1810 }
1811 size_t nodal_value = cur_pos - global_node_map.begin();
1812 local_mesh[p].localNodeToGlobal[i] = nodal_value;
1813 ++cur_pos;
1814 }
1815 }
1816 }
1817 }
1818
1819 void get_put_variable_names(int id, int out, Excn::Variables &vars, Excn::SystemInterface &si,
1820 int *combined_status_variable_index)
1821 {
1822 if (vars.count(Excn::InOut::OUT_) > 0) {
1823
1824 char **output_name_list =
1825 get_name_array(vars.count(Excn::InOut::OUT_), Excn::ExodusFile::max_name_length());
1826
1827 int num_vars = vars.index_.size();
1828 int extra = vars.addStatus ? 1 : 0;
1829 int num_input_vars = num_vars - extra;
1830
1831 char **input_name_list = get_name_array(num_vars, Excn::ExodusFile::max_name_length());
1832 if (num_input_vars > 0) {
1833 int error = ex_get_variable_names(id, vars.type(), num_input_vars, input_name_list);
1834 if (error != EX_NOERR) {
1835 fmt::print(stderr, "ERROR: Cannot get {} variable names\n", vars.label());
1836 exit(EXIT_FAILURE);
1837 }
1838 }
1839
1840 std::string status;
1841 if (vars.type() == EX_ELEM_BLOCK || vars.type() == EX_NODAL) {
1842 if (vars.type() == EX_ELEM_BLOCK) {
1843 status = si.element_status_variable();
1844 if (status != "NONE") {
1845 copy_string(input_name_list[num_vars - 1], status,
1846 Excn::ExodusFile::max_name_length() + 1);
1847 }
1848 }
1849 else if (vars.type() == EX_NODAL) {
1850 status = si.nodal_status_variable();
1851 if (status != "NONE") {
1852 copy_string(input_name_list[num_vars - 1], status,
1853 Excn::ExodusFile::max_name_length() + 1);
1854 }
1855 }
1856 }
1857
1858 // Iterate through the 'var_index' and transfer
1859 // Assume that the number of pointers is limited to
1860 // the number of results variables
1861 size_t maxlen = 0;
1862 for (int i = 0; i < num_vars; i++) {
1863 if (vars.index_[i] > 0) {
1864 copy_string(output_name_list[vars.index_[i] - 1], input_name_list[i],
1865 Excn::ExodusFile::max_name_length() + 1);
1866 if (strlen(input_name_list[i]) > maxlen) {
1867 maxlen = strlen(input_name_list[i]);
1868 }
1869 }
1870 }
1871
1872 // See if any of the variable names conflict with the status variable name...
1873 if (status != "NONE") {
1874 for (size_t i = 0; i < static_cast<size_t>(vars.count(Excn::InOut::OUT_)) - 1; i++) {
1875 if (case_compare(output_name_list[i], status)) {
1876 // Error -- duplicate element variable names on output database.
1877 fmt::print(stderr,
1878 "\nERROR: A {} variable already exists on the input database with the "
1879 "same name as the status variable '{}'. This is not allowed.\n\n",
1880 vars.label(), status);
1881 exit(EXIT_FAILURE);
1882 }
1883 }
1884 }
1885
1886 maxlen += 2;
1887 // Assume 8 characters for initial tab...
1888 int width = si.screen_width();
1889 int nfield = (width - 8) / maxlen;
1890 if (nfield < 1) {
1891 nfield = 1;
1892 }
1893
1894 fmt::print("Found {} {} variables.\n\t", vars.count(Excn::InOut::OUT_), vars.label());
1895 {
1896 int i = 0;
1897 int ifld = 1;
1898 while (i < vars.count(Excn::InOut::OUT_)) {
1899 fmt::print("{:<{}}", output_name_list[i++], maxlen);
1900 if (++ifld > nfield && i < vars.count(Excn::InOut::OUT_)) {
1901 fmt::print("\n\t");
1902 ifld = 1;
1903 }
1904 }
1905 fmt::print("\n\n");
1906 }
1907
1908 ex_put_variable_names(out, vars.type(), vars.count(Excn::InOut::OUT_), output_name_list);
1909
1910 // KLUGE: Handle finding combined status index variable here since it is
1911 // the only place we have the list of output variable names...
1912 if (vars.type() == EX_ELEM_BLOCK && (combined_status_variable_index != nullptr)) {
1913 *combined_status_variable_index = 0;
1914 const std::string &comb_stat = si.combined_mesh_status_variable();
1915 if (!comb_stat.empty()) {
1916 for (int i = 0; i < vars.count(Excn::InOut::OUT_); i++) {
1917 if (case_compare(comb_stat, output_name_list[i])) {
1918 *combined_status_variable_index = i + 1;
1919 break;
1920 }
1921 }
1922 }
1923 }
1924 free_name_array(output_name_list, vars.count(Excn::InOut::OUT_));
1925 free_name_array(input_name_list, num_vars);
1926 }
1927 }
1928
1929 void get_variable_params(int id, Excn::Variables &vars, const StringIdVector &variable_list)
1930 {
1931 // Determines the number of variables of type 'type()' that will
1932 // be written to the output database. The 'variable_list' vector
1933 // specifies a possibly empty list of variable names that the user
1934 // wants transferred to the output database. If 'variable_list' is
1935 // empty, then all variables of that type will be transferred; if
1936 // the 'variable_list' size is 1 and it contains the string 'NONE',
1937 // then no variables of that type will be transferred; if size is 1
1938 // and it contains the string 'ALL', then all variables of that type
1939 // will be transferred.
1940 //
1941 // Returns the number of variables which will be output Also creates
1942 // a 'var_index'. The var_index is zero-based and of size
1943 // 'input_variable_count'. If:
1944 // var_index[i] ==0, variable not written to output database
1945 // var_index[i] > 0, variable written; is variable 'var_index[i]'
1946
1947 // If 'type' is ELEMENT or NODE, then reserve space for the 'status' variable.
1948 int extra = vars.addStatus ? 1 : 0;
1949 int num_vars;
1950 ex_get_variable_param(id, vars.type(), &num_vars);
1951
1952 vars.index_.resize(num_vars + extra);
1953
1954 // Create initial index which defaults to no output...
1955 std::fill(vars.index_.begin(), vars.index_.end(), 0);
1956 // ...Except for the status variable (if any)
1957 if (extra == 1) {
1958 vars.index_[num_vars] = 1;
1959 }
1960
1961 // If 'variable_list' is empty or specified 'ALL', then all
1962 // variables are to be output
1963 if (variable_list.empty() ||
1964 (variable_list.size() == 1 && case_compare(variable_list[0].first, "all"))) {
1965 std::iota(vars.index_.begin(), vars.index_.end(), 1);
1966 vars.outputCount = num_vars + extra;
1967 return;
1968 }
1969
1970 // Another possibility is user specifies "NONE" for the variable
1971 // list so no variables will be written. Just return 0.
1972 if (variable_list.size() == 1 && case_compare(variable_list[0].first, "none")) {
1973 vars.outputCount = extra;
1974 return;
1975 }
1976
1977 // At this point, the variable_list specifies at least one
1978 // variable to be output to the database.
1979 // Get the list that the user entered and the list of files
1980 // from the input database...
1981 {
1982 StringVector exo_names = get_exodus_variable_names(id, vars.type(), num_vars);
1983
1984 // Iterate 'variable_list' and find position in 'exo_names'. If
1985 // not found, there is an error -- set index to -1; otherwise set
1986 // index to position (1-based) in variable_list. Others are
1987 // already set to '0' by default initialization.
1988
1989 // The variable_list may contain multiple entries for each
1990 // variable if the user is specifying output only on certain
1991 // element blocks...
1992 std::string var_name;
1993 int var_count = 0;
1994 for (const auto &elem : variable_list) {
1995 if (var_name == elem.first) {
1996 continue;
1997 }
1998 var_name = elem.first;
1999 bool found = false;
2000 for (size_t j = 0; j < exo_names.size() && !found; j++) {
2001 if (case_compare(exo_names[j], var_name)) {
2002 found = true;
2003 vars.index_[j] = ++var_count;
2004 }
2005 }
2006 if (!found) {
2007 fmt::print(stderr, "ERROR: Variable '{}' is not valid.\n", elem.first);
2008 exit(EXIT_FAILURE);
2009 }
2010 }
2011 // Count non-zero entries in var_index;
2012 int nz_count = 0;
2013 for (auto &elem : vars.index_) {
2014 if (elem > 0) {
2015 nz_count++;
2016 }
2017 }
2018 SMART_ASSERT(nz_count == var_count + extra)(nz_count)(var_count);
2019
2020 if (vars.addStatus) {
2021 vars.index_[num_vars] = nz_count; // Already counted above...
2022 }
2023 vars.outputCount = nz_count;
2024 return;
2025 }
2026 }
2027
2028 bool check_variable_params(size_t p, const Excn::Variables &vars)
2029 {
2030 // Determines the number of variables of type 'type()' that will
2031 // be written to the output database. The 'variable_list' vector
2032 // specifies a possibly empty list of variable names that the user
2033 // wants transferred to the output database. If 'variable_list' is
2034 // empty, then all variables of that type will be transferred; if
2035 // the 'variable_list' size is 1 and it contains the string 'NONE',
2036 // then no variables of that type will be transferred; if size is 1
2037 // and it contains the string 'ALL', then all variables of that type
2038 // will be transferred.
2039 //
2040 // Returns the number of variables which will be output Also creates
2041 // a 'var_index'. The var_index is zero-based and of size
2042 // 'input_variable_count'. If:
2043 // var_index[i] ==0, variable not written to output database
2044 // var_index[i] > 0, variable written; is variable 'var_index[i]'
2045
2046 // If 'type' is ELEMENT or NODE, then reserve space for the 'status' variable.
2047 int extra = vars.addStatus ? 1 : 0;
2048 int num_vars;
2049 auto id = Excn::ExodusFile(p);
2050 ex_get_variable_param(id, vars.type(), &num_vars);
2051 if ((size_t)num_vars != vars.index_.size() - extra) {
2052 fmt::print("ERROR: Part mesh {} has a different number of {} variables ({}) than the root "
2053 "part mesh ({}) which is not allowed.\n",
2054 p, vars.label(), num_vars, vars.index_.size() - extra);
2055 return true;
2056 }
2057 return false;
2058 }
2059
2060 template <typename INT> void put_mesh_summary(const Excn::Mesh<INT> &mesh)
2061 {
2062 // Write out Mesh info
2063 fmt::print(" Title: {}\n\n", mesh.title);
2064 fmt::print(" Number of coordinates per node ={:14}\n",
2065 fmt::group_digits(mesh.count(Excn::ObjectType::DIM)));
2066 fmt::print(" Number of nodes ={:14}\n",
2067 fmt::group_digits(mesh.count(Excn::ObjectType::NODE)));
2068 fmt::print(" Number of elements ={:14}\n",
2069 fmt::group_digits(mesh.count(Excn::ObjectType::ELEM)));
2070 fmt::print(" Number of element blocks ={:14}\n",
2071 fmt::group_digits(mesh.count(Excn::ObjectType::EBLK)));
2072 fmt::print(" Number of nodal point sets ={:14}\n",
2073 fmt::group_digits(mesh.count(Excn::ObjectType::NSET)));
2074 fmt::print(" Number of element side sets ={:14}\n",
2075 fmt::group_digits(mesh.count(Excn::ObjectType::SSET)));
2076 }
2077
2078 template <typename INT>
2079 void get_nodesets(size_t total_node_count, std::vector<Excn::Mesh<INT>> &local_mesh,
2080 std::vector<std::vector<Excn::NodeSet<INT>>> &nodesets,
2081 std::vector<Excn::NodeSet<INT>> &glob_sets)
2082 {
2083 // Find number of nodesets in the global model...
2084 std::set<INT> set_ids;
2085 std::vector<INT> ids;
2086
2087 size_t part_count = local_mesh.size();
2088 int bad_ns = 0;
2089 {
2090 int ns_count;
2091 for (size_t p = 0; p < part_count; p++) {
2092 Excn::ExodusFile id(p);
2093 ns_count = ex_inquire_int(id, EX_INQ_NODE_SETS);
2094
2095 // Get the ids for these
2096 ids.resize(ns_count);
2097 if (ns_count > 0) {
2098 ex_get_ids(id, EX_NODE_SET, Data(ids));
2099 }
2100
2101 for (int iset = 0; iset < ns_count; iset++) {
2102 if (ids[iset] != 0) {
2103 set_ids.insert(ids[iset]);
2104 }
2105 else {
2106 bad_ns++;
2107 }
2108 }
2109 }
2110 }
2111
2112 if (bad_ns != 0) {
2113 fmt::print(stderr,
2114 "ERROR: There were {} nodesets (counting all files) which had an id equal to "
2115 "0 which is not allowed.\n",
2116 bad_ns);
2117 }
2118
2119 if (set_ids.empty()) {
2120 return;
2121 }
2122
2123 // set_ids now contains the union of all nodeset ids...
2124 glob_sets.resize(set_ids.size());
2125 size_t gnset_size = set_ids.size();
2126 ids.resize(gnset_size);
2127
2128 {
2129 size_t i = 0;
2130 for (auto &set_id : set_ids) {
2131 glob_sets[i].id = set_id;
2132 glob_sets[i].position_ = i;
2133 i++;
2134 }
2135 }
2136
2137 {
2138 for (size_t p = 0; p < part_count; p++) {
2139 Excn::ExodusFile id(p);
2140
2141 nodesets[p].resize(set_ids.size());
2142
2143 // Get the ids again so we can map current order back to file order...
2144 int ns_count = ex_inquire_int(id, EX_INQ_NODE_SETS);
2145 if (ns_count > 0) {
2146 ex_get_ids(id, EX_NODE_SET, Data(ids));
2147 }
2148
2149 for (int i = 0; i < ns_count; i++) {
2150 nodesets[p][i].id = ids[i];
2151
2152 // Find which global nodeset this id corresponds to...
2153 size_t gi = gnset_size;
2154 for (size_t j = 0; j < gnset_size; j++) {
2155 if (ids[i] == glob_sets[j].id) {
2156 gi = j;
2157 break;
2158 }
2159 }
2160 SMART_ASSERT(gi != gnset_size);
2161 glob_sets[gi].position_ = i;
2162 nodesets[p][i].position_ = gi;
2163
2164 // Get the parameters for this nodeset...
2165 if (ex_int64_status(id) & EX_BULK_INT64_API) {
2166 int64_t node_count;
2167 int64_t df_count;
2168 ex_get_set_param(id, EX_NODE_SET, nodesets[p][i].id, &node_count, &df_count);
2169 nodesets[p][i].nodeCount = node_count;
2170 nodesets[p][i].dfCount = df_count;
2171 }
2172 else {
2173 int node_count;
2174 int df_count;
2175 ex_get_set_param(id, EX_NODE_SET, nodesets[p][i].id, &node_count, &df_count);
2176 nodesets[p][i].nodeCount = node_count;
2177 nodesets[p][i].dfCount = df_count;
2178 }
2179
2180 std::vector<char> name(Excn::ExodusFile::max_name_length() + 1);
2181 ex_get_name(id, EX_NODE_SET, nodesets[p][i].id, Data(name));
2182 if (name[0] != '\0') {
2183 nodesets[p][i].name_ = Data(name);
2184 if (glob_sets[gi].name_.empty()) {
2185 glob_sets[gi].name_ = Data(name);
2186 }
2187 }
2188
2189 if (debug_level & 32) {
2190 fmt::print("Part {} ", p + 1);
2191 nodesets[p][i].dump();
2192 }
2193 }
2194 }
2195 }
2196
2197 verify_set_position_mapping("nodeset", part_count, glob_sets, nodesets);
2198
2199 {
2200 // Now get the nodeset nodes and df.
2201 // Currently ignore the DF. Could add if people need it...
2202
2203 // This is inefficient since the part loop is on
2204 // the inside... The other ordering would use more memory...
2205
2206 std::vector<INT> ns_nodes;
2207 for (size_t ns = 0; ns < set_ids.size(); ns++) {
2208
2209 std::vector<INT> glob_ns_nodes(total_node_count + 1);
2210 std::fill(glob_ns_nodes.begin(), glob_ns_nodes.end(), (INT)0);
2211
2212 size_t lns = glob_sets[ns].position_;
2213 for (size_t p = 0; p < part_count; p++) {
2214 Excn::ExodusFile id(p);
2215
2216 glob_sets[ns].name_ = nodesets[p][lns].name_;
2217
2218 size_t size = nodesets[p][lns].entity_count();
2219 if (size > 0) {
2220 ns_nodes.resize(size);
2221 ex_get_set(id, EX_NODE_SET, nodesets[p][lns].id, Data(ns_nodes), nullptr);
2222
2223 // The node ids are in local space -- map to global
2224 for (size_t iset = 0; iset < size; iset++) {
2225 size_t global_node = local_mesh[p].localNodeToGlobal[ns_nodes[iset] - 1] + 1;
2226 glob_ns_nodes[global_node] = 1;
2227 }
2228 }
2229 }
2230 // Count number of nonzero entries and transfer to the
2231 // output nodeset
2232 // NOTE: global_node above is 1-based.
2233 glob_sets[ns].nodeCount =
2234 std::accumulate(glob_ns_nodes.begin(), glob_ns_nodes.end(), INT(0));
2235 glob_sets[ns].nodeSetNodes.resize(glob_sets[ns].entity_count());
2236 glob_sets[ns].dfCount = 0;
2237
2238 size_t j = 0;
2239 for (size_t i = 1; i <= total_node_count; i++) {
2240 if (glob_ns_nodes[i] == 1) {
2241 glob_sets[ns].nodeSetNodes[j++] = i;
2242 glob_ns_nodes[i] = j;
2243 }
2244 }
2245 SMART_ASSERT(j == glob_sets[ns].entity_count())(j)(glob_sets[ns].entity_count())(ns);
2246
2247 // See if we need a map from local nodeset position to global nodeset position
2248 // Only needed if there are nodeset variables (or dist factors).
2249 // Assume all files have same number of variables...
2250 int num_vars;
2251 {
2252 Excn::ExodusFile id(0);
2253 ex_get_variable_param(id, EX_NODE_SET, &num_vars);
2254 }
2255 if (num_vars > 0) {
2256 for (size_t p = 0; p < part_count; p++) {
2257 Excn::ExodusFile id(p);
2258 // Get the nodelist, but store it in nodeOrderMap.
2259 // global_pos = nodeOrderMap[i]
2260 Excn::NodeSet<INT> &nset = nodesets[p][ns];
2261 size_t nnodes = nset.entity_count();
2262 nset.nodeOrderMap.resize(nnodes);
2263 ex_get_set(id, EX_NODE_SET, nset.id, Data(nset.nodeOrderMap), nullptr);
2264
2265 for (size_t i = 0; i < nnodes; i++) {
2266 size_t local_node = nset.nodeOrderMap[i]; // 1-based
2267 size_t global_node = local_mesh[p].localNodeToGlobal[local_node - 1] + 1; // 1-based
2268 size_t global_pos = glob_ns_nodes[global_node]; // 1-based
2269 nset.nodeOrderMap[i] = global_pos - 1;
2270 }
2271 #if 0
2272 if (debug_level & 32)
2273 nset.dump_order();
2274 #endif
2275 }
2276 }
2277 }
2278 }
2279 }
2280
2281 template <typename INT> void put_nodesets(std::vector<Excn::NodeSet<INT>> &glob_sets)
2282 {
2283 int exoid = Excn::ExodusFile::output();
2284
2285 if (debug_level & 32) {
2286 fmt::print("\nOutput NodeSets:\n");
2287 }
2288 for (auto &glob_set : glob_sets) {
2289 ex_put_set(exoid, EX_NODE_SET, glob_set.id, Data(glob_set.nodeSetNodes), nullptr);
2290 // ex_put_node_set_dist_fact(exoid, glob_sets[ns].id, &glob_sets[ns].distFactors[0]);
2291 // Done with the memory; clear out the vector containing the bulk data nodes and distFactors.
2292 clear(glob_set.nodeSetNodes);
2293 clear(glob_set.distFactors);
2294
2295 if (debug_level & 32) {
2296 glob_set.dump();
2297 }
2298 }
2299 }
2300
2301 template <typename INT>
2302 void get_sideset_metadata(std::vector<Excn::Mesh<INT>> &local_mesh,
2303 std::vector<std::vector<Excn::SideSet<INT>>> &sets,
2304 std::vector<Excn::SideSet<INT>> &glob_ssets)
2305 {
2306 // Find number of sidesets in the global model...
2307 std::set<int> set_ids;
2308 std::vector<INT> ids;
2309
2310 size_t part_count = local_mesh.size();
2311 int bad_ss = 0;
2312 {
2313 for (size_t p = 0; p < part_count; p++) {
2314 Excn::ExodusFile id(p);
2315 int ss_count = ex_inquire_int(id, EX_INQ_SIDE_SETS);
2316
2317 // Get the ids for these
2318 ids.resize(ss_count);
2319 if (ss_count > 0) {
2320 ex_get_ids(id, EX_SIDE_SET, Data(ids));
2321 }
2322
2323 for (int i = 0; i < ss_count; i++) {
2324 if (ids[i] != 0) {
2325 set_ids.insert(ids[i]);
2326 }
2327 else {
2328 bad_ss++;
2329 }
2330 }
2331 }
2332 }
2333
2334 if (bad_ss != 0) {
2335 fmt::print(stderr,
2336 "ERROR: There were {} sidesets (counting all files) which had an id equal to 0 "
2337 "which is not allowed.\n",
2338 bad_ss);
2339 }
2340
2341 if (set_ids.empty()) {
2342 return;
2343 }
2344
2345 // set_ids now contains the union of all sideset ids...
2346 glob_ssets.resize(set_ids.size());
2347 size_t gsset_size = set_ids.size();
2348 ids.resize(gsset_size);
2349
2350 {
2351 size_t i = 0;
2352 for (auto set_id : set_ids) {
2353 glob_ssets[i].id = set_id;
2354 glob_ssets[i].position_ = i; // Not used
2355 i++;
2356 }
2357 }
2358
2359 {
2360 std::vector<char> name(Excn::ExodusFile::max_name_length() + 1);
2361 for (size_t p = 0; p < part_count; p++) {
2362 Excn::ExodusFile id(p);
2363
2364 sets[p].resize(set_ids.size());
2365
2366 // Get the ids again so we can map current order back to file order...
2367 int ss_count = ex_inquire_int(id, EX_INQ_SIDE_SETS);
2368 if (ss_count > 0) {
2369 ex_get_ids(id, EX_SIDE_SET, Data(ids));
2370 }
2371
2372 for (int i = 0; i < ss_count; i++) {
2373 sets[p][i].id = ids[i];
2374
2375 // Find which global sideset this id corresponds to...
2376 size_t gi = gsset_size;
2377 for (size_t j = 0; j < gsset_size; j++) {
2378 if (ids[i] == glob_ssets[j].id) {
2379 gi = j;
2380 break;
2381 }
2382 }
2383 SMART_ASSERT(gi != gsset_size);
2384 sets[p][i].position_ = gi;
2385
2386 // Get the parameters for this sideset...
2387 ex_set set{};
2388 set.type = EX_SIDE_SET;
2389 set.id = sets[p][i].id;
2390 set.entry_list = nullptr;
2391 set.extra_list = nullptr;
2392 set.distribution_factor_list = nullptr;
2393 int error = ex_get_sets(id, 1, &set);
2394 if (error != EX_NOERR) {
2395 fmt::print(stderr, "ERROR: Cannot get side set with id {}\n", set.id);
2396 exit(EXIT_FAILURE);
2397 }
2398
2399 sets[p][i].sideCount = set.num_entry;
2400 sets[p][i].dfCount = set.num_distribution_factor;
2401 glob_ssets[gi].sideCount += sets[p][i].entity_count();
2402 glob_ssets[gi].dfCount += sets[p][i].dfCount;
2403
2404 ex_get_name(id, EX_SIDE_SET, sets[p][i].id, Data(name));
2405 if (name[0] != '\0') {
2406 sets[p][i].name_ = Data(name);
2407 glob_ssets[gi].name_ = Data(name);
2408 }
2409 }
2410 }
2411
2412 verify_set_position_mapping("sideset", part_count, glob_ssets, sets);
2413
2414 // See if we need a map from local sideset position to global sideset position
2415 // Only needed if there are sideeset variables (or dist factors which are currently ignored).
2416 // Assume all files have same number of variables...
2417 bool need_sideset_map = false;
2418 {
2419 int num_vars;
2420 Excn::ExodusFile id(0);
2421 ex_get_variable_param(id, EX_SIDE_SET, &num_vars);
2422 need_sideset_map = num_vars > 0;
2423 }
2424
2425 // Now get the sideset elements/sides. Ignoring DF for now...
2426 for (size_t ss = 0; ss < set_ids.size(); ss++) {
2427 // This is maximum possible size; will probably be reduced by duplicate elimination...
2428 using ElemSideMap = std::vector<std::pair<INT, INT>>;
2429 ElemSideMap elem_side(glob_ssets[ss].sideCount);
2430 int ss_id = glob_ssets[ss].id;
2431 size_t offset = 0;
2432
2433 for (size_t p = 0; p < part_count; p++) {
2434 for (size_t lss = 0; lss < sets[p].size(); lss++) {
2435 if (sets[p][lss].position_ == ss) {
2436 Excn::ExodusFile id(p);
2437 sets[p][lss].elems.resize(sets[p][lss].sideCount);
2438 sets[p][lss].sides.resize(sets[p][lss].sideCount);
2439 ex_get_set(id, EX_SIDE_SET, ss_id, Data(sets[p][lss].elems),
2440 Data(sets[p][lss].sides));
2441
2442 // Add these to the elem_side vector...
2443 for (size_t i = 0; i < sets[p][lss].sideCount; i++) {
2444 size_t global_elem =
2445 local_mesh[p].localElementToGlobal[sets[p][lss].elems[i] - 1] + 1;
2446 elem_side[offset + i] =
2447 std::make_pair((INT)global_elem, (INT)sets[p][lss].sides[i]);
2448 }
2449 offset += sets[p][lss].sideCount;
2450 break;
2451 }
2452 }
2453 }
2454
2455 uniquify(elem_side);
2456
2457 // Set the output sideset definition...
2458 glob_ssets[ss].sideCount = elem_side.size();
2459 glob_ssets[ss].dfCount = 0;
2460 glob_ssets[ss].elems.resize(elem_side.size());
2461 glob_ssets[ss].sides.resize(elem_side.size());
2462
2463 // Populate the global sideset elements and sides...
2464 for (size_t i = 0; i < elem_side.size(); i++) {
2465 std::tie(glob_ssets[ss].elems[i], glob_ssets[ss].sides[i]) = elem_side[i];
2466 }
2467
2468 if (need_sideset_map) {
2469 // For this sideset in each part, figure out the mapping for
2470 // its (elem, side, variable) position into the corresponding
2471 // global sideset position...
2472
2473 // Try the lower_bound searching of elem_side for now. If
2474 // inefficient, fix later...
2475 for (size_t p = 0; p < part_count; p++) {
2476 for (size_t lss = 0; lss < sets[p].size(); lss++) {
2477 if (sets[p][lss].position_ == ss) {
2478 sets[p][lss].elemOrderMap.resize(sets[p][lss].sideCount);
2479 for (size_t i = 0; i < sets[p][lss].sideCount; i++) {
2480 size_t global_elem =
2481 local_mesh[p].localElementToGlobal[sets[p][lss].elems[i] - 1] + 1;
2482 std::pair<INT, INT> es =
2483 std::make_pair((INT)global_elem, (INT)sets[p][lss].sides[i]);
2484
2485 auto iter = std::lower_bound(elem_side.begin(), elem_side.end(), es);
2486 size_t pos = iter - elem_side.begin();
2487 sets[p][lss].elemOrderMap[i] = pos;
2488 }
2489 break;
2490 }
2491 }
2492 }
2493 }
2494 }
2495
2496 // Calculate sideset offset
2497 for (size_t ss = 0; ss < glob_ssets.size(); ss++) {
2498 size_t sum = 0;
2499 for (size_t p = 0; p < part_count; p++) {
2500 for (size_t lss = 0; lss < sets[p].size(); lss++) {
2501 if (sets[p][lss].position_ == ss) {
2502 sets[p][lss].offset_ = sum;
2503 sum += sets[p][lss].entity_count();
2504
2505 if (debug_level & 16) {
2506 fmt::print("Part {} ", p + 1);
2507 sets[p][lss].dump();
2508 }
2509 break;
2510 }
2511 }
2512 }
2513 }
2514
2515 // Free some memory which is no longer needed...
2516 // (Could move up into sideset loop above)
2517 for (size_t p = 0; p < part_count; p++) {
2518 for (auto &elem : sets[p]) {
2519 clear(elem.elems);
2520 clear(elem.sides);
2521 clear(elem.distFactors);
2522 }
2523 }
2524 }
2525 }
2526
2527 template <typename INT> void get_put_sidesets(std::vector<Excn::SideSet<INT>> &glob_ssets)
2528 {
2529 int exoid = Excn::ExodusFile::output(); // output file identifier
2530 for (auto &glob_sset : glob_ssets) {
2531 ex_put_set(exoid, EX_SIDE_SET, glob_sset.id, const_cast<INT *>(Data(glob_sset.elems)),
2532 const_cast<INT *>(&glob_sset.sides[0]));
2533 if (glob_sset.dfCount > 0) {
2534 ex_put_set_dist_fact(exoid, EX_SIDE_SET, glob_sset.id,
2535 reinterpret_cast<void *>(Data(glob_sset.distFactors)));
2536 }
2537 }
2538
2539 for (auto &glob_sset : glob_ssets) {
2540 clear(glob_sset.elems);
2541 clear(glob_sset.sides);
2542 clear(glob_sset.distFactors);
2543 }
2544 }
2545
2546 template <typename T, typename INT>
2547 void add_status_variable(int id_out, const Excn::Mesh<INT> &global,
2548 const std::vector<Excn::Block> &blocks,
2549 const std::vector<Excn::Block> &glob_blocks,
2550 const std::vector<INT> &local_element_to_global, int step, int variable,
2551 T alive, int combined_variable_index)
2552 {
2553 SMART_ASSERT(sizeof(T) == Excn::ExodusFile::io_word_size());
2554 std::vector<T> status;
2555 SMART_ASSERT(alive == 0.0 || alive == 1.0)(alive);
2556
2557 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
2558 status.resize(glob_blocks[b].entity_count());
2559 std::fill(status.begin(), status.end(), T(1.0 - alive));
2560 size_t boffset = blocks[b].offset_;
2561 size_t goffset = glob_blocks[b].offset_;
2562 size_t element_count = blocks[b].entity_count();
2563 for (size_t e = 0; e < element_count; e++) {
2564 size_t global_block_pos = local_element_to_global[(e + boffset)] - goffset;
2565 status[global_block_pos] = alive;
2566 }
2567
2568 // If combining this status variable with a mesh status
2569 // variable, do that now...
2570 if (combined_variable_index > 0) {
2571 std::vector<T> mesh_status(glob_blocks[b].entity_count());
2572 ex_get_var(id_out, step, EX_ELEM_BLOCK, combined_variable_index, glob_blocks[b].id,
2573 glob_blocks[b].entity_count(), Data(mesh_status));
2574
2575 if (alive == 1.0) {
2576 for (size_t i = 0; i < glob_blocks[b].entity_count(); i++) {
2577 status[i] = status[i] * mesh_status[i];
2578 }
2579 }
2580 else {
2581 SMART_ASSERT(alive == 0.0);
2582 for (size_t i = 0; i < glob_blocks[b].entity_count(); i++) {
2583 status[i] = 1.0 - ((1.0 - status[i]) * (1.0 - mesh_status[i]));
2584 }
2585 }
2586 }
2587 ex_put_var(id_out, step, EX_ELEM_BLOCK, variable, glob_blocks[b].id,
2588 glob_blocks[b].entity_count(), Data(status));
2589 }
2590 }
2591
2592 StringVector get_exodus_variable_names(int id, ex_entity_type elType, size_t var_count)
2593 {
2594 // Allocate space for variable names...
2595 char **name_list = get_name_array(var_count, Excn::ExodusFile::max_name_length());
2596 int error = ex_get_variable_names(id, elType, var_count, name_list);
2597 if (error != EX_NOERR) {
2598 fmt::print(stderr, "ERROR: Cannot get variable names\n");
2599 exit(EXIT_FAILURE);
2600 }
2601
2602 StringVector names(var_count);
2603 for (size_t j = 0; j < var_count; j++) {
2604 compress_white_space(name_list[j]);
2605 names[j] = std::string(name_list[j]);
2606 }
2607
2608 free_name_array(name_list, var_count);
2609 return names;
2610 }
2611
2612 template <typename T, typename INT>
2613 void filter_truth_table(int id, Excn::Mesh<INT> &global, std::vector<T> &glob_blocks,
2614 Excn::Variables &vars, const StringIdVector &variable_names)
2615 {
2616 // This routine checks the 'variable_names' list to see if the user
2617 // has restricted the output of certain variables to certain element
2618 // blocks. If so, then the truth table is modified to match the
2619 // users request.
2620 if (variable_names.empty()) {
2621 return;
2622 }
2623
2624 // Check for a non-zero id entry in the variable_names list which
2625 // will signify a user-specified element block.
2626 bool found_it = false;
2627 for (size_t i = 0; i < variable_names.size() && !found_it; i++) {
2628 if (variable_names[i].second > 0) {
2629 found_it = true;
2630 }
2631 }
2632 if (!found_it) {
2633 return;
2634 }
2635
2636 // At this point, know that there is at least one block-restricted
2637 // variable output specification. For each one, modify the global
2638 // truth table to match the specification.
2639 StringVector exo_names = get_exodus_variable_names(id, vars.type(), vars.count());
2640
2641 std::string var_name;
2642 int out_position = -1;
2643 for (auto [v_name, v_blkid] : variable_names) {
2644 if (v_blkid > 0 && var_name != v_name) {
2645 var_name = v_name;
2646 // Find which exodus variable matches this name
2647 out_position = -1;
2648 for (size_t j = 0; j < exo_names.size(); j++) {
2649 if (case_compare(exo_names[j], var_name)) {
2650 out_position = vars.index_[j] - 1;
2651 break;
2652 }
2653 }
2654 if (out_position < 0) {
2655 fmt::print(stderr, "ERROR: Variable '{}' does not exist on any block in this database.\n",
2656 v_name);
2657 exit(EXIT_FAILURE);
2658 }
2659
2660 // Set all truth table entries for this variable to negative
2661 // of current value and then iterate over specified blocks and
2662 // set those positive. This way can make sure that the
2663 // variable truly exists for the block that the user specified.
2664 found_it = false;
2665 for (size_t b = 0; b < global.count(vars.objectType); b++) {
2666 if (glob_blocks[b].id == v_blkid) {
2667 if (glob_blocks[b].truthTable[out_position] == 0) {
2668 fmt::print(stderr, "ERROR: Variable '{}' does not exist on block {}.\n", v_name,
2669 v_blkid);
2670 exit(EXIT_FAILURE);
2671 }
2672 else {
2673 found_it = true;
2674 break;
2675 }
2676 }
2677 else {
2678 // User does not want this variable output on these blocks...
2679 glob_blocks[b].truthTable[out_position] = 0;
2680 }
2681 }
2682
2683 if (!found_it) {
2684 fmt::print(stderr,
2685 "ERROR: User-specified block id of {} for variable '{}' does not exist.\n",
2686 v_blkid, v_name);
2687 exit(EXIT_FAILURE);
2688 }
2689 }
2690 }
2691 }
2692
2693 template <typename T, typename INT>
2694 void get_truth_table(Excn::Mesh<INT> &global, std::vector<std::vector<T>> &blocks,
2695 std::vector<T> &glob_blocks, Excn::Variables &vars, int debug)
2696 {
2697 // read truth table - sum across all parts since many will
2698 // have values set to zero for zero length blocks the element
2699 // variable truth table organized as a 2D array:
2700 // [global.count(Excn::EBLK)][num_elem_vars]
2701
2702 Excn::ObjectType object_type = vars.objectType;
2703
2704 if (vars.count(Excn::InOut::OUT_) > 0) {
2705 // For each input exodus file, get it's truth table and fill
2706 // in the location in the output truth table...
2707
2708 size_t part_count = blocks.size();
2709 for (size_t p = 0; p < part_count; p++) {
2710 Excn::ExodusFile id(p);
2711
2712 for (size_t b = 0; b < global.count(object_type); b++) {
2713 size_t gb = 0;
2714 for (; gb < global.count(object_type); gb++) {
2715 if (glob_blocks[gb].id == blocks[p][b].id) {
2716 break;
2717 }
2718 }
2719 SMART_ASSERT(glob_blocks[gb].id == blocks[p][b].id);
2720
2721 if (p == 0) {
2722 glob_blocks[gb].truthTable.resize(vars.count(Excn::InOut::OUT_));
2723 }
2724
2725 if (vars.count(Excn::InOut::IN_) > 0) {
2726 blocks[p][b].truthTable.resize(vars.count(Excn::InOut::IN_));
2727 ex_get_object_truth_vector(id, vars.type(), blocks[p][b].id,
2728 vars.count(Excn::InOut::IN_), Data(blocks[p][b].truthTable));
2729
2730 // Find global block corresponding to this block. (Ids match)
2731 for (int j = 0; j < vars.count(Excn::InOut::IN_); j++) {
2732 if (vars.index_[j] > 0) {
2733 glob_blocks[gb].truthTable[vars.index_[j] - 1] += blocks[p][b].truthTable[j];
2734 }
2735 }
2736 }
2737 if (vars.addStatus) {
2738 glob_blocks[gb].truthTable[vars.count(Excn::InOut::OUT_) - 1] = 1;
2739 }
2740 }
2741 }
2742
2743 // reset truth table values that may be greater than 1
2744 for (size_t b = 0; b < global.count(object_type); b++) {
2745 for (int j = 0; j < vars.count(Excn::InOut::OUT_); j++) {
2746 if (glob_blocks[b].truthTable[j] > 0) {
2747 glob_blocks[b].truthTable[j] = 1;
2748 }
2749 }
2750 }
2751
2752 if (debug_level & debug) {
2753 fmt::print("Truth table for {}\t{} variables\t{} sets\n", vars.label(),
2754 vars.count(Excn::InOut::OUT_), global.count(object_type));
2755 for (size_t b = 0; b < global.count(object_type); b++) {
2756 for (int j = 0; j < vars.count(Excn::InOut::OUT_); j++) {
2757 fmt::print("{}", glob_blocks[b].truthTable[j]);
2758 }
2759 fmt::print("\n");
2760 }
2761 }
2762 }
2763 }
2764
2765 bool case_compare(const std::string &s1, const std::string &s2)
2766 {
2767 return (s1.size() == s2.size()) &&
2768 std::equal(s1.begin(), s1.end(), s2.begin(),
2769 [](char a, char b) { return std::tolower(a) == std::tolower(b); });
2770 }
2771
2772 void add_info_record(char *info_record, int size)
2773 {
2774 // Add 'uname' output to the passed in character string.
2775 // Maximum size of string is 'size' (not including terminating nullptr)
2776 // This is used as information data in the concatenated results file
2777 // to help in tracking when/where/... the file was created
2778 auto info = sys_info("CONJOIN");
2779 copy_string(info_record, info, size + 1);
2780 }
2781
2782 inline bool is_whitespace(char c)
2783 {
2784 static char white_space[] = {' ', '\t', '\n', '\r', ',', '\0'};
2785 return std::strchr(white_space, c) != nullptr;
2786 }
2787
2788 void compress_white_space(char *str)
2789 {
2790 char *ibuf = str;
2791 char *obuf = str;
2792
2793 int i = 0;
2794 int cnt = 0;
2795
2796 // Don't process an empty string.
2797 if (str == nullptr) {
2798 return;
2799 }
2800
2801 // Skip leading...
2802 while (*ibuf != 0 && is_whitespace(*ibuf)) {
2803 ++ibuf;
2804 }
2805
2806 while (*ibuf != 0) {
2807 if (is_whitespace(*ibuf) && cnt > 0) {
2808 ibuf++;
2809 }
2810 else {
2811 if (!is_whitespace(*ibuf)) {
2812 cnt = 0;
2813 }
2814 else {
2815 *ibuf = ' ';
2816 cnt = 1;
2817 }
2818 obuf[i++] = *ibuf++;
2819 }
2820 }
2821 obuf[i--] = '\0';
2822
2823 // Skip trailing whitespace
2824 while (i > 0 && is_whitespace(obuf[i])) {
2825 obuf[i--] = '\0';
2826 }
2827 }
2828
2829 int get_width(size_t max_value)
2830 {
2831 // Returns the field width which will accommodate the
2832 // largest value.
2833 int width = 0;
2834 if (max_value >= 10) {
2835 width = int(log10(static_cast<double>(max_value)));
2836 }
2837 return width + 1;
2838 }
2839
2840 template <typename T, typename INT>
2841 void map_element_vars(size_t loffset, size_t goffset, size_t entity_count, std::vector<T> &values,
2842 std::vector<T> &global_values, INT *part_loc_elem_to_global)
2843 {
2844 // copy values to master element value information
2845 for (size_t j = 0; j < entity_count; j++) {
2846 size_t global_block_pos = part_loc_elem_to_global[(j + loffset)] - goffset;
2847 global_values[global_block_pos] = values[j];
2848 }
2849 }
2850
2851 template <typename T, typename U>
2852 void map_sideset_vars(U & /*unused*/, size_t /*unused*/, std::vector<T> & /*unused*/,
2853 std::vector<T> & /*unused*/)
2854 {
2855 throw std::runtime_error("Internal Error.");
2856 }
2857
2858 template <typename INT>
2859 void map_sideset_vars(Excn::SideSet<INT> &local_set, size_t entity_count,
2860 std::vector<double> &values, std::vector<double> &global_values)
2861 {
2862 // copy values to master nodeset value information
2863 for (size_t j = 0; j < entity_count; j++) {
2864 size_t global_loc = local_set.elemOrderMap[j];
2865 SMART_ASSERT(global_loc < global_values.size());
2866 global_values[global_loc] = values[j];
2867 }
2868 }
2869
2870 template <typename INT>
2871 void map_sideset_vars(Excn::SideSet<INT> &local_set, size_t entity_count,
2872 std::vector<float> &values, std::vector<float> &global_values)
2873 {
2874 // copy values to master nodeset value information
2875 for (size_t j = 0; j < entity_count; j++) {
2876 size_t global_loc = local_set.elemOrderMap[j];
2877 SMART_ASSERT(global_loc < global_values.size());
2878 global_values[global_loc] = values[j];
2879 }
2880 }
2881
2882 template <typename T, typename U>
2883 void map_nodeset_vars(U & /*unused*/, size_t /*unused*/, std::vector<T> & /*unused*/,
2884 std::vector<T> & /*unused*/)
2885 {
2886 throw std::runtime_error("Internal Error.");
2887 }
2888
2889 template <typename INT>
2890 void map_nodeset_vars(Excn::NodeSet<INT> &local_set, size_t entity_count,
2891 std::vector<double> &values, std::vector<double> &global_values)
2892 {
2893 // copy values to master nodeset value information
2894 for (size_t j = 0; j < entity_count; j++) {
2895 size_t global_loc = local_set.nodeOrderMap[j];
2896 SMART_ASSERT(global_loc < global_values.size());
2897 global_values[global_loc] = values[j];
2898 }
2899 }
2900
2901 template <typename INT>
2902 void map_nodeset_vars(Excn::NodeSet<INT> &local_set, size_t entity_count,
2903 std::vector<float> &values, std::vector<float> &global_values)
2904 {
2905 // copy values to master nodeset value information
2906 for (size_t j = 0; j < entity_count; j++) {
2907 size_t global_loc = local_set.nodeOrderMap[j];
2908 SMART_ASSERT(global_loc < global_values.size());
2909 global_values[global_loc] = values[j];
2910 }
2911 }
2912
2913 template <typename T, typename U, typename INT>
2914 int read_write_master_values(Excn::Variables &vars, const Excn::Mesh<INT> &global,
2915 std::vector<U> &global_sets,
2916 std::vector<Excn::Mesh<INT>> &local_mesh,
2917 std::vector<std::vector<U>> &local_sets, std::vector<T> &values,
2918 size_t p, Excn::ExodusFile &id, int time_step, int time_step_out)
2919 {
2920 int error = 0;
2921 int id_out = Excn::ExodusFile::output(); // output file identifier
2922
2923 size_t max_size = 0;
2924 for (size_t b = 0; b < global.count(vars.objectType); b++) {
2925 if (max_size < global_sets[b].entity_count()) {
2926 max_size = global_sets[b].entity_count();
2927 }
2928 }
2929 std::vector<T> master_values(max_size);
2930
2931 // Only needed for element, but haven't cleaned this up yet...
2932 INT *part_loc_elem_to_global = Data(local_mesh[p].localElementToGlobal);
2933
2934 for (int i = 0; i < vars.count(Excn::InOut::IN_); i++) {
2935 if (vars.index_[i] > 0) {
2936 int ivar = vars.index_[i] - 1;
2937
2938 for (size_t b = 0; b < global.count(vars.objectType); b++) {
2939 size_t lb = 0;
2940 for (; lb < global.count(vars.objectType); lb++) {
2941 if (global_sets[b].id == local_sets[p][lb].id) {
2942 break;
2943 }
2944 }
2945 SMART_ASSERT(global_sets[b].id == local_sets[p][lb].id);
2946
2947 if (global_sets[b].truthTable[ivar] && local_sets[p][lb].entity_count() > 0) {
2948
2949 int entity_count = local_sets[p][lb].entity_count();
2950
2951 if (local_sets[p][lb].truthTable[i] > 0) {
2952 error += ex_get_var(id, time_step + 1, exodus_object_type(vars.objectType), i + 1,
2953 local_sets[p][lb].id, entity_count, Data(values));
2954
2955 switch (vars.objectType) {
2956 case Excn::ObjectType::EBLK:
2957 map_element_vars(local_sets[p][lb].offset_, global_sets[b].offset_, entity_count,
2958 values, master_values, part_loc_elem_to_global);
2959 break;
2960
2961 case Excn::ObjectType::SSET:
2962 map_sideset_vars(local_sets[p][lb], entity_count, values, master_values);
2963 break;
2964
2965 case Excn::ObjectType::NSET:
2966 map_nodeset_vars(local_sets[p][lb], entity_count, values, master_values);
2967 break;
2968 default: break;
2969 }
2970 }
2971 ex_put_var(id_out, time_step_out, exodus_object_type(vars.objectType), ivar + 1,
2972 global_sets[b].id, global_sets[b].entity_count(), Data(master_values));
2973 }
2974 }
2975 }
2976 }
2977 return error;
2978 }
2979
2980 template <typename U>
2981 void create_output_truth_table(std::vector<U> &global_sets, Excn::Variables &vars,
2982 std::vector<int> &truth_table)
2983 {
2984 truth_table.resize(global_sets.size() * vars.count(Excn::InOut::OUT_));
2985 for (auto &global_set : global_sets) {
2986 int bout = global_set.position_;
2987 SMART_ASSERT(bout >= 0);
2988 for (int j = 0; j < vars.count(Excn::InOut::OUT_); j++) {
2989 int out_ttable_loc = (bout * vars.count(Excn::InOut::OUT_)) + j;
2990 truth_table[out_ttable_loc] = global_set.truthTable[j];
2991 }
2992 }
2993 }
2994
2995 template <typename INT>
2996 size_t find_max_entity_count(size_t part_count, std::vector<Excn::Mesh<INT>> &local_mesh,
2997 const Excn::Mesh<INT> &global,
2998 std::vector<std::vector<Excn::Block>> &blocks,
2999 std::vector<std::vector<Excn::NodeSet<INT>>> &nodesets,
3000 std::vector<std::vector<Excn::SideSet<INT>>> &sidesets)
3001 {
3002 size_t max_ent = local_mesh[0].count(Excn::ObjectType::NODE);
3003 for (size_t p = 1; p < part_count; p++) {
3004 if ((size_t)local_mesh[p].count(Excn::ObjectType::NODE) > max_ent) {
3005 max_ent = local_mesh[p].count(Excn::ObjectType::NODE);
3006 }
3007 }
3008
3009 for (size_t p = 0; p < part_count; p++) {
3010 for (size_t b = 0; b < global.count(Excn::ObjectType::EBLK); b++) {
3011 if (blocks[p][b].entity_count() > max_ent) {
3012 max_ent = blocks[p][b].entity_count();
3013 }
3014 }
3015 }
3016
3017 // Nodesets...
3018 for (size_t p = 0; p < part_count; p++) {
3019 for (size_t b = 0; b < global.count(Excn::ObjectType::NSET); b++) {
3020 if (nodesets[p][b].entity_count() > max_ent) {
3021 max_ent = nodesets[p][b].entity_count();
3022 }
3023 }
3024 }
3025
3026 // Sidesets...
3027 for (size_t p = 0; p < part_count; p++) {
3028 for (size_t b = 0; b < global.count(Excn::ObjectType::SSET); b++) {
3029 if (sidesets[p][b].entity_count() > max_ent) {
3030 max_ent = sidesets[p][b].entity_count();
3031 }
3032 }
3033 }
3034 return max_ent;
3035 }
3036
3037 void sort_file_times(StringVector &input_files)
3038 {
3039 // Sort files based on minimum timestep time
3040 std::vector<std::pair<double, std::string>> file_time_name;
3041 file_time_name.reserve(input_files.size());
3042 for (auto &filename : input_files) {
3043 float version = 0.0;
3044 int cpu_word_size = sizeof(float);
3045 int io_wrd_size = 0;
3046 int exoid = ex_open(filename.c_str(), EX_READ, &cpu_word_size, &io_wrd_size, &version);
3047 if (exoid < 0) {
3048 fmt::print(stderr, "ERROR: Cannot open file '{}'\n", filename);
3049 exit(EXIT_FAILURE);
3050 }
3051
3052 int nts = ex_inquire_int(exoid, EX_INQ_TIME);
3053 double time = 0.0;
3054 if (nts > 0) {
3055 ex_get_time(exoid, 1, &time);
3056 }
3057 file_time_name.emplace_back(time, filename);
3058 ex_close(exoid);
3059 }
3060
3061 std::sort(file_time_name.begin(), file_time_name.end());
3062 input_files.clear();
3063 input_files.reserve(file_time_name.size());
3064
3065 for (const auto &entry : file_time_name) {
3066 input_files.push_back(entry.second);
3067 }
3068 }
3069
3070 } // namespace