Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021, 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 * testwt - test write an ExodusII database file
11 *
12 * author - Sandia National Laboratories
13 * Larry A. Schoof - Original
14 * Vic Yarberry - Added headers and error logging
15 * 7/7/93 Modified for use with Exodus 2.00
16 *
17 *
18 * environment - UNIX
19 *
20 * entry conditions -
21 *
22 * exit conditions -
23 *
24 * revision history -
25 *
26 * This is a test program for the C binding of the EXODUS II
27 * database write routines.
28 *
29 *
30 *****************************************************************************/
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "exodusII.h"
37
38 #define STRINGIFY(x) #x
39 #define TOSTRING(x) STRINGIFY(x)
40
41 #define EXCHECK(funcall) \
42 do { \
43 int error = (funcall); \
44 printf("after %s, error = %d\n", TOSTRING(funcall), error); \
45 if (error != EX_NOERR) { \
46 fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \
47 ex_close(exoid); \
48 exit(-1); \
49 } \
50 } while (0)
51
52 int main(int argc, char **argv)
53 {
54 ex_opts(EX_VERBOSE);
55
56 /* Specify compute and i/o word size */
57 int CPU_word_size = 8;
58 int IO_word_size = 8;
59
60 /* create EXODUS II file */
61 int exoid = ex_create("test-assembly.exo", /* filename path */
62 EX_CLOBBER, /* create mode */
63 &CPU_word_size, /* CPU double word size in bytes */
64 &IO_word_size); /* I/O double word size in bytes */
65 printf("after ex_create for test.exo, exoid = %d\n", exoid);
66 printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size);
67
68 int num_elem_blk = 7;
69 int num_assembly = 4;
70 {
71 /* initialize file with parameters */
72 int num_dim = 3;
73 int num_nodes = 1;
74 int num_elem = 7;
75 int num_node_sets = 0;
76 int num_side_sets = 0;
77 ex_init_params par = {.num_dim = num_dim,
78 .num_nodes = num_nodes,
79 .num_elem = num_elem,
80 .num_elem_blk = num_elem_blk,
81 .num_node_sets = num_node_sets,
82 .num_side_sets = num_side_sets,
83 .num_assembly = num_assembly};
84
85 char *title = "This is a test";
86 ex_copy_string(par.title, title, MAX_LINE_LENGTH + 1);
87 EXCHECK(ex_put_init_ext(exoid, &par));
88 }
89
90 double coord[] = {0.0};
91 EXCHECK(ex_put_coord(exoid, coord, coord, coord));
92
93 /* ======================================================================== */
94 /* write element block parameters */
95 struct ex_block blocks[7];
96 for (int i = 0; i < num_elem_blk; i++) {
97 blocks[i] = (ex_block){.type = EX_ELEM_BLOCK, .num_entry = 1, .id = i + 10};
98 }
99
100 ex_copy_string(blocks[0].topology, "sphere", MAX_STR_LENGTH + 1);
101 ex_copy_string(blocks[1].topology, "sphere", MAX_STR_LENGTH + 1);
102 ex_copy_string(blocks[2].topology, "sphere", MAX_STR_LENGTH + 1);
103 ex_copy_string(blocks[3].topology, "sphere", MAX_STR_LENGTH + 1);
104 ex_copy_string(blocks[4].topology, "sphere", MAX_STR_LENGTH + 1);
105 ex_copy_string(blocks[5].topology, "sphere", MAX_STR_LENGTH + 1);
106 ex_copy_string(blocks[6].topology, "sphere", MAX_STR_LENGTH + 1);
107
108 blocks[0].num_nodes_per_entry = 1;
109 blocks[1].num_nodes_per_entry = 1;
110 blocks[2].num_nodes_per_entry = 1;
111 blocks[3].num_nodes_per_entry = 1;
112 blocks[4].num_nodes_per_entry = 1;
113 blocks[5].num_nodes_per_entry = 1;
114 blocks[6].num_nodes_per_entry = 1;
115
116 EXCHECK(ex_put_block_params(exoid, num_elem_blk, blocks));
117
118 int connect[] = {1};
119 for (int i = 0; i < num_elem_blk; i++) {
120 EXCHECK(ex_put_conn(exoid, EX_ELEM_BLOCK, blocks[i].id, connect, NULL, NULL));
121 }
122
123 /* Write element block names */
124 char *block_names[] = {"block_A", "block_B", "block_C", "block_D",
125 "block_E", "block_F", "block_G"};
126 for (int i = 0; i < num_elem_blk; i++) {
127 EXCHECK(ex_put_name(exoid, EX_ELEM_BLOCK, blocks[i].id, block_names[i]));
128 }
129
130 /* ======================================================================== */
131 /* Define and Output Assemblies */
132 int64_t list_100[] = {200, 300, 400};
133 int64_t list_200[] = {10, 11, 12, 13};
134 int64_t list_300[] = {14, 15, 16};
135 int64_t list_400[] = {10, 16};
136
137 /* Define assemblies. Note that first and last do not have an entity list at time of definition.
138 */
139 ex_assembly assembly[] = {{100, "Root", EX_ASSEMBLY, 3, NULL},
140 {200, "Child2", EX_ELEM_BLOCK, 4, list_200},
141 {300, "Child3", EX_ELEM_BLOCK, 3, list_300},
142 {400, "Child4", EX_ELEM_BLOCK, 2, NULL}};
143
144 for (int i = 0; i < num_assembly; i++) {
145 EXCHECK(ex_put_assembly(exoid, assembly[i]));
146 }
147
148 /* Now verify that can put entity list at a later time... */
149 assembly[0].entity_list = list_100;
150 EXCHECK(ex_put_assembly(exoid, assembly[0]));
151
152 assembly[3].entity_list = list_400;
153 EXCHECK(ex_put_assembly(exoid, assembly[3]));
154
155 /* ======================================================================== */
156 /* Add some arbitrary attributes to the assemblies */
157 {
158 double scale = 1.5;
159 double offset[] = {1.1, 2.2, 3.3};
160 char *dimension = "length";
161 int units[] = {1, 0, 0, -1};
162
163 EXCHECK(ex_put_double_attribute(exoid, EX_ASSEMBLY, 100, "Scale", 1, &scale));
164 EXCHECK(ex_put_double_attribute(exoid, EX_ASSEMBLY, 200, "Offset", 3, offset));
165 EXCHECK(ex_put_text_attribute(exoid, EX_ASSEMBLY, 300, "Dimension", dimension));
166 EXCHECK(ex_put_integer_attribute(exoid, EX_ASSEMBLY, 400, "Units", 4, units));
167
168 ex_attribute attribute = {.entity_type = EX_ASSEMBLY,
169 .entity_id = 100,
170 .name = {"Units"},
171 .type = EX_INTEGER,
172 .value_count = 4,
173 .values = units};
174 EXCHECK(ex_put_attribute(exoid, attribute));
175
176 ex_attribute attr_offset = {EX_ASSEMBLY, 300, "Offset", EX_DOUBLE, 3, offset};
177 EXCHECK(ex_put_attribute(exoid, attr_offset));
178
179 /* Make sure this works for non-assemblies also... */
180 EXCHECK(ex_put_integer_attribute(exoid, EX_ELEM_BLOCK, 11, "Units", 4, units));
181 EXCHECK(ex_put_text_attribute(exoid, EX_GLOBAL, 0, "SOLID_MODEL", "STEP-X-43-1547836-Rev 0"));
182 }
183
184 /* ======================================================================== */
185 /* Transient Variables */
186 int num_assem_vars = 4;
187 EXCHECK(ex_put_reduction_variable_param(exoid, EX_ASSEMBLY, num_assem_vars));
188
189 {
190 char *var_names[] = {"Momentum_X", "Momentum_Y", "Momentum_Z", "Kinetic_Energy"};
191 EXCHECK(ex_put_reduction_variable_names(exoid, EX_ASSEMBLY, num_assem_vars, var_names));
192 }
193
194 { /* Output time steps ... */
195 double *var_vals = (double *)calloc(num_assem_vars, CPU_word_size);
196 for (int ts = 0; ts < 10; ts++) {
197 double time_val = (double)(ts + 1) / 100.0;
198
199 EXCHECK(ex_put_time(exoid, ts + 1, &time_val));
200
201 /* write assembly variables */
202 for (int k = 0; k < num_assembly; k++) {
203 for (int var_idx = 0; var_idx < num_assem_vars; var_idx++) {
204 var_vals[var_idx] = (double)(var_idx + 2) * time_val + k;
205 }
206 EXCHECK(ex_put_reduction_vars(exoid, ts + 1, EX_ASSEMBLY, assembly[k].id, num_assem_vars,
207 var_vals));
208 }
209 }
210 free(var_vals);
211 }
212
213 /* close the EXODUS files
214 */
215 EXCHECK(ex_close(exoid));
216 return 0;
217 }
218
219 /*! Reduction Variables:
220 * - Transient values applied to the object instead of each member of the object.
221 * - for example, a value for the element block; not for each element in the block.
222 * - Each object type has the same variables
223 * - All values for a specific object are output at same time -- Vals for "assembly 100"
224 * - GLOBAL values can be considered reduction variables
225 * - for BW compat, can output either with:
226 * - ex_put_var(exoid, ts, EX_GLOBAL, ?, ?, #gvar, gvar), or
227 * - ex_put_reduction_var(exoid, ts, EX_GLOBAL, ?, #gvar, gvar)
228 * -
229 * - ex_put_reduc_var(exoid, ts, EX_ASSEMBLY, id, #var, var)
230 * - ex_get_reduc_var(exoid, ts, EX_ASSEMBLY, id, #var, &var)
231 *
232 * - ex_put_reduc_variable_names()
233 * - ex_put_reduc_variable_param()
234
235 */
236 /* int ex_put_reduction_var(exoid, time_step, obj_type, obj_id, #values, values); */