Debian Codegraph @

packages/seacas/libraries/ioss/src/visualization/exodus/Iovs_exodus_DatabaseIO.C

raw
1 // Copyright(C) 1999-2024 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 "Ioss_CodeTypes.h"
8 #include <tokenize.h>
9 #include <visualization/exodus/Iovs_exodus_DatabaseIO.h>
10 #include <visualization/utils/Iovs_Utils.h>
11
12 #include <cctype>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iterator>
16
17 #include "Ioss_ElementTopology.h"
18 #include "Ioss_FileInfo.h"
19 #include "Ioss_ParallelUtils.h"
20 #include "Ioss_SerializeIO.h"
21 #include "Ioss_SubSystem.h"
22 #include "Ioss_SurfaceSplit.h"
23 #include "Ioss_Utils.h"
24
25 namespace { // Internal helper functions
26 int64_t get_id(const Ioss::GroupingEntity *entity, Iovs_exodus::EntityIdSet *idset);
27 bool set_id(const Ioss::GroupingEntity *entity, Iovs_exodus::EntityIdSet *idset);
28 int64_t extract_id(const std::string &name_id);
29 size_t get_num_to_get(const Ioss::Field &field, size_t data_size);
30 } // End anonymous namespace
31
32 namespace Iovs_exodus {
33 int field_warning(const Ioss::GroupingEntity *ge, const Ioss::Field &field,
34 const std::string &inout);
35
36 DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename,
37 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
38 const Ioss::PropertyManager &props)
39 : Ioss::DatabaseIO(region,
40 Iovs::Utils::getInstance().getDatabaseOutputFilePath(filename, props),
41 db_usage, communicator, props),
42 isInput(false), singleProcOnly(false), doLogging(false), nodeBlockCount(0),
43 elementBlockCount(0)
44 {
45
46 Iovs::Utils::DatabaseInfo dbinfo;
47 dbinfo.databaseFilename = this->DBFilename;
48 dbinfo.separatorCharacter = std::string(1, this->get_field_separator());
49 dbinfo.parallelUtils = &this->util();
50
51 Iovs::Utils::getInstance().checkDbUsage(db_usage);
52 Iovs::Utils::getInstance().createDatabaseOutputFile(dbinfo);
53 dbState = Ioss::STATE_UNKNOWN;
54 this->globalNodeAndElementIDsCreated = false;
55
56 Iovs::Utils::getInstance().writeToCatalystLogFile(dbinfo, props);
57 this->catExoMesh = Iovs::Utils::getInstance().createCatalystExodusMesh(dbinfo, props);
58 }
59
60 DatabaseIO::~DatabaseIO() { this->catExoMesh->Delete(); }
61
62 bool DatabaseIO::begin_nl(Ioss::State state)
63 {
64 dbState = state;
65 Ioss::Region *region = this->get_region();
66
67 if (state == Ioss::STATE_MODEL) {
68 CatalystExodusMeshBase::ElementBlockIdNameList ebinList;
69 Ioss::ElementBlockContainer const &ebc = region->get_element_blocks();
70 for (auto i : ebc) {
71 ebinList.emplace_back((int)get_id(i, &ids_), i->name());
72 }
73 this->catExoMesh->InitializeElementBlocks(ebinList);
74 }
75 return true;
76 }
77
78 bool DatabaseIO::end_nl(Ioss::State state)
79 {
80 // Transitioning out of state 'state'
81 assert(state == dbState);
82 switch (state) {
83 case Ioss::STATE_DEFINE_MODEL: write_meta_data(); break;
84 case Ioss::STATE_DEFINE_TRANSIENT: break;
85 default: // ignore everything else...
86 break;
87 }
88
89 {
90 dbState = Ioss::STATE_UNKNOWN;
91 }
92
93 return true;
94 }
95
96 // Default versions do nothing at this time...
97 // Will be used for global variables...
98 bool DatabaseIO::begin_state_nl(int state, double time)
99 {
100 Ioss::SerializeIO serializeIO_(this);
101
102 if (!this->globalNodeAndElementIDsCreated) {
103 this->create_global_node_and_element_ids();
104 }
105
106 this->catExoMesh->SetTimeData(time, state - 1);
107
108 return true;
109 }
110
111 bool DatabaseIO::end_state_nl(int /*state*/, double /*time*/)
112 {
113 Ioss::SerializeIO serializeIO_(this);
114 std::vector<int> error_codes;
115 std::vector<std::string> error_messages;
116 this->catExoMesh->logMemoryUsageAndTakeTimerReading();
117 this->catExoMesh->PerformCoProcessing(error_codes, error_messages);
118 this->catExoMesh->logMemoryUsageAndTakeTimerReading();
119 Iovs::Utils::getInstance().reportCatalystErrorMessages(error_codes, error_messages,
120 this->parallel_rank());
121 this->catExoMesh->ReleaseMemory();
122
123 return true;
124 }
125
126 void DatabaseIO::read_meta_data_nl() {}
127
128 void DatabaseIO::create_global_node_and_element_ids() const
129 {
130 const Ioss::ElementBlockContainer &element_blocks = this->get_region()->get_element_blocks();
131 Ioss::ElementBlockContainer::const_iterator I;
132 for (I = element_blocks.begin(); I != element_blocks.end(); ++I) {
133 int bid = get_id((*I), &ids_);
134 int64_t eb_offset = (*I)->get_offset();
135 this->catExoMesh->CreateElementVariable("ids", 1, bid, &this->elemMap.map()[eb_offset + 1]);
136 std::vector<int> object_id((*I)->entity_count(), bid);
137 this->catExoMesh->CreateElementVariable("object_id", 1, bid, Data(object_id));
138 }
139 this->catExoMesh->CreateNodalVariable("ids", 1, &this->nodeMap.map()[1]);
140
141 this->globalNodeAndElementIDsCreated = true;
142 }
143
144 //------------------------------------------------------------------------
145 int64_t DatabaseIO::put_field_internal(const Ioss::Region * /* region */,
146 const Ioss::Field &field, void *data,
147 size_t data_size) const
148 {
149 // For now, assume that all TRANSIENT fields on a region
150 // are REDUCTION fields (1 value). We need to gather these
151 // and output them all at one time. The storage location is a
152 // 'globalVariables' array
153 Ioss::SerializeIO serializeIO_(this);
154
155 size_t num_to_get = field.verify(data_size);
156 Ioss::Field::RoleType role = field.get_role();
157 if ((role == Ioss::Field::TRANSIENT || role == Ioss::Field::REDUCTION) && num_to_get == 1) {
158 Ioss::Field::BasicType ioss_type = field.get_type();
159 std::string field_name = field.get_name();
160 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
161
162 if (ioss_type == Ioss::Field::COMPLEX) {
163 comp_count *= 2;
164 }
165
166 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
167 this->catExoMesh->CreateGlobalVariable(field_name, comp_count, static_cast<double *>(data));
168 }
169 else if (ioss_type == Ioss::Field::INTEGER) {
170 this->catExoMesh->CreateGlobalVariable(field_name, comp_count, static_cast<int *>(data));
171 }
172 else if (ioss_type == Ioss::Field::INT64) {
173 this->catExoMesh->CreateGlobalVariable(field_name, comp_count,
174 static_cast<int64_t *>(data));
175 }
176 }
177 else if (num_to_get != 1) {
178 // There should have been a warning/error message printed to the
179 // log file earlier for this, so we won't print anything else
180 // here since it would be printed for each and every timestep....
181 ;
182 }
183 else {
184 std::ostringstream errmsg;
185 errmsg << "The variable named '" << field.get_name()
186 << "' is of the wrong type. A region variable must be of type"
187 << " TRANSIENT or REDUCTION.\n"
188 << "This is probably an internal error; please notify [email protected]";
189 IOSS_ERROR(errmsg);
190 }
191 return num_to_get;
192 }
193
194 int64_t DatabaseIO::put_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
195 void *data, size_t data_size) const
196 {
197 Ioss::SerializeIO serializeIO_(this);
198 size_t num_to_get = field.verify(data_size);
199
200 if (num_to_get > 0) {
201 Ioss::Field::RoleType role = field.get_role();
202
203 if (role == Ioss::Field::MESH) {
204 if (field.get_name() == "mesh_model_coordinates") {
205 this->catExoMesh->InitializeGlobalPoints(num_to_get,
206 nb->get_property("component_degree").get_int(),
207 static_cast<double *>(data));
208 }
209 else if (field.get_name() == "ids") {
210 // The ids coming in are the global ids; their position is the
211 // local id -1 (That is, data[0] contains the global id of local
212 // node 1)
213
214 // Another 'const-cast' since we are modifying the database just
215 // for efficiency; which the client does not see...
216 auto *new_this = const_cast<DatabaseIO *>(this);
217 /*64 bit should be okay*/
218 new_this->handle_node_ids(data, num_to_get);
219 }
220 else if (field.get_name() == "connectivity") {
221 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
222 }
223 else {
224 return field_warning(nb, field, "mesh output");
225 }
226 }
227 else if (role == Ioss::Field::TRANSIENT) {
228 Ioss::Field::BasicType ioss_type = field.get_type();
229 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
230 std::string field_name = field.get_name();
231
232 if (ioss_type == Ioss::Field::COMPLEX) {
233 comp_count *= 2;
234 }
235
236 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
237 this->catExoMesh->CreateNodalVariable(field_name, comp_count,
238 static_cast<double *>(data));
239 }
240 else if (ioss_type == Ioss::Field::INTEGER) {
241 this->catExoMesh->CreateNodalVariable(field_name, comp_count, static_cast<int *>(data));
242 }
243 else if (ioss_type == Ioss::Field::INT64) {
244 this->catExoMesh->CreateNodalVariable(field_name, comp_count,
245 static_cast<int64_t *>(data));
246 }
247 }
248 else if (role == Ioss::Field::REDUCTION) {
249 }
250 }
251 return num_to_get;
252 }
253
254 int64_t DatabaseIO::put_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
255 void *data, size_t data_size) const
256 {
257 Ioss::SerializeIO serializeIO_(this);
258
259 size_t num_to_get = field.verify(data_size);
260 if (num_to_get > 0) {
261
262 // Get the element block id and element count
263
264 int64_t element_count = eb->entity_count();
265 Ioss::Field::RoleType role = field.get_role();
266
267 if (role == Ioss::Field::MESH) {
268 // Handle the MESH fields required for an ExodusII file model.
269 // (The 'genesis' portion)
270 if (field.get_name() == "connectivity") {
271 if (element_count > 0) {
272 // Map element connectivity from global node id to local node id.
273 // Do it in 'data' ...
274 int element_nodes = eb->topology()->number_nodes();
275 assert(field.transformed_storage()->component_count() == element_nodes);
276 nodeMap.reverse_map_data(data, field, num_to_get * element_nodes);
277 Ioss::Field::BasicType ioss_type = field.get_type();
278 int64_t eb_offset = eb->get_offset();
279 int id = get_id(eb, &ids_);
280
281 if (ioss_type == Ioss::Field::INTEGER) {
282 this->catExoMesh->CreateElementBlock(
283 eb->name().c_str(), id, eb->get_property("topology_type").get_string(),
284 element_nodes, num_to_get, &this->elemMap.map()[eb_offset + 1],
285 static_cast<int *>(data));
286 }
287 else if (ioss_type == Ioss::Field::INT64) {
288 this->catExoMesh->CreateElementBlock(
289 eb->name().c_str(), id, eb->get_property("topology_type").get_string(),
290 element_nodes, num_to_get, &this->elemMap.map()[eb_offset + 1],
291 static_cast<int64_t *>(data));
292 }
293 }
294 }
295 else if (field.get_name() == "ids") {
296 // Another 'const-cast' since we are modifying the database just
297 // for efficiency; which the client does not see...
298 auto *new_this = const_cast<DatabaseIO *>(this);
299 new_this->handle_element_ids(eb, data, num_to_get);
300 }
301 else if (field.get_name() == "skin") {
302 // Not applicable to viz output.
303 }
304 else {
305 Ioss::WarnOut() << " ElementBlock " << eb->name() << ". Unknown field "
306 << field.get_name();
307 num_to_get = 0;
308 }
309 }
310 else if (role == Ioss::Field::ATTRIBUTE) {
311 /* TODO */
312 }
313 else if (role == Ioss::Field::TRANSIENT) {
314 Ioss::Field::BasicType ioss_type = field.get_type();
315 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
316 int bid = get_id(eb, &ids_);
317 std::string field_name = field.get_name();
318
319 if (ioss_type == Ioss::Field::COMPLEX) {
320 comp_count *= 2;
321 }
322
323 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
324 this->catExoMesh->CreateElementVariable(field_name, comp_count, bid,
325 static_cast<double *>(data));
326 }
327 else if (ioss_type == Ioss::Field::INTEGER) {
328 this->catExoMesh->CreateElementVariable(field_name, comp_count, bid,
329 static_cast<int *>(data));
330 }
331 else if (ioss_type == Ioss::Field::INT64) {
332 this->catExoMesh->CreateElementVariable(field_name, comp_count, bid,
333 static_cast<int64_t *>(data));
334 }
335 }
336 else if (role == Ioss::Field::REDUCTION) {
337 }
338 }
339 return num_to_get;
340 }
341
342 void DatabaseIO::write_meta_data()
343 {
344 Ioss::Region *region = get_region();
345
346 // Node Blocks --
347 {
348 const Ioss::NodeBlockContainer &node_blocks = region->get_node_blocks();
349 assert(node_blocks.size() == 1);
350 nodeCount = node_blocks[0]->entity_count();
351 }
352
353 // NodeSets ...
354 {
355 const Ioss::NodeSetContainer &nodesets = region->get_nodesets();
356 Ioss::NodeSetContainer::const_iterator I;
357 for (I = nodesets.begin(); I != nodesets.end(); ++I) {
358 set_id(*I, &ids_);
359 }
360 }
361
362 // SideSets ...
363 {
364 const Ioss::SideSetContainer &ssets = region->get_sidesets();
365 Ioss::SideSetContainer::const_iterator I;
366
367 for (I = ssets.begin(); I != ssets.end(); ++I) {
368 set_id(*I, &ids_);
369 }
370 }
371
372 // Element Blocks --
373 {
374 const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks();
375 Ioss::ElementBlockContainer::const_iterator I;
376 // Set ids of all entities that have "id" property...
377 for (I = element_blocks.begin(); I != element_blocks.end(); ++I) {
378 set_id(*I, &ids_);
379 }
380
381 elementBlockCount = 0;
382 elementCount = 0;
383 for (I = element_blocks.begin(); I != element_blocks.end(); ++I) {
384 elementBlockCount++;
385 elementCount += (*I)->entity_count();
386 }
387 }
388 }
389
390 int64_t DatabaseIO::handle_node_ids(void *ids, int64_t num_to_get)
391 {
392 // std::cerr << "DatabaseIO::handle_node_ids executing\n";
393 /*!
394 * There are two modes we need to support in this routine:
395 * 1. Initial definition of node map (local->global) and
396 * reverseNodeMap (global->local).
397 * 2. Redefinition of node map via 'reordering' of the original
398 * map when the nodes on this processor are the same, but their
399 * order is changed (or count because of ghosting)
400 *
401 * So, there will be two maps the 'nodeMap' map is a 'direct lookup'
402 * map which maps current local position to global id and the
403 * 'reverseNodeMap' is an associative lookup which maps the
404 * global id to 'original local'. There is also a
405 * 'reorderNodeMap' which is direct lookup and maps current local
406 * position to original local.
407
408 * The ids coming in are the global ids; their position is the
409 * "local id-1" (That is, data[0] contains the global id of local
410 * node 1 in this node block).
411 *
412 * int local_position = reverseNodeMap[NodeMap[i+1]]
413 * (the nodeMap and reverseNodeMap are 1-based)
414 *
415 * To determine which map to update on a call to this function, we
416 * use the following hueristics:
417 * -- If the database state is 'STATE_MODEL:', then update the
418 * 'reverseNodeMap' and 'nodeMap'
419 *
420 * -- If the database state is not STATE_MODEL, then leave the
421 * 'reverseNodeMap' and 'nodeMap' alone since they correspond to the
422 * information already written to the database. [May want to add a
423 * STATE_REDEFINE_MODEL]
424 *
425 * -- In both cases, update the reorderNodeMap
426 *
427 * NOTE: The mapping is done on TRANSIENT fields only; MODEL fields
428 * should be in the original order...
429 */
430 assert(num_to_get == nodeCount);
431
432 nodeMap.set_size(nodeCount);
433
434 bool in_define = (dbState == Ioss::STATE_MODEL) || (dbState == Ioss::STATE_DEFINE_MODEL);
435 if (int_byte_size_api() == 4) {
436 nodeMap.set_map(static_cast<int *>(ids), num_to_get, 0, in_define);
437 }
438 else {
439 nodeMap.set_map(static_cast<int64_t *>(ids), num_to_get, 0, in_define);
440 }
441
442 if (in_define) {
443 // Only a single nodeblock and all set
444 assert(get_region()->get_property("node_block_count").get_int() == 1);
445 }
446 return num_to_get;
447 }
448
449 size_t handle_block_ids(const Ioss::EntityBlock *eb, Ioss::State db_state, Ioss::Map &entity_map,
450 void *ids, size_t int_byte_size, size_t num_to_get,
451 /*int file_pointer,*/ int /*my_processor*/)
452 {
453 /*!
454 * NOTE: "element" is generic for "element", "face", or "edge"
455 *
456 * There are two modes we need to support in this routine:
457 * 1. Initial definition of element map (local->global) and
458 * reverseElementMap (global->local).
459 * 2. Redefinition of element map via 'reordering' of the original
460 * map when the elements on this processor are the same, but their
461 * order is changed.
462 *
463 * So, there will be two maps the 'elementMap' map is a 'direct lookup'
464 * map which maps current local position to global id and the
465 * 'reverseElementMap' is an associative lookup which maps the
466 * global id to 'original local'. There is also a
467 * 'reorderElementMap' which is direct lookup and maps current local
468 * position to original local.
469
470 * The ids coming in are the global ids; their position is the
471 * local id -1 (That is, data[0] contains the global id of local
472 * element 1 in this element block). The 'model-local' id is
473 * given by eb_offset + 1 + position:
474 *
475 * int local_position = reverseElementMap[ElementMap[i+1]]
476 * (the elementMap and reverseElementMap are 1-based)
477 *
478 * But, this assumes 1..numel elements are being output at the same
479 * time; we are actually outputting a blocks worth of elements at a
480 * time, so we need to consider the block offsets.
481 * So... local-in-block position 'i' is index 'eb_offset+i' in
482 * 'elementMap' and the 'local_position' within the element
483 * blocks data arrays is 'local_position-eb_offset'. With this, the
484 * position within the data array of this element block is:
485 *
486 * int eb_position =
487 * reverseElementMap[elementMap[eb_offset+i+1]]-eb_offset-1
488 *
489 * To determine which map to update on a call to this function, we
490 * use the following hueristics:
491 * -- If the database state is 'Ioss::STATE_MODEL:', then update the
492 * 'reverseElementMap'.
493 * -- If the database state is not Ioss::STATE_MODEL, then leave
494 * the 'reverseElementMap' alone since it corresponds to the
495 * information already written to the database. [May want to add
496 * a Ioss::STATE_REDEFINE_MODEL]
497 * -- Always update elementMap to match the passed in 'ids'
498 * array.
499 *
500 * NOTE: the maps are built an element block at a time...
501 * NOTE: The mapping is done on TRANSIENT fields only; MODEL fields
502 * should be in the original order...
503 */
504
505 // Overwrite this portion of the 'elementMap', but keep other
506 // parts as they were. We are adding elements starting at position
507 // 'eb_offset+offset' and ending at
508 // 'eb_offset+offset+num_to_get'. If the entire block is being
509 // processed, this reduces to the range 'eb_offset..eb_offset+my_element_count'
510
511 int64_t eb_offset = eb->get_offset();
512
513 bool in_define = (db_state == Ioss::STATE_MODEL) || (db_state == Ioss::STATE_DEFINE_MODEL);
514 if (int_byte_size == 4) {
515 entity_map.set_map(static_cast<int *>(ids), num_to_get, eb_offset, in_define);
516 }
517 else {
518 entity_map.set_map(static_cast<int64_t *>(ids), num_to_get, eb_offset, in_define);
519 }
520 return num_to_get;
521 }
522
523 int64_t DatabaseIO::handle_element_ids(const Ioss::ElementBlock *eb, void *ids, size_t num_to_get)
524 {
525 elemMap.set_size(elementCount);
526 return handle_block_ids(eb, dbState, elemMap, ids, int_byte_size_api(), num_to_get,
527 /*get_file_pointer(),*/ myProcessor);
528 }
529
530 const Ioss::Map &DatabaseIO::get_node_map() const
531 {
532 // Allocate space for node number map and read it in...
533 // Can be called multiple times, allocate 1 time only
534 if (nodeMap.map().empty()) {
535 nodeMap.set_size(nodeCount);
536
537 // Output database; nodeMap not set yet... Build a default map.
538 for (int64_t i = 1; i < nodeCount + 1; i++) {
539 nodeMap.map()[i] = i;
540 }
541 }
542 return nodeMap;
543 }
544
545 // Not used...
546 const Ioss::Map &DatabaseIO::get_element_map() const
547 {
548 // std::cercercerrn new nathan Iovs DatabaseIO::get_element_map\n";
549 // Allocate space for element number map and read it in...
550 // Can be called multiple times, allocate 1 time only
551 if (elemMap.map().empty()) {
552 elemMap.set_size(elementCount);
553
554 // Output database; elementMap not set yet... Build a default map.
555 for (int64_t i = 1; i < elementCount + 1; i++) {
556 elemMap.map()[i] = i;
557 }
558 }
559 return elemMap;
560 }
561
562 int field_warning(const Ioss::GroupingEntity *ge, const Ioss::Field &field,
563 const std::string &inout)
564 {
565 Ioss::WarnOut() << ge->type() << " '" << ge->name() << "'. Unknown " << inout << " field '"
566 << field.get_name() << "'";
567 return -4;
568 }
569
570 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeBlock * /*eb*/, const Ioss::Field &field,
571 void * /*data*/, size_t data_size) const
572 {
573 return get_num_to_get(field, data_size);
574 }
575 int64_t DatabaseIO::put_field_internal(const Ioss::FaceBlock * /*fb*/, const Ioss::Field &field,
576 void * /*data*/, size_t data_size) const
577 {
578 return get_num_to_get(field, data_size);
579 }
580 int64_t DatabaseIO::put_field_internal(const Ioss::SideBlock * /*sb*/, const Ioss::Field &field,
581 void * /*data*/, size_t data_size) const
582 {
583 return get_num_to_get(field, data_size);
584 }
585 int64_t DatabaseIO::put_field_internal(const Ioss::NodeSet * /*ns*/, const Ioss::Field &field,
586 void * /*data*/, size_t data_size) const
587 {
588 return get_num_to_get(field, data_size);
589 }
590 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeSet * /*es*/, const Ioss::Field &field,
591 void * /*data*/, size_t data_size) const
592 {
593 return get_num_to_get(field, data_size);
594 }
595 int64_t DatabaseIO::put_field_internal(const Ioss::FaceSet * /*fs*/, const Ioss::Field &field,
596 void * /*data*/, size_t data_size) const
597 {
598 return get_num_to_get(field, data_size);
599 }
600 int64_t DatabaseIO::put_field_internal(const Ioss::ElementSet * /*es*/, const Ioss::Field &field,
601 void * /*data*/, size_t data_size) const
602 {
603 return get_num_to_get(field, data_size);
604 }
605 int64_t DatabaseIO::put_field_internal(const Ioss::SideSet * /*ss*/, const Ioss::Field &field,
606 void * /*data*/, size_t data_size) const
607 {
608 return get_num_to_get(field, data_size);
609 }
610 int64_t DatabaseIO::put_field_internal(const Ioss::CommSet * /*cs*/, const Ioss::Field &field,
611 void * /*data*/, size_t data_size) const
612 {
613 return get_num_to_get(field, data_size);
614 }
615 int64_t DatabaseIO::put_field_internal(const Ioss::StructuredBlock * /*sb*/,
616 const Ioss::Field &field, void * /*data*/,
617 size_t data_size) const
618 {
619 return get_num_to_get(field, data_size);
620 }
621 int64_t DatabaseIO::put_field_internal(const Ioss::Assembly * /*as*/, const Ioss::Field &field,
622 void * /*data*/, size_t data_size) const
623 {
624 return get_num_to_get(field, data_size);
625 }
626 int64_t DatabaseIO::put_field_internal(const Ioss::Blob * /*bl*/, const Ioss::Field &field,
627 void * /*data*/, size_t data_size) const
628 {
629 return get_num_to_get(field, data_size);
630 }
631 } // namespace Iovs_exodus
632
633 namespace {
634
635 size_t get_num_to_get(const Ioss::Field &field, size_t data_size)
636 {
637 size_t num_to_get = field.verify(data_size);
638 return num_to_get;
639 }
640
641 int64_t get_id(const Ioss::GroupingEntity *entity, Iovs_exodus::EntityIdSet *idset)
642 {
643 // Sierra uses names to refer to grouping entities; however,
644 // exodusII requires integer ids. When reading an exodusII file,
645 // the DatabaseIO creates a name by concatenating the entity
646 // type (e.g., 'block') and the id separated by an underscore. For
647 // example, an exodusII element block with an id of 100 would be
648 // encoded into "block_100"
649
650 // This routine tries to determine the id of the entity using 3
651 // approaches:
652 //
653 // 1. If the entity contains a property named 'id', this is used.
654 // The DatabaseIO actually stores the id in the "id" property;
655 // however, other grouping entity creators are not required to do
656 // this so the property is not guaranteed to exist.
657 //
658 // 2.If property does not exist, it tries to decode the entity name
659 // based on the above encoding. Again, it is not required that the
660 // name follow this convention so success is not guaranteed.
661 //
662 // 3. If all other schemes fail, the routine picks an id for the entity
663 // and returns it. It also stores this id in the "id" property so an
664 // entity will always return the same id for multiple calls.
665 // Note that this violates the 'const'ness of the entity so we use
666 // a const-cast.
667
668 // Avoid a few string constructors/destructors
669 static std::string prop_name("name");
670 static std::string id_prop("id");
671
672 int64_t id = 1;
673
674 if (entity->property_exists(id_prop)) {
675 id = entity->get_property(id_prop).get_int();
676 return id;
677 }
678
679 // Try to decode an id from the name.
680 std::string name_string = entity->get_property(prop_name).get_string();
681 id = extract_id(name_string);
682 if (id <= 0) {
683 id = 1;
684 }
685
686 // At this point, we either have an id equal to '1' or we have an id
687 // extracted from the entities name. Increment it until it is
688 // unique...
689 int type = static_cast<int>(entity->type());
690 while (idset->find(std::make_pair(type, id)) != idset->end()) {
691 ++id;
692 }
693
694 // 'id' is a unique id for this entity type...
695 idset->insert(std::make_pair(type, id));
696 auto *new_entity = const_cast<Ioss::GroupingEntity *>(entity);
697 new_entity->property_add(Ioss::Property(id_prop, id));
698 return id;
699 }
700
701 bool set_id(const Ioss::GroupingEntity *entity, Iovs_exodus::EntityIdSet *idset)
702 {
703 // See description of 'get_id' function. This function just primes
704 // the idset with existing ids so that when we start generating ids,
705 // we don't overwrite an existing one.
706
707 // Avoid a few string constructors/destructors
708 static std::string prop_name("name");
709 static std::string id_prop("id");
710
711 bool succeed = false;
712 if (entity->property_exists(id_prop)) {
713 int64_t id = entity->get_property(id_prop).get_int();
714
715 // See whether it already exists...
716 int type = static_cast<int>(entity->type());
717 succeed = idset->insert(std::make_pair(type, id)).second;
718 if (!succeed) {
719 // Need to remove the property so it doesn't cause problems
720 // later...
721 auto *new_entity = const_cast<Ioss::GroupingEntity *>(entity);
722 new_entity->property_erase(id_prop);
723 assert(!entity->property_exists(id_prop));
724 }
725 }
726 return succeed;
727 }
728
729 int64_t extract_id(const std::string &name_id)
730 {
731 auto tokens = Ioss::tokenize(name_id, "_");
732 if (tokens.size() == 1) {
733 return 0;
734 }
735
736 // Check whether last token is an integer...
737 std::string str_id = tokens[tokens.size() - 1];
738 size_t len = str_id.length();
739 bool is_int = true;
740 for (size_t i = 0; i < len; i++) {
741 if (str_id[i] < '0' || str_id[i] > '9') {
742 is_int = false;
743 break;
744 }
745 }
746 if (is_int) {
747 return std::atoi(str_id.c_str());
748 }
749
750 return 0;
751 }
752
753 } // end anonymous namespace