Debian Codegraph @

packages/seacas/libraries/exodus/test/testrd-assembly.c

raw
1 /*
2 * Copyright(C) 1999-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 *
10 * testrd - read exodus file test.exo created by testwt
11 *
12 * author - Sandia National Laboratories
13 * Larry A. Schoof - Original
14 *
15 *
16 * environment - UNIX
17 *
18 * entry conditions -
19 * input parameters:
20 * int exoid exodus file id
21 *
22 * exit conditions -
23 *
24 * revision history -
25 *
26 *
27 *****************************************************************************/
28
29 #include "exodusII.h"
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #define STRINGIFY(x) #x
36 #define TOSTRING(x) STRINGIFY(x)
37
38 #define EXCHECK(funcall) \
39 do { \
40 int error = (funcall); \
41 printf("after %s, error = %d\n", TOSTRING(funcall), error); \
42 if (error != EX_NOERR && error != EX_WARN) { \
43 fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \
44 ex_close(exoid); \
45 exit(-1); \
46 } \
47 } while (0)
48
49 int main(int argc, char **argv)
50 {
51 int num_elem_blk, num_assembly;
52
53 ex_opts(EX_VERBOSE | EX_ABORT);
54
55 /* open EXODUS II files */
56 int CPU_word_size = 0; /* sizeof(float) */
57 int IO_word_size = 0; /* use what is stored in file */
58 float version;
59 int exoid = ex_open("test-assembly.exo", /* filename path */
60 EX_READ, /* access mode = READ */
61 &CPU_word_size, /* CPU word size */
62 &IO_word_size, /* IO word size */
63 &version); /* ExodusII library version */
64
65 printf("\nafter ex_open\n");
66 if (exoid < 0) {
67 exit(1);
68 }
69
70 printf("test-assembly.exo is an EXODUSII file; version %4.2f\n", version);
71 /* printf (" CPU word size %1d\n",CPU_word_size); */
72 printf(" I/O word size %1d\n", IO_word_size);
73 char *cdum = NULL;
74 int idum;
75 ex_inquire(exoid, EX_INQ_API_VERS, &idum, &version, cdum);
76 printf("EXODUSII API; version %4.2f\n", version);
77
78 ex_inquire(exoid, EX_INQ_LIB_VERS, &idum, &version, cdum);
79 printf("EXODUSII Library API; version %4.2f (%d)\n", version, idum);
80
81 /* read database parameters */
82 {
83 ex_init_params par;
84 EXCHECK(ex_get_init_ext(exoid, &par));
85
86 printf("database parameters:\n");
87 printf("title = '%s'\n", par.title);
88 printf("num_dim = %" PRId64 "\n", par.num_dim);
89 printf("num_assembly = %" PRId64 "\n", par.num_assembly);
90 printf("num_nodes = %" PRId64 "\n", par.num_nodes);
91 printf("num_edge = %" PRId64 "\n", par.num_edge);
92 printf("num_face = %" PRId64 "\n", par.num_face);
93 printf("num_elem = %" PRId64 "\n", par.num_elem);
94 printf("num_elem_blk = %" PRId64 "\n", par.num_elem_blk);
95 printf("num_node_sets = %" PRId64 "\n", par.num_node_sets);
96 printf("num_side_sets = %" PRId64 "\n", par.num_side_sets);
97
98 /* Check that ex_inquire gives same title */
99 char title_chk[MAX_LINE_LENGTH + 1];
100 float fdum;
101 EXCHECK(ex_inquire(exoid, EX_INQ_TITLE, &idum, &fdum, title_chk));
102 if (strcmp(par.title, title_chk) != 0) {
103 printf("error in ex_inquire for EX_INQ_TITLE %s, vs %s\n", par.title, title_chk);
104 }
105 num_elem_blk = par.num_elem_blk;
106 num_assembly = par.num_assembly;
107 }
108
109 /* read element block parameters */
110 int *ids = NULL;
111 int *num_elem_in_block = NULL;
112 int *num_nodes_per_elem = NULL;
113 int *num_attr = NULL;
114 if (num_elem_blk > 0) {
115 ids = (int *)calloc(num_elem_blk, sizeof(int));
116 num_elem_in_block = (int *)calloc(num_elem_blk, sizeof(int));
117 num_nodes_per_elem = (int *)calloc(num_elem_blk, sizeof(int));
118 num_attr = (int *)calloc(num_elem_blk, sizeof(int));
119
120 EXCHECK(ex_get_ids(exoid, EX_ELEM_BLOCK, ids));
121
122 char *block_names[10];
123 for (int i = 0; i < num_elem_blk; i++) {
124 block_names[i] = (char *)calloc((MAX_STR_LENGTH + 1), sizeof(char));
125 }
126
127 EXCHECK(ex_get_names(exoid, EX_ELEM_BLOCK, block_names));
128
129 for (int i = 0; i < num_elem_blk; i++) {
130 char name[MAX_STR_LENGTH + 1];
131 EXCHECK(ex_get_name(exoid, EX_ELEM_BLOCK, ids[i], name));
132 if (strcmp(name, block_names[i]) != 0) {
133 printf("error in ex_get_name for block id %d\n", ids[i]);
134 }
135 char elem_type[MAX_STR_LENGTH + 1];
136 EXCHECK(ex_get_block(exoid, EX_ELEM_BLOCK, ids[i], elem_type, &(num_elem_in_block[i]),
137 &(num_nodes_per_elem[i]), 0, 0, &(num_attr[i])));
138 printf("element block id = %2d\n", ids[i]);
139 printf("element type = '%s'\n", elem_type);
140 printf("num_elem_in_block = %2d\n", num_elem_in_block[i]);
141 printf("num_nodes_per_elem = %2d\n", num_nodes_per_elem[i]);
142 printf("num_attr = %2d\n", num_attr[i]);
143 printf("name = '%s'\n", block_names[i]);
144 }
145
146 free(ids);
147 free(num_elem_in_block);
148 free(num_nodes_per_elem);
149 free(num_attr);
150 for (int i = 0; i < num_elem_blk; i++) {
151 free(block_names[i]);
152 }
153 }
154
155 /* Read assembly information */
156 /* Verify ex_inquire_int gives same value for assembly count... */
157 int chk_num_assembly = ex_inquire_int(exoid, EX_INQ_ASSEMBLY);
158 if (chk_num_assembly != num_assembly) {
159 printf("error in ex_inquire_int for EX_INQ_ASSEMBLY: %d vs %d\n", chk_num_assembly,
160 num_assembly);
161 }
162 char *assembly_names[10];
163 if (num_assembly > 0) {
164 int assembly_ids[10];
165 ex_get_ids(exoid, EX_ASSEMBLY, assembly_ids);
166
167 char *assembly_name2[10];
168 for (int i = 0; i < num_assembly; i++) {
169 assembly_names[i] = (char *)calloc((MAX_STR_LENGTH + 1), sizeof(char));
170 assembly_name2[i] = (char *)calloc((MAX_STR_LENGTH + 1), sizeof(char));
171 }
172
173 /* Verify that we can get the names via the `ex_get_names` function... */
174 EXCHECK(ex_get_names(exoid, EX_ASSEMBLY, assembly_name2));
175
176 ex_assembly assemblies[10];
177 int64_t entity[10];
178 for (int i = 0; i < num_assembly; i++) {
179 assemblies[i].id = assembly_ids[i];
180 assemblies[i].name = assembly_names[i];
181 /* Clear out name to make sure still getting same name */
182 assemblies[i].name[0] = '\0';
183
184 assemblies[i].entity_list = (int64_t *)&entity;
185 EXCHECK(ex_get_assembly(exoid, &assemblies[i]));
186 printf("Assembly named '%s' has id %" PRId64 ". It contains %d entities of type '%s'\n\t",
187 assemblies[i].name, assemblies[i].id, assemblies[i].entity_count,
188 ex_name_of_object(assemblies[i].type));
189 for (int j = 0; j < assemblies[i].entity_count; j++) {
190 printf("%" PRId64 ", ", entity[j]);
191 }
192 if (strcmp(assembly_name2[i], assemblies[i].name) != 0) {
193 printf("error in ex_get_names for EX_ASSEMBLY\n");
194 }
195 printf("\n");
196 }
197
198 ex_assembly assmbly[10];
199 for (int i = 0; i < num_assembly; i++) {
200 assmbly[i].name = NULL;
201 assmbly[i].name = assembly_names[i];
202 /* Clear out name to make sure still getting same name */
203 assmbly[i].name[0] = '\0';
204 assmbly[i].entity_list = NULL;
205 }
206 EXCHECK(ex_get_assemblies(exoid, assmbly));
207 for (int i = 0; i < num_assembly; i++) {
208 printf("Assembly named '%s' has id %" PRId64 ". It contains %d entities of type '%s'\n",
209 assmbly[i].name, assmbly[i].id, assmbly[i].entity_count,
210 ex_name_of_object(assmbly[i].type));
211 if (strcmp(assembly_name2[i], assemblies[i].name) != 0) {
212 printf("error in ex_get_names for EX_ASSEMBLY\n");
213 }
214 }
215
216 for (int i = 0; i < num_assembly; i++) {
217 free(assembly_name2[i]);
218 }
219
220 /* Read attributes... */
221 ex_attribute attr[10];
222
223 for (int i = 0; i < num_assembly; i++) {
224 memset(attr, 0, sizeof(ex_attribute) * 10);
225 int att_count = ex_get_attribute_count(exoid, EX_ASSEMBLY, assmbly[i].id);
226 printf("Assembly named '%s' with id %" PRId64 ". It contains %d attributes:\n",
227 assmbly[i].name, assmbly[i].id, att_count);
228
229 ex_get_attribute_param(exoid, EX_ASSEMBLY, assmbly[i].id, attr);
230 ex_get_attributes(exoid, att_count, attr);
231
232 for (int j = 0; j < att_count; j++) {
233 printf("\tName: '%s', Type = %d, Value Count = %d\n\t", attr[j].name, attr[j].type,
234 (int)attr[j].value_count);
235 for (int k = 0; k < attr[j].value_count; k++) {
236 if (attr[j].type == EX_INTEGER) {
237 int *vals = attr[j].values;
238 printf("\t%d", vals[k]);
239 }
240 else if (attr[j].type == EX_DOUBLE) {
241 double *vals = attr[j].values;
242 printf("\t%g", vals[k]);
243 }
244 else if (attr[j].type == EX_CHAR) {
245 char *vals = attr[j].values;
246 if (vals[k] != '\0') {
247 printf("\t%c", vals[k]);
248 }
249 }
250 }
251 printf("\n");
252 free(attr[j].values);
253 attr[j].values = NULL;
254 }
255 printf("\n");
256 }
257
258 { /* Global attributes (includes exodus-internal attributes) */
259 int att_count = ex_get_attribute_count(exoid, EX_GLOBAL, 0);
260 printf("GLOBAL contains %d attributes:\n", att_count);
261 for (int j = 0; j < att_count; j++) {
262 ex_get_attribute_param(exoid, EX_GLOBAL, 0, attr);
263 printf("\tName: '%s', Type = %d, Value Count = %d\n", attr[j].name, attr[j].type,
264 (int)attr[j].value_count);
265 }
266 }
267
268 int num_assembly_vars;
269 EXCHECK(ex_get_reduction_variable_param(exoid, EX_ASSEMBLY, &num_assembly_vars));
270
271 if (num_assembly_vars > 0) {
272 char *var_names[10];
273 for (int i = 0; i < num_assembly_vars; i++) {
274 var_names[i] = (char *)calloc((MAX_STR_LENGTH + 1), sizeof(char));
275 }
276
277 EXCHECK(ex_get_reduction_variable_names(exoid, EX_ASSEMBLY, num_assembly_vars, var_names));
278
279 printf("There are %2d assembly reduction variables; their names are :\n", num_assembly_vars);
280 for (int i = 0; i < num_assembly_vars; i++) {
281 printf(" '%s'\n", var_names[i]);
282 free(var_names[i]);
283 }
284
285 /* determine how many time steps are stored */
286 int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME);
287 printf("There are %2d time steps in the database.\n", num_time_steps);
288
289 float *var_values = (float *)calloc(num_assembly_vars, sizeof(float));
290 for (int i = 0; i < num_time_steps; i++) {
291 float time_value;
292 EXCHECK(ex_get_time(exoid, i + 1, &time_value));
293 printf("Time at step %d is %f.\n", i + 1, time_value);
294
295 for (int k = 0; k < num_assembly; k++) {
296 EXCHECK(ex_get_reduction_vars(exoid, i + 1, EX_ASSEMBLY, assmbly[k].id, num_assembly_vars,
297 var_values));
298 printf("Values for Assembly %" PRId64 " at step %d:", assmbly[k].id, i + 1);
299 for (int kk = 0; kk < num_assembly_vars; kk++) {
300 printf("\t%f", var_values[kk]);
301 }
302 printf("\n");
303 }
304 }
305 free(var_values);
306 }
307 }
308 for (int i = 0; i < num_assembly; i++) {
309 free(assembly_names[i]);
310 }
311 EXCHECK(ex_close(exoid));
312 return 0;
313 }