Debian Codegraph @

packages/seacas/applications/epu/EP_Internals.C

raw
1 /*
2 * Copyright(C) 1999-2024, National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include <EP_ExodusEntity.h> // for Block, Mesh
10 #include <EP_Internals.h> // for Internals, Redefine
11
12 #include "vector_data.h"
13 #include <algorithm> // for sort
14 #include <array>
15 #include <copy_string_cpp.h>
16 #include <cstddef> // for size_t
17 #include <cstdint> // for int64_t
18 #include <cstdlib> // for exit, EXIT_FAILURE
19 #include <cstring> // for strlen, memset
20 #include <fmt/ostream.h>
21 #include <smart_assert.h> // for SMART_ASSERT
22 #include <string> // for string, basic_string
23 #include <vector> // for vector
24
25 extern "C" {
26 #define NO_NETCDF_2
27 #include <exodusII.h>
28 #include <exodusII_int.h>
29 #include <netcdf.h>
30 }
31
32 // Explicit Initialization of the functions used...
33 namespace Excn {
34 template int Internals<int>::write_meta_data(const Mesh &mesh, const std::vector<Block> &blocks,
35 const std::vector<NodeSet<int>> &nodesets,
36 const std::vector<SideSet<int>> &sidesets,
37 const std::vector<EdgeBlock<int>> &edgeblock,
38 const std::vector<FaceBlock<int>> &faceblock,
39 const CommunicationMetaData &comm);
40
41 template bool Internals<int>::check_meta_data(const Mesh &mesh, const std::vector<Block> &blocks,
42 const std::vector<NodeSet<int>> &nodesets,
43 const std::vector<SideSet<int>> &sidesets,
44 const std::vector<EdgeBlock<int>> &edgeblock,
45 const std::vector<FaceBlock<int>> &faceblock,
46 const CommunicationMetaData &comm);
47
48 template int Internals<int64_t>::write_meta_data(const Mesh &mesh,
49 const std::vector<Block> &blocks,
50 const std::vector<NodeSet<int64_t>> &nodesets,
51 const std::vector<SideSet<int64_t>> &sidesets,
52 const std::vector<EdgeBlock<int64_t>> &edgeblock,
53 const std::vector<FaceBlock<int64_t>> &faceblock,
54 const CommunicationMetaData &comm);
55
56 template bool Internals<int64_t>::check_meta_data(
57 const Mesh &mesh, const std::vector<Block> &blocks,
58 const std::vector<NodeSet<int64_t>> &nodesets, const std::vector<SideSet<int64_t>> &sidesets,
59 const std::vector<EdgeBlock<int64_t>> &edgeblock,
60 const std::vector<FaceBlock<int64_t>> &faceblock, const CommunicationMetaData &comm);
61 } // namespace Excn
62
63 namespace {
64 bool lessOffset(const Excn::Block &b1, const Excn::Block &b2) { return b1.offset_ < b2.offset_; }
65
66 nc_type get_type(int exoid, unsigned int type)
67 {
68 if ((ex_int64_status(exoid) & type) != 0U) {
69 return NC_INT64;
70 }
71
72 return NC_INT;
73 }
74
75 int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num,
76 const char *stat_var, const char *id_var, const char *name_var);
77
78 template <typename INT>
79 int put_array(int exoid, const char *var_type, const std::vector<INT> &array);
80
81 int put_id_array(int exoid, const char *var_type, const std::vector<ex_entity_id> &ids);
82
83 int define_coordinate_vars(int exodusFilePtr, int64_t nodes, int node_dim, int dimension,
84 int dim_dim, int str_dim);
85 } // namespace
86
87 bool Excn::is_path_absolute(const std::string &path)
88 {
89 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
90 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
91 return path[0] == '\\' || path[1] == ':';
92 #else
93 return path[0] == '/';
94 #endif
95 }
96
97 Excn::Redefine::Redefine(int exoid) : exodusFilePtr(exoid)
98 {
99 // Enter define mode...
100 int status = nc_redef(exodusFilePtr);
101 if (status != NC_NOERR) {
102 ex_opts(EX_VERBOSE);
103 std::string errmsg;
104 errmsg = fmt::format("Error: failed to put file id {} into define mode", exodusFilePtr);
105 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
106 exit(EXIT_FAILURE);
107 }
108 }
109
110 Excn::Redefine::~Redefine()
111 {
112 try {
113 int status = nc_enddef(exodusFilePtr);
114 if (status != NC_NOERR) {
115 ex_opts(EX_VERBOSE);
116 std::string errmsg;
117 errmsg = fmt::format("Error: failed to complete variable definitions in file id {}",
118 exodusFilePtr);
119 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
120 exit(EXIT_FAILURE);
121 }
122 }
123 catch (...) {
124 }
125 }
126
127 template <typename INT>
128 int Excn::Internals<INT>::write_meta_data(const Mesh &mesh, const std::vector<Block> &blocks,
129 const std::vector<NodeSet<INT>> &nodesets,
130 const std::vector<SideSet<INT>> &sidesets,
131 const std::vector<EdgeBlock<INT>> &edgeblocks,
132 const std::vector<FaceBlock<INT>> &faceblocks,
133 const CommunicationMetaData &comm)
134 {
135 SMART_ASSERT((int)blocks.size() == mesh.blockCount);
136 SMART_ASSERT((int)nodesets.size() == mesh.nodesetCount);
137 SMART_ASSERT((int)sidesets.size() == mesh.sidesetCount);
138 SMART_ASSERT((int)edgeblocks.size() == mesh.edgeBlockCount);
139 SMART_ASSERT((int)faceblocks.size() == mesh.faceBlockCount);
140
141 // May need to reorder the element blocks based on the 'offset_'
142 // member. An element block contains the elements from 'offset_+1'
143 // to 'offset_+elementCount'. Typically, this is the order that
144 // they appear in the 'blocks' array, but not always...
145 //
146 // This is a very hard error to track down, so we just copy the
147 // 'blocks' array to a new array and sort it on the 'offset_'
148 // member...
149 //
150 // All block-related calls after these meta-data calls are based on
151 // the block id and not the block order, so we don't need to fix
152 // them.
153 for (size_t i = 0; i < blocks.size(); i++) {
154 const_cast<Excn::Block &>(blocks[i]).position_ = i;
155 }
156
157 // Check whether current order is consistent. The problem with just
158 // sorting the data is that if a subset of the entire model is being
159 // joined, there may be zero-length element blocks. These blocks
160 // have an offset of zero since there is no "lowest id" to get the
161 // offset from.
162 //
163 // If all that is wanted is a single subset of the entire model, it
164 // doesn't matter if the blocks are reordered due to the zero
165 // offsets; however, if the user then later joins multiple subsets
166 // together, there will be issues of block ordering mismatches.
167 //
168 // So, to avoid sorting the data into incorrect order, we check
169 // whether the data are consistent before sorting and if so, don't
170 // do the sort. Concsistent means that all blocks that have a
171 // nonzero element count are in the correct order.
172 int64_t last_offset = 0;
173 bool order_ok = true;
174 for (const auto &block : blocks) {
175 if (block.elementCount > 0) {
176 if (block.offset_ < last_offset) {
177 order_ok = false;
178 break;
179 }
180 last_offset = block.offset_;
181 }
182 }
183
184 std::vector<Block> sorted_blocks(blocks);
185 if (!order_ok) {
186 std::sort(sorted_blocks.begin(), sorted_blocks.end(), lessOffset);
187
188 // Now, update the position_ field based on the sorted order.
189 for (size_t i = 0; i < blocks.size(); i++) {
190 int orig_position = sorted_blocks[i].position_;
191 const_cast<Excn::Block &>(blocks[orig_position]).position_ = i;
192 SMART_ASSERT(blocks[orig_position].id == sorted_blocks[i].id);
193 }
194 }
195
196 int ierr;
197 {
198 Excn::Redefine the_database(exodusFilePtr);
199
200 ierr = put_metadata(mesh, comm);
201 if (ierr != EX_NOERR) {
202 return ierr;
203 }
204
205 ierr = put_metadata(sorted_blocks);
206 if (ierr != EX_NOERR) {
207 return ierr;
208 }
209
210 ierr = put_metadata(nodesets);
211 if (ierr != EX_NOERR) {
212 return ierr;
213 }
214
215 ierr = put_metadata(sidesets);
216 if (ierr != EX_NOERR) {
217 return ierr;
218 }
219
220 ierr = put_metadata(edgeblocks);
221 if (ierr != EX_NOERR) {
222 return ierr;
223 }
224
225 ierr = put_metadata(faceblocks);
226 if (ierr != EX_NOERR) {
227 return ierr;
228 }
229 }
230
231 // NON-Define mode output...
232 ierr = put_non_define_data(mesh, comm);
233 if (ierr != EX_NOERR) {
234 return ierr;
235 }
236
237 ierr = put_non_define_data(sorted_blocks);
238 if (ierr != EX_NOERR) {
239 return ierr;
240 }
241
242 ierr = put_non_define_data(nodesets);
243 if (ierr != EX_NOERR) {
244 return ierr;
245 }
246
247 ierr = put_non_define_data(sidesets);
248 if (ierr != EX_NOERR) {
249 return ierr;
250 }
251
252 ierr = put_non_define_data(edgeblocks);
253 if (ierr != EX_NOERR) {
254 return ierr;
255 }
256
257 ierr = put_non_define_data(faceblocks);
258 if (ierr != EX_NOERR) {
259 return ierr;
260 }
261
262 // For now, put entity names using the ExodusII api...
263 {
264 int max_entity = std::max({mesh.blockCount, mesh.nodesetCount, mesh.sidesetCount,
265 mesh.edgeBlockCount, mesh.faceBlockCount});
266 for (const auto &block : blocks) {
267 max_entity = std::max(max_entity, block.attributeCount);
268 }
269
270 size_t name_size = ex_inquire_int(exodusFilePtr, EX_INQ_MAX_READ_NAME_LENGTH);
271 auto *names = new char *[max_entity];
272 for (int i = 0; i < max_entity; i++) {
273 names[i] = new char[name_size + 1];
274 }
275
276 if (mesh.blockCount > 0) {
277 for (int i = 0; i < mesh.blockCount; i++) {
278 copy_string(names[i], sorted_blocks[i].name_, name_size + 1);
279 }
280 ex_put_names(exodusFilePtr, EX_ELEM_BLOCK, names);
281
282 for (int i = 0; i < mesh.blockCount; i++) {
283 if (blocks[i].attributeCount > 0) {
284 SMART_ASSERT((size_t)blocks[i].attributeCount == blocks[i].attributeNames.size());
285 for (int j = 0; j < blocks[i].attributeCount; j++) {
286 std::memset(names[j], '\0', name_size + 1);
287 if (!blocks[i].attributeNames[j].empty()) {
288 copy_string(names[j], blocks[i].attributeNames[j], name_size + 1);
289 }
290 }
291 ierr = ex_put_attr_names(exodusFilePtr, EX_ELEM_BLOCK, blocks[i].id, names);
292 SMART_ASSERT(ierr == 0);
293 }
294 }
295 }
296
297 if (mesh.nodesetCount > 0) {
298 for (int i = 0; i < mesh.nodesetCount; i++) {
299 copy_string(names[i], nodesets[i].name_, name_size + 1);
300 }
301 ex_put_names(exodusFilePtr, EX_NODE_SET, names);
302 }
303
304 if (mesh.sidesetCount > 0) {
305 for (int i = 0; i < mesh.sidesetCount; i++) {
306 copy_string(names[i], sidesets[i].name_, name_size + 1);
307 }
308 ex_put_names(exodusFilePtr, EX_SIDE_SET, names);
309 }
310
311 if (mesh.edgeBlockCount > 0) {
312 for (int i = 0; i < mesh.edgeBlockCount; i++) {
313 copy_string(names[i], edgeblocks[i].name_, name_size + 1);
314 }
315 ex_put_names(exodusFilePtr, EX_EDGE_BLOCK, names);
316 }
317
318 if (mesh.faceBlockCount > 0) {
319 for (int i = 0; i < mesh.faceBlockCount; i++) {
320 copy_string(names[i], faceblocks[i].name_, name_size + 1);
321 }
322 ex_put_names(exodusFilePtr, EX_FACE_BLOCK, names);
323 }
324
325 for (int i = 0; i < max_entity; i++) {
326 delete[] names[i];
327 }
328 delete[] names;
329 }
330
331 ex_update(exodusFilePtr);
332 return EX_NOERR;
333 }
334
335 template <typename INT>
336 bool Excn::Internals<INT>::check_meta_data(const Mesh &mesh, const std::vector<Block> & /*unused*/,
337 const std::vector<NodeSet<INT>> & /*unused*/,
338 const std::vector<SideSet<INT>> & /*unused*/,
339 const std::vector<EdgeBlock<INT>> & /*unused*/,
340 const std::vector<FaceBlock<INT>> & /*unused*/,
341 const CommunicationMetaData & /*unused*/)
342 {
343 ex_init_params init_data{};
344 ex_get_init_ext(exodusFilePtr, &init_data);
345
346 bool matches = true;
347 if (mesh.dimensionality != init_data.num_dim) {
348 fmt::print(stderr,
349 "ERROR: (EPU) original mesh dimensionality ({}) does not match current "
350 "dimensionality ({})\n",
351 mesh.dimensionality, init_data.num_dim);
352 matches = false;
353 }
354
355 if (mesh.nodeCount != init_data.num_nodes) {
356 fmt::print(
357 stderr,
358 "ERROR: (EPU) original mesh node count ({}) does not match current node count ({})\n",
359 mesh.nodeCount, init_data.num_nodes);
360 matches = false;
361 }
362
363 if (mesh.elementCount != init_data.num_elem) {
364 fmt::print(
365 stderr,
366 "ERROR: (EPU) original mesh element count ({}) does not match current element count ({})\n",
367 mesh.elementCount, init_data.num_elem);
368 matches = false;
369 }
370
371 if (mesh.blockCount != init_data.num_elem_blk) {
372 fmt::print(stderr,
373 "ERROR: (EPU) original mesh element block count ({}) does not match current element "
374 "block count ({})\n",
375 mesh.blockCount, init_data.num_elem_blk);
376 matches = false;
377 }
378
379 if (mesh.nodesetCount != init_data.num_node_sets) {
380 fmt::print(
381 stderr,
382 "ERROR: (EPU) original mesh nodeset count ({}) does not match current nodeset count ({})\n",
383 mesh.nodesetCount, init_data.num_node_sets);
384 matches = false;
385 }
386
387 if (mesh.sidesetCount != init_data.num_side_sets) {
388 fmt::print(
389 stderr,
390 "ERROR: (EPU) original mesh sideset count ({}) does not match current sideset count ({})\n",
391 mesh.sidesetCount, init_data.num_side_sets);
392 matches = false;
393 }
394
395 if (mesh.edgeBlockCount != init_data.num_edge_blk) {
396 fmt::print(stderr,
397 "ERROR: (EPU) original mesh edgeblock count ({}) does not match current edgeblock "
398 "count ({})\n",
399 mesh.edgeBlockCount, init_data.num_edge_blk);
400 matches = false;
401 }
402
403 if (mesh.faceBlockCount != init_data.num_face_blk) {
404 fmt::print(stderr,
405 "ERROR: (EPU) original mesh faceblock count ({}) does not match current faceblock "
406 "count ({})\n",
407 mesh.faceBlockCount, init_data.num_face_blk);
408 matches = false;
409 }
410
411 return matches;
412 }
413
414 template <typename INT>
415 int Excn::Internals<INT>::put_metadata(const Mesh &mesh, const CommunicationMetaData & /*unused*/)
416 {
417 int numdimdim = 0;
418 int timedim = 0;
419 int numnoddim = 0;
420 int namestrdim = 0;
421
422 int map_type = get_type(exodusFilePtr, EX_MAPS_INT64_DB);
423
424 std::string errmsg;
425 int status;
426
427 if (changeSetNumber <= 1) {
428 // define some attributes...
429 int rootid = exodusFilePtr & EX_FILE_ID_MASK;
430
431 status =
432 nc_put_att_text(rootid, NC_GLOBAL, ATT_TITLE, mesh.title.length() + 1, mesh.title.c_str());
433 if (status != NC_NOERR) {
434 errmsg = fmt::format("Error: failed to define title attribute to file id {}", exodusFilePtr);
435 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
436 return EX_FATAL;
437 }
438
439 // For use later to help readers know how much memory to allocate
440 // for name storage, we define an attribute containing the maximum
441 // size of any name.
442 {
443 int current_len = 0;
444 status = nc_put_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &current_len);
445 if (status != NC_NOERR) {
446 ex_opts(EX_VERBOSE);
447 errmsg = fmt::format("Error: failed to define ATT_MAX_NAME_LENGTH attribute to file id {}",
448 exodusFilePtr);
449 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
450 return EX_FATAL;
451 }
452 }
453
454 // create name string length dimension
455 if (maximumNameLength < 32) {
456 maximumNameLength = 32;
457 }
458 status = nc_def_dim(rootid, DIM_STR_NAME, maximumNameLength + 1, &namestrdim);
459 if (status != NC_NOERR) {
460 ex_opts(EX_VERBOSE);
461 errmsg =
462 fmt::format("Error: failed to define name string length in file id {}", exodusFilePtr);
463 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
464 return EX_FATAL;
465 }
466 }
467
468 // ...and some dimensions..
469 status = nc_def_dim(exodusFilePtr, DIM_NUM_DIM, mesh.dimensionality, &numdimdim);
470 if (status != NC_NOERR) {
471 ex_opts(EX_VERBOSE);
472 errmsg =
473 fmt::format("Error: failed to define number of dimensions in file id {}", exodusFilePtr);
474 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
475 return EX_FATAL;
476 }
477
478 if ((status = nc_def_dim(exodusFilePtr, DIM_TIME, NC_UNLIMITED, &timedim)) != NC_NOERR) {
479 errmsg = fmt::format("Error: failed to define time dimension in file id {}", exodusFilePtr);
480 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
481 return EX_FATAL;
482 }
483
484 {
485 std::array dim{timedim};
486 int varid = 0;
487 if ((status = nc_def_var(exodusFilePtr, VAR_WHOLE_TIME, nc_flt_code(exodusFilePtr), 1,
488 Data(dim), &varid)) != NC_NOERR) {
489 errmsg = fmt::format("Error: failed to define whole time step variable in file id {}",
490 exodusFilePtr);
491 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
492 return EX_FATAL;
493 }
494
495 struct exi_file_item *file = exi_find_file_item(exodusFilePtr);
496 if (file) {
497 file->time_varid = varid;
498 }
499
500 exi_compress_variable(exodusFilePtr, varid, -2); /* don't compress, but do set collective io */
501 }
502
503 if (mesh.nodeCount > 0) {
504 status = nc_def_dim(exodusFilePtr, DIM_NUM_NODES, mesh.nodeCount, &numnoddim);
505 if (status != NC_NOERR) {
506 ex_opts(EX_VERBOSE);
507 errmsg = fmt::format("Error: failed to define number of nodes in file id {}", exodusFilePtr);
508 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
509 return EX_FATAL;
510 }
511
512 // Define the node map here to avoid a later redefine call
513 if (mesh.needNodeMap) {
514 std::array dims{numnoddim};
515 int varid = 0;
516 status = nc_def_var(exodusFilePtr, VAR_NODE_NUM_MAP, map_type, 1, Data(dims), &varid);
517 if (status != NC_NOERR) {
518 ex_opts(EX_VERBOSE);
519 if (status == NC_ENAMEINUSE) {
520 errmsg =
521 fmt::format("Error: node numbering map already exists in file id {}", exodusFilePtr);
522 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
523 }
524 else {
525 errmsg = fmt::format("Error: failed to create node numbering map array in file id {}",
526 exodusFilePtr);
527 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
528 }
529 return EX_FATAL;
530 }
531 exi_compress_variable(exodusFilePtr, varid, 1);
532 }
533 }
534
535 if (mesh.elementCount > 0) {
536 int numelemdim = 0;
537 status = nc_def_dim(exodusFilePtr, DIM_NUM_ELEM, mesh.elementCount, &numelemdim);
538 if (status != NC_NOERR) {
539 ex_opts(EX_VERBOSE);
540 errmsg =
541 fmt::format("Error: failed to define number of elements in file id {}", exodusFilePtr);
542 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
543 return EX_FATAL;
544 }
545
546 // Define the element map here to avoid a later redefine call
547 if (mesh.needElementMap) {
548 std::array dims{numelemdim};
549 int varid = 0;
550 status = nc_def_var(exodusFilePtr, VAR_ELEM_NUM_MAP, map_type, 1, Data(dims), &varid);
551 if (status != NC_NOERR) {
552 ex_opts(EX_VERBOSE);
553 if (status == NC_ENAMEINUSE) {
554 errmsg = fmt::format("Error: element numbering map already exists in file id {}",
555 exodusFilePtr);
556 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
557 }
558 else {
559 errmsg = fmt::format("Error: failed to create element numbering map in file id {}",
560 exodusFilePtr);
561 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
562 }
563 return EX_FATAL;
564 }
565 exi_compress_variable(exodusFilePtr, varid, 1);
566 }
567 }
568
569 if (mesh.blockCount > 0) {
570 status = define_netcdf_vars(exodusFilePtr, "element block", mesh.blockCount, DIM_NUM_EL_BLK,
571 VAR_STAT_EL_BLK, VAR_ID_EL_BLK, VAR_NAME_EL_BLK);
572 if (status != EX_NOERR) {
573 return EX_FATAL;
574 }
575 }
576
577 // node set id array:
578 if (mesh.nodesetCount > 0) {
579 status = define_netcdf_vars(exodusFilePtr, "node set", mesh.nodesetCount, DIM_NUM_NS,
580 VAR_NS_STAT, VAR_NS_IDS, VAR_NAME_NS);
581 if (status != EX_NOERR) {
582 return EX_FATAL;
583 }
584 }
585
586 // side set id array:
587 if (mesh.sidesetCount > 0) {
588 status = define_netcdf_vars(exodusFilePtr, "side set", mesh.sidesetCount, DIM_NUM_SS,
589 VAR_SS_STAT, VAR_SS_IDS, VAR_NAME_SS);
590 if (status != EX_NOERR) {
591 return EX_FATAL;
592 }
593 }
594
595 // edge block id array:
596 if (mesh.edgeBlockCount > 0) {
597 status = define_netcdf_vars(exodusFilePtr, "edge block", mesh.edgeBlockCount, DIM_NUM_ED_BLK,
598 VAR_STAT_ED_BLK, VAR_ID_ED_BLK, VAR_NAME_ED_BLK);
599 if (status != EX_NOERR) {
600 return EX_FATAL;
601 }
602 }
603
604 // face block id array:
605 if (mesh.faceBlockCount > 0) {
606 status = define_netcdf_vars(exodusFilePtr, "face block", mesh.faceBlockCount, DIM_NUM_FA_BLK,
607 VAR_STAT_FA_BLK, VAR_ID_FA_BLK, VAR_NAME_FA_BLK);
608 if (status != EX_NOERR) {
609 return EX_FATAL;
610 }
611 }
612
613 status = define_coordinate_vars(exodusFilePtr, mesh.nodeCount, numnoddim, mesh.dimensionality,
614 numdimdim, namestrdim);
615 if (status != EX_NOERR) {
616 return EX_FATAL;
617 }
618
619 return EX_NOERR;
620 }
621
622 template <typename INT> int Excn::Internals<INT>::put_metadata(const std::vector<Block> &blocks)
623 {
624 std::string errmsg;
625
626 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
627
628 int status = 0; // clear error code
629
630 if (blocks.empty()) {
631 return EX_NOERR;
632 }
633
634 // Get number of element blocks defined for this file
635 int dimid;
636 size_t num_elem_blk = 0;
637 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_EL_BLK, &dimid);
638 if (status != NC_NOERR) {
639 ex_opts(EX_VERBOSE);
640 errmsg = fmt::format("Error: no element blocks defined in file id {}", exodusFilePtr);
641 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
642 return EX_FATAL;
643 }
644
645 int namestrdim;
646 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
647 if (status != NC_NOERR) {
648 ex_opts(EX_VERBOSE);
649 errmsg = fmt::format("Error: failed to get name string length in file id {}", exodusFilePtr);
650 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
651 return EX_FATAL;
652 }
653
654 status = nc_inq_dimlen(exodusFilePtr, dimid, &num_elem_blk);
655 if (status != NC_NOERR) {
656 ex_opts(EX_VERBOSE);
657 errmsg =
658 fmt::format("Error: failed to get number of element blocks in file id {}", exodusFilePtr);
659 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
660 return EX_FATAL;
661 }
662
663 SMART_ASSERT(blocks.size() == num_elem_blk);
664
665 // Iterate over element blocks ...
666 for (size_t iblk = 0; iblk < num_elem_blk; iblk++) {
667
668 exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_ELEM_BLOCK));
669
670 if (blocks[iblk].elementCount == 0) {
671 continue;
672 }
673
674 // define some dimensions and variables
675 int numelbdim;
676 status = nc_def_dim(exodusFilePtr, DIM_NUM_EL_IN_BLK(iblk + 1), blocks[iblk].elementCount,
677 &numelbdim);
678 if (status != NC_NOERR) {
679 if (status == NC_ENAMEINUSE) { // duplicate entry
680 ex_opts(EX_VERBOSE);
681 errmsg = fmt::format("Error: element block {} already defined in file id {}",
682 blocks[iblk].id, exodusFilePtr);
683 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
684 }
685 else {
686 ex_opts(EX_VERBOSE);
687 errmsg =
688 fmt::format("Error: failed to define number of elements/block for block {} file id {}",
689 blocks[iblk].id, exodusFilePtr);
690 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
691 }
692 return EX_FATAL;
693 }
694
695 int nelnoddim;
696 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_EL(iblk + 1), blocks[iblk].nodesPerElement,
697 &nelnoddim);
698 if (status != NC_NOERR) {
699 ex_opts(EX_VERBOSE);
700 errmsg =
701 fmt::format("Error: failed to define number of nodes/element for block {} in file id {}",
702 blocks[iblk].id, exodusFilePtr);
703 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
704 return EX_FATAL;
705 }
706
707 // element attribute array
708 if (blocks[iblk].attributeCount > 0) {
709 int numattrdim;
710 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_BLK(iblk + 1), blocks[iblk].attributeCount,
711 &numattrdim);
712 if (status != NC_NOERR) {
713 ex_opts(EX_VERBOSE);
714 errmsg =
715 fmt::format("Error: failed to define number of attributes in block {} in file id {}",
716 blocks[iblk].id, exodusFilePtr);
717 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
718 return EX_FATAL;
719 }
720
721 std::array dims{numelbdim, numattrdim};
722 int varid = 0;
723 status = nc_def_var(exodusFilePtr, VAR_ATTRIB(iblk + 1), nc_flt_code(exodusFilePtr), 2,
724 Data(dims), &varid);
725 if (status != NC_NOERR) {
726 ex_opts(EX_VERBOSE);
727 errmsg =
728 fmt::format("Error: failed to define attributes for element block {} in file id {}",
729 blocks[iblk].id, exodusFilePtr);
730 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
731 return EX_FATAL;
732 }
733 exi_compress_variable(exodusFilePtr, varid, 2);
734
735 // Attribute name array...
736 dims[0] = numattrdim;
737 dims[1] = namestrdim;
738
739 status = nc_def_var(exodusFilePtr, VAR_NAME_ATTRIB(iblk + 1), NC_CHAR, 2, Data(dims), &varid);
740 if (status != NC_NOERR) {
741 ex_opts(EX_VERBOSE);
742 errmsg = fmt::format(
743 "Error: failed to define attribute name array for element block {} in file id {}",
744 blocks[iblk].id, exodusFilePtr);
745 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
746 return EX_FATAL;
747 }
748 }
749
750 // element connectivity array
751 std::array dims{numelbdim, nelnoddim};
752
753 int connid;
754 status = nc_def_var(exodusFilePtr, VAR_CONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
755 if (status != NC_NOERR) {
756 ex_opts(EX_VERBOSE);
757 errmsg = fmt::format("Error: failed to create connectivity array for block {} in file id {}",
758 blocks[iblk].id, exodusFilePtr);
759 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
760 return EX_FATAL;
761 }
762 exi_compress_variable(exodusFilePtr, connid, 1);
763
764 // store element type as attribute of connectivity variable
765 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
766 static_cast<int>(std::strlen(blocks[iblk].elType)) + 1,
767 blocks[iblk].elType);
768 if (status != NC_NOERR) {
769 ex_opts(EX_VERBOSE);
770 errmsg = fmt::format("Error: failed to store element type name {} in file id {}",
771 blocks[iblk].elType, exodusFilePtr);
772 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
773 return EX_FATAL;
774 }
775 }
776 return EX_NOERR;
777 }
778
779 template <typename INT>
780 int Excn::Internals<INT>::put_non_define_data(const Mesh & /*unused*/,
781 const CommunicationMetaData & /*unused*/)
782 {
783 return EX_NOERR;
784 }
785
786 template <typename INT>
787 int Excn::Internals<INT>::put_non_define_data(const std::vector<Block> &blocks)
788 {
789 int num_elem_blk = blocks.size(); // Verified via SMART_ASSERT earlier...
790
791 if (num_elem_blk > 0) {
792 // first get id of element block ids array variable
793 std::vector<ex_entity_id> elem_blk_id(num_elem_blk);
794 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
795 elem_blk_id[iblk] = blocks[iblk].id;
796 }
797
798 if (put_id_array(exodusFilePtr, VAR_ID_EL_BLK, elem_blk_id) != NC_NOERR) {
799 return EX_FATAL;
800 }
801
802 // Now, write the element block status array
803 std::vector<int> elem_blk_status(num_elem_blk);
804 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
805 elem_blk_status[iblk] = blocks[iblk].elementCount > 0 ? 1 : 0;
806 }
807
808 if (put_array(exodusFilePtr, VAR_STAT_EL_BLK, elem_blk_status) != NC_NOERR) {
809 return EX_FATAL;
810 }
811 }
812 return EX_NOERR;
813 }
814
815 // ========================================================================
816 template <typename INT>
817 int Excn::Internals<INT>::put_metadata(const std::vector<NodeSet<INT>> &nodesets)
818 {
819 if (nodesets.empty()) {
820 return EX_NOERR;
821 }
822
823 std::string errmsg;
824
825 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
826
827 int status = 0; // clear error code
828
829 // Get number of node sets defined for this file
830 int dimid;
831 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_NS, &dimid);
832 if (status != NC_NOERR) {
833 ex_opts(EX_VERBOSE);
834 if (status == NC_EBADDIM) {
835 errmsg = fmt::format("Error: no node sets defined for file id {}", exodusFilePtr);
836 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
837 }
838 else {
839 errmsg =
840 fmt::format("Error: failed to locate node sets defined in file id {}", exodusFilePtr);
841 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
842 }
843 return EX_FATAL;
844 }
845
846 // inquire how many node sets are to be stored
847 int num_node_sets = ex_inquire_int(exodusFilePtr, EX_INQ_NODE_SETS);
848 SMART_ASSERT(static_cast<int>(nodesets.size()) == num_node_sets);
849
850 for (int i = 0; i < num_node_sets; i++) {
851
852 // NOTE: exi_inc_file_item is used to find the number of node sets
853 // for a specific file and returns that value incremented.
854 int cur_num_node_sets =
855 (int)exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_NODE_SET));
856
857 if (nodesets[i].nodeCount == 0) {
858 continue;
859 }
860
861 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_NS(cur_num_node_sets + 1), nodesets[i].nodeCount,
862 &dimid);
863 if (status != NC_NOERR) {
864 ex_opts(EX_VERBOSE);
865 if (status == NC_ENAMEINUSE) {
866 errmsg = fmt::format("Error: node set {} already defined in file id {}", nodesets[i].id,
867 exodusFilePtr);
868 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
869 }
870 else {
871 errmsg = fmt::format("Error: failed to define number of nodes for set {} in file id {}",
872 nodesets[i].id, exodusFilePtr);
873 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
874 }
875 return EX_FATAL;
876 }
877
878 // define variable to store node set node list here instead of in expns
879 std::array dims{dimid};
880 int varid = 0;
881 status = nc_def_var(exodusFilePtr, VAR_NODE_NS(cur_num_node_sets + 1), bulk_type, 1, Data(dims),
882 &varid);
883 if (status != NC_NOERR) {
884 ex_opts(EX_VERBOSE);
885 if (status == NC_ENAMEINUSE) {
886 errmsg = fmt::format("Error: node set {} node list already defined in file id {}",
887 nodesets[i].id, exodusFilePtr);
888 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
889 }
890 else {
891 errmsg = fmt::format("Error: failed to create node set {} node list in file id {}",
892 nodesets[i].id, exodusFilePtr);
893 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
894 }
895 return EX_FATAL;
896 }
897 exi_compress_variable(exodusFilePtr, varid, 1);
898
899 // Create variable for distribution factors if required
900 if (nodesets[i].dfCount > 0) {
901 // num_dist_per_set should equal num_nodes_per_set
902 if (nodesets[i].dfCount != nodesets[i].nodeCount) {
903 status = EX_FATAL;
904 ex_opts(EX_VERBOSE);
905 errmsg =
906 fmt::format("Error: # dist fact ({}) not equal to # nodes ({}) "
907 "in node set {} file id {}",
908 nodesets[i].dfCount, nodesets[i].nodeCount, nodesets[i].id, exodusFilePtr);
909 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
910 return EX_FATAL;
911 }
912
913 // create variable for distribution factors
914 status = nc_def_var(exodusFilePtr, VAR_FACT_NS(cur_num_node_sets + 1),
915 nc_flt_code(exodusFilePtr), 1, Data(dims), &varid);
916 if (status != NC_NOERR) {
917 ex_opts(EX_VERBOSE);
918 if (status == NC_ENAMEINUSE) {
919 errmsg = fmt::format("Error: node set {} dist factors already exist in file id {}",
920 nodesets[i].id, exodusFilePtr);
921 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
922 }
923 else {
924 errmsg = fmt::format("Error: failed to create node set {} dist factors in file id {}",
925 nodesets[i].id, exodusFilePtr);
926 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
927 }
928 return EX_FATAL;
929 }
930 exi_compress_variable(exodusFilePtr, varid, 2);
931 }
932 }
933 return EX_NOERR;
934 }
935
936 template <typename INT>
937 int Excn::Internals<INT>::put_non_define_data(const std::vector<NodeSet<INT>> &nodesets)
938 {
939 if (nodesets.empty()) {
940 return EX_NOERR;
941 }
942
943 // Output nodeset ids...
944 size_t num_nodesets = nodesets.size();
945 std::vector<ex_entity_id> nodeset_id(num_nodesets);
946 for (size_t i = 0; i < num_nodesets; i++) {
947 nodeset_id[i] = nodesets[i].id;
948 }
949
950 if (put_id_array(exodusFilePtr, VAR_NS_IDS, nodeset_id) != NC_NOERR) {
951 return EX_FATAL;
952 }
953
954 // Now, write the status array
955 std::vector<int> status(num_nodesets);
956 for (size_t i = 0; i < num_nodesets; i++) {
957 status[i] = nodesets[i].nodeCount > 0 ? 1 : 0;
958 }
959
960 if (put_array(exodusFilePtr, VAR_NS_STAT, status) != NC_NOERR) {
961 return EX_FATAL;
962 }
963
964 return EX_NOERR;
965 }
966
967 // ========================================================================
968 template <typename INT>
969 int Excn::Internals<INT>::put_metadata(const std::vector<SideSet<INT>> &sidesets)
970 {
971 if (sidesets.empty()) {
972 return EX_NOERR;
973 }
974
975 std::string errmsg;
976
977 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
978
979 int status = 0; // clear error code
980
981 // Get number of side sets defined for this file
982 int dimid;
983 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_SS, &dimid);
984 if (status != NC_NOERR) {
985 ex_opts(EX_VERBOSE);
986 if (status == NC_EBADDIM) {
987 errmsg = fmt::format("Error: no side sets defined for file id {}", exodusFilePtr);
988 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
989 }
990 else {
991 errmsg =
992 fmt::format("Error: failed to locate side sets defined in file id {}", exodusFilePtr);
993 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
994 }
995 return EX_FATAL;
996 }
997
998 size_t num_side_sets = ex_inquire_int(exodusFilePtr, EX_INQ_SIDE_SETS);
999 SMART_ASSERT(sidesets.size() == num_side_sets);
1000
1001 for (size_t i = 0; i < num_side_sets; i++) {
1002
1003 // NOTE: exi_inc_file_item is used to find the number of side sets
1004 // for a specific file and returns that value incremented.
1005 int cur_num_side_sets =
1006 (int)exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_SIDE_SET));
1007
1008 if (sidesets[i].sideCount == 0) {
1009 continue;
1010 }
1011
1012 status = nc_def_dim(exodusFilePtr, DIM_NUM_SIDE_SS(cur_num_side_sets + 1),
1013 sidesets[i].sideCount, &dimid);
1014 if (status != NC_NOERR) {
1015 ex_opts(EX_VERBOSE);
1016 if (status == NC_ENAMEINUSE) {
1017 errmsg = fmt::format("Error: side set {} already defined in file id {}", sidesets[i].id,
1018 exodusFilePtr);
1019 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1020 }
1021 else {
1022 errmsg = fmt::format("Error: failed to define number of sides for set {} in file id {}",
1023 sidesets[i].id, exodusFilePtr);
1024 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1025 }
1026 return EX_FATAL;
1027 }
1028
1029 std::array dims{dimid};
1030 int varid = 0;
1031 status = nc_def_var(exodusFilePtr, VAR_ELEM_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims),
1032 &varid);
1033 if (status != NC_NOERR) {
1034 ex_opts(EX_VERBOSE);
1035 if (status == NC_ENAMEINUSE) {
1036 errmsg = fmt::format("Error: side set {} element list already defined in file id {}",
1037 sidesets[i].id, exodusFilePtr);
1038 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1039 }
1040 else {
1041 errmsg = fmt::format("Error: failed to create side set {} element list in file id {}",
1042 sidesets[i].id, exodusFilePtr);
1043 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1044 }
1045 return EX_FATAL;
1046 }
1047 exi_compress_variable(exodusFilePtr, varid, 1);
1048
1049 // create side list variable for side set
1050 status = nc_def_var(exodusFilePtr, VAR_SIDE_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims),
1051 &varid);
1052 if (status != NC_NOERR) {
1053 ex_opts(EX_VERBOSE);
1054 if (status == NC_ENAMEINUSE) {
1055 errmsg = fmt::format("Error: side list already exists for side set {} in file id {}",
1056 sidesets[i].id, exodusFilePtr);
1057 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1058 }
1059 else {
1060 errmsg = fmt::format("Error: failed to create side list for side set {} in file id {}",
1061 sidesets[i].id, exodusFilePtr);
1062 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1063 }
1064 return EX_FATAL;
1065 }
1066 exi_compress_variable(exodusFilePtr, varid, 1);
1067
1068 // Create variable for distribution factors if required
1069 if (sidesets[i].dfCount > 0) {
1070 status = nc_def_dim(exodusFilePtr, DIM_NUM_DF_SS(cur_num_side_sets + 1), sidesets[i].dfCount,
1071 &dimid);
1072 if (status != NC_NOERR) {
1073 ex_opts(EX_VERBOSE);
1074 if (status == NC_ENAMEINUSE) {
1075 errmsg = fmt::format("Error: side set df count {} already defined in file id {}",
1076 sidesets[i].id, exodusFilePtr);
1077 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1078 }
1079 else {
1080 errmsg = fmt::format("Error: failed to define side set df count for set {} in file id {}",
1081 sidesets[i].id, exodusFilePtr);
1082 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1083 }
1084 return EX_FATAL;
1085 }
1086
1087 // create distribution factor list variable for side set
1088 dims[0] = dimid;
1089 status = nc_def_var(exodusFilePtr, VAR_FACT_SS(cur_num_side_sets + 1),
1090 nc_flt_code(exodusFilePtr), 1, Data(dims), &varid);
1091 if (status != NC_NOERR) {
1092 ex_opts(EX_VERBOSE);
1093 if (status == NC_ENAMEINUSE) {
1094 errmsg =
1095 fmt::format("Error: dist factor list already exists for side set {} in file id {}",
1096 sidesets[i].id, exodusFilePtr);
1097 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1098 }
1099 else {
1100 errmsg =
1101 fmt::format("Error: failed to create dist factor list for side set {} in file id {}",
1102 sidesets[i].id, exodusFilePtr);
1103 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1104 }
1105 return EX_FATAL;
1106 }
1107 exi_compress_variable(exodusFilePtr, varid, 2);
1108 }
1109 }
1110 return EX_NOERR;
1111 }
1112
1113 template <typename INT>
1114 int Excn::Internals<INT>::put_non_define_data(const std::vector<SideSet<INT>> &sidesets)
1115 {
1116 if (sidesets.empty()) {
1117 return EX_NOERR;
1118 }
1119
1120 // Output sideset ids...
1121 int num_sidesets = (int)sidesets.size();
1122 std::vector<ex_entity_id> sideset_id(num_sidesets);
1123 for (int i = 0; i < num_sidesets; i++) {
1124 sideset_id[i] = sidesets[i].id;
1125 }
1126
1127 if (put_id_array(exodusFilePtr, VAR_SS_IDS, sideset_id) != NC_NOERR) {
1128 return EX_FATAL;
1129 }
1130
1131 // Now, write the status array
1132 std::vector<int> status(num_sidesets);
1133 for (int i = 0; i < num_sidesets; i++) {
1134 status[i] = sidesets[i].sideCount > 0 ? 1 : 0;
1135 }
1136
1137 if (put_array(exodusFilePtr, VAR_SS_STAT, status) != NC_NOERR) {
1138 return EX_FATAL;
1139 }
1140
1141 return EX_NOERR;
1142 }
1143
1144 // ========================================================================
1145 template <typename INT>
1146 int Excn::Internals<INT>::put_metadata(const std::vector<EdgeBlock<INT>> &edgeblocks)
1147 {
1148 if (edgeblocks.empty()) {
1149 return EX_NOERR;
1150 }
1151
1152 std::string errmsg;
1153
1154 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
1155
1156 int status = 0; // clear error code
1157
1158 // Get number of edge blocks defined for this file
1159 int dimid;
1160 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_ED_BLK, &dimid);
1161 if (status != NC_NOERR) {
1162 ex_opts(EX_VERBOSE);
1163 if (status == NC_EBADDIM) {
1164 errmsg = fmt::format("Error: no edge blocks defined for file id {}", exodusFilePtr);
1165 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1166 }
1167 else {
1168 errmsg =
1169 fmt::format("Error: failed to locate edge blocks defined in file id {}", exodusFilePtr);
1170 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1171 }
1172 return EX_FATAL;
1173 }
1174
1175 size_t num_edge_blocks = ex_inquire_int(exodusFilePtr, EX_INQ_EDGE_BLK);
1176 SMART_ASSERT(edgeblocks.size() == num_edge_blocks);
1177
1178 for (size_t i = 0; i < num_edge_blocks; i++) {
1179
1180 // NOTE: exi_inc_file_item is used to find the number of edge blocks
1181 // for a specific file and returns that value incremented.
1182 int cur_num_edge_blocks =
1183 (int)exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_EDGE_BLOCK));
1184
1185 if (edgeblocks[i].edgeCount == 0) {
1186 continue;
1187 }
1188
1189 int num_edges_in_edgeblock_dim;
1190 status = nc_def_dim(exodusFilePtr, DIM_NUM_ED_IN_EBLK(cur_num_edge_blocks + 1),
1191 edgeblocks[i].edgeCount, &num_edges_in_edgeblock_dim);
1192 if (status != NC_NOERR) {
1193 ex_opts(EX_VERBOSE);
1194 if (status == NC_ENAMEINUSE) {
1195 errmsg = fmt::format("Error: edge block {} already defined in file id {}", edgeblocks[i].id,
1196 exodusFilePtr);
1197 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1198 }
1199 else {
1200 errmsg =
1201 fmt::format("Error: failed to define number of edges for edge block {} in file id {}",
1202 edgeblocks[i].id, exodusFilePtr);
1203 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1204 }
1205 return EX_FATAL;
1206 }
1207
1208 int num_nodes_per_edge_dim;
1209 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_ED(cur_num_edge_blocks + 1),
1210 edgeblocks[i].nodesPerEdge, &num_nodes_per_edge_dim);
1211 if (status != NC_NOERR) {
1212 ex_opts(EX_VERBOSE);
1213 errmsg =
1214 fmt::format("Error: failed to define number of nodes/edge for block {} in file id {}",
1215 edgeblocks[i].id, exodusFilePtr);
1216 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1217 return EX_FATAL;
1218 }
1219
1220 // element connectivity array
1221 std::array dims{num_edges_in_edgeblock_dim, num_nodes_per_edge_dim};
1222 int connid;
1223 status = nc_def_var(exodusFilePtr, VAR_EBCONN(cur_num_edge_blocks + 1), bulk_type, 2,
1224 Data(dims), &connid);
1225 if (status != NC_NOERR) {
1226 ex_opts(EX_VERBOSE);
1227 errmsg =
1228 fmt::format("Error: failed to create connectivity array for edge block {} in file id {}",
1229 edgeblocks[i].id, exodusFilePtr);
1230 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1231 return EX_FATAL;
1232 }
1233 exi_compress_variable(exodusFilePtr, connid, 1);
1234
1235 // store edge type as attribute of connectivity variable
1236 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
1237 static_cast<int>(std::strlen(edgeblocks[i].elType)) + 1,
1238 edgeblocks[i].elType);
1239 if (status != NC_NOERR) {
1240 ex_opts(EX_VERBOSE);
1241 errmsg = fmt::format("Error: failed to store edge type name {} in file id {}",
1242 edgeblocks[i].elType, exodusFilePtr);
1243 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1244 return EX_FATAL;
1245 }
1246 }
1247 return EX_NOERR;
1248 }
1249
1250 template <typename INT>
1251 int Excn::Internals<INT>::put_non_define_data(const std::vector<EdgeBlock<INT>> &edgeblocks)
1252 {
1253 if (edgeblocks.empty()) {
1254 return EX_NOERR;
1255 }
1256
1257 // Output edgeblock ids...
1258 int num_edgeblocks = (int)edgeblocks.size();
1259 std::vector<ex_entity_id> edgeblock_id(num_edgeblocks);
1260 for (int i = 0; i < num_edgeblocks; i++) {
1261 edgeblock_id[i] = edgeblocks[i].id;
1262 }
1263
1264 if (put_id_array(exodusFilePtr, VAR_ID_ED_BLK, edgeblock_id) != NC_NOERR) {
1265 return EX_FATAL;
1266 }
1267
1268 // Now, write the status array
1269 std::vector<int> status(num_edgeblocks);
1270 for (int i = 0; i < num_edgeblocks; i++) {
1271 status[i] = edgeblocks[i].edgeCount > 0 ? 1 : 0;
1272 }
1273
1274 if (put_array(exodusFilePtr, VAR_STAT_ED_BLK, status) != NC_NOERR) {
1275 return EX_FATAL;
1276 }
1277
1278 return EX_NOERR;
1279 }
1280
1281 // ========================================================================
1282 template <typename INT>
1283 int Excn::Internals<INT>::put_metadata(const std::vector<FaceBlock<INT>> &faceblocks)
1284 {
1285 if (faceblocks.empty()) {
1286 return EX_NOERR;
1287 }
1288
1289 std::string errmsg;
1290
1291 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
1292
1293 int status = 0; // clear error code
1294
1295 // Get number of face blocks defined for this file
1296 int dimid;
1297 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_FA_BLK, &dimid);
1298 if (status != NC_NOERR) {
1299 ex_opts(EX_VERBOSE);
1300 if (status == NC_EBADDIM) {
1301 errmsg = fmt::format("Error: no face blocks defined for file id {}", exodusFilePtr);
1302 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1303 }
1304 else {
1305 errmsg =
1306 fmt::format("Error: failed to locate face blocks defined in file id {}", exodusFilePtr);
1307 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1308 }
1309 return EX_FATAL;
1310 }
1311
1312 size_t num_face_blocks = ex_inquire_int(exodusFilePtr, EX_INQ_FACE_BLK);
1313 SMART_ASSERT(faceblocks.size() == num_face_blocks);
1314
1315 for (size_t i = 0; i < num_face_blocks; i++) {
1316
1317 // NOTE: exi_inc_file_item is used to find the number of face blocks
1318 // for a specific file and returns that value incremented.
1319 int cur_num_face_blocks =
1320 (int)exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_FACE_BLOCK));
1321
1322 if (faceblocks[i].faceCount == 0) {
1323 continue;
1324 }
1325
1326 int num_faces_in_faceblock_dim;
1327 status = nc_def_dim(exodusFilePtr, DIM_NUM_FA_IN_FBLK(cur_num_face_blocks + 1),
1328 faceblocks[i].faceCount, &num_faces_in_faceblock_dim);
1329 if (status != NC_NOERR) {
1330 ex_opts(EX_VERBOSE);
1331 if (status == NC_ENAMEINUSE) {
1332 errmsg = fmt::format("Error: face block {} already defined in file id {}", faceblocks[i].id,
1333 exodusFilePtr);
1334 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1335 }
1336 else {
1337 errmsg =
1338 fmt::format("Error: failed to define number of faces for face block {} in file id {}",
1339 faceblocks[i].id, exodusFilePtr);
1340 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1341 }
1342 return EX_FATAL;
1343 }
1344
1345 int num_nodes_per_face_dim;
1346 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_FA(cur_num_face_blocks + 1),
1347 faceblocks[i].nodesPerFace, &num_nodes_per_face_dim);
1348 if (status != NC_NOERR) {
1349 ex_opts(EX_VERBOSE);
1350 errmsg =
1351 fmt::format("Error: failed to define number of nodes/face for block {} in file id {}",
1352 faceblocks[i].id, exodusFilePtr);
1353 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1354 return EX_FATAL;
1355 }
1356
1357 // element connectivity array
1358 std::array dims{num_faces_in_faceblock_dim, num_nodes_per_face_dim};
1359
1360 int connid;
1361 status = nc_def_var(exodusFilePtr, VAR_FBCONN(cur_num_face_blocks + 1), bulk_type, 2,
1362 Data(dims), &connid);
1363 if (status != NC_NOERR) {
1364 ex_opts(EX_VERBOSE);
1365 errmsg =
1366 fmt::format("Error: failed to create connectivity array for face block {} in file id {}",
1367 faceblocks[i].id, exodusFilePtr);
1368 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1369 return EX_FATAL;
1370 }
1371 exi_compress_variable(exodusFilePtr, connid, 1);
1372
1373 // store face type as attribute of connectivity variable
1374 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
1375 static_cast<int>(std::strlen(faceblocks[i].elType)) + 1,
1376 faceblocks[i].elType);
1377 if (status != NC_NOERR) {
1378 ex_opts(EX_VERBOSE);
1379 errmsg = fmt::format("Error: failed to store face type name {} in file id {}",
1380 faceblocks[i].elType, exodusFilePtr);
1381 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1382 return EX_FATAL;
1383 }
1384 }
1385 return EX_NOERR;
1386 }
1387
1388 template <typename INT>
1389 int Excn::Internals<INT>::put_non_define_data(const std::vector<FaceBlock<INT>> &faceblocks)
1390 {
1391 if (faceblocks.empty()) {
1392 return EX_NOERR;
1393 }
1394
1395 // Output faceblock ids...
1396 int num_faceblocks = (int)faceblocks.size();
1397 std::vector<ex_entity_id> faceblock_id(num_faceblocks);
1398 for (int i = 0; i < num_faceblocks; i++) {
1399 faceblock_id[i] = faceblocks[i].id;
1400 }
1401
1402 if (put_id_array(exodusFilePtr, VAR_ID_FA_BLK, faceblock_id) != NC_NOERR) {
1403 return EX_FATAL;
1404 }
1405
1406 // Now, write the status array
1407 std::vector<int> status(num_faceblocks);
1408 for (int i = 0; i < num_faceblocks; i++) {
1409 status[i] = faceblocks[i].faceCount > 0 ? 1 : 0;
1410 }
1411
1412 if (put_array(exodusFilePtr, VAR_STAT_FA_BLK, status) != NC_NOERR) {
1413 return EX_FATAL;
1414 }
1415
1416 return EX_NOERR;
1417 }
1418
1419 namespace {
1420 template <typename INT>
1421 int put_array(int exoid, const char *var_type, const std::vector<INT> &array)
1422 {
1423 std::string errmsg;
1424 int var_id;
1425 int status;
1426
1427 status = nc_inq_varid(exoid, var_type, &var_id);
1428 if (status != NC_NOERR) {
1429 ex_opts(EX_VERBOSE);
1430 errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid);
1431 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1432 return EX_FATAL;
1433 }
1434
1435 if (sizeof(INT) == sizeof(int64_t)) {
1436 status = nc_put_var_longlong(exoid, var_id, (long long int *)Data(array));
1437 }
1438 else {
1439 status = nc_put_var_int(exoid, var_id, Data(array));
1440 }
1441
1442 if (status != NC_NOERR) {
1443 ex_opts(EX_VERBOSE);
1444 errmsg = fmt::format("Error: failed to write {} array in file id {}", var_type, exoid);
1445 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1446 return EX_FATAL;
1447 }
1448 return EX_NOERR;
1449 }
1450
1451 int put_id_array(int exoid, const char *var_type, const std::vector<ex_entity_id> &ids)
1452 {
1453 std::string errmsg;
1454 int var_id;
1455
1456 int status = nc_inq_varid(exoid, var_type, &var_id);
1457 if (status != NC_NOERR) {
1458 ex_opts(EX_VERBOSE);
1459 errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid);
1460 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1461 return EX_FATAL;
1462 }
1463
1464 int id_type = get_type(exoid, EX_IDS_INT64_API);
1465
1466 if (id_type == NC_INT64) {
1467 status = nc_put_var_longlong(exoid, var_id, (long long int *)Data(ids));
1468 }
1469 else {
1470 // Have ex_entity_id (long long), need ints...
1471 std::vector<int> int_ids(ids.size());
1472 #ifdef _MSC_VER
1473 #pragma warning(push)
1474 #pragma warning(disable : 4244)
1475 #endif
1476 int_ids.assign(ids.begin(), ids.end());
1477 #ifdef _MSC_VER
1478 #pragma warning(pop)
1479 #endif
1480 status = nc_put_var_int(exoid, var_id, Data(int_ids));
1481 }
1482
1483 if (status != NC_NOERR) {
1484 ex_opts(EX_VERBOSE);
1485 errmsg = fmt::format("Error: failed to write {} array in file id {}", var_type, exoid);
1486 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1487 return EX_FATAL;
1488 }
1489 return EX_NOERR;
1490 }
1491
1492 int define_coordinate_vars(int exodusFilePtr, int64_t nodes, int node_dim, int dimension,
1493 int dim_dim, int str_dim)
1494 {
1495 std::string errmsg;
1496 int status;
1497 int varid;
1498
1499 if (nodes > 0) {
1500 // node coordinate arrays -- separate storage...
1501 std::array dim{node_dim};
1502 if (dimension > 0) {
1503 status = nc_def_var(exodusFilePtr, VAR_COORD_X, nc_flt_code(exodusFilePtr), 1, Data(dim),
1504 &varid);
1505 if (status != NC_NOERR) {
1506 ex_opts(EX_VERBOSE);
1507 errmsg = fmt::format("Error: failed to define node x coordinate array in file id {}",
1508 exodusFilePtr);
1509 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1510 return EX_FATAL;
1511 }
1512 exi_compress_variable(exodusFilePtr, varid, 2);
1513 }
1514
1515 if (dimension > 1) {
1516 status = nc_def_var(exodusFilePtr, VAR_COORD_Y, nc_flt_code(exodusFilePtr), 1, Data(dim),
1517 &varid);
1518 if (status != NC_NOERR) {
1519 ex_opts(EX_VERBOSE);
1520 errmsg = fmt::format("Error: failed to define node y coordinate array in file id {}",
1521 exodusFilePtr);
1522 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1523 return EX_FATAL;
1524 }
1525 exi_compress_variable(exodusFilePtr, varid, 2);
1526 }
1527
1528 if (dimension > 2) {
1529 status = nc_def_var(exodusFilePtr, VAR_COORD_Z, nc_flt_code(exodusFilePtr), 1, Data(dim),
1530 &varid);
1531 if (status != NC_NOERR) {
1532 ex_opts(EX_VERBOSE);
1533 errmsg = fmt::format("Error: failed to define node z coordinate array in file id {}",
1534 exodusFilePtr);
1535 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1536 return EX_FATAL;
1537 }
1538 exi_compress_variable(exodusFilePtr, varid, 2);
1539 }
1540 }
1541
1542 // coordinate names array
1543 std::array dims{dim_dim, str_dim};
1544 status = nc_def_var(exodusFilePtr, VAR_NAME_COOR, NC_CHAR, 2, Data(dims), &varid);
1545 if (status != NC_NOERR) {
1546 ex_opts(EX_VERBOSE);
1547 errmsg =
1548 fmt::format("Error: failed to define coordinate name array in file id {}", exodusFilePtr);
1549 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1550 return EX_FATAL;
1551 }
1552 return EX_NOERR;
1553 }
1554
1555 int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num,
1556 const char *stat_var, const char *id_var, const char *name_var)
1557 {
1558 int dimid = 0;
1559 int varid = 0;
1560 int namestrdim = 0;
1561 std::string errmsg;
1562
1563 int status = nc_inq_dimid(exoid, DIM_STR_NAME, &namestrdim);
1564 if (status != NC_NOERR) {
1565 ex_opts(EX_VERBOSE);
1566 errmsg = fmt::format("Error: failed to get string name dimension in file id {}", exoid);
1567 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1568 return EX_FATAL;
1569 }
1570
1571 status = nc_def_dim(exoid, dim_num, count, &dimid);
1572 if (status != NC_NOERR) {
1573 ex_opts(EX_VERBOSE);
1574 errmsg = fmt::format("Error: failed to define number of {}s in file id {}", type, exoid);
1575 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1576 return EX_FATAL;
1577 }
1578
1579 // id status array:
1580 std::array dim{dimid};
1581 status = nc_def_var(exoid, stat_var, NC_INT, 1, Data(dim), &varid);
1582 if (status != NC_NOERR) {
1583 ex_opts(EX_VERBOSE);
1584 errmsg = fmt::format("Error: failed to define side {} status in file id {}", type, exoid);
1585 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1586 return EX_FATAL;
1587 }
1588
1589 // id array:
1590 int ids_type = get_type(exoid, EX_IDS_INT64_DB);
1591 status = nc_def_var(exoid, id_var, ids_type, 1, Data(dim), &varid);
1592 if (status != NC_NOERR) {
1593 ex_opts(EX_VERBOSE);
1594 errmsg = fmt::format("Error: failed to define {} property in file id {}", type, exoid);
1595 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1596 return EX_FATAL;
1597 }
1598
1599 // store property name as attribute of property array variable
1600 status = nc_put_att_text(exoid, varid, ATT_PROP_NAME, 3, "ID");
1601 if (status != NC_NOERR) {
1602 ex_opts(EX_VERBOSE);
1603 errmsg = fmt::format("Error: failed to store {} property name {} in file id {}", type, "ID",
1604 exoid);
1605 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1606 return EX_FATAL;
1607 }
1608
1609 std::array dims{dimid, namestrdim};
1610 status = nc_def_var(exoid, name_var, NC_CHAR, 2, Data(dims), &varid);
1611 if (status != NC_NOERR) {
1612 ex_opts(EX_VERBOSE);
1613 errmsg = fmt::format("Error: failed to define {} name array in file id {}", type, exoid);
1614 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
1615 return EX_FATAL;
1616 }
1617 return EX_NOERR;
1618 }
1619 } // namespace