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 *
37 * testwt - test write an ExodusII database file
38 *
39 * author - Sandia National Laboratories
40 *          Larry A. Schoof - Original
41 *          Vic Yarberry    - Added headers and error logging
42 *               7/7/93          Modified for use with Exodus 2.00
43 *
44 *
45 * environment - UNIX
46 *
47 * entry conditions -
48 *
49 * exit conditions -
50 *
51 * revision history -
52 *
53 *  This is a test program for the C binding of the EXODUS II
54 *  database write routines.
55 *
56 *
57 *****************************************************************************/
58 
59 
60 #include <stdlib.h>
61 #include <stdio.h>
62 /* #include "netcdf.h" */
63 #include "exodusII.h"
64 
main(int argc,char ** argv)65 int main (int argc, char **argv)
66 {
67    int exoid, num_dim, num_nodes, num_elem, num_elem_blk;
68    int num_node_sets, num_side_sets, error;
69    int i, j;
70    int  num_qa_rec, num_info;
71    int num_glo_vars;
72    int whole_time_step, num_time_steps;
73    int CPU_word_size,IO_word_size;
74 
75    float *glob_var_vals;
76    float time_value;
77    char *coord_names[3], *qa_record[2][4], *info[3], *var_names[3];
78 
79    ex_opts (EX_VERBOSE | EX_ABORT );
80 
81 /* Specify compute and i/o word size */
82 
83    CPU_word_size = 0;                   /* sizeof(float) */
84    IO_word_size = 4;                    /* (4 bytes) */
85 
86 /* create EXODUS II file */
87 
88    exoid = ex_create ("test.exo",       /* filename path */
89                        EX_CLOBBER,      /* create mode */
90                        &CPU_word_size,  /* CPU float word size in bytes */
91                        &IO_word_size);  /* I/O float word size in bytes */
92    printf ("after ex_create for test.exo, exoid = %d\n", exoid);
93    printf (" cpu word size: %d io word size: %d\n",CPU_word_size,IO_word_size);
94 
95    /* ncopts = NC_VERBOSE; */
96 
97 /* initialize file with parameters */
98 
99    num_dim = 1;
100    num_nodes = 0;
101    num_elem = 0;
102    num_elem_blk = 0;
103    num_node_sets = 0;
104    num_side_sets = 0;
105 
106    error = ex_put_init (exoid, "This is a test", num_dim, num_nodes, num_elem,
107                         num_elem_blk, num_node_sets, num_side_sets);
108 
109    printf ("after ex_put_init, error = %d\n", error);
110 
111    if (error) {
112      ex_close (exoid);
113      exit(-1);
114    }
115 
116    coord_names[0] = "xcoor";
117 
118    error = ex_put_coord_names (exoid, coord_names);
119    printf ("after ex_put_coord_names, error = %d\n", error);
120 
121    if (error) {
122      ex_close (exoid);
123      exit(-1);
124    }
125 
126 /* write QA records; test empty and just blank-filled records */
127 
128    num_qa_rec = 2;
129 
130 
131    qa_record[0][0] = "TESTWT";
132    qa_record[0][1] = "testwt";
133    qa_record[0][2] = "07/07/93";
134    qa_record[0][3] = "15:41:33";
135    qa_record[1][0] = "";
136    qa_record[1][1] = "                            ";
137    qa_record[1][2] = "";
138    qa_record[1][3] = "                        ";
139 
140    error = ex_put_qa (exoid, num_qa_rec, qa_record);
141    printf ("after ex_put_qa, error = %d\n", error);
142 
143    if (error) {
144      ex_close (exoid);
145      exit(-1);
146    }
147 
148 
149 /* write information records; test empty and just blank-filled records */
150 
151    num_info = 3;
152 
153 
154    info[0] = "This is the first information record.";
155    info[1] = "";
156    info[2] = "                                     ";
157 
158    error = ex_put_info (exoid, num_info, info);
159    printf ("after ex_put_info, error = %d\n", error);
160 
161    if (error) {
162      ex_close (exoid);
163      exit(-1);
164    }
165 
166 
167 
168 /* write results variables parameters and names */
169 
170    num_glo_vars = 1;
171 
172    var_names[0] = "glo_vars";
173 
174    error = ex_put_var_param (exoid, "g", num_glo_vars);
175    printf ("after ex_put_var_param, error = %d\n", error);
176    if (error) {
177      ex_close (exoid);
178      exit(-1);
179    }
180 
181    error = ex_put_var_names (exoid, "g", num_glo_vars, var_names);
182    printf ("after ex_put_var_names, error = %d\n", error);
183    if (error) {
184      ex_close (exoid);
185      exit(-1);
186    }
187 
188 
189 
190 /* for each time step, write the analysis results;
191  * the code below fills the arrays glob_var_vals,
192  * nodal_var_vals, and elem_var_vals with values for debugging purposes;
193  * obviously the analysis code will populate these arrays
194  */
195 
196    whole_time_step = 1;
197    num_time_steps = 10;
198 
199    glob_var_vals = (float *) calloc (num_glo_vars, CPU_word_size);
200 
201    for (i=0; i<num_time_steps; i++)
202    {
203      time_value = (float)(i+1)/100.;
204 
205 /* write time value */
206 
207      error = ex_put_time (exoid, whole_time_step, &time_value);
208      printf ("after ex_put_time, error = %d\n", error);
209 
210      if (error) {
211        ex_close (exoid);
212        exit(-1);
213      }
214 
215 /* write global variables */
216 
217      for (j=0; j<num_glo_vars; j++)
218      {
219        glob_var_vals[j] = (float)(j+2) * time_value;
220      }
221 
222      error = ex_put_glob_vars (exoid, whole_time_step, num_glo_vars,
223                                glob_var_vals);
224      printf ("after ex_put_glob_vars, error = %d\n", error);
225 
226      if (error) {
227        ex_close (exoid);
228        exit(-1);
229      }
230 
231      whole_time_step++;
232 
233 /* update the data file; this should be done at the end of every time step
234  * to ensure that no data is lost if the analysis dies
235  */
236      error = ex_update (exoid);
237      printf ("after ex_update, error = %d\n", error);
238      if (error) {
239        ex_close (exoid);
240        exit(-1);
241      }
242    }
243    free(glob_var_vals);
244 
245 
246 /* close the EXODUS files
247  */
248    error = ex_close (exoid);
249    printf ("after ex_close, error = %d\n", error);
250    if (error) {
251      ex_close (exoid);
252      exit(-1);
253    }
254    return 0;
255 }
256