Debian Codegraph @

packages/seacas/libraries/ioss/src/main/shell_to_hex.C

raw
1 // Copyright(C) 1999-2023 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 "Ionit_Initializer.h"
8 #include "Ioss_Hex8.h"
9 #include "Ioss_Wedge6.h"
10 #include <cassert>
11 #include <cstdlib>
12 #include <cstring>
13 #include <fstream>
14 #include <iomanip>
15 #include <iostream>
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
19
20 #include "Ioss_DBUsage.h"
21 #include "Ioss_DatabaseIO.h"
22 #include "Ioss_ElementBlock.h"
23 #include "Ioss_ElementTopology.h"
24 #include "Ioss_GroupingEntity.h"
25 #include "Ioss_IOFactory.h"
26 #include "Ioss_NodeBlock.h"
27 #include "Ioss_ParallelUtils.h"
28 #include "Ioss_Property.h"
29 #include "Ioss_Region.h"
30 #include "Ioss_ScopeGuard.h"
31 #include "Ioss_State.h"
32 #include "Ioss_Utils.h"
33 #include "vector3d.h"
34
35 // ========================================================================
36
37 namespace {
38
39 struct Globals
40 {
41 bool debug{};
42 bool do_normals{};
43 bool reverse_normals{};
44 double thickness{};
45 std::string working_directory{};
46 };
47
48 void show_usage(const std::string &prog);
49
50 void transfer_nodeblock(Ioss::Region &region, Ioss::Region &output_region, bool debug);
51 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region, bool debug);
52 void transfer_properties(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge);
53
54 void output_normals(Ioss::Region &region, Ioss::Region &output_region, bool reverse_normals,
55 double thickness);
56 void calculate_normals(std::vector<double> &node_normal, int num_elem, int num_node_per_elem,
57 std::vector<double> &coord, std::vector<int> &conn, bool reverse_normals);
58
59 void file_copy(const std::string &inpfile, const std::string &input_type,
60 const std::string &outfile, const std::string &output_type, Globals &globals);
61 } // namespace
62 // ========================================================================
63
64 namespace {
65 std::string codename;
66 std::string version = "0.9";
67 } // namespace
68
69 int main(int argc, char *argv[])
70 {
71 #ifdef SEACAS_HAVE_MPI
72 MPI_Init(&argc, &argv);
73 ON_BLOCK_EXIT(MPI_Finalize);
74 #endif
75
76 std::string in_type = "exodusII";
77 std::string out_type = "exodusII";
78
79 Globals globals;
80
81 globals.do_normals = false;
82 globals.reverse_normals = false;
83 globals.thickness = 1.0;
84
85 codename = argv[0];
86 size_t ind = codename.find_last_of('/', codename.size());
87 if (ind != std::string::npos) {
88 codename = codename.substr(ind + 1, codename.size());
89 }
90
91 // Check the program name to see if of the form 'exosaf' or 'safexo'
92 // and if it is, set the in_type and out_type accordingly...
93 if (Ioss::Utils::str_equal(codename, "shell_to_hex")) {
94 codename = "shell_to_hex";
95 in_type = "exodusII";
96 out_type = "exodusII";
97 globals.do_normals = true;
98 }
99 else {
100 codename = "SIERRA";
101 }
102
103 Ioss::Init::Initializer io;
104
105 globals.debug = false;
106
107 // Skip past any options...
108 int i = 1;
109 while (i < argc && argv[i][0] == '-') {
110 if (std::strcmp("-directory", argv[i]) == 0 || std::strcmp("--directory", argv[i]) == 0 ||
111 std::strcmp("-d", argv[i]) == 0) {
112 i++;
113 globals.working_directory = argv[i++];
114 }
115 else if (std::strcmp("--reverse", argv[i]) == 0) {
116 globals.reverse_normals = true;
117 i++;
118 }
119 else if (std::strcmp("--Reverse_Normals", argv[i]) == 0) {
120 globals.reverse_normals = true;
121 i++;
122 }
123 else if (std::strcmp("--Calculate_Normals", argv[i]) == 0) {
124 globals.do_normals = true;
125 i++;
126 }
127 else if (std::strcmp("--thickness", argv[i]) == 0) {
128 i++;
129 globals.thickness = std::strtod(argv[i++], nullptr);
130 }
131
132 // Found an option. See if it has an argument...
133 else if (i + 1 < argc && argv[i + 1][0] == '-') {
134 // No argument, another option
135 i++;
136 }
137 else {
138 // Skip the argument...
139 i += 2;
140 }
141 }
142
143 std::string in_file;
144 std::string out_file;
145
146 // If a single argument is specified at end of command line, it is
147 // input file which contains the names and types of the files to be
148 // converted.
149 // If two or more arguments are specified at end of command line, they are
150 // the input and output files to be converted.
151 // The file types are assumed to be as 'hardwired' above...
152
153 if (argc - i == 2) {
154 in_file = Ioss::Utils::local_filename(argv[i++], in_type, globals.working_directory);
155 out_file = Ioss::Utils::local_filename(argv[i++], out_type, globals.working_directory);
156 }
157 else if (argc - i == 1) {
158 std::string input_file =
159 Ioss::Utils::local_filename(argv[i++], "text", globals.working_directory);
160
161 std::ifstream input(input_file);
162 if (!input) {
163 std::cerr << "Error opening file '" << input_file << "'.\n";
164 show_usage(codename);
165 return (EXIT_FAILURE);
166 }
167 // First line should be input_file_name input_file_type
168 // Last line should be output_file_name output_file_type
169 std::string tmp;
170 input >> tmp >> in_type;
171 in_file = Ioss::Utils::local_filename(tmp, in_type, globals.working_directory);
172
173 input >> tmp >> out_type;
174 out_file = Ioss::Utils::local_filename(tmp, out_type, globals.working_directory);
175 }
176 else {
177 show_usage(codename);
178 return (EXIT_FAILURE);
179 }
180
181 std::cerr << "Input: '" << in_file << "', Type: " << in_type << '\n';
182 std::cerr << "Output: '" << out_file << "', Type: " << out_type << '\n';
183 if (globals.reverse_normals) {
184 std::cerr << "Reversing Normals\n";
185 }
186 std::cerr << "Thickness: " << globals.thickness << "\n";
187 std::cerr << '\n';
188
189 if (globals.do_normals) {
190 globals.debug = false;
191 }
192
193 file_copy(in_file, in_type, out_file, out_type, globals);
194
195 std::cerr << "\n" << codename << " execution successful.\n";
196 return EXIT_SUCCESS;
197 }
198
199 namespace {
200 void show_usage(const std::string &prog)
201 {
202 std::cerr << "\nUSAGE: " << prog << " in_file out_file\n";
203
204 std::cerr << "...or: " << prog << " command_file\n";
205 std::cerr << " version: " << version << "\n";
206 Ioss::NameList db_types = Ioss::IOFactory::describe();
207 std::cerr << "\nSupports database types:\n\t";
208 for (const auto &db : db_types) {
209 std::cerr << db << " ";
210 }
211 std::cerr << "\n\n";
212 }
213
214 void file_copy(const std::string &inpfile, const std::string &input_type,
215 const std::string &outfile, const std::string &output_type, Globals &globals)
216 {
217 //========================================================================
218 // INPUT ...
219 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
220 //========================================================================
221 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::READ_RESTART,
222 Ioss::ParallelUtils::comm_world());
223 if (dbi == nullptr || !dbi->ok()) {
224 std::cerr << "ERROR: Could not open database '" << inpfile << "' of type '" << input_type
225 << "'\n";
226 std::exit(EXIT_FAILURE);
227 }
228
229 // NOTE: 'region' owns 'db' pointer at this time...
230 Ioss::Region region(dbi, "region_1");
231
232 //========================================================================
233 // OUTPUT ...
234 //========================================================================
235 Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(output_type, outfile, Ioss::WRITE_RESTART,
236 Ioss::ParallelUtils::comm_world());
237 if (dbo == nullptr || !dbo->ok()) {
238 std::cerr << "ERROR: Could not create output database '" << outfile << "' of type '"
239 << output_type << "'\n";
240 std::exit(EXIT_FAILURE);
241 }
242
243 // NOTE: 'output_region' owns 'dbo' pointer at this time
244 Ioss::Region output_region(dbo, "region_2");
245
246 // Set the qa information...
247 output_region.property_add(Ioss::Property(std::string("code_name"), codename));
248 output_region.property_add(Ioss::Property(std::string("code_version"), version));
249
250 if (globals.debug) {
251 std::cerr << "DEFINING MODEL ... \n";
252 }
253 if (!output_region.begin_mode(Ioss::STATE_DEFINE_MODEL)) {
254 std::cerr << "ERROR: Could not put output region into define model state\n";
255 std::exit(EXIT_FAILURE);
256 }
257
258 // Get all properties of input database...
259 transfer_properties(&region, &output_region);
260 transfer_nodeblock(region, output_region, globals.debug);
261 transfer_elementblock(region, output_region, globals.debug);
262
263 if (globals.debug) {
264 std::cerr << "END STATE_DEFINE_MODEL... " << '\n';
265 }
266 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
267
268 if (globals.debug) {
269 std::cerr << "TRANSFERRING MESH FIELD DATA ... " << '\n';
270 // Model defined, now fill in the model data...
271 }
272 output_region.begin_mode(Ioss::STATE_MODEL);
273 output_normals(region, output_region, globals.reverse_normals, globals.thickness);
274 output_region.end_mode(Ioss::STATE_MODEL);
275 }
276
277 void transfer_nodeblock(Ioss::Region &region, Ioss::Region &output_region, bool debug)
278 {
279 const auto &nbs = region.get_node_blocks();
280 auto i = nbs.begin();
281 while (i != nbs.end()) {
282 const std::string &name = (*i)->name();
283 if (debug) {
284 std::cerr << name << ", ";
285 }
286 auto num_nodes = (*i)->entity_count();
287 int degree = (*i)->get_property("component_degree").get_int();
288 if (!debug) {
289 std::cerr << " Number of coordinates per node =" << std::setw(9) << degree << "\n";
290 std::cerr << " Number of nodes =" << std::setw(9) << num_nodes << "\n";
291 }
292
293 auto nb = new Ioss::NodeBlock(output_region.get_database(), name, 2 * num_nodes, degree);
294 output_region.add(nb);
295 ++i;
296 }
297 if (debug) {
298 std::cerr << '\n';
299 }
300 }
301
302 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region, bool debug)
303 {
304 const auto &ebs = region.get_element_blocks();
305 auto i = ebs.begin();
306 size_t total_elements = 0;
307 while (i != ebs.end()) {
308 const std::string &name = (*i)->name();
309 if (debug) {
310 std::cerr << name << ", ";
311 }
312 int num_elem = (*i)->entity_count();
313 total_elements += num_elem;
314
315 std::string type;
316 int num_node_per_elem = (*i)->topology()->number_nodes();
317 if (num_node_per_elem == 4) {
318 type = Ioss::Hex8::name;
319 }
320 else {
321 type = Ioss::Wedge6::name;
322 }
323 auto eb = new Ioss::ElementBlock(output_region.get_database(), name, type, num_elem);
324 output_region.add(eb);
325 ++i;
326 }
327 if (!debug) {
328 std::cerr << " Number of elements =" << std::setw(9) << total_elements
329 << "\n";
330 std::cerr << " Number of element blocks =" << std::setw(9) << ebs.size()
331 << "\n\n";
332 }
333 else {
334 std::cerr << '\n';
335 }
336 }
337
338 void transfer_properties(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge)
339 {
340 auto names = ige->property_describe();
341 // Iterate through properties and transfer to output database...
342 for (const auto &name : names) {
343 if (!oge->property_exists(name)) {
344 oge->property_add(ige->get_property(name));
345 }
346 }
347 }
348
349 void output_normals(Ioss::Region &region, Ioss::Region &output_region, bool reverse_normals,
350 double thickness)
351 {
352 Ioss::NodeBlock *nb = (*region.get_node_blocks().begin());
353 Ioss::NodeBlock *nbo = (*output_region.get_node_blocks().begin());
354
355 // Get the nodal coordinates...
356 auto num_nodes = nb->entity_count();
357
358 {
359 std::vector<int> ids(2 * num_nodes);
360 for (int i = 0; i < num_nodes; i++) {
361 ids[i] = i + 1;
362 ids[num_nodes + i] = num_nodes + i + 1;
363 }
364 nbo->put_field_data("ids", ids);
365 }
366
367 std::vector<double> coord(3 * num_nodes);
368 nb->get_field_data("mesh_model_coordinates", coord);
369
370 // Also get an array for the average nodal normal vector...
371 std::vector<double> node_normal(3 * num_nodes);
372 std::fill(node_normal.begin(), node_normal.end(), 0.0);
373
374 // Iterate over the element blocks and calculate node normals
375 std::vector<int> conn;
376 std::vector<int> output_conn;
377 const auto &ebs = region.get_element_blocks();
378 const auto &out_ebs = output_region.get_element_blocks();
379
380 auto ib = ebs.begin();
381 auto out_ib = out_ebs.begin();
382 while (ib != ebs.end() && out_ib != out_ebs.end()) {
383 Ioss::ElementBlock *eb = *ib;
384 ++ib;
385 Ioss::ElementBlock *out_eb = *out_ib;
386 ++out_ib;
387
388 auto num_elem = eb->entity_count();
389 auto num_node_per_elem = eb->topology()->number_nodes();
390
391 // Get the connectivity array...
392 conn.resize(num_elem * num_node_per_elem);
393 output_conn.resize(2 * conn.size());
394
395 std::vector<int> eids(num_elem);
396 eb->get_field_data("ids", eids);
397 out_eb->put_field_data("ids", eids);
398
399 eb->get_field_data("connectivity", conn);
400
401 // Connectivity is in global id space; change to local...
402 for (int64_t i = 0; i < num_elem * num_node_per_elem; i++) {
403 int local = region.node_global_to_local(conn[i]);
404 conn[i] = local - 1;
405 }
406
407 calculate_normals(node_normal, num_elem, num_node_per_elem, coord, conn, reverse_normals);
408
409 assert(num_node_per_elem == 3 || num_node_per_elem == 4);
410 if (reverse_normals) {
411 for (int i = 0; i < num_elem; i++) {
412 if (num_node_per_elem == 4) {
413 output_conn[8 * i + 0] = 1 + conn[4 * i + 0] + num_nodes;
414 output_conn[8 * i + 1] = 1 + conn[4 * i + 1] + num_nodes;
415 output_conn[8 * i + 2] = 1 + conn[4 * i + 2] + num_nodes;
416 output_conn[8 * i + 3] = 1 + conn[4 * i + 3] + num_nodes;
417
418 output_conn[8 * i + 4] = 1 + conn[4 * i + 0];
419 output_conn[8 * i + 5] = 1 + conn[4 * i + 1];
420 output_conn[8 * i + 6] = 1 + conn[4 * i + 2];
421 output_conn[8 * i + 7] = 1 + conn[4 * i + 3];
422 }
423 else {
424 output_conn[6 * i + 0] = 1 + conn[3 * i + 0] + num_nodes;
425 output_conn[6 * i + 1] = 1 + conn[3 * i + 1] + num_nodes;
426 output_conn[6 * i + 2] = 1 + conn[3 * i + 2] + num_nodes;
427
428 output_conn[6 * i + 4] = 1 + conn[3 * i + 0];
429 output_conn[6 * i + 5] = 1 + conn[3 * i + 1];
430 output_conn[6 * i + 6] = 1 + conn[3 * i + 2];
431 }
432 }
433 }
434 else {
435 for (int i = 0; i < num_elem; i++) {
436 if (num_node_per_elem == 4) {
437 output_conn[8 * i + 0] = 1 + conn[4 * i + 0];
438 output_conn[8 * i + 1] = 1 + conn[4 * i + 1];
439 output_conn[8 * i + 2] = 1 + conn[4 * i + 2];
440 output_conn[8 * i + 3] = 1 + conn[4 * i + 3];
441
442 output_conn[8 * i + 4] = 1 + conn[4 * i + 0] + num_nodes;
443 output_conn[8 * i + 5] = 1 + conn[4 * i + 1] + num_nodes;
444 output_conn[8 * i + 6] = 1 + conn[4 * i + 2] + num_nodes;
445 output_conn[8 * i + 7] = 1 + conn[4 * i + 3] + num_nodes;
446 }
447 else {
448 output_conn[6 * i + 0] = 1 + conn[3 * i + 0];
449 output_conn[6 * i + 1] = 1 + conn[3 * i + 1];
450 output_conn[6 * i + 2] = 1 + conn[3 * i + 2];
451
452 output_conn[6 * i + 4] = 1 + conn[3 * i + 0] + num_nodes;
453 output_conn[6 * i + 5] = 1 + conn[3 * i + 1] + num_nodes;
454 output_conn[6 * i + 6] = 1 + conn[3 * i + 2] + num_nodes;
455 }
456 }
457 }
458 out_eb->put_field_data("connectivity", output_conn);
459 }
460
461 int nsize = node_normal.size();
462 for (int i = 0; i < nsize; i += 3) {
463 vector3d a(node_normal[i + 0], node_normal[i + 1], node_normal[i + 2]);
464 a.normalize();
465
466 node_normal[i + 0] = a.x;
467 node_normal[i + 1] = a.y;
468 node_normal[i + 2] = a.z;
469 }
470
471 // The output created nodes of the new hexes are simply the input nodes
472 // translated along the nodal vector the specified distance. The node id is
473 // simply the input node id + number_of_input_nodes.
474 std::vector<double> output_coord(2 * num_nodes * 3);
475 for (int i = 0; i < num_nodes; i++) {
476 output_coord[3 * i + 0] = coord[3 * i + 0];
477 output_coord[3 * i + 1] = coord[3 * i + 1];
478 output_coord[3 * i + 2] = coord[3 * i + 2];
479
480 output_coord[3 * (num_nodes + i) + 0] = coord[3 * i + 0] + thickness * node_normal[3 * i + 0];
481 output_coord[3 * (num_nodes + i) + 1] = coord[3 * i + 1] + thickness * node_normal[3 * i + 1];
482 output_coord[3 * (num_nodes + i) + 2] = coord[3 * i + 2] + thickness * node_normal[3 * i + 2];
483 }
484 nbo->put_field_data("mesh_model_coordinates", output_coord);
485 }
486
487 void calculate_normals(std::vector<double> &node_normal, int num_elem, int num_node_per_elem,
488 std::vector<double> &coord, std::vector<int> &conn, bool reverse_normals)
489 {
490 // Iterate the connectivity array and calculate normals...
491 // The elements should all be shells with the outward normal
492 // defined by the node ordering 1.2.3.
493
494 for (int iel = 0; iel < num_elem; iel++) {
495 int ioff = iel * num_node_per_elem;
496
497 // Triangular faces...
498 if (num_node_per_elem == 3) {
499 vector3d local[3];
500 for (int i = 0; i < 3; i++) {
501 int node = conn[ioff + i];
502 local[i].set(coord[node * 3 + 0], coord[node * 3 + 1], coord[node * 3 + 2]);
503 }
504
505 vector3d plnorm = vector3d::plane_normal(local[0], local[1], local[2]);
506 plnorm.normalize();
507 if (reverse_normals) {
508 plnorm.reverse();
509 }
510
511 for (int i = 0; i < 3; i++) {
512 int node = conn[ioff + i];
513 node_normal[node * 3 + 0] += plnorm.x;
514 node_normal[node * 3 + 1] += plnorm.y;
515 node_normal[node * 3 + 2] += plnorm.z;
516 }
517 }
518 else {
519
520 // Quadrilateral faces...
521 assert(num_node_per_elem == 4);
522 vector3d local[4];
523 for (int i = 0; i < 4; i++) {
524 int node = conn[ioff + i];
525 local[i].set(&coord[node * 3]);
526 }
527
528 for (int i = 0; i < 4; i++) {
529 // at node 0 -- vector from 3-0 X 0-1
530 // at node 1 -- vector from 0-1 X 1-2
531 // at node 2 -- vector from 1-2 X 2-3
532 // at node 3 -- vector from 2-3 X 3-0
533 int nb = (i + 3) % 4;
534 int na = (i + 1) % 4;
535
536 vector3d a = vector3d::plane_normal(local[nb], local[i], local[na]);
537 a.normalize();
538 if (reverse_normals) {
539 a.reverse();
540 }
541
542 int node = conn[ioff + i];
543 node_normal[node * 3 + 0] += a.x;
544 node_normal[node * 3 + 1] += a.y;
545 node_normal[node * 3 + 2] += a.z;
546 }
547 }
548 }
549 }
550 } // namespace