Debian Codegraph @

packages/seacas/applications/conjoin/CJ_ExodusFile.C

raw
1 // Copyright(C) 1999-2021, 2023, 2024, 2025 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "CJ_CodeTypes.h" // for StringIdVector, etc
8 #include "CJ_ExodusFile.h"
9 #include "CJ_SystemInterface.h"
10 #include "open_file_limit.h"
11 #include "smart_assert.h"
12
13 #include <climits>
14 #include <cstdlib>
15 #include <fmt/ostream.h>
16 #include <iomanip>
17 #include <iostream>
18 #include <string>
19
20 #include <exodusII.h>
21
22 std::vector<int> Excn::ExodusFile::fileids_;
23 std::vector<std::string> Excn::ExodusFile::filenames_;
24 int Excn::ExodusFile::outputId_ = -1;
25 int Excn::ExodusFile::ioWordSize_ = 0;
26 int Excn::ExodusFile::cpuWordSize_ = 0;
27 std::string Excn::ExodusFile::outputFilename_;
28 bool Excn::ExodusFile::keepOpen_ = false;
29 bool Excn::ExodusFile::usingChangeSets_ = false;
30 int Excn::ExodusFile::maximumNameLength_ = 32;
31 int Excn::ExodusFile::exodusMode_ = 0;
32
33 Excn::ExodusFile::ExodusFile(size_t which) : myLocation_(which)
34 {
35 SMART_ASSERT(which < filenames_.size())(which)(filenames_.size());
36 SMART_ASSERT(fileids_.size() == filenames_.size());
37 if (!keepOpen_ && which != 0) {
38 float version = 0.0;
39 int cpu_word_size = cpuWordSize_;
40 int io_wrd_size = ioWordSize_;
41 SMART_ASSERT(fileids_[which] == -1)(which)(fileids_[which]);
42 fileids_[which] = ex_open(filenames_[which].c_str(), EX_READ | exodusMode_, &cpu_word_size,
43 &io_wrd_size, &version);
44 if (fileids_[which] < 0) {
45 fmt::print(stderr, "ERROR: Cannot open file '{}' - exiting\n", filenames_[which]);
46 exit(1);
47 }
48 ex_set_max_name_length(fileids_[which], maximumNameLength_);
49
50 SMART_ASSERT(io_wrd_size == ioWordSize_);
51 SMART_ASSERT(cpu_word_size == cpuWordSize_);
52 }
53 }
54
55 bool Excn::ExodusFile::ints_64_bit() { return exodusMode_ & EX_ALL_INT64_API; }
56
57 int Excn::ExodusFile::output()
58 {
59 SMART_ASSERT(outputId_ >= 0);
60 return outputId_;
61 }
62
63 Excn::ExodusFile::operator int() const
64 {
65 SMART_ASSERT(fileids_[myLocation_] >= 0);
66 return fileids_[myLocation_];
67 }
68
69 Excn::ExodusFile::~ExodusFile()
70 {
71 try {
72 if (!keepOpen_ && myLocation_ != 0) {
73 SMART_ASSERT(fileids_[myLocation_] > 0)(myLocation_)(fileids_[myLocation_]);
74 ex_close(fileids_[myLocation_]);
75 fileids_[myLocation_] = -1;
76 }
77 }
78 catch (...) {
79 }
80 }
81
82 void Excn::ExodusFile::close_all()
83 {
84 if (usingChangeSets_) {
85 ex_close(fileids_[0]);
86 }
87 else {
88 for (auto &elem : fileids_) {
89 if (elem > 0) {
90 ex_close(elem);
91 }
92 elem = -1;
93 }
94 }
95 ex_close(outputId_);
96 outputId_ = -1;
97 }
98
99 bool Excn::ExodusFile::initialize(const SystemInterface &si)
100 {
101 // See if we can keep files open
102 size_t max_files = open_file_limit() - 1; // We also have an output exodus file...
103 if (si.inputFiles_.size() <= max_files) {
104 keepOpen_ = true;
105 if ((si.debug() & 1) != 0) {
106 fmt::print("Files kept open... (Max open = {})\n\n", max_files);
107 }
108 }
109 else {
110 keepOpen_ = false;
111 fmt::print("Single file mode... (Max open = {})\n\n", max_files);
112 }
113
114 float version = 0.0;
115 int overall_max_name_length = 32;
116
117 if (si.inputFiles_.size() == 1) {
118 // The file should contain multiple change sets which will be concatenated...
119 int cpu_word_size = sizeof(double);
120 int io_wrd_size = sizeof(double);
121 std::string name = si.inputFiles_[0];
122 int exoid = ex_open(name.c_str(), EX_READ, &cpu_word_size, &io_wrd_size, &version);
123 if (exoid < 0) {
124 fmt::print(stderr, "ERROR: Cannot open file '{}'\n", name);
125 return false;
126 }
127
128 int num_change_sets = ex_inquire_int(exoid, EX_INQ_NUM_CHILD_GROUPS);
129 if (num_change_sets > 1) {
130 usingChangeSets_ = true;
131
132 if (io_wrd_size < static_cast<int>(sizeof(float))) {
133 io_wrd_size = sizeof(float);
134 }
135
136 ioWordSize_ = io_wrd_size;
137 cpuWordSize_ = io_wrd_size;
138
139 if (((ex_int64_status(exoid) & EX_ALL_INT64_DB) != 0) || si.ints_64_bit()) {
140 exodusMode_ = EX_ALL_INT64_API;
141 }
142
143 int name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH);
144 if (name_length > overall_max_name_length) {
145 overall_max_name_length = name_length;
146 }
147
148 // create exo names
149 filenames_.resize(num_change_sets);
150 fileids_.resize(num_change_sets, exoid);
151
152 // Get names of change sets...
153 int group_name_length = ex_inquire_int(exoid, EX_INQ_GROUP_NAME_LEN);
154 group_name_length = std::max(32, group_name_length);
155 std::vector<char> group_name(group_name_length + 1, '\0');
156
157 for (int i = 1; i <= num_change_sets; i++) {
158 int idum = 0;
159 float rdum = 0.0;
160 // Get name of this group...
161 int ierr = ex_inquire(exoid + i, EX_INQ_GROUP_NAME, &idum, &rdum, group_name.data());
162 if (ierr != EX_NOERR) {
163 fmt::print(stderr, "ERROR: Could not get name for group {} in input file '{}'\n", i + 1,
164 name);
165 return false;
166 }
167 filenames_[i - 1] = std::string(group_name.data());
168 fileids_[i - 1] = exoid + i;
169 fmt::print("Part {}: '{}'\n", i, filenames_[i - 1]);
170 }
171 }
172 }
173 if (!usingChangeSets_) {
174 // create exo names
175 filenames_.resize(si.inputFiles_.size());
176 fileids_.resize(si.inputFiles_.size(), -1);
177
178 for (size_t p = 0; p < si.inputFiles_.size(); p++) {
179 std::string name = si.inputFiles_[p];
180
181 filenames_[p] = name;
182
183 if (p == 0) {
184 int cpu_word_size = sizeof(float);
185 int io_wrd_size = 0;
186 int exoid = ex_open(filenames_[p].c_str(), EX_READ, &cpu_word_size, &io_wrd_size, &version);
187 if (exoid < 0) {
188 fmt::print(stderr, "ERROR: Cannot open file '{}'\n", filenames_[p]);
189 return false;
190 }
191
192 int name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH);
193 if (name_length > overall_max_name_length) {
194 overall_max_name_length = name_length;
195 }
196
197 if (((ex_int64_status(exoid) & EX_ALL_INT64_DB) != 0) || si.ints_64_bit()) {
198 exodusMode_ = EX_ALL_INT64_API;
199 }
200
201 ex_close(exoid);
202
203 if (io_wrd_size < static_cast<int>(sizeof(float))) {
204 io_wrd_size = sizeof(float);
205 }
206
207 ioWordSize_ = io_wrd_size;
208 cpuWordSize_ = io_wrd_size;
209 }
210
211 if (keepOpen_ || p == 0) {
212 int io_wrd_size = 0;
213 int mode = EX_READ | exodusMode_;
214
215 fileids_[p] = ex_open(filenames_[p].c_str(), mode, &cpuWordSize_, &io_wrd_size, &version);
216 if (fileids_[p] < 0) {
217 fmt::print(stderr, "ERROR: Cannot open file '{}'\n", filenames_[p]);
218 return false;
219 }
220 int num_change_sets = ex_inquire_int(fileids_[p], EX_INQ_NUM_CHILD_GROUPS);
221 if (num_change_sets > 1) {
222 fmt::print(stderr,
223 "ERROR: Cannot (yet) handle multiple input files containing change "
224 "sets.\n\tFile '{}' contains {} change sets.\n",
225 filenames_[p], num_change_sets);
226 return false;
227 }
228
229 SMART_ASSERT(ioWordSize_ == io_wrd_size)(ioWordSize_)(io_wrd_size);
230 }
231
232 fmt::print("Part {}: '{}'\n", p + 1, name);
233 }
234 }
235
236 maximumNameLength_ = overall_max_name_length;
237 if (keepOpen_) {
238 for (size_t p = 0; p < si.inputFiles_.size(); p++) {
239 ex_set_max_name_length(fileids_[p], maximumNameLength_);
240 }
241 }
242 else {
243 ex_set_max_name_length(fileids_[0], maximumNameLength_);
244 }
245 return true;
246 }
247
248 bool Excn::ExodusFile::create_output(const SystemInterface &si)
249 // Create output file...
250 {
251 outputFilename_ = si.outputName_;
252
253 int mode = EX_CLOBBER | exodusMode_;
254 if (si.ints_64_bit()) {
255 mode |= EX_ALL_INT64_DB;
256 }
257
258 if (si.compress_data() > 0 || si.use_netcdf4() || si.szip()) {
259 mode |= EX_NETCDF4;
260 }
261
262 fmt::print("Output: '{}'\n", outputFilename_);
263 outputId_ = ex_create(outputFilename_.c_str(), mode, &cpuWordSize_, &ioWordSize_);
264 if (outputId_ < 0) {
265 fmt::print(stderr, "ERROR: Cannot open file '{}'\n", outputFilename_);
266 return false;
267 }
268
269 if (si.compress_data() > 0) {
270 ex_set_option(outputId_, EX_OPT_COMPRESSION_LEVEL, si.compress_data());
271 ex_set_option(outputId_, EX_OPT_COMPRESSION_SHUFFLE, 1);
272
273 if (si.szip()) {
274 ex_set_option(outputId_, EX_OPT_COMPRESSION_TYPE, EX_COMPRESS_SZIP);
275 }
276 else if (si.zlib()) {
277 ex_set_option(outputId_, EX_OPT_COMPRESSION_TYPE, EX_COMPRESS_ZLIB);
278 }
279 else if (si.zstd()) {
280 ex_set_option(outputId_, EX_OPT_COMPRESSION_TYPE, EX_COMPRESS_ZSTD);
281 }
282 else if (si.bz2()) {
283 ex_set_option(outputId_, EX_OPT_COMPRESSION_TYPE, EX_COMPRESS_BZ2);
284 }
285
286 if (si.quantize()) {
287 ex_set_option(outputId_, EX_OPT_QUANTIZE_NSD, si.quantize_nsd());
288 }
289 }
290
291 fmt::print("IO Word size is {} bytes.\n", ioWordSize_);
292 ex_set_max_name_length(outputId_, maximumNameLength_);
293 return true;
294 }