Debian Codegraph @

packages/seacas/libraries/ioss/src/exonull/Ioexnl_BaseDatabaseIO.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_CodeTypes.h"
8 #include "Ioss_IOFactory.h"
9 #include "Ioss_ParallelUtils.h"
10 #include "Ioss_Utils.h"
11 #include "exonull/Ioexnl_BaseDatabaseIO.h"
12 #include <cassert>
13 #include <ctime>
14 #include <exodusII.h>
15 #include <fmt/core.h>
16 #include <fmt/ostream.h>
17 #include <map>
18 #include <sstream>
19 #include <string>
20 #include <tokenize.h>
21 #include <vector>
22
23 #include "Ioexnl_Utils.h"
24 #include "Ioss_Assembly.h"
25 #include "Ioss_Blob.h"
26 #include "Ioss_DBUsage.h"
27 #include "Ioss_DatabaseIO.h"
28 #include "Ioss_EdgeBlock.h"
29 #include "Ioss_EdgeSet.h"
30 #include "Ioss_ElementBlock.h"
31 #include "Ioss_ElementSet.h"
32 #include "Ioss_EntityBlock.h"
33 #include "Ioss_EntityType.h"
34 #include "Ioss_FaceBlock.h"
35 #include "Ioss_FaceSet.h"
36 #include "Ioss_Field.h"
37 #include "Ioss_FileInfo.h"
38 #include "Ioss_GroupingEntity.h"
39 #include "Ioss_Map.h"
40 #include "Ioss_MeshType.h"
41 #include "Ioss_NodeBlock.h"
42 #include "Ioss_NodeSet.h"
43 #include "Ioss_Property.h"
44 #include "Ioss_PropertyManager.h"
45 #include "Ioss_Region.h"
46 #include "Ioss_SideBlock.h"
47 #include "Ioss_SideSet.h"
48 #include "Ioss_SmartAssert.h"
49 #include "Ioss_State.h"
50
51 // Transitioning from treating global variables as Ioss::Field::TRANSIENT
52 // to Ioss::Field::REDUCTION. To get the old behavior, define the value
53 // below to '1'.
54 #define GLOBALS_ARE_TRANSIENT 0
55
56 // ========================================================================
57 // Static internal helper functions
58 // ========================================================================
59 namespace {
60 std::vector<ex_entity_type> exodus_types({EX_GLOBAL, EX_BLOB, EX_ASSEMBLY, EX_NODE_BLOCK,
61 EX_EDGE_BLOCK, EX_FACE_BLOCK, EX_ELEM_BLOCK,
62 EX_NODE_SET, EX_EDGE_SET, EX_FACE_SET, EX_ELEM_SET,
63 EX_SIDE_SET});
64
65 void check_variable_consistency(const ex_var_params &exo_params, int my_processor,
66 const std::string &filename, const Ioss::ParallelUtils &util);
67
68 void check_attribute_index_order(Ioss::GroupingEntity *block);
69
70 template <typename T>
71 void write_attribute_names(int exoid, ex_entity_type type, const std::vector<T *> &entities);
72
73 char **get_name_array(size_t count, int size)
74 {
75 auto *names = new char *[count];
76 for (size_t i = 0; i < count; i++) {
77 names[i] = new char[size + 1];
78 std::memset(names[i], '\0', size + 1);
79 }
80 return names;
81 }
82
83 void delete_name_array(char **names, int count)
84 {
85 for (int i = 0; i < count; i++) {
86 delete[] names[i];
87 }
88 delete[] names;
89 }
90
91 } // namespace
92
93 namespace Ioexnl {
94 BaseDatabaseIO::BaseDatabaseIO(Ioss::Region *region, const std::string &filename,
95 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
96 const Ioss::PropertyManager &props)
97 : Ioss::DatabaseIO(region, filename, db_usage, communicator, props)
98 {
99 m_groupCount[EX_GLOBAL] = 1; // To make some common code work more cleanly.
100 m_groupCount[EX_NODE_BLOCK] = 1; // To make some common code work more cleanly.
101
102 // A history file is only written on processor 0...
103 if (db_usage == Ioss::WRITE_HISTORY) {
104 isParallel = false;
105 }
106
107 timeLastFlush = time(nullptr);
108 dbState = Ioss::STATE_UNKNOWN;
109
110 // Set exodusII warning level.
111 if (util().get_environment("EX_DEBUG", isParallel)) {
112 fmt::print(
113 Ioss::DebugOut(),
114 "IOEX: Setting EX_VERBOSE|EX_DEBUG because EX_DEBUG environment variable is set.\n");
115 ex_opts(EX_VERBOSE | EX_DEBUG);
116 }
117
118 if (!is_input()) {
119 if (util().get_environment("EX_MODE", exodusMode, isParallel)) {
120 fmt::print(
121 Ioss::OUTPUT(),
122 "IOEX: Exodus create mode set to {} from value of EX_MODE environment variable.\n",
123 exodusMode);
124 }
125
126 if (util().get_environment("EX_MINIMIZE_OPEN_FILES", isParallel)) {
127 fmt::print(Ioss::OUTPUT(),
128 "IOEX: Minimizing open files because EX_MINIMIZE_OPEN_FILES environment "
129 "variable is set.\n");
130 minimizeOpenFiles = true;
131 }
132 else {
133 Ioss::Utils::check_set_bool_property(properties, "MINIMIZE_OPEN_FILES", minimizeOpenFiles);
134 }
135
136 {
137 bool file_per_state = false;
138 Ioss::Utils::check_set_bool_property(properties, "FILE_PER_STATE", file_per_state);
139 if (file_per_state) {
140 set_file_per_state(true);
141 }
142 }
143 }
144
145 // See if there are any properties that need to (or can) be
146 // handled prior to opening/creating database...
147 bool compress = ((properties.exists("COMPRESSION_LEVEL") &&
148 properties.get("COMPRESSION_LEVEL").get_int() > 0) ||
149 (properties.exists("COMPRESSION_SHUFFLE") &&
150 properties.get("COMPRESSION_SHUFFLE").get_int() > 0));
151
152 if (compress) {
153 exodusMode |= EX_NETCDF4;
154 }
155
156 if (properties.exists("FILE_TYPE")) {
157 std::string type = properties.get("FILE_TYPE").get_string();
158 if (type == "netcdf4" || type == "netcdf-4" || type == "hdf5") {
159 exodusMode |= EX_NETCDF4;
160 }
161 else if (type == "netcdf5" || type == "netcdf-5" || type == "cdf5") {
162 exodusMode |= EX_64BIT_DATA;
163 }
164 }
165
166 if (properties.exists("ENABLE_FILE_GROUPS")) {
167 exodusMode |= EX_NETCDF4;
168 exodusMode |= EX_NOCLASSIC;
169 }
170
171 if (properties.exists("MAXIMUM_NAME_LENGTH")) {
172 maximumNameLength = properties.get("MAXIMUM_NAME_LENGTH").get_int();
173 }
174
175 if (properties.exists("REAL_SIZE_DB")) {
176 int rsize = properties.get("REAL_SIZE_DB").get_int();
177 if (rsize == 4) {
178 dbRealWordSize = 4; // Only used for file create...
179 }
180 }
181
182 if (properties.exists("INTEGER_SIZE_DB")) {
183 int isize = properties.get("INTEGER_SIZE_DB").get_int();
184 if (isize == 8) {
185 exodusMode |= EX_ALL_INT64_DB;
186 }
187 }
188
189 if (properties.exists("INTEGER_SIZE_API")) {
190 int isize = properties.get("INTEGER_SIZE_API").get_int();
191 if (isize == 8) {
192 set_int_byte_size_api(Ioss::USE_INT64_API);
193 }
194 }
195
196 if (!is_input()) {
197 if (properties.exists("FLUSH_INTERVAL")) {
198 int interval = properties.get("FLUSH_INTERVAL").get_int();
199 flushInterval = interval;
200 }
201 }
202
203 // Don't open output files until they are actually going to be
204 // written to. This is needed for proper support of the topology
205 // files and auto restart so we don't overwrite a file with data we
206 // need to save...
207 }
208
209 void BaseDatabaseIO::set_int_byte_size_api(Ioss::DataSize size) const
210 {
211 dbIntSizeAPI = size; // mutable
212 }
213
214 // Returns byte size of integers stored on the database...
215 int BaseDatabaseIO::int_byte_size_db() const { return 8; }
216
217 // common
218 unsigned BaseDatabaseIO::entity_field_support() const
219 {
220 return Ioss::NODEBLOCK | Ioss::EDGEBLOCK | Ioss::FACEBLOCK | Ioss::ELEMENTBLOCK |
221 Ioss::NODESET | Ioss::EDGESET | Ioss::FACESET | Ioss::ELEMENTSET | Ioss::SIDESET |
222 Ioss::SIDEBLOCK | Ioss::REGION | Ioss::SUPERELEMENT;
223 }
224
225 // common
226 int BaseDatabaseIO::get_file_pointer() const { return 0; }
227
228 int BaseDatabaseIO::free_file_pointer() const { return 0; }
229
230 bool BaseDatabaseIO::ok_nl(bool, std::string *, int *) const { return true; }
231
232 // common
233 void BaseDatabaseIO::put_qa()
234 {
235 struct qa_element
236 {
237 char *qa_record[1][4];
238 };
239
240 size_t num_qa_records = qaRecords.size() / 4;
241
242 if (using_parallel_io() && myProcessor != 0) {}
243 else {
244 std::vector<qa_element> qa(num_qa_records + 1);
245 for (size_t i = 0; i < num_qa_records + 1; i++) {
246 for (int j = 0; j < 4; j++) {
247 qa[i].qa_record[0][j] = new char[MAX_STR_LENGTH + 1];
248 }
249 }
250
251 {
252 int j = 0;
253 for (size_t i = 0; i < num_qa_records; i++) {
254 Ioss::Utils::copy_string(qa[i].qa_record[0][0], qaRecords[j++], MAX_STR_LENGTH + 1);
255 Ioss::Utils::copy_string(qa[i].qa_record[0][1], qaRecords[j++], MAX_STR_LENGTH + 1);
256 Ioss::Utils::copy_string(qa[i].qa_record[0][2], qaRecords[j++], MAX_STR_LENGTH + 1);
257 Ioss::Utils::copy_string(qa[i].qa_record[0][3], qaRecords[j++], MAX_STR_LENGTH + 1);
258 }
259 }
260
261 Ioss::Utils::time_and_date(qa[num_qa_records].qa_record[0][3],
262 qa[num_qa_records].qa_record[0][2], MAX_STR_LENGTH);
263
264 std::string codename = "unknown";
265 std::string version = "unknown";
266
267 if (get_region()->property_exists("code_name")) {
268 codename = get_region()->get_property("code_name").get_string();
269 }
270 if (get_region()->property_exists("code_version")) {
271 version = get_region()->get_property("code_version").get_string();
272 }
273
274 Ioss::Utils::copy_string(qa[num_qa_records].qa_record[0][0], codename, MAX_STR_LENGTH + 1);
275 Ioss::Utils::copy_string(qa[num_qa_records].qa_record[0][1], version, MAX_STR_LENGTH + 1);
276
277 for (size_t i = 0; i < num_qa_records + 1; i++) {
278 for (int j = 0; j < 4; j++) {
279 delete[] qa[i].qa_record[0][j];
280 }
281 }
282 }
283 }
284
285 // common
286 void BaseDatabaseIO::put_info() {}
287
288 // common
289 int BaseDatabaseIO::get_current_state() const
290 {
291 int step = get_region()->get_current_state();
292
293 if (step <= 0) {
294 std::ostringstream errmsg;
295 fmt::print(errmsg,
296 "ERROR: No currently active state. The calling code must call "
297 "Ioss::Region::begin_state(int step)\n"
298 " to set the database timestep from which to read the transient data.\n"
299 " [{}]\n",
300 get_filename());
301 IOSS_ERROR(errmsg);
302 }
303 return step;
304 }
305
306 size_t BaseDatabaseIO::handle_block_ids(const Ioss::EntityBlock *eb, ex_entity_type,
307 Ioss::Map &entity_map, void *ids, size_t num_to_get,
308 size_t) const
309 {
310 /*!
311 * NOTE: "element" is generic for "element", "face", or "edge"
312 *
313 * There are two modes we need to support in this routine:
314 * 1. Initial definition of element map (local->global) and
315 * elemMap.reverse (global->local).
316 * 2. Redefinition of element map via 'reordering' of the original
317 * map when the elements on this processor are the same, but their
318 * order is changed.
319 *
320 * So, there will be two maps the 'elemMap.map' map is a 'direct lookup'
321 * map which maps current local position to global id and the
322 * 'elemMap.reverse' is an associative lookup which maps the
323 * global id to 'original local'. There is also a
324 * 'elemMap.reorder' which is direct lookup and maps current local
325 * position to original local.
326
327 * The ids coming in are the global ids; their position is the
328 * local id -1 (That is, data[0] contains the global id of local
329 * element 1 in this element block). The 'model-local' id is
330 * given by eb_offset + 1 + position:
331 *
332 * int local_position = elemMap.reverse[ElementMap[i+1]]
333 * (the elemMap.map and elemMap.reverse are 1-based)
334 *
335 * But, this assumes 1..numel elements are being output at the same
336 * time; we are actually outputting a blocks worth of elements at a
337 * time, so we need to consider the block offsets.
338 * So... local-in-block position 'i' is index 'eb_offset+i' in
339 * 'elemMap.map' and the 'local_position' within the element
340 * blocks data arrays is 'local_position-eb_offset'. With this, the
341 * position within the data array of this element block is:
342 *
343 * int eb_position =
344 * elemMap.reverse[elemMap.map[eb_offset+i+1]]-eb_offset-1
345 *
346 * To determine which map to update on a call to this function, we
347 * use the following heuristics:
348 * -- If the database state is 'Ioss::STATE_MODEL:', then update the
349 * 'elemMap.reverse'.
350 * -- If the database state is not Ioss::STATE_MODEL, then leave
351 * the 'elemMap.reverse' alone since it corresponds to the
352 * information already written to the database. [May want to add
353 * a Ioss::STATE_REDEFINE_MODEL]
354 * -- Always update elemMap.map to match the passed in 'ids'
355 * array.
356 *
357 * NOTE: the maps are built an element block at a time...
358 * NOTE: The mapping is done on TRANSIENT fields only; MODEL fields
359 * should be in the original order...
360 */
361
362 // Overwrite this portion of the 'elemMap.map', but keep other
363 // parts as they were. We are adding elements starting at position
364 // 'eb_offset+offset' and ending at
365 // 'eb_offset+offset+num_to_get'. If the entire block is being
366 // processed, this reduces to the range 'eb_offset..eb_offset+my_element_count'
367
368 bool in_define = (dbState == Ioss::STATE_MODEL) || (dbState == Ioss::STATE_DEFINE_MODEL);
369 int64_t eb_offset = eb->get_offset();
370 if (int_byte_size_api() == 4) {
371 entity_map.set_map(static_cast<int *>(ids), num_to_get, eb_offset, in_define);
372 }
373 else {
374 entity_map.set_map(static_cast<int64_t *>(ids), num_to_get, eb_offset, in_define);
375 }
376
377 // Now, if the state is Ioss::STATE_MODEL, output this portion of
378 // the entity number map...
379 return num_to_get;
380 }
381
382 // common
383 void BaseDatabaseIO::compute_block_membership_nl(Ioss::SideBlock *efblock,
384 Ioss::NameList &block_membership) const
385 {
386 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
387 assert(Ioss::Utils::check_block_order(element_blocks));
388
389 Ioss::Int64Vector block_ids(element_blocks.size());
390 if (block_ids.size() == 1) {
391 block_ids[0] = 1;
392 }
393 else {
394 Ioss::Int64Vector element_side;
395 if (int_byte_size_api() == 4) {
396 Ioss::IntVector es32;
397 efblock->get_field_data("element_side", es32);
398 element_side.resize(es32.size());
399 std::copy(es32.begin(), es32.end(), element_side.begin());
400 }
401 else {
402 efblock->get_field_data("element_side", element_side);
403 }
404
405 size_t number_sides = element_side.size() / 2;
406 Ioss::ElementBlock *block = nullptr;
407 for (size_t iel = 0; iel < number_sides; iel++) {
408 int64_t elem_id = element_side[2 * iel]; // Vector contains both element and side.
409 elem_id = elemMap.global_to_local(elem_id);
410 if (block == nullptr || !block->contains(elem_id)) {
411 block = get_region()->get_element_block(elem_id);
412 assert(block != nullptr);
413 size_t block_order = block->get_property("original_block_order").get_int();
414 assert(block_order < block_ids.size());
415 block_ids[block_order] = 1;
416 }
417 }
418 }
419
420 // Synchronize among all processors....
421 if (isParallel) {
422 util().global_array_minmax(block_ids, Ioss::ParallelUtils::DO_MAX);
423 }
424
425 for (const auto &block : element_blocks) {
426 size_t block_order = block->get_property("original_block_order").get_int();
427 assert(block_order < block_ids.size());
428 if (block_ids[block_order] == 1) {
429 if (!Ioss::Utils::block_is_omitted(block)) {
430 block_membership.push_back(block->name());
431 }
432 }
433 }
434 }
435
436 int64_t BaseDatabaseIO::put_field_internal(const Ioss::Region * /* region */,
437 const Ioss::Field &field, void *data,
438 size_t data_size) const
439 {
440 // For now, assume that all TRANSIENT fields on a region
441 // are REDUCTION fields (1 value). We need to gather these
442 // and output them all at one time. The storage location is a
443 // 'globalVariables' array
444 {
445 Ioss::Field::RoleType role = field.get_role();
446 size_t num_to_get = field.verify(data_size);
447
448 if ((role == Ioss::Field::TRANSIENT || role == Ioss::Field::REDUCTION) && num_to_get == 1) {
449 store_reduction_field(field, get_region(), data);
450 }
451 else if (num_to_get != 1) {
452 // There should have been a warning/error message printed to the
453 // log file earlier for this, so we won't print anything else
454 // here since it would be printed for each and every timestep....
455 ;
456 }
457 else {
458 std::ostringstream errmsg;
459 fmt::print(
460 errmsg,
461 "ERROR: The variable named '{}' is of the wrong type. A region variable must be of type"
462 " TRANSIENT or REDUCTION.\n"
463 "This is probably an internal error; please notify [email protected]",
464 field.get_name());
465 IOSS_ERROR(errmsg);
466 }
467 return num_to_get;
468 }
469 }
470
471 namespace {
472 // common
473 template <typename T>
474 void generate_block_truth_table(const VariableNameMap &variables, Ioss::IntVector &truth_table,
475 std::vector<T *> &blocks, char field_suffix_separator)
476 {
477 size_t block_count = blocks.size();
478 size_t var_count = variables.size();
479
480 if (var_count == 0 || block_count == 0) {
481 return;
482 }
483
484 truth_table.resize(block_count * var_count);
485
486 // Fill in the truth table. It is conceptually a two-dimensional array of
487 // the form 'array[num_blocks][num_element_var]'. In C++,
488 // the values for the first block are first, followed by
489 // next block, ...
490 size_t offset = 0;
491 for (const auto &block : blocks) {
492 // Get names of all transient and reduction fields...
493 Ioss::NameList results_fields = block->field_describe(Ioss::Field::TRANSIENT);
494 block->field_describe(Ioss::Field::REDUCTION, &results_fields);
495
496 for (const auto &fn : results_fields) {
497 Ioss::Field field = block->get_field(fn);
498 Ioss::Field::BasicType ioss_type = field.get_type();
499
500 int re_im = 1;
501 if (ioss_type == Ioss::Field::COMPLEX) {
502 re_im = 2;
503 }
504 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
505 for (int i = 1; i <= field.get_component_count(Ioss::Field::InOut::INPUT); i++) {
506 std::string var_string =
507 field.get_component_name(i, Ioss::Field::InOut::INPUT, field_suffix_separator);
508 // Find position of 'var_string' in 'variables'
509 auto VN = variables.find(var_string);
510 if (VN != variables.end()) {
511 // Index '(*VN).second' is 1-based...
512 truth_table[offset + (*VN).second - 1] = 1;
513 }
514 }
515 }
516 }
517 offset += var_count;
518 }
519 assert(offset == var_count * block_count);
520 }
521 } // namespace
522 // common
523 void BaseDatabaseIO::store_reduction_field(const Ioss::Field &field,
524 const Ioss::GroupingEntity *ge, void *variables) const
525 {
526 Ioss::Field::BasicType ioss_type = field.get_type();
527 assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER ||
528 ioss_type == Ioss::Field::INT64 || ioss_type == Ioss::Field::COMPLEX);
529 auto *rvar = static_cast<double *>(variables);
530 auto *ivar = static_cast<int *>(variables);
531 auto *ivar64 = static_cast<int64_t *>(variables);
532
533 auto id = ge->get_optional_property("id", 0);
534
535 auto type = Ioexnl::map_exodus_type(ge->type());
536
537 // Note that if the field's basic type is COMPLEX, then each component of
538 // the VariableType is a complex variable consisting of a real and
539 // imaginary part. Since exodus cannot handle complex variables,
540 // we have to output a (real and imaginary) X (number of
541 // components) fields. For example, if V is a 3d vector of complex
542 // data, the data in the 'variables' array are v_x, v.im_x, v_y,
543 // v.im_y, v_z, v.im_z which need to be output in six separate
544 // exodus fields. These fields were already defined in
545 // "write_results_metadata".
546
547 // get number of components, cycle through each component
548 // and add suffix to base 'field_name'. Look up index
549 // of this name in 'm_variables[EX_GLOBAL]' map
550 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
551 int var_index = 0;
552
553 int re_im = 1;
554 if (field.get_type() == Ioss::Field::COMPLEX) {
555 re_im = 2;
556 }
557 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
558 for (int i = 0; i < comp_count; i++) {
559 std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1);
560
561 #if GLOBALS_ARE_TRANSIENT
562 if (type == EX_GLOBAL) {
563 SMART_ASSERT(m_variables[type].find(var_name) != m_variables[type].end())(type)(var_name);
564 var_index = m_variables[type].find(var_name)->second;
565 }
566 else {
567 SMART_ASSERT(m_reductionVariables[type].find(var_name) !=
568 m_reductionVariables[type].end())
569 (type)(var_name);
570 var_index = m_reductionVariables[type].find(var_name)->second;
571 }
572 #else
573 SMART_ASSERT(m_reductionVariables[type].find(var_name) != m_reductionVariables[type].end())
574 (type)(var_name);
575 var_index = m_reductionVariables[type].find(var_name)->second;
576 #endif
577
578 SMART_ASSERT(static_cast<int>(m_reductionValues[type][id].size()) >= var_index)
579 (id)(m_reductionValues[type][id].size())(var_index);
580
581 // Transfer from 'variables' array.
582 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
583 m_reductionValues[type][id][var_index - 1] = rvar[i];
584 }
585 else if (ioss_type == Ioss::Field::INTEGER) {
586 m_reductionValues[type][id][var_index - 1] = ivar[i];
587 }
588 else if (ioss_type == Ioss::Field::INT64) {
589 m_reductionValues[type][id][var_index - 1] = ivar64[i]; // FIX 64 UNSAFE
590 }
591 }
592 }
593 }
594
595 // common
596 void BaseDatabaseIO::get_reduction_field(const Ioss::Field &field, const Ioss::GroupingEntity *ge,
597 void *variables) const
598 {
599 auto id = ge->get_optional_property("id", 0);
600 auto type = Ioexnl::map_exodus_type(ge->type());
601
602 Ioss::Field::BasicType ioss_type = field.get_type();
603 assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER ||
604 ioss_type == Ioss::Field::INT64);
605 auto *rvar = static_cast<double *>(variables);
606 auto *ivar = static_cast<int *>(variables);
607 auto *i64var = static_cast<int64_t *>(variables);
608
609 // get number of components, cycle through each component
610 // and add suffix to base 'field_name'. Look up index
611 // of this name in 'm_variables[type]' map
612
613 int comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
614 for (int i = 0; i < comp_count; i++) {
615 int var_index = 0;
616 std::string var_name = get_component_name(field, Ioss::Field::InOut::INPUT, i + 1);
617
618 #if GLOBALS_ARE_TRANSIENT
619 if (type == EX_GLOBAL) {
620 assert(m_variables[type].find(var_name) != m_variables[type].end());
621 var_index = m_variables[type].find(var_name)->second;
622 }
623 else {
624 assert(m_reductionVariables[type].find(var_name) != m_reductionVariables[type].end());
625 var_index = m_reductionVariables[type].find(var_name)->second;
626 }
627
628 assert(static_cast<int>(m_reductionValues[type][id].size()) >= var_index);
629 #else
630 SMART_ASSERT(m_reductionVariables[type].find(var_name) != m_reductionVariables[type].end())
631 (type)(var_name);
632 var_index = m_reductionVariables[type].find(var_name)->second;
633 SMART_ASSERT(static_cast<int>(m_reductionValues[type][id].size()) >= var_index);
634 #endif
635 // Transfer to 'variables' array.
636 if (ioss_type == Ioss::Field::REAL) {
637 rvar[i] = m_reductionValues[type][id][var_index - 1];
638 }
639 else if (ioss_type == Ioss::Field::INT64) {
640 i64var[i] = static_cast<int64_t>(m_reductionValues[type][id][var_index - 1]);
641 }
642 else if (ioss_type == Ioss::Field::INTEGER) {
643 ivar[i] = static_cast<int>(m_reductionValues[type][id][var_index - 1]);
644 }
645 }
646 }
647
648 // common
649 void BaseDatabaseIO::write_reduction_fields() const {}
650
651 // common
652 bool BaseDatabaseIO::begin_nl(Ioss::State state)
653 {
654 dbState = state;
655 return true;
656 }
657
658 // common
659 bool BaseDatabaseIO::end_nl(Ioss::State state)
660 {
661 // Transitioning out of state 'state'
662 assert(state == dbState);
663 switch (state) {
664 case Ioss::STATE_DEFINE_MODEL:
665 if (!is_input()) {
666 write_meta_data(open_create_behavior());
667 }
668 break;
669 case Ioss::STATE_DEFINE_TRANSIENT:
670 if (!is_input()) {
671 write_results_metadata(true, open_create_behavior());
672 }
673 break;
674 default: // ignore everything else...
675 break;
676 }
677
678 {
679 if (!is_input()) {
680 if (minimizeOpenFiles) {
681 free_file_pointer();
682 }
683 }
684 dbState = Ioss::STATE_UNKNOWN;
685 }
686
687 return true;
688 }
689
690 bool BaseDatabaseIO::begin_state_nl(int, double)
691 {
692 if (!is_input()) {
693 // Zero global variable array...
694 for (const auto &type : exodus_types) {
695 auto &id_values = m_reductionValues[type];
696 for (auto &values : id_values) {
697 auto &vals = values.second;
698 std::fill(vals.begin(), vals.end(), 0.0);
699 }
700 }
701 }
702 return true;
703 }
704
705 // common
706 bool BaseDatabaseIO::end_state_nl(int state, double time)
707 {
708 if (!is_input()) {
709 write_reduction_fields();
710 time /= timeScaleFactor;
711 finalize_write(state, time);
712 if (minimizeOpenFiles) {
713 free_file_pointer();
714 }
715 }
716 return true;
717 }
718
719 // common
720 void BaseDatabaseIO::write_results_metadata(bool gather_data,
721 Ioss::IfDatabaseExistsBehavior behavior)
722 {
723 if (gather_data) {
724 int glob_index = 0;
725 #if GLOBALS_ARE_TRANSIENT
726 glob_index = gather_names(m_variables[EX_GLOBAL], get_region(), glob_index, true);
727 #else
728 glob_index = gather_names(m_reductionVariables[EX_GLOBAL], get_region(), glob_index, true);
729 #endif
730 m_reductionValues[EX_GLOBAL][0].resize(glob_index);
731
732 const Ioss::NodeBlockContainer &node_blocks = get_region()->get_node_blocks();
733 assert(node_blocks.size() <= 1);
734 internal_gather_results_metadata(EX_NODE_BLOCK, node_blocks);
735
736 const Ioss::EdgeBlockContainer &edge_blocks = get_region()->get_edge_blocks();
737 internal_gather_results_metadata(EX_EDGE_BLOCK, edge_blocks);
738
739 const Ioss::FaceBlockContainer &face_blocks = get_region()->get_face_blocks();
740 internal_gather_results_metadata(EX_FACE_BLOCK, face_blocks);
741
742 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
743 internal_gather_results_metadata(EX_ELEM_BLOCK, element_blocks);
744
745 const Ioss::NodeSetContainer &nodesets = get_region()->get_nodesets();
746 internal_gather_results_metadata(EX_NODE_SET, nodesets);
747
748 const Ioss::EdgeSetContainer &edgesets = get_region()->get_edgesets();
749 internal_gather_results_metadata(EX_EDGE_SET, edgesets);
750
751 const Ioss::FaceSetContainer &facesets = get_region()->get_facesets();
752 internal_gather_results_metadata(EX_FACE_SET, facesets);
753
754 const Ioss::ElementSetContainer &elementsets = get_region()->get_elementsets();
755 internal_gather_results_metadata(EX_ELEM_SET, elementsets);
756
757 const auto &blobs = get_region()->get_blobs();
758 internal_gather_results_metadata(EX_BLOB, blobs);
759
760 const auto &assemblies = get_region()->get_assemblies();
761 internal_gather_results_metadata(EX_ASSEMBLY, assemblies);
762
763 {
764 int index = 0;
765 const Ioss::SideSetContainer &sidesets = get_region()->get_sidesets();
766 for (const auto &sideset : sidesets) {
767 const Ioss::SideBlockContainer &side_blocks = sideset->get_side_blocks();
768 for (const auto &block : side_blocks) {
769 glob_index = gather_names(m_reductionVariables[EX_SIDE_SET], block, glob_index, true);
770 index = gather_names(m_variables[EX_SIDE_SET], block, index, false);
771 }
772 }
773 generate_sideset_truth_table();
774 }
775 }
776
777 if (behavior != Ioss::DB_APPEND && behavior != Ioss::DB_MODIFY) {
778 ex_var_params exo_params{};
779 #if GLOBALS_ARE_TRANSIENT
780 exo_params.num_glob = m_variables[EX_GLOBAL].size();
781 #else
782 exo_params.num_glob = m_reductionVariables[EX_GLOBAL].size();
783 #endif
784 exo_params.num_node = m_variables[EX_NODE_BLOCK].size();
785 exo_params.num_edge = m_variables[EX_EDGE_BLOCK].size();
786 exo_params.num_face = m_variables[EX_FACE_BLOCK].size();
787 exo_params.num_elem = m_variables[EX_ELEM_BLOCK].size();
788 exo_params.num_nset = m_variables[EX_NODE_SET].size();
789 exo_params.num_eset = m_variables[EX_EDGE_SET].size();
790 exo_params.num_fset = m_variables[EX_FACE_SET].size();
791 exo_params.num_sset = m_variables[EX_SIDE_SET].size();
792 exo_params.num_elset = m_variables[EX_ELEM_SET].size();
793
794 exo_params.edge_var_tab = Data(m_truthTable[EX_EDGE_BLOCK]);
795 exo_params.face_var_tab = Data(m_truthTable[EX_FACE_BLOCK]);
796 exo_params.elem_var_tab = Data(m_truthTable[EX_ELEM_BLOCK]);
797 exo_params.nset_var_tab = Data(m_truthTable[EX_NODE_SET]);
798 exo_params.eset_var_tab = Data(m_truthTable[EX_EDGE_SET]);
799 exo_params.fset_var_tab = Data(m_truthTable[EX_FACE_SET]);
800 exo_params.sset_var_tab = Data(m_truthTable[EX_SIDE_SET]);
801 exo_params.elset_var_tab = Data(m_truthTable[EX_ELEM_SET]);
802
803 if (isParallel) {
804 // Check consistency among all processors. They should all
805 // have the same number of each variable type...
806 // The called function will throw an exception if the counts differ.
807 check_variable_consistency(exo_params, myProcessor, get_filename(), util());
808 }
809
810 {
811 for (const auto &type : exodus_types) {
812 output_results_names(type, m_variables[type], false);
813 output_results_names(type, m_reductionVariables[type], true);
814 }
815 }
816 }
817 }
818
819 // common
820 template <typename T>
821 void BaseDatabaseIO::internal_gather_results_metadata(ex_entity_type type,
822 std::vector<T *> entities)
823 {
824 int index = 0;
825 int red_index = 0;
826 for (const auto &entity : entities) {
827 red_index = gather_names(m_reductionVariables[type], entity, red_index, true);
828 index = gather_names(m_variables[type], entity, index, false);
829 }
830
831 #if GLOBALS_ARE_TRANSIENT
832 size_t value_size =
833 type == EX_GLOBAL ? m_variables[type].size() : m_reductionVariables[type].size();
834 #else
835 size_t value_size = m_reductionVariables[type].size();
836 #endif
837 for (const auto &entity : entities) {
838 auto id = entity->get_optional_property("id", 0);
839 m_reductionValues[type][id].resize(value_size);
840 }
841
842 generate_block_truth_table(m_variables[type], m_truthTable[type], entities,
843 get_field_separator());
844 }
845
846 // common
847 int BaseDatabaseIO::gather_names(VariableNameMap &variables, const Ioss::GroupingEntity *ge,
848 int index, bool reduction)
849 {
850 int new_index = index;
851
852 auto type = Ioexnl::map_exodus_type(ge->type());
853 bool nblock = (type == EX_NODE_BLOCK);
854
855 // Get names of all transient and reduction fields...
856 Ioss::NameList results_fields;
857 if (reduction) {
858 ge->field_describe(Ioss::Field::REDUCTION, &results_fields);
859 }
860
861 if (!reduction || type == EX_GLOBAL) {
862 ge->field_describe(Ioss::Field::TRANSIENT, &results_fields);
863 }
864
865 // Some applications will set the index on the field to get a specific
866 // ordering of the fields. For exodus, we typically use that to get the
867 // same output ordering as the input ordering. The output from `field_describe`
868 // comes back sorted on field names. Lets check whether any of the fields
869 // have an index set and if so, then sort the fields based on the index...
870 std::vector<Ioss::Field> fields;
871 fields.reserve(results_fields.size());
872 for (const auto &name : results_fields) {
873 fields.push_back(ge->get_field(name));
874 }
875 std::stable_sort(fields.begin(), fields.end(), [](const Ioss::Field &a, const Ioss::Field &b) {
876 return a.get_index() < b.get_index();
877 });
878
879 // NOTE: For exodusII, the convention is that the displacement
880 // fields are the first 'ndim' fields in the file.
881 // Try to find a likely displacement field
882 std::string disp_name;
883 bool has_disp = false;
884 if (!reduction && nblock && new_index == 0) {
885 has_disp = find_displacement_field(results_fields, ge, spatialDimension, &disp_name);
886 if (has_disp) {
887 new_index += spatialDimension;
888 }
889 }
890
891 int save_index = 0;
892 for (const auto &field : fields) {
893 if (has_disp && field.get_name() == disp_name && new_index != 0) {
894 save_index = new_index;
895 new_index = 0;
896 }
897
898 int re_im = 1;
899 if (field.get_type() == Ioss::Field::COMPLEX) {
900 re_im = 2;
901 }
902 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
903 for (int i = 1; i <= field.get_component_count(Ioss::Field::InOut::OUTPUT); i++) {
904 std::string var_string = get_component_name(field, Ioss::Field::InOut::OUTPUT, i);
905
906 if (variables.find(var_string) == variables.end()) {
907 variables.insert(VNMValuePair(var_string, ++new_index));
908 }
909 }
910 }
911 if (has_disp && field.get_name() == disp_name) {
912 new_index = save_index;
913 }
914 }
915 return new_index;
916 }
917
918 // common
919 void BaseDatabaseIO::generate_sideset_truth_table()
920 {
921 size_t var_count = m_variables[EX_SIDE_SET].size();
922
923 if (var_count == 0 || m_groupCount[EX_SIDE_SET] == 0) {
924 return;
925 }
926
927 // Member variable. Will be deleted in destructor...
928 m_truthTable[EX_SIDE_SET].resize(m_groupCount[EX_SIDE_SET] * var_count);
929
930 // Fill in the truth table. It is conceptually a two-dimensional array of
931 // the form 'array[num_blocks][num_var]'. In C++,
932 // the values for the first block are first, followed by
933 // next block, ...
934 size_t offset = 0;
935
936 const Ioss::SideSetContainer &sidesets = get_region()->get_sidesets();
937 for (const auto &sideset : sidesets) {
938
939 const Ioss::SideBlockContainer &side_blocks = sideset->get_side_blocks();
940 for (const auto &block : side_blocks) {
941 // See if this sideblock has a corresponding entry in the sideset list.
942 if (block->property_exists("invalid")) {
943 continue;
944 }
945
946 // Get names of all transient and reduction fields...
947 Ioss::NameList results_fields = block->field_describe(Ioss::Field::TRANSIENT);
948 block->field_describe(Ioss::Field::REDUCTION, &results_fields);
949
950 for (const auto &fn : results_fields) {
951 Ioss::Field field = block->get_field(fn);
952 Ioss::Field::BasicType ioss_type = field.get_type();
953
954 int re_im = 1;
955 if (ioss_type == Ioss::Field::COMPLEX) {
956 re_im = 2;
957 }
958 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
959 for (int i = 1; i <= field.get_component_count(Ioss::Field::InOut::OUTPUT); i++) {
960 std::string var_string = get_component_name(field, Ioss::Field::InOut::OUTPUT, i);
961 // Find position of 'var_string' in 'm_variables[]'
962 auto VN = m_variables[EX_SIDE_SET].find(var_string);
963 if (VN != m_variables[EX_SIDE_SET].end()) {
964 // Index '(*VN).second' is 1-based...
965 m_truthTable[EX_SIDE_SET][offset + (*VN).second - 1] = 1;
966 }
967 }
968 }
969 }
970 }
971 offset += var_count;
972 }
973 assert(offset == var_count * m_groupCount[EX_SIDE_SET]);
974 }
975
976 // common
977 void BaseDatabaseIO::output_results_names(ex_entity_type, VariableNameMap &, bool) const {}
978
979 // common
980 // Handle special output time requests -- primarily restart (cycle, overwrite)
981 // Given the global region step, return the step on the database...
982 int BaseDatabaseIO::get_database_step(int global_step) const
983 {
984 if (get_file_per_state()) {
985 return 1;
986 }
987
988 assert(overlayCount >= 0 && cycleCount >= 0);
989 if (overlayCount == 0 && cycleCount == 0) {
990 return global_step;
991 }
992
993 int local_step = global_step - 1;
994 local_step /= (overlayCount + 1);
995 if (cycleCount > 0) {
996 local_step %= cycleCount;
997 }
998 return local_step + 1;
999 }
1000
1001 // common
1002 void BaseDatabaseIO::flush_database_nl() const {}
1003
1004 void BaseDatabaseIO::finalize_write(int, double) {}
1005
1006 void BaseDatabaseIO::common_write_metadata(Ioss::IfDatabaseExistsBehavior behavior)
1007 {
1008 Ioss::Region *region = get_region();
1009
1010 // Verify that exodus supports the mesh_type...
1011 if (region->mesh_type() != Ioss::MeshType::UNSTRUCTURED) {
1012 std::ostringstream errmsg;
1013 fmt::print(errmsg,
1014 "ERROR: The mesh type is '{}' which Exodus does not support.\n"
1015 " Only 'Unstructured' is supported at this time.\n",
1016 region->mesh_type_string());
1017 IOSS_ERROR(errmsg);
1018 }
1019
1020 const Ioss::NodeBlockContainer &node_blocks = region->get_node_blocks();
1021 assert(node_blocks.size() <= 1);
1022 if (!node_blocks.empty()) {
1023 Ioexnl::get_id(node_blocks[0], &ids_);
1024 nodeCount = node_blocks[0]->entity_count();
1025 spatialDimension = node_blocks[0]->get_property("component_degree").get_int();
1026 }
1027 else {
1028 spatialDimension = 1;
1029 }
1030
1031 // Assemblies --
1032 {
1033 const auto &assemblies = region->get_assemblies();
1034 if (behavior != Ioss::DB_MODIFY) {
1035 // Set ids of all entities that have "id" property...
1036 for (const auto &assem : assemblies) {
1037 Ioexnl::set_id(assem, &ids_);
1038 }
1039
1040 for (const auto &assem : assemblies) {
1041 Ioexnl::get_id(assem, &ids_);
1042 }
1043 }
1044 m_groupCount[EX_ASSEMBLY] = assemblies.size();
1045 }
1046
1047 // Blobs --
1048 {
1049 const auto &blobs = region->get_blobs();
1050 // Set ids of all entities that have "id" property...
1051 if (behavior != Ioss::DB_MODIFY) {
1052 for (const auto &blob : blobs) {
1053 Ioexnl::set_id(blob, &ids_);
1054 }
1055
1056 for (const auto &blob : blobs) {
1057 Ioexnl::get_id(blob, &ids_);
1058 }
1059 }
1060 m_groupCount[EX_BLOB] = blobs.size();
1061 }
1062
1063 // Edge Blocks --
1064 {
1065 const Ioss::EdgeBlockContainer &edge_blocks = region->get_edge_blocks();
1066 assert(Ioss::Utils::check_block_order(edge_blocks));
1067 // Set ids of all entities that have "id" property...
1068 if (behavior != Ioss::DB_MODIFY) {
1069 for (const auto &edge_block : edge_blocks) {
1070 Ioexnl::set_id(edge_block, &ids_);
1071 }
1072
1073 edgeCount = 0;
1074 for (const auto &edge_block : edge_blocks) {
1075 edgeCount += edge_block->entity_count();
1076 // Set ids of all entities that do not have "id" property...
1077 Ioexnl::get_id(edge_block, &ids_);
1078 }
1079 }
1080 m_groupCount[EX_EDGE_BLOCK] = edge_blocks.size();
1081 }
1082
1083 // Face Blocks --
1084 {
1085 const Ioss::FaceBlockContainer &face_blocks = region->get_face_blocks();
1086 assert(Ioss::Utils::check_block_order(face_blocks));
1087 // Set ids of all entities that have "id" property...
1088 if (behavior != Ioss::DB_MODIFY) {
1089 for (const auto &face_block : face_blocks) {
1090 Ioexnl::set_id(face_block, &ids_);
1091 }
1092
1093 faceCount = 0;
1094 for (auto &face_block : face_blocks) {
1095 faceCount += face_block->entity_count();
1096 // Set ids of all entities that do not have "id" property...
1097 Ioexnl::get_id(face_block, &ids_);
1098 }
1099 }
1100 m_groupCount[EX_FACE_BLOCK] = face_blocks.size();
1101 }
1102
1103 // Element Blocks --
1104 {
1105 const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks();
1106 assert(Ioss::Utils::check_block_order(element_blocks));
1107 if (behavior != Ioss::DB_MODIFY) {
1108 // Set ids of all entities that have "id" property...
1109 for (const auto &element_block : element_blocks) {
1110 Ioexnl::set_id(element_block, &ids_);
1111 }
1112 }
1113 elementCount = 0;
1114 Ioss::Int64Vector element_counts;
1115 element_counts.reserve(element_blocks.size());
1116 for (const auto &element_block : element_blocks) {
1117 elementCount += element_block->entity_count();
1118 element_counts.push_back(element_block->entity_count());
1119 // Set ids of all entities that do not have "id" property...
1120 if (behavior != Ioss::DB_MODIFY) {
1121 Ioexnl::get_id(element_block, &ids_);
1122 }
1123 }
1124 m_groupCount[EX_ELEM_BLOCK] = element_blocks.size();
1125
1126 if (isParallel) {
1127 // Set "global_entity_count" property on all blocks.
1128 // Used to skip output on "globally" empty blocks.
1129 Ioss::Int64Vector global_counts(element_counts.size());
1130 util().global_count(element_counts, global_counts);
1131 size_t idx = 0;
1132 for (const auto &element_block : element_blocks) {
1133 element_block->property_add(Ioss::Property("global_entity_count", global_counts[idx++]));
1134 }
1135 }
1136 }
1137
1138 // NodeSets ...
1139 {
1140 const Ioss::NodeSetContainer &nodesets = region->get_nodesets();
1141 if (behavior != Ioss::DB_MODIFY) {
1142 for (const auto &set : nodesets) {
1143 Ioexnl::set_id(set, &ids_);
1144 }
1145
1146 for (const auto &set : nodesets) {
1147 Ioexnl::get_id(set, &ids_);
1148 }
1149 }
1150 m_groupCount[EX_NODE_SET] = nodesets.size();
1151 }
1152
1153 // EdgeSets ...
1154 {
1155 const Ioss::EdgeSetContainer &edgesets = region->get_edgesets();
1156 if (behavior != Ioss::DB_MODIFY) {
1157 for (const auto &set : edgesets) {
1158 Ioexnl::set_id(set, &ids_);
1159 }
1160
1161 for (const auto &set : edgesets) {
1162 Ioexnl::get_id(set, &ids_);
1163 }
1164 }
1165 m_groupCount[EX_EDGE_SET] = edgesets.size();
1166 }
1167
1168 // FaceSets ...
1169 {
1170 const Ioss::FaceSetContainer &facesets = region->get_facesets();
1171 if (behavior != Ioss::DB_MODIFY) {
1172 for (const auto &set : facesets) {
1173 Ioexnl::set_id(set, &ids_);
1174 }
1175
1176 for (const auto &set : facesets) {
1177 Ioexnl::get_id(set, &ids_);
1178 }
1179 }
1180 m_groupCount[EX_FACE_SET] = facesets.size();
1181 }
1182
1183 // ElementSets ...
1184 {
1185 const Ioss::ElementSetContainer &elementsets = region->get_elementsets();
1186 if (behavior != Ioss::DB_MODIFY) {
1187 for (const auto &set : elementsets) {
1188 Ioexnl::set_id(set, &ids_);
1189 }
1190
1191 for (const auto &set : elementsets) {
1192 Ioexnl::get_id(set, &ids_);
1193 }
1194 }
1195 m_groupCount[EX_ELEM_SET] = elementsets.size();
1196 }
1197
1198 // SideSets ...
1199 {
1200 const Ioss::SideSetContainer &ssets = region->get_sidesets();
1201 if (behavior != Ioss::DB_MODIFY) {
1202 for (const auto &set : ssets) {
1203 Ioexnl::set_id(set, &ids_);
1204 }
1205 }
1206 // Get entity counts for all face sets... Create SideSets.
1207 for (const auto &set : ssets) {
1208 if (behavior != Ioss::DB_MODIFY) {
1209 Ioexnl::get_id(set, &ids_);
1210 }
1211 int64_t id = set->get_property("id").get_int();
1212 int64_t entity_count = 0;
1213 int64_t df_count = 0;
1214
1215 const Ioss::SideBlockContainer &side_blocks = set->get_side_blocks();
1216 for (const auto &block : side_blocks) {
1217 // Add "*_offset" properties to specify at what offset
1218 // the data for this block appears in the containing set.
1219 auto *new_block = const_cast<Ioss::SideBlock *>(block);
1220 new_block->property_add(Ioss::Property("set_offset", entity_count));
1221 new_block->property_add(Ioss::Property("set_df_offset", df_count));
1222
1223 // If combining sideblocks into sidesets on output, then
1224 // the id of the sideblock must be the same as the sideset
1225 // id.
1226 new_block->property_update("id", id);
1227 new_block->property_update("guid", util().generate_guid(id));
1228
1229 entity_count += block->entity_count();
1230 df_count += block->get_property("distribution_factor_count").get_int();
1231 }
1232 auto *new_entity = const_cast<Ioss::SideSet *>(set);
1233 new_entity->property_add(Ioss::Property("entity_count", entity_count));
1234 new_entity->property_add(Ioss::Property("distribution_factor_count", df_count));
1235 }
1236 m_groupCount[EX_SIDE_SET] = ssets.size();
1237 }
1238 }
1239
1240 void BaseDatabaseIO::output_other_metadata()
1241 {
1242 // Write attribute names (if any)...
1243 write_attribute_names(get_file_pointer(), EX_NODE_SET, get_region()->get_nodesets());
1244 write_attribute_names(get_file_pointer(), EX_EDGE_SET, get_region()->get_edgesets());
1245 write_attribute_names(get_file_pointer(), EX_FACE_SET, get_region()->get_facesets());
1246 write_attribute_names(get_file_pointer(), EX_ELEM_SET, get_region()->get_elementsets());
1247 write_attribute_names(get_file_pointer(), EX_NODE_BLOCK, get_region()->get_node_blocks());
1248 write_attribute_names(get_file_pointer(), EX_EDGE_BLOCK, get_region()->get_edge_blocks());
1249 write_attribute_names(get_file_pointer(), EX_FACE_BLOCK, get_region()->get_face_blocks());
1250 write_attribute_names(get_file_pointer(), EX_ELEM_BLOCK, get_region()->get_element_blocks());
1251 write_attribute_names(get_file_pointer(), EX_ASSEMBLY, get_region()->get_assemblies());
1252 write_attribute_names(get_file_pointer(), EX_BLOB, get_region()->get_blobs());
1253
1254 // Write "reduction" attributes...
1255 std::vector<Ioss::Region *> regions;
1256 regions.push_back(get_region());
1257 Ioexnl::write_reduction_attributes(get_file_pointer(), regions);
1258 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_nodesets());
1259 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_nodesets());
1260 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_edgesets());
1261 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_facesets());
1262 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_elementsets());
1263 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_node_blocks());
1264 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_edge_blocks());
1265 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_face_blocks());
1266 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_element_blocks());
1267 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_assemblies());
1268 Ioexnl::write_reduction_attributes(get_file_pointer(), get_region()->get_blobs());
1269
1270 // Write coordinate names...
1271
1272 // Determine number of node, element maps (client-specified)
1273 // Set the index/order of the maps for later output.
1274 // Note that some fields have more than a single component and each component maps to a
1275 // different map
1276 size_t node_map_cnt = 0;
1277 if (get_region()->get_property("node_block_count").get_int() > 0) {
1278 auto *node_block = get_region()->get_node_blocks()[0];
1279 auto node_map_fields = node_block->field_describe(Ioss::Field::MAP);
1280 for (const auto &field_name : node_map_fields) {
1281 const auto &field = node_block->get_fieldref(field_name);
1282 if (field.get_index() == 0) {
1283 field.set_index(node_map_cnt + 1);
1284 }
1285 node_map_cnt += field.get_component_count(Ioss::Field::InOut::OUTPUT);
1286 }
1287 }
1288
1289 Ioss::NameList elem_map_fields;
1290 const auto &blocks = get_region()->get_element_blocks();
1291 for (const auto &block : blocks) {
1292 block->field_describe(Ioss::Field::MAP, &elem_map_fields);
1293 }
1294
1295 Ioss::Utils::uniquify(elem_map_fields);
1296
1297 // Now need to set the map index on any element map fields...
1298 // Note that not all blocks will potentially have all maps...
1299 size_t elem_map_cnt = 0;
1300 for (const auto &field_name : elem_map_fields) {
1301 int comp_count = 0;
1302 for (const auto &block : blocks) {
1303 if (block->field_exists(field_name)) {
1304 auto &field = block->get_fieldref(field_name);
1305 if (field.get_index() == 0) {
1306 field.set_index(elem_map_cnt + 1);
1307 }
1308 // Assumes all maps of a type have same component count
1309 comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1310 }
1311 }
1312 elem_map_cnt += comp_count;
1313 }
1314
1315 if (node_map_cnt > 0) {
1316 char **names = get_name_array(node_map_cnt, maximumNameLength);
1317 auto *node_block =
1318 get_region()->get_node_blocks()[0]; // If there are node_maps, then there is a node_block
1319 auto node_map_fields = node_block->field_describe(Ioss::Field::MAP);
1320 for (const auto &field_name : node_map_fields) {
1321 const auto &field = node_block->get_fieldref(field_name);
1322 int component_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1323 if (component_count == 1) {
1324 Ioss::Utils::copy_string(names[field.get_index() - 1], field_name, maximumNameLength + 1);
1325 }
1326 else {
1327 for (int i = 0; i < component_count; i++) {
1328 auto name = fmt::format("{}:{}", field_name, i + 1);
1329 Ioss::Utils::copy_string(names[field.get_index() + i - 1], name, maximumNameLength + 1);
1330 }
1331 }
1332 }
1333 delete_name_array(names, node_map_cnt);
1334 }
1335
1336 if (elem_map_cnt > 0) {
1337 char **names = get_name_array(elem_map_cnt, maximumNameLength);
1338 for (const auto &field_name : elem_map_fields) {
1339 // Now, we need to find an element block that has this field...
1340 for (const auto &block : blocks) {
1341 if (block->field_exists(field_name)) {
1342 const auto &field = block->get_fieldref(field_name);
1343 int component_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1344 if (component_count == 1) {
1345 Ioss::Utils::copy_string(names[field.get_index() - 1], field_name,
1346 maximumNameLength + 1);
1347 }
1348 else {
1349 for (int i = 0; i < component_count; i++) {
1350 auto name = fmt::format("{}:{}", field_name, i + 1);
1351 if (field_name == "skin") {
1352 name = i == 0 ? "skin:parent_element_id" : "skin:parent_element_side_number";
1353 }
1354 else if (field_name == "chain") {
1355 name = i == 0 ? "chain:root_element_id" : "chain:depth_from_root";
1356 }
1357 Ioss::Utils::copy_string(names[field.get_index() + i - 1], name,
1358 maximumNameLength + 1);
1359 }
1360 }
1361 break;
1362 }
1363 }
1364 }
1365 delete_name_array(names, elem_map_cnt);
1366 }
1367
1368 // Write coordinate frame data...
1369 write_coordinate_frames(get_file_pointer(), get_region()->get_coordinate_frames());
1370 }
1371 } // namespace Ioexnl
1372
1373 namespace {
1374 template <typename T>
1375 void write_attribute_names(int, ex_entity_type, const std::vector<T *> &entities)
1376 {
1377 // For the entity, determine the attribute fields and the correct
1378 // order. Write the names of these fields. However, be aware that
1379 // the field "attribute" always exists to contain all attributes
1380 // and its name should not be used even if it is the only
1381 // attribute field.
1382 for (const auto &ge : entities) {
1383 int attribute_count = ge->get_property("attribute_count").get_int();
1384 if (attribute_count > 0) {
1385
1386 check_attribute_index_order(ge);
1387
1388 std::vector<char *> names(attribute_count);
1389 Ioss::NameList names_str(attribute_count);
1390
1391 // Get the attribute fields...
1392 Ioss::NameList results_fields = ge->field_describe(Ioss::Field::ATTRIBUTE);
1393
1394 for (const auto &field_name : results_fields) {
1395 const Ioss::Field &field = ge->get_fieldref(field_name);
1396 assert(field.get_index() != 0);
1397
1398 if (field_name == "attribute") {
1399 field.set_index(1);
1400 continue;
1401 }
1402
1403 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1404 int field_offset = field.get_index();
1405 for (int i = 0; i < comp_count; i++) {
1406 names_str[field_offset - 1 + i] =
1407 ge->get_database()->get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1);
1408 names[field_offset - 1 + i] =
1409 const_cast<char *>(names_str[field_offset - 1 + i].c_str());
1410 }
1411 }
1412 }
1413 }
1414 }
1415
1416 // common
1417 void check_attribute_index_order(Ioss::GroupingEntity *block)
1418 {
1419 int attribute_count = block->get_property("attribute_count").get_int();
1420 if (attribute_count == 0) {
1421 return;
1422 }
1423 int component_sum = 0;
1424
1425 std::vector<int> attributes(attribute_count + 1);
1426
1427 // Get the attribute fields...
1428 Ioss::NameList results_fields = block->field_describe(Ioss::Field::ATTRIBUTE);
1429
1430 bool all_attributes_indexed = true;
1431 bool some_attributes_indexed = false;
1432
1433 for (const auto &field_name : results_fields) {
1434 const Ioss::Field &field = block->get_fieldref(field_name);
1435
1436 if (field_name == "attribute") {
1437 field.set_index(1);
1438 if (results_fields.size() == 1) {
1439 return;
1440 }
1441 continue;
1442 }
1443
1444 int field_offset = field.get_index();
1445 if (field_offset == 0) {
1446 all_attributes_indexed = false;
1447 }
1448 else {
1449 some_attributes_indexed = true;
1450 }
1451
1452 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1453 component_sum += comp_count;
1454
1455 if (field_offset == 0) {
1456 continue;
1457 }
1458
1459 if (field_offset + comp_count - 1 > attribute_count) {
1460 std::ostringstream errmsg;
1461 fmt::print(
1462 errmsg,
1463 "INTERNAL ERROR: For block '{}', attribute '{}', the indexing is incorrect.\n"
1464 "Something is wrong in the Ioexnl::BaseDatabaseIO class, function {}. Please report.\n",
1465 block->name(), field_name, __func__);
1466 IOSS_ERROR(errmsg);
1467 }
1468
1469 for (int i = field_offset; i < field_offset + comp_count; i++) {
1470 if (attributes[i] != 0) {
1471 std::ostringstream errmsg;
1472 fmt::print(
1473 errmsg,
1474 "INTERNAL ERROR: For block '{}', attribute '{}', indexes into the same location as a "
1475 "previous attribute.\n"
1476 "Something is wrong in the Ioexnl::BaseDatabaseIO class, function {}. Please "
1477 "report.\n",
1478 block->name(), field_name, __func__);
1479 IOSS_ERROR(errmsg);
1480 }
1481 attributes[i] = 1;
1482 }
1483 }
1484
1485 if (component_sum > attribute_count) {
1486 std::ostringstream errmsg;
1487 fmt::print(
1488 errmsg,
1489 "INTERNAL ERROR: Block '{}' is supposed to have {} attributes, but {} attributes "
1490 "were counted.\n"
1491 "Something is wrong in the Ioexnl::BaseDatabaseIO class, function {}. Please report.\n",
1492 block->name(), attribute_count, component_sum, __func__);
1493 IOSS_ERROR(errmsg);
1494 }
1495
1496 // Take care of the easy cases first...
1497 if (all_attributes_indexed) {
1498 // Check that all attributes are defined. This should have
1499 // caught above in the duplicate index check.
1500 for (int i = 1; i <= attribute_count; i++) {
1501 if (attributes[i] == 0) {
1502 std::ostringstream errmsg;
1503 fmt::print(errmsg,
1504 "INTERNAL ERROR: Block '{}' has an incomplete set of attributes.\n"
1505 "Something is wrong in the Ioexnl::BaseDatabaseIO class, function {}. Please "
1506 "report.\n",
1507 block->name(), __func__);
1508 IOSS_ERROR(errmsg);
1509 }
1510 }
1511 return;
1512 }
1513
1514 if (!some_attributes_indexed) {
1515 // Index was not set for any of the attributes; set them all...
1516 size_t offset = 1;
1517 for (const auto &field_name : results_fields) {
1518 const Ioss::Field &field = block->get_fieldref(field_name);
1519
1520 if (field_name == "attribute") {
1521 field.set_index(1);
1522 continue;
1523 }
1524
1525 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1526
1527 assert(field.get_index() == 0);
1528 field.set_index(offset);
1529 offset += comp_count;
1530 }
1531 assert((int)offset == attribute_count + 1);
1532 return;
1533 }
1534
1535 // At this point, we have a partially indexed set of attributes. Some have an index and some
1536 // don't
1537 // The easy case is if the missing indices are at the end of the list...
1538 assert(!all_attributes_indexed && some_attributes_indexed);
1539 int last_defined = 0;
1540 for (int i = 1; i < attribute_count + 1; i++) {
1541 if (attributes[i] != 0) {
1542 last_defined = i;
1543 }
1544 }
1545 int first_undefined = attribute_count;
1546 for (int i = attribute_count; i > 0; i--) {
1547 if (attributes[i] == 0) {
1548 first_undefined = i;
1549 }
1550 }
1551 if (last_defined < first_undefined) {
1552 for (const auto &field_name : results_fields) {
1553 const Ioss::Field &field = block->get_fieldref(field_name);
1554
1555 if (field_name == "attribute") {
1556 field.set_index(1);
1557 continue;
1558 }
1559
1560 if (field.get_index() == 0) {
1561 field.set_index(first_undefined);
1562 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1563 first_undefined += comp_count;
1564 }
1565 }
1566 assert(first_undefined == attribute_count + 1);
1567 return;
1568 }
1569
1570 // Take the easy way out... Just reindex all attributes.
1571 size_t offset = 1;
1572 for (const auto &field_name : results_fields) {
1573 const Ioss::Field &field = block->get_fieldref(field_name);
1574
1575 if (field_name == "attribute") {
1576 field.set_index(1);
1577 continue;
1578 }
1579
1580 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
1581
1582 assert(field.get_index() == 0);
1583 field.set_index(offset);
1584 offset += comp_count;
1585 }
1586 assert((int)offset == attribute_count + 1);
1587 }
1588
1589 void check_variable_consistency(IOSS_MAYBE_UNUSED const ex_var_params &exo_params,
1590 IOSS_MAYBE_UNUSED int my_processor,
1591 IOSS_MAYBE_UNUSED const std::string &filename,
1592 IOSS_MAYBE_UNUSED const Ioss::ParallelUtils &util)
1593 {
1594 IOSS_PAR_UNUSED(exo_params);
1595 IOSS_PAR_UNUSED(my_processor);
1596 IOSS_PAR_UNUSED(filename);
1597 IOSS_PAR_UNUSED(util);
1598 #ifdef SEACAS_HAVE_MPI
1599 const int num_types = 10;
1600 std::vector<int> var_counts(num_types);
1601 var_counts[0] = exo_params.num_glob;
1602 var_counts[1] = exo_params.num_node;
1603 var_counts[2] = exo_params.num_edge;
1604 var_counts[3] = exo_params.num_face;
1605 var_counts[4] = exo_params.num_elem;
1606 var_counts[5] = exo_params.num_nset;
1607 var_counts[6] = exo_params.num_eset;
1608 var_counts[7] = exo_params.num_fset;
1609 var_counts[8] = exo_params.num_sset;
1610 var_counts[9] = exo_params.num_elset;
1611
1612 Ioss::IntVector all_counts;
1613 util.gather(var_counts, all_counts);
1614
1615 bool any_diff = false;
1616 std::ostringstream errmsg;
1617 if (my_processor == 0) {
1618 bool diff[num_types];
1619 // See if any differ...
1620 for (int iv = 0; iv < 10; iv++) {
1621 diff[iv] = false;
1622 std::string type;
1623 switch (iv) {
1624 case 0: type = "global"; break;
1625 case 1: type = "nodal"; break;
1626 case 2: type = "edge"; break;
1627 case 3: type = "face"; break;
1628 case 4: type = "element"; break;
1629 case 5: type = "nodeset"; break;
1630 case 6: type = "edgeset"; break;
1631 case 7: type = "faceset"; break;
1632 case 8: type = "sideset"; break;
1633 case 9: type = "elementset"; break;
1634 }
1635
1636 for (int ip = 1; ip < util.parallel_size(); ip++) {
1637 if (var_counts[iv] != all_counts[ip * num_types + iv]) {
1638 any_diff = true;
1639 if (!diff[iv]) {
1640 Ioss::FileInfo db(filename);
1641 diff[iv] = true;
1642 fmt::print(errmsg,
1643 "\nERROR: Number of {} variables is not consistent on all processors.\n"
1644 " Database: '{}'\n"
1645 "\tProcessor 0 count = {}\n",
1646 type, db.tailname(), var_counts[iv]);
1647 }
1648 fmt::print(errmsg, "\tProcessor {} count = {}\n", ip, all_counts[ip * num_types + iv]);
1649 }
1650 }
1651 }
1652 }
1653 else {
1654 // Give the other processors something to say...
1655 fmt::print(errmsg, "ERROR: Variable type counts are inconsistent. See processor 0 output for "
1656 "more details.\n");
1657 }
1658 int idiff = any_diff ? 1 : 0;
1659 util.broadcast(idiff);
1660 any_diff = idiff == 1;
1661
1662 if (any_diff) {
1663 std::runtime_error x(errmsg.str());
1664 throw x;
1665 }
1666 #endif
1667 }
1668 } // namespace