Debian Codegraph @

packages/seacas/libraries/ioss/src/exodus/Ioex_DatabaseIO.C

raw
1 // Copyright(C) 1999-2025 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_CodeTypes.h"
8 #include "Ioss_FileInfo.h"
9 #include "Ioss_ParallelUtils.h"
10 #include "Ioss_SerializeIO.h"
11 #include "Ioss_SmartAssert.h"
12 #include "Ioss_SurfaceSplit.h"
13 #include "Ioss_Utils.h"
14 #include "exodus/Ioex_DatabaseIO.h"
15 #include "exodus/Ioex_Internals.h"
16 #include "exodus/Ioex_Utils.h"
17 #include <array>
18 #include <cassert>
19 #include <cfloat>
20 #include <cstring>
21 #include <ctime>
22 #include <exodusII.h>
23 #include <fmt/format.h>
24 #include <fmt/ostream.h>
25 #include <limits>
26 #include <map>
27 #include <numeric>
28 #include <sstream>
29 #include <stdexcept>
30 #include <string>
31 #include <tokenize.h>
32 #include <vector>
33
34 #include "Ioex_BaseDatabaseIO.h"
35 #include "Ioss_Assembly.h"
36 #include "Ioss_Blob.h"
37 #include "Ioss_CommSet.h"
38 #include "Ioss_DBUsage.h"
39 #include "Ioss_EdgeBlock.h"
40 #include "Ioss_EdgeSet.h"
41 #include "Ioss_ElementBlock.h"
42 #include "Ioss_ElementSet.h"
43 #include "Ioss_ElementTopology.h"
44 #include "Ioss_EntityBlock.h"
45 #include "Ioss_EntitySet.h"
46 #include "Ioss_EntityType.h"
47 #include "Ioss_FaceBlock.h"
48 #include "Ioss_FaceSet.h"
49 #include "Ioss_Field.h"
50 #include "Ioss_GroupingEntity.h"
51 #include "Ioss_Map.h"
52 #include "Ioss_NodeBlock.h"
53 #include "Ioss_NodeSet.h"
54 #include "Ioss_Property.h"
55 #include "Ioss_PropertyManager.h"
56 #include "Ioss_Region.h"
57 #include "Ioss_SideBlock.h"
58 #include "Ioss_SideSet.h"
59 #include "Ioss_State.h"
60 #include "Ioss_VariableType.h"
61 #include "hopscotch_hash.h"
62
63 // ========================================================================
64 // Static internal helper functions
65 // ========================================================================
66 namespace {
67 const size_t max_line_length = MAX_LINE_LENGTH;
68
69 std::string SEP() { return std::string("@"); } // Separator for attribute offset storage
70 std::array<std::string, 2> complex_suffix{".re", ".im"};
71
72 void get_connectivity_data(int exoid, void *data, ex_entity_type type, ex_entity_id id,
73 int position)
74 {
75 int ierr = 0;
76 if ((ex_int64_status(exoid) & EX_BULK_INT64_API) != 0) {
77 std::array<int64_t *, 3> conn{nullptr, nullptr, nullptr};
78 conn[position] = static_cast<int64_t *>(data);
79 ierr = ex_get_conn(exoid, type, id, conn[0], conn[1], conn[2]);
80 }
81 else {
82 std::array<int *, 3> conn{nullptr, nullptr, nullptr};
83 conn[position] = static_cast<int *>(data);
84 ierr = ex_get_conn(exoid, type, id, conn[0], conn[1], conn[2]);
85 }
86 if (ierr < 0) {
87 Ioex::exodus_error(exoid, __LINE__, __func__, __FILE__);
88 }
89 }
90
91 template <typename T>
92 void compute_internal_border_maps(T *entities, T *internal, size_t count, size_t entity_count)
93 {
94 // Construct the node/element map (internal vs. border).
95 // Border nodes/elements are those in the communication map (use entities array)
96 // Internal nodes/elements are the rest. Allocate array to hold all nodes/elements,
97 // initialize all to '1', then zero out the nodes/elements in 'entities'.
98 // Iterate through array again and consolidate all '1's
99 for (size_t ij = 0; ij < count; ij++) {
100 internal[ij] = 1;
101 }
102 for (size_t J = 0; J < entity_count; J++) {
103 internal[entities[J] - 1] = 0;
104 }
105
106 size_t b = 0;
107 for (size_t ij = 0; ij < count; ij++) {
108 if (internal[ij] == 0) {
109 entities[b++] = ij + 1;
110 }
111 }
112
113 size_t k = 0;
114 for (size_t ij = 0; ij < count; ij++) {
115 if (internal[ij] == 1) {
116 internal[k++] = ij + 1;
117 }
118 }
119 }
120
121 template <typename T>
122 void extract_data(std::vector<double> &local_data, T *data, size_t num_entity, size_t comp_count,
123 size_t offset)
124 {
125 local_data.resize(num_entity);
126 if (comp_count == 1 && offset == 0) {
127 for (size_t j = 0; j < num_entity; j++) {
128 local_data[j] = data[j];
129 }
130 }
131 else {
132 for (size_t j = 0; j < num_entity; j++) {
133 local_data[j] = data[offset];
134 offset += comp_count;
135 }
136 }
137 }
138
139 } // namespace
140
141 namespace Ioex {
142 DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename,
143 Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator,
144 const Ioss::PropertyManager &props)
145 : Ioex::BaseDatabaseIO(region, filename, db_usage, communicator, props)
146 {
147 if (!is_input()) {
148 // Check whether appending to or modify existing file...
149 if (open_create_behavior() == Ioss::DB_APPEND ||
150 open_create_behavior() == Ioss::DB_APPEND_GROUP ||
151 open_create_behavior() == Ioss::DB_MODIFY) {
152 // Append to file if it already exists -- See if the file exists.
153 auto file = Ioss::FileInfo(decoded_filename());
154 fileExists = file.exists();
155 }
156 }
157
158 if (properties.exists("processor_count") && properties.exists("my_processor")) {
159 if (!isParallel) {
160 isSerialParallel = true;
161 }
162 else {
163 IOSS_ERROR(
164 "ERROR: Processor id and processor count are specified via the "
165 "'processor_count' and 'processor_id' properties which indicates that this "
166 "database is "
167 "being run in 'serial-parallel' mode, but the database constructor was passed an "
168 "mpi communicator which has more than 1 processor. This is not allowed.\n");
169 }
170 }
171 }
172
173 bool DatabaseIO::check_valid_file_ptr(bool write_message, std::string *error_msg, int *bad_count,
174 bool abort_if_error) const
175 {
176 bool no_collective_calls = Ioss::SerializeIO::isEnabled();
177 if (isParallel && no_collective_calls) {
178 // Can't output a nice error message on processor 0 and throw a consistent error.
179 // Have to just write message on processors that have issue and throw exception.
180 if (m_exodusFilePtr < 0) {
181 std::ostringstream errmsg;
182 std::string open_create = is_input() ? "open input" : "create output";
183 fmt::print(errmsg, "ERROR: Unable to {} exodus decomposed database file '{}'\n",
184 open_create, decoded_filename());
185
186 if (abort_if_error) {
187 IOSS_ERROR(errmsg);
188 }
189 Ioss::WarnOut() << errmsg.str();
190 return false;
191 }
192 return true; // At least on this processor...
193 }
194
195 // Check for valid exodus_file_ptr (valid >= 0; invalid < 0)
196 int global_file_ptr = m_exodusFilePtr;
197 if (isParallel) {
198 global_file_ptr = util().global_minmax(m_exodusFilePtr, Ioss::ParallelUtils::DO_MIN);
199 }
200
201 if (global_file_ptr < 0) {
202 if (write_message || error_msg != nullptr || bad_count != nullptr) {
203 Ioss::IntVector status;
204 if (isParallel) {
205 util().all_gather(m_exodusFilePtr, status);
206 }
207 else {
208 status.push_back(m_exodusFilePtr);
209 }
210
211 std::string open_create = is_input() ? "open input" : "create output";
212 if (write_message || error_msg != nullptr) {
213 // See which processors could not open/create the file...
214 std::ostringstream errmsg;
215 if (isParallel) {
216 fmt::print(errmsg, "ERROR: Unable to {} exodus decomposed database files:\n",
217 open_create);
218 for (int i = 0; i < util().parallel_size(); i++) {
219 if (status[i] < 0) {
220 fmt::print(errmsg, "\t{}\n",
221 Ioss::Utils::decode_filename(get_filename(), i, util().parallel_size()));
222 }
223 }
224 }
225 else {
226 fmt::print(errmsg, "ERROR: Unable to {} database '{}' of type 'exodusII'", open_create,
227 get_filename());
228 }
229 fmt::print(errmsg, "\n");
230 if (error_msg != nullptr) {
231 *error_msg = errmsg.str();
232 }
233 if (write_message && myProcessor == 0) {
234 Ioss::WarnOut() << errmsg.str();
235 }
236 }
237 if (bad_count != nullptr) {
238 *bad_count = std::count_if(status.begin(), status.end(), [](int i) { return i < 0; });
239 }
240 if (abort_if_error) {
241 IOSS_ERROR(fmt::format("ERROR: Cannot {} file '{}'\n", open_create, get_filename()));
242 }
243 }
244 return false;
245 }
246 return true;
247 }
248
249 bool DatabaseIO::open_input_file(bool write_message, std::string *error_msg, int *bad_count,
250 bool abort_if_error) const
251 {
252 int cpu_word_size = sizeof(double);
253 int io_word_size = 0;
254 float version;
255
256 int mode = exodusMode;
257 if (int_byte_size_api() == 8) {
258 mode |= EX_ALL_INT64_API;
259 }
260
261 #if defined EX_DISKLESS
262 // Experimental -- in memory read by netcdf library
263 if (properties.exists("MEMORY_READ")) {
264 mode |= EX_DISKLESS;
265 }
266 #endif
267
268 bool do_timer = false;
269 Ioss::Utils::check_set_bool_property(properties, "IOSS_TIME_FILE_OPEN_CLOSE", do_timer);
270 double t_begin = ((do_timer && isParallel) ? Ioss::Utils::timer() : 0);
271
272 int app_opt_val = ex_opts(EX_VERBOSE);
273 m_exodusFilePtr = ex_open(decoded_filename().c_str(), EX_READ | mode, &cpu_word_size,
274 &io_word_size, &version);
275
276 if (do_timer && isParallel) {
277 double t_end = Ioss::Utils::timer();
278 double duration = util().global_minmax(t_end - t_begin, Ioss::ParallelUtils::DO_MAX);
279 if (myProcessor == 0) {
280 fmt::print(Ioss::DebugOut(), "Input File Open Time = {} ({})\n", duration,
281 decoded_filename());
282 }
283 }
284
285 bool is_ok = check_valid_file_ptr(write_message, error_msg, bad_count, abort_if_error);
286
287 if (is_ok) {
288 finalize_file_open();
289 }
290 ex_opts(app_opt_val); // Reset back to what it was.
291 return is_ok;
292 }
293
294 bool DatabaseIO::handle_output_file(bool write_message, std::string *error_msg, int *bad_count,
295 bool overwrite, bool abort_if_error) const
296 {
297 // If 'overwrite' is false, we do not want to overwrite or clobber
298 // the output file if it already exists since the app might be
299 // reading the restart data from this file and then later
300 // clobbering it and then writing restart data to the same
301 // file. So, for output, we first check whether the file exists
302 // and if it it and is writable, assume that we can later create a
303 // new or append to existing file.
304
305 // if 'overwrite' is true, then clobber/append
306 bool is_ok = false;
307
308 if (!overwrite) {
309 // check if file exists and is writeable. If so, return true.
310 Ioss::FileInfo file(decoded_filename());
311 int int_is_ok = file.exists() && file.is_writable() ? 1 : 0;
312
313 // Check for consistency among all processors.
314 // OK if *all* 0 or *all* 1
315 int sum = util().global_minmax(int_is_ok, Ioss::ParallelUtils::DO_SUM);
316 if (sum == util().parallel_size()) {
317 // Note that at this point, we cannot totally guarantee that
318 // we will be able to create the file when needed, but we have
319 // a pretty good chance. We can't guarantee creation without
320 // creating and the app (or calling function) doesn't want us to overwrite...
321 return true;
322 }
323 // File doesn't exist on any or all processors, so fall through and try to
324 // create file since we won't be overwriting anything...
325 }
326
327 int cpu_word_size = sizeof(double);
328 int io_word_size = 0;
329 float version;
330
331 int mode = exodusMode;
332 if (int_byte_size_api() == 8) {
333 mode |= EX_ALL_INT64_API;
334 }
335
336 #if defined EX_DISKLESS
337 // Experimental -- in memory write by netcdf library
338 if (properties.exists("MEMORY_WRITE")) {
339 mode |= EX_DISKLESS;
340 }
341 #endif
342 int app_opt_val = ex_opts(EX_VERBOSE);
343 if (fileExists) {
344 m_exodusFilePtr = ex_open(decoded_filename().c_str(), EX_WRITE | mode, &cpu_word_size,
345 &io_word_size, &version);
346 }
347 else {
348 // If the first write for this file, create it...
349 if (int_byte_size_api() == 8) {
350 // Check whether client actually wants 4-byte output on db
351 // - If they specified INTEGER_SIZE_DB and the size isn't 8,
352 // then don't change mode and use the default 4-byte output.
353 if (properties.exists("INTEGER_SIZE_DB")) {
354 if (properties.get("INTEGER_SIZE_DB").get_int() == 8) {
355 mode |= EX_ALL_INT64_DB;
356 }
357 }
358 else {
359 mode |= EX_ALL_INT64_DB;
360 }
361 }
362 m_exodusFilePtr =
363 ex_create(decoded_filename().c_str(), mode, &cpu_word_size, &dbRealWordSize);
364 }
365
366 is_ok = check_valid_file_ptr(write_message, error_msg, bad_count, abort_if_error);
367
368 if (is_ok) {
369 ex_set_max_name_length(m_exodusFilePtr, maximumNameLength);
370
371 if (fileExists) {
372 open_root_group_nl();
373 open_child_group_nl(0);
374 }
375
376 // Check properties handled post-create/open...
377 if (properties.exists("COMPRESSION_METHOD")) {
378 auto method = properties.get("COMPRESSION_METHOD").get_string();
379 method = Ioss::Utils::lowercase(method);
380 ex_compression_type exo_method = EX_COMPRESS_ZLIB;
381 if (method == "zlib" || method == "libz" || method == "gzip") {
382 exo_method = EX_COMPRESS_ZLIB;
383 }
384 else if (method == "szip") {
385 #if !defined(NC_HAS_SZIP_WRITE)
386 #define NC_HAS_SZIP_WRITE 0
387 #endif
388 #if NC_HAS_SZIP_WRITE
389 exo_method = EX_COMPRESS_SZIP;
390 #else
391 if (myProcessor == 0) {
392 fmt::print(Ioss::WarnOut(), "The NetCDF library does not have SZip compression enabled."
393 " 'zlib' will be used instead.\n\n");
394 }
395 #endif
396 }
397 else if (method == "zstd") {
398 #if NC_HAS_ZSTD == 1
399 exo_method = EX_COMPRESS_ZSTD;
400 #else
401 if (myProcessor == 0) {
402 fmt::print(Ioss::WarnOut(),
403 "The NetCDF library does not have ZStandard compression enabled."
404 " 'zlib' will be used instead.\n\n");
405 }
406 #endif
407 }
408 else if (method == "bzip2") {
409 #if NC_HAS_BZ2 == 1
410 exo_method = EX_COMPRESS_BZ2;
411 #else
412 if (myProcessor == 0) {
413 fmt::print(Ioss::WarnOut(),
414 "The NetCDF library does not have Bzip2 / BZ2 compression enabled."
415 " 'zlib' will be used instead.\n\n");
416 }
417 #endif
418 }
419 else {
420 if (myProcessor == 0) {
421 fmt::print(Ioss::WarnOut(),
422 "Unrecognized compression method specified: '{}'."
423 " 'zlib' will be used instead.\n\n",
424 method);
425 }
426 }
427 ex_set_option(m_exodusFilePtr, EX_OPT_COMPRESSION_TYPE, exo_method);
428 }
429
430 if (properties.exists("COMPRESSION_QUANTIZE_NSD")) {
431 int quant_level = properties.get("COMPRESSION_QUANTIZE_NSD").get_int();
432 ex_set_option(m_exodusFilePtr, EX_OPT_QUANTIZE_NSD, quant_level);
433 }
434
435 if (properties.exists("COMPRESSION_LEVEL")) {
436 int comp_level = properties.get("COMPRESSION_LEVEL").get_int();
437 ex_set_option(m_exodusFilePtr, EX_OPT_COMPRESSION_LEVEL, comp_level);
438 }
439
440 if (properties.exists("COMPRESSION_SHUFFLE")) {
441 int shuffle = properties.get("COMPRESSION_SHUFFLE").get_int();
442 ex_set_option(m_exodusFilePtr, EX_OPT_COMPRESSION_SHUFFLE, shuffle);
443 }
444 }
445 ex_opts(app_opt_val); // Reset back to what it was.
446 return is_ok;
447 }
448
449 int DatabaseIO::get_file_pointer() const
450 {
451 // Returns the file_pointer used to access the file on disk.
452 // Checks that the file is open and if not, opens it first.
453 if (Ioss::SerializeIO::isEnabled()) {
454 if (!Ioss::SerializeIO::inBarrier()) {
455 IOSS_ERROR(fmt::format("ERROR: Process {} is attempting to do I/O without serialized I/O",
456 Ioss::SerializeIO::getRank()));
457 }
458
459 if (!Ioss::SerializeIO::inMyGroup()) {
460 IOSS_ERROR(fmt::format("ERROR: Process {} is attempting to do I/O while {} owns the token",
461 Ioss::SerializeIO::getRank(), Ioss::SerializeIO::getOwner()));
462 }
463 }
464
465 return Ioex::BaseDatabaseIO::get_file_pointer();
466 }
467
468 void DatabaseIO::read_meta_data_nl()
469 {
470 // If this is a HISTORY file, there isn't really any metadata
471 // Other than a single node and single element. Just hardwire
472 // it here (needed when appending to existing history file)
473 if (dbUsage == Ioss::WRITE_HISTORY) {
474 if (myProcessor == 0) {
475 nodeCount = 1;
476 elementCount = 1;
477 auto *nb = new Ioss::NodeBlock(this, "nodeblock_1", 1, 3);
478 get_region()->add(nb);
479
480 // Element Block
481 auto *eb = new Ioss::ElementBlock(this, "e1", "sphere", 1);
482 eb->property_add(Ioss::Property("id", 1));
483 eb->property_add(Ioss::Property("guid", util().generate_guid(1)));
484 get_region()->add(eb);
485 get_step_times_nl();
486 add_region_fields();
487 }
488 return;
489 }
490
491 // APPENDING:
492 // There is an assumption that the writing process (mesh, vars) is
493 // the same for the original run that created this database and
494 // for this run which is appending to the database so the defining
495 // of the output database should be the same except we don't write
496 // anything since it is already there. We do need the number of
497 // steps though...
498 if (open_create_behavior() == Ioss::DB_APPEND || dbUsage == Ioss::QUERY_TIMESTEPS_ONLY) {
499 get_step_times_nl();
500 return;
501 }
502
503 {
504 Ioss::SerializeIO serializeIO_(this);
505
506 if (isParallel) {
507 Ioex::check_processor_info(decoded_filename(), get_file_pointer(), util().parallel_size(),
508 myProcessor);
509 }
510
511 read_region();
512 read_communication_metadata();
513
514 Ioex::read_exodus_basis(get_file_pointer());
515 Ioex::read_exodus_quadrature(get_file_pointer());
516 }
517
518 get_step_times_nl();
519
520 get_nodeblocks();
521 get_edgeblocks();
522 get_faceblocks();
523 get_elemblocks();
524
525 check_side_topology();
526
527 get_sidesets();
528 get_nodesets();
529 get_edgesets();
530 get_facesets();
531 get_elemsets();
532
533 get_commsets();
534
535 // Add assemblies now that all entities should be defined... consistent across processors
536 // (metadata)
537 get_assemblies();
538 get_blobs();
539
540 handle_groups();
541
542 add_region_fields();
543
544 if (!is_input() && open_create_behavior() == Ioss::DB_APPEND) {
545 (void)get_map(EX_NODE_BLOCK);
546 (void)get_map(EX_EDGE_BLOCK);
547 (void)get_map(EX_FACE_BLOCK);
548 (void)get_map(EX_ELEM_BLOCK);
549 }
550 }
551
552 void DatabaseIO::read_region()
553 {
554 // Add properties and fields to the 'owning' region.
555 // Also defines member variables of this class...
556 ex_init_params info{};
557 int error = ex_get_init_ext(get_file_pointer(), &info);
558 if (error < 0) {
559 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
560 }
561
562 spatialDimension = info.num_dim;
563 nodeCount = info.num_nodes;
564 edgeCount = info.num_edge;
565 faceCount = info.num_face;
566 elementCount = info.num_elem;
567
568 m_groupCount[EX_NODE_BLOCK] = 1;
569 m_groupCount[EX_EDGE_BLOCK] = info.num_edge_blk;
570 m_groupCount[EX_FACE_BLOCK] = info.num_face_blk;
571 m_groupCount[EX_ELEM_BLOCK] = info.num_elem_blk;
572
573 m_groupCount[EX_NODE_SET] = info.num_node_sets;
574 m_groupCount[EX_EDGE_SET] = info.num_edge_sets;
575 m_groupCount[EX_FACE_SET] = info.num_face_sets;
576 m_groupCount[EX_ELEM_SET] = info.num_elem_sets;
577
578 m_groupCount[EX_SIDE_SET] = info.num_side_sets;
579 m_groupCount[EX_ASSEMBLY] = info.num_assembly;
580 m_groupCount[EX_BLOB] = info.num_blob;
581
582 if (nodeCount == 0 && info.num_blob == 0) {
583 fmt::print(Ioss::WarnOut(), "No nodes were found in the model, file '{}'\n",
584 decoded_filename());
585 }
586 else if (nodeCount < 0) {
587 // NOTE: Code will not continue past this call...
588 IOSS_ERROR(fmt::format("ERROR: Negative node count was found in the model\n"
589 " File: '{}'.\n",
590 decoded_filename()));
591 }
592
593 if (elementCount == 0 && info.num_blob == 0) {
594 fmt::print(Ioss::WarnOut(), "No elements were found in the model, file '{}'\n",
595 decoded_filename());
596 }
597
598 if (elementCount < 0) {
599 // NOTE: Code will not continue past this call...
600 IOSS_ERROR(fmt::format("ERROR: Negative element count was found in the model\n"
601 " File: '{}'.\n",
602 decoded_filename()));
603 }
604
605 if (elementCount > 0 && m_groupCount[EX_ELEM_BLOCK] <= 0) {
606 // NOTE: Code will not continue past this call...
607 IOSS_ERROR(fmt::format("ERROR: No element blocks were found in the model\n"
608 " File: '{}'.\n",
609 decoded_filename()));
610 }
611
612 Ioss::Region *this_region = get_region();
613
614 // See if any coordinate frames exist on mesh. If so, define them on region.
615 Ioex::add_coordinate_frames(get_file_pointer(), this_region);
616
617 this_region->property_add(Ioss::Property(std::string("title"), info.title));
618
619 // Get QA records from database and add to qaRecords...
620 int num_qa = ex_inquire_int(get_file_pointer(), EX_INQ_QA);
621 if (num_qa > 0) {
622 struct qa_element
623 {
624 char *qa_record[1][4];
625 };
626
627 std::vector<qa_element> qa(num_qa);
628 for (int i = 0; i < num_qa; i++) {
629 for (int j = 0; j < 4; j++) {
630 qa[i].qa_record[0][j] = new char[MAX_STR_LENGTH + 1];
631 }
632 }
633
634 ex_get_qa(get_file_pointer(), qa[0].qa_record);
635 for (int i = 0; i < num_qa; i++) {
636 add_qa_record(qa[i].qa_record[0][0], qa[i].qa_record[0][1], qa[i].qa_record[0][2],
637 qa[i].qa_record[0][3]);
638 }
639 for (int i = 0; i < num_qa; i++) {
640 for (int j = 0; j < 4; j++) {
641 delete[] qa[i].qa_record[0][j];
642 }
643 }
644 }
645
646 // Get information records from database and add to informationRecords...
647 int num_info = ex_inquire_int(get_file_pointer(), EX_INQ_INFO);
648 if (num_info > 0) {
649 char **info_rec =
650 Ioex::get_name_array(num_info, max_line_length); // 'total_lines' pointers to char buffers
651 ex_get_info(get_file_pointer(), info_rec);
652 for (int i = 0; i < num_info; i++) {
653 add_information_record(info_rec[i]);
654 }
655 Ioex::delete_name_array(info_rec, num_info);
656 }
657 }
658
659 std::vector<double> DatabaseIO::internal_get_step_times_nl(bool setRegionTimeSteps)
660 {
661 bool exists = false;
662 double last_time = DBL_MAX;
663 int tstepCount = 0;
664 std::vector<double> tsteps(0);
665
666 // Use reference to make sure that no Region modifications occur based on the input flag
667 // setRegionTimeSteps flag determines whether we are actually populating the region
668 // timesteps or just querying the timesteps that are on a specific database without
669 // populating the regions timesteps data and setting the number of timesteps on the region
670 int &timestepCount = setRegionTimeSteps ? m_timestepCount : tstepCount;
671
672 if (dbUsage == Ioss::WRITE_HISTORY) {
673 if (myProcessor == 0) {
674 timestepCount = ex_inquire_int(get_file_pointer(), EX_INQ_TIME);
675 if (timestepCount <= 0) {
676 return tsteps;
677 }
678
679 // For an exodus file, timesteps are global and are stored in the region.
680 // A history file only stores that last time / step
681 // Read the timesteps and add them to the region.
682 // Since we can't access the Region's stateCount directly, we just add
683 // all of the steps and assume the Region is dealing with them directly...
684 tsteps.resize(timestepCount);
685
686 int error = ex_get_all_times(get_file_pointer(), Data(tsteps));
687 if (error < 0) {
688 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
689 }
690
691 int max_step = properties.get_optional("APPEND_OUTPUT_AFTER_STEP", timestepCount);
692 max_step = std::min(max_step, timestepCount);
693
694 double max_time =
695 properties.get_optional("APPEND_OUTPUT_AFTER_TIME", std::numeric_limits<double>::max());
696
697 Ioss::Region *this_region = get_region();
698 int numSteps = 0;
699 for (int i = 0; i < max_step; i++) {
700 if (tsteps[i] <= max_time) {
701 if (setRegionTimeSteps) {
702 this_region->add_state_nl(tsteps[i] * timeScaleFactor);
703 }
704
705 tsteps[i] *= timeScaleFactor;
706 numSteps++;
707 }
708 }
709 tsteps.resize(numSteps);
710 }
711 }
712 else {
713 {
714 Ioss::SerializeIO serializeIO_(this);
715 timestepCount = ex_inquire_int(get_file_pointer(), EX_INQ_TIME);
716 }
717 int exTimestepCount = timestepCount;
718 // Need to sync timestep count across ranks if parallel...
719 if (isParallel) {
720 auto min_timestep_count = util().global_minmax(timestepCount, Ioss::ParallelUtils::DO_MIN);
721 if (min_timestep_count == 0) {
722 auto max_timestep_count =
723 util().global_minmax(timestepCount, Ioss::ParallelUtils::DO_MAX);
724 if (max_timestep_count != 0) {
725 if (myProcessor == 0) {
726 // NOTE: Don't want to warn on all processors if the
727 // timestep count is zero on some, but not all ranks.
728 fmt::print(Ioss::WarnOut(),
729 "At least one database has no timesteps. No times will be read on ANY"
730 " database for consistency.\n");
731 }
732 }
733 }
734 timestepCount = min_timestep_count;
735 }
736
737 if (timestepCount <= 0) {
738 return tsteps;
739 }
740
741 // For an exodus file, timesteps are global and are stored in the region.
742 // Read the timesteps and add to the region
743 tsteps.resize(exTimestepCount, -std::numeric_limits<double>::max());
744
745 // The `EXODUS_CALL_GET_ALL_TIMES=NO` is typically only used in
746 // isSerialParallel mode and the client is responsible for
747 // making sure that the step times are handled correctly. All
748 // databases will know about the number of timesteps, but if
749 // this is skipped, then the times will all be zero. Use case
750 // is that in isSerialParallel, each call to
751 // `ex_get_all_times` for all files is performed sequentially,
752 // so if you have hundreds to thousands of files, the time for
753 // the call is additive and since timesteps are record
754 // variables in netCDF, accessing the data for all timesteps
755 // involves lseeks throughout the file.
756 bool call_ex_get_all_times = true;
757 Ioss::Utils::check_set_bool_property(properties, "EXODUS_CALL_GET_ALL_TIMES",
758 call_ex_get_all_times);
759 if (call_ex_get_all_times) {
760 Ioss::SerializeIO serializeIO_(this);
761 int error = ex_get_all_times(get_file_pointer(), Data(tsteps));
762 if (error < 0) {
763 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
764 }
765 }
766
767 // See if the "last_written_time" attribute exists and if it
768 // does, check that it matches the largest time in 'tsteps'.
769 {
770 Ioss::SerializeIO serializeIO_(this);
771 exists = Ioex::read_last_time_attribute(get_file_pointer(), &last_time);
772 }
773
774 if (exists && isParallel) {
775 // Assume that if it exists on 1 processor, it exists on
776 // all... Sync value among processors since could have a
777 // corrupt step on only a single database.
778 last_time = util().global_minmax(last_time, Ioss::ParallelUtils::DO_MIN);
779 }
780
781 // Only add states that are less than or equal to the
782 // 'last_time' value which is either DBL_MAX or the value of
783 // the last time successfully written to the database and
784 // flushed to disk. This is used to avoid corrupt data arising
785 // from a job that crashed during the writing of the last step
786 // on the database. Output a warning message if there is
787 // potentially corrupt data on the database...
788
789 // Check whether user or application wants to limit the times even further...
790 // One use case is that job is restarting at a time prior to what has been
791 // written to the results file, so want to start appending after
792 // restart time instead of at end time on database.
793 int max_step = properties.get_optional("APPEND_OUTPUT_AFTER_STEP", timestepCount);
794 max_step = std::min(max_step, timestepCount);
795
796 double max_time =
797 properties.get_optional("APPEND_OUTPUT_AFTER_TIME", std::numeric_limits<double>::max());
798 last_time = std::min(last_time, max_time);
799
800 Ioss::Region *this_region = get_region();
801 int numSteps = 0;
802 for (int i = 0; i < max_step; i++) {
803 if (tsteps[i] <= last_time) {
804 if (setRegionTimeSteps) {
805 this_region->add_state_nl(tsteps[i] * timeScaleFactor);
806 }
807
808 tsteps[i] *= timeScaleFactor;
809 numSteps++;
810 }
811 else {
812 if (myProcessor == 0 && max_time == std::numeric_limits<double>::max()) {
813 // NOTE: Don't want to warn on all processors if there are
814 // corrupt steps on all databases, but this will only print
815 // a warning if there is a corrupt step on processor
816 // 0... Need better warnings which won't overload in the
817 // worst case...
818 fmt::print(Ioss::WarnOut(),
819 "Skipping step {} at time {} in database file\n\t{}.\n"
820 "\tThe data for that step is possibly corrupt since the last time written "
821 "successfully was {}.\n",
822 fmt::group_digits(i + 1), tsteps[i], get_filename(), last_time);
823 }
824 }
825 }
826
827 tsteps.resize(numSteps);
828 }
829
830 return tsteps;
831 }
832
833 std::vector<double> DatabaseIO::get_db_step_times_nl()
834 {
835 return internal_get_step_times_nl(false);
836 }
837
838 void DatabaseIO::get_step_times_nl() { internal_get_step_times_nl(true); }
839
840 void DatabaseIO::read_communication_metadata()
841 {
842 // Check that file is nemesis.
843 int num_proc; // Number of processors file was decomposed for
844 int num_proc_in_file; // Number of processors this file has info for
845 char file_type[2]; // "s" for scalar, "p" for parallel
846
847 // Get global data (over all processors)
848 int64_t global_nodes = nodeCount;
849 int64_t global_elements = elementCount;
850 int64_t global_eblocks = 0; // unused
851 int64_t global_nsets = 0; // unused
852 int64_t global_ssets = 0; // unused
853
854 int64_t num_external_nodes; // unused
855 int64_t num_elem_cmaps = 0;
856 int64_t num_node_cmaps = 0;
857 int64_t num_internal_nodes = nodeCount;
858 int64_t num_border_nodes = 0;
859 int64_t num_internal_elems = elementCount;
860 int64_t num_border_elems = 0;
861
862 bool nemesis_file = true;
863 int error = ex_get_init_info(get_file_pointer(), &num_proc, &num_proc_in_file, &file_type[0]);
864 if (error < 0) {
865 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
866 }
867
868 if (num_proc == 1 && num_proc_in_file == 1) {
869 // Not a nemesis file
870 nemesis_file = false;
871 if (isParallel && util().parallel_size() > 1) {
872 IOSS_ERROR(fmt::format("ERROR: Exodus file '{}' does not contain nemesis information.\n",
873 get_filename()));
874 }
875 file_type[0] = 'p';
876 }
877 else {
878 if (!isParallel) {
879 // The file contains nemesis parallel information.
880 // Even though we are running in serial, make the information
881 // available to the application.
882 isSerialParallel = true;
883 get_region()->property_add(Ioss::Property("processor_count", num_proc));
884 }
885 }
886
887 if (isParallel && num_proc != util().parallel_size() && util().parallel_size() > 1) {
888 IOSS_ERROR(fmt::format(
889 "ERROR: Exodus file '{}' was decomposed for {} processors; application is currently "
890 "being run on {} processors",
891 get_filename(), num_proc, util().parallel_size()));
892 }
893 if (num_proc_in_file != 1) {
894 IOSS_ERROR(fmt::format(
895 "ERROR: Exodus file '{}' contains data for {} processors; application requires 1 "
896 "processor per file.",
897 get_filename(), num_proc_in_file));
898 }
899 if (file_type[0] != 'p') {
900 IOSS_ERROR(fmt::format(
901 "ERROR: Exodus file '{}' contains scalar nemesis data; application requires parallel "
902 "nemesis data.",
903 get_filename()));
904 }
905
906 bool minimal_nemesis = false;
907 if (nemesis_file) {
908 // A nemesis file typically separates nodes into multiple
909 // communication sets by processor. (each set specifies
910 // nodes/elements that communicate with only a single
911 // processor). For Sierra, we want a single node communication
912 // map and a single element communication map specifying all
913 // communications so we combine all sets into a single set.
914
915 if (int_byte_size_api() == 4) {
916 int gn, ge, geb, gns, gss;
917 error = ex_get_init_global(get_file_pointer(), &gn, &ge, &geb, &gns, &gss);
918 global_nodes = gn;
919 global_elements = ge;
920 global_eblocks = geb;
921 global_nsets = gns;
922 global_ssets = gss;
923 }
924 else {
925 error = ex_get_init_global(get_file_pointer(), &global_nodes, &global_elements,
926 &global_eblocks, &global_nsets, &global_ssets);
927 }
928 if (error < 0) {
929 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
930 }
931
932 // If the file was written with minimal nemesis data, then these will not exist...
933 if (int_byte_size_api() == 4) {
934 int nin, nbn, nen, nie, nbe, nnc, nec;
935 error = ex_get_loadbal_param(get_file_pointer(), &nin, &nbn, &nen, &nie, &nbe, &nnc, &nec,
936 myProcessor);
937 if (error == 0) {
938 num_external_nodes = nen;
939 num_elem_cmaps = nec;
940 num_node_cmaps = nnc;
941 num_internal_nodes = nin;
942 num_border_nodes = nbn;
943 num_internal_elems = nie;
944 num_border_elems = nbe;
945 }
946 else {
947 minimal_nemesis = true;
948 }
949 }
950 else {
951 error = ex_get_loadbal_param(get_file_pointer(), &num_internal_nodes, &num_border_nodes,
952 &num_external_nodes, &num_internal_elems, &num_border_elems,
953 &num_node_cmaps, &num_elem_cmaps, myProcessor);
954 if (error < 0) {
955 minimal_nemesis = true;
956 num_external_nodes = 0;
957 num_elem_cmaps = 0;
958 num_node_cmaps = 0;
959 num_internal_nodes = 0;
960 num_border_nodes = 0;
961 num_internal_elems = 0;
962 num_border_elems = 0;
963 }
964 }
965 }
966
967 commsetNodeCount = num_node_cmaps;
968 commsetElemCount = num_elem_cmaps;
969
970 Ioss::Region *region = get_region();
971 region->property_add(Ioss::Property("global_node_count", global_nodes));
972 region->property_add(Ioss::Property("global_element_count", global_elements));
973 region->property_add(Ioss::Property("global_element_block_count", global_eblocks));
974 region->property_add(Ioss::Property("global_node_set_count", global_nsets));
975 region->property_add(Ioss::Property("global_side_set_count", global_ssets));
976
977 if (!minimal_nemesis) {
978 region->property_add(Ioss::Property("internal_node_count", num_internal_nodes));
979 region->property_add(Ioss::Property("border_node_count", num_border_nodes));
980 region->property_add(Ioss::Property("internal_element_count", num_internal_elems));
981 region->property_add(Ioss::Property("border_element_count", num_border_elems));
982
983 // Possibly, the following 4 fields should be nodesets and element
984 // sets instead of fields on the region...
985 region->field_add(Ioss::Field("internal_nodes", region->field_int_type(), IOSS_SCALAR(),
986 Ioss::Field::COMMUNICATION, num_internal_nodes));
987 region->field_add(Ioss::Field("border_nodes", region->field_int_type(), IOSS_SCALAR(),
988 Ioss::Field::COMMUNICATION, num_border_nodes));
989 region->field_add(Ioss::Field("internal_elements", region->field_int_type(), IOSS_SCALAR(),
990 Ioss::Field::COMMUNICATION, num_internal_elems));
991 region->field_add(Ioss::Field("border_elements", region->field_int_type(), IOSS_SCALAR(),
992 Ioss::Field::COMMUNICATION, num_border_elems));
993
994 SMART_ASSERT(nodeCount == num_internal_nodes + num_border_nodes)
995 (nodeCount)(num_internal_nodes)(num_border_nodes);
996 SMART_ASSERT(elementCount == num_internal_elems + num_border_elems)
997 (elementCount)(num_internal_elems)(num_border_elems);
998 }
999 }
1000
1001 const Ioss::Map &DatabaseIO::get_map(ex_entity_type type) const
1002 {
1003 switch (type) {
1004 case EX_NODE_BLOCK:
1005 case EX_NODE_SET: return get_map(nodeMap, nodeCount, EX_NODE_MAP, EX_INQ_NODE_MAP);
1006
1007 case EX_ELEM_BLOCK:
1008 case EX_ELEM_SET: return get_map(elemMap, elementCount, EX_ELEM_MAP, EX_INQ_ELEM_MAP);
1009
1010 case EX_FACE_BLOCK:
1011 case EX_FACE_SET: return get_map(faceMap, faceCount, EX_FACE_MAP, EX_INQ_FACE_MAP);
1012
1013 case EX_EDGE_BLOCK:
1014 case EX_EDGE_SET: return get_map(edgeMap, edgeCount, EX_EDGE_MAP, EX_INQ_EDGE_MAP);
1015
1016 default:
1017 IOSS_ERROR("INTERNAL ERROR: Invalid map type. "
1018 "Something is wrong in the Ioex::DatabaseIO::get_map() function. "
1019 "Please report.\n");
1020 }
1021 }
1022
1023 const Ioss::Map &DatabaseIO::get_map(Ioss::Map &entity_map, int64_t entity_count,
1024 ex_entity_type entity_type, ex_inquiry inquiry_type) const
1025 {
1026 // Allocate space for node number map and read it in...
1027 // Can be called multiple times, allocate 1 time only
1028 bool read_exodus_map = true;
1029 if (entity_map.map().empty()) {
1030 switch (entity_type) {
1031 case EX_NODE_MAP: read_exodus_map = !properties.exists("IGNORE_NODE_MAP"); break;
1032 case EX_ELEM_MAP: read_exodus_map = !properties.exists("IGNORE_ELEMENT_MAP"); break;
1033 case EX_FACE_MAP: read_exodus_map = !properties.exists("IGNORE_FACE_MAP"); break;
1034 case EX_EDGE_MAP: read_exodus_map = !properties.exists("IGNORE_EDGE_MAP"); break;
1035 default:
1036 IOSS_ERROR("INTERNAL ERROR: Invalid map type. "
1037 "Something is wrong in the Ioex::DatabaseIO::get_map() function. "
1038 "Please report.\n");
1039 }
1040
1041 if (is_input() || open_create_behavior() == Ioss::DB_APPEND) {
1042 entity_map.set_size(entity_count);
1043
1044 Ioss::SerializeIO serializeIO_(this);
1045 // Check whether there is a "original_global_id_map" map on
1046 // the database. If so, use it instead of the "node_num_map".
1047 bool map_read = false;
1048 int map_count = ex_inquire_int(get_file_pointer(), inquiry_type);
1049 if (read_exodus_map && map_count > 0) {
1050 char **names = Ioex::get_name_array(map_count, maximumNameLength);
1051 int ierr = ex_get_names(get_file_pointer(), entity_type, names);
1052 if (ierr < 0) {
1053 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1054 }
1055
1056 for (int i = 0; i < map_count; i++) {
1057 if (Ioss::Utils::str_equal(names[i], "original_global_id_map")) {
1058 int error = 0;
1059 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
1060 Ioss::Int64Vector tmp_map(entity_map.size());
1061 error = ex_get_num_map(get_file_pointer(), entity_type, i + 1, Data(tmp_map));
1062 if (error >= 0) {
1063 entity_map.set_map(Data(tmp_map), tmp_map.size(), 0, true);
1064 map_read = true;
1065 break;
1066 }
1067 }
1068 else {
1069 // Ioss stores as 64-bit, read as 32-bit and copy over...
1070 Ioss::IntVector tmp_map(entity_map.size());
1071 error = ex_get_num_map(get_file_pointer(), entity_type, i + 1, Data(tmp_map));
1072 if (error >= 0) {
1073 entity_map.set_map(Data(tmp_map), tmp_map.size(), 0, true);
1074 map_read = true;
1075 break;
1076 }
1077 }
1078 }
1079 }
1080 Ioex::delete_name_array(names, map_count);
1081 }
1082
1083 if (!map_read) {
1084 if (isParallel || read_exodus_map) {
1085 int error = 0;
1086 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
1087 Ioss::Int64Vector tmp_map(entity_map.size());
1088 error = ex_get_id_map(get_file_pointer(), entity_type, Data(tmp_map));
1089 if (error >= 0) {
1090 entity_map.set_map(Data(tmp_map), tmp_map.size(), 0, true);
1091 }
1092 }
1093 else {
1094 // Ioss stores as 64-bit, read as 32-bit and copy over...
1095 Ioss::IntVector tmp_map(entity_map.size());
1096 error = ex_get_id_map(get_file_pointer(), entity_type, Data(tmp_map));
1097 if (error >= 0) {
1098 entity_map.set_map(Data(tmp_map), tmp_map.size(), 0, true);
1099 }
1100 }
1101 if (error < 0) {
1102 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1103 }
1104 }
1105 else {
1106 // Ignoring exodus database map (if any)
1107 entity_map.set_default(entity_map.size());
1108 }
1109 }
1110 }
1111 else {
1112 // Output database; entity_map.map not set yet... Build a default map.
1113 entity_map.set_default(entity_count);
1114 }
1115 }
1116 return entity_map;
1117 }
1118
1119 void DatabaseIO::get_elemblocks() { get_blocks(EX_ELEM_BLOCK, 0, "block"); }
1120
1121 void DatabaseIO::get_faceblocks() { get_blocks(EX_FACE_BLOCK, 1, "faceblock"); }
1122
1123 void DatabaseIO::get_edgeblocks() { get_blocks(EX_EDGE_BLOCK, 2, "edgeblock"); }
1124
1125 void DatabaseIO::get_blocks(ex_entity_type entity_type, int rank_offset,
1126 const std::string &basename)
1127 {
1128 // Attributes of an X block are: (X = element, face, or edge)
1129 // -- id
1130 // -- name
1131 // -- X type
1132 // -- number of Xs
1133 // -- number of attributes per X
1134 // -- number of nodes per X (derivable from type)
1135 // -- number of faces per X (derivable from type)
1136 // -- number of edges per X (derivable from type)
1137
1138 // In a parallel execution, it is possible that an X block will have
1139 // no Xs on a particular processor...
1140
1141 // NOTE: This routine may be called multiple times on a single database.
1142 // make sure it is not dependent on being called one time only...
1143
1144 // Get exodus X block metadata
1145 if (m_groupCount[entity_type] == 0) {
1146 return;
1147 }
1148
1149 // If the model contains assemblies, we want to retain the empty blocks since the blocks
1150 // might be in an assembly. This is typically the case when an application is running
1151 // in parallel, but is telling IOSS that it is "serial" (MPI_COMM_SELF) and taking care
1152 // of synchronization at the app level instead of down here...
1153 bool retain_empty_blocks = m_groupCount[EX_ASSEMBLY] > 0;
1154
1155 // The application can override this setting using the `RETAIN_EMPTY_BLOCKS` property.
1156 // This can either set to TRUE or FALSE... Note that `retain_empty_blocks` will not be
1157 // changed unless the property exists.
1158 Ioss::Utils::check_set_bool_property(properties, "RETAIN_EMPTY_BLOCKS", retain_empty_blocks);
1159
1160 Ioss::Int64Vector X_block_ids(m_groupCount[entity_type]);
1161
1162 int error;
1163 {
1164 Ioss::SerializeIO serializeIO_(this);
1165
1166 if ((ex_int64_status(get_file_pointer()) & EX_IDS_INT64_API) != 0) {
1167 error = ex_get_ids(get_file_pointer(), entity_type, Data(X_block_ids));
1168 }
1169 else {
1170 Ioss::IntVector tmp_set_ids(X_block_ids.size());
1171 error = ex_get_ids(get_file_pointer(), entity_type, Data(tmp_set_ids));
1172 if (error >= 0) {
1173 std::copy(tmp_set_ids.begin(), tmp_set_ids.end(), X_block_ids.begin());
1174 }
1175 }
1176 if (error < 0) {
1177 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1178 }
1179 }
1180
1181 size_t all_X_type_length = m_groupCount[entity_type] * (MAX_STR_LENGTH + 1);
1182 std::vector<char> all_X_type(all_X_type_length);
1183
1184 Ioss::Int64Vector counts(m_groupCount[entity_type] * 4);
1185 Ioss::Int64Vector local_X_count(m_groupCount[entity_type]);
1186 Ioss::Int64Vector global_X_count(m_groupCount[entity_type]);
1187
1188 {
1189 Ioss::SerializeIO serializeIO_(this);
1190
1191 for (int iblk = 0; iblk < m_groupCount[entity_type]; iblk++) {
1192 int index = 4 * iblk;
1193 int64_t id = X_block_ids[iblk];
1194
1195 char *const X_type = Data(all_X_type) + iblk * (MAX_STR_LENGTH + 1);
1196
1197 ex_block block{};
1198 block.id = id;
1199 block.type = entity_type;
1200 error = ex_get_block_param(get_file_pointer(), &block);
1201 if (error < 0) {
1202 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1203 }
1204
1205 local_X_count[iblk] = block.num_entry;
1206
1207 counts[index + 0] = block.num_nodes_per_entry;
1208 counts[index + 1] = block.num_edges_per_entry;
1209 counts[index + 2] = block.num_faces_per_entry;
1210 counts[index + 3] = block.num_attribute;
1211
1212 if (block.num_entry == 0) {
1213 std::memset(X_type, 0, MAX_STR_LENGTH + 1);
1214 }
1215 else {
1216 Ioss::Utils::copy_string(X_type, block.topology, MAX_STR_LENGTH + 1);
1217 }
1218 }
1219 }
1220
1221 // This is a collective call...
1222 util().attribute_reduction(all_X_type_length, Data(all_X_type));
1223
1224 // This is a collective call...
1225 util().global_array_minmax(counts, Ioss::ParallelUtils::DO_MAX);
1226
1227 // Determine global X count for each X block....
1228 // Can also get this from an nemesis call, but the data may not always be there in all cases.
1229 util().global_count(local_X_count, global_X_count);
1230
1231 // The 'offset' is used to map an X location within an X
1232 // block to the X 'file descriptor'. For example, the file
1233 // descriptor of the 37th X in the 4th block is calculated by:
1234 // file_descriptor = offset of block 4 + 37 This can also be used to
1235 // determine which X block an X with a file_descriptor
1236 // maps into. An particular X block contains all Xs in
1237 // the range:
1238 // offset < file_descriptor <= offset+number_Xs_per_block
1239 int used_blocks = 0;
1240
1241 int nvar = std::numeric_limits<int>::max(); // Number of 'block' vars on database. Used to skip
1242 // querying if none.
1243 int nmap = std::numeric_limits<int>::max(); // Number of 'block' vars on database. Used to skip
1244 // querying if none.
1245 for (int iblk = 0; iblk < m_groupCount[entity_type]; iblk++) {
1246 int index = 4 * iblk;
1247 int64_t nodes_per_X = counts[index + 0];
1248 int64_t edges_per_X = counts[index + 1];
1249 int64_t faces_per_X = counts[index + 2];
1250 int64_t attributes = counts[index + 3];
1251
1252 int64_t id = X_block_ids[iblk];
1253 std::string alias = Ioss::Utils::encode_entity_name(basename, id);
1254 char *const X_type = Data(all_X_type) + iblk * (MAX_STR_LENGTH + 1);
1255
1256 bool db_has_name = false;
1257 std::string block_name;
1258 if (ignore_database_names()) {
1259 block_name = alias;
1260 }
1261 else {
1262 Ioss::SerializeIO serializeio_(this);
1263 block_name = Ioex::get_entity_name(get_file_pointer(), entity_type, id, basename,
1264 maximumNameLength, db_has_name);
1265 }
1266 if (get_use_generic_canonical_name()) {
1267 std::swap(block_name, alias);
1268 }
1269
1270 std::string save_type = X_type;
1271 std::string type =
1272 Ioss::Utils::fixup_type(X_type, nodes_per_X, spatialDimension - rank_offset);
1273 if (local_X_count[iblk] == 0 && type.empty()) {
1274 // For an empty block, exodus does not store the X
1275 // type information and returns "nullptr" If there are no
1276 // Xs on any processors for this block, it will have
1277 // an empty type which is invalid and will throw an
1278 // exception in the XBlock constructor. Try to discern
1279 // the correct X type based on the block_name.
1280 auto tokens = Ioss::tokenize(block_name, "_");
1281 if (tokens.size() >= 2) {
1282 // Check whether last token names an X topology type...
1283 const Ioss::ElementTopology *topology =
1284 Ioss::ElementTopology::factory(tokens.back(), true);
1285 if (topology != nullptr) {
1286 type = topology->name();
1287 }
1288 }
1289 }
1290
1291 if (type == "null" || type.empty()) {
1292 // If we have no idea what the topology type for an empty
1293 // X block is, call it "unknown"
1294 type = "unknown";
1295
1296 // If there are no Xs on any processor for this block and
1297 // we have no idea what the topology type is, skip it...
1298 if (!retain_empty_blocks && global_X_count[iblk] == 0) {
1299 continue;
1300 }
1301 }
1302
1303 Ioss::EntityBlock *block = nullptr;
1304 if (entity_type == EX_ELEM_BLOCK) {
1305 auto eblock = new Ioss::ElementBlock(this, block_name, type, local_X_count[iblk]);
1306 block = eblock;
1307 block->property_add(
1308 Ioss::Property("id", id)); // Do before adding for better error messages.
1309 get_region()->add(eblock);
1310 }
1311 else if (entity_type == EX_FACE_BLOCK) {
1312 auto fblock = new Ioss::FaceBlock(this, block_name, type, local_X_count[iblk]);
1313 block = fblock;
1314 block->property_add(
1315 Ioss::Property("id", id)); // Do before adding for better error messages.
1316 get_region()->add(fblock);
1317 }
1318 else if (entity_type == EX_EDGE_BLOCK) {
1319 auto eblock = new Ioss::EdgeBlock(this, block_name, type, local_X_count[iblk]);
1320 block = eblock;
1321 block->property_add(
1322 Ioss::Property("id", id)); // Do before adding for better error messages.
1323 get_region()->add(eblock);
1324 }
1325 else {
1326 IOSS_ERROR("ERROR: Invalid type in get_blocks()");
1327 }
1328
1329 // See which connectivity options were defined for this block.
1330 // X -> Node is always defined.
1331 // X -> Face?
1332 if (faces_per_X > 0 && rank_offset < 1) {
1333 std::string storage = "Real[" + std::to_string(faces_per_X) + "]";
1334 block->field_add(
1335 Ioss::Field("connectivity_face", block->field_int_type(), storage, Ioss::Field::MESH));
1336 }
1337 // X -> Edge?
1338 if (edges_per_X > 0 && rank_offset < 2) {
1339 std::string storage = "Real[" + std::to_string(edges_per_X) + "]";
1340 block->field_add(
1341 Ioss::Field("connectivity_edge", block->field_int_type(), storage, Ioss::Field::MESH));
1342 }
1343
1344 block->property_add(Ioss::Property("guid", util().generate_guid(id)));
1345 if (db_has_name) {
1346 std::string *db_name = &block_name;
1347 if (get_use_generic_canonical_name()) {
1348 db_name = &alias;
1349 }
1350 if (alias != block_name) {
1351 block->property_add(Ioss::Property("db_name", *db_name));
1352 }
1353 }
1354
1355 // Maintain block order on output database...
1356 block->property_add(Ioss::Property("original_block_order", used_blocks++));
1357
1358 if (save_type != "null" && !save_type.empty()) {
1359 block->property_update("original_topology_type", save_type);
1360 }
1361
1362 block->property_add(Ioss::Property("global_entity_count", global_X_count[iblk]));
1363
1364 get_region()->add_alias(block_name, alias, block->type());
1365
1366 // Check for additional variables.
1367 add_attribute_fields(block, attributes, type);
1368 if (nvar > 0) {
1369 nvar = add_results_fields(block, iblk);
1370 }
1371 add_reduction_results_fields(block);
1372 add_mesh_reduction_fields(id, block);
1373
1374 if (entity_type == EX_ELEM_BLOCK) {
1375 Ioss::SerializeIO serializeio_(this);
1376 if (nmap > 0) {
1377 auto *elb = dynamic_cast<Ioss::ElementBlock *>(block);
1378 Ioss::Utils::check_dynamic_cast(elb);
1379 nmap =
1380 Ioex::add_map_fields(get_file_pointer(), elb, local_X_count[iblk], maximumNameLength);
1381 }
1382 }
1383 }
1384 m_groupCount[entity_type] = used_blocks;
1385
1386 if (entity_type == EX_ELEM_BLOCK) {
1387 if (!assemblyOmissions.empty() || !assemblyInclusions.empty()) {
1388 update_block_omissions_from_assemblies();
1389 }
1390
1391 assert(blockOmissions.empty() || blockInclusions.empty()); // Only one can be non-empty
1392
1393 // Handle all block omissions or inclusions...
1394 // This only affects the generation of surfaces...
1395 if (!blockOmissions.empty()) {
1396 for (const auto &name : blockOmissions) {
1397 auto block = get_region()->get_element_block(name);
1398 if (block != nullptr) {
1399 block->property_add(Ioss::Property(std::string("omitted"), 1));
1400 }
1401 }
1402 }
1403
1404 if (!blockInclusions.empty()) {
1405 const auto &blocks = get_region()->get_element_blocks();
1406 for (auto &block : blocks) {
1407 block->property_add(Ioss::Property(std::string("omitted"), 1));
1408 }
1409
1410 // Now, erase the property on any blocks in the inclusion list...
1411 for (const auto &name : blockInclusions) {
1412 auto block = get_region()->get_element_block(name);
1413 if (block != nullptr) {
1414 block->property_erase("omitted");
1415 }
1416 }
1417 }
1418 }
1419 }
1420
1421 void DatabaseIO::compute_node_status() const
1422 {
1423 // Create a field for all nodes in the model indicating
1424 // the connectivity 'status' of the node. The status values are:
1425 // 0 -- node not connected to any elements
1426 // 1 -- node only connected to omitted elements
1427 // 2 -- node only connected to active elements
1428 // 3 -- node at border of active and omitted elements.
1429
1430 /// \todo Get working for parallel...
1431
1432 if (nodeConnectivityStatusCalculated) {
1433 return;
1434 }
1435
1436 nodeConnectivityStatus.resize(nodeCount);
1437
1438 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
1439 assert(Ioss::Utils::check_block_order(element_blocks));
1440
1441 for (Ioss::ElementBlock *block : element_blocks) {
1442 unsigned char status = 2;
1443 if (Ioss::Utils::block_is_omitted(block)) {
1444 status = 1;
1445 }
1446
1447 int64_t id = block->get_property("id").get_int();
1448 int element_nodes = block->topology()->number_nodes();
1449 int64_t my_element_count = block->entity_count();
1450 if (my_element_count > 0) {
1451 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
1452 std::vector<int64_t> conn(my_element_count * element_nodes);
1453 ex_get_conn(get_file_pointer(), EX_ELEM_BLOCK, id, Data(conn), nullptr, nullptr);
1454 for (int64_t j = 0; j < my_element_count * element_nodes; j++) {
1455 nodeConnectivityStatus[conn[j] - 1] |= status;
1456 }
1457 }
1458 else {
1459 std::vector<int> conn(my_element_count * element_nodes);
1460 ex_get_conn(get_file_pointer(), EX_ELEM_BLOCK, id, Data(conn), nullptr, nullptr);
1461 for (int64_t j = 0; j < my_element_count * element_nodes; j++) {
1462 nodeConnectivityStatus[conn[j] - 1] |= status;
1463 }
1464 }
1465 }
1466 }
1467 nodeConnectivityStatusCalculated = true;
1468 }
1469
1470 namespace {
1471 void get_element_sides_lists(int exoid, int64_t id, int int_byte_size, int64_t number_sides,
1472 Ioss::Int64Vector &element, Ioss::Int64Vector &sides)
1473 {
1474 // Check whether we already populated the element/sides vectors.
1475 if (element.empty() && sides.empty() && number_sides > 0) {
1476 element.resize(number_sides);
1477 sides.resize(number_sides);
1478 // Easier below here if the element and sides are a known 64-bit size...
1479 // Kluge here to do that...
1480 if (int_byte_size == 4) {
1481 Ioss::IntVector e32(number_sides);
1482 Ioss::IntVector s32(number_sides);
1483 int ierr = ex_get_set(exoid, EX_SIDE_SET, id, Data(e32), Data(s32));
1484 if (ierr < 0) {
1485 Ioex::exodus_error(exoid, __LINE__, __func__, __FILE__);
1486 }
1487 std::copy(e32.begin(), e32.end(), element.begin());
1488 std::copy(s32.begin(), s32.end(), sides.begin());
1489 }
1490 else {
1491 int ierr = ex_get_set(exoid, EX_SIDE_SET, id, Data(element), Data(sides));
1492 if (ierr < 0) {
1493 Ioex::exodus_error(exoid, __LINE__, __func__, __FILE__);
1494 }
1495 }
1496 }
1497 }
1498 } // namespace
1499
1500 void DatabaseIO::get_sidesets()
1501 {
1502 // This function creates all sidesets (surfaces) for a
1503 // model. Note that a sideset contains 1 or more sideblocks
1504 // which are homogeneous (same topology). In serial execution,
1505 // this is fairly straightforward since there are no null sets and
1506 // we have all the information we need. (...except see below for
1507 // surface evolution).
1508 //
1509 // However, in a parallel execution, we have the possibility that a
1510 // side set will have no sides or distribution factors on
1511 // a particular processor. We then don't know the block topology of
1512 // the block(s) contained in this set. We could do some
1513 // communication and get a good idea of the topologies that are in
1514 // the set.
1515
1516 if (m_groupCount[EX_SIDE_SET] > 0) {
1517 check_side_topology();
1518
1519 // Get exodus sideset metadata
1520
1521 // Get the names (may not exist) of all sidesets and see if they are actually
1522 // side "blocks" (perhaps written by IO system for a restart). In that case,
1523 // they were split by a previous run and we need to reconstruct the side "set"
1524 // that may contain one or more of them.
1525 Ioex::SideSetMap fs_map;
1526 Ioex::SideSetSet fs_set;
1527
1528 Ioss::Int64Vector side_set_ids(m_groupCount[EX_SIDE_SET]);
1529 {
1530 Ioss::SerializeIO serializeio_(this);
1531 int error;
1532 if ((ex_int64_status(get_file_pointer()) & EX_IDS_INT64_API) != 0) {
1533 error = ex_get_ids(get_file_pointer(), EX_SIDE_SET, Data(side_set_ids));
1534 }
1535 else {
1536 Ioss::IntVector tmp_set_ids(side_set_ids.size());
1537 error = ex_get_ids(get_file_pointer(), EX_SIDE_SET, Data(tmp_set_ids));
1538 if (error >= 0) {
1539 std::copy(tmp_set_ids.begin(), tmp_set_ids.end(), side_set_ids.begin());
1540 }
1541 }
1542 if (error < 0) {
1543 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1544 }
1545
1546 for (const auto &id : side_set_ids) {
1547 std::vector<char> ss_name(maximumNameLength + 1);
1548 error = ex_get_name(get_file_pointer(), EX_SIDE_SET, id, Data(ss_name));
1549 if (error < 0) {
1550 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1551 }
1552 if (ss_name[0] != '\0') {
1553 Ioss::Utils::fixup_name(Data(ss_name));
1554 Ioex::decode_surface_name(fs_map, fs_set, Data(ss_name));
1555 }
1556 }
1557 }
1558
1559 // Create sidesets for each entry in the fs_set... These are the
1560 // sidesets which were probably written by a previous run of the
1561 // IO system and are already split into homogeneous pieces...
1562 {
1563 for (const auto &fs_name : fs_set) {
1564 auto side_set = new Ioss::SideSet(this, fs_name);
1565 get_region()->add(side_set);
1566 int64_t id = Ioex::extract_id(fs_name);
1567 if (id > 0) {
1568 side_set->property_add(Ioss::Property("id", id));
1569 side_set->property_add(Ioss::Property("guid", util().generate_guid(id)));
1570 }
1571 }
1572 }
1573
1574 for (int iss = 0; iss < m_groupCount[EX_SIDE_SET]; iss++) {
1575 int64_t id = side_set_ids[iss];
1576 std::string sid;
1577 Ioex::TopologyMap topo_map;
1578 Ioex::TopologyMap side_map; // Used to determine side consistency
1579
1580 Ioss::SurfaceSplitType split_type = splitType;
1581 std::string side_set_name;
1582 Ioss::SideSet *side_set = nullptr;
1583
1584 bool db_has_name = false;
1585 {
1586 Ioss::SerializeIO serializeio_(this);
1587
1588 std::string alias = Ioss::Utils::encode_entity_name("surface", id);
1589 if (ignore_database_names()) {
1590 side_set_name = alias;
1591 }
1592 else {
1593 side_set_name = Ioex::get_entity_name(get_file_pointer(), EX_SIDE_SET, id, "surface",
1594 maximumNameLength, db_has_name);
1595 }
1596
1597 if (side_set_name == "universal_sideset") {
1598 split_type = Ioss::SPLIT_BY_DONT_SPLIT;
1599 }
1600
1601 bool in_fs_map = false;
1602 auto FSM = fs_map.find(side_set_name);
1603 if (FSM != fs_map.end()) {
1604 in_fs_map = true;
1605 std::string efs_name = (*FSM).second;
1606 side_set = get_region()->get_sideset(efs_name);
1607 Ioss::Utils::check_non_null(side_set, "sideset", efs_name, __func__);
1608 }
1609 else {
1610 if (get_use_generic_canonical_name()) {
1611 std::swap(side_set_name, alias);
1612 }
1613 side_set = new Ioss::SideSet(this, side_set_name);
1614 side_set->property_add(Ioss::Property("id", id));
1615 side_set->property_add(Ioss::Property("guid", util().generate_guid(id)));
1616 if (db_has_name) {
1617 std::string *db_name = &side_set_name;
1618 if (get_use_generic_canonical_name()) {
1619 db_name = &alias;
1620 }
1621 if (alias != side_set_name) {
1622 side_set->property_add(Ioss::Property("db_name", *db_name));
1623 }
1624 }
1625
1626 get_region()->add(side_set);
1627
1628 get_region()->add_alias(side_set_name, alias, Ioss::SIDESET);
1629 get_region()->add_alias(side_set_name, Ioss::Utils::encode_entity_name("sideset", id),
1630 Ioss::SIDESET);
1631 }
1632
1633 // split_type = SPLIT_BY_ELEMENT_BLOCK;
1634 // split_type = SPLIT_BY_TOPOLOGIES;
1635 // split_type = SPLIT_BY_DONT_SPLIT;
1636
1637 // Determine how many side blocks compose this side set.
1638 ex_set set_param[1];
1639 set_param[0].id = id;
1640 set_param[0].type = EX_SIDE_SET;
1641 set_param[0].entry_list = nullptr;
1642 set_param[0].extra_list = nullptr;
1643 set_param[0].distribution_factor_list = nullptr;
1644
1645 int error = ex_get_sets(get_file_pointer(), 1, set_param);
1646 if (error < 0) {
1647 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1648 }
1649
1650 int64_t number_sides = set_param[0].num_entry;
1651
1652 Ioss::Int64Vector element;
1653 Ioss::Int64Vector sides;
1654
1655 if (!blockOmissions.empty() || !blockInclusions.empty()) {
1656 get_element_sides_lists(get_file_pointer(), id, int_byte_size_api(), number_sides,
1657 element, sides);
1658 Ioex::filter_element_list(get_region(), element, sides, true);
1659 number_sides = element.size();
1660 SMART_ASSERT(element.size() == sides.size())(element.size())(sides.size());
1661 }
1662
1663 if (split_type == Ioss::SPLIT_BY_TOPOLOGIES && sideTopology.size() == 1) {
1664 // There is only one side type for all elements in the model
1665 topo_map[std::make_pair(sideTopology[0].first->name(), sideTopology[0].second)] =
1666 number_sides;
1667 }
1668 else if (split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
1669 const Ioss::ElementTopology *mixed_topo = Ioss::ElementTopology::factory("unknown");
1670 topo_map[std::make_pair(std::string("unknown"), mixed_topo)] = number_sides;
1671 }
1672 else if (in_fs_map) {
1673 auto tokens = Ioss::tokenize(side_set_name, "_");
1674 SMART_ASSERT(tokens.size() >= 4)(tokens.size());
1675 // The sideset should have only a single topology which is
1676 // given by the sideset name...
1677 const Ioss::ElementTopology *side_topo =
1678 Ioss::ElementTopology::factory(tokens[tokens.size() - 2]);
1679 assert(side_topo != nullptr);
1680 const Ioss::ElementTopology *element_topo =
1681 Ioss::ElementTopology::factory(tokens[tokens.size() - 3], true);
1682 std::string name;
1683 if (element_topo != nullptr) {
1684 name = element_topo->name();
1685 }
1686 else {
1687 // -4 -3 -2 -1
1688 // Name is of the form name_block_id_sidetopo_id
1689 name = tokens[tokens.size() - 4] + "_" + tokens[tokens.size() - 3];
1690 }
1691
1692 topo_map[std::make_pair(name, side_topo)] = number_sides;
1693
1694 // We want the id to match the id on the sideset in this
1695 // case so that the generated name will match the current
1696 // name. Instead of converting from string to int back to
1697 // string, we just set a variable to query later.
1698 sid = tokens.back();
1699 }
1700 else if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) {
1701 // There are multiple side types in the model.
1702 // Iterate through the elements in the sideset, determine
1703 // their parent element block using the blocks element
1704 // topology and the side number, determine the side
1705 // type.
1706
1707 for (const auto &elem : sideTopology) {
1708 topo_map[std::make_pair(elem.first->name(), elem.second)] = 0;
1709 side_map[std::make_pair(elem.first->name(), elem.second)] = 0;
1710 }
1711
1712 get_element_sides_lists(get_file_pointer(), id, int_byte_size_api(), number_sides,
1713 element, sides);
1714 Ioex::separate_surface_element_sides(element, sides, get_region(), topo_map, side_map,
1715 split_type, side_set_name);
1716 }
1717 else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
1718 // There are multiple side types in the model. Iterate
1719 // through the elements in the sideset, determine their parent
1720 // element block using blocks element topology and the side
1721 // number, determine the side type.
1722
1723 // Seed the topo_map map with <block->name, side_topo>
1724 // pairs so we are sure that all processors have the same
1725 // starting topo_map (size and order).
1726 const Ioss::ElementBlockContainer &element_blocks = get_region()->get_element_blocks();
1727 assert(Ioss::Utils::check_block_order(element_blocks));
1728
1729 for (Ioss::ElementBlock *block : element_blocks) {
1730 if (!Ioss::Utils::block_is_omitted(block)) {
1731 std::string name = block->name();
1732 const Ioss::ElementTopology *common_ftopo = block->topology()->boundary_type(0);
1733 if (common_ftopo != nullptr) {
1734 // All sides of this element block's topology have the same topology
1735 topo_map[std::make_pair(name, common_ftopo)] = 0;
1736 side_map[std::make_pair(name, common_ftopo)] = 0;
1737 }
1738 else {
1739 // The sides have different topology, iterate over
1740 // them and create an entry for the unique side
1741 // topology types
1742 int par_dim = block->topology()->parametric_dimension();
1743 if (par_dim == 2 || par_dim == 3) {
1744 int64_t my_side_count = block->topology()->number_boundaries();
1745 for (int64_t ii = 0; ii < my_side_count; ii++) {
1746 const Ioss::ElementTopology *topo = block->topology()->boundary_type(ii + 1);
1747 topo_map[std::make_pair(name, topo)] = 0;
1748 side_map[std::make_pair(name, topo)] = 0;
1749 }
1750 }
1751 }
1752 }
1753 }
1754 get_element_sides_lists(get_file_pointer(), id, int_byte_size_api(), number_sides,
1755 element, sides);
1756 Ioex::separate_surface_element_sides(element, sides, get_region(), topo_map, side_map,
1757 split_type, side_set_name);
1758 }
1759 }
1760
1761 // End of first step in splitting. Check among all processors
1762 // to see which potential splits have sides in them...
1763 Ioss::Int64Vector global_side_counts(topo_map.size());
1764 {
1765 int64_t i = 0;
1766 for (const auto &topo : topo_map) {
1767 global_side_counts[i++] = topo.second;
1768 }
1769
1770 // If splitting by element block, also sync the side_map
1771 // information which specifies whether the sideset has
1772 // consistent sides for all elements. Only really used for
1773 // shells, but easier to just set the value on all surfaces
1774 // in the element block split case.
1775 if (side_map.size() == topo_map.size()) {
1776 global_side_counts.resize(topo_map.size() + side_map.size());
1777 for (const auto &side : side_map) {
1778 global_side_counts[i++] = side.second;
1779 }
1780 }
1781
1782 // See if any processor has non-zero count for the topo_map counts
1783 // For the side_map, need the max value.
1784 util().global_array_minmax(global_side_counts, Ioss::ParallelUtils::DO_MAX);
1785 }
1786
1787 // Create Side Blocks
1788
1789 int64_t i = 0;
1790 for (const auto &topo : topo_map) {
1791 if (global_side_counts[i++] > 0) {
1792 const std::string topo_or_block_name = topo.first.first;
1793 const Ioss::ElementTopology *side_topo = topo.first.second;
1794 assert(side_topo != nullptr);
1795 #if 0
1796 if (side_topo->parametric_dimension() == topology_dimension-1 ||
1797 split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
1798 #else
1799 if (true) {
1800 #endif
1801 int64_t my_side_count = topo.second;
1802
1803 std::string side_block_name =
1804 "surface_" + topo_or_block_name + "_" + side_topo->name();
1805 if (split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
1806 side_block_name = side_set_name;
1807 }
1808 else {
1809 if (db_has_name) {
1810 side_block_name =
1811 side_set->name() + "_" + topo_or_block_name + "_" + side_topo->name();
1812 }
1813 else {
1814 if (sid.empty()) {
1815 side_block_name = Ioss::Utils::encode_entity_name(side_block_name, id);
1816 }
1817 else {
1818 side_block_name += "_";
1819 side_block_name += sid;
1820 }
1821 }
1822 }
1823
1824 Ioss::ElementBlock *block = nullptr;
1825 // Need to get elem_topo....
1826 const Ioss::ElementTopology *elem_topo = nullptr;
1827 if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) {
1828 elem_topo = Ioss::ElementTopology::factory(topo_or_block_name);
1829 }
1830 else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
1831 block = get_region()->get_element_block(topo_or_block_name);
1832 if (block == nullptr || Ioss::Utils::block_is_omitted(block)) {
1833 IOSS_ERROR(fmt::format(
1834 "INTERNAL ERROR: Could not find element block '{}' Something is wrong "
1835 "in the Ioex::DatabaseIO class. Please report.\n",
1836 topo_or_block_name));
1837 }
1838 elem_topo = block->topology();
1839 }
1840 else if (split_type == Ioss::SPLIT_BY_DONT_SPLIT) {
1841 // Most likely this is "unknown", but can be a true
1842 // topology if there is only a single element block in
1843 // the model.
1844 elem_topo = Ioss::ElementTopology::factory(topo_or_block_name);
1845 }
1846 else {
1847 IOSS_ERROR(fmt::format(
1848 "INTERNAL ERROR: Invalid setting for `split_type` {}. Something is wrong "
1849 "in the Ioex::DatabaseIO class. Please report.\n",
1850 static_cast<int>(split_type)));
1851 }
1852 assert(elem_topo != nullptr);
1853
1854 auto side_block = new Ioss::SideBlock(this, side_block_name, side_topo->name(),
1855 elem_topo->name(), my_side_count);
1856 side_set->add(side_block);
1857
1858 // Note that all sideblocks within a specific
1859 // sideset might have the same id.
1860 assert(side_block != nullptr);
1861 side_block->property_add(Ioss::Property("id", id));
1862 side_block->property_add(Ioss::Property("guid", util().generate_guid(id)));
1863
1864 // If splitting by element block, need to set the
1865 // element block member on this side block.
1866 if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) {
1867 side_block->set_parent_element_block(block);
1868 }
1869
1870 // If we calculated whether the element side is
1871 // consistent for all sides in this block, then
1872 // tell the block which side it is, or that they are
1873 // inconsistent. If it wasn't calculated above, then it
1874 // will be calculated on the fly when/if requested.
1875 // This is to avoid reading the sideset bulk data in
1876 // cases where we don't need to read it, but if we are
1877 // already reading it (to split the sidesets), then use
1878 // the data when we have it.
1879 if (!side_map.empty()) {
1880 // Set a property indicating which element side
1881 // (1-based) all sides in this block are applied to.
1882 // If they are not all assigned to the same element
1883 // side, indicate this with a side equal to 0.
1884 //
1885 // (note: 'i' has already been incremented earlier in
1886 // the loop. We need previous value here...)
1887 int side = global_side_counts[i - 1 + topo_map.size()];
1888 if (side == 999) {
1889 side = 0;
1890 }
1891 SMART_ASSERT(side <= elem_topo->number_boundaries())
1892 (side)(elem_topo->number_boundaries());
1893 side_block->set_consistent_side_number(side);
1894 }
1895
1896 // Add an alias...
1897 get_region()->add_alias(side_block);
1898
1899 if (split_type != Ioss::SPLIT_BY_DONT_SPLIT && side_set_name != "universal_sideset") {
1900 std::string storage = "Real[";
1901 storage += std::to_string(side_topo->number_nodes());
1902 storage += "]";
1903 side_block->field_add(Ioss::Field("distribution_factors", Ioss::Field::REAL,
1904 storage, Ioss::Field::MESH));
1905 }
1906
1907 if (side_set_name == "universal_sideset") {
1908 side_block->field_add(Ioss::Field("side_ids", side_block->field_int_type(),
1909 "scalar", Ioss::Field::MESH));
1910 }
1911
1912 int num_attr = 0;
1913 {
1914 Ioss::SerializeIO serializeio_(this);
1915 int ierr = ex_get_attr_param(get_file_pointer(), EX_SIDE_SET, 1, &num_attr);
1916 if (ierr < 0) {
1917 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1918 }
1919 }
1920 // Add additional fields
1921 add_attribute_fields(side_block, num_attr, "");
1922 add_results_fields(side_block, iss);
1923 }
1924 }
1925 }
1926 }
1927 }
1928 } // namespace Ioex
1929
1930 template <typename T>
1931 void DatabaseIO::get_sets(ex_entity_type type, int64_t count, const std::string &base,
1932 const T * /* set_type */)
1933 {
1934 // Attributes of a Xset are:
1935 // -- id
1936 // -- name
1937 // -- number of nodes
1938 // -- number of distribution factors (see next comment)
1939 // ----the #distribution factors should equal #Xs or 0, any
1940 // other value does not make sense. If it is 0, then a substitute
1941 // list will be created returning 1.0 for the factor
1942
1943 // In a parallel execution, it is possible that a Xset will have
1944 // no Xs or distribution factors on a particular processor...
1945
1946 // Get exodus Xset metadata
1947 if (count > 0) {
1948 Ioss::Int64Vector Xset_ids(count);
1949 Ioss::IntVector attributes(count);
1950 std::vector<T *> Xsets(count);
1951 {
1952 Ioss::SerializeIO serializeio_(this);
1953 if (ex_int64_status(get_file_pointer()) & EX_IDS_INT64_API) {
1954 int error = ex_get_ids(get_file_pointer(), type, Data(Xset_ids));
1955 if (error < 0) {
1956 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1957 }
1958 }
1959 else {
1960 Ioss::IntVector tmp_set_ids(count);
1961 int error = ex_get_ids(get_file_pointer(), type, Data(tmp_set_ids));
1962 if (error < 0) {
1963 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1964 }
1965 std::copy(tmp_set_ids.begin(), tmp_set_ids.end(), Xset_ids.begin());
1966 }
1967
1968 std::vector<ex_set> set_params(count);
1969 for (int ins = 0; ins < count; ins++) {
1970 set_params[ins].type = type;
1971 set_params[ins].id = Xset_ids[ins];
1972 set_params[ins].entry_list = nullptr;
1973 set_params[ins].extra_list = nullptr;
1974 set_params[ins].distribution_factor_list = nullptr;
1975 }
1976
1977 int error = ex_get_sets(get_file_pointer(), count, Data(set_params));
1978 if (error < 0) {
1979 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1980 }
1981
1982 for (int ins = 0; ins < count; ins++) {
1983 int64_t id = set_params[ins].id;
1984 int num_attr = 0;
1985 int ierr = ex_get_attr_param(get_file_pointer(), type, id, &num_attr);
1986 if (ierr < 0) {
1987 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
1988 }
1989 attributes[ins] = num_attr;
1990
1991 bool db_has_name = false;
1992 std::string alias = Ioss::Utils::encode_entity_name(base + "list", id);
1993 std::string Xset_name;
1994 if (ignore_database_names()) {
1995 Xset_name = alias;
1996 }
1997 else {
1998 Xset_name = Ioex::get_entity_name(get_file_pointer(), type, id, base + "list",
1999 maximumNameLength, db_has_name);
2000 }
2001
2002 if (get_use_generic_canonical_name()) {
2003 std::swap(Xset_name, alias);
2004 }
2005
2006 bool filtered = false;
2007 int64_t original_set_size = set_params[ins].num_entry;
2008 Ioss::Int64Vector active_node_index;
2009 if ((!blockOmissions.empty() || !blockInclusions.empty()) && type == EX_NODE_SET) {
2010 active_node_index.resize(set_params[ins].num_entry);
2011 set_params[ins].entry_list = Data(active_node_index);
2012
2013 int old_status = ex_int64_status(get_file_pointer());
2014 ex_set_int64_status(get_file_pointer(), EX_BULK_INT64_API);
2015 error = ex_get_sets(get_file_pointer(), 1, &set_params[ins]);
2016 if (error < 0) {
2017 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2018 }
2019 ex_set_int64_status(get_file_pointer(), old_status);
2020
2021 compute_node_status();
2022 filtered = Ioex::filter_node_list(active_node_index, nodeConnectivityStatus);
2023 set_params[ins].num_entry = active_node_index.size();
2024 }
2025 auto *Xset = new T(this, Xset_name, set_params[ins].num_entry);
2026 Xsets[ins] = Xset;
2027 Xset->property_add(Ioss::Property("id", id));
2028 Xset->property_add(Ioss::Property("guid", util().generate_guid(id)));
2029 if (db_has_name) {
2030 std::string *db_name = &Xset_name;
2031 if (get_use_generic_canonical_name()) {
2032 db_name = &alias;
2033 }
2034 if (alias != Xset_name) {
2035 Xset->property_add(Ioss::Property("db_name", *db_name));
2036 }
2037 }
2038 if (filtered && type == EX_NODE_SET) {
2039 Xset->property_add(Ioss::Property("filtered_db_set_size", original_set_size));
2040 activeNodeSetNodesIndex[Xset_name].swap(active_node_index);
2041 }
2042 get_region()->add(Xset);
2043 get_region()->add_alias(Xset_name, alias, Xset->type());
2044 get_region()->add_alias(Xset_name, Ioss::Utils::encode_entity_name(base + "set", id),
2045 Xset->type());
2046 }
2047 }
2048
2049 // The attribute count will either be 0 if there are no
2050 // entities in the grouping entity on this processor, or it will be
2051 // the number of attributes (> 0). Therefore, if we take the 'max'
2052 // over all processors, each processor will then have the correct
2053 // attribute count...
2054 // This is a collective call...
2055 util().global_array_minmax(attributes, Ioss::ParallelUtils::DO_MAX);
2056
2057 for (int ins = 0; ins < count; ins++) {
2058 add_attribute_fields(Xsets[ins], attributes[ins], "");
2059 add_results_fields(Xsets[ins], ins);
2060 }
2061 }
2062 }
2063
2064 void DatabaseIO::get_nodesets()
2065 {
2066 get_sets(EX_NODE_SET, m_groupCount[EX_NODE_SET], "node", (Ioss::NodeSet *)nullptr);
2067 }
2068
2069 void DatabaseIO::get_edgesets()
2070 {
2071 get_sets(EX_EDGE_SET, m_groupCount[EX_EDGE_SET], "edge", (Ioss::EdgeSet *)nullptr);
2072 }
2073
2074 void DatabaseIO::get_facesets()
2075 {
2076 get_sets(EX_FACE_SET, m_groupCount[EX_FACE_SET], "face", (Ioss::FaceSet *)nullptr);
2077 }
2078
2079 void DatabaseIO::get_elemsets()
2080 {
2081 get_sets(EX_ELEM_SET, m_groupCount[EX_ELEM_SET], "element", (Ioss::ElementSet *)nullptr);
2082 }
2083
2084 void DatabaseIO::get_commsets()
2085 {
2086 // Attributes of a commset are:
2087 // -- id (property)
2088 // -- name (property)
2089 // -- number of node--CPU pairs (field)
2090
2091 // In a parallel execution, it is possible that a commset will have
2092 // no nodes on a particular processor...
2093
2094 // If this is a serial execution, there will be no communication
2095 // nodesets, just return an empty container.
2096
2097 if (isParallel || isSerialParallel) {
2098 Ioss::SerializeIO serializeio_(this);
2099 // This is a parallel run. There should be communications data
2100 // Get nemesis commset metadata
2101 int64_t my_node_count = 0;
2102 int64_t elem_count = 0;
2103
2104 // NOTE: It is possible for a parallel run to have no
2105 // communications maps if the decomposition occurs along contact
2106 // surfaces. In this case, we create empty node and element
2107 // communication maps.
2108 if (commsetNodeCount > 0 || commsetElemCount > 0) {
2109 if (commsetNodeCount > 0) {
2110 nodeCmapIds.resize(commsetNodeCount);
2111 nodeCmapNodeCnts.resize(commsetNodeCount);
2112 }
2113 if (commsetElemCount > 0) {
2114 elemCmapIds.resize(commsetElemCount);
2115 elemCmapElemCnts.resize(commsetElemCount);
2116 }
2117
2118 int error;
2119 if (int_byte_size_api() == 4) {
2120 Ioss::IntVector nci(nodeCmapIds.size());
2121 Ioss::IntVector ncnc(nodeCmapNodeCnts.size());
2122 Ioss::IntVector eci(elemCmapIds.size());
2123 Ioss::IntVector ecec(elemCmapElemCnts.size());
2124 error = ex_get_cmap_params(get_file_pointer(), Data(nci), Data(ncnc), Data(eci),
2125 Data(ecec), myProcessor);
2126 if (error >= 0) {
2127 std::copy(nci.begin(), nci.end(), nodeCmapIds.begin());
2128 std::copy(ncnc.begin(), ncnc.end(), nodeCmapNodeCnts.begin());
2129 std::copy(eci.begin(), eci.end(), elemCmapIds.begin());
2130 std::copy(ecec.begin(), ecec.end(), elemCmapElemCnts.begin());
2131 }
2132 }
2133 else {
2134 error = ex_get_cmap_params(get_file_pointer(), Data(nodeCmapIds), Data(nodeCmapNodeCnts),
2135 Data(elemCmapIds), Data(elemCmapElemCnts), myProcessor);
2136 }
2137 if (error < 0) {
2138 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2139 }
2140
2141 // Count nodes, elements, and convert counts to offsets.
2142 my_node_count +=
2143 std::accumulate(nodeCmapNodeCnts.begin(), nodeCmapNodeCnts.end(), int64_t(0));
2144
2145 elem_count += std::accumulate(elemCmapElemCnts.begin(), elemCmapElemCnts.end(), int64_t(0));
2146 }
2147 // Create a single node commset and a single element commset
2148 auto *commset = new Ioss::CommSet(this, "commset_node", "node", my_node_count);
2149 commset->property_add(Ioss::Property("id", 1));
2150 commset->property_add(Ioss::Property("guid", util().generate_guid(1)));
2151 get_region()->add(commset);
2152
2153 commset = new Ioss::CommSet(this, "commset_side", "side", elem_count);
2154 commset->property_add(Ioss::Property("id", 1));
2155 commset->property_add(Ioss::Property("guid", util().generate_guid(1)));
2156 get_region()->add(commset);
2157 }
2158 }
2159
2160 int64_t DatabaseIO::get_field_internal(const Ioss::Region *reg, const Ioss::Field &field,
2161 void *data, size_t data_size) const
2162 {
2163 return Ioex::BaseDatabaseIO::get_field_internal(reg, field, data, data_size);
2164 }
2165
2166 int64_t DatabaseIO::get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
2167 void *data, size_t data_size) const
2168 {
2169 {
2170 Ioss::SerializeIO serializeio_(this);
2171
2172 size_t num_to_get = field.verify(data_size);
2173 if (num_to_get > 0) {
2174
2175 #ifndef NDEBUG
2176 int64_t my_node_count = field.raw_count();
2177 SMART_ASSERT(my_node_count == nodeCount)(my_node_count)(nodeCount);
2178 #endif
2179
2180 Ioss::Field::RoleType role = field.get_role();
2181 if (role == Ioss::Field::MESH) {
2182 if (field.get_name() == "mesh_model_coordinates_x") {
2183 auto *rdata = static_cast<double *>(data);
2184 int ierr = ex_get_coord(get_file_pointer(), rdata, nullptr, nullptr);
2185 if (ierr < 0) {
2186 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2187 }
2188 }
2189
2190 else if (field.get_name() == "mesh_model_coordinates_y") {
2191 auto *rdata = static_cast<double *>(data);
2192 int ierr = ex_get_coord(get_file_pointer(), nullptr, rdata, nullptr);
2193 if (ierr < 0) {
2194 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2195 }
2196 }
2197
2198 else if (field.get_name() == "mesh_model_coordinates_z") {
2199 auto *rdata = static_cast<double *>(data);
2200 int ierr = ex_get_coord(get_file_pointer(), nullptr, nullptr, rdata);
2201 if (ierr < 0) {
2202 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2203 }
2204 }
2205
2206 else if (field.get_name() == "mesh_model_coordinates") {
2207 // Data required by upper classes store x0, y0, z0, ... xn,
2208 // yn, zn. Data stored in exodus file is x0, ..., xn, y0,
2209 // ..., yn, z0, ..., zn so we have to allocate some scratch
2210 // memory to read in the data and then map into supplied
2211 // 'data'
2212 std::vector<double> x(num_to_get);
2213 std::vector<double> y;
2214 if (spatialDimension > 1) {
2215 y.resize(num_to_get);
2216 }
2217 std::vector<double> z;
2218 if (spatialDimension == 3) {
2219 z.resize(num_to_get);
2220 }
2221
2222 // Cast 'data' to correct size -- double
2223 auto *rdata = static_cast<double *>(data);
2224
2225 int ierr = ex_get_coord(get_file_pointer(), Data(x), Data(y), Data(z));
2226 if (ierr < 0) {
2227 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2228 }
2229
2230 size_t index = 0;
2231 for (size_t i = 0; i < num_to_get; i++) {
2232 rdata[index++] = x[i];
2233 if (spatialDimension > 1) {
2234 rdata[index++] = y[i];
2235 }
2236 if (spatialDimension == 3) {
2237 rdata[index++] = z[i];
2238 }
2239 }
2240 }
2241
2242 else if (field.get_name() == "ids") {
2243 // Map the local ids in this node block
2244 // (1...node_count) to global node ids.
2245 get_map(EX_NODE_BLOCK).map_implicit_data(data, field, num_to_get, 0);
2246 }
2247
2248 // The 1..global_node_count id. In a parallel-decomposed run,
2249 // it maps the node back to its implicit position in the serial
2250 // undecomposed mesh file. This is ONLY provided for backward-
2251 // compatibility and should not be used unless absolutely required.
2252 else if (field.get_name() == "implicit_ids") {
2253 // If not parallel, then this is just 1..node_count
2254 // If parallel, then it is the data in the ex_get_id_map created by nem_spread.
2255 if (isParallel) {
2256 int error = ex_get_id_map(get_file_pointer(), EX_NODE_MAP, data);
2257 if (error < 0) {
2258 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2259 }
2260 }
2261 else {
2262 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
2263 auto *idata = static_cast<int64_t *>(data);
2264 for (int64_t i = 0; i < nodeCount; i++) {
2265 idata[i] = i + 1;
2266 }
2267 }
2268 else {
2269 auto *idata = static_cast<int *>(data);
2270 for (int64_t i = 0; i < nodeCount; i++) {
2271 idata[i] = i + 1;
2272 }
2273 }
2274 }
2275 }
2276
2277 else if (field.get_name() == "connectivity") {
2278 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2279 }
2280 else if (field.get_name() == "connectivity_raw") {
2281 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2282 }
2283 else if (field.get_name() == "node_connectivity_status") {
2284 compute_node_status();
2285 char *status = static_cast<char *>(data);
2286 std::copy(nodeConnectivityStatus.begin(), nodeConnectivityStatus.end(), status);
2287 }
2288
2289 else if (field.get_name() == "owning_processor") {
2290 // owning_processor field is always 32-bit.
2291 if (isParallel) {
2292 Ioss::CommSet *css = get_region()->get_commset("commset_node");
2293 int *idata = static_cast<int *>(data);
2294 for (int64_t i = 0; i < nodeCount; i++) {
2295 idata[i] = myProcessor;
2296 }
2297
2298 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
2299 Ioss::Field ep_field = css->get_field("entity_processor_raw");
2300 std::vector<int64_t> ent_proc(
2301 ep_field.raw_count() * ep_field.get_component_count(Ioss::Field::InOut::INPUT));
2302 size_t ep_data_size = ent_proc.size() * sizeof(int64_t);
2303 get_field_internal(css, ep_field, Data(ent_proc), ep_data_size);
2304 for (size_t i = 0; i < ent_proc.size(); i += 2) {
2305 int64_t node = ent_proc[i + 0];
2306 int64_t proc = ent_proc[i + 1];
2307 if (proc < myProcessor) {
2308 idata[node - 1] = proc;
2309 }
2310 }
2311 }
2312 else {
2313 Ioss::Field ep_field = css->get_field("entity_processor_raw");
2314 std::vector<int> ent_proc(ep_field.raw_count() *
2315 ep_field.get_component_count(Ioss::Field::InOut::INPUT));
2316 size_t ep_data_size = ent_proc.size() * sizeof(int);
2317 get_field_internal(css, ep_field, Data(ent_proc), ep_data_size);
2318 for (size_t i = 0; i < ent_proc.size(); i += 2) {
2319 int node = ent_proc[i + 0];
2320 int proc = ent_proc[i + 1];
2321 if (proc < myProcessor) {
2322 idata[node - 1] = proc;
2323 }
2324 }
2325 }
2326 }
2327 else {
2328 // Serial case...
2329 int *idata = static_cast<int *>(data);
2330 for (int64_t i = 0; i < nodeCount; i++) {
2331 idata[i] = 0;
2332 }
2333 }
2334 }
2335 else {
2336 num_to_get = Ioss::Utils::field_warning(nb, field, "input");
2337 }
2338 }
2339 else if (role == Ioss::Field::TRANSIENT) {
2340 // Check if the specified field exists on this node block.
2341 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2342 // exist on the database as scalars with the appropriate
2343 // extensions.
2344
2345 // Read in each component of the variable and transfer into
2346 // 'data'. Need temporary storage area of size 'number of
2347 // nodes in this block.
2348 num_to_get = read_transient_field(m_variables[EX_NODE_BLOCK], field, nb, data);
2349 }
2350 else if (role == Ioss::Field::REDUCTION) {
2351 get_reduction_field(field, nb, data);
2352 }
2353 else if (role == Ioss::Field::ATTRIBUTE) {
2354 num_to_get = read_attribute_field(field, nb, data);
2355 }
2356 }
2357 return num_to_get;
2358 }
2359 }
2360
2361 int64_t DatabaseIO::get_field_internal(const Ioss::Blob *blob, const Ioss::Field &field,
2362 void *data, size_t data_size) const
2363 {
2364 {
2365 Ioss::SerializeIO serializeio_(this);
2366
2367 size_t num_to_get = field.verify(data_size);
2368 if (num_to_get > 0) {
2369
2370 Ioss::Field::RoleType role = field.get_role();
2371 if (role == Ioss::Field::MESH) {
2372 if (field.get_name() == "ids") {
2373 // Map the local ids in this node block
2374 // (1...node_count) to global node ids.
2375 // get_map(EX_BLOB).map_implicit_data(data, field, num_to_get, 0);
2376 }
2377
2378 else if (field.get_name() == "connectivity") {
2379 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2380 }
2381 else if (field.get_name() == "connectivity_raw") {
2382 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2383 }
2384 else {
2385 num_to_get = Ioss::Utils::field_warning(blob, field, "input");
2386 }
2387 }
2388 else if (role == Ioss::Field::TRANSIENT) {
2389 // Check if the specified field exists on this blob.
2390 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2391 // exist on the database as scalars with the appropriate
2392 // extensions.
2393
2394 // Read in each component of the variable and transfer into
2395 // 'data'. Need temporary storage area of size 'number of
2396 // items in this blob.
2397 num_to_get = read_transient_field(m_variables[EX_BLOB], field, blob, data);
2398 }
2399 else if (role == Ioss::Field::REDUCTION) {
2400 get_reduction_field(field, blob, data);
2401 }
2402 else if (role == Ioss::Field::ATTRIBUTE) {
2403 num_to_get = read_attribute_field(field, blob, data);
2404 }
2405 }
2406 return num_to_get;
2407 }
2408 }
2409
2410 int64_t DatabaseIO::get_field_internal(const Ioss::Assembly *assembly, const Ioss::Field &field,
2411 void *data, size_t data_size) const
2412 {
2413 {
2414 Ioss::SerializeIO serializeio_(this);
2415
2416 size_t num_to_get = field.verify(data_size);
2417 if (num_to_get > 0) {
2418
2419 Ioss::Field::RoleType role = field.get_role();
2420 if (role == Ioss::Field::MESH) {
2421 if (field.get_name() == "ids") {
2422 // Map the local ids in this node block
2423 // (1...node_count) to global node ids.
2424 // get_map(EX_ASSEMBLY).map_implicit_data(data, field, num_to_get, 0);
2425 }
2426
2427 else if (field.get_name() == "connectivity") {
2428 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2429 }
2430 else if (field.get_name() == "connectivity_raw") {
2431 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2432 }
2433 else {
2434 num_to_get = Ioss::Utils::field_warning(assembly, field, "input");
2435 }
2436 }
2437 else if (role == Ioss::Field::TRANSIENT) {
2438 // Check if the specified field exists on this assembly.
2439 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2440 // exist on the database as scalars with the appropriate
2441 // extensions.
2442
2443 // Read in each component of the variable and transfer into
2444 // 'data'. Need temporary storage area of size 'number of
2445 // items in this assembly.
2446 num_to_get = read_transient_field(m_variables[EX_ASSEMBLY], field, assembly, data);
2447 }
2448 else if (role == Ioss::Field::REDUCTION) {
2449 get_reduction_field(field, assembly, data);
2450 }
2451 else if (role == Ioss::Field::ATTRIBUTE) {
2452 num_to_get = read_attribute_field(field, assembly, data);
2453 }
2454 }
2455 return num_to_get;
2456 }
2457 }
2458
2459 int64_t DatabaseIO::get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
2460 void *data, size_t data_size) const
2461 {
2462 {
2463 Ioss::SerializeIO serializeio_(this);
2464
2465 size_t num_to_get = field.verify(data_size);
2466 if (num_to_get > 0) {
2467
2468 int64_t id = Ioex::get_id(eb, &ids_);
2469 size_t my_element_count = eb->entity_count();
2470 Ioss::Field::RoleType role = field.get_role();
2471
2472 if (role == Ioss::Field::MESH) {
2473 // Handle the MESH fields required for an Exodus file model.
2474 // (The 'genesis' portion)
2475
2476 if (field.get_name() == "connectivity") {
2477 int element_nodes = eb->topology()->number_nodes();
2478 assert(field.get_component_count(Ioss::Field::InOut::INPUT) == element_nodes);
2479
2480 // The connectivity is stored in a 1D array.
2481 // The element_node index varies fastet
2482 if (my_element_count > 0) {
2483 get_connectivity_data(get_file_pointer(), data, EX_ELEM_BLOCK, id, 0);
2484 get_map(EX_NODE_BLOCK).map_data(data, field, num_to_get * element_nodes);
2485 }
2486 }
2487 else if (field.get_name() == "connectivity_face") {
2488 // The connectivity is stored in a 1D array.
2489 // The element_face index varies fastest
2490 if (my_element_count > 0) {
2491 int face_count = field.get_component_count(Ioss::Field::InOut::INPUT);
2492 get_connectivity_data(get_file_pointer(), data, EX_ELEM_BLOCK, id, 2);
2493 get_map(EX_FACE_BLOCK).map_data(data, field, num_to_get * face_count);
2494 }
2495 }
2496 else if (field.get_name() == "connectivity_edge") {
2497 // The connectivity is stored in a 1D array.
2498 // The element_edge index varies fastest
2499 if (my_element_count > 0) {
2500 int edge_count = field.get_component_count(Ioss::Field::InOut::INPUT);
2501 get_connectivity_data(get_file_pointer(), data, EX_ELEM_BLOCK, id, 1);
2502 get_map(EX_EDGE_BLOCK).map_data(data, field, num_to_get * edge_count);
2503 }
2504 }
2505 else if (field.get_name() == "connectivity_raw") {
2506 // "connectivity_raw" has nodes in local id space (1-based)
2507 assert(field.get_component_count(Ioss::Field::InOut::INPUT) ==
2508 eb->topology()->number_nodes());
2509
2510 // The connectivity is stored in a 1D array.
2511 // The element_node index varies fastest
2512 if (my_element_count > 0) {
2513 get_connectivity_data(get_file_pointer(), data, EX_ELEM_BLOCK, id, 0);
2514 }
2515 }
2516 else if (field.get_name() == "ids") {
2517 // Map the local ids in this element block
2518 // (eb_offset+1...eb_offset+1+my_element_count) to global element ids.
2519 get_map(EX_ELEM_BLOCK).map_implicit_data(data, field, num_to_get, eb->get_offset());
2520 }
2521 else if (field.get_name() == "implicit_ids") {
2522 // If not parallel, then this is just one..element_count
2523 // If parallel, then it is the data in the ex_get_id_map created by nem_spread.
2524 size_t eb_offset_plus_one = eb->get_offset() + 1;
2525 if (isParallel) {
2526 int error = ex_get_partial_id_map(get_file_pointer(), EX_ELEM_MAP, eb_offset_plus_one,
2527 my_element_count, data);
2528 if (error < 0) {
2529 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2530 }
2531 }
2532 else {
2533 if ((ex_int64_status(get_file_pointer()) & EX_BULK_INT64_API) != 0) {
2534 auto *idata = static_cast<int64_t *>(data);
2535 for (size_t i = 0; i < my_element_count; i++) {
2536 idata[i] = eb_offset_plus_one + i;
2537 }
2538 }
2539 else {
2540 auto *idata = static_cast<int *>(data);
2541 for (size_t i = 0; i < my_element_count; i++) {
2542 idata[i] = eb_offset_plus_one + i;
2543 }
2544 }
2545 }
2546 }
2547 }
2548 else if (role == Ioss::Field::MAP) {
2549 int component_count = field.get_component_count(Ioss::Field::InOut::INPUT);
2550 if (component_count == 1) {
2551 if (field.is_type(Ioss::Field::INTEGER)) {
2552 Ioss::IntVector component(my_element_count);
2553 int *data32 = reinterpret_cast<int *>(data);
2554
2555 size_t eb_offset = eb->get_offset();
2556 ex_get_partial_num_map(get_file_pointer(), EX_ELEM_MAP, field.get_index(),
2557 eb_offset + 1, my_element_count, Data(component));
2558
2559 for (size_t i = 0; i < my_element_count; i++) {
2560 data32[i] = component[i];
2561 }
2562 }
2563 else {
2564 Ioss::Int64Vector component(my_element_count);
2565 auto *data64 = reinterpret_cast<int64_t *>(data);
2566
2567 size_t eb_offset = eb->get_offset();
2568 ex_get_partial_num_map(get_file_pointer(), EX_ELEM_MAP, field.get_index(),
2569 eb_offset + 1, my_element_count, Data(component));
2570
2571 for (size_t i = 0; i < my_element_count; i++) {
2572 data64[i] = component[i];
2573 }
2574 }
2575 }
2576 else {
2577 // Multi-component field...
2578 if (field.is_type(Ioss::Field::INTEGER)) {
2579 Ioss::IntVector component(my_element_count);
2580 int *data32 = reinterpret_cast<int *>(data);
2581 size_t eb_offset = eb->get_offset();
2582
2583 for (int comp = 0; comp < component_count; comp++) {
2584 ex_get_partial_num_map(get_file_pointer(), EX_ELEM_MAP, field.get_index() + comp,
2585 eb_offset + 1, my_element_count, Data(component));
2586
2587 int index = comp;
2588 for (size_t i = 0; i < my_element_count; i++) {
2589 data32[index] = component[i];
2590 index += component_count;
2591 }
2592 }
2593 }
2594 else {
2595 Ioss::Int64Vector component(my_element_count);
2596 auto *data64 = reinterpret_cast<int64_t *>(data);
2597 size_t eb_offset = eb->get_offset();
2598
2599 for (int comp = 0; comp < component_count; comp++) {
2600 ex_get_partial_num_map(get_file_pointer(), EX_ELEM_MAP, field.get_index() + comp,
2601 eb_offset + 1, my_element_count, Data(component));
2602
2603 int index = comp;
2604 for (size_t i = 0; i < my_element_count; i++) {
2605 data64[index] = component[i];
2606 index += component_count;
2607 }
2608 }
2609 }
2610 }
2611 }
2612 else if (role == Ioss::Field::ATTRIBUTE) {
2613 num_to_get = read_attribute_field(field, eb, data);
2614 }
2615 else if (role == Ioss::Field::TRANSIENT) {
2616 // Check if the specified field exists on this element block.
2617 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2618 // exist on the database as scalars with the appropriate
2619 // extensions.
2620
2621 // Read in each component of the variable and transfer into
2622 // 'data'. Need temporary storage area of size 'number of
2623 // elements in this block.
2624 num_to_get = read_transient_field(m_variables[EX_ELEM_BLOCK], field, eb, data);
2625 }
2626 else if (role == Ioss::Field::REDUCTION) {
2627 get_reduction_field(field, eb, data);
2628 }
2629 }
2630 return num_to_get;
2631 }
2632 }
2633
2634 int64_t DatabaseIO::get_field_internal(const Ioss::FaceBlock *eb, const Ioss::Field &field,
2635 void *data, size_t data_size) const
2636 {
2637 {
2638 Ioss::SerializeIO serializeio_(this);
2639
2640 size_t num_to_get = field.verify(data_size);
2641 if (num_to_get > 0) {
2642
2643 int64_t id = Ioex::get_id(eb, &ids_);
2644 size_t my_face_count = eb->entity_count();
2645 Ioss::Field::RoleType role = field.get_role();
2646
2647 if (role == Ioss::Field::MESH) {
2648 // Handle the MESH fields required for an Exodus file model.
2649 // (The 'genesis' portion)
2650
2651 if (field.get_name() == "connectivity") {
2652 // The connectivity is stored in a 1D array.
2653 // The face_node index varies fastet
2654 if (my_face_count > 0) {
2655 int face_nodes = eb->topology()->number_nodes();
2656 assert(field.get_component_count(Ioss::Field::InOut::INPUT) == face_nodes);
2657
2658 get_connectivity_data(get_file_pointer(), data, EX_FACE_BLOCK, id, 0);
2659 get_map(EX_NODE_BLOCK).map_data(data, field, num_to_get * face_nodes);
2660 }
2661 }
2662 else if (field.get_name() == "connectivity_edge") {
2663 // The connectivity is stored in a 1D array.
2664 // The face_edge index varies fastest
2665 if (my_face_count > 0) {
2666 int edge_count = field.get_component_count(Ioss::Field::InOut::INPUT);
2667 get_connectivity_data(get_file_pointer(), data, EX_FACE_BLOCK, id, 1);
2668 get_map(EX_EDGE_BLOCK).map_data(data, field, num_to_get * edge_count);
2669 }
2670 }
2671 else if (field.get_name() == "connectivity_raw") {
2672 // The connectivity is stored in a 1D array.
2673 // The face_node index varies fastet
2674 if (my_face_count > 0) {
2675 // "connectivity_raw" has nodes in local id space (1-based)
2676 assert(field.get_component_count(Ioss::Field::InOut::INPUT) ==
2677 eb->topology()->number_nodes());
2678
2679 get_connectivity_data(get_file_pointer(), data, EX_FACE_BLOCK, id, 0);
2680 }
2681 }
2682 else if (field.get_name() == "ids") {
2683 // Map the local ids in this face block
2684 // (eb_offset+1...eb_offset+1+my_face_count) to global face ids.
2685 get_map(EX_FACE_BLOCK).map_implicit_data(data, field, num_to_get, eb->get_offset());
2686 }
2687 else {
2688 num_to_get = Ioss::Utils::field_warning(eb, field, "input");
2689 }
2690 }
2691 else if (role == Ioss::Field::ATTRIBUTE) {
2692 num_to_get = read_attribute_field(field, eb, data);
2693 }
2694 else if (role == Ioss::Field::TRANSIENT) {
2695 // Check if the specified field exists on this element block.
2696 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2697 // exist on the database as scalars with the appropriate
2698 // extensions.
2699
2700 // Read in each component of the variable and transfer into
2701 // 'data'. Need temporary storage area of size 'number of
2702 // elements in this block.
2703 num_to_get = read_transient_field(m_variables[EX_FACE_BLOCK], field, eb, data);
2704 }
2705 else if (role == Ioss::Field::REDUCTION) {
2706 get_reduction_field(field, eb, data);
2707 }
2708 }
2709 return num_to_get;
2710 }
2711 }
2712
2713 int64_t DatabaseIO::get_field_internal(const Ioss::EdgeBlock *eb, const Ioss::Field &field,
2714 void *data, size_t data_size) const
2715 {
2716 {
2717 Ioss::SerializeIO serializeio_(this);
2718
2719 size_t num_to_get = field.verify(data_size);
2720 if (num_to_get > 0) {
2721
2722 int64_t id = Ioex::get_id(eb, &ids_);
2723 int64_t my_edge_count = eb->entity_count();
2724 Ioss::Field::RoleType role = field.get_role();
2725
2726 if (role == Ioss::Field::MESH) {
2727 // Handle the MESH fields required for an Exodus file model.
2728 // (The 'genesis' portion)
2729
2730 if (field.get_name() == "connectivity") {
2731 int edge_nodes = eb->topology()->number_nodes();
2732 assert(field.get_component_count(Ioss::Field::InOut::INPUT) == edge_nodes);
2733
2734 // The connectivity is stored in a 1D array.
2735 // The edge_node index varies fastet
2736 if (my_edge_count > 0) {
2737 get_connectivity_data(get_file_pointer(), data, EX_EDGE_BLOCK, id, 0);
2738 get_map(EX_NODE_BLOCK).map_data(data, field, num_to_get * edge_nodes);
2739 }
2740 }
2741 else if (field.get_name() == "connectivity_raw") {
2742 // "connectivity_raw" has nodes in local id space (1-based)
2743 assert(field.get_component_count(Ioss::Field::InOut::INPUT) ==
2744 eb->topology()->number_nodes());
2745
2746 // The connectivity is stored in a 1D array.
2747 // The edge_node index varies fastet
2748 if (my_edge_count > 0) {
2749 get_connectivity_data(get_file_pointer(), data, EX_EDGE_BLOCK, id, 0);
2750 }
2751 }
2752 else if (field.get_name() == "ids") {
2753 // Map the local ids in this edge block
2754 // (eb_offset+1...eb_offset+1+my_edge_count) to global edge ids.
2755 get_map(EX_EDGE_BLOCK).map_implicit_data(data, field, num_to_get, eb->get_offset());
2756 }
2757 else {
2758 num_to_get = Ioss::Utils::field_warning(eb, field, "input");
2759 }
2760 }
2761 else if (role == Ioss::Field::ATTRIBUTE) {
2762 num_to_get = read_attribute_field(field, eb, data);
2763 }
2764 else if (role == Ioss::Field::TRANSIENT) {
2765 // Check if the specified field exists on this element block.
2766 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2767 // exist on the database as scalars with the appropriate
2768 // extensions.
2769
2770 // Read in each component of the variable and transfer into
2771 // 'data'. Need temporary storage area of size 'number of
2772 // elements in this block.
2773 num_to_get = read_transient_field(m_variables[EX_EDGE_BLOCK], field, eb, data);
2774 }
2775 else if (role == Ioss::Field::REDUCTION) {
2776 get_reduction_field(field, eb, data);
2777 }
2778 }
2779 return num_to_get;
2780 }
2781 }
2782
2783 int64_t DatabaseIO::get_Xset_field_internal(const Ioss::EntitySet *ns, const Ioss::Field &field,
2784 void *data, size_t data_size) const
2785 {
2786 {
2787 int ierr;
2788 Ioss::SerializeIO serializeio_(this);
2789
2790 size_t num_to_get = field.verify(data_size);
2791 if (num_to_get > 0) {
2792
2793 ex_entity_type type = Ioex::map_exodus_type(ns->type());
2794 int64_t id = Ioex::get_id(ns, &ids_);
2795 Ioss::Field::RoleType role = field.get_role();
2796 if (role == Ioss::Field::MESH) {
2797
2798 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
2799 if (field.get_type() == Ioss::Field::INTEGER) {
2800 ierr = ex_get_set(get_file_pointer(), type, id, static_cast<int *>(data), nullptr);
2801 }
2802 else {
2803 ierr =
2804 ex_get_set(get_file_pointer(), type, id, static_cast<int64_t *>(data), nullptr);
2805 }
2806 if (ierr < 0) {
2807 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2808 }
2809
2810 if (field.get_name() == "ids") {
2811 // Convert the local node ids to global ids
2812 get_map(type).map_data(data, field, num_to_get);
2813 }
2814 }
2815 else if (field.get_name() == "orientation") {
2816 if (field.get_type() == Ioss::Field::INTEGER) {
2817 ierr = ex_get_set(get_file_pointer(), type, id, nullptr, static_cast<int *>(data));
2818 }
2819 else {
2820 ierr =
2821 ex_get_set(get_file_pointer(), type, id, nullptr, static_cast<int64_t *>(data));
2822 }
2823 if (ierr < 0) {
2824 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2825 }
2826 }
2827 else if (field.get_name() == "distribution_factors") {
2828 ex_set set_param[1];
2829 set_param[0].id = id;
2830 set_param[0].type = type;
2831 set_param[0].entry_list = nullptr;
2832 set_param[0].extra_list = nullptr;
2833 set_param[0].distribution_factor_list = nullptr;
2834 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
2835 if (ierr < 0) {
2836 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2837 }
2838
2839 if (set_param[0].num_distribution_factor == 0) {
2840 auto *rdata = static_cast<double *>(data);
2841 for (size_t i = 0; i < num_to_get; i++) {
2842 rdata[i] = 1.0;
2843 }
2844 }
2845 else {
2846 set_param[0].distribution_factor_list = static_cast<double *>(data);
2847 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
2848 if (ierr < 0) {
2849 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2850 }
2851 }
2852 }
2853 else {
2854 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
2855 }
2856 }
2857 else if (role == Ioss::Field::ATTRIBUTE) {
2858 num_to_get = read_attribute_field(field, ns, data);
2859 }
2860 else if (role == Ioss::Field::REDUCTION) {
2861 get_reduction_field(field, ns, data);
2862 }
2863 else if (role == Ioss::Field::TRANSIENT) {
2864 // Check if the specified field exists on this node block.
2865 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
2866 // exist on the database as scalars with the appropriate
2867 // extensions.
2868
2869 // Read in each component of the variable and transfer into
2870 // 'data'. Need temporary storage area of size 'number of
2871 // nodes in this block.
2872 num_to_get = read_transient_field(m_variables[type], field, ns, data);
2873 }
2874 }
2875 return num_to_get;
2876 }
2877 }
2878
2879 int64_t DatabaseIO::get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
2880 void *data, size_t data_size) const
2881 {
2882 if (!ns->property_exists("filtered_db_set_size")) {
2883 return get_Xset_field_internal(ns, field, data, data_size);
2884 }
2885
2886 size_t db_size = ns->get_property("filtered_db_set_size").get_int();
2887
2888 int ierr;
2889 Ioss::SerializeIO serializeio_(this);
2890
2891 size_t num_to_get = field.verify(data_size);
2892 if (num_to_get > 0) {
2893
2894 int64_t id = Ioex::get_id(ns, &ids_);
2895 Ioss::Field::RoleType role = field.get_role();
2896 if (role == Ioss::Field::MESH) {
2897
2898 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
2899 if (field.get_type() == Ioss::Field::INTEGER) {
2900 Ioss::IntVector dbvals(db_size);
2901 ierr = ex_get_set(get_file_pointer(), EX_NODE_SET, id, Data(dbvals), nullptr);
2902 if (ierr >= 0) {
2903 Ioex::filter_node_list(static_cast<int *>(data), dbvals,
2904 activeNodeSetNodesIndex[ns->name()]);
2905 }
2906 }
2907 else {
2908 Ioss::Int64Vector dbvals(db_size);
2909 ierr = ex_get_set(get_file_pointer(), EX_NODE_SET, id, Data(dbvals), nullptr);
2910 if (ierr >= 0) {
2911 Ioex::filter_node_list(static_cast<int64_t *>(data), dbvals,
2912 activeNodeSetNodesIndex[ns->name()]);
2913 }
2914 }
2915 if (ierr < 0) {
2916 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2917 }
2918
2919 if (field.get_name() == "ids") {
2920 // Convert the local node ids to global ids
2921 get_map(EX_NODE_SET).map_data(data, field, num_to_get);
2922 }
2923 }
2924 else if (field.get_name() == "distribution_factors") {
2925 ex_set set_param[1];
2926 set_param[0].id = id;
2927 set_param[0].type = EX_NODE_SET;
2928 set_param[0].entry_list = nullptr;
2929 set_param[0].extra_list = nullptr;
2930 set_param[0].distribution_factor_list = nullptr;
2931 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
2932 if (ierr < 0) {
2933 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2934 }
2935
2936 if (set_param[0].num_distribution_factor == 0) {
2937 auto *rdata = static_cast<double *>(data);
2938 for (size_t i = 0; i < num_to_get; i++) {
2939 rdata[i] = 1.0;
2940 }
2941 }
2942 else {
2943 std::vector<double> dbvals(db_size);
2944 set_param[0].distribution_factor_list = Data(dbvals);
2945 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
2946 if (ierr < 0) {
2947 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
2948 }
2949 Ioex::filter_node_list(static_cast<double *>(data), dbvals,
2950 activeNodeSetNodesIndex[ns->name()]);
2951 set_param[0].distribution_factor_list = nullptr;
2952 }
2953 }
2954 else {
2955 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
2956 }
2957 }
2958 else if (role == Ioss::Field::ATTRIBUTE) {
2959 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
2960 }
2961 else if (role == Ioss::Field::REDUCTION) {
2962 num_to_get = Ioss::Utils::field_warning(ns, field, "input");
2963 }
2964 else if (role == Ioss::Field::TRANSIENT) {
2965 // Filtered not currently implemented for transient or attributes....
2966 }
2967 }
2968 return num_to_get;
2969 }
2970
2971 int64_t DatabaseIO::get_field_internal(const Ioss::EdgeSet *ns, const Ioss::Field &field,
2972 void *data, size_t data_size) const
2973 {
2974 return get_Xset_field_internal(ns, field, data, data_size);
2975 }
2976
2977 int64_t DatabaseIO::get_field_internal(const Ioss::FaceSet *ns, const Ioss::Field &field,
2978 void *data, size_t data_size) const
2979 {
2980 return get_Xset_field_internal(ns, field, data, data_size);
2981 }
2982
2983 int64_t DatabaseIO::get_field_internal(const Ioss::ElementSet *ns, const Ioss::Field &field,
2984 void *data, size_t data_size) const
2985 {
2986 return get_Xset_field_internal(ns, field, data, data_size);
2987 }
2988
2989 int64_t DatabaseIO::get_field_internal(const Ioss::SideSet *fs, const Ioss::Field &field,
2990 void * /* data */, size_t data_size) const
2991 {
2992 size_t num_to_get = field.verify(data_size);
2993 if (field.get_name() == "ids") {
2994 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
2995 }
2996 else {
2997 num_to_get = Ioss::Utils::field_warning(fs, field, "input");
2998 }
2999 return num_to_get;
3000 }
3001
3002 int64_t DatabaseIO::get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
3003 void *data, size_t data_size) const
3004 {
3005 {
3006 Ioss::SerializeIO serializeio_(this);
3007
3008 size_t num_to_get = field.verify(data_size);
3009
3010 if (num_to_get > 0) {
3011 int64_t entity_count = cs->entity_count();
3012
3013 // Return the <entity (node or side), processor> pair
3014 if (field.get_name() == "entity_processor" || field.get_name() == "entity_processor_raw") {
3015
3016 // Check type -- node or side
3017 std::string type = cs->get_property("entity_type").get_string();
3018
3019 // Allocate temporary storage space
3020 std::vector<char> entities(num_to_get * int_byte_size_api());
3021 std::vector<char> procs(num_to_get * int_byte_size_api());
3022
3023 if (type == "node") {
3024 int64_t cm_offset = 0;
3025
3026 for (int64_t i = 0; i < commsetNodeCount; i++) {
3027 int ierr = ex_get_node_cmap(get_file_pointer(), nodeCmapIds[i], &entities[cm_offset],
3028 &procs[cm_offset], myProcessor);
3029 if (ierr < 0) {
3030 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3031 }
3032 cm_offset += (nodeCmapNodeCnts[i] * int_byte_size_api());
3033 }
3034 assert(cm_offset == entity_count * int_byte_size_api());
3035
3036 // Convert local node id to global node id and store in 'data'
3037 if (int_byte_size_api() == 4) {
3038 int *entity_proc = static_cast<int *>(data);
3039 int *ents = reinterpret_cast<int *>(Data(entities));
3040 int *pros = reinterpret_cast<int *>(Data(procs));
3041
3042 size_t j = 0;
3043 if (field.get_name() == "entity_processor") {
3044 const Ioss::MapContainer &map = get_map(EX_NODE_BLOCK).map();
3045
3046 for (int64_t i = 0; i < entity_count; i++) {
3047 int local_id = ents[i];
3048 entity_proc[j++] = map[local_id];
3049 entity_proc[j++] = pros[i];
3050 }
3051 }
3052 else { // "entity_processor_raw"
3053 for (int64_t i = 0; i < entity_count; i++) {
3054 entity_proc[j++] = ents[i];
3055 entity_proc[j++] = pros[i];
3056 }
3057 }
3058 }
3059 else {
3060 auto *entity_proc = static_cast<int64_t *>(data);
3061 auto *ents = reinterpret_cast<int64_t *>(Data(entities));
3062 auto *pros = reinterpret_cast<int64_t *>(Data(procs));
3063
3064 size_t j = 0;
3065 if (field.get_name() == "entity_processor") {
3066 const Ioss::MapContainer &map = get_map(EX_NODE_BLOCK).map();
3067
3068 for (int64_t i = 0; i < entity_count; i++) {
3069 int64_t local_id = ents[i];
3070 entity_proc[j++] = map[local_id];
3071 entity_proc[j++] = pros[i];
3072 }
3073 }
3074 else { // "entity_processor_raw"
3075 for (int64_t i = 0; i < entity_count; i++) {
3076 entity_proc[j++] = ents[i];
3077 entity_proc[j++] = pros[i];
3078 }
3079 }
3080 }
3081 }
3082 else if (type == "side") {
3083 std::vector<char> sides(entity_count * int_byte_size_api());
3084 int64_t cm_offset = 0;
3085 for (int64_t i = 0; i < commsetElemCount; i++) {
3086 int ierr = ex_get_elem_cmap(get_file_pointer(), elemCmapIds[i], &entities[cm_offset],
3087 &sides[cm_offset], &procs[cm_offset], myProcessor);
3088 if (ierr < 0) {
3089 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3090 }
3091 cm_offset += (elemCmapElemCnts[i] * int_byte_size_api());
3092 }
3093 assert(cm_offset == entity_count * int_byte_size_api());
3094
3095 if (int_byte_size_api() == 4) {
3096 int *entity_proc = static_cast<int *>(data);
3097 int *ents = reinterpret_cast<int *>(Data(entities));
3098 int *pros = reinterpret_cast<int *>(Data(procs));
3099 int *sids = reinterpret_cast<int *>(Data(sides));
3100
3101 size_t j = 0;
3102 if (field.get_name() == "entity_processor") {
3103 const Ioss::MapContainer &map = get_map(EX_ELEM_BLOCK).map();
3104
3105 for (int64_t i = 0; i < entity_count; i++) {
3106 entity_proc[j++] = map[ents[i]];
3107 entity_proc[j++] = sids[i];
3108 entity_proc[j++] = pros[i];
3109 }
3110 }
3111 else { // "entity_processor_raw"
3112 for (int64_t i = 0; i < entity_count; i++) {
3113 entity_proc[j++] = ents[i];
3114 entity_proc[j++] = sids[i];
3115 entity_proc[j++] = pros[i];
3116 }
3117 }
3118 }
3119 else {
3120 auto *entity_proc = static_cast<int64_t *>(data);
3121 auto *ents = reinterpret_cast<int64_t *>(Data(entities));
3122 auto *pros = reinterpret_cast<int64_t *>(Data(procs));
3123 auto *sids = reinterpret_cast<int64_t *>(Data(sides));
3124
3125 size_t j = 0;
3126 if (field.get_name() == "entity_processor") {
3127 const Ioss::MapContainer &map = get_map(EX_ELEM_BLOCK).map();
3128
3129 for (int64_t i = 0; i < entity_count; i++) {
3130 entity_proc[j++] = map[ents[i]];
3131 entity_proc[j++] = sids[i];
3132 entity_proc[j++] = pros[i];
3133 }
3134 }
3135 else { // "entity_processor_raw"
3136 for (int64_t i = 0; i < entity_count; i++) {
3137 entity_proc[j++] = ents[i];
3138 entity_proc[j++] = sids[i];
3139 entity_proc[j++] = pros[i];
3140 }
3141 }
3142 }
3143 }
3144 else {
3145 IOSS_ERROR(fmt::format("ERROR: Invalid commset type {}", type));
3146 }
3147 }
3148 else if (field.get_name() == "ids") {
3149 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
3150 }
3151 else {
3152 num_to_get = Ioss::Utils::field_warning(cs, field, "input");
3153 }
3154 }
3155 return num_to_get;
3156 }
3157 }
3158
3159 int64_t DatabaseIO::get_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field,
3160 void *data, size_t data_size) const
3161 {
3162 Ioss::SerializeIO serializeio_(this);
3163 int64_t num_to_get = field.verify(data_size);
3164 if (num_to_get > 0) {
3165
3166 int64_t id = Ioex::get_id(fb, &ids_);
3167 int64_t entity_count = fb->entity_count();
3168 if (num_to_get != entity_count) {
3169 IOSS_ERROR("ERROR: Partial field input not yet implemented for side blocks");
3170 }
3171
3172 ex_set set_param[1];
3173 set_param[0].id = id;
3174 set_param[0].type = EX_SIDE_SET;
3175 set_param[0].entry_list = nullptr;
3176 set_param[0].extra_list = nullptr;
3177 set_param[0].distribution_factor_list = nullptr;
3178 int ierr = ex_get_sets(get_file_pointer(), 1, set_param);
3179 if (ierr < 0) {
3180 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3181 }
3182
3183 int64_t number_sides = set_param[0].num_entry;
3184 int64_t number_distribution_factors = set_param[0].num_distribution_factor;
3185
3186 Ioss::Field::RoleType role = field.get_role();
3187 if (role == Ioss::Field::MESH) {
3188
3189 // In exodus, we may have split the sideset into multiple
3190 // side blocks if there are multiple side topologies in the
3191 // sideset. Because of this, the passed in 'data' may not be
3192 // large enough to hold the data residing in the sideset and we
3193 // may need to allocate a temporary array... This can be checked
3194 // by comparing the size of the sideset with the 'side_count' of
3195 // the side block.
3196
3197 // Get size of data stored on the file...
3198 // FIX 64: FIX THIS -- STORING INT IN DOUBLE WON'T WORK
3199 if (field.get_name() == "side_ids" && fb->name() == "universal_sideset") {
3200 // The side ids are being stored as the distribution factor
3201 // field on the universal sideset. There should be no other
3202 // side sets that request this field... (Eventually,
3203 // create an id field to store this info.
3204
3205 if (number_distribution_factors == num_to_get) {
3206 std::vector<double> real_ids(num_to_get);
3207 set_param[0].distribution_factor_list = Data(real_ids);
3208 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
3209 if (ierr < 0) {
3210 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3211 }
3212 set_param[0].distribution_factor_list = nullptr;
3213
3214 if (field.get_type() == Ioss::Field::INTEGER) {
3215 // Need to convert 'double' to 'int' for Sierra use...
3216 int *ids = static_cast<int *>(data);
3217 for (int64_t i = 0; i < num_to_get; i++) {
3218 ids[i] = static_cast<int>(real_ids[i]);
3219 }
3220 }
3221 else {
3222 // Need to convert 'double' to 'int' for Sierra use...
3223 auto *ids = static_cast<int64_t *>(data);
3224 for (int64_t i = 0; i < num_to_get; i++) {
3225 ids[i] = static_cast<int64_t>(real_ids[i]);
3226 }
3227 }
3228 }
3229 }
3230
3231 else if (field.get_name() == "side_ids") {
3232 }
3233
3234 else if (field.get_name() == "ids") {
3235 // In exodus, the 'side set' is stored as a sideset. A
3236 // sideset has a list of elements and a corresponding local
3237 // element side (1-based) The side id is: side_id =
3238 // 10*element_id + local_side_number This assumes that all
3239 // sides in a sideset are boundary sides. Since we
3240 // only have a single array, we need to allocate an extra array
3241 // to store all of the data. Note also that the element_id is
3242 // the global id but only the local id is stored so we need to
3243 // map from local_to_global prior to generating the side id...
3244
3245 Ioss::Field el_side = fb->get_field("element_side");
3246 std::vector<char> element_side(2 * number_sides * int_byte_size_api());
3247 get_field_internal(fb, el_side, Data(element_side), element_side.size());
3248
3249 // At this point, have the 'element_side' data containing
3250 // the global element ids and the sides... Iterate
3251 // through to generate the ids...
3252 if (int_byte_size_api() == 4) {
3253 int64_t int_max = std::numeric_limits<int>::max();
3254 int *ids = static_cast<int *>(data);
3255 int *els = reinterpret_cast<int *>(Data(element_side));
3256 size_t idx = 0;
3257 for (int64_t iel = 0; iel < 2 * entity_count; iel += 2) {
3258 int64_t new_id = static_cast<int64_t>(10) * els[iel] + els[iel + 1];
3259 if (new_id > int_max) {
3260 IOSS_ERROR(fmt::format(
3261 "ERROR: Process {} accessing the sideset field 'ids'\n"
3262 "\t\thas exceeded the integer bounds for entity {}, local side id {}"
3263 ".\n\t\tTry using 64-bit mode to read the file '{}'.\n",
3264 Ioss::SerializeIO::getRank(), els[iel], els[iel + 1], decoded_filename()));
3265 }
3266
3267 ids[idx++] = static_cast<int>(new_id);
3268 }
3269 }
3270 else {
3271 auto *ids = static_cast<int64_t *>(data);
3272 auto *els = reinterpret_cast<int64_t *>(Data(element_side));
3273 size_t idx = 0;
3274 for (int64_t iel = 0; iel < 2 * entity_count; iel += 2) {
3275 int64_t new_id = 10 * els[iel] + els[iel + 1];
3276 ids[idx++] = new_id;
3277 }
3278 }
3279 }
3280 else if (field.get_name() == "element_side" || field.get_name() == "element_side_raw") {
3281 // In exodus, the 'side set' is stored as a sideset. A sideset
3282 // has a list of elements and a corresponding local element side
3283 // (1-based)
3284
3285 // Since we only have a single array, we need to allocate an extra
3286 // array to store all of the data. Note also that the element_id
3287 // is the global id but only the local id is stored so we need to
3288 // map from local_to_global prior to generating the side id...
3289
3290 // Get the element number map (1-based)...
3291 // Not used for "element_side_raw", but reduces code duplication...
3292 // Although combining code for raw and regular introduces an if in the
3293 // middle of a loop, we rely on the branch predictor to learn that
3294 // `do_map` does not change during the iteration...
3295 const Ioss::MapContainer &map = get_map(EX_ELEM_BLOCK).map();
3296
3297 bool do_map = field.get_name() == "element_side";
3298
3299 // Allocate space for local side number and element numbers
3300 // numbers.
3301
3302 // See if edges or faces...
3303 int64_t side_offset = Ioss::Utils::get_side_offset(fb);
3304
3305 std::vector<char> element(number_sides * int_byte_size_api());
3306 std::vector<char> sides(number_sides * int_byte_size_api());
3307
3308 ierr = ex_get_set(get_file_pointer(), EX_SIDE_SET, id, Data(element), Data(sides));
3309 if (ierr < 0) {
3310 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3311 }
3312
3313 if (number_sides == entity_count) {
3314 int64_t index = 0;
3315 if (int_byte_size_api() == 4) {
3316 auto *element_side = static_cast<int *>(data);
3317 auto *element32 = reinterpret_cast<int *>(Data(element));
3318 auto *sides32 = reinterpret_cast<int *>(Data(sides));
3319 for (int64_t iel = 0; iel < entity_count; iel++) {
3320 if (do_map) {
3321 element_side[index++] = map[element32[iel]];
3322 }
3323 else {
3324 element_side[index++] = element32[iel];
3325 }
3326 element_side[index++] = sides32[iel] - side_offset;
3327 }
3328 }
3329 else {
3330 auto *element_side = static_cast<int64_t *>(data);
3331 auto *element64 = reinterpret_cast<int64_t *>(Data(element));
3332 auto *sides64 = reinterpret_cast<int64_t *>(Data(sides));
3333 for (int64_t iel = 0; iel < entity_count; iel++) {
3334 if (do_map) {
3335 element_side[index++] = map[element64[iel]];
3336 }
3337 else {
3338 element_side[index++] = element64[iel];
3339 }
3340 element_side[index++] = sides64[iel] - side_offset;
3341 }
3342 }
3343 assert(index / 2 == entity_count);
3344 }
3345 else {
3346 Ioss::IntVector is_valid_side;
3347 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, int_byte_size_api(),
3348 Data(element), Data(sides), number_sides,
3349 get_region());
3350
3351 int64_t index = 0;
3352 if (int_byte_size_api() == 4) {
3353 auto *element_side = static_cast<int *>(data);
3354 auto *element32 = reinterpret_cast<int *>(Data(element));
3355 auto *sides32 = reinterpret_cast<int *>(Data(sides));
3356 for (int64_t iel = 0; iel < number_sides; iel++) {
3357 if (is_valid_side[iel] == 1) {
3358 // This side belongs in the side block
3359 if (do_map) {
3360 element_side[index++] = map[element32[iel]];
3361 }
3362 else {
3363 element_side[index++] = element32[iel];
3364 }
3365 element_side[index++] = sides32[iel] - side_offset;
3366 }
3367 }
3368 }
3369 else {
3370 auto *element_side = static_cast<int64_t *>(data);
3371 auto *element64 = reinterpret_cast<int64_t *>(Data(element));
3372 auto *sides64 = reinterpret_cast<int64_t *>(Data(sides));
3373 for (int64_t iel = 0; iel < number_sides; iel++) {
3374 if (is_valid_side[iel] == 1) {
3375 // This side belongs in the side block
3376 if (do_map) {
3377 element_side[index++] = map[element64[iel]];
3378 }
3379 else {
3380 element_side[index++] = element64[iel];
3381 }
3382 element_side[index++] = sides64[iel] - side_offset;
3383 }
3384 }
3385 }
3386 assert(index / 2 == entity_count);
3387 }
3388 }
3389 else if (field.get_name() == "connectivity") {
3390 // The side connectivity needs to be generated 'on-the-fly' from
3391 // the element number and local side of that element. A sideset
3392 // can span multiple element blocks, and contain multiple side
3393 // types; the side block contains side of similar topology.
3394 ierr = get_side_connectivity(fb, id, entity_count, data, true);
3395 if (ierr < 0) {
3396 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3397 }
3398 }
3399 else if (field.get_name() == "connectivity_raw") {
3400 // The side connectivity needs to be generated 'on-the-fly' from
3401 // the element number and local side of that element. A sideset
3402 // can span multiple element blocks, and contain multiple side
3403 // types; the side block contains side of similar topology.
3404 ierr = get_side_connectivity(fb, id, entity_count, data, false);
3405 if (ierr < 0) {
3406 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3407 }
3408 }
3409 else if (field.get_name() == "distribution_factors") {
3410 ierr = get_side_distributions(fb, id, entity_count, static_cast<double *>(data),
3411 data_size / sizeof(double));
3412 if (ierr < 0) {
3413 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3414 }
3415 }
3416 else {
3417 num_to_get = Ioss::Utils::field_warning(fb, field, "input");
3418 }
3419 }
3420 else if (role == Ioss::Field::TRANSIENT) {
3421 // Check if the specified field exists on this block.
3422 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
3423 // exist on the database as scalars with the appropriate
3424 // extensions.
3425
3426 if (number_sides == entity_count) {
3427 num_to_get = read_transient_field(m_variables[EX_SIDE_SET], field, fb, data);
3428 }
3429 else {
3430 // Need to read all values for the specified field and then
3431 // filter down to the elements actually in this side block.
3432
3433 // Determine which sides are member of this block
3434 Ioss::IntVector is_valid_side;
3435 {
3436 //----
3437 std::vector<char> element(number_sides * int_byte_size_api());
3438 std::vector<char> sides(number_sides * int_byte_size_api());
3439 ierr = ex_get_set(get_file_pointer(), EX_SIDE_SET, id, Data(element), Data(sides));
3440 if (ierr < 0) {
3441 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3442 }
3443 //----
3444 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, int_byte_size_api(),
3445 Data(element), Data(sides), number_sides,
3446 get_region());
3447 }
3448
3449 num_to_get = read_ss_transient_field(field, id, data, is_valid_side);
3450 }
3451 }
3452 }
3453 return num_to_get;
3454 }
3455
3456 int64_t DatabaseIO::write_attribute_field(const Ioss::Field &field,
3457 const Ioss::GroupingEntity *ge, void *data) const
3458 {
3459 std::string att_name = ge->name() + SEP() + field.get_name();
3460 int64_t num_entity = ge->entity_count();
3461 int64_t fld_offset = field.get_index();
3462
3463 ex_entity_type type = Ioex::map_exodus_type(ge->type());
3464 int64_t id = Ioex::get_id(ge, &ids_);
3465 int attribute_count = ge->get_property("attribute_count").get_int();
3466 assert(fld_offset > 0);
3467 assert(fld_offset - 1 + field.get_component_count(Ioss::Field::InOut::OUTPUT) <=
3468 attribute_count);
3469
3470 Ioss::Field::BasicType ioss_type = field.get_type();
3471 assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER ||
3472 ioss_type == Ioss::Field::INT64);
3473
3474 if (ioss_type == Ioss::Field::INT64) {
3475 Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)data, num_entity);
3476 }
3477
3478 if (fld_offset == 1 &&
3479 field.get_component_count(Ioss::Field::InOut::OUTPUT) == attribute_count) {
3480 // Write all attributes in one big chunk...
3481 std::vector<double> temp;
3482 double *rdata = nullptr;
3483 if (ioss_type == Ioss::Field::INTEGER) {
3484 int *idata = static_cast<int *>(data);
3485 extract_data(temp, idata, attribute_count * num_entity, 1, 0);
3486 rdata = Data(temp);
3487 }
3488 else if (ioss_type == Ioss::Field::INT64) {
3489 auto *idata = static_cast<int64_t *>(data);
3490 extract_data(temp, idata, attribute_count * num_entity, 1, 0);
3491 rdata = Data(temp);
3492 }
3493 else {
3494 rdata = static_cast<double *>(data);
3495 }
3496
3497 int ierr = ex_put_attr(get_file_pointer(), type, id, rdata);
3498 if (ierr < 0) {
3499 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3500 }
3501 }
3502 else {
3503 // Write a subset of the attributes. If scalar, write one;
3504 // if higher-order (vector3d, ..) write each component.
3505 if (field.get_component_count(Ioss::Field::InOut::OUTPUT) == 1) {
3506 std::vector<double> temp;
3507 double *rdata = nullptr;
3508 if (ioss_type == Ioss::Field::INTEGER) {
3509 int *idata = static_cast<int *>(data);
3510 extract_data(temp, idata, num_entity, 1, 0);
3511 rdata = Data(temp);
3512 }
3513 else if (ioss_type == Ioss::Field::INT64) {
3514 auto *idata = static_cast<int64_t *>(data);
3515 extract_data(temp, idata, num_entity, 1, 0);
3516 rdata = Data(temp);
3517 }
3518 else {
3519 rdata = static_cast<double *>(data);
3520 }
3521
3522 int ierr = ex_put_one_attr(get_file_pointer(), type, id, fld_offset, rdata);
3523 if (ierr < 0) {
3524 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3525 }
3526 }
3527 else {
3528 // Multi-component... Need a local memory space to push
3529 // data into and then write that out to the file...
3530 std::vector<double> local_data(num_entity);
3531 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
3532 for (int i = 0; i < comp_count; i++) {
3533 size_t offset = i;
3534 if (ioss_type == Ioss::Field::REAL) {
3535 auto *rdata = static_cast<double *>(data);
3536 extract_data(local_data, rdata, num_entity, comp_count, offset);
3537 }
3538 else if (ioss_type == Ioss::Field::INTEGER) {
3539 int *idata = static_cast<int *>(data);
3540 extract_data(local_data, idata, num_entity, comp_count, offset);
3541 }
3542 else if (ioss_type == Ioss::Field::INT64) {
3543 auto *idata = static_cast<int64_t *>(data);
3544 extract_data(local_data, idata, num_entity, comp_count, offset);
3545 }
3546
3547 int ierr =
3548 ex_put_one_attr(get_file_pointer(), type, id, fld_offset + i, Data(local_data));
3549 if (ierr < 0) {
3550 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3551 }
3552 }
3553 }
3554 }
3555 return num_entity;
3556 }
3557
3558 int64_t DatabaseIO::read_attribute_field(const Ioss::Field &field, const Ioss::GroupingEntity *ge,
3559 void *data) const
3560 {
3561 // TODO: Handle INTEGER fields...
3562
3563 int64_t num_entity = ge->entity_count();
3564 if (num_entity == 0) {
3565 return 0;
3566 }
3567
3568 Ioss::Field::BasicType ioss_type = field.get_type();
3569 if (ioss_type == Ioss::Field::INTEGER || ioss_type == Ioss::Field::INT64) {
3570 IOSS_ERROR("INTERNAL ERROR: Integer attribute fields are not yet handled for read. "
3571 "Please report.\n");
3572 }
3573
3574 int attribute_count = ge->get_property("attribute_count").get_int();
3575 ex_entity_type type = Ioex::map_exodus_type(ge->type());
3576 int64_t id = Ioex::get_id(ge, &ids_);
3577
3578 std::string att_name = ge->name() + SEP() + field.get_name();
3579 int64_t offset = field.get_index();
3580 assert(offset - 1 + field.get_component_count(Ioss::Field::InOut::INPUT) <= attribute_count);
3581 if (offset == 1 && field.get_component_count(Ioss::Field::InOut::INPUT) == attribute_count) {
3582 // Read all attributes in one big chunk...
3583 int ierr = ex_get_attr(get_file_pointer(), type, id, static_cast<double *>(data));
3584 if (ierr < 0) {
3585 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3586 }
3587 }
3588 else {
3589 // Read a subset of the attributes. If scalar, read one;
3590 // if higher-order (vector3d, ..) read each component and
3591 // put into correct location...
3592 if (field.get_component_count(Ioss::Field::InOut::INPUT) == 1) {
3593 int ierr =
3594 ex_get_one_attr(get_file_pointer(), type, id, offset, static_cast<double *>(data));
3595 if (ierr < 0) {
3596 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3597 }
3598 }
3599 else {
3600 // Multi-component...
3601 // Need a local memory space to read data into and
3602 // then push that into the user-supplied data block...
3603 std::vector<double> local_data(num_entity);
3604 int comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
3605 auto *rdata = static_cast<double *>(data);
3606 for (int i = 0; i < comp_count; i++) {
3607 int ierr = ex_get_one_attr(get_file_pointer(), type, id, offset + i, Data(local_data));
3608 if (ierr < 0) {
3609 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3610 }
3611
3612 size_t k = i;
3613 for (int64_t j = 0; j < num_entity; j++) {
3614 rdata[k] = local_data[j];
3615 k += comp_count;
3616 }
3617 }
3618 }
3619 }
3620 return num_entity;
3621 }
3622
3623 int64_t DatabaseIO::read_transient_field(const Ioex::VariableNameMap &variables,
3624 const Ioss::Field &field, const Ioss::GroupingEntity *ge,
3625 void *data) const
3626 {
3627 // Read into a double variable since that is all Exodus can store...
3628 size_t num_entity = ge->entity_count();
3629 std::vector<double> temp(num_entity);
3630
3631 size_t step = get_current_state();
3632
3633 // get number of components, cycle through each component
3634 // and add suffix to base 'field_name'. Look up index
3635 // of this name in 'nodeVariables' map
3636 size_t comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
3637
3638 ex_entity_type type = Ioex::map_exodus_type(ge->type());
3639 if (comp_count == 1 && field.get_type() == Ioss::Field::REAL) {
3640 std::string var_name = get_component_name(field, Ioss::Field::InOut::INPUT, 1);
3641 if (lowerCaseVariableNames) {
3642 Ioss::Utils::fixup_name(var_name);
3643 }
3644
3645 // Read the variable...
3646 int64_t id = Ioex::get_id(ge, &ids_);
3647 int ierr = 0;
3648
3649 auto var_iter = variables.find(var_name);
3650 if (var_iter == variables.end()) {
3651 IOSS_ERROR(fmt::format("ERROR: Could not find field '{}'\n", var_name));
3652 }
3653 size_t var_index = var_iter->second;
3654 assert(var_index > 0);
3655 ierr = ex_get_var(get_file_pointer(), step, type, var_index, id, num_entity, data);
3656 if (ierr < 0) {
3657 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3658 }
3659 }
3660 else {
3661 for (size_t i = 0; i < comp_count; i++) {
3662 std::string var_name = get_component_name(field, Ioss::Field::InOut::INPUT, i + 1);
3663 if (lowerCaseVariableNames) {
3664 Ioss::Utils::fixup_name(var_name);
3665 }
3666
3667 // Read the variable...
3668 int64_t id = Ioex::get_id(ge, &ids_);
3669 int ierr = 0;
3670 auto var_iter = variables.find(var_name);
3671 if (var_iter == variables.end()) {
3672 IOSS_ERROR(fmt::format("ERROR: Could not find field '{}'\n", var_name));
3673 }
3674 size_t var_index = var_iter->second;
3675 assert(var_index > 0);
3676 ierr = ex_get_var(get_file_pointer(), step, type, var_index, id, num_entity, Data(temp));
3677 if (ierr < 0) {
3678 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3679 }
3680
3681 // Transfer to 'data' array.
3682 size_t k = 0;
3683 if (field.get_type() == Ioss::Field::INTEGER) {
3684 int *ivar = static_cast<int *>(data);
3685 for (size_t j = i; j < num_entity * comp_count; j += comp_count) {
3686 ivar[j] = static_cast<int>(temp[k++]);
3687 }
3688 }
3689 else if (field.get_type() == Ioss::Field::INT64) { // FIX 64 UNSAFE
3690 auto *ivar = static_cast<int64_t *>(data);
3691 for (size_t j = i; j < num_entity * comp_count; j += comp_count) {
3692 ivar[j] = static_cast<int64_t>(temp[k++]);
3693 }
3694 }
3695 else if (field.get_type() == Ioss::Field::REAL) {
3696 auto *rvar = static_cast<double *>(data);
3697 for (size_t j = i; j < num_entity * comp_count; j += comp_count) {
3698 rvar[j] = temp[k++];
3699 }
3700 }
3701 else {
3702 IOSS_ERROR(
3703 fmt::format("IOSS_ERROR: Field storage type must be either integer or double.\n"
3704 " Field '{}' is invalid.\n",
3705 field.get_name()));
3706 }
3707 assert(k == num_entity);
3708 }
3709 }
3710 return num_entity;
3711 }
3712
3713 int64_t DatabaseIO::read_ss_transient_field(const Ioss::Field &field, int64_t id, void *variables,
3714 Ioss::IntVector &is_valid_side) const
3715 {
3716 size_t num_valid_sides = 0;
3717 size_t my_side_count = is_valid_side.size();
3718 std::vector<double> temp(my_side_count);
3719
3720 size_t step = get_current_state();
3721
3722 // get number of components, cycle through each component
3723 // and add suffix to base 'field_name'. Look up index
3724 // of this name in 'nodeVariables' map
3725 size_t comp_count = field.get_component_count(Ioss::Field::InOut::INPUT);
3726
3727 for (size_t i = 0; i < comp_count; i++) {
3728 std::string var_name = get_component_name(field, Ioss::Field::InOut::INPUT, i + 1);
3729 if (lowerCaseVariableNames) {
3730 Ioss::Utils::fixup_name(var_name);
3731 }
3732
3733 // Read the variable...
3734 int ierr = 0;
3735 auto var_iter = m_variables[EX_SIDE_SET].find(var_name);
3736 if (var_iter == m_variables[EX_SIDE_SET].end()) {
3737 IOSS_ERROR(fmt::format("ERROR: Could not find Sideset field '{}'\n", var_name));
3738 }
3739 size_t var_index = var_iter->second;
3740 assert(var_index > 0);
3741 ierr = ex_get_var(get_file_pointer(), step, EX_SIDE_SET, var_index, id, my_side_count,
3742 Data(temp));
3743 if (ierr < 0) {
3744 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3745 }
3746
3747 // Transfer to 'variables' array.
3748 size_t j = i;
3749 if (field.get_type() == Ioss::Field::INTEGER) {
3750 int *ivar = static_cast<int *>(variables);
3751 for (size_t k = 0; k < my_side_count; k++) {
3752 if (is_valid_side[k] == 1) {
3753 ivar[j] = static_cast<int>(temp[k]);
3754 j += comp_count;
3755 }
3756 }
3757 }
3758 else if (field.get_type() == Ioss::Field::INT64) { // FIX 64 UNSAFE
3759 auto *ivar = static_cast<int64_t *>(variables);
3760 for (size_t k = 0; k < my_side_count; k++) {
3761 if (is_valid_side[k] == 1) {
3762 ivar[j] = static_cast<int64_t>(temp[k]);
3763 j += comp_count;
3764 }
3765 }
3766 }
3767 else if (field.get_type() == Ioss::Field::REAL) {
3768 auto *rvar = static_cast<double *>(variables);
3769 for (size_t k = 0; k < my_side_count; k++) {
3770 if (is_valid_side[k] == 1) {
3771 rvar[j] = temp[k];
3772 j += comp_count;
3773 }
3774 }
3775 }
3776 else {
3777 IOSS_ERROR(fmt::format("IOSS_ERROR: Field storage type must be either integer or double.\n"
3778 " Field '{}' is invalid.\n",
3779 field.get_name()));
3780 }
3781 if (i + 1 == comp_count) {
3782 num_valid_sides = j / comp_count;
3783 }
3784 }
3785 return num_valid_sides;
3786 }
3787
3788 template <typename INT>
3789 int64_t DatabaseIO::get_side_connectivity_internal(const Ioss::SideBlock *fb, int64_t id,
3790 int64_t /*unused*/, INT *fconnect,
3791 bool map_ids) const
3792 {
3793 // Get size of data stored on the file...
3794 ex_set set_param[1];
3795 set_param[0].id = id;
3796 set_param[0].type = EX_SIDE_SET;
3797 set_param[0].entry_list = nullptr;
3798 set_param[0].extra_list = nullptr;
3799 set_param[0].distribution_factor_list = nullptr;
3800 int ierr = ex_get_sets(get_file_pointer(), 1, set_param);
3801 if (ierr < 0) {
3802 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3803 }
3804
3805 int64_t number_sides = set_param[0].num_entry;
3806
3807 // Allocate space for element and local side number
3808 assert(number_sides > 0);
3809 //----
3810 std::vector<INT> element(number_sides);
3811 std::vector<INT> side(number_sides);
3812
3813 set_param[0].entry_list = Data(element);
3814 set_param[0].extra_list = Data(side);
3815 ierr = ex_get_sets(get_file_pointer(), 1, set_param);
3816 if (ierr < 0) {
3817 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3818 }
3819 //----
3820
3821 Ioss::IntVector is_valid_side;
3822 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, int_byte_size_api(),
3823 (void *)Data(element), (void *)Data(side),
3824 number_sides, get_region());
3825
3826 std::vector<INT> elconnect;
3827 int64_t elconsize = 0; // Size of currently allocated connectivity block
3828 Ioss::ElementBlock *conn_block = nullptr; // Block that we currently
3829 // have connectivity for
3830
3831 Ioss::ElementBlock *block = nullptr;
3832 Ioss::IntVector side_elem_map; // Maps the side into the elements
3833
3834 // connectivity array
3835 int64_t current_side = -1;
3836 int nelnode = 0;
3837 int nfnodes = 0;
3838 int ieb = 0;
3839 size_t offset = 0;
3840 for (int64_t iel = 0; iel < number_sides; iel++) {
3841 if (is_valid_side[iel] == 1) {
3842
3843 int64_t elem_id = element[iel];
3844
3845 // ensure we have correct connectivity
3846 block = get_region()->get_element_block(elem_id);
3847 assert(block != nullptr);
3848 if (conn_block != block) {
3849 int64_t nelem = block->entity_count();
3850 nelnode = block->topology()->number_nodes();
3851 // Used to map element number into position in connectivity array.
3852 // E.g., element 97 is the (97-offset)th element in this block and
3853 // is stored in array index (97-offset-1).
3854 offset = block->get_offset() + 1;
3855 if (elconsize < nelem * nelnode) {
3856 elconsize = nelem * nelnode;
3857 elconnect.resize(elconsize);
3858 }
3859 if (map_ids) {
3860 get_field_internal(block, block->get_field("connectivity"), Data(elconnect),
3861 nelem * nelnode * int_byte_size_api());
3862 }
3863 else {
3864 get_field_internal(block, block->get_field("connectivity_raw"), Data(elconnect),
3865 nelem * nelnode * int_byte_size_api());
3866 }
3867 conn_block = block;
3868 current_side = -1;
3869 }
3870
3871 // NOTE: Element connectivity is returned with nodes in global id space if "map_ids" false,
3872 // otherwise it is in local space.
3873 int64_t side_id = side[iel];
3874
3875 if (current_side != side_id) {
3876 side_elem_map = block->topology()->boundary_connectivity(side_id);
3877 current_side = side_id;
3878 nfnodes = block->topology()->boundary_type(side_id)->number_nodes();
3879 }
3880 for (int inode = 0; inode < nfnodes; inode++) {
3881 size_t index = (elem_id - offset) * nelnode + side_elem_map[inode];
3882 fconnect[ieb++] = elconnect[index];
3883 }
3884 }
3885 }
3886 return ierr;
3887 }
3888
3889 int64_t DatabaseIO::get_side_connectivity(const Ioss::SideBlock *fb, int64_t id,
3890 int64_t my_side_count, void *fconnect,
3891 bool map_ids) const
3892 {
3893 if (int_byte_size_api() == 4) {
3894 return get_side_connectivity_internal(fb, id, my_side_count,
3895 reinterpret_cast<int *>(fconnect), map_ids);
3896 }
3897 return get_side_connectivity_internal(fb, id, my_side_count,
3898 reinterpret_cast<int64_t *>(fconnect), map_ids);
3899 }
3900
3901 // Get distribution factors for the specified side block
3902 int64_t DatabaseIO::get_side_distributions(const Ioss::SideBlock *fb, int64_t id,
3903 int64_t my_side_count, double *dist_fact,
3904 size_t /* data_size */) const
3905 {
3906 // Allocate space for elements and local side numbers
3907 // Get size of data stored on the file...
3908 ex_set set_param[1];
3909 set_param[0].id = id;
3910 set_param[0].type = EX_SIDE_SET;
3911 set_param[0].entry_list = nullptr;
3912 set_param[0].extra_list = nullptr;
3913 set_param[0].distribution_factor_list = nullptr;
3914
3915 int error = ex_get_sets(get_file_pointer(), 1, set_param);
3916 if (error < 0) {
3917 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3918 }
3919 int64_t number_sides = set_param[0].num_entry;
3920 int64_t number_distribution_factors = set_param[0].num_distribution_factor;
3921
3922 const Ioss::ElementTopology *ftopo = fb->topology();
3923 int nfnodes = ftopo->number_nodes();
3924
3925 if (number_distribution_factors == 0) {
3926 // Fill in the array with '1.0'...
3927 for (int64_t i = 0; i < nfnodes * my_side_count; i++) {
3928 dist_fact[i] = 1.0;
3929 }
3930 return 0;
3931 }
3932
3933 // Take care of the easy situation -- If 'side_count' ==
3934 // 'number_sides' then the sideset is stored in a single sideblock
3935 // and all distribution factors on the database are transferred
3936 // 1-to-1 into 'dist_fact' array.
3937 if (my_side_count == number_sides) {
3938 // Verify that number_distribution_factors is sane...
3939 if (number_sides * nfnodes != number_distribution_factors &&
3940 number_sides != number_distribution_factors) {
3941 IOSS_ERROR(fmt::format(
3942 "ERROR: SideBlock '{}' in file '{}'\n"
3943 "\thas incorrect distribution factor count.\n"
3944 "\tThere are {} '{}' sides with "
3945 "{} nodes per side, but there are {} distribution factors which is not correct.\n"
3946 "\tThere should be either {} or {} distribution factors.\n",
3947 fb->name(), get_filename(), number_sides, ftopo->name(), nfnodes,
3948 number_distribution_factors, number_sides, number_sides * nfnodes));
3949 }
3950 return ex_get_set_dist_fact(get_file_pointer(), EX_SIDE_SET, id, dist_fact);
3951 }
3952
3953 // Allocate space for distribution factors.
3954 std::vector<double> dist(number_distribution_factors);
3955 int ierr = ex_get_set_dist_fact(get_file_pointer(), EX_SIDE_SET, id, Data(dist));
3956 if (ierr < 0) {
3957 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3958 }
3959
3960 // Another easy situation (and common for exodus) is if the input
3961 // distribution factors are all the same value (typically 1). In
3962 // that case, we only have to fill in the output array with that
3963 // value.
3964 {
3965 double value = dist[0];
3966 bool constant = true;
3967 for (int64_t i = 1; i < number_distribution_factors; i++) {
3968 if (dist[i] != value) {
3969 constant = false;
3970 break;
3971 }
3972 }
3973 if (constant) {
3974 if (value == 0.0) {
3975 value = 1.0; // Take care of some buggy mesh generators
3976 }
3977 for (int64_t j = 0; j < my_side_count * nfnodes; j++) {
3978 dist_fact[j] = value;
3979 }
3980 return 0;
3981 }
3982 }
3983
3984 // If we get to here, the underlying sideset contains multiple side
3985 // topologies and the distribution factors are non-constant. Need to
3986 // allocate space to store all distribution factors and then pull
3987 // out those that are applied to sides with the correct topology.
3988
3989 // Allocate space for element and local side number (this is bulk
3990 // data...)
3991 //----
3992 std::vector<char> element(number_sides * int_byte_size_api());
3993 std::vector<char> side(number_sides * int_byte_size_api());
3994
3995 ierr = ex_get_set(get_file_pointer(), EX_SIDE_SET, id, Data(element), Data(side));
3996 if (ierr < 0) {
3997 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
3998 }
3999 //----
4000
4001 Ioss::IntVector is_valid_side;
4002 Ioss::Utils::calculate_sideblock_membership(is_valid_side, fb, int_byte_size_api(),
4003 Data(element), Data(side), number_sides,
4004 get_region());
4005
4006 int64_t ieb = 0; // counter for distribution factors in this sideblock
4007 int64_t idb = 0; // counter for distribution factors read from database
4008 Ioss::ElementBlock *block = nullptr;
4009
4010 int *element32 = nullptr;
4011 int64_t *element64 = nullptr;
4012 int *side32 = nullptr;
4013 int64_t *side64 = nullptr;
4014
4015 if (int_byte_size_api() == 4) {
4016 element32 = reinterpret_cast<int *>(Data(element));
4017 side32 = reinterpret_cast<int *>(Data(side));
4018 }
4019 else {
4020 element64 = reinterpret_cast<int64_t *>(Data(element));
4021 side64 = reinterpret_cast<int64_t *>(Data(side));
4022 }
4023
4024 for (int64_t iel = 0; iel < number_sides; iel++) {
4025 int64_t elem_id = 0;
4026 int64_t side_id = 0;
4027 if (int_byte_size_api() == 4) {
4028 elem_id = element32[iel];
4029 side_id = side32[iel];
4030 }
4031 else {
4032 elem_id = element64[iel];
4033 side_id = side64[iel];
4034 }
4035
4036 if (block == nullptr || !block->contains(elem_id)) {
4037 block = get_region()->get_element_block(elem_id);
4038 }
4039
4040 if (block == nullptr) {
4041 IOSS_ERROR(fmt::format(
4042 "INTERNAL ERROR: Could not find element block containing element with id {}. "
4043 "Something is wrong in the Ioex::DatabaseIO class. Please report.\n",
4044 elem_id));
4045 }
4046
4047 const Ioss::ElementTopology *topo = block->topology()->boundary_type(side_id);
4048
4049 if (topo == nullptr) {
4050 IOSS_ERROR("INTERNAL ERROR: Could not find topology of element block boundary. "
4051 "Something is wrong in the Ioex::DatabaseIO class. Please report.\n");
4052 }
4053
4054 int nside_nodes = topo->number_nodes();
4055
4056 if (is_valid_side[iel] == 1) {
4057 // This side belongs in the sideblock
4058 for (int64_t i = 0; i < nside_nodes; i++) {
4059 dist_fact[ieb++] = dist[idb++];
4060 }
4061 }
4062 else {
4063 // Skip over unused 'dist' factors
4064 idb += topo->number_nodes();
4065 }
4066 }
4067
4068 assert(ieb == my_side_count * nfnodes);
4069 // If the following assert fails, it may be due to bug in Patran
4070 // which writes too many distribution factors to the database in a
4071 // mixed element case. Note that this is checked earlier also with a
4072 // better error message.
4073 assert(idb == number_distribution_factors);
4074 return 0;
4075 }
4076
4077 int64_t DatabaseIO::put_field_internal(const Ioss::Region *reg, const Ioss::Field &field,
4078 void *data, size_t data_size) const
4079 {
4080 return Ioex::BaseDatabaseIO::put_field_internal(reg, field, data, data_size);
4081 }
4082
4083 int64_t DatabaseIO::put_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field,
4084 void *data, size_t data_size) const
4085 {
4086 {
4087 Ioss::SerializeIO serializeio_(this);
4088
4089 size_t num_to_get = field.verify(data_size);
4090 if (num_to_get > 0) {
4091
4092 Ioss::Field::RoleType role = field.get_role();
4093
4094 if (role == Ioss::Field::MESH) {
4095 if (field.get_name() == "mesh_model_coordinates_x") {
4096 auto *rdata = static_cast<double *>(data);
4097 int ierr = ex_put_coord(get_file_pointer(), rdata, nullptr, nullptr);
4098 if (ierr < 0) {
4099 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4100 }
4101 }
4102
4103 else if (field.get_name() == "mesh_model_coordinates_y") {
4104 auto *rdata = static_cast<double *>(data);
4105 int ierr = ex_put_coord(get_file_pointer(), nullptr, rdata, nullptr);
4106 if (ierr < 0) {
4107 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4108 }
4109 }
4110
4111 else if (field.get_name() == "mesh_model_coordinates_z") {
4112 auto *rdata = static_cast<double *>(data);
4113 int ierr = ex_put_coord(get_file_pointer(), nullptr, nullptr, rdata);
4114 if (ierr < 0) {
4115 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4116 }
4117 }
4118
4119 else if (field.get_name() == "mesh_model_coordinates") {
4120 // Data required by upper classes store x0, y0, z0, ... xn, yn, zn
4121 // Data stored in exodus file is x0, ..., xn, y0, ..., yn, z0, ..., zn
4122 // so we have to allocate some scratch memory to read in the data
4123 // and then map into supplied 'data'
4124 std::vector<double> x;
4125 x.reserve(num_to_get);
4126 std::vector<double> y;
4127 if (spatialDimension > 1) {
4128 y.reserve(num_to_get);
4129 }
4130 std::vector<double> z;
4131 if (spatialDimension == 3) {
4132 z.reserve(num_to_get);
4133 }
4134
4135 // Cast 'data' to correct size -- double
4136 auto *rdata = static_cast<double *>(data);
4137
4138 size_t index = 0;
4139 for (size_t i = 0; i < num_to_get; i++) {
4140 x.push_back(rdata[index++]);
4141 if (spatialDimension > 1) {
4142 y.push_back(rdata[index++]);
4143 }
4144 if (spatialDimension == 3) {
4145 z.push_back(rdata[index++]);
4146 }
4147 }
4148 int ierr = ex_put_coord(get_file_pointer(), Data(x), Data(y), Data(z));
4149 if (ierr < 0) {
4150 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4151 }
4152 }
4153 else if (field.get_name() == "ids") {
4154 // The ids coming in are the global ids; their position is the
4155 // local id -1 (That is, data[0] contains the global id of local
4156 // node 1)
4157 handle_node_ids(data, num_to_get);
4158 }
4159 else if (field.get_name() == "connectivity") {
4160 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4161 }
4162 else if (field.get_name() == "connectivity_raw") {
4163 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4164 }
4165 else if (field.get_name() == "node_connectivity_status") {
4166 // Do nothing, input only field.
4167 }
4168 else if (field.get_name() == "implicit_ids") {
4169 // Do nothing, input only field.
4170 }
4171 else {
4172 return Ioss::Utils::field_warning(nb, field, "mesh output");
4173 }
4174 }
4175 else if (role == Ioss::Field::TRANSIENT) {
4176 // Check if the specified field exists on this node block.
4177 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4178 // exist on the database as scalars with the appropriate
4179 // extensions.
4180
4181 // Transfer each component of the variable into 'data' and then
4182 // output. Need temporary storage area of size 'number of
4183 // nodes in this block.
4184 write_nodal_transient_field(field, nb, num_to_get, data);
4185 }
4186 else if (role == Ioss::Field::REDUCTION) {
4187 store_reduction_field(field, nb, data);
4188 }
4189 else if (role == Ioss::Field::ATTRIBUTE) {
4190 num_to_get = write_attribute_field(field, nb, data);
4191 }
4192 }
4193 return num_to_get;
4194 }
4195 }
4196
4197 int64_t DatabaseIO::put_field_internal(const Ioss::Blob *blob, const Ioss::Field &field,
4198 void *data, size_t data_size) const
4199 {
4200 {
4201 Ioss::SerializeIO serializeio_(this);
4202
4203 size_t num_to_get = field.verify(data_size);
4204 if (num_to_get > 0) {
4205
4206 Ioss::Field::RoleType role = field.get_role();
4207
4208 if (role == Ioss::Field::MESH) {
4209 if (field.get_name() == "ids") {
4210 // The ids coming in are the global ids; their position is the
4211 // local id -1 (That is, data[0] contains the global id of local
4212 // node 1)
4213 // handle_node_ids(data, num_to_get);
4214 }
4215 else if (field.get_name() == "connectivity") {
4216 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4217 }
4218 else if (field.get_name() == "connectivity_raw") {
4219 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4220 }
4221 else if (field.get_name() == "node_connectivity_status") {
4222 // Do nothing, input only field.
4223 }
4224 else if (field.get_name() == "implicit_ids") {
4225 // Do nothing, input only field.
4226 }
4227 else {
4228 return Ioss::Utils::field_warning(blob, field, "mesh output");
4229 }
4230 }
4231 else if (role == Ioss::Field::TRANSIENT) {
4232 // Check if the specified field exists on this node block.
4233 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4234 // exist on the database as scalars with the appropriate
4235 // extensions.
4236
4237 // Transfer each component of the variable into 'data' and then
4238 // output. Need temporary storage area of size 'number of
4239 // nodes in this block.
4240 write_entity_transient_field(field, blob, num_to_get, data);
4241 }
4242 else if (role == Ioss::Field::REDUCTION) {
4243 store_reduction_field(field, blob, data);
4244 }
4245 else if (role == Ioss::Field::ATTRIBUTE) {
4246 num_to_get = write_attribute_field(field, blob, data);
4247 }
4248 }
4249 return num_to_get;
4250 }
4251 }
4252
4253 int64_t DatabaseIO::put_field_internal(const Ioss::Assembly *assembly, const Ioss::Field &field,
4254 void *data, size_t data_size) const
4255 {
4256 {
4257 Ioss::SerializeIO serializeio_(this);
4258
4259 size_t num_to_get = field.verify(data_size);
4260 if (num_to_get > 0) {
4261
4262 Ioss::Field::RoleType role = field.get_role();
4263
4264 if (role == Ioss::Field::MESH) {
4265 if (field.get_name() == "ids") {
4266 // The ids coming in are the global ids; their position is the
4267 // local id -1 (That is, data[0] contains the global id of local
4268 // node 1)
4269 // handle_node_ids(data, num_to_get);
4270 }
4271 else if (field.get_name() == "connectivity") {
4272 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4273 }
4274 else if (field.get_name() == "connectivity_raw") {
4275 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
4276 }
4277 else if (field.get_name() == "node_connectivity_status") {
4278 // Do nothing, input only field.
4279 }
4280 else if (field.get_name() == "implicit_ids") {
4281 // Do nothing, input only field.
4282 }
4283 else {
4284 return Ioss::Utils::field_warning(assembly, field, "mesh output");
4285 }
4286 }
4287 else if (role == Ioss::Field::TRANSIENT) {
4288 // Check if the specified field exists on this node block.
4289 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4290 // exist on the database as scalars with the appropriate
4291 // extensions.
4292
4293 // Transfer each component of the variable into 'data' and then
4294 // output. Need temporary storage area of size 'number of
4295 // nodes in this block.
4296 write_entity_transient_field(field, assembly, num_to_get, data);
4297 }
4298 else if (role == Ioss::Field::REDUCTION) {
4299 store_reduction_field(field, assembly, data);
4300 }
4301 else if (role == Ioss::Field::ATTRIBUTE) {
4302 num_to_get = write_attribute_field(field, assembly, data);
4303 }
4304 }
4305 return num_to_get;
4306 }
4307 }
4308
4309 int64_t DatabaseIO::put_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field,
4310 void *data, size_t data_size) const
4311 {
4312 {
4313 Ioss::SerializeIO serializeio_(this);
4314
4315 size_t num_to_get = field.verify(data_size);
4316
4317 if (num_to_get > 0) {
4318 int ierr = 0;
4319
4320 // Get the element block id and element count
4321 int64_t id = Ioex::get_id(eb, &ids_);
4322 size_t my_element_count = eb->entity_count();
4323 Ioss::Field::RoleType role = field.get_role();
4324
4325 if (role == Ioss::Field::MESH) {
4326 // Handle the MESH fields required for an Exodus file model.
4327 // (The 'genesis' portion)
4328 if (field.get_name() == "connectivity") {
4329 if (my_element_count > 0) {
4330 // Map element connectivity from global node id to local node id.
4331 int element_nodes = eb->topology()->number_nodes();
4332 nodeMap.reverse_map_data(data, field, num_to_get * element_nodes);
4333 ierr = ex_put_conn(get_file_pointer(), EX_ELEM_BLOCK, id, data, nullptr, nullptr);
4334 if (ierr < 0) {
4335 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4336 }
4337 }
4338 }
4339 else if (field.get_name() == "connectivity_edge") {
4340 if (my_element_count > 0) {
4341 // Map element connectivity from global edge id to local edge id.
4342 int element_edges = field.transformed_storage()->component_count();
4343 edgeMap.reverse_map_data(data, field, num_to_get * element_edges);
4344 ierr = ex_put_conn(get_file_pointer(), EX_ELEM_BLOCK, id, nullptr, data, nullptr);
4345 if (ierr < 0) {
4346 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4347 }
4348 }
4349 }
4350 else if (field.get_name() == "connectivity_face") {
4351 if (my_element_count > 0) {
4352 // Map element connectivity from global face id to local face id.
4353 int element_faces = field.transformed_storage()->component_count();
4354 faceMap.reverse_map_data(data, field, num_to_get * element_faces);
4355 ierr = ex_put_conn(get_file_pointer(), EX_ELEM_BLOCK, id, nullptr, nullptr, data);
4356 if (ierr < 0) {
4357 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4358 }
4359 }
4360 }
4361 else if (field.get_name() == "connectivity_raw") {
4362 if (my_element_count > 0) {
4363 // Element connectivity is already in local node id.
4364 ierr = ex_put_conn(get_file_pointer(), EX_ELEM_BLOCK, id, data, nullptr, nullptr);
4365 if (ierr < 0) {
4366 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4367 }
4368 }
4369 }
4370 else if (field.get_name() == "ids") {
4371 handle_element_ids(eb, data, num_to_get);
4372 }
4373 else if (field.get_name() == "implicit_ids") {
4374 // Do nothing, input only field.
4375 }
4376 }
4377 else if (role == Ioss::Field::MAP) {
4378 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
4379 for (int comp = 0; comp < comp_count; comp++) {
4380 std::vector<char> component(my_element_count * int_byte_size_api());
4381
4382 if (int_byte_size_api() == 4) {
4383 auto *data32 = reinterpret_cast<int *>(data);
4384 auto *comp32 = reinterpret_cast<int *>(Data(component));
4385
4386 int index = comp;
4387 for (size_t i = 0; i < my_element_count; i++) {
4388 comp32[i] = data32[index];
4389 index += comp_count;
4390 }
4391 }
4392 else {
4393 auto *data64 = reinterpret_cast<int64_t *>(data);
4394 auto *comp64 = reinterpret_cast<int64_t *>(Data(component));
4395
4396 int index = comp;
4397 for (size_t i = 0; i < my_element_count; i++) {
4398 comp64[i] = data64[index];
4399 index += comp_count;
4400 }
4401 }
4402 size_t eb_offset = eb->get_offset();
4403 int index = -1 * (static_cast<int>(field.get_index()) + comp);
4404 ierr = ex_put_partial_num_map(get_file_pointer(), EX_ELEM_MAP, index, eb_offset + 1,
4405 my_element_count, Data(component));
4406 }
4407 }
4408 else if (role == Ioss::Field::ATTRIBUTE) {
4409 num_to_get = write_attribute_field(field, eb, data);
4410 }
4411 else if (role == Ioss::Field::TRANSIENT) {
4412 // Check if the specified field exists on this element block.
4413 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4414 // exist on the database as scalars with the appropriate
4415 // extensions.
4416
4417 // Transfer each component of the variable into 'data' and then
4418 // output. Need temporary storage area of size 'number of
4419 // elements in this block.
4420 write_entity_transient_field(field, eb, my_element_count, data);
4421 }
4422 else if (role == Ioss::Field::REDUCTION) {
4423 store_reduction_field(field, eb, data);
4424 }
4425 }
4426 return num_to_get;
4427 }
4428 }
4429
4430 int64_t DatabaseIO::put_field_internal(const Ioss::FaceBlock *eb, const Ioss::Field &field,
4431 void *data, size_t data_size) const
4432 {
4433 {
4434 Ioss::SerializeIO serializeio_(this);
4435
4436 size_t num_to_get = field.verify(data_size);
4437
4438 if (num_to_get > 0) {
4439 int ierr = 0;
4440
4441 // Get the face block id and face count
4442 int64_t id = Ioex::get_id(eb, &ids_);
4443 int64_t my_face_count = eb->entity_count();
4444 Ioss::Field::RoleType role = field.get_role();
4445
4446 if (role == Ioss::Field::MESH) {
4447 // Handle the MESH fields required for an Exodus file model.
4448 // (The 'genesis' portion)
4449 if (field.get_name() == "connectivity") {
4450 if (my_face_count > 0) {
4451 // Map face connectivity from global node id to local node id.
4452 // Do it in 'data' ...
4453 int face_nodes = eb->topology()->number_nodes();
4454 nodeMap.reverse_map_data(data, field, num_to_get * face_nodes);
4455 ierr = ex_put_conn(get_file_pointer(), EX_FACE_BLOCK, id, data, nullptr, nullptr);
4456 if (ierr < 0) {
4457 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4458 }
4459 }
4460 }
4461 else if (field.get_name() == "connectivity_edge") {
4462 if (my_face_count > 0) {
4463 // Map face connectivity from global edge id to local edge id.
4464 int face_edges = field.transformed_storage()->component_count();
4465 edgeMap.reverse_map_data(data, field, num_to_get * face_edges);
4466 ierr = ex_put_conn(get_file_pointer(), EX_FACE_BLOCK, id, nullptr, data, nullptr);
4467 if (ierr < 0) {
4468 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4469 }
4470 }
4471 }
4472 else if (field.get_name() == "connectivity_raw") {
4473 // Do nothing, input only field.
4474 }
4475 else if (field.get_name() == "ids") {
4476 handle_face_ids(eb, data, num_to_get);
4477 }
4478 else {
4479 num_to_get = Ioss::Utils::field_warning(eb, field, "mesh output");
4480 }
4481 }
4482 else if (role == Ioss::Field::ATTRIBUTE) {
4483 num_to_get = write_attribute_field(field, eb, data);
4484 }
4485 else if (role == Ioss::Field::TRANSIENT) {
4486 // Check if the specified field exists on this face block.
4487 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4488 // exist on the database as scalars with the appropriate
4489 // extensions.
4490
4491 // Transfer each component of the variable into 'data' and then
4492 // output. Need temporary storage area of size 'number of
4493 // faces in this block.
4494 write_entity_transient_field(field, eb, my_face_count, data);
4495 }
4496 else if (role == Ioss::Field::REDUCTION) {
4497 store_reduction_field(field, eb, data);
4498 }
4499 }
4500 return num_to_get;
4501 }
4502 }
4503
4504 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeBlock *eb, const Ioss::Field &field,
4505 void *data, size_t data_size) const
4506 {
4507 {
4508 Ioss::SerializeIO serializeio_(this);
4509
4510 size_t num_to_get = field.verify(data_size);
4511
4512 if (num_to_get > 0) {
4513 int ierr = 0;
4514
4515 // Get the edge block id and edge count
4516 int64_t id = Ioex::get_id(eb, &ids_);
4517 int64_t my_edge_count = eb->entity_count();
4518 Ioss::Field::RoleType role = field.get_role();
4519
4520 if (role == Ioss::Field::MESH) {
4521 // Handle the MESH fields required for an Exodus file model. (The 'genesis' portion)
4522 if (field.get_name() == "connectivity") {
4523 if (my_edge_count > 0) {
4524 // Map edge connectivity from global node id to local node id.
4525 // Do it in 'data' ...
4526 int edge_nodes = eb->topology()->number_nodes();
4527 nodeMap.reverse_map_data(data, field, num_to_get * edge_nodes);
4528 ierr = ex_put_conn(get_file_pointer(), EX_EDGE_BLOCK, id, data, nullptr, nullptr);
4529 if (ierr < 0) {
4530 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4531 }
4532 }
4533 }
4534 else if (field.get_name() == "connectivity_raw") {
4535 // Do nothing, input only field.
4536 }
4537 else if (field.get_name() == "ids") {
4538 handle_edge_ids(eb, data, num_to_get);
4539 }
4540 else {
4541 num_to_get = Ioss::Utils::field_warning(eb, field, "mesh output");
4542 }
4543 }
4544 else if (role == Ioss::Field::ATTRIBUTE) {
4545 num_to_get = write_attribute_field(field, eb, data);
4546 }
4547 else if (role == Ioss::Field::TRANSIENT) {
4548 // Check if the specified field exists on this edge block.
4549 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4550 // exist on the database as scalars with the appropriate
4551 // extensions.
4552
4553 // Transfer each component of the variable into 'data' and then
4554 // output. Need temporary storage area of size 'number of
4555 // edges in this block.
4556 write_entity_transient_field(field, eb, my_edge_count, data);
4557 }
4558 else if (role == Ioss::Field::REDUCTION) {
4559 store_reduction_field(field, eb, data);
4560 }
4561 }
4562 return num_to_get;
4563 }
4564 }
4565
4566 int64_t DatabaseIO::handle_node_ids(void *ids, int64_t num_to_get) const
4567 {
4568 /*!
4569 * There are two modes we need to support in this routine:
4570 * 1. Initial definition of node map (local->global) and
4571 * nodeMap.reverse (global->local).
4572 * 2. Redefinition of node map via 'reordering' of the original
4573 * map when the nodes on this processor are the same, but their
4574 * order is changed (or count because of ghosting)
4575 *
4576 * So, there will be two maps the 'nodeMap.map' map is a 'direct lookup'
4577 * map which maps current local position to global id and the
4578 * 'nodeMap.reverse' is an associative lookup which maps the
4579 * global id to 'original local'. There is also a
4580 * 'nodeMap.reorder' which is direct lookup and maps current local
4581 * position to original local.
4582
4583 * The ids coming in are the global ids; their position is the
4584 * "local id-1" (That is, data[0] contains the global id of local
4585 * node 1 in this node block).
4586 *
4587 * int local_position = nodeMap.reverse[NodeMap[i+1]]
4588 * (the nodeMap.map and nodeMap.reverse are 1-based)
4589 *
4590 * To determine which map to update on a call to this function, we
4591 * use the following heuristics:
4592 * -- If the database state is 'STATE_MODEL:', then update the
4593 * 'nodeMap.reverse' and 'nodeMap.map'
4594 *
4595 * -- If the database state is not STATE_MODEL, then leave the
4596 * 'nodeMap.reverse' and 'nodeMap.map' alone since they correspond to the
4597 * information already written to the database. [May want to add a
4598 * STATE_REDEFINE_MODEL]
4599 *
4600 * -- In both cases, update the nodeMap.reorder
4601 *
4602 * NOTE: The mapping is done on TRANSIENT fields only; MODEL fields
4603 * should be in the original order...
4604 */
4605 SMART_ASSERT(num_to_get == nodeCount)(num_to_get)(nodeCount);
4606
4607 nodeMap.set_size(nodeCount);
4608
4609 bool in_define = (dbState == Ioss::STATE_MODEL) || (dbState == Ioss::STATE_DEFINE_MODEL);
4610 if (int_byte_size_api() == 4) {
4611 nodeMap.set_map(static_cast<int *>(ids), num_to_get, 0, in_define);
4612 }
4613 else {
4614 nodeMap.set_map(static_cast<int64_t *>(ids), num_to_get, 0, in_define);
4615 }
4616
4617 if (in_define) {
4618 // Only a single nodeblock and all set
4619 assert(get_region()->get_property("node_block_count").get_int() == 1);
4620
4621 // Write to the database...
4622 if (ex_put_id_map(get_file_pointer(), EX_NODE_MAP, ids) < 0) {
4623 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4624 }
4625 }
4626 return num_to_get;
4627 }
4628
4629 int64_t DatabaseIO::handle_element_ids(const Ioss::ElementBlock *eb, void *ids,
4630 size_t num_to_get) const
4631 {
4632 elemMap.set_size(elementCount);
4633 size_t offset = eb->get_offset();
4634 return handle_block_ids(eb, EX_ELEM_MAP, elemMap, ids, num_to_get, offset);
4635 }
4636
4637 int64_t DatabaseIO::handle_face_ids(const Ioss::FaceBlock *eb, void *ids, size_t num_to_get) const
4638 {
4639 faceMap.set_size(faceCount);
4640 size_t offset = eb->get_offset();
4641 return handle_block_ids(eb, EX_FACE_MAP, faceMap, ids, num_to_get, offset);
4642 }
4643
4644 int64_t DatabaseIO::handle_edge_ids(const Ioss::EdgeBlock *eb, void *ids, size_t num_to_get) const
4645 {
4646 edgeMap.set_size(edgeCount);
4647 size_t offset = eb->get_offset();
4648 return handle_block_ids(eb, EX_EDGE_MAP, edgeMap, ids, num_to_get, offset);
4649 }
4650
4651 void DatabaseIO::write_nodal_transient_field(const Ioss::Field &field,
4652 const Ioss::NodeBlock * /* ge */, int64_t count,
4653 void *variables) const
4654 {
4655 Ioss::Field::BasicType ioss_type = field.get_type();
4656 assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER ||
4657 ioss_type == Ioss::Field::INT64 || ioss_type == Ioss::Field::COMPLEX);
4658
4659 if (ioss_type == Ioss::Field::INT64) {
4660 Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)variables, count);
4661 }
4662
4663 // Note that if the field's basic type is COMPLEX, then each component of
4664 // the VariableType is a complex variable consisting of a real and
4665 // imaginary part. Since exodus cannot handle complex variables,
4666 // we have to output a (real and imaginary) X (number of
4667 // components) fields. For example, if V is a 3d vector of complex
4668 // data, the data in the 'variables' array are v_x, v.im_x, v_y,
4669 // v.im_y, v_z, v.im_z which need to be output in six separate
4670 // exodus fields. These fields were already defined in
4671 // "write_results_metadata".
4672
4673 std::vector<double> temp(count);
4674
4675 int step = get_current_state();
4676 step = get_database_step(step);
4677
4678 // get number of components, cycle through each component
4679 // and add suffix to base 'field_name'. Look up index
4680 // of this name in 'm_variables[EX_NODE_BLOCK]' map
4681 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
4682 int var_index = 0;
4683
4684 int re_im = 1;
4685 if (ioss_type == Ioss::Field::COMPLEX) {
4686 re_im = 2;
4687 }
4688 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
4689 std::string field_name = field.get_name();
4690 if (re_im == 2) {
4691 field_name += complex_suffix[complex_comp];
4692 }
4693
4694 for (int i = 0; i < comp_count; i++) {
4695 std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1);
4696
4697 auto var_iter = m_variables[EX_NODE_BLOCK].find(var_name);
4698 if (var_iter == m_variables[EX_NODE_BLOCK].end()) {
4699 IOSS_ERROR(fmt::format("ERROR: Could not find nodal variable '{}'\n", var_name));
4700 }
4701 var_index = var_iter->second;
4702
4703 size_t begin_offset = (re_im * i) + complex_comp;
4704 size_t stride = re_im * comp_count;
4705 int64_t num_out = 0;
4706
4707 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
4708 num_out = nodeMap.map_field_to_db_scalar_order(static_cast<double *>(variables), temp,
4709 begin_offset, count, stride, 0);
4710 }
4711 else if (ioss_type == Ioss::Field::INTEGER) {
4712 num_out = nodeMap.map_field_to_db_scalar_order(static_cast<int *>(variables), temp,
4713 begin_offset, count, stride, 0);
4714 }
4715 else if (ioss_type == Ioss::Field::INT64) {
4716 num_out = nodeMap.map_field_to_db_scalar_order(static_cast<int64_t *>(variables), temp,
4717 begin_offset, count, stride, 0);
4718 }
4719
4720 if (num_out != nodeCount) {
4721 IOSS_ERROR(fmt::format(
4722 "ERROR: Problem outputting nodal variable '{}' with index = {} to file '{}'\n"
4723 "Should have output {} values, but instead only output {} values.\n",
4724 var_name, var_index, decoded_filename(), nodeCount, num_out));
4725 }
4726
4727 // Write the variable...
4728 int ierr =
4729 ex_put_var(get_file_pointer(), step, EX_NODE_BLOCK, var_index, 0, num_out, Data(temp));
4730 if (ierr < 0) {
4731 std::ostringstream errmsg;
4732 fmt::print(errmsg,
4733 "Problem outputting nodal variable '{}' with index = {} to file '{}'\n",
4734 var_name, var_index, decoded_filename());
4735 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__, errmsg.str());
4736 }
4737 }
4738 }
4739 }
4740
4741 void DatabaseIO::write_entity_transient_field(const Ioss::Field &field,
4742 const Ioss::GroupingEntity *ge, int64_t count,
4743 void *variables) const
4744 {
4745 static Ioss::Map non_element_map; // Used as an empty map for ge->type() != element block.
4746 std::vector<double> temp(count);
4747
4748 int step = get_current_state();
4749 step = get_database_step(step);
4750
4751 Ioss::Map *map = nullptr;
4752 int64_t eb_offset = 0;
4753 if (ge->type() == Ioss::ELEMENTBLOCK) {
4754 const auto *elb = dynamic_cast<const Ioss::ElementBlock *>(ge);
4755 Ioss::Utils::check_dynamic_cast(elb);
4756 eb_offset = elb->get_offset();
4757 map = &elemMap;
4758 }
4759 else {
4760 map = &non_element_map;
4761 }
4762
4763 Ioss::Field::BasicType ioss_type = field.get_type();
4764 assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER ||
4765 ioss_type == Ioss::Field::INT64 || ioss_type == Ioss::Field::COMPLEX);
4766
4767 if (ioss_type == Ioss::Field::INT64) {
4768 Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)variables, count);
4769 }
4770
4771 // Note that if the field's basic type is COMPLEX, then each component of
4772 // the VariableType is a complex variable consisting of a real and
4773 // imaginary part. Since exodus cannot handle complex variables,
4774 // we have to output a (real and imaginary) X (number of
4775 // components) fields. For example, if V is a 3d vector of complex
4776 // data, the data in the 'variables' array are v_x, v.im_x, v_y,
4777 // v.im_y, v_z, v.im_z which need to be output in six separate
4778 // exodus fields. These fields were already defined in
4779 // "write_results_metadata".
4780
4781 // get number of components, cycle through each component
4782 // and add suffix to base 'field_name'. Look up index
4783 // of this name in 'm_variables[type]' map
4784 int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT);
4785
4786 // Handle quick easy, hopefully common case first...
4787 ex_entity_type type = Ioex::map_exodus_type(ge->type());
4788 if (comp_count == 1 && ioss_type == Ioss::Field::REAL && type != EX_SIDE_SET &&
4789 !map->reorders()) {
4790 // Simply output the variable...
4791 int64_t id = Ioex::get_id(ge, &ids_);
4792 std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, 1);
4793 auto var_iter = m_variables[type].find(var_name);
4794 if (var_iter == m_variables[type].end()) {
4795 IOSS_ERROR(fmt::format("ERROR: Could not find field '{}'\n", var_name));
4796 }
4797 int var_index = var_iter->second;
4798 assert(var_index > 0);
4799 int ierr = ex_put_var(get_file_pointer(), step, type, var_index, id, count, variables);
4800
4801 if (ierr < 0) {
4802 std::ostringstream extra_info;
4803 fmt::print(extra_info, "Outputting field {} at step {} on {} {}.", field.get_name(),
4804 fmt::group_digits(step), ge->type_string(), ge->name());
4805 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__, extra_info.str());
4806 }
4807 return;
4808 }
4809 int re_im = 1;
4810 if (ioss_type == Ioss::Field::COMPLEX) {
4811 re_im = 2;
4812 }
4813 for (int complex_comp = 0; complex_comp < re_im; complex_comp++) {
4814 std::string field_name = field.get_name();
4815 if (re_im == 2) {
4816 field_name += complex_suffix[complex_comp];
4817 }
4818
4819 for (int i = 0; i < comp_count; i++) {
4820 std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1);
4821
4822 auto var_iter = m_variables[type].find(var_name);
4823 if (var_iter == m_variables[type].end()) {
4824 IOSS_ERROR(fmt::format("ERROR: Could not find field '{}'\n", var_name));
4825 }
4826 int var_index = var_iter->second;
4827 assert(var_index > 0);
4828
4829 // var is a [count,comp,re_im] array; re_im = 1(real) or 2(complex)
4830 // beg_offset = (re_im*i)+complex_comp
4831 // number_values = count
4832 // stride = re_im*comp_count
4833 int64_t begin_offset = (re_im * i) + complex_comp;
4834 int64_t stride = re_im * comp_count;
4835
4836 if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) {
4837 map->map_field_to_db_scalar_order(static_cast<double *>(variables), temp, begin_offset,
4838 count, stride, eb_offset);
4839 }
4840 else if (ioss_type == Ioss::Field::INTEGER) {
4841 map->map_field_to_db_scalar_order(static_cast<int *>(variables), temp, begin_offset,
4842 count, stride, eb_offset);
4843 }
4844 else if (ioss_type == Ioss::Field::INT64) {
4845 map->map_field_to_db_scalar_order(static_cast<int64_t *>(variables), temp, begin_offset,
4846 count, stride, eb_offset);
4847 }
4848
4849 // Write the variable...
4850 int64_t id = Ioex::get_id(ge, &ids_);
4851 int ierr;
4852 if (type == EX_SIDE_SET) {
4853 size_t offset = ge->get_property("set_offset").get_int();
4854 ierr = ex_put_partial_var(get_file_pointer(), step, type, var_index, id, offset + 1,
4855 count, Data(temp));
4856 }
4857 else {
4858 ierr = ex_put_var(get_file_pointer(), step, type, var_index, id, count, Data(temp));
4859 }
4860
4861 if (ierr < 0) {
4862 std::ostringstream extra_info;
4863 fmt::print(extra_info, "Outputting component {} of field {} at step {} on {} {}.", i,
4864 field_name, fmt::group_digits(step), ge->type_string(), ge->name());
4865 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__, extra_info.str());
4866 }
4867 }
4868 }
4869 }
4870
4871 int64_t DatabaseIO::put_Xset_field_internal(const Ioss::EntitySet *ns, const Ioss::Field &field,
4872 void *data, size_t data_size) const
4873 {
4874 {
4875 Ioss::SerializeIO serializeio_(this);
4876 // ex_update(get_file_pointer());
4877
4878 size_t entity_count = ns->entity_count();
4879 size_t num_to_get = field.verify(data_size);
4880 if (num_to_get > 0) {
4881
4882 ex_entity_type type = Ioex::map_exodus_type(ns->type());
4883 int64_t id = Ioex::get_id(ns, &ids_);
4884 Ioss::Field::RoleType role = field.get_role();
4885
4886 if (role == Ioss::Field::MESH) {
4887
4888 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
4889 // Map node id from global node id to local node id.
4890 // Do it in 'data' ...
4891
4892 if (field.get_name() == "ids") {
4893 nodeMap.reverse_map_data(data, field, num_to_get);
4894 }
4895 int ierr = ex_put_set(get_file_pointer(), type, id, data, nullptr);
4896 if (ierr < 0) {
4897 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4898 }
4899 }
4900 else if (field.get_name() == "orientation") {
4901 int ierr = ex_put_set(get_file_pointer(), type, id, nullptr, data);
4902 if (ierr < 0) {
4903 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4904 }
4905 }
4906 else if (field.get_name() == "distribution_factors") {
4907 int ierr =
4908 ex_put_set_dist_fact(get_file_pointer(), type, id, static_cast<double *>(data));
4909 if (ierr < 0) {
4910 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
4911 }
4912 }
4913 else {
4914 num_to_get = Ioss::Utils::field_warning(ns, field, "output");
4915 }
4916 }
4917 else if (role == Ioss::Field::TRANSIENT) {
4918 // Check if the specified field exists on this element block.
4919 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
4920 // exist on the database as scalars with the appropriate
4921 // extensions.
4922
4923 // Transfer each component of the variable into 'data' and then
4924 // output. Need temporary storage area of size 'number of
4925 // elements in this block.
4926 write_entity_transient_field(field, ns, entity_count, data);
4927 }
4928 else if (role == Ioss::Field::ATTRIBUTE) {
4929 num_to_get = write_attribute_field(field, ns, data);
4930 }
4931 else if (role == Ioss::Field::REDUCTION) {
4932 store_reduction_field(field, ns, data);
4933 }
4934 }
4935 return num_to_get;
4936 }
4937 }
4938
4939 int64_t DatabaseIO::put_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field,
4940 void *data, size_t data_size) const
4941 {
4942 return put_Xset_field_internal(ns, field, data, data_size);
4943 }
4944
4945 int64_t DatabaseIO::put_field_internal(const Ioss::EdgeSet *ns, const Ioss::Field &field,
4946 void *data, size_t data_size) const
4947 {
4948 return put_Xset_field_internal(ns, field, data, data_size);
4949 }
4950
4951 int64_t DatabaseIO::put_field_internal(const Ioss::FaceSet *ns, const Ioss::Field &field,
4952 void *data, size_t data_size) const
4953 {
4954 return put_Xset_field_internal(ns, field, data, data_size);
4955 }
4956
4957 int64_t DatabaseIO::put_field_internal(const Ioss::ElementSet *ns, const Ioss::Field &field,
4958 void *data, size_t data_size) const
4959 {
4960 return put_Xset_field_internal(ns, field, data, data_size);
4961 }
4962
4963 int64_t DatabaseIO::put_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field,
4964 void *data, size_t data_size) const
4965 {
4966 size_t num_to_get = field.verify(data_size);
4967 size_t entity_count = cs->entity_count();
4968
4969 assert(num_to_get == entity_count);
4970 if (num_to_get == 0) {
4971 return 0;
4972 }
4973
4974 // Return the <entity (node or side), processor> pair
4975 if (field.get_name() == "entity_processor") {
4976
4977 // Check type -- node or side
4978 std::string type = cs->get_property("entity_type").get_string();
4979
4980 // Allocate temporary storage space
4981 std::vector<char> entities(entity_count * int_byte_size_api());
4982 std::vector<char> procs(entity_count * int_byte_size_api());
4983
4984 if (type == "node") {
4985 Ioss::SerializeIO serializeio_(this);
4986 // Convert global node id to local node id and store in 'entities'
4987 if (int_byte_size_api() == 4) {
4988 int *entity_proc = static_cast<int *>(data);
4989 int *ent = reinterpret_cast<int *>(Data(entities));
4990 int *pro = reinterpret_cast<int *>(Data(procs));
4991 int j = 0;
4992 for (size_t i = 0; i < entity_count; i++) {
4993 int global_id = entity_proc[j++];
4994 ent[i] = nodeMap.global_to_local(global_id, true);
4995 pro[i] = entity_proc[j++];
4996 }
4997 }
4998 else {
4999 auto *entity_proc = static_cast<int64_t *>(data);
5000 auto *ent = reinterpret_cast<int64_t *>(Data(entities));
5001 auto *pro = reinterpret_cast<int64_t *>(Data(procs));
5002 int64_t j = 0;
5003 for (size_t i = 0; i < entity_count; i++) {
5004 int64_t global_id = entity_proc[j++];
5005 ent[i] = nodeMap.global_to_local(global_id, true);
5006 pro[i] = entity_proc[j++];
5007 }
5008 }
5009
5010 if (commsetNodeCount > 0) {
5011 int ierr = ex_put_node_cmap(get_file_pointer(), Ioex::get_id(cs, &ids_), Data(entities),
5012 Data(procs), myProcessor);
5013 if (ierr < 0) {
5014 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5015 }
5016 }
5017
5018 if (commsetNodeCount == 1) {
5019 // NOTE: The internal and border node maps must be output in one call.
5020 // In this routine, we only have one commset at a time and can't
5021 // construct the entire map at one time. This is not really needed,
5022 // so for now we just skip if there is more than one commset. If
5023 // this information is really needed, need to cache the information
5024 // until all commsets have been processed. Also need to change
5025 // write_communication_metada() [Maybe, unless client sets correct
5026 // properties.]
5027
5028 // Construct the node map (internal vs. border).
5029 // Border nodes are those in the communication map (use entities array)
5030 // Internal nodes are the rest. Allocate array to hold all nodes,
5031 // initialize all to '1', then zero out the nodes in 'entities'.
5032 // Iterate through array again and consolidate all '1's
5033
5034 std::vector<char> internal(nodeCount * int_byte_size_api());
5035 if (int_byte_size_api() == 4) {
5036 compute_internal_border_maps(reinterpret_cast<int *>(Data(entities)),
5037 reinterpret_cast<int *>(Data(internal)), nodeCount,
5038 entity_count);
5039 }
5040 else {
5041 compute_internal_border_maps(reinterpret_cast<int64_t *>(Data(entities)),
5042 reinterpret_cast<int64_t *>(Data(internal)), nodeCount,
5043 entity_count);
5044 }
5045
5046 int ierr = ex_put_processor_node_maps(get_file_pointer(), Data(internal), Data(entities),
5047 nullptr, myProcessor);
5048 if (ierr < 0) {
5049 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5050 }
5051 }
5052 }
5053 else if (type == "side") {
5054 Ioss::SerializeIO serializeio_(this);
5055 std::vector<char> sides(entity_count * int_byte_size_api());
5056 if (int_byte_size_api() == 4) {
5057 int *entity_proc = static_cast<int *>(data);
5058 int *ent = reinterpret_cast<int *>(Data(entities));
5059 int *sid = reinterpret_cast<int *>(Data(sides));
5060 int *pro = reinterpret_cast<int *>(Data(procs));
5061 int j = 0;
5062 for (size_t i = 0; i < entity_count; i++) {
5063 ent[i] = elemMap.global_to_local(entity_proc[j++]);
5064 sid[i] = entity_proc[j++];
5065 pro[i] = entity_proc[j++];
5066 }
5067 }
5068 else {
5069 auto *entity_proc = static_cast<int64_t *>(data);
5070 auto *ent = reinterpret_cast<int64_t *>(Data(entities));
5071 auto *sid = reinterpret_cast<int64_t *>(Data(sides));
5072 auto *pro = reinterpret_cast<int64_t *>(Data(procs));
5073 int64_t j = 0;
5074 for (size_t i = 0; i < entity_count; i++) {
5075 ent[i] = elemMap.global_to_local(entity_proc[j++]);
5076 sid[i] = entity_proc[j++];
5077 pro[i] = entity_proc[j++];
5078 }
5079 }
5080
5081 int ierr = ex_put_elem_cmap(get_file_pointer(), Ioex::get_id(cs, &ids_), Data(entities),
5082 Data(sides), Data(procs), myProcessor);
5083 if (ierr < 0) {
5084 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5085 }
5086
5087 // Construct the element map (internal vs. border).
5088 // Border elements are those in the communication map (use entities array)
5089 // Internal elements are the rest. Allocate array to hold all elements,
5090 // initialize all to '1', then zero out the elements in 'entities'.
5091 // Iterate through array again and consolidate all '1's
5092 std::vector<char> internal(elementCount * int_byte_size_api());
5093 if (int_byte_size_api() == 4) {
5094 compute_internal_border_maps(reinterpret_cast<int *>(Data(entities)),
5095 reinterpret_cast<int *>(Data(internal)), elementCount,
5096 entity_count);
5097 }
5098 else {
5099 compute_internal_border_maps(reinterpret_cast<int64_t *>(Data(entities)),
5100 reinterpret_cast<int64_t *>(Data(internal)), elementCount,
5101 entity_count);
5102 }
5103
5104 ierr = ex_put_processor_elem_maps(get_file_pointer(), Data(internal), Data(entities),
5105 myProcessor);
5106 if (ierr < 0) {
5107 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5108 }
5109 }
5110 else {
5111 IOSS_ERROR(fmt::format("ERROR: Invalid commset type {}", type));
5112 }
5113 }
5114 else if (field.get_name() == "ids") {
5115 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
5116 }
5117 else {
5118 num_to_get = Ioss::Utils::field_warning(cs, field, "output");
5119 }
5120 return num_to_get;
5121 }
5122
5123 int64_t DatabaseIO::put_field_internal(const Ioss::SideSet *fs, const Ioss::Field &field,
5124 void * /* data */, size_t data_size) const
5125 {
5126 size_t num_to_get = field.verify(data_size);
5127 if (field.get_name() == "ids") {
5128 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
5129 }
5130 else {
5131 num_to_get = Ioss::Utils::field_warning(fs, field, "output");
5132 }
5133 return num_to_get;
5134 }
5135
5136 int64_t DatabaseIO::put_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field,
5137 void *data, size_t data_size) const
5138 {
5139 Ioss::SerializeIO serializeio_(this);
5140 size_t num_to_get = field.verify(data_size);
5141 if (num_to_get > 0) {
5142
5143 int64_t id = Ioex::get_id(fb, &ids_);
5144
5145 size_t entity_count = fb->entity_count();
5146 size_t offset = fb->get_property("set_offset").get_int();
5147
5148 Ioss::Field::RoleType role = field.get_role();
5149
5150 if (role == Ioss::Field::MESH) {
5151 if (field.get_name() == "side_ids" && fb->name() == "universal_sideset") {
5152 // The side ids are being stored as the distribution factor
5153 // field on the universal sideset. There should be no other
5154 // side sets that request this field... (Eventually,
5155 // create an id field to store this info.
5156
5157 // Need to convert 'ints' to 'double' for storage on mesh...
5158 // FIX 64
5159 if (field.get_type() == Ioss::Field::INTEGER) {
5160 int *ids = static_cast<int *>(data);
5161 std::vector<double> real_ids(num_to_get);
5162 for (size_t i = 0; i < num_to_get; i++) {
5163 real_ids[i] = static_cast<double>(ids[i]);
5164 }
5165 int ierr = ex_put_partial_set_dist_fact(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5166 entity_count, Data(real_ids));
5167 if (ierr < 0) {
5168 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5169 }
5170 }
5171 else {
5172 auto *ids = static_cast<int64_t *>(data);
5173 std::vector<double> real_ids(num_to_get);
5174 for (size_t i = 0; i < num_to_get; i++) {
5175 real_ids[i] = static_cast<double>(ids[i]);
5176 }
5177 int ierr = ex_put_partial_set_dist_fact(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5178 entity_count, Data(real_ids));
5179 if (ierr < 0) {
5180 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5181 }
5182 }
5183 }
5184
5185 else if (field.get_name() == "side_ids") {
5186 }
5187
5188 else if (field.get_name() == "ids") {
5189 // =============================================================
5190 // NOTE: Code is currently commented out since we have
5191 // redundant ways of getting the data (element/side) out to
5192 // the database. The 'ids' field method relies on a numbering
5193 // kluge, so for now trying the 'element_side' field...
5194 // =============================================================
5195 }
5196
5197 else if (field.get_name() == "distribution_factors") {
5198 int ierr;
5199 size_t df_offset = fb->get_property("set_df_offset").get_int();
5200 size_t df_count = fb->get_property("distribution_factor_count").get_int();
5201 ierr = ex_put_partial_set_dist_fact(get_file_pointer(), EX_SIDE_SET, id, df_offset + 1,
5202 df_count, static_cast<double *>(data));
5203 if (ierr < 0) {
5204 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5205 }
5206 }
5207 else if (field.get_name() == "element_side") {
5208 // In exodus, the 'side block' is stored as a sideset. A
5209 // sideset has a list of elements and a corresponding local
5210 // element side (1-based)
5211
5212 // The 'data' passed into the function is stored as a
5213 // 2D vector e0,f0,e1,f1,... (e=element, f=side)
5214
5215 // To avoid overwriting the passed in data, we allocate
5216 // two arrays to store the data for this sideset.
5217
5218 // The element_id passed in is the global id; we need to
5219 // output the local id.
5220
5221 // Allocate space for local side number and element numbers
5222 // numbers.
5223 // See if edges or faces...
5224 size_t side_offset = Ioss::Utils::get_side_offset(fb);
5225
5226 size_t index = 0;
5227
5228 if (field.get_type() == Ioss::Field::INTEGER) {
5229 Ioss::IntVector element(num_to_get);
5230 Ioss::IntVector side(num_to_get);
5231 int *el_side = reinterpret_cast<int *>(data);
5232
5233 try {
5234 for (size_t i = 0; i < num_to_get; i++) {
5235 element[i] = elemMap.global_to_local(el_side[index++]);
5236 side[i] = el_side[index++] + side_offset;
5237 }
5238 }
5239 catch (const std::runtime_error &x) {
5240 IOSS_ERROR(fmt::format("{}On SideBlock `{}` while outputting field `elem_side`\n",
5241 x.what(), fb->name()));
5242 }
5243
5244 int ierr = ex_put_partial_set(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5245 entity_count, Data(element), Data(side));
5246 if (ierr < 0) {
5247 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5248 }
5249 }
5250 else {
5251 Ioss::Int64Vector element(num_to_get);
5252 Ioss::Int64Vector side(num_to_get);
5253 auto *el_side = reinterpret_cast<int64_t *>(data);
5254
5255 try {
5256 for (size_t i = 0; i < num_to_get; i++) {
5257 element[i] = elemMap.global_to_local(el_side[index++]);
5258 side[i] = el_side[index++] + side_offset;
5259 }
5260 }
5261 catch (const std::runtime_error &x) {
5262 IOSS_ERROR(fmt::format("{}On SideBlock `{}` while outputting field `elem_side`\n",
5263 x.what(), fb->name()));
5264 }
5265
5266 int ierr = ex_put_partial_set(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5267 entity_count, Data(element), Data(side));
5268 if (ierr < 0) {
5269 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5270 }
5271 }
5272 }
5273 else if (field.get_name() == "element_side_raw") {
5274 // In exodus, the 'side block' is stored as a sideset. A
5275 // sideset has a list of elements and a corresponding local
5276 // element side (1-based)
5277
5278 // The 'data' passed into the function is stored as a
5279 // 2D vector e0,f0,e1,f1,... (e=element, f=side)
5280
5281 // To avoid overwriting the passed in data, we allocate
5282 // two arrays to store the data for this sideset.
5283
5284 // The element_id passed in is the local id.
5285
5286 // See if edges or faces...
5287 size_t side_offset = Ioss::Utils::get_side_offset(fb);
5288
5289 size_t index = 0;
5290 if (field.get_type() == Ioss::Field::INTEGER) {
5291 Ioss::IntVector element(num_to_get);
5292 Ioss::IntVector side(num_to_get);
5293 int *el_side = reinterpret_cast<int *>(data);
5294
5295 for (size_t i = 0; i < num_to_get; i++) {
5296 element[i] = el_side[index++];
5297 side[i] = el_side[index++] + side_offset;
5298 }
5299
5300 int ierr = ex_put_partial_set(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5301 entity_count, Data(element), Data(side));
5302 if (ierr < 0) {
5303 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5304 }
5305 }
5306 else {
5307 Ioss::Int64Vector element(num_to_get);
5308 Ioss::Int64Vector side(num_to_get);
5309 auto *el_side = reinterpret_cast<int64_t *>(data);
5310
5311 for (size_t i = 0; i < num_to_get; i++) {
5312 element[i] = el_side[index++];
5313 side[i] = el_side[index++] + side_offset;
5314 }
5315
5316 int ierr = ex_put_partial_set(get_file_pointer(), EX_SIDE_SET, id, offset + 1,
5317 entity_count, Data(element), Data(side));
5318 if (ierr < 0) {
5319 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5320 }
5321 }
5322 }
5323 else if (field.get_name() == "connectivity") {
5324 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
5325 }
5326 else if (field.get_name() == "connectivity_raw") {
5327 // Do nothing, just handles an idiosyncrasy of the GroupingEntity
5328 }
5329 else {
5330 num_to_get = Ioss::Utils::field_warning(fb, field, "output");
5331 }
5332 }
5333 else if (role == Ioss::Field::TRANSIENT) {
5334 // Check if the specified field exists on this block.
5335 // Note that 'higher-order' storage types (e.g. SYM_TENSOR)
5336 // exist on the database as scalars with the appropriate
5337 // extensions.
5338
5339 // Transfer each component of the variable into 'data' and then
5340 // output. Need temporary storage area of size 'number of
5341 // entities in this block.
5342 write_entity_transient_field(field, fb, entity_count, data);
5343 }
5344 else if (role == Ioss::Field::ATTRIBUTE) {
5345 num_to_get = write_attribute_field(field, fb, data);
5346 }
5347 else if (role == Ioss::Field::REDUCTION) {
5348 store_reduction_field(field, fb, data);
5349 }
5350 }
5351 return num_to_get;
5352 }
5353
5354 void DatabaseIO::write_meta_data(Ioss::IfDatabaseExistsBehavior behavior)
5355 {
5356 Ioss::Region *region = get_region();
5357 common_write_metadata(behavior);
5358
5359 char the_title[max_line_length + 1];
5360
5361 // Title...
5362 if (region->property_exists("title")) {
5363 std::string title_str = region->get_property("title").get_string();
5364 Ioss::Utils::copy_string(the_title, title_str);
5365 }
5366 else {
5367 Ioss::Utils::copy_string(the_title, "IOSS Default Output Title");
5368 }
5369
5370 bool file_per_processor = true;
5371 Ioex::Mesh mesh(spatialDimension, the_title, util(), file_per_processor);
5372 {
5373 bool omit_maps = false;
5374 Ioss::Utils::check_set_bool_property(properties, "OMIT_EXODUS_NUM_MAPS", omit_maps);
5375 if (omit_maps) {
5376 // Used for special cases only -- typically very large meshes with *known* 1..count maps
5377 // and workarounds that avoid calling the "ids" put_field calls.
5378 mesh.use_node_map = false;
5379 mesh.use_elem_map = false;
5380 mesh.use_face_map = false;
5381 mesh.use_edge_map = false;
5382 }
5383
5384 bool minimal_nemesis = false;
5385 Ioss::Utils::check_set_bool_property(properties, "MINIMAL_NEMESIS_DATA", minimal_nemesis);
5386 if (minimal_nemesis) {
5387 // Only output the node communication map data... This is all that stk/sierra needs
5388 mesh.full_nemesis_data = false;
5389 }
5390
5391 Ioss::SerializeIO serializeio_(this);
5392 mesh.populate(region);
5393 gather_communication_metadata(&mesh.comm);
5394
5395 if (behavior != Ioss::DB_APPEND && behavior != Ioss::DB_MODIFY) {
5396 bool omit_qa = false;
5397 Ioss::Utils::check_set_bool_property(properties, "OMIT_QA_RECORDS", omit_qa);
5398 if (!omit_qa) {
5399 put_qa();
5400 }
5401
5402 bool omit_info = false;
5403 Ioss::Utils::check_set_bool_property(properties, "OMIT_INFO_RECORDS", omit_info);
5404 if (!omit_info) {
5405 put_info();
5406 }
5407
5408 // Write the metadata to the exodus file...
5409 Ioex::Internals data(get_file_pointer(), maximumNameLength, util());
5410 int ierr = data.write_meta_data(mesh);
5411
5412 if (ierr < 0) {
5413 Ioex::exodus_error(get_file_pointer(), __LINE__, __func__, __FILE__);
5414 }
5415 output_other_metadata();
5416 }
5417 }
5418 }
5419
5420 void DatabaseIO::gather_communication_metadata(Ioex::CommunicationMetaData *meta)
5421 {
5422 // It's possible that we are a serial program outputting information
5423 // for later use by a parallel program.
5424
5425 meta->processorCount = 0;
5426 meta->processorId = 0;
5427 meta->outputNemesis = false;
5428
5429 if (isParallel) {
5430 meta->processorCount = util().parallel_size();
5431 meta->processorId = myProcessor;
5432 meta->outputNemesis = true;
5433 }
5434 else {
5435 if (properties.exists("processor_count")) {
5436 meta->processorCount = properties.get("processor_count").get_int();
5437 }
5438 else if (get_region()->property_exists("processor_count")) {
5439 meta->processorCount = get_region()->get_property("processor_count").get_int();
5440 }
5441
5442 if (properties.exists("my_processor")) {
5443 meta->processorId = properties.get("my_processor").get_int();
5444 }
5445 else if (get_region()->property_exists("my_processor")) {
5446 meta->processorId = get_region()->get_property("my_processor").get_int();
5447 }
5448
5449 if (!get_region()->get_commsets().empty()) {
5450 isSerialParallel = true;
5451 meta->outputNemesis = true;
5452 }
5453 }
5454
5455 if (isSerialParallel || meta->processorCount > 0) {
5456 meta->globalNodes = get_region()->get_optional_property("global_node_count", 1);
5457 meta->globalElements = get_region()->get_optional_property("global_element_count", 1);
5458 meta->globalElementBlocks = get_region()->get_optional_property(
5459 "global_element_block_count", get_region()->get_element_blocks().size());
5460 meta->globalNodeSets = get_region()->get_optional_property(
5461 "global_node_set_count", get_region()->get_nodesets().size());
5462 meta->globalSideSets = get_region()->get_optional_property(
5463 "global_side_set_count", get_region()->get_sidesets().size());
5464
5465 // ========================================================================
5466 // Load balance parameters (NEMESIS, p15)
5467 meta->nodesInternal = get_region()->get_optional_property("internal_node_count", nodeCount);
5468 meta->nodesBorder = get_region()->get_optional_property("border_node_count", 0);
5469 meta->nodesExternal = 0; // Shadow nodes == 0 for now
5470 meta->elementsInternal =
5471 get_region()->get_optional_property("internal_element_count", elementCount);
5472 meta->elementsBorder = get_region()->get_optional_property("border_element_count", 0);
5473
5474 const Ioss::CommSetContainer &comm_sets = get_region()->get_commsets();
5475 for (auto &cs : comm_sets) {
5476 std::string type = cs->get_property("entity_type").get_string();
5477 size_t count = cs->entity_count();
5478 int64_t id = Ioex::get_id(cs, &ids_);
5479
5480 if (type == "node") {
5481 meta->nodeMap.emplace_back(id, count, 'n');
5482 }
5483 else if (type == "side") {
5484 meta->elementMap.emplace_back(id, count, 'e');
5485 }
5486 else {
5487 IOSS_ERROR("Internal Program Error...");
5488 }
5489 }
5490 }
5491 commsetNodeCount = meta->nodeMap.size();
5492 commsetElemCount = meta->elementMap.size();
5493 }
5494 } // namespace Ioex