| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020 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 <math.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
|
| 13 |
#include "exodusII.h" |
| 14 |
|
| 15 |
#define EXCHECK(funcall) \ |
| 16 |
do { \ |
| 17 |
if ((error = (funcall)) != NC_NOERR) { \ |
| 18 |
fprintf(stderr, "ERROR Calling " #funcall ", error = %d\n", error); \ |
| 19 |
ex_close(exoid); \ |
| 20 |
exit(-1); \ |
| 21 |
} \ |
| 22 |
} while (0) |
| 23 |
|
| 24 |
int main(int argc, char **argv) |
| 25 |
{ |
| 26 |
int error; |
| 27 |
int exoid, num_dim, num_nodes, num_elem, num_elem_blk; |
| 28 |
int num_elem_in_block[10], num_nodes_per_elem[10]; |
| 29 |
int num_nodes_in_nset[10]; |
| 30 |
int num_node_sets, num_side_sets; |
| 31 |
int i, j, k, m, *elem_map, *connect; |
| 32 |
int node_list[100]; |
| 33 |
int ebids[10], nsids[10]; |
| 34 |
int num_qa_rec, num_info; |
| 35 |
int num_nod_vars, num_ele_vars, num_nset_vars; |
| 36 |
int *truth_tab; |
| 37 |
int whole_time_step, num_time_steps; |
| 38 |
int CPU_word_size, IO_word_size; |
| 39 |
int prop_array[2]; |
| 40 |
|
| 41 |
int elem_list[1], side_list[1]; |
| 42 |
float *nodal_var_vals, *elem_var_vals; |
| 43 |
float *nset_var_vals; |
| 44 |
float time_value; |
| 45 |
float x[100]; |
| 46 |
float attrib[10], dist_fact[100]; |
| 47 |
char *coord_names[3], *qa_record[2][4], *info[3], *var_names[3]; |
| 48 |
char *block_names[10], *set_names[10]; |
| 49 |
char *prop_names[2], *attrib_names[2]; |
| 50 |
char *title = "This is a test"; |
| 51 |
ex_opts(EX_VERBOSE | EX_ABORT); |
| 52 |
|
| 53 |
/* Specify compute and i/o word size */ |
| 54 |
|
| 55 |
CPU_word_size = 0; /* sizeof(float) */ |
| 56 |
IO_word_size = 4; /* (4 bytes) */ |
| 57 |
|
| 58 |
/* create EXODUS file */ |
| 59 |
exoid = ex_create("test.exo", /* filename path */ |
| 60 |
EX_CLOBBER, /* create mode */ |
| 61 |
&CPU_word_size, /* CPU float word size in bytes */ |
| 62 |
&IO_word_size); /* I/O float word size in bytes */ |
| 63 |
printf("after ex_create for oned.e, exoid = %d\n", exoid); |
| 64 |
printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size); |
| 65 |
|
| 66 |
EXCHECK(ex_set_max_name_length(exoid, 40)); |
| 67 |
|
| 68 |
/* initialize file with parameters */ |
| 69 |
|
| 70 |
num_dim = 1; |
| 71 |
num_nodes = 10; |
| 72 |
num_elem = 10; /* 9 lines plus a point */ |
| 73 |
num_elem_blk = 3; |
| 74 |
num_node_sets = 2; |
| 75 |
num_side_sets = 2; |
| 76 |
|
| 77 |
EXCHECK(ex_put_init(exoid, title, num_dim, num_nodes, num_elem, num_elem_blk, num_node_sets, |
| 78 |
num_side_sets)); |
| 79 |
|
| 80 |
for (i = 0; i < num_nodes; i++) { |
| 81 |
x[i] = exp((float)i / 10.0f); |
| 82 |
} |
| 83 |
|
| 84 |
EXCHECK(ex_put_coord(exoid, x, NULL, NULL)); |
| 85 |
|
| 86 |
coord_names[0] = "xcoor"; |
| 87 |
EXCHECK(ex_put_coord_names(exoid, coord_names)); |
| 88 |
|
| 89 |
/* Add nodal attributes */ |
| 90 |
EXCHECK(ex_put_attr_param(exoid, EX_NODAL, 0, 1)); |
| 91 |
|
| 92 |
EXCHECK(ex_put_one_attr(exoid, EX_NODAL, 0, 1, x)); |
| 93 |
|
| 94 |
attrib_names[0] = "Node_attr_1"; |
| 95 |
EXCHECK(ex_put_attr_names(exoid, EX_NODAL, 0, attrib_names)); |
| 96 |
|
| 97 |
/* write element order map */ |
| 98 |
elem_map = (int *)calloc(num_elem, sizeof(int)); |
| 99 |
|
| 100 |
for (i = 1; i <= num_elem; i++) { |
| 101 |
elem_map[i - 1] = 10 * i; |
| 102 |
} |
| 103 |
|
| 104 |
EXCHECK(ex_put_id_map(exoid, EX_ELEM_MAP, elem_map)); |
| 105 |
free(elem_map); |
| 106 |
|
| 107 |
/* write element block parameters */ |
| 108 |
block_names[0] = "left_side"; |
| 109 |
block_names[1] = "right_side"; |
| 110 |
block_names[2] = "center"; |
| 111 |
|
| 112 |
num_elem_in_block[0] = 4; |
| 113 |
num_elem_in_block[1] = 5; |
| 114 |
num_elem_in_block[2] = 1; |
| 115 |
|
| 116 |
num_nodes_per_elem[0] = 2; |
| 117 |
num_nodes_per_elem[1] = 2; |
| 118 |
num_nodes_per_elem[2] = 1; |
| 119 |
|
| 120 |
ebids[0] = 10; |
| 121 |
ebids[1] = 20; |
| 122 |
ebids[2] = 30; |
| 123 |
|
| 124 |
EXCHECK(ex_put_block(exoid, EX_ELEM_BLOCK, ebids[0], "bar", num_elem_in_block[0], |
| 125 |
num_nodes_per_elem[0], 0, 0, 1)); |
| 126 |
EXCHECK(ex_put_block(exoid, EX_ELEM_BLOCK, ebids[1], "bar", num_elem_in_block[1], |
| 127 |
num_nodes_per_elem[1], 0, 0, 1)); |
| 128 |
EXCHECK(ex_put_block(exoid, EX_ELEM_BLOCK, ebids[2], "point", num_elem_in_block[2], |
| 129 |
num_nodes_per_elem[2], 0, 0, 0)); |
| 130 |
|
| 131 |
/* Write element block names */ |
| 132 |
EXCHECK(ex_put_names(exoid, EX_ELEM_BLOCK, block_names)); |
| 133 |
|
| 134 |
/* write element block properties */ |
| 135 |
prop_names[0] = "DENSITY"; |
| 136 |
EXCHECK(ex_put_prop_names(exoid, EX_ELEM_BLOCK, 1, prop_names)); |
| 137 |
EXCHECK(ex_put_prop(exoid, EX_ELEM_BLOCK, ebids[0], prop_names[0], 1)); |
| 138 |
EXCHECK(ex_put_prop(exoid, EX_ELEM_BLOCK, ebids[1], prop_names[0], 10)); |
| 139 |
EXCHECK(ex_put_prop(exoid, EX_ELEM_BLOCK, ebids[2], prop_names[0], 34)); |
| 140 |
|
| 141 |
/* write element connectivity */ |
| 142 |
connect = (int *)calloc(20, sizeof(int)); |
| 143 |
for (i = 0; i < num_elem * 2; i += 2) { |
| 144 |
connect[i] = i / 2 + 1; |
| 145 |
connect[i + 1] = i / 2 + 2; |
| 146 |
} |
| 147 |
|
| 148 |
EXCHECK(ex_put_conn(exoid, EX_ELEM_BLOCK, ebids[0], connect, NULL, NULL)); |
| 149 |
EXCHECK(ex_put_conn(exoid, EX_ELEM_BLOCK, ebids[1], connect + 8, NULL, NULL)); |
| 150 |
|
| 151 |
/* Circle */ |
| 152 |
connect[0] = 5; |
| 153 |
EXCHECK(ex_put_conn(exoid, EX_ELEM_BLOCK, ebids[2], connect, NULL, NULL)); |
| 154 |
free(connect); |
| 155 |
|
| 156 |
/* write element block attributes */ |
| 157 |
for (i = 0; i < num_elem; i++) { |
| 158 |
attrib[i] = 3.14159 * i; |
| 159 |
} |
| 160 |
EXCHECK(ex_put_attr(exoid, EX_ELEM_BLOCK, ebids[0], attrib)); |
| 161 |
EXCHECK(ex_put_attr(exoid, EX_ELEM_BLOCK, ebids[1], attrib + num_elem_in_block[0])); |
| 162 |
|
| 163 |
attrib_names[0] = "THICKNESS"; |
| 164 |
EXCHECK(ex_put_attr_names(exoid, EX_ELEM_BLOCK, ebids[0], attrib_names)); |
| 165 |
attrib_names[0] = "WIDTH"; |
| 166 |
EXCHECK(ex_put_attr_names(exoid, EX_ELEM_BLOCK, ebids[1], attrib_names)); |
| 167 |
|
| 168 |
/* write individual node sets */ |
| 169 |
num_nodes_in_nset[0] = 5; |
| 170 |
num_nodes_in_nset[1] = 3; |
| 171 |
|
| 172 |
nsids[0] = 20; |
| 173 |
nsids[1] = 21; |
| 174 |
|
| 175 |
EXCHECK(ex_put_set_param(exoid, EX_NODE_SET, nsids[0], 5, 5)); |
| 176 |
|
| 177 |
node_list[0] = 1; |
| 178 |
node_list[1] = 3; |
| 179 |
node_list[2] = 5; |
| 180 |
node_list[3] = 7; |
| 181 |
node_list[4] = 9; |
| 182 |
|
| 183 |
dist_fact[0] = 1.0; |
| 184 |
dist_fact[1] = 2.0; |
| 185 |
dist_fact[2] = 3.0; |
| 186 |
dist_fact[3] = 4.0; |
| 187 |
dist_fact[4] = 5.0; |
| 188 |
|
| 189 |
EXCHECK(ex_put_set(exoid, EX_NODE_SET, nsids[0], node_list, NULL)); |
| 190 |
EXCHECK(ex_put_set_dist_fact(exoid, EX_NODE_SET, nsids[0], dist_fact)); |
| 191 |
|
| 192 |
EXCHECK(ex_put_set_param(exoid, EX_NODE_SET, nsids[1], 3, 3)); |
| 193 |
|
| 194 |
node_list[0] = 2; |
| 195 |
node_list[1] = 4; |
| 196 |
node_list[2] = 6; |
| 197 |
|
| 198 |
dist_fact[0] = 1.0; |
| 199 |
dist_fact[1] = 2.0; |
| 200 |
dist_fact[2] = 3.0; |
| 201 |
|
| 202 |
EXCHECK(ex_put_set(exoid, EX_NODE_SET, nsids[1], node_list, NULL)); |
| 203 |
EXCHECK(ex_put_set_dist_fact(exoid, EX_NODE_SET, nsids[1], dist_fact)); |
| 204 |
|
| 205 |
/* Write node set names */ |
| 206 |
set_names[0] = "all_odd_nodes"; |
| 207 |
set_names[1] = "some_even_nodes"; |
| 208 |
|
| 209 |
EXCHECK(ex_put_names(exoid, EX_NODE_SET, set_names)); |
| 210 |
EXCHECK(ex_put_prop(exoid, EX_NODE_SET, nsids[0], "FACE", 4)); |
| 211 |
|
| 212 |
EXCHECK(ex_put_prop(exoid, EX_NODE_SET, nsids[1], "FACE", 5)); |
| 213 |
|
| 214 |
prop_array[0] = 1000; |
| 215 |
prop_array[1] = 2000; |
| 216 |
|
| 217 |
EXCHECK(ex_put_prop_array(exoid, EX_NODE_SET, "VELOCITY", prop_array)); |
| 218 |
/* Add nodeset attributes */ |
| 219 |
EXCHECK(ex_put_attr_param(exoid, EX_NODE_SET, nsids[0], 1)); |
| 220 |
|
| 221 |
EXCHECK(ex_put_attr(exoid, EX_NODE_SET, nsids[0], x)); |
| 222 |
|
| 223 |
attrib_names[0] = "Nodeset_attribute"; |
| 224 |
EXCHECK(ex_put_attr_names(exoid, EX_NODE_SET, nsids[0], attrib_names)); |
| 225 |
|
| 226 |
EXCHECK(ex_put_set_param(exoid, EX_SIDE_SET, 1, 1, 1)); |
| 227 |
EXCHECK(ex_put_set_param(exoid, EX_SIDE_SET, 2, 1, 1)); |
| 228 |
|
| 229 |
elem_list[0] = 1; |
| 230 |
side_list[0] = 1; |
| 231 |
dist_fact[0] = 2.0; |
| 232 |
EXCHECK(ex_put_set(exoid, EX_SIDE_SET, 1, elem_list, side_list)); |
| 233 |
EXCHECK(ex_put_set_dist_fact(exoid, EX_SIDE_SET, 1, dist_fact)); |
| 234 |
|
| 235 |
elem_list[0] = 9; |
| 236 |
side_list[0] = 2; |
| 237 |
dist_fact[0] = 3.0; |
| 238 |
EXCHECK(ex_put_set(exoid, EX_SIDE_SET, 2, elem_list, side_list)); |
| 239 |
EXCHECK(ex_put_set_dist_fact(exoid, EX_SIDE_SET, 2, dist_fact)); |
| 240 |
|
| 241 |
/* Write side set names */ |
| 242 |
set_names[0] = "left_boundary"; |
| 243 |
set_names[1] = "right_boundary"; |
| 244 |
|
| 245 |
EXCHECK(ex_put_names(exoid, EX_SIDE_SET, set_names)); |
| 246 |
/* write QA records; test empty and just blank-filled records */ |
| 247 |
num_qa_rec = 2; |
| 248 |
|
| 249 |
qa_record[0][0] = "TESTWT"; |
| 250 |
qa_record[0][1] = "testwt"; |
| 251 |
qa_record[0][2] = "07/07/93"; |
| 252 |
qa_record[0][3] = "15:41:33"; |
| 253 |
qa_record[1][0] = ""; |
| 254 |
qa_record[1][1] = " "; |
| 255 |
qa_record[1][2] = ""; |
| 256 |
qa_record[1][3] = " "; |
| 257 |
|
| 258 |
EXCHECK(ex_put_qa(exoid, num_qa_rec, qa_record)); |
| 259 |
|
| 260 |
/* write information records; test empty and just blank-filled records */ |
| 261 |
num_info = 3; |
| 262 |
|
| 263 |
info[0] = "This is the first information record."; |
| 264 |
info[1] = ""; |
| 265 |
info[2] = " "; |
| 266 |
|
| 267 |
EXCHECK(ex_put_info(exoid, num_info, info)); |
| 268 |
|
| 269 |
/* write results variables parameters and names */ |
| 270 |
num_nod_vars = 2; |
| 271 |
/* 12345678901234567890123456789012 */ |
| 272 |
var_names[0] = "node_variable_a_very_long_name_0"; |
| 273 |
var_names[1] = "nod_var1"; |
| 274 |
|
| 275 |
EXCHECK(ex_put_variable_param(exoid, EX_NODAL, num_nod_vars)); |
| 276 |
EXCHECK(ex_put_variable_names(exoid, EX_NODAL, num_nod_vars, var_names)); |
| 277 |
|
| 278 |
num_ele_vars = 3; |
| 279 |
/* 0 1 2 3 */ |
| 280 |
/* 12345678901234567890123456789012 */ |
| 281 |
var_names[0] = "this_variable_name_is_short"; |
| 282 |
var_names[1] = "this_variable_name_is_just_right"; |
| 283 |
var_names[2] = "this_variable_name_is_tooooo_long"; |
| 284 |
|
| 285 |
EXCHECK(ex_put_variable_param(exoid, EX_ELEM_BLOCK, num_ele_vars)); |
| 286 |
EXCHECK(ex_put_variable_names(exoid, EX_ELEM_BLOCK, num_ele_vars, var_names)); |
| 287 |
|
| 288 |
num_nset_vars = 3; |
| 289 |
|
| 290 |
var_names[0] = "ns_var0"; |
| 291 |
var_names[1] = "ns_var1"; |
| 292 |
var_names[2] = "ns_var2"; |
| 293 |
|
| 294 |
EXCHECK(ex_put_variable_param(exoid, EX_NODE_SET, num_nset_vars)); |
| 295 |
EXCHECK(ex_put_variable_names(exoid, EX_NODE_SET, num_nset_vars, var_names)); |
| 296 |
|
| 297 |
/* write element variable truth table */ |
| 298 |
truth_tab = (int *)calloc((num_elem_blk * num_ele_vars), sizeof(int)); |
| 299 |
|
| 300 |
k = 0; |
| 301 |
for (i = 0; i < num_elem_blk; i++) { |
| 302 |
for (j = 0; j < num_ele_vars; j++) { |
| 303 |
truth_tab[k++] = 1; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
EXCHECK(ex_put_truth_table(exoid, EX_ELEM_BLOCK, num_elem_blk, num_ele_vars, truth_tab)); |
| 308 |
|
| 309 |
free(truth_tab); |
| 310 |
|
| 311 |
/* for each time step, write the analysis results; |
| 312 |
* the code below fills the arrays glob_var_vals, |
| 313 |
* nodal_var_vals, and elem_var_vals with values for debugging purposes; |
| 314 |
*/ |
| 315 |
|
| 316 |
whole_time_step = 1; |
| 317 |
num_time_steps = 10; |
| 318 |
|
| 319 |
nodal_var_vals = (float *)calloc(num_nodes, CPU_word_size); |
| 320 |
elem_var_vals = (float *)calloc(num_elem, CPU_word_size); |
| 321 |
nset_var_vals = (float *)calloc(10, CPU_word_size); |
| 322 |
|
| 323 |
for (i = 0; i < num_time_steps; i++) { |
| 324 |
time_value = (float)(i + 1) / 100.0f; |
| 325 |
|
| 326 |
/* write time value */ |
| 327 |
EXCHECK(ex_put_time(exoid, whole_time_step, &time_value)); |
| 328 |
|
| 329 |
/* write nodal variables */ |
| 330 |
for (k = 1; k <= num_nod_vars; k++) { |
| 331 |
for (j = 0; j < num_nodes; j++) { |
| 332 |
nodal_var_vals[j] = (float)k + ((float)(j + 1) * time_value); |
| 333 |
} |
| 334 |
EXCHECK(ex_put_var(exoid, whole_time_step, EX_NODAL, k, 1, num_nodes, nodal_var_vals)); |
| 335 |
} |
| 336 |
|
| 337 |
/* write element variables */ |
| 338 |
for (k = 1; k <= num_ele_vars; k++) { |
| 339 |
for (j = 0; j < num_elem_blk; j++) { |
| 340 |
for (m = 0; m < num_elem_in_block[j]; m++) { |
| 341 |
elem_var_vals[m] = (float)(k + 1) + (float)(j + 2) + ((float)(m + 1) * time_value); |
| 342 |
} |
| 343 |
EXCHECK(ex_put_var(exoid, whole_time_step, EX_ELEM_BLOCK, k, ebids[j], num_elem_in_block[j], |
| 344 |
elem_var_vals)); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/* write nodeset variables */ |
| 349 |
for (k = 1; k <= num_nset_vars; k++) { |
| 350 |
for (j = 0; j < num_node_sets; j++) { |
| 351 |
for (m = 0; m < num_nodes_in_nset[j]; m++) { |
| 352 |
nset_var_vals[m] = (float)(k + 3) + (float)(j + 4) + ((float)(m + 1) * time_value); |
| 353 |
} |
| 354 |
EXCHECK(ex_put_var(exoid, whole_time_step, EX_NODE_SET, k, nsids[j], num_nodes_in_nset[j], |
| 355 |
nset_var_vals)); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
whole_time_step++; |
| 360 |
|
| 361 |
/* update the data file; this should be done at the end of every time step |
| 362 |
* to ensure that no data is lost if the analysis dies |
| 363 |
*/ |
| 364 |
EXCHECK(ex_update(exoid)); |
| 365 |
} |
| 366 |
|
| 367 |
free(nodal_var_vals); |
| 368 |
free(elem_var_vals); |
| 369 |
free(nset_var_vals); |
| 370 |
|
| 371 |
/* close the EXODUS files */ |
| 372 |
EXCHECK(ex_close(exoid)); |
| 373 |
return 0; |
| 374 |
} |