1 /*
2 Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
3 LLNL-CODE-425250.
4 All rights reserved.
5 
6 This file is part of Silo. For details, see silo.llnl.gov.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the disclaimer below.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the disclaimer (as noted
16      below) in the documentation and/or other materials provided with
17      the distribution.
18    * Neither the name of the LLNS/LLNL 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  LAWRENCE
26 LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
27 CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
29 PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
32 NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 This work was produced at Lawrence Livermore National Laboratory under
36 Contract  No.   DE-AC52-07NA27344 with  the  DOE.  Neither the  United
37 States Government  nor Lawrence  Livermore National Security,  LLC nor
38 any of  their employees,  makes any warranty,  express or  implied, or
39 assumes   any   liability   or   responsibility  for   the   accuracy,
40 completeness, or usefulness of any information, apparatus, product, or
41 process  disclosed, or  represents  that its  use  would not  infringe
42 privately-owned   rights.  Any  reference   herein  to   any  specific
43 commercial products,  process, or  services by trade  name, trademark,
44 manufacturer or otherwise does not necessarily constitute or imply its
45 endorsement,  recommendation,   or  favoring  by   the  United  States
46 Government or Lawrence Livermore National Security, LLC. The views and
47 opinions  of authors  expressed  herein do  not  necessarily state  or
48 reflect those  of the United  States Government or  Lawrence Livermore
49 National  Security, LLC,  and shall  not  be used  for advertising  or
50 product endorsement purposes.
51 */
52 #include <silo.h>
53 #include <stdio.h>
54 #include <math.h>
55 #include <string.h>
56 #include <float.h>
57 
58 #define ASSERT(PRED) if(!(PRED)){fprintf(stderr,"Assertion \"%s\" at line %d failed\n",#PRED,__LINE__);abort();}
59 
60 #define IND(i,j) i-1][j-1
61 
62 #define matrix_assign(matrix,a11,a12,a13,a14,a21,a22,a23,a24,a31,a32,a33,a34,a41,a42,a43,a44)         \
63    {                                                                          \
64    matrix [IND(1,1)] = a11 ;                                              \
65    matrix [IND(1,2)] = a12 ;                                              \
66    matrix [IND(1,3)] = a13 ;                                              \
67    matrix [IND(1,4)] = a14 ;                                              \
68    matrix [IND(2,1)] = a21 ;                                              \
69    matrix [IND(2,2)] = a22 ;                                              \
70    matrix [IND(2,3)] = a23 ;                                              \
71    matrix [IND(2,4)] = a24 ;                                              \
72    matrix [IND(3,1)] = a31 ;                                              \
73    matrix [IND(3,2)] = a32 ;                                              \
74    matrix [IND(3,3)] = a33 ;                                              \
75    matrix [IND(3,4)] = a34 ;                                              \
76    matrix [IND(4,1)] = a41 ;                                              \
77    matrix [IND(4,2)] = a42 ;                                              \
78    matrix [IND(4,3)] = a43 ;                                              \
79    matrix [IND(4,4)] = a44 ;                                              \
80    }
81 
82 #define matrix_mult(matrixa, matrixb, matrixc)                                \
83    {                                                                          \
84    for (i = 1; i < 5; i++) {                                                  \
85       for (j = 1; j < 5; j++) {                                               \
86          matrixc [IND(i,j)] = matrixa [IND(i,1)] * matrixb [IND(1,j)] + \
87                                   matrixa [IND(i,2)] * matrixb [IND(2,j)] + \
88                                   matrixa [IND(i,3)] * matrixb [IND(3,j)] + \
89                                   matrixa [IND(i,4)] * matrixb [IND(4,j)] ; \
90          }                                                                    \
91       }                                                                       \
92    }
93 
94 #ifndef M_PI        /* yea, Solaris 5 */
95 #define M_PI        3.14159265358979323846264338327950288   /* pi */
96 #endif
97 #define RAD(deg)    M_PI*(deg/180.0)
98 
99 #include <std.c>
100 
101 /******************************************************************************
102  * Modifications:
103  *   Mark C. Miller, Tue Mar  9 17:16:17 PST 2010
104  *   Added explicit logic to set split vfd as an example of how to do it.
105  *   Added missing DBFreeFacelist() so valgrind reports no leaks.
106  *
107  *   Mark C. Miller, Wed Jul 14 15:43:10 PDT 2010
108  *   Changed name of 'example' option to 'split'. Added code to Unregister
109  *   option sets.
110  *
111  *****************************************************************************/
112 
113 int
main(int argc,char * argv[])114 main(int argc, char *argv[])
115 {
116     DBfile         *dbfile = NULL;
117     char const * const coordnames[3] = {"xcoords", "ycoords", "zcoords"};
118     double         *coords[3];
119     int             nodelist[8];
120     double          x[8], y[8], z[8];
121     int             shapesize[1];
122     int             shapecnt[1];
123     DBfacelist     *facelist = NULL;
124     int             matnos[1], matlist[1], dims[1];
125     int             i, j, len;
126     char            mesh_command[256];
127     float           rot1[4][4], rot2[4][4], final[4][4];
128     float           angle;
129     double          var[8];
130     int		    driver = DB_PDB;
131     int             setinf = 0;
132     int             setnan = 0;
133     char 	    *filename = "onehex.silo";
134     int             show_all_errors = FALSE;
135     int             append = FALSE;
136 
137     int alloc_inc, vfd, core_vfd;
138     char *mext, *rext;
139     int meta_opts_id, raw_opts_id, split_opts_id;
140     DBoptlist *core_opts = 0, *split_opts = 0;
141 
142     /* Parse command-line */
143     for (i=1; i<argc; i++) {
144 	if (!strncmp(argv[i], "DB_", 3)) {
145 	    driver = StringToDriver(argv[i]);
146 	} else if (!strcmp(argv[i], "split")) {
147 
148             /* set up the meta file options for core vfd with 1K alloc */
149             core_opts = DBMakeOptlist(10);
150 
151             /* indicate the vfd is core */
152             core_vfd = DB_H5VFD_CORE;
153             DBAddOption(core_opts, DBOPT_H5_VFD, &core_vfd);
154 
155             /* indicate the allocation increment is 1K */
156             alloc_inc = 1<<10;
157             DBAddOption(core_opts, DBOPT_H5_CORE_ALLOC_INC, &alloc_inc);
158 
159             /* register the core file options set with the library */
160             meta_opts_id = DBRegisterFileOptionsSet(core_opts);
161 
162             /* set up the raw file options */
163             /* We're using pre-defined default file options for the raw part */
164             raw_opts_id = DB_FILE_OPTS_H5_DEFAULT_SEC2;
165 
166             /* now, set up the split file options */
167             split_opts = DBMakeOptlist(10);
168 
169             /* indicate the vfd is split */
170             vfd = DB_H5VFD_SPLIT;
171             DBAddOption(split_opts, DBOPT_H5_VFD, &vfd);
172 
173             /* indicate the meta file options set */
174             DBAddOption(split_opts, DBOPT_H5_META_FILE_OPTS, &meta_opts_id);
175 
176             /* indicate the meta file extension */
177             mext = "silo-meta";
178             DBAddOption(split_opts, DBOPT_H5_META_EXTENSION, mext);
179 
180             /* indicate the raw file options set */
181             DBAddOption(split_opts, DBOPT_H5_RAW_FILE_OPTS, &raw_opts_id);
182 
183             /* indicate the raw file extension */
184             /* Note, this is NOT an extension but an sprintf name pattern */
185             rext = "silo_%s_raw.dat";
186             DBAddOption(split_opts, DBOPT_H5_RAW_EXTENSION, rext);
187 
188             /* register the split file options set with the library */
189             split_opts_id = DBRegisterFileOptionsSet(split_opts);
190 
191             /* set the 'driver' */
192             driver = DB_HDF5_OPTS(split_opts_id);
193 
194             /* DO NOT FREE THE ASSOCIATED OPTLIST UNTIL AFTER OPEN/CREATE */
195 
196 	} else if (!strcmp(argv[i], "append")) {
197             append = TRUE;
198 	} else if (!strcmp(argv[i], "inf")) {
199             setinf = 1;
200 	} else if (!strcmp(argv[i], "nan")) {
201             setnan = 1;
202         } else if (!strcmp(argv[i], "show-all-errors")) {
203             show_all_errors = 1;
204 	} else if (argv[i][0] != '\0') {
205 	    fprintf(stderr, "%s: ignored argument `%s'\n", argv[0], argv[i]);
206 	}
207     }
208 
209     DBShowErrors(show_all_errors?DB_ALL_AND_DRVR:DB_ABORT, NULL);
210     printf("Creating test file \"%s\".\n", filename);
211     if (append)
212     {
213         dbfile = DBOpen(filename, driver, DB_APPEND);
214         DBMkDir(dbfile, "dir1");
215         DBSetDir(dbfile, "dir1");
216     }
217     else
218         dbfile = DBCreate(filename, DB_CLOBBER, DB_LOCAL,
219             "This is a test to see how well this string displays in Qt widget in Silex\n"
220             "This is a test to see how well this string displays in Qt widget in Silex\n"
221             "This is a test to see how well this string displays in Qt widget in Silex\n"
222             "This is a test to see how well this string displays in Qt widget in Silex\n",
223             driver);
224 
225     /* Ok, now we can safely free the file options sets optlists */
226     if (core_opts)
227     {
228         DBUnregisterFileOptionsSet(meta_opts_id);
229         DBFreeOptlist(core_opts);
230     }
231     if (split_opts)
232     {
233         DBUnregisterFileOptionsSet(split_opts_id);
234         DBFreeOptlist(split_opts);
235     }
236 
237 
238     x[0] = 0; y[0] = 0; z[0] = 0;
239     x[1] = 1; y[1] = 0; z[1] = 0;
240     x[2] = 1; y[2] = 0; z[2] = 1;
241     x[3] = 0; y[3] = 0; z[3] = 1;
242     x[4] = 0; y[4] = 1; z[4] = 0;
243     x[5] = 1; y[5] = 1; z[5] = 0;
244     x[6] = 1; y[6] = 1; z[6] = 1;
245     x[7] = 0; y[7] = 1; z[7] = 1;
246 
247     coords[0] = x;
248     coords[1] = y;
249     coords[2] = z;
250 
251     angle = 45;
252     angle = M_PI*(angle/180.0);
253     matrix_assign(rot1,
254                   1, 0, 0, 0,
255                   0, cos(angle), sin(angle), 0,
256                   0, -sin(angle), cos(angle), 0,
257                   0, 0, 0, 1);
258     matrix_assign(rot2,
259                   cos(angle), 0, -sin(angle), 0,
260                   0, 1, 0, 0,
261                   sin(angle), 0, cos(angle), 0,
262                   0, 0, 0, 1);
263     matrix_mult(rot1, rot2, final);
264 
265     for (i = 0; i < 8; i++)
266     {
267         float           tx, ty,tz;
268 
269         tx = x[i]*final[IND(1,1)] + y[i]*final[IND(1,2)] + z[i]*final[IND(1,3)] + final[IND(1,4)];
270         ty = x[i]*final[IND(2,1)] + y[i]*final[IND(2,2)] + z[i]*final[IND(2,3)] + final[IND(2,4)];
271         tz = x[i]*final[IND(3,1)] + y[i]*final[IND(3,2)] + z[i]*final[IND(3,3)] + final[IND(3,4)];
272 
273         x[i] = tx;
274         y[i] = ty;
275         z[i] = tz;
276 
277         var[i] = x[i]+y[i]*z[i];
278     }
279 
280     DBPutUcdmesh(dbfile, "hex", 3, coordnames, coords, 8, 1, "zonelist",
281                  "facelist", DB_DOUBLE, NULL);
282 
283     matnos[0] = 1;
284     matlist[0] = 1;
285     dims[0] = 1;
286 
287     DBPutMaterial(dbfile, "mat", "hex", 1, matnos, matlist, dims,
288                   1, NULL, NULL, NULL, NULL, 0, DB_FLOAT, NULL);
289 
290     if (setinf)
291     {
292         double d = 1.0;
293         var[7] = d/(d-1.0);
294     }
295     if (setnan)
296     {
297 #ifndef _WIN32
298         var[0] = nan("");
299         if (var[0] == 0.0)
300 #endif
301             var[0] = sqrt((double)-1.0);
302     }
303     DBPutUcdvar1(dbfile, "v", "hex", var, 8, NULL, 0, DB_DOUBLE, DB_NODECENT, NULL);
304 
305     nodelist[0] = 0;
306     nodelist[1] = 1;
307     nodelist[2] = 2;
308     nodelist[3] = 3;
309     nodelist[4] = 4;
310     nodelist[5] = 5;
311     nodelist[6] = 6;
312     nodelist[7] = 7;
313 
314     shapesize[0] = 8;
315     shapecnt[0] = 1;
316 
317     DBSetDeprecateWarnings(0);
318     DBPutZonelist(dbfile, "zonelist", 1, 3, nodelist, 8, 0, shapesize,
319                   shapecnt, 1);
320     DBSetDeprecateWarnings(3);
321 
322     facelist = DBCalcExternalFacelist(nodelist, 8, 0, shapesize, shapecnt, 1,
323                                       NULL, 0);
324 
325     DBPutFacelist(dbfile, "facelist", facelist->nfaces, facelist->ndims,
326                facelist->nodelist, facelist->lnodelist, facelist->origin,
327                facelist->zoneno, facelist->shapesize, facelist->shapecnt,
328                   facelist->nshapes, facelist->types, facelist->typelist,
329                   facelist->ntypes);
330 
331     DBFreeFacelist(facelist);
332     sprintf(mesh_command, "mesh hex; contour v");
333     len = strlen(mesh_command) + 1;
334     DBWrite(dbfile, "_meshtvinfo", mesh_command, &len, 1, DB_CHAR);
335 
336     DBClose(dbfile);
337 
338     CleanupDriverStuff();
339 
340     return 0;
341 }
342