Debian Codegraph @

packages/seacas/libraries/ioss/src/main/skinner.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
7 #include <array>
8 #include <cassert>
9 #include <cmath>
10 #include <cstdlib>
11 #include <exception>
12 #include <fmt/core.h>
13 #include <fmt/format.h>
14 #include <fmt/ostream.h>
15 #include <iostream>
16 #include <limits>
17 #include <map>
18 #include <numeric>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string>
22 #include <vector>
23
24 #include "Ionit_Initializer.h"
25 #include "Ioss_DBUsage.h"
26 #include "Ioss_DataSize.h"
27 #include "Ioss_DatabaseIO.h"
28 #include "Ioss_ElementBlock.h"
29 #include "Ioss_ElementTopology.h"
30 #include "Ioss_EntityBlock.h"
31 #include "Ioss_FaceGenerator.h"
32 #include "Ioss_Field.h"
33 #include "Ioss_FileInfo.h"
34 #include "Ioss_GroupingEntity.h"
35 #include "Ioss_IOFactory.h"
36 #include "Ioss_NodeBlock.h"
37 #include "Ioss_ParallelUtils.h"
38 #include "Ioss_Property.h"
39 #include "Ioss_PropertyManager.h"
40 #include "Ioss_Region.h"
41 #include "Ioss_ScopeGuard.h"
42 #include "Ioss_State.h"
43 #include "Ioss_Utils.h"
44 #include "robin_hash.h"
45 #include "skinner_interface.h"
46
47 // ========================================================================
48 namespace {
49 template <typename INT> void skinner(Skinner::Interface &interFace, INT /*dummy*/);
50 std::string codename;
51 std::string version = "1.02";
52
53 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
54 Ioss::Field::RoleType role, const Ioss::IntVector &ref_nodes);
55
56 void transfer_field_data(Ioss::EntityBlock *ige, Ioss::EntityBlock *oge,
57 Ioss::Field::RoleType role,
58 const std::vector<Ioss::Face> &boundary_faces);
59
60 void output_table(const Ioss::ElementBlockContainer &ebs,
61 std::map<std::string, std::vector<Ioss::Face>> &boundary_faces)
62 {
63 // Get maximum name and face_count length...
64 size_t max_name = std::string("Block Name").length();
65 size_t max_face = std::string("Face Count").length();
66 for (auto &eb : ebs) {
67 const std::string &name = eb->name();
68 max_name = std::max(max_name, name.length());
69 size_t face_width = Ioss::Utils::number_width(boundary_faces[name].size());
70 max_face = std::max(max_face, face_width);
71 }
72 max_name += 4; // Padding
73 max_face += 4;
74
75 fmt::print("\t+{2:-^{0}}+{2:-^{1}}+\n", max_name, max_face, "");
76 fmt::print("\t|{2:^{0}}|{3:^{1}}|\n", max_name, max_face, "Block Name", "Face Count");
77 fmt::print("\t+{2:-^{0}}+{2:-^{1}}+\n", max_name, max_face, "");
78 for (auto &eb : ebs) {
79 const std::string &name = eb->name();
80 fmt::print("\t|{2:^{0}}|{3:{1}} |\n", max_name, max_face - 2, name,
81 fmt::group_digits(boundary_faces[name].size()));
82 }
83 fmt::print("\t+{2:-^{0}}+{2:-^{1}}+\n", max_name, max_face, "");
84 }
85
86 Ioss::IntVector get_selected_steps(Ioss::Region &region, const Skinner::Interface &options)
87 {
88 // This routine checks all steps of the input database and selects those which
89 // meet the requirements specified in `options`. The returned (1-based) vector will have a
90 // value of `1` if the step is to be output and `0` if skipped.
91 int step_count = (int)region.get_property("state_count").get_int();
92 Ioss::IntVector selected_steps(step_count + 1);
93
94 // If user specified a list of times to transfer to output database,
95 // process the list and find the times on the input database that are
96 // closest to the times in the list.
97 if (!options.selected_times.empty()) {
98 int selected_step = 0;
99 for (auto time : options.selected_times) {
100 double diff = std::numeric_limits<double>::max();
101 for (int step = 1; step <= step_count; step++) {
102 double db_time = region.get_state_time(step);
103 double cur_diff = std::abs(db_time - time);
104 if (cur_diff < diff) {
105 diff = std::abs(db_time - time);
106 selected_step = step;
107 }
108 }
109 if (selected_step > 0) {
110 selected_steps[selected_step] = 1;
111 }
112 }
113 }
114 else {
115 // User did not select specific times to be output...
116 // Just select them all
117 for (int i = 1; i <= step_count; i++) {
118 selected_steps[i] = 1;
119 }
120 }
121
122 // Now, filter by min and max time...
123 for (int istep = 1; istep <= step_count; istep++) {
124 double time = region.get_state_time(istep);
125 if (time < options.minimum_time) {
126 selected_steps[istep] = 0;
127 }
128 if (time > options.maximum_time) {
129 selected_steps[istep] = 0;
130 }
131 }
132 return selected_steps;
133 }
134 } // namespace
135
136 int main(int argc, char *argv[])
137 {
138 #ifdef SEACAS_HAVE_MPI
139 MPI_Init(&argc, &argv);
140 ON_BLOCK_EXIT(MPI_Finalize);
141 #endif
142 Ioss::ParallelUtils pu{};
143 int my_rank = pu.parallel_rank();
144
145 codename = Ioss::FileInfo(argv[0]).basename();
146
147 Skinner::Interface interFace(version);
148 bool success = interFace.parse_options(argc, argv);
149 if (!success) {
150 return EXIT_FAILURE;
151 }
152
153 Ioss::Init::Initializer io;
154
155 if (my_rank == 0) {
156 fmt::print("\nInput: '{}', Type: {}\n", interFace.input_filename(), interFace.input_type());
157 if (!interFace.no_output()) {
158 fmt::print("Output: '{}', Type: {}\n", interFace.output_filename(),
159 interFace.output_type());
160 }
161 }
162
163 double begin = Ioss::Utils::timer();
164 try {
165 if (interFace.ints_64_bit()) {
166 skinner(interFace, static_cast<int64_t>(0));
167 }
168 else {
169 skinner(interFace, 0);
170 }
171 }
172 catch (std::exception &e) {
173 fmt::print(stderr, "\n{}\n\nskinner terminated due to exception\n", e.what());
174 exit(EXIT_FAILURE);
175 }
176 pu.barrier();
177 double end = Ioss::Utils::timer();
178
179 if (my_rank == 0) {
180 fmt::print("\n\tTotal Execution Time = {:.4} seconds\n", end - begin);
181 fmt::print("\n{} execution successful.\n\n", codename);
182 }
183 return EXIT_SUCCESS;
184 }
185
186 namespace {
187 Ioss::PropertyManager set_properties(Skinner::Interface &interFace);
188
189 template <typename INT> void skinner(Skinner::Interface &interFace, INT /*dummy*/)
190 {
191 std::string inpfile = interFace.input_filename();
192 std::string input_type = interFace.input_type();
193
194 Ioss::PropertyManager properties = set_properties(interFace);
195
196 //========================================================================
197 // INPUT ...
198 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
199 //========================================================================
200 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::READ_RESTART,
201 Ioss::ParallelUtils::comm_world(), properties);
202 if (dbi == nullptr || !dbi->ok(true)) {
203 std::exit(EXIT_FAILURE);
204 }
205
206 if (interFace.ints_64_bit()) {
207 dbi->set_int_byte_size_api(Ioss::USE_INT64_API);
208 }
209
210 // NOTE: 'region' owns 'db' pointer at this time...
211 Ioss::Region region(dbi, "region_1");
212 int my_rank = region.get_database()->util().parallel_rank();
213
214 if (my_rank == 0) {
215 region.output_summary(std::cerr, false);
216 }
217
218 // Generate the faces...
219 Ioss::FaceGenerator face_generator(region);
220 face_generator.generate_faces((INT)0, true);
221
222 // Get vector of all boundary faces which will be output as the skin...
223
224 std::map<std::string, std::vector<Ioss::Face>> boundary_faces;
225 const Ioss::ElementBlockContainer &ebs = region.get_element_blocks();
226 for (auto &eb : ebs) {
227 const std::string &name = eb->name();
228 auto &boundary = boundary_faces[name];
229 auto &faces = face_generator.faces(name);
230 for (auto &face : faces) {
231 if (face.element_count() == 1) {
232 boundary.push_back(face);
233 }
234 }
235 }
236 output_table(ebs, boundary_faces);
237
238 if (interFace.no_output()) {
239 return;
240 }
241
242 // Iterate the boundary faces and determine which nodes are referenced...
243 size_t my_node_count = region.get_property("node_count").get_int();
244 Ioss::IntVector ref_nodes(my_node_count);
245 for (const auto &boundaries : boundary_faces) {
246 for (const auto &face : boundaries.second) {
247 for (auto &gnode : face.connectivity_) {
248 if (gnode > 0) {
249 auto node = region.get_database()->node_global_to_local(gnode, true) - 1;
250 ref_nodes[node] = 1;
251 }
252 }
253 }
254 }
255
256 size_t ref_count = std::accumulate(ref_nodes.begin(), ref_nodes.end(), 0);
257
258 // Map ids from total mesh down to skin mesh...
259 // Also get coordinates...
260 std::vector<double> coord_in;
261 Ioss::NodeBlock *nb = region.get_node_blocks()[0];
262 nb->get_field_data("mesh_model_coordinates", coord_in);
263
264 Ioss::IntVector ids;
265 nb->get_field_data("ids", ids);
266
267 Ioss::IntVector owner;
268 nb->get_field_data("owning_processor", owner);
269
270 Ioss::IntVector ref_ids(ref_count);
271 std::vector<double> coord_out(3 * ref_count);
272 Ioss::IntVector owner_out(ref_count);
273
274 size_t j = 0;
275 for (size_t i = 0; i < ref_nodes.size(); i++) {
276 if (ref_nodes[i] == 1) {
277 coord_out[3 * j + 0] = coord_in[3 * i + 0];
278 coord_out[3 * j + 1] = coord_in[3 * i + 1];
279 coord_out[3 * j + 2] = coord_in[3 * i + 2];
280 owner_out[j] = owner[i];
281 ref_ids[j++] = ids[i];
282 }
283 }
284 Ioss::Utils::clear(coord_in);
285 Ioss::Utils::clear(ids);
286 Ioss::Utils::clear(owner);
287
288 std::string file = interFace.output_filename();
289 std::string type = interFace.output_type();
290 Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(type, file, Ioss::WRITE_RESTART,
291 Ioss::ParallelUtils::comm_world(), properties);
292 if (dbo == nullptr || !dbo->ok(true)) {
293 std::exit(EXIT_FAILURE);
294 }
295
296 // NOTE: 'output_region' owns 'dbo' pointer at this time
297 Ioss::Region output_region(dbo, "skin");
298 output_region.property_add(Ioss::Property(std::string("code_name"), codename));
299 output_region.property_add(Ioss::Property(std::string("code_version"), version));
300
301 output_region.begin_mode(Ioss::STATE_DEFINE_MODEL);
302
303 auto *nbo = new Ioss::NodeBlock(output_region.get_database(), "nodeblock_1", ref_count, 3);
304
305 // Count number of nodes owned by this processor (owner_out[i] == myProcessor);
306 size_t owned = std::count_if(owner_out.begin(), owner_out.end(),
307 [my_rank](int i) { return i == my_rank; });
308 nbo->property_add(Ioss::Property("locally_owned_count", (INT)owned));
309
310 output_region.add(nbo);
311
312 // Output element blocks: will have a "skin" block for each input element block.
313 //
314 // NOTE: currently only handle a single skin face topology per
315 // input element block. Eventually may need to handle the case
316 // where there are two skin face topologies per input element
317 // block (wedge -> tri and quad).
318
319 // Count faces per element block and create output element block...
320 for (auto &eb : ebs) {
321 const std::string &name = eb->name();
322 const auto &boundary = boundary_faces[name];
323 auto face_topo = eb->topology()->face_type(0);
324 std::string topo = "shell";
325 if (face_topo == nullptr) {
326 IOSS_ERROR(fmt::format("ERROR: Block '{}' with topology '{}' does not have"
327 " a unique face topology.\nThis is not supported at this time.\n",
328 name, eb->topology()->name()));
329 }
330 if (face_topo->name() == "tri3") {
331 topo = "trishell";
332 }
333 auto block =
334 new Ioss::ElementBlock(output_region.get_database(), name, topo, boundary.size());
335 output_region.add(block);
336 }
337 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
338
339 output_region.begin_mode(Ioss::STATE_MODEL);
340 nbo->put_field_data("ids", ref_ids);
341 nbo->put_field_data("owning_processor", owner_out);
342 nbo->put_field_data("mesh_model_coordinates", coord_out);
343 Ioss::Utils::clear(coord_out);
344 Ioss::Utils::clear(owner_out);
345 Ioss::Utils::clear(ref_ids);
346
347 bool use_face_hash_ids = interFace.useFaceHashIds_;
348 INT fid = 0;
349 for (auto &eb : ebs) {
350 const std::string &name = eb->name();
351 const auto &boundary = boundary_faces[name];
352 auto *block = output_region.get_element_block(name);
353 size_t node_count = block->topology()->number_corner_nodes();
354
355 Ioss::IntVector conn;
356 Ioss::IntVector elids;
357 conn.reserve(node_count * boundary.size());
358 elids.reserve(boundary.size());
359
360 for (auto &face : boundary) {
361 if (use_face_hash_ids) {
362 fid = face.hashId_;
363 if (fid < 0) { // Due to (unsigned)size_t -> (signed)INT conversion
364 fid = -fid;
365 }
366 }
367 else {
368 fid = face.element[0];
369 }
370
371 for (size_t i = 0; i < node_count; i++) {
372 conn.push_back(face.connectivity_[i]);
373 }
374 elids.push_back(fid);
375 }
376 block->put_field_data("ids", elids);
377 block->put_field_data("connectivity", conn);
378 }
379 output_region.end_mode(Ioss::STATE_MODEL);
380
381 size_t ts_count = region.get_optional_property("state_count", 0);
382 if (ts_count > 0 && interFace.output_transient()) {
383
384 // Transfer all nodal variable names to the output database.
385 Ioss::NodeBlock *nbi = region.get_node_blocks()[0];
386
387 output_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
388 Ioss::NameList fields = nbi->field_describe(Ioss::Field::TRANSIENT);
389 for (const auto &field_name : fields) {
390 Ioss::Field field = nbi->get_field(field_name);
391 field.reset_count(ref_count);
392 nbo->field_add(field);
393 }
394
395 // All element block variables...
396 for (auto &eb : ebs) {
397 const std::string &name = eb->name();
398 auto *ebo = output_region.get_element_block(name);
399 if (ebo != nullptr) {
400 auto ebo_count = ebo->entity_count();
401 Ioss::NameList efields = eb->field_describe(Ioss::Field::TRANSIENT);
402 for (const auto &field_name : efields) {
403 Ioss::Field field = eb->get_field(field_name);
404 field.reset_count(ebo_count);
405 ebo->field_add(field);
406 }
407 }
408 }
409
410 output_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
411
412 auto selected_steps = get_selected_steps(region, interFace);
413
414 output_region.begin_mode(Ioss::STATE_TRANSIENT);
415 for (size_t istep = 1; istep <= ts_count; istep++) {
416 if (selected_steps[istep] != 1) {
417 continue;
418 }
419 double time = region.get_state_time(istep);
420 int ostep = output_region.add_state(time);
421 fmt::print(stderr, "\r\tTime step {:5d} at time {:10.5e}", ostep, time);
422
423 output_region.begin_state(ostep);
424 region.begin_state(istep);
425
426 transfer_field_data(nbi, nbo, Ioss::Field::TRANSIENT, ref_nodes);
427
428 // All element block variables...
429 for (auto &eb : ebs) {
430 const std::string &name = eb->name();
431 auto *ebo = output_region.get_element_block(name);
432 if (ebo != nullptr) {
433 transfer_field_data(eb, ebo, Ioss::Field::TRANSIENT, boundary_faces[name]);
434 }
435 }
436
437 output_region.end_state(ostep);
438 region.end_state(istep);
439 }
440 output_region.end_mode(Ioss::STATE_TRANSIENT);
441 }
442
443 if (my_rank == 0) {
444 output_region.output_summary(std::cerr, false);
445 }
446 }
447
448 Ioss::PropertyManager set_properties(Skinner::Interface &interFace)
449 {
450 Ioss::PropertyManager properties{};
451 if (interFace.ints_64_bit()) {
452 properties.add(Ioss::Property("INTEGER_SIZE_DB", 8));
453 properties.add(Ioss::Property("INTEGER_SIZE_API", 8));
454 }
455
456 if (interFace.debug) {
457 properties.add(Ioss::Property("LOGGING", 1));
458 }
459
460 if (!interFace.decomp_method.empty()) {
461 properties.add(Ioss::Property("DECOMPOSITION_METHOD", interFace.decomp_method));
462 }
463
464 if (interFace.compression_level > 0 || interFace.shuffle) {
465 properties.add(Ioss::Property("FILE_TYPE", "netcdf4"));
466 properties.add(Ioss::Property("COMPRESSION_LEVEL", interFace.compression_level));
467 properties.add(Ioss::Property("COMPRESSION_SHUFFLE", interFace.shuffle));
468 }
469
470 if (interFace.compose_output == "default") {
471 properties.add(Ioss::Property("COMPOSE_RESULTS", "NO"));
472 properties.add(Ioss::Property("COMPOSE_RESTART", "NO"));
473 }
474 else if (interFace.compose_output == "external") {
475 properties.add(Ioss::Property("COMPOSE_RESULTS", "NO"));
476 properties.add(Ioss::Property("COMPOSE_RESTART", "NO"));
477 }
478 else if (interFace.compose_output != "none") {
479 properties.add(Ioss::Property("COMPOSE_RESULTS", "YES"));
480 properties.add(Ioss::Property("COMPOSE_RESTART", "YES"));
481 }
482
483 if (interFace.netcdf4_) {
484 properties.add(Ioss::Property("FILE_TYPE", "netcdf4"));
485 }
486
487 return properties;
488 }
489
490 void transfer_field_data_internal(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
491 const std::string &field_name, const Ioss::IntVector &ref_nodes)
492 {
493 if (oge->field_exists(field_name)) {
494 std::vector<double> in;
495 ige->get_field_data(field_name, in);
496
497 // Determine component count...
498 auto field = ige->get_field(field_name);
499 int comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
500 std::vector<double> out;
501 out.reserve(oge->entity_count() * comp_count);
502
503 for (size_t i = 0; i < ref_nodes.size(); i++) {
504 if (ref_nodes[i] == 1) {
505 for (int j = 0; j < comp_count; j++) {
506 auto value = in[comp_count * i + j];
507 out.push_back(value);
508 }
509 }
510 }
511 assert(out.size() == (size_t)oge->entity_count() * (size_t)comp_count);
512 oge->put_field_data(field_name, out);
513 }
514 }
515
516 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
517 Ioss::Field::RoleType role, const Ioss::IntVector &ref_nodes)
518 {
519 // Iterate through the TRANSIENT-role fields of the input
520 // database and transfer to output database.
521 Ioss::NameList state_fields = ige->field_describe(role);
522
523 for (const auto &field_name : state_fields) {
524 if (oge->field_exists(field_name)) {
525 transfer_field_data_internal(ige, oge, field_name, ref_nodes);
526 }
527 }
528 }
529
530 void transfer_field_data_internal(Ioss::EntityBlock *ieb, Ioss::EntityBlock *oeb,
531 const std::string &field_name,
532 const std::vector<Ioss::Face> &boundary_faces)
533 {
534 if (oeb->field_exists(field_name)) {
535 std::vector<double> in;
536 ieb->get_field_data(field_name, in);
537
538 // Determine component count...
539 auto field = ieb->get_field(field_name);
540 int comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
541 std::vector<double> out;
542 out.reserve(oeb->entity_count() * comp_count);
543
544 auto offset = ieb->get_offset();
545 for (const auto &face : boundary_faces) {
546 auto element_id = face.element[0] / 10 - 1;
547 for (int j = 0; j < comp_count; j++) {
548 auto value = in[comp_count * (element_id - offset) + j];
549 out.push_back(value);
550 }
551 }
552 assert(out.size() == (size_t)oeb->entity_count() * (size_t)comp_count);
553 oeb->put_field_data(field_name, out);
554 }
555 }
556
557 void transfer_field_data(Ioss::EntityBlock *ieb, Ioss::EntityBlock *oeb,
558 Ioss::Field::RoleType role,
559 const std::vector<Ioss::Face> &boundary_faces)
560 {
561 // Iterate through the TRANSIENT-role fields of the input
562 // database and transfer to output database.
563 Ioss::NameList state_fields = ieb->field_describe(role);
564
565 for (const auto &field_name : state_fields) {
566 if (oeb->field_exists(field_name)) {
567 transfer_field_data_internal(ieb, oeb, field_name, boundary_faces);
568 }
569 }
570 }
571
572 } // namespace