Debian Codegraph @

packages/seacas/applications/exomatlab/EML_SystemInterface.C

raw
1 /*
2 * Copyright(C) 1999-2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "EML_CodeTypes.h" // for StringIdVector
10 #include "EML_SystemInterface.h"
11 #include "EML_Version.h" // for qainfo
12 #include "GetLongOpt.h" // for GetLongOption, etc
13 #include "SL_tokenize.h" // for tokenize
14 #include <Ioss_FileInfo.h> // for FileInfo
15 #include <algorithm> // for sort, transform
16 #include <cctype> // for tolower
17 #include <copyright.h>
18 #include <cstddef> // for size_t
19 #include <cstdlib> // for exit, strtod, EXIT_SUCCESS, etc
20 #include <cstring> // for strcmp
21 #include <fmt/format.h>
22 #include <iosfwd> // for ostream
23 #include <utility> // for pair, make_pair
24 #include <vector> // for vector
25
26 namespace {
27 void parse_variable_names(const char *tokens, StringIdVector *variable_list);
28 } // namespace
29
30 SystemInterface::SystemInterface() { enroll_options(); }
31
32 void SystemInterface::enroll_options()
33 {
34 options_.usage("[options] input_database output_matlab_script_file\n"
35 "\t\tIf output name not specified, then output file will be the\n"
36 "\t\tbasename of the input file with suffix '.m'");
37
38 options_.enroll("help", GetLongOption::NoValue, "Print this summary and exit", nullptr);
39
40 options_.enroll("version", GetLongOption::NoValue, "Print version and exit", nullptr);
41
42 options_.enroll(
43 "field_suffix", GetLongOption::MandatoryValue,
44 "Character used to separate a field component suffix from the field name.\n"
45 "\t\tEnter '_' to treat field_x, field_y, field_z as a 3-component vector 'field'.\n"
46 "\t\tDefault = none (field_x, field_y, field_z are different fields)",
47 "none");
48
49 options_.enroll("minimum_time", GetLongOption::MandatoryValue,
50 "Minimum timestep for which to transfer data to matlab file.", nullptr);
51
52 options_.enroll("maximum_time", GetLongOption::MandatoryValue,
53 "Maximum timestep for which to transfer data to matlab file.", nullptr);
54
55 options_.enroll("list", GetLongOption::MandatoryValue,
56 "List global, nodal, element, nodeset, or sideset variables.\n\t\tEnter 'all' to "
57 "list all types.\n"
58 "\t\tCode exits after listing variable names.",
59 nullptr);
60
61 options_.enroll("gvar", GetLongOption::MandatoryValue,
62 "Comma-separated list of global variables to be output or ALL or NONE.", "ALL");
63
64 #if 0
65 options_.enroll("evar", GetLongOption::MandatoryValue,
66 "(Not Yet Implemented) Comma-separated list of element variables to be output or ALL or NONE.\n"
67 "\t\tVariables can be limited to certain blocks by appending a\n"
68 "\t\tcolon followed by the block id. E.g. -evar sigxx:10:20",
69 nullptr);
70
71 options_.enroll("nvar", GetLongOption::MandatoryValue,
72 "(Not Yet Implemented) Comma-separated list of nodal variables to be output or ALL or NONE.\n"
73 "\t\tVariables can be limited to certain nodes by appending a\n"
74 "\t\tcolon followed by the node id. E.g. -nvar disp:10:20",
75 nullptr);
76
77 options_.enroll("nsetvar", GetLongOption::MandatoryValue,
78 "(Not Yet Implemented) Comma-separated list of nodeset variables to be output or ALL or NONE.",
79 nullptr);
80
81 options_.enroll("ssetvar", GetLongOption::MandatoryValue,
82 "(Not Yet Implemented) Comma-separated list of sideset variables to be output or ALL or NONE.",
83 nullptr);
84 #endif
85
86 options_.enroll("copyright", GetLongOption::NoValue, "Show copyright and license data.", nullptr);
87 }
88
89 bool SystemInterface::parse_options(int argc, char **argv)
90 {
91 int option_index = options_.parse(argc, argv);
92 if (option_index < 1) {
93 return false;
94 }
95
96 // Get options from environment variable also...
97 char *options = getenv("exomatlab");
98 if (options != nullptr) {
99 fmt::print(stderr,
100 "\nThe following options were specified via the EXOMATLAB_OPTIONS environment "
101 "variable:\n\t{}\n\n",
102 options);
103 options_.parse(options, GetLongOption::basename(*argv));
104 }
105
106 if (options_.retrieve("help") != nullptr) {
107 options_.usage();
108 fmt::print(stderr,
109 "\n\tCan also set options via EXOMATLAB_OPTIONS environment variable.\n"
110 "\n\tDocumentation: "
111 "https://sandialabs.github.io/seacas-docs/sphinx/html/index.html#exomatlab\n"
112 "\n\t->->-> Send email to [email protected] for exomatlab support.<-<-<-\n");
113
114 exit(EXIT_SUCCESS);
115 }
116
117 if (options_.retrieve("version") != nullptr) {
118 // Version is printed up front, just exit...
119 exit(0);
120 }
121
122 {
123 const char *temp = options_.retrieve("field_suffix");
124 if (temp != nullptr) {
125 if (strcmp("none", temp) == 0) {
126 // This is ASCII 1 which means it won't be found so
127 // vector/tensor won't be recognized by default.
128 fieldSuffix_ = 1;
129 }
130 else {
131 fieldSuffix_ = temp[0];
132 }
133 }
134 }
135
136 {
137 const char *temp = options_.retrieve("list");
138 if (temp != nullptr) {
139 listVars_ = true;
140 parse_variable_names(temp, &varsToList_);
141 }
142 }
143
144 {
145 const char *temp = options_.retrieve("gvar");
146 parse_variable_names(temp, &globalVarNames_);
147 }
148
149 #if 0
150 {
151 const char *temp = options_.retrieve("nvar");
152 parse_variable_names(temp, &nodeVarNames_);
153 }
154
155 {
156 const char *temp = options_.retrieve("evar");
157 parse_variable_names(temp, &elemVarNames_);
158 }
159
160 {
161 const char *temp = options_.retrieve("nsetvar");
162 parse_variable_names(temp, &nsetVarNames_);
163 }
164
165 {
166 const char *temp = options_.retrieve("ssetvar");
167 parse_variable_names(temp, &ssetVarNames_);
168 }
169 #endif
170
171 {
172 const char *temp = options_.retrieve("minimum_time");
173 if (temp != nullptr) {
174 minimumTime_ = strtod(temp, nullptr);
175 }
176 }
177
178 {
179 const char *temp = options_.retrieve("maximum_time");
180 if (temp != nullptr) {
181 maximumTime_ = strtod(temp, nullptr);
182 }
183 }
184
185 if (options_.retrieve("copyright") != nullptr) {
186 fmt::print("{}", copyright("2011-2021"));
187 exit(EXIT_SUCCESS);
188 }
189
190 // Parse remaining options as input file.
191 if (option_index < argc) {
192 inputFile_ = argv[option_index++];
193 if (option_index < argc) {
194 outputFile_ = argv[option_index++];
195 }
196 else {
197 outputFile_ = Ioss::FileInfo(inputFile_).basename() + ".m";
198 }
199 }
200 else {
201 options_.usage();
202 fmt::print(stderr, "\nERROR: no files specified\n\n");
203 return false;
204 }
205 return true;
206 }
207
208 void SystemInterface::show_version()
209 {
210 fmt::print("{}\n"
211 "\t(A code for outputting exodusII global variable data for use in matlab.)\n"
212 "\t(Version: {}) Modified: {}\n",
213 qainfo[0], qainfo[2], qainfo[1]);
214 }
215
216 namespace {
217 std::string LowerCase(const std::string &name)
218 {
219 std::string s = name;
220 std::transform(s.begin(), s.end(), // source
221 s.begin(), // destination
222 ::tolower); // operation
223 return s;
224 }
225
226 using StringVector = std::vector<std::string>;
227 bool string_id_sort(const std::pair<std::string, int> &t1, const std::pair<std::string, int> &t2)
228 {
229 return t1.first < t2.first || (!(t2.first < t1.first) && t1.second < t2.second);
230 }
231
232 void parse_variable_names(const char *tokens, StringIdVector *variable_list)
233 {
234 // Break into tokens separated by ","
235 if (tokens != nullptr) {
236 std::string token_string(tokens);
237 StringVector var_list = SLIB::tokenize(token_string, ",");
238
239 // At this point, var_list is either a single string, or a
240 // string separated from 1 or more ids with ":" delimiter. For
241 // example, sigxx:1:10:100 would indicate that the variable
242 // "sigxx" should be written only for elements with id 1, 10,
243 // and 100. "sigxx" would indicate that the variable should be
244 // written for all elements.
245 auto I = var_list.begin();
246 while (I != var_list.end()) {
247 StringVector name_id = SLIB::tokenize(*I, ":");
248 std::string var_name = LowerCase(name_id[0]);
249 if (name_id.size() == 1) {
250 (*variable_list).emplace_back(var_name, 0);
251 }
252 else {
253 for (size_t i = 1; i < name_id.size(); i++) {
254 // Convert string to integer...
255 int id = std::stoi(name_id[i]);
256 (*variable_list).emplace_back(var_name, id);
257 }
258 }
259 ++I;
260 }
261 // Sort the list...
262 std::sort(variable_list->begin(), variable_list->end(), string_id_sort);
263 }
264 }
265 } // namespace