Debian Codegraph @

packages/seacas/libraries/ioss/src/pamgen/Iopg_DatabaseIO.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 <create_inline_mesh.h>
8 #include <pamgen_im_exodusII.h>
9 #include <pamgen_im_ne_nemesisI.h>
10
11 #include "pamgen/Iopg_DatabaseIO.h"
12
13 #include "Ioss_CodeTypes.h"
14 #include "Ioss_SubSystem.h"
15 #include "Ioss_Utils.h"
16
17 #include <algorithm>
18 #include <cctype>
19 #include <cfloat>
20 #include <climits>
21 #include <cstdlib>
22 #include <cstring>
23 #include <ctime>
24 #include <fstream>
25 #include <iostream>
26 #include <iterator>
27 #include <map>
28 #include <set>
29 #include <string>
30 #include <vector>
31
32 namespace Iopg {
33 using SideSetSet = std::set<std::string>;
34 using SideSetMap = std::map<std::string, const std::string, std::less<const std::string>>;
35
36 struct TopologyMapCompare
37 {
38 bool operator()(const std::pair<std::string, const Ioss::ElementTopology *> &lhs,
39 const std::pair<std::string, const Ioss::ElementTopology *> &rhs) const
40 {
41 assert(lhs.second != nullptr);
42 assert(rhs.second != nullptr);
43 return lhs.first < rhs.first ||
44 (!(rhs.first < lhs.first) && lhs.second->name() < rhs.second->name());
45 }
46 };
47
48 using TopologyMap =
49 std::map<std::pair<std::string, const Ioss::ElementTopology *>, int, TopologyMapCompare>;
50 } // namespace Iopg
51
52 namespace {
53 // Output a message that the operation is unsupported and die...
54 void unsupported(const char *operation)
55 {
56 std::ostringstream errmsg;
57 errmsg << "ERROR: Unsupported functionality called: " << operation << '\n';
58 IOSS_ERROR(errmsg);
59 }
60
61 const size_t max_string_length = MAX_STR_LENGTH;
62 const size_t max_line_length = MAX_LINE_LENGTH;
63
64 void separate_surface_element_sides(Ioss::IntVector &element, Ioss::IntVector &sides,
65 Ioss::Region *region, Iopg::TopologyMap &topo_map,
66 Iopg::TopologyMap &side_map,
67 Ioss::SurfaceSplitType split_type);
68
69 const char *Version() { return "Iopg_DatabaseIO.C 2010/09/22"; }
70
71 void pamgen_error(int exoid, int lineno, int /* processor */)
72 {
73 std::ostringstream errmsg;
74 errmsg << "Pamgen error at line " << lineno << " in file '" << Version()
75 << "' Please report to [email protected] if you need help.";
76 IOSS_ERROR(errmsg);
77 }
78 } // namespace
79
80 namespace Iopg {
81 // ========================================================================
82 const IOFactory *IOFactory::factory()
83 {
84 static IOFactory registerThis;
85 return &registerThis;
86 }
87
88 IOFactory::IOFactory() : Ioss::IOFactory("pamgen") {}
89
90 Ioss::DatabaseIO *IOFactory::make_IO(const std::string &filename, Ioss::DatabaseUsage db_usage,
91 Ioss_MPI_Comm communicator,
92 const Ioss::PropertyManager &properties) const
93 {
94 return new DatabaseIO(nullptr, filename, db_usage, communicator, properties);
95 }
96
97 // ========================================================================
98 DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename,
99 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
100 const Ioss::PropertyManager &props)
101 : Ioss::DatabaseIO(region, filename, db_usage, communicator, props)
102 {
103 if (is_input()) {
104 dbState = Ioss::STATE_UNKNOWN;
105 }
106 else {
107 std::ostringstream errmsg;
108 errmsg << "Pamgen mesh option is only valid for input mesh.";
109 IOSS_ERROR(errmsg);
110 }
111 }
112
113 DatabaseIO::~DatabaseIO() { Delete_Pamgen_Mesh(); }
114
115 std::string massagePamgenInputString(std::string sinput, int &dimension)
116 {
117 std::string mesh_description;
118 std::string line;
119 std::istringstream input(sinput);
120 while (std::getline(input, line)) {
121 if (line.empty() || line[0] == '$' || line[0] == '#') {
122 continue;
123 }
124 else {
125 bool twod = line.find('2') != std::string::npos;
126 bool trid = line.find('3') != std::string::npos;
127 if (twod && trid) {
128 std::ostringstream errmsg;
129 errmsg << "PAMGEN: Cannot determine spatial dimension; found both 2 and 3 on first line "
130 "of file.";
131 IOSS_ERROR(errmsg);
132 }
133 else if (twod) {
134 dimension = 2;
135 }
136 else if (trid) {
137 dimension = 3;
138 }
139 break;
140 }
141 }
142
143 mesh_description += "mesh\n";
144 while (std::getline(input, line)) {
145 if (line.empty() || line[0] == '$' || line[0] == '#' ||
146 (line.find("mesh") != std::string::npos) || (line.find("MESH") != std::string::npos)) {
147 ;
148 }
149 else {
150 mesh_description += line;
151 mesh_description += "\n";
152 }
153 }
154 return mesh_description;
155 }
156
157 void DatabaseIO::read_meta_data_nl()
158 {
159 // The file for pamgen contains the mesh description.
160 // The Iopg routine is expecting the mesh description to be a
161 // single string...
162
163 // Read the data and convert it to a string which is then passed
164 // in to the Create_Pamgen_Mesh routine.
165
166 // Check if this is a multi line filename implies it is actually the mesh specification
167 std::string mesh_description;
168
169 std::string tfilename = get_filename();
170 std::string raw_string;
171 // If this is a multi-line string then it actually includes the
172 // mesh description; otherwise, it is a file that must be opened
173 // and read.
174 std::string nlstring = std::string("\n");
175 std::size_t found = tfilename.find(nlstring);
176 if (found != std::string::npos) {
177 std::istringstream f(tfilename);
178 std::string line;
179 while (std::getline(f, line)) {
180 raw_string += line;
181 raw_string += "\n";
182 }
183 }
184 else {
185 std::ifstream f(get_filename());
186 if (!f) {
187 std::ostringstream errmsg;
188 errmsg << "Error opening file '" << get_filename() << "'.";
189 IOSS_ERROR(errmsg);
190 }
191 else {
192 std::string line;
193 while (std::getline(f, line)) {
194 raw_string += line;
195 raw_string += "\n";
196 }
197 }
198 }
199
200 // pulls out dimension, gets rid of comments, prepends mesh...
201 int dimension = 3;
202 mesh_description = massagePamgenInputString(raw_string, dimension);
203
204 int retval = ERROR_FREE_CREATION;
205 bool error_detected = false;
206 try {
207 retval = Create_Pamgen_Mesh(mesh_description.c_str(), dimension, util().parallel_rank(),
208 util().parallel_size(), INT_MAX);
209 }
210 catch (...) {
211 error_detected = true;
212 }
213
214 if (error_detected || retval != ERROR_FREE_CREATION) {
215 char *error = nullptr;
216 if (retval == ERROR_CREATING_MS) {
217 int err_size = getPamgenErrorStreamSize();
218 error = new char[err_size + 1];
219 getPamgenErrorStream(error);
220 }
221 else {
222 int err_size = getPamgenEchoStreamSize();
223 error = new char[err_size + 1];
224 getPamgenEchoStream(error);
225 }
226 // handle error
227 std::ostringstream errmsg;
228 errmsg << "Pamgen mesh generation failed:\n";
229 errmsg << error;
230 delete[] error;
231 IOSS_ERROR(errmsg);
232 }
233
234 read_region();
235 read_communication_metadata();
236
237 get_elemblocks();
238 check_side_topology();
239
240 get_nodeblocks();
241 get_sidesets();
242
243 get_nodesets();
244 get_commsets();
245 }
246
247 void DatabaseIO::read_region()
248 {
249 // Add properties and fields to the 'owning' region.
250 // Also defines member variables of this class...
251
252 char dbtitle[max_line_length + 1];
253 std::memset(dbtitle, 0, max_line_length + 1);
254
255 int node_count = 0;
256 int elem_count = 0;
257 int error = im_ex_get_init(get_file_pointer(), dbtitle, &spatialDimension, &node_count,
258 &elem_count, &elementBlockCount, &nodesetCount, &sidesetCount);
259 if (error < 0)
260 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
261
262 nodeCount = node_count;
263 elementCount = elem_count;
264
265 nodeBlockCount = 1;
266
267 if (nodeCount == 0) {
268 Ioss::WarnOut() << "No nodes were found in the model, file '" << decoded_filename() << "'";
269 }
270 else if (nodeCount < 0) {
271 // NOTE: Code will not continue past this call...
272 std::ostringstream errmsg;
273 errmsg << "Negative node count was found in the model\n"
274 << " File: '" << decoded_filename() << "'.\n";
275 IOSS_ERROR(errmsg);
276 }
277
278 if (elementCount == 0) {
279 Ioss::WarnOut() << "No elements were found in the model, file: '" << decoded_filename()
280 << "'";
281 }
282
283 if (elementCount < 0) {
284 // NOTE: Code will not continue past this call...
285 std::ostringstream errmsg;
286 errmsg << "Negative element count was found in the model, file: '" << decoded_filename()
287 << "'";
288 IOSS_ERROR(errmsg);
289 }
290
291 if (elementCount > 0 && elementBlockCount <= 0) {
292 // NOTE: Code will not continue past this call...
293 std::ostringstream errmsg;
294 errmsg << "No element blocks were found in the model, file: '" << decoded_filename() << "'";
295 IOSS_ERROR(errmsg);
296 }
297
298 Ioss::Region *this_region = get_region();
299 this_region->property_add(Ioss::Property(std::string("title"), dbtitle));
300 }
301
302 void DatabaseIO::read_communication_metadata()
303 {
304 // Sierra does not use/need the element communication map data,
305 // but the Nemesis api call to get the nodal communication map
306 // data also gets the element communication data. So,...,we need to
307 // do some redundant reads to get back the element comm data
308 // and also some extra memory allocations to give them a place
309 // to go.
310
311 // Check that file is nemesis.
312 int num_proc; // Number of processors file was decomposed for
313 int num_proc_in_file; // Number of processors this file has info for
314 char file_type[2]; // "s" for scalar, "p" for parallel
315 // Sierra is set up to handle 1 processor per file; parallel file
316
317 int error =
318 im_ne_get_init_info(get_file_pointer(), &num_proc, &num_proc_in_file, &file_type[0]);
319 if (error < 0) {
320 // Not a nemesis file
321 if (util().parallel_size() > 1) {
322 std::ostringstream errmsg;
323 errmsg << "Exodus file does not contain nemesis information.\n";
324 IOSS_ERROR(errmsg);
325 }
326 }
327 else if (num_proc != util().parallel_size() && util().parallel_size() > 1) {
328 std::ostringstream errmsg;
329 errmsg << "Exodus file was decomposed for " << num_proc
330 << " processors; application is currently being run on " << util().parallel_size()
331 << " processors";
332 IOSS_ERROR(errmsg);
333 }
334 else if (num_proc_in_file != 1) {
335 std::ostringstream errmsg;
336 errmsg << "Exodus file contains data for " << num_proc_in_file
337 << " processors; application requires 1 processor per file.";
338 IOSS_ERROR(errmsg);
339 }
340 else if (file_type[0] != 'p') {
341 std::ostringstream errmsg;
342 errmsg << "Exodus file contains scalar nemesis data; application requires parallel nemesis "
343 "data.";
344 IOSS_ERROR(errmsg);
345 }
346
347 // Get global data (over all processors)
348 int global_nodes = nodeCount;
349 int global_elements = elementCount;
350 int global_eblocks = 0; // unused
351 int global_nsets = 0; // unused
352 int global_ssets = 0; // unused
353
354 int num_external_nodes; // unused
355 int num_elem_cmaps = 0;
356 int num_node_cmaps = 0;
357 int num_internal_nodes = nodeCount;
358 int num_border_nodes = 0;
359 int num_internal_elems = elementCount;
360 int num_border_elems = 0;
361
362 if (isParallel) {
363
364 error = im_ne_get_loadbal_param(get_file_pointer(), &num_internal_nodes, &num_border_nodes,
365 &num_external_nodes, &num_internal_elems, &num_border_elems,
366 &num_node_cmaps, &num_elem_cmaps, myProcessor);
367 if (error < 0)
368 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
369
370 commsetNodeCount = num_node_cmaps;
371 commsetElemCount = num_elem_cmaps;
372
373 // A nemesis file typically separates nodes into multiple
374 // communication sets by processor. (each set specifies
375 // nodes/elements that communicate with only a single
376 // processor). For Sierra, we want a single node communication
377 // map and a single element communication map specifying all
378 // communications so we combine all sets into a single set.
379 error = im_ne_get_init_global(get_file_pointer(), &global_nodes, &global_elements,
380 &global_eblocks, &global_nsets, &global_ssets);
381 if (error < 0)
382 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
383 }
384
385 Ioss::Region *region = get_region();
386 region->property_add(Ioss::Property("internal_node_count", num_internal_nodes));
387 region->property_add(Ioss::Property("border_node_count", num_border_nodes));
388 region->property_add(Ioss::Property("internal_element_count", num_internal_elems));
389 region->property_add(Ioss::Property("border_element_count", num_border_elems));
390 region->property_add(Ioss::Property("global_node_count", global_nodes));
391 region->property_add(Ioss::Property("global_element_count", global_elements));
392
393 // Possibly, the following 4 fields should be nodesets and element
394 // sets instead of fields on the region...
395 region->field_add(Ioss::Field("internal_nodes", Ioss::Field::INTEGER, IOSS_SCALAR(),
396 Ioss::Field::COMMUNICATION, num_internal_nodes));
397 region->field_add(Ioss::Field("border_nodes", Ioss::Field::INTEGER, IOSS_SCALAR(),
398 Ioss::Field::COMMUNICATION, num_border_nodes));
399 region->field_add(Ioss::Field("internal_elements", Ioss::Field::INTEGER, IOSS_SCALAR(),
400 Ioss::Field::COMMUNICATION, num_internal_elems));
401 region->field_add(Ioss::Field("border_elements", Ioss::Field::INTEGER, IOSS_SCALAR(),
402 Ioss::Field::COMMUNICATION, num_border_elems));
403
404 assert(nodeCount == num_internal_nodes + num_border_nodes);
405 assert(elementCount == num_internal_elems + num_border_elems);
406 }
407
408 void DatabaseIO::get_nodeblocks()
409 {
410 // For exodusII, there is only a single node block which contains
411 // all of the nodes.
412 // The default id assigned is '1' and the name is 'nodeblock_1'
413 std::string block_name = "nodeblock_1";
414 Ioss::NodeBlock *block = new Ioss::NodeBlock(this, block_name, nodeCount, spatialDimension);
415 block->property_add(Ioss::Property("id", 1));
416 block->property_add(Ioss::Property("guid", util().generate_guid(1)));
417
418 get_region()->add(block);
419 }
420
421 void DatabaseIO::get_elemblocks()
422 {
423 // Attributes of an element block are:
424 // -- id
425 // -- name
426 // -- element type
427 // -- number of elements
428 // -- number of attributes per element
429 // -- number of nodes per element (derivable from type)
430 // -- number of faces per element (derivable from type)
431 // -- number of edges per element (derivable from type)
432
433 // In a parallel execution, it is possible that an element block will have
434 // no elements on a particular processor...
435
436 // NOTE: This routine may be called multiple times on a single database.
437 // make sure it is not dependent on being called one time only...
438
439 // Get exodusII element block metadata
440 if (elementBlockCount == 0)
441 return;
442
443 Ioss::IntVector element_block_ids(elementBlockCount);
444
445 int error = im_ex_get_elem_blk_ids(get_file_pointer(), Data(element_block_ids));
446 if (error < 0) {
447 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
448 }
449
450 size_t all_element_type_length = elementBlockCount * (max_string_length + 1);
451 std::vector<char> all_element_type(all_element_type_length);
452
453 Ioss::IntVector attributes(elementBlockCount);
454 Ioss::IntVector my_node_count(elementBlockCount);
455 Ioss::IntVector local_element_count(elementBlockCount);
456 Ioss::IntVector global_element_count(elementBlockCount);
457 int iblk;
458
459 for (iblk = 0; iblk < elementBlockCount; iblk++) {
460 int id = element_block_ids[iblk];
461 int number_elements;
462 int nodes_per_element;
463 int attributes_per_element;
464
465 char *const element_type = Data(all_element_type) + iblk * (max_string_length + 1);
466
467 error = im_ex_get_elem_block(get_file_pointer(), id, element_type, &number_elements,
468 &nodes_per_element, &attributes_per_element);
469 if (error < 0) {
470 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
471 }
472
473 local_element_count[iblk] = number_elements;
474
475 if (number_elements == 0) {
476 attributes[iblk] = 0;
477 my_node_count[iblk] = 0;
478 }
479 else {
480 attributes[iblk] = attributes_per_element;
481 my_node_count[iblk] = nodes_per_element;
482 }
483 }
484
485 // Determine global element count for each element block....
486 // Can also get this from an nemesis call, but the data may not always be there in all cases.
487 util().global_count(local_element_count, global_element_count);
488
489 // The 'offset' is used to map an element location within an element
490 // block to the element 'file descriptor'. For example, the file
491 // descriptor of the 37th element in the 4th block is calculated by:
492 // file_descriptor = offset of block 4 + 37 This can also be used to
493 // determine which element block an element with a file_descriptor
494 // maps into. An particular element block contains all elements in
495 // the range:
496 // offset < file_descriptor <= offset+number_elements_per_block
497 int offset = 0;
498
499 for (iblk = 0; iblk < elementBlockCount; iblk++) {
500 int id = element_block_ids[iblk];
501 std::string alias = Ioss::Utils::encode_entity_name("block", id);
502 char *const element_type = Data(all_element_type) + iblk * (max_string_length + 1);
503
504 Ioss::ElementBlock *block = nullptr;
505 std::string block_name = Ioss::Utils::encode_entity_name("block", id);
506
507 Ioss::Utils::fixup_name(element_type); // Convert to lowercase; replace spaces with '_'
508 std::string type = std::string(element_type);
509 std::string save_type = type;
510
511 // Fixup an exodusII kluge/ambiguity.
512 // The element block type does not fully define the element. For
513 // example, a block of type 'triangle' may have either 3 or 6
514 // nodes. To fix this, check the block type name and see if it
515 // ends with a number. If it does, assume it is OK; if not, append
516 // the 'nodes_per_element'.
517 if (!isdigit(*(type.rbegin()))) {
518 if (my_node_count[iblk] > 1) {
519 type += std::to_string(my_node_count[iblk]);
520 }
521 }
522
523 block = new Ioss::ElementBlock(this, block_name, type, local_element_count[iblk]);
524
525 block->property_add(Ioss::Property("id", id));
526 block->property_add(Ioss::Property("guid", util().generate_guid(id)));
527 block->property_add(Ioss::Property("original_block_order", iblk));
528
529 if (block->topology()->name() != save_type && save_type != "null" && save_type != "") {
530 // Maintain original element type on output database if possible.
531 block->property_add(Ioss::Property("original_topology_type", save_type));
532 }
533
534 block->property_add(Ioss::Property("global_entity_count", global_element_count[iblk]));
535
536 offset += local_element_count[iblk];
537
538 get_region()->add(block);
539 if (block_name != alias) {
540 get_region()->add_alias(block_name, alias, block->type());
541 }
542 }
543 assert(elementCount == offset);
544 }
545
546 void DatabaseIO::get_nodesets()
547 {
548 // Attributes of a nodeset are:
549 // -- id
550 // -- name
551 // -- number of nodes
552 // -- number of distribution factors (see next comment)
553 // ----the #distribution factors should equal #nodes or 0, any
554 // other value does not make sense. If it is 0, then a substitute
555 // list will be created returning 1.0 for the factor
556
557 // In a parallel execution, it is possible that a nodeset will have
558 // no nodes or distribution factors on a particular processor...
559
560 // Get exodusII nodeset metadata
561 if (nodesetCount > 0) {
562 Ioss::IntVector nodeset_ids(nodesetCount);
563 int error = im_ex_get_node_set_ids(get_file_pointer(), Data(nodeset_ids));
564 if (error < 0) {
565 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
566 }
567
568 for (int ins = 0; ins < nodesetCount; ins++) {
569 int id = nodeset_ids[ins];
570 int number_nodes;
571 int number_distribution_factors;
572
573 error = im_ex_get_node_set_param(get_file_pointer(), id, &number_nodes,
574 &number_distribution_factors);
575 if (error < 0) {
576 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
577 }
578
579 std::string nodeset_name = Ioss::Utils::encode_entity_name("nodelist", id);
580 Ioss::NodeSet *nodeset = new Ioss::NodeSet(this, nodeset_name, number_nodes);
581 nodeset->property_add(Ioss::Property("id", id));
582 nodeset->property_add(Ioss::Property("guid", util().generate_guid(id)));
583 get_region()->add(nodeset);
584
585 get_region()->add_alias(nodeset_name, Ioss::Utils::encode_entity_name("nodelist", id),
586 Ioss::NODESET);
587 get_region()->add_alias(nodeset_name, Ioss::Utils::encode_entity_name("nodeset", id),
588 Ioss::NODESET);
589 }
590 }
591 }
592
593 void DatabaseIO::get_commsets()
594 {
595 // Attributes of a commset are:
596 // -- id (property)
597 // -- name (property)
598 // -- number of node--CPU pairs (field)
599
600 // In a parallel execution, it is possible that a commset will have
601 // no nodes on a particular processor...
602
603 // If this is a serial execution, there will be no communication
604 // nodesets, just return an empty container.
605
606 if (isParallel) {
607 // This is a parallel run. There should be communications data
608 // Get nemesis commset metadata
609 int my_node_count = 0;
610 int elem_count = 0;
611
612 // NOTE: It is possible for a parallel run to have no
613 // communications maps if the decomposition occurs along contact
614 // surfaces. In this case, we create empty node and element
615 // communication maps.
616
617 if (commsetNodeCount > 0 || commsetElemCount > 0) {
618 if (commsetNodeCount > 0) {
619 nodeCmapIds.resize(commsetNodeCount);
620 nodeCmapNodeCnts.resize(commsetNodeCount);
621 }
622 if (commsetElemCount > 0) {
623 elemCmapIds.resize(commsetElemCount);
624 elemCmapElemCnts.resize(commsetElemCount);
625 }
626
627 int error =
628 im_ne_get_cmap_params(get_file_pointer(), Data(nodeCmapIds), Data(nodeCmapNodeCnts),
629 Data(elemCmapIds), Data(elemCmapElemCnts), myProcessor);
630 if (error < 0)
631 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
632
633 // Count nodes, elements, and convert counts to offsets.
634 //
635 for (int ics = 0; ics < commsetNodeCount; ics++) {
636 my_node_count += nodeCmapNodeCnts[ics];
637 }
638 for (int ecs = 0; ecs < commsetElemCount; ecs++) {
639 elem_count += elemCmapElemCnts[ecs];
640 }
641 }
642 // Create a single node commset and a single element commset
643 Ioss::CommSet *commset = new Ioss::CommSet(this, "commset_node", "node", my_node_count);
644 commset->property_add(Ioss::Property("id", 1));
645 commset->property_add(Ioss::Property("guid", util().generate_guid(1)));
646 get_region()->add(commset);
647
648 commset = new Ioss::CommSet(this, "commset_side", "side", elem_count);
649 commset->property_add(Ioss::Property("id", 1));
650 commset->property_add(Ioss::Property("guid", util().generate_guid(1)));
651 get_region()->add(commset);
652 }
653 }
654
655 void DatabaseIO::get_sidesets()
656 {
657 // This function creates all sidesets (surfaces) for a
658 // model. Note that a side set contains 1 or more side
659 // blocks which are homogeneous (same topology). In serial execution,
660 // this is fairly straightforward since there are no null sets and
661 // we have all the information we need. (...except see below for
662 // surface evolution).
663 //
664 // However, in a parallel execution, we have the possibility that a
665 // side set will have no edges/faces or distribution factors on
666 // a particular processor. We then don't know the block topology of
667 // the block(s) contained in this set. We could do some
668 // communication and get a good idea of the topologies that are in
669 // the set.
670 //
671 // for now, If the database is three-dimensional, then sidesets are
672 // assumed to translate into 'sidesets'; if two-dimensional, then
673 // 'edgesets'
674
675 if (sidesetCount > 0) {
676 check_side_topology();
677
678 // Get exodusII sideset metadata
679
680 Ioss::IntVector side_set_ids(sidesetCount);
681 int error = im_ex_get_side_set_ids(get_file_pointer(), Data(side_set_ids));
682 if (error < 0) {
683 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
684 }
685
686 for (int iss = 0; iss < sidesetCount; iss++) {
687 int id = side_set_ids[iss];
688 std::string sid = "";
689 int number_sides;
690 int number_distribution_factors;
691 TopologyMap topo_map;
692 TopologyMap side_map; // Used to determine side consistency
693
694 Ioss::SurfaceSplitType split_type = splitType;
695 std::string side_set_name;
696
697 side_set_name = Ioss::Utils::encode_entity_name("surface", id);
698
699 Ioss::SideSet *side_set = new Ioss::SideSet(this, side_set_name);
700 get_region()->add(side_set);
701 side_set->property_add(Ioss::Property("id", id));
702 side_set->property_add(Ioss::Property("guid", util().generate_guid(id)));
703
704 get_region()->add_alias(side_set_name, Ioss::Utils::encode_entity_name("surface", id),
705 Ioss::SIDESET);
706 get_region()->add_alias(side_set_name, Ioss::Utils::encode_entity_name("sideset", id),
707 Ioss::SIDESET);
708
709 // split_type = SPLIT_BY_ELEMENT_BLOCK;
710 // split_type = SPLIT_BY_TOPOLOGIES;
711 // split_type = SPLIT_BY_DONT_SPLIT;
712
713 // Determine how many side blocks compose this side set.
714 error = im_ex_get_side_set_param(get_file_pointer(), id, &number_sides,
715 &number_distribution_factors);
716 if (error < 0) {
717 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
718 }
719
720 Ioss::IntVector element(number_sides);
721 Ioss::IntVector sides(number_sides);
722
723 int ierr = im_ex_get_side_set(get_file_pointer(), id, Data(element), Data(sides));
724 if (ierr < 0)
725 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
726
727 if (split_type == Ioss::SPLIT_BY_TOPOLOGIES && sideTopology.size() == 1) {
728 // There is only one side type for all elements in the model
729 topo_map[std::make_pair(sideTopology[0].first->name(), sideTopology[0].second)] =
730 number_sides;
731 }
732 else if (split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
733 const Ioss::ElementTopology *mixed_topo = Ioss::ElementTopology::factory("unknown");
734 topo_map[std::make_pair("unknown", mixed_topo)] = number_sides;
735 }
736 else if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) {
737 // There are multiple side types in the model.
738 // Iterate through the elements in the sideset, determine
739 // their parent element block using the blocks element
740 // topology and the side number, determine the side
741 // type.
742
743 for (Ioss::TopoContainer::size_type i = 0; i < sideTopology.size(); i++) {
744 topo_map[std::make_pair(sideTopology[i].first->name(), sideTopology[i].second)] = 0;
745 side_map[std::make_pair(sideTopology[i].first->name(), sideTopology[i].second)] = 0;
746 }
747
748 separate_surface_element_sides(element, sides, get_region(), topo_map, side_map,
749 split_type);
750 }
751 else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
752 // There are multiple side types in the model. Iterate
753 // through the elements in the sideset, determine their parent
754 // element block using blocks element topology and the side
755 // number, determine the side type.
756
757 // Seed the topo_map map with <block->name, face_topo>
758 // pairs so we are sure that all processors have the same
759 // starting topo_map (size and order).
760 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
761
762 for (int i = 0; i < elementBlockCount; i++) {
763 Ioss::ElementBlock *block = element_blocks[i];
764 const std::string &name = block->name();
765 const Ioss::ElementTopology *common_ftopo = block->topology()->boundary_type(0);
766 if (common_ftopo != nullptr) {
767 // All sides of this element block's topology have the same topology
768 topo_map[std::make_pair(name, common_ftopo)] = 0;
769 side_map[std::make_pair(name, common_ftopo)] = 0;
770 }
771 else {
772 // The sides have different topology, iterate over
773 // them and create an entry for the unique face
774 // topology types
775 int par_dim = block->topology()->parametric_dimension();
776 if (par_dim == 2 || par_dim == 3) {
777 int my_side_count = block->topology()->number_boundaries();
778 for (int ii = 0; ii < my_side_count; ii++) {
779 const Ioss::ElementTopology *topo = block->topology()->boundary_type(ii + 1);
780 topo_map[std::make_pair(name, topo)] = 0;
781 side_map[std::make_pair(name, topo)] = 0;
782 }
783 }
784 }
785 }
786 separate_surface_element_sides(element, sides, get_region(), topo_map, side_map,
787 split_type);
788 }
789
790 // End of first step in splitting. Check among all processors
791 // to see which potential splits have sides in them...
792 Ioss::IntVector global_side_counts(topo_map.size());
793 {
794 int i = 0;
795 {
796 TopologyMap::const_iterator I = topo_map.begin();
797 while (I != topo_map.end()) {
798 global_side_counts[i++] = (*I).second;
799 ++I;
800 }
801 }
802
803 // If splitting by element block, also sync the side_map
804 // information which specifies whether the sideset has
805 // consistent sides for all elements. Only really used for
806 // shells, but easier to just set the value on all surfaces
807 // in the element block split case.
808 if (side_map.size() == topo_map.size()) {
809 global_side_counts.resize(topo_map.size() + side_map.size());
810
811 {
812 TopologyMap::const_iterator I = side_map.begin();
813 while (I != side_map.end()) {
814 global_side_counts[i++] = (*I).second;
815 ++I;
816 }
817 }
818 }
819
820 // See if any processor has non-zero count for the topo_map counts
821 // For the side_map, need the max value.
822 util().global_array_minmax(global_side_counts, Ioss::ParallelUtils::DO_MAX);
823 }
824
825 // Create Side Blocks
826
827 int i = 0;
828 TopologyMap::const_iterator I = topo_map.begin();
829 assert(I != topo_map.end());
830 while (I != topo_map.end()) {
831 if (global_side_counts[i++] > 0) {
832 const std::string topo_or_block_name = (*I).first.first;
833 const Ioss::ElementTopology *side_topo = (*I).first.second;
834 assert(side_topo != nullptr);
835 #if 0
836 if (side_topo->parametric_dimension() == topology_dimension-1 ||
837 split_type == Ioss::SPLIT_BY_DONT_SPLIT ) {
838 #else
839 if (true) {
840 #endif
841 int my_side_count = (*I).second;
842
843 std::string side_block_name =
844 "surface_" + topo_or_block_name + "_" + side_topo->name();
845 if (side_set_name == "universal_sideset") {
846 side_block_name = side_set_name;
847 }
848 else {
849 if (sid == "")
850 side_block_name = Ioss::Utils::encode_entity_name(side_block_name, id);
851 else {
852 side_block_name += "_";
853 side_block_name += sid;
854 }
855 }
856
857 Ioss::ElementBlock *block = nullptr;
858 // Need to get elem_topo....
859 const Ioss::ElementTopology *elem_topo = nullptr;
860 if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) {
861 elem_topo = Ioss::ElementTopology::factory(topo_or_block_name);
862 }
863 else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
864 block = get_region()->get_element_block(topo_or_block_name);
865 if (block == nullptr) {
866 std::ostringstream errmsg;
867 errmsg << "INTERNAL ERROR: Could not find element block '" << topo_or_block_name
868 << "' Something is wrong in the Iopg::DatabaseIO class. Please report.\n";
869 IOSS_ERROR(errmsg);
870 }
871 elem_topo = block->topology();
872 }
873 if (split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
874 // Most likely this is "unknown", but can be a true
875 // topology if there is only a single element block in
876 // the model.
877 elem_topo = Ioss::ElementTopology::factory(topo_or_block_name);
878 }
879 assert(elem_topo != nullptr);
880
881 Ioss::SideBlock *side_block = new Ioss::SideBlock(
882 this, side_block_name, side_topo->name(), elem_topo->name(), my_side_count);
883 side_set->add(side_block);
884
885 // Note that all sideblocks within a specific
886 // sideset might have the same id.
887 assert(side_block != nullptr);
888 side_block->property_add(Ioss::Property("id", id));
889 side_block->property_add(Ioss::Property("guid", util().generate_guid(id)));
890
891 // If splitting by element block, need to set the
892 // element block member on this side block.
893 if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
894 side_block->set_parent_element_block(block);
895 }
896
897 // If we calculated whether the element side is
898 // consistent for all faces in this block, then
899 // tell the block which side it is, or that they are
900 // inconsistent. If it wasn't calculated above, then it
901 // will be calculated on the fly when/if requested.
902 // This is to avoid reading the sideset bulk data in
903 // cases where we don't need to read it, but if we are
904 // already reading it (to split the sidesets), then use
905 // the data when we have it.
906 if (!side_map.empty()) {
907 // Set a property indicating which element side
908 // (1-based) all faces in this block are applied to.
909 // If they are not all assigned to the same element
910 // side, indicate this with a side equal to 0.
911 //
912 // (note: 'i' has already been incremented earlier in
913 // the loop. We need previous value here...)
914 int side = global_side_counts[i - 1 + topo_map.size()];
915 if (side == 999)
916 side = 0;
917 assert(side <= elem_topo->number_boundaries());
918 side_block->set_consistent_side_number(side);
919 }
920
921 // Add an alias...
922 get_region()->add_alias(side_block);
923
924 if (split_type != Ioss::SPLIT_BY_DONT_SPLIT && side_set_name != "universal_sideset") {
925 std::string storage = "Real[";
926 storage += std::to_string(side_topo->number_nodes());
927 storage += "]";
928 side_block->field_add(Ioss::Field("distribution_factors", Ioss::Field::REAL,
929 storage, Ioss::Field::MESH, my_side_count));
930 }
931
932 if (side_set_name == "universal_sideset") {
933 side_block->field_add(Ioss::Field("side_ids", Ioss::Field::INTEGER, "scalar",
934 Ioss::Field::MESH, my_side_count));
935 }
936 }
937 }
938 ++I;
939 }
940 }
941 }
942 } // namespace Iopg
943
944 bool DatabaseIO::begin_nl(Ioss::State /* state */) { return true; }
945
946 bool DatabaseIO::end_nl(Ioss::State /* state */) { return true; }
947
948 int64_t DatabaseIO::get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
949 void *data, size_t data_size) const
950 {
951 size_t num_to_get = field.verify(data_size);
952 if (num_to_get > 0) {
953
954 Ioss::Field::RoleType role = field.get_role();
955 if (role == Ioss::Field::MESH) {
956
957 if ((field.get_name() == "mesh_model_coordinates_x") ||
958 (field.get_name() == "mesh_model_coordinates_y") ||
959 (field.get_name() == "mesh_model_coordinates_z")) {
960
961 std::vector<double> x(num_to_get);
962 std::vector<double> y(num_to_get);
963 std::vector<double> z;
964 if (spatialDimension == 3)
965 z.resize(num_to_get);
966
967 double *rdata = static_cast<double *>(data);
968
969 int ierr = im_ex_get_coord(get_file_pointer(), Data(x), Data(y), Data(z));
970 if (ierr < 0) {
971 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
972 }
973 size_t index = 0;
974
975 if (field.get_name() == "mesh_model_coordinates_x") {
976 for (size_t i = 0; i < num_to_get; i++) {
977 rdata[index++] = x[i];
978 }
979 }
980 else if (field.get_name() == "mesh_model_coordinates_y") {
981 for (size_t i = 0; i < num_to_get; i++) {
982 rdata[index++] = y[i];
983 }
984 }
985 else {
986 for (size_t i = 0; i < num_to_get; i++) {
987 if (spatialDimension == 3)
988 rdata[index++] = z[i];
989 }
990 }
991 }
992
993 else if (field.get_name() == "mesh_model_coordinates") {
994 // Data required by upper classes store x0, y0, z0, ... xn,
995 // yn, zn. Data stored in exodusII file is x0, ..., xn, y0,
996 // ..., yn, z0, ..., zn so we have to allocate some scratch
997 // memory to read in the data and then map into supplied
998 // 'data'
999 std::vector<double> x(num_to_get);
1000 std::vector<double> y(num_to_get);
1001 std::vector<double> z;
1002 if (spatialDimension == 3)
1003 z.resize(num_to_get);
1004
1005 // Cast 'data' to correct size -- double
1006 double *rdata = static_cast<double *>(data);
1007
1008 int ierr = im_ex_get_coord(get_file_pointer(), Data(x), Data(y), Data(z));
1009 if (ierr < 0)
1010 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1011
1012 size_t index = 0;
1013 for (size_t i = 0; i < num_to_get; i++) {
1014 rdata[index++] = x[i];
1015 rdata[index++] = y[i];
1016 if (spatialDimension == 3)
1017 rdata[index++] = z[i];
1018 }
1019 }
1020
1021 // NOTE: The implicit_ids field is ONLY provided for backward-
1022 // compatibility and should not be used unless absolutely
1023 // required. For generated mesh, the implicit_ids and ids are the same.
1024 else if (field.get_name() == "ids" || field.get_name() == "implicit_ids") {
1025 // Map the local ids in this node block
1026 // (1...node_count) to global node ids.
1027 get_node_map().map_implicit_data(data, field, num_to_get, 0);
1028 }
1029 else if (field.get_name() == "connectivity") {
1030 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
1031 }
1032 else if (field.get_name() == "owning_processor") {
1033 if (isParallel) {
1034 Ioss::CommSet *css = get_region()->get_commset("commset_node");
1035 int *idata = static_cast<int *>(data);
1036 for (size_t i = 0; i < num_to_get; i++) {
1037 idata[i] = myProcessor;
1038 }
1039
1040 std::vector<int> ent_proc;
1041 css->get_field_data("entity_processor_raw", ent_proc);
1042 for (size_t i = 0; i < ent_proc.size(); i += 2) {
1043 int node = ent_proc[i + 0];
1044 int proc = ent_proc[i + 1];
1045 if (proc < myProcessor) {
1046 idata[node - 1] = proc;
1047 }
1048 }
1049 }
1050 else {
1051 // Serial case...
1052 int *idata = static_cast<int *>(data);
1053 for (int64_t i = 0; i < nodeCount; i++) {
1054 idata[i] = 0;
1055 }
1056 }
1057 }
1058 else {
1059 num_to_get = Ioss::Utils::field_warning(nb, field, "input");
1060 }
1061 }
1062 }
1063 return num_to_get;
1064 }
1065
1066 int64_t DatabaseIO::get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
1067 void *data, size_t data_size) const
1068 {
1069 size_t num_to_get = field.verify(data_size);
1070 if (num_to_get > 0) {
1071
1072 int id = eb->get_property("id").get_int();
1073 int my_element_count = eb->entity_count();
1074 Ioss::Field::RoleType role = field.get_role();
1075
1076 if (role == Ioss::Field::MESH) {
1077 // Handle the MESH fields required for an ExodusII file model.
1078 // (The 'genesis' portion)
1079
1080 if (field.get_name() == "connectivity" || field.get_name() == "connectivity_raw") {
1081 int element_nodes = eb->topology()->number_nodes();
1082 assert(field.raw_storage()->component_count() == element_nodes);
1083
1084 // The connectivity is stored in a 1D array.
1085 // The element_node index varies fastet
1086 if (my_element_count > 0) {
1087 int ierr = im_ex_get_elem_conn(get_file_pointer(), id, static_cast<int *>(data));
1088 if (ierr < 0)
1089 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1090
1091 // Now, map the nodes in the connectivity from local to global ids
1092 if (field.get_name() == "connectivity") {
1093 get_node_map().map_data(data, field, num_to_get * element_nodes);
1094 }
1095 }
1096 }
1097 else if (field.get_name() == "ids") {
1098 // Map the local ids in this element block
1099 // (eb_offset+1...eb_offset+1+my_element_count) to global element ids.
1100 get_element_map().map_implicit_data(data, field, num_to_get, eb->get_offset());
1101 }
1102 else {
1103 num_to_get = Ioss::Utils::field_warning(eb, field, "input");
1104 }
1105 }
1106
1107 else if (role == Ioss::Field::ATTRIBUTE) {
1108 // ERROR: Pamgen doesn't support elements with attributes...
1109 }
1110 else if (role == Ioss::Field::TRANSIENT) {
1111 // ERROR: Pamgen doesn't support transient data...
1112 }
1113 else if (role == Ioss::Field::REDUCTION) {
1114 num_to_get = Ioss::Utils::field_warning(eb, field, "input reduction");
1115 }
1116 }
1117 return num_to_get;
1118 }
1119
1120 int64_t DatabaseIO::get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
1121 void *data, size_t data_size) const
1122 {
1123 {
1124 Ioss::SerializeIO serializeIO_(this);
1125
1126 size_t num_to_get = field.verify(data_size);
1127
1128 if (num_to_get > 0) {
1129 int entity_count = cs->entity_count();
1130
1131 // Return the <entity (node or side), processor> pair
1132 if (field.get_name() == "entity_processor" || field.get_name() == "entity_processor_raw") {
1133
1134 // Check type -- node or side
1135 std::string type = cs->get_property("entity_type").get_string();
1136
1137 // Allocate temporary storage space
1138 Ioss::IntVector entities(num_to_get);
1139 Ioss::IntVector procs(num_to_get);
1140
1141 if (type == "node") {
1142 int cm_offset = 0;
1143 int i;
1144 for (i = 0; i < commsetNodeCount; i++) {
1145 int ierr = im_ne_get_node_cmap(get_file_pointer(), nodeCmapIds[i],
1146 &entities[cm_offset], &procs[cm_offset], myProcessor);
1147 if (ierr < 0)
1148 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1149 cm_offset += nodeCmapNodeCnts[i];
1150 }
1151 assert(cm_offset == entity_count);
1152
1153 // Convert local node id to global node id and store in 'data'
1154 int *entity_proc = static_cast<int *>(data);
1155 if (field.get_name() == "entity_processor") {
1156 const Ioss::MapContainer &map = get_node_map().map();
1157
1158 int j = 0;
1159 for (i = 0; i < entity_count; i++) {
1160 int local_id = entities[i];
1161 entity_proc[j++] = map[local_id];
1162 entity_proc[j++] = procs[i];
1163 }
1164 }
1165 else {
1166 int j = 0;
1167 for (i = 0; i < entity_count; i++) {
1168 entity_proc[j++] = entities[i];
1169 entity_proc[j++] = procs[i];
1170 }
1171 }
1172 }
1173 else if (type == "side") {
1174 Ioss::IntVector sides(entity_count);
1175 int cm_offset = 0;
1176 int i;
1177 for (i = 0; i < commsetElemCount; i++) {
1178 int ierr =
1179 im_ne_get_elem_cmap(get_file_pointer(), elemCmapIds[i], &entities[cm_offset],
1180 &sides[cm_offset], &procs[cm_offset], myProcessor);
1181 if (ierr < 0)
1182 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1183 cm_offset += elemCmapElemCnts[i];
1184 }
1185 assert(cm_offset == entity_count);
1186
1187 int *entity_proc = static_cast<int *>(data);
1188 if (field.get_name() == "entity_processor") {
1189 const Ioss::MapContainer &map = get_element_map().map();
1190
1191 int j = 0;
1192 for (i = 0; i < entity_count; i++) {
1193 int local_id = entities[i];
1194 entity_proc[j++] = map[local_id];
1195 entity_proc[j++] = sides[i];
1196 entity_proc[j++] = procs[i];
1197 }
1198 }
1199 else {
1200 int j = 0;
1201 for (i = 0; i < entity_count; i++) {
1202 entity_proc[j++] = entities[i];
1203 entity_proc[j++] = sides[i];
1204 entity_proc[j++] = procs[i];
1205 }
1206 }
1207 }
1208 else {
1209 std::ostringstream errmsg;
1210 errmsg << "Invalid commset type " << type;
1211 IOSS_ERROR(errmsg);
1212 }
1213 }
1214 else if (field.get_name() == "ids") {
1215 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
1216 }
1217 else {
1218 num_to_get = Ioss::Utils::field_warning(cs, field, "input");
1219 }
1220 }
1221 return num_to_get;
1222 }
1223 }
1224
1225 int64_t DatabaseIO::get_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field,
1226 void *data, size_t data_size) const
1227 {
1228 size_t num_to_get = field.verify(data_size);
1229 if (num_to_get > 0) {
1230
1231 int id = fb->get_property("id").get_int();
1232 size_t entity_count = fb->entity_count();
1233 if (num_to_get != entity_count) {
1234 std::ostringstream errmsg;
1235 errmsg << "Partial field input not yet implemented for side blocks";
1236 IOSS_ERROR(errmsg);
1237 }
1238
1239 int number_sides;
1240 int number_distribution_factors;
1241 int ierr = im_ex_get_side_set_param(get_file_pointer(), id, &number_sides,
1242 &number_distribution_factors);
1243 if (ierr < 0)
1244 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1245
1246 Ioss::Field::RoleType role = field.get_role();
1247 if (role == Ioss::Field::MESH) {
1248
1249 // In exodusII, we may have split the sideset into multiple
1250 // side blocks if there are multiple side topologies in the
1251 // sideset. Because of this, the passed in 'data' may not be
1252 // large enough to hold the data residing in the sideset and we
1253 // may need to allocate a temporary array... This can be checked
1254 // by comparing the size of the sideset with the 'my_side_count' of
1255 // the side block.
1256
1257 if (field.get_name() == "side_ids") {}
1258
1259 else if (field.get_name() == "ids") {
1260 // In exodusII, the 'side set' is stored as a sideset. A
1261 // sideset has a list of elements and a corresponding local
1262 // element side (1-based) The side id is: side_id =
1263 // 10*element_id + local_side_number This assumes that all
1264 // sides in a sideset are boundary sides. Since we
1265 // only have a single array, we need to allocate an extra array
1266 // to store all of the data. Note also that the element_id is
1267 // the global id but only the local id is stored so we need to
1268 // map from local_to_global prior to generating the side id...
1269
1270 // Get the element number map (1-based)...
1271 const Ioss::MapContainer &map = get_element_map().map();
1272
1273 // Allocate space for local side number, use 'data' as temporary
1274 // storage for element numbers and overwrite with the side
1275 // numbers.
1276 Ioss::IntVector sides;
1277 int *element = nullptr;
1278 int *ids = static_cast<int *>(data);
1279 if (number_sides == static_cast<int>(entity_count)) {
1280 // Only 1 side block in this sideset
1281 sides.resize(entity_count);
1282 element = static_cast<int *>(data);
1283 }
1284 else {
1285 // Multiple side blocks in sideset. Need to allocate memory to
1286 // store sideset
1287 sides.resize(number_sides);
1288 element = new int[number_sides];
1289 }
1290
1291 ierr = im_ex_get_side_set(get_file_pointer(), id, element, Data(sides));
1292 if (ierr < 0)
1293 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1294
1295 if (number_sides == static_cast<int>(entity_count)) {
1296 for (size_t iel = 0; iel < entity_count; iel++) {
1297 int new_id = 10 * map[element[iel]] + sides[iel];
1298 ids[iel] = new_id;
1299 }
1300 }
1301 else {
1302 Ioss::IntVector is_valid_side;
1303 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, 4, element, Data(sides),
1304 number_sides, get_region());
1305 size_t ieb = 0;
1306 for (int iel = 0; iel < number_sides; iel++) {
1307 if (is_valid_side[iel] == 1) {
1308 int new_id = 10 * map[element[iel]] + sides[iel];
1309 ids[ieb++] = new_id;
1310 }
1311 }
1312 assert(ieb == entity_count);
1313 }
1314
1315 if (number_sides != static_cast<int>(entity_count))
1316 delete[] element;
1317 }
1318 else if (field.get_name() == "element_side") {
1319 // In exodusII, the 'side set' is stored as a sideset. A sideset
1320 // has a list of elements and a corresponding local element side
1321 // (1-based)
1322
1323 // Since we only have a single array, we need to allocate an extra
1324 // array to store all of the data. Note also that the element_id
1325 // is the global id but only the local id is stored so we need to
1326 // map from local_to_global prior to generating the side id...
1327
1328 // Get the element number map (1-based)...
1329 const Ioss::MapContainer &map = get_element_map().map();
1330
1331 // Allocate space for local side number and element numbers
1332 // numbers.
1333 int *element_side = static_cast<int *>(data);
1334
1335 Ioss::IntVector element(number_sides);
1336 Ioss::IntVector sides(number_sides);
1337
1338 ierr = im_ex_get_side_set(get_file_pointer(), id, Data(element), Data(sides));
1339 if (ierr < 0)
1340 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1341
1342 if (number_sides == static_cast<int>(entity_count)) {
1343 size_t index = 0;
1344 for (size_t iel = 0; iel < entity_count; iel++) {
1345 element_side[index++] = map[element[iel]];
1346 element_side[index++] = sides[iel];
1347 }
1348 assert(index / 2 == entity_count);
1349 }
1350 else {
1351 Ioss::IntVector is_valid_side;
1352 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, 4, Data(element),
1353 Data(sides), number_sides, get_region());
1354
1355 size_t index = 0;
1356 for (int iel = 0; iel < number_sides; iel++) {
1357 if (is_valid_side[iel] == 1) {
1358 // This side belongs in the side block
1359 element_side[index++] = map[element[iel]];
1360 element_side[index++] = sides[iel];
1361 }
1362 }
1363 assert(index / 2 == entity_count);
1364 }
1365 }
1366 else if (field.get_name() == "connectivity") {
1367 // The side connectivity needs to be generated 'on-the-fly' from
1368 // the element number and local side of that element. A sideset
1369 // can span multiple element blocks, and contain multiple side
1370 // types; the side block contains side of similar topology.
1371 ierr = get_side_connectivity(fb, id, entity_count, static_cast<int *>(data),
1372 data_size / sizeof(int));
1373 if (ierr < 0)
1374 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1375 }
1376 else if (field.get_name() == "distribution_factors") {
1377 ierr = get_side_distributions(fb, id, entity_count, static_cast<double *>(data),
1378 data_size / sizeof(double));
1379 if (ierr < 0)
1380 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1381 }
1382 else {
1383 num_to_get = Ioss::Utils::field_warning(fb, field, "input");
1384 }
1385 }
1386 else if (role == Ioss::Field::TRANSIENT) {
1387 unsupported("side set transient fields");
1388 }
1389 }
1390 return num_to_get;
1391 }
1392
1393 int64_t DatabaseIO::get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
1394 void *data, size_t data_size) const
1395 {
1396 size_t num_to_get = field.verify(data_size);
1397 if (num_to_get > 0) {
1398
1399 int id = ns->get_property("id").get_int();
1400 Ioss::Field::RoleType role = field.get_role();
1401 if (role == Ioss::Field::MESH) {
1402
1403 if (field.get_name() == "ids") {
1404 int ierr = im_ex_get_node_set(get_file_pointer(), id, static_cast<int *>(data));
1405 if (ierr < 0)
1406 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1407
1408 // Convert the local node ids to global ids
1409 get_node_map().map_data(data, field, num_to_get);
1410 }
1411 else if (field.get_name() == "distribution_factors") {
1412 // Not supported by pamgen. Fill data with '1'
1413 double *rdata = static_cast<double *>(data);
1414 for (size_t i = 0; i < num_to_get; i++)
1415 rdata[i] = 1.0;
1416 }
1417 else {
1418 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
1419 }
1420 }
1421 else if (role == Ioss::Field::TRANSIENT) {
1422 unsupported("transient nodeset fields");
1423 }
1424 }
1425 return num_to_get;
1426 }
1427
1428 const Ioss::Map &DatabaseIO::get_node_map() const
1429 {
1430 // Allocate space for node number map and read it in...
1431 // Can be called multiple times, allocate 1 time only
1432 if (nodeMap.map().empty()) {
1433 nodeMap.set_size(nodeCount);
1434
1435 if (is_input()) {
1436 std::vector<int> node_map(nodeMap.map().size() - 1);
1437 int error = im_ex_get_node_num_map(get_file_pointer(), Data(node_map));
1438 if (error < 0) {
1439 // Clear out the vector...
1440 Ioss::MapContainer().swap(nodeMap.map());
1441 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1442 }
1443 nodeMap.set_map(Data(node_map), node_map.size(), 0, true);
1444 }
1445 else {
1446 unsupported("output nodal id map");
1447 }
1448 }
1449 return nodeMap;
1450 }
1451
1452 const Ioss::Map &DatabaseIO::get_element_map() const
1453 {
1454 // Allocate space for element number map and read it in...
1455 // Can be called multiple times, allocate 1 time only
1456 if (elemMap.map().empty()) {
1457 elemMap.set_size(elementCount);
1458
1459 if (is_input()) {
1460 std::vector<int> elem_map(elemMap.map().size() - 1);
1461 int error = im_ex_get_elem_num_map(get_file_pointer(), Data(elem_map));
1462 if (error < 0) {
1463 // Clear out the vector...
1464 Ioss::MapContainer().swap(elemMap.map());
1465 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1466 }
1467 elemMap.set_map(Data(elem_map), elem_map.size(), 0, true);
1468 }
1469 else {
1470 unsupported("output element map");
1471 }
1472 }
1473 return elemMap;
1474 }
1475
1476 void DatabaseIO::compute_block_membership_nl(Ioss::SideBlock *sideblock,
1477 Ioss::NameList &block_membership) const
1478 {
1479 Ioss::IntVector block_ids(elementBlockCount);
1480 if (elementBlockCount == 1) {
1481 block_ids[0] = 1;
1482 }
1483 else {
1484 Ioss::IntVector element_side;
1485 sideblock->get_field_data("element_side", element_side);
1486
1487 size_t number_sides = element_side.size() / 2;
1488 Ioss::ElementBlock *block = nullptr;
1489 for (size_t iel = 0; iel < number_sides; iel++) {
1490 int elem_id = element_side[2 * iel]; // Vector contains both element and side.
1491 elem_id = element_global_to_local(elem_id);
1492 if (block == nullptr || !block->contains(elem_id)) {
1493 block = get_region()->get_element_block(elem_id);
1494 assert(block != nullptr);
1495 int block_order = block->get_property("original_block_order").get_int();
1496 block_ids[block_order] = 1;
1497 }
1498 }
1499 }
1500
1501 // Synchronize among all processors....
1502 if (isParallel) {
1503 util().global_array_minmax(block_ids, Ioss::ParallelUtils::DO_MAX);
1504 }
1505
1506 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
1507
1508 for (int i = 0; i < elementBlockCount; i++) {
1509 if (block_ids[i] == 1) {
1510 Ioss::ElementBlock *block = element_blocks[i];
1511 block_membership.push_back(block->name());
1512 }
1513 }
1514 }
1515
1516 int DatabaseIO::get_side_connectivity(const Ioss::SideBlock *fb, int id, int, int *fconnect,
1517 size_t /* data_size */) const
1518 {
1519 // Get size of data stored on the file...
1520 int number_sides;
1521 int number_distribution_factors;
1522 int ierr = im_ex_get_side_set_param(get_file_pointer(), id, &number_sides,
1523 &number_distribution_factors);
1524 if (ierr < 0)
1525 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1526
1527 // Allocate space for element and local side number
1528 assert(number_sides > 0);
1529 //----
1530 Ioss::IntVector element(number_sides);
1531 Ioss::IntVector side(number_sides);
1532
1533 ierr = im_ex_get_side_set(get_file_pointer(), id, Data(element), Data(side));
1534 if (ierr < 0)
1535 pamgen_error(get_file_pointer(), __LINE__, myProcessor);
1536 //----
1537
1538 Ioss::IntVector is_valid_side;
1539 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, 4, Data(element), Data(side),
1540 number_sides, get_region());
1541
1542 Ioss::IntVector elconnect;
1543 int elconsize = 0; // Size of currently allocated connectivity block
1544 Ioss::ElementBlock *conn_block = nullptr; // Block that we currently
1545 // have connectivity for
1546
1547 Ioss::ElementBlock *block = nullptr;
1548
1549 Ioss::IntVector side_elem_map; // Maps the side into the elements
1550 // connectivity array
1551 int current_side = -1;
1552 int nelnode = 0;
1553 int nfnodes = 0;
1554 int ieb = 0;
1555 int offset = 0;
1556 for (int iel = 0; iel < number_sides; iel++) {
1557 if (is_valid_side[iel] == 1) {
1558
1559 int elem_id = element[iel];
1560
1561 // ensure we have correct connectivity
1562 block = get_region()->get_element_block(elem_id);
1563 assert(block != nullptr);
1564 assert(block->topology() != nullptr);
1565
1566 if (conn_block != block) {
1567 int nelem = block->entity_count();
1568 nelnode = block->topology()->number_nodes();
1569 // Used to map element number into position in connectivity array.
1570 // E.g., element 97 is the (97-offset)th element in this block and
1571 // is stored in array index (97-offset-1).
1572 offset = block->get_offset() + 1;
1573 if (elconsize < nelem * nelnode) {
1574 elconsize = nelem * nelnode;
1575 elconnect.resize(elconsize);
1576 }
1577 get_field_internal(block, block->get_field("connectivity"), Data(elconnect),
1578 nelem * nelnode * sizeof(int));
1579 conn_block = block;
1580 current_side = -1;
1581 }
1582
1583 // NOTE: Element connectivity is returned with nodes in global id space
1584 if (current_side != side[iel]) {
1585 int side_id = side[iel];
1586 side_elem_map = block->topology()->boundary_connectivity(side_id);
1587 current_side = side[iel];
1588 assert(block->topology()->boundary_type(side[iel]) != nullptr);
1589 nfnodes = block->topology()->boundary_type(side[iel])->number_nodes();
1590 }
1591 for (int inode = 0; inode < nfnodes; inode++) {
1592 int global_node = elconnect[(elem_id - offset) * nelnode + side_elem_map[inode]];
1593 fconnect[ieb++] = global_node;
1594 }
1595 }
1596 }
1597 return ierr;
1598 }
1599
1600 // Get distribution factors for the specified side block
1601 int DatabaseIO::get_side_distributions(const Ioss::SideBlock *fb, int id, int my_side_count,
1602 double *dist_fact, size_t /* data_size */) const
1603 {
1604 // NOTE: In pamgen, all distribution factors are 1.0
1605 const Ioss::ElementTopology *ftopo = fb->topology();
1606 int nfnodes = ftopo->number_nodes();
1607 for (int i = 0; i < nfnodes * my_side_count; i++)
1608 dist_fact[i] = 1.0;
1609 return 0;
1610 }
1611
1612 //------------------------------------------------------------------------
1613 } // namespace Iopg
1614 namespace {
1615 void separate_surface_element_sides(Ioss::IntVector &element, Ioss::IntVector &sides,
1616 Ioss::Region *region, Iopg::TopologyMap &topo_map,
1617 Iopg::TopologyMap &side_map,
1618 Ioss::SurfaceSplitType split_type)
1619 {
1620 if (!element.empty()) {
1621 Ioss::ElementBlock *block = nullptr;
1622 // Topology of sides in current element block
1623 const Ioss::ElementTopology *common_ftopo = nullptr;
1624 const Ioss::ElementTopology *topo = nullptr; // Topology of current side
1625 int current_side = -1;
1626
1627 for (size_t iel = 0; iel < element.size(); iel++) {
1628 int elem_id = element[iel];
1629 if (block == nullptr || !block->contains(elem_id)) {
1630 block = region->get_element_block(elem_id);
1631 assert(block != nullptr);
1632
1633 // nullptr if hetero sides on element
1634 common_ftopo = block->topology()->boundary_type(0);
1635 if (common_ftopo != nullptr)
1636 topo = common_ftopo;
1637 current_side = -1;
1638 }
1639
1640 if (common_ftopo == nullptr && sides[iel] != current_side) {
1641 current_side = sides[iel];
1642 topo = block->topology()->boundary_type(sides[iel]);
1643 assert(topo != nullptr);
1644 }
1645 std::pair<std::string, const Ioss::ElementTopology *> name_topo;
1646 if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) {
1647 name_topo = std::make_pair(block->topology()->name(), topo);
1648 }
1649 else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
1650 name_topo = std::make_pair(block->name(), topo);
1651 }
1652 topo_map[name_topo]++;
1653 if (side_map[name_topo] == 0)
1654 side_map[name_topo] = sides[iel];
1655 else if (side_map[name_topo] != sides[iel]) {
1656 // Not a consistent side for all sides in this
1657 // sideset. Set to large number. Note that maximum
1658 // sides/element is 6, so don't have to worry about
1659 // a valid element having 999 sides (unless go to
1660 // arbitrary polyhedra some time...) Using a large
1661 // number instead of -1 makes it easier to check the
1662 // parallel consistency...
1663 side_map[name_topo] = 999;
1664 }
1665 }
1666 }
1667 }
1668 } // namespace