Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_Region.C

raw
1 // Copyright(C) 1999-2025 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_Assembly.h"
8 #include "Ioss_Blob.h"
9 #include "Ioss_CodeTypes.h"
10 #include "Ioss_CommSet.h"
11 #include "Ioss_CoordinateFrame.h"
12 #include "Ioss_DBUsage.h"
13 #include "Ioss_DatabaseIO.h"
14 #include "Ioss_DynamicTopologyFileControl.h"
15 #include "Ioss_DynamicTopologyStateLocator.h"
16 #include "Ioss_EdgeBlock.h"
17 #include "Ioss_EdgeSet.h"
18 #include "Ioss_ElementBlock.h"
19 #include "Ioss_ElementSet.h"
20 #include "Ioss_EntityBlock.h"
21 #include "Ioss_EntityType.h"
22 #include "Ioss_FaceBlock.h"
23 #include "Ioss_FaceSet.h"
24 #include "Ioss_Field.h"
25 #include "Ioss_FileInfo.h"
26 #include "Ioss_GroupingEntity.h"
27 #include "Ioss_IOFactory.h"
28 #include "Ioss_NodeBlock.h"
29 #include "Ioss_NodeSet.h"
30 #include "Ioss_Property.h"
31 #include "Ioss_PropertyManager.h"
32 #include "Ioss_Region.h"
33 #include "Ioss_SideBlock.h"
34 #include "Ioss_SideSet.h"
35 #include "Ioss_SmartAssert.h"
36 #include "Ioss_Sort.h"
37 #include "Ioss_State.h"
38 #include "Ioss_StructuredBlock.h"
39 #include <array>
40 #include <climits>
41 #include <cstddef>
42 #include <fmt/core.h>
43 #include <fmt/format.h>
44 #include <fmt/ostream.h>
45 #include <map>
46 #include <string>
47 #include <tuple>
48 #include <vector>
49
50 #include <assert.h>
51 #include <iomanip>
52 #include <iostream>
53 #include <sstream>
54
55 #include "Ioss_MeshType.h"
56 #include "Ioss_ParallelUtils.h"
57
58 namespace {
59 std::string id_str() { return {"id"}; }
60 std::string db_name_str() { return {"db_name"}; }
61 std::string orig_topo_str() { return {"original_topology_type"}; }
62 std::string orig_block_order() { return {"original_block_order"}; }
63
64 template <typename T>
65 Ioss::GroupingEntity *get_entity_internal(int64_t id, const std::vector<T> &entities)
66 {
67 for (auto &ent : entities) {
68 if (ent->property_exists(id_str())) {
69 if (id == ent->get_property(id_str()).get_int()) {
70 return ent;
71 }
72 }
73 }
74 return nullptr;
75 }
76
77 template <typename T>
78 size_t internal_get_variable_count(const std::vector<T> &entities, Ioss::Field::RoleType role)
79 {
80 Ioss::NameList names;
81 for (auto &ent : entities) {
82 ent->field_describe(role, &names);
83 }
84 Ioss::Utils::uniquify(names);
85 return names.size();
86 }
87
88 template <typename T> size_t get_variable_count(const std::vector<T> &entities)
89 {
90 return internal_get_variable_count(entities, Ioss::Field::TRANSIENT);
91 }
92
93 template <typename T> size_t get_reduction_variable_count(const std::vector<T> &entities)
94 {
95 return internal_get_variable_count(entities, Ioss::Field::REDUCTION);
96 }
97
98 template <typename T> int64_t get_entity_count(const std::vector<T> &entities)
99 {
100 int64_t count = 0;
101 for (auto &ent : entities) {
102 count += ent->entity_count();
103 }
104 return count;
105 }
106
107 void update_database(Ioss::DatabaseIO *db, Ioss::GroupingEntity *entity)
108 {
109 entity->reset_database(db);
110 }
111
112 void update_database(const Ioss::Region *region, Ioss::GroupingEntity *entity)
113 {
114 update_database(region->get_database(), entity);
115 }
116
117 void update_database(Ioss::DatabaseIO *db, Ioss::SideSet *sset)
118 {
119 sset->reset_database(db);
120 const auto &blocks = sset->get_side_blocks();
121 for (const auto &block : blocks) {
122 block->reset_database(db);
123 }
124 }
125
126 void update_database(const Ioss::Region *region, Ioss::SideSet *sset)
127 {
128 update_database(region->get_database(), sset);
129 }
130
131 constexpr unsigned numberOfBits(unsigned x) { return x < 2 ? x : 1 + numberOfBits(x >> 1); }
132
133 size_t compute_hash(Ioss::GroupingEntity *entity, size_t which)
134 {
135 // Can add more properties and or fields later. For now just do
136 // name and optional id.
137 size_t hash = entity->hash();
138 if (entity->property_exists(id_str())) {
139 hash += which * entity->get_property(id_str()).get_int();
140 }
141 return hash;
142 }
143
144 template <typename T>
145 void compute_hashes(const std::vector<T> &entities,
146 std::array<size_t, Ioss::entityTypeCount> &hashes, Ioss::EntityType type)
147 {
148 auto index = numberOfBits(type) - 1;
149 SMART_ASSERT(index < hashes.size())(type)(index)(hashes.size());
150
151 size_t which = 1;
152 for (const auto &entity : entities) {
153 hashes[index] += compute_hash(entity, which++);
154 }
155 }
156
157 bool check_hashes(const std::vector<size_t> &min_hash, const std::vector<size_t> &max_hash,
158 Ioss::EntityType type)
159 {
160 auto index = numberOfBits(type) - 1;
161 SMART_ASSERT(index < min_hash.size())(type)(index)(min_hash.size());
162 return (min_hash[index] == max_hash[index]);
163 }
164
165 template <typename T>
166 void report_inconsistency(const std::vector<T> &entities, Ioss::ParallelUtils &util)
167 {
168 // Know that there is some mismatch in name or (optional)id. Let user know where...
169 std::vector<size_t> hashes;
170
171 size_t which = 1;
172 hashes.reserve(entities.size());
173 for (const auto &entity : entities) {
174 hashes.push_back(compute_hash(entity, which++));
175 }
176
177 std::ostringstream errmsg;
178 fmt::print(errmsg, "IOSS: ERROR: Parallel Consistency Error.\n\t\t");
179
180 auto min_hash = hashes;
181 auto max_hash = hashes;
182 // Now find mismatched location...
183 util.global_array_minmax(min_hash, Ioss::ParallelUtils::DO_MIN);
184 util.global_array_minmax(max_hash, Ioss::ParallelUtils::DO_MAX);
185
186 if (util.parallel_rank() == 0) {
187 int count = 0;
188 for (size_t i = 0; i < hashes.size(); i++) {
189 if (min_hash[i] != max_hash[i]) {
190 auto ge = entities[i];
191 if (count == 0) {
192 fmt::print(errmsg, "{}(s) ", ge->type_string());
193 }
194 else {
195 fmt::print(errmsg, ", ");
196 }
197 fmt::print(errmsg, "'{}'", ge->name());
198 count++;
199 }
200 }
201 fmt::print(errmsg,
202 " {} not consistently defined on all processors.\n\t\t"
203 "Check that name and id matches across processors.\n",
204 (count == 1 ? "is" : "are"));
205 IOSS_ERROR(errmsg);
206 }
207 }
208
209 bool check_parallel_consistency(const Ioss::Region &region)
210 {
211 if (!region.get_database()->is_parallel()) {
212 return true;
213 }
214
215 // Want a good approximate test that the grouping entity lists on
216 // all processor contain the same entities in the same order.
217 // We will say an entity is the same if the name and optional id match.
218 //
219 // Hash the name and multiply it by position in list and add id+1.
220 // Do this for each type separately... Then verify that they
221 // match on all processors...
222 std::array<size_t, Ioss::entityTypeCount> hashes{};
223
224 compute_hashes(region.get_node_blocks(), hashes, Ioss::NODEBLOCK);
225 compute_hashes(region.get_edge_blocks(), hashes, Ioss::EDGEBLOCK);
226 compute_hashes(region.get_face_blocks(), hashes, Ioss::FACEBLOCK);
227 compute_hashes(region.get_element_blocks(), hashes, Ioss::ELEMENTBLOCK);
228 compute_hashes(region.get_nodesets(), hashes, Ioss::NODESET);
229 compute_hashes(region.get_edgesets(), hashes, Ioss::EDGESET);
230 compute_hashes(region.get_facesets(), hashes, Ioss::FACESET);
231 compute_hashes(region.get_elementsets(), hashes, Ioss::ELEMENTSET);
232 compute_hashes(region.get_sidesets(), hashes, Ioss::SIDESET);
233 compute_hashes(region.get_commsets(), hashes, Ioss::COMMSET);
234 compute_hashes(region.get_structured_blocks(), hashes, Ioss::STRUCTUREDBLOCK);
235 compute_hashes(region.get_assemblies(), hashes, Ioss::ASSEMBLY);
236 compute_hashes(region.get_blobs(), hashes, Ioss::BLOB);
237
238 auto util = region.get_database()->util();
239 std::vector<size_t> min_hash(hashes.begin(), hashes.end());
240 std::vector<size_t> max_hash(hashes.begin(), hashes.end());
241 util.global_array_minmax(min_hash, Ioss::ParallelUtils::DO_MIN);
242 util.global_array_minmax(max_hash, Ioss::ParallelUtils::DO_MAX);
243
244 bool differ = false;
245 if (!check_hashes(min_hash, max_hash, Ioss::NODEBLOCK)) {
246 report_inconsistency(region.get_node_blocks(), util);
247 differ = true;
248 }
249 if (!check_hashes(min_hash, max_hash, Ioss::EDGEBLOCK)) {
250 report_inconsistency(region.get_edge_blocks(), util);
251 differ = true;
252 }
253 if (!check_hashes(min_hash, max_hash, Ioss::FACEBLOCK)) {
254 report_inconsistency(region.get_face_blocks(), util);
255 differ = true;
256 }
257 if (!check_hashes(min_hash, max_hash, Ioss::ELEMENTBLOCK)) {
258 report_inconsistency(region.get_element_blocks(), util);
259 differ = true;
260 }
261 if (!check_hashes(min_hash, max_hash, Ioss::NODESET)) {
262 report_inconsistency(region.get_nodesets(), util);
263 differ = true;
264 }
265 if (!check_hashes(min_hash, max_hash, Ioss::EDGESET)) {
266 report_inconsistency(region.get_edgesets(), util);
267 differ = true;
268 }
269 if (!check_hashes(min_hash, max_hash, Ioss::FACESET)) {
270 report_inconsistency(region.get_facesets(), util);
271 differ = true;
272 }
273 if (!check_hashes(min_hash, max_hash, Ioss::ELEMENTSET)) {
274 report_inconsistency(region.get_elementsets(), util);
275 differ = true;
276 }
277 if (!check_hashes(min_hash, max_hash, Ioss::SIDESET)) {
278 report_inconsistency(region.get_sidesets(), util);
279 differ = true;
280 }
281 if (!check_hashes(min_hash, max_hash, Ioss::COMMSET)) {
282 report_inconsistency(region.get_commsets(), util);
283 differ = true;
284 }
285 if (!check_hashes(min_hash, max_hash, Ioss::STRUCTUREDBLOCK)) {
286 report_inconsistency(region.get_structured_blocks(), util);
287 differ = true;
288 }
289 if (!check_hashes(min_hash, max_hash, Ioss::ASSEMBLY)) {
290 report_inconsistency(region.get_assemblies(), util);
291 differ = true;
292 }
293 if (!check_hashes(min_hash, max_hash, Ioss::BLOB)) {
294 report_inconsistency(region.get_blobs(), util);
295 differ = true;
296 }
297 return !differ;
298 }
299
300 bool is_input_or_appending_output(const Ioss::DatabaseIO *iodatabase)
301 {
302 return iodatabase->is_input() || iodatabase->open_create_behavior() == Ioss::DB_APPEND ||
303 iodatabase->open_create_behavior() == Ioss::DB_MODIFY;
304 }
305
306 template <typename T>
307 void internal_erase_fields(const std::vector<T> &entities, Ioss::Field::RoleType role)
308 {
309 for (const auto &entity : entities) {
310 entity->field_erase(role);
311 }
312 }
313
314 } // namespace
315
316 namespace Ioss {
317
318 /** \brief Constructor reads in all metadata from disk.
319 *
320 * This constructor connects this region to the database, opens the
321 * underlying file, reads all the metadata in the file into Region
322 * and its subentities, and closes the underlying file. Region properties,
323 * such as spatial_dimension, element_block_count, element_count, etc, are
324 * also added to the Region's property manager.
325 *
326 * \param[in] iodatabase The name of the database associated with the Region.
327 * \param[in] my_name The name of the Region.
328 *
329 */
330 Region::Region(DatabaseIO *iodatabase, const std::string &my_name)
331 : GroupingEntity(iodatabase, my_name, 1)
332 {
333 SMART_ASSERT(iodatabase != nullptr);
334 iodatabase->set_region(this);
335
336 if (iodatabase->usage() != Ioss::WRITE_HEARTBEAT &&
337 (is_input_or_appending_output(iodatabase))) {
338 Region::begin_mode(STATE_DEFINE_MODEL);
339 iodatabase->read_meta_data();
340 Region::end_mode(STATE_DEFINE_MODEL);
341 if (iodatabase->open_create_behavior() != Ioss::DB_APPEND &&
342 iodatabase->open_create_behavior() != Ioss::DB_MODIFY) {
343 modelDefined = true;
344 transientDefined = true;
345 Region::begin_mode(STATE_READONLY);
346 }
347 }
348
349 properties.add(Property(this, "spatial_dimension", Property::INTEGER));
350 properties.add(Property(this, "node_block_count", Property::INTEGER));
351 properties.add(Property(this, "edge_block_count", Property::INTEGER));
352 properties.add(Property(this, "face_block_count", Property::INTEGER));
353 properties.add(Property(this, "element_block_count", Property::INTEGER));
354 properties.add(Property(this, "structured_block_count", Property::INTEGER));
355 properties.add(Property(this, "assembly_count", Property::INTEGER));
356 properties.add(Property(this, "blob_count", Property::INTEGER));
357 properties.add(Property(this, "side_set_count", Property::INTEGER));
358 properties.add(Property(this, "node_set_count", Property::INTEGER));
359 properties.add(Property(this, "edge_set_count", Property::INTEGER));
360 properties.add(Property(this, "face_set_count", Property::INTEGER));
361 properties.add(Property(this, "element_set_count", Property::INTEGER));
362 properties.add(Property(this, "comm_set_count", Property::INTEGER));
363 properties.add(Property(this, "node_count", Property::INTEGER));
364 properties.add(Property(this, "edge_count", Property::INTEGER));
365 properties.add(Property(this, "face_count", Property::INTEGER));
366 properties.add(Property(this, "element_count", Property::INTEGER));
367 properties.add(Property(this, "coordinate_frame_count", Property::INTEGER));
368 properties.add(Property(this, "state_count", Property::INTEGER));
369 properties.add(Property(this, "current_state", Property::INTEGER));
370 properties.add(Property(this, "database_name", Property::STRING));
371
372 property_add(Property("base_filename", iodatabase->get_property_manager().get_optional(
373 "base_filename", iodatabase->get_filename())));
374 property_add(Property("database_type",
375 iodatabase->get_property_manager().get_optional("database_type", "")));
376 }
377
378 Region::~Region()
379 {
380 // Do anything to the database to make it consistent prior to closing and destructing...
381 get_database()->finalize_database();
382
383 // Region owns all sub-grouping entities it contains...
384 try {
385 IOSS_FUNC_ENTER(m_);
386 reset_region();
387
388 // Region owns the database pointer even though other entities use it.
389 GroupingEntity::really_delete_database();
390
391 if (topologyObserver) {
392 topologyObserver->register_region(nullptr);
393 }
394 }
395 catch (...) {
396 }
397 }
398
399 void Region::reset_region()
400 {
401 for (const auto &nb : nodeBlocks) {
402 delete (nb);
403 }
404 nodeBlocks.clear();
405
406 for (const auto &eb : edgeBlocks) {
407 delete (eb);
408 }
409 edgeBlocks.clear();
410
411 for (const auto &fb : faceBlocks) {
412 delete (fb);
413 }
414 faceBlocks.clear();
415
416 for (const auto &eb : elementBlocks) {
417 delete (eb);
418 }
419 elementBlocks.clear();
420
421 for (const auto &sb : structuredBlocks) {
422 delete (sb);
423 }
424 structuredBlocks.clear();
425
426 for (const auto &ss : sideSets) {
427 delete (ss);
428 }
429 sideSets.clear();
430
431 for (const auto &ns : nodeSets) {
432 delete (ns);
433 }
434 nodeSets.clear();
435
436 for (const auto &es : edgeSets) {
437 delete (es);
438 }
439 edgeSets.clear();
440
441 for (const auto &fs : faceSets) {
442 delete (fs);
443 }
444 faceSets.clear();
445
446 for (const auto &es : elementSets) {
447 delete (es);
448 }
449 elementSets.clear();
450
451 for (const auto &cs : commSets) {
452 delete (cs);
453 }
454 commSets.clear();
455
456 for (const auto &as : assemblies) {
457 delete (as);
458 }
459 assemblies.clear();
460
461 for (const auto &bl : blobs) {
462 delete (bl);
463 }
464 blobs.clear();
465
466 stateTimes.clear();
467
468 currentState = -1;
469 stateCount = 0;
470
471 modelDefined = false;
472 transientDefined = false;
473
474 // Ioex:DatabaseIO::read_communication_metadata() adds comm fields that need to be cleared
475 erase_fields(Field::COMMUNICATION);
476
477 aliases_.clear();
478 }
479
480 void Region::delete_database() { GroupingEntity::really_delete_database(); }
481
482 bool Region::node_major() const { return get_database()->node_major(); }
483
484 MeshType Region::mesh_type() const
485 {
486 if (elementBlocks.empty() && structuredBlocks.empty()) {
487 return MeshType::UNSTRUCTURED;
488 }
489 if (!elementBlocks.empty() && !structuredBlocks.empty()) {
490 return MeshType::HYBRID;
491 }
492 if (!structuredBlocks.empty()) {
493 return MeshType::STRUCTURED;
494 }
495 SMART_ASSERT(!elementBlocks.empty());
496 return MeshType::UNSTRUCTURED;
497 }
498
499 std::string Region::mesh_type_string() const
500 {
501 switch (mesh_type()) {
502 case MeshType::UNKNOWN: return "Unknown";
503 case MeshType::HYBRID: return "Hybrid";
504 case MeshType::STRUCTURED: return "Structured";
505 case MeshType::UNSTRUCTURED: return "Unstructured";
506 }
507 SMART_ASSERT(1 == 0 && "Program Error");
508 return "Invalid";
509 }
510
511 /** \brief Print a summary of entities in the region.
512 *
513 * \param[in,out] strm The output stream to use for printing.
514 * \param[in] do_transient deprecated and ignored
515 */
516 void Region::output_summary(std::ostream &strm, bool /* do_transient */) const
517 {
518 int64_t total_cells = get_entity_count(get_structured_blocks());
519 int64_t total_fs_faces = get_entity_count(get_facesets());
520 int64_t total_ns_nodes = get_entity_count(get_nodesets());
521 int64_t total_es_edges = get_entity_count(get_edgesets());
522 int64_t total_es_elements = get_entity_count(get_elementsets());
523
524 int64_t total_sides = 0;
525 {
526 const Ioss::SideSetContainer &sss = get_sidesets();
527 for (const auto &fs : sss) {
528 total_sides += get_entity_count(fs->get_side_blocks());
529 }
530 }
531
532 int64_t total_nodes = get_property("node_count").get_int();
533 int64_t total_elements = get_property("element_count").get_int();
534 auto max_entity = std::max({total_sides, total_es_elements, total_fs_faces, total_es_edges,
535 total_ns_nodes, total_cells, total_nodes, total_elements});
536
537 int64_t num_ts = get_property("state_count").get_int();
538 auto max_sb = std::max(
539 {get_property("spatial_dimension").get_int(), get_property("node_block_count").get_int(),
540 get_property("edge_block_count").get_int(), get_property("face_block_count").get_int(),
541 get_property("element_block_count").get_int(),
542 get_property("structured_block_count").get_int(), get_property("node_set_count").get_int(),
543 get_property("edge_set_count").get_int(), get_property("face_set_count").get_int(),
544 get_property("element_set_count").get_int(), get_property("side_set_count").get_int(),
545 get_property("assembly_count").get_int(), get_property("blob_count").get_int(), num_ts});
546
547 // Global variables transitioning from TRANSIENT to REDUCTION..
548 size_t num_glo_vars = field_count(Ioss::Field::TRANSIENT);
549 size_t num_nod_vars = get_variable_count(get_node_blocks());
550 size_t num_edg_vars = get_variable_count(get_edge_blocks());
551 size_t num_fac_vars = get_variable_count(get_face_blocks());
552 size_t num_ele_vars = get_variable_count(get_element_blocks());
553 size_t num_str_vars = get_variable_count(get_structured_blocks());
554 size_t num_ns_vars = get_variable_count(get_nodesets());
555 size_t num_es_vars = get_variable_count(get_edgesets());
556 size_t num_fs_vars = get_variable_count(get_facesets());
557 size_t num_els_vars = get_variable_count(get_elementsets());
558 size_t num_asm_vars = get_variable_count(get_assemblies());
559 size_t num_blob_vars = get_variable_count(get_blobs());
560
561 size_t num_glo_red_vars = field_count(Ioss::Field::REDUCTION);
562 size_t num_nod_red_vars = get_reduction_variable_count(get_node_blocks());
563 size_t num_edg_red_vars = get_reduction_variable_count(get_edge_blocks());
564 size_t num_fac_red_vars = get_reduction_variable_count(get_face_blocks());
565 size_t num_ele_red_vars = get_reduction_variable_count(get_element_blocks());
566 size_t num_str_red_vars = get_reduction_variable_count(get_structured_blocks());
567 size_t num_ns_red_vars = get_reduction_variable_count(get_nodesets());
568 size_t num_es_red_vars = get_reduction_variable_count(get_edgesets());
569 size_t num_fs_red_vars = get_reduction_variable_count(get_facesets());
570 size_t num_els_red_vars = get_reduction_variable_count(get_elementsets());
571 size_t num_asm_red_vars = get_reduction_variable_count(get_assemblies());
572 size_t num_blob_red_vars = get_reduction_variable_count(get_blobs());
573
574 size_t num_ss_vars = 0;
575 Ioss::NameList names;
576 const Ioss::SideSetContainer &sss = get_sidesets();
577 for (const auto &ss : sss) {
578 const auto &sbs = ss->get_side_blocks();
579 for (const auto &sb : sbs) {
580 sb->field_describe(Ioss::Field::TRANSIENT, &names);
581 }
582 }
583 Ioss::Utils::uniquify(names);
584 num_ss_vars = names.size();
585
586 auto max_vr = std::max({num_glo_vars, num_nod_vars, num_ele_vars, num_str_vars,
587 num_ns_vars, num_ss_vars, num_edg_vars, num_fac_vars,
588 num_es_vars, num_fs_vars, num_els_vars, num_blob_vars,
589 num_asm_vars, num_glo_red_vars, num_nod_red_vars, num_edg_red_vars,
590 num_fac_red_vars, num_ele_red_vars, num_str_red_vars, num_ns_red_vars,
591 num_es_red_vars, num_fs_red_vars, num_els_red_vars, num_asm_red_vars,
592 num_blob_red_vars});
593 int vr_width = Ioss::Utils::number_width(max_vr, true) + 2;
594 int num_width = Ioss::Utils::number_width(max_entity, true) + 2;
595 int sb_width = Ioss::Utils::number_width(max_sb, true) + 2;
596
597 int change_set_count = get_database()->num_internal_change_set();
598 auto change_set_name = get_internal_change_set_name();
599 if (!change_set_name.empty() && change_set_name != "/") {
600 change_set_name = ",\t[CS: " + change_set_name + "]";
601 }
602 else {
603 change_set_name.clear();
604 }
605
606 // clang-format off
607 fmt::print(
608 strm,
609 "\n Database: {0}{56}\n"
610 " Mesh Type = {1}, {39}. Change Sets = {57}\n"
611 " {38:{24}s}\t {38:{23}s}\t Variables : Transient / Reduction\n"
612 " Spatial dimensions = {2:{24}}\t {38:{23}s}\t Global = {26:{25}}\t{44:{25}}\n"
613 " Node blocks = {7:{24}}\t Nodes = {3:{23}}\t Nodal = {27:{25}}\t{45:{25}}\n"
614 " Edge blocks = {8:{24}}\t Edges = {4:{23}}\t Edge = {33:{25}}\t{46:{25}}\n"
615 " Face blocks = {9:{24}}\t Faces = {5:{23}}\t Face = {34:{25}}\t{47:{25}}\n"
616 " Element blocks = {10:{24}}\t Elements = {6:{23}}\t Element = {28:{25}}\t{48:{25}}\n"
617 " Structured blocks = {11:{24}}\t Cells = {17:{23}}\t Structured = {29:{25}}\t{49:{25}}\n"
618 " Node sets = {12:{24}}\t Node list = {18:{23}}\t Nodeset = {30:{25}}\t{50:{25}}\n"
619 " Edge sets = {13:{24}}\t Edge list = {19:{23}}\t Edgeset = {35:{25}}\t{51:{25}}\n"
620 " Face sets = {14:{24}}\t Face list = {20:{23}}\t Faceset = {36:{25}}\t{52:{25}}\n"
621 " Element sets = {15:{24}}\t Element list = {21:{23}}\t Elementset = {37:{25}}\t{53:{25}}\n"
622 " Element side sets = {16:{24}}\t Element sides = {22:{23}}\t Sideset = {31:{25}}\n"
623 " Assemblies = {40:{24}}\t {38:{23}s}\t Assembly = {41:{25}}\t{54:{25}}\n"
624 " Blobs = {42:{24}}\t {38:{23}s}\t Blob = {43:{25}}\t{55:{25}}\n\n"
625 " Time steps = {32:{24}}\n",
626 get_database()->get_filename(), mesh_type_string(), /* 0, 1 */
627 fmt::group_digits(get_property("spatial_dimension").get_int()),
628 fmt::group_digits(get_property("node_count").get_int()),
629 fmt::group_digits(get_property("edge_count").get_int()),
630 fmt::group_digits(get_property("face_count").get_int()),
631 fmt::group_digits(get_property("element_count").get_int()),
632 fmt::group_digits(get_property("node_block_count").get_int()),
633 fmt::group_digits(get_property("edge_block_count").get_int()),
634 fmt::group_digits(get_property("face_block_count").get_int()),
635 fmt::group_digits(get_property("element_block_count").get_int()), /* 10 */
636 fmt::group_digits(get_property("structured_block_count").get_int()),
637 fmt::group_digits(get_property("node_set_count").get_int()),
638 fmt::group_digits(get_property("edge_set_count").get_int()),
639 fmt::group_digits(get_property("face_set_count").get_int()),
640 fmt::group_digits(get_property("element_set_count").get_int()),
641 fmt::group_digits(get_property("side_set_count").get_int()),
642 fmt::group_digits(total_cells),
643 fmt::group_digits(total_ns_nodes),
644 fmt::group_digits(total_es_edges),
645 fmt::group_digits(total_fs_faces), /* 20 */
646 fmt::group_digits(total_es_elements),
647 fmt::group_digits(total_sides),
648 num_width,
649 sb_width,
650 vr_width,
651 fmt::group_digits(num_glo_vars),
652 fmt::group_digits(num_nod_vars),
653 fmt::group_digits(num_ele_vars),
654 fmt::group_digits(num_str_vars),
655 fmt::group_digits(num_ns_vars), /* 30 */
656 fmt::group_digits(num_ss_vars),
657 fmt::group_digits(num_ts),
658 fmt::group_digits(num_edg_vars),
659 fmt::group_digits(num_fac_vars),
660 fmt::group_digits(num_es_vars),
661 fmt::group_digits(num_fs_vars),
662 fmt::group_digits(num_els_vars),
663 " ",
664 get_database()->get_format(),
665 fmt::group_digits(get_property("assembly_count").get_int()), /* 40 */
666 fmt::group_digits(num_asm_vars),
667 fmt::group_digits(get_property("blob_count").get_int()),
668 fmt::group_digits(num_blob_vars),
669 fmt::group_digits(num_glo_red_vars),
670 fmt::group_digits(num_nod_red_vars),
671 fmt::group_digits(num_edg_red_vars),
672 fmt::group_digits(num_fac_red_vars),
673 fmt::group_digits(num_ele_red_vars),
674 fmt::group_digits(num_str_red_vars),
675 fmt::group_digits(num_ns_red_vars), /* 50 */
676 fmt::group_digits(num_es_red_vars),
677 fmt::group_digits(num_fs_red_vars),
678 fmt::group_digits(num_els_red_vars),
679 fmt::group_digits(num_asm_red_vars),
680 fmt::group_digits(num_blob_red_vars),
681 change_set_name, change_set_count);
682 // clang-format on
683 }
684
685 /** \brief Set the Region and the associated DatabaseIO to the given State.
686 *
687 * All transitions must begin from the 'STATE_CLOSED' state or be to
688 * the 'STATE_CLOSED' state (There are no nested begin/end pairs at
689 * this time.)
690 *
691 * \param[in] new_state The new State to which the Region and DatabaseIO should be set.
692 * \returns True if successful.
693 *
694 */
695 bool Region::begin_mode(State new_state)
696 {
697 bool success = false;
698 {
699 IOSS_FUNC_ENTER(m_);
700 success = begin_mode_nl(new_state);
701 }
702 // Pass the 'begin state' message on to the database so it can do any
703 // cleanup/data checking/manipulations it needs to do.
704 if (success) {
705 DatabaseIO *db = get_database();
706
707 if (new_state == STATE_DEFINE_TRANSIENT && db->usage() == Ioss::WRITE_HISTORY &&
708 !(is_input_or_appending_output(db))) {
709 set_state(STATE_CLOSED);
710 Ioss::Utils::generate_history_mesh(this);
711 set_state(new_state);
712 }
713 success = db->begin(new_state);
714 }
715 return success;
716 }
717
718 bool Region::begin_mode_nl(State new_state)
719 {
720 bool success = false;
721 if (new_state == STATE_CLOSED) {
722 success = set_state(new_state);
723 }
724 else {
725 bool has_output_observer = topologyObserver && !get_database()->is_input();
726
727 if (new_state == STATE_DEFINE_MODEL) {
728 if (has_output_observer && (topologyObserver->get_control_option() ==
729 FileControlOption::CONTROL_AUTO_GROUP_FILE)) {
730 if (!fileGroupsStarted) {
731 int steps = get_property("state_count").get_int();
732 bool force_addition = true;
733 add_output_database_change_set(steps, force_addition);
734
735 fileGroupsStarted = true;
736 }
737 }
738 }
739 else if (new_state == STATE_TRANSIENT) {
740 update_dynamic_topology();
741 }
742 switch (get_state()) {
743 case STATE_CLOSED:
744 // Make sure we can go to the specified state.
745 success = set_state(new_state);
746 break;
747
748 // For the invalid transitions; provide a more meaningful
749 // message in certain cases...
750 case STATE_READONLY: {
751 IOSS_ERROR(fmt::format("Cannot change state of an input (readonly) database in {}",
752 get_database()->get_filename()));
753 }
754
755 default: {
756 IOSS_ERROR(fmt::format("Invalid nesting of begin/end pairs in {}",
757 get_database()->get_filename()));
758 }
759 }
760 }
761 return success;
762 }
763
764 /** \brief Return the Region and the associated DatabaseIO to STATE_CLOSED.
765 *
766 * \param[in] current_state The State to end.
767 * \returns True if successful.
768 *
769 */
770 bool Region::end_mode(State current_state)
771 {
772 {
773 IOSS_FUNC_ENTER(m_);
774 end_mode_nl(current_state);
775 }
776
777 // Pass the 'end state' message on to the database so it can do any
778 // cleanup/data checking/manipulations it needs to do.
779 bool success = get_database()->end(current_state);
780 begin_mode(STATE_CLOSED);
781 return success;
782 }
783
784 bool Region::end_mode_nl(State current_state)
785 {
786 bool success = true;
787 // Check that 'current_state' matches the current state of the
788 // Region (that is, we are leaving the state we are in).
789 if (get_state() != current_state) {
790 IOSS_ERROR(fmt::format("ERROR: Specified end state does not match currently open state\n"
791 " [{}]\n",
792 get_database()->get_filename()));
793 }
794
795 if (current_state == STATE_DEFINE_MODEL) {
796 if (is_input_or_appending_output(get_database())) {
797 auto sortName = [](const Ioss::EntityBlock *b1, const Ioss::EntityBlock *b2) {
798 return (b1->name() < b2->name());
799 };
800 Ioss::sort(structuredBlocks.begin(), structuredBlocks.end(), sortName);
801 }
802 else {
803 // Sort the element blocks based on the idOffset field, followed by
804 // name...
805 auto lessOffset = [](const Ioss::EntityBlock *b1, const Ioss::EntityBlock *b2) {
806 SMART_ASSERT(b1->property_exists(orig_block_order()));
807 SMART_ASSERT(b2->property_exists(orig_block_order()));
808 int64_t b1_orderInt = b1->get_property(orig_block_order()).get_int();
809 int64_t b2_orderInt = b2->get_property(orig_block_order()).get_int();
810 return ((b1_orderInt == b2_orderInt) ? (b1->name() < b2->name())
811 : (b1_orderInt < b2_orderInt));
812 };
813
814 Ioss::sort(elementBlocks.begin(), elementBlocks.end(), lessOffset);
815 Ioss::sort(faceBlocks.begin(), faceBlocks.end(), lessOffset);
816 Ioss::sort(edgeBlocks.begin(), edgeBlocks.end(), lessOffset);
817
818 // Now update the block offsets based on this new order...
819 {
820 int64_t offset = 0;
821 for (auto &eb : elementBlocks) {
822 eb->set_offset(offset);
823 offset += eb->entity_count();
824 }
825 }
826 {
827 int64_t offset = 0;
828 for (auto &fb : faceBlocks) {
829 fb->set_offset(offset);
830 offset += fb->entity_count();
831 }
832 }
833 {
834 int64_t offset = 0;
835 for (auto &eb : edgeBlocks) {
836 eb->set_offset(offset);
837 offset += eb->entity_count();
838 }
839 }
840 }
841
842 // GroupingEntity consistency check:
843 // -- debug and parallel -- default to true; can disable via environment variable
844 // -- non-debug and parallel -- default to false; can enable via environment variable
845 #ifndef NDEBUG
846 bool check_consistency = true;
847 #else
848 bool check_consistency = false;
849 #endif
850 Ioss::Utils::check_set_bool_property(get_database()->get_property_manager(),
851 "CHECK_PARALLEL_CONSISTENCY", check_consistency);
852 if (check_consistency) {
853 bool ok = check_parallel_consistency(*this);
854 if (!ok) {
855 IOSS_ERROR(fmt::format("ERROR: Parallel Consistency Failure for {} database '{}'.",
856 (get_database()->is_input() ? "input" : "output"),
857 get_database()->get_filename()));
858 }
859 }
860
861 modelDefined = true;
862 }
863 else if (current_state == STATE_DEFINE_TRANSIENT) {
864 transientDefined = true;
865 }
866 else if (current_state == STATE_MODEL) {
867 modelWritten = true;
868 }
869 else if (current_state == STATE_TRANSIENT) {
870 transientWritten = true;
871 }
872
873 return success;
874 }
875
876 /** \brief Add a state for a specified time.
877 *
878 * The states in the region will be 1-based.
879 *
880 * \param[in] time The time at the new state.
881 * \returns The state index (1-based).
882 */
883 int Region::add_state_nl(double time)
884 {
885 // NOTE: For restart input databases, it is possible that the time
886 // is not monotonically increasing...
887 auto util = get_database()->util();
888 if (util.parallel_rank() == 0) {
889 if (!get_database()->is_input() && !stateTimes.empty() && time <= stateTimes.back()) {
890 // Check that time is increasing...
891 static bool warning_output = false;
892 if (!warning_output) {
893 fmt::print(Ioss::WarnOut(),
894 "Current time {} is not greater than previous time {} in\n\t{}.\n"
895 "This may cause problems in applications that assume monotonically increasing "
896 "time values.\n",
897 time, stateTimes.back(), get_database()->get_filename());
898 warning_output = true;
899 }
900 }
901 }
902
903 if (get_state() == STATE_TRANSIENT) {
904 // Makes sure we return proper stateCount in case of dynamic topology changes
905 update_dynamic_topology();
906 }
907
908 if (get_database()->is_input() || get_database()->usage() == WRITE_RESULTS ||
909 get_database()->usage() == WRITE_RESTART) {
910 stateTimes.push_back(time);
911 SMART_ASSERT((int)stateTimes.size() == stateCount + 1)(stateTimes.size())(stateCount);
912 }
913 else {
914
915 // Keep only the last time in the vector... This is to avoid
916 // memory growth for output databases that write lots of steps
917 // (heartbeat, history). There is no need to keep a list of
918 // times that have been written since they are just streamed out
919 // and never read We do sometimes need the list of times written
920 // to restart or results files though...
921 if (stateTimes.empty()) {
922 stateTimes.push_back(time);
923 }
924 else {
925 stateTimes[0] = time;
926 }
927 }
928 return ++stateCount;
929 ;
930 }
931
932 /** \brief Get the time corresponding to the specified state or the currently active state.
933 *
934 * \param[in] state The state index (1-based) or -1 for the currently active state.
935 * \returns The time at the specified state or the currently active state.
936 */
937 double Region::get_state_time(int state) const
938 {
939 IOSS_FUNC_ENTER(m_);
940 double time = 0.0;
941 if (state == -1) {
942 if (get_database()->is_input() || get_database()->usage() == WRITE_RESULTS ||
943 get_database()->usage() == WRITE_RESTART) {
944 if (currentState == -1) {
945 IOSS_ERROR(fmt::format("ERROR: No currently active state.\n [{}]\n",
946 get_database()->get_filename()));
947 }
948 else {
949 SMART_ASSERT((int)stateTimes.size() >= currentState)(stateTimes.size())(currentState);
950 time = stateTimes[currentState - 1];
951 }
952 }
953 else {
954 SMART_ASSERT(!stateTimes.empty());
955 time = stateTimes[0];
956 }
957 }
958 else if (state <= 0 || state > stateCount) {
959 IOSS_ERROR(
960 fmt::format("ERROR: Requested state ({}) is invalid. State must be between 1 and {}.\n"
961 " [{}]\n",
962 state, stateCount, get_database()->get_filename()));
963 }
964 else {
965 if (get_database()->is_input() || get_database()->usage() == WRITE_RESULTS ||
966 get_database()->usage() == WRITE_RESTART) {
967 SMART_ASSERT((int)stateTimes.size() >= state)(stateTimes.size())(state);
968 time = stateTimes[state - 1];
969 }
970 else {
971 SMART_ASSERT(!stateTimes.empty());
972 time = stateTimes[0];
973 }
974 }
975 return time;
976 }
977
978 /** \brief Get the maximum time step index (1-based) and time for the region.
979 *
980 * \returns A pair consisting of the step (1-based) corresponding to
981 * the maximum time on the database and the corresponding maximum
982 * time value. Note that this may not necessarily be the last step
983 * on the database if cycle and overlay are being used.
984 */
985 std::pair<int, double> Region::get_max_time() const
986 {
987 IOSS_FUNC_ENTER(m_);
988 if (!get_database()->is_input() && get_database()->usage() != WRITE_RESULTS &&
989 get_database()->usage() != WRITE_RESTART) {
990 return std::make_pair(currentState, stateTimes[0]);
991 }
992 // Cleanout the stateTimes vector and reload with current data in
993 // case the database is being read and written at the same time.
994 // This is rare, but is a supported use case.
995 stateCount = 0;
996 Ioss::Utils::clear(stateTimes);
997 DatabaseIO *db = get_database();
998 db->get_step_times();
999
1000 int step = stateTimes.empty() ? -1 : 0;
1001 double max_time = stateTimes.empty() ? -1 : stateTimes[0];
1002 for (int i = 1; i < static_cast<int>(stateTimes.size()); i++) {
1003 if (stateTimes[i] > max_time) {
1004 step = i;
1005 max_time = stateTimes[i];
1006 }
1007 }
1008 return std::make_pair(step + 1, max_time);
1009 }
1010
1011 /** \brief Get the minimum time step index (1-based) and time for the region.
1012 *
1013 * \returns A pair consisting of the step (1-based) corresponding to
1014 * the minimum time on the database and the corresponding minimum
1015 * time value. Note that this may not necessarily be the first step
1016 * on the database if cycle and overlay are being used.
1017 */
1018 std::pair<int, double> Region::get_min_time() const
1019 {
1020 IOSS_FUNC_ENTER(m_);
1021 if (!get_database()->is_input() && get_database()->usage() != WRITE_RESULTS &&
1022 get_database()->usage() != WRITE_RESTART) {
1023 return std::make_pair(currentState, stateTimes[0]);
1024 }
1025 // Cleanout the stateTimes vector and reload with current data in
1026 // case the database is being read and written at the same time.
1027 // This is rare, but is a supported use case.
1028 stateCount = 0;
1029 Ioss::Utils::clear(stateTimes);
1030 DatabaseIO *db = get_database();
1031 db->get_step_times();
1032
1033 int step = 0;
1034 double min_time = stateTimes[0];
1035 for (int i = 1; i < static_cast<int>(stateTimes.size()); i++) {
1036 if (stateTimes[i] < min_time) {
1037 step = i;
1038 min_time = stateTimes[i];
1039 }
1040 }
1041 return std::make_pair(step + 1, min_time);
1042 }
1043
1044 /** \brief Begin a state (moment in time).
1045 *
1046 * \param[in] state The state index (1-based).
1047 * \returns The time of this state.
1048 */
1049 double Region::begin_state(int state)
1050 {
1051 double time = 0.0;
1052 if (get_database()->is_input() && stateCount == 0) {
1053 IOSS_ERROR(fmt::format("ERROR: There are no states (time steps) on the input database.\n"
1054 " [{}]\n",
1055 get_database()->get_filename()));
1056 }
1057 if (state <= 0 || state > stateCount) {
1058 IOSS_ERROR(
1059 fmt::format("ERROR: Requested state ({}) is invalid. State must be between 1 and {}.\n"
1060 " [{}]\n",
1061 state, stateCount, get_database()->get_filename()));
1062 }
1063 else if (currentState != -1 && !get_database()->is_input()) {
1064 IOSS_ERROR(
1065 fmt::format("ERROR: State {} was not ended. Can not begin new state.\n [{}]\n",
1066 currentState, get_database()->get_filename()));
1067 }
1068 else {
1069 {
1070 IOSS_FUNC_ENTER(m_);
1071
1072 SMART_ASSERT(state <= stateCount)(state)(stateCount);
1073 if (get_database()->is_input() || get_database()->usage() == WRITE_RESULTS ||
1074 get_database()->usage() == WRITE_RESTART) {
1075 SMART_ASSERT((int)stateTimes.size() >= state)(stateTimes.size())(state);
1076 time = stateTimes[state - 1];
1077 }
1078 else {
1079 SMART_ASSERT(!stateTimes.empty());
1080 time = stateTimes[0];
1081 }
1082 currentState = state;
1083 }
1084
1085 update_dynamic_topology();
1086
1087 DatabaseIO *db = get_database();
1088 db->begin_state(state, time);
1089 }
1090 return time;
1091 }
1092
1093 /** \brief End a state (moment in time).
1094 *
1095 * \param[in] state The state index (1-based).
1096 * \returns The time of this state.
1097 */
1098 double Region::end_state(int state)
1099 {
1100 if (state != currentState) {
1101 IOSS_ERROR(fmt::format(
1102 "ERROR: The current database state ({}) does not match the ending state ({}).\n"
1103 " [{}]\n",
1104 currentState, state, get_database()->get_filename()));
1105 }
1106 DatabaseIO *db = get_database();
1107 double time = 0.0;
1108 {
1109 IOSS_FUNC_ENTER(m_);
1110 if (get_database()->is_input() || get_database()->usage() == WRITE_RESULTS ||
1111 get_database()->usage() == WRITE_RESTART) {
1112 SMART_ASSERT((int)stateTimes.size() >= state)(stateTimes.size())(state);
1113 time = stateTimes[state - 1];
1114 }
1115 else {
1116 SMART_ASSERT(!stateTimes.empty());
1117 time = stateTimes[0];
1118 }
1119 }
1120 db->end_state(state, time);
1121 currentState = -1;
1122 return time;
1123 }
1124
1125 void Region::erase_fields(Field::RoleType role)
1126 {
1127 field_erase(role);
1128 internal_erase_fields(get_node_blocks(), role);
1129 internal_erase_fields(get_edge_blocks(), role);
1130 internal_erase_fields(get_face_blocks(), role);
1131 internal_erase_fields(get_element_blocks(), role);
1132 internal_erase_fields(get_nodesets(), role);
1133 internal_erase_fields(get_edgesets(), role);
1134 internal_erase_fields(get_facesets(), role);
1135 internal_erase_fields(get_elementsets(), role);
1136 internal_erase_fields(get_sidesets(), role);
1137 internal_erase_fields(get_commsets(), role);
1138 internal_erase_fields(get_structured_blocks(), role);
1139 internal_erase_fields(get_assemblies(), role);
1140 internal_erase_fields(get_blobs(), role);
1141 }
1142
1143 /** \brief Add a structured block to the region.
1144 *
1145 * \param[in] structured_block The structured block to add
1146 * \returns True if successful.
1147 */
1148 bool Region::add(StructuredBlock *structured_block)
1149 {
1150 check_for_duplicate_names(structured_block);
1151 update_database(this, structured_block);
1152 IOSS_FUNC_ENTER(m_);
1153
1154 // Check that region is in correct state for adding entities
1155 if (get_state() == STATE_DEFINE_MODEL) {
1156 // Add node and cell offsets based on the node_count and
1157 // cell_count of the previous block. Only add if there is more
1158 // than one block; use default for first block (or user-defined
1159 // values)
1160 if (!structuredBlocks.empty()) {
1161 auto *prev_block = structuredBlocks.back();
1162 size_t num_node = prev_block->get_property("node_count").get_int();
1163 size_t num_cell = prev_block->get_property("cell_count").get_int();
1164 num_node += prev_block->get_node_offset();
1165 num_cell += prev_block->get_cell_offset();
1166
1167 structured_block->set_node_offset(num_node);
1168 structured_block->set_cell_offset(num_cell);
1169
1170 size_t global_num_node = prev_block->get_property("global_node_count").get_int();
1171 size_t global_num_cell = prev_block->get_property("global_cell_count").get_int();
1172 global_num_node += prev_block->get_node_global_offset();
1173 global_num_cell += prev_block->get_cell_global_offset();
1174
1175 structured_block->set_node_global_offset(global_num_node);
1176 structured_block->set_cell_global_offset(global_num_cell);
1177 }
1178
1179 structured_block->property_add(
1180 Ioss::Property(orig_block_order(), (int)structuredBlocks.size()));
1181 structuredBlocks.push_back(structured_block);
1182
1183 // This will possibly be overwritten at a later time when the block is output
1184 // to the cgns file
1185 structured_block->property_add(Ioss::Property("zone", (int)structuredBlocks.size()));
1186 structured_block->property_add(Ioss::Property("base", 1));
1187 // Add name as alias to itself to simplify later uses...
1188 add_alias_nl(structured_block);
1189 return true;
1190 }
1191 return false;
1192 }
1193
1194 /** \brief Add a node block to the region.
1195 *
1196 * \param[in] node_block The node block to add
1197 * \returns True if successful.
1198 */
1199 bool Region::add(NodeBlock *node_block)
1200 {
1201 check_for_duplicate_names(node_block);
1202 update_database(this, node_block);
1203 IOSS_FUNC_ENTER(m_);
1204
1205 // Check that region is in correct state for adding entities
1206 if (get_state() == STATE_DEFINE_MODEL) {
1207 nodeBlocks.push_back(node_block);
1208 // Add name as alias to itself to simplify later uses...
1209 add_alias_nl(node_block);
1210
1211 return true;
1212 }
1213 return false;
1214 }
1215
1216 /** \brief Remove an assembly to the region.
1217 *
1218 * \param[in] removal The assembly to remove
1219 * \returns True if successful.
1220 *
1221 * Checks other assemblies for uses of this assembly and removes
1222 * if from their member lists.
1223 */
1224 bool Region::remove(Assembly *removal)
1225 {
1226 IOSS_FUNC_ENTER(m_);
1227
1228 bool changed = false;
1229 // Check that region is in correct state for modifying entities
1230 if (get_state() == STATE_DEFINE_MODEL) {
1231 // Check all existing assemblies to see if any contain this assembly:
1232 for (auto *assembly : assemblies) {
1233 bool removed = assembly->remove(removal);
1234 if (removed) {
1235 changed = true;
1236 }
1237 }
1238
1239 // Now remove this assembly...
1240 for (size_t i = 0; i < assemblies.size(); i++) {
1241 if (assemblies[i] == removal) {
1242 assemblies.erase(assemblies.begin() + i);
1243 changed = true;
1244 }
1245 }
1246 }
1247 return changed;
1248 }
1249
1250 /** \brief Add an assembly to the region.
1251 *
1252 * \param[in] assembly The assembly to add
1253 * \returns True if successful.
1254 */
1255 bool Region::add(Assembly *assembly)
1256 {
1257 check_for_duplicate_names(assembly);
1258 update_database(this, assembly);
1259 IOSS_FUNC_ENTER(m_);
1260
1261 // Check that region is in correct state for adding entities
1262 if (get_state() == STATE_DEFINE_MODEL) {
1263 assemblies.push_back(assembly);
1264 // Add name as alias to itself to simplify later uses...
1265 add_alias_nl(assembly);
1266
1267 return true;
1268 }
1269 return false;
1270 }
1271
1272 /** \brief Add an blob to the region.
1273 *
1274 * \param[in] blob The blob to add
1275 * \returns True if successful.
1276 */
1277 bool Region::add(Blob *blob)
1278 {
1279 check_for_duplicate_names(blob);
1280 update_database(this, blob);
1281 IOSS_FUNC_ENTER(m_);
1282
1283 // Check that region is in correct state for adding entities
1284 if (get_state() == STATE_DEFINE_MODEL) {
1285 blobs.push_back(blob);
1286 // Add name as alias to itself to simplify later uses...
1287 add_alias_nl(blob);
1288
1289 return true;
1290 }
1291 return false;
1292 }
1293
1294 /** \brief Add a coordinate frame to the region.
1295 *
1296 * \param[in] frame The coordinate frame to add
1297 * \returns True if successful.
1298 */
1299 bool Region::add(const CoordinateFrame &frame)
1300 {
1301 IOSS_FUNC_ENTER(m_);
1302 // Check that region is in correct state for adding entities
1303 if (get_state() == STATE_DEFINE_MODEL) {
1304 coordinateFrames.push_back(frame);
1305 return true;
1306 }
1307 return false;
1308 }
1309
1310 /** \brief Add an element block to the region.
1311 *
1312 * \param[in] element_block The element block to add
1313 * \returns True if successful.
1314 */
1315 bool Region::add(ElementBlock *element_block)
1316 {
1317 check_for_duplicate_names(element_block);
1318 update_database(this, element_block);
1319 IOSS_FUNC_ENTER(m_);
1320
1321 // Check that region is in correct state for adding entities
1322 if (get_state() == STATE_DEFINE_MODEL) {
1323 // Add name as alias to itself to simplify later uses...
1324 add_alias_nl(element_block);
1325
1326 // An input database defines these in the order matching the order
1327 // on the "file". For output, we need to order based on the
1328 // "original_block_order" property and calculate the offset at that
1329 // point. This is done in "end".
1330
1331 if (is_input_or_appending_output(get_database())) {
1332 size_t nblocks = elementBlocks.size();
1333 int64_t offset = 0;
1334 if (nblocks > 0) {
1335 offset =
1336 elementBlocks[nblocks - 1]->get_offset() + elementBlocks[nblocks - 1]->entity_count();
1337 }
1338 SMART_ASSERT(offset >= 0)(offset);
1339 element_block->set_offset(offset);
1340 }
1341 #if 0
1342 // Would like to use this, but gives issue in legacy contact...
1343 // If this is enabled, then remove all settings of
1344 // "orig_block_order()" from individual DatabaseIO classes.
1345 element_block->property_add(Ioss::Property(orig_block_order(), (int)elementBlocks.size()));
1346 #else
1347 else {
1348 // Check whether the "original_block_order" property exists on
1349 // this element block. If it isn't there, then add it with a
1350 // large value. If this is an element block read from the
1351 // input mesh, then the value will be updated during the
1352 // 'synchronize_id_and_name' function; if it is a block
1353 // created by the application during execution, then this
1354 // value will persist. Add the property with a very large
1355 // number such that it will later be sorted after all
1356 // "original" blocks. Note that it doesn't matter if two of
1357 // the "new" blocks have the same value since there is no
1358 // ordering of new blocks that must be preserved. (Use
1359 // int_MAX/2 just to avoid some paranoia about strange issue
1360 // that might arise from int_MAX)
1361 if (!element_block->property_exists(orig_block_order())) {
1362 element_block->property_add(Property(orig_block_order(), INT_MAX / 2));
1363 }
1364 }
1365 #endif
1366 elementBlocks.push_back(element_block);
1367 return true;
1368 }
1369 return false;
1370 }
1371
1372 /** \brief Add a face block to the region.
1373 *
1374 * \param[in] face_block The face block to add
1375 * \returns True if successful.
1376 */
1377 bool Region::add(FaceBlock *face_block)
1378 {
1379 check_for_duplicate_names(face_block);
1380 update_database(this, face_block);
1381 IOSS_FUNC_ENTER(m_);
1382
1383 // Check that region is in correct state for adding entities
1384 if (get_state() == STATE_DEFINE_MODEL) {
1385 // Add name as alias to itself to simplify later uses...
1386 add_alias_nl(face_block);
1387
1388 // An input database defines these in the order matching the order
1389 // on the "file". For output, we need to order based on the
1390 // "original_block_order" property and calculate the offset at that
1391 // point. This is done in "end".
1392
1393 if (is_input_or_appending_output(get_database())) {
1394 size_t nblocks = faceBlocks.size();
1395 int64_t offset = 0;
1396 if (nblocks > 0) {
1397 offset = faceBlocks[nblocks - 1]->get_offset() + faceBlocks[nblocks - 1]->entity_count();
1398 }
1399 face_block->set_offset(offset);
1400 }
1401 face_block->property_add(Ioss::Property(orig_block_order(), (int)faceBlocks.size()));
1402 faceBlocks.push_back(face_block);
1403 return true;
1404 }
1405 return false;
1406 }
1407
1408 /** \brief Add an edge block to the region.
1409 *
1410 * \param[in] edge_block The edge block to add
1411 * \returns True if successful.
1412 */
1413 bool Region::add(EdgeBlock *edge_block)
1414 {
1415 check_for_duplicate_names(edge_block);
1416 update_database(this, edge_block);
1417 IOSS_FUNC_ENTER(m_);
1418
1419 // Check that region is in correct state for adding entities
1420 if (get_state() == STATE_DEFINE_MODEL) {
1421 // Add name as alias to itself to simplify later uses...
1422 add_alias_nl(edge_block);
1423
1424 // An input database defines these in the order matching the order
1425 // on the "file". For output, we need to order based on the
1426 // "original_block_order" property and calculate the offset at that
1427 // point. This is done in "end".
1428
1429 if (is_input_or_appending_output(get_database())) {
1430 size_t nblocks = edgeBlocks.size();
1431 int64_t offset = 0;
1432 if (nblocks > 0) {
1433 offset = edgeBlocks[nblocks - 1]->get_offset() + edgeBlocks[nblocks - 1]->entity_count();
1434 }
1435 edge_block->set_offset(offset);
1436 }
1437 edge_block->property_add(Ioss::Property(orig_block_order(), (int)edgeBlocks.size()));
1438 edgeBlocks.push_back(edge_block);
1439 return true;
1440 }
1441 return false;
1442 }
1443
1444 /** \brief Add a side set to the region.
1445 *
1446 * \param[in] sideset The side set to add
1447 * \returns True if successful.
1448 */
1449 bool Region::add(SideSet *sideset)
1450 {
1451 check_for_duplicate_names(sideset);
1452 update_database(this, sideset);
1453 IOSS_FUNC_ENTER(m_);
1454 // Check that region is in correct state for adding entities
1455 if (get_state() == STATE_DEFINE_MODEL) {
1456 // Add name as alias to itself to simplify later uses...
1457 add_alias_nl(sideset);
1458
1459 // Also add "sideset_{id}" as an alias.
1460 auto id = sideset->get_optional_property(id_str(), -1);
1461 if (id != -1) {
1462 std::string ss_alias = fmt::format("sideset_{}", id);
1463 add_alias_nl(sideset->name(), ss_alias, sideset->type());
1464 }
1465 sideSets.push_back(sideset);
1466 return true;
1467 }
1468 return false;
1469 }
1470
1471 /** \brief Add a node set to the region.
1472 *
1473 * \param[in] nodeset The node set to add
1474 * \returns True if successful.
1475 */
1476 bool Region::add(NodeSet *nodeset)
1477 {
1478 check_for_duplicate_names(nodeset);
1479 update_database(this, nodeset);
1480 IOSS_FUNC_ENTER(m_);
1481 // Check that region is in correct state for adding entities
1482 if (get_state() == STATE_DEFINE_MODEL) {
1483 // Add name as alias to itself to simplify later uses...
1484 add_alias_nl(nodeset);
1485
1486 // Also add "nodeset_{id}" as an alias.
1487 auto id = nodeset->get_optional_property(id_str(), -1);
1488 if (id != -1) {
1489 std::string ns_alias = fmt::format("nodeset_{}", id);
1490 add_alias_nl(nodeset->name(), ns_alias, nodeset->type());
1491 }
1492 nodeSets.push_back(nodeset);
1493 return true;
1494 }
1495 return false;
1496 }
1497
1498 /** \brief Add an edge set to the region.
1499 *
1500 * \param[in] edgeset The edge set to add
1501 * \returns True if successful.
1502 */
1503 bool Region::add(EdgeSet *edgeset)
1504 {
1505 check_for_duplicate_names(edgeset);
1506 update_database(this, edgeset);
1507 IOSS_FUNC_ENTER(m_);
1508 // Check that region is in correct state for adding entities
1509 if (get_state() == STATE_DEFINE_MODEL) {
1510 // Add name as alias to itself to simplify later uses...
1511 add_alias_nl(edgeset);
1512 edgeSets.push_back(edgeset);
1513 return true;
1514 }
1515 return false;
1516 }
1517
1518 /** \brief Add a face set to the region.
1519 *
1520 * \param[in] faceset The face set to add
1521 * \returns True if successful.
1522 */
1523 bool Region::add(FaceSet *faceset)
1524 {
1525 check_for_duplicate_names(faceset);
1526 update_database(this, faceset);
1527 IOSS_FUNC_ENTER(m_);
1528 // Check that region is in correct state for adding entities
1529 if (get_state() == STATE_DEFINE_MODEL) {
1530 // Add name as alias to itself to simplify later uses...
1531 add_alias_nl(faceset);
1532 faceSets.push_back(faceset);
1533 return true;
1534 }
1535 return false;
1536 }
1537
1538 /** \brief Add an element set to the region.
1539 *
1540 * \param[in] elementset The element set to add
1541 * \returns True if successful.
1542 */
1543 bool Region::add(ElementSet *elementset)
1544 {
1545 check_for_duplicate_names(elementset);
1546 update_database(this, elementset);
1547 IOSS_FUNC_ENTER(m_);
1548 // Check that region is in correct state for adding entities
1549 if (get_state() == STATE_DEFINE_MODEL) {
1550 // Add name as alias to itself to simplify later uses...
1551 add_alias_nl(elementset);
1552 elementSets.push_back(elementset);
1553 return true;
1554 }
1555 return false;
1556 }
1557
1558 /** \brief Add a comm set to the region.
1559 *
1560 * \param[in] commset The comm set to add
1561 * \returns True if successful.
1562 */
1563 bool Region::add(CommSet *commset)
1564 {
1565 check_for_duplicate_names(commset);
1566 update_database(this, commset);
1567 IOSS_FUNC_ENTER(m_);
1568 // Check that region is in correct state for adding entities
1569 if (get_state() == STATE_DEFINE_MODEL) {
1570 // Add name as alias to itself to simplify later uses...
1571 add_alias_nl(commset);
1572 commSets.push_back(commset);
1573 return true;
1574 }
1575 return false;
1576 }
1577
1578 /** \brief Get all the region's Assembly objects.
1579 *
1580 * \returns A vector of all the region's Assembly objects.
1581 */
1582 const AssemblyContainer &Region::get_assemblies() const { return assemblies; }
1583
1584 /** \brief Get all the region's Blob objects.
1585 *
1586 * \returns A vector of all the region's Blob objects.
1587 */
1588 const BlobContainer &Region::get_blobs() const { return blobs; }
1589
1590 /** \brief Get all the region's NodeBlock objects.
1591 *
1592 * \returns A vector of all the region's NodeBlock objects.
1593 */
1594 const NodeBlockContainer &Region::get_node_blocks() const { return nodeBlocks; }
1595
1596 /** \brief Get all the region's EdgeBlock objects.
1597 *
1598 * \returns A vector of all the region's EdgeBlock objects.
1599 */
1600 const EdgeBlockContainer &Region::get_edge_blocks() const { return edgeBlocks; }
1601
1602 /** \brief Get all the region's FaceBlock objects.
1603 *
1604 * \returns A vector of all the region's FaceBlock objects.
1605 */
1606 const FaceBlockContainer &Region::get_face_blocks() const { return faceBlocks; }
1607
1608 /** \brief Get all the region's ElementBlock objects.
1609 *
1610 * \returns A vector of all the region's ElementBlock objects.
1611 */
1612 const ElementBlockContainer &Region::get_element_blocks() const { return elementBlocks; }
1613
1614 /** \brief Get all the region's StructuredBlock objects.
1615 *
1616 * \returns A vector of all the region's StructuredBlock objects.
1617 */
1618 const StructuredBlockContainer &Region::get_structured_blocks() const { return structuredBlocks; }
1619
1620 /** \brief Get all the region's SideSet objects.
1621 *
1622 * \returns A vector of all the region's SideSet objects.
1623 */
1624 const SideSetContainer &Region::get_sidesets() const { return sideSets; }
1625
1626 /** \brief Get all the region's NodeSet objects.
1627 *
1628 * \returns A vector of all the region's NodeSet objects.
1629 */
1630 const NodeSetContainer &Region::get_nodesets() const { return nodeSets; }
1631
1632 /** \brief Get all the region's EdgeSet objects.
1633 *
1634 * \returns A vector of all the region's EdgeSet objects.
1635 */
1636 const EdgeSetContainer &Region::get_edgesets() const { return edgeSets; }
1637
1638 /** \brief Get all the region's FaceSet objects.
1639 *
1640 * \returns A vector of all the region's FaceSet objects.
1641 */
1642 const FaceSetContainer &Region::get_facesets() const { return faceSets; }
1643
1644 /** \brief Get all the region's ElementSet objects.
1645 *
1646 * \returns A vector of all the region's ElementSet objects.
1647 */
1648 const ElementSetContainer &Region::get_elementsets() const { return elementSets; }
1649
1650 /** \brief Get all the region's CommSet objects.
1651 *
1652 * \returns A vector of all the region's CommSet objects.
1653 */
1654 const CommSetContainer &Region::get_commsets() const { return commSets; }
1655
1656 /** \brief Get all the region's CoordinateFrame objects.
1657 *
1658 * \returns A vector of all the region's CoordinateFrame objects.
1659 */
1660 const CoordinateFrameContainer &Region::get_coordinate_frames() const { return coordinateFrames; }
1661
1662 /** \brief Add a grouping entity's name as an alias for itself.
1663 *
1664 * \param[in] ge The grouping entity.
1665 * \returns True if successful
1666 */
1667 bool Region::add_alias(const GroupingEntity *ge)
1668 {
1669 IOSS_FUNC_ENTER(m_);
1670 return add_alias_nl(ge);
1671 }
1672
1673 bool Region::add_alias_nl(const GroupingEntity *ge)
1674 {
1675 // See if an entity with this name and type already exists...
1676 const auto &db_name = ge->name();
1677 const std::string alias = get_alias_nl(db_name, ge->type());
1678
1679 if (!alias.empty()) {
1680 const GroupingEntity *old_ge = get_entity(db_name, ge->type());
1681 if (old_ge != nullptr && ge != old_ge) {
1682 if (!((old_ge->type() == SIDEBLOCK && ge->type() == SIDESET) ||
1683 (ge->type() == SIDEBLOCK && old_ge->type() == SIDESET))) {
1684 auto old_id = old_ge->get_optional_property(id_str(), -1);
1685 auto new_id = ge->get_optional_property(id_str(), -1);
1686 IOSS_ERROR(fmt::format(
1687 "\n\nERROR: Duplicate names detected.\n"
1688 " The name '{}' was found for both {} {} and {} {}.\n"
1689 " Names must be unique over all types in a finite element model.\n\n",
1690 db_name, old_ge->type_string(), old_id, ge->type_string(), new_id));
1691 }
1692 }
1693 }
1694 bool success = add_alias_nl(db_name, db_name, ge->type());
1695
1696 // "db_name" property is used with the canonical name setting.
1697 if (success && ge->property_exists("db_name")) {
1698 std::string canon_name = ge->get_property("db_name").get_string();
1699 if (canon_name != db_name) {
1700 success = add_alias_nl(db_name, canon_name, ge->type());
1701 }
1702 }
1703
1704 return success;
1705 }
1706
1707 /** \brief Add an alias for a name in a region.
1708 *
1709 * For use with the USTRING type in Sierra, create an uppercase
1710 * version of all aliases...
1711 *
1712 * \param[in] db_name The original name.
1713 * \param[in] alias the alias
1714 * \param[in] type the entity type
1715 * \returns True if successful
1716 */
1717 bool Region::add_alias(const std::string &db_name, const std::string &alias, EntityType type)
1718 {
1719 IOSS_FUNC_ENTER(m_);
1720 return add_alias_nl(db_name, alias, type);
1721 }
1722
1723 bool Region::add_alias_nl(const std::string &db_name, const std::string &alias, EntityType type)
1724 {
1725 // Possible that 'db_name' is itself an alias, resolve down to "canonical"
1726 // name...
1727 std::string canon = db_name;
1728 if (db_name != alias) {
1729 canon = get_alias_nl(db_name, type);
1730 }
1731
1732 if (!canon.empty()) {
1733 std::string uname = Ioss::Utils::uppercase(alias);
1734 if (uname != alias) {
1735 aliases_[type].insert(std::make_pair(uname, canon));
1736 }
1737
1738 bool result;
1739 std::tie(std::ignore, result) = aliases_[type].insert(std::make_pair(alias, canon));
1740 return result;
1741 }
1742 IOSS_ERROR(fmt::format(
1743 "\n\nERROR: The entity named '{}' of type {} which is being aliased to '{}' does "
1744 "not exist in "
1745 "region '{}'.\n",
1746 db_name, static_cast<int>(type), alias, name()));
1747 }
1748
1749 bool Region::add_alias(const std::string &db_name, const std::string &alias)
1750 {
1751 auto *entity = get_entity(db_name);
1752 IOSS_FUNC_ENTER(m_);
1753 if (entity != nullptr) {
1754 return add_alias_nl(db_name, alias, entity->type());
1755 }
1756 return false;
1757 }
1758
1759 /** \brief Get the original name for an alias.
1760 *
1761 * \param[in] alias The alias name.
1762 * \param[in] type the entity type
1763 * \returns The original name.
1764 */
1765 std::string Region::get_alias(const std::string &alias, EntityType type) const
1766 {
1767 IOSS_FUNC_ENTER(m_);
1768 return get_alias_nl(alias, type);
1769 }
1770
1771 std::string Region::get_alias_nl(const std::string &alias, EntityType type) const
1772 {
1773 std::string ci_alias = Ioss::Utils::uppercase(alias);
1774 auto I = aliases_[type].find(ci_alias);
1775 if (I == aliases_[type].end()) {
1776 if (type == Ioss::SIDEBLOCK) {
1777 I = aliases_[Ioss::SIDESET].find(ci_alias);
1778 if (I != aliases_[Ioss::SIDESET].end()) {
1779 return (*I).second;
1780 }
1781 }
1782 return "";
1783 }
1784 return (*I).second;
1785 }
1786
1787 /** \brief Get all aliases for a name in the region.
1788 *
1789 * \param[in] my_name The original name.
1790 * \param[in] type the entity type
1791 * \param[in,out] aliases On input, any vector of strings.
1792 * On output, all aliases for my_name are appended.
1793 * \returns The number of aliases that were appended.
1794 *
1795 */
1796 int Region::get_aliases(const std::string &my_name, EntityType type,
1797 Ioss::NameList &aliases) const
1798 {
1799 IOSS_FUNC_ENTER(m_);
1800 size_t size = aliases.size();
1801 for (const auto &alias_pair : aliases_[type]) {
1802 std::string alias = alias_pair.first;
1803 std::string base = alias_pair.second;
1804 if (base == my_name) {
1805 aliases.push_back(alias);
1806 }
1807 }
1808 return static_cast<int>(aliases.size() - size);
1809 }
1810
1811 /** \brief Get all original name / alias pairs for the region.
1812 *
1813 * \returns All original name / alias pairs for the region.
1814 */
1815 const AliasMap &Region::get_alias_map(EntityType type) const { return aliases_[type]; }
1816
1817 /** \brief Get an entity of a known EntityType
1818 *
1819 * \param[in] my_name The name of the entity to get
1820 * \param[in] io_type The known type of the entity.
1821 * \returns The entity with the given name of the given type, or nullptr if not found.
1822 */
1823 GroupingEntity *Region::get_entity(const std::string &my_name, EntityType io_type) const
1824 {
1825 if (io_type == NODEBLOCK) {
1826 return get_node_block(my_name);
1827 }
1828 if (io_type == ELEMENTBLOCK) {
1829 return get_element_block(my_name);
1830 }
1831 if (io_type == STRUCTUREDBLOCK) {
1832 return get_structured_block(my_name);
1833 }
1834 if (io_type == FACEBLOCK) {
1835 return get_face_block(my_name);
1836 }
1837 if (io_type == EDGEBLOCK) {
1838 return get_edge_block(my_name);
1839 }
1840 if (io_type == SIDESET) {
1841 return get_sideset(my_name);
1842 }
1843 if (io_type == NODESET) {
1844 return get_nodeset(my_name);
1845 }
1846 if (io_type == EDGESET) {
1847 return get_edgeset(my_name);
1848 }
1849 if (io_type == FACESET) {
1850 return get_faceset(my_name);
1851 }
1852 if (io_type == ELEMENTSET) {
1853 return get_elementset(my_name);
1854 }
1855 if (io_type == COMMSET) {
1856 return get_commset(my_name);
1857 }
1858 if (io_type == SIDEBLOCK) {
1859 return get_sideblock(my_name);
1860 }
1861 if (io_type == ASSEMBLY) {
1862 return get_assembly(my_name);
1863 }
1864 if (io_type == BLOB) {
1865 return get_blob(my_name);
1866 }
1867 return nullptr;
1868 }
1869
1870 /** \brief Get an entity of a unknown EntityType
1871 *
1872 * Searches for an entity with the given name in a fixed order of
1873 * entity types. First NODEBLOCK entities are searched. Then ELEMENTBLOCK
1874 * entities, etc.
1875 *
1876 * \param[in] my_name The name of the entity to get
1877 * \returns The entity with the given name, or nullptr if not found.
1878 */
1879 GroupingEntity *Region::get_entity(const std::string &my_name) const
1880 {
1881 int nfound = 0;
1882 GroupingEntity *entity = nullptr;
1883 GroupingEntity *nb = get_node_block(my_name);
1884 if (nb != nullptr) {
1885 entity = nb;
1886 nfound++;
1887 }
1888 GroupingEntity *eb = get_element_block(my_name);
1889 if (eb != nullptr) {
1890 entity = eb;
1891 nfound++;
1892 }
1893 GroupingEntity *sb = get_structured_block(my_name);
1894 if (sb != nullptr) {
1895 entity = sb;
1896 nfound++;
1897 }
1898 GroupingEntity *fb = get_face_block(my_name);
1899 if (fb != nullptr) {
1900 entity = fb;
1901 nfound++;
1902 }
1903 GroupingEntity *edb = get_edge_block(my_name);
1904 if (edb != nullptr) {
1905 entity = edb;
1906 nfound++;
1907 }
1908 GroupingEntity *ns = get_nodeset(my_name);
1909 if (ns != nullptr) {
1910 entity = ns;
1911 nfound++;
1912 }
1913 GroupingEntity *es = get_edgeset(my_name);
1914 if (es != nullptr) {
1915 entity = es;
1916 nfound++;
1917 }
1918 GroupingEntity *fs = get_faceset(my_name);
1919 if (fs != nullptr) {
1920 entity = fs;
1921 nfound++;
1922 }
1923 GroupingEntity *els = get_elementset(my_name);
1924 if (els != nullptr) {
1925 entity = els;
1926 nfound++;
1927 }
1928 GroupingEntity *cs = get_commset(my_name);
1929 if (cs != nullptr) {
1930 entity = cs;
1931 nfound++;
1932 }
1933 GroupingEntity *ss = get_sideset(my_name);
1934 if (ss != nullptr) {
1935 entity = ss;
1936 nfound++;
1937 }
1938 GroupingEntity *sib = get_sideblock(my_name);
1939 if (ss == nullptr && sib != nullptr) {
1940 // Allowable for sideset and a contained sideblock to have
1941 // same name. Historically, the sideset is returned in this case
1942 entity = sib;
1943 nfound++;
1944 }
1945 GroupingEntity *as = get_assembly(my_name);
1946 if (as != nullptr) {
1947 entity = as;
1948 nfound++;
1949 }
1950 GroupingEntity *bl = get_blob(my_name);
1951 if (bl != nullptr) {
1952 entity = bl;
1953 nfound++;
1954 }
1955 if (nfound > 1) {
1956 std::string filename = get_database()->get_filename();
1957 IOSS_ERROR(fmt::format(
1958 "ERROR: There are multiple ({}) blocks, sets, assemblies and/or blobs with the name '{}' "
1959 "defined in the "
1960 "database file '{}'.\n"
1961 "\tThis is allowed in general, but this application uses an API function (get_entity) "
1962 "that does not support duplicate names.",
1963 nfound, my_name, filename));
1964 }
1965 return entity;
1966 }
1967
1968 /** \brief Get an entity of a known EntityType and specified id
1969 *
1970 * \param[in] id The id of the entity to get
1971 * \param[in] io_type The known type of the entity.
1972 * \returns The entity with the given id of the given type, or nullptr if not found or if ids not
1973 * supported.
1974 */
1975 GroupingEntity *Region::get_entity(int64_t id, EntityType io_type) const
1976 {
1977 if (io_type == NODEBLOCK) {
1978 return get_entity_internal(id, get_node_blocks());
1979 }
1980 if (io_type == ELEMENTBLOCK) {
1981 return get_entity_internal(id, get_element_blocks());
1982 }
1983 if (io_type == STRUCTUREDBLOCK) {
1984 return get_entity_internal(id, get_structured_blocks());
1985 }
1986 if (io_type == FACEBLOCK) {
1987 return get_entity_internal(id, get_face_blocks());
1988 }
1989 if (io_type == EDGEBLOCK) {
1990 return get_entity_internal(id, get_edge_blocks());
1991 }
1992 if (io_type == SIDESET) {
1993 return get_entity_internal(id, get_sidesets());
1994 }
1995 if (io_type == NODESET) {
1996 return get_entity_internal(id, get_nodesets());
1997 }
1998 if (io_type == EDGESET) {
1999 return get_entity_internal(id, get_edgesets());
2000 }
2001 if (io_type == FACESET) {
2002 return get_entity_internal(id, get_facesets());
2003 }
2004 if (io_type == ELEMENTSET) {
2005 return get_entity_internal(id, get_elementsets());
2006 }
2007 if (io_type == COMMSET) {
2008 return get_entity_internal(id, get_commsets());
2009 }
2010 if (io_type == ASSEMBLY) {
2011 return get_entity_internal(id, get_assemblies());
2012 }
2013 if (io_type == BLOB) {
2014 return get_entity_internal(id, get_blobs());
2015 }
2016 return nullptr;
2017 }
2018
2019 /** \brief Get the assembly with the given name.
2020 *
2021 * \param[in] my_name The name of the assembly to get.
2022 * \returns The assembly, or nullptr if not found.
2023 */
2024 Assembly *Region::get_assembly(const std::string &my_name) const
2025 {
2026 IOSS_FUNC_ENTER(m_);
2027 const std::string db_name = get_alias_nl(my_name, ASSEMBLY);
2028 unsigned int db_hash = Ioss::Utils::hash(db_name);
2029
2030 Assembly *ge = nullptr;
2031 for (const auto &as : assemblies) {
2032 if (db_hash == as->hash() && as->name() == db_name) {
2033 ge = as;
2034 break;
2035 }
2036 }
2037 return ge;
2038 }
2039
2040 /** \brief Get the blob with the given name.
2041 *
2042 * \param[in] my_name The name of the blob to get.
2043 * \returns The blob, or nullptr if not found.
2044 */
2045 Blob *Region::get_blob(const std::string &my_name) const
2046 {
2047 IOSS_FUNC_ENTER(m_);
2048 const std::string db_name = get_alias_nl(my_name, BLOB);
2049 unsigned int db_hash = Ioss::Utils::hash(db_name);
2050
2051 Blob *ge = nullptr;
2052 for (const auto &bl : blobs) {
2053 if (db_hash == bl->hash() && bl->name() == db_name) {
2054 ge = bl;
2055 break;
2056 }
2057 }
2058 return ge;
2059 }
2060
2061 /** \brief Get the node block with the given name.
2062 *
2063 * \param[in] my_name The name of the node block to get.
2064 * \returns The node block, or nullptr if not found.
2065 */
2066 NodeBlock *Region::get_node_block(const std::string &my_name) const
2067 {
2068 const std::string db_name = get_alias_nl(my_name, NODEBLOCK);
2069 unsigned int db_hash = Ioss::Utils::hash(db_name);
2070
2071 NodeBlock *ge = nullptr;
2072 for (const auto &nb : nodeBlocks) {
2073 if (db_hash == nb->hash() && nb->name() == db_name) {
2074 ge = nb;
2075 break;
2076 }
2077 }
2078 return ge;
2079 }
2080
2081 /** \brief Get the edge block with the given name.
2082 *
2083 * \param[in] my_name The name of the edge block to get.
2084 * \returns The edge block, or nullptr if not found.
2085 */
2086 EdgeBlock *Region::get_edge_block(const std::string &my_name) const
2087 {
2088 const std::string db_name = get_alias_nl(my_name, EDGEBLOCK);
2089 unsigned int db_hash = Ioss::Utils::hash(db_name);
2090
2091 EdgeBlock *ge = nullptr;
2092 for (const auto &eb : edgeBlocks) {
2093 if (db_hash == eb->hash() && eb->name() == db_name) {
2094 ge = eb;
2095 break;
2096 }
2097 }
2098 return ge;
2099 }
2100
2101 /** \brief Get the face block with the given name.
2102 *
2103 * \param[in] my_name The name of the face block to get.
2104 * \returns The face block, or nullptr if not found.
2105 */
2106 FaceBlock *Region::get_face_block(const std::string &my_name) const
2107 {
2108 const std::string db_name = get_alias_nl(my_name, FACEBLOCK);
2109 unsigned int db_hash = Ioss::Utils::hash(db_name);
2110
2111 FaceBlock *ge = nullptr;
2112 for (const auto &fb : faceBlocks) {
2113 if (db_hash == fb->hash() && fb->name() == db_name) {
2114 ge = fb;
2115 break;
2116 }
2117 }
2118 return ge;
2119 }
2120
2121 /** \brief Get the element block with the given name.
2122 *
2123 * \param[in] my_name The name of the element block to get.
2124 * \returns The element block, or nullptr if not found.
2125 */
2126 ElementBlock *Region::get_element_block(const std::string &my_name) const
2127 {
2128 const std::string db_name = get_alias_nl(my_name, ELEMENTBLOCK);
2129 unsigned int db_hash = Ioss::Utils::hash(db_name);
2130
2131 ElementBlock *ge = nullptr;
2132 for (const auto &eb : elementBlocks) {
2133 if (db_hash == eb->hash() && eb->name() == db_name) {
2134 ge = eb;
2135 break;
2136 }
2137 }
2138 return ge;
2139 }
2140
2141 /** \brief Get the structured block with the given name.
2142 *
2143 * \param[in] my_name The name of the structured block to get.
2144 * \returns The structured block, or nullptr if not found.
2145 */
2146 StructuredBlock *Region::get_structured_block(const std::string &my_name) const
2147 {
2148 const std::string db_name = get_alias_nl(my_name, STRUCTUREDBLOCK);
2149 unsigned int db_hash = Ioss::Utils::hash(db_name);
2150
2151 StructuredBlock *ge = nullptr;
2152 for (const auto &sb : structuredBlocks) {
2153 if (db_hash == sb->hash() && sb->name() == db_name) {
2154 ge = sb;
2155 break;
2156 }
2157 }
2158 return ge;
2159 }
2160
2161 /** \brief Get the side set with the given name.
2162 *
2163 * \param[in] my_name The name of the side set to get.
2164 * \returns The side set, or nullptr if not found.
2165 */
2166 SideSet *Region::get_sideset(const std::string &my_name) const
2167 {
2168 const std::string db_name = get_alias_nl(my_name, SIDESET);
2169 unsigned int db_hash = Ioss::Utils::hash(db_name);
2170
2171 SideSet *ge = nullptr;
2172 for (const auto &ss : sideSets) {
2173 if (db_hash == ss->hash() && ss->name() == db_name) {
2174 ge = ss;
2175 break;
2176 }
2177 }
2178 return ge;
2179 }
2180
2181 /** \brief Get the side block with the given name.
2182 *
2183 * \param[in] my_name The name of the side block to get.
2184 * \returns The side block, or nullptr if not found.
2185 */
2186 SideBlock *Region::get_sideblock(const std::string &my_name) const
2187 {
2188 SideBlock *ge = nullptr;
2189 for (const auto &ss : sideSets) {
2190 ge = ss->get_side_block(my_name);
2191 if (ge != nullptr) {
2192 break;
2193 }
2194 }
2195 return ge;
2196 }
2197
2198 /** \brief Get the node set with the given name.
2199 *
2200 * \param[in] my_name The name of the node set to get.
2201 * \returns The node set, or nullptr if not found.
2202 */
2203 NodeSet *Region::get_nodeset(const std::string &my_name) const
2204 {
2205 const std::string db_name = get_alias_nl(my_name, NODESET);
2206 unsigned int db_hash = Ioss::Utils::hash(db_name);
2207
2208 NodeSet *ge = nullptr;
2209 for (const auto &ns : nodeSets) {
2210 if (db_hash == ns->hash() && ns->name() == db_name) {
2211 ge = ns;
2212 break;
2213 }
2214 }
2215 return ge;
2216 }
2217
2218 /** \brief Get the edge set with the given name.
2219 *
2220 * \param[in] my_name The name of the edge set to get.
2221 * \returns The edge set, or nullptr if not found.
2222 */
2223 EdgeSet *Region::get_edgeset(const std::string &my_name) const
2224 {
2225 const std::string db_name = get_alias_nl(my_name, EDGESET);
2226 unsigned int db_hash = Ioss::Utils::hash(db_name);
2227
2228 EdgeSet *ge = nullptr;
2229 for (const auto &es : edgeSets) {
2230 if (db_hash == es->hash() && es->name() == db_name) {
2231 ge = es;
2232 break;
2233 }
2234 }
2235 return ge;
2236 }
2237
2238 /** \brief Get the face set with the given name.
2239 *
2240 * \param[in] my_name The name of the face set to get.
2241 * \returns The face set, or nullptr if not found.
2242 */
2243 FaceSet *Region::get_faceset(const std::string &my_name) const
2244 {
2245 const std::string db_name = get_alias_nl(my_name, FACESET);
2246 unsigned int db_hash = Ioss::Utils::hash(db_name);
2247
2248 FaceSet *ge = nullptr;
2249 for (const auto &fs : faceSets) {
2250 if (db_hash == fs->hash() && fs->name() == db_name) {
2251 ge = fs;
2252 break;
2253 }
2254 }
2255 return ge;
2256 }
2257
2258 /** \brief Get the element set with the given name.
2259 *
2260 * \param[in] my_name The name of the element set to get.
2261 * \returns The element set, or nullptr if not found.
2262 */
2263 ElementSet *Region::get_elementset(const std::string &my_name) const
2264 {
2265 const std::string db_name = get_alias_nl(my_name, ELEMENTSET);
2266 unsigned int db_hash = Ioss::Utils::hash(db_name);
2267
2268 ElementSet *ge = nullptr;
2269 for (const auto &es : elementSets) {
2270 if (db_hash == es->hash() && es->name() == db_name) {
2271 ge = es;
2272 break;
2273 }
2274 }
2275 return ge;
2276 }
2277
2278 /** \brief Get the comm set with the given name.
2279 *
2280 * \param[in] my_name The name of the comm set to get.
2281 * \returns The comm set, or nullptr if not found.
2282 */
2283 CommSet *Region::get_commset(const std::string &my_name) const
2284 {
2285 const std::string db_name = get_alias_nl(my_name, COMMSET);
2286 unsigned int db_hash = Ioss::Utils::hash(db_name);
2287
2288 CommSet *ge = nullptr;
2289 for (const auto &cs : commSets) {
2290 if (db_hash == cs->hash() && cs->name() == db_name) {
2291 ge = cs;
2292 break;
2293 }
2294 }
2295 return ge;
2296 }
2297
2298 /** \brief Get the coordinate frame with the given id
2299 *
2300 * \param[in] id The id of the coordinate frame to get.
2301 * \returns The coordinate frame, or nullptr if not found.
2302 */
2303 const CoordinateFrame &Region::get_coordinate_frame(int64_t id) const
2304 {
2305 for (const auto &coor_frame : coordinateFrames) {
2306 if (coor_frame.id() == id) {
2307 return coor_frame;
2308 }
2309 }
2310 IOSS_ERROR(fmt::format("Error: Invalid id {} specified for coordinate frame.", id));
2311 }
2312
2313 /** \brief Determine whether the entity with the given name and type exists.
2314 *
2315 * \param[in] my_name The name of the entity to search for.
2316 * \param[in] io_type The type of the entity.
2317 * \param[out] my_type A string representing the type if the entity is found.
2318 * "INVALID" if the entity is not found or the type is invalid.
2319 * \returns True if the type is valid and the entity is found.
2320 */
2321 bool Region::is_valid_io_entity(const std::string &my_name, unsigned int io_type,
2322 std::string *my_type) const
2323 {
2324 // Search all entities defined on this region for the name 'my_name'.
2325 // If found, then set 'type' (if non-nullptr) to the type of the entity
2326 // (the 'type' values are from client code that was developed prior
2327 // to this function, so they are somewhat exodusII specific...).
2328 if (((io_type & NODEBLOCK) != 0U) && get_node_block(my_name) != nullptr) {
2329 if (my_type != nullptr) {
2330 *my_type = "NODE_BLOCK";
2331 }
2332 return true;
2333 }
2334 if (((io_type & ASSEMBLY) != 0U) && get_assembly(my_name) != nullptr) {
2335 if (my_type != nullptr) {
2336 *my_type = "ASSEMBLY";
2337 }
2338 return true;
2339 }
2340 if (((io_type & BLOB) != 0U) && get_blob(my_name) != nullptr) {
2341 if (my_type != nullptr) {
2342 *my_type = "BLOB";
2343 }
2344 return true;
2345 }
2346 if (((io_type & EDGEBLOCK) != 0U) && get_edge_block(my_name) != nullptr) {
2347 if (my_type != nullptr) {
2348 *my_type = "EDGE_BLOCK";
2349 }
2350 return true;
2351 }
2352 if (((io_type & FACEBLOCK) != 0U) && get_face_block(my_name) != nullptr) {
2353 if (my_type != nullptr) {
2354 *my_type = "FACE_BLOCK";
2355 }
2356 return true;
2357 }
2358 if (((io_type & ELEMENTBLOCK) != 0U) && get_element_block(my_name) != nullptr) {
2359 if (my_type != nullptr) {
2360 *my_type = "ELEMENT_BLOCK";
2361 }
2362 return true;
2363 }
2364 if (((io_type & STRUCTUREDBLOCK) != 0U) && get_structured_block(my_name) != nullptr) {
2365 if (my_type != nullptr) {
2366 *my_type = "STRUCTURED_BLOCK";
2367 }
2368 return true;
2369 }
2370 if (((io_type & SIDESET) != 0U) && get_sideset(my_name) != nullptr) {
2371 if (my_type != nullptr) {
2372 *my_type = "SURFACE";
2373 }
2374 return true;
2375 }
2376 if (((io_type & NODESET) != 0U) && get_nodeset(my_name) != nullptr) {
2377 if (my_type != nullptr) {
2378 *my_type = "NODESET";
2379 }
2380 return true;
2381 }
2382 if (((io_type & EDGESET) != 0U) && get_edgeset(my_name) != nullptr) {
2383 if (my_type != nullptr) {
2384 *my_type = "EDGESET";
2385 }
2386 return true;
2387 }
2388 if (((io_type & FACESET) != 0U) && get_faceset(my_name) != nullptr) {
2389 if (my_type != nullptr) {
2390 *my_type = "FACESET";
2391 }
2392 return true;
2393 }
2394 if (((io_type & ELEMENTSET) != 0U) && get_elementset(my_name) != nullptr) {
2395 if (my_type != nullptr) {
2396 *my_type = "ELEMENTSET";
2397 }
2398 return true;
2399 }
2400 if (((io_type & COMMSET) != 0U) && get_commset(my_name) != nullptr) {
2401 if (my_type != nullptr) {
2402 *my_type = "COMMSET";
2403 }
2404 return true;
2405 }
2406 if (my_type != nullptr) {
2407 *my_type = "INVALID";
2408 }
2409 return false;
2410 }
2411
2412 /** \brief Get the element block containing a specified element.
2413 *
2414 * \param[in] local_id The local database id (1-based), not the global id.
2415 * \returns The element block, or nullptr if no element block contains this
2416 * element (local_id <= 0 or greater than number of elements in database)
2417 */
2418 ElementBlock *Region::get_element_block(size_t local_id) const
2419 {
2420 IOSS_FUNC_ENTER(m_);
2421 for (const auto &eb : elementBlocks) {
2422 if (eb->contains(local_id)) {
2423 return eb;
2424 }
2425 }
2426 // Should not reach this point...
2427 IOSS_ERROR(fmt::format(
2428 "ERROR: In Ioss::Region::get_element_block, an invalid local_id of {} is specified. "
2429 " The valid range is 1 to {}",
2430 local_id, get_implicit_property("element_count").get_int()));
2431 }
2432
2433 /** \brief Get the structured block containing a specified global-offset-node.
2434 *
2435 * \param[in] global_offset The offset of cell-nodes for all blocks; 0-based.
2436 * \returns The structured block, or nullptr if no structured block contains this
2437 * node (local_id <= 0 or greater than number of cell-nodes in database)
2438 */
2439 StructuredBlock *Region::get_structured_block(size_t global_offset) const
2440 {
2441 IOSS_FUNC_ENTER(m_);
2442 for (const auto &sb : structuredBlocks) {
2443 if (sb->contains_node(global_offset)) {
2444 return sb;
2445 }
2446 }
2447 // Should not reach this point...
2448 IOSS_ERROR(fmt::format(
2449 "ERROR: In Ioss::Region::get_structured_block, an invalid global_offset of {} is "
2450 "specified.",
2451 global_offset));
2452 }
2453
2454 /** \brief Get an implicit property -- These are calculated from data stored
2455 * in the grouping entity instead of having an explicit value assigned.
2456 *
2457 * An example would be 'element_block_count' for a region.
2458 *
2459 * \param[in] my_name The property name.
2460 * \returns The property.
2461 */
2462 Property Region::get_implicit_property(const std::string &my_name) const
2463 {
2464 if (my_name == "spatial_dimension") {
2465 if (!nodeBlocks.empty()) {
2466 return nodeBlocks[0]->get_property("component_degree");
2467 }
2468
2469 return {my_name, 0};
2470 }
2471
2472 if (my_name == "node_block_count") {
2473 return {my_name, static_cast<int>(nodeBlocks.size())};
2474 }
2475
2476 if (my_name == "edge_block_count") {
2477 return {my_name, static_cast<int>(edgeBlocks.size())};
2478 }
2479
2480 if (my_name == "face_block_count") {
2481 return {my_name, static_cast<int>(faceBlocks.size())};
2482 }
2483
2484 if (my_name == "element_block_count") {
2485 return {my_name, static_cast<int>(elementBlocks.size())};
2486 }
2487
2488 if (my_name == "structured_block_count") {
2489 return {my_name, static_cast<int>(structuredBlocks.size())};
2490 }
2491
2492 if (my_name == "assembly_count") {
2493 return {my_name, static_cast<int>(assemblies.size())};
2494 }
2495
2496 if (my_name == "blob_count") {
2497 return {my_name, static_cast<int>(blobs.size())};
2498 }
2499
2500 if (my_name == "side_set_count") {
2501 return {my_name, static_cast<int>(sideSets.size())};
2502 }
2503
2504 if (my_name == "node_set_count") {
2505 return {my_name, static_cast<int>(nodeSets.size())};
2506 }
2507
2508 if (my_name == "edge_set_count") {
2509 return {my_name, static_cast<int>(edgeSets.size())};
2510 }
2511
2512 if (my_name == "face_set_count") {
2513 return {my_name, static_cast<int>(faceSets.size())};
2514 }
2515
2516 if (my_name == "element_set_count") {
2517 return {my_name, static_cast<int>(elementSets.size())};
2518 }
2519
2520 if (my_name == "comm_set_count") {
2521 return {my_name, static_cast<int>(commSets.size())};
2522 }
2523
2524 if (my_name == "coordinate_frame_count") {
2525 return {my_name, static_cast<int>(coordinateFrames.size())};
2526 }
2527
2528 if (my_name == "state_count") {
2529 return {my_name, stateCount};
2530 }
2531
2532 if (my_name == "current_state") {
2533 return {my_name, currentState};
2534 }
2535
2536 if (my_name == "element_count") {
2537 int64_t count = 0;
2538 for (const auto &eb : elementBlocks) {
2539 count += eb->entity_count();
2540 }
2541 return {my_name, count};
2542 }
2543
2544 if (my_name == "cell_count") {
2545 int64_t count = 0;
2546 for (const auto &eb : structuredBlocks) {
2547 count += eb->get_property("cell_count").get_int();
2548 }
2549 return {my_name, count};
2550 }
2551
2552 if (my_name == "face_count") {
2553 int64_t count = 0;
2554 for (const auto &fb : faceBlocks) {
2555 count += fb->entity_count();
2556 }
2557 return {my_name, count};
2558 }
2559
2560 if (my_name == "edge_count") {
2561 int64_t count = 0;
2562 for (const auto &eb : edgeBlocks) {
2563 count += eb->entity_count();
2564 }
2565 return {my_name, count};
2566 }
2567
2568 if (my_name == "node_count") {
2569 int64_t count = 0;
2570 for (const auto &nb : nodeBlocks) {
2571 count += nb->entity_count();
2572 }
2573 return {my_name, count};
2574 }
2575
2576 if (my_name == "database_name") {
2577 std::string filename = get_database()->get_filename();
2578 return {my_name, filename};
2579 }
2580
2581 {
2582 return GroupingEntity::get_implicit_property(my_name);
2583 }
2584 }
2585
2586 int64_t Region::internal_get_field_data(const Field &field, void *data, size_t data_size) const
2587 {
2588 return get_database()->get_field(this, field, data, data_size);
2589 }
2590
2591 int64_t Region::internal_put_field_data(const Field &field, void *data, size_t data_size) const
2592 {
2593 return get_database()->put_field(this, field, data, data_size);
2594 }
2595
2596 int64_t Region::internal_get_zc_field_data(const Field &field, void **data,
2597 size_t *data_size) const
2598 {
2599 return get_database()->get_zc_field(this, field, data, data_size);
2600 }
2601
2602 /** \brief Transfer all relevant aliases from this region to another region
2603 *
2604 * \param[in] to The region to which the aliases are to be transferred.
2605 */
2606 void Region::transfer_mesh_aliases(Region *to) const
2607 {
2608 IOSS_FUNC_ENTER(m_);
2609 // Iterate through list, [ returns <alias, base_entity_name> ], if
2610 // 'base_entity_name' is defined on the restart file, add 'alias' as
2611 // an alias for it...
2612 for (const auto &alias_map : aliases_) {
2613 for (const auto &alias_pair : alias_map.second) {
2614 std::string alias = alias_pair.first;
2615 std::string base = alias_pair.second;
2616 if (alias != base && to->get_entity(base) != nullptr) {
2617 to->add_alias_nl(base, alias, alias_map.first);
2618 }
2619 }
2620 }
2621 }
2622
2623 /** \brief Ensure that the restart and results files have the same ids.
2624 *
2625 * There is very little connection between an input (mesh) database
2626 * and an output (results/restart) database. Basically, the entity
2627 * names are the same between the two files. This works fine in the
2628 * case that an input database has 'generated' entity names of the
2629 * form 'block_10' or 'surface_32' since then the output database
2630 * can de-generate or decode the name and infer that the block
2631 * should have an id of 10 and the surface an id of 32.
2632 *
2633 * However, if alias or other renaming happens, then the output
2634 * block may have a name of the form 'fireset' and the underlying
2635 * database cannot infer that the id of the block should be 10.
2636 * Instead, it sets the id to an arbitrary number (1,2,...). This
2637 * is annoying in the case of the results file since there is no
2638 * correspondence between the mesh numbering and the results
2639 * numbering. In the case of the restart output file, it can be
2640 * disastrous since when the file is used to restart the analysis,
2641 * there is no match between the mesh blocks and those found on the
2642 * restart file and the restart fails.
2643 *
2644 * So... We need to somehow ensure that the restart (and results)
2645 * files have the same ids. To do this, we do the following:
2646 *
2647 * 1. The mesh database will set the property 'id' on input.
2648 *
2649 * 2. The results/restart files will have a 'name' based on either
2650 * the true name or an alias name. Whichever, that alias will
2651 * appear on the mesh database also, so we can query the mesh
2652 * database aliases to get the entity.
2653 *
2654 * 3. Once we have the entity, we can query the 'id' property and
2655 * add the same 'id' property to the results/restart database.
2656 * 4. Also set the 'name' property to the base 'name' on the output file.
2657 *
2658 * 5. Note that a property may already exist and must be removed
2659 * before the 'correct' value is set.
2660 */
2661 void Region::synchronize_id_and_name(const Region *from, bool sync_attribute_field_names)
2662 {
2663 for (const auto &alias_map : aliases_) {
2664 for (const auto &alias_pair : alias_map.second) {
2665 std::string alias = alias_pair.first;
2666 std::string base = alias_pair.second;
2667
2668 if (alias == base) {
2669
2670 // Query the 'from' database to get the entity (if any) referred
2671 // to by the 'alias'
2672 GroupingEntity *ge = from->get_entity(base);
2673
2674 if (ge != nullptr) {
2675 // Get the entity from this region... Must be non-nullptr
2676 GroupingEntity *this_ge = get_entity(base);
2677 if (this_ge == nullptr) {
2678 IOSS_ERROR(fmt::format(
2679 "INTERNAL ERROR: Could not find entity '{}' in synchronize_id_and_name() "
2680 " [{}]\n",
2681 base, get_database()->get_filename()));
2682 }
2683
2684 // See if there is an 'id' property...
2685 if (ge->property_exists(id_str())) {
2686 int64_t id = ge->get_property(id_str()).get_int();
2687 this_ge->property_update(id_str(), id);
2688 }
2689 else {
2690 // No id, make sure the base name matches in both databases...
2691 // There is always a 'name' property on an entity
2692 if (this_ge->name() != base) {
2693 this_ge->set_name(base);
2694 }
2695 }
2696
2697 // See if there is an 'db_name' property...
2698 if (ge->property_exists(db_name_str())) {
2699 std::string db_name = ge->get_property(db_name_str()).get_string();
2700 // Set the new property
2701 this_ge->property_update(db_name_str(), db_name);
2702 }
2703
2704 // See if there is a 'original_topology_type' property...
2705 if (ge->property_exists(orig_topo_str())) {
2706 std::string oes = ge->get_property(orig_topo_str()).get_string();
2707 this_ge->property_update(orig_topo_str(), oes);
2708 }
2709
2710 // Specific to entity blocks. Transfer the "original_block_order"
2711 // property.
2712 if (ge->property_exists(orig_block_order())) {
2713 int64_t offset = ge->get_property(orig_block_order()).get_int();
2714 this_ge->property_update(orig_block_order(), offset);
2715 }
2716
2717 if (sync_attribute_field_names) {
2718 // If there are any attribute fields, then copy those over
2719 // to the new entity in order to maintain the same order
2720 // since some codes access attributes by implicit order and
2721 // not name... (typically, element blocks only)
2722 size_t count = this_ge->entity_count();
2723
2724 Ioss::NameList attr_fields = ge->field_describe(Ioss::Field::ATTRIBUTE);
2725 for (auto &field_name : attr_fields) {
2726 const Ioss::Field &field = ge->get_fieldref(field_name);
2727 if (this_ge->field_exists(field_name)) {
2728 // If the field is already defined on the entity, make
2729 // sure that the attribute index matches...
2730 size_t index = field.get_index();
2731 const Ioss::Field &this_field = this_ge->get_fieldref(field_name);
2732 this_field.set_index(index);
2733 }
2734 else {
2735 // If the field does not already exist, add it to the
2736 // output node block
2737 if (field.raw_count() != count) {
2738 Ioss::Field new_field(field);
2739 new_field.reset_count(count);
2740 this_ge->field_add(new_field);
2741 }
2742 else {
2743 this_ge->field_add(field);
2744 }
2745 }
2746 }
2747 }
2748 }
2749 }
2750 }
2751 }
2752
2753 for (const auto &alias_map : aliases_) {
2754 for (const auto &alias_pair : alias_map.second) {
2755 std::string alias = alias_pair.first;
2756 std::string base = alias_pair.second;
2757
2758 if (alias != base) {
2759 GroupingEntity *ge = get_entity(base);
2760 if (ge != nullptr) {
2761 add_alias_nl(base, alias, alias_map.first);
2762 }
2763 }
2764 }
2765 }
2766 }
2767
2768 void Region::check_for_duplicate_names(const Ioss::GroupingEntity *entity) const
2769 {
2770 const std::string &name = entity->name();
2771
2772 // See if any alias with this name...
2773 std::string alias = get_alias_nl(name, entity->type());
2774
2775 if (!alias.empty()) {
2776 // There is an entity with this name...
2777 const Ioss::GroupingEntity *old_ge = get_entity(name);
2778
2779 if (old_ge != nullptr &&
2780 !(old_ge->type() == Ioss::SIDEBLOCK || old_ge->type() == Ioss::SIDESET)) {
2781 std::string filename = get_database()->get_filename();
2782 int64_t id1 = entity->get_optional_property(id_str(), 0);
2783 int64_t id2 = old_ge->get_optional_property(id_str(), 0);
2784 IOSS_ERROR(fmt::format(
2785 "ERROR: There are multiple blocks, sets, assemblies, and/or blobs with the same "
2786 "name defined in the "
2787 "database file '{}'.\n"
2788 "\tBoth {} {} and {} {} are named '{}'. All names must be unique.",
2789 filename, entity->type_string(), id1, old_ge->type_string(), id2, name));
2790 }
2791 }
2792 }
2793
2794 void
2795 Region::register_mesh_modification_observer(std::shared_ptr<DynamicTopologyObserver> observer)
2796 {
2797 if (observer) {
2798 if (observer->get_control_option() == FileControlOption::CONTROL_AUTO_GROUP_FILE) {
2799 const Ioss::PropertyManager &db_properties = get_database()->get_property_manager();
2800 if (!db_properties.exists("ENABLE_FILE_GROUPS")) {
2801 IOSS_ERROR(fmt::format("ERROR: File groups are not enabled in the database file '{}'.\n",
2802 get_database()->get_filename()));
2803 }
2804 }
2805
2806 topologyObserver = observer;
2807 topologyObserver->register_region(this);
2808 }
2809 }
2810
2811 void Region::start_new_output_database_entry(int steps)
2812 {
2813 if (get_database()->is_input())
2814 return;
2815
2816 if (!topologyObserver)
2817 return;
2818
2819 switch (topologyObserver->get_control_option()) {
2820 case FileControlOption::CONTROL_AUTO_MULTI_FILE:
2821 clone_and_replace_output_database(steps);
2822 break;
2823 case FileControlOption::CONTROL_AUTO_GROUP_FILE: add_output_database_change_set(steps); break;
2824 case FileControlOption::CONTROL_NONE:
2825 default: return; break;
2826 }
2827 }
2828
2829 void Region::add_output_database_change_set(int steps, bool force_addition)
2830 {
2831 if (get_database()->is_input())
2832 return;
2833
2834 const Ioss::PropertyManager &db_properties = get_database()->get_property_manager();
2835 if (!db_properties.exists("ENABLE_FILE_GROUPS")) {
2836 IOSS_ERROR(fmt::format("ERROR: File groups are not enabled in the database file '{}'.\n",
2837 get_database()->get_filename()));
2838 }
2839
2840 if (topologyObserver &&
2841 (topologyObserver->get_control_option() == FileControlOption::CONTROL_AUTO_MULTI_FILE)) {
2842 IOSS_ERROR(fmt::format(
2843 "ERROR: TopologyObserver for database file '{}' does not support file groups.\n",
2844 get_database()->get_filename()));
2845 }
2846
2847 int state = steps;
2848 if ((topologyObserver && topologyObserver->is_topology_modified()) || force_addition) {
2849 // Determine how many steps have been written already...
2850 state = get_property("state_count").get_int();
2851
2852 if (state == 0)
2853 state = steps;
2854
2855 // See if this is a continuation database...
2856 if (property_exists("state_offset"))
2857 state += get_property("state_offset").get_int();
2858
2859 state++; // For the state we are going to write.
2860
2861 if (topologyObserver) {
2862 topologyObserver->initialize_region();
2863 }
2864
2865 DynamicTopologyFileControl fileControl(this);
2866 fileControl.add_output_database_change_set(state);
2867
2868 // Reset based on fileControl values
2869 dbChangeCount = fileControl.get_topology_change_count();
2870 ifDatabaseExists = fileControl.get_if_database_exists_behavior();
2871 }
2872 }
2873
2874 void Region::clone_and_replace_output_database(int steps)
2875 {
2876 if (get_database()->is_input())
2877 return;
2878
2879 if (!topologyObserver)
2880 return;
2881
2882 int state = steps;
2883 if (topologyObserver->is_topology_modified() || fileCyclicCount > 0) {
2884 // Determine how many steps have been written already...
2885 state = get_property("state_count").get_int();
2886
2887 // Needed for automatic restart... The current database has not
2888 // been written to, but we want to open a new one instead of
2889 // (possibly) overwriting the current one...
2890 // If this is not an automatic restart, then we don't need a new database...
2891 if (state == 0 && topologyObserver->is_automatic_restart())
2892 return;
2893
2894 if (topologyObserver->is_automatic_restart() && ifDatabaseExists == Ioss::DB_APPEND)
2895 return;
2896
2897 if (state == 0)
2898 state = steps;
2899
2900 // See if this is a continuation database...
2901 if (property_exists("state_offset"))
2902 state += get_property("state_offset").get_int();
2903
2904 state++; // For the state we are going to write.
2905
2906 topologyObserver->initialize_region();
2907
2908 DynamicTopologyFileControl fileControl(this);
2909 fileControl.clone_and_replace_output_database(state);
2910
2911 // Reset based on fileControl values
2912 dbChangeCount = fileControl.get_topology_change_count();
2913 ifDatabaseExists = fileControl.get_if_database_exists_behavior();
2914 }
2915 }
2916
2917 void Region::reset_topology_modification()
2918 {
2919 if (topologyObserver) {
2920 topologyObserver->reset_topology_modification();
2921 }
2922 }
2923
2924 void Region::set_topology_modification(unsigned int type)
2925 {
2926 if (topologyObserver) {
2927 topologyObserver->set_topology_modification(type);
2928 }
2929 }
2930
2931 unsigned int Region::get_topology_modification() const
2932 {
2933 if (topologyObserver) {
2934 return topologyObserver->get_topology_modification();
2935 }
2936
2937 return TOPOLOGY_SAME;
2938 }
2939
2940 bool Region::load_internal_change_set_mesh(const std::string &set_name)
2941 {
2942 DatabaseIO *iodatabase = get_database();
2943
2944 if (!iodatabase->is_input())
2945 return false;
2946
2947 if (!iodatabase->open_internal_change_set(set_name))
2948 return false;
2949
2950 if (topologyObserver) {
2951 topologyObserver->initialize_region();
2952 }
2953 else {
2954 reset_region();
2955 }
2956
2957 iodatabase->release_memory();
2958
2959 Region::set_state(STATE_CLOSED);
2960 modelDefined = false;
2961 transientDefined = false;
2962
2963 Region::begin_mode(STATE_DEFINE_MODEL);
2964 iodatabase->read_meta_data();
2965 Region::end_mode(STATE_DEFINE_MODEL);
2966 if (iodatabase->open_create_behavior() != Ioss::DB_APPEND &&
2967 iodatabase->open_create_behavior() != Ioss::DB_MODIFY) {
2968 modelDefined = true;
2969 transientDefined = true;
2970 Region::begin_mode(STATE_READONLY);
2971 }
2972
2973 return true;
2974 }
2975
2976 bool Region::load_internal_change_set_mesh(const int child_group_index)
2977 {
2978 DatabaseIO *iodatabase = get_database();
2979
2980 if (!iodatabase->is_input())
2981 return false;
2982
2983 if (!iodatabase->open_internal_change_set(child_group_index))
2984 return false;
2985
2986 if (topologyObserver) {
2987 topologyObserver->initialize_region();
2988 }
2989 else {
2990 reset_region();
2991 }
2992
2993 iodatabase->release_memory();
2994
2995 Region::set_state(STATE_CLOSED);
2996 modelDefined = false;
2997 transientDefined = false;
2998
2999 Region::begin_mode(STATE_DEFINE_MODEL);
3000 iodatabase->read_meta_data();
3001 Region::end_mode(STATE_DEFINE_MODEL);
3002 if (iodatabase->open_create_behavior() != Ioss::DB_APPEND &&
3003 iodatabase->open_create_behavior() != Ioss::DB_MODIFY) {
3004 modelDefined = true;
3005 transientDefined = true;
3006 Region::begin_mode(STATE_READONLY);
3007 }
3008
3009 return true;
3010 }
3011
3012 void Region::update_dynamic_topology()
3013 {
3014 bool has_output_observer = topologyObserver && !get_database()->is_input();
3015 if (has_output_observer && topologyObserver->needs_new_output_file()) {
3016 if (topologyObserver->get_control_option() != FileControlOption::CONTROL_NONE) {
3017 int steps = get_property("state_count").get_int();
3018 start_new_output_database_entry(steps);
3019
3020 topologyObserver->define_model();
3021 topologyObserver->write_model();
3022 topologyObserver->define_transient();
3023 }
3024 topologyObserver->reset_topology_modification();
3025 }
3026 }
3027
3028 std::string Region::get_internal_change_set_name() const
3029 {
3030 return get_database()->get_internal_change_set_name();
3031 }
3032
3033 std::tuple<std::string, int, double> Region::locate_db_state(double targetTime) const
3034 {
3035 auto *cregion = const_cast<Region *>(this);
3036 DynamicTopologyStateLocator locator(cregion);
3037
3038 return locator.locate_db_state(targetTime);
3039 }
3040
3041 std::tuple<std::string, int, double> Region::get_db_max_time() const
3042 {
3043 IOSS_FUNC_ENTER(m_);
3044 auto db = get_database();
3045 if (!db->is_input() && db->usage() != WRITE_RESULTS && db->usage() != WRITE_RESTART) {
3046 return std::make_tuple(get_internal_change_set_name(), currentState, stateTimes[0]);
3047 }
3048
3049 auto *cregion = const_cast<Region *>(this);
3050 DynamicTopologyStateLocator locator(cregion);
3051
3052 return locator.get_db_max_time();
3053 }
3054
3055 std::tuple<std::string, int, double> Region::get_db_min_time() const
3056 {
3057 IOSS_FUNC_ENTER(m_);
3058 auto db = get_database();
3059 if (!db->is_input() && db->usage() != WRITE_RESULTS && db->usage() != WRITE_RESTART) {
3060 return std::make_tuple(get_internal_change_set_name(), currentState, stateTimes[0]);
3061 }
3062
3063 auto *cregion = const_cast<Region *>(this);
3064 DynamicTopologyStateLocator locator(cregion);
3065
3066 return locator.get_db_min_time();
3067 }
3068
3069 } // namespace Ioss