Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021, 2023 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
58 int CPU_word_size = 8;
59 int IO_word_size = 8;
60
61 /* create EXODUS II file */
62
63 int exoid = ex_create("test-blob.exo", /* filename path */
64 EX_CLOBBER, /* create mode */
65 &CPU_word_size, /* CPU double word size in bytes */
66 &IO_word_size); /* I/O double word size in bytes */
67 printf("after ex_create for test.exo, exoid = %d\n", exoid);
68 printf(" cpu word size: %d io word size: %d\n", CPU_word_size, IO_word_size);
69
70 /* initialize file with parameters */
71 int num_blob = 3;
72 {
73 int num_dim = 3;
74 int num_nodes = 0;
75 int num_elem = 0;
76 int num_elem_blk = 0;
77 int num_node_sets = 0;
78 int num_side_sets = 0;
79 int num_assembly = 0;
80
81 ex_init_params par = {.num_dim = num_dim,
82 .num_nodes = num_nodes,
83 .num_elem = num_elem,
84 .num_elem_blk = num_elem_blk,
85 .num_node_sets = num_node_sets,
86 .num_side_sets = num_side_sets,
87 .num_assembly = num_assembly,
88 .num_blob = num_blob};
89
90 char *title = "This is a test";
91 ex_copy_string(par.title, title, MAX_LINE_LENGTH + 1);
92 EXCHECK(ex_put_init_ext(exoid, &par));
93 }
94
95 /* ======================================================================== */
96 /* Define and Output Blobs */
97 /* Define blobs.
98 */
99 ex_blob blob[] = {{100, "Tempus", 10}, // ID, Name, Size
100 {200, "IOSS", 20},
101 {300, "Solver", 15}};
102
103 for (int i = 0; i < num_blob; i++) {
104 EXCHECK(ex_put_blob(exoid, blob[i]));
105 }
106
107 /* ======================================================================== */
108 /* Add some arbitrary attributes to the blobs */
109 {
110 double scale = 1.5;
111 double offset[] = {1.1, 2.2, 3.3};
112 char *dimension = "length";
113 int units[] = {1, 0, 0, -1};
114
115 EXCHECK(ex_put_double_attribute(exoid, EX_BLOB, 100, "Scale", 1, &scale));
116 EXCHECK(ex_put_double_attribute(exoid, EX_BLOB, 200, "Offset", 3, offset));
117 EXCHECK(ex_put_text_attribute(exoid, EX_BLOB, 300, "Dimension", dimension));
118
119 ex_attribute attribute = {.entity_type = EX_BLOB,
120 .entity_id = 100,
121 .name = {"Units"},
122 .type = EX_INTEGER,
123 .value_count = 4,
124 .values = units};
125 EXCHECK(ex_put_attribute(exoid, attribute));
126
127 ex_attribute attr_offset = {EX_BLOB, 300, "Offset", EX_DOUBLE, 3, offset};
128 EXCHECK(ex_put_attribute(exoid, attr_offset));
129
130 EXCHECK(ex_put_text_attribute(exoid, EX_GLOBAL, 0, "SOLID_MODEL", "STEP-X-43-1547836-Rev 0"));
131 }
132
133 /* ======================================================================== */
134 /* Transient and Reduction Variables */
135 int num_red_blob_vars = 4;
136 int num_blob_vars = 3;
137 EXCHECK(ex_put_reduction_variable_param(exoid, EX_BLOB, num_red_blob_vars));
138 EXCHECK(ex_put_variable_param(exoid, EX_BLOB, num_blob_vars));
139
140 {
141 char *red_var_names[] = {"Momentum_X", "Momentum_Y", "Momentum_Z", "Kinetic_Energy"};
142 EXCHECK(ex_put_reduction_variable_names(exoid, EX_BLOB, num_red_blob_vars, red_var_names));
143 }
144
145 {
146 char *var_names[] = {"X", "XDOT", "XDDOT"};
147 EXCHECK(ex_put_variable_names(exoid, EX_BLOB, num_blob_vars, var_names));
148 }
149
150 { /* Output time steps ... */
151 int64_t max_count = 0;
152 for (int k = 0; k < num_blob; k++) {
153 if (blob[k].num_entry > max_count) {
154 max_count = blob[k].num_entry;
155 }
156 }
157
158 double *var_vals = (double *)calloc(num_red_blob_vars, CPU_word_size);
159 double *vals = (double *)calloc(max_count, CPU_word_size);
160 for (int ts = 0; ts < 4; ts++) {
161 double time_val = (double)(ts + 1) / 100.0f;
162
163 EXCHECK(ex_put_time(exoid, ts + 1, &time_val));
164
165 /* write blob reduction variables */
166 for (int k = 0; k < num_blob; k++) {
167 for (int var_idx = 0; var_idx < num_red_blob_vars; var_idx++) {
168 var_vals[var_idx] = (double)(var_idx + 2) * time_val + k;
169 }
170 EXCHECK(
171 ex_put_reduction_vars(exoid, ts + 1, EX_BLOB, blob[k].id, num_red_blob_vars, var_vals));
172 }
173
174 /* write blob variables */
175 for (int k = 0; k < num_blob; k++) {
176 for (int var_idx = 0; var_idx < num_blob_vars; var_idx++) {
177 for (int j = 0; j < blob[k].num_entry; j++) {
178 vals[j] = (double)(var_idx + 2) * time_val + j + k / 100.0;
179 }
180 EXCHECK(
181 ex_put_var(exoid, ts + 1, EX_BLOB, var_idx + 1, blob[k].id, blob[k].num_entry, vals));
182 }
183 }
184 }
185 free(var_vals);
186 free(vals);
187 }
188
189 /* close the EXODUS files
190 */
191 EXCHECK(ex_close(exoid));
192 return 0;
193 }
194
195 /*! Reduction Variables:
196 * - Transient values applied to the object instead of each member of the object.
197 * - for example, a value for the element block; not for each element in the block.
198 * - Each object type has the same variables
199 * - All values for a specific object are output at same time -- Vals for "blob 100"
200 * - GLOBAL values can be considered reduction variables
201 * - for BW compat, can output either with:
202 * - ex_put_var(exoid, ts, EX_GLOBAL, ?, ?, #gvar, gvar), or
203 * - ex_put_reduction_var(exoid, ts, EX_GLOBAL, ?, #gvar, gvar)
204 * -
205 * - ex_put_reduc_var(exoid, ts, EX_BLOB, id, #var, var)
206 * - ex_get_reduc_var(exoid, ts, EX_BLOB, id, #var, &var)
207 *
208 * - ex_put_reduc_variable_names()
209 * - ex_put_reduc_variable_param()
210
211 */
212 /* int ex_put_reduction_var(exoid, time_step, obj_type, obj_id, #values, values); */