1 /*
2  * Copyright (c) 2005 Sandia Corporation. Under the terms of Contract
3  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement
4  * retains certain rights in this software.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  *     * Neither the name of Sandia Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include "exodusII.h"
39 
main(int argc,char ** argv)40 int main (int argc, char **argv)
41 {
42   int num_glo_vars = 10;
43   int num_nod_vars = 2;
44   int CPU_word_size = 8;
45   int IO_word_size = 8;
46   const char* title = "This is a 2D mesh example with tri, quad, beam, truss, circle";
47   int ebids[] = {100, 200, 300, 400, 500};
48   int num_dim   =  2;
49   int num_nodes = 13;
50   int num_elem  = 20;
51   int num_elem_blk = 5;
52   int num_node_sets = 2;
53   int num_side_sets = 2;
54 
55   /* create EXODUS II file */
56   int exoid = ex_create ("twod.e",       /* filename path */
57 			 EX_CLOBBER,      /* create mode */
58 			 &CPU_word_size,  /* CPU float word size in bytes */
59 			 &IO_word_size);  /* I/O float word size in bytes */
60 
61    ex_opts(EX_VERBOSE);
62 
63    /* initialize file with parameters */
64      ex_put_init (exoid, title, num_dim, num_nodes, num_elem,
65 			  num_elem_blk, num_node_sets, num_side_sets);
66 
67      /* write nodal coordinates values and names to database */
68      {
69        double x[13], y[13];
70        x[0]  =  0.0; y[0]  =  0.0;
71        x[1]  = -0.5; y[1]  = -0.5;
72        x[2]  =  0.5; y[2]  = -0.5;
73        x[3]  =  0.5; y[3]  =  0.5;
74        x[4]  = -0.5; y[4]  =  0.5;
75        x[5]  = -1.0; y[5]  = -1.0;
76        x[6]  =  1.0; y[6]  = -1.0;
77        x[7]  =  1.0; y[7]  =  1.0;
78        x[8]  = -1.0; y[8]  =  1.0;
79        x[9]  = -2.0; y[9]  =  0.0;
80        x[10] =  0.0; y[10] = -2.0;
81        x[11] =  2.0; y[11] =  0.0;
82        x[12] =  0.0; y[12] =  2.0;
83 
84        ex_put_coord (exoid, x, y, 0);
85      }
86 
87      {
88        const char* coord_names[] = {"xcoor", "ycoor"};
89        ex_put_coord_names (exoid, (char**)coord_names);
90      }
91 
92 
93      {
94        int node_map[] = {10,20,30,40,50,60,70,80,90,100,110,120,130};
95        ex_put_node_num_map(exoid, node_map);
96      }
97 
98      /* write element order map */
99      {
100        int elem_map[] = {11,21,31,41, 52, 62, 72, 82, 93,103,113,123,133,143,153,163,  174,184,194,204};
101        ex_put_elem_num_map (exoid, elem_map);
102      }
103 
104      /* write element block parameters */
105      {
106        const char* block_names[] = {"Triangles", "Quadrilaterals", "", "Trusses", "Circles"};
107        int num_elem_in_block[] = {4, 4, 4, 4, 4};
108        int num_nodes_per_elem[] = {3, 4, 2, 2, 1};
109 
110        ex_put_elem_block (exoid, ebids[0], "triangle", num_elem_in_block[0], num_nodes_per_elem[0], 0);
111        ex_put_elem_block (exoid, ebids[1], "quad",     num_elem_in_block[1], num_nodes_per_elem[1], 0);
112        ex_put_elem_block (exoid, ebids[2], "beam",     num_elem_in_block[2], num_nodes_per_elem[2], 3);
113        ex_put_elem_block (exoid, ebids[3], "truss",    num_elem_in_block[3], num_nodes_per_elem[3], 1);
114        ex_put_elem_block (exoid, ebids[4], "circle",   num_elem_in_block[4], num_nodes_per_elem[4], 2);
115 
116        /* Write element block names */
117        ex_put_names(exoid, EX_ELEM_BLOCK, (char**)block_names);
118      }
119 
120      /* write element connectivity */
121      {
122        int conn_t[] = {2,3,1,  3,4,1,  4,5,1,  5,2,1};
123        int conn_q[] = {6,7,3,2,  7,8,4,3,  8,9,5,4,  9,6,2,5};
124        int conn_B[] = {11,7,  8,13,  13,9,  6,11};
125        int conn_T[] = {10,6,  9,10,  7,12,  12,8};
126        int conn_c[] = {6,7,8,9};
127 
128        ex_put_elem_conn (exoid, ebids[0], conn_t);
129        ex_put_elem_conn (exoid, ebids[1], conn_q);
130        ex_put_elem_conn (exoid, ebids[2], conn_B);
131        ex_put_elem_conn (exoid, ebids[3], conn_T);
132        ex_put_elem_conn (exoid, ebids[4], conn_c);
133      }
134 
135      /* write element block attributes */
136      {
137        const char* attn_T[] = {"Area"};
138        double attr_T[] = {1.0, 1.1, 1.2, 1.3};
139 
140        const char* attn_B[] = {"A", "I", "J"};
141        double attr_B[] = {1.0, 100.0, 200.0,   1.1, 100.1, 200.1,  1.2, 100.2, 200.2,  1.3, 100.3, 200.3};
142 
143        const char* attn_c[] = {"Radius", "A"};
144        double attr_c[] = {1.0, 3.14, 1.1, 4.14, 1.2, 5.14, 1.3, 6.14};
145 
146        ex_put_elem_attr (exoid, ebids[2], attr_B);
147        ex_put_elem_attr (exoid, ebids[3], attr_T);
148        ex_put_elem_attr (exoid, ebids[4], attr_c);
149 
150        ex_put_elem_attr_names (exoid, ebids[2], (char**)attn_B);
151        ex_put_elem_attr_names (exoid, ebids[3], (char**)attn_T);
152        ex_put_elem_attr_names (exoid, ebids[4], (char**)attn_c);
153      }
154 
155      /* write individual node sets */
156      {
157        int num_nodes_in_nset[] = {5, 8};
158        int nsids[] = {20, 22};
159        int nod1[] = {5,4,3,2,1};
160        int nod2[] = {6,7,8,9,2,3,4,5};
161        const char* nset_names[] = {"Triangle_Nodes", "Quadrilateral_Nodes"};
162 
163        ex_put_set_param (exoid, EX_NODE_SET, nsids[0], num_nodes_in_nset[0], 0);
164        ex_put_set_param (exoid, EX_NODE_SET, nsids[1], num_nodes_in_nset[1], 0);
165 
166        ex_put_set (exoid, EX_NODE_SET, nsids[0], nod1, 0);
167        ex_put_set (exoid, EX_NODE_SET, nsids[1], nod2, 0);
168        ex_put_names(exoid, EX_NODE_SET, (char**)nset_names);
169      }
170 
171      {
172        /* write individual side sets */
173        int num_face_in_sset[] =  {4,4};
174        int ssids[] = {100,200};
175        int ss1el[] = {1,2,3,4};
176        int ss1si[] = {1,1,1,1};
177 
178        int ss2el[] = {5,7,6,8};
179        int ss2si[] = {1,1,1,1};
180        const char* sset_names[] = {"A", "B"};
181 
182        ex_put_set_param (exoid, EX_SIDE_SET, ssids[0], num_face_in_sset[0], 0);
183        ex_put_set_param (exoid, EX_SIDE_SET, ssids[1], num_face_in_sset[1], 0);
184 
185        ex_put_set (exoid, EX_SIDE_SET, ssids[0], ss1el, ss1si);
186        ex_put_set (exoid, EX_SIDE_SET, ssids[1], ss2el, ss2si);
187        ex_put_names(exoid, EX_SIDE_SET, (char**)sset_names);
188      }
189 
190 
191      /* write results variables parameters and names */
192      {
193        const char* gvarn[] = {"g_01", "g_02", "g_03", "g_04", "g_05", "g_06", "g_07", "g_08", "g_09", "g_10"};
194        ex_put_variable_param (exoid, EX_GLOBAL, num_glo_vars);
195        ex_put_variable_names (exoid, EX_GLOBAL, num_glo_vars, (char**)gvarn);
196      }
197 
198      {
199        const char* nvarn[] = {"disp_x", "disp_y"};
200        ex_put_variable_param (exoid, EX_NODAL, num_nod_vars);
201        ex_put_variable_names (exoid, EX_NODAL, num_nod_vars, (char**)nvarn);
202      }
203 
204 
205 #if 0
206    num_ele_vars = 3;
207    /*              0        1         2         3   */
208    /*              12345678901234567890123456789012 */
209    var_names[0] = "this_variable_name_is_short";
210    var_names[1] = "this_variable_name_is_just_right";
211    var_names[2] = "this_variable_name_is_tooooo_long";
212 
213    ex_put_var_param (exoid, "e", num_ele_vars);
214    printf ("after ex_put_var_param, %d\n", error);
215    if (error) {
216      ex_close (exoid);
217      exit(-1);
218    }
219 
220    ex_put_var_names (exoid, "e", num_ele_vars, var_names);
221    printf ("after ex_put_var_names, %d\n", error);
222    if (error) {
223      ex_close (exoid);
224      exit(-1);
225    }
226 
227    {
228      num_nset_vars = 3;
229 
230      var_names[0] = "ns_var0";
231      var_names[1] = "ns_var1";
232      var_names[2] = "ns_var2";
233 
234      ex_put_var_param (exoid, "m", num_nset_vars);
235      printf ("after ex_put_var_param, %d\n", error);
236      if (error) {
237        ex_close (exoid);
238        exit(-1);
239      }
240 
241      ex_put_var_names (exoid, "m", num_nset_vars, var_names);
242      printf ("after ex_put_var_names, %d\n", error);
243      if (error) {
244        ex_close (exoid);
245        exit(-1);
246      }
247    }
248 
249    {
250      num_sset_vars = 3;
251 
252      var_names[0] = "ss_var0";
253      var_names[1] = "ss_var1";
254      var_names[2] = "ss_var2";
255 
256      ex_put_var_param (exoid, "s", num_sset_vars);
257      printf ("after ex_put_var_param, %d\n", error);
258      if (error) {
259        ex_close (exoid);
260        exit(-1);
261      }
262 
263      ex_put_var_names (exoid, "s", num_sset_vars, var_names);
264      printf ("after ex_put_var_names, %d\n", error);
265      if (error) {
266        ex_close (exoid);
267        exit(-1);
268      }
269    }
270 #endif
271 
272    /* for each time step, write the analysis results;
273     * the code below fills the arrays glob_var_vals,
274     * nodal_var_vals, and elem_var_vals with values for debugging purposes;
275     * obviously the analysis code will populate these arrays
276     */
277 
278    {
279      int i, j, k;
280      int whole_time_step = 1;
281      int num_time_steps = 10;
282 
283      double gvar[10];
284      double nvar[20];
285 
286      for (i=0; i<num_time_steps; i++) {
287        double time_value = (double)(i)/100.;
288 
289        ex_put_time (exoid, whole_time_step, &time_value);
290 
291        for (j=0; j<num_glo_vars; j++) {
292 	 gvar[j] = (double)(j+2) * time_value;
293        }
294        ex_put_glob_vars (exoid, whole_time_step, num_glo_vars, gvar);
295 
296        /* write nodal variables */
297        for (k=0; k < num_nod_vars; k++) {
298 	 for (j=0; j<num_nodes; j++) {
299 	   nvar[j] = (double)k + ((double)(j+1) * time_value);
300 	 }
301 
302 	 ex_put_nodal_var (exoid, whole_time_step, k+1, num_nodes, nvar);
303        }
304 
305 #if 0
306 /* write element variables */
307 
308      for (k=1; k<=num_ele_vars; k++)
309      {
310        for (j=0; j<num_elem_blk; j++)
311        {
312          for (m=0; m<num_elem_in_block[j]; m++)
313          {
314            elem_var_vals[m] = (float)(k+1) + (float)(j+2) +
315                               ((float)(m+1)*time_value);
316            /* printf("elem_var_vals[%d]: %f\n",m,elem_var_vals[m]); */
317          }
318          ex_put_elem_var (exoid, whole_time_step, k, ebids[j],
319                                   num_elem_in_block[j], elem_var_vals);
320          printf ("after ex_put_elem_var, %d\n", error);
321          if (error) {
322            ex_close (exoid);
323            exit(-1);
324          }
325        }
326      }
327 
328 /* write sideset variables */
329 
330      for (k=1; k<=num_sset_vars; k++)
331      {
332        for (j=0; j<num_side_sets; j++)
333        {
334          for (m=0; m<num_face_in_sset[j]; m++)
335          {
336            sset_var_vals[m] = (float)(k+2) + (float)(j+3) +
337                               ((float)(m+1)*time_value);
338            /* printf("sset_var_vals[%d]: %f\n",m,sset_var_vals[m]); */
339          }
340          ex_put_sset_var (exoid, whole_time_step, k, ssids[j],
341                                   num_face_in_sset[j], sset_var_vals);
342          printf ("after ex_put_sset_var, %d\n", error);
343          if (error) {
344            ex_close (exoid);
345            exit(-1);
346          }
347        }
348      }
349 
350 /* write nodeset variables */
351 
352      for (k=1; k<=num_nset_vars; k++)
353      {
354        for (j=0; j<num_node_sets; j++)
355        {
356          for (m=0; m<num_nodes_in_nset[j]; m++)
357          {
358            nset_var_vals[m] = (float)(k+3) + (float)(j+4) +
359                               ((float)(m+1)*time_value);
360            /* printf("nset_var_vals[%d]: %f\n",m,nset_var_vals[m]); */
361          }
362          ex_put_nset_var (exoid, whole_time_step, k, nsids[j],
363                                   num_nodes_in_nset[j], nset_var_vals);
364          printf ("after ex_put_nset_var, %d\n", error);
365          if (error) {
366            ex_close (exoid);
367            exit(-1);
368          }
369        }
370      }
371 #endif
372 
373      whole_time_step++;
374    }
375    }
376    ex_close (exoid);
377    return 0;
378 }
379