Debian Codegraph @

packages/seacas/libraries/ioss/src/adios/Ioad_DatabaseIO.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include <tokenize.h>
8
9 #include "Ioss_CodeTypes.h" // for HAVE_MPI
10 #include "Ioss_CommSet.h" // for CommSet
11 #include "Ioss_DBUsage.h" // for DatabaseUsage, etc
12 #include "Ioss_DatabaseIO.h" // for DatabaseIO
13 #include "Ioss_EdgeBlock.h" // for EdgeBlock
14 #include "Ioss_EdgeSet.h" // for EdgeSet
15 #include "Ioss_ElementBlock.h" // for ElementBlock
16 #include "Ioss_ElementSet.h" // for ElementSet
17 #include "Ioss_ElementTopology.h" // for NameList
18 #include "Ioss_EntityType.h" // for EntityType::ELEMENTBLOCK
19 #include "Ioss_FaceBlock.h" // for FaceBlock
20 #include "Ioss_FaceSet.h" // for FaceSet
21 #include "Ioss_FileInfo.h" // for FileInfo
22 #include "Ioss_Map.h" // for Map, MapContainer
23 #include "Ioss_NodeBlock.h" // for NodeBlock
24 #include "Ioss_NodeSet.h" // for NodeSet
25 #include "Ioss_ParallelUtils.h" // for ParallelUtils, etc
26 #include "Ioss_Property.h" // for Property
27 #include "Ioss_SerializeIO.h" // for SerializeIO
28 #include "Ioss_SideBlock.h" // for SideBlock
29 #include "Ioss_Utils.h" // for Utils, IOSS_ERROR, etc
30 #include <exodusII.h>
31
32 #include "adios/Ioad_Constants.h"
33 #include "adios/Ioad_Helper.h"
34 #include "adios/Ioad_TemplateToValue.h"
35
36 #include <climits>
37
38 #include "adios/Ioad_DatabaseIO.h"
39
40 namespace Ioss {
41 class PropertyManager;
42 }
43
44 namespace Ioad {
45
46 DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename,
47 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
48 const Ioss::PropertyManager &props)
49 : Ioss::DatabaseIO(region, filename, db_usage, communicator, props), rank(RankInit()),
50 adios_wrapper(communicator, filename, is_input(), rank, props)
51 {
52 dbState = Ioss::STATE_UNKNOWN;
53 // Always 64 bits
54 dbIntSizeAPI = Ioss::USE_INT64_API;
55 // Streaming initialization
56 is_streaming = adios_wrapper.IsStreaming();
57 previous_time_streaming = -1.0;
58 }
59
60 // Used to force `rank` initialization before creating `adios_wrapper`.
61 int DatabaseIO::RankInit()
62 {
63 Ioss::SerializeIO serializeIO_(this);
64 number_proc = Ioss::SerializeIO::getSize();
65 return Ioss::SerializeIO::getRank();
66 }
67
68 bool DatabaseIO::begin_nl(Ioss::State state)
69 {
70 // initialization
71 Ioss::Region *this_region = get_region();
72
73 // this_region->property_update("streaming_status", -1);
74
75 dbState = state;
76 if (state == Ioss::STATE_DEFINE_MODEL) {
77 // Should `BeginStep()` only be if (!is_input() || is_streaming) ???
78
79 adios2::StepStatus status = adios_wrapper.BeginStep();
80 this_region->property_update("streaming_status", static_cast<int>(status));
81 }
82 return true;
83 }
84
85 void DatabaseIO::write_properties(const Ioss::GroupingEntity *const entity,
86 const std::string &encoded_name)
87 {
88 // Write field properties
89 std::vector<std::string> property_list = properties_to_save(entity);
90 for (auto &property_name : property_list) {
91 Ioss::Property property = entity->get_property(property_name);
92
93 std::string variable_name = get_property_variable_name(property_name);
94 switch (property.get_type()) {
95 case Ioss::Property::BasicType::REAL:
96 adios_wrapper.PutMetaVariable<double>(variable_name, property.get_real(), encoded_name);
97 break;
98 case Ioss::Property::BasicType::INTEGER:
99 adios_wrapper.PutMetaVariable<int>(variable_name, property.get_int(), encoded_name);
100 break;
101 case Ioss::Property::BasicType::STRING:
102 adios_wrapper.PutMetaVariable<std::string>(variable_name, property.get_string(),
103 encoded_name);
104 break;
105 case Ioss::Property::BasicType::POINTER:
106 case Ioss::Property::BasicType::INVALID:
107 default:
108 // Do not save properties with an invalid type.
109 break;
110 }
111 }
112 }
113
114 // TODO: refactor and consolidate `write_meta_data_container` functions to avoid code duplication.
115 template <typename T> int64_t DatabaseIO::write_meta_data_container(const T &entity_blocks)
116 {
117 int64_t count = 0;
118 for (auto &entity_block : entity_blocks) {
119 count += entity_block->entity_count();
120 std::string entity_type = entity_block->type_string();
121 std::string entity_name = entity_block->name();
122 std::string encoded_name = encode_field_name({entity_type, entity_name});
123 write_properties(entity_block, encoded_name);
124 adios_wrapper.PutMetaVariable<std::string>(Topology_meta, entity_block->topology()->name(),
125 encoded_name);
126 }
127 return count;
128 }
129
130 template <>
131 int64_t DatabaseIO::write_meta_data_container<Ioss::CommSetContainer>(
132 const Ioss::CommSetContainer &entity_blocks)
133 {
134 for (auto &entity_block : entity_blocks) {
135 std::string entity_type = entity_block->type_string();
136 std::string entity_name = entity_block->name();
137 std::string encoded_name = encode_field_name({entity_type, entity_name});
138 write_properties(entity_block, encoded_name);
139 }
140 return 0;
141 }
142
143 std::pair<int64_t, int64_t>
144 DatabaseIO::write_meta_data_sideblockcontainer(const Ioss::SideBlockContainer &entity_blocks)
145 {
146 int64_t count = 0;
147 int64_t df_count = 0;
148
149 for (auto &entity_block : entity_blocks) {
150 count += entity_block->entity_count();
151 df_count += entity_block->get_property("distribution_factor_count").get_int();
152 std::string entity_type = entity_block->type_string();
153 std::string entity_name = entity_block->name();
154 std::string encoded_name = encode_field_name({entity_type, entity_name});
155 adios_wrapper.PutMetaVariable<std::string>(Topology_meta, entity_block->topology()->name(),
156 encoded_name);
157 adios_wrapper.PutMetaVariable<std::string>(
158 Parent_topology_meta, entity_block->parent_element_topology()->name(), encoded_name);
159 write_properties(entity_block, encoded_name);
160 }
161 return {count, df_count};
162 }
163
164 template <>
165 int64_t
166 DatabaseIO::write_meta_data_container<Ioss::SideSetContainer>(const Ioss::SideSetContainer &ssets)
167 {
168 int64_t count = 0;
169
170 for (auto &sset : ssets) {
171 count += sset->entity_count();
172 const std::string entity_type = sset->type_string();
173 const std::string entity_name = sset->name();
174 write_properties(sset, encode_field_name({entity_type, entity_name}));
175
176 // side blocks
177 const Ioss::SideBlockContainer &sblocks = sset->get_side_blocks();
178 std::string encoded_name = encode_sideblock_name(entity_type, entity_name);
179
180 std::string stringified_sblock_names = stringify_side_block_names(sblocks);
181 adios_wrapper.InquireAndPut<std::string>(encoded_name, &stringified_sblock_names);
182
183 int sblock_count = 0;
184 int df_count = 0;
185 std::tie(sblock_count, df_count) = write_meta_data_sideblockcontainer(sblocks);
186 Ioss::SideSet *new_entity = const_cast<Ioss::SideSet *>(sset);
187 new_entity->property_add(Ioss::Property("entity_count", sblock_count));
188 new_entity->property_add(Ioss::Property("distribution_factor_count", df_count));
189 }
190 return count;
191 }
192
193 std::string DatabaseIO::encoded_coordinate_frame_name(Ioss::CoordinateFrame coordinate_frame)
194 {
195 int64_t id = coordinate_frame.id();
196
197 char tag = coordinate_frame.tag();
198 std::string tag_str(1, tag);
199 return encode_field_name({coordinate_frame_name, std::to_string(id), tag_str});
200 }
201
202 template <>
203 int64_t DatabaseIO::write_meta_data_container<Ioss::CoordinateFrameContainer>(
204 const Ioss::CoordinateFrameContainer &coordinate_frames)
205 {
206 int64_t count = 0;
207 for (auto &coordinate_frame : coordinate_frames) {
208 std::string encoded_name = encoded_coordinate_frame_name(coordinate_frame);
209 adios_wrapper.InquireAndPut<double>(encoded_name, coordinate_frame.coordinates());
210 }
211 return count;
212 }
213
214 // Write data defined per block, not per field.
215 void DatabaseIO::write_meta_data()
216 {
217 Ioss::Region *region = get_region();
218 const Ioss::NodeBlockContainer &node_blocks = region->get_node_blocks();
219
220 assert(node_blocks.size() == 1);
221 spatialDimension = node_blocks[0]->get_property("component_degree").get_int();
222
223 // Region
224 write_properties(region, encode_field_name({region->type_string(), region_name}));
225 // Node blocks --
226 nodeCount = write_meta_data_container<Ioss::NodeBlockContainer>(node_blocks);
227 // Edge Blocks --
228 const Ioss::EdgeBlockContainer &edge_blocks = region->get_edge_blocks();
229 edgeCount = write_meta_data_container<Ioss::EdgeBlockContainer>(edge_blocks);
230 // Face Blocks --
231 const Ioss::FaceBlockContainer &face_blocks = region->get_face_blocks();
232 faceCount = write_meta_data_container<Ioss::FaceBlockContainer>(face_blocks);
233 // Element Blocks --
234 const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks();
235 elementCount = write_meta_data_container<Ioss::ElementBlockContainer>(element_blocks);
236 // Side Blocks ...
237 const Ioss::SideSetContainer &ssets = region->get_sidesets();
238 write_meta_data_container<Ioss::SideSetContainer>(ssets);
239 // Comm Sets --
240 const Ioss::CommSetContainer &comm_sets = region->get_commsets();
241 write_meta_data_container<Ioss::CommSetContainer>(comm_sets);
242 // Coordinate frames --
243 const Ioss::CoordinateFrameContainer &coordinate_frames = region->get_coordinate_frames();
244 write_meta_data_container<Ioss::CoordinateFrameContainer>(coordinate_frames);
245 // Global meta data
246 put_data<unsigned long>(static_cast<void *>(&rank), Processor_id_meta);
247 }
248
249 void DatabaseIO::check_model()
250 {
251 Ioss::Region *region = get_region();
252
253 // Add region missing properties.
254 if (!region->property_exists("title")) {
255 Ioss::Property title_property("title", "IOSS Default Output Title");
256 region->property_add(title_property);
257 }
258 }
259
260 bool DatabaseIO::end_nl(Ioss::State state)
261 {
262 // Transitioning out of state 'state'
263 assert(state == dbState);
264 switch (state) {
265 case Ioss::STATE_DEFINE_MODEL:
266 if (!is_input()) {
267 check_model();
268 define_model();
269 define_global_variables();
270 write_meta_data();
271 }
272 break;
273 case Ioss::STATE_DEFINE_TRANSIENT:
274 if (!is_input()) {
275 if (is_streaming) {
276 // If streaming, we need to write the meta data available at
277 // every step.
278 write_meta_data();
279 }
280 Ioss::Field::RoleType role = Ioss::Field::RoleType::TRANSIENT;
281 define_model(&role);
282 }
283 break;
284 default: // ignore everything else...
285 break;
286 }
287
288 {
289 dbState = Ioss::STATE_UNKNOWN;
290 }
291
292 return true;
293 }
294
295 bool DatabaseIO::begin_state_nl(int state, double time)
296 {
297 Ioss::Region *this_region = get_region();
298 if (!is_input()) {
299 // `BeginStep()` should not be used at the same time as random access. Since at read time,
300 // we currently read variables with random access, `BeginStep()` should not be used
301 // at read time.
302 // Begin step for transient data
303 adios2::StepStatus status = adios_wrapper.BeginStep();
304 this_region->property_update("streaming_status", static_cast<int>(status));
305
306 // Add time to adios
307 adios_wrapper.PutMetaVariable<double>(Time_meta, time / timeScaleFactor);
308 }
309 else {
310 if (is_streaming) {
311 // Begin step for transient data if streaming. Otherwise, data will be accessed with
312 // `SetStepSelection()`.
313 adios2::StepStatus status = adios_wrapper.BeginStep();
314 this_region->property_update("streaming_status", static_cast<int>(status));
315 }
316 }
317
318 return true;
319 }
320
321 // common
322 bool DatabaseIO::end_state_nl(int state, double time)
323 {
324 Ioss::Region *this_region = get_region();
325
326 // if (!is_input()) {
327 // End step for transient data
328 // End step only for writing. Streaming is closed in `BeginStep()` call if time is different
329 // from previous time.
330 // if (!is_streaming || !is_input()) {
331 adios_wrapper.EndStep();
332 //}
333 this_region->property_update("streaming_status", -1);
334
335 //}
336 return true;
337 }
338
339 void DatabaseIO::define_field_meta_variables(const std::string &encoded_name)
340 {
341 adios_wrapper.DefineMetaVariable<int>(Role_meta, encoded_name);
342 adios_wrapper.DefineMetaVariable<std::string>(Var_type_meta, encoded_name);
343 }
344
345 template <typename T, typename>
346 void DatabaseIO::define_entity_meta_variables(const std::string &encoded_name)
347 {
348 adios_wrapper.DefineMetaVariable<std::string>(Topology_meta, encoded_name);
349 }
350
351 template <typename T, typename, typename>
352 void DatabaseIO::define_entity_meta_variables(const std::string &encoded_name)
353 {
354 // no op
355 }
356
357 template <>
358 void DatabaseIO::define_entity_meta_variables<Ioss::SideBlock>(const std::string &encoded_name)
359 {
360 adios_wrapper.DefineMetaVariable<std::string>(Topology_meta, encoded_name);
361 adios_wrapper.DefineMetaVariable<std::string>(Parent_topology_meta, encoded_name);
362 }
363
364 template <typename T>
365 void DatabaseIO::define_model_internal(const Ioss::Field &field, const std::string &encoded_name,
366 const std::string &entity_type,
367 const std::string &field_name)
368 {
369 size_t component_count;
370 size_t local_size;
371
372 if (use_transformed_storage(field, entity_type, field_name)) {
373 component_count = field.transformed_storage()->component_count();
374 local_size = field.transformed_count();
375 }
376 else {
377 component_count = field.raw_storage()->component_count();
378 local_size = field.raw_count();
379 }
380 adios_wrapper.DefineVariable<T>(encoded_name, {number_proc, INT_MAX, component_count},
381 {rank, 0, 0}, {1, local_size, component_count});
382 }
383
384 void DatabaseIO::define_properties(const Ioss::GroupingEntity *const entity_block,
385 const std::string &encoded_entity_name)
386 {
387 std::vector<std::string> property_list = properties_to_save(entity_block);
388 for (auto &property_name : property_list) {
389 Ioss::Property property = entity_block->get_property(property_name);
390 std::string variable_name = get_property_variable_name(property_name);
391 switch (property.get_type()) {
392 case Ioss::Property::BasicType::REAL:
393 adios_wrapper.DefineMetaVariable<double>(variable_name, encoded_entity_name);
394 break;
395 case Ioss::Property::BasicType::INTEGER:
396 adios_wrapper.DefineMetaVariable<int>(variable_name, encoded_entity_name);
397 break;
398 case Ioss::Property::BasicType::STRING:
399 adios_wrapper.DefineMetaVariable<std::string>(variable_name, encoded_entity_name);
400 break;
401 case Ioss::Property::BasicType::POINTER:
402 case Ioss::Property::BasicType::INVALID:
403 default:
404 // Do not save properties with an invalid type.
405 break;
406 }
407 }
408 }
409
410 template <typename T>
411 void DatabaseIO::define_entity_internal(const T &entity_blocks, Ioss::Field::RoleType *role)
412 {
413 using cv_removed_value_type = typename std::remove_pointer_t<typename T::value_type>;
414 for (auto &entity_block : entity_blocks) {
415 std::string entity_type = entity_block->type_string();
416 std::string entity_name = entity_block->name();
417 if (!role) {
418 // Meta variables and properties are only defined if state is STATE_DEFINE_MODEL:
419 std::string encoded_entity_name = encode_field_name({entity_type, entity_name});
420 define_entity_meta_variables<cv_removed_value_type>(encoded_entity_name);
421 define_properties(entity_block, encoded_entity_name);
422 }
423 Ioss::NameList field_names = entity_block->field_describe();
424 for (auto &field_name : field_names) {
425 // Skip ignored fields
426 if (find_field_in_mapset(entity_type, field_name, Ignore_fields)) {
427 continue;
428 }
429 // Define entity block variables
430 auto field = entity_block->get_fieldref(field_name);
431 if (role && field.get_role() != *role) {
432 continue;
433 }
434 std::string encoded_name = encode_field_name({entity_type, entity_name, field_name});
435 switch (field.get_type()) {
436 // REAL and DOUBLE have same value. Code is left here for documentation purposes
437 // but commented out to compile.
438 // case Ioss::Field::BasicType::REAL:
439 case Ioss::Field::BasicType::DOUBLE:
440 define_model_internal<double>(field, encoded_name, entity_type, field_name);
441 break;
442 case Ioss::Field::BasicType::INT32:
443 define_model_internal<int32_t>(field, encoded_name, entity_type, field_name);
444 break;
445 case Ioss::Field::BasicType::INT64:
446 define_model_internal<int64_t>(field, encoded_name, entity_type, field_name);
447 break;
448 case Ioss::Field::BasicType::COMPLEX:
449 define_model_internal<Complex>(field, encoded_name, entity_type, field_name);
450 break;
451 case Ioss::Field::BasicType::CHARACTER:
452 define_model_internal<char>(field, encoded_name, entity_type, field_name);
453 break;
454 case Ioss::Field::BasicType::STRING:
455 adios_wrapper.DefineVariable<std::string>(
456 encoded_name,
457 // Global dimensions
458 {field.raw_count()},
459 // starting offset of the local array in the global space
460 {0},
461 // local size, could be defined later using SetSelection()
462 {field.raw_count()});
463 break;
464 default:
465 std::ostringstream errmsg;
466 errmsg
467 << "INTERNAL ERROR: Invalid field type. "
468 << "Something is wrong in the Ioad::DatabaseIO::define_entity_internal() function. "
469 << "Please report.\n";
470 IOSS_ERROR(errmsg);
471 }
472 define_field_meta_variables(encoded_name);
473 }
474 }
475 }
476
477 void DatabaseIO::define_coordinate_frames_internal(
478 const Ioss::CoordinateFrameContainer &coordinate_frames)
479 {
480 for (auto &coordinate_frame : coordinate_frames) {
481 std::string encoded_name = encoded_coordinate_frame_name(coordinate_frame);
482 adios_wrapper.DefineVariable<double>(encoded_name, {number_proc, 9, 1}, {rank, 0, 0},
483 {1, 9, 1});
484 }
485 }
486
487 // Similar to `write_meta_data()` function in other DatabaseIO. This function has been renamed in
488 // this database to reflect more precisely what it accomplishes.
489 void DatabaseIO::define_model(Ioss::Field::RoleType *role)
490 {
491
492 Ioss::Region *region = get_region();
493 const Ioss::NodeBlockContainer &node_blocks = region->get_node_blocks();
494
495 // A single nodeblock named "nodeblock_1" will be created for the mesh. It contains information
496 // for every node that exists in the model (Ioss-exodus-mapping.pdf).
497 assert(node_blocks.size() == 1);
498
499 if (!role) {
500 // Schema
501 adios_wrapper.DefineAttribute<unsigned int>(Schema_version_string, 1);
502 // Region
503 define_properties(region, encode_field_name({region->type_string(), region_name}));
504 }
505 // Node blocks --
506 define_entity_internal(node_blocks, role);
507 // Edge Blocks --
508 const Ioss::EdgeBlockContainer &edge_blocks = region->get_edge_blocks();
509 define_entity_internal(edge_blocks, role);
510 // Face Blocks --
511 const Ioss::FaceBlockContainer &face_blocks = region->get_face_blocks();
512 define_entity_internal(face_blocks, role);
513 // Element Blocks --
514 const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks();
515 define_entity_internal(element_blocks, role);
516 // Nodesets ...
517 const Ioss::NodeSetContainer &nodesets = region->get_nodesets();
518 define_entity_internal(nodesets, role);
519 // Edgesets ...
520 const Ioss::EdgeSetContainer &edgesets = region->get_edgesets();
521 define_entity_internal(edgesets, role);
522 // Facesets ...
523 const Ioss::FaceSetContainer &facesets = region->get_facesets();
524 define_entity_internal(facesets, role);
525 // Elementsets ...
526 const Ioss::ElementSetContainer &elementsets = region->get_elementsets();
527 define_entity_internal(elementsets, role);
528 // CommSets ...
529 const Ioss::CommSetContainer &csets = region->get_commsets();
530 define_entity_internal(csets, role);
531 // SideSets ...
532 const Ioss::SideSetContainer &ssets = region->get_sidesets();
533 define_entity_internal(ssets, role);
534 // SideBlocks ...
535 for (auto &sset : ssets) {
536 std::string encoded_name = encode_sideblock_name(sset->type_string(), sset->name());
537 const Ioss::SideBlockContainer &sblocks = sset->get_side_blocks();
538 adios_wrapper.DefineVariable<std::string>(encoded_name);
539 define_entity_internal(sblocks, role);
540 }
541 // Coordinate frames ...
542 const Ioss::CoordinateFrameContainer &coordinate_frames = region->get_coordinate_frames();
543 define_coordinate_frames_internal(coordinate_frames);
544 }
545
546 void DatabaseIO::define_global_variables()
547 {
548 adios_wrapper.DefineAttribute<double>(Time_scale_factor, timeScaleFactor);
549 adios_wrapper.DefineMetaVariable<double>(Time_meta);
550 adios_wrapper.DefineVariable<unsigned long>(Processor_id_meta, {number_proc}, {rank}, {1});
551 adios_wrapper.DefineAttribute<unsigned long>(Processor_number_meta, number_proc);
552 }
553
554 int64_t DatabaseIO::put_field_internal(const Ioss::Region *reg, const Ioss::Field &field,
555 void *data, size_t data_size) const
556 {
557 return put_field_internal_t(reg, field, data, data_size);
558 }
559
560 // Returns byte size of integers stored on the database...
561 int DatabaseIO::int_byte_size_db() const { return 8; }
562
563 unsigned DatabaseIO::entity_field_support() const
564 {
565 return Ioss::NODEBLOCK | Ioss::EDGEBLOCK | Ioss::FACEBLOCK | Ioss::ELEMENTBLOCK |
566 Ioss::NODESET | Ioss::EDGESET | Ioss::FACESET | Ioss::ELEMENTSET | Ioss::SIDESET |
567 Ioss::SIDEBLOCK | Ioss::REGION;
568 }
569
570 int64_t DatabaseIO::put_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
571 void *data, size_t data_size) const
572 {
573 return put_field_internal_t(nb, field, data, data_size);
574 }
575
576 template <typename T> void DatabaseIO::put_data(void *data, const std::string &encoded_name) const
577 {
578 T *rdata = static_cast<T *>(data);
579 adios_wrapper.InquireAndPut<T>(encoded_name, rdata);
580 }
581
582 void DatabaseIO::put_meta_variables(const std::string &encoded_name, const Ioss::Field &field,
583 const std::string &entity_type,
584 const std::string &field_name) const
585 {
586 adios_wrapper.PutMetaVariable<int>(Role_meta, field.get_role(), encoded_name);
587 bool transformed_field = use_transformed_storage(field, entity_type, field_name);
588 std::string var_type =
589 transformed_field ? field.transformed_storage()->name() : field.raw_storage()->name();
590 adios_wrapper.PutMetaVariable<std::string>(Var_type_meta, var_type, encoded_name);
591 }
592
593 template <typename T>
594 int64_t DatabaseIO::put_field_internal_t(T entity, const Ioss::Field &field, void *data,
595 size_t data_size) const
596 {
597 if (!data || !data_size) {
598 return 0;
599 }
600 std::string entity_type = entity->type_string();
601 const std::string &field_name = field.get_name();
602 if (find_field_in_mapset(entity_type, field_name, Ignore_fields)) {
603 return 0;
604 }
605
606 int num_to_get = field.verify(data_size);
607 if (num_to_get > 0) {
608 std::string encoded_name = encode_field_name({entity_type, entity->name(), field_name});
609 switch (field.get_type()) {
610 case Ioss::Field::BasicType::DOUBLE: put_data<double>(data, encoded_name); break;
611 case Ioss::Field::BasicType::INT32: put_data<int32_t>(data, encoded_name); break;
612 case Ioss::Field::BasicType::INT64: put_data<int64_t>(data, encoded_name); break;
613 case Ioss::Field::BasicType::COMPLEX: put_data<Complex>(data, encoded_name); break;
614 case Ioss::Field::BasicType::CHARACTER: put_data<char>(data, encoded_name); break;
615 default: {
616 std::ostringstream errmsg;
617 errmsg << "INTERNAL ERROR: Invalid field type. "
618 << "Something is wrong in the Ioad::DatabaseIO::put_field_internal_t() function. "
619 << "Please report.\n";
620 IOSS_ERROR(errmsg);
621 }
622 }
623 put_meta_variables(encoded_name, field, entity_type, field_name);
624 }
625 return num_to_get;
626 }
627
628 int64_t DatabaseIO::put_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
629 void *data, size_t data_size) const
630 {
631 return put_field_internal_t(eb, field, data, data_size);
632 }
633
634 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeBlock *eb, const Ioss::Field &field,
635 void *data, size_t data_size) const
636 {
637 return put_field_internal_t(eb, field, data, data_size);
638 }
639
640 int64_t DatabaseIO::put_field_internal(const Ioss::FaceBlock *fb, const Ioss::Field &field,
641 void *data, size_t data_size) const
642 {
643 return put_field_internal_t(fb, field, data, data_size);
644 }
645
646 int64_t DatabaseIO::put_field_internal(const Ioss::SideBlock *sb, const Ioss::Field &field,
647 void *data, size_t data_size) const
648 {
649 return put_field_internal_t(sb, field, data, data_size);
650 }
651
652 int64_t DatabaseIO::put_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
653 void *data, size_t data_size) const
654 {
655 return put_field_internal_t(ns, field, data, data_size);
656 }
657
658 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeSet *es, const Ioss::Field &field,
659 void *data, size_t data_size) const
660 {
661 return put_field_internal_t(es, field, data, data_size);
662 }
663
664 int64_t DatabaseIO::put_field_internal(const Ioss::FaceSet *fs, const Ioss::Field &field,
665 void *data, size_t data_size) const
666 {
667 return put_field_internal_t(fs, field, data, data_size);
668 }
669
670 int64_t DatabaseIO::put_field_internal(const Ioss::ElementSet *es, const Ioss::Field &field,
671 void *data, size_t data_size) const
672 {
673 return put_field_internal_t(es, field, data, data_size);
674 }
675
676 int64_t DatabaseIO::put_field_internal(const Ioss::SideSet *ss, const Ioss::Field &field,
677 void *data, size_t data_size) const
678 {
679 return put_field_internal_t(ss, field, data, data_size);
680 }
681
682 int64_t DatabaseIO::put_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
683 void *data, size_t data_size) const
684 {
685 // TODO: Make sure that Commset should be handled like a "set" and not like a "block".
686 return put_field_internal_t(cs, field, data, data_size);
687 }
688
689 template <typename T>
690 DatabaseIO::FieldInfoType DatabaseIO::get_expected_variable_infos_from_map(
691 const EntityMapType &entity_map, const std::string &entity_type,
692 const std::string &block_name, const std::string &var_name) const
693 {
694 if (entity_map.find(block_name) == entity_map.end()) {
695 std::ostringstream errmsg;
696 errmsg << "ERROR: block name " << block_name << " not found.\n";
697 IOSS_ERROR(errmsg);
698 }
699
700 if (entity_map.at(block_name).find(var_name) == entity_map.at(block_name).end()) {
701 std::ostringstream errmsg;
702 errmsg << "ERROR: block name not found or does not contain `" << var_name << "` field.\n";
703 IOSS_ERROR(errmsg);
704 }
705
706 if (entity_map.at(block_name).at(var_name).second != GetType<T>()) {
707 std::ostringstream errmsg;
708 errmsg << "ERROR: `" << var_name << "` field type does not match expected type.\n";
709 IOSS_ERROR(errmsg);
710 }
711 std::string variable = encode_field_name({entity_type, block_name, var_name});
712 return get_variable_infos<T>(variable);
713 }
714
715 DatabaseIO::FieldInfoType DatabaseIO::get_variable_infos_from_map(
716 const EntityMapType &entity_map, const std::string &entity_type,
717 const std::string &block_name, const std::string &var_name) const
718 {
719 // No check to verify that the block_name, entity_type, variable_name, and type exist as they
720 // have been extracted earlier from the file and everything should still be up to date.
721 std::string variable = encode_field_name({entity_type, block_name, var_name});
722 std::string type = entity_map.at(block_name).at(var_name).second;
723 // Simply to start the "else if" section with "if".
724 if (type == "not supported") {}
725 #define declare_template_instantiation(T) \
726 else if (type == GetType<T>()) \
727 { \
728 return get_variable_infos<T>(variable); \
729 }
730 ADIOS2_FOREACH_TYPE_1ARG(declare_template_instantiation)
731 #undef declare_template_instantiation
732 else
733 {
734 std::ostringstream errmsg;
735 errmsg << "INTERNAL ERROR: Invalid variable type. "
736 << "Something is wrong in the "
737 "Ioad::DatabaseIO::get_variable_infos_from_map() function."
738 << "Please report.\n";
739 IOSS_ERROR(errmsg);
740 }
741 return FieldInfoType();
742 }
743
744 template <>
745 int64_t DatabaseIO::get_entities<Ioss::NodeBlock>(const FieldsMapType &fields_map,
746 const FieldsMapType &properties_map)
747 {
748 const std::string block_type = get_entity_type<Ioss::NodeBlock>();
749 // For exodusII, there is only a single node block which contains
750 // all of the nodes.
751 // The default id assigned is '1' and the name is 'nodeblock_1'
752 const EntityMapType &entity_map = fields_map.at(block_type);
753 std::string block_name = "nodeblock_1";
754
755 // `mesh_model_coordinates` field is automatically created in NodeBlock constructor.
756 std::string coord_var_name = "mesh_model_coordinates";
757 // Get `node_count` and `spatialDimension`.
758 FieldInfoType model_coordinates_infos = get_expected_variable_infos_from_map<double>(
759 entity_map, block_type, block_name, coord_var_name);
760 spatialDimension = model_coordinates_infos.component_count;
761 if (!spatialDimension) {
762 std::ostringstream errmsg;
763 errmsg << "ERROR: Variable `" << coord_var_name
764 << "` in BP file without any dimension information.\n";
765 IOSS_ERROR(errmsg);
766 }
767 auto block = new Ioss::NodeBlock(this, block_name, model_coordinates_infos.node_boundaries_size,
768 spatialDimension);
769 block->property_add(Ioss::Property("id", 1));
770 block->property_add(Ioss::Property("guid", util().generate_guid(1)));
771 for (auto &variable : entity_map.at(block_name)) {
772 // Since some fields are created automatically, we need to avoid recreating them when loading
773 // the file.
774 if (!block->field_exists(variable.first)) {
775 FieldInfoType variable_infos =
776 get_variable_infos_from_map(entity_map, block_type, block_name, variable.first);
777 Ioss::Field field(variable.first, variable_infos.basic_type, variable_infos.variable_type,
778 variable_infos.role, variable_infos.node_boundaries_size);
779 block->field_add(field);
780 }
781 }
782 add_entity_properties(block, properties_map);
783 bool added = get_region()->add(block);
784 if (!added) {
785 delete block;
786 }
787 return model_coordinates_infos.node_boundaries_size;
788 }
789
790 template <typename T>
791 void DatabaseIO::add_entity_property(Ioss::GroupingEntity *ge, const std::string &encoded_name,
792 const std::string &property_name)
793 {
794 T variable;
795 // Meta variable read with `Get()` instead of `GetMetaVariable()` because name is already
796 // encoded. `GetMetaVariable()` simply encodes the variable name as a meta variable before
797 // performing a `Get()` call.
798 adios_wrapper.GetSync<T>(encoded_name, variable);
799 Ioss::Property property(property_name, variable);
800 ge->property_add(property);
801 }
802
803 void DatabaseIO::add_entity_properties(Ioss::GroupingEntity *ge,
804 const FieldsMapType &properties_map, std::string name)
805 {
806 std::string entity_type = ge->type_string();
807 if (properties_map.find(entity_type) == properties_map.end()) {
808 return;
809 }
810 const EntityMapType &entity_property_map = properties_map.at(entity_type);
811 GlobalMapType properties_info;
812 if (!name.empty()) {
813 // Verify that the given name exists.
814 if (entity_property_map.find(name) == entity_property_map.end()) {
815 std::ostringstream errmsg;
816 errmsg << "ERROR: Property map doesn't contain " << name << " inside " << entity_type
817 << ".\n";
818 IOSS_ERROR(errmsg);
819 }
820 properties_info = entity_property_map.at(name);
821 }
822 else {
823 std::string entity_name = ge->name();
824 if (entity_property_map.find(entity_name) == entity_property_map.end()) {
825 return;
826 }
827 properties_info = entity_property_map.at(entity_name);
828 }
829 for (const auto &property_info : properties_info) {
830 std::string property_name = property_info.first;
831 std::string encoded_name = property_info.second.first;
832 std::string type = property_info.second.second;
833 if (type == GetType<std::string>()) {
834 add_entity_property<std::string>(ge, encoded_name, property_name);
835 }
836 else if (type == GetType<double>()) {
837 add_entity_property<double>(ge, encoded_name, property_name);
838 }
839 else if (type == GetType<int>() || type == "int32_t") {
840 add_entity_property<int>(ge, encoded_name, property_name);
841 }
842 else {
843 std::ostringstream errmsg;
844 errmsg << "ERROR: Property '" << property_name << "' with type `" << type
845 << "` not supported.\n";
846 IOSS_ERROR(errmsg);
847 }
848 }
849 }
850
851 template <typename T>
852 std::string DatabaseIO::get_property_value(const FieldsMapType &properties_map,
853 const std::string &entity_type,
854 const std::string &entity_name,
855 const std::string &property_name) const
856 {
857 T property_value;
858 if (properties_map.find(entity_type) == properties_map.end()) {
859 return property_value;
860 }
861 const EntityMapType &entity_property_map = properties_map.at(entity_type);
862 if (entity_property_map.find(entity_name) == entity_property_map.end()) {
863 return property_value;
864 }
865 const GlobalMapType properties_info = entity_property_map.at(entity_name);
866 const auto &property_info = properties_info.find(property_name);
867 if (property_info != properties_info.end()) {
868 const std::string encoded_name = property_info->second.first;
869 const std::string type = property_info->second.second;
870 const std::string expected_type = GetType<T>();
871 if (type != expected_type) {
872 std::ostringstream errmsg;
873 errmsg << "ERROR: Property type `" << type << "` doesn't match expected type `"
874 << expected_type << "`.\n";
875 IOSS_ERROR(errmsg);
876 }
877 // Meta variable read with `Get()` instead of `GetMetaVariable()` because name is already
878 // encoded. `GetMetaVariable()` simply encodes the variable name as a meta variable before
879 // performing a `Get()` call.
880 adios_wrapper.GetSync<T>(encoded_name, property_value);
881 }
882 return property_value;
883 }
884
885 template <>
886 int64_t DatabaseIO::get_entities<Ioss::SideSet>(const FieldsMapType &fields_map,
887 const FieldsMapType &properties_map)
888 {
889 int64_t count = 0;
890 std::string entity_type = get_entity_type<Ioss::SideSet>();
891 if (fields_map.find(entity_type) == fields_map.end()) {
892 return count;
893 }
894 std::string sideblock_type = get_entity_type<Ioss::SideBlock>();
895 // Do SideBlocks exist in the fields_map. This will be used when loading the SideBlock
896 // for each SideSet, but we check this here outside of a loop as it only needs to be done once.
897 bool no_sideblocks = (fields_map.find(sideblock_type) == fields_map.end());
898
899 const EntityMapType &sidesets_map = fields_map.at(entity_type);
900 const EntityMapType &sideblocks_map = fields_map.at(sideblock_type);
901 for (const auto &entity : sidesets_map) {
902 std::string entity_name = entity.first;
903 Ioss::SideSet *ss = new Ioss::SideSet(this, entity_name);
904 bool ss_added = get_region()->add(ss);
905 add_entity_properties(ss, properties_map);
906 if (!ss_added) {
907 delete ss;
908 return count;
909 }
910 for (const auto &variable_pair : entity.second) {
911 // Since some fields are created automatically, we need to avoid recreating them when
912 // loading the file.
913 // First, check that field is actually a field and not a list of sideblocks
914 if (!is_sideblock_name(variable_pair.first)) {
915 if (!ss->field_exists(variable_pair.first)) {
916 FieldInfoType infos = get_variable_infos_from_map(sidesets_map, entity_type,
917 entity_name, variable_pair.first);
918
919 Ioss::Field field(variable_pair.first, infos.basic_type, infos.variable_type,
920 infos.role, infos.node_boundaries_size);
921 count += infos.node_boundaries_size;
922 ss->field_add(field);
923 }
924 }
925 else {
926 // The field in the bp file is actually a list of sideblocks
927 // If no sideblock, don't worry about this section and move on to the next
928 // sideset.
929 if (no_sideblocks) {
930 continue;
931 }
932 adios2::Variable<std::string> sideblock_var =
933 adios_wrapper.InquireVariable<std::string>(variable_pair.second.first);
934
935 if (sideblock_var) {
936 std::string stringified_names;
937 adios_wrapper.GetSync<std::string>(sideblock_var, stringified_names);
938 std::vector<std::string> block_names =
939 Ioss::tokenize(stringified_names, Sideblock_separator);
940 for (std::string block_name : block_names) {
941 if (sideblocks_map.find(block_name) != sideblocks_map.end()) {
942 bool first = true;
943 for (const auto &sideblock_field_pair : sideblocks_map.at(block_name)) {
944 std::string field_name = sideblock_field_pair.first;
945 FieldInfoType block_infos = get_variable_infos_from_map(
946 sideblocks_map, sideblock_type, block_name, field_name);
947 // Create sideblock first
948 if (first) {
949 Ioss::SideBlock *sideblock = new Ioss::SideBlock(
950 this, block_name, block_infos.topology, block_infos.parent_topology,
951 block_infos.node_boundaries_size);
952 bool block_added = ss->add(sideblock);
953 if (!block_added) {
954 delete sideblock;
955 std::ostringstream errmsg;
956 errmsg << "ERROR: Could not add sideblock `" << block_name << "` to sideset `"
957 << ss->name() << "`.\n";
958 IOSS_ERROR(errmsg);
959 }
960 first = false;
961 }
962 Ioss::SideBlock *sideblock = ss->get_side_block(block_name);
963 add_entity_properties(sideblock, properties_map);
964 // Add fields to sideblock if it was not automatically added.
965 if (!sideblock->field_exists(sideblock_field_pair.first)) {
966 Ioss::Field field(field_name, block_infos.basic_type, block_infos.variable_type,
967 block_infos.role, block_infos.node_boundaries_size);
968 sideblock->field_add(field);
969 }
970 }
971 }
972 else {
973 // throw an error if sideblock not found? Check if sideblock name matches sideset
974 // name: it could
975 // have been added automatically at read time if the list was empty (see STK).
976 }
977 }
978 }
979 }
980 }
981 }
982 return count;
983 }
984
985 template <typename T>
986 int64_t DatabaseIO::get_entities(const FieldsMapType &fields_map,
987 const FieldsMapType &properties_map)
988 {
989 int64_t count = 0;
990 std::string entity_type = get_entity_type<T>();
991 if (fields_map.find(entity_type) == fields_map.end()) {
992 return count;
993 }
994 const EntityMapType &entity_map = fields_map.at(entity_type);
995 for (const auto &variable_pair : entity_map) {
996 std::string entity_name = variable_pair.first;
997 // Get size and type info for the entity using the first element in the map.
998 FieldInfoType infos_to_create_entity = get_variable_infos_from_map(
999 entity_map, entity_type, entity_name, variable_pair.second.begin()->first);
1000 std::string block_type =
1001 get_property_value<std::string>(properties_map, entity_type, entity_name, "entity_type");
1002 block_type = block_type.empty() ? infos_to_create_entity.topology : block_type;
1003 T *entity =
1004 NewEntity<T>(this, entity_name, block_type, infos_to_create_entity.node_boundaries_size);
1005 count += infos_to_create_entity.node_boundaries_size;
1006 add_entity_properties(entity, properties_map);
1007
1008 bool added = get_region()->add(entity);
1009 if (!added) {
1010 delete entity;
1011 }
1012 for (const auto &field_pair : variable_pair.second) {
1013 // Since some fields are created automatically, we need to avoid recreating them when
1014 // loading the file.
1015 // Note: We get the information about the first field twice: once before this loop, and
1016 // once inside the loop. The code could be modified to perform this action only once but
1017 // most likely the code will be more complex and will not save a lot of computation time.
1018 if (!entity->field_exists(field_pair.first)) {
1019 FieldInfoType infos =
1020 get_variable_infos_from_map(entity_map, entity_type, entity_name, field_pair.first);
1021 Ioss::Field field(field_pair.first, infos.basic_type, infos.variable_type, infos.role,
1022 infos.node_boundaries_size);
1023 entity->field_add(field);
1024 }
1025 }
1026 }
1027 return count;
1028 }
1029
1030 // TODO: Consolidate this code with generice `get_entities()` function as there is very little
1031 // difference between the two.
1032 template <>
1033 int64_t DatabaseIO::get_entities<Ioss::CommSet>(const FieldsMapType &fields_map,
1034 const FieldsMapType &properties_map)
1035 {
1036 int64_t count = 0;
1037 std::string entity_type = get_entity_type<Ioss::CommSet>();
1038 if (fields_map.find(entity_type) == fields_map.end()) {
1039 return count;
1040 }
1041 const EntityMapType &entity_map = fields_map.at(entity_type);
1042 for (const auto &variable_pair : entity_map) {
1043 std::string entity_name = variable_pair.first;
1044 // Get size and type info for the entity using the first element in the map.
1045 FieldInfoType infos_to_create_entity = get_variable_infos_from_map(
1046 entity_map, entity_type, entity_name, variable_pair.second.begin()->first);
1047 std::string block_type =
1048 get_property_value<std::string>(properties_map, entity_type, entity_name, "entity_type");
1049 std::string commset_name = "commset_" + block_type;
1050
1051 block_type = block_type.empty() ? infos_to_create_entity.topology : block_type;
1052 Ioss::CommSet *entity = NewEntity<Ioss::CommSet>(this, commset_name, block_type,
1053 infos_to_create_entity.node_boundaries_size);
1054 count += infos_to_create_entity.node_boundaries_size;
1055 add_entity_properties(entity, properties_map);
1056 // Save original name as entity property
1057 Ioss::Property property(original_name, entity_name);
1058 entity->property_add(property);
1059
1060 bool added = get_region()->add(entity);
1061 if (!added) {
1062 delete entity;
1063 }
1064 for (const auto &field_pair : variable_pair.second) {
1065 // Since some fields are created automatically, we need to avoid recreating them when
1066 // loading the file.
1067 // Note: We get the information about the first field twice: once before this loop, and
1068 // once inside the loop. The code could be modified to perform this action only once but
1069 // most likely the code will be more complex and will not save a lot of computation time.
1070 if (!entity->field_exists(field_pair.first)) {
1071 FieldInfoType infos =
1072 get_variable_infos_from_map(entity_map, entity_type, entity_name, field_pair.first);
1073 Ioss::Field field(field_pair.first, infos.basic_type, infos.variable_type, infos.role,
1074 infos.node_boundaries_size);
1075 entity->field_add(field);
1076 }
1077 }
1078 }
1079 return count;
1080 }
1081
1082 std::string DatabaseIO::get_optional_string_variable(const std::string &field_name,
1083 const std::string &string_variable) const
1084 {
1085 auto tokens = Ioss::tokenize(field_name, Name_separator);
1086 std::string entity = encode_field_name({tokens[0], tokens[1]});
1087 auto v = adios_wrapper.InquireVariable<std::string>(
1088 adios_wrapper.EncodeMetaVariable(string_variable, entity));
1089 if (v) {
1090 return adios_wrapper.GetMetaVariable<std::string>(string_variable, entity);
1091 }
1092 else {
1093 return std::string{};
1094 }
1095 }
1096
1097 template <typename T>
1098 DatabaseIO::BlockInfoType DatabaseIO::get_block_infos(const adios2::Variable<T> &var) const
1099 {
1100 std::map<size_t, std::vector<typename adios2::Variable<T>::Info>> allblocks =
1101 adios_wrapper.AllStepsBlocksInfo(var);
1102 if (allblocks.empty()) {
1103 std::ostringstream errmsg;
1104 errmsg << "ERROR: Empty BP variable\n";
1105 IOSS_ERROR(errmsg);
1106 }
1107 BlockInfoType infos;
1108 // For each time step.
1109 for (const auto &blockpair : allblocks) {
1110 const std::vector<typename adios2::Variable<T>::Info> &blocks = blockpair.second;
1111 // Find in vector if this variable is defined for the current rank process. This means
1112 // that there is one block for which the rank encoded as the first value in the `Start` array
1113 // matches the current rank.
1114 // Note: one block per rank.
1115 for (const auto &block : blocks) {
1116 if (block.Start[0] != rank) {
1117 infos.global_size += block.Count[1];
1118 // This is not the block corresponding to the current process (rank).
1119 continue;
1120 }
1121 if (infos.Count.empty()) {
1122 infos.Count = block.Count;
1123 }
1124 else if (infos.Count != block.Count) {
1125 std::ostringstream errmsg;
1126 errmsg
1127 << "ERROR: Variable changes sizes over steps. Not supported by Ioad::DatabaseIO.\n";
1128 IOSS_ERROR(errmsg);
1129 }
1130 // }
1131 // Steps: will save last step
1132 infos.steps.push_back(blockpair.first);
1133 // We found the block we were searching for, no need to continue looping over all
1134 // the other blocks.
1135 break;
1136 }
1137 }
1138 return infos;
1139 }
1140
1141 template <typename T>
1142 DatabaseIO::FieldInfoType DatabaseIO::get_variable_infos(const std::string &var_name) const
1143 {
1144 FieldInfoType infos;
1145 adios2::Variable<T> var = adios_wrapper.InquireVariable<T>(var_name);
1146
1147 // Dimension is expected to be 3 because:
1148 // 0 = rank
1149 // 1 = size of field
1150 // 2 = number of components
1151 size_t ndim = var.Shape().size();
1152 if (ndim != 3) {
1153 std::ostringstream errmsg;
1154 errmsg << "ERROR: BP variable dimension should be 3.\n";
1155 IOSS_ERROR(errmsg);
1156 }
1157 BlockInfoType block_infos = get_block_infos<T>(var);
1158 size_t laststep = 0;
1159 if (!block_infos.steps.empty() && !block_infos.Count.empty()) {
1160 laststep = block_infos.steps.back();
1161 // Skipping dimension=0 which is equal to the rank. Dimension=1 encodes beginning and count
1162 // of node count for this variable. Dimension=2 encodes the number of components for this
1163 // variables, and should always start at 0;
1164 infos.component_count = block_infos.Count[2];
1165 infos.node_boundaries_size = block_infos.Count[1];
1166 infos.steps = block_infos.steps;
1167 }
1168 // Get topology: Only relevant for blocks, not for sets.
1169 infos.topology = get_optional_string_variable(var_name, Topology_meta);
1170 // Get parent topology: Only relevant for sideblocks.
1171 infos.parent_topology = get_optional_string_variable(var_name, Parent_topology_meta);
1172 // Get VariableType
1173 infos.variable_type = adios_wrapper.GetMetaVariable<std::string>(Var_type_meta, var_name);
1174 infos.basic_type = template_to_basic_type<T>();
1175 // Only transient fields can have steps.
1176 infos.role =
1177 static_cast<Ioss::Field::RoleType>(adios_wrapper.GetMetaVariable<int>(Role_meta, var_name));
1178 if (infos.role != Ioss::Field::RoleType::TRANSIENT && laststep != 0) {
1179 std::ostringstream errmsg;
1180 errmsg << "ERROR: Last step should be 0 for non-transient fields. "
1181 << "Something is wrong in the Ioad::DatabaseIO::get_variable_size() function.\n";
1182 IOSS_ERROR(errmsg);
1183 }
1184 return infos;
1185 }
1186
1187 void DatabaseIO::get_globals(const GlobalMapType &globals_map,
1188 const FieldsMapType &properties_map)
1189 {
1190 // Check "time" attribute and global variable.
1191 timeScaleFactor = adios_wrapper.GetAttribute<double>(Time_scale_factor, true, 1.0);
1192 Ioss::SerializeIO serializeIO_(this);
1193 Ioss::Region *this_region = get_region();
1194 if (globals_map.find(Time_meta) != globals_map.end()) {
1195 // Load time steps
1196 // 1) Check that the time type is `double` as expected.
1197 adios2::Variable<double> time_var = adios_wrapper.InquireVariable<double>(Time_meta);
1198 if (time_var) {
1199 std::vector<double> tsteps(time_var.Steps());
1200 if (!is_streaming) {
1201 time_var.SetStepSelection(std::make_pair(
1202 time_var.StepsStart(), time_var.Steps())); // Doesn't work with streaming.
1203 }
1204 else {
1205 // For streaming, we probably only want to read the current time as we do not want to
1206 // use SetStepSelection that would prohibit the usage of `BeginStep()/EndStep()`.
1207 // std::ostringstream errmsg;
1208 // errmsg << "ERROR: Streaming is not yet supported for reading.\n";
1209 // IOSS_ERROR(errmsg);
1210 }
1211 adios_wrapper.GetSync(time_var, Data(tsteps));
1212 for (size_t step = 0; step < time_var.Steps(); step++) {
1213 // if (tsteps[i] <= last_time) { TODO: Check last time step before writing everything
1214 this_region->add_state_nl(tsteps[step] * timeScaleFactor);
1215 }
1216 }
1217 else {
1218 std::ostringstream errmsg;
1219 errmsg << "ERROR: Timestep global detected in file but cannot read it.\n";
1220 IOSS_ERROR(errmsg);
1221 }
1222 }
1223 // Add region properties
1224 add_entity_properties(this_region, properties_map, region_name);
1225 }
1226
1227 void DatabaseIO::read_meta_data_nl()
1228 {
1229 check_processor_info();
1230 Ioss::Region *region = get_region();
1231
1232 // Define properties
1233 if (is_streaming) {
1234 region->property_update("streaming", 1);
1235 }
1236 // Only get schema version attribute as it is the only one we expect.
1237 adios_wrapper.GetAttribute<unsigned int>(Schema_version_string);
1238
1239 // Get all variables
1240 GlobalMapType globals_map;
1241 // Entity_type/Entity_name::Property -> name & Type
1242 FieldsMapType properties_map;
1243 // Entity_type/Entity_name/Field -> name & Type
1244 FieldsMapType fields_map;
1245
1246 const std::map<std::string, std::map<std::string, std::string>> variables =
1247 adios_wrapper.AvailableVariables();
1248 for (const auto &vpair : variables) {
1249 const std::string &name = vpair.first;
1250 auto name_type_pair = std::make_pair(name, vpair.second.at("Type"));
1251 auto tokens = Ioss::tokenize(name, Name_separator);
1252 switch (tokens.size()) {
1253 case 1: globals_map[tokens[0]] = name_type_pair; break;
1254 case 2: {
1255 // Only save properties.
1256 std::string meta_var = "";
1257 std::string property = "";
1258 std::tie(meta_var, property) = adios_wrapper.DecodeMetaName(tokens[1]);
1259 if (!property.compare(0, property_meta.size(), property_meta)) {
1260 properties_map[tokens[0]][meta_var][property.substr(property_meta.size())] =
1261 name_type_pair;
1262 }
1263 break;
1264 }
1265 case 3: {
1266 std::string is_meta = "";
1267 std::tie(std::ignore, is_meta) = adios_wrapper.DecodeMetaName(tokens[2]);
1268 if (is_meta.empty()) {
1269 fields_map[tokens[0]][tokens[1]][tokens[2]] = name_type_pair;
1270 }
1271 break;
1272 }
1273 default: {
1274 std::ostringstream errmsg;
1275 errmsg << "ERROR: Invalid encoded entity name.\n";
1276 IOSS_ERROR(errmsg);
1277 }
1278 }
1279 }
1280
1281 get_globals(globals_map, properties_map);
1282 nodeCount = get_entities<Ioss::NodeBlock>(fields_map, properties_map);
1283 edgeCount = get_entities<Ioss::EdgeBlock>(fields_map, properties_map);
1284 faceCount = get_entities<Ioss::FaceBlock>(fields_map, properties_map);
1285 elementCount = get_entities<Ioss::ElementBlock>(fields_map, properties_map);
1286 check_side_topology();
1287 get_entities<Ioss::SideSet>(fields_map, properties_map);
1288 get_entities<Ioss::NodeSet>(fields_map, properties_map);
1289 get_entities<Ioss::EdgeSet>(fields_map, properties_map);
1290 get_entities<Ioss::FaceSet>(fields_map, properties_map);
1291 get_entities<Ioss::ElementSet>(fields_map, properties_map);
1292 get_entities<Ioss::CommSet>(fields_map, properties_map);
1293
1294 read_region(fields_map);
1295 read_communication_metadata();
1296 }
1297
1298 void DatabaseIO::read_region(const FieldsMapType &fields_map)
1299 {
1300 // // Add properties and fields to the 'owning' region.
1301 // // Also defines member variables of this class...
1302
1303 if (nodeCount == 0) {
1304 Ioss::WarnOut() << "No nodes were found in the model, file '" << decoded_filename() << "'\n";
1305 }
1306 else if (nodeCount < 0) {
1307 // NOTE: Code will not continue past this call...
1308 std::ostringstream errmsg;
1309 errmsg << "ERROR: Negative node count was found in the model\n"
1310 << " File: '" << decoded_filename() << "'.\n";
1311 IOSS_ERROR(errmsg);
1312 }
1313
1314 if (elementCount == 0) {
1315 Ioss::WarnOut() << "No elements were found in the model, file: '" << decoded_filename()
1316 << "'\n";
1317 }
1318
1319 if (elementCount < 0) {
1320 // NOTE: Code will not continue past this call...
1321 std::ostringstream errmsg;
1322 errmsg << "ERROR: Negative element count was found in the model, file: '"
1323 << decoded_filename() << "'";
1324 IOSS_ERROR(errmsg);
1325 }
1326 Ioss::Region *region = get_region();
1327
1328 // See if any coordinate frames exist on mesh. If so, define them on region.
1329 // add_coordinate_frames();
1330 if (fields_map.find(coordinate_frame_name) != fields_map.end()) {
1331 const EntityMapType &entity_map = fields_map.at(coordinate_frame_name);
1332 for (const auto &variable_pair : entity_map) {
1333 std::vector<double> coord(9, 0);
1334
1335 int64_t id = std::stoll(variable_pair.first);
1336 char tag = variable_pair.second.begin()->first[0];
1337 std::string var_name = variable_pair.second.begin()->second.first;
1338 adios_wrapper.GetSync(var_name, Data(coord));
1339 Ioss::CoordinateFrame coordinate_frame(id, tag, Data(coord));
1340 region->add(coordinate_frame);
1341 }
1342 }
1343 }
1344
1345 void DatabaseIO::read_communication_metadata()
1346 {
1347 // Number of processors file was decomposed for
1348 int num_proc = adios_wrapper.GetAttribute<unsigned long>(Processor_number_meta);
1349
1350 // Get global data (over all processors)
1351 int64_t global_nodes = nodeCount;
1352 int64_t global_elements = elementCount;
1353 int64_t global_eblocks = 0; // unused
1354 int64_t global_nsets = 0; // unused
1355 int64_t global_ssets = 0; // unused
1356
1357 int64_t num_internal_nodes = nodeCount;
1358 int64_t num_border_nodes = 0;
1359 int64_t num_internal_elems = elementCount;
1360 int64_t num_border_elems = 0;
1361
1362 Ioss::Region *region = get_region();
1363
1364 region->property_add(Ioss::Property("processor_count", num_proc));
1365
1366 region->property_add(Ioss::Property("internal_node_count", num_internal_nodes));
1367 region->property_add(Ioss::Property("border_node_count", num_border_nodes));
1368 region->property_add(Ioss::Property("internal_element_count", num_internal_elems));
1369 region->property_add(Ioss::Property("border_element_count", num_border_elems));
1370 region->property_add(Ioss::Property("global_node_count", global_nodes));
1371 region->property_add(Ioss::Property("global_element_count", global_elements));
1372 region->property_add(Ioss::Property("global_element_block_count", global_eblocks));
1373 region->property_add(Ioss::Property("global_node_set_count", global_nsets));
1374 region->property_add(Ioss::Property("global_side_set_count", global_ssets));
1375
1376 // Possibly, the following 4 fields should be nodesets and element
1377 // sets instead of fields on the region...
1378 region->field_add(Ioss::Field("internal_nodes", region->field_int_type(), IOSS_SCALAR(),
1379 Ioss::Field::COMMUNICATION, num_internal_nodes));
1380 region->field_add(Ioss::Field("border_nodes", region->field_int_type(), IOSS_SCALAR(),
1381 Ioss::Field::COMMUNICATION, num_border_nodes));
1382 region->field_add(Ioss::Field("internal_elements", region->field_int_type(), IOSS_SCALAR(),
1383 Ioss::Field::COMMUNICATION, num_internal_elems));
1384 region->field_add(Ioss::Field("border_elements", region->field_int_type(), IOSS_SCALAR(),
1385 Ioss::Field::COMMUNICATION, num_border_elems));
1386
1387 assert(nodeCount == num_internal_nodes + num_border_nodes);
1388 assert(elementCount == num_internal_elems + num_border_elems);
1389 }
1390 void DatabaseIO::check_processor_info()
1391 {
1392 std::ostringstream errmsg;
1393 // Get Processor information
1394 unsigned long number_proc_read =
1395 adios_wrapper.GetAttribute<unsigned long>(Processor_number_meta);
1396
1397 if (number_proc < number_proc_read) {
1398 errmsg << "Processor decomposition count in file (" << number_proc_read
1399 << ") is larger than current processor count (" << number_proc
1400 << "). Configuration not supported.\n";
1401 IOSS_ERROR(errmsg);
1402 }
1403 else if (number_proc > number_proc_read) {
1404 Ioss::WarnOut() << "This file was originally written on " << number_proc_read
1405 << " processors, but is now being read using " << number_proc
1406 << " processors.\n";
1407 }
1408 if (rank < number_proc_read) {
1409 // Only get info for processors that actually have an id.
1410 unsigned long processor_id;
1411 get_data<unsigned long>(static_cast<void *>(&processor_id), Processor_id_meta);
1412 if (rank != processor_id) {
1413 Ioss::WarnOut() << "This file was originally written on processor " << processor_id
1414 << ", but is now being read on processor " << rank
1415 << ". This may cause problems if there is any processor-dependent data on "
1416 "the file.\n";
1417 }
1418 }
1419 }
1420
1421 int64_t DatabaseIO::get_field_internal(const Ioss::Region *reg, const Ioss::Field &field,
1422 void *data, size_t data_size) const
1423 {
1424 return get_field_internal_t(reg, field, data, data_size);
1425 }
1426 int64_t DatabaseIO::get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
1427 void *data, size_t data_size) const
1428 {
1429 size_t num_to_get = field.verify(data_size);
1430 if (num_to_get > 0) {
1431 if (field.get_name() == "mesh_model_coordinates_x" ||
1432 field.get_name() == "mesh_model_coordinates_y" ||
1433 field.get_name() == "mesh_model_coordinates_z") {
1434 Ioss::Field coord_field = nb->get_field("mesh_model_coordinates");
1435 std::vector<double> coord(num_to_get * spatialDimension);
1436 get_field_internal_t(nb, coord_field, Data(coord), data_size * spatialDimension);
1437
1438 // Cast 'data' to correct size -- double
1439 double *rdata = static_cast<double *>(data);
1440 int offset = 0;
1441 if (field.get_name() == "mesh_model_coordinates_x") {
1442 offset = 0;
1443 }
1444 else if (field.get_name() == "mesh_model_coordinates_y") {
1445 offset = 1;
1446 }
1447 else if (field.get_name() == "mesh_model_coordinates_z") {
1448 offset = 2;
1449 }
1450 for (size_t i = 0; i < num_to_get; i++) {
1451 rdata[i] = coord[i * spatialDimension + offset];
1452 }
1453 }
1454 else {
1455 return get_field_internal_t(nb, field, data, data_size);
1456 }
1457 }
1458 return num_to_get;
1459 }
1460
1461 int64_t DatabaseIO::get_field_internal(const Ioss::EdgeBlock *eb, const Ioss::Field &field,
1462 void *data, size_t data_size) const
1463 {
1464 return get_field_internal_t(eb, field, data, data_size);
1465 }
1466 int64_t DatabaseIO::get_field_internal(const Ioss::FaceBlock *fb, const Ioss::Field &field,
1467 void *data, size_t data_size) const
1468 {
1469 return get_field_internal_t(fb, field, data, data_size);
1470 }
1471 int64_t DatabaseIO::get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
1472 void *data, size_t data_size) const
1473 {
1474 return get_field_internal_t(eb, field, data, data_size);
1475 }
1476 int64_t DatabaseIO::get_field_internal(const Ioss::SideBlock *sb, const Ioss::Field &field,
1477 void *data, size_t data_size) const
1478 {
1479 return get_field_internal_t(sb, field, data, data_size);
1480 }
1481 int64_t DatabaseIO::get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
1482 void *data, size_t data_size) const
1483 {
1484 return get_field_internal_t(ns, field, data, data_size);
1485 }
1486 int64_t DatabaseIO::get_field_internal(const Ioss::EdgeSet *es, const Ioss::Field &field,
1487 void *data, size_t data_size) const
1488 {
1489 return get_field_internal_t(es, field, data, data_size);
1490 }
1491 int64_t DatabaseIO::get_field_internal(const Ioss::FaceSet *fs, const Ioss::Field &field,
1492 void *data, size_t data_size) const
1493 {
1494 return get_field_internal_t(fs, field, data, data_size);
1495 }
1496 int64_t DatabaseIO::get_field_internal(const Ioss::ElementSet *es, const Ioss::Field &field,
1497 void *data, size_t data_size) const
1498 {
1499 return get_field_internal_t(es, field, data, data_size);
1500 }
1501 int64_t DatabaseIO::get_field_internal(const Ioss::SideSet *ss, const Ioss::Field &field,
1502 void *data, size_t data_size) const
1503 {
1504 return get_field_internal_t(ss, field, data, data_size);
1505 }
1506 int64_t DatabaseIO::get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
1507 void *data, size_t data_size) const
1508 {
1509 return get_field_internal_t(cs, field, data, data_size);
1510 }
1511
1512 int64_t DatabaseIO::get_field_internal_t(const Ioss::GroupingEntity *entity,
1513 const Ioss::Field &field, void *data,
1514 size_t data_size) const
1515 {
1516 if (!data || !data_size) {
1517 return 0;
1518 }
1519 int num_to_get = field.verify(data_size);
1520 if (num_to_get > 0) {
1521 const std::string entity_type = entity->type_string();
1522
1523 // Check if field name has changed. Rely on property `original_name` if
1524 // it exists.
1525 const std::string entity_name = entity->property_exists(original_name)
1526 ? entity->get_property(original_name).get_string()
1527 : entity->name();
1528 const std::string &field_name = field.get_name();
1529
1530 if (find_field_in_mapset(entity_type, field_name, Ignore_fields)) {
1531 return num_to_get;
1532 }
1533
1534 std::string encoded_name = encode_field_name({entity_type, entity_name, field_name});
1535 bool use_step_selection = false;
1536 if (field.get_role() == Ioss::Field::RoleType::TRANSIENT && !is_streaming) {
1537 use_step_selection = true;
1538 }
1539 switch (field.get_type()) {
1540 case Ioss::Field::BasicType::DOUBLE:
1541 get_data<double>(data, encoded_name, use_step_selection);
1542 break;
1543 case Ioss::Field::BasicType::INT32:
1544 get_data<int32_t>(data, encoded_name, use_step_selection);
1545 break;
1546 case Ioss::Field::BasicType::INT64:
1547 get_data<int64_t>(data, encoded_name, use_step_selection);
1548 break;
1549 case Ioss::Field::BasicType::COMPLEX:
1550 get_data<Complex>(data, encoded_name, use_step_selection);
1551 break;
1552 case Ioss::Field::BasicType::CHARACTER:
1553 get_data<char>(data, encoded_name, use_step_selection);
1554 break;
1555 default:
1556 std::ostringstream errmsg;
1557 errmsg << "INTERNAL ERROR: Invalid field type. "
1558 << "Something is wrong in the Ioad::DatabaseIO::get_field_internal_t() function. "
1559 << "Please report.\n";
1560 IOSS_ERROR(errmsg);
1561 }
1562 }
1563 return num_to_get;
1564 }
1565
1566 template <typename T>
1567 void DatabaseIO::get_data(void *data, const std::string &encoded_name,
1568 bool use_step_selection) const
1569 {
1570 adios2::Variable<T> entities = adios_wrapper.InquireVariable<T>(encoded_name);
1571 if (entities) {
1572 T *rdata = static_cast<T *>(data);
1573 adios2::Dims size = entities.Shape();
1574 size[0] = 1;
1575 adios2::Dims offset = entities.Start();
1576 offset[0] = rank;
1577
1578 entities.SetSelection(adios2::Box<adios2::Dims>(offset, size));
1579 // if transient, set step that should be read if not streaming.
1580 if (use_step_selection) {
1581 size_t step = get_current_state();
1582 entities.SetStepSelection(std::make_pair(step, 1));
1583 }
1584 else {
1585 // if streaming, we are reading step selected by `BeginStep()/EndStep()`.
1586 }
1587
1588 // TODO: Set selection per rank. Support written by N processes, and loaded by M processes.
1589 adios_wrapper.GetSync<T>(entities, rdata);
1590 }
1591 else {
1592 if (!is_streaming) {
1593 std::ostringstream errmsg;
1594 errmsg << "ERROR: Required `" << encoded_name << "` variable not found in file.\n";
1595 IOSS_ERROR(errmsg);
1596 }
1597 else {
1598 Ioss::WarnOut() << "The variable `" << encoded_name << "` was not found.\n";
1599 }
1600 }
1601 }
1602
1603 void DatabaseIO::compute_block_membership_nl(Ioss::SideBlock *efblock,
1604 std::vector<std::string> &block_membership) const
1605 {
1606 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
1607 assert(Ioss::Utils::check_block_order(element_blocks));
1608
1609 Ioss::Int64Vector block_ids(element_blocks.size());
1610 if (block_ids.size() == 1) {
1611 block_ids[0] = 1;
1612 }
1613 else {
1614 Ioss::Int64Vector element_side;
1615 efblock->get_field_data("element_side", element_side);
1616
1617 size_t number_sides = element_side.size() / 2;
1618 Ioss::ElementBlock *block = nullptr;
1619 for (size_t iel = 0; iel < number_sides; iel++) {
1620 int64_t elem_id = element_side[2 * iel]; // Vector contains both element and side.
1621 // elem_id = elemMap.global_to_local(elem_id);
1622 if (block == nullptr || !block->contains(elem_id)) {
1623 block = get_region()->get_element_block(elem_id);
1624 assert(block != nullptr);
1625 size_t block_order = block->get_property("original_block_order").get_int();
1626 assert(block_order < block_ids.size());
1627 block_ids[block_order] = 1;
1628 }
1629 }
1630 }
1631
1632 // Synchronize among all processors....
1633 if (isParallel) {
1634 util().global_array_minmax(block_ids, Ioss::ParallelUtils::DO_MAX);
1635 }
1636
1637 for (const auto block : element_blocks) {
1638 size_t block_order = block->get_property("original_block_order").get_int();
1639 assert(block_order < block_ids.size());
1640 if (block_ids[block_order] == 1) {
1641 if (!Ioss::Utils::block_is_omitted(block)) {
1642 block_membership.push_back(block->name());
1643 }
1644 }
1645 }
1646 }
1647
1648 int DatabaseIO::get_current_state() const
1649 {
1650 // value returned is 1-based, whereas ADIOS expect 0-based values
1651 int step = get_region()->get_current_state() - 1;
1652
1653 if (step < 0) {
1654 std::ostringstream errmsg;
1655 errmsg << "ERROR: No currently active state. The calling code must call "
1656 "Ioss::Region::begin_state(int step)\n"
1657 << " to set the database timestep from which to read the transient data.\n"
1658 << " [" << get_filename() << "]\n";
1659 IOSS_ERROR(errmsg);
1660 }
1661 return step;
1662 }
1663
1664 } // namespace Ioad