Debian Codegraph @

packages/seacas/libraries/exodus/test/testwt-field-metadata.c

raw
1 /*
2 * Copyright(C) 2022, 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 #undef NDEBUG
10 #include <assert.h>
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "exodusII.h"
17
18 #define STRINGIFY(x) #x
19 #define TOSTRING(x) STRINGIFY(x)
20
21 #define EXCHECK(funcall) \
22 do { \
23 int error = (funcall); \
24 printf("after %s, error = %d\n", TOSTRING(funcall), error); \
25 if (error != EX_NOERR) { \
26 fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \
27 ex_close(exoid); \
28 exit(-1); \
29 } \
30 } while (0)
31
32 int main(int argc, char **argv)
33 {
34 ex_opts(EX_VERBOSE);
35
36 /* Specify compute and i/o word size */
37 int CPU_word_size = 8;
38 int IO_word_size = 8;
39
40 /* create EXODUS II file */
41 int exoid = ex_create("test-field-metadata.exo", /* filename path */
42 EX_CLOBBER, /* create mode */
43 &CPU_word_size, /* CPU double word size in bytes */
44 &IO_word_size); /* I/O double word size in bytes */
45 printf("after ex_create for test.exo, exoid = %d\n", exoid);
46 printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size);
47
48 int num_elem_blk = 3;
49 {
50 ex_init_params par = {.num_dim = 3,
51 .num_nodes = 1,
52 .num_elem = 3,
53 .num_elem_blk = num_elem_blk,
54 .num_node_sets = 0,
55 .num_side_sets = 0,
56 .num_assembly = 0};
57
58 char *title = "This is a test";
59 ex_copy_string(par.title, title, MAX_LINE_LENGTH + 1);
60 EXCHECK(ex_put_init_ext(exoid, &par));
61 }
62
63 double coord[] = {0.0};
64 EXCHECK(ex_put_coord(exoid, coord, coord, coord));
65
66 /* ======================================================================== */
67 /* write element block parameters */
68 struct ex_block blocks[num_elem_blk];
69 for (int i = 0; i < num_elem_blk; i++) {
70 blocks[i] = (ex_block){.type = EX_ELEM_BLOCK, .num_entry = 1, .id = i + 10};
71 ex_copy_string(blocks[i].topology, "sphere", MAX_STR_LENGTH + 1);
72 blocks[i].num_nodes_per_entry = 1;
73 }
74
75 EXCHECK(ex_put_block_params(exoid, num_elem_blk, blocks));
76
77 int connect[] = {1};
78 for (int i = 0; i < num_elem_blk; i++) {
79 EXCHECK(ex_put_conn(exoid, EX_ELEM_BLOCK, blocks[i].id, connect, NULL, NULL));
80 }
81
82 /* Write element block names */
83 for (int i = 0; i < num_elem_blk; i++) {
84 char block_names[32];
85 snprintf(block_names, 32, "block_%c", i + 'A');
86 EXCHECK(ex_put_name(exoid, EX_ELEM_BLOCK, blocks[i].id, block_names));
87 }
88
89 {
90 int units[] = {1, 0, 0, -1};
91
92 EXCHECK(ex_put_integer_attribute(exoid, EX_ELEM_BLOCK, 11, "Units", 4, units));
93 EXCHECK(ex_put_text_attribute(exoid, EX_GLOBAL, 0, "SOLID_MODEL", "STEP-X-43-1547836-Rev 0"));
94 }
95
96 /* ======================================================================== */
97 /* Transient Variables */
98 char *var_names[] = {
99 "DispX", "DispY", "DispZ", "Velocity%X", "Velocity%Y",
100 "Velocity%Z", "Gradient-X$0", "Gradient-Y$0", "Gradient-Z$0", "Gradient-X$1",
101 "Gradient-Y$1", "Gradient-Z$1", "Gradient-X$2", "Gradient-Y$2", "Gradient-Z$2",
102 "Gradient-X$3", "Gradient-Y$3", "Gradient-Z$3", "Gradient-X$4", "Gradient-Y$4",
103 "Gradient-Z$4", "Gradient-X$5", "Gradient-Y$5", "Gradient-Z$5", "Gradient-X$6",
104 "Gradient-Y$6", "Gradient-Z$6", "Gradient-X$7", "Gradient-Y$7", "Gradient-Z$7",
105 "Gradient-X$8", "Gradient-Y$8", "Gradient-Z$8", "Curl@1", "Curl@2",
106 "Curl@3", "Curl@4", "Curl@5", "Curl@6", "Curl@7",
107 "Curl@8", "Species_h2o-0", "Species_gas-0", "Species_ch4-0", "Species_methane-0",
108 "Species_h2o-1", "Species_gas-1", "Species_ch4-1", "Species_methane-1"};
109 int num_block_vars = sizeof(var_names) / sizeof(var_names[0]);
110 int num_node_vars = 6;
111
112 EXCHECK(ex_put_variable_param(exoid, EX_ELEM_BLOCK, num_block_vars));
113 EXCHECK(ex_put_variable_names(exoid, EX_ELEM_BLOCK, num_block_vars, var_names));
114 EXCHECK(ex_put_variable_param(exoid, EX_NODAL, num_node_vars));
115 EXCHECK(ex_put_variable_names(exoid, EX_NODAL, num_node_vars, var_names));
116
117 int vname = 0;
118 {
119 struct ex_field field = (ex_field){.entity_type = EX_ELEM_BLOCK,
120 .entity_id = blocks[0].id,
121 .name = "Disp",
122 .type = {EX_VECTOR_3D},
123 .nesting = 1,
124 .component_separator[0] = 0};
125 EXCHECK(ex_put_field_metadata(exoid, field));
126
127 /* Put same field on the nodes... */
128 field.entity_type = EX_NODAL;
129 EXCHECK(ex_put_field_metadata(exoid, field));
130
131 int cardinality =
132 field.cardinality[0] != 0 ? field.cardinality[0] : ex_field_cardinality(field.type[0]);
133 for (int i = 0; i < cardinality; i++) {
134 const char *name = ex_component_field_name(&field, (int[]){i + 1, 0});
135 assert(strcmp(var_names[vname++], name) == 0);
136 }
137 }
138
139 {
140 struct ex_field field = (ex_field){.entity_type = EX_ELEM_BLOCK,
141 .entity_id = blocks[0].id,
142 .name = "Velocity",
143 .type = {EX_VECTOR_3D},
144 .nesting = 1,
145 .component_separator[0] = '%'};
146 EXCHECK(ex_put_field_metadata(exoid, field));
147
148 /* Put same field on the nodes... */
149 field.entity_type = EX_NODAL;
150 EXCHECK(ex_put_field_metadata(exoid, field));
151
152 int cardinality =
153 field.cardinality[0] != 0 ? field.cardinality[0] : ex_field_cardinality(field.type[0]);
154 for (int i = 0; i < cardinality; i++) {
155 const char *name = ex_component_field_name(&field, (int[]){i + 1, 0});
156 assert(strcmp(var_names[vname++], name) == 0);
157 }
158 }
159
160 {
161 double Q = 1.0 / sqrt(3.0);
162 double xi[] = {-Q, Q, -Q, Q, -Q, Q, -Q, Q};
163 double eta[] = {-Q, -Q, Q, Q, -Q, -Q, Q, Q};
164 double zeta[] = {-Q, -Q, -Q, -Q, Q, Q, Q, Q};
165 double weight[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
166
167 struct ex_quadrature quad = (ex_quadrature){
168 .name = "2x2x2", .cardinality = 8, .xi = xi, .eta = eta, .zeta = zeta, .weight = weight};
169 EXCHECK(ex_put_quadrature(exoid, quad));
170 }
171
172 {
173 double xi[] = {-0.5, 0.5};
174 double eta[] = {0.5, -0.5};
175 double zeta[] = {-0.5, 0.5};
176 double weight[] = {1.0, 1.0};
177
178 struct ex_quadrature quad = (ex_quadrature){
179 .name = "1x2x1", .cardinality = 2, .xi = xi, .eta = eta, .zeta = zeta, .weight = weight};
180 EXCHECK(ex_put_quadrature(exoid, quad));
181 }
182
183 {
184 int subc_dim[] = {0, 0, 0, 0, 1, 1, 1, 1, 2};
185 int subc_ordinal[] = {0, 1, 2, 3, 0, 1, 2, 3, 0};
186 int subc_dof_ordinal[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
187 int subc_num_dof[] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
188 double xi[] = {-1.0, 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0};
189 double eta[] = {-1.0, -1.0, 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 0.0};
190 double zeta[] = {1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0};
191
192 struct ex_basis basis = (ex_basis){.name = "HGRAD_QUAD_C2_FEM",
193 .cardinality = 9,
194 .subc_dim = subc_dim,
195 .subc_ordinal = subc_ordinal,
196 .subc_dof_ordinal = subc_dof_ordinal,
197 .subc_num_dof = subc_num_dof,
198 .xi = xi,
199 .eta = eta,
200 .zeta = NULL};
201 EXCHECK(ex_put_basis(exoid, basis));
202
203 struct ex_basis basis1 = (ex_basis){.name = "TESTING_SECOND_BASIS",
204 .cardinality = 3,
205 .subc_dim = subc_dim,
206 .subc_ordinal = subc_ordinal,
207 .subc_dof_ordinal = subc_dof_ordinal,
208 .subc_num_dof = subc_num_dof,
209 .xi = xi,
210 .eta = eta,
211 .zeta = zeta};
212
213 EXCHECK(ex_put_basis(exoid, basis1));
214
215 struct ex_field field = (ex_field){.entity_type = EX_ELEM_BLOCK,
216 .entity_id = blocks[1].id,
217 .name = "Gradient",
218 .type_name = {",HGRAD_QUAD_C2_FEM"},
219 .type = {EX_VECTOR_3D, EX_BASIS},
220 .nesting = 2,
221 .component_separator = {'-', '$'}};
222 EXCHECK(ex_put_field_metadata(exoid, field));
223
224 struct ex_field field2 = (ex_field){.entity_type = EX_ELEM_BLOCK,
225 .entity_id = blocks[1].id,
226 .name = "Curl",
227 .type_name = {"2x2x2"},
228 .type = {EX_QUADRATURE},
229 .nesting = 1,
230 .component_separator = {'@'}};
231 EXCHECK(ex_put_field_metadata(exoid, field2));
232 }
233
234 {
235 struct ex_field field = (ex_field){.entity_type = EX_ELEM_BLOCK,
236 .entity_id = blocks[1].id,
237 .name = "Species",
238 .type = {EX_FIELD_TYPE_USER_DEFINED, EX_QUADRATURE},
239 .type_name = {",1x2x1"},
240 .nesting = 2,
241 .cardinality = {4},
242 .component_separator = {'_', '-'}};
243 EXCHECK(ex_put_field_metadata(exoid, field));
244 EXCHECK(ex_put_field_suffices(exoid, field, "h2o,gas,ch4,methane"));
245 }
246
247 { /* Output time steps ... */
248 for (int ts = 0; ts < 1; ts++) {
249 double time_val = (double)(ts + 1) / 100.0;
250
251 EXCHECK(ex_put_time(exoid, ts + 1, &time_val));
252
253 /* write variables */
254 for (int k = 0; k < num_elem_blk; k++) {
255 double *var_vals = (double *)calloc(blocks[k].num_entry, CPU_word_size);
256 for (int var_idx = 0; var_idx < num_block_vars; var_idx++) {
257 for (int elem = 0; elem < blocks[k].num_entry; elem++) {
258 var_vals[elem] = (double)(var_idx + 2) * time_val + elem;
259 }
260 EXCHECK(ex_put_var(exoid, ts + 1, EX_ELEM_BLOCK, var_idx + 1, blocks[k].id,
261 blocks[k].num_entry, var_vals));
262 }
263 free(var_vals);
264 }
265 }
266 }
267
268 /* close the EXODUS files
269 */
270 EXCHECK(ex_close(exoid));
271 return 0;
272 }