Debian Codegraph @

packages/seacas/applications/mat2exo/mat2exo.C

raw
1 /*
2 * Copyright(C) 1999-2021, 2023, 2024 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 /* matlab mat file to exodus II. This takes .mat files exactly as
10 generated by the tool exo2mat written by mrtabba and converts them
11 back to exodus II format
12
13 rmnaeth. August 8, 2003
14
15 modified by D. Todd Griffith on 12/09/2005
16 * modifications include:
17 1) writes global, nodal and element variable names
18 2) writes global, nodal and elemnent variable results
19 3) writes complete set of time steps (previous version
20 skipped first step)
21 4) writes complete node set information (node set numbers,
22 dist. factors, etc)
23 5) writes complete side set information (side set numbers,
24 dist. factors, etc)
25
26 modified by D. Todd Griffith on 12/16/2005
27 * side set distribution factors now written as double (not int)
28
29 modified by Greg Sjaardema, 07/05/2012 to use matio instead of matlab libraries.
30 */
31
32 #include "SL_tokenize.h" // for tokenize
33 #include "add_to_log.h" // for add_to_log
34 #include "matio.h" // for matvar_t, Mat_VarFree, etc
35 #include "matio_pubconf.h" // for MATIO_VERSION
36 #include "vector_data.h"
37
38 #include <array>
39 #include <cstddef> // for size_t
40 #include <cstring> // for strtok, memcpy, strlen, etc
41 #include <exodusII.h> // for ex_put_variable_param, etc
42 #include <fmt/printf.h>
43 #include <numeric> // for accumulate
44 #include <smart_assert.h>
45 #include <string> // for char_traits, string
46 #include <vector> // for vector
47
48 #if MATIO_VERSION < 151
49 #error "MatIO Version 1.5.1 or greater is required"
50 #endif
51
52 /**********************************************************************/
53 mat_t *mat_file = nullptr; /* file for binary .mat input */
54
55 /**********************************************************************/
56 namespace {
57 const std::array<std::string, 3> qainfo{"mat2exo", "2021/09/27", "4.06"};
58 }
59
60 /**********************************************************************/
61 void get_put_names(int exo_file, ex_entity_type entity, int num_vars, const std::string &name);
62 void get_put_user_names(int exo_file, ex_entity_type entity, int num_entity, const char *mname);
63 void get_put_attr_names(int exo_file, int seq, int id, int num_attr);
64 void get_put_vars(int exo_file, ex_entity_type type, const std::vector<int> &ids, int num_blocks,
65 int num_vars, int num_time_steps, const std::vector<int> &num_per_block,
66 const char *mname);
67
68 std::vector<std::string> matGetStr(const std::string &name);
69 int matGetDbl(const std::string &name, size_t n1, size_t n2, std::vector<double> &data);
70 int matGetInt(const std::string &name, size_t n1, size_t n2, std::vector<int> &data);
71 int matGetInt(const std::string &name);
72 int matArrNRow(const std::string &name);
73 int matArrNCol(const std::string &name);
74
75 /**********************************************************************/
76 int main(int argc, char *argv[])
77 {
78
79 /* QA Info */
80 fmt::printf("%s: %s, %s\n", qainfo[0], qainfo[2], qainfo[1]);
81
82 /* usage message*/
83 if (argc != 2) {
84 fmt::printf("%s matlab_file_name.\n", argv[0]);
85 fmt::printf(" the matlab_file_name is required\n");
86 fmt::printf("%d", argc);
87 exit(1);
88 }
89
90 /*open input file*/
91 mat_file = Mat_Open(argv[1], MAT_ACC_RDONLY);
92 if (mat_file == nullptr) {
93 fmt::printf("Error opening matlab file %s\n", argv[1]);
94 return (1);
95 }
96
97 /*open output file*/
98 int cpu_word_size = sizeof(double);
99 int io_word_size = sizeof(double);
100
101 ex_opts(EX_VERBOSE);
102
103 const std::string ext{".exo"};
104 std::string line(argv[1]);
105 line = line.substr(0, line.find('.'));
106 line += ext;
107 int exo_file = ex_create(line.c_str(), EX_CLOBBER, &cpu_word_size, &io_word_size);
108 if (exo_file < 0) {
109 fmt::print(stderr, "MAT2EXO: error creating '{}'\n", line);
110 exit(1);
111 }
112
113 fmt::print(stderr, "translating '{}' to '{}'\n", argv[1], line);
114
115 int num_axes = matGetInt("naxes");
116 int num_nodes = matGetInt("nnodes");
117 int num_elements = matGetInt("nelems");
118 int num_blocks = matGetInt("nblks");
119 int num_node_sets = matGetInt("nnsets");
120 int num_side_sets = matGetInt("nssets");
121 int num_time_steps = matGetInt("nsteps");
122 int num_global_vars = matGetInt("ngvars");
123 int num_nodal_vars = matGetInt("nnvars");
124 int num_element_vars = matGetInt("nevars");
125 int num_nodeset_vars = matGetInt("nnsvars");
126 int num_sideset_vars = matGetInt("nssvars");
127
128 ex_put_init(exo_file, line.c_str(), num_axes, num_nodes, num_elements, num_blocks, num_node_sets,
129 num_side_sets);
130
131 if (num_global_vars > 0) {
132 ex_put_variable_param(exo_file, EX_GLOBAL, num_global_vars);
133 }
134
135 if (num_nodal_vars > 0) {
136 ex_put_variable_param(exo_file, EX_NODAL, num_nodal_vars);
137 }
138
139 if (num_element_vars > 0) {
140 ex_put_variable_param(exo_file, EX_ELEM_BLOCK, num_element_vars);
141 }
142
143 if (num_nodeset_vars > 0) {
144 ex_put_variable_param(exo_file, EX_NODE_SET, num_nodeset_vars);
145 }
146
147 if (num_sideset_vars > 0) {
148 ex_put_variable_param(exo_file, EX_SIDE_SET, num_sideset_vars);
149 }
150
151 /* nodal coordinates */
152 {
153 std::vector<double> x;
154 std::vector<double> y;
155 std::vector<double> z;
156 matGetDbl("x0", num_nodes, 1, x);
157 if (num_axes > 1) {
158 matGetDbl("y0", num_nodes, 1, y);
159 }
160 if (num_axes > 2) {
161 matGetDbl("z0", num_nodes, 1, z);
162 }
163 ex_put_coord(exo_file, Data(x), Data(y), Data(z));
164 }
165
166 /* side sets */
167 std::vector<int> num_sideset_sides(num_side_sets);
168 if (num_side_sets > 0) {
169
170 std::vector<int> ids;
171 matGetInt("ssids", num_side_sets, 1, ids);
172 matGetInt("nsssides", num_side_sets, 1, num_sideset_sides);
173 std::vector<int> nssdfac(num_side_sets);
174 matGetInt("nssdfac", num_side_sets, 1, nssdfac);
175
176 std::vector<int> elem_list;
177 std::vector<int> side_list;
178 std::vector<double> dist_fact;
179 for (int i = 0; i < num_side_sets; i++) {
180 ex_put_set_param(exo_file, EX_SIDE_SET, ids[i], num_sideset_sides[i], nssdfac[i]);
181
182 std::string name = fmt::sprintf("sselem%02d", i + 1);
183 matGetInt(name, num_sideset_sides[i], 1, elem_list);
184
185 name = fmt::sprintf("ssside%02d", i + 1);
186 matGetInt(name, num_sideset_sides[i], 1, side_list);
187 ex_put_set(exo_file, EX_SIDE_SET, ids[i], Data(elem_list), Data(side_list));
188
189 if (nssdfac[i] > 0) {
190 name = fmt::sprintf("ssfac%02d", i + 1);
191 matGetDbl(name, nssdfac[i], 1, dist_fact);
192 ex_put_set_dist_fact(exo_file, EX_SIDE_SET, ids[i], Data(dist_fact));
193 }
194 }
195
196 get_put_user_names(exo_file, EX_SIDE_SET, num_side_sets, "ssusernames");
197 }
198
199 /* node sets */
200 std::vector<int> num_nodeset_nodes;
201 if (num_node_sets > 0) {
202
203 std::vector<int> ids;
204 matGetInt("nsids", num_node_sets, 1, ids);
205 matGetInt("nnsnodes", num_node_sets, 1, num_nodeset_nodes);
206 std::vector<int> ndfac;
207 matGetInt("nnsdfac", num_node_sets, 1, ndfac);
208
209 std::vector<double> dist_fact;
210 std::vector<int> node_list;
211 for (int i = 0; i < num_node_sets; i++) {
212 ex_put_set_param(exo_file, EX_NODE_SET, ids[i], num_nodeset_nodes[i], ndfac[i]);
213
214 std::string name = fmt::sprintf("nsnod%02d", i + 1);
215 matGetInt(name, num_nodeset_nodes[i], 1, node_list);
216 ex_put_set(exo_file, EX_NODE_SET, ids[i], Data(node_list), nullptr);
217
218 if (ndfac[i] > 0) {
219 name = fmt::sprintf("nsfac%02d", i + 1);
220 matGetDbl(name, ndfac[i], 1, dist_fact);
221 ex_put_set_dist_fact(exo_file, EX_NODE_SET, ids[i], Data(dist_fact));
222 }
223 }
224
225 get_put_user_names(exo_file, EX_NODE_SET, num_node_sets, "nsusernames");
226 }
227
228 /* element blocks */
229 std::vector<int> num_elem_in_block(num_blocks);
230 {
231 std::vector<int> ids;
232 matGetInt("blkids", num_blocks, 1, ids);
233
234 /* get elem block types */
235 auto block_names = matGetStr("blknames");
236 SMART_ASSERT(block_names.size() == (size_t)num_blocks);
237 std::vector<int> connect;
238 for (int i = 0; i < num_blocks; i++) {
239 std::string name = fmt::sprintf("blk%02d", i + 1);
240 int num_node_per_elem = matArrNRow(name);
241 num_elem_in_block[i] = matArrNCol(name);
242 matGetInt(name, num_node_per_elem, num_elem_in_block[i], connect);
243
244 name = fmt::sprintf("blk%02d_nattr", i + 1);
245 int num_attr_per_elem = matGetInt(name);
246 ex_put_block(exo_file, EX_ELEM_BLOCK, ids[i], block_names[i].c_str(), num_elem_in_block[i],
247 num_node_per_elem, 0, 0, num_attr_per_elem);
248 ex_put_conn(exo_file, EX_ELEM_BLOCK, ids[i], Data(connect), nullptr, nullptr);
249
250 if (num_attr_per_elem > 0) {
251 get_put_attr_names(exo_file, i + 1, ids[i], num_attr_per_elem);
252 std::vector<double> attr_data;
253 for (int j = 0; j < num_attr_per_elem; j++) {
254 name = fmt::sprintf("blk%02d_attr%02d", i + 1, j + 1);
255 matGetDbl(name, num_elem_in_block[i], 1, attr_data);
256 ex_put_one_attr(exo_file, EX_ELEM_BLOCK, ids[i], j + 1, Data(attr_data));
257 }
258 }
259 }
260 get_put_user_names(exo_file, EX_ELEM_BLOCK, num_blocks, "blkusernames");
261 }
262
263 /* time values */
264 if (num_time_steps > 0) {
265 std::vector<double> times;
266 matGetDbl("time", num_time_steps, 1, times);
267 for (int i = 0; i < num_time_steps; i++) {
268 ex_put_time(exo_file, i + 1, &times[i]);
269 }
270 }
271
272 /* global variables */
273 if (num_global_vars > 0) {
274 get_put_names(exo_file, EX_GLOBAL, num_global_vars, "gnames");
275
276 std::vector<double> var_vals(num_global_vars * num_time_steps);
277 std::vector<double> temp(num_time_steps);
278 for (int j = 0; j < num_global_vars; j++) {
279 std::string name = fmt::sprintf("gvar%02d", j + 1);
280 matGetDbl(name, num_time_steps, 1, temp);
281 for (int i = 0; i < num_time_steps; i++) {
282 var_vals[num_global_vars * i + j] = temp[i];
283 }
284 }
285 for (int i = 0; i < num_time_steps; i++) {
286 size_t offset = num_global_vars * i;
287 ex_put_var(exo_file, i + 1, EX_GLOBAL, 1, 0, num_global_vars, &var_vals[offset]);
288 }
289 }
290
291 /* nodal variables */
292 if (num_nodal_vars > 0) {
293 get_put_names(exo_file, EX_NODAL, num_nodal_vars, "nnames");
294
295 std::vector<int> ids(1, 1);
296 std::vector<int> node_block(1, num_nodes);
297
298 get_put_vars(exo_file, EX_NODAL, ids, 1, num_nodal_vars, num_time_steps, node_block,
299 "nvar%02d");
300 }
301
302 /* element variables */
303 if (num_element_vars > 0) {
304 std::vector<int> ids;
305 matGetInt("blkids", num_blocks, 1, ids);
306 get_put_names(exo_file, EX_ELEM_BLOCK, num_element_vars, "enames");
307
308 get_put_vars(exo_file, EX_ELEM_BLOCK, ids, num_blocks, num_element_vars, num_time_steps,
309 num_elem_in_block, "evar%02d");
310 }
311
312 /* nodeset variables */
313 if (num_nodeset_vars > 0) {
314 std::vector<int> ids;
315 matGetInt("nsids", num_node_sets, 1, ids);
316 get_put_names(exo_file, EX_NODE_SET, num_nodeset_vars, "nsnames");
317
318 get_put_vars(exo_file, EX_NODE_SET, ids, num_node_sets, num_nodeset_vars, num_time_steps,
319 num_nodeset_nodes, "nsvar%02d");
320 }
321
322 /* sideset variables */
323 if (num_sideset_vars > 0) {
324 std::vector<int> ids;
325 matGetInt("ssids", num_side_sets, 1, ids);
326 get_put_names(exo_file, EX_SIDE_SET, num_sideset_vars, "ssnames");
327
328 get_put_vars(exo_file, EX_SIDE_SET, ids, num_side_sets, num_sideset_vars, num_time_steps,
329 num_sideset_sides, "ssvar%02d");
330 }
331
332 /* node and element number maps */
333 {
334 std::vector<int> ids;
335 if (matGetInt("node_num_map", num_nodes, 1, ids) == 0) {
336 ex_put_id_map(exo_file, EX_NODE_MAP, Data(ids));
337 }
338 }
339
340 {
341 std::vector<int> ids;
342 if (matGetInt("elem_num_map", num_elements, 1, ids) == 0) {
343 ex_put_id_map(exo_file, EX_ELEM_MAP, Data(ids));
344 }
345 }
346
347 ex_close(exo_file);
348 Mat_Close(mat_file);
349
350 fmt::printf("done.\n");
351
352 add_to_log("mat2exo", 0);
353 return (0);
354 }
355
356 /**********************************************************************/
357 std::vector<std::string> matGetStr(const std::string &name)
358 {
359 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
360 if (matvar == nullptr) {
361 return {};
362 }
363
364 if (matvar->dims[0] != 1) {
365 fmt::printf("Error: Multiline string copy attempted\n");
366 }
367
368 size_t bytes = matvar->nbytes;
369 if (matvar->data_size == 2 && matvar->data_type == MAT_T_UINT16) {
370 // Data stored as 16bit, but we want 8bit (This is due to some UTF
371 // strangeness in matio)
372 char *data = reinterpret_cast<char *>(matvar->data);
373 for (size_t i = 0, j = 0; i < matvar->nbytes; i += 2, j++) {
374 data[j] = data[i];
375 }
376 bytes /= 2;
377 }
378 std::string mat_names(reinterpret_cast<char *>(matvar->data), bytes);
379 auto names = SLIB::tokenize(mat_names, "\n", true);
380 Mat_VarFree(matvar);
381
382 return names;
383 }
384
385 /**********************************************************************/
386 int matGetDbl(const std::string &name, size_t n1, size_t n2, std::vector<double> &data)
387 {
388 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
389 if (matvar == nullptr) {
390 return -1;
391 }
392
393 SMART_ASSERT(matvar->dims[0] == n1);
394 SMART_ASSERT(matvar->dims[1] == n2);
395
396 data.resize(n1 * n2);
397 memcpy(Data(data), static_cast<int *>(matvar->data), n1 * n2 * sizeof(double));
398
399 Mat_VarFree(matvar);
400 return 0;
401 }
402
403 /**********************************************************************/
404 int matGetInt(const std::string &name, size_t n1, size_t n2, std::vector<int> &data)
405 {
406 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
407 if (matvar == nullptr) {
408 return -1;
409 }
410
411 SMART_ASSERT(matvar->dims[0] == n1);
412 SMART_ASSERT(matvar->dims[1] == n2);
413
414 data.resize(n1 * n2);
415 memcpy(Data(data), static_cast<int *>(matvar->data), n1 * n2 * sizeof(int));
416
417 Mat_VarFree(matvar);
418 return 0;
419 }
420
421 /**********************************************************************/
422 int matGetInt(const std::string &name)
423 {
424 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
425 if (matvar == nullptr) {
426 return -1;
427 }
428
429 SMART_ASSERT(matvar->dims[0] == 1);
430 SMART_ASSERT(matvar->dims[1] == 1);
431
432 int data = static_cast<int *>(matvar->data)[0];
433
434 Mat_VarFree(matvar);
435 return data;
436 }
437
438 /**********************************************************************/
439 int matArrNRow(const std::string &name)
440 {
441 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
442 if (matvar == nullptr) {
443 return -1;
444 }
445
446 int nrow = matvar->dims[0];
447 Mat_VarFree(matvar);
448 return nrow;
449 }
450
451 /**********************************************************************/
452 int matArrNCol(const std::string &name)
453 {
454 matvar_t *matvar = Mat_VarRead(mat_file, name.c_str());
455 if (matvar == nullptr) {
456 return -1;
457 }
458
459 int ncol = matvar->dims[1];
460 Mat_VarFree(matvar);
461 return ncol;
462 }
463
464 void get_put_names(int exo_file, ex_entity_type entity, int num_vars, const std::string &name)
465 {
466 auto names = matGetStr(name);
467 SMART_ASSERT(names.size() == (size_t)num_vars);
468 std::vector<const char *> str2(num_vars);
469 for (int i = 0; i < num_vars; i++) {
470 str2[i] = names[i].c_str();
471 }
472 ex_put_variable_names(exo_file, entity, num_vars, const_cast<char **>(Data(str2)));
473 }
474
475 void get_put_user_names(int exo_file, ex_entity_type entity, int num_entity, const char *mname)
476 {
477 auto names = matGetStr(mname);
478 SMART_ASSERT(names.size() == (size_t)num_entity)(names.size())(num_entity);
479 std::vector<const char *> str2(num_entity);
480 for (int i = 0; i < num_entity; i++) {
481 str2[i] = names[i].c_str();
482 }
483 ex_put_names(exo_file, entity, const_cast<char **>(Data(str2)));
484 }
485
486 void get_put_attr_names(int exo_file, int seq, int id, int num_attr)
487 {
488 std::string str = fmt::sprintf("blk%02d_attrnames", seq);
489
490 auto names = matGetStr(str);
491 SMART_ASSERT(names.size() == (size_t)num_attr);
492 std::vector<const char *> str2(num_attr);
493 for (int i = 0; i < num_attr; i++) {
494 str2[i] = names[i].c_str();
495 }
496 ex_put_attr_names(exo_file, EX_ELEM_BLOCK, id, const_cast<char **>(Data(str2)));
497 }
498
499 void get_put_vars(int exo_file, ex_entity_type type, const std::vector<int> &ids, int num_blocks,
500 int num_vars, int num_time_steps, const std::vector<int> &num_per_block,
501 const char *mname)
502 {
503 size_t num_entity = std::accumulate(num_per_block.begin(), num_per_block.end(), 0);
504
505 for (int i = 0; i < num_vars; i++) {
506 std::string name;
507 name = fmt::sprintf(mname, i + 1);
508 std::vector<double> var_vals;
509 matGetDbl(name, num_entity, num_time_steps, var_vals);
510 size_t n = 0;
511 for (int j = 0; j < num_time_steps; j++) {
512 for (int k = 0; k < num_blocks; k++) {
513 ex_put_var(exo_file, j + 1, type, i + 1, ids[k], num_per_block[k], &var_vals[n]);
514 n += num_per_block[k];
515 }
516 }
517 }
518 }