| 1 |
/* |
| 2 |
* Copyright(C) 1999-2025 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 |
/* exodus II to matlab m file, copy of |
| 9 |
exo2mat. |
| 10 |
exo2mat was written by mrtabba |
| 11 |
exo2m modifications by gmreese to permit usage on machines without |
| 12 |
the matlab libraries. |
| 13 |
|
| 14 |
modified by D. Todd Griffith, 01/12/2006 |
| 15 |
to include changes made in versions 1.4 through 1.6 on the |
| 16 |
SEACAS tools repository as the previous modifications dated |
| 17 |
12/08 and 12/15/2005 were made starting with Version 1.3. |
| 18 |
In particular, the only changes are those made here are those |
| 19 |
made in Version 1.6 which include a special provision |
| 20 |
for exodus files which contain no distribution factors |
| 21 |
modified by D. Todd Griffith, 12/15/2005 |
| 22 |
to write distribution factors as double precision type |
| 23 |
modified by D. Todd Griffith 12/08/2005 |
| 24 |
to include complete writing of side set and node set information |
| 25 |
so it will be available for the mat2exo conversion stage |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <array> |
| 31 |
#include <cstring> // for strlen, etc |
| 32 |
#include <fmt/chrono.h> |
| 33 |
#include <fmt/ostream.h> |
| 34 |
#include <fmt/printf.h> |
| 35 |
#include <iostream> |
| 36 |
#include <numeric> |
| 37 |
#include <string> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "add_to_log.h" // for add_to_log |
| 41 |
#include "exodusII.h" // for ex_get_variable_param, etc |
| 42 |
#include "matio.h" // for Mat_VarCreate, Mat_VarFree, etc |
| 43 |
#include "time_stamp.h" |
| 44 |
#include "vector_data.h" |
| 45 |
|
| 46 |
#include <cassert> // for assert |
| 47 |
#include <cstddef> // for size_t |
| 48 |
#include <cstdlib> // for free, calloc, exit |
| 49 |
#include <ctime> |
| 50 |
#if MATIO_VERSION < 151 |
| 51 |
#error "MatIO Version 1.5.1 or greater is required" |
| 52 |
#endif |
| 53 |
|
| 54 |
namespace { |
| 55 |
const std::string EXT = ".mat"; |
| 56 |
int textfile = 0; |
| 57 |
|
| 58 |
FILE *m_file = nullptr; /* file for m file output */ |
| 59 |
mat_t *mat_file = nullptr; /* file for binary .mat output */ |
| 60 |
bool debug = false; |
| 61 |
|
| 62 |
const std::array<std::string, 3> qainfo{"exo2mat", "2025/02/03", "4.09"}; |
| 63 |
|
| 64 |
void logger(const char *message) |
| 65 |
{ |
| 66 |
const std::string tsFormat = "[%H:%M:%S] "; |
| 67 |
fmt::print(std::clog, "{}: {}\n", time_stamp(tsFormat), message); |
| 68 |
} |
| 69 |
|
| 70 |
void usage() |
| 71 |
{ |
| 72 |
const auto *v5default = MAT_FT_DEFAULT == MAT_FT_MAT5 ? "[default]" : ""; |
| 73 |
const auto *v7default = MAT_FT_DEFAULT == MAT_FT_MAT73 ? "[default]" : ""; |
| 74 |
fmt::print("exo2mat [options] exodus_file_name.\n" |
| 75 |
" the exodus_file_name is required (exodus only).\n" |
| 76 |
" Options:\n" |
| 77 |
" -t write a text (.m) file rather than a binary .mat\n" |
| 78 |
" -o output file name (rather than auto generate)\n" |
| 79 |
" -c use cell arrays for transient variables.\n" |
| 80 |
" -v5 output version 5 mat file {}\n" |
| 81 |
" -v73 output version 7.3 mat file (hdf5-based) {}\n" |
| 82 |
" -v7.3 output version 7.3 mat file (hdf5-based)\n" |
| 83 |
" -change_set cs_# Read from change set #.\n" |
| 84 |
" ** note **\n" |
| 85 |
"Binary files are written by default on all platforms.\n", |
| 86 |
v5default, v7default); |
| 87 |
} |
| 88 |
|
| 89 |
/* put a string into an m file. If the string has |
| 90 |
line feeds, we put it as ints, and use 'char()' to convert it */ |
| 91 |
void mPutStr(const std::string &name, const char *str) |
| 92 |
{ |
| 93 |
assert(m_file != nullptr); |
| 94 |
if (strchr(str, '\n') == nullptr) { |
| 95 |
fmt::fprintf(m_file, "%s='%s';\n", name, str); |
| 96 |
} |
| 97 |
else { |
| 98 |
fmt::fprintf(m_file, "%s=[", name); |
| 99 |
size_t i; |
| 100 |
size_t j; |
| 101 |
for (j = i = 0; i < std::strlen(str); i++, j++) { |
| 102 |
if (j >= 20) { |
| 103 |
j = 0; |
| 104 |
fmt::fprintf(m_file, "...\n"); |
| 105 |
} |
| 106 |
fmt::fprintf(m_file, "%d ", str[i]); |
| 107 |
} |
| 108 |
fmt::fprintf(m_file, "];\n"); |
| 109 |
fmt::fprintf(m_file, "%s=char(%s);\n", name, name); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/* put double array in m file */ |
| 114 |
void mPutDbl(const std::string &name, int n1, int n2, double *pd) |
| 115 |
{ |
| 116 |
assert(m_file != nullptr); |
| 117 |
if (n1 == 1 && n2 == 1) { |
| 118 |
fmt::fprintf(m_file, "%s=%15.8e;\n", name, *pd); |
| 119 |
return; |
| 120 |
} |
| 121 |
fmt::fprintf(m_file, "%s=zeros(%d,%d);\n", name, n1, n2); |
| 122 |
for (int i = 0; i < n1; i++) { |
| 123 |
for (int j = 0; j < n2; j++) { |
| 124 |
fmt::fprintf(m_file, "%s(%d,%d)=%15.8e;\n", name, i + 1, j + 1, pd[i * n2 + j]); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* put integer array in m file */ |
| 130 |
void mPutInt(const std::string &name, int pd) |
| 131 |
{ |
| 132 |
assert(m_file != nullptr); |
| 133 |
fmt::fprintf(m_file, "%s=%d;\n", name, pd); |
| 134 |
} |
| 135 |
|
| 136 |
/* put integer array in m file */ |
| 137 |
void mPutInt(const std::string &name, int n1, int n2, int *pd) |
| 138 |
{ |
| 139 |
assert(m_file != nullptr); |
| 140 |
if (n1 == 1 && n2 == 1) { |
| 141 |
fmt::fprintf(m_file, "%s=%d;\n", name, *pd); |
| 142 |
return; |
| 143 |
} |
| 144 |
fmt::fprintf(m_file, "%s=zeros(%d,%d);\n", name, n1, n2); |
| 145 |
for (int i = 0; i < n1; i++) { |
| 146 |
for (int j = 0; j < n2; j++) { |
| 147 |
fmt::fprintf(m_file, "%s(%d,%d)=%d;\n", name, i + 1, j + 1, pd[i * n2 + j]); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* put string in mat file*/ |
| 153 |
int matPutStr(const std::string &name, char *str) |
| 154 |
{ |
| 155 |
int error = 0; |
| 156 |
matvar_t *matvar = nullptr; |
| 157 |
size_t dims[2]; |
| 158 |
|
| 159 |
dims[0] = 1; |
| 160 |
dims[1] = std::strlen(str); |
| 161 |
|
| 162 |
matvar = |
| 163 |
Mat_VarCreate(name.c_str(), MAT_C_CHAR, MAT_T_UINT8, 2, dims, str, MAT_F_DONT_COPY_DATA); |
| 164 |
if (matvar != nullptr) { |
| 165 |
error = Mat_VarWrite(mat_file, matvar, MAT_COMPRESSION_NONE); |
| 166 |
Mat_VarFree(matvar); |
| 167 |
} |
| 168 |
else { |
| 169 |
error = 1; |
| 170 |
} |
| 171 |
return error; |
| 172 |
} |
| 173 |
|
| 174 |
/* put double in mat file*/ |
| 175 |
int matPutDbl(const std::string &name, int n1, int n2, double *pd) |
| 176 |
{ |
| 177 |
int error = 0; |
| 178 |
matvar_t *matvar = nullptr; |
| 179 |
|
| 180 |
size_t dims[2]; |
| 181 |
dims[0] = n1; |
| 182 |
dims[1] = n2; |
| 183 |
|
| 184 |
matvar = |
| 185 |
Mat_VarCreate(name.c_str(), MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, pd, MAT_F_DONT_COPY_DATA); |
| 186 |
if (matvar != nullptr) { |
| 187 |
error = Mat_VarWrite(mat_file, matvar, MAT_COMPRESSION_ZLIB); |
| 188 |
Mat_VarFree(matvar); |
| 189 |
} |
| 190 |
else { |
| 191 |
error = 1; |
| 192 |
} |
| 193 |
return error; |
| 194 |
} |
| 195 |
|
| 196 |
/* put integer in mat file*/ |
| 197 |
int matPutInt(const std::string &name, int n1, int n2, int *pd) |
| 198 |
{ |
| 199 |
int error = 0; |
| 200 |
matvar_t *matvar = nullptr; |
| 201 |
|
| 202 |
size_t dims[2]; |
| 203 |
dims[0] = n1; |
| 204 |
dims[1] = n2; |
| 205 |
|
| 206 |
matvar = |
| 207 |
Mat_VarCreate(name.c_str(), MAT_C_INT32, MAT_T_INT32, 2, dims, pd, MAT_F_DONT_COPY_DATA); |
| 208 |
if (matvar != nullptr) { |
| 209 |
error = Mat_VarWrite(mat_file, matvar, MAT_COMPRESSION_ZLIB); |
| 210 |
Mat_VarFree(matvar); |
| 211 |
} |
| 212 |
else { |
| 213 |
error = 1; |
| 214 |
} |
| 215 |
return error; |
| 216 |
} |
| 217 |
|
| 218 |
/* wrappers for the output routine types */ |
| 219 |
void PutStr(const std::string &name, const std::string &str) |
| 220 |
{ |
| 221 |
if (textfile != 0) { |
| 222 |
mPutStr(name, str.c_str()); |
| 223 |
} |
| 224 |
else { |
| 225 |
matPutStr(name, const_cast<char *>(str.c_str())); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
int PutInt(const std::string &name, int pd) |
| 230 |
{ |
| 231 |
int error = 0; |
| 232 |
if (textfile != 0) { |
| 233 |
mPutInt(name, pd); |
| 234 |
} |
| 235 |
else { |
| 236 |
error = matPutInt(name, 1, 1, &pd); |
| 237 |
} |
| 238 |
return error; |
| 239 |
} |
| 240 |
|
| 241 |
int PutInt(const std::string &name, int n1, int n2, int *pd) |
| 242 |
{ |
| 243 |
int error = 0; |
| 244 |
if (textfile != 0) { |
| 245 |
mPutInt(name, n1, n2, pd); |
| 246 |
} |
| 247 |
else { |
| 248 |
error = matPutInt(name, n1, n2, pd); |
| 249 |
} |
| 250 |
return error; |
| 251 |
} |
| 252 |
|
| 253 |
int PutDbl(const std::string &name, int n1, int n2, double *pd) |
| 254 |
{ |
| 255 |
int error = 0; |
| 256 |
if (textfile != 0) { |
| 257 |
mPutDbl(name, n1, n2, pd); |
| 258 |
} |
| 259 |
else { |
| 260 |
error = matPutDbl(name, n1, n2, pd); |
| 261 |
} |
| 262 |
return error; |
| 263 |
} |
| 264 |
|
| 265 |
char **get_exodus_names(size_t count, int size) |
| 266 |
{ |
| 267 |
auto *names = new char *[count]; |
| 268 |
for (size_t i = 0; i < count; i++) { |
| 269 |
names[i] = new char[size + 1]; |
| 270 |
std::memset(names[i], '\0', size + 1); |
| 271 |
} |
| 272 |
return names; |
| 273 |
} |
| 274 |
|
| 275 |
void delete_exodus_names(char **names, int count) |
| 276 |
{ |
| 277 |
for (int i = 0; i < count; i++) { |
| 278 |
delete[] names[i]; |
| 279 |
} |
| 280 |
delete[] names; |
| 281 |
} |
| 282 |
|
| 283 |
void get_put_user_names(int exo_file, ex_entity_type type, int num_blocks, |
| 284 |
const std::string &mname) |
| 285 |
{ |
| 286 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 287 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 288 |
ex_set_max_name_length(exo_file, max_name_length); |
| 289 |
char **names = get_exodus_names(num_blocks, max_name_length + 1); |
| 290 |
ex_get_names(exo_file, type, names); |
| 291 |
|
| 292 |
std::string user_names; |
| 293 |
for (int j = 0; j < num_blocks; j++) { |
| 294 |
user_names += names[j]; |
| 295 |
user_names += "\n"; |
| 296 |
} |
| 297 |
PutStr(mname, user_names); |
| 298 |
delete_exodus_names(names, num_blocks); |
| 299 |
} |
| 300 |
|
| 301 |
void get_put_names(int exo_file, ex_entity_type type, int num_vars, const std::string &mname) |
| 302 |
{ |
| 303 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 304 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 305 |
char **names = get_exodus_names(num_vars, max_name_length + 1); |
| 306 |
|
| 307 |
if (debug) { |
| 308 |
logger("\tReading variable names"); |
| 309 |
} |
| 310 |
ex_get_variable_names(exo_file, type, num_vars, names); |
| 311 |
|
| 312 |
std::string mat; |
| 313 |
for (int i = 0; i < num_vars; i++) { |
| 314 |
mat += names[i]; |
| 315 |
mat += "\n"; |
| 316 |
} |
| 317 |
if (debug) { |
| 318 |
logger("\tWriting variable names"); |
| 319 |
} |
| 320 |
PutStr(mname, mat); |
| 321 |
|
| 322 |
delete_exodus_names(names, num_vars); |
| 323 |
} |
| 324 |
|
| 325 |
std::vector<std::string> get_names(int exo_file, ex_entity_type type, int num_vars) |
| 326 |
{ |
| 327 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 328 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 329 |
char **names = get_exodus_names(num_vars, max_name_length + 1); |
| 330 |
|
| 331 |
if (debug) { |
| 332 |
logger("\tReading variable names"); |
| 333 |
} |
| 334 |
ex_get_variable_names(exo_file, type, num_vars, names); |
| 335 |
|
| 336 |
std::vector<std::string> mat(num_vars); |
| 337 |
for (int i = 0; i < num_vars; i++) { |
| 338 |
mat[i] = names[i]; |
| 339 |
} |
| 340 |
delete_exodus_names(names, num_vars); |
| 341 |
return mat; |
| 342 |
} |
| 343 |
|
| 344 |
void get_put_vars(int exo_file, ex_entity_type type, int num_blocks, int num_vars, |
| 345 |
int num_time_steps, const std::vector<int> &num_per_block, |
| 346 |
const std::string &prefix, bool use_cell_arrays) |
| 347 |
|
| 348 |
{ |
| 349 |
/* truth table */ |
| 350 |
if (debug) { |
| 351 |
logger("\tTruth Table"); |
| 352 |
} |
| 353 |
std::vector<int> truth_table(num_vars * num_blocks); |
| 354 |
ex_get_truth_table(exo_file, type, num_blocks, num_vars, Data(truth_table)); |
| 355 |
|
| 356 |
std::vector<int> ids(num_blocks); |
| 357 |
ex_get_ids(exo_file, type, Data(ids)); |
| 358 |
|
| 359 |
size_t num_entity = std::accumulate(num_per_block.begin(), num_per_block.end(), 0); |
| 360 |
|
| 361 |
if (use_cell_arrays) { |
| 362 |
std::string var_name = prefix + "var"; |
| 363 |
|
| 364 |
size_t dims[2]; |
| 365 |
dims[0] = 2; |
| 366 |
dims[1] = num_vars; |
| 367 |
matvar_t *cell_array = |
| 368 |
Mat_VarCreate(var_name.c_str(), MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 369 |
assert(cell_array); |
| 370 |
|
| 371 |
std::vector<double> scr(static_cast<size_t>(num_vars) * num_time_steps * num_entity); |
| 372 |
dims[0] = num_entity; |
| 373 |
dims[1] = num_time_steps; |
| 374 |
size_t offset = 0; |
| 375 |
|
| 376 |
// Get vector of variable names... |
| 377 |
auto names = get_names(exo_file, type, num_vars); |
| 378 |
|
| 379 |
std::vector<matvar_t *> cell_element(num_vars * 2); |
| 380 |
|
| 381 |
int j = 0; |
| 382 |
for (int i = 0; i < num_vars; i++) { |
| 383 |
size_t sdims[2]; |
| 384 |
sdims[0] = 1; |
| 385 |
sdims[1] = names[i].length(); |
| 386 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, sdims, |
| 387 |
(void *)names[i].c_str(), MAT_F_DONT_COPY_DATA); |
| 388 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 389 |
j++; |
| 390 |
|
| 391 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &scr[offset], |
| 392 |
MAT_F_DONT_COPY_DATA); |
| 393 |
assert(cell_element[j]); |
| 394 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 395 |
size_t n = 0; |
| 396 |
for (int jj = 0; jj < num_time_steps; jj++) { |
| 397 |
for (int k = 0; k < num_blocks; k++) { |
| 398 |
if (truth_table[num_vars * k + i] == 1) { |
| 399 |
ex_get_var(exo_file, jj + 1, type, i + 1, ids[k], num_per_block[k], &scr[n + offset]); |
| 400 |
} |
| 401 |
n += num_per_block[k]; |
| 402 |
} |
| 403 |
} |
| 404 |
offset += num_time_steps * num_entity; |
| 405 |
j++; |
| 406 |
} |
| 407 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 408 |
Mat_VarFree(cell_array); |
| 409 |
} |
| 410 |
else { |
| 411 |
std::string var_name = prefix + "names"; |
| 412 |
get_put_names(exo_file, type, num_vars, var_name); |
| 413 |
|
| 414 |
std::vector<double> scr(num_entity * num_time_steps); |
| 415 |
|
| 416 |
std::string format = prefix + "var%02d"; |
| 417 |
for (int i = 0; i < num_vars; i++) { |
| 418 |
if (debug) { |
| 419 |
logger("\tReading"); |
| 420 |
} |
| 421 |
std::fill(scr.begin(), scr.end(), 0.0); |
| 422 |
size_t n = 0; |
| 423 |
std::string str = fmt::sprintf(format.c_str(), i + 1); |
| 424 |
for (int j = 0; j < num_time_steps; j++) { |
| 425 |
for (int k = 0; k < num_blocks; k++) { |
| 426 |
if (truth_table[num_vars * k + i] == 1) { |
| 427 |
ex_get_var(exo_file, j + 1, type, i + 1, ids[k], num_per_block[k], &scr[n]); |
| 428 |
} |
| 429 |
n = n + num_per_block[k]; |
| 430 |
} |
| 431 |
} |
| 432 |
if (debug) { |
| 433 |
logger("\tWriting"); |
| 434 |
} |
| 435 |
PutDbl(str, num_entity, num_time_steps, Data(scr)); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
std::vector<int> handle_element_blocks(int exo_file, int num_blocks, bool use_cell_arrays) |
| 441 |
{ |
| 442 |
std::vector<int> ids(num_blocks); |
| 443 |
ex_get_ids(exo_file, EX_ELEM_BLOCK, Data(ids)); |
| 444 |
|
| 445 |
std::vector<int> num_elem_in_block(num_blocks); |
| 446 |
|
| 447 |
// Storing: |
| 448 |
// 1) name |
| 449 |
// 2) id |
| 450 |
// 3) block topology type |
| 451 |
// 4) connectivity |
| 452 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 453 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 454 |
if (use_cell_arrays) { |
| 455 |
int num_field = 4; |
| 456 |
size_t dims[2]; |
| 457 |
dims[0] = num_field; |
| 458 |
dims[1] = num_blocks; |
| 459 |
matvar_t *cell_array = |
| 460 |
Mat_VarCreate("element_blocks", MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 461 |
assert(cell_array); |
| 462 |
|
| 463 |
std::vector<matvar_t *> cell_element(num_blocks * num_field); |
| 464 |
|
| 465 |
std::vector<int> num_node_per_elem(num_blocks); |
| 466 |
|
| 467 |
size_t conn_size = 0; |
| 468 |
std::vector<std::string> types(num_blocks); |
| 469 |
|
| 470 |
for (int i = 0; i < num_blocks; i++) { |
| 471 |
char type[33]; |
| 472 |
int num_elem = 0; |
| 473 |
int num_node = 0; |
| 474 |
int num_attr = 0; |
| 475 |
ex_get_block(exo_file, EX_ELEM_BLOCK, ids[i], type, &num_elem, &num_node, nullptr, nullptr, |
| 476 |
&num_attr); |
| 477 |
types[i] = std::string(type); |
| 478 |
num_elem_in_block[i] = num_elem; |
| 479 |
num_node_per_elem[i] = num_node; |
| 480 |
conn_size += static_cast<size_t>(num_elem) * num_node; |
| 481 |
} |
| 482 |
|
| 483 |
std::vector<int> connect(conn_size); |
| 484 |
size_t conn_off = 0; |
| 485 |
for (int i = 0; i < num_blocks; i++) { |
| 486 |
std::vector<char> name(max_name_length + 1); |
| 487 |
ex_get_name(exo_file, EX_ELEM_BLOCK, ids[i], Data(name)); |
| 488 |
dims[0] = 1; |
| 489 |
dims[1] = std::strlen(Data(name)); |
| 490 |
size_t index = num_field * i + 0; |
| 491 |
cell_element[index] = |
| 492 |
Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, dims, (void *)Data(name), 0); |
| 493 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 494 |
|
| 495 |
dims[0] = 1; |
| 496 |
dims[1] = 1; |
| 497 |
index = num_field * i + 1; |
| 498 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, &ids[i], |
| 499 |
MAT_F_DONT_COPY_DATA); |
| 500 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 501 |
|
| 502 |
dims[0] = 1; |
| 503 |
dims[1] = types[i].length(); |
| 504 |
index = num_field * i + 2; |
| 505 |
cell_element[index] = |
| 506 |
Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, dims, (void *)types[i].c_str(), 0); |
| 507 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 508 |
|
| 509 |
dims[0] = num_node_per_elem[i]; |
| 510 |
dims[1] = num_elem_in_block[i]; |
| 511 |
index = num_field * i + 3; |
| 512 |
ex_get_conn(exo_file, EX_ELEM_BLOCK, ids[i], &connect[conn_off], nullptr, nullptr); |
| 513 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 514 |
&connect[conn_off], MAT_F_DONT_COPY_DATA); |
| 515 |
assert(cell_element[index]); |
| 516 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 517 |
|
| 518 |
conn_off += num_node_per_elem[i] * num_elem_in_block[i]; |
| 519 |
} |
| 520 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 521 |
Mat_VarFree(cell_array); |
| 522 |
} |
| 523 |
else { |
| 524 |
std::vector<int> connect; |
| 525 |
std::vector<double> attr; |
| 526 |
|
| 527 |
PutInt("blkids", num_blocks, 1, Data(ids)); |
| 528 |
std::vector<char> type(max_name_length + 1); |
| 529 |
std::string types; |
| 530 |
for (int i = 0; i < num_blocks; i++) { |
| 531 |
int num_elem = 0; |
| 532 |
int num_node = 0; |
| 533 |
int num_attr = 0; |
| 534 |
ex_get_block(exo_file, EX_ELEM_BLOCK, ids[i], Data(type), &num_elem, &num_node, nullptr, |
| 535 |
nullptr, &num_attr); |
| 536 |
types += Data(type); |
| 537 |
types += "\n"; |
| 538 |
num_elem_in_block[i] = num_elem; |
| 539 |
connect.resize(static_cast<size_t>(num_elem) * num_node); |
| 540 |
ex_get_conn(exo_file, EX_ELEM_BLOCK, ids[i], Data(connect), nullptr, nullptr); |
| 541 |
std::string str = fmt::sprintf("blk%02d", i + 1); |
| 542 |
PutInt(str, num_node, num_elem, Data(connect)); |
| 543 |
|
| 544 |
// Handle block attributes (if any...) |
| 545 |
attr.resize(num_elem); |
| 546 |
str = fmt::sprintf("blk%02d_nattr", i + 1); |
| 547 |
PutInt(str, num_attr); |
| 548 |
if (num_attr > 0) { |
| 549 |
std::string attr_names; |
| 550 |
char **names = get_exodus_names(num_attr, max_name_length + 1); |
| 551 |
ex_get_attr_names(exo_file, EX_ELEM_BLOCK, ids[i], names); |
| 552 |
for (int j = 0; j < num_attr; j++) { |
| 553 |
attr_names += names[j]; |
| 554 |
attr_names += "\n"; |
| 555 |
} |
| 556 |
str = fmt::sprintf("blk%02d_attrnames", i + 1); |
| 557 |
PutStr(str, attr_names); |
| 558 |
delete_exodus_names(names, num_attr); |
| 559 |
|
| 560 |
for (int j = 0; j < num_attr; j++) { |
| 561 |
str = fmt::sprintf("blk%02d_attr%02d", i + 1, j + 1); |
| 562 |
ex_get_one_attr(exo_file, EX_ELEM_BLOCK, ids[i], j + 1, Data(attr)); |
| 563 |
PutDbl(str, num_elem, 1, Data(attr)); |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
get_put_user_names(exo_file, EX_ELEM_BLOCK, num_blocks, "blkusernames"); |
| 569 |
PutStr("blknames", types); |
| 570 |
} |
| 571 |
return num_elem_in_block; |
| 572 |
} |
| 573 |
|
| 574 |
std::vector<int> handle_node_sets(int exo_file, int num_sets, bool use_cell_arrays) |
| 575 |
{ |
| 576 |
std::vector<int> num_nodes(num_sets); |
| 577 |
if (num_sets > 0) { |
| 578 |
if (debug) { |
| 579 |
logger("Node Sets"); |
| 580 |
} |
| 581 |
std::vector<int> ids(num_sets); |
| 582 |
ex_get_ids(exo_file, EX_NODE_SET, Data(ids)); |
| 583 |
|
| 584 |
size_t tot_nodes = 0; |
| 585 |
size_t tot_dfac = 0; |
| 586 |
std::vector<int> num_df(num_sets); |
| 587 |
for (int i = 0; i < num_sets; i++) { |
| 588 |
int n1; |
| 589 |
int n2; |
| 590 |
ex_get_set_param(exo_file, EX_NODE_SET, ids[i], &n1, &n2); |
| 591 |
num_nodes[i] = n1; |
| 592 |
num_df[i] = n2; |
| 593 |
tot_nodes += n1; |
| 594 |
tot_dfac += n2; |
| 595 |
} |
| 596 |
|
| 597 |
// Storing: |
| 598 |
// 1) name |
| 599 |
// 2) id |
| 600 |
// 3) node list |
| 601 |
// 4) distribution factors |
| 602 |
if (use_cell_arrays) { |
| 603 |
size_t dims[2]; |
| 604 |
dims[0] = 4; |
| 605 |
dims[1] = num_sets; |
| 606 |
matvar_t *cell_array = |
| 607 |
Mat_VarCreate("node_sets", MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 608 |
assert(cell_array); |
| 609 |
|
| 610 |
std::vector<matvar_t *> cell_element(num_sets * 4); |
| 611 |
|
| 612 |
std::vector<int> node_list(tot_nodes); |
| 613 |
std::vector<double> dist_fac(tot_dfac); |
| 614 |
size_t nl_off = 0; |
| 615 |
size_t df_off = 0; |
| 616 |
|
| 617 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 618 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 619 |
|
| 620 |
for (int i = 0; i < num_sets; i++) { |
| 621 |
std::vector<char> name(max_name_length + 1); |
| 622 |
ex_get_name(exo_file, EX_NODE_SET, ids[i], Data(name)); |
| 623 |
dims[0] = 1; |
| 624 |
dims[1] = std::strlen(Data(name)); |
| 625 |
size_t index = 4 * i + 0; |
| 626 |
cell_element[index] = |
| 627 |
Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, dims, (void *)Data(name), 0); |
| 628 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 629 |
|
| 630 |
dims[0] = 1; |
| 631 |
dims[1] = 1; |
| 632 |
index = 4 * i + 1; |
| 633 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, &ids[i], |
| 634 |
MAT_F_DONT_COPY_DATA); |
| 635 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 636 |
|
| 637 |
dims[0] = num_nodes[i]; |
| 638 |
dims[1] = 1; |
| 639 |
index = 4 * i + 2; |
| 640 |
ex_get_set(exo_file, EX_NODE_SET, ids[i], &node_list[nl_off], nullptr); |
| 641 |
/* nodes list */ |
| 642 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 643 |
&node_list[nl_off], MAT_F_DONT_COPY_DATA); |
| 644 |
assert(cell_element[index]); |
| 645 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 646 |
|
| 647 |
/* distribution-factors list */ |
| 648 |
ex_get_set_dist_fact(exo_file, EX_NODE_SET, ids[i], &dist_fac[df_off]); |
| 649 |
index = 4 * i + 3; |
| 650 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, |
| 651 |
&dist_fac[df_off], MAT_F_DONT_COPY_DATA); |
| 652 |
assert(cell_element[index]); |
| 653 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 654 |
|
| 655 |
nl_off += num_nodes[i]; |
| 656 |
df_off += num_df[i]; |
| 657 |
} |
| 658 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 659 |
Mat_VarFree(cell_array); |
| 660 |
} |
| 661 |
else { |
| 662 |
PutInt("nsids", num_sets, 1, Data(ids)); |
| 663 |
|
| 664 |
for (int i = 0; i < num_sets; i++) { |
| 665 |
std::vector<int> node_list(num_nodes[i]); |
| 666 |
ex_get_set(exo_file, EX_NODE_SET, ids[i], Data(node_list), nullptr); |
| 667 |
/* nodes list */ |
| 668 |
std::string str; |
| 669 |
str = fmt::sprintf("nsnod%02d", i + 1); |
| 670 |
PutInt(str, node_list.size(), 1, Data(node_list)); |
| 671 |
|
| 672 |
/* distribution-factors list */ |
| 673 |
if (num_df[i] > 0) { |
| 674 |
std::vector<double> dist_fac(num_df[i]); |
| 675 |
ex_get_set_dist_fact(exo_file, EX_NODE_SET, ids[i], Data(dist_fac)); |
| 676 |
str = fmt::sprintf("nsfac%02d", i + 1); |
| 677 |
PutDbl(str, dist_fac.size(), 1, Data(dist_fac)); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
get_put_user_names(exo_file, EX_NODE_SET, num_sets, "nsusernames"); |
| 683 |
|
| 684 |
/* Store # nodes and # dis. factors per node set */ |
| 685 |
PutInt("nnsnodes", num_sets, 1, Data(num_nodes)); |
| 686 |
PutInt("nnsdfac", num_sets, 1, Data(num_df)); |
| 687 |
} |
| 688 |
return num_nodes; |
| 689 |
} |
| 690 |
|
| 691 |
std::vector<int> handle_side_sets(int exo_file, int num_sets, bool use_cell_arrays) |
| 692 |
{ |
| 693 |
std::vector<int> num_sideset_sides(num_sets); |
| 694 |
std::vector<int> num_sideset_dfac(num_sets); |
| 695 |
std::vector<int> num_sideset_nodes(num_sets); |
| 696 |
if (num_sets > 0) { |
| 697 |
std::vector<int> ids(num_sets); |
| 698 |
ex_get_ids(exo_file, EX_SIDE_SET, Data(ids)); |
| 699 |
|
| 700 |
// Storing: |
| 701 |
// 1) name |
| 702 |
// 2) id |
| 703 |
// 3) element list |
| 704 |
// 4) side list |
| 705 |
// 5) node count per face |
| 706 |
// 6) face node list |
| 707 |
// 7) distribution factors |
| 708 |
|
| 709 |
if (use_cell_arrays) { |
| 710 |
size_t dims[2]; |
| 711 |
dims[0] = 7; |
| 712 |
dims[1] = num_sets; |
| 713 |
matvar_t *cell_array = |
| 714 |
Mat_VarCreate("side_sets", MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 715 |
assert(cell_array); |
| 716 |
|
| 717 |
std::vector<matvar_t *> cell_element(num_sets * 7); |
| 718 |
|
| 719 |
size_t num_sides = ex_inquire_int(exo_file, EX_INQ_SS_ELEM_LEN); |
| 720 |
size_t num_nodes = ex_inquire_int(exo_file, EX_INQ_SS_NODE_LEN); |
| 721 |
std::vector<int> elem_list(num_sides); |
| 722 |
std::vector<int> side_list(num_sides); |
| 723 |
std::vector<int> num_nodes_per_side(num_sides); |
| 724 |
std::vector<int> side_nodes(num_nodes); |
| 725 |
|
| 726 |
// size_t num_df = ex_inquire_int(exo_file, EX_INQ_SS_DF_LEN); |
| 727 |
// If `num_df == 0` or if it isn't equal to `num_nodes`, then |
| 728 |
// all df will be set to 1.0, but in any case, the size of |
| 729 |
// `ssdfac` should be num_nodes and not num_df. |
| 730 |
std::vector<double> ssdfac(num_nodes); |
| 731 |
|
| 732 |
size_t side_off = 0; |
| 733 |
size_t node_off = 0; |
| 734 |
size_t df_off = 0; |
| 735 |
|
| 736 |
int max_name_length = ex_inquire_int(exo_file, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 737 |
max_name_length = max_name_length < 32 ? 32 : max_name_length; |
| 738 |
|
| 739 |
for (int i = 0; i < num_sets; i++) { |
| 740 |
std::vector<char> name(max_name_length + 1); |
| 741 |
ex_get_name(exo_file, EX_SIDE_SET, ids[i], Data(name)); |
| 742 |
dims[0] = 1; |
| 743 |
dims[1] = std::strlen(Data(name)); |
| 744 |
size_t index = 7 * i + 0; |
| 745 |
cell_element[index] = |
| 746 |
Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, dims, (void *)Data(name), 0); |
| 747 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 748 |
|
| 749 |
dims[0] = 1; |
| 750 |
dims[1] = 1; |
| 751 |
index = 7 * i + 1; |
| 752 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, &ids[i], |
| 753 |
MAT_F_DONT_COPY_DATA); |
| 754 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 755 |
|
| 756 |
int n1; |
| 757 |
int n2; |
| 758 |
ex_get_set_param(exo_file, EX_SIDE_SET, ids[i], &n1, &n2); |
| 759 |
num_sideset_sides[i] = n1; |
| 760 |
num_sideset_dfac[i] = n2; |
| 761 |
|
| 762 |
bool has_ss_dfac = (n2 != 0 && n1 != n2); |
| 763 |
if (!has_ss_dfac) { |
| 764 |
num_sideset_dfac[i] = num_sideset_nodes[i]; |
| 765 |
} |
| 766 |
|
| 767 |
ex_get_side_set_node_list_len(exo_file, ids[i], &num_sideset_nodes[i]); |
| 768 |
|
| 769 |
/* element and side list for side sets (dgriffi) */ |
| 770 |
ex_get_set(exo_file, EX_SIDE_SET, ids[i], &elem_list[side_off], &side_list[side_off]); |
| 771 |
dims[0] = num_sideset_sides[i]; |
| 772 |
dims[1] = 1; |
| 773 |
index = 7 * i + 2; |
| 774 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 775 |
&elem_list[side_off], MAT_F_DONT_COPY_DATA); |
| 776 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 777 |
|
| 778 |
index = 7 * i + 3; |
| 779 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 780 |
&side_list[side_off], MAT_F_DONT_COPY_DATA); |
| 781 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 782 |
|
| 783 |
ex_get_side_set_node_list(exo_file, ids[i], &num_nodes_per_side[side_off], |
| 784 |
&side_nodes[node_off]); |
| 785 |
|
| 786 |
/* number-of-nodes-per-side list */ |
| 787 |
index = 7 * i + 4; |
| 788 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 789 |
&num_nodes_per_side[side_off], MAT_F_DONT_COPY_DATA); |
| 790 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 791 |
|
| 792 |
dims[0] = num_sideset_nodes[i]; |
| 793 |
dims[1] = 1; |
| 794 |
index = 7 * i + 5; |
| 795 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_INT32, MAT_T_INT32, 2, dims, |
| 796 |
&side_nodes[node_off], MAT_F_DONT_COPY_DATA); |
| 797 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 798 |
|
| 799 |
/* distribution-factors list */ |
| 800 |
if (has_ss_dfac) { |
| 801 |
ex_get_set_dist_fact(exo_file, EX_SIDE_SET, ids[i], &ssdfac[df_off]); |
| 802 |
} |
| 803 |
else { |
| 804 |
n2 = num_sideset_dfac[i]; |
| 805 |
for (int j = 0; j < n2; j++) { |
| 806 |
ssdfac[j] = 1.0; |
| 807 |
} |
| 808 |
} |
| 809 |
dims[0] = num_sideset_dfac[i]; |
| 810 |
dims[1] = 1; |
| 811 |
index = 7 * i + 6; |
| 812 |
cell_element[index] = Mat_VarCreate(nullptr, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, |
| 813 |
&ssdfac[df_off], MAT_F_DONT_COPY_DATA); |
| 814 |
Mat_VarSetCell(cell_array, index, cell_element[index]); |
| 815 |
|
| 816 |
side_off += num_sideset_sides[i]; |
| 817 |
node_off += num_sideset_nodes[i]; |
| 818 |
df_off += num_sideset_dfac[i]; |
| 819 |
} |
| 820 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 821 |
Mat_VarFree(cell_array); |
| 822 |
} |
| 823 |
else { |
| 824 |
PutInt("ssids", num_sets, 1, Data(ids)); |
| 825 |
std::vector<int> elem_list; |
| 826 |
std::vector<int> side_list; |
| 827 |
std::vector<int> num_nodes_per_side; |
| 828 |
std::vector<int> side_nodes; |
| 829 |
std::vector<double> ssdfac; |
| 830 |
for (int i = 0; i < num_sets; i++) { |
| 831 |
int n1; |
| 832 |
int n2; |
| 833 |
ex_get_set_param(exo_file, EX_SIDE_SET, ids[i], &n1, &n2); |
| 834 |
num_sideset_sides[i] = n1; |
| 835 |
num_sideset_dfac[i] = n2; |
| 836 |
|
| 837 |
bool has_ss_dfac = (n2 != 0); |
| 838 |
if (n2 == 0 || n1 == n2) { |
| 839 |
ex_get_side_set_node_list_len(exo_file, ids[i], &n2); |
| 840 |
} |
| 841 |
|
| 842 |
num_nodes_per_side.resize(n1); |
| 843 |
side_nodes.resize(n2); |
| 844 |
ex_get_side_set_node_list(exo_file, ids[i], Data(num_nodes_per_side), Data(side_nodes)); |
| 845 |
|
| 846 |
/* number-of-nodes-per-side list */ |
| 847 |
std::string str; |
| 848 |
str = fmt::sprintf("ssnum%02d", i + 1); |
| 849 |
PutInt(str, n1, 1, Data(num_nodes_per_side)); |
| 850 |
/* nodes list */ |
| 851 |
str = fmt::sprintf("ssnod%02d", i + 1); |
| 852 |
PutInt(str, n2, 1, Data(side_nodes)); |
| 853 |
|
| 854 |
/* distribution-factors list */ |
| 855 |
if (has_ss_dfac) { |
| 856 |
ssdfac.resize(n2); |
| 857 |
ex_get_set_dist_fact(exo_file, EX_SIDE_SET, ids[i], Data(ssdfac)); |
| 858 |
str = fmt::sprintf("ssfac%02d", i + 1); |
| 859 |
PutDbl(str, n2, 1, Data(ssdfac)); |
| 860 |
} |
| 861 |
|
| 862 |
/* element and side list for side sets (dgriffi) */ |
| 863 |
elem_list.resize(n1); |
| 864 |
side_list.resize(n1); |
| 865 |
ex_get_set(exo_file, EX_SIDE_SET, ids[i], Data(elem_list), Data(side_list)); |
| 866 |
str = fmt::sprintf("ssside%02d", i + 1); |
| 867 |
PutInt(str, n1, 1, Data(side_list)); |
| 868 |
str = fmt::sprintf("sselem%02d", i + 1); |
| 869 |
PutInt(str, n1, 1, Data(elem_list)); |
| 870 |
} |
| 871 |
} |
| 872 |
get_put_user_names(exo_file, EX_SIDE_SET, num_sets, "ssusernames"); |
| 873 |
|
| 874 |
/* Store # sides and # dis. factors per side set (dgriffi) */ |
| 875 |
PutInt("nsssides", num_sets, 1, Data(num_sideset_sides)); |
| 876 |
PutInt("nssdfac", num_sets, 1, Data(num_sideset_dfac)); |
| 877 |
} |
| 878 |
return num_sideset_sides; |
| 879 |
} |
| 880 |
|
| 881 |
void handle_coordinates(int exo_file, size_t num_nodes, int num_axes) |
| 882 |
{ |
| 883 |
if (debug) { |
| 884 |
logger("Coordinates"); |
| 885 |
} |
| 886 |
std::vector<double> x, y, z; |
| 887 |
x.resize(num_nodes); |
| 888 |
if (num_axes >= 2) { |
| 889 |
y.resize(num_nodes); |
| 890 |
} |
| 891 |
if (num_axes == 3) { |
| 892 |
z.resize(num_nodes); |
| 893 |
} |
| 894 |
ex_get_coord(exo_file, Data(x), Data(y), Data(z)); |
| 895 |
PutDbl("x0", num_nodes, 1, Data(x)); |
| 896 |
if (num_axes >= 2) { |
| 897 |
PutDbl("y0", num_nodes, 1, Data(y)); |
| 898 |
} |
| 899 |
if (num_axes == 3) { |
| 900 |
PutDbl("z0", num_nodes, 1, Data(z)); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
/**********************************************************************/ |
| 905 |
/* remove an argument from the list */ |
| 906 |
void del_arg(int *argc, char *argv[], int j) |
| 907 |
{ |
| 908 |
for (int jj = j + 1; jj < *argc; jj++) { |
| 909 |
argv[jj - 1] = argv[jj]; |
| 910 |
} |
| 911 |
(*argc)--; |
| 912 |
argv[*argc] = nullptr; |
| 913 |
} |
| 914 |
} // namespace |
| 915 |
|
| 916 |
/**********************************************************************/ |
| 917 |
int main(int argc, char *argv[]) |
| 918 |
{ |
| 919 |
std::string oname{}; |
| 920 |
std::string filename{}; |
| 921 |
|
| 922 |
std::string str; |
| 923 |
|
| 924 |
std::string ext = EXT; |
| 925 |
|
| 926 |
int err; |
| 927 |
int num_axes; |
| 928 |
int num_blocks; |
| 929 |
int num_side_sets; |
| 930 |
int num_node_sets; |
| 931 |
int num_time_steps; |
| 932 |
int num_info_lines; |
| 933 |
int num_global_vars; |
| 934 |
int num_nodal_vars; |
| 935 |
int num_element_vars; |
| 936 |
int num_nodeset_vars; |
| 937 |
int num_sideset_vars; |
| 938 |
|
| 939 |
size_t num_nodes = 0; |
| 940 |
size_t num_elements = 0; |
| 941 |
|
| 942 |
enum mat_ft mat_version = MAT_FT_DEFAULT; |
| 943 |
bool use_cell_arrays = false; |
| 944 |
|
| 945 |
int cs_index = 0; |
| 946 |
|
| 947 |
/* process arguments */ |
| 948 |
for (int j = 1; j < argc && argv[j][0] == '-'; j++) { |
| 949 |
if (strcmp(argv[j], "-t") == 0) { /* write text file (*.m) */ |
| 950 |
del_arg(&argc, argv, j); |
| 951 |
textfile = 1; |
| 952 |
j--; |
| 953 |
continue; |
| 954 |
} |
| 955 |
if (strcmp(argv[j], "-h") == 0 || strcmp(argv[j], "--help") == 0) { /* write help info */ |
| 956 |
del_arg(&argc, argv, j); |
| 957 |
usage(); |
| 958 |
exit(1); |
| 959 |
} |
| 960 |
if (strcmp(argv[j], "-d") == 0) { /* write help info */ |
| 961 |
del_arg(&argc, argv, j); |
| 962 |
j--; |
| 963 |
debug = true; |
| 964 |
continue; |
| 965 |
} |
| 966 |
if (strcmp(argv[j], "-c") == 0) { /* use cell arrays */ |
| 967 |
del_arg(&argc, argv, j); |
| 968 |
j--; |
| 969 |
use_cell_arrays = true; |
| 970 |
continue; |
| 971 |
} |
| 972 |
if (strcmp(argv[j], "-v73") == 0) { /* Version 7.3 */ |
| 973 |
del_arg(&argc, argv, j); |
| 974 |
mat_version = MAT_FT_MAT73; |
| 975 |
j--; |
| 976 |
continue; |
| 977 |
} |
| 978 |
// This matches the option used in matlab |
| 979 |
if ((strcmp(argv[j], "-v7.3") == 0) || (strcmp(argv[j], "-V7.3") == 0)) { /* Version 7.3 */ |
| 980 |
del_arg(&argc, argv, j); |
| 981 |
mat_version = MAT_FT_MAT73; |
| 982 |
j--; |
| 983 |
continue; |
| 984 |
} |
| 985 |
if (strcmp(argv[j], "-v5") == 0) { /* Version 5 (default) */ |
| 986 |
del_arg(&argc, argv, j); |
| 987 |
mat_version = MAT_FT_MAT5; |
| 988 |
j--; |
| 989 |
continue; |
| 990 |
} |
| 991 |
if (strcmp(argv[j], "-o") == 0) { /* specify output file name */ |
| 992 |
del_arg(&argc, argv, j); |
| 993 |
if (argv[j] != nullptr) { |
| 994 |
oname = argv[j]; |
| 995 |
del_arg(&argc, argv, j); |
| 996 |
fmt::print("output file: {}\n", oname); |
| 997 |
} |
| 998 |
else { |
| 999 |
fmt::print(stderr, "ERROR: Invalid output file specification.\n"); |
| 1000 |
return 2; |
| 1001 |
} |
| 1002 |
j--; |
| 1003 |
continue; |
| 1004 |
} |
| 1005 |
if (strcmp(argv[j], "-change_set") == 0) { |
| 1006 |
del_arg(&argc, argv, j); |
| 1007 |
if (argv[j] != nullptr) { |
| 1008 |
cs_index = std::stoi(argv[j]); |
| 1009 |
del_arg(&argc, argv, j); |
| 1010 |
} |
| 1011 |
else { |
| 1012 |
fmt::print(stderr, "ERROR: Invalid output file specification.\n"); |
| 1013 |
return 2; |
| 1014 |
} |
| 1015 |
j--; |
| 1016 |
continue; |
| 1017 |
} |
| 1018 |
// Unrecognized option... |
| 1019 |
fmt::print(stderr, "ERROR: Unrecognized option: '{}'\n", argv[j]); |
| 1020 |
usage(); |
| 1021 |
exit(1); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* QA Info */ |
| 1025 |
fmt::print("{}: {}, {}\n", qainfo[0], qainfo[2], qainfo[1]); |
| 1026 |
|
| 1027 |
/* usage message*/ |
| 1028 |
if (argc != 2) { |
| 1029 |
usage(); |
| 1030 |
exit(1); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* open output file */ |
| 1034 |
if (textfile != 0) { |
| 1035 |
ext = ".m"; |
| 1036 |
} |
| 1037 |
if (oname.empty()) { |
| 1038 |
filename = argv[1]; |
| 1039 |
size_t pos = filename.find_last_of('.'); |
| 1040 |
if (pos != std::string::npos) { |
| 1041 |
filename = filename.substr(0, pos); |
| 1042 |
} |
| 1043 |
filename += ext; |
| 1044 |
} |
| 1045 |
else { |
| 1046 |
filename = oname; |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (textfile != 0) { |
| 1050 |
m_file = fopen(filename.c_str(), "w"); |
| 1051 |
if (m_file == nullptr) { |
| 1052 |
fmt::print(stderr, "ERROR: Unable to open '{}'\n", filename); |
| 1053 |
exit(1); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
else { |
| 1057 |
mat_file = Mat_CreateVer(filename.c_str(), nullptr, mat_version); |
| 1058 |
if (mat_file == nullptr) { |
| 1059 |
fmt::print(stderr, "ERROR: Unable to create matlab file '{}'\n", filename); |
| 1060 |
exit(1); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
ex_opts(EX_VERBOSE); |
| 1065 |
|
| 1066 |
/* word sizes */ |
| 1067 |
int cpu_word_size = sizeof(double); |
| 1068 |
int io_word_size = 0; |
| 1069 |
|
| 1070 |
/* open exodus file */ |
| 1071 |
float exo_version; |
| 1072 |
int exo_file = ex_open(argv[1], EX_READ, &cpu_word_size, &io_word_size, &exo_version); |
| 1073 |
if (exo_file < 0) { |
| 1074 |
fmt::print(stderr, "ERROR: Cannot open '{}'\n", argv[1]); |
| 1075 |
exit(1); |
| 1076 |
} |
| 1077 |
// See if file has change sets... |
| 1078 |
int num_change_sets = ex_inquire_int(exo_file, EX_INQ_NUM_CHILD_GROUPS); |
| 1079 |
if (num_change_sets > 0) { |
| 1080 |
if (cs_index == 0) { |
| 1081 |
fmt::print(stderr, |
| 1082 |
"WARNING: Exodus database contains {} change sets.\n Setting to read from " |
| 1083 |
"first change set since `-change_set #` option not specified.\n\n", |
| 1084 |
num_change_sets); |
| 1085 |
exo_file++; |
| 1086 |
} |
| 1087 |
else if (cs_index > num_change_sets) { |
| 1088 |
fmt::print(stderr, |
| 1089 |
"ERROR: The selected change set ({}) exceeds the number of change sets ({}) on " |
| 1090 |
"the exodus database.\n", |
| 1091 |
cs_index, num_change_sets); |
| 1092 |
exit(1); |
| 1093 |
} |
| 1094 |
else { |
| 1095 |
// Set to selected change set |
| 1096 |
fmt::print(stderr, "NOTE: Exodus data will be read from change set {}.\n\n", cs_index); |
| 1097 |
exo_file += cs_index; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
else if (cs_index > 0) { |
| 1101 |
// Error, selected change set, but there are none. |
| 1102 |
fmt::print(stderr, |
| 1103 |
"ERROR: Change set {} was selected, but the exodus database does not contain change " |
| 1104 |
"sets.\n", |
| 1105 |
cs_index, num_change_sets); |
| 1106 |
exit(1); |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* print */ |
| 1110 |
fmt::print("\ttranslating {} to {}...\n", argv[1], filename); |
| 1111 |
|
| 1112 |
/* read database parameters */ |
| 1113 |
std::array<char, MAX_LINE_LENGTH + 1> line; |
| 1114 |
ex_get_init(exo_file, line.data(), &num_axes, &num_nodes, &num_elements, &num_blocks, |
| 1115 |
&num_node_sets, &num_side_sets); |
| 1116 |
num_info_lines = ex_inquire_int(exo_file, EX_INQ_INFO); |
| 1117 |
num_time_steps = ex_inquire_int(exo_file, EX_INQ_TIME); |
| 1118 |
ex_get_variable_param(exo_file, EX_GLOBAL, &num_global_vars); |
| 1119 |
ex_get_variable_param(exo_file, EX_NODAL, &num_nodal_vars); |
| 1120 |
ex_get_variable_param(exo_file, EX_ELEM_BLOCK, &num_element_vars); |
| 1121 |
ex_get_variable_param(exo_file, EX_NODE_SET, &num_nodeset_vars); |
| 1122 |
ex_get_variable_param(exo_file, EX_SIDE_SET, &num_sideset_vars); |
| 1123 |
|
| 1124 |
/* export parameters */ |
| 1125 |
PutInt("naxes", num_axes); |
| 1126 |
PutInt("nnodes", num_nodes); |
| 1127 |
PutInt("nelems", num_elements); |
| 1128 |
PutInt("nblks", num_blocks); |
| 1129 |
PutInt("nnsets", num_node_sets); |
| 1130 |
PutInt("nssets", num_side_sets); |
| 1131 |
PutInt("nsteps", num_time_steps); |
| 1132 |
PutInt("ngvars", num_global_vars); |
| 1133 |
PutInt("nnvars", num_nodal_vars); |
| 1134 |
PutInt("nevars", num_element_vars); |
| 1135 |
PutInt("nnsvars", num_nodeset_vars); |
| 1136 |
PutInt("nssvars", num_sideset_vars); |
| 1137 |
|
| 1138 |
/* allocate -char- scratch space*/ |
| 1139 |
int nstr2 = num_info_lines; |
| 1140 |
nstr2 = std::max(nstr2, num_blocks); |
| 1141 |
nstr2 = std::max(nstr2, num_node_sets); |
| 1142 |
nstr2 = std::max(nstr2, num_side_sets); |
| 1143 |
char **str2 = get_exodus_names(nstr2, 512); |
| 1144 |
|
| 1145 |
/* title */ |
| 1146 |
PutStr("Title", line.data()); |
| 1147 |
|
| 1148 |
/* information records */ |
| 1149 |
if (num_info_lines > 0) { |
| 1150 |
ex_get_info(exo_file, str2); |
| 1151 |
std::string ostr; |
| 1152 |
for (int i = 0; i < num_info_lines; i++) { |
| 1153 |
if (std::strlen(str2[i]) > 0) { |
| 1154 |
ostr += str2[i]; |
| 1155 |
ostr += "\n"; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
PutStr("info", ostr); |
| 1159 |
ostr = ""; |
| 1160 |
for (int i = 0; i < num_info_lines; i++) { |
| 1161 |
if (std::strlen(str2[i]) > 0 && strncmp(str2[i], "cavi", 4) == 0) { |
| 1162 |
ostr += str2[i]; |
| 1163 |
ostr += "\n"; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
PutStr("cvxp", ostr); |
| 1167 |
} |
| 1168 |
|
| 1169 |
/* nodal coordinates */ |
| 1170 |
handle_coordinates(exo_file, num_nodes, num_axes); |
| 1171 |
|
| 1172 |
/* side sets */ |
| 1173 |
if (debug) { |
| 1174 |
logger("Side Sets"); |
| 1175 |
} |
| 1176 |
auto num_sideset_sides = handle_side_sets(exo_file, num_side_sets, use_cell_arrays); |
| 1177 |
|
| 1178 |
/* node sets (section by dgriffi) */ |
| 1179 |
auto num_nodeset_nodes = handle_node_sets(exo_file, num_node_sets, use_cell_arrays); |
| 1180 |
|
| 1181 |
/* element blocks */ |
| 1182 |
if (debug) { |
| 1183 |
logger("Element Blocks"); |
| 1184 |
} |
| 1185 |
auto num_elem_in_block = handle_element_blocks(exo_file, num_blocks, use_cell_arrays); |
| 1186 |
|
| 1187 |
/* time values */ |
| 1188 |
if (num_time_steps > 0) { |
| 1189 |
if (debug) { |
| 1190 |
logger("Time Steps"); |
| 1191 |
} |
| 1192 |
std::vector<double> scr(num_time_steps); |
| 1193 |
ex_get_all_times(exo_file, Data(scr)); |
| 1194 |
PutDbl("time", num_time_steps, 1, Data(scr)); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* global variables */ |
| 1198 |
if (num_global_vars > 0) { |
| 1199 |
if (debug) { |
| 1200 |
logger("Global Variables"); |
| 1201 |
} |
| 1202 |
|
| 1203 |
if (use_cell_arrays) { |
| 1204 |
size_t dims[2]; |
| 1205 |
dims[0] = 2; |
| 1206 |
dims[1] = num_global_vars; |
| 1207 |
matvar_t *cell_array = Mat_VarCreate("gvar", MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 1208 |
assert(cell_array); |
| 1209 |
std::vector<double> scr(num_time_steps * num_global_vars); |
| 1210 |
dims[0] = num_time_steps; |
| 1211 |
dims[1] = 1; |
| 1212 |
size_t offset = 0; |
| 1213 |
// Get vector of variable names... |
| 1214 |
auto gnames = get_names(exo_file, EX_GLOBAL, num_global_vars); |
| 1215 |
|
| 1216 |
std::vector<matvar_t *> cell_element(num_global_vars * 2); |
| 1217 |
int j = 0; |
| 1218 |
for (int i = 0; i < num_global_vars; i++) { |
| 1219 |
size_t sdims[2]; |
| 1220 |
sdims[0] = 1; |
| 1221 |
sdims[1] = gnames[i].length(); |
| 1222 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, sdims, |
| 1223 |
(void *)gnames[i].c_str(), MAT_F_DONT_COPY_DATA); |
| 1224 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 1225 |
j++; |
| 1226 |
|
| 1227 |
ex_get_var_time(exo_file, EX_GLOBAL, i + 1, 1, 1, num_time_steps, &scr[offset]); |
| 1228 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &scr[offset], |
| 1229 |
MAT_F_DONT_COPY_DATA); |
| 1230 |
assert(cell_element[j]); |
| 1231 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 1232 |
offset += num_time_steps; |
| 1233 |
j++; |
| 1234 |
} |
| 1235 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 1236 |
Mat_VarFree(cell_array); |
| 1237 |
} |
| 1238 |
else { |
| 1239 |
get_put_names(exo_file, EX_GLOBAL, num_global_vars, "gnames"); |
| 1240 |
std::vector<double> scr(num_time_steps); |
| 1241 |
for (int i = 0; i < num_global_vars; i++) { |
| 1242 |
str = fmt::sprintf("gvar%02d", i + 1); |
| 1243 |
ex_get_var_time(exo_file, EX_GLOBAL, i + 1, 1, 1, num_time_steps, Data(scr)); |
| 1244 |
PutDbl(str, num_time_steps, 1, Data(scr)); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
/* nodal variables */ |
| 1250 |
if (num_nodal_vars > 0) { |
| 1251 |
if (debug) { |
| 1252 |
logger("Nodal Variables"); |
| 1253 |
} |
| 1254 |
if (debug) { |
| 1255 |
logger("\tNames"); |
| 1256 |
} |
| 1257 |
if (use_cell_arrays) { |
| 1258 |
size_t dims[2]; |
| 1259 |
dims[0] = 2; |
| 1260 |
dims[1] = num_nodal_vars; |
| 1261 |
matvar_t *cell_array = Mat_VarCreate("nvar", MAT_C_CELL, MAT_T_CELL, 2, dims, nullptr, 0); |
| 1262 |
assert(cell_array); |
| 1263 |
std::vector<double> scr(static_cast<size_t>(num_nodal_vars) * num_time_steps * num_nodes); |
| 1264 |
dims[0] = num_nodes; |
| 1265 |
dims[1] = num_time_steps; |
| 1266 |
size_t offset = 0; |
| 1267 |
// Get vector of variable names... |
| 1268 |
auto nnames = get_names(exo_file, EX_NODAL, num_nodal_vars); |
| 1269 |
|
| 1270 |
std::vector<matvar_t *> cell_element(num_nodal_vars * 2); |
| 1271 |
int j = 0; |
| 1272 |
for (int i = 0; i < num_nodal_vars; i++) { |
| 1273 |
size_t sdims[2]; |
| 1274 |
sdims[0] = 1; |
| 1275 |
sdims[1] = nnames[i].length(); |
| 1276 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_CHAR, MAT_T_UINT8, 2, sdims, |
| 1277 |
(void *)nnames[i].c_str(), MAT_F_DONT_COPY_DATA); |
| 1278 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 1279 |
j++; |
| 1280 |
|
| 1281 |
cell_element[j] = Mat_VarCreate(nullptr, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &scr[offset], |
| 1282 |
MAT_F_DONT_COPY_DATA); |
| 1283 |
assert(cell_element[j]); |
| 1284 |
Mat_VarSetCell(cell_array, j, cell_element[j]); |
| 1285 |
for (int k = 0; k < num_time_steps; k++) { |
| 1286 |
ex_get_var(exo_file, k + 1, EX_NODAL, i + 1, 1, num_nodes, &scr[num_nodes * k + offset]); |
| 1287 |
} |
| 1288 |
offset += num_time_steps * num_nodes; |
| 1289 |
j++; |
| 1290 |
} |
| 1291 |
Mat_VarWrite(mat_file, cell_array, MAT_COMPRESSION_NONE); |
| 1292 |
Mat_VarFree(cell_array); |
| 1293 |
} |
| 1294 |
else { |
| 1295 |
get_put_names(exo_file, EX_NODAL, num_nodal_vars, "nnames"); |
| 1296 |
|
| 1297 |
std::vector<double> scr(num_nodes * num_time_steps); |
| 1298 |
for (int i = 0; i < num_nodal_vars; i++) { |
| 1299 |
str = fmt::sprintf("nvar%02d", i + 1); |
| 1300 |
if (debug) { |
| 1301 |
logger("\tReading"); |
| 1302 |
} |
| 1303 |
for (int j = 0; j < num_time_steps; j++) { |
| 1304 |
ex_get_var(exo_file, j + 1, EX_NODAL, i + 1, 1, num_nodes, &scr[num_nodes * j]); |
| 1305 |
} |
| 1306 |
if (debug) { |
| 1307 |
logger("\tWriting"); |
| 1308 |
} |
| 1309 |
PutDbl(str, num_nodes, num_time_steps, Data(scr)); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
/* element variables */ |
| 1315 |
if (num_element_vars > 0) { |
| 1316 |
if (debug) { |
| 1317 |
logger("Element Variables"); |
| 1318 |
} |
| 1319 |
get_put_vars(exo_file, EX_ELEM_BLOCK, num_blocks, num_element_vars, num_time_steps, |
| 1320 |
num_elem_in_block, "e", use_cell_arrays); |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* nodeset variables */ |
| 1324 |
if (num_nodeset_vars > 0) { |
| 1325 |
if (debug) { |
| 1326 |
logger("Nodeset Variables"); |
| 1327 |
} |
| 1328 |
get_put_vars(exo_file, EX_NODE_SET, num_node_sets, num_nodeset_vars, num_time_steps, |
| 1329 |
num_nodeset_nodes, "ns", use_cell_arrays); |
| 1330 |
} |
| 1331 |
|
| 1332 |
/* sideset variables */ |
| 1333 |
if (num_sideset_vars > 0) { |
| 1334 |
if (debug) { |
| 1335 |
logger("Sideset Variables"); |
| 1336 |
} |
| 1337 |
get_put_vars(exo_file, EX_SIDE_SET, num_side_sets, num_sideset_vars, num_time_steps, |
| 1338 |
num_sideset_sides, "ss", use_cell_arrays); |
| 1339 |
} |
| 1340 |
|
| 1341 |
/* node and element number maps */ |
| 1342 |
if (debug) { |
| 1343 |
logger("Node and Element Number Maps"); |
| 1344 |
} |
| 1345 |
ex_opts(0); /* turn off error reporting. It is not an error to have no map*/ |
| 1346 |
std::vector<int> ids(num_nodes); |
| 1347 |
err = ex_get_id_map(exo_file, EX_NODE_MAP, Data(ids)); |
| 1348 |
if (err == 0) { |
| 1349 |
PutInt("node_num_map", num_nodes, 1, Data(ids)); |
| 1350 |
} |
| 1351 |
|
| 1352 |
ids.resize(num_elements); |
| 1353 |
err = ex_get_id_map(exo_file, EX_ELEM_MAP, Data(ids)); |
| 1354 |
if (err == 0) { |
| 1355 |
PutInt("elem_num_map", num_elements, 1, Data(ids)); |
| 1356 |
} |
| 1357 |
|
| 1358 |
if (debug) { |
| 1359 |
logger("Closing file"); |
| 1360 |
} |
| 1361 |
ex_close(exo_file); |
| 1362 |
|
| 1363 |
if (textfile != 0) { |
| 1364 |
fclose(m_file); |
| 1365 |
} |
| 1366 |
else { |
| 1367 |
Mat_Close(mat_file); |
| 1368 |
} |
| 1369 |
|
| 1370 |
delete_exodus_names(str2, nstr2); |
| 1371 |
|
| 1372 |
fmt::print("done...\n"); |
| 1373 |
|
| 1374 |
/* exit status */ |
| 1375 |
add_to_log("exo2mat", 0); |
| 1376 |
return (0); |
| 1377 |
} |