Debian Codegraph @

packages/seacas/libraries/ioss/src/main/cth_pressure_map.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_Utils.h"
9 #include <cassert>
10 #include <cstdlib>
11 #include <cstring>
12 #include <fstream>
13 #include <iomanip>
14 #include <iostream>
15 #include <map>
16 #include <stdexcept>
17 #include <string>
18 #include <vector>
19
20 #include "Ioss_CommSet.h"
21 #include "Ioss_DBUsage.h"
22 #include "Ioss_DatabaseIO.h"
23 #include "Ioss_ElementBlock.h"
24 #include "Ioss_ElementTopology.h"
25 #include "Ioss_EntityType.h"
26 #include "Ioss_Field.h"
27 #include "Ioss_GroupingEntity.h"
28 #include "Ioss_IOFactory.h"
29 #include "Ioss_NodeBlock.h"
30 #include "Ioss_NodeSet.h"
31 #include "Ioss_ParallelUtils.h"
32 #include "Ioss_Property.h"
33 #include "Ioss_Region.h"
34 #include "Ioss_ScopeGuard.h"
35 #include "Ioss_SideBlock.h"
36 #include "Ioss_SideSet.h"
37 #include "Ioss_State.h"
38 #include "Ioss_VariableType.h"
39 #include "vector3d.h"
40
41 // ========================================================================
42
43 namespace {
44
45 // Data space shared by most field input/output routines...
46 std::vector<char> data;
47
48 struct Globals
49 {
50 enum FinalPressureType { FINAL, INITIAL, ZERO, OFFSET };
51 bool debug{};
52 bool do_normals{};
53 bool reverse_normals{};
54 bool add_sset{};
55 bool convert_gage{};
56 FinalPressureType final_pressure{FINAL};
57 double delta_time{};
58 double maximum_time{};
59 double minimum_time{};
60 double offset_time{};
61 double offset_pressure{};
62 std::string working_directory{};
63 };
64
65 void show_usage(const std::string &prog, bool add_sset);
66 void show_step(int istep, double time);
67
68 void transfer_nodeblock(Ioss::Region &region, Ioss::Region &output_region, bool debug);
69 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region, bool debug);
70 void transfer_nodesets(Ioss::Region &region, Ioss::Region &output_region, bool debug);
71 void transfer_sidesets(Ioss::Region &region, Ioss::Region &output_region, bool debug);
72 void transfer_commsets(Ioss::Region &region, Ioss::Region &output_region, bool debug);
73 void add_sideset(Ioss::Region &ss_region, Ioss::Region &region, Ioss::Region &output_region,
74 Globals &globals);
75 void add_sideset_mesh_fields(Ioss::Region &ss_region, Ioss::Region &region,
76 Ioss::Region &output_region, Globals &globals);
77 void add_sideset_transient_fields(Ioss::Region &ss_region, Ioss::Region &region,
78 Ioss::Region &output_region, Globals &globals);
79
80 void transfer_sideset_field_data(Ioss::Region &ss_region, Ioss::Region &region,
81 Ioss::Region &output_region, Globals &globals);
82
83 void transfer_fields(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
84 Ioss::Field::RoleType role, const std::string &prefix = "");
85
86 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
87 Ioss::Field::RoleType role, const std::string &prefix = "",
88 bool transfer_connectivity = true);
89
90 void transfer_properties(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge);
91
92 void output_normals(Ioss::Region &region, Ioss::Region &output_region, bool reverse_normals);
93 void calculate_normals(std::vector<double> &node_normal, std::vector<double> &face_normal,
94 int num_elem, int num_node_per_elem, std::vector<double> &coord,
95 std::vector<int> &conn, bool reverse_normals);
96 void transfer_field_data_internal(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
97 const std::string &field_name);
98
99 void file_copy(const std::string &inpfile, const std::string &input_type,
100 const std::string &outfile, const std::string &output_type,
101 const std::string &ss_file, Globals &globals);
102 } // namespace
103 // ========================================================================
104
105 namespace {
106 std::string codename;
107 std::string version = "$Revision$";
108 } // namespace
109
110 int main(int argc, char *argv[])
111 {
112 #ifdef SEACAS_HAVE_MPI
113 MPI_Init(&argc, &argv);
114 ON_BLOCK_EXIT(MPI_Finalize);
115 #endif
116
117 std::string in_type = "exodusII";
118 std::string out_type = "exodusII";
119 std::string ss_type = "exodusII";
120
121 Globals globals;
122
123 globals.do_normals = false;
124 globals.reverse_normals = false;
125 globals.add_sset = true; // This is the CTH Pressure map executable
126 globals.convert_gage = false;
127 globals.final_pressure = Globals::FINAL;
128 globals.delta_time = 0.0;
129 globals.maximum_time = 0.0;
130 globals.minimum_time = 0.0;
131 globals.offset_time = 0.0;
132 globals.offset_pressure = 0.0;
133
134 codename = argv[0];
135 size_t ind = codename.find_last_of('/', codename.size());
136 if (ind != std::string::npos) {
137 codename = codename.substr(ind + 1, codename.size());
138 }
139
140 Ioss::Init::Initializer io;
141
142 globals.debug = false;
143
144 std::string input_file;
145 // Skip past any options...
146 int i = 1;
147 while (i < argc && argv[i][0] == '-') {
148 if (std::strcmp("-directory", argv[i]) == 0 || std::strcmp("--directory", argv[i]) == 0 ||
149 std::strcmp("-d", argv[i]) == 0) {
150 i++;
151 globals.working_directory = argv[i++];
152 }
153 else if (std::strcmp("-i", argv[i]) == 0) {
154 i++;
155 input_file = argv[i++];
156 }
157 else if (std::strcmp("-reverse", argv[i]) == 0) {
158 globals.reverse_normals = true;
159 i++;
160 }
161 else if (std::strcmp("--Reverse_Normals", argv[i]) == 0) {
162 globals.reverse_normals = true;
163 i++;
164 }
165 else if (std::strcmp("--Add_Surface_Fields", argv[i]) == 0) {
166 globals.add_sset = true;
167 i++;
168 }
169 else if (std::strcmp("--Calculate_Normals", argv[i]) == 0) {
170 globals.do_normals = true;
171 globals.add_sset = false;
172 i++;
173 }
174 else if (std::strcmp("--Convert_Gage", argv[i]) == 0) {
175 globals.convert_gage = true;
176 i++;
177 }
178 else if (std::strcmp("--Final_Pressure", argv[i]) == 0) {
179 i++;
180 if (std::strcmp("ZERO", argv[i]) == 0) {
181 globals.final_pressure = Globals::ZERO;
182 }
183 else if (std::strcmp("INITIAL", argv[i]) == 0) {
184 globals.final_pressure = Globals::INITIAL;
185 }
186 else if (std::strcmp("FINAL", argv[i]) == 0) {
187 globals.final_pressure = Globals::FINAL;
188 }
189 else if (std::strcmp("OFFSET", argv[i]) == 0) {
190 globals.final_pressure = Globals::OFFSET;
191 }
192 i++;
193 }
194 else if (std::strcmp("--Final_Time_Delta", argv[i]) == 0) {
195 i++;
196 globals.delta_time = std::strtod(argv[i++], nullptr);
197 }
198 else if (std::strcmp("--Maximum_Time", argv[i]) == 0) {
199 i++;
200 globals.maximum_time = std::strtod(argv[i++], nullptr);
201 }
202 else if (std::strcmp("--Minimum_Time", argv[i]) == 0) {
203 i++;
204 globals.minimum_time = std::strtod(argv[i++], nullptr);
205 }
206 else if (std::strcmp("--Offset_Time", argv[i]) == 0) {
207 i++;
208 globals.offset_time = std::strtod(argv[i++], nullptr);
209 }
210 else if (std::strcmp("--Offset_Pressure", argv[i]) == 0) {
211 i++;
212 globals.offset_pressure = std::strtod(argv[i++], nullptr);
213 }
214
215 // Found an option. See if it has an argument...
216 else if (i + 1 < argc && argv[i + 1][0] == '-') {
217 // No argument, another option
218 i++;
219 }
220 else {
221 // Skip the argument...
222 i += 2;
223 }
224 }
225
226 std::string in_file;
227 std::string sset_file;
228 std::string out_file;
229
230 // If a single argument is specified at end of command line, it is
231 // input file which contains the names and types of the files to be
232 // converted.
233 // If two or more arguments are specified at end of command line, they are
234 // the input and output files to be converted.
235 // The file types are assumed to be as 'hardwired' above...
236
237 std::string cwd = globals.working_directory;
238
239 if (input_file.empty()) {
240 if (argc - i == 3) {
241 in_file = Ioss::Utils::local_filename(argv[i++], in_type, cwd);
242 sset_file = Ioss::Utils::local_filename(argv[i++], ss_type, cwd);
243 out_file = Ioss::Utils::local_filename(argv[i++], out_type, cwd);
244 }
245 else if (argc - i == 2) {
246 in_file = Ioss::Utils::local_filename(argv[i++], in_type, cwd);
247 out_file = Ioss::Utils::local_filename(argv[i++], out_type, cwd);
248 }
249 else if (argc - i == 1) {
250 input_file = Ioss::Utils::local_filename(argv[i++], "text", cwd);
251 }
252 }
253
254 if (!input_file.empty()) {
255 std::ifstream input(input_file);
256 if (!input) {
257 std::cerr << "Error opening file '" << input_file << "'.\n";
258 show_usage(codename, globals.add_sset);
259 return (EXIT_FAILURE);
260 }
261 // First line should be input_file_name input_file_type
262 // If (add_sset), second line is sset file.
263 // Last line should be output_file_name output_file_type
264 std::string tmp;
265 input >> tmp >> in_type;
266 in_file = Ioss::Utils::local_filename(tmp, in_type, cwd);
267
268 if (globals.add_sset) {
269 input >> tmp >> ss_type;
270 sset_file = Ioss::Utils::local_filename(tmp, ss_type, cwd);
271 }
272
273 input >> tmp >> out_type;
274 out_file = Ioss::Utils::local_filename(tmp, out_type, cwd);
275 }
276 if (in_file.empty() || out_file.empty() || sset_file.empty()) {
277 show_usage(codename, globals.add_sset);
278 return (EXIT_FAILURE);
279 }
280
281 std::cerr << "Input: '" << in_file << "', Type: " << in_type << '\n';
282 if (globals.add_sset) {
283 std::cerr << "Pressure: '" << sset_file << "', Type: " << in_type << '\n';
284 }
285 std::cerr << "Output: '" << out_file << "', Type: " << out_type << '\n';
286 std::cerr << '\n';
287
288 if (globals.add_sset || globals.do_normals) {
289 globals.debug = false;
290 }
291
292 if (!(globals.add_sset || globals.do_normals)) {
293 std::cerr << "\n" << codename << "ERROR: Either add sideset or do normals must be selected.\n";
294 return EXIT_FAILURE;
295 }
296
297 file_copy(in_file, in_type, out_file, out_type, sset_file, globals);
298
299 std::cerr << "\n" << codename << " execution successful.\n";
300 return EXIT_SUCCESS;
301 }
302
303 namespace {
304 void show_usage(const std::string &prog, bool add_sset)
305 {
306 if (add_sset) {
307 std::cerr << "\nUSAGE: " << prog << " in_file sset_file out_file\n";
308 }
309 else {
310 std::cerr << "\nUSAGE: " << prog << " in_file out_file\n";
311 }
312 std::cerr << "...or: " << prog << " command_file\n";
313 std::cerr << " version: " << version << "\n";
314 Ioss::NameList db_types = Ioss::IOFactory::describe();
315 std::cerr << "\nSupports database types:\n\t";
316 for (const auto &db : db_types) {
317 std::cerr << db << " ";
318 }
319 std::cerr << "\n\n";
320 }
321
322 void file_copy(const std::string &inpfile, const std::string &input_type,
323 const std::string &outfile, const std::string &output_type,
324 const std::string &ss_file, Globals &globals)
325 {
326 //========================================================================
327 // INPUT ...
328 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
329 //========================================================================
330 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(input_type, inpfile, Ioss::READ_RESTART,
331 Ioss::ParallelUtils::comm_world());
332 if (dbi == nullptr || !dbi->ok()) {
333 std::cerr << "ERROR: Could not open database '" << inpfile << "' of type '" << input_type
334 << "'\n";
335 std::exit(EXIT_FAILURE);
336 }
337
338 // NOTE: 'region' owns 'db' pointer at this time...
339 Ioss::Region region(dbi, "region_1");
340
341 //========================================================================
342 // OUTPUT ...
343 //========================================================================
344 Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(output_type, outfile, Ioss::WRITE_RESTART,
345 Ioss::ParallelUtils::comm_world());
346 if (dbo == nullptr || !dbo->ok()) {
347 std::cerr << "ERROR: Could not create output database '" << outfile << "' of type '"
348 << output_type << "'\n";
349 std::exit(EXIT_FAILURE);
350 }
351
352 // NOTE: 'output_region' owns 'dbo' pointer at this time
353 Ioss::Region output_region(dbo, "region_2");
354
355 //========================================================================
356 // Optional sideset file...
357 // NOTE: The "READ_RESTART" mode ensures that the node and element ids will be mapped.
358 //========================================================================
359 Ioss::DatabaseIO *dbs = nullptr;
360 Ioss::Region *ss_region = nullptr;
361 if (globals.add_sset) {
362 // Open the file containing the data which will be the new sideset...
363 dbs = Ioss::IOFactory::create(input_type, ss_file, Ioss::READ_RESTART,
364 Ioss::ParallelUtils::comm_world());
365 if (dbs == nullptr || !dbs->ok()) {
366 std::cerr << "ERROR: Could not open database '" << ss_file << "' of type '" << input_type
367 << "'\n";
368 std::exit(EXIT_FAILURE);
369 }
370
371 // NOTE: 'region' owns 'db' pointer at this time...
372 ss_region = new Ioss::Region(dbs, "sideset_region");
373 }
374
375 // Set the qa information...
376 output_region.property_add(Ioss::Property(std::string("code_name"), codename));
377 output_region.property_add(Ioss::Property(std::string("code_version"), version));
378
379 if (globals.debug) {
380 std::cerr << "DEFINING MODEL ... \n";
381 }
382 if (!output_region.begin_mode(Ioss::STATE_DEFINE_MODEL)) {
383 std::cerr << "ERROR: Could not put output region into define model state\n";
384 std::exit(EXIT_FAILURE);
385 }
386
387 // Get all properties of input database...
388 transfer_properties(&region, &output_region);
389 transfer_nodeblock(region, output_region, globals.debug);
390 transfer_elementblock(region, output_region, globals.debug);
391 transfer_nodesets(region, output_region, globals.debug);
392 if (!globals.do_normals) {
393 transfer_sidesets(region, output_region, globals.debug);
394 }
395 transfer_commsets(region, output_region, globals.debug);
396
397 // If adding the sideset record from an external file, do it here...
398 if (globals.add_sset) {
399 add_sideset(*ss_region, region, output_region, globals);
400 }
401
402 if (globals.debug) {
403 std::cerr << "END STATE_DEFINE_MODEL... " << '\n';
404 }
405 output_region.end_mode(Ioss::STATE_DEFINE_MODEL);
406
407 if (globals.debug) {
408 std::cerr << "TRANSFERRING MESH FIELD DATA ... " << '\n';
409 // Model defined, now fill in the model data...
410 }
411 output_region.begin_mode(Ioss::STATE_MODEL);
412
413 // Transfer MESH field_data from input to output...
414 {
415 Ioss::NodeBlock *nb = region.get_node_blocks()[0];
416 assert(nb != nullptr);
417
418 if (nb != nullptr) {
419 const std::string &name = nb->name();
420 if (globals.debug) {
421 std::cerr << name << ", ";
422
423 // Find the corresponding output element_block...
424 }
425 Ioss::NodeBlock *onb = output_region.get_node_block(name);
426 assert(onb != nullptr);
427 transfer_field_data(nb, onb, Ioss::Field::MESH);
428 transfer_field_data(nb, onb, Ioss::Field::ATTRIBUTE);
429 }
430 if (globals.debug) {
431 std::cerr << '\n';
432 }
433 }
434
435 // Now do the same for element blocks...
436 {
437 const auto &ebs = region.get_element_blocks();
438 auto I = ebs.cbegin();
439
440 while (I != ebs.end()) {
441 const std::string &name = (*I)->name();
442 if (globals.debug) {
443 std::cerr << name << ", ";
444
445 // Find the corresponding output element_block...
446 }
447 Ioss::ElementBlock *oeb = output_region.get_element_block(name);
448 assert(oeb != nullptr);
449
450 if (oeb != nullptr) {
451 transfer_field_data(*I, oeb, Ioss::Field::MESH);
452 transfer_field_data(*I, oeb, Ioss::Field::ATTRIBUTE);
453 }
454 ++I;
455 }
456 if (globals.debug) {
457 std::cerr << '\n';
458 }
459 }
460
461 // Node Sets
462 {
463 const auto &nss = region.get_nodesets();
464 auto I = nss.cbegin();
465 while (I != nss.end()) {
466 const std::string &name = (*I)->name();
467 if (globals.debug) {
468 std::cerr << name << ", ";
469
470 // Find matching output nodeset
471 }
472 Ioss::NodeSet *ons = output_region.get_nodeset(name);
473 if (ons != nullptr) {
474 transfer_field_data(*I, ons, Ioss::Field::MESH);
475 transfer_field_data(*I, ons, Ioss::Field::ATTRIBUTE);
476 }
477 ++I;
478 }
479 if (globals.debug) {
480 std::cerr << '\n';
481 }
482 }
483
484 // Comm Sets
485 {
486 const auto &css = region.get_commsets();
487 auto I = css.cbegin();
488 while (I != css.end()) {
489 const std::string &name = (*I)->name();
490 if (globals.debug) {
491 std::cerr << name << ", ";
492
493 // Find matching output commset
494 }
495 Ioss::CommSet *ocs = output_region.get_commset(name);
496 if (ocs != nullptr) {
497 transfer_field_data(*I, ocs, Ioss::Field::MESH);
498 transfer_field_data(*I, ocs, Ioss::Field::ATTRIBUTE);
499 transfer_field_data(*I, ocs, Ioss::Field::COMMUNICATION);
500 }
501 ++I;
502 }
503 if (globals.debug) {
504 std::cerr << '\n';
505 }
506 }
507
508 // Side Sets
509 if (!globals.do_normals) {
510 const auto &fss = region.get_sidesets();
511 auto I = fss.cbegin();
512 while (I != fss.end()) {
513 const std::string &name = (*I)->name();
514 if (globals.debug) {
515 std::cerr << name << ", ";
516
517 // Find matching output sideset
518 }
519 Ioss::SideSet *ofs = output_region.get_sideset(name);
520
521 if (ofs != nullptr) {
522 transfer_field_data(*I, ofs, Ioss::Field::MESH);
523 transfer_field_data(*I, ofs, Ioss::Field::ATTRIBUTE);
524
525 const auto &fbs = (*I)->get_side_blocks();
526 auto J = fbs.cbegin();
527 while (J != fbs.end()) {
528
529 // Find matching output sideblock
530 const std::string &fbname = (*J)->name();
531 if (globals.debug) {
532 std::cerr << fbname << ", ";
533 }
534 Ioss::SideBlock *ofb = ofs->get_side_block(fbname);
535
536 if (ofb != nullptr) {
537 transfer_field_data(*J, ofb, Ioss::Field::MESH, "", false);
538 transfer_field_data(*J, ofb, Ioss::Field::ATTRIBUTE, "", false);
539 }
540 ++J;
541 }
542 }
543 ++I;
544 }
545 if (globals.debug) {
546 std::cerr << '\n';
547 }
548 }
549
550 if (globals.add_sset) {
551 add_sideset_mesh_fields(*ss_region, region, output_region, globals);
552 }
553
554 if (globals.debug) {
555 std::cerr << "END STATE_MODEL... " << '\n';
556 }
557 output_region.end_mode(Ioss::STATE_MODEL);
558
559 if (globals.do_normals) {
560 output_normals(region, output_region, globals.reverse_normals);
561 }
562 else if (globals.add_sset) {
563 if (ss_region->property_exists("state_count") &&
564 ss_region->get_property("state_count").get_int() > 0) {
565 output_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
566 add_sideset_transient_fields(*ss_region, region, output_region, globals);
567 output_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
568 }
569 }
570 else {
571 std::cerr << "Internal Error\n";
572 std::abort();
573 }
574
575 if (globals.debug) {
576 std::cerr << "TRANSFERRING TRANSIENT FIELDS ... " << '\n';
577 }
578 output_region.begin_mode(Ioss::STATE_TRANSIENT);
579 // Get the timesteps from the input database. Step through them
580 // and transfer fields to output database...
581
582 if (globals.do_normals) {
583 // Do nothing, normals were already output above...
584 }
585 else if (globals.add_sset) {
586 transfer_sideset_field_data(*ss_region, region, output_region, globals);
587 }
588 else {
589 std::cerr << "Internal Error\n";
590 std::abort();
591 }
592 if (globals.debug) {
593 std::cerr << "END STATE_TRANSIENT... " << '\n';
594 }
595 output_region.end_mode(Ioss::STATE_TRANSIENT);
596 delete ss_region;
597 }
598
599 void transfer_nodeblock(Ioss::Region &region, Ioss::Region &output_region, bool debug)
600 {
601 const auto &nbs = region.get_node_blocks();
602 auto i = nbs.cbegin();
603 while (i != nbs.end()) {
604 const std::string &name = (*i)->name();
605 if (debug) {
606 std::cerr << name << ", ";
607 }
608 int num_nodes = (*i)->entity_count();
609 int degree = (*i)->get_property("component_degree").get_int();
610 if (!debug) {
611 std::cerr << " Number of coordinates per node =" << std::setw(9) << degree << "\n";
612 std::cerr << " Number of nodes =" << std::setw(9) << num_nodes << "\n";
613 }
614
615 auto nb = new Ioss::NodeBlock(output_region.get_database(), name, num_nodes, degree);
616 output_region.add(nb);
617
618 transfer_properties(*i, nb);
619 transfer_fields(*i, nb, Ioss::Field::MESH);
620 transfer_fields(*i, nb, Ioss::Field::ATTRIBUTE);
621 ++i;
622 }
623 if (debug) {
624 std::cerr << '\n';
625 }
626 }
627
628 void transfer_elementblock(Ioss::Region &region, Ioss::Region &output_region, bool debug)
629 {
630 const auto &ebs = region.get_element_blocks();
631 auto i = ebs.cbegin();
632 int total_elements = 0;
633 while (i != ebs.end()) {
634 const std::string &name = (*i)->name();
635 if (debug) {
636 std::cerr << name << ", ";
637 }
638 std::string type = (*i)->topology()->name();
639 int num_elem = (*i)->entity_count();
640 total_elements += num_elem;
641
642 auto eb = new Ioss::ElementBlock(output_region.get_database(), name, type, num_elem);
643 output_region.add(eb);
644 transfer_properties(*i, eb);
645 transfer_fields(*i, eb, Ioss::Field::MESH);
646 transfer_fields(*i, eb, Ioss::Field::ATTRIBUTE);
647 ++i;
648 }
649 if (!debug) {
650 std::cerr << " Number of elements =" << std::setw(9) << total_elements
651 << "\n";
652 std::cerr << " Number of element blocks =" << std::setw(9) << ebs.size()
653 << "\n\n";
654 }
655 else {
656 std::cerr << '\n';
657 }
658 }
659
660 void transfer_sidesets(Ioss::Region &region, Ioss::Region &output_region, bool debug)
661 {
662 const auto &fss = region.get_sidesets();
663 auto i = fss.cbegin();
664 int total_sides = 0;
665 while (i != fss.end()) {
666 const std::string &name = (*i)->name();
667 if (debug) {
668 std::cerr << name << ", ";
669 }
670 auto surf = new Ioss::SideSet(output_region.get_database(), name);
671
672 const auto &fbs = (*i)->get_side_blocks();
673 auto j = fbs.cbegin();
674 while (j != fbs.end()) {
675 const std::string &fbname = (*j)->name();
676 if (debug) {
677 std::cerr << fbname << ", ";
678 }
679 std::string fbtype = (*j)->topology()->name();
680 std::string partype = (*j)->parent_element_topology()->name();
681 int num_side = (*j)->entity_count();
682 total_sides += num_side;
683
684 auto block =
685 new Ioss::SideBlock(output_region.get_database(), fbname, fbtype, partype, num_side);
686 surf->add(block);
687 transfer_properties(*j, block);
688 transfer_fields(*j, block, Ioss::Field::MESH);
689 transfer_fields(*j, block, Ioss::Field::ATTRIBUTE);
690 ++j;
691 }
692 transfer_properties(*i, surf);
693 transfer_fields(*i, surf, Ioss::Field::MESH);
694 transfer_fields(*i, surf, Ioss::Field::ATTRIBUTE);
695 output_region.add(surf);
696 ++i;
697 }
698 if (!debug) {
699 std::cerr << " Number of element side sets =" << std::setw(9) << fss.size() << "\n";
700 std::cerr << " Number of element sides =" << std::setw(9) << total_sides << "\n";
701 }
702 else {
703 std::cerr << '\n';
704 }
705 }
706
707 void transfer_nodesets(Ioss::Region &region, Ioss::Region &output_region, bool debug)
708 {
709 const auto &nss = region.get_nodesets();
710 auto i = nss.cbegin();
711 int total_nodes = 0;
712 while (i != nss.end()) {
713 const std::string &name = (*i)->name();
714 if (debug) {
715 std::cerr << name << ", ";
716 }
717 int count = (*i)->entity_count();
718 total_nodes += count;
719 auto ns = new Ioss::NodeSet(output_region.get_database(), name, count);
720 output_region.add(ns);
721 transfer_properties(*i, ns);
722 transfer_fields(*i, ns, Ioss::Field::MESH);
723 transfer_fields(*i, ns, Ioss::Field::ATTRIBUTE);
724 ++i;
725 }
726 if (!debug) {
727 std::cerr << " Number of nodal point sets =" << std::setw(9) << nss.size() << "\n";
728 std::cerr << " Length of node list =" << std::setw(9) << total_nodes << "\n";
729 }
730 else {
731 std::cerr << '\n';
732 }
733 }
734
735 void transfer_commsets(Ioss::Region &region, Ioss::Region &output_region, bool debug)
736 {
737 const auto &css = region.get_commsets();
738 auto i = css.cbegin();
739 while (i != css.end()) {
740 const std::string &name = (*i)->name();
741 if (debug) {
742 std::cerr << name << ", ";
743 }
744 std::string type = (*i)->get_property("entity_type").get_string();
745 int count = (*i)->entity_count();
746 auto cs = new Ioss::CommSet(output_region.get_database(), name, type, count);
747 output_region.add(cs);
748 transfer_properties(*i, cs);
749 transfer_fields(*i, cs, Ioss::Field::MESH);
750 transfer_fields(*i, cs, Ioss::Field::ATTRIBUTE);
751 transfer_fields(*i, cs, Ioss::Field::COMMUNICATION);
752 ++i;
753 }
754 if (debug) {
755 std::cerr << '\n';
756 }
757 }
758
759 void transfer_fields(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
760 Ioss::Field::RoleType role, const std::string &prefix)
761 {
762 // Check for transient fields...
763 Ioss::NameList fields = ige->field_describe(role);
764
765 // Iterate through results fields and transfer to output
766 // database... If a prefix is specified, only transfer fields
767 // whose names begin with the prefix
768 for (const auto &field_name : fields) {
769 if (field_name != "ids" && !oge->field_exists(field_name) &&
770 Ioss::Utils::substr_equal(prefix, field_name)) {
771 // If the field does not already exist, add it to the output node block
772 Ioss::Field field = ige->get_field(field_name);
773 oge->field_add(std::move(field));
774 }
775 }
776 }
777
778 void transfer_field_data(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
779 Ioss::Field::RoleType role, const std::string &prefix,
780 bool transfer_connectivity)
781 {
782 // Iterate through the TRANSIENT-role fields of the input
783 // database and transfer to output database.
784 Ioss::NameList state_fields = ige->field_describe(role);
785
786 // Complication here is that if the 'role' is 'Ioss::Field::MESH',
787 // then the 'ids' field must be transferred first...
788 if (role == Ioss::Field::MESH) {
789 for (const auto &field_name : state_fields) {
790 assert(oge->field_exists(field_name));
791 if (field_name == "ids") {
792 transfer_field_data_internal(ige, oge, field_name);
793 break;
794 }
795 }
796 }
797
798 for (const auto &field_name : state_fields) {
799 // All of the 'Ioss::EntityBlock' derived classes have a
800 // 'connectivity' field, but it is only interesting on the
801 // Ioss::ElementBlock class. On the other classes, it just
802 // generates overhead...
803 if (!transfer_connectivity && field_name == "connectivity") {
804 continue;
805 }
806
807 if (field_name != "ids" && Ioss::Utils::substr_equal(prefix, field_name)) {
808 assert(oge->field_exists(field_name));
809 transfer_field_data_internal(ige, oge, field_name);
810 }
811 }
812 }
813
814 void transfer_field_data_internal(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge,
815 const std::string &field_name)
816 {
817
818 size_t isize = ige->get_field(field_name).get_size();
819 assert(isize == oge->get_field(field_name).get_size());
820
821 data.resize(isize);
822
823 if (field_name == "mesh_model_coordinates_x") {
824 return;
825 }
826 if (field_name == "mesh_model_coordinates_y") {
827 return;
828 }
829 if (field_name == "mesh_model_coordinates_z") {
830 return;
831 }
832 if (field_name == "connectivity_raw") {
833 return;
834 }
835 if (field_name == "element_side_raw") {
836 return;
837 }
838 if (field_name == "ids_raw") {
839 return;
840 }
841 if (field_name == "node_connectivity_status") {
842 return;
843 }
844 if (field_name == "entity_processor_raw") {
845 return;
846 }
847 if (field_name == "owning_processor") {
848 return;
849 }
850 if (ige->type() == Ioss::SIDEBLOCK && field_name == "ids") {
851 return;
852 }
853 ige->get_field_data(field_name, Data(data), isize);
854 oge->put_field_data(field_name, Data(data), isize);
855 }
856
857 void transfer_properties(Ioss::GroupingEntity *ige, Ioss::GroupingEntity *oge)
858 {
859 Ioss::NameList names = ige->property_describe();
860
861 // Iterate through properties and transfer to output database...
862 Ioss::NameList::const_iterator I;
863 for (I = names.begin(); I != names.end(); ++I) {
864 if (!oge->property_exists(*I)) {
865 oge->property_add(ige->get_property(*I));
866 }
867 }
868 }
869
870 void add_sideset(Ioss::Region &ss_region, Ioss::Region & /* region */,
871 Ioss::Region &output_region, Globals &globals)
872 {
873 auto *pressures = new Ioss::SideSet(output_region.get_database(), "cth_pressures");
874 // Each element block in the sset file will be a surside in the mesh file...
875 {
876 const auto &ebs = ss_region.get_element_blocks();
877 auto i = ebs.begin();
878 while (i != ebs.end()) {
879 std::string name = (*i)->name();
880 name = "ss" + name;
881
882 if (globals.debug) {
883 std::cerr << name << ", ";
884 }
885 int num_elem = (*i)->entity_count();
886 std::string type = (*i)->topology()->name();
887
888 // Should be able to get this from the input mesh element blocks...
889 std::string partype = "unknown";
890 auto fb = new Ioss::SideBlock(output_region.get_database(), name, type, partype, num_elem);
891 pressures->add(fb);
892 ++i;
893 }
894 output_region.add(pressures);
895 }
896 }
897
898 void add_sideset_mesh_fields(Ioss::Region &ss_region, Ioss::Region & /* region */,
899 Ioss::Region &output_region, Globals &globals)
900 {
901 // Each element block in the sset file will be a surface in the mesh file...
902 {
903 const auto &ebs = ss_region.get_element_blocks();
904 auto i = ebs.begin();
905 while (i != ebs.end()) {
906 std::string name = (*i)->name();
907 name = "ss" + name;
908 Ioss::SideBlock *fb = output_region.get_sideblock(name);
909 if (fb == nullptr) {
910 std::cerr << "INTERNAL ERROR: Could not find sideblock named '" << name << "'\n";
911 std::exit(EXIT_FAILURE);
912 }
913
914 if (globals.debug) {
915 std::cerr << name << ", ";
916
917 // The "skin" field should exist on each element block. It
918 // contains the map back to the element/local face in the
919 // original (region/output_region) mesh. It is in the exact
920 // same format as a sidesets "element_side" field...
921 //
922 // See if the field exists...
923 }
924 if ((*i)->field_exists("skin")) {
925 Ioss::Field skin = (*i)->get_field("skin");
926
927 int isize = skin.get_size();
928 int osize = fb->get_field("element_side").get_size();
929 assert(isize == osize);
930
931 data.resize(isize);
932
933 (*i)->get_field_data("skin", Data(data), isize);
934 fb->put_field_data("element_side", Data(data), osize);
935 ++i;
936 }
937 else {
938 std::cerr << "ERROR: Field 'skin' does not exist on element block '" << name << "'.\n";
939 std::exit(EXIT_FAILURE);
940 }
941 }
942 }
943 }
944
945 void transfer_sideset_field_data(Ioss::Region &ss_region, Ioss::Region & /* region */,
946 Ioss::Region &output_region, Globals &globals)
947 {
948 int step_count = ss_region.get_property("state_count").get_int();
949 int istep = 1; // Need this to exist after end of step_count loop.
950
951 int initial_state = 1;
952 // Find first state >= globals.minimum_time;
953 for (; initial_state <= step_count; initial_state++) {
954 double time = ss_region.get_state_time(initial_state);
955 if (time >= globals.minimum_time) {
956 globals.minimum_time = time;
957 break;
958 }
959 }
960
961 const std::string cth_pressure = "cth_pressure";
962
963 // If the user specified the "Convert_Gage" option, then we need
964 // to save the field values at time 'globals.minimum_time' so they
965 // can be subtracted from all field data.
966 std::map<std::string, double *> time_zero_field_data;
967 if (globals.convert_gage) {
968 ss_region.begin_state(1);
969
970 const auto &ebs = ss_region.get_element_blocks();
971 auto i = ebs.begin();
972 while (i != ebs.end()) {
973
974 // The gage pressure conversion is currently only applied to the field "cth_pressure"
975 const std::string &name = (*i)->name();
976 if ((*i)->field_exists(cth_pressure)) {
977 int isize = (*i)->get_field(cth_pressure).get_size();
978 void *zdata = new char[isize];
979 time_zero_field_data[name] = reinterpret_cast<double *>(zdata);
980 (*i)->get_field_data(cth_pressure, zdata, isize);
981 }
982 else {
983 time_zero_field_data[name] = (double *)nullptr;
984 }
985 ++i;
986 }
987 ss_region.end_state(1);
988 }
989
990 // If globals.offset_time is non-zero, then the output times will be
991 // offset by the specified amount. We need to add a time=0.0 step
992 // with all pressures set to globals.offset_pressure.
993 if (globals.offset_time > 0.0) {
994 int ostep = output_region.add_state(0.0);
995 output_region.begin_state(ostep);
996 const auto &ebs = ss_region.get_element_blocks();
997 auto i = ebs.begin();
998 while (i != ebs.end()) {
999 const std::string &eb_name = (*i)->name();
1000 std::string name = "ss" + eb_name;
1001 Ioss::SideBlock *fb = output_region.get_sideblock(name);
1002 if (fb == nullptr) {
1003 std::cerr << "INTERNAL ERROR: Could not find sideblock named '" << name << "'\n";
1004 std::exit(EXIT_FAILURE);
1005 }
1006
1007 auto state_fields = (*i)->field_describe(Ioss::Field::TRANSIENT);
1008 for (const auto &field_name : state_fields) {
1009 // NOTE: Only dealing with the "cth_" fields here.
1010 // If there are other fields, we probably have an invalid
1011 // output database...
1012 if (Ioss::Utils::substr_equal("cth_", field_name)) {
1013 int isize = (*i)->get_field(field_name).get_size();
1014 int count = (*i)->get_field(field_name).raw_count();
1015 data.resize(isize);
1016 auto *rdata = reinterpret_cast<double *>(Data(data));
1017 for (int ii = 0; ii < count; ii++) {
1018 rdata[ii] = globals.offset_pressure;
1019 }
1020 fb->put_field_data(field_name, Data(data), isize);
1021 break;
1022 }
1023 }
1024 ++i;
1025 }
1026 output_region.end_state(ostep);
1027 }
1028
1029 for (istep = initial_state; istep <= step_count; istep++) {
1030 double time = ss_region.get_state_time(istep);
1031 if (time < globals.minimum_time) {
1032 continue;
1033 }
1034
1035 if (globals.maximum_time > 0.0 && time > globals.maximum_time) {
1036 break;
1037 }
1038
1039 int ostep = output_region.add_state(time - globals.minimum_time + globals.offset_time);
1040 show_step(istep, time);
1041
1042 output_region.begin_state(ostep);
1043 ss_region.begin_state(istep);
1044 const auto &ebs = ss_region.get_element_blocks();
1045 auto i = ebs.begin();
1046 while (i != ebs.end()) {
1047 const std::string &eb_name = (*i)->name();
1048 std::string name = "ss" + eb_name;
1049 auto *fb = output_region.get_sideblock(name);
1050 if (fb == nullptr) {
1051 std::cerr << "INTERNAL ERROR: Could not find side block named '" << name << "'\n";
1052 std::exit(EXIT_FAILURE);
1053 }
1054
1055 auto state_fields = (*i)->field_describe(Ioss::Field::TRANSIENT);
1056 for (const auto &field_name : state_fields) {
1057 if (globals.convert_gage && field_name == cth_pressure) {
1058 // Subtract the time zero pressures (stored in
1059 // time_zero_field_data) from each time step
1060 double *zdata = time_zero_field_data[eb_name];
1061 assert(zdata != nullptr);
1062
1063 int isize = (*i)->get_field(field_name).get_size();
1064 int count = (*i)->get_field(field_name).raw_count();
1065 data.resize(isize);
1066 auto *rdata = reinterpret_cast<double *>(Data(data));
1067
1068 (*i)->get_field_data(field_name, Data(data), isize);
1069 for (int ii = 0; ii < count; ii++) {
1070 rdata[ii] -= zdata[ii];
1071 }
1072
1073 if (globals.offset_pressure != 0.0) {
1074 for (int ii = 0; ii < count; ii++) {
1075 rdata[ii] += globals.offset_pressure;
1076 }
1077 }
1078 fb->put_field_data(field_name, Data(data), isize);
1079 }
1080 else if (globals.offset_pressure != 0.0 && field_name == cth_pressure) {
1081 int isize = (*i)->get_field(field_name).get_size();
1082 int count = (*i)->get_field(field_name).raw_count();
1083 data.resize(isize);
1084 auto *rdata = reinterpret_cast<double *>(Data(data));
1085
1086 (*i)->get_field_data(field_name, Data(data), isize);
1087 for (int ii = 0; ii < count; ii++) {
1088 rdata[ii] += globals.offset_pressure;
1089 }
1090 fb->put_field_data(field_name, Data(data), isize);
1091 }
1092 else if (Ioss::Utils::substr_equal("cth_", field_name)) {
1093 assert(fb->field_exists(field_name));
1094 transfer_field_data_internal(*i, fb, field_name);
1095 }
1096 }
1097 ++i;
1098 }
1099 ss_region.end_state(istep);
1100 output_region.end_state(ostep);
1101 }
1102
1103 step_count = istep - 1;
1104
1105 // See if special handling of the last step is specified...
1106 if (globals.final_pressure == Globals::INITIAL || globals.final_pressure == Globals::ZERO ||
1107 globals.final_pressure == Globals::OFFSET) {
1108 double time = ss_region.get_state_time(step_count);
1109 double delta_time;
1110 if (globals.delta_time > 0.0) {
1111 delta_time = globals.delta_time;
1112 }
1113 else {
1114 delta_time = time - ss_region.get_state_time(step_count - 1);
1115 }
1116 time += delta_time;
1117 int ostep = output_region.add_state(time - globals.minimum_time + globals.offset_time);
1118
1119 output_region.begin_state(ostep);
1120 ss_region.begin_state(initial_state);
1121
1122 // Repeat the data from the first step at the last step. This is to bring the model back to
1123 // an
1124 // "equilibrium" state in case the CTH analysis was not run out to an equilibrium
1125 // state. If ZERO was specified, then it simply zeros out the pressure field at the last step.
1126 const auto &ebs = ss_region.get_element_blocks();
1127 auto i = ebs.begin();
1128 while (i != ebs.end()) {
1129 const std::string &eb_name = (*i)->name();
1130 std::string name = "ss" + eb_name;
1131 Ioss::SideBlock *fb = output_region.get_sideblock(name);
1132 if (fb == nullptr) {
1133 std::ostringstream msg;
1134 msg << " INTERNAL_ERROR: Could not find sideblock '" << name << "'\n";
1135 throw std::runtime_error(msg.str());
1136 }
1137
1138 auto state_fields = (*i)->field_describe(Ioss::Field::TRANSIENT);
1139 for (const auto &field_name : state_fields) {
1140 if (Ioss::Utils::substr_equal("cth_", field_name)) {
1141 if (field_name == cth_pressure &&
1142 (globals.final_pressure == Globals::ZERO ||
1143 globals.final_pressure == Globals::OFFSET || globals.convert_gage)) {
1144
1145 // If convert_gage is true and we are outputting the
1146 // cth_pressure variable, then for both the INITIAL and
1147 // ZERO cases, we output a zero-filled field...
1148
1149 double value = 0.0;
1150 if (globals.final_pressure == Globals::OFFSET) {
1151 value = globals.offset_pressure;
1152 }
1153
1154 int isize = (*i)->get_field(field_name).get_size();
1155 int count = (*i)->get_field(field_name).raw_count();
1156 data.resize(isize);
1157 auto *rdata = reinterpret_cast<double *>(Data(data));
1158 for (int ii = 0; ii < count; ii++) {
1159 rdata[ii] = value;
1160 }
1161 fb->put_field_data(field_name, Data(data), isize);
1162 }
1163 else {
1164 // Replicate first field as last field...
1165 assert(fb->field_exists(field_name));
1166 transfer_field_data_internal(*i, fb, field_name);
1167 }
1168 }
1169 }
1170 ++i;
1171 }
1172 output_region.end_state(ostep);
1173 ss_region.end_state(initial_state);
1174 }
1175 if (globals.convert_gage) {
1176 auto i = time_zero_field_data.begin();
1177 auto ie = time_zero_field_data.end();
1178 while (i != ie) {
1179 delete[] reinterpret_cast<char *>((*i).second);
1180 ++i;
1181 }
1182 }
1183 }
1184
1185 void add_sideset_transient_fields(Ioss::Region &ss_region, Ioss::Region & /* region */,
1186 Ioss::Region &output_region, Globals &globals)
1187 {
1188 // Each element block in the sset file will be a surface in the mesh file...
1189 {
1190 const auto &ebs = ss_region.get_element_blocks();
1191 for (auto *eb : ebs) {
1192 std::string name = eb->name();
1193 name = "ss" + name;
1194 auto *fb = output_region.get_sideblock(name);
1195 assert(fb != nullptr);
1196
1197 if (globals.debug) {
1198 std::cerr << name << ", ";
1199
1200 // Each element variable in the sset file which begins with
1201 // "cth_" will be a sideset variable in the outptut file...
1202 }
1203 transfer_fields(eb, fb, Ioss::Field::TRANSIENT, "cth_");
1204 }
1205 }
1206 }
1207
1208 void output_normals(Ioss::Region &region, Ioss::Region &output_region, bool reverse_normals)
1209 {
1210 // Define output fields...
1211 {
1212 const auto *v3d = Ioss::VariableType::factory("vector_3d");
1213 output_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
1214 auto *nb = (*output_region.get_node_blocks().begin());
1215 int num_nodes = nb->entity_count();
1216 Ioss::Field node_normal("node_normal", Ioss::Field::REAL, v3d, Ioss::Field::TRANSIENT,
1217 num_nodes);
1218 nb->field_add(std::move(node_normal));
1219
1220 // Iterate over the element blocks and calculate both node normals and face normals...
1221 const auto &ebs = output_region.get_element_blocks();
1222 for (auto *eb : ebs) {
1223 int num_elem = eb->entity_count();
1224 Ioss::Field face_normal("face_normal", Ioss::Field::REAL, v3d, Ioss::Field::TRANSIENT,
1225 num_elem);
1226 eb->field_add(std::move(face_normal));
1227 }
1228 output_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
1229 }
1230
1231 output_region.begin_mode(Ioss::STATE_TRANSIENT);
1232 int ostep = output_region.add_state(0.0);
1233 output_region.begin_state(ostep);
1234
1235 auto *nb = (*region.get_node_blocks().begin());
1236 auto *nbo = (*output_region.get_node_blocks().begin());
1237
1238 // Get the nodal coordinates...
1239 int num_nodes = nb->entity_count();
1240 int coord_size = nb->get_field("mesh_model_coordinates").get_size();
1241
1242 std::vector<double> coord(3 * num_nodes);
1243 assert(3 * num_nodes * sizeof(double) == (size_t)coord_size);
1244
1245 nb->get_field_data("mesh_model_coordinates", Data(coord), coord_size);
1246
1247 // Also get an array for the average nodal normal vector...
1248 std::vector<double> node_normal(3 * num_nodes);
1249 std::fill(node_normal.begin(), node_normal.end(), 0.0);
1250
1251 // Iterate over the element blocks and calculate both node normals and face normals...
1252 std::vector<int> conn;
1253 std::vector<double> face_normal;
1254 const auto &ebs = region.get_element_blocks();
1255 for (const auto *eb : ebs) {
1256 const std::string &name = (*eb).name();
1257
1258 auto *ebo = output_region.get_element_block(name);
1259 if (ebo == nullptr) {
1260 std::cerr << "INTERNAL ERROR: Could not find element block named '" << name << "'\n";
1261 std::exit(EXIT_FAILURE);
1262 }
1263
1264 int num_elem = eb->entity_count();
1265 int num_node_per_elem = eb->topology()->number_nodes();
1266
1267 // Get the connectivity array...
1268 conn.resize(num_elem * num_node_per_elem);
1269 eb->get_field_data("connectivity", conn);
1270
1271 // Connectivity is in global id space; change to local...
1272 for (int i = 0; i < num_elem * num_node_per_elem; i++) {
1273 int local = region.node_global_to_local(conn[i]);
1274 conn[i] = local - 1;
1275 }
1276 // Get an array for storing the face normals for this block...
1277 face_normal.resize(3 * num_elem);
1278 std::fill(face_normal.begin(), face_normal.end(), 0.0);
1279
1280 calculate_normals(node_normal, face_normal, num_elem, num_node_per_elem, coord, conn,
1281 reverse_normals);
1282 ebo->put_field_data("face_normal", face_normal);
1283 }
1284
1285 int nsize = node_normal.size();
1286 for (int i = 0; i < nsize; i += 3) {
1287 vector3d a(node_normal[i + 0], node_normal[i + 1], node_normal[i + 2]);
1288 a.normalize();
1289
1290 node_normal[i + 0] = a.x;
1291 node_normal[i + 1] = a.y;
1292 node_normal[i + 2] = a.z;
1293 }
1294 nbo->put_field_data("node_normal", node_normal);
1295 output_region.end_state(ostep);
1296 output_region.end_mode(Ioss::STATE_TRANSIENT);
1297 }
1298
1299 void calculate_normals(std::vector<double> &node_normal, std::vector<double> &face_normal,
1300 int num_elem, int num_node_per_elem, std::vector<double> &coord,
1301 std::vector<int> &conn, bool reverse_normals)
1302 {
1303 // Iterate the connectivity array and calculate normals...
1304 // The elements should all be shells with the outward normal
1305 // defined by the node ordering 1.2.3.
1306 // The face normal will be the average of the face-corner node
1307 // normals for a 4-node face. For a triangular face, all
1308 // normals are the same...
1309
1310 for (int iel = 0; iel < num_elem; iel++) {
1311 int ioff = iel * num_node_per_elem;
1312
1313 // Triangular faces...
1314 if (num_node_per_elem == 3) {
1315 vector3d local[3];
1316 for (int i = 0; i < 3; i++) {
1317 int node = conn[ioff + i];
1318 local[i].set(coord[node * 3 + 0], coord[node * 3 + 1], coord[node * 3 + 2]);
1319 }
1320
1321 vector3d plnorm = vector3d::plane_normal(local[0], local[1], local[2]);
1322 plnorm.normalize();
1323 if (reverse_normals) {
1324 plnorm.reverse();
1325 }
1326
1327 face_normal[iel * 3 + 0] = plnorm.x;
1328 face_normal[iel * 3 + 1] = plnorm.y;
1329 face_normal[iel * 3 + 2] = plnorm.z;
1330
1331 for (int i = 0; i < 3; i++) {
1332 int node = conn[ioff + i];
1333 node_normal[node * 3 + 0] += plnorm.x;
1334 node_normal[node * 3 + 1] += plnorm.y;
1335 node_normal[node * 3 + 2] += plnorm.z;
1336 }
1337 }
1338 else {
1339
1340 // Quadrilateral faces...
1341 assert(num_node_per_elem == 4);
1342 vector3d local[4];
1343 for (int i = 0; i < 4; i++) {
1344 int node = conn[ioff + i];
1345 local[i].set(coord[node * 3 + 0], coord[node * 3 + 1], coord[node * 3 + 2]);
1346 }
1347
1348 for (int i = 0; i < 4; i++) {
1349 // at node 0 -- vector from 3-0 X 0-1
1350 // at node 1 -- vector from 0-1 X 1-2
1351 // at node 2 -- vector from 1-2 X 2-3
1352 // at node 3 -- vector from 2-3 X 3-0
1353 int nb = (i + 3) % 4;
1354 int na = (i + 1) % 4;
1355
1356 vector3d a = vector3d::plane_normal(local[nb], local[i], local[na]);
1357 a.normalize();
1358 if (reverse_normals) {
1359 a.reverse();
1360 }
1361
1362 face_normal[iel * 3 + 0] += a.x;
1363 face_normal[iel * 3 + 1] += a.y;
1364 face_normal[iel * 3 + 2] += a.z;
1365
1366 int node = conn[ioff + i];
1367 node_normal[node * 3 + 0] += a.x;
1368 node_normal[node * 3 + 1] += a.y;
1369 node_normal[node * 3 + 2] += a.z;
1370 }
1371 }
1372 vector3d a(face_normal[iel * 3 + 0], face_normal[iel * 3 + 1], face_normal[iel * 3 + 2]);
1373 a.normalize();
1374 face_normal[iel * 3 + 0] = a.x;
1375 face_normal[iel * 3 + 1] = a.y;
1376 face_normal[iel * 3 + 2] = a.z;
1377 }
1378 }
1379
1380 void show_step(int istep, double time)
1381 {
1382 std::cerr.setf(std::ios::scientific);
1383 std::cerr.setf(std::ios::showpoint);
1384 std::cerr << " Time step " << std::setw(5) << istep << " at time " << std::setprecision(5)
1385 << time << '\n';
1386 }
1387 } // namespace