Debian Codegraph @

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

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "exodus/Ioex_Internals.h" // for Internals, ElemBlock, etc
8 #include "exodus/Ioex_Utils.h"
9 #include <fmt/core.h>
10
11 #include "Ioss_ElementTopology.h"
12 #include "Ioss_GroupingEntity.h"
13 #include "exodusII.h" // for ex_err, ex_opts, etc
14
15 extern "C" {
16 #include <exodusII_int.h>
17 }
18
19 #include <array>
20 #include <cstdlib> // for exit, EXIT_FAILURE
21 #include <cstring> // for strlen
22 #include <netcdf.h> // for NC_NOERR, nc_def_var, etc
23 #include <string> // for string, operator==, etc
24 #include <vector> // for vector
25
26 #include "Ioss_Assembly.h"
27 #include "Ioss_Blob.h"
28 #include "Ioss_EdgeBlock.h"
29 #include "Ioss_EdgeSet.h"
30 #include "Ioss_ElementBlock.h"
31 #include "Ioss_ElementSet.h"
32 #include "Ioss_FaceBlock.h"
33 #include "Ioss_FaceSet.h"
34 #include "Ioss_Field.h"
35 #include "Ioss_NodeBlock.h"
36 #include "Ioss_NodeSet.h"
37 #include "Ioss_Property.h"
38 #include "Ioss_Region.h"
39 #include "Ioss_SideBlock.h"
40 #include "Ioss_SideSet.h"
41 #include "Ioss_SmartAssert.h"
42 #include "Ioss_Utils.h"
43 #include "Ioss_VariableType.h"
44
45 using namespace Ioex;
46
47 namespace {
48 nc_type get_type(int exoid, unsigned int type)
49 {
50 if ((ex_int64_status(exoid) & type) != 0U) {
51 return NC_INT64;
52 }
53
54 return NC_INT;
55 }
56 int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num,
57 const char *stat_var, const char *id_var, const char *name_var);
58 int define_variable(int exodusFilePtr, int64_t size, const char *dim, const char *var,
59 nc_type type);
60 int define_variables(int exodusFilePtr, int64_t size, const char *dim, const char *var[],
61 const nc_type *types);
62 int conditional_define_variable(int exodusFilePtr, const char *var, int dimid, int *varid,
63 nc_type type);
64
65 int put_int_array(int exoid, const char *var_type, const std::vector<int> &array);
66 int put_int_array(int exoid, const char *var_type, const std::vector<int64_t> &array);
67
68 int put_id_array(int exoid, const char *var_type, const std::vector<entity_id> &ids);
69 int define_coordinate_vars(int exodusFilePtr, int64_t nodes, int node_dim, int dimension,
70 int dim_dim, int str_dim);
71 template <typename T>
72 int output_names(const std::vector<T> &entities, int exoid, ex_entity_type ent_type);
73 template <typename T> int get_max_name_length(const std::vector<T> &entities, int old_max);
74 } // namespace
75
76 Redefine::Redefine(int exoid) : exodusFilePtr(exoid)
77 {
78 // Enter define mode...
79 int status = nc_redef(exodusFilePtr);
80 if (status != NC_NOERR) {
81 ex_opts(EX_VERBOSE);
82 std::string errmsg =
83 fmt::format("Error: failed to put file id {} into define mode", exodusFilePtr);
84 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
85 exit(EXIT_FAILURE);
86 }
87 }
88
89 Redefine::~Redefine()
90 {
91 try {
92 int status = nc_enddef(exodusFilePtr);
93 if (status != NC_NOERR) {
94 ex_opts(EX_VERBOSE);
95 std::string errmsg = fmt::format(
96 "Error: failed to complete variable definitions in file id {}", exodusFilePtr);
97 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
98 exit(EXIT_FAILURE);
99 }
100 }
101 catch (...) {
102 }
103 }
104
105 Assembly::Assembly(const Ioss::Assembly &other)
106 {
107 if (other.property_exists("db_name")) {
108 name = other.get_property("db_name").get_string();
109 }
110 else {
111 name = other.name();
112 }
113
114 id = other.get_optional_property("id", 1);
115 entityCount = other.member_count();
116 attributeCount = other.get_property("attribute_count").get_int();
117 type = Ioex::map_exodus_type(other.get_member_type());
118
119 const auto &members = other.get_members();
120 for (const auto &member : members) {
121 SMART_ASSERT(member->property_exists("id"));
122 memberIdList.push_back(member->get_property("id").get_int());
123 }
124 }
125
126 Blob::Blob(const Ioss::Blob &other)
127 {
128 if (other.property_exists("db_name")) {
129 name = other.get_property("db_name").get_string();
130 }
131 else {
132 name = other.name();
133 }
134
135 id = other.get_optional_property("id", 1);
136 entityCount = other.entity_count();
137 attributeCount = other.get_property("attribute_count").get_int();
138 }
139
140 NodeBlock::NodeBlock(const Ioss::NodeBlock &other)
141 {
142 if (other.property_exists("db_name")) {
143 name = other.get_property("db_name").get_string();
144 }
145 else {
146 name = other.name();
147 }
148
149 id = other.get_optional_property("id", 1);
150 entityCount = other.entity_count();
151 localOwnedCount = other.get_optional_property("locally_owned_count", entityCount);
152 attributeCount = other.get_property("attribute_count").get_int();
153 procOffset = 0;
154 }
155
156 EdgeBlock::EdgeBlock(const Ioss::EdgeBlock &other)
157 {
158 if (other.property_exists("db_name")) {
159 name = other.get_property("db_name").get_string();
160 }
161 else {
162 name = other.name();
163 }
164
165 id = other.get_property("id").get_int();
166 entityCount = other.entity_count();
167 nodesPerEntity = other.topology()->number_nodes();
168 attributeCount = other.get_property("attribute_count").get_int();
169
170 std::string el_type = other.topology()->name();
171 if (other.property_exists("original_topology_type")) {
172 el_type = other.get_property("original_topology_type").get_string();
173 }
174
175 Ioss::Utils::copy_string(elType, el_type);
176 procOffset = 0;
177 }
178
179 FaceBlock::FaceBlock(const Ioss::FaceBlock &other)
180 {
181 if (other.property_exists("db_name")) {
182 name = other.get_property("db_name").get_string();
183 }
184 else {
185 name = other.name();
186 }
187
188 id = other.get_property("id").get_int();
189 entityCount = other.entity_count();
190 nodesPerEntity = other.topology()->number_nodes();
191 if (other.field_exists("connectivty_edge")) {
192 edgesPerEntity = other.get_field("connectivity_edge").raw_storage()->component_count();
193 }
194 else {
195 edgesPerEntity = 0;
196 }
197 attributeCount = other.get_property("attribute_count").get_int();
198
199 std::string el_type = other.topology()->name();
200 if (other.property_exists("original_topology_type")) {
201 el_type = other.get_property("original_topology_type").get_string();
202 }
203
204 Ioss::Utils::copy_string(elType, el_type);
205 procOffset = 0;
206 }
207
208 ElemBlock::ElemBlock(const Ioss::ElementBlock &other)
209 {
210 if (other.property_exists("db_name")) {
211 name = other.get_property("db_name").get_string();
212 }
213 else {
214 name = other.name();
215 }
216
217 id = other.get_property("id").get_int();
218 entityCount = other.entity_count();
219 globalEntityCount = other.get_optional_property("global_entity_count", 0);
220 nodesPerEntity = other.topology()->number_nodes();
221
222 if (other.field_exists("connectivity_edge")) {
223 edgesPerEntity = other.get_field("connectivity_edge").raw_storage()->component_count();
224 }
225 else {
226 edgesPerEntity = 0;
227 }
228
229 if (other.field_exists("connectivity_face")) {
230 facesPerEntity = other.get_field("connectivity_face").raw_storage()->component_count();
231 }
232 else {
233 facesPerEntity = 0;
234 }
235
236 attributeCount = other.get_property("attribute_count").get_int();
237 offset_ = other.get_offset();
238 std::string el_type =
239 other.get_optional_property("original_topology_type", other.topology()->name());
240
241 Ioss::Utils::copy_string(elType, el_type);
242
243 // Fixup an exodusII kluge. For triangular elements, the same
244 // name is used for 2D elements and 3D shell elements. Convert
245 // to unambiguous names for the IO Subsystem. The 2D name
246 // stays the same, the 3D name becomes 'trishell#'
247 // Here, we need to map back to the 'triangle' name...
248 if (std::strncmp(elType, "trishell", 8) == 0) {
249 Ioss::Utils::copy_string(elType, "triangle");
250 }
251 procOffset = 0;
252 }
253
254 NodeSet::NodeSet(const Ioss::NodeSet &other)
255 {
256 if (other.property_exists("db_name")) {
257 name = other.get_property("db_name").get_string();
258 }
259 else {
260 name = other.name();
261 }
262
263 id = other.get_property("id").get_int();
264 entityCount = other.entity_count();
265 globalEntityCount = other.get_optional_property("global_entity_count", 0);
266 localOwnedCount = other.get_optional_property("locally_owned_count", entityCount);
267 attributeCount = other.get_property("attribute_count").get_int();
268 dfCount = other.get_property("distribution_factor_count").get_int();
269 if (dfCount > 0 && dfCount != entityCount) {
270 dfCount = entityCount;
271 }
272 procOffset = 0;
273 }
274
275 EdgeSet::EdgeSet(const Ioss::EdgeSet &other)
276 {
277 if (other.property_exists("db_name")) {
278 name = other.get_property("db_name").get_string();
279 }
280 else {
281 name = other.name();
282 }
283
284 id = other.get_property("id").get_int();
285 entityCount = other.entity_count();
286 attributeCount = other.get_property("attribute_count").get_int();
287 dfCount = other.get_property("distribution_factor_count").get_int();
288 procOffset = 0;
289 }
290
291 FaceSet::FaceSet(const Ioss::FaceSet &other)
292 {
293 if (other.property_exists("db_name")) {
294 name = other.get_property("db_name").get_string();
295 }
296 else {
297 name = other.name();
298 }
299
300 id = other.get_property("id").get_int();
301 entityCount = other.entity_count();
302 attributeCount = other.get_property("attribute_count").get_int();
303 dfCount = other.get_property("distribution_factor_count").get_int();
304 procOffset = 0;
305 }
306
307 ElemSet::ElemSet(const Ioss::ElementSet &other)
308 {
309 if (other.property_exists("db_name")) {
310 name = other.get_property("db_name").get_string();
311 }
312 else {
313 name = other.name();
314 }
315
316 id = other.get_property("id").get_int();
317 entityCount = other.entity_count();
318 attributeCount = other.get_property("attribute_count").get_int();
319 dfCount = other.get_property("distribution_factor_count").get_int();
320 procOffset = 0;
321 }
322
323 SideSet::SideSet(const Ioss::SideBlock &other)
324 {
325 if (other.property_exists("db_name")) {
326 name = other.get_property("db_name").get_string();
327 }
328 else {
329 name = other.name();
330 }
331
332 id = other.get_property("id").get_int();
333 entityCount = other.entity_count();
334 globalEntityCount = other.get_optional_property("global_entity_count", 0);
335 dfCount = other.get_property("distribution_factor_count").get_int();
336 const std::string &io_name = other.name();
337
338 // KLUGE: universal_sideset has side dfCount...
339 if (io_name == "universal_sideset") {
340 dfCount = entityCount;
341 }
342 procOffset = 0;
343 dfProcOffset = 0;
344 }
345
346 SideSet::SideSet(const Ioss::SideSet &other)
347 {
348 if (other.property_exists("db_name")) {
349 name = other.get_property("db_name").get_string();
350 }
351 else {
352 name = other.name();
353 }
354
355 id = other.get_property("id").get_int();
356 entityCount = other.entity_count();
357 globalEntityCount = other.get_optional_property("global_entity_count", 0);
358 dfCount = other.get_property("distribution_factor_count").get_int();
359 const std::string &io_name = other.name();
360
361 // KLUGE: universal_sideset has side dfCount...
362 if (io_name == "universal_sideset") {
363 dfCount = entityCount;
364 }
365 procOffset = 0;
366 dfProcOffset = 0;
367 }
368
369 Internals::Internals(int exoid, int maximum_name_length, const Ioss::ParallelUtils &util)
370 : exodusFilePtr(exoid), maximumNameLength(maximum_name_length), parallelUtil(util)
371 {
372 }
373
374 int Internals::initialize_state_file(Mesh &mesh, const ex_var_params &var_params,
375 const std::string &base_filename)
376 {
377 // Determine global counts...
378 if (!mesh.file_per_processor) {
379 mesh.get_global_counts();
380 }
381
382 int ierr = 0;
383 {
384 Redefine the_database(exodusFilePtr);
385 int old_fill = 0;
386
387 int status = nc_set_fill(exodusFilePtr, NC_NOFILL, &old_fill);
388 if (status != EX_NOERR) {
389 return EX_FATAL;
390 }
391
392 status = nc_put_att_text(exodusFilePtr, NC_GLOBAL, "base_database",
393 static_cast<int>(base_filename.length()) + 1, base_filename.c_str());
394
395 if (status != NC_NOERR) {
396 ex_opts(EX_VERBOSE);
397 std::string errmsg = fmt::format(
398 "Error: failed to define 'base_database' attribute to file id {}", exodusFilePtr);
399 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
400 return EX_FATAL;
401 }
402
403 // Time Dimension...
404 int timedim;
405 if ((status = nc_def_dim(exodusFilePtr, DIM_TIME, NC_UNLIMITED, &timedim)) != NC_NOERR) {
406 std::string errmsg =
407 fmt::format("Error: failed to define time dimension in file id {}", exodusFilePtr);
408 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
409 return EX_FATAL;
410 }
411
412 // Name String Length...
413 int namestrdim;
414 status = nc_def_dim(exodusFilePtr, DIM_STR_NAME, maximumNameLength + 1, &namestrdim);
415 if (status != NC_NOERR) {
416 ex_opts(EX_VERBOSE);
417 std::string errmsg =
418 fmt::format("Error: failed to define 'name string length' in file id {}", exodusFilePtr);
419 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
420 return EX_FATAL;
421 }
422
423 // Nodes (Node Block) ...
424 if (var_params.num_node > 0) {
425 int numnoddim;
426 status = nc_def_dim(exodusFilePtr, DIM_NUM_NODES, mesh.nodeblocks[0].entityCount, &numnoddim);
427 if (status != NC_NOERR) {
428 ex_opts(EX_VERBOSE);
429 std::string errmsg =
430 fmt::format("Error: failed to define number of nodes in file id {}", exodusFilePtr);
431 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
432 return EX_FATAL;
433 }
434 }
435
436 // ========================================================================
437 // Blocks...
438 size_t elem_count = 0;
439 for (const auto &elem : mesh.elemblocks) {
440 elem_count += elem.entityCount;
441 }
442
443 if (elem_count > 0 && var_params.num_elem > 0) {
444 int numelemdim;
445 status = nc_def_dim(exodusFilePtr, DIM_NUM_ELEM, elem_count, &numelemdim);
446 if (status != NC_NOERR) {
447 ex_opts(EX_VERBOSE);
448 std::string errmsg =
449 fmt::format("Error: failed to define number of elements in file id {}", exodusFilePtr);
450 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
451 return EX_FATAL;
452 }
453 if (define_netcdf_vars(exodusFilePtr, "element block", mesh.elemblocks.size(), DIM_NUM_EL_BLK,
454 VAR_STAT_EL_BLK, VAR_ID_EL_BLK, VAR_NAME_EL_BLK) != EX_NOERR) {
455 return EX_FATAL;
456 }
457 }
458
459 size_t face_count = 0;
460 for (const auto &face : mesh.faceblocks) {
461 face_count += face.entityCount;
462 }
463
464 if (face_count > 0 && var_params.num_face > 0) {
465 int numfacedim;
466 status = nc_def_dim(exodusFilePtr, DIM_NUM_FACE, face_count, &numfacedim);
467 if (status != NC_NOERR) {
468 ex_opts(EX_VERBOSE);
469 std::string errmsg =
470 fmt::format("Error: failed to define number of faces in file id {}", exodusFilePtr);
471 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
472 return EX_FATAL;
473 }
474 if (define_netcdf_vars(exodusFilePtr, "face block", mesh.faceblocks.size(), DIM_NUM_FA_BLK,
475 VAR_STAT_FA_BLK, VAR_ID_FA_BLK, VAR_NAME_FA_BLK) != EX_NOERR) {
476 return EX_FATAL;
477 }
478 }
479
480 size_t edge_count = 0;
481 for (const auto &edge : mesh.edgeblocks) {
482 edge_count += edge.entityCount;
483 }
484
485 if (edge_count > 0 && var_params.num_edge > 0) {
486 int numedgedim;
487 status = nc_def_dim(exodusFilePtr, DIM_NUM_EDGE, edge_count, &numedgedim);
488 if (status != NC_NOERR) {
489 ex_opts(EX_VERBOSE);
490 std::string errmsg =
491 fmt::format("Error: failed to define number of edges in file id {}", exodusFilePtr);
492 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
493 return EX_FATAL;
494 }
495 if (define_netcdf_vars(exodusFilePtr, "edge block", mesh.edgeblocks.size(), DIM_NUM_ED_BLK,
496 VAR_STAT_ED_BLK, VAR_ID_ED_BLK, VAR_NAME_ED_BLK) != EX_NOERR) {
497 return EX_FATAL;
498 }
499 }
500
501 // ========================================================================
502 // Sets...
503 if (var_params.num_nset > 0) {
504 if (define_netcdf_vars(exodusFilePtr, "node set", mesh.nodesets.size(), DIM_NUM_NS,
505 VAR_NS_STAT, VAR_NS_IDS, VAR_NAME_NS) != EX_NOERR) {
506 return EX_FATAL;
507 }
508 }
509
510 if (var_params.num_eset > 0) {
511 if (define_netcdf_vars(exodusFilePtr, "edge set", mesh.edgesets.size(), DIM_NUM_ES,
512 VAR_ES_STAT, VAR_ES_IDS, VAR_NAME_ES) != EX_NOERR) {
513 return EX_FATAL;
514 }
515 }
516
517 if (var_params.num_fset > 0) {
518 if (define_netcdf_vars(exodusFilePtr, "face set", mesh.facesets.size(), DIM_NUM_FS,
519 VAR_FS_STAT, VAR_FS_IDS, VAR_NAME_FS) != EX_NOERR) {
520 return EX_FATAL;
521 }
522 }
523
524 if (var_params.num_elset > 0) {
525 if (define_netcdf_vars(exodusFilePtr, "element set", mesh.elemsets.size(), DIM_NUM_ELS,
526 VAR_ELS_STAT, VAR_ELS_IDS, VAR_NAME_ELS) != EX_NOERR) {
527 return EX_FATAL;
528 }
529 }
530
531 // ========================================================================
532 // side sets...
533 if (var_params.num_sset > 0) {
534 if (define_netcdf_vars(exodusFilePtr, "side set", mesh.sidesets.size(), DIM_NUM_SS,
535 VAR_SS_STAT, VAR_SS_IDS, VAR_NAME_SS) != EX_NOERR) {
536 return EX_FATAL;
537 }
538 }
539
540 if (var_params.num_edge > 0) {
541 if ((ierr = put_metadata(mesh.edgeblocks, true)) != EX_NOERR) {
542 EX_FUNC_LEAVE(ierr);
543 }
544 }
545
546 if (var_params.num_face > 0) {
547 if ((ierr = put_metadata(mesh.faceblocks, true)) != EX_NOERR) {
548 EX_FUNC_LEAVE(ierr);
549 }
550 }
551
552 if (var_params.num_elem > 0) {
553 if ((ierr = put_metadata(mesh.elemblocks, true)) != EX_NOERR) {
554 EX_FUNC_LEAVE(ierr);
555 }
556 }
557
558 if (var_params.num_nset > 0) {
559 if ((ierr = put_metadata(mesh.nodesets, true)) != EX_NOERR) {
560 EX_FUNC_LEAVE(ierr);
561 }
562 }
563
564 if (var_params.num_eset > 0) {
565 if ((ierr = put_metadata(mesh.edgesets, true)) != EX_NOERR) {
566 EX_FUNC_LEAVE(ierr);
567 }
568 }
569
570 if (var_params.num_fset > 0) {
571 if ((ierr = put_metadata(mesh.facesets, true)) != EX_NOERR) {
572 EX_FUNC_LEAVE(ierr);
573 }
574 }
575
576 if (var_params.num_elset > 0) {
577 if ((ierr = put_metadata(mesh.elemsets, true)) != EX_NOERR) {
578 EX_FUNC_LEAVE(ierr);
579 }
580 }
581
582 if (var_params.num_sset > 0) {
583 if ((ierr = put_metadata(mesh.sidesets, true)) != EX_NOERR) {
584 EX_FUNC_LEAVE(ierr);
585 }
586 }
587
588 int varid;
589 std::array dim{timedim};
590 if ((status = nc_def_var(exodusFilePtr, VAR_WHOLE_TIME, nc_flt_code(exodusFilePtr), 1,
591 Data(dim), &varid)) != NC_NOERR) {
592 std::string errmsg = fmt::format(
593 "Error: failed to define whole time step variable in file id {}", exodusFilePtr);
594 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
595 return EX_FATAL;
596 }
597
598 struct exi_file_item *file = exi_find_file_item(exodusFilePtr);
599 if (file) {
600 file->time_varid = varid;
601 }
602
603 exi_compress_variable(exodusFilePtr, varid, -2); /* don't compress, but do set collective io */
604 } // Exit redefine mode
605
606 bool output_global_data = (mesh.comm.outputNemesis && mesh.comm.processorCount > 1);
607 if (var_params.num_edge > 0) {
608 if ((ierr = put_non_define_data(mesh.edgeblocks)) != EX_NOERR) {
609 EX_FUNC_LEAVE(ierr);
610 }
611 output_names(mesh.edgeblocks, exodusFilePtr, EX_EDGE_BLOCK);
612 }
613
614 if (var_params.num_face > 0) {
615 if ((ierr = put_non_define_data(mesh.faceblocks)) != EX_NOERR) {
616 EX_FUNC_LEAVE(ierr);
617 }
618 output_names(mesh.faceblocks, exodusFilePtr, EX_FACE_BLOCK);
619 }
620
621 if (var_params.num_elem > 0) {
622 if ((ierr = put_non_define_data(mesh.elemblocks, output_global_data)) != EX_NOERR) {
623 EX_FUNC_LEAVE(ierr);
624 }
625 output_names(mesh.elemblocks, exodusFilePtr, EX_ELEM_BLOCK);
626 }
627
628 if (var_params.num_nset > 0) {
629 if ((ierr = put_non_define_data(mesh.nodesets, output_global_data)) != EX_NOERR) {
630 EX_FUNC_LEAVE(ierr);
631 }
632 output_names(mesh.nodesets, exodusFilePtr, EX_NODE_SET);
633 }
634
635 if (var_params.num_eset > 0) {
636 if ((ierr = put_non_define_data(mesh.edgesets)) != EX_NOERR) {
637 EX_FUNC_LEAVE(ierr);
638 }
639 output_names(mesh.edgesets, exodusFilePtr, EX_EDGE_SET);
640 }
641
642 if (var_params.num_fset > 0) {
643 if ((ierr = put_non_define_data(mesh.facesets)) != EX_NOERR) {
644 EX_FUNC_LEAVE(ierr);
645 }
646 output_names(mesh.facesets, exodusFilePtr, EX_FACE_SET);
647 }
648
649 if (var_params.num_elset > 0) {
650 if ((ierr = put_non_define_data(mesh.elemsets)) != EX_NOERR) {
651 EX_FUNC_LEAVE(ierr);
652 }
653 output_names(mesh.elemsets, exodusFilePtr, EX_ELEM_SET);
654 }
655
656 if (var_params.num_sset > 0) {
657 if ((ierr = put_non_define_data(mesh.sidesets, output_global_data)) != EX_NOERR) {
658 EX_FUNC_LEAVE(ierr);
659 }
660 output_names(mesh.sidesets, exodusFilePtr, EX_SIDE_SET);
661 }
662
663 return EX_NOERR;
664 }
665
666 void Mesh::populate(Ioss::Region *region)
667 {
668 {
669 const auto &node_blocks = region->get_node_blocks();
670 if (!node_blocks.empty()) {
671 Ioex::NodeBlock N(*node_blocks[0]);
672 nodeblocks.push_back(N);
673 }
674 }
675
676 // Assemblies --
677 {
678 const auto &assem = region->get_assemblies();
679 for (auto &assembly : assem) {
680 Ioex::Assembly T(*(assembly));
681 assemblies.push_back(T);
682 }
683 }
684
685 // Blobs --
686 {
687 const auto &blbs = region->get_blobs();
688 for (auto &blob : blbs) {
689 Ioex::Blob T(*(blob));
690 blobs.push_back(T);
691 }
692 }
693
694 // Edge Blocks --
695 {
696 const Ioss::EdgeBlockContainer &edge_blocks = region->get_edge_blocks();
697 for (auto &edge_block : edge_blocks) {
698 Ioex::EdgeBlock T(*(edge_block));
699 edgeblocks.push_back(T);
700 }
701 }
702
703 // Face Blocks --
704 {
705 const Ioss::FaceBlockContainer &face_blocks = region->get_face_blocks();
706 for (auto &face_block : face_blocks) {
707 Ioex::FaceBlock T(*(face_block));
708 faceblocks.push_back(T);
709 }
710 }
711
712 // Element Blocks --
713 {
714 const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks();
715 for (auto &element_block : element_blocks) {
716 Ioex::ElemBlock T(*(element_block));
717 elemblocks.push_back(T);
718 }
719 }
720
721 // NodeSets ...
722 {
723 const Ioss::NodeSetContainer &node_sets = region->get_nodesets();
724 for (auto &set : node_sets) {
725 const Ioex::NodeSet T(*(set));
726 nodesets.push_back(T);
727 }
728 }
729
730 // EdgeSets ...
731 {
732 const Ioss::EdgeSetContainer &edge_sets = region->get_edgesets();
733 for (auto &set : edge_sets) {
734 const Ioex::EdgeSet T(*(set));
735 edgesets.push_back(T);
736 }
737 }
738
739 // FaceSets ...
740 {
741 const Ioss::FaceSetContainer &face_sets = region->get_facesets();
742 for (auto &set : face_sets) {
743 const Ioex::FaceSet T(*(set));
744 facesets.push_back(T);
745 }
746 }
747
748 // ElementSets ...
749 {
750 const Ioss::ElementSetContainer &element_sets = region->get_elementsets();
751 for (auto &set : element_sets) {
752 const Ioex::ElemSet T(*(set));
753 elemsets.push_back(T);
754 }
755 }
756
757 // SideSets ...
758 {
759 const Ioss::SideSetContainer &ssets = region->get_sidesets();
760 for (auto &set : ssets) {
761 // Add a SideSet corresponding to this SideSet/SideBlock
762 Ioex::SideSet T(*set);
763 sidesets.push_back(T);
764 }
765 }
766
767 // Determine global counts...
768 if (!file_per_processor) {
769 get_global_counts();
770 }
771 }
772
773 void Mesh::get_global_counts()
774 {
775 #if defined(SEACAS_HAVE_MPI)
776 std::vector<int64_t> counts;
777 std::vector<int64_t> global_counts;
778
779 for (auto &nodeblock : nodeblocks) {
780 counts.push_back(nodeblock.localOwnedCount);
781 }
782 for (auto &edgeblock : edgeblocks) {
783 counts.push_back(edgeblock.entityCount);
784 }
785 for (auto &faceblock : faceblocks) {
786 counts.push_back(faceblock.entityCount);
787 }
788 for (auto &elemblock : elemblocks) {
789 counts.push_back(elemblock.entityCount);
790 }
791 for (auto &nodeset : nodesets) {
792 counts.push_back(nodeset.localOwnedCount);
793 counts.push_back(nodeset.dfCount);
794 }
795 for (auto &edgeset : edgesets) {
796 counts.push_back(edgeset.entityCount);
797 counts.push_back(edgeset.dfCount);
798 }
799 for (auto &faceset : facesets) {
800 counts.push_back(faceset.entityCount);
801 counts.push_back(faceset.dfCount);
802 }
803 for (auto &elemset : elemsets) {
804 counts.push_back(elemset.entityCount);
805 counts.push_back(elemset.dfCount);
806 }
807 for (auto &sideset : sidesets) {
808 counts.push_back(sideset.entityCount);
809 counts.push_back(sideset.dfCount);
810 }
811 for (auto &blob : blobs) {
812 counts.push_back(blob.entityCount);
813 }
814
815 // Now gather this information on each processor so
816 // they can determine the offsets and totals...
817 global_counts.resize(counts.size() * parallelUtil.parallel_size());
818
819 MPI_Allgather(Data(counts), counts.size(), MPI_LONG_LONG_INT, Data(global_counts), counts.size(),
820 MPI_LONG_LONG_INT, parallelUtil.communicator());
821
822 std::vector<int64_t> offsets(counts.size());
823
824 size_t my_proc = parallelUtil.parallel_rank();
825 size_t proc_count = parallelUtil.parallel_size();
826
827 // Calculate offsets for each entity on each processor
828 for (size_t j = 0; j < offsets.size(); j++) {
829 for (size_t i = 0; i < my_proc; i++) {
830 offsets[j] += global_counts[i * offsets.size() + j];
831 }
832 }
833
834 // Now calculate the total count of entities over all processors
835 for (size_t j = 0; j < offsets.size(); j++) {
836 for (size_t i = 1; i < proc_count; i++) {
837 global_counts[j] += global_counts[i * offsets.size() + j];
838 }
839 }
840
841 size_t j = 0;
842 for (auto &nodeblock : nodeblocks) {
843 nodeblock.procOffset = offsets[j];
844 nodeblock.entityCount = global_counts[j++];
845 }
846 for (auto &edgeblock : edgeblocks) {
847 edgeblock.procOffset = offsets[j];
848 edgeblock.entityCount = global_counts[j++];
849 }
850 for (auto &faceblock : faceblocks) {
851 faceblock.procOffset = offsets[j];
852 faceblock.entityCount = global_counts[j++];
853 }
854 for (auto &elemblock : elemblocks) {
855 elemblock.procOffset = offsets[j];
856 elemblock.entityCount = global_counts[j++];
857 }
858 for (auto &nodeset : nodesets) {
859 nodeset.procOffset = offsets[j];
860 nodeset.entityCount = global_counts[j++];
861 nodeset.dfCount = global_counts[j++];
862 if (nodeset.dfCount != 0) {
863 // Need to adjust for locally-owned only in the auto-join output.
864 nodeset.dfCount = nodeset.entityCount;
865 }
866 }
867 for (auto &edgeset : edgesets) {
868 edgeset.procOffset = offsets[j];
869 edgeset.entityCount = global_counts[j++];
870 edgeset.dfCount = global_counts[j++];
871 }
872 for (auto &faceset : facesets) {
873 faceset.procOffset = offsets[j];
874 faceset.entityCount = global_counts[j++];
875 faceset.dfCount = global_counts[j++];
876 }
877 for (auto &elemset : elemsets) {
878 elemset.procOffset = offsets[j];
879 elemset.entityCount = global_counts[j++];
880 elemset.dfCount = global_counts[j++];
881 }
882 for (auto &sideset : sidesets) {
883 sideset.procOffset = offsets[j];
884 sideset.entityCount = global_counts[j++];
885 sideset.dfProcOffset = offsets[j];
886 sideset.dfCount = global_counts[j++];
887 }
888 for (auto &blob : blobs) {
889 blob.procOffset = offsets[j];
890 blob.entityCount = global_counts[j++];
891 }
892 #endif
893 }
894
895 int Internals::write_meta_data(Mesh &mesh)
896 {
897 EX_FUNC_ENTER();
898 int ierr;
899 {
900 // Determine length of longest name... Reduces calls to put_att
901 maximumNameLength = get_max_name_length(mesh.edgeblocks, maximumNameLength);
902 maximumNameLength = get_max_name_length(mesh.faceblocks, maximumNameLength);
903 maximumNameLength = get_max_name_length(mesh.elemblocks, maximumNameLength);
904 maximumNameLength = get_max_name_length(mesh.nodesets, maximumNameLength);
905 maximumNameLength = get_max_name_length(mesh.edgesets, maximumNameLength);
906 maximumNameLength = get_max_name_length(mesh.facesets, maximumNameLength);
907 maximumNameLength = get_max_name_length(mesh.elemsets, maximumNameLength);
908 maximumNameLength = get_max_name_length(mesh.sidesets, maximumNameLength);
909 maximumNameLength = get_max_name_length(mesh.blobs, maximumNameLength);
910 maximumNameLength = get_max_name_length(mesh.assemblies, maximumNameLength);
911
912 Redefine the_database(exodusFilePtr);
913 // Set the database to NOFILL mode. Only writes values we want written...
914 int old_fill = 0;
915
916 if ((ierr = nc_set_fill(exodusFilePtr, NC_NOFILL, &old_fill)) != EX_NOERR) {
917 EX_FUNC_LEAVE(ierr);
918 }
919
920 if ((ierr = put_metadata(mesh, mesh.comm)) != EX_NOERR) {
921 EX_FUNC_LEAVE(ierr);
922 }
923
924 if ((ierr = put_metadata(mesh.edgeblocks)) != EX_NOERR) {
925 EX_FUNC_LEAVE(ierr);
926 }
927
928 if ((ierr = put_metadata(mesh.faceblocks)) != EX_NOERR) {
929 EX_FUNC_LEAVE(ierr);
930 }
931
932 if ((ierr = put_metadata(mesh.elemblocks)) != EX_NOERR) {
933 EX_FUNC_LEAVE(ierr);
934 }
935
936 if ((ierr = put_metadata(mesh.nodesets)) != EX_NOERR) {
937 EX_FUNC_LEAVE(ierr);
938 }
939
940 if ((ierr = put_metadata(mesh.edgesets)) != EX_NOERR) {
941 EX_FUNC_LEAVE(ierr);
942 }
943
944 if ((ierr = put_metadata(mesh.facesets)) != EX_NOERR) {
945 EX_FUNC_LEAVE(ierr);
946 }
947
948 if ((ierr = put_metadata(mesh.elemsets)) != EX_NOERR) {
949 EX_FUNC_LEAVE(ierr);
950 }
951
952 if ((ierr = put_metadata(mesh.sidesets)) != EX_NOERR) {
953 EX_FUNC_LEAVE(ierr);
954 }
955
956 if ((ierr = put_metadata(mesh.blobs)) != EX_NOERR) {
957 EX_FUNC_LEAVE(ierr);
958 }
959
960 if ((ierr = put_metadata(mesh.assemblies)) != EX_NOERR) {
961 EX_FUNC_LEAVE(ierr);
962 }
963 }
964
965 // NON-Define mode output...
966 bool output_global_data = (mesh.comm.outputNemesis && mesh.comm.processorCount > 1);
967
968 if ((ierr = put_non_define_data(mesh.comm, mesh.full_nemesis_data)) != EX_NOERR) {
969 EX_FUNC_LEAVE(ierr);
970 }
971
972 if ((ierr = put_non_define_data(mesh.edgeblocks)) != EX_NOERR) {
973 EX_FUNC_LEAVE(ierr);
974 }
975
976 if ((ierr = put_non_define_data(mesh.faceblocks)) != EX_NOERR) {
977 EX_FUNC_LEAVE(ierr);
978 }
979
980 if ((ierr = put_non_define_data(mesh.elemblocks, output_global_data)) != EX_NOERR) {
981 EX_FUNC_LEAVE(ierr);
982 }
983
984 if ((ierr = put_non_define_data(mesh.nodesets, output_global_data)) != EX_NOERR) {
985 EX_FUNC_LEAVE(ierr);
986 }
987
988 if ((ierr = put_non_define_data(mesh.edgesets)) != EX_NOERR) {
989 EX_FUNC_LEAVE(ierr);
990 }
991
992 if ((ierr = put_non_define_data(mesh.facesets)) != EX_NOERR) {
993 EX_FUNC_LEAVE(ierr);
994 }
995
996 if ((ierr = put_non_define_data(mesh.elemsets)) != EX_NOERR) {
997 EX_FUNC_LEAVE(ierr);
998 }
999
1000 if ((ierr = put_non_define_data(mesh.sidesets, output_global_data)) != EX_NOERR) {
1001 EX_FUNC_LEAVE(ierr);
1002 }
1003
1004 if ((ierr = put_non_define_data(mesh.blobs)) != EX_NOERR) {
1005 EX_FUNC_LEAVE(ierr);
1006 }
1007
1008 if ((ierr = put_non_define_data(mesh.assemblies)) != EX_NOERR) {
1009 EX_FUNC_LEAVE(ierr);
1010 }
1011
1012 // For now, put entity names using the ExodusII api...
1013 output_names(mesh.edgeblocks, exodusFilePtr, EX_EDGE_BLOCK);
1014 output_names(mesh.faceblocks, exodusFilePtr, EX_FACE_BLOCK);
1015 output_names(mesh.elemblocks, exodusFilePtr, EX_ELEM_BLOCK);
1016 output_names(mesh.nodesets, exodusFilePtr, EX_NODE_SET);
1017 output_names(mesh.edgesets, exodusFilePtr, EX_EDGE_SET);
1018 output_names(mesh.facesets, exodusFilePtr, EX_FACE_SET);
1019 output_names(mesh.elemsets, exodusFilePtr, EX_ELEM_SET);
1020 output_names(mesh.sidesets, exodusFilePtr, EX_SIDE_SET);
1021
1022 EX_FUNC_LEAVE(EX_NOERR);
1023 }
1024
1025 void Internals::copy_database(int in_file, int out_file, bool transient_also)
1026 {
1027 ex_copy(in_file, out_file);
1028 if (transient_also) {
1029 ex_copy_transient(in_file, out_file);
1030 }
1031 }
1032
1033 void Internals::update_assembly_data(int exoid, std::vector<Assembly> &assemblies, int stage)
1034 {
1035 Ioss::ParallelUtils pm;
1036 Internals internal{exoid, 0, pm};
1037
1038 if (stage == 0 || stage == 1) {
1039 Redefine the_database(exoid);
1040 internal.put_metadata(assemblies);
1041 }
1042
1043 if (stage == 0 || stage == 2) {
1044 internal.put_non_define_data(assemblies);
1045 }
1046 }
1047
1048 int Internals::put_metadata(const Mesh &mesh, const CommunicationMetaData &comm)
1049 {
1050 int numdimdim = 0;
1051 int numnoddim = 0;
1052 int namestrdim = 0;
1053 int varid = 0;
1054 int timedim = 0;
1055
1056 int map_type = get_type(exodusFilePtr, EX_MAPS_INT64_DB);
1057 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
1058 int ids_type = get_type(exodusFilePtr, EX_IDS_INT64_DB);
1059
1060 int rootid = static_cast<unsigned>(exodusFilePtr) & EX_FILE_ID_MASK;
1061
1062 if (rootid == exodusFilePtr && nc_inq_dimid(exodusFilePtr, DIM_NUM_DIM, &numdimdim) == NC_NOERR) {
1063 std::string errmsg =
1064 fmt::format("Error: initialization already done for file id {}", exodusFilePtr);
1065 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), EX_MSG);
1066 return EX_FATAL;
1067 }
1068
1069 if (rootid == exodusFilePtr) {
1070 // We are creating a grouped file, the title and other attributes have
1071 // already been defined when the root group was created; don't redo now.
1072 int status = nc_put_att_text(rootid, NC_GLOBAL, ATT_TITLE,
1073 static_cast<int>(std::strlen(mesh.title)) + 1, mesh.title);
1074
1075 // define some attributes...
1076 if (status != NC_NOERR) {
1077 ex_opts(EX_VERBOSE);
1078 std::string errmsg =
1079 fmt::format("Error: failed to define title attribute to file id {}", exodusFilePtr);
1080 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1081 return EX_FATAL;
1082 }
1083
1084 // For use later as a consistency check, define the number of processors and
1085 // the current processor id as an attribute of the file...
1086 if (comm.outputNemesis && comm.processorCount > 1) {
1087 std::array ltempsv{comm.processorCount, comm.processorId};
1088 status = nc_put_att_int(rootid, NC_GLOBAL, "processor_info", NC_INT, 2, Data(ltempsv));
1089 if (status != NC_NOERR) {
1090 ex_opts(EX_VERBOSE);
1091 std::string errmsg = fmt::format(
1092 "Error: failed to define processor info attribute to file id {}", exodusFilePtr);
1093 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1094 return EX_FATAL;
1095 }
1096 }
1097
1098 // For use later to determine whether a timestep is corrupt, we define an attribute
1099 // containing the last written time...
1100 {
1101 double fake_time = -1.0e38;
1102 status = nc_put_att_double(rootid, NC_GLOBAL, "last_written_time", NC_DOUBLE, 1, &fake_time);
1103 if (status != NC_NOERR) {
1104 ex_opts(EX_VERBOSE);
1105 std::string errmsg = fmt::format(
1106 "Error: failed to define 'last_written_time' attribute to file id {}", exodusFilePtr);
1107 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1108 return EX_FATAL;
1109 }
1110 }
1111
1112 // For use later to help readers know how much memory to allocate
1113 // for name storage, we define an attribute containing the maximum
1114 // size of any name.
1115 {
1116 int current_len = 0;
1117 status = nc_put_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &current_len);
1118 if (status != NC_NOERR) {
1119 ex_opts(EX_VERBOSE);
1120 std::string errmsg = fmt::format(
1121 "Error: failed to define ATT_MAX_NAME_LENGTH attribute to file id {}", exodusFilePtr);
1122 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1123 return EX_FATAL;
1124 }
1125 }
1126 }
1127
1128 /* create name string length dimension */
1129 if (maximumNameLength < 32) {
1130 maximumNameLength = 32;
1131 }
1132 if (nc_inq_dimid(rootid, DIM_STR_NAME, &namestrdim) != NC_NOERR) {
1133 int status = nc_def_dim(rootid, DIM_STR_NAME, maximumNameLength + 1, &namestrdim);
1134 if (status != NC_NOERR) {
1135 ex_opts(EX_VERBOSE);
1136 std::string errmsg =
1137 fmt::format("Error: failed to define name string length in file id {}", exodusFilePtr);
1138 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1139 return EX_FATAL;
1140 }
1141 }
1142
1143 int status = nc_def_dim(exodusFilePtr, DIM_NUM_DIM, mesh.dimensionality, &numdimdim);
1144 if (status != NC_NOERR) {
1145 ex_opts(EX_VERBOSE);
1146 std::string errmsg =
1147 fmt::format("Error: failed to define number of dimensions in file id {}", exodusFilePtr);
1148 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1149 return EX_FATAL;
1150 }
1151
1152 if ((status = nc_def_dim(exodusFilePtr, DIM_TIME, NC_UNLIMITED, &timedim)) != NC_NOERR) {
1153 std::string errmsg =
1154 fmt::format("Error: failed to define time dimension in file id {}", exodusFilePtr);
1155 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1156 return EX_FATAL;
1157 }
1158
1159 std::array dim{timedim};
1160 if ((status = nc_def_var(exodusFilePtr, VAR_WHOLE_TIME, nc_flt_code(exodusFilePtr), 1, Data(dim),
1161 &varid)) != NC_NOERR) {
1162 std::string errmsg = fmt::format(
1163 "Error: failed to define whole time step variable in file id {}", exodusFilePtr);
1164 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1165 return EX_FATAL;
1166 }
1167 {
1168 struct exi_file_item *file = exi_find_file_item(exodusFilePtr);
1169 if (file != nullptr) {
1170 file->time_varid = varid;
1171 }
1172 }
1173 exi_compress_variable(exodusFilePtr, varid, -2);
1174
1175 if (!mesh.nodeblocks.empty() && mesh.nodeblocks[0].entityCount > 0) {
1176 status = nc_def_dim(exodusFilePtr, DIM_NUM_NODES, mesh.nodeblocks[0].entityCount, &numnoddim);
1177 if (status != NC_NOERR) {
1178 ex_opts(EX_VERBOSE);
1179 std::string errmsg =
1180 fmt::format("Error: failed to define number of nodes in file id {}", exodusFilePtr);
1181 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1182 return EX_FATAL;
1183 }
1184
1185 // Define the node map here to avoid a later redefine call
1186 if (mesh.use_node_map) {
1187 std::array dims1{numnoddim};
1188 status = nc_def_var(exodusFilePtr, VAR_NODE_NUM_MAP, map_type, 1, Data(dims1), &varid);
1189 if (status != NC_NOERR) {
1190 ex_opts(EX_VERBOSE);
1191 if (status == NC_ENAMEINUSE) {
1192 std::string errmsg =
1193 fmt::format("Error: node numbering map already exists in file id {}", exodusFilePtr);
1194 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1195 }
1196 else {
1197 std::string errmsg = fmt::format(
1198 "Error: failed to create node numbering map array in file id {}", exodusFilePtr);
1199 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1200 }
1201 return EX_FATAL;
1202 }
1203 exi_compress_variable(exodusFilePtr, varid, 1);
1204 }
1205 }
1206
1207 if (!mesh.nodeblocks.empty() && mesh.nodeblocks[0].attributeCount > 0) {
1208 int numattrdim;
1209 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_NBLK, mesh.nodeblocks[0].attributeCount,
1210 &numattrdim);
1211 if (status != NC_NOERR) {
1212 ex_opts(EX_VERBOSE);
1213 std::string errmsg =
1214 fmt::format("Error: failed to define number of attributes in node block {}"
1215 " in file id {}",
1216 static_cast<entity_id>(mesh.nodeblocks[0].id), exodusFilePtr);
1217 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1218 return EX_FATAL;
1219 }
1220
1221 std::array dims{numnoddim, numattrdim};
1222 status =
1223 nc_def_var(exodusFilePtr, VAR_NATTRIB, nc_flt_code(exodusFilePtr), 2, Data(dims), &varid);
1224 if (status != NC_NOERR) {
1225 ex_opts(EX_VERBOSE);
1226 std::string errmsg =
1227 fmt::format("Error: failed to define attributes for node block {} in file id {}",
1228 mesh.nodeblocks[0].id, exodusFilePtr);
1229 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1230 return EX_FATAL;
1231 }
1232 exi_compress_variable(exodusFilePtr, varid, 2);
1233
1234 // Attribute name array...
1235 dims[0] = numattrdim;
1236 dims[1] = namestrdim;
1237
1238 status = nc_def_var(exodusFilePtr, VAR_NAME_NATTRIB, NC_CHAR, 2, Data(dims), &varid);
1239 if (status != NC_NOERR) {
1240 ex_opts(EX_VERBOSE);
1241 std::string errmsg =
1242 fmt::format("Error: failed to define attribute name array for node block {}"
1243 " in file id {}",
1244 mesh.nodeblocks[0].id, exodusFilePtr);
1245 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1246 return EX_FATAL;
1247 }
1248 exi_set_compact_storage(exodusFilePtr, varid);
1249 }
1250
1251 size_t elem_count = 0;
1252 for (const auto &elem : mesh.elemblocks) {
1253 elem_count += elem.entityCount;
1254 }
1255
1256 if (elem_count > 0) {
1257 int numelemdim;
1258 status = nc_def_dim(exodusFilePtr, DIM_NUM_ELEM, elem_count, &numelemdim);
1259 if (status != NC_NOERR) {
1260 ex_opts(EX_VERBOSE);
1261 std::string errmsg =
1262 fmt::format("Error: failed to define number of elements in file id {}", exodusFilePtr);
1263 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1264 return EX_FATAL;
1265 }
1266
1267 // Define the element map here to avoid a later redefine call
1268 if (mesh.use_elem_map) {
1269 std::array dims{numelemdim};
1270 varid = 0;
1271 status = nc_def_var(exodusFilePtr, VAR_ELEM_NUM_MAP, map_type, 1, Data(dims), &varid);
1272 if (status != NC_NOERR) {
1273 ex_opts(EX_VERBOSE);
1274 if (status == NC_ENAMEINUSE) {
1275 std::string errmsg = fmt::format(
1276 "Error: element numbering map already exists in file id {}", exodusFilePtr);
1277 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1278 }
1279 else {
1280 std::string errmsg = fmt::format(
1281 "Error: failed to create element numbering map in file id {}", exodusFilePtr);
1282 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1283 }
1284 return EX_FATAL;
1285 }
1286 exi_compress_variable(exodusFilePtr, varid, 1);
1287 }
1288 }
1289
1290 size_t face_count = 0;
1291 for (const auto &elem : mesh.faceblocks) {
1292 face_count += elem.entityCount;
1293 }
1294
1295 if (face_count > 0) {
1296 int numfacedim;
1297 status = nc_def_dim(exodusFilePtr, DIM_NUM_FACE, face_count, &numfacedim);
1298 if (status != NC_NOERR) {
1299 ex_opts(EX_VERBOSE);
1300 std::string errmsg =
1301 fmt::format("Error: failed to define number of faces in file id {}", exodusFilePtr);
1302 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1303 return EX_FATAL;
1304 }
1305
1306 // Define the face map here to avoid a later redefine call
1307 if (mesh.use_face_map) {
1308 std::array dims{numfacedim};
1309 varid = 0;
1310 status = nc_def_var(exodusFilePtr, VAR_FACE_NUM_MAP, map_type, 1, Data(dims), &varid);
1311 if (status != NC_NOERR) {
1312 ex_opts(EX_VERBOSE);
1313 if (status == NC_ENAMEINUSE) {
1314 std::string errmsg =
1315 fmt::format("Error: face numbering map already exists in file id {}", exodusFilePtr);
1316 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1317 }
1318 else {
1319 std::string errmsg = fmt::format(
1320 "Error: failed to create face numbering map in file id {}", exodusFilePtr);
1321 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1322 }
1323 return EX_FATAL;
1324 }
1325 exi_compress_variable(exodusFilePtr, varid, 1);
1326 }
1327 }
1328
1329 size_t edge_count = 0;
1330 for (const auto &elem : mesh.edgeblocks) {
1331 edge_count += elem.entityCount;
1332 }
1333
1334 if (edge_count > 0) {
1335 int numedgedim;
1336 status = nc_def_dim(exodusFilePtr, DIM_NUM_EDGE, edge_count, &numedgedim);
1337 if (status != NC_NOERR) {
1338 ex_opts(EX_VERBOSE);
1339 std::string errmsg =
1340 fmt::format("Error: failed to define number of edges in file id {}", exodusFilePtr);
1341 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1342 return EX_FATAL;
1343 }
1344
1345 // Define the edge map here to avoid a later redefine call
1346 if (mesh.use_edge_map) {
1347 std::array dims{numedgedim};
1348 varid = 0;
1349 status = nc_def_var(exodusFilePtr, VAR_EDGE_NUM_MAP, map_type, 1, Data(dims), &varid);
1350 if (status != NC_NOERR) {
1351 ex_opts(EX_VERBOSE);
1352 if (status == NC_ENAMEINUSE) {
1353 std::string errmsg =
1354 fmt::format("Error: edge numbering map already exists in file id {}", exodusFilePtr);
1355 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1356 }
1357 else {
1358 std::string errmsg = fmt::format(
1359 "Error: failed to create edge numbering map in file id {}", exodusFilePtr);
1360 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1361 }
1362 return EX_FATAL;
1363 }
1364 exi_compress_variable(exodusFilePtr, varid, 1);
1365 }
1366 }
1367
1368 // ========================================================================
1369 // Blocks...
1370 if (define_netcdf_vars(exodusFilePtr, "edge block", mesh.edgeblocks.size(), DIM_NUM_ED_BLK,
1371 VAR_STAT_ED_BLK, VAR_ID_ED_BLK, VAR_NAME_ED_BLK) != EX_NOERR) {
1372 return EX_FATAL;
1373 }
1374
1375 if (define_netcdf_vars(exodusFilePtr, "face block", mesh.faceblocks.size(), DIM_NUM_FA_BLK,
1376 VAR_STAT_FA_BLK, VAR_ID_FA_BLK, VAR_NAME_FA_BLK) != EX_NOERR) {
1377 return EX_FATAL;
1378 }
1379
1380 if (define_netcdf_vars(exodusFilePtr, "element block", mesh.elemblocks.size(), DIM_NUM_EL_BLK,
1381 VAR_STAT_EL_BLK, VAR_ID_EL_BLK, VAR_NAME_EL_BLK) != EX_NOERR) {
1382 return EX_FATAL;
1383 }
1384
1385 // ========================================================================
1386 // Sets...
1387 if (define_netcdf_vars(exodusFilePtr, "node set", mesh.nodesets.size(), DIM_NUM_NS, VAR_NS_STAT,
1388 VAR_NS_IDS, VAR_NAME_NS) != EX_NOERR) {
1389 return EX_FATAL;
1390 }
1391
1392 if (define_netcdf_vars(exodusFilePtr, "edge set", mesh.edgesets.size(), DIM_NUM_ES, VAR_ES_STAT,
1393 VAR_ES_IDS, VAR_NAME_ES) != EX_NOERR) {
1394 return EX_FATAL;
1395 }
1396
1397 if (define_netcdf_vars(exodusFilePtr, "face set", mesh.facesets.size(), DIM_NUM_FS, VAR_FS_STAT,
1398 VAR_FS_IDS, VAR_NAME_FS) != EX_NOERR) {
1399 return EX_FATAL;
1400 }
1401
1402 if (define_netcdf_vars(exodusFilePtr, "element set", mesh.elemsets.size(), DIM_NUM_ELS,
1403 VAR_ELS_STAT, VAR_ELS_IDS, VAR_NAME_ELS) != EX_NOERR) {
1404 return EX_FATAL;
1405 }
1406
1407 // ========================================================================
1408 // side sets...
1409 if (define_netcdf_vars(exodusFilePtr, "side set", mesh.sidesets.size(), DIM_NUM_SS, VAR_SS_STAT,
1410 VAR_SS_IDS, VAR_NAME_SS) != EX_NOERR) {
1411 return EX_FATAL;
1412 }
1413
1414 // ========================================================================
1415 if (!mesh.nodeblocks.empty()) {
1416 if (define_coordinate_vars(exodusFilePtr, mesh.nodeblocks[0].entityCount, numnoddim,
1417 mesh.dimensionality, numdimdim, namestrdim) != EX_NOERR) {
1418 return EX_FATAL;
1419 }
1420 }
1421
1422 // Define dimension for the number of processors
1423 if (comm.outputNemesis) {
1424 if (comm.processorCount > 0) {
1425 int procdim;
1426 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_PROCS, &procdim);
1427 if (status != NC_NOERR) {
1428 int ltempsv = comm.processorCount;
1429 status = nc_def_dim(exodusFilePtr, DIM_NUM_PROCS, ltempsv, &procdim);
1430 if (status != NC_NOERR) {
1431 ex_opts(EX_VERBOSE);
1432 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file ID {}",
1433 DIM_NUM_PROCS, exodusFilePtr);
1434 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1435 return EX_FATAL;
1436 }
1437 }
1438 }
1439
1440 // If this is a parallel file then the status vectors are size 1
1441 int dimid_npf;
1442 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_PROCS_F, &dimid_npf);
1443 if ((status) != NC_NOERR) {
1444 int ltempsv = 1; // 1 processor per file...
1445 status = nc_def_dim(exodusFilePtr, DIM_NUM_PROCS_F, ltempsv, &dimid_npf);
1446 if (status != NC_NOERR) {
1447 ex_opts(EX_VERBOSE);
1448 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file ID {}",
1449 DIM_NUM_PROCS_F, exodusFilePtr);
1450 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1451 return EX_FATAL;
1452 }
1453 }
1454
1455 // Define the file type variable...
1456 status = nc_inq_varid(exodusFilePtr, VAR_FILE_TYPE, &varid);
1457 if (status != NC_NOERR) {
1458 status = nc_def_var(exodusFilePtr, VAR_FILE_TYPE, NC_INT, 0, nullptr, &varid);
1459 if (status != NC_NOERR) {
1460 ex_opts(EX_VERBOSE);
1461 std::string errmsg =
1462 fmt::format("Error: failed to define file type in file ID {}", exodusFilePtr);
1463 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1464 return EX_FATAL;
1465 }
1466 }
1467 exi_set_compact_storage(exodusFilePtr, varid);
1468
1469 // Output the file version
1470 int ierr = exi_put_nemesis_version(exodusFilePtr);
1471 if (ierr < 0) {
1472 return (ierr);
1473 }
1474
1475 if (comm.globalNodes > 0) {
1476 // Define dimension for number of global nodes
1477 size_t ltempsv = comm.globalNodes;
1478 int glonoddim = 0;
1479 status = nc_def_dim(exodusFilePtr, DIM_NUM_NODES_GLOBAL, ltempsv, &glonoddim);
1480 if (status != NC_NOERR) {
1481 ex_opts(EX_VERBOSE);
1482 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file ID {}",
1483 DIM_NUM_NODES_GLOBAL, exodusFilePtr);
1484 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1485 return EX_FATAL;
1486 }
1487 }
1488
1489 if (comm.globalElements > 0) {
1490 // Define dimension for number of global elements
1491 size_t ltempsv = comm.globalElements;
1492 int gloelemdim = 0;
1493 status = nc_def_dim(exodusFilePtr, DIM_NUM_ELEMS_GLOBAL, ltempsv, &gloelemdim);
1494 if (status != NC_NOERR) {
1495 ex_opts(EX_VERBOSE);
1496 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file ID {}",
1497 DIM_NUM_ELEMS_GLOBAL, exodusFilePtr);
1498 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1499 return EX_FATAL;
1500 }
1501 }
1502
1503 // Output the number of global element blocks. This is output as a
1504 // dimension since the vector of global element block IDs is sized
1505 // by this quantity.
1506 {
1507 std::array<const char *, 3> vars{VAR_ELBLK_IDS_GLOBAL, VAR_ELBLK_CNT_GLOBAL, nullptr};
1508 const std::array types{ids_type, bulk_type};
1509
1510 status = define_variables(exodusFilePtr, static_cast<int>(comm.globalElementBlocks),
1511 DIM_NUM_ELBLK_GLOBAL, Data(vars), Data(types));
1512 if (status != EX_NOERR) {
1513 return EX_FATAL;
1514 }
1515 }
1516
1517 // Output the number of global node sets. This is output as a
1518 // dimension since the vector of global element block IDs is sized
1519 // by this quantity.
1520 {
1521 std::array<const char *, 4> vars{VAR_NS_IDS_GLOBAL, VAR_NS_NODE_CNT_GLOBAL,
1522 VAR_NS_DF_CNT_GLOBAL, nullptr};
1523 const std::array types{ids_type, bulk_type, bulk_type};
1524
1525 status = define_variables(exodusFilePtr, static_cast<int>(comm.globalNodeSets),
1526 DIM_NUM_NS_GLOBAL, Data(vars), Data(types));
1527 if (status != EX_NOERR) {
1528 return EX_FATAL;
1529 }
1530 }
1531
1532 // Output the number of global side sets. This is output as a
1533 // dimension since the vector of global element block IDs is sized
1534 // by this quantity.
1535 {
1536 std::array<const char *, 4> vars{VAR_SS_IDS_GLOBAL, VAR_SS_SIDE_CNT_GLOBAL,
1537 VAR_SS_DF_CNT_GLOBAL, nullptr};
1538 const std::array types{ids_type, bulk_type, bulk_type};
1539
1540 status = define_variables(exodusFilePtr, static_cast<int>(comm.globalSideSets),
1541 DIM_NUM_SS_GLOBAL, Data(vars), Data(types));
1542 if (status != EX_NOERR) {
1543 return EX_FATAL;
1544 }
1545 }
1546
1547 // Internal Node status
1548 if (mesh.full_nemesis_data) {
1549 status = conditional_define_variable(exodusFilePtr, VAR_INT_N_STAT, dimid_npf,
1550 &nodeMapVarID[0], NC_INT);
1551 if (status != EX_NOERR) {
1552 return EX_FATAL;
1553 }
1554
1555 // Border node status
1556 status = conditional_define_variable(exodusFilePtr, VAR_BOR_N_STAT, dimid_npf,
1557 &nodeMapVarID[1], NC_INT);
1558 if (status != EX_NOERR) {
1559 return EX_FATAL;
1560 }
1561
1562 // External Node status
1563 status = conditional_define_variable(exodusFilePtr, VAR_EXT_N_STAT, dimid_npf,
1564 &nodeMapVarID[2], NC_INT);
1565 if (status != EX_NOERR) {
1566 return EX_FATAL;
1567 }
1568
1569 // Define the variable IDs for the elemental status vectors
1570 // Internal elements
1571 status = conditional_define_variable(exodusFilePtr, VAR_INT_E_STAT, dimid_npf,
1572 &elementMapVarID[0], NC_INT);
1573 if (status != EX_NOERR) {
1574 return EX_FATAL;
1575 }
1576
1577 // Border elements
1578 status = conditional_define_variable(exodusFilePtr, VAR_BOR_E_STAT, dimid_npf,
1579 &elementMapVarID[1], NC_INT);
1580 if (status != EX_NOERR) {
1581 return EX_FATAL;
1582 }
1583
1584 // Define variable for the internal element information
1585 status = define_variable(exodusFilePtr, comm.elementsInternal, DIM_NUM_INT_ELEMS,
1586 VAR_ELEM_MAP_INT, bulk_type);
1587 if (status != EX_NOERR) {
1588 return EX_FATAL;
1589 }
1590
1591 // Define variable for the border element information
1592 status = define_variable(exodusFilePtr, comm.elementsBorder, DIM_NUM_BOR_ELEMS,
1593 VAR_ELEM_MAP_BOR, bulk_type);
1594 if (status != EX_NOERR) {
1595 return EX_FATAL;
1596 }
1597
1598 // Define variable for vector of internal FEM node IDs
1599 status = define_variable(exodusFilePtr, comm.nodesInternal, DIM_NUM_INT_NODES,
1600 VAR_NODE_MAP_INT, bulk_type);
1601 if (status != EX_NOERR) {
1602 return EX_FATAL;
1603 }
1604
1605 // Define variable for vector of border FEM node IDs
1606 status = define_variable(exodusFilePtr, comm.nodesBorder, DIM_NUM_BOR_NODES, VAR_NODE_MAP_BOR,
1607 bulk_type);
1608 if (status != EX_NOERR) {
1609 return EX_FATAL;
1610 }
1611
1612 // Define dimension for vector of external FEM node IDs
1613 status = define_variable(exodusFilePtr, comm.nodesExternal, DIM_NUM_EXT_NODES,
1614 VAR_NODE_MAP_EXT, bulk_type);
1615 if (status != EX_NOERR) {
1616 return EX_FATAL;
1617 }
1618 }
1619
1620 // Add the nodal communication map count
1621
1622 size_t ncnt_cmap = 0;
1623 for (const auto &elem : comm.nodeMap) {
1624 ncnt_cmap += elem.entityCount;
1625 }
1626
1627 {
1628 std::array<const char *, 4> vars{VAR_N_COMM_IDS, VAR_N_COMM_STAT, VAR_N_COMM_DATA_IDX,
1629 nullptr};
1630 const std::array types{ids_type, NC_INT, bulk_type};
1631
1632 status = define_variables(exodusFilePtr, static_cast<int>(comm.nodeMap.size()),
1633 DIM_NUM_N_CMAPS, Data(vars), Data(types));
1634 if (status != EX_NOERR) {
1635 return EX_FATAL;
1636 }
1637 }
1638 {
1639 std::array<const char *, 3> vars{VAR_N_COMM_NIDS, VAR_N_COMM_PROC, nullptr};
1640 const std::array types{ids_type, NC_INT};
1641
1642 // Add dimensions for all of the nodal communication maps
1643 status = define_variables(exodusFilePtr, ncnt_cmap, DIM_NCNT_CMAP, Data(vars), Data(types));
1644 if (status != EX_NOERR) {
1645 return EX_FATAL;
1646 }
1647 }
1648
1649 // Add the nodal communication map count
1650 size_t ecnt_cmap = 0;
1651 for (const auto &elem : comm.elementMap) {
1652 ecnt_cmap += elem.entityCount;
1653 }
1654
1655 if (mesh.full_nemesis_data) {
1656 {
1657 std::array<const char *, 4> vars{VAR_E_COMM_IDS, VAR_E_COMM_STAT, VAR_E_COMM_DATA_IDX,
1658 nullptr};
1659 const std::array types{ids_type, NC_INT, bulk_type};
1660
1661 status = define_variables(exodusFilePtr, static_cast<int>(comm.elementMap.size()),
1662 DIM_NUM_E_CMAPS, Data(vars), Data(types));
1663 if (status != EX_NOERR) {
1664 return EX_FATAL;
1665 }
1666 }
1667 {
1668 std::array<const char *, 4> vars{VAR_E_COMM_EIDS, VAR_E_COMM_PROC, VAR_E_COMM_SIDS,
1669 nullptr};
1670 const std::array types{ids_type, NC_INT, bulk_type};
1671 status = define_variables(exodusFilePtr, ecnt_cmap, DIM_ECNT_CMAP, Data(vars), Data(types));
1672 if (status != EX_NOERR) {
1673 return EX_FATAL;
1674 }
1675 }
1676 }
1677 }
1678 return EX_NOERR;
1679 }
1680
1681 int Internals::put_metadata(const std::vector<Assembly> &assemblies)
1682 {
1683 if (assemblies.empty()) {
1684 return EX_NOERR;
1685 }
1686 int status;
1687 if ((status = exi_check_valid_file_id(exodusFilePtr, __func__)) != EX_NOERR) {
1688 std::string errmsg = fmt::format("Error: Invalid exodus file handle: {}", exodusFilePtr);
1689 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1690 return EX_FATAL;
1691 }
1692
1693 int int_type = NC_INT;
1694 if (ex_int64_status(exodusFilePtr) & EX_IDS_INT64_DB) {
1695 int_type = NC_INT64;
1696 }
1697
1698 for (const auto &assembly : assemblies) {
1699 char *numentryptr = DIM_NUM_ENTITY_ASSEMBLY(assembly.id);
1700
1701 /* define dimensions and variables */
1702 int dimid;
1703 status = nc_def_dim(exodusFilePtr, numentryptr, assembly.entityCount, &dimid);
1704 if (status != NC_NOERR) {
1705 ex_opts(EX_VERBOSE);
1706 std::string errmsg = fmt::format(
1707 "Error: failed to define number of entities in assembly in file id {}", exodusFilePtr);
1708 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1709 return EX_FATAL;
1710 }
1711
1712 /* create variable array in which to store the entry lists */
1713 int entlst_id;
1714 std::array dims{dimid};
1715 if ((status = nc_def_var(exodusFilePtr, VAR_ENTITY_ASSEMBLY(assembly.id), int_type, 1,
1716 Data(dims), &entlst_id)) != NC_NOERR) {
1717 std::string errmsg = fmt::format(
1718 "Error: failed to define entity assembly variable in file id {}", exodusFilePtr);
1719 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1720 return EX_FATAL;
1721 }
1722 exi_compress_variable(exodusFilePtr, entlst_id, 1);
1723
1724 if (ex_int64_status(exodusFilePtr) & EX_IDS_INT64_DB) {
1725 long long tmp = assembly.id;
1726 status = nc_put_att_longlong(exodusFilePtr, entlst_id, EX_ATTRIBUTE_ID, NC_INT64, 1, &tmp);
1727 }
1728 else {
1729 int id = assembly.id;
1730 status = nc_put_att_int(exodusFilePtr, entlst_id, EX_ATTRIBUTE_ID, NC_INT, 1, &id);
1731 }
1732 if (status != NC_NOERR) {
1733 ex_opts(EX_VERBOSE);
1734 std::string errmsg = fmt::format("Error: failed to define '{}' attribute to file id {}",
1735 EX_ATTRIBUTE_ID, exodusFilePtr);
1736 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1737 return EX_FATAL;
1738 }
1739
1740 int type = assembly.type;
1741 status = nc_put_att_int(exodusFilePtr, entlst_id, EX_ATTRIBUTE_TYPE, NC_INT, 1, &type);
1742 if (status != NC_NOERR) {
1743 ex_opts(EX_VERBOSE);
1744 std::string errmsg = fmt::format("Error: failed to define '{}' attribute to file id {}",
1745 EX_ATTRIBUTE_TYPE, exodusFilePtr);
1746 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1747 return EX_FATAL;
1748 }
1749
1750 status = nc_put_att_text(exodusFilePtr, entlst_id, EX_ATTRIBUTE_NAME, assembly.name.size() + 1,
1751 assembly.name.c_str());
1752 if (status != NC_NOERR) {
1753 ex_opts(EX_VERBOSE);
1754 std::string errmsg = fmt::format("Error: failed to define '{}' attribute to file id {}",
1755 EX_ATTRIBUTE_NAME, exodusFilePtr);
1756 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1757 return EX_FATAL;
1758 }
1759
1760 {
1761 char *contains = ex_name_of_object(assembly.type);
1762 status = nc_put_att_text(exodusFilePtr, entlst_id, EX_ATTRIBUTE_TYPENAME,
1763 strlen(contains) + 1, contains);
1764 if (status != NC_NOERR) {
1765 ex_opts(EX_VERBOSE);
1766 std::string errmsg = fmt::format("Error: failed to define '{}' attribute to file id {}",
1767 EX_ATTRIBUTE_TYPENAME, exodusFilePtr);
1768 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1769 return EX_FATAL;
1770 }
1771 }
1772
1773 /* Increment assembly count */
1774 struct exi_file_item *file = exi_find_file_item(exodusFilePtr);
1775 if (file != nullptr) {
1776 file->assembly_count++;
1777 }
1778 }
1779 return EX_NOERR;
1780 }
1781
1782 int Internals::put_metadata(const std::vector<Blob> &blobs)
1783 {
1784 if (blobs.empty()) {
1785 return EX_NOERR;
1786 }
1787
1788 int status;
1789
1790 int n1dim;
1791 if ((status = nc_def_dim(exodusFilePtr, DIM_N1, 1L, &n1dim)) != NC_NOERR) {
1792 ex_opts(EX_VERBOSE);
1793 std::string errmsg =
1794 fmt::format("Error: failed to define number \"1\" dimension in file id {}", exodusFilePtr);
1795 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1796 return EX_FATAL;
1797 }
1798
1799 for (const auto &blob : blobs) {
1800 char *numentryptr = DIM_NUM_VALUES_BLOB(blob.id);
1801
1802 // define dimensions and variables
1803 int dimid;
1804 if ((status = nc_def_dim(exodusFilePtr, numentryptr, blob.entityCount, &dimid)) != NC_NOERR) {
1805 ex_opts(EX_VERBOSE);
1806 std::string errmsg =
1807 fmt::format("Error: failed to define number of entries in blob {} in file id {}", blob.id,
1808 exodusFilePtr);
1809 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1810 return EX_FATAL;
1811 }
1812
1813 // create a variable just as a way to have a blob and its attributes; values not used for
1814 // anything
1815 std::array dims{n1dim};
1816 int entlst;
1817 if ((status = nc_def_var(exodusFilePtr, VAR_ENTITY_BLOB(blob.id), NC_INT, 1, Data(dims),
1818 &entlst)) != NC_NOERR) {
1819 std::string errmsg = fmt::format("Error: failed to create entity for blob {} in file id {}",
1820 blob.id, exodusFilePtr);
1821 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1822 return EX_FATAL;
1823 }
1824 exi_set_compact_storage(exodusFilePtr, entlst);
1825
1826 if (ex_int64_status(exodusFilePtr) & EX_IDS_INT64_DB) {
1827 long long tmp = blob.id;
1828 status = nc_put_att_longlong(exodusFilePtr, entlst, EX_ATTRIBUTE_ID, NC_INT64, 1, &tmp);
1829 }
1830 else {
1831 int id = blob.id;
1832 status = nc_put_att_int(exodusFilePtr, entlst, EX_ATTRIBUTE_ID, NC_INT, 1, &id);
1833 }
1834 if (status != NC_NOERR) {
1835 std::string errmsg =
1836 fmt::format("Error: failed to store blob id {} in file id {}", blob.id, exodusFilePtr);
1837 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1838 return EX_FATAL;
1839 }
1840
1841 if ((status = nc_put_att_text(exodusFilePtr, entlst, EX_ATTRIBUTE_NAME, blob.name.length() + 1,
1842 blob.name.c_str())) != NC_NOERR) {
1843 std::string errmsg = fmt::format("Error: failed to store blob name {} in file id {}",
1844 blob.name, exodusFilePtr);
1845 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1846 return EX_FATAL;
1847 }
1848 }
1849 return EX_NOERR;
1850 }
1851
1852 int Internals::put_metadata(const std::vector<ElemBlock> &blocks, bool count_only)
1853 {
1854 int status = 0; // clear error code
1855
1856 if (blocks.empty()) {
1857 return EX_NOERR;
1858 }
1859
1860 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
1861
1862 // Get number of element blocks defined for this file
1863 int dimid;
1864 size_t num_elem_blk = 0;
1865 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_EL_BLK, &dimid);
1866 if (status != NC_NOERR) {
1867 ex_opts(EX_VERBOSE);
1868 std::string errmsg =
1869 fmt::format("Error: no element blocks defined in file id {}", exodusFilePtr);
1870 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1871 return EX_FATAL;
1872 }
1873
1874 int namestrdim;
1875 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
1876 if (status != NC_NOERR) {
1877 ex_opts(EX_VERBOSE);
1878 std::string errmsg =
1879 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
1880 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1881 return EX_FATAL;
1882 }
1883
1884 status = nc_inq_dimlen(exodusFilePtr, dimid, &num_elem_blk);
1885 if (status != NC_NOERR) {
1886 ex_opts(EX_VERBOSE);
1887 std::string errmsg =
1888 fmt::format("Error: failed to get number of element blocks in file id {}", exodusFilePtr);
1889 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1890 return EX_FATAL;
1891 }
1892
1893 SMART_ASSERT(blocks.size() == num_elem_blk);
1894
1895 // Iterate over blocks ...
1896 for (size_t iblk = 0; iblk < num_elem_blk; iblk++) {
1897 exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_ELEM_BLOCK));
1898
1899 if (blocks[iblk].entityCount == 0) {
1900 continue;
1901 }
1902
1903 // define some dimensions and variables
1904 int numelbdim;
1905 status = nc_def_dim(exodusFilePtr, DIM_NUM_EL_IN_BLK(iblk + 1), blocks[iblk].entityCount,
1906 &numelbdim);
1907 if (status != NC_NOERR) {
1908 if (status == NC_ENAMEINUSE) { // duplicate entry
1909 ex_opts(EX_VERBOSE);
1910 std::string errmsg = fmt::format("Error: element block {} already defined in file id {}",
1911 blocks[iblk].id, exodusFilePtr);
1912 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1913 }
1914 else {
1915 ex_opts(EX_VERBOSE);
1916 std::string errmsg =
1917 fmt::format("Error: failed to define number of elements/block for block {}"
1918 " file id {}",
1919 blocks[iblk].id, exodusFilePtr);
1920 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1921 }
1922 return EX_FATAL;
1923 }
1924 if (count_only) {
1925 continue;
1926 }
1927
1928 int nelnoddim;
1929 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_EL(iblk + 1), blocks[iblk].nodesPerEntity,
1930 &nelnoddim);
1931 if (status != NC_NOERR) {
1932 ex_opts(EX_VERBOSE);
1933 std::string errmsg =
1934 fmt::format("Error: failed to define number of nodes/element for block {}"
1935 " in file id {}",
1936 blocks[iblk].id, exodusFilePtr);
1937 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1938 return EX_FATAL;
1939 }
1940
1941 // element connectivity array
1942 {
1943 std::array dims{numelbdim, nelnoddim};
1944 int connid = 0;
1945 status = nc_def_var(exodusFilePtr, VAR_CONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
1946 if (status != NC_NOERR) {
1947 ex_opts(EX_VERBOSE);
1948 std::string errmsg = fmt::format("Error: failed to create connectivity array for block {}"
1949 " in file id {}",
1950 blocks[iblk].id, exodusFilePtr);
1951 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1952 return EX_FATAL;
1953 }
1954 exi_compress_variable(exodusFilePtr, connid, 1);
1955
1956 // store element type as attribute of connectivity variable
1957 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
1958 static_cast<int>(std::strlen(blocks[iblk].elType)) + 1,
1959 blocks[iblk].elType);
1960 if (status != NC_NOERR) {
1961 ex_opts(EX_VERBOSE);
1962 std::string errmsg =
1963 fmt::format("Error: failed to store element type name {} in file id {}",
1964 blocks[iblk].elType, exodusFilePtr);
1965 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1966 return EX_FATAL;
1967 }
1968 }
1969
1970 if (blocks[iblk].edgesPerEntity > 0) {
1971 int neledgdim;
1972 status = nc_def_dim(exodusFilePtr, DIM_NUM_EDG_PER_EL(iblk + 1), blocks[iblk].edgesPerEntity,
1973 &neledgdim);
1974 if (status != NC_NOERR) {
1975 ex_opts(EX_VERBOSE);
1976 std::string errmsg =
1977 fmt::format("Error: failed to define number of edges/element for block {}"
1978 " in file id {}",
1979 blocks[iblk].id, exodusFilePtr);
1980 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1981 return EX_FATAL;
1982 }
1983
1984 // element->edge connectivity array
1985 std::array dims{numelbdim, neledgdim};
1986
1987 int connid = 0;
1988 status = nc_def_var(exodusFilePtr, VAR_ECONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
1989 if (status != NC_NOERR) {
1990 ex_opts(EX_VERBOSE);
1991 std::string errmsg =
1992 fmt::format("Error: failed to create element->edge connectivity array for block {}"
1993 " in file id {}",
1994 blocks[iblk].id, exodusFilePtr);
1995 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
1996 return EX_FATAL;
1997 }
1998 exi_compress_variable(exodusFilePtr, connid, 1);
1999 }
2000
2001 if (blocks[iblk].facesPerEntity > 0) {
2002 int nelfacdim;
2003 status = nc_def_dim(exodusFilePtr, DIM_NUM_FAC_PER_EL(iblk + 1), blocks[iblk].facesPerEntity,
2004 &nelfacdim);
2005 if (status != NC_NOERR) {
2006 ex_opts(EX_VERBOSE);
2007 std::string errmsg =
2008 fmt::format("Error: failed to define number of faces/element for block {}"
2009 " in file id {}",
2010 blocks[iblk].id, exodusFilePtr);
2011 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2012 return EX_FATAL;
2013 }
2014
2015 // element->face connectivity array
2016 std::array dims{numelbdim, nelfacdim};
2017
2018 int connid = 0;
2019 status = nc_def_var(exodusFilePtr, VAR_FCONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
2020 if (status != NC_NOERR) {
2021 ex_opts(EX_VERBOSE);
2022 std::string errmsg =
2023 fmt::format("Error: failed to create element->edge connectivity array for block {}"
2024 " in file id {}",
2025 blocks[iblk].id, exodusFilePtr);
2026 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2027 return EX_FATAL;
2028 }
2029 exi_compress_variable(exodusFilePtr, connid, 1);
2030 }
2031
2032 // element attribute array
2033 if (blocks[iblk].attributeCount > 0) {
2034 int numattrdim;
2035 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_BLK(iblk + 1), blocks[iblk].attributeCount,
2036 &numattrdim);
2037 if (status != NC_NOERR) {
2038 ex_opts(EX_VERBOSE);
2039 std::string errmsg = fmt::format("Error: failed to define number of attributes in block {}"
2040 " in file id {}",
2041 blocks[iblk].id, exodusFilePtr);
2042 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2043 return EX_FATAL;
2044 }
2045
2046 {
2047 std::array dims{numelbdim, numattrdim};
2048 int varid = 0;
2049 status = nc_def_var(exodusFilePtr, VAR_ATTRIB(iblk + 1), nc_flt_code(exodusFilePtr), 2,
2050 Data(dims), &varid);
2051 if (status != NC_NOERR) {
2052 ex_opts(EX_VERBOSE);
2053 std::string errmsg =
2054 fmt::format("Error: failed to define attributes for element block {}"
2055 " in file id {}",
2056 blocks[iblk].id, exodusFilePtr);
2057 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2058 return EX_FATAL;
2059 }
2060 exi_compress_variable(exodusFilePtr, varid, 2);
2061
2062 #if defined(PARALLEL_AWARE_EXODUS)
2063 // There is currently a bug in netcdf-4.5.1-devel and earlier
2064 // for partial parallel output of strided arrays in collective
2065 // mode for netcdf-4-based output. If the number of attributes >
2066 // 1 and in parallel mode, set the mode to independent.
2067 if (blocks[iblk].attributeCount > 1) {
2068 struct exi_file_item *file = exi_find_file_item(exodusFilePtr);
2069 if (file && file->is_parallel && file->is_hdf5) {
2070 nc_var_par_access(exodusFilePtr, varid, NC_INDEPENDENT);
2071 }
2072 }
2073 #endif
2074 }
2075
2076 {
2077 // Attribute name array...
2078 std::array dims{numattrdim, namestrdim};
2079 int varid = 0;
2080 status =
2081 nc_def_var(exodusFilePtr, VAR_NAME_ATTRIB(iblk + 1), NC_CHAR, 2, Data(dims), &varid);
2082 if (status != NC_NOERR) {
2083 ex_opts(EX_VERBOSE);
2084 std::string errmsg =
2085 fmt::format("Error: failed to define attribute name array for element block {}"
2086 " in file id {}",
2087 blocks[iblk].id, exodusFilePtr);
2088 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2089 return EX_FATAL;
2090 }
2091 exi_set_compact_storage(exodusFilePtr, varid);
2092 }
2093 }
2094 }
2095 return EX_NOERR;
2096 }
2097
2098 int Internals::put_metadata(const std::vector<FaceBlock> &blocks, bool count_only)
2099 {
2100 int status = 0; // clear error code
2101
2102 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
2103
2104 if (blocks.empty()) {
2105 return EX_NOERR;
2106 }
2107
2108 // Get number of face blocks defined for this file
2109 int dimid;
2110 size_t num_face_blk = 0;
2111 status = nc_inq_dimid(exodusFilePtr, DIM_NUM_FA_BLK, &dimid);
2112 if (status != NC_NOERR) {
2113 ex_opts(EX_VERBOSE);
2114 std::string errmsg = fmt::format("Error: no face blocks defined in file id {}", exodusFilePtr);
2115 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2116 return EX_FATAL;
2117 }
2118
2119 int namestrdim;
2120 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
2121 if (status != NC_NOERR) {
2122 ex_opts(EX_VERBOSE);
2123 std::string errmsg =
2124 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
2125 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2126 return EX_FATAL;
2127 }
2128
2129 status = nc_inq_dimlen(exodusFilePtr, dimid, &num_face_blk);
2130 if (status != NC_NOERR) {
2131 ex_opts(EX_VERBOSE);
2132 std::string errmsg =
2133 fmt::format("Error: failed to get number of face blocks in file id {}", exodusFilePtr);
2134 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2135 return EX_FATAL;
2136 }
2137
2138 SMART_ASSERT(blocks.size() == num_face_blk);
2139
2140 // Iterate over blocks ...
2141 for (size_t iblk = 0; iblk < num_face_blk; iblk++) {
2142 exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_FACE_BLOCK));
2143
2144 if (blocks[iblk].entityCount == 0) {
2145 continue;
2146 }
2147
2148 // define some dimensions and variables
2149 int numelbdim;
2150 status = nc_def_dim(exodusFilePtr, DIM_NUM_FA_IN_FBLK(iblk + 1), blocks[iblk].entityCount,
2151 &numelbdim);
2152 if (status != NC_NOERR) {
2153 if (status == NC_ENAMEINUSE) { // duplicate entry
2154 ex_opts(EX_VERBOSE);
2155 std::string errmsg = fmt::format("Error: face block {} already defined in file id {}",
2156 blocks[iblk].id, exodusFilePtr);
2157 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2158 }
2159 else {
2160 ex_opts(EX_VERBOSE);
2161 std::string errmsg =
2162 fmt::format("Error: failed to define number of faces/block for block {}"
2163 " file id {}",
2164 blocks[iblk].id, exodusFilePtr);
2165 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2166 }
2167 return EX_FATAL;
2168 }
2169 if (count_only) {
2170 continue;
2171 }
2172
2173 int nelnoddim;
2174 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_FA(iblk + 1), blocks[iblk].nodesPerEntity,
2175 &nelnoddim);
2176 if (status != NC_NOERR) {
2177 ex_opts(EX_VERBOSE);
2178 std::string errmsg = fmt::format("Error: failed to define number of nodes/face for block {}"
2179 " in file id {}",
2180 blocks[iblk].id, exodusFilePtr);
2181 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2182 return EX_FATAL;
2183 }
2184
2185 // face attribute array
2186 if (blocks[iblk].attributeCount > 0) {
2187 int numattrdim;
2188 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_FBLK(iblk + 1), blocks[iblk].attributeCount,
2189 &numattrdim);
2190 if (status != NC_NOERR) {
2191 ex_opts(EX_VERBOSE);
2192 std::string errmsg = fmt::format("Error: failed to define number of attributes in block {}"
2193 " in file id {}",
2194 blocks[iblk].id, exodusFilePtr);
2195 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2196 return EX_FATAL;
2197 }
2198
2199 {
2200 std::array dims{numelbdim, numattrdim};
2201 int varid = 0;
2202 status = nc_def_var(exodusFilePtr, VAR_FATTRIB(iblk + 1), nc_flt_code(exodusFilePtr), 2,
2203 Data(dims), &varid);
2204 if (status != NC_NOERR) {
2205 ex_opts(EX_VERBOSE);
2206 std::string errmsg =
2207 fmt::format("Error: failed to define attributes for face block {} in file id {}",
2208 blocks[iblk].id, exodusFilePtr);
2209 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2210 return EX_FATAL;
2211 }
2212 exi_compress_variable(exodusFilePtr, varid, 2);
2213 }
2214 {
2215 // Attribute name array...
2216 std::array dims{numattrdim, namestrdim};
2217 int varid = 0;
2218
2219 status =
2220 nc_def_var(exodusFilePtr, VAR_NAME_FATTRIB(iblk + 1), NC_CHAR, 2, Data(dims), &varid);
2221 if (status != NC_NOERR) {
2222 ex_opts(EX_VERBOSE);
2223 std::string errmsg =
2224 fmt::format("Error: failed to define attribute name array for face block {}"
2225 " in file id {}",
2226 blocks[iblk].id, exodusFilePtr);
2227 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2228 return EX_FATAL;
2229 }
2230 exi_set_compact_storage(exodusFilePtr, varid);
2231 }
2232 }
2233
2234 {
2235 // face connectivity array
2236 std::array dims{numelbdim, nelnoddim};
2237 int connid = 0;
2238 status = nc_def_var(exodusFilePtr, VAR_FBCONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
2239 if (status != NC_NOERR) {
2240 ex_opts(EX_VERBOSE);
2241 std::string errmsg =
2242 fmt::format("Error: failed to create connectivity array for block {} in file id {}",
2243 blocks[iblk].id, exodusFilePtr);
2244 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2245 return EX_FATAL;
2246 }
2247 exi_compress_variable(exodusFilePtr, connid, 1);
2248
2249 // store element type as attribute of connectivity variable
2250 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
2251 static_cast<int>(std::strlen(blocks[iblk].elType)) + 1,
2252 blocks[iblk].elType);
2253 if (status != NC_NOERR) {
2254 ex_opts(EX_VERBOSE);
2255 std::string errmsg =
2256 fmt::format("Error: failed to store element type name {} in file id {}",
2257 blocks[iblk].elType, exodusFilePtr);
2258 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2259 return EX_FATAL;
2260 }
2261 }
2262 }
2263 return EX_NOERR;
2264 }
2265
2266 int Internals::put_metadata(const std::vector<EdgeBlock> &blocks, bool count_only)
2267 {
2268 if (blocks.empty()) {
2269 return EX_NOERR;
2270 }
2271
2272 // Get number of edge blocks defined for this file
2273 int dimid;
2274 size_t num_edge_blk = 0;
2275 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_ED_BLK, &dimid);
2276 if (status != NC_NOERR) {
2277 ex_opts(EX_VERBOSE);
2278 std::string errmsg = fmt::format("Error: no edge blocks defined in file id {}", exodusFilePtr);
2279 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2280 return EX_FATAL;
2281 }
2282
2283 int namestrdim;
2284 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
2285 if (status != NC_NOERR) {
2286 ex_opts(EX_VERBOSE);
2287 std::string errmsg =
2288 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
2289 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2290 return EX_FATAL;
2291 }
2292
2293 status = nc_inq_dimlen(exodusFilePtr, dimid, &num_edge_blk);
2294 if (status != NC_NOERR) {
2295 ex_opts(EX_VERBOSE);
2296 std::string errmsg =
2297 fmt::format("Error: failed to get number of edge blocks in file id {}", exodusFilePtr);
2298 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2299 return EX_FATAL;
2300 }
2301
2302 SMART_ASSERT(blocks.size() == num_edge_blk);
2303
2304 // Iterate over blocks ...
2305 for (size_t iblk = 0; iblk < num_edge_blk; iblk++) {
2306 exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_EDGE_BLOCK));
2307
2308 if (blocks[iblk].entityCount == 0) {
2309 continue;
2310 }
2311
2312 // define some dimensions and variables
2313 int numelbdim;
2314 status = nc_def_dim(exodusFilePtr, DIM_NUM_ED_IN_EBLK(iblk + 1), blocks[iblk].entityCount,
2315 &numelbdim);
2316 if (status != NC_NOERR) {
2317 if (status == NC_ENAMEINUSE) { // duplicate entry
2318 ex_opts(EX_VERBOSE);
2319 std::string errmsg = fmt::format("Error: edge block {} already defined in file id {}",
2320 blocks[iblk].id, exodusFilePtr);
2321 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2322 }
2323 else {
2324 ex_opts(EX_VERBOSE);
2325 std::string errmsg =
2326 fmt::format("Error: failed to define number of edges/block for block {}"
2327 " file id {}",
2328 blocks[iblk].id, exodusFilePtr);
2329 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2330 }
2331 return EX_FATAL;
2332 }
2333 if (count_only) {
2334 continue;
2335 }
2336
2337 int nelnoddim;
2338 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_ED(iblk + 1), blocks[iblk].nodesPerEntity,
2339 &nelnoddim);
2340 if (status != NC_NOERR) {
2341 ex_opts(EX_VERBOSE);
2342 std::string errmsg =
2343 fmt::format("Error: failed to define number of nodes/edge ({}) for block {}"
2344 " in file id {}",
2345 blocks[iblk].nodesPerEntity, blocks[iblk].id, exodusFilePtr);
2346 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2347 return EX_FATAL;
2348 }
2349
2350 // edge attribute array
2351 if (blocks[iblk].attributeCount > 0) {
2352 int numattrdim;
2353 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_EBLK(iblk + 1), blocks[iblk].attributeCount,
2354 &numattrdim);
2355 if (status != NC_NOERR) {
2356 ex_opts(EX_VERBOSE);
2357 std::string errmsg = fmt::format("Error: failed to define number of attributes in block {}"
2358 " in file id {}",
2359 blocks[iblk].id, exodusFilePtr);
2360 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2361 return EX_FATAL;
2362 }
2363
2364 std::array dims{numelbdim, numattrdim};
2365 int varid = 0;
2366 status = nc_def_var(exodusFilePtr, VAR_EATTRIB(iblk + 1), nc_flt_code(exodusFilePtr), 2,
2367 Data(dims), &varid);
2368 if (status != NC_NOERR) {
2369 ex_opts(EX_VERBOSE);
2370 std::string errmsg =
2371 fmt::format("Error: failed to define attributes for edge block {} in file id {}",
2372 blocks[iblk].id, exodusFilePtr);
2373 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2374 return EX_FATAL;
2375 }
2376 exi_compress_variable(exodusFilePtr, varid, 2);
2377
2378 // Attribute name array...
2379 dims[0] = numattrdim;
2380 dims[1] = namestrdim;
2381
2382 status =
2383 nc_def_var(exodusFilePtr, VAR_NAME_EATTRIB(iblk + 1), NC_CHAR, 2, Data(dims), &varid);
2384 if (status != NC_NOERR) {
2385 ex_opts(EX_VERBOSE);
2386 std::string errmsg =
2387 fmt::format("Error: failed to define attribute name array for edge block {}"
2388 " in file id {}",
2389 blocks[iblk].id, exodusFilePtr);
2390 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2391 return EX_FATAL;
2392 }
2393 exi_set_compact_storage(exodusFilePtr, varid);
2394 }
2395
2396 // edge connectivity array
2397 std::array dims{numelbdim, nelnoddim};
2398
2399 int connid = 0;
2400 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
2401 status = nc_def_var(exodusFilePtr, VAR_EBCONN(iblk + 1), bulk_type, 2, Data(dims), &connid);
2402 if (status != NC_NOERR) {
2403 ex_opts(EX_VERBOSE);
2404 std::string errmsg =
2405 fmt::format("Error: failed to create connectivity array for block {} in file id {}",
2406 blocks[iblk].id, exodusFilePtr);
2407 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2408 return EX_FATAL;
2409 }
2410 exi_compress_variable(exodusFilePtr, connid, 1);
2411
2412 // store element type as attribute of connectivity variable
2413 status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB,
2414 static_cast<int>(std::strlen(blocks[iblk].elType)) + 1,
2415 blocks[iblk].elType);
2416 if (status != NC_NOERR) {
2417 ex_opts(EX_VERBOSE);
2418 std::string errmsg = fmt::format("Error: failed to store element type name {} in file id {}",
2419 blocks[iblk].elType, exodusFilePtr);
2420 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2421 return EX_FATAL;
2422 }
2423 }
2424 return EX_NOERR;
2425 }
2426
2427 int Internals::put_non_define_data(const CommunicationMetaData &comm, bool full_nemesis_data)
2428 {
2429 // Metadata that must be written outside of define mode...
2430 if (comm.outputNemesis) {
2431 int status = 0;
2432
2433 // Output the file type
2434 int varid;
2435 status = nc_inq_varid(exodusFilePtr, VAR_FILE_TYPE, &varid);
2436 if (status != NC_NOERR) {
2437 ex_opts(EX_VERBOSE);
2438 std::string errmsg =
2439 fmt::format("Error: failed to locate file type in file ID {}", exodusFilePtr);
2440 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2441 return EX_FATAL;
2442 }
2443
2444 int lftype = 0; // Parallel file...
2445 status = nc_put_var1_int(exodusFilePtr, varid, nullptr, &lftype);
2446 if (status != NC_NOERR) {
2447 ex_opts(EX_VERBOSE);
2448 std::string errmsg =
2449 fmt::format("Error: unable to output file type variable in file ID {}", exodusFilePtr);
2450 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2451 return EX_FATAL;
2452 }
2453
2454 if (full_nemesis_data) {
2455 int nmstat = comm.nodesInternal == 0 ? 0 : 1;
2456 status = nc_put_var_int(exodusFilePtr, nodeMapVarID[0], &nmstat);
2457 if (status != NC_NOERR) {
2458 ex_opts(EX_VERBOSE);
2459 std::string errmsg = fmt::format(
2460 "Error: failed to output status for internal node map in file ID {}", exodusFilePtr);
2461 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2462 return EX_FATAL;
2463 }
2464
2465 nmstat = comm.nodesBorder == 0 ? 0 : 1;
2466 status = nc_put_var_int(exodusFilePtr, nodeMapVarID[1], &nmstat);
2467 if (status != NC_NOERR) {
2468 ex_opts(EX_VERBOSE);
2469 std::string errmsg = fmt::format(
2470 "Error: failed to output status for border node map in file ID {}", exodusFilePtr);
2471 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2472 return EX_FATAL;
2473 }
2474
2475 nmstat = comm.nodesExternal == 0 ? 0 : 1;
2476 status = nc_put_var_int(exodusFilePtr, nodeMapVarID[2], &nmstat);
2477 if (status != NC_NOERR) {
2478 ex_opts(EX_VERBOSE);
2479 std::string errmsg = fmt::format(
2480 "Error: failed to output status for external node map in file ID {}", exodusFilePtr);
2481 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2482 return EX_FATAL;
2483 }
2484
2485 nmstat = comm.elementsInternal == 0 ? 0 : 1;
2486 status = nc_put_var_int(exodusFilePtr, elementMapVarID[0], &nmstat);
2487 if (status != NC_NOERR) {
2488 ex_opts(EX_VERBOSE);
2489 std::string errmsg = fmt::format(
2490 "Error: failed to output status for internal elem map in file ID {}", exodusFilePtr);
2491 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2492 return EX_FATAL;
2493 }
2494
2495 nmstat = comm.elementsBorder == 0 ? 0 : 1;
2496 status = nc_put_var_int(exodusFilePtr, elementMapVarID[1], &nmstat);
2497 if (status != NC_NOERR) {
2498 ex_opts(EX_VERBOSE);
2499 std::string errmsg = fmt::format(
2500 "Error: failed to output status for border elem map in file ID {}", exodusFilePtr);
2501 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2502 return EX_FATAL;
2503 }
2504 }
2505
2506 size_t ncnt_cmap = 0;
2507 for (const auto &nmap : comm.nodeMap) {
2508 ncnt_cmap += nmap.entityCount;
2509 }
2510
2511 if (!comm.nodeMap.empty() && ncnt_cmap > 0) {
2512 int n_varid;
2513 status = nc_inq_varid(exodusFilePtr, VAR_N_COMM_STAT, &n_varid);
2514 if (status != NC_NOERR) {
2515 ex_opts(EX_VERBOSE);
2516 std::string errmsg =
2517 fmt::format("Error: failed to find variable ID for \"{}\" in file ID {}",
2518 VAR_N_COMM_STAT, exodusFilePtr);
2519 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2520 return EX_FATAL;
2521 }
2522
2523 long long nl_ncnt_cmap = 0;
2524 for (size_t icm = 0; icm < comm.nodeMap.size(); icm++) {
2525
2526 std::array start{icm};
2527 int nmstat = comm.nodeMap[icm].entityCount > 0 ? 1 : 0;
2528 status = nc_put_var1_int(exodusFilePtr, n_varid, Data(start), &nmstat);
2529 if (status != NC_NOERR) {
2530 ex_opts(EX_VERBOSE);
2531 std::string errmsg =
2532 fmt::format("Error: unable to output variable in file ID {}", exodusFilePtr);
2533 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2534 return EX_FATAL;
2535 }
2536
2537 // increment to the next starting position
2538 nl_ncnt_cmap += comm.nodeMap[icm].entityCount;
2539
2540 // fill the cmap data index
2541 status = nc_inq_varid(exodusFilePtr, VAR_N_COMM_DATA_IDX, &commIndexVar);
2542 if (status != NC_NOERR) {
2543 ex_opts(EX_VERBOSE);
2544 std::string errmsg = fmt::format(
2545 "Error: failed to locate node communication map in file id {}", exodusFilePtr);
2546 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2547 return EX_FATAL;
2548 }
2549 status = nc_put_var1_longlong(exodusFilePtr, commIndexVar, Data(start), &nl_ncnt_cmap);
2550
2551 if (status != NC_NOERR) {
2552 ex_opts(EX_VERBOSE);
2553 std::string errmsg = fmt::format(
2554 "Error: failed to output node communication map index in file ID {}", exodusFilePtr);
2555 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2556 return EX_FATAL;
2557 }
2558 } // End "for(icm=0; icm < num_n_comm_maps; icm++)"
2559
2560 // Put Communication set ids...
2561 std::vector<entity_id> node_cmap_ids(comm.nodeMap.size());
2562 for (size_t i = 0; i < comm.nodeMap.size(); i++) {
2563 node_cmap_ids[i] = comm.nodeMap[i].id;
2564 }
2565 if (put_id_array(exodusFilePtr, VAR_N_COMM_IDS, node_cmap_ids) != NC_NOERR) {
2566 return EX_FATAL;
2567 }
2568 }
2569 // Set the status of the elemental communication maps
2570 long long ecnt_cmap = 0;
2571 for (const auto &elem : comm.elementMap) {
2572 ecnt_cmap += elem.entityCount;
2573 }
2574
2575 if (!comm.elementMap.empty() && ecnt_cmap > 0) {
2576
2577 // Get variable ID for elemental status vector
2578 int e_varid;
2579 status = nc_inq_varid(exodusFilePtr, VAR_E_COMM_STAT, &e_varid);
2580 if (status != NC_NOERR) {
2581 ex_opts(EX_VERBOSE);
2582 std::string errmsg =
2583 fmt::format("Error: failed to find variable ID for \"{}\" in file ID {}",
2584 VAR_E_COMM_STAT, exodusFilePtr);
2585 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2586 return EX_FATAL;
2587 }
2588
2589 long long nl_ecnt_cmap = 0; // reset this for index
2590 for (size_t icm = 0; icm < comm.elementMap.size(); icm++) {
2591
2592 std::array start{icm};
2593 auto nmstat = comm.elementMap[icm].entityCount > 0 ? 1 : 0;
2594
2595 status = nc_put_var1_int(exodusFilePtr, e_varid, Data(start), &nmstat);
2596 if (status != NC_NOERR) {
2597 ex_opts(EX_VERBOSE);
2598 std::string errmsg =
2599 fmt::format("Error: unable to output variable in file ID {}", exodusFilePtr);
2600 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2601 return EX_FATAL;
2602 }
2603
2604 // increment to the next starting position
2605 nl_ecnt_cmap += comm.elementMap[icm].entityCount;
2606
2607 // fill the cmap data index
2608 status = nc_inq_varid(exodusFilePtr, VAR_E_COMM_DATA_IDX, &elemCommIndexVar);
2609 if (status != NC_NOERR) {
2610 ex_opts(EX_VERBOSE);
2611 std::string errmsg = fmt::format(
2612 "Error: failed to locate element communication map in file id {}", exodusFilePtr);
2613 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2614 return EX_FATAL;
2615 }
2616 status = nc_put_var1_longlong(exodusFilePtr, elemCommIndexVar, Data(start), &nl_ecnt_cmap);
2617 if (status != NC_NOERR) {
2618 ex_opts(EX_VERBOSE);
2619 std::string errmsg = fmt::format(
2620 "Error: failed to output int elem map index in file ID {}", exodusFilePtr);
2621 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2622 return EX_FATAL;
2623 }
2624 } // End "for(icm=0; icm < num_e_comm_maps; icm++)"
2625
2626 // Get the variable ID for the elemental comm map IDs vector
2627 std::vector<entity_id> elem_cmap_ids(comm.elementMap.size());
2628 for (size_t i = 0; i < comm.elementMap.size(); i++) {
2629 elem_cmap_ids[i] = comm.elementMap[i].id;
2630 }
2631 if (put_id_array(exodusFilePtr, VAR_E_COMM_IDS, elem_cmap_ids) != NC_NOERR) {
2632 return EX_FATAL;
2633 }
2634 }
2635 }
2636 return EX_NOERR;
2637 }
2638
2639 int Internals::put_non_define_data(const std::vector<Blob> &blobs)
2640 {
2641 int status;
2642 int entlst_id;
2643
2644 size_t name_length = 0;
2645 for (const auto &blob : blobs) {
2646 name_length = std::max(name_length, blob.name.length());
2647 if ((status = nc_inq_varid(exodusFilePtr, VAR_ENTITY_BLOB(blob.id), &entlst_id)) != NC_NOERR) {
2648 std::string errmsg =
2649 fmt::format("Error: failed to locate entity list array for blob {} in file id {}",
2650 blob.id, exodusFilePtr);
2651 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2652 return EX_FATAL;
2653 }
2654
2655 long dummy = 0;
2656 if ((status = nc_put_var_long(exodusFilePtr, entlst_id, &dummy)) != EX_NOERR) {
2657 std::string errmsg = fmt::format(
2658 "Error: failed to output dummy value for blob {} in file id {}", blob.id, exodusFilePtr);
2659 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2660 return EX_FATAL;
2661 }
2662 }
2663 exi_update_max_name_length(exodusFilePtr, name_length);
2664 return EX_NOERR;
2665 }
2666
2667 int Internals::put_non_define_data(const std::vector<Assembly> &assemblies)
2668 {
2669 size_t name_length = 0;
2670 for (const auto &assembly : assemblies) {
2671 int status = EX_NOERR;
2672 name_length = std::max(name_length, assembly.name.length());
2673
2674 if (!assembly.memberIdList.empty()) {
2675 int entlst_id = 0;
2676 if ((status = nc_inq_varid(exodusFilePtr, VAR_ENTITY_ASSEMBLY(assembly.id), &entlst_id)) !=
2677 EX_NOERR) {
2678 std::string errmsg =
2679 fmt::format("Error: failed to locate entity list for assembly {} in file id {}",
2680 assembly.id, exodusFilePtr);
2681 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2682 return EX_FATAL;
2683 }
2684 if ((status = nc_put_var_longlong(exodusFilePtr, entlst_id,
2685 (long long int *)Data(assembly.memberIdList))) !=
2686 EX_NOERR) {
2687 std::string errmsg =
2688 fmt::format("Error: failed to output entity list for assembly {} in file {}",
2689 assembly.id, exodusFilePtr);
2690 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2691 return EX_FATAL;
2692 }
2693 }
2694 }
2695 exi_update_max_name_length(exodusFilePtr, name_length);
2696 return EX_NOERR;
2697 }
2698
2699 int Internals::put_non_define_data(const std::vector<ElemBlock> &blocks, bool output_global_data)
2700 {
2701 int num_elem_blk = static_cast<int>(blocks.size()); // Verified via SMART_ASSERT earlier...
2702
2703 if (num_elem_blk > 0) {
2704 // first get id of element block ids array variable
2705 std::vector<entity_id> elem_blk_id(num_elem_blk);
2706 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
2707 elem_blk_id[iblk] = blocks[iblk].id;
2708 }
2709
2710 if (put_id_array(exodusFilePtr, VAR_ID_EL_BLK, elem_blk_id) != NC_NOERR) {
2711 return EX_FATAL;
2712 }
2713
2714 if (output_global_data) {
2715 if (put_id_array(exodusFilePtr, VAR_ELBLK_IDS_GLOBAL, elem_blk_id) != NC_NOERR) {
2716 return EX_FATAL;
2717 }
2718
2719 std::vector<int64_t> counts(num_elem_blk);
2720 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
2721 counts[iblk] = blocks[iblk].globalEntityCount;
2722 }
2723 if (put_int_array(exodusFilePtr, VAR_ELBLK_CNT_GLOBAL, counts) != NC_NOERR) {
2724 return EX_FATAL;
2725 }
2726 }
2727
2728 // Now, write the element block status array
2729 std::vector<int> elem_blk_status(num_elem_blk);
2730 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
2731 elem_blk_status[iblk] = blocks[iblk].entityCount > 0 ? 1 : 0;
2732 }
2733
2734 if (put_int_array(exodusFilePtr, VAR_STAT_EL_BLK, elem_blk_status) != NC_NOERR) {
2735 return EX_FATAL;
2736 }
2737
2738 // TODO: Is this code correct? `text` is never set...
2739 std::string text;
2740 std::array<size_t, 2> start{0, 0};
2741 std::array<size_t, 2> count{1, text.size() + 1};
2742
2743 for (int iblk = 0; iblk < num_elem_blk; iblk++) {
2744 if (blocks[iblk].attributeCount > 0 && blocks[iblk].entityCount > 0) {
2745 int varid;
2746 int status = nc_inq_varid(exodusFilePtr, VAR_NAME_ATTRIB(iblk + 1), &varid);
2747 if (status != NC_NOERR) {
2748 ex_opts(EX_VERBOSE);
2749 std::string errmsg = fmt::format(
2750 "Error: failed to locate variable name attribute in file id {}", exodusFilePtr);
2751 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2752 return EX_FATAL;
2753 }
2754
2755 for (int i = 0; i < blocks[iblk].attributeCount; i++) {
2756 start[0] = i;
2757 nc_put_vara_text(exodusFilePtr, varid, Data(start), Data(count), text.c_str());
2758 }
2759 }
2760 }
2761 }
2762 return EX_NOERR;
2763 }
2764
2765 int Internals::put_non_define_data(const std::vector<FaceBlock> &blocks)
2766 {
2767 int num_face_blk = static_cast<int>(blocks.size()); // Verified via SMART_ASSERT earlier...
2768
2769 if (num_face_blk > 0) {
2770 // first get id of face block ids array variable
2771 std::vector<entity_id> face_blk_id(num_face_blk);
2772 for (int iblk = 0; iblk < num_face_blk; iblk++) {
2773 face_blk_id[iblk] = blocks[iblk].id;
2774 }
2775
2776 if (put_id_array(exodusFilePtr, VAR_ID_FA_BLK, face_blk_id) != NC_NOERR) {
2777 return EX_FATAL;
2778 }
2779
2780 // Now, write the face block status array
2781 std::vector<int> face_blk_status(num_face_blk);
2782 for (int iblk = 0; iblk < num_face_blk; iblk++) {
2783 face_blk_status[iblk] = blocks[iblk].entityCount > 0 ? 1 : 0;
2784 }
2785
2786 if (put_int_array(exodusFilePtr, VAR_STAT_FA_BLK, face_blk_status) != NC_NOERR) {
2787 return EX_FATAL;
2788 }
2789
2790 // TODO: Is this code correct? `text` is never set...
2791 std::string text;
2792 std::array<size_t, 2> start{0, 0};
2793 std::array<size_t, 2> count{1, text.size() + 1};
2794
2795 for (int iblk = 0; iblk < num_face_blk; iblk++) {
2796 if (blocks[iblk].attributeCount > 0 && blocks[iblk].entityCount > 0) {
2797 int varid;
2798 int status = nc_inq_varid(exodusFilePtr, VAR_NAME_FATTRIB(iblk + 1), &varid);
2799 if (status != NC_NOERR) {
2800 ex_opts(EX_VERBOSE);
2801 std::string errmsg = fmt::format(
2802 "Error: failed to locate face variable name attribute in file id {}", exodusFilePtr);
2803 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2804 return EX_FATAL;
2805 }
2806
2807 for (int i = 0; i < blocks[iblk].attributeCount; i++) {
2808 start[0] = i;
2809 nc_put_vara_text(exodusFilePtr, varid, Data(start), Data(count), text.c_str());
2810 }
2811 }
2812 }
2813 }
2814 return EX_NOERR;
2815 }
2816
2817 int Internals::put_non_define_data(const std::vector<EdgeBlock> &blocks)
2818 {
2819 int num_edge_blk = static_cast<int>(blocks.size()); // Verified via SMART_ASSERT earlier...
2820
2821 if (num_edge_blk > 0) {
2822 // first get id of edge block ids array variable
2823 std::vector<entity_id> edge_blk_id(num_edge_blk);
2824 for (int iblk = 0; iblk < num_edge_blk; iblk++) {
2825 edge_blk_id[iblk] = blocks[iblk].id;
2826 }
2827
2828 if (put_id_array(exodusFilePtr, VAR_ID_ED_BLK, edge_blk_id) != NC_NOERR) {
2829 return EX_FATAL;
2830 }
2831
2832 // Now, write the edge block status array
2833 std::vector<int> edge_blk_status(num_edge_blk);
2834 for (int iblk = 0; iblk < num_edge_blk; iblk++) {
2835 edge_blk_status[iblk] = blocks[iblk].entityCount > 0 ? 1 : 0;
2836 }
2837
2838 if (put_int_array(exodusFilePtr, VAR_STAT_ED_BLK, edge_blk_status) != NC_NOERR) {
2839 return EX_FATAL;
2840 }
2841
2842 // TODO: Is this code correct? `text` is never set...
2843 std::string text;
2844 std::array<size_t, 2> start{0, 0};
2845 std::array<size_t, 2> count{1, text.size() + 1};
2846 for (int iblk = 0; iblk < num_edge_blk; iblk++) {
2847 if (blocks[iblk].attributeCount > 0 && blocks[iblk].entityCount > 0) {
2848 int varid;
2849 int status = nc_inq_varid(exodusFilePtr, VAR_NAME_EATTRIB(iblk + 1), &varid);
2850 if (status != NC_NOERR) {
2851 ex_opts(EX_VERBOSE);
2852 std::string errmsg =
2853 fmt::format("Error: failed to locate element variable name attribute in file id {}",
2854 exodusFilePtr);
2855 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2856 return EX_FATAL;
2857 }
2858
2859 for (int i = 0; i < blocks[iblk].attributeCount; i++) {
2860 start[0] = i;
2861 nc_put_vara_text(exodusFilePtr, varid, Data(start), Data(count), text.c_str());
2862 }
2863 }
2864 }
2865 }
2866 return EX_NOERR;
2867 }
2868
2869 // ========================================================================
2870 int Internals::put_metadata(const std::vector<NodeSet> &nodesets, bool count_only)
2871 {
2872 if (nodesets.empty()) {
2873 return EX_NOERR;
2874 }
2875
2876 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
2877
2878 // Get number of node sets defined for this file
2879 int dimid;
2880 int num_node_sets = 0;
2881 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_NS, &dimid);
2882 if (status != NC_NOERR) {
2883 ex_opts(EX_VERBOSE);
2884 if (status == NC_EBADDIM) {
2885 std::string errmsg = fmt::format("Error: no node sets defined for file id {}", exodusFilePtr);
2886 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2887 }
2888 else {
2889 std::string errmsg =
2890 fmt::format("Error: failed to locate node sets defined in file id {}", exodusFilePtr);
2891 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2892 }
2893 return EX_FATAL;
2894 }
2895
2896 // inquire how many node sets are to be stored
2897 num_node_sets = ex_inquire_int(exodusFilePtr, EX_INQ_NODE_SETS);
2898
2899 int namestrdim;
2900 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
2901 if (status != NC_NOERR) {
2902 ex_opts(EX_VERBOSE);
2903 std::string errmsg =
2904 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
2905 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2906 return EX_FATAL;
2907 }
2908
2909 SMART_ASSERT(static_cast<int>(nodesets.size()) == num_node_sets);
2910
2911 for (int i = 0; i < num_node_sets; i++) {
2912
2913 // NOTE: exi_inc_file_item is used to find the number of node sets
2914 // for a specific file and returns that value incremented.
2915 int cur_num_node_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_NODE_SET));
2916
2917 if (nodesets[i].entityCount == 0) {
2918 continue;
2919 }
2920
2921 status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_NS(cur_num_node_sets + 1),
2922 nodesets[i].entityCount, &dimid);
2923 if (status != NC_NOERR) {
2924 ex_opts(EX_VERBOSE);
2925 if (status == NC_ENAMEINUSE) {
2926 std::string errmsg = fmt::format("Error: node set {} already defined in file id {}",
2927 nodesets[i].id, exodusFilePtr);
2928 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2929 }
2930 else {
2931 std::string errmsg =
2932 fmt::format("Error: failed to define number of nodes for set {} in file id {}",
2933 nodesets[i].id, exodusFilePtr);
2934 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2935 }
2936 return EX_FATAL;
2937 }
2938 if (count_only) {
2939 continue;
2940 }
2941
2942 // define variable to store node set node list here instead of in expns
2943 std::array dims1{dimid};
2944 int varid;
2945 status = nc_def_var(exodusFilePtr, VAR_NODE_NS(cur_num_node_sets + 1), bulk_type, 1,
2946 Data(dims1), &varid);
2947 if (status != NC_NOERR) {
2948 ex_opts(EX_VERBOSE);
2949 if (status == NC_ENAMEINUSE) {
2950 std::string errmsg =
2951 fmt::format("Error: node set {} node list already defined in file id {}",
2952 nodesets[i].id, exodusFilePtr);
2953 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2954 }
2955 else {
2956 std::string errmsg =
2957 fmt::format("Error: failed to create node set {} node list in file id {}",
2958 nodesets[i].id, exodusFilePtr);
2959 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2960 }
2961 return EX_FATAL;
2962 }
2963 exi_compress_variable(exodusFilePtr, varid, 1);
2964
2965 // Create variable for distribution factors if required
2966 if (nodesets[i].dfCount > 0) {
2967 // num_dist_per_set should equal num_nodes_per_set
2968 if (nodesets[i].dfCount != nodesets[i].entityCount) {
2969 status = EX_FATAL;
2970 ex_opts(EX_VERBOSE);
2971 std::string errmsg = fmt::format("Error: # dist fact ({}) not equal to # nodes ({}) "
2972 "in node set {} file id {}",
2973 nodesets[i].dfCount, nodesets[i].entityCount,
2974 nodesets[i].id, exodusFilePtr);
2975 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2976 return EX_FATAL;
2977 }
2978 // create variable for distribution factors
2979 status = nc_def_var(exodusFilePtr, VAR_FACT_NS(cur_num_node_sets + 1),
2980 nc_flt_code(exodusFilePtr), 1, Data(dims1), &varid);
2981 if (status != NC_NOERR) {
2982 ex_opts(EX_VERBOSE);
2983 if (status == NC_ENAMEINUSE) {
2984 std::string errmsg =
2985 fmt::format("Error: node set {} dist factors already exist in file id {}",
2986 nodesets[i].id, exodusFilePtr);
2987 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2988 }
2989 else {
2990 std::string errmsg =
2991 fmt::format("Error: failed to create node set {} dist factors in file id {}",
2992 nodesets[i].id, exodusFilePtr);
2993 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
2994 }
2995 return EX_FATAL;
2996 }
2997 exi_compress_variable(exodusFilePtr, varid, 2);
2998 }
2999
3000 if (nodesets[i].attributeCount > 0) {
3001 int numattrdim;
3002 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_NS(cur_num_node_sets + 1),
3003 nodesets[i].attributeCount, &numattrdim);
3004 if (status != NC_NOERR) {
3005 ex_opts(EX_VERBOSE);
3006 std::string errmsg =
3007 fmt::format("Error: failed to define number of attributes in nodeset {}"
3008 " in file id {}",
3009 nodesets[i].id, exodusFilePtr);
3010 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3011 return EX_FATAL;
3012 }
3013
3014 {
3015 std::array dims{dimid, numattrdim};
3016 status = nc_def_var(exodusFilePtr, VAR_NSATTRIB(cur_num_node_sets + 1),
3017 nc_flt_code(exodusFilePtr), 2, Data(dims), &varid);
3018 if (status != NC_NOERR) {
3019 ex_opts(EX_VERBOSE);
3020 std::string errmsg =
3021 fmt::format("Error: failed to define attributes for element nodeset {}"
3022 " in file id {}",
3023 nodesets[i].id, exodusFilePtr);
3024 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3025 return EX_FATAL;
3026 }
3027 exi_compress_variable(exodusFilePtr, varid, 2);
3028 }
3029
3030 {
3031 // Attribute name array...
3032 std::array dims{numattrdim, namestrdim};
3033
3034 status = nc_def_var(exodusFilePtr, VAR_NAME_NSATTRIB(cur_num_node_sets + 1), NC_CHAR, 2,
3035 Data(dims), &varid);
3036 if (status != NC_NOERR) {
3037 ex_opts(EX_VERBOSE);
3038 std::string errmsg =
3039 fmt::format("Error: failed to define attribute name array for nodeset {}"
3040 " in file id {}",
3041 nodesets[i].id, exodusFilePtr);
3042 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3043 return EX_FATAL;
3044 }
3045 exi_set_compact_storage(exodusFilePtr, varid);
3046 }
3047 }
3048 }
3049 return EX_NOERR;
3050 }
3051
3052 // ========================================================================
3053 int Internals::put_metadata(const std::vector<EdgeSet> &edgesets, bool count_only)
3054 {
3055 if (edgesets.empty()) {
3056 return EX_NOERR;
3057 }
3058 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
3059
3060 // Get number of edge sets defined for this file
3061 int dimid;
3062 int num_edge_sets = 0;
3063 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_ES, &dimid);
3064 if (status != NC_NOERR) {
3065 ex_opts(EX_VERBOSE);
3066 if (status == NC_EBADDIM) {
3067 std::string errmsg = fmt::format("Error: no edge sets defined for file id {}", exodusFilePtr);
3068 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3069 }
3070 else {
3071 std::string errmsg =
3072 fmt::format("Error: failed to locate edge sets defined in file id {}", exodusFilePtr);
3073 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3074 }
3075 return EX_FATAL;
3076 }
3077
3078 // inquire how many edge sets are to be stored
3079 num_edge_sets = ex_inquire_int(exodusFilePtr, EX_INQ_EDGE_SETS);
3080
3081 SMART_ASSERT(static_cast<int>(edgesets.size()) == num_edge_sets);
3082
3083 int namestrdim;
3084 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
3085 if (status != NC_NOERR) {
3086 ex_opts(EX_VERBOSE);
3087 std::string errmsg =
3088 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
3089 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3090 return EX_FATAL;
3091 }
3092
3093 for (int i = 0; i < num_edge_sets; i++) {
3094
3095 // NOTE: exi_inc_file_item is used to find the number of edge sets
3096 // for a specific file and returns that value incremented.
3097 int cur_num_edge_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_EDGE_SET));
3098
3099 if (edgesets[i].entityCount == 0) {
3100 continue;
3101 }
3102
3103 status = nc_def_dim(exodusFilePtr, DIM_NUM_EDGE_ES(cur_num_edge_sets + 1),
3104 edgesets[i].entityCount, &dimid);
3105 if (status != NC_NOERR) {
3106 ex_opts(EX_VERBOSE);
3107 if (status == NC_ENAMEINUSE) {
3108 std::string errmsg = fmt::format("Error: edge set {} already defined in file id {}",
3109 edgesets[i].id, exodusFilePtr);
3110 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3111 }
3112 else {
3113 std::string errmsg =
3114 fmt::format("Error: failed to define number of edges for set {} in file id {}",
3115 edgesets[i].id, exodusFilePtr);
3116 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3117 }
3118 return EX_FATAL;
3119 }
3120 if (count_only) {
3121 continue;
3122 }
3123
3124 // define variable to store edge set edge list here instead of in expns
3125 std::array dims1{dimid};
3126 int varid;
3127 status = nc_def_var(exodusFilePtr, VAR_EDGE_ES(cur_num_edge_sets + 1), bulk_type, 1,
3128 Data(dims1), &varid);
3129 if (status != NC_NOERR) {
3130 ex_opts(EX_VERBOSE);
3131 if (status == NC_ENAMEINUSE) {
3132 std::string errmsg =
3133 fmt::format("Error: edge set {} edge list already defined in file id {}",
3134 edgesets[i].id, exodusFilePtr);
3135 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3136 }
3137 else {
3138 std::string errmsg =
3139 fmt::format("Error: failed to create edge set {} edge list in file id {}",
3140 edgesets[i].id, exodusFilePtr);
3141 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3142 }
3143 return EX_FATAL;
3144 }
3145 exi_compress_variable(exodusFilePtr, varid, 1);
3146
3147 // Orientation variable
3148 status = nc_def_var(exodusFilePtr, VAR_ORNT_ES(cur_num_edge_sets + 1), bulk_type, 1,
3149 Data(dims1), &varid);
3150 if (status != NC_NOERR) {
3151 ex_opts(EX_VERBOSE);
3152 if (status == NC_ENAMEINUSE) {
3153 std::string errmsg =
3154 fmt::format("Error: extra list already exists for edge set {} in file id {}",
3155 edgesets[i].id, exodusFilePtr);
3156 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3157 }
3158 else {
3159 std::string errmsg =
3160 fmt::format("Error: failed to create extra list for edge set {} in file id {}",
3161 edgesets[i].id, exodusFilePtr);
3162 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3163 }
3164 return EX_FATAL;
3165 }
3166 exi_compress_variable(exodusFilePtr, varid, 1);
3167
3168 // Create variable for distribution factors if required
3169 if (edgesets[i].dfCount > 0) {
3170 // num_dist_per_set should equal num_edges_per_set
3171 if (edgesets[i].dfCount != edgesets[i].entityCount) {
3172 status = EX_FATAL;
3173 ex_opts(EX_VERBOSE);
3174 std::string errmsg = fmt::format("Error: # dist fact ({}) not equal to # edges ({}) "
3175 "in edge set {} file id {}",
3176 edgesets[i].dfCount, edgesets[i].entityCount,
3177 edgesets[i].id, exodusFilePtr);
3178 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3179 return EX_FATAL;
3180 }
3181 // create variable for distribution factors
3182 status = nc_def_var(exodusFilePtr, VAR_FACT_ES(cur_num_edge_sets + 1),
3183 nc_flt_code(exodusFilePtr), 1, Data(dims1), &varid);
3184 if (status != NC_NOERR) {
3185 ex_opts(EX_VERBOSE);
3186 if (status == NC_ENAMEINUSE) {
3187 std::string errmsg =
3188 fmt::format("Error: edge set {} dist factors already exist in file id {}",
3189 edgesets[i].id, exodusFilePtr);
3190 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3191 }
3192 else {
3193 std::string errmsg =
3194 fmt::format("Error: failed to create edge set {} dist factors in file id {}",
3195 edgesets[i].id, exodusFilePtr);
3196 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3197 }
3198 return EX_FATAL;
3199 }
3200 exi_compress_variable(exodusFilePtr, varid, 2);
3201 }
3202 if (edgesets[i].attributeCount > 0) {
3203 int numattrdim;
3204 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_ES(cur_num_edge_sets + 1),
3205 edgesets[i].attributeCount, &numattrdim);
3206 if (status != NC_NOERR) {
3207 ex_opts(EX_VERBOSE);
3208 std::string errmsg =
3209 fmt::format("Error: failed to define number of attributes in edgeset {}"
3210 " in file id {}",
3211 edgesets[i].id, exodusFilePtr);
3212 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3213 return EX_FATAL;
3214 }
3215
3216 {
3217 std::array dims{dimid, numattrdim};
3218 status = nc_def_var(exodusFilePtr, VAR_ESATTRIB(cur_num_edge_sets + 1),
3219 nc_flt_code(exodusFilePtr), 2, Data(dims), &varid);
3220 if (status != NC_NOERR) {
3221 ex_opts(EX_VERBOSE);
3222 std::string errmsg =
3223 fmt::format("Error: failed to define attributes for element edgeset {}"
3224 " in file id {}",
3225 edgesets[i].id, exodusFilePtr);
3226 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3227 return EX_FATAL;
3228 }
3229 exi_compress_variable(exodusFilePtr, varid, 2);
3230 }
3231
3232 {
3233 // Attribute name array...
3234 std::array dims{numattrdim, namestrdim};
3235
3236 status = nc_def_var(exodusFilePtr, VAR_NAME_ESATTRIB(cur_num_edge_sets + 1), NC_CHAR, 2,
3237 Data(dims), &varid);
3238 if (status != NC_NOERR) {
3239 ex_opts(EX_VERBOSE);
3240 std::string errmsg =
3241 fmt::format("Error: failed to define attribute name array for edgeset {}"
3242 " in file id {}",
3243 edgesets[i].id, exodusFilePtr);
3244 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3245 return EX_FATAL;
3246 }
3247 exi_set_compact_storage(exodusFilePtr, varid);
3248 }
3249 }
3250 }
3251 return EX_NOERR;
3252 }
3253
3254 // ========================================================================
3255 int Internals::put_metadata(const std::vector<FaceSet> &facesets, bool count_only)
3256 {
3257 if (facesets.empty()) {
3258 return EX_NOERR;
3259 }
3260 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
3261
3262 // Get number of face sets defined for this file
3263 int dimid;
3264 int num_face_sets = 0;
3265 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_FS, &dimid);
3266 if (status != NC_NOERR) {
3267 ex_opts(EX_VERBOSE);
3268 if (status == NC_EBADDIM) {
3269 std::string errmsg = fmt::format("Error: no face sets defined for file id {}", exodusFilePtr);
3270 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3271 }
3272 else {
3273 std::string errmsg =
3274 fmt::format("Error: failed to locate face sets defined in file id {}", exodusFilePtr);
3275 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3276 }
3277 return EX_FATAL;
3278 }
3279
3280 // inquire how many face sets are to be stored
3281 num_face_sets = ex_inquire_int(exodusFilePtr, EX_INQ_FACE_SETS);
3282
3283 SMART_ASSERT(static_cast<int>(facesets.size()) == num_face_sets);
3284
3285 int namestrdim;
3286 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
3287 if (status != NC_NOERR) {
3288 ex_opts(EX_VERBOSE);
3289 std::string errmsg =
3290 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
3291 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3292 return EX_FATAL;
3293 }
3294
3295 for (int i = 0; i < num_face_sets; i++) {
3296
3297 // NOTE: exi_inc_file_item is used to find the number of face sets
3298 // for a specific file and returns that value incremented.
3299 int cur_num_face_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_FACE_SET));
3300
3301 if (facesets[i].entityCount == 0) {
3302 continue;
3303 }
3304
3305 status = nc_def_dim(exodusFilePtr, DIM_NUM_FACE_FS(cur_num_face_sets + 1),
3306 facesets[i].entityCount, &dimid);
3307 if (status != NC_NOERR) {
3308 ex_opts(EX_VERBOSE);
3309 if (status == NC_ENAMEINUSE) {
3310 std::string errmsg = fmt::format("Error: face set {} already defined in file id {}",
3311 facesets[i].id, exodusFilePtr);
3312 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3313 }
3314 else {
3315 std::string errmsg =
3316 fmt::format("Error: failed to define number of faces for set {} in file id {}",
3317 facesets[i].id, exodusFilePtr);
3318 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3319 }
3320 return EX_FATAL;
3321 }
3322 if (count_only) {
3323 continue;
3324 }
3325
3326 // define variable to store face set face list here instead of in expns
3327 std::array dims1{dimid};
3328 int varid;
3329 status = nc_def_var(exodusFilePtr, VAR_FACE_FS(cur_num_face_sets + 1), bulk_type, 1,
3330 Data(dims1), &varid);
3331 if (status != NC_NOERR) {
3332 ex_opts(EX_VERBOSE);
3333 if (status == NC_ENAMEINUSE) {
3334 std::string errmsg =
3335 fmt::format("Error: face set {} face list already defined in file id {}",
3336 facesets[i].id, exodusFilePtr);
3337 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3338 }
3339 else {
3340 std::string errmsg =
3341 fmt::format("Error: failed to create face set {} face list in file id {}",
3342 facesets[i].id, exodusFilePtr);
3343 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3344 }
3345 return EX_FATAL;
3346 }
3347 exi_compress_variable(exodusFilePtr, varid, 1);
3348
3349 // Orientation variable
3350 status = nc_def_var(exodusFilePtr, VAR_ORNT_FS(cur_num_face_sets + 1), bulk_type, 1,
3351 Data(dims1), &varid);
3352 if (status != NC_NOERR) {
3353 ex_opts(EX_VERBOSE);
3354 if (status == NC_ENAMEINUSE) {
3355 std::string errmsg =
3356 fmt::format("Error: extra list already exists for face set {} in file id {}",
3357 facesets[i].id, exodusFilePtr);
3358 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3359 }
3360 else {
3361 std::string errmsg =
3362 fmt::format("Error: failed to create extra list for face set {} in file id {}",
3363 facesets[i].id, exodusFilePtr);
3364 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3365 }
3366 return EX_FATAL;
3367 }
3368 exi_compress_variable(exodusFilePtr, varid, 1);
3369
3370 // Create variable for distribution factors if required
3371 if (facesets[i].dfCount > 0) {
3372 // num_dist_per_set should equal num_faces_per_set
3373 if (facesets[i].dfCount != facesets[i].entityCount) {
3374 status = EX_FATAL;
3375 ex_opts(EX_VERBOSE);
3376 std::string errmsg = fmt::format("Error: # dist fact ({}) not equal to # faces ({}) "
3377 "in face set {} file id {}",
3378 facesets[i].dfCount, facesets[i].entityCount,
3379 facesets[i].id, exodusFilePtr);
3380 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3381 return EX_FATAL;
3382 }
3383 // create variable for distribution factors
3384 status = nc_def_var(exodusFilePtr, VAR_FACT_FS(cur_num_face_sets + 1),
3385 nc_flt_code(exodusFilePtr), 1, Data(dims1), &varid);
3386 if (status != NC_NOERR) {
3387 ex_opts(EX_VERBOSE);
3388 if (status == NC_ENAMEINUSE) {
3389 std::string errmsg =
3390 fmt::format("Error: face set {} dist factors already exist in file id {}",
3391 facesets[i].id, exodusFilePtr);
3392 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3393 }
3394 else {
3395 std::string errmsg =
3396 fmt::format("Error: failed to create face set {} dist factors in file id {}",
3397 facesets[i].id, exodusFilePtr);
3398 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3399 }
3400 return EX_FATAL;
3401 }
3402 exi_compress_variable(exodusFilePtr, varid, 2);
3403 }
3404 if (facesets[i].attributeCount > 0) {
3405 int numattrdim;
3406 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_FS(cur_num_face_sets + 1),
3407 facesets[i].attributeCount, &numattrdim);
3408 if (status != NC_NOERR) {
3409 ex_opts(EX_VERBOSE);
3410 std::string errmsg =
3411 fmt::format("Error: failed to define number of attributes in faceset {}"
3412 " in file id {}",
3413 facesets[i].id, exodusFilePtr);
3414 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3415 return EX_FATAL;
3416 }
3417
3418 {
3419 std::array dims{dimid, numattrdim};
3420 status = nc_def_var(exodusFilePtr, VAR_FSATTRIB(cur_num_face_sets + 1),
3421 nc_flt_code(exodusFilePtr), 2, Data(dims), &varid);
3422 if (status != NC_NOERR) {
3423 ex_opts(EX_VERBOSE);
3424 std::string errmsg =
3425 fmt::format("Error: failed to define attributes for element faceset {}"
3426 " in file id {}",
3427 facesets[i].id, exodusFilePtr);
3428 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3429 return EX_FATAL;
3430 }
3431 exi_compress_variable(exodusFilePtr, varid, 2);
3432 }
3433
3434 {
3435 // Attribute name array...
3436 std::array dims{numattrdim, namestrdim};
3437
3438 status = nc_def_var(exodusFilePtr, VAR_NAME_FSATTRIB(cur_num_face_sets + 1), NC_CHAR, 2,
3439 Data(dims), &varid);
3440 if (status != NC_NOERR) {
3441 ex_opts(EX_VERBOSE);
3442 std::string errmsg =
3443 fmt::format("Error: failed to define attribute name array for faceset {}"
3444 " in file id {}",
3445 facesets[i].id, exodusFilePtr);
3446 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3447 return EX_FATAL;
3448 }
3449 exi_set_compact_storage(exodusFilePtr, varid);
3450 }
3451 }
3452 }
3453 return EX_NOERR;
3454 }
3455
3456 // ========================================================================
3457 int Internals::put_metadata(const std::vector<ElemSet> &elemsets, bool count_only)
3458 {
3459 if (elemsets.empty()) {
3460 return EX_NOERR;
3461 }
3462 // Get number of element sets defined for this file
3463 int dimid;
3464 int num_elem_sets = 0;
3465 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_ELS, &dimid);
3466 if (status != NC_NOERR) {
3467 ex_opts(EX_VERBOSE);
3468 if (status == NC_EBADDIM) {
3469 std::string errmsg =
3470 fmt::format("Error: no element sets defined for file id {}", exodusFilePtr);
3471 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3472 }
3473 else {
3474 std::string errmsg =
3475 fmt::format("Error: failed to locate element sets defined in file id {}", exodusFilePtr);
3476 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3477 }
3478 return EX_FATAL;
3479 }
3480
3481 // inquire how many element sets are to be stored
3482 num_elem_sets = ex_inquire_int(exodusFilePtr, EX_INQ_ELEM_SETS);
3483
3484 SMART_ASSERT(static_cast<int>(elemsets.size()) == num_elem_sets);
3485
3486 int namestrdim;
3487 status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim);
3488 if (status != NC_NOERR) {
3489 ex_opts(EX_VERBOSE);
3490 std::string errmsg =
3491 fmt::format("Error: failed to get string length in file id {}", exodusFilePtr);
3492 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3493 return EX_FATAL;
3494 }
3495
3496 for (int i = 0; i < num_elem_sets; i++) {
3497
3498 // NOTE: exi_inc_file_item is used to find the number of elem sets
3499 // for a specific file and returns that value incremented.
3500 int cur_num_elem_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_ELEM_SET));
3501
3502 if (elemsets[i].entityCount == 0) {
3503 continue;
3504 }
3505
3506 status = nc_def_dim(exodusFilePtr, DIM_NUM_ELE_ELS(cur_num_elem_sets + 1),
3507 elemsets[i].entityCount, &dimid);
3508 if (status != NC_NOERR) {
3509 ex_opts(EX_VERBOSE);
3510 if (status == NC_ENAMEINUSE) {
3511 std::string errmsg = fmt::format("Error: elem set {} already defined in file id {}",
3512 elemsets[i].id, exodusFilePtr);
3513 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3514 }
3515 else {
3516 std::string errmsg =
3517 fmt::format("Error: failed to define number of elems for set {} in file id {}",
3518 elemsets[i].id, exodusFilePtr);
3519 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3520 }
3521 return EX_FATAL;
3522 }
3523 if (count_only) {
3524 continue;
3525 }
3526
3527 // define variable to store element set element list here instead of in expns
3528 std::array dims1{dimid};
3529 int varid;
3530 status = nc_def_var(exodusFilePtr, VAR_ELEM_ELS(cur_num_elem_sets + 1),
3531 get_type(exodusFilePtr, EX_BULK_INT64_DB), 1, Data(dims1), &varid);
3532
3533 if (status != NC_NOERR) {
3534 ex_opts(EX_VERBOSE);
3535 if (status == NC_ENAMEINUSE) {
3536 std::string errmsg =
3537 fmt::format("Error: element set {} element list already defined in file id {}",
3538 elemsets[i].id, exodusFilePtr);
3539 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3540 }
3541 else {
3542 std::string errmsg =
3543 fmt::format("Error: failed to create element set {} element list in file id {}",
3544 elemsets[i].id, exodusFilePtr);
3545 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3546 }
3547 return EX_FATAL;
3548 }
3549 exi_compress_variable(exodusFilePtr, varid, 1);
3550
3551 // Create variable for distribution factors if required
3552 if (elemsets[i].dfCount > 0) {
3553 // num_dist_per_set should equal num_elems_per_set
3554 if (elemsets[i].dfCount != elemsets[i].entityCount) {
3555 status = EX_FATAL;
3556 ex_opts(EX_VERBOSE);
3557 std::string errmsg = fmt::format("Error: # dist fact ({}) not equal to # elements ({}) "
3558 "in element set {} file id {}",
3559 elemsets[i].dfCount, elemsets[i].entityCount,
3560 elemsets[i].id, exodusFilePtr);
3561 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3562 return EX_FATAL;
3563 }
3564 // create variable for distribution factors
3565 status = nc_def_var(exodusFilePtr, VAR_FACT_ELS(cur_num_elem_sets + 1),
3566 nc_flt_code(exodusFilePtr), 1, Data(dims1), &varid);
3567 if (status != NC_NOERR) {
3568 ex_opts(EX_VERBOSE);
3569 if (status == NC_ENAMEINUSE) {
3570 std::string errmsg =
3571 fmt::format("Error: element set {} dist factors already exist in file id {}",
3572 elemsets[i].id, exodusFilePtr);
3573 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3574 }
3575 else {
3576 std::string errmsg =
3577 fmt::format("Error: failed to create element set {} dist factors in file id {}",
3578 elemsets[i].id, exodusFilePtr);
3579 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3580 }
3581 return EX_FATAL;
3582 }
3583 exi_compress_variable(exodusFilePtr, varid, 2);
3584 }
3585 if (elemsets[i].attributeCount > 0) {
3586 int numattrdim;
3587 status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_ES(cur_num_elem_sets + 1),
3588 elemsets[i].attributeCount, &numattrdim);
3589 if (status != NC_NOERR) {
3590 ex_opts(EX_VERBOSE);
3591 std::string errmsg =
3592 fmt::format("Error: failed to define number of attributes in elemset {}"
3593 " in file id {}",
3594 elemsets[i].id, exodusFilePtr);
3595 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3596 return EX_FATAL;
3597 }
3598
3599 {
3600 std::array dims{dimid, numattrdim};
3601 status = nc_def_var(exodusFilePtr, VAR_ESATTRIB(cur_num_elem_sets + 1),
3602 nc_flt_code(exodusFilePtr), 2, Data(dims), &varid);
3603 if (status != NC_NOERR) {
3604 ex_opts(EX_VERBOSE);
3605 std::string errmsg =
3606 fmt::format("Error: failed to define attributes for element elemset {}"
3607 " in file id {}",
3608 elemsets[i].id, exodusFilePtr);
3609 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3610 return EX_FATAL;
3611 }
3612 exi_compress_variable(exodusFilePtr, varid, 2);
3613 }
3614
3615 {
3616 // Attribute name array...
3617 std::array dims{numattrdim, namestrdim};
3618
3619 status = nc_def_var(exodusFilePtr, VAR_NAME_ESATTRIB(cur_num_elem_sets + 1), NC_CHAR, 2,
3620 Data(dims), &varid);
3621 if (status != NC_NOERR) {
3622 ex_opts(EX_VERBOSE);
3623 std::string errmsg =
3624 fmt::format("Error: failed to define attribute name array for elemset {}"
3625 " in file id {}",
3626 elemsets[i].id, exodusFilePtr);
3627 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3628 return EX_FATAL;
3629 }
3630 exi_set_compact_storage(exodusFilePtr, varid);
3631 }
3632 }
3633 }
3634 return EX_NOERR;
3635 }
3636
3637 int Internals::put_non_define_data(const std::vector<NodeSet> &nodesets, bool output_global_data)
3638 {
3639 if (nodesets.empty()) {
3640 return EX_NOERR;
3641 }
3642
3643 // Output nodeset ids...
3644 size_t num_nodesets = nodesets.size();
3645 std::vector<entity_id> nodeset_id(num_nodesets);
3646 for (size_t i = 0; i < num_nodesets; i++) {
3647 nodeset_id[i] = nodesets[i].id;
3648 }
3649
3650 if (put_id_array(exodusFilePtr, VAR_NS_IDS, nodeset_id) != NC_NOERR) {
3651 return EX_FATAL;
3652 }
3653
3654 if (output_global_data) {
3655 if (put_id_array(exodusFilePtr, VAR_NS_IDS_GLOBAL, nodeset_id) != NC_NOERR) {
3656 return EX_FATAL;
3657 }
3658
3659 std::vector<int64_t> counts(num_nodesets);
3660 for (size_t iset = 0; iset < num_nodesets; iset++) {
3661 counts[iset] = nodesets[iset].globalEntityCount;
3662 }
3663 if (put_int_array(exodusFilePtr, VAR_NS_NODE_CNT_GLOBAL, counts) != NC_NOERR) {
3664 return EX_FATAL;
3665 }
3666 }
3667
3668 // Now, write the status array
3669 std::vector<int> status(num_nodesets);
3670 for (size_t i = 0; i < num_nodesets; i++) {
3671 status[i] = nodesets[i].entityCount > 0 ? 1 : 0;
3672 }
3673
3674 if (put_int_array(exodusFilePtr, VAR_NS_STAT, status) != NC_NOERR) {
3675 return EX_FATAL;
3676 }
3677
3678 return EX_NOERR;
3679 }
3680
3681 int Internals::put_non_define_data(const std::vector<EdgeSet> &edgesets)
3682 {
3683 if (edgesets.empty()) {
3684 return EX_NOERR;
3685 }
3686
3687 // Output edgeset ids...
3688 size_t num_edgesets = edgesets.size();
3689 std::vector<entity_id> edgeset_id(num_edgesets);
3690 for (size_t i = 0; i < num_edgesets; i++) {
3691 edgeset_id[i] = edgesets[i].id;
3692 }
3693
3694 if (put_id_array(exodusFilePtr, VAR_ES_IDS, edgeset_id) != NC_NOERR) {
3695 return EX_FATAL;
3696 }
3697
3698 // Now, write the status array
3699 std::vector<int> status(num_edgesets);
3700 for (size_t i = 0; i < num_edgesets; i++) {
3701 status[i] = edgesets[i].entityCount > 0 ? 1 : 0;
3702 }
3703
3704 if (put_int_array(exodusFilePtr, VAR_ES_STAT, status) != NC_NOERR) {
3705 return EX_FATAL;
3706 }
3707
3708 return EX_NOERR;
3709 }
3710
3711 int Internals::put_non_define_data(const std::vector<FaceSet> &facesets)
3712 {
3713 if (facesets.empty()) {
3714 return EX_NOERR;
3715 }
3716
3717 // Output faceset ids...
3718 size_t num_facesets = facesets.size();
3719 std::vector<entity_id> faceset_id(num_facesets);
3720 for (size_t i = 0; i < num_facesets; i++) {
3721 faceset_id[i] = facesets[i].id;
3722 }
3723
3724 if (put_id_array(exodusFilePtr, VAR_FS_IDS, faceset_id) != NC_NOERR) {
3725 return EX_FATAL;
3726 }
3727
3728 // Now, write the status array
3729 std::vector<int> status(num_facesets);
3730 for (size_t i = 0; i < num_facesets; i++) {
3731 status[i] = facesets[i].entityCount > 0 ? 1 : 0;
3732 }
3733
3734 if (put_int_array(exodusFilePtr, VAR_FS_STAT, status) != NC_NOERR) {
3735 return EX_FATAL;
3736 }
3737
3738 return EX_NOERR;
3739 }
3740
3741 int Internals::put_non_define_data(const std::vector<ElemSet> &elemsets)
3742 {
3743 if (elemsets.empty()) {
3744 return EX_NOERR;
3745 }
3746
3747 // Output elemset ids...
3748 size_t num_elemsets = elemsets.size();
3749 std::vector<entity_id> elemset_id(num_elemsets);
3750 for (size_t i = 0; i < num_elemsets; i++) {
3751 elemset_id[i] = elemsets[i].id;
3752 }
3753
3754 if (put_id_array(exodusFilePtr, VAR_ELS_IDS, elemset_id) != NC_NOERR) {
3755 return EX_FATAL;
3756 }
3757
3758 // Now, write the status array
3759 std::vector<int> status(num_elemsets);
3760 for (size_t i = 0; i < num_elemsets; i++) {
3761 status[i] = elemsets[i].entityCount > 0 ? 1 : 0;
3762 }
3763
3764 if (put_int_array(exodusFilePtr, VAR_ELS_STAT, status) != NC_NOERR) {
3765 return EX_FATAL;
3766 }
3767
3768 return EX_NOERR;
3769 }
3770
3771 // ========================================================================
3772 int Internals::put_metadata(const std::vector<SideSet> &sidesets, bool count_only)
3773 {
3774 if (sidesets.empty()) {
3775 return EX_NOERR;
3776 }
3777 int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB);
3778
3779 // Get number of side sets defined for this file
3780 int dimid;
3781 int num_side_sets = 0;
3782 int status = nc_inq_dimid(exodusFilePtr, DIM_NUM_SS, &dimid);
3783 if (status != NC_NOERR) {
3784 ex_opts(EX_VERBOSE);
3785 if (status == NC_EBADDIM) {
3786 std::string errmsg = fmt::format("Error: no side sets defined for file id {}", exodusFilePtr);
3787 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3788 }
3789 else {
3790 std::string errmsg =
3791 fmt::format("Error: failed to locate side sets defined in file id {}", exodusFilePtr);
3792 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3793 }
3794 return EX_FATAL;
3795 }
3796
3797 // inquire how many side sets are to be stored
3798 num_side_sets = ex_inquire_int(exodusFilePtr, EX_INQ_SIDE_SETS);
3799
3800 SMART_ASSERT(static_cast<int>(sidesets.size()) == num_side_sets);
3801
3802 for (int i = 0; i < num_side_sets; i++) {
3803
3804 // NOTE: exi_inc_file_item is used to find the number of side sets
3805 // for a specific file and returns that value incremented.
3806 int cur_num_side_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_SIDE_SET));
3807
3808 if (sidesets[i].entityCount == 0) {
3809 continue;
3810 }
3811
3812 status = nc_def_dim(exodusFilePtr, DIM_NUM_SIDE_SS(cur_num_side_sets + 1),
3813 sidesets[i].entityCount, &dimid);
3814 if (status != NC_NOERR) {
3815 ex_opts(EX_VERBOSE);
3816 if (status == NC_ENAMEINUSE) {
3817 std::string errmsg = fmt::format("Error: side set {} already defined in file id {}",
3818 sidesets[i].id, exodusFilePtr);
3819 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3820 }
3821 else {
3822 std::string errmsg =
3823 fmt::format("Error: failed to define number of sides for set {} in file id {}",
3824 sidesets[i].id, exodusFilePtr);
3825 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3826 }
3827 return EX_FATAL;
3828 }
3829 if (count_only) {
3830 continue;
3831 }
3832
3833 std::array dims{dimid};
3834 int varid = 0;
3835 status = nc_def_var(exodusFilePtr, VAR_ELEM_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims),
3836 &varid);
3837 if (status != NC_NOERR) {
3838 ex_opts(EX_VERBOSE);
3839 if (status == NC_ENAMEINUSE) {
3840 std::string errmsg =
3841 fmt::format("Error: side set {} element list already defined in file id {}",
3842 sidesets[i].id, exodusFilePtr);
3843 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3844 }
3845 else {
3846 std::string errmsg =
3847 fmt::format("Error: failed to create side set {} element list in file id {}",
3848 sidesets[i].id, exodusFilePtr);
3849 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3850 }
3851 return EX_FATAL;
3852 }
3853 exi_compress_variable(exodusFilePtr, varid, 1);
3854
3855 // create side list variable for side set
3856 status = nc_def_var(exodusFilePtr, VAR_SIDE_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims),
3857 &varid);
3858 if (status != NC_NOERR) {
3859 ex_opts(EX_VERBOSE);
3860 if (status == NC_ENAMEINUSE) {
3861 std::string errmsg =
3862 fmt::format("Error: side list already exists for side set {} in file id {}",
3863 sidesets[i].id, exodusFilePtr);
3864 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3865 }
3866 else {
3867 std::string errmsg =
3868 fmt::format("Error: failed to create side list for side set {} in file id {}",
3869 sidesets[i].id, exodusFilePtr);
3870 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3871 }
3872 return EX_FATAL;
3873 }
3874 exi_compress_variable(exodusFilePtr, varid, 1);
3875
3876 // Create variable for distribution factors if required
3877 if (sidesets[i].dfCount > 0) {
3878 status = nc_def_dim(exodusFilePtr, DIM_NUM_DF_SS(cur_num_side_sets + 1), sidesets[i].dfCount,
3879 &dimid);
3880 if (status != NC_NOERR) {
3881 ex_opts(EX_VERBOSE);
3882 if (status == NC_ENAMEINUSE) {
3883 std::string errmsg =
3884 fmt::format("Error: side set df count {} already defined in file id {}",
3885 sidesets[i].id, exodusFilePtr);
3886 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3887 }
3888 else {
3889 std::string errmsg =
3890 fmt::format("Error: failed to define side set df count for set {} in file id {}",
3891 sidesets[i].id, exodusFilePtr);
3892 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3893 }
3894 return EX_FATAL;
3895 }
3896
3897 // create distribution factor list variable for side set
3898 dims[0] = dimid;
3899 status = nc_def_var(exodusFilePtr, VAR_FACT_SS(cur_num_side_sets + 1),
3900 nc_flt_code(exodusFilePtr), 1, Data(dims), &varid);
3901 if (status != NC_NOERR) {
3902 ex_opts(EX_VERBOSE);
3903 if (status == NC_ENAMEINUSE) {
3904 std::string errmsg = fmt::format("Error: dist factor list already exists for side set {}"
3905 " in file id {}",
3906 sidesets[i].id, exodusFilePtr);
3907 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3908 }
3909 else {
3910 std::string errmsg =
3911 fmt::format("Error: failed to create dist factor list for side set {}"
3912 " in file id {}",
3913 sidesets[i].id, exodusFilePtr);
3914 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
3915 }
3916 return EX_FATAL;
3917 }
3918 }
3919 exi_compress_variable(exodusFilePtr, varid, 2);
3920 }
3921 return EX_NOERR;
3922 }
3923
3924 int Internals::put_non_define_data(const std::vector<SideSet> &sidesets, bool output_global_data)
3925 {
3926 if (sidesets.empty()) {
3927 return EX_NOERR;
3928 }
3929
3930 // Output sideset ids...
3931 int num_sidesets = static_cast<int>(sidesets.size());
3932 std::vector<entity_id> sideset_id(num_sidesets);
3933 for (int i = 0; i < num_sidesets; i++) {
3934 sideset_id[i] = sidesets[i].id;
3935 }
3936
3937 if (put_id_array(exodusFilePtr, VAR_SS_IDS, sideset_id) != NC_NOERR) {
3938 return EX_FATAL;
3939 }
3940
3941 if (output_global_data) {
3942 if (put_id_array(exodusFilePtr, VAR_SS_IDS_GLOBAL, sideset_id) != NC_NOERR) {
3943 return EX_FATAL;
3944 }
3945
3946 std::vector<int64_t> counts(num_sidesets);
3947 for (int iset = 0; iset < num_sidesets; iset++) {
3948 counts[iset] = sidesets[iset].globalEntityCount;
3949 }
3950 if (put_int_array(exodusFilePtr, VAR_SS_SIDE_CNT_GLOBAL, counts) != NC_NOERR) {
3951 return EX_FATAL;
3952 }
3953 }
3954
3955 // Now, write the status array
3956 std::vector<int> status(num_sidesets);
3957 for (int i = 0; i < num_sidesets; i++) {
3958 status[i] = sidesets[i].entityCount > 0 ? 1 : 0;
3959 }
3960
3961 if (put_int_array(exodusFilePtr, VAR_SS_STAT, status) != NC_NOERR) {
3962 return EX_FATAL;
3963 }
3964
3965 return EX_NOERR;
3966 }
3967
3968 namespace {
3969 template <typename T> int get_max_name_length(const std::vector<T> &entities, int old_max)
3970 {
3971 for (const auto &entity : entities) {
3972 old_max = std::max(old_max, static_cast<int>(entity.name.size()));
3973 }
3974 return (old_max);
3975 }
3976
3977 template <typename T>
3978 int output_names(const std::vector<T> &entities, int exoid, ex_entity_type ent_type)
3979 {
3980 if (!entities.empty()) {
3981 std::vector<char *> names;
3982
3983 names.resize(entities.size());
3984 for (size_t i = 0; i < entities.size(); i++) {
3985 names[i] = (char *)entities[i].name.c_str();
3986 }
3987 return (ex_put_names(exoid, ent_type, Data(names)));
3988 }
3989 return EX_NOERR;
3990 }
3991
3992 int conditional_define_variable(int exodusFilePtr, const char *var, int dimid, int *varid,
3993 nc_type type)
3994 {
3995 int status = nc_inq_varid(exodusFilePtr, var, varid);
3996 if (status != NC_NOERR) {
3997 status = nc_def_var(exodusFilePtr, var, type, 1, &dimid, varid);
3998 if (status != NC_NOERR) {
3999 ex_opts(EX_VERBOSE);
4000 std::string errmsg = fmt::format("Error: Failed to define variable \"{}\" in file ID {}",
4001 var, exodusFilePtr);
4002 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4003 return EX_FATAL;
4004 }
4005 }
4006 exi_compress_variable(exodusFilePtr, *varid, 1);
4007 return EX_NOERR;
4008 }
4009
4010 int define_variable(int exodusFilePtr, int64_t size, const char *dim, const char *var,
4011 nc_type type)
4012 {
4013
4014 if (size > 0) {
4015 std::array<int, 1> dimid;
4016 int status = nc_def_dim(exodusFilePtr, dim, size, Data(dimid));
4017 if (status != NC_NOERR) {
4018 ex_opts(EX_VERBOSE);
4019 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file id {}",
4020 DIM_NUM_BOR_ELEMS, exodusFilePtr);
4021 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4022 return EX_FATAL;
4023 }
4024
4025 int varid;
4026 status = nc_def_var(exodusFilePtr, var, type, 1, Data(dimid), &varid);
4027 if (status != NC_NOERR) {
4028 ex_opts(EX_VERBOSE);
4029 std::string errmsg = fmt::format("Error: failed to define variable \"{}\" in file ID {}",
4030 VAR_ELEM_MAP_BOR, exodusFilePtr);
4031 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4032 return EX_FATAL;
4033 }
4034 exi_compress_variable(exodusFilePtr, varid, 1);
4035 }
4036 return EX_NOERR;
4037 }
4038
4039 int define_variables(int exodusFilePtr, int64_t size, const char *dim, const char *var[],
4040 const nc_type *types)
4041 {
4042 if (size > 0) {
4043 std::array<int, 1> dimid;
4044 int status = nc_def_dim(exodusFilePtr, dim, size, Data(dimid));
4045 if (status != NC_NOERR) {
4046 ex_opts(EX_VERBOSE);
4047 std::string errmsg = fmt::format("Error: failed to dimension \"{}\" in file id {}",
4048 DIM_NUM_BOR_ELEMS, exodusFilePtr);
4049 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4050 return EX_FATAL;
4051 }
4052
4053 int i = 0;
4054 while (var[i] != nullptr) {
4055 int varid;
4056 status = nc_def_var(exodusFilePtr, var[i], types[i], 1, Data(dimid), &varid);
4057 if (status != NC_NOERR) {
4058 ex_opts(EX_VERBOSE);
4059 std::string errmsg = fmt::format("Error: failed to define variable \"{}\" in file ID {}",
4060 var[i], exodusFilePtr);
4061 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4062 return EX_FATAL;
4063 }
4064 exi_set_compact_storage(exodusFilePtr, varid);
4065 i++;
4066 }
4067 }
4068 return EX_NOERR;
4069 }
4070
4071 int put_int_array(int exoid, const char *var_type, const std::vector<int> &array)
4072 {
4073 int var_id;
4074 int status = nc_inq_varid(exoid, var_type, &var_id);
4075 if (status != NC_NOERR) {
4076 ex_opts(EX_VERBOSE);
4077 std::string errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid);
4078 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4079 return EX_FATAL;
4080 }
4081
4082 status = nc_put_var_int(exoid, var_id, Data(array));
4083 if (status != NC_NOERR) {
4084 ex_opts(EX_VERBOSE);
4085 std::string errmsg =
4086 fmt::format("Error: failed to write {} array in file id {}", var_type, exoid);
4087 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4088 return EX_FATAL;
4089 }
4090 return EX_NOERR;
4091 }
4092
4093 int put_int_array(int exoid, const char *var_type, const std::vector<int64_t> &array)
4094 {
4095 int var_id;
4096 int status = nc_inq_varid(exoid, var_type, &var_id);
4097 if (status != NC_NOERR) {
4098 ex_opts(EX_VERBOSE);
4099 std::string errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid);
4100 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4101 return EX_FATAL;
4102 }
4103
4104 status = nc_put_var_longlong(exoid, var_id, (long long *)Data(array));
4105 if (status != NC_NOERR) {
4106 ex_opts(EX_VERBOSE);
4107 std::string errmsg =
4108 fmt::format("Error: failed to write {} array in file id {}", var_type, exoid);
4109 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4110 return EX_FATAL;
4111 }
4112 return EX_NOERR;
4113 }
4114
4115 int put_id_array(int exoid, const char *var_type, const std::vector<entity_id> &ids)
4116 {
4117 int var_id;
4118 int status = nc_inq_varid(exoid, var_type, &var_id);
4119 if (status != NC_NOERR) {
4120 ex_opts(EX_VERBOSE);
4121 std::string errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid);
4122 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4123 return EX_FATAL;
4124 }
4125
4126 int id_type = get_type(exoid, EX_IDS_INT64_API);
4127
4128 if (id_type == NC_INT64) {
4129 status = nc_put_var_longlong(exoid, var_id, (long long int *)Data(ids));
4130 }
4131 else {
4132 // Have entity_id (long long), need ints...
4133 std::vector<int> int_ids(ids.size());
4134 #ifdef _MSC_VER
4135 #pragma warning(push)
4136 #pragma warning(disable : 4244)
4137 #endif
4138 int_ids.assign(ids.begin(), ids.end());
4139 #ifdef _MSC_VER
4140 #pragma warning(pop)
4141 #endif
4142 status = nc_put_var_int(exoid, var_id, Data(int_ids));
4143 }
4144
4145 if (status != NC_NOERR) {
4146 ex_opts(EX_VERBOSE);
4147 std::string errmsg =
4148 fmt::format("Error: failed to write {} array in file id {}", var_type, exoid);
4149 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4150 return EX_FATAL;
4151 }
4152 return EX_NOERR;
4153 }
4154
4155 int define_coordinate_vars(int exodusFilePtr, int64_t nodes, int node_dim, int dimension,
4156 int dim_dim, int str_dim)
4157 {
4158 int varid;
4159
4160 if (nodes > 0) {
4161 // node coordinate arrays -- separate storage...
4162 std::array dim{node_dim};
4163 if (dimension > 0) {
4164 int status = nc_def_var(exodusFilePtr, VAR_COORD_X, nc_flt_code(exodusFilePtr), 1,
4165 Data(dim), &varid);
4166 if (status != NC_NOERR) {
4167 ex_opts(EX_VERBOSE);
4168 std::string errmsg = fmt::format(
4169 "Error: failed to define node x coordinate array in file id {}", exodusFilePtr);
4170 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4171 return EX_FATAL;
4172 }
4173 exi_compress_variable(exodusFilePtr, varid, 2);
4174 }
4175
4176 if (dimension > 1) {
4177 int status = nc_def_var(exodusFilePtr, VAR_COORD_Y, nc_flt_code(exodusFilePtr), 1,
4178 Data(dim), &varid);
4179 if (status != NC_NOERR) {
4180 ex_opts(EX_VERBOSE);
4181 std::string errmsg = fmt::format(
4182 "Error: failed to define node y coordinate array in file id {}", exodusFilePtr);
4183 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4184 return EX_FATAL;
4185 }
4186 exi_compress_variable(exodusFilePtr, varid, 2);
4187 }
4188
4189 if (dimension > 2) {
4190 int status = nc_def_var(exodusFilePtr, VAR_COORD_Z, nc_flt_code(exodusFilePtr), 1,
4191 Data(dim), &varid);
4192 if (status != NC_NOERR) {
4193 ex_opts(EX_VERBOSE);
4194 std::string errmsg = fmt::format(
4195 "Error: failed to define node z coordinate array in file id {}", exodusFilePtr);
4196 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4197 return EX_FATAL;
4198 }
4199 exi_compress_variable(exodusFilePtr, varid, 2);
4200 }
4201 }
4202
4203 // coordinate names array
4204 std::array dim{dim_dim, str_dim};
4205
4206 int status = nc_def_var(exodusFilePtr, VAR_NAME_COOR, NC_CHAR, 2, Data(dim), &varid);
4207 if (status != NC_NOERR) {
4208 ex_opts(EX_VERBOSE);
4209 std::string errmsg =
4210 fmt::format("Error: failed to define coordinate name array in file id {}", exodusFilePtr);
4211 ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status);
4212 return EX_FATAL;
4213 }
4214 exi_set_compact_storage(exodusFilePtr, varid);
4215 return EX_NOERR;
4216 }
4217
4218 int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num,
4219 const char *stat_var, const char *id_var, const char *name_var)
4220 {
4221 if (count == 0) {
4222 return EX_NOERR;
4223 }
4224
4225 size_t sixty_four_kb = 64 * 1024;
4226 int dimid = 0;
4227 int varid = 0;
4228 int namestrdim = 0;
4229
4230 int status = nc_inq_dimid(exoid, DIM_STR_NAME, &namestrdim);
4231 if (status != NC_NOERR) {
4232 std::string errmsg = fmt::format("Error: failed to get string length in file id {}", exoid);
4233 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4234 return EX_FATAL;
4235 }
4236
4237 status = nc_def_dim(exoid, dim_num, count, &dimid);
4238 if (status != NC_NOERR) {
4239 ex_opts(EX_VERBOSE);
4240 std::string errmsg =
4241 fmt::format("Error: failed to define number of {}s in file id {}", type, exoid);
4242 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4243 return EX_FATAL;
4244 }
4245
4246 // id status array:
4247 std::array dim1{dimid};
4248 status = nc_def_var(exoid, stat_var, NC_INT, 1, Data(dim1), &varid);
4249 if (status != NC_NOERR) {
4250 ex_opts(EX_VERBOSE);
4251 std::string errmsg =
4252 fmt::format("Error: failed to define side {} status in file id {}", type, exoid);
4253 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4254 return EX_FATAL;
4255 }
4256 if (count * 4 < sixty_four_kb) {
4257 exi_set_compact_storage(exoid, varid);
4258 }
4259
4260 // id array:
4261 int ids_type = get_type(exoid, EX_IDS_INT64_DB);
4262 int ids_size = ids_type == NC_INT ? 4 : 8;
4263 status = nc_def_var(exoid, id_var, ids_type, 1, Data(dim1), &varid);
4264 if (status != NC_NOERR) {
4265 ex_opts(EX_VERBOSE);
4266 std::string errmsg =
4267 fmt::format("Error: failed to define {} property in file id {}", type, exoid);
4268 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4269 return EX_FATAL;
4270 }
4271 if (count * ids_size < sixty_four_kb) {
4272 exi_set_compact_storage(exoid, varid);
4273 }
4274
4275 // store property name as attribute of property array variable
4276 status = nc_put_att_text(exoid, varid, ATT_PROP_NAME, 3, "ID");
4277 if (status != NC_NOERR) {
4278 ex_opts(EX_VERBOSE);
4279 std::string errmsg = fmt::format("Error: failed to store {} property name {} in file id {}",
4280 type, "ID", exoid);
4281 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4282 return EX_FATAL;
4283 }
4284
4285 if (name_var != nullptr) {
4286 std::array dim{dimid, namestrdim};
4287 status = nc_def_var(exoid, name_var, NC_CHAR, 2, Data(dim), &varid);
4288 if (status != NC_NOERR) {
4289 ex_opts(EX_VERBOSE);
4290 std::string errmsg =
4291 fmt::format("Error: failed to define {} name array in file id {}", type, exoid);
4292 ex_err_fn(exoid, __func__, errmsg.c_str(), status);
4293 return EX_FATAL;
4294 }
4295 exi_set_compact_storage(exoid, varid);
4296 }
4297 return EX_NOERR;
4298 }
4299 } // namespace