Debian Codegraph @

packages/seacas/applications/cpup/cpup.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 #include <cstdlib>
7 #include <exception>
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "add_to_log.h"
14 #define FMT_DEPRECATED_OSTREAM
15 #include "fmt/ostream.h"
16 #include "fmt/ranges.h"
17 #include "format_time.h"
18 #include "hwm.h"
19 #include "open_file_limit.h"
20 #include "time_stamp.h"
21
22 #include <Ionit_Initializer.h>
23 #include <Ioss_SmartAssert.h>
24 #include <Ioss_SubSystem.h>
25 #include <Ioss_Utils.h>
26 #include <Ioss_use_fmt.h>
27
28 #include <cgns/Iocgns_Utils.h>
29
30 #include "CP_SystemInterface.h"
31 #include "CP_Version.h"
32
33 #ifdef SEACAS_HAVE_MPI
34 #include <mpi.h>
35 #endif
36
37 unsigned int debug_level = 0;
38
39 namespace {
40 std::string tsFormat = "[{:%H:%M:%S}] ";
41
42 using GlobalZgcMap = std::map<std::pair<std::string, std::string>, Ioss::ZoneConnectivity>;
43 using GlobalBcMap = std::map<std::pair<std::string, std::string>, Ioss::BoundaryCondition>;
44 using GlobalBlockMap = std::map<const std::string, const Ioss::StructuredBlock *>;
45 using GlobalIJKMap = std::map<const std::string, Ioss::IJK_t>;
46 using PartVector = std::vector<std::unique_ptr<Ioss::Region>>;
47
48 GlobalZgcMap generate_global_zgc(const PartVector &part_mesh);
49 GlobalBcMap generate_global_bc(const PartVector &part_mesh);
50
51 void info_structuredblock(const Ioss::Region &region);
52 void resolve_offsets(const PartVector &part_mesh, GlobalBlockMap &all_blocks);
53 void update_global_ijk(const PartVector &part_mesh, GlobalIJKMap &global_block);
54 void transfer_nodal_field(const Ioss::StructuredBlock *sb, const std::vector<double> &input,
55 std::vector<double> &output);
56 void transfer_cell_field(const Ioss::StructuredBlock *sb, const std::vector<double> &input,
57 std::vector<double> &output);
58 void transfer_nodal_coordinates(const PartVector &part_mesh, Ioss::Region &output_region,
59 bool minimize_open_files);
60 double transfer_step(const PartVector &part_mesh, Ioss::Region &output_region, int istep,
61 bool minimize_open_files);
62 void union_zgc_range(Ioss::ZoneConnectivity &zgc_i, const Ioss::ZoneConnectivity &zgc_j);
63 void union_bc_range(Ioss::IJK_t &g_beg, Ioss::IJK_t &g_end, const Ioss::IJK_t &l_beg,
64 const Ioss::IJK_t &l_end, const Ioss::IJK_t &offset);
65
66 int get_constant_face(const Ioss::IJK_t &beg, const Ioss::IJK_t &end)
67 {
68 for (int i = 0; i < 3; i++) {
69 if (beg[i] == end[i]) {
70 return (beg[i] == 1) ? i : i + 3;
71 }
72 }
73 return 7;
74 }
75
76 bool is_field_valid(const Cpup::StringVector &variable_list, const std::string &field_name)
77 {
78 if (variable_list.empty() ||
79 (variable_list.size() == 1 && Ioss::Utils::str_equal(variable_list[0], "all"))) {
80 return true;
81 }
82
83 // At this point, the variable_list contains one or more entries
84 // of fields that should be output on combined file. Run through
85 // list and see if `field_name` is in the list.
86 return std::any_of(
87 variable_list.begin(), variable_list.end(),
88 [&field_name](const auto &valid) { return Ioss::Utils::str_equal(valid, field_name); });
89 }
90
91 int verify_timestep_count(const PartVector &part_mesh)
92 {
93 int num_time_steps = part_mesh[0]->get_property("state_count").get_int();
94
95 bool differ = false;
96 for (const auto &part : part_mesh) {
97 int nts = part->get_property("state_count").get_int();
98 if (nts != num_time_steps) {
99 differ = true;
100 }
101 num_time_steps = num_time_steps < nts ? num_time_steps : nts;
102 }
103 if (differ) {
104 fmt::print(stderr,
105 "\nWARNING: The number of time steps is not the same on all input databases.\n"
106 " Using minimum count of {}\n\n",
107 num_time_steps);
108 }
109 else {
110 fmt::print(stderr, "\nNumber of time steps on input databases = {}\n\n", num_time_steps);
111 }
112 return num_time_steps;
113 }
114
115 } // namespace
116
117 template <typename INT> void cpup(Cpup::SystemInterface &interFace, INT dummy);
118
119 int main(int argc, char *argv[])
120 {
121 #ifdef SEACAS_HAVE_MPI
122 MPI_Init(&argc, &argv);
123 #endif
124
125 try {
126 Cpup::SystemInterface::show_version();
127 Ioss::Init::Initializer io;
128
129 Cpup::SystemInterface interFace;
130 bool ok = interFace.parse_options(argc, argv);
131
132 debug_level = interFace.debug();
133
134 if (!ok) {
135 fmt::print(stderr, "\nERROR: Problems parsing command line arguments.\n\n");
136 exit(EXIT_FAILURE);
137 }
138
139 int error = 0;
140 double begin = Ioss::Utils::timer();
141 cpup(interFace, static_cast<int64_t>(0));
142 double end = Ioss::Utils::timer();
143
144 fmt::print(stderr,
145 "\nTotal Execution Time = {:.2f} seconds, Maximum memory = {} MiBytes.\n******* "
146 "END *******\n\n",
147 end - begin,
148 fmt::group_digits((get_hwm_memory_info() + 1024 * 1024 - 1) / (1024 * 1024)));
149
150 add_to_log(argv[0], end - begin);
151
152 #ifdef SEACAS_HAVE_MPI
153 MPI_Finalize();
154 #endif
155
156 return (error);
157 }
158 catch (std::exception &e) {
159 fmt::print(stderr, "ERROR: Standard exception: {}\n", e.what());
160 }
161 exit(EXIT_SUCCESS);
162 }
163
164 template <typename INT> void cpup(Cpup::SystemInterface &interFace, INT /*dummy*/)
165 {
166 auto width = Ioss::Utils::number_width(interFace.processor_count(), false);
167
168 bool minimize_open_files = interFace.minimize_open_files();
169
170 if (!minimize_open_files) {
171 // Query the system to see if the number of files exceeds the system limit and we
172 // need to force use of minimize_open_files...
173 int max_files = open_file_limit() - 1; // We also have an output file.
174 if (interFace.processor_count() > max_files) {
175 minimize_open_files = true;
176 fmt::print("Single file mode... (Max open = {})\n", max_files);
177 }
178 }
179
180 PartVector part_mesh(interFace.processor_count());
181 for (int p = 0; p < interFace.processor_count(); p++) {
182 std::string root_dir = interFace.root_dir();
183 std::string sub_dir = interFace.sub_dir();
184 std::string prepend{};
185
186 if (!root_dir.empty()) {
187 prepend = root_dir + "/";
188 }
189 else if (Ioss::Utils::is_path_absolute(prepend)) {
190 prepend = "";
191 }
192 else {
193 prepend = "./";
194 }
195 if (!sub_dir.empty()) {
196 prepend += sub_dir + "/";
197 }
198
199 prepend += interFace.basename() + "." + interFace.cgns_suffix();
200 auto filename = Ioss::Utils::decode_filename(prepend, p, interFace.processor_count());
201
202 if (debug_level & 1) {
203 fmt::print(stderr, "{} Processor rank {:{}}, file {}\n", time_stamp(tsFormat), p, width,
204 filename);
205 }
206 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create("cgns", filename, Ioss::READ_RESTART,
207 Ioss::ParallelUtils::comm_world());
208 if (dbi == nullptr || !dbi->ok(true)) {
209 std::exit(EXIT_FAILURE);
210 }
211
212 dbi->set_field_separator(1);
213
214 // NOTE: region owns database pointer at this time...
215 std::string name = "CPUP_" + std::to_string(p + 1);
216 part_mesh[p] = std::make_unique<Ioss::Region>(dbi, name);
217
218 if (part_mesh[p]->mesh_type() != Ioss::MeshType::STRUCTURED) {
219 part_mesh[p]->output_summary(std::cerr);
220 fmt::print(stderr,
221 "\nERROR: Only UNSTRUCTURED CGNS file joining is supported at this time.\n");
222 exit(EXIT_FAILURE);
223 }
224
225 if (debug_level & 2) {
226 part_mesh[p]->output_summary(std::cerr);
227 fmt::print(stderr, "\n");
228 }
229 if (minimize_open_files) {
230 part_mesh[p]->get_database()->closeDatabase();
231 }
232 }
233
234 // Each processor may have a different set of zones. This routine
235 // will sync the information such that at end, there is a consistent
236 // set of structuredBlocks defined with the correct local and
237 // global, i,j,k ranges and offsets.
238
239 GlobalBlockMap all_blocks;
240 GlobalIJKMap global_block;
241 for (const auto &part : part_mesh) {
242 const auto &blocks = part->get_structured_blocks();
243 for (const auto &block : blocks) {
244 const auto &name = block->name();
245 all_blocks[name] = block;
246
247 // Build map of unique blocks in the mesh.
248 auto [part_name, proc] = Iocgns::Utils::decompose_name(name, true);
249 global_block[part_name];
250
251 // Set the zgc 'from_decomp' property...
252 for (const auto &zgc : block->m_zoneConnectivity) {
253 const auto [zgc_name, zgc_proc] = Iocgns::Utils::decompose_name(zgc.m_donorName, true);
254 if (zgc_name == part_name) {
255 zgc.m_fromDecomp = true;
256 }
257 }
258 }
259 }
260
261 // Resolve the offsets for each split block based on the decomp-ZGC
262 // with the same block on other processors
263 resolve_offsets(part_mesh, all_blocks);
264
265 // Offsets are now correct. Need to calculate and update the global size of each unique block...
266 update_global_ijk(part_mesh, global_block);
267
268 // Need a consistent set of Boundary Conditions for each zone unioned across the proc-local-zones
269 GlobalBcMap global_bc = generate_global_bc(part_mesh);
270
271 // Need a consistent set of ZGC for each zone unioned across the proc-local-zones...
272 // Skip the ZGC that are "from_decomp"
273 GlobalZgcMap global_zgc = generate_global_zgc(part_mesh);
274
275 // Create output file...
276 Ioss::PropertyManager properties{};
277 properties.add(Ioss::Property("FLUSH_INTERVAL", 0));
278 Ioss::DatabaseIO *dbo =
279 Ioss::IOFactory::create("cgns", interFace.output_filename(), Ioss::WRITE_RESTART,
280 Ioss::ParallelUtils::comm_world(), properties);
281 if (dbo == nullptr || !dbo->ok(true)) {
282 std::exit(EXIT_FAILURE);
283 }
284
285 // NOTE: 'output_region' owns 'dbo' pointer at this time
286 Ioss::Region output_region(dbo, "cpup_output_region");
287 output_region.property_add(Ioss::Property("code_name", qainfo[0]));
288 output_region.property_add(Ioss::Property("code_version", qainfo[1] + ":" + qainfo[2]));
289
290 output_region.begin_mode(Ioss::STATE_DEFINE_MODEL);
291
292 // KLUGE: Remove this...
293 // Doesn't affect the output model at all, so not a big issue, ...
294 auto *nb = new Ioss::NodeBlock(dbo, "nodeblock_1", 1, 3);
295 output_region.add(nb);
296
297 // Create the output structured blocks...
298 for (auto &block_range : global_block) {
299 const auto &block_name = block_range.first;
300 auto *block = new Ioss::StructuredBlock(dbo, block_name, 3, block_range.second);
301 output_region.add(block);
302
303 // Add BC to the block...
304 for (const auto &bc_map : global_bc) {
305 if (bc_map.first.first == block_name) {
306 block->m_boundaryConditions.push_back(bc_map.second);
307 }
308 }
309
310 // Add ZGC to the block...
311 for (const auto &zgc_map : global_zgc) {
312 if (zgc_map.first.first == block_name) {
313 block->m_zoneConnectivity.push_back(zgc_map.second);
314 }
315 }
316 }
317
318 // Copy the sidesets and assemblies from the proc-0 input file to the output file...
319 auto &part = part_mesh[0];
320 const auto &ssets = part->get_sidesets();
321 for (const auto &sset : ssets) {
322 auto *oss = new Ioss::SideSet(*sset);
323 output_region.add(oss);
324 }
325
326 const auto &assems = part->get_assemblies();
327 for (const auto &assem : assems) {
328 auto *oass = new Ioss::Assembly(*assem);
329 output_region.add(oass);
330 }
331
332 if (debug_level & 4) {
333 info_structuredblock(output_region);
334 }
335
336 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
337
338 output_region.begin_mode(Ioss::STATE_MODEL);
339 transfer_nodal_coordinates(part_mesh, output_region, minimize_open_files);
340 output_region.end_mode(Ioss::STATE_MODEL);
341
342 // ******* Transient Data...
343 output_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
344
345 // ... Iterate the output_region structured blocks,
346 // .. Find corresponding structured blocks on part meshes.
347 // .. Add each valid block and node_block field
348 const auto &variable_list = interFace.var_names();
349 if (!(variable_list.size() == 1 && Ioss::Utils::str_equal(variable_list[0], "none") == 0)) {
350 const auto &blocks = output_region.get_structured_blocks();
351 for (const auto &block : blocks) {
352 int64_t num_cell = block->get_property("cell_count").get_int();
353 int64_t num_node = block->get_property("node_count").get_int();
354
355 auto &onb = block->get_node_block();
356
357 // Find all corresponding blocks on the input part meshes...
358 for (const auto &prt : part_mesh) {
359 const auto &pblocks = prt->get_structured_blocks();
360 for (const auto &pblock : pblocks) {
361 const auto &name = pblock->name();
362 auto [part_name, part_proc] = Iocgns::Utils::decompose_name(name, true);
363 if (part_name == block->name()) {
364 Ioss::NameList fields = pblock->field_describe(Ioss::Field::TRANSIENT);
365
366 for (const auto &field_name : fields) {
367 if (is_field_valid(variable_list, field_name)) {
368 Ioss::Field field = pblock->get_field(field_name);
369 if (!block->field_exists(field_name)) {
370 // If the field does not already exist, add it to the output block...
371 field.reset_count(num_cell);
372 block->field_add(field);
373 }
374 }
375 }
376
377 // Now transfer the fields on the embedded node block...
378 const auto &pnb = pblock->get_node_block();
379 fields.clear();
380 pnb.field_describe(Ioss::Field::TRANSIENT, &fields);
381 for (const auto &field_name : fields) {
382 if (is_field_valid(variable_list, field_name)) {
383 Ioss::Field field = pnb.get_field(field_name);
384 if (!onb.field_exists(field_name)) {
385 field.reset_count(num_node);
386 // If the field does not already exist, add it to the output block...
387 onb.field_add(field);
388 }
389 }
390 }
391
392 break; // Should be only a single instance of each block on a part mesh.
393 }
394 }
395 }
396 }
397 }
398
399 output_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
400
401 output_region.begin_mode(Ioss::STATE_TRANSIENT);
402
403 int num_time_steps = verify_timestep_count(part_mesh);
404
405 // Determine if user wants a subset of timesteps transferred to the output file.
406 int ts_min = interFace.step_min();
407 int ts_max = interFace.step_max();
408 int ts_step = interFace.step_interval();
409
410 if (ts_min < 0) {
411 ts_min = num_time_steps + 1 + ts_min;
412 }
413 if (ts_max < 0) {
414 ts_max = num_time_steps + 1 + ts_max;
415 }
416
417 // Time steps for output file
418 int time_step_out = 0;
419
420 ts_max = ts_max < num_time_steps ? ts_max : num_time_steps;
421 if (ts_min <= ts_max) {
422 fmt::print(stderr, "\tTransferring step {} to step {} by {}\n", ts_min, ts_max, ts_step);
423 }
424
425 // Determine how many steps will be written...
426 int output_steps = (ts_max - ts_min) / ts_step + 1;
427
428 double start_time = Ioss::Utils::timer();
429 double cur_time = start_time;
430 for (int time_step = ts_min; time_step <= ts_max; time_step += ts_step) {
431 time_step_out++;
432 double time_val = transfer_step(part_mesh, output_region, time_step, minimize_open_files);
433
434 double time_per_step = Ioss::Utils::timer() - cur_time;
435 cur_time = Ioss::Utils::timer();
436 double elapsed = cur_time - start_time;
437 double avg_time_per_step = elapsed / time_step_out;
438 double percentage_done = (time_step_out * 100.0) / output_steps;
439 double estimated_remaining = avg_time_per_step * (output_steps - time_step_out);
440 if (debug_level & 1) {
441 fmt::print(stderr,
442 "{} \tWrote step {:6}, time {:8.4e}\t[{:5.1f}%, Elapsed={}, \tETA={}, \tTPS={}]\n",
443 time_stamp(tsFormat), fmt::group_digits(time_step), time_val, percentage_done,
444 format_time(elapsed), format_time(estimated_remaining),
445 format_time(time_per_step));
446 }
447 else {
448 fmt::print(stderr,
449 "\tWrote step {:6}, time {:8.4e}\t[{:5.1f}%, Elapsed={}, ETA={}, TPS={}] \r",
450 fmt::group_digits(time_step), time_val, percentage_done, format_time(elapsed),
451 format_time(estimated_remaining), format_time(time_per_step));
452 }
453 }
454 output_region.end_mode(Ioss::STATE_TRANSIENT);
455
456 fmt::print(stderr, "\n\n********************* OUTPUT DATABASE ********************\n");
457 output_region.output_summary(std::cerr);
458 }
459
460 namespace {
461 GlobalZgcMap generate_global_zgc(const PartVector &part_mesh)
462 {
463 GlobalZgcMap global_zgc;
464 for (const auto &part : part_mesh) {
465 const auto &blocks = part->get_structured_blocks();
466 for (const auto &block : blocks) {
467 const auto [part_name, proc] = Iocgns::Utils::decompose_name(block->name(), true);
468
469 for (const auto &zgc : block->m_zoneConnectivity) {
470 if (!zgc.m_fromDecomp) {
471 auto &gzgc = global_zgc[std::make_pair(part_name, zgc.m_connectionName)];
472 if (gzgc.m_connectionName.empty()) {
473 // First time this ZGC has been found. Copy from the per-proc instance and update...
474 gzgc.m_connectionName = zgc.m_connectionName;
475 gzgc.m_donorName = Iocgns::Utils::decompose_name(zgc.m_donorName, true).first;
476 gzgc.m_transform = zgc.m_transform;
477 }
478
479 // Create a temporary zgc; adjust its ranges to "global" by adding the offsets
480 // and then union if with the global instance...
481 Ioss::IJK_t own_off = block->get_ijk_offset();
482
483 auto tmp_zgc{zgc};
484 tmp_zgc.m_ownerRangeBeg[0] += own_off[0];
485 tmp_zgc.m_ownerRangeBeg[1] += own_off[1];
486 tmp_zgc.m_ownerRangeBeg[2] += own_off[2];
487 tmp_zgc.m_ownerRangeEnd[0] += own_off[0];
488 tmp_zgc.m_ownerRangeEnd[1] += own_off[1];
489 tmp_zgc.m_ownerRangeEnd[2] += own_off[2];
490
491 // Now find the donor block...
492 auto donor = Iocgns::Utils::decompose_name(zgc.m_donorName, true);
493 auto *donor_block = part_mesh[donor.second]->get_structured_block(zgc.m_donorName);
494 SMART_ASSERT(donor_block != nullptr);
495 Ioss::IJK_t don_off = donor_block->get_ijk_offset();
496
497 tmp_zgc.m_donorRangeBeg[0] += don_off[0];
498 tmp_zgc.m_donorRangeBeg[1] += don_off[1];
499 tmp_zgc.m_donorRangeBeg[2] += don_off[2];
500 tmp_zgc.m_donorRangeEnd[0] += don_off[0];
501 tmp_zgc.m_donorRangeEnd[1] += don_off[1];
502 tmp_zgc.m_donorRangeEnd[2] += don_off[2];
503
504 union_zgc_range(gzgc, tmp_zgc);
505 }
506 }
507 }
508 }
509 return global_zgc;
510 }
511
512 GlobalBcMap generate_global_bc(const PartVector &part_mesh)
513 {
514 GlobalBcMap global_bc;
515 for (const auto &part : part_mesh) {
516 const auto &blocks = part->get_structured_blocks();
517 for (const auto &block : blocks) {
518 Ioss::IJK_t offset = block->get_ijk_offset();
519 const auto [part_name, proc] = Iocgns::Utils::decompose_name(block->name(), true);
520 const auto &sb_bc = block->m_boundaryConditions;
521 for (const auto &bc : sb_bc) {
522 auto &gbc = global_bc[std::make_pair(part_name, bc.m_bcName)];
523 if (gbc.m_bcName.empty()) {
524 gbc.m_bcName = bc.m_bcName;
525 gbc.m_famName = bc.m_famName;
526 }
527 union_bc_range(gbc.m_rangeBeg, gbc.m_rangeEnd, bc.m_rangeBeg, bc.m_rangeEnd, offset);
528 SMART_ASSERT(gbc.which_face() == bc.which_face());
529 }
530 }
531 }
532 return global_bc;
533 }
534
535 double transfer_step(const PartVector &part_mesh, Ioss::Region &output_region, int istep,
536 bool minimize_open_files)
537 {
538 double time = part_mesh[0]->get_state_time(istep);
539 int ostep = output_region.add_state(time);
540
541 output_region.begin_state(ostep);
542
543 for (const auto &part : part_mesh) {
544 part->begin_state(istep);
545 }
546
547 const auto &blocks = output_region.get_structured_blocks();
548 for (const auto &block : blocks) {
549 int64_t num_cell = block->get_property("cell_count").get_int();
550 std::vector<double> output(num_cell);
551 std::vector<double> input;
552
553 Ioss::NameList fields = block->field_describe(Ioss::Field::TRANSIENT);
554
555 // Not sure if this is the best ordering of loops, but it minimizes the
556 // amount of data gathered at one time at the cost of multiple iterations
557 // through the block-finding loop...
558 for (const auto &field_name : fields) {
559
560 // Find all corresponding blocks on the input part meshes...
561 for (const auto &part : part_mesh) {
562 const auto &pblocks = part->get_structured_blocks();
563 for (const auto &pblock : pblocks) {
564 const auto &name = pblock->name();
565 const auto [part_name, proc] = Iocgns::Utils::decompose_name(name, true);
566 if (part_name == block->name()) {
567 if (pblock->field_exists(field_name)) {
568 pblock->get_field_data(field_name, input);
569 transfer_cell_field(pblock, input, output);
570 }
571 break; // Should be only a single instance of each block on a part mesh.
572 }
573 }
574 if (minimize_open_files) {
575 part->get_database()->closeDatabase();
576 }
577 }
578 block->put_field_data(field_name, output);
579 }
580 }
581
582 // Now do the fields on the embedded node block...
583 for (const auto &block : blocks) {
584 int64_t num_node = block->get_property("node_count").get_int();
585 auto &onb = block->get_node_block();
586
587 std::vector<double> output(num_node);
588 std::vector<double> input;
589
590 Ioss::NameList fields = onb.field_describe(Ioss::Field::TRANSIENT);
591
592 // Not sure if this is the best ordering of loops, but it minimizes the
593 // amount of data gathered at one time at the cost of multiple iterations
594 // through the block-finding loop...
595 for (const auto &field_name : fields) {
596
597 // Find all corresponding blocks on the input part meshes...
598 for (const auto &part : part_mesh) {
599 const auto &pblocks = part->get_structured_blocks();
600 for (const auto &pblock : pblocks) {
601 const auto &name = pblock->name();
602 const auto [part_name, proc] = Iocgns::Utils::decompose_name(name, true);
603 if (part_name == block->name()) {
604 auto &inb = pblock->get_node_block();
605 if (inb.field_exists(field_name)) {
606 inb.get_field_data(field_name, input);
607 transfer_nodal_field(pblock, input, output);
608 }
609 break; // Should be only a single instance of each block on a part mesh.
610 }
611 }
612 if (minimize_open_files) {
613 part->get_database()->closeDatabase();
614 }
615 }
616 onb.put_field_data(field_name, output);
617 }
618 }
619
620 output_region.end_state(ostep);
621 for (const auto &part : part_mesh) {
622 part->end_state(istep);
623 }
624 return time;
625 }
626
627 void resolve_offsets(const PartVector &part_mesh, GlobalBlockMap &all_blocks)
628 {
629 bool change_made;
630 do {
631 change_made = false;
632 for (const auto &part : part_mesh) {
633 const auto &blocks = part->get_structured_blocks();
634 for (const auto &block : blocks) {
635 for (const auto &zgc : block->m_zoneConnectivity) {
636 if (zgc.is_from_decomp()) {
637 auto plane = get_constant_face(zgc.m_ownerRangeBeg, zgc.m_ownerRangeEnd);
638 if (plane < 3) {
639 // This zone connects to another zone "below" it.
640 // Find the connecting zone and adjust the correct offset
641 const auto *donor = all_blocks[zgc.m_donorName];
642 auto offset = donor->get_ijk_offset();
643 auto range = donor->get_ijk_local();
644 auto my_offset = block->get_ijk_offset();
645 if (my_offset[plane] != range[plane] + offset[plane]) {
646 block->set_ijk_offset(plane, range[plane] + offset[plane]);
647 change_made = true;
648 }
649 }
650 }
651 }
652 }
653 }
654 } while (change_made);
655 }
656
657 void update_global_ijk(const PartVector &part_mesh, GlobalIJKMap &global_block)
658 {
659 for (const auto &part : part_mesh) {
660 const auto &blocks = part->get_structured_blocks();
661 for (const auto &block : blocks) {
662 auto ijk_o = block->get_ijk_offset();
663 auto ijk_g = block->get_ijk_global();
664 const auto [part_name, proc] = Iocgns::Utils::decompose_name(block->name(), true);
665 auto &cur_global = global_block[part_name];
666 cur_global[0] = std::max(ijk_o[0] + ijk_g[0], cur_global[0]);
667 cur_global[1] = std::max(ijk_o[1] + ijk_g[1], cur_global[1]);
668 cur_global[2] = std::max(ijk_o[2] + ijk_g[2], cur_global[2]);
669 }
670 }
671
672 for (const auto &part : part_mesh) {
673 const auto &blocks = part->get_structured_blocks();
674 for (const auto &block : blocks) {
675 const auto [part_name, proc] = Iocgns::Utils::decompose_name(block->name(), true);
676 auto &cur_global = global_block[part_name];
677 block->set_ijk_global(cur_global);
678 }
679 if (debug_level & 4) {
680 info_structuredblock(*part);
681 }
682 }
683 }
684
685 void info_structuredblock(const Ioss::Region &region)
686 {
687 const Ioss::StructuredBlockContainer &sbs = region.get_structured_blocks();
688 for (const auto &sb : sbs) {
689
690 auto ijk_global = sb->get_ijk_global();
691 fmt::print(stderr, "\n{} '{}' {}", sb->type_string(), sb->name(), fmt::join(ijk_global, "x"));
692
693 auto ijk_offset = sb->get_ijk_offset();
694 auto ijk_local = sb->get_ijk_local();
695 fmt::print(stderr, " [{}, Offset = {}] ", fmt::join(ijk_local, "x"),
696 fmt::join(ijk_offset, ", "));
697
698 int64_t num_cell = sb->get_property("cell_count").get_int();
699 int64_t num_node = sb->get_property("node_count").get_int();
700 fmt::print(stderr, " {:14} cells, {:14} nodes ", fmt::group_digits(num_cell),
701 fmt::group_digits(num_node));
702
703 #if defined(__NVCC__)
704 #define CONST
705 #else
706 #define CONST const
707 #endif
708 if (!sb->m_zoneConnectivity.empty()) {
709 fmt::print(stderr, "\n\tConnectivity with other blocks:\n");
710 for (CONST auto &zgc : sb->m_zoneConnectivity) {
711 fmt::print(stderr, "{}\n", zgc);
712 }
713 }
714
715 if (!sb->m_boundaryConditions.empty()) {
716 fmt::print(stderr, "\tBoundary Conditions:\n");
717 for (CONST auto &bc : sb->m_boundaryConditions) {
718 fmt::print(stderr, "{}\n", bc);
719 }
720 }
721 #undef CONST
722 }
723 }
724
725 void transfer_nodal_field(const Ioss::StructuredBlock *sb, const std::vector<double> &input,
726 std::vector<double> &output)
727 {
728 // Get the IJK subset that this nodeblock covers...
729 auto ijkg = sb->get_ijk_global();
730 auto ijkl = sb->get_ijk_local();
731
732 // Get the IJK offset...
733 auto offset = sb->get_ijk_offset();
734
735 SMART_ASSERT(input.size() == (size_t)(ijkl[0] + 1) * (ijkl[1] + 1) * (ijkl[2] + 1))
736 (input.size())(ijkl[0])(ijkl[1])(ijkl[2]);
737 SMART_ASSERT(output.size() == (size_t)(ijkg[0] + 1) * (ijkg[1] + 1) * (ijkg[2] + 1))
738 (output.size())(ijkg[0])(ijkg[1])(ijkg[2]);
739
740 size_t idx = 0;
741 for (int k = offset[2]; k < offset[2] + ijkl[2] + 1; k++) {
742 for (int j = offset[1]; j < offset[1] + ijkl[1] + 1; j++) {
743 for (int i = offset[0]; i < offset[0] + ijkl[0] + 1; i++) {
744 size_t oidx = i + (ijkg[0] + 1) * j + (ijkg[0] + 1) * (ijkg[1] + 1) * k;
745 output[oidx] = input[idx++];
746 }
747 }
748 }
749 }
750
751 void transfer_cell_field(const Ioss::StructuredBlock *sb, const std::vector<double> &input,
752 std::vector<double> &output)
753 {
754 // Get the IJK subset that this structured covers...
755 auto ijkg = sb->get_ijk_global();
756 auto ijkl = sb->get_ijk_local();
757
758 // Get the IJK offset...
759 auto offset = sb->get_ijk_offset();
760
761 SMART_ASSERT(input.size() == (size_t)ijkl[0] * ijkl[1] * ijkl[2])
762 (input.size())(ijkl[0])(ijkl[1])(ijkl[2]);
763 SMART_ASSERT(output.size() == (size_t)ijkg[0] * ijkg[1] * ijkg[2])
764 (output.size())(ijkg[0])(ijkg[1])(ijkg[2]);
765
766 size_t idx = 0;
767 for (int k = offset[2]; k < offset[2] + ijkl[2]; k++) {
768 for (int j = offset[1]; j < offset[1] + ijkl[1]; j++) {
769 for (int i = offset[0]; i < offset[0] + ijkl[0]; i++) {
770 size_t oidx = i + ijkg[0] * j + ijkg[0] * ijkg[1] * k;
771 output[oidx] = input[idx++];
772 }
773 }
774 }
775 }
776
777 void transfer_nodal_coordinates(const PartVector &part_mesh, Ioss::Region &output_region,
778 bool minimize_open_files)
779 {
780 // This implementation results in having to iterate over the part
781 // mesh 3 times -- once for each coordinate axis, but minimizes
782 // the memory requirements. Will monitor whether this results in
783 // excessive computation time, but does permit larger models to be
784 // processed.
785
786 std::array<std::string, 3> fields{"mesh_model_coordinates_x", "mesh_model_coordinates_y",
787 "mesh_model_coordinates_z"};
788
789 const auto &blocks = output_region.get_structured_blocks();
790 for (const auto &block : blocks) {
791 // Get size of node_block...
792 auto &onb = block->get_node_block();
793 size_t num_coord = onb.entity_count();
794 std::vector<double> coord(num_coord);
795 for (int dim = 0; dim < 3; dim++) {
796
797 // Find all corresponding blocks on the input part meshes...
798 for (const auto &part : part_mesh) {
799 const auto &pblocks = part->get_structured_blocks();
800 for (const auto &pblock : pblocks) {
801 const auto &name = pblock->name();
802 const auto [part_name, proc] = Iocgns::Utils::decompose_name(name, true);
803 if (part_name == block->name()) {
804 std::vector<double> lcoord;
805 pblock->get_field_data(fields[dim], lcoord);
806 transfer_nodal_field(pblock, lcoord, coord);
807 break; // Should be only a single instance of each block on a part mesh.
808 }
809 }
810 if (minimize_open_files) {
811 part->get_database()->closeDatabase();
812 }
813 }
814 block->put_field_data(fields[dim], coord);
815 }
816 }
817 }
818
819 void union_bc_range(Ioss::IJK_t &g_beg, Ioss::IJK_t &g_end, const Ioss::IJK_t &l_beg,
820 const Ioss::IJK_t &l_end, const Ioss::IJK_t &offset)
821 {
822 for (int i = 0; i < 3; i++) {
823 g_beg[i] = g_beg[i] == 0 ? l_beg[i] + offset[i] : std::min(g_beg[i], l_beg[i] + offset[i]);
824 g_end[i] = std::max(g_end[i], l_end[i] + offset[i]);
825 }
826 }
827
828 void union_zgc_range(Ioss::ZoneConnectivity &zgc_i, const Ioss::ZoneConnectivity &zgc_j)
829 {
830 assert(zgc_i.m_transform == zgc_j.m_transform);
831 if (zgc_i.m_ownerRangeBeg[0] == 0 && zgc_i.m_ownerRangeBeg[1] == 0 &&
832 zgc_i.m_ownerRangeBeg[2] == 0) {
833 // This is a newly-created zgc that hasn't been unioned with anything yet.
834 zgc_i.m_ownerRangeBeg = zgc_j.m_ownerRangeBeg;
835 zgc_i.m_ownerRangeEnd = zgc_j.m_ownerRangeEnd;
836 zgc_i.m_donorRangeBeg = zgc_j.m_donorRangeBeg;
837 zgc_i.m_donorRangeEnd = zgc_j.m_donorRangeEnd;
838 }
839 else {
840 for (int i = 0; i < 3; i++) {
841 if (zgc_i.m_ownerRangeBeg[i] <= zgc_i.m_ownerRangeEnd[i]) {
842 zgc_i.m_ownerRangeBeg[i] = std::min(zgc_i.m_ownerRangeBeg[i], zgc_j.m_ownerRangeBeg[i]);
843 zgc_i.m_ownerRangeEnd[i] = std::max(zgc_i.m_ownerRangeEnd[i], zgc_j.m_ownerRangeEnd[i]);
844 }
845 else {
846 zgc_i.m_ownerRangeBeg[i] = std::max(zgc_i.m_ownerRangeBeg[i], zgc_j.m_ownerRangeBeg[i]);
847 zgc_i.m_ownerRangeEnd[i] = std::min(zgc_i.m_ownerRangeEnd[i], zgc_j.m_ownerRangeEnd[i]);
848 }
849
850 if (zgc_i.m_donorRangeBeg[i] <= zgc_i.m_donorRangeEnd[i]) {
851 zgc_i.m_donorRangeBeg[i] = std::min(zgc_i.m_donorRangeBeg[i], zgc_j.m_donorRangeBeg[i]);
852 zgc_i.m_donorRangeEnd[i] = std::max(zgc_i.m_donorRangeEnd[i], zgc_j.m_donorRangeEnd[i]);
853 }
854 else {
855 zgc_i.m_donorRangeBeg[i] = std::max(zgc_i.m_donorRangeBeg[i], zgc_j.m_donorRangeBeg[i]);
856 zgc_i.m_donorRangeEnd[i] = std::min(zgc_i.m_donorRangeEnd[i], zgc_j.m_donorRangeEnd[i]);
857 }
858 }
859 }
860 }
861 } // namespace