Debian Codegraph @

packages/seacas/applications/conjoin/CJ_SystemInterface.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 "CJ_SystemInterface.h"
8 #include "CJ_Version.h" // for qainfo
9 #include "SL_tokenize.h" // for tokenize
10 #include <algorithm> // for sort, transform
11 #include <cctype> // for tolower
12 #include <copyright.h>
13 #include <cstddef> // for size_t
14 #include <cstdlib> // for exit, strtol, EXIT_SUCCESS, etc
15 #include <fmt/format.h>
16 #include <term_width.h>
17 #include <utility> // for pair, make_pair
18 #include <vector> // for vector
19
20 namespace {
21 void parse_variable_names(const char *tokens, StringIdVector *variable_list);
22 } // namespace
23
24 Excn::SystemInterface::SystemInterface() { enroll_options(); }
25
26 void Excn::SystemInterface::enroll_options()
27 {
28 options_.usage("[options] list_of_files_to_join");
29
30 options_.enroll("help", GetLongOption::NoValue, "Print this summary and exit", nullptr);
31
32 options_.enroll("version", GetLongOption::NoValue, "Print version and exit", nullptr);
33
34 options_.enroll("output", GetLongOption::MandatoryValue, "Name of output file to create",
35 "conjoin-out.e", nullptr, true);
36
37 options_.enroll("alive_value", GetLongOption::MandatoryValue,
38 "Value (1 or 0) to indicate that an element is alive, default = 0", "0");
39
40 options_.enroll("combine_status_variables", GetLongOption::MandatoryValue,
41 "The conjoin elem_status variable will be combined\n"
42 "\t\twith the specified status variable ($val) existing on the mesh.\n"
43 "\t\tBoth variables must have the same value (1 or 0) for 'alive'.\n"
44 "\t\t\tIf 1 is alive, then the combined variable is the min of the two values.\n"
45 "\t\t\tIf 0 is alive, then the combined variable is the max of the two values.\n"
46 "\t\tUse the 'alive_value' option to set conjoin's alive value",
47 "");
48 options_.enroll("element_status_variable", GetLongOption::MandatoryValue,
49 "Name to use as element existence status variable;\n"
50 "\t\tmust not exist on input files. If NONE, then not created.\n"
51 "\t\tDefault = elem_status",
52 "elem_status");
53
54 options_.enroll("nodal_status_variable", GetLongOption::MandatoryValue,
55 "Name to use as nodal status variable;\n\t\tmust not exist on input files.\n"
56 "\t\tIf NONE, then not created. Default = node_status",
57 "node_status", nullptr, true);
58
59 options_.enroll("netcdf4", GetLongOption::NoValue,
60 "Create output database using the HDF5-based "
61 "netcdf which allows for up to 2.1 GB "
62 "nodes and elements",
63 nullptr);
64
65 options_.enroll("64-bit", GetLongOption::NoValue,
66 "True if forcing the use of 64-bit integers for the output file", nullptr);
67
68 options_.enroll("zlib", GetLongOption::NoValue,
69 "Use the Zlib / libz compression method if compression is enabled (default) "
70 "[exodus only, enables netcdf-4].",
71 nullptr);
72
73 options_.enroll("szip", GetLongOption::NoValue,
74 "Use SZip compression. [exodus only, enables netcdf-4]", nullptr);
75 options_.enroll("zstd", GetLongOption::NoValue,
76 "Use Zstd compression. [exodus only, enables netcdf-4, experimental]", nullptr);
77 options_.enroll("bzip2", GetLongOption::NoValue,
78 "Use Bzip2 compression. [exodus only, enables netcdf-4, experimental]", nullptr);
79
80 options_.enroll("compress", GetLongOption::MandatoryValue,
81 "Specify the compression level to be used. Values depend on algorithm:\n"
82 "\t\tzlib/bzip2: 0..9\t\tszip: even, 4..32\t\tzstd: -131072..22",
83 nullptr);
84
85 options_.enroll("quantize_nsd", GetLongOption::MandatoryValue,
86 "Use the lossy quantize compression method. Value specifies number of digits to "
87 "retain (1..15) [exodus only]",
88 nullptr, nullptr, true);
89
90 options_.enroll("sort_times", GetLongOption::NoValue,
91 "Sort the input files on the minimum timestep time in the file.\n"
92 "\t\tDefault is to process files in the order they appear on the command line.",
93 nullptr);
94
95 options_.enroll("ignore_coordinate_check", GetLongOption::NoValue,
96 "Do not use nodal coordinates to determine if node in part 1 same as node in\n"
97 "\t\tother parts; use ids only.\n"
98 "\t\tUse only if you know that the ids are consistent for all parts",
99 nullptr);
100
101 options_.enroll("omit_nodesets", GetLongOption::NoValue,
102 "Don't transfer nodesets to output file.", nullptr);
103
104 options_.enroll("omit_sidesets", GetLongOption::NoValue,
105 "Don't transfer sidesets to output file.", nullptr, nullptr, true);
106
107 options_.enroll("gvar", GetLongOption::MandatoryValue,
108 "Comma-separated list of global variables to be joined or ALL or NONE.", nullptr);
109
110 options_.enroll("evar", GetLongOption::MandatoryValue,
111 "Comma-separated list of element variables to be joined or ALL or NONE.\n"
112 "\t\tVariables can be limited to certain blocks by appending a\n"
113 "\t\tcolon followed by the block id. E.g. -evar sigxx:10:20",
114 nullptr);
115
116 options_.enroll("nvar", GetLongOption::MandatoryValue,
117 "Comma-separated list of nodal variables to be joined or ALL or NONE.", nullptr);
118
119 options_.enroll("nsetvar", GetLongOption::MandatoryValue,
120 "Comma-separated list of nodeset variables to be joined or ALL or NONE.",
121 nullptr);
122
123 options_.enroll("ssetvar", GetLongOption::MandatoryValue,
124 "Comma-separated list of sideset variables to be joined or ALL or NONE.", nullptr,
125 nullptr, true);
126
127 options_.enroll(
128 "interpart_minimum_time_delta", GetLongOption::MandatoryValue,
129 "If the time delta between the maximum time on one\n\t\tdatabase and the minimum time on "
130 "the next database is less than this value, the\n\t\ttime will not be retained in the output "
131 "file",
132 "0");
133
134 options_.enroll("debug", GetLongOption::MandatoryValue,
135 "debug level (values are or'd)\n"
136 "\t\t 1 = timing information.\n"
137 "\t\t 4 = Verbose Element block information.\n"
138 "\t\t 8 = Check consistent nodal coordinates between parts.\n"
139 "\t\t 16 = Verbose Sideset information.\n"
140 "\t\t 32 = Verbose Nodeset information.\n"
141 "\t\t 64 = put exodus library into verbose mode.\n"
142 "\t\t128 = Check consistent global field values between parts.",
143 "0");
144
145 options_.enroll("width", GetLongOption::MandatoryValue, "Width of output screen, default = 80",
146 nullptr);
147
148 options_.enroll("copyright", GetLongOption::NoValue, "Show copyright and license data.", nullptr);
149 }
150
151 bool Excn::SystemInterface::parse_options(int argc, char **argv)
152 {
153 int option_index = options_.parse(argc, argv);
154 if (option_index < 1) {
155 return false;
156 }
157
158 // Get options from environment variable also...
159 char *options = getenv("CONJOIN_OPTIONS");
160 if (options != nullptr) {
161 fmt::print(
162 "\nThe following options were specified via the CONJOIN_OPTIONS environment variable:\n"
163 "\t{}\n\n",
164 options);
165 options_.parse(options, GetLongOption::basename(*argv));
166 }
167
168 if (options_.retrieve("help") != nullptr) {
169 options_.usage();
170 fmt::print("\n\tCan also set options via CONJOIN_OPTIONS environment variable.\n"
171 "\n\tDocumentation: "
172 "https://sandialabs.github.io/seacas-docs/sphinx/html/index.html#conjoin\n"
173 "\n\t->->-> Send email to [email protected] for conjoin support.<-<-<-\n");
174 exit(EXIT_SUCCESS);
175 }
176
177 if (options_.retrieve("version") != nullptr) {
178 // Version is printed up front, just exit...
179 exit(0);
180 }
181
182 debugLevel_ = options_.get_option_value("debug", debugLevel_);
183
184 {
185 const char *temp = options_.retrieve("alive_value");
186 if (temp != nullptr) {
187 int value = strtol(temp, nullptr, 10);
188 if (value == 1 || value == 0) {
189 aliveValue_ = value;
190 }
191 else {
192 fmt::print(stderr,
193 "\nERROR: Invalid value specified for node and element status."
194 "\nValid values are '1' or '0'. Found '{}'\n",
195 value);
196 exit(EXIT_FAILURE);
197 }
198 }
199 }
200
201 interpartMinimumTimeDelta_ =
202 options_.get_option_value("interpart_minimum_time_delta", interpartMinimumTimeDelta_);
203 elementStatusVariable_ =
204 options_.get_option_value("element_status_variable", elementStatusVariable_);
205 nodalStatusVariable_ = options_.get_option_value("nodal_status_variable", nodalStatusVariable_);
206 meshCombineStatusVariable_ =
207 options_.get_option_value("combine_status_variables", meshCombineStatusVariable_);
208
209 screenWidth_ = options_.get_option_value("width", term_width());
210 outputName_ = options_.get_option_value("output", outputName_);
211
212 {
213 const char *temp = options_.retrieve("gvar");
214 if (temp != nullptr) {
215 parse_variable_names(temp, &globalVarNames_);
216 }
217 }
218
219 {
220 const char *temp = options_.retrieve("nvar");
221 if (temp != nullptr) {
222 parse_variable_names(temp, &nodeVarNames_);
223 }
224 }
225
226 {
227 const char *temp = options_.retrieve("evar");
228 if (temp != nullptr) {
229 parse_variable_names(temp, &elemVarNames_);
230 }
231 }
232
233 {
234 const char *temp = options_.retrieve("nsetvar");
235 if (temp != nullptr) {
236 parse_variable_names(temp, &nsetVarNames_);
237 }
238 }
239
240 {
241 const char *temp = options_.retrieve("ssetvar");
242 if (temp != nullptr) {
243 parse_variable_names(temp, &ssetVarNames_);
244 }
245 }
246
247 useNetcdf4_ = options_.retrieve("netcdf4") != nullptr;
248 sortTimes_ = options_.retrieve("sort_times") != nullptr;
249 ints64Bit_ = options_.retrieve("64-bit") != nullptr;
250 ignoreCoordinates_ = options_.retrieve("ignore_coordinate_check") != nullptr;
251 omitNodesets_ = options_.retrieve("omit_nodesets") != nullptr;
252 omitSidesets_ = options_.retrieve("omit_sidesets") != nullptr;
253
254 zlib_ = (options_.retrieve("zlib") != nullptr);
255 szip_ = (options_.retrieve("szip") != nullptr);
256 zstd_ = (options_.retrieve("zstd") != nullptr);
257 bz2_ = (options_.retrieve("bzip2") != nullptr);
258
259 if (szip_ + zlib_ + zstd_ + bz2_ > 1) {
260 fmt::print(stderr,
261 "ERROR: Only one of 'szip' or 'zlib' or 'zstd' or 'bzip2' can be specified.\n");
262 }
263
264 {
265 const char *temp = options_.retrieve("compress");
266 if (temp != nullptr) {
267 compressionLevel_ = std::strtol(temp, nullptr, 10);
268 if (!szip_ && !zlib_ && !zstd_ && !bz2_) {
269 zlib_ = true;
270 }
271
272 if (zlib_ || bz2_) {
273 if (compressionLevel_ < 0 || compressionLevel_ > 9) {
274 fmt::print(stderr,
275 "ERROR: Bad compression level {}, valid value is between 0 and 9 inclusive "
276 "for gzip/zlib/bzip2 compression.\n",
277 compressionLevel_);
278 return false;
279 }
280 }
281 else if (szip_) {
282 if (compressionLevel_ % 2 != 0) {
283 fmt::print(
284 stderr,
285 "ERROR: Bad compression level {}. Must be an even value for szip compression.\n",
286 compressionLevel_);
287 return false;
288 }
289 if (compressionLevel_ < 4 || compressionLevel_ > 32) {
290 fmt::print(stderr,
291 "ERROR: Bad compression level {}, valid value is between 4 and 32 inclusive "
292 "for szip compression.\n",
293 compressionLevel_);
294 return false;
295 }
296 }
297 }
298 }
299
300 {
301 const char *temp = options_.retrieve("quantize_nsd");
302 if (temp != nullptr) {
303 quantizeNSD_ = std::strtol(temp, nullptr, 10);
304 if (!szip_ && !zlib_ && !zstd_ && !bz2_) {
305 zlib_ = true;
306 }
307 }
308 }
309
310 if (options_.retrieve("copyright") != nullptr) {
311 fmt::print("{}", copyright("2009-2021"));
312 exit(EXIT_SUCCESS);
313 }
314
315 // Parse remaining options as directory paths.
316 if (option_index < argc) {
317 while (option_index < argc) {
318 inputFiles_.emplace_back(argv[option_index++]);
319 }
320 }
321 else {
322 fmt::print(stderr, "\nERROR: no files specified\n\n");
323 return false;
324 }
325 return true;
326 }
327
328 void Excn::SystemInterface::dump(std::ostream & /*unused*/) const {}
329
330 void Excn::SystemInterface::show_version()
331 {
332 fmt::print(
333 "{}\n"
334 "\t(A code for sequentially appending Exodus databases. Supersedes conex and conex2.)\n"
335 "\t(Version: {}) Modified: {}\n",
336 qainfo[0], qainfo[1], qainfo[2]);
337 }
338
339 namespace {
340 std::string LowerCase(const std::string &name)
341 {
342 std::string s = name;
343 std::transform(s.begin(), s.end(), // source
344 s.begin(), // destination
345 ::tolower); // operation
346 return s;
347 }
348
349 using StringVector = std::vector<std::string>;
350 bool string_id_sort(const std::pair<std::string, int> &t1, const std::pair<std::string, int> &t2)
351 {
352 return t1.first < t2.first || (!(t2.first < t1.first) && t1.second < t2.second);
353 }
354
355 void parse_variable_names(const char *tokens, StringIdVector *variable_list)
356 {
357 // Break into tokens separated by ","
358 if (tokens != nullptr) {
359 std::string token_string(tokens);
360 StringVector var_list = SLIB::tokenize(token_string, ",");
361
362 // At this point, var_list is either a single string, or a string
363 // separated from 1 or more block ids with ":" delimiter.
364 // For example, sigxx:1:10:100 would indicate that the variable
365 // "sigxx" should be written only for blocks with id 1, 10, and
366 // 100. "sigxx" would indicate that the variable should be
367 // written for all blocks.
368 auto I = var_list.begin();
369 while (I != var_list.end()) {
370 StringVector name_id = SLIB::tokenize(*I, ":");
371 std::string var_name = LowerCase(name_id[0]);
372 if (name_id.size() == 1) {
373 (*variable_list).emplace_back(var_name, 0);
374 }
375 else {
376 for (size_t i = 1; i < name_id.size(); i++) {
377 // Convert string to integer...
378 int id = std::stoi(name_id[i]);
379 (*variable_list).emplace_back(var_name, id);
380 }
381 }
382 ++I;
383 }
384 // Sort the list...
385 std::sort(variable_list->begin(), variable_list->end(), string_id_sort);
386 }
387 }
388 } // namespace