Debian Codegraph @

packages/seacas/applications/ejoin/EJoin.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 <cctype>
8 #include <cfloat>
9 #include <cmath>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <ctime>
14 #include <exception>
15 #include <iterator>
16 #include <limits>
17 #include <map>
18 #include <numeric>
19 #include <set>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23
24 #include "add_to_log.h"
25 #include "fmt/ostream.h"
26 #include "time_stamp.h"
27
28 #include <exodusII.h>
29
30 #include <Ionit_Initializer.h>
31 #include <Ioss_SmartAssert.h>
32 #include <Ioss_SubSystem.h>
33 #include <Ioss_Transform.h>
34 #include <Ioss_Utils.h>
35
36 #include "EJ_CodeTypes.h"
37 #include "EJ_SystemInterface.h"
38 #include "EJ_Version.h"
39 #include "EJ_mapping.h"
40 #include "EJ_match_xyz.h"
41 #include "EJ_vector3d.h"
42
43 #ifdef SEACAS_HAVE_MPI
44 #include <mpi.h>
45 #endif
46
47 namespace {
48 std::string tsFormat = "[%H:%M:%S] ";
49 unsigned int debug_level = 0;
50
51 bool valid_variable(const std::string &variable, size_t id, const StringIdVector &variable_list);
52 bool check_variable_mismatch(const std::string &type, const StringIdVector &variable_list,
53 const Ioss::NameList &fields);
54
55 bool define_global_fields(Ioss::Region &output_region, RegionVector &part_mesh,
56 const StringIdVector &variable_list);
57 bool define_nodal_fields(Ioss::Region &output_region, RegionVector &part_mesh,
58 const StringIdVector &variable_list, SystemInterface &interFace);
59 bool define_element_fields(Ioss::Region &output_region, RegionVector &part_mesh,
60 const StringIdVector &variable_list);
61 bool define_nset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
62 const StringIdVector &variable_list);
63 bool define_sset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
64 const StringIdVector &variable_list);
65 void define_nodal_nodeset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
66 const StringIdVector &variable_list, SystemInterface &interFace);
67
68 template <typename INT>
69 void output_nodeblock(Ioss::Region &output_region, RegionVector &part_mesh,
70 const std::vector<INT> &local_node_map, std::vector<INT> &global_node_map);
71 template <typename INT>
72 void output_elementblock(Ioss::Region &output_region, RegionVector &part_mesh,
73 const std::vector<INT> &local_node_map,
74 const std::vector<INT> &local_element_map, bool ignore_element_ids);
75 template <typename INT>
76 void output_nodeset(Ioss::Region &output_region, RegionVector &part_mesh,
77 const std::vector<INT> &local_node_map);
78 template <typename INT>
79 void output_sideset(Ioss::Region &output_region, RegionVector &part_mesh,
80 const std::vector<INT> &local_element_map);
81 template <typename INT>
82 void output_nodal_nodeset(Ioss::Region &output_region, RegionVector &part_mesh,
83 SystemInterface &interFace, const std::vector<INT> &local_node_map);
84 template <typename INT>
85 void output_transient_state(Ioss::Region &output_region, RegionVector &part_mesh, double time,
86 const std::vector<INT> &local_node_map, SystemInterface &interFace);
87 void process_nset_omissions(RegionVector &part_mesh, const Omissions &omit);
88 void process_sset_omissions(RegionVector &part_mesh, const Omissions &omit);
89 void process_assembly_omissions(RegionVector &part_mesh, const Omissions &omit);
90
91 int count_omissions(Ioss::Region *region)
92 {
93 int omitted = 0;
94 const auto &blocks = region->get_element_blocks();
95 for (const auto &block : blocks) {
96 if (block->property_exists(std::string("omitted"))) {
97 omitted++;
98 }
99 }
100 return omitted;
101 }
102
103 template <typename T> bool approx_equal(T v1, T v2, T offset)
104 {
105 #if 1
106 static const T tolerance = 100.0f * std::numeric_limits<float>::epsilon();
107 double d1 = std::fabs(v1 - v2);
108 double d2 = std::fabs((v1 - offset) + (v2 - offset)) * tolerance;
109 return d1 <= d2;
110 #else
111 return (float)v1 == (float)v2;
112 #endif
113 }
114
115 using EntityIdSet = std::set<std::pair<Ioss::EntityType, int64_t>>;
116 EntityIdSet id_set;
117
118 void set_id(Ioss::GroupingEntity *old_ge, Ioss::GroupingEntity *new_ge)
119 {
120 if (old_ge->property_exists("id")) {
121 int64_t id = old_ge->get_property("id").get_int();
122 bool succeed = id_set.insert(std::make_pair(new_ge->type(), id)).second;
123 if (succeed) {
124 // Add id as the property "id" to ge
125 new_ge->property_add(Ioss::Property("id", id));
126 }
127 }
128 }
129
130 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region,
131 bool create_assemblies, bool debug);
132 void transfer_assembly(const Ioss::Region &region, Ioss::Region &output_region, bool debug);
133 void transfer_nodesets(Ioss::Region &region, Ioss::Region &output_region, bool debug);
134 void transfer_sidesets(Ioss::Region &region, Ioss::Region &output_region, bool debug);
135 void create_nodal_nodeset(Ioss::Region &region, Ioss::Region &output_region, bool debug);
136 void transfer_fields(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
137 Ioss::Field::RoleType role, const std::string &prefix = "");
138
139 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
140 Ioss::Field::RoleType role, const std::string &prefix = "",
141 bool transfer_connectivity = true);
142
143 void transfer_field_data_internal(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
144 const std::string &field_name);
145
146 // Used when combining two or more input set into a single output set (WIP)
147 // May reequire other operations to completely combine the entities...
148 // This only handles the entity count.
149 void add_to_entity_count(Ioss::GroupingEntity *ge, int64_t entity_count_increment)
150 {
151 auto ec_property = ge->get_property("entity_count");
152 auto old_count = ec_property.get_int();
153
154 auto origin = ec_property.get_origin();
155 ge->property_erase("entity_count");
156 ge->property_add(Ioss::Property("entity_count", entity_count_increment + old_count, origin));
157
158 auto field_names = ge->field_describe();
159 for (const auto &field_name : field_names) {
160 const auto &field_ref = ge->get_fieldref(field_name);
161 const_cast<Ioss::Field &>(field_ref).reset_count(entity_count_increment + old_count);
162 }
163 }
164 } // namespace
165
166 template <typename INT>
167 double ejoin(SystemInterface &interFace, std::vector<Ioss::Region *> &part_mesh, INT dummy);
168
169 int main(int argc, char *argv[])
170 {
171 #ifdef SEACAS_HAVE_MPI
172 MPI_Init(&argc, &argv);
173 #endif
174
175 try {
176 SystemInterface::show_version();
177 Ioss::Init::Initializer io;
178
179 SystemInterface interFace;
180 bool ok = interFace.parse_options(argc, argv);
181
182 if (!ok) {
183 fmt::print(stderr, "\nERROR: Problems parsing command line arguments.\n\n");
184 exit(EXIT_FAILURE);
185 }
186
187 debug_level = interFace.debug();
188
189 if ((debug_level & 64) != 0U) {
190 ex_opts(EX_VERBOSE | EX_DEBUG);
191 }
192 else {
193 ex_opts(0);
194 }
195
196 int error = 0;
197
198 int int_byte_size = 4;
199 if (interFace.ints64bit()) {
200 int_byte_size = 8;
201 }
202
203 const Omissions &omissions = interFace.block_omissions();
204 const Omissions &inclusions = interFace.block_inclusions();
205 std::vector<Ioss::Region *> part_mesh(interFace.inputFiles_.size());
206 std::vector<Ioss::DatabaseIO *> dbi(interFace.inputFiles_.size());
207 for (size_t p = 0; p < interFace.inputFiles_.size(); p++) {
208 dbi[p] = Ioss::IOFactory::create("exodusII", interFace.inputFiles_[p], Ioss::READ_RESTART,
209 Ioss::ParallelUtils::comm_world());
210 if (dbi[p] == nullptr || !dbi[p]->ok(true)) {
211 std::exit(EXIT_FAILURE);
212 }
213
214 if (dbi[p]->int_byte_size_api() > int_byte_size) {
215 int_byte_size = dbi[p]->int_byte_size_api();
216 }
217 }
218
219 for (size_t p = 0; p < interFace.inputFiles_.size(); p++) {
220 dbi[p]->set_surface_split_type(Ioss::SPLIT_BY_DONT_SPLIT);
221
222 if (int_byte_size == 8) {
223 dbi[p]->set_int_byte_size_api(Ioss::USE_INT64_API);
224 }
225
226 if (interFace.disable_field_recognition()) {
227 dbi[p]->set_field_separator(1);
228 }
229
230 if (!omissions[p].empty() || !inclusions[p].empty()) {
231 dbi[p]->set_block_omissions(omissions[p], inclusions[p]);
232 }
233
234 // Generate a name for the region based on the part number...
235 std::string prefix = interFace.block_prefix();
236 std::string name = prefix + std::to_string(p + 1);
237 // NOTE: region owns database pointer at this time...
238 part_mesh[p] = new Ioss::Region(dbi[p], name);
239
240 int omission_count = count_omissions(part_mesh[p]);
241 part_mesh[p]->property_add(Ioss::Property("block_omission_count", omission_count));
242
243 vector3d offset = interFace.offset();
244 if (p > 0 && (offset.x != 0.0 || offset.y != 0.0 || offset.z != 0.0)) {
245 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
246 Ioss::Field coord = nb->get_field("mesh_model_coordinates");
247 auto *transform = Ioss::Transform::create("offset3D");
248 assert(transform != nullptr);
249 std::vector<double> values(3);
250 values[0] = offset.x * p;
251 values[1] = offset.y * p;
252 values[2] = offset.z * p;
253 transform->set_properties("offset", values);
254 coord.add_transform(transform);
255 nb->field_erase("mesh_model_coordinates");
256 nb->field_add(coord);
257 }
258 }
259
260 process_nset_omissions(part_mesh, interFace.nset_omissions());
261 process_sset_omissions(part_mesh, interFace.sset_omissions());
262 process_assembly_omissions(part_mesh, interFace.assembly_omissions());
263
264 double time = 0.0;
265
266 if (int_byte_size == 4) {
267 time = ejoin(interFace, part_mesh, 0);
268 }
269 else {
270 time = ejoin(interFace, part_mesh, static_cast<int64_t>(0));
271 }
272
273 for (auto &pm : part_mesh) {
274 delete pm;
275 }
276
277 add_to_log(argv[0], time);
278
279 #ifdef SEACAS_HAVE_MPI
280 MPI_Comm parent_comm;
281 MPI_Comm_get_parent(&parent_comm);
282 if (parent_comm != MPI_COMM_NULL) {
283 MPI_Barrier(parent_comm);
284 }
285 MPI_Finalize();
286 #endif
287
288 return error;
289 }
290 catch (std::exception &e) {
291 fmt::print(stderr, "ERROR: Standard exception: {}\n", e.what());
292 }
293 }
294
295 template <typename INT>
296 double ejoin(SystemInterface &interFace, std::vector<Ioss::Region *> &part_mesh, INT /*dummy*/)
297 {
298 double begin = Ioss::Utils::timer();
299 size_t part_count = interFace.inputFiles_.size();
300 SMART_ASSERT(part_count == part_mesh.size());
301
302 Ioss::PropertyManager properties{};
303 if (sizeof(INT) == 8) {
304 properties.add(Ioss::Property("INTEGER_SIZE_DB", 8));
305 properties.add(Ioss::Property("INTEGER_SIZE_API", 8));
306 }
307
308 if (interFace.use_netcdf4()) {
309 properties.add(Ioss::Property("FILE_TYPE", "netcdf4"));
310 }
311
312 if (interFace.compression_level() > 0 || interFace.szip() || interFace.quantize() ||
313 interFace.zlib() || interFace.zstd() || interFace.bz2()) {
314 properties.add(Ioss::Property("FILE_TYPE", "netcdf4"));
315 properties.add(Ioss::Property("COMPRESSION_LEVEL", interFace.compression_level()));
316 properties.add(Ioss::Property("COMPRESSION_SHUFFLE", 1));
317
318 if (interFace.zlib()) {
319 properties.add(Ioss::Property("COMPRESSION_METHOD", "zlib"));
320 }
321 else if (interFace.szip()) {
322 properties.add(Ioss::Property("COMPRESSION_METHOD", "szip"));
323 }
324 else if (interFace.zstd()) {
325 properties.add(Ioss::Property("COMPRESSION_METHOD", "zstd"));
326 }
327 else if (interFace.bz2()) {
328 properties.add(Ioss::Property("COMPRESSION_METHOD", "bzip2"));
329 }
330
331 if (interFace.quantize()) {
332 properties.add(Ioss::Property("COMPRESSION_QUANTIZE_NSD", interFace.quantize_nsd()));
333 }
334 }
335
336 // Get maximum length of names used on any of the input files...
337 int max_name_length = 32;
338 for (const auto *mesh : part_mesh) {
339 int max_name_used = mesh->get_database()->maximum_symbol_length();
340 max_name_length = std::max(max_name_length, max_name_used);
341 }
342 properties.add(Ioss::Property("MAXIMUM_NAME_LENGTH", max_name_length));
343
344 properties.add(Ioss::Property("FLUSH_INTERVAL", 0));
345 Ioss::DatabaseIO *dbo =
346 Ioss::IOFactory::create("exodusII", interFace.outputName_, Ioss::WRITE_RESTART,
347 Ioss::ParallelUtils::comm_world(), properties);
348 if (dbo == nullptr || !dbo->ok(true)) {
349 std::exit(EXIT_FAILURE);
350 }
351
352 // NOTE: 'output_region' owns 'dbo' pointer at this time
353 Ioss::Region output_region(dbo, "ejoin_output_region");
354
355 output_region.begin_mode(Ioss::STATE_DEFINE_MODEL);
356
357 output_region.property_add(Ioss::Property("code_name", qainfo[0]));
358 output_region.property_add(Ioss::Property("code_version", qainfo[2]));
359
360 if (debug_level & 1) {
361 fmt::print(stderr, "{}", time_stamp(tsFormat));
362 }
363
364 INT node_offset = 0;
365 INT element_offset = 0;
366 for (auto &pm : part_mesh) {
367 pm->property_add(Ioss::Property("node_offset", node_offset));
368 pm->property_add(Ioss::Property("element_offset", element_offset));
369 INT local_node_count = pm->get_property("node_count").get_int();
370 INT local_elem_count = pm->get_property("element_count").get_int();
371 node_offset += local_node_count;
372 element_offset += local_elem_count;
373 }
374
375 INT node_count = node_offset; // Sum of nodes in part meshes.
376 std::vector<INT> local_node_map(node_count);
377 std::vector<INT> global_node_map;
378
379 // This is the map from local element position to global element
380 // position (0-based). If there are no element block omissions, then
381 // the map is simply [0..number_elements). If there are omissions,
382 // Then local_element_map[j] will be -1 for an omitted element.
383 // If not omitted, local_element_map[part_offset+j] gives the global
384 // position of local element j in the current part.
385 std::vector<INT> local_element_map(element_offset);
386 build_local_element_map(part_mesh, local_element_map);
387
388 // Need a map from local node to global node. Position in the map
389 // is part_mesh.offset+local_node_position Return value is position
390 // in the global node list. If no mapping, then the list is simply
391 // 0..number_of_global_nodes where number_of_global_nodes is the sum
392 // of the node counts in the individual files.
393 // This routine also eliminates nodes if there are omitted element blocks.
394 if (interFace.match_node_ids()) {
395 build_reverse_node_map(output_region, part_mesh, global_node_map, local_node_map);
396 }
397 else if (interFace.match_node_xyz()) {
398 match_node_xyz(part_mesh, interFace.tolerance(), global_node_map, local_node_map);
399 }
400 else {
401 // Eliminate all nodes that were only connected to the omitted element blocks (if any).
402 bool fill_global = true;
403 eliminate_omitted_nodes(part_mesh, global_node_map, local_node_map, fill_global);
404 }
405
406 node_count = global_node_map.size();
407 size_t merged = local_node_map.size() - global_node_map.size();
408 if (merged > 0) {
409 fmt::print("*** {} Nodes were merged/omitted.\n", fmt::group_digits(merged));
410 }
411
412 // Verify nodemap...
413 #ifndef NDEBUG
414 std::vector<int> glob(node_count);
415 for (auto id : local_node_map) {
416 if (id >= 0) {
417 glob[id] = 1;
418 }
419 }
420 for (int i : glob) {
421 SMART_ASSERT(i == 1);
422 }
423 #endif
424
425 // Transfer some common data...
426 output_region.property_add(part_mesh[0]->get_property("title"));
427
428 // Define a node block...
429 std::string block_name = "nodeblock_1";
430 int spatial_dimension = part_mesh[0]->get_property("spatial_dimension").get_int();
431 auto *block =
432 new Ioss::NodeBlock(output_region.get_database(), block_name, node_count, spatial_dimension);
433 block->property_add(Ioss::Property("id", 1));
434
435 output_region.add(block);
436
437 // Add element blocks, nodesets, sidesets
438 for (size_t p = 0; p < part_count; p++) {
439 transfer_elementblock(*part_mesh[p], output_region, interFace.create_assemblies(), false);
440 if (interFace.convert_nodes_to_nodesets(p + 1)) {
441 create_nodal_nodeset(*part_mesh[p], output_region, false);
442 }
443 if (!interFace.omit_nodesets()) {
444 transfer_nodesets(*part_mesh[p], output_region, false);
445 }
446 if (!interFace.omit_sidesets()) {
447 transfer_sidesets(*part_mesh[p], output_region, false);
448 }
449 if (!interFace.omit_assemblies()) {
450 transfer_assembly(*part_mesh[p], output_region, false);
451 }
452 }
453
454 if (!interFace.information_record_parts().empty()) {
455 const std::vector<int> &info_parts = interFace.information_record_parts();
456 if (info_parts[0] == 0) {
457 // Transfer info records from all parts...
458 for (const auto &pm : part_mesh) {
459 const StringVector &info = pm->get_information_records();
460 output_region.add_information_records(info);
461 }
462 }
463 else {
464 for (int info_part : info_parts) {
465 const StringVector &info = part_mesh[info_part - 1]->get_information_records();
466 output_region.add_information_records(info);
467 }
468 }
469 }
470
471 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
472
473 output_region.begin_mode(Ioss::STATE_MODEL);
474
475 output_nodeblock(output_region, part_mesh, local_node_map, global_node_map);
476 output_elementblock(output_region, part_mesh, local_node_map, local_element_map,
477 interFace.ignore_element_ids());
478 output_nodal_nodeset(output_region, part_mesh, interFace, local_node_map);
479
480 if (!interFace.omit_nodesets()) {
481 output_nodeset(output_region, part_mesh, local_node_map);
482 }
483 if (!interFace.omit_sidesets()) {
484 output_sideset(output_region, part_mesh, local_element_map);
485 }
486
487 output_region.end_mode(Ioss::STATE_MODEL);
488
489 // ####################TRANSIENT DATA SECTION###########################
490 // ***********************************************************************
491 // 9. Get Variable Information and names
492
493 if (debug_level & 1) {
494 fmt::print(stderr, "{}", time_stamp(tsFormat));
495 }
496
497 output_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
498
499 bool error = false;
500 error |= define_global_fields(output_region, part_mesh, interFace.global_var_names());
501
502 error |= define_nodal_fields(output_region, part_mesh, interFace.node_var_names(), interFace);
503 define_nodal_nodeset_fields(output_region, part_mesh, interFace.node_var_names(), interFace);
504
505 error |= define_element_fields(output_region, part_mesh, interFace.elem_var_names());
506
507 if (!interFace.omit_nodesets()) {
508 error |= define_nset_fields(output_region, part_mesh, interFace.nset_var_names());
509 }
510 if (!interFace.omit_sidesets()) {
511 error |= define_sset_fields(output_region, part_mesh, interFace.sset_var_names());
512 }
513
514 output_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
515
516 if (error) {
517 fmt::print(stderr,
518 "ERROR: Specified field(s) (see above) were not found. Fix input and rerun.\n\n");
519 exit(EXIT_FAILURE);
520 }
521
522 // Get database times...
523 // Different parts can have either no times or the times must match
524 //! \todo If ts_min, ts_max, ts_step is specified, then only check steps in that range...
525 std::vector<double> global_times;
526
527 for (size_t p = 0; p < part_count; p++) {
528 int nts = part_mesh[p]->get_property("state_count").get_int();
529 std::vector<double> times(nts);
530 // If multiple databases have timesteps, they must all match.
531 for (int i = 0; i < nts; i++) {
532 times[i] = part_mesh[p]->get_state_time(i + 1);
533 }
534
535 if (nts > 0) {
536 if (global_times.empty()) {
537 std::copy(times.begin(), times.end(), std::back_inserter(global_times));
538 }
539 else {
540 if (global_times.size() != times.size()) {
541 fmt::print(stderr, "ERROR: Time step sizes must match. (Global = {}, Part {} = {}",
542 global_times.size(), p, times.size());
543 exit(EXIT_FAILURE);
544 }
545
546 for (size_t i = 0; i < global_times.size(); i++) {
547 if (!approx_equal(global_times[i], times[i], global_times[0])) {
548 fmt::print(stderr,
549 "ERROR: Time step {} in part {} does not match time steps in previous "
550 "part(s): previous: "
551 "{}, current: {}\n",
552 i + 1, p + 1, global_times[i], times[i]);
553 exit(EXIT_FAILURE);
554 }
555 }
556 }
557 }
558 }
559
560 output_region.begin_mode(Ioss::STATE_TRANSIENT);
561 fmt::print("\n");
562 int ts_min = interFace.step_min();
563 int ts_max = interFace.step_max();
564 int ts_step = interFace.step_interval();
565 int num_steps = static_cast<int>(global_times.size());
566
567 if (ts_min < 0) {
568 ts_min = num_steps + 1 + ts_min;
569 }
570 if (ts_max < 0) {
571 ts_max = num_steps + 1 + ts_max;
572 }
573 ts_max = ts_max < num_steps ? ts_max : num_steps;
574
575 double ts_begin = Ioss::Utils::timer();
576 int steps = 0;
577 int nsteps = (ts_max - ts_min + 1) / ts_step;
578 for (int step = ts_min - 1; step < ts_max; step += ts_step) {
579 int ostep = output_region.add_state(global_times[step]);
580 output_region.begin_state(ostep);
581 output_transient_state(output_region, part_mesh, global_times[step], local_node_map, interFace);
582 fmt::print("\rWrote step {:4}/{:4}, time {:8.4e}", step + 1, nsteps, global_times[step]);
583 output_region.end_state(ostep);
584 steps++;
585 }
586 double end = Ioss::Utils::timer();
587 fmt::print("\n");
588 output_region.end_mode(Ioss::STATE_TRANSIENT);
589
590 /*************************************************************************/
591 // EXIT program
592 if (debug_level & 1) {
593 fmt::print(stderr, "{}", time_stamp(tsFormat));
594 }
595 output_region.output_summary(std::cout);
596 fmt::print("******* END *******\n");
597 fmt::print(stderr, "\nTotal Execution Time = {:.5} seconds.\n", end - begin);
598 if (steps > 0) {
599 fmt::print(stderr, "\tMesh = {:.5} seconds; Timesteps = {:.5} seconds / step.\n\n",
600 (ts_begin - begin), (end - ts_begin) / (double)(steps));
601 }
602 return end - begin;
603 }
604
605 namespace {
606 bool entity_is_omitted(Ioss::GroupingEntity *block)
607 {
608 bool omitted = block->get_optional_property("omitted", 0) == 1;
609 return omitted;
610 }
611
612 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region,
613 bool create_assemblies, bool debug)
614 {
615 static int used_blocks = 0;
616 const std::string &prefix = region.name();
617
618 Ioss::Assembly *assem = nullptr;
619
620 const Ioss::ElementBlockContainer &ebs = region.get_element_blocks();
621 for (const auto &eb : ebs) {
622 if (!entity_is_omitted(eb)) {
623 std::string name = eb->name();
624 if (output_region.get_element_block(name) != nullptr) {
625 name = prefix + "_" + eb->name();
626 if (output_region.get_element_block(name) != nullptr) {
627 fmt::print(stderr, "ERROR: Duplicate element blocks named '{}'\n", name);
628 exit(EXIT_FAILURE);
629 }
630 }
631 eb->property_add(Ioss::Property("name_in_output", name));
632
633 if (debug) {
634 fmt::print(stderr, "{}, ", name);
635 }
636 std::string type = eb->topology()->name();
637 size_t num_elem = eb->entity_count();
638
639 if (num_elem > 0) {
640 auto *ebn = new Ioss::ElementBlock(output_region.get_database(), name, type, num_elem);
641 ebn->property_add(Ioss::Property("original_block_order", used_blocks++));
642 output_region.add(ebn);
643 transfer_fields(eb, ebn, Ioss::Field::ATTRIBUTE);
644
645 set_id(eb, ebn);
646
647 if (eb->property_exists("original_topology_type")) {
648 std::string oes = eb->get_property("original_topology_type").get_string();
649
650 // Set the new property
651 ebn->property_add(Ioss::Property("original_topology_type", oes));
652 }
653
654 if (create_assemblies) {
655 if (assem == nullptr) {
656 assem = new Ioss::Assembly(output_region.get_database(), region.name());
657 output_region.add(assem);
658 assem->property_add(Ioss::Property("name_in_output", region.name()));
659 }
660 assem->add(ebn);
661 }
662 }
663 }
664 }
665 }
666
667 void transfer_assembly(const Ioss::Region &region, Ioss::Region &output_region, bool debug)
668 {
669 // All assemblies on the input parts will be transferred to the output mesh
670 // Possibly renamed if a name conflict
671 // Also need to update names of the entities in the assembly since they were possibly
672 // renamed on the output...
673 // TODO: handle renamed nested assemblies...
674 const std::string &prefix = region.name();
675
676 const Ioss::AssemblyContainer &assems = region.get_assemblies();
677 for (const auto &as : assems) {
678 if (!entity_is_omitted(as)) {
679 std::string name = as->name();
680 if (output_region.get_assembly(name) != nullptr) {
681 name = prefix + "_" + as->name();
682 if (output_region.get_assembly(name) != nullptr) {
683 fmt::print(stderr, "ERROR: Duplicate assemblies named '{}'\n", name);
684 exit(EXIT_FAILURE);
685 }
686 }
687 as->property_add(Ioss::Property("name_in_output", name));
688
689 if (debug) {
690 fmt::print(stderr, "{}, ", name);
691 }
692 size_t num_members = as->entity_count();
693 if (num_members > 0) {
694 auto member_type = as->get_member_type();
695 auto *asn = new Ioss::Assembly(output_region.get_database(), name);
696 output_region.add(asn);
697
698 const auto &members = as->get_members();
699 for (const auto &member : members) {
700 auto output_name = member->get_property("name_in_output").get_string();
701
702 auto *entity = output_region.get_entity(output_name, member_type);
703 assert(entity != nullptr);
704 asn->add(entity);
705 }
706 }
707 }
708 }
709 }
710
711 void transfer_sidesets(Ioss::Region &region, Ioss::Region &output_region, bool debug)
712 {
713 const std::string &prefix = region.name();
714
715 const Ioss::SideSetContainer &fss = region.get_sidesets();
716 for (const auto &fs : fss) {
717 if (!entity_is_omitted(fs)) {
718 std::string name = fs->name();
719 if (output_region.get_sideset(name) != nullptr) {
720 name = prefix + "_" + fs->name();
721 if (output_region.get_sideset(name) != nullptr) {
722 fmt::print(stderr, "ERROR: Duplicate side sets named '{}'\n", name);
723 exit(EXIT_FAILURE);
724 }
725 }
726 fs->property_add(Ioss::Property("name_in_output", name));
727 if (debug) {
728 fmt::print(stderr, "{}, ", name);
729 }
730 auto *surf = new Ioss::SideSet(output_region.get_database(), name);
731 set_id(fs, surf);
732
733 const Ioss::SideBlockContainer &fbs = fs->get_side_blocks();
734 for (const auto &fb : fbs) {
735 const std::string &fbname = prefix + "_" + fb->name();
736 if (debug) {
737 fmt::print(stderr, "{}, ", fbname);
738 }
739 fb->property_add(Ioss::Property("name_in_output", fbname));
740 std::string fbtype = fb->topology()->name();
741 std::string partype = fb->parent_element_topology()->name();
742 size_t num_side = fb->entity_count();
743
744 auto *block =
745 new Ioss::SideBlock(output_region.get_database(), fbname, fbtype, partype, num_side);
746 surf->add(block);
747 }
748 output_region.add(surf);
749 }
750 }
751 }
752
753 // Create a nodeset on the output region consisting of all the nodes
754 // in the input region.
755 void create_nodal_nodeset(Ioss::Region &region, Ioss::Region &output_region, bool debug)
756 {
757 const std::string &prefix = region.name();
758
759 std::string name = prefix + "_nodes";
760 if (output_region.get_nodeset(name) != nullptr) {
761 fmt::print(stderr, "ERROR: Duplicate node sets named '{}'\n", name);
762 exit(EXIT_FAILURE);
763 }
764 if (debug) {
765 fmt::print(stderr, "{}, ", name);
766 }
767 size_t count = region.get_property("node_count").get_int();
768 auto *ns = new Ioss::NodeSet(output_region.get_database(), name, count);
769 output_region.add(ns);
770 }
771
772 // Output the bulk data for a nodeset on the output region
773 // consisting of all the nodes in the input region.
774 template <typename INT>
775 void output_nodal_nodeset(Ioss::Region &output_region, RegionVector &part_mesh,
776 SystemInterface &interFace, const std::vector<INT> &local_node_map)
777 {
778 size_t part_count = part_mesh.size();
779 for (size_t p = 0; p < part_count; p++) {
780 if (interFace.convert_nodes_to_nodesets(p + 1)) {
781 std::string name = part_mesh[p]->name() + "_nodes";
782 Ioss::NodeSet *ons = output_region.get_nodeset(name);
783 SMART_ASSERT(ons != nullptr)(name);
784
785 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
786 SMART_ASSERT(nb != nullptr);
787 size_t node_offset = part_mesh[p]->get_property("node_offset").get_int();
788
789 std::vector<INT> nodelist;
790 nb->get_field_data("ids", nodelist);
791 // This needs to make sure that the nodelist comes back as local id (1..numnodes)
792 for (auto &node : nodelist) {
793 size_t loc_node = part_mesh[p]->node_global_to_local(node, true) - 1;
794 auto gpos = local_node_map[node_offset + loc_node];
795 if (gpos >= 0) {
796 node = gpos + 1;
797 }
798 }
799 ons->put_field_data("ids_raw", nodelist);
800
801 // Output distribution factors -- set all to 1.0
802 std::vector<double> factors(nodelist.size(), 1.0);
803 ons->put_field_data("distribution_factors", factors);
804 }
805 }
806 }
807
808 void define_nodal_nodeset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
809 const StringIdVector &variable_list, SystemInterface &interFace)
810 {
811 // This routine does not check that all variables in `variable_list` have been
812 // found since the checking has already been done in define_nodal_fields.
813 if (!variable_list.empty() && variable_list[0].first == "none") {
814 return;
815 }
816
817 size_t part_count = part_mesh.size();
818 for (size_t p = 0; p < part_count; p++) {
819
820 if (interFace.convert_nodes_to_nodesets(p + 1)) {
821 // Find nodeset in output region corresponding to the nodes in this part...
822 std::string name = part_mesh[p]->name() + "_nodes";
823 Ioss::NodeSet *ons = output_region.get_nodeset(name);
824 SMART_ASSERT(ons != nullptr)(name);
825
826 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
827 SMART_ASSERT(nb != nullptr);
828
829 SMART_ASSERT(part_mesh[p]->get_property("node_count").get_int() == nb->entity_count());
830
831 Ioss::NameList fields = nb->field_describe(Ioss::Field::TRANSIENT);
832 for (const auto &field_name : fields) {
833 if (valid_variable(field_name, 0, variable_list)) {
834 Ioss::Field field = nb->get_field(field_name);
835 ons->field_add(std::move(field));
836 }
837 }
838 }
839 }
840 }
841
842 void transfer_nodesets(Ioss::Region &region, Ioss::Region &output_region, bool debug)
843 {
844 bool combine_similar = false;
845 const std::string &prefix = region.name();
846
847 const Ioss::NodeSetContainer &nss = region.get_nodesets();
848 for (const auto &ns : nss) {
849 if (!entity_is_omitted(ns)) {
850 std::string name = ns->name();
851 auto *ons = output_region.get_nodeset(name);
852 if (ons != nullptr) {
853 if (combine_similar) {
854 // Combine nodesets with similar names...
855 size_t count = ns->entity_count();
856 add_to_entity_count(ons, count);
857 continue;
858 }
859 else {
860 name = prefix + "_" + ns->name();
861 if (output_region.get_nodeset(name) != nullptr) {
862 fmt::print(stderr, "ERROR: Duplicate node sets named '{}'\n", name);
863 exit(EXIT_FAILURE);
864 }
865 }
866 }
867 ns->property_add(Ioss::Property("name_in_output", name));
868 if (debug) {
869 fmt::print(stderr, "{}, ", name);
870 }
871 size_t count = ns->entity_count();
872 auto *node_set = new Ioss::NodeSet(output_region.get_database(), name, count);
873 output_region.add(node_set);
874 set_id(ns, node_set);
875 }
876 }
877 }
878
879 template <typename T, typename INT>
880 void map_element_vars(size_t loffset, size_t goffset, size_t entity_count, std::vector<T> &values,
881 std::vector<T> &global_values, INT *part_loc_elem_to_global)
882 {
883 // copy values to master element value information
884 T *local_values = Data(values);
885 for (size_t j = 0; j < entity_count; j++) {
886 size_t global_block_pos = part_loc_elem_to_global[(j + loffset)] - goffset;
887 global_values[global_block_pos] = local_values[j];
888 }
889 }
890
891 template <typename T>
892 void map_sideset_vars(size_t loffset, size_t entity_count, std::vector<T> &values,
893 std::vector<T> &global_values)
894 {
895 // copy values to master sideset value information
896 T *local_values = Data(values);
897 for (size_t j = 0; j < entity_count; j++) {
898 global_values[j + loffset] = local_values[j];
899 }
900 }
901
902 template <typename T, typename U>
903 void map_nodeset_vars(U & /*unused*/, int /*unused*/, int /*unused*/, std::vector<T> & /*unused*/,
904 std::vector<T> & /*unused*/)
905 {
906 SMART_ASSERT(1 == 0 && "Internal Error!");
907 }
908 } // namespace
909
910 namespace {
911 template <typename INT>
912 void output_nodeblock(Ioss::Region &output_region, RegionVector &part_mesh,
913 const std::vector<INT> &local_node_map, std::vector<INT> &global_node_map)
914 {
915 Ioss::NodeBlock *onb = output_region.get_node_blocks()[0];
916 SMART_ASSERT(onb != nullptr);
917
918 onb->put_field_data("ids", global_node_map);
919
920 int spatial_dimension = output_region.get_property("spatial_dimension").get_int();
921 std::vector<double> coord(global_node_map.size() * spatial_dimension);
922 for (const auto &pm : part_mesh) {
923 Ioss::NodeBlock *nb = pm->get_node_blocks()[0];
924 SMART_ASSERT(nb != nullptr);
925 std::vector<double> coordinates;
926 nb->get_field_data("mesh_model_coordinates", coordinates);
927 size_t node_count = nb->entity_count();
928 size_t offset = pm->get_property("node_offset").get_int();
929 for (size_t i = 0; i < node_count; i++) {
930 auto glob_pos = local_node_map[i + offset];
931 if (glob_pos >= 0) {
932 coord[glob_pos * spatial_dimension + 0] = coordinates[i * spatial_dimension + 0];
933 coord[glob_pos * spatial_dimension + 1] = coordinates[i * spatial_dimension + 1];
934 coord[glob_pos * spatial_dimension + 2] = coordinates[i * spatial_dimension + 2];
935 }
936 }
937 }
938 onb->put_field_data("mesh_model_coordinates", coord);
939 }
940
941 template <typename INT>
942 void output_elementblock(Ioss::Region &output_region, RegionVector &part_mesh,
943 const std::vector<INT> &local_node_map,
944 const std::vector<INT> &local_element_map, bool ignore_element_ids)
945 {
946
947 const Ioss::ElementBlockContainer &ebs = output_region.get_element_blocks();
948
949 size_t element_count = output_region.get_property("element_count").get_int();
950 std::vector<INT> ids(element_count);
951
952 if (ignore_element_ids) {
953 // Just generate 1..numel ids (much faster for large models)
954 std::iota(ids.begin(), ids.end(), 1);
955 }
956 else {
957 // Try to maintain the original element ids if possible...
958 generate_element_ids(part_mesh, local_element_map, ids);
959 }
960 size_t element_offset = 0;
961 for (const auto &eb : ebs) {
962 eb->put_field_data("ids", &ids[element_offset], ids.size() * sizeof(INT));
963 element_offset += eb->entity_count();
964 }
965
966 SMART_ASSERT(element_offset == element_count);
967
968 // Connectivity...
969 for (const auto &pm : part_mesh) {
970 const Ioss::ElementBlockContainer &iebs = pm->get_element_blocks();
971 size_t node_offset = pm->get_property("node_offset").get_int();
972
973 for (const auto &ieb : iebs) {
974 if (entity_is_omitted(ieb)) {
975 continue;
976 }
977 std::string name = pm->name() + "_" + ieb->name();
978 Ioss::ElementBlock *oeb = output_region.get_element_block(name);
979 if (oeb == nullptr) {
980 name = ieb->name();
981 oeb = output_region.get_element_block(name);
982 }
983 if (oeb != nullptr) {
984 std::vector<INT> connectivity;
985 ieb->get_field_data("connectivity_raw", connectivity);
986
987 SMART_ASSERT(ieb->entity_count() == oeb->entity_count());
988 for (auto &node : connectivity) {
989 // connectivity is in part-local node ids [1..num_node]
990 // loc_node = the position of node in the local [0..num_node)
991 // local_node_map[node_offset+loc_node] gives the position of this node in the global
992 // list
993 size_t loc_node = node - 1;
994 SMART_ASSERT(node_offset + loc_node < local_node_map.size());
995 auto gpos = local_node_map[node_offset + loc_node];
996 if (gpos >= 0) {
997 node = gpos + 1;
998 }
999 }
1000 oeb->put_field_data("connectivity_raw", connectivity);
1001 transfer_field_data(ieb, oeb, Ioss::Field::ATTRIBUTE);
1002 }
1003 }
1004 }
1005 }
1006
1007 template <typename INT>
1008 void output_nodeset(Ioss::Region &output_region, RegionVector &part_mesh,
1009 const std::vector<INT> &local_node_map)
1010 {
1011 if (output_region.get_nodesets().empty()) {
1012 return;
1013 }
1014
1015 for (const auto &pm : part_mesh) {
1016 size_t node_offset = pm->get_property("node_offset").get_int();
1017 const Ioss::NodeSetContainer &ins = pm->get_nodesets();
1018 for (const auto &in : ins) {
1019 if (!entity_is_omitted(in)) {
1020 std::vector<INT> nodelist;
1021 in->get_field_data("ids", nodelist);
1022
1023 std::string name = pm->name() + "_" + in->name();
1024 Ioss::NodeSet *ons = output_region.get_nodeset(name);
1025 if (ons == nullptr) {
1026 name = in->name();
1027 ons = output_region.get_nodeset(name);
1028 }
1029 SMART_ASSERT(ons != nullptr)(name);
1030 SMART_ASSERT(in->entity_count() == ons->entity_count());
1031
1032 // This needs to make sure that the nodelist comes back as local id (1..numnodes)
1033 for (auto &node : nodelist) {
1034 size_t loc_node = pm->node_global_to_local(node, true) - 1;
1035 auto gpos = local_node_map[node_offset + loc_node];
1036 if (gpos >= 0) {
1037 node = gpos + 1;
1038 }
1039 }
1040 ons->put_field_data("ids_raw", nodelist);
1041
1042 std::vector<double> df;
1043 in->get_field_data("distribution_factors", df);
1044 ons->put_field_data("distribution_factors", df);
1045 }
1046 }
1047 }
1048 }
1049
1050 template <typename INT>
1051 void output_sideset(Ioss::Region &output_region, RegionVector &part_mesh,
1052 const std::vector<INT> &local_element_map)
1053 {
1054 const Ioss::SideSetContainer &os = output_region.get_sidesets();
1055
1056 Ioss::SideBlockContainer out_eb;
1057 // Put all output side blocks in the same list...
1058 for (const auto &oss : os) {
1059 const Ioss::SideBlockContainer &obs = oss->get_side_blocks();
1060 std::copy(obs.begin(), obs.end(), std::back_inserter(out_eb));
1061 }
1062
1063 // Assuming (with checks) that the output side blocks will be
1064 // iterated in same order as input side blocks...
1065 auto II = out_eb.begin();
1066
1067 for (const auto &pm : part_mesh) {
1068 size_t element_offset = pm->get_property("element_offset").get_int();
1069
1070 const Ioss::SideSetContainer &is = pm->get_sidesets();
1071 for (const auto &iss : is) {
1072 if (!entity_is_omitted(iss)) {
1073 const Ioss::SideBlockContainer &ebs = iss->get_side_blocks();
1074
1075 for (const auto &eb : ebs) {
1076 SMART_ASSERT((eb->name() == (*II)->name()) ||
1077 (pm->name() + "_" + eb->name() == (*II)->name()))
1078 (eb->name())((*II)->name());
1079 SMART_ASSERT(eb->entity_count() == (*II)->entity_count());
1080 std::vector<INT> elem_side_list;
1081 eb->get_field_data("element_side_raw", elem_side_list);
1082
1083 // The 'elem_side_list' contains
1084 // (local_element_position,side_ordinal) pairs. The
1085 // 'local_element_position' is 1-based offset in the
1086 // current part. Need to map to its location in the
1087 // output region...
1088 for (size_t i = 0; i < elem_side_list.size();
1089 i += 2) { // just get the elem part of the pair...
1090 size_t local_position = elem_side_list[i] - 1;
1091 auto gpos = local_element_map[element_offset + local_position];
1092 SMART_ASSERT(gpos >= 0)(gpos)(i); // Inactive elements should be filtered by Ioss
1093 elem_side_list[i] = gpos + 1;
1094 }
1095 (*II)->put_field_data("element_side_raw", elem_side_list);
1096 ++II;
1097 }
1098 }
1099 }
1100 }
1101 }
1102
1103 void output_globals(Ioss::Region &output_region, RegionVector &part_mesh)
1104 {
1105 for (const auto &pm : part_mesh) {
1106 Ioss::NameList fields = pm->field_describe(Ioss::Field::REDUCTION);
1107 for (const auto &field : fields) {
1108 std::vector<double> data;
1109 pm->get_field_data(field, data);
1110 output_region.put_field_data(field, data);
1111 }
1112 }
1113 }
1114
1115 template <typename INT>
1116 void output_nodal(Ioss::Region &output_region, RegionVector &part_mesh,
1117 const std::vector<INT> &local_node_map, SystemInterface &interFace)
1118 {
1119 size_t part_count = part_mesh.size();
1120
1121 Ioss::NodeBlock *onb = output_region.get_node_blocks()[0];
1122 SMART_ASSERT(onb != nullptr);
1123 size_t node_count = onb->entity_count();
1124
1125 Ioss::NameList fields = onb->field_describe(Ioss::Field::TRANSIENT);
1126 for (const auto &field : fields) {
1127 size_t comp_count = onb->get_field(field).raw_storage()->component_count();
1128 std::vector<double> data(node_count * comp_count);
1129 for (size_t p = 0; p < part_count; p++) {
1130 if (!interFace.convert_nodes_to_nodesets(p + 1)) {
1131 size_t offset = part_mesh[p]->get_property("node_offset").get_int();
1132 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
1133 SMART_ASSERT(nb != nullptr);
1134 if (nb->field_exists(field)) {
1135 SMART_ASSERT((int)comp_count == nb->get_field(field).raw_storage()->component_count());
1136 std::vector<double> loc_data;
1137 nb->get_field_data(field, loc_data);
1138 size_t nc = nb->entity_count();
1139 SMART_ASSERT(loc_data.size() == nc * comp_count);
1140 for (size_t i = 0; i < nc; i++) {
1141 auto glob_pos = local_node_map[offset + i];
1142 if (glob_pos >= 0) {
1143 for (size_t j = 0; j < comp_count; j++) {
1144 data[glob_pos * comp_count + j] = loc_data[i * comp_count + j];
1145 }
1146 }
1147 }
1148 }
1149 }
1150 }
1151 onb->put_field_data(field, data);
1152 }
1153 }
1154
1155 void output_nodal_nodeset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1156 SystemInterface &interFace)
1157 {
1158 size_t part_count = part_mesh.size();
1159 for (size_t p = 0; p < part_count; p++) {
1160 if (interFace.convert_nodes_to_nodesets(p + 1)) {
1161
1162 // Find nodeset in output region corresponding to the nodes in this part...
1163 std::string name = part_mesh[p]->name() + "_nodes";
1164 Ioss::NodeSet *ons = output_region.get_nodeset(name);
1165 SMART_ASSERT(ons != nullptr)(name);
1166
1167 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
1168 SMART_ASSERT(nb != nullptr);
1169
1170 SMART_ASSERT(part_mesh[p]->get_property("node_count").get_int() == nb->entity_count());
1171
1172 // NOTE: The node order in the output nodeset 'ons' was
1173 // defined as the same node order in the input nodeblock 'nb',
1174 // so we shouldn't have to do any reordering of the data at
1175 // this time--just read then write.
1176 Ioss::NameList fields = ons->field_describe(Ioss::Field::TRANSIENT);
1177 std::vector<double> data;
1178 for (const auto &field : fields) {
1179 nb->get_field_data(field, data);
1180 ons->put_field_data(field, data);
1181 }
1182 }
1183 }
1184 }
1185
1186 void output_element(Ioss::Region &output_region, RegionVector &part_mesh)
1187 {
1188 for (const auto &pm : part_mesh) {
1189 const Ioss::ElementBlockContainer &iebs = pm->get_element_blocks();
1190 for (const auto &ieb : iebs) {
1191 if (!entity_is_omitted(ieb)) {
1192 std::string name = pm->name() + "_" + ieb->name();
1193 Ioss::ElementBlock *oeb = output_region.get_element_block(name);
1194 if (oeb == nullptr) {
1195 name = ieb->name();
1196 oeb = output_region.get_element_block(name);
1197 }
1198 if (oeb != nullptr) {
1199 Ioss::NameList fields = ieb->field_describe(Ioss::Field::TRANSIENT);
1200 for (const auto &field : fields) {
1201 if (oeb->field_exists(field)) {
1202 transfer_field_data_internal(ieb, oeb, field);
1203 }
1204 }
1205 }
1206 }
1207 }
1208 }
1209 }
1210
1211 void output_nset(Ioss::Region &output_region, RegionVector &part_mesh)
1212 {
1213 if (output_region.get_nodesets().empty()) {
1214 return;
1215 }
1216
1217 for (const auto &pm : part_mesh) {
1218 const Ioss::NodeSetContainer &ins = pm->get_nodesets();
1219 for (const auto &in : ins) {
1220 if (!entity_is_omitted(in)) {
1221 std::string name = pm->name() + "_" + in->name();
1222 Ioss::NodeSet *ons = output_region.get_nodeset(name);
1223 if (ons == nullptr) {
1224 name = in->name();
1225 ons = output_region.get_nodeset(name);
1226 }
1227 SMART_ASSERT(ons != nullptr)(name);
1228
1229 Ioss::NameList fields = in->field_describe(Ioss::Field::TRANSIENT);
1230 for (const auto &field : fields) {
1231 if (ons->field_exists(field)) {
1232 transfer_field_data_internal(in, ons, field);
1233 }
1234 }
1235 }
1236 }
1237 }
1238 }
1239
1240 void output_sset(Ioss::Region &output_region, RegionVector &part_mesh)
1241 {
1242 const Ioss::SideSetContainer &os = output_region.get_sidesets();
1243 if (os.empty()) {
1244 return;
1245 }
1246
1247 Ioss::SideBlockContainer out_eb;
1248 // Put all output side blocks in the same list...
1249 for (const auto &oss : os) {
1250 const Ioss::SideBlockContainer &obs = oss->get_side_blocks();
1251 std::copy(obs.begin(), obs.end(), std::back_inserter(out_eb));
1252 }
1253
1254 // Assuming (with checks) that the output side blocks will be
1255 // iterated in same order as input side blocks...
1256 auto II = out_eb.begin();
1257
1258 for (const auto &pm : part_mesh) {
1259 const Ioss::SideSetContainer &is = pm->get_sidesets();
1260 for (const auto &iss : is) {
1261 if (!entity_is_omitted(iss)) {
1262 const Ioss::SideBlockContainer &ebs = iss->get_side_blocks();
1263 for (const auto &eb : ebs) {
1264 SMART_ASSERT((pm->name() + "_" + eb->name() == (*II)->name()) ||
1265 (eb->name() == (*II)->name()));
1266 Ioss::NameList fields = eb->field_describe(Ioss::Field::TRANSIENT);
1267 for (const auto &field : fields) {
1268 if ((*II)->field_exists(field)) {
1269 transfer_field_data_internal(eb, *II, field);
1270 }
1271 }
1272 ++II;
1273 }
1274 }
1275 }
1276 }
1277 }
1278
1279 template <typename INT>
1280 void output_transient_state(Ioss::Region &output_region, RegionVector &part_mesh, double time,
1281 const std::vector<INT> &local_node_map, SystemInterface &interFace)
1282 {
1283 // Determine which state on each input mesh corresponds to 'time'
1284 std::vector<int> steps(part_mesh.size());
1285 for (size_t p = 0; p < part_mesh.size(); p++) {
1286 double min_delta = 1.0e39;
1287 size_t min_step = 0;
1288
1289 size_t nts = part_mesh[p]->get_property("state_count").get_int();
1290 steps[p] = 0;
1291 for (size_t i = 0; i < nts; i++) {
1292 double delta = std::fabs(part_mesh[p]->get_state_time(i + 1) - time);
1293 if (delta < min_delta) {
1294 min_delta = delta;
1295 min_step = i;
1296 if (delta == 0.0) {
1297 break;
1298 }
1299 }
1300 else {
1301 // Delta is increasing; times are moving apart...
1302 // (Assumes monotonically increasing time values...)
1303 break;
1304 }
1305 }
1306 if (nts > 0) {
1307 steps[p] = min_step + 1;
1308 part_mesh[p]->begin_state(steps[p]);
1309 }
1310 }
1311
1312 output_globals(output_region, part_mesh);
1313 output_nodal(output_region, part_mesh, local_node_map, interFace);
1314 output_element(output_region, part_mesh);
1315 output_nodal_nodeset_fields(output_region, part_mesh, interFace);
1316 if (!interFace.omit_nodesets()) {
1317 output_nset(output_region, part_mesh);
1318 }
1319 if (!interFace.omit_sidesets()) {
1320 output_sset(output_region, part_mesh);
1321 }
1322
1323 for (size_t p = 0; p < part_mesh.size(); p++) {
1324 if (steps[p] != 0) {
1325 part_mesh[p]->end_state(steps[p]);
1326 }
1327 }
1328 }
1329
1330 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
1331 Ioss::Field::RoleType role, const std::string &prefix,
1332 bool transfer_connectivity)
1333 {
1334 // Iterate through the TRANSIENT-role fields of the input
1335 // database and transfer to output database.
1336 Ioss::NameList state_fields = ige->field_describe(role);
1337
1338 // Complication here is that if the 'role' is 'Ioss::Field::MESH',
1339 // then the 'ids' field must be transferred first...
1340 if (role == Ioss::Field::MESH) {
1341 for (const auto &field_name : state_fields) {
1342 if (oge->field_exists(field_name)) {
1343 if (field_name == "ids") {
1344 transfer_field_data_internal(ige, oge, field_name);
1345 break;
1346 }
1347 }
1348 }
1349 }
1350
1351 for (const auto &field_name : state_fields) {
1352 // All of the 'Ioss::EntityBlock' derived classes have a
1353 // 'connectivity' field, but it is only interesting on the
1354 // Ioss::ElementBlock class. On the other classes, it just
1355 // generates overhead...
1356 if (!transfer_connectivity && field_name == "connectivity") {
1357 continue;
1358 }
1359
1360 if (field_name != "ids" && Ioss::Utils::substr_equal(prefix, field_name)) {
1361 if (oge->field_exists(field_name)) {
1362 transfer_field_data_internal(ige, oge, field_name);
1363 }
1364 }
1365 }
1366 }
1367
1368 void transfer_field_data_internal(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
1369 const std::string &field_name)
1370 {
1371 static std::vector<double> data;
1372
1373 assert(ige->get_field(field_name).get_size() == oge->get_field(field_name).get_size());
1374
1375 ige->get_field_data(field_name, data);
1376 oge->put_field_data(field_name, data);
1377 }
1378
1379 bool define_global_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1380 const StringIdVector &variable_list)
1381 {
1382 bool error = false;
1383 if (!variable_list.empty() && variable_list[0].first == "none") {
1384 return error;
1385 }
1386 for (const auto &pm : part_mesh) {
1387 Ioss::NameList fields = pm->field_describe(Ioss::Field::REDUCTION);
1388 for (const auto &field_name : fields) {
1389 if (valid_variable(field_name, 0, variable_list)) {
1390 Ioss::Field field = pm->get_field(field_name);
1391 output_region.field_add(std::move(field));
1392 }
1393 }
1394 }
1395 // Now that we have defined all fields, check `variable_list` and make
1396 // sure that all fields that have been explicitly specified now exist
1397 // on `output_region`...
1398 if (!variable_list.empty() && variable_list[0].first != "all") {
1399 // The user has specified at least one variable...
1400 Ioss::NameList fields = output_region.field_describe(Ioss::Field::REDUCTION);
1401 error = check_variable_mismatch("Global", variable_list, fields);
1402 }
1403 return error;
1404 }
1405
1406 bool define_nodal_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1407 const StringIdVector &variable_list, SystemInterface &interFace)
1408 {
1409 bool error = false;
1410 if (!variable_list.empty() && variable_list[0].first == "none") {
1411 return error;
1412 }
1413 Ioss::NodeBlock *onb = output_region.get_node_blocks()[0];
1414 SMART_ASSERT(onb != nullptr);
1415 size_t node_count = onb->entity_count();
1416 size_t part_count = part_mesh.size();
1417 for (size_t p = 0; p < part_count; p++) {
1418 if (!interFace.convert_nodes_to_nodesets(p + 1)) {
1419 Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0];
1420 SMART_ASSERT(nb != nullptr);
1421 Ioss::NameList fields = nb->field_describe(Ioss::Field::TRANSIENT);
1422 for (const auto &field_name : fields) {
1423 if (valid_variable(field_name, 0, variable_list)) {
1424 Ioss::Field field = nb->get_field(field_name);
1425 field.reset_count(node_count);
1426 onb->field_add(std::move(field));
1427 }
1428 }
1429 }
1430 }
1431 // Now that we have defined all fields, check `variable_list` and make
1432 // sure that all fields that have been explicitly specified now exist
1433 // on `output_region`...
1434 if (!variable_list.empty() && variable_list[0].first != "all") {
1435 // The user has specified at least one variable...
1436 Ioss::NameList fields = onb->field_describe(Ioss::Field::REDUCTION);
1437 error = check_variable_mismatch("Nodal", variable_list, fields);
1438 }
1439 return error;
1440 }
1441
1442 bool define_element_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1443 const StringIdVector &variable_list)
1444 {
1445 bool error = false;
1446 // Element Block Fields...
1447 if (!variable_list.empty() && variable_list[0].first == "none") {
1448 return error;
1449 }
1450 bool subsetting_fields = !variable_list.empty() && variable_list[0].first != "all";
1451 Ioss::NameList defined_fields;
1452
1453 for (const auto &pm : part_mesh) {
1454 const Ioss::ElementBlockContainer &iebs = pm->get_element_blocks();
1455 for (const auto &ieb : iebs) {
1456 if (!entity_is_omitted(ieb)) {
1457 std::string name = pm->name() + "_" + ieb->name();
1458 Ioss::ElementBlock *oeb = output_region.get_element_block(name);
1459 if (oeb == nullptr) {
1460 name = ieb->name();
1461 oeb = output_region.get_element_block(name);
1462 }
1463 if (oeb != nullptr) {
1464 size_t id = oeb->get_property("id").get_int();
1465 Ioss::NameList fields = ieb->field_describe(Ioss::Field::TRANSIENT);
1466 for (const auto &field_name : fields) {
1467 if (valid_variable(field_name, id, variable_list)) {
1468 Ioss::Field field = ieb->get_field(field_name);
1469 oeb->field_add(std::move(field));
1470 if (subsetting_fields) {
1471 defined_fields.push_back(field_name);
1472 }
1473 }
1474 }
1475 }
1476 }
1477 }
1478 }
1479 // Now that we have defined all fields, check `variable_list` and make
1480 // sure that all fields that have been explicitly specified now exist
1481 // on `output_region`...
1482 if (subsetting_fields) {
1483 // The user has specified at least one variable...
1484 Ioss::Utils::uniquify(defined_fields);
1485 error = check_variable_mismatch("Element", variable_list, defined_fields);
1486 }
1487 return error;
1488 }
1489
1490 bool define_nset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1491 const StringIdVector &variable_list)
1492 {
1493 bool error = false;
1494 // Nodeset fields...
1495 if (!variable_list.empty() && variable_list[0].first == "none") {
1496 return error;
1497 }
1498
1499 bool subsetting_fields = !variable_list.empty() && variable_list[0].first != "all";
1500 Ioss::NameList defined_fields;
1501
1502 for (const auto &pm : part_mesh) {
1503 const Ioss::NodeSetContainer &ins = pm->get_nodesets();
1504 for (const auto &in : ins) {
1505 if (!entity_is_omitted(in)) {
1506 std::string name = pm->name() + "_" + in->name();
1507 Ioss::NodeSet *ons = output_region.get_nodeset(name);
1508 if (ons == nullptr) {
1509 name = in->name();
1510 ons = output_region.get_nodeset(name);
1511 }
1512 SMART_ASSERT(ons != nullptr)(name);
1513
1514 size_t id = in->get_property("id").get_int();
1515 Ioss::NameList fields = in->field_describe(Ioss::Field::TRANSIENT);
1516 for (const auto &field_name : fields) {
1517 if (valid_variable(field_name, id, variable_list)) {
1518 Ioss::Field field = in->get_field(field_name);
1519 ons->field_add(std::move(field));
1520 if (subsetting_fields) {
1521 defined_fields.push_back(field_name);
1522 }
1523 }
1524 }
1525 }
1526 }
1527 }
1528 // Now that we have defined all fields, check `variable_list` and make
1529 // sure that all fields that have been explicitly specified now exist
1530 // on `output_region`...
1531 if (subsetting_fields) {
1532 // The user has specified at least one variable...
1533 Ioss::Utils::uniquify(defined_fields);
1534 error = check_variable_mismatch("Nodeset", variable_list, defined_fields);
1535 }
1536 return error;
1537 }
1538
1539 bool define_sset_fields(Ioss::Region &output_region, RegionVector &part_mesh,
1540 const StringIdVector &variable_list)
1541 {
1542 bool error = false;
1543 if (!variable_list.empty() && variable_list[0].first == "none") {
1544 return error;
1545 }
1546 bool subsetting_fields = !variable_list.empty() && variable_list[0].first != "all";
1547 Ioss::NameList defined_fields;
1548
1549 const auto &os = output_region.get_sidesets();
1550
1551 Ioss::SideBlockContainer out_eb;
1552 // Put all output side blocks in the same list...
1553 for (const auto &oss : os) {
1554 const auto &obs = oss->get_side_blocks();
1555 std::copy(obs.begin(), obs.end(), std::back_inserter(out_eb));
1556 }
1557
1558 // Assuming (with checks) that the output side blocks will be
1559 // iterated in same order as input side blocks...
1560 auto II = out_eb.begin();
1561
1562 for (const auto &pm : part_mesh) {
1563 const auto &is = pm->get_sidesets();
1564 for (const auto &iss : is) {
1565 if (!entity_is_omitted(iss)) {
1566 size_t id = iss->get_property("id").get_int();
1567 const auto &ebs = iss->get_side_blocks();
1568 for (const auto &eb : ebs) {
1569 SMART_ASSERT((pm->name() + "_" + eb->name() == (*II)->name()) ||
1570 (eb->name() == (*II)->name()));
1571 auto fields = eb->field_describe(Ioss::Field::TRANSIENT);
1572 for (const auto &field_name : fields) {
1573 if (valid_variable(field_name, id, variable_list)) {
1574 Ioss::Field field = eb->get_field(field_name);
1575 (*II)->field_add(std::move(field));
1576 if (subsetting_fields) {
1577 defined_fields.push_back(field_name);
1578 }
1579 }
1580 }
1581 ++II;
1582 }
1583 }
1584 }
1585 }
1586 // Now that we have defined all fields, check `variable_list` and make
1587 // sure that all fields that have been explicitly specified now exist
1588 // on `output_region`...
1589 if (subsetting_fields) {
1590 // The user has specified at least one variable...
1591 Ioss::Utils::uniquify(defined_fields);
1592 error = check_variable_mismatch("Sideset", variable_list, defined_fields);
1593 }
1594 return error;
1595 }
1596
1597 void transfer_fields(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
1598 Ioss::Field::RoleType role, const std::string &prefix)
1599 {
1600 // Check for transient fields...
1601 auto fields = ige->field_describe(role);
1602
1603 // Iterate through results fields and transfer to output
1604 // database... If a prefix is specified, only transfer fields
1605 // whose names begin with the prefix
1606 for (const auto &field_name : fields) {
1607 if (field_name != "ids" && !oge->field_exists(field_name) &&
1608 Ioss::Utils::substr_equal(prefix, field_name)) {
1609 // If the field does not already exist, add it to the output node block
1610 Ioss::Field field = ige->get_field(field_name);
1611 oge->field_add(std::move(field));
1612 }
1613 }
1614 }
1615
1616 bool valid_variable(const std::string &variable, size_t id, const StringIdVector &variable_list)
1617 {
1618 if (variable_list.empty() || variable_list[0].first == "all") {
1619 return true;
1620 }
1621 if (variable_list[0].first == "none") {
1622 return false;
1623 }
1624
1625 for (const auto &var : variable_list) {
1626 if (var.first == variable) {
1627 if (id == 0 || id == var.second || var.second == 0) {
1628 return true;
1629 }
1630 }
1631 }
1632 return false;
1633 }
1634
1635 bool check_variable_mismatch(const std::string &type, const StringIdVector &variable_list,
1636 const Ioss::NameList &fields)
1637 {
1638 // Check all variables in `variable_list` and see if they are found in `fields`
1639 if (variable_list.empty() || variable_list[0].first == "all") {
1640 return false; // No error
1641 }
1642
1643 bool error = false;
1644 for (const auto &var : variable_list) {
1645 if (std::find(fields.begin(), fields.end(), var.first) == std::end(fields)) {
1646 fmt::print(stderr, "ERROR: {} Variable '{}' was not found in the list of valid fields.\n",
1647 type, var.first);
1648 error = true;
1649 }
1650 }
1651 return error;
1652 }
1653
1654 void process_nset_omissions(RegionVector &part_mesh, const Omissions &omit)
1655 {
1656 size_t part_count = part_mesh.size();
1657 for (size_t p = 0; p < part_count; p++) {
1658 if (!omit[p].empty()) {
1659 // Get the nodesets for this part and set the "omitted" property on the nodeset
1660 if (omit[p][0] == "ALL") {
1661 const Ioss::NodeSetContainer &nodesets = part_mesh[p]->get_nodesets();
1662 for (const auto &ns : nodesets) {
1663 ns->property_add(Ioss::Property(std::string("omitted"), 1));
1664 }
1665 }
1666 else {
1667 for (const auto &omitted : omit[p]) {
1668 Ioss::NodeSet *ns = part_mesh[p]->get_nodeset(omitted);
1669 if (ns != nullptr) {
1670 ns->property_add(Ioss::Property(std::string("omitted"), 1));
1671 }
1672 }
1673 }
1674 }
1675 }
1676 }
1677
1678 void process_sset_omissions(RegionVector &part_mesh, const Omissions &omit)
1679 {
1680 size_t part_count = part_mesh.size();
1681 for (size_t p = 0; p < part_count; p++) {
1682 if (!omit[p].empty()) {
1683 // Get the sidesets for this part and set the "omitted" property on the sideset
1684 if (omit[p][0] == "ALL") {
1685 const Ioss::SideSetContainer &sidesets = part_mesh[p]->get_sidesets();
1686 for (const auto &ss : sidesets) {
1687 ss->property_add(Ioss::Property(std::string("omitted"), 1));
1688 }
1689 }
1690 else {
1691 for (const auto &omitted : omit[p]) {
1692 Ioss::SideSet *ss = part_mesh[p]->get_sideset(omitted);
1693 if (ss != nullptr) {
1694 ss->property_add(Ioss::Property(std::string("omitted"), 1));
1695 }
1696 }
1697 }
1698 }
1699 }
1700 }
1701
1702 void process_assembly_omissions(RegionVector &part_mesh, const Omissions &omit)
1703 {
1704 size_t part_count = part_mesh.size();
1705 for (size_t p = 0; p < part_count; p++) {
1706 if (!omit[p].empty()) {
1707 // Get the assemblies for this part and set the "omitted" property on the assembly
1708 if (omit[p][0] == "ALL") {
1709 const auto &assemblies = part_mesh[p]->get_assemblies();
1710 for (const auto &as : assemblies) {
1711 as->property_add(Ioss::Property(std::string("omitted"), 1));
1712 }
1713 }
1714 else {
1715 for (const auto &omitted : omit[p]) {
1716 auto *as = part_mesh[p]->get_assembly(omitted);
1717 if (as != nullptr) {
1718 as->property_add(Ioss::Property(std::string("omitted"), 1));
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 } // namespace