Debian Codegraph @

packages/seacas/libraries/aprepro_lib/apr_exodus.cc

raw
1 // Copyright(C) 1999-2022, 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 #if defined(EXODUS_SUPPORT)
8 #include "aprepro.h"
9 #include "exodusII.h"
10 #include "exodusII_int.h"
11
12 #include "apr_symrec.h"
13 #include "apr_util.h"
14 #include "aprepro_parser.h"
15
16 #include "fmt/format.h"
17 #include <algorithm>
18 #include <cctype>
19 #include <cstdlib>
20 #include <cstring>
21
22 namespace {
23 std::string LowerCase(std::string name);
24
25 bool matches_prefix(const char *pre, const char *str)
26 {
27 return strncmp(pre, str, strlen(pre)) == 0;
28 }
29
30 std::string entity_type_name(ex_entity_type ent_type)
31 {
32 switch (ent_type) {
33 case EX_ASSEMBLY: return "assembly_";
34 case EX_ELEM_BLOCK: return "block_";
35 case EX_NODE_SET: return "nodeset_";
36 case EX_SIDE_SET: return "sideset_";
37 default: return "invalid_";
38 }
39 }
40
41 void add_name(int exoid, ex_entity_type ent_type, int64_t id, char *name, std::string &names)
42 {
43 std::string str_name;
44 ex_get_name(exoid, ent_type, id, name);
45 if (name[0] == '\0') {
46 str_name = entity_type_name(ent_type) + std::to_string(id);
47 }
48 else {
49 str_name = name;
50 }
51
52 if (names.length() > 0) {
53 names += ",";
54 }
55 names += str_name;
56 }
57
58 void get_change_set_names(int exoid, SEAMS::Aprepro *aprepro)
59 {
60 int idum;
61 float rdum;
62
63 // Get root of database...
64 int rootid = exoid & EX_FILE_ID_MASK;
65
66 std::string cs_names;
67
68 int group_name_length = ex_inquire_int(rootid, EX_INQ_GROUP_NAME_LEN);
69 std::vector<char> group_name(group_name_length + 1, '\0');
70
71 int num_children = ex_inquire_int(rootid, EX_INQ_NUM_CHILD_GROUPS);
72 for (int i = 0; i < num_children; i++) {
73 ex_inquire(rootid + 1 + i, EX_INQ_GROUP_NAME, &idum, &rdum, group_name.data());
74 if (i > 0) {
75 cs_names += ",";
76 }
77 cs_names += group_name.data();
78 }
79 aprepro->add_variable("ex_change_set_names", cs_names);
80 }
81 } // namespace
82
83 namespace SEAMS {
84 extern SEAMS::Aprepro *aprepro;
85
86 int open_exodus_file(char *filename, int cs_idx, bool do_all_cs_defines)
87 {
88 int cpu = sizeof(double);
89 int io = 0;
90 float version;
91
92 int exo = ex_open(filename, EX_READ | EX_ALL_INT64_API, &cpu, &io, &version);
93 if (exo < 0) {
94 // If there is an include path specified, try opening file there
95 std::string file_path(aprepro->ap_options.include_path);
96 if (!file_path.empty()) {
97 file_path += "/";
98 file_path += filename;
99 exo = ex_open(file_path.c_str(), EX_READ | EX_ALL_INT64_API, &cpu, &io, &version);
100 }
101 if (exo < 0) {
102 yyerror(*aprepro, "Error opening exodusII file.");
103 }
104 }
105
106 // See if the file contains change sets. If it does, open the first one.
107 int num_change_sets = ex_inquire_int(exo, EX_INQ_NUM_CHILD_GROUPS);
108 int active_change_set = 0;
109 if (num_change_sets >= 1) {
110 if (do_all_cs_defines) {
111 if (cs_idx == 0) {
112 cs_idx = 1;
113 aprepro->warning(
114 fmt::format("Input database contains {} change sets. Rreading from change set {}.",
115 num_change_sets, cs_idx));
116 }
117 }
118 if (cs_idx <= num_change_sets) {
119 active_change_set = cs_idx;
120 exo += cs_idx;
121 get_change_set_names(exo, aprepro);
122 }
123 else {
124 yyerror(*aprepro, fmt::format("Specified change set index {} exceeds count {}", cs_idx,
125 num_change_sets));
126 return -1;
127 }
128 }
129 else {
130 if (cs_idx > 0) {
131 aprepro->warning(
132 fmt::format("Input database does not contain change sets, but a change set "
133 "index {} was specified. Ignoring.",
134 cs_idx));
135 }
136 }
137
138 aprepro->add_variable("ex_change_set_count", num_change_sets);
139 if (do_all_cs_defines) {
140 aprepro->add_variable("ex_active_change_set", active_change_set);
141 aprepro->add_variable("ex_version", version);
142 }
143 return exo;
144 }
145
146 const char *do_exodus_query_change_sets(char *filename)
147 {
148 // This will open the file and determine whether there are any change sets on the file.
149 // It will define the `ex_change_set_count`, and `ex_change_set_names` variables.
150
151 // Open the specified exodusII file, read the info records
152 // then parse them as input to aprepro.
153 int exoid = open_exodus_file(filename, 0, false);
154 if (exoid > 0) {
155 ex_close(exoid);
156 }
157 return "";
158 }
159
160 const char *do_exodus_info(char *filename, char *prefix)
161 {
162 char *ret_string = nullptr;
163
164 // Open the specified exodusII file, read the info records
165 // then parse them as input to aprepro.
166 int exoid = open_exodus_file(filename, 0, true);
167 if (exoid <= 0) {
168 return "";
169 }
170
171 int count = ex_inquire_int(exoid, EX_INQ_INFO);
172
173 if (count > 0) {
174 auto info = new char *[count];
175 for (int i = 0; i < count; i++) {
176 info[i] = new char[MAX_LINE_LENGTH + 1];
177 memset(info[i], '\0', MAX_LINE_LENGTH + 1);
178 }
179
180 ex_get_info(exoid, info);
181
182 std::string lines;
183 size_t prefix_len = strlen(prefix);
184 for (int i = 0; i < count; i++) {
185 if (matches_prefix(prefix, info[i])) {
186 lines += std::string(info[i]).substr(prefix_len);
187 lines += "\n";
188 }
189 }
190
191 new_string(lines, &ret_string);
192
193 for (int i = 0; i < count; i++) {
194 delete[] info[i];
195 }
196 delete[] info;
197
198 ex_close(exoid);
199 return ret_string;
200 }
201
202 ex_close(exoid);
203 return "";
204 }
205
206 const char *do_exodus_info_range(char *filename, char *beg, char *end)
207 {
208 char *ret_string = nullptr;
209
210 // Open the specified exodusII file, read the info records
211 // then parse them as input to aprepro.
212 int exoid = open_exodus_file(filename, 0, true);
213 if (exoid <= 0) {
214 return "";
215 }
216
217 int count = ex_inquire_int(exoid, EX_INQ_INFO);
218
219 if (count > 0) {
220 auto info = new char *[count];
221 for (int i = 0; i < count; i++) {
222 info[i] = new char[MAX_LINE_LENGTH + 1];
223 memset(info[i], '\0', MAX_LINE_LENGTH + 1);
224 }
225
226 ex_get_info(exoid, info);
227
228 bool in_range = false;
229 std::string lines;
230 for (int i = 0; i < count; i++) {
231 if (in_range && strcmp(info[i], end) == 0) {
232 in_range = false;
233 }
234 if (in_range) {
235 lines += std::string(info[i]);
236 lines += "\n";
237 }
238 if (!in_range && strcmp(info[i], beg) == 0) {
239 in_range = true;
240 }
241 }
242
243 new_string(lines, &ret_string);
244
245 for (int i = 0; i < count; i++) {
246 delete[] info[i];
247 }
248 delete[] info;
249
250 ex_close(exoid);
251 return ret_string;
252 }
253
254 ex_close(exoid);
255 return "";
256 }
257
258 const char *do_exodus_meta_cd(char *filename, double cs_index)
259 {
260 // Open the specified exodusII file, read the metadata and set
261 // variables for each item.
262 // Examples include "node_count", "element_count", ...
263 int exoid = open_exodus_file(filename, (int)cs_index, true);
264 if (exoid <= 0) {
265 return "";
266 }
267
268 // read database parameters
269 ex_init_params info;
270 ex_get_init_ext(exoid, &info);
271
272 aprepro->add_variable("ex_title", info.title);
273 aprepro->add_variable("ex_dimension", info.num_dim);
274 aprepro->add_variable("ex_node_count", info.num_nodes);
275 aprepro->add_variable("ex_element_count", info.num_elem);
276 aprepro->add_variable("ex_block_count", info.num_elem_blk);
277 aprepro->add_variable("ex_assembly_count", info.num_assembly);
278 aprepro->add_variable("ex_nodeset_count", info.num_node_sets);
279 aprepro->add_variable("ex_sideset_count", info.num_side_sets);
280
281 { // Nemesis Information
282 int proc_count;
283 int proc_in_file;
284 char file_type[MAX_STR_LENGTH + 1];
285
286 ex_get_init_info(exoid, &proc_count, &proc_in_file, file_type);
287
288 if (proc_count > 1) {
289 int64_t global_nodes;
290 int64_t global_elements;
291 int64_t global_blocks;
292 int64_t global_nsets;
293 int64_t global_ssets;
294
295 aprepro->add_variable("ex_processor_count", proc_count);
296
297 ex_get_init_global(exoid, &global_nodes, &global_elements, &global_blocks, &global_nsets,
298 &global_ssets);
299
300 aprepro->add_variable("ex_node_count_global", global_nodes);
301 aprepro->add_variable("ex_element_count_global", global_elements);
302 }
303 }
304
305 // Read The Element Blocks, Node Sets, and Side Sets and set variables for each of these.
306 // The Scheme Is:
307 // -- 'ex_block_ids' Is an array of the element block ids. (ex_block_count, 1)
308 // -- 'ex_block_info' is an array of the element block info (id, num_elem, num_node_per_element,
309 // num_attrib) for each block (ex_block_count,4)
310 // -- 'ex_nodeset_ids'
311 // -- 'ex_nodeset_info'
312 // -- 'ex_sideset_ids'
313 // -- 'ex_sideset_info'
314 int max_name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH);
315 ex_set_max_name_length(exoid, max_name_length);
316 char *name = new char[max_name_length + 1];
317 std::string str_name;
318
319 if (info.num_elem_blk > 0) {
320 auto array_data = aprepro->make_array(info.num_elem_blk, 1);
321 auto array_block_info = aprepro->make_array(info.num_elem_blk, 4);
322
323 std::vector<int64_t> ids(info.num_elem_blk);
324 ex_get_ids(exoid, EX_ELEM_BLOCK, ids.data());
325
326 char type[MAX_STR_LENGTH + 1];
327 int64_t nel;
328 int64_t nnel;
329 int64_t natr;
330
331 std::string names;
332 std::string topology;
333
334 int64_t idx = 0;
335 for (int64_t i = 0; i < info.num_elem_blk; i++) {
336 ex_get_block(exoid, EX_ELEM_BLOCK, ids[i], type, &nel, &nnel, nullptr, nullptr, &natr);
337 array_data->data[i] = ids[i];
338 array_block_info->data[idx++] = ids[i];
339 array_block_info->data[idx++] = nel;
340 array_block_info->data[idx++] = nnel;
341 array_block_info->data[idx++] = natr;
342
343 if (i > 0) {
344 topology += ",";
345 }
346 topology += type;
347 add_name(exoid, EX_ELEM_BLOCK, ids[i], name, names);
348 }
349
350 topology = LowerCase(topology);
351 aprepro->add_variable("ex_block_topology", topology);
352 aprepro->add_variable("ex_block_names", names);
353 aprepro->add_variable("ex_block_ids", array_data);
354 aprepro->add_variable("ex_block_info", array_block_info);
355 }
356
357 if (info.num_assembly > 0) {
358 std::vector<int64_t> ids(info.num_assembly);
359 ex_get_ids(exoid, EX_ASSEMBLY, ids.data());
360
361 std::string names;
362 std::string type;
363 auto array_data = aprepro->make_array(info.num_assembly, 1);
364 auto array_info = aprepro->make_array(info.num_assembly, 1);
365
366 for (int64_t i = 0; i < info.num_assembly; i++) {
367 ex_assembly assembly;
368 assembly.id = ids[i];
369 assembly.name = new char[max_name_length + 1];
370 assembly.entity_list = nullptr;
371
372 ex_get_assembly(exoid, &assembly);
373 if (i > 0) {
374 names += ",";
375 type += ",";
376 }
377 array_data->data[i] = ids[i];
378 array_info->data[i] = assembly.entity_count;
379 names += assembly.name;
380 type += ex_name_of_object(assembly.type);
381 }
382 aprepro->add_variable("ex_assembly_type", type);
383 aprepro->add_variable("ex_assembly_names", names);
384 aprepro->add_variable("ex_assembly_ids", array_data);
385 aprepro->add_variable("ex_assembly_info", array_info);
386 }
387
388 // Nodesets...
389 if (info.num_node_sets > 0) {
390 auto array_data = aprepro->make_array(info.num_node_sets, 1);
391 auto array_set_info = aprepro->make_array(info.num_node_sets, 3);
392
393 std::vector<int64_t> ids(info.num_node_sets);
394 ex_get_ids(exoid, EX_NODE_SET, ids.data());
395
396 std::string names;
397 int64_t idx = 0;
398 for (int64_t i = 0; i < info.num_node_sets; i++) {
399 int64_t num_entry;
400 int64_t num_dist;
401 ex_get_set_param(exoid, EX_NODE_SET, ids[i], &num_entry, &num_dist);
402 array_data->data[i] = ids[i];
403 array_set_info->data[idx++] = ids[i];
404 array_set_info->data[idx++] = num_entry;
405 array_set_info->data[idx++] = num_dist;
406
407 add_name(exoid, EX_NODE_SET, ids[i], name, names);
408 }
409
410 aprepro->add_variable("ex_nodeset_names", names);
411 aprepro->add_variable("ex_nodeset_ids", array_data);
412 aprepro->add_variable("ex_nodeset_info", array_set_info);
413 }
414
415 // Sidesets...
416 if (info.num_side_sets > 0) {
417 auto array_data = aprepro->make_array(info.num_side_sets, 1);
418 auto array_set_info = aprepro->make_array(info.num_side_sets, 3);
419
420 std::vector<int64_t> ids(info.num_side_sets);
421 ex_get_ids(exoid, EX_SIDE_SET, ids.data());
422
423 std::string names;
424 int64_t idx = 0;
425 for (int64_t i = 0; i < info.num_side_sets; i++) {
426 int64_t num_entry;
427 int64_t num_dist;
428 ex_get_set_param(exoid, EX_SIDE_SET, ids[i], &num_entry, &num_dist);
429 array_data->data[i] = ids[i];
430 array_set_info->data[idx++] = ids[i];
431 array_set_info->data[idx++] = num_entry;
432 array_set_info->data[idx++] = num_dist;
433
434 add_name(exoid, EX_SIDE_SET, ids[i], name, names);
435 }
436
437 aprepro->add_variable("ex_sideset_names", names);
438 aprepro->add_variable("ex_sideset_ids", array_data);
439 aprepro->add_variable("ex_sideset_info", array_set_info);
440 }
441
442 // Get timestep count
443 int64_t ts_count = ex_inquire_int(exoid, EX_INQ_TIME);
444 aprepro->add_variable("ex_timestep_count", ts_count);
445
446 if (ts_count > 0) {
447 std::vector<double> timesteps(ts_count);
448 ex_get_all_times(exoid, timesteps.data());
449
450 auto ts_array_data = aprepro->make_array(ts_count, 1);
451 for (int64_t i = 0; i < ts_count; i++) {
452 ts_array_data->data[i] = timesteps[i];
453 }
454 aprepro->add_variable("ex_timestep_times", ts_array_data);
455 }
456
457 // See if any global variables on file...
458 int num_global = 0;
459 ex_get_variable_param(exoid, EX_GLOBAL, &num_global);
460 if (num_global > 0) {
461 std::string names;
462 for (int i = 0; i < num_global; i++) {
463 ex_get_variable_name(exoid, EX_GLOBAL, i + 1, name);
464 if (i > 0) {
465 names += ",";
466 }
467 names += name;
468 }
469 aprepro->add_variable("ex_global_var_names", names);
470
471 auto glo_array_data = aprepro->make_array(ts_count, num_global);
472 std::vector<double> globals(num_global);
473 int index = 0;
474 for (int64_t i = 0; i < ts_count; i++) {
475 ex_get_var(exoid, i + 1, EX_GLOBAL, 0, 0, num_global, globals.data());
476 for (int j = 0; j < num_global; j++) {
477 glo_array_data->data[index++] = globals[j];
478 }
479 }
480 aprepro->add_variable("ex_global_var_value", glo_array_data);
481 }
482
483 delete[] name;
484 ex_close(exoid);
485 return "";
486 }
487
488 const char *do_exodus_meta(char *filename) { return do_exodus_meta_cd(filename, 0); }
489
490 } // namespace SEAMS
491
492 namespace {
493 inline int to_lower(int c) { return std::tolower(c); }
494 std::string LowerCase(std::string name)
495 {
496 std::transform(name.begin(), name.end(), name.begin(), to_lower);
497 return name;
498 }
499 } // namespace
500 #endif