~ubuntu-branches/ubuntu/trusty/silo-llnl/trusty

« back to all changes in this revision

Viewing changes to tests/onehex.c

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2011-01-02 00:03:01 UTC
  • Revision ID: james.westby@ubuntu.com-20110102000301-9s2hfsjrkguz6h4r
Tags: upstream-4.8
ImportĀ upstreamĀ versionĀ 4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 IND(i,j) i-1][j-1
 
59
 
 
60
#define matrix_assign(matrix,a11,a12,a13,a14,a21,a22,a23,a24,a31,a32,a33,a34,a41,a42,a43,a44)         \
 
61
   {                                                                          \
 
62
   matrix [IND(1,1)] = a11 ;                                              \
 
63
   matrix [IND(1,2)] = a12 ;                                              \
 
64
   matrix [IND(1,3)] = a13 ;                                              \
 
65
   matrix [IND(1,4)] = a14 ;                                              \
 
66
   matrix [IND(2,1)] = a21 ;                                              \
 
67
   matrix [IND(2,2)] = a22 ;                                              \
 
68
   matrix [IND(2,3)] = a23 ;                                              \
 
69
   matrix [IND(2,4)] = a24 ;                                              \
 
70
   matrix [IND(3,1)] = a31 ;                                              \
 
71
   matrix [IND(3,2)] = a32 ;                                              \
 
72
   matrix [IND(3,3)] = a33 ;                                              \
 
73
   matrix [IND(3,4)] = a34 ;                                              \
 
74
   matrix [IND(4,1)] = a41 ;                                              \
 
75
   matrix [IND(4,2)] = a42 ;                                              \
 
76
   matrix [IND(4,3)] = a43 ;                                              \
 
77
   matrix [IND(4,4)] = a44 ;                                              \
 
78
   }
 
79
 
 
80
#define matrix_mult(matrixa, matrixb, matrixc)                                \
 
81
   {                                                                          \
 
82
   for (i = 1; i < 5; i++) {                                                  \
 
83
      for (j = 1; j < 5; j++) {                                               \
 
84
         matrixc [IND(i,j)] = matrixa [IND(i,1)] * matrixb [IND(1,j)] + \
 
85
                                  matrixa [IND(i,2)] * matrixb [IND(2,j)] + \
 
86
                                  matrixa [IND(i,3)] * matrixb [IND(3,j)] + \
 
87
                                  matrixa [IND(i,4)] * matrixb [IND(4,j)] ; \
 
88
         }                                                                    \
 
89
      }                                                                       \
 
90
   }
 
91
 
 
92
#ifndef M_PI        /* yea, Solaris 5 */
 
93
#define M_PI        3.14159265358979323846264338327950288   /* pi */
 
94
#endif
 
95
#define RAD(deg)    M_PI*(deg/180.0)
 
96
 
 
97
#include <std.c>
 
98
 
 
99
/******************************************************************************
 
100
 * Modifications:
 
101
 *   Mark C. Miller, Tue Mar  9 17:16:17 PST 2010
 
102
 *   Added explicit logic to set split vfd as an example of how to do it.
 
103
 *   Added missing DBFreeFacelist() so valgrind reports no leaks.
 
104
 *
 
105
 *   Mark C. Miller, Wed Jul 14 15:43:10 PDT 2010
 
106
 *   Changed name of 'example' option to 'split'. Added code to Unregister
 
107
 *   option sets.
 
108
 * 
 
109
 *****************************************************************************/
 
110
 
 
111
int
 
112
main(int argc, char *argv[])
 
113
{
 
114
    DBfile         *dbfile = NULL;
 
115
    char           *coordnames[3];
 
116
    float          *coords[3];
 
117
    int             nodelist[8];
 
118
    float           x[8], y[8], z[8];
 
119
    int             shapesize[1];
 
120
    int             shapecnt[1];
 
121
    DBfacelist     *facelist = NULL;
 
122
    int             matnos[1], matlist[1], dims[1];
 
123
    int             i, j, len;
 
124
    char            mesh_command[256];
 
125
    float           rot1[4][4], rot2[4][4], final[4][4];
 
126
    float           angle;
 
127
    float           var[8];
 
128
    int             driver = DB_PDB;
 
129
    int             setinf = 0;
 
130
    int             setnan = 0;
 
131
    char            *filename = "onehex.silo";
 
132
    int             show_all_errors = FALSE;
 
133
    int             append = FALSE;
 
134
 
 
135
    int alloc_inc, vfd, core_vfd;
 
136
    char *mext, *rext;
 
137
    int meta_opts_id, raw_opts_id, split_opts_id;
 
138
    DBoptlist *core_opts = 0, *split_opts = 0;
 
139
 
 
140
    /* Parse command-line */
 
141
    for (i=1; i<argc; i++) {
 
142
        if (!strncmp(argv[i], "DB_", 3)) {
 
143
            driver = StringToDriver(argv[i]);
 
144
        } else if (!strcmp(argv[i], "split")) {
 
145
 
 
146
            /* set up the meta file options for core vfd with 1K alloc */
 
147
            core_opts = DBMakeOptlist(10);
 
148
 
 
149
            /* indicate the vfd is core */
 
150
            core_vfd = DB_H5VFD_CORE;
 
151
            DBAddOption(core_opts, DBOPT_H5_VFD, &core_vfd);
 
152
 
 
153
            /* indicate the allocation increment is 1K */
 
154
            alloc_inc = 1<<10;
 
155
            DBAddOption(core_opts, DBOPT_H5_CORE_ALLOC_INC, &alloc_inc);
 
156
 
 
157
            /* register the core file options set with the library */
 
158
            meta_opts_id = DBRegisterFileOptionsSet(core_opts); 
 
159
           
 
160
            /* set up the raw file options */
 
161
            /* We're using pre-defined default file options for the raw part */
 
162
            raw_opts_id = DB_FILE_OPTS_H5_DEFAULT_SEC2;
 
163
 
 
164
            /* now, set up the split file options */
 
165
            split_opts = DBMakeOptlist(10);
 
166
 
 
167
            /* indicate the vfd is split */
 
168
            vfd = DB_H5VFD_SPLIT;
 
169
            DBAddOption(split_opts, DBOPT_H5_VFD, &vfd);
 
170
 
 
171
            /* indicate the meta file options set */
 
172
            DBAddOption(split_opts, DBOPT_H5_META_FILE_OPTS, &meta_opts_id);
 
173
 
 
174
            /* indicate the meta file extension */
 
175
            mext = "silo-meta";
 
176
            DBAddOption(split_opts, DBOPT_H5_META_EXTENSION, mext);
 
177
 
 
178
            /* indicate the raw file options set */
 
179
            DBAddOption(split_opts, DBOPT_H5_RAW_FILE_OPTS, &raw_opts_id);
 
180
 
 
181
            /* indicate the raw file extension */
 
182
            /* Note, this is NOT an extension but an sprintf name pattern */
 
183
            rext = "silo_%s_raw.dat";
 
184
            DBAddOption(split_opts, DBOPT_H5_RAW_EXTENSION, rext);
 
185
 
 
186
            /* register the split file options set with the library */
 
187
            split_opts_id = DBRegisterFileOptionsSet(split_opts); 
 
188
 
 
189
            /* set the 'driver' */
 
190
            driver = DB_HDF5_OPTS(split_opts_id);
 
191
 
 
192
            /* DO NOT FREE THE ASSOCIATED OPTLIST UNTIL AFTER OPEN/CREATE */
 
193
 
 
194
        } else if (!strcmp(argv[i], "append")) {
 
195
            append = TRUE;
 
196
        } else if (!strcmp(argv[i], "inf")) {
 
197
            setinf = 1;
 
198
        } else if (!strcmp(argv[i], "nan")) {
 
199
            setnan = 1;
 
200
        } else if (!strcmp(argv[i], "show-all-errors")) {
 
201
            show_all_errors = 1;
 
202
        } else if (argv[i][0] != '\0') {
 
203
            fprintf(stderr, "%s: ignored argument `%s'\n", argv[0], argv[i]);
 
204
        }
 
205
    }
 
206
    
 
207
    DBShowErrors(show_all_errors?DB_ALL_AND_DRVR:DB_ABORT, NULL);
 
208
    printf("Creating test file \"%s\".\n", filename);
 
209
    if (append)
 
210
    {
 
211
        dbfile = DBOpen(filename, driver, DB_APPEND);
 
212
        DBMkDir(dbfile, "dir1");
 
213
        DBSetDir(dbfile, "dir1");
 
214
    }
 
215
    else
 
216
        dbfile = DBCreate(filename, DB_CLOBBER, DB_LOCAL, "3D ucd hex", driver);
 
217
 
 
218
    /* Ok, now we can safely free the file options sets optlists */
 
219
    if (core_opts)
 
220
    {
 
221
        DBUnregisterFileOptionsSet(meta_opts_id);
 
222
        DBFreeOptlist(core_opts);
 
223
    }
 
224
    if (split_opts)
 
225
    {
 
226
        DBUnregisterFileOptionsSet(split_opts_id);
 
227
        DBFreeOptlist(split_opts);
 
228
    }
 
229
 
 
230
    coordnames[0] = "xcoords";
 
231
    coordnames[1] = "ycoords";
 
232
    coordnames[2] = "zcoords";
 
233
 
 
234
    x[0] = 0; y[0] = 0; z[0] = 0;
 
235
    x[1] = 1; y[1] = 0; z[1] = 0;
 
236
    x[2] = 1; y[2] = 0; z[2] = 1;
 
237
    x[3] = 0; y[3] = 0; z[3] = 1;
 
238
    x[4] = 0; y[4] = 1; z[4] = 0;
 
239
    x[5] = 1; y[5] = 1; z[5] = 0;
 
240
    x[6] = 1; y[6] = 1; z[6] = 1;
 
241
    x[7] = 0; y[7] = 1; z[7] = 1;
 
242
 
 
243
    coords[0] = x;
 
244
    coords[1] = y;
 
245
    coords[2] = z;
 
246
 
 
247
    angle = 45;
 
248
    angle = M_PI*(angle/180.0);
 
249
    matrix_assign(rot1,
 
250
                  1, 0, 0, 0,
 
251
                  0, cos(angle), sin(angle), 0,
 
252
                  0, -sin(angle), cos(angle), 0,
 
253
                  0, 0, 0, 1);
 
254
    matrix_assign(rot2,
 
255
                  cos(angle), 0, -sin(angle), 0,
 
256
                  0, 1, 0, 0,
 
257
                  sin(angle), 0, cos(angle), 0,
 
258
                  0, 0, 0, 1);
 
259
    matrix_mult(rot1, rot2, final);
 
260
 
 
261
    for (i = 0; i < 8; i++)
 
262
    {
 
263
        float           tx, ty,tz;
 
264
 
 
265
        tx = x[i]*final[IND(1,1)] + y[i]*final[IND(1,2)] + z[i]*final[IND(1,3)] + final[IND(1,4)];
 
266
        ty = x[i]*final[IND(2,1)] + y[i]*final[IND(2,2)] + z[i]*final[IND(2,3)] + final[IND(2,4)];
 
267
        tz = x[i]*final[IND(3,1)] + y[i]*final[IND(3,2)] + z[i]*final[IND(3,3)] + final[IND(3,4)];
 
268
 
 
269
        x[i] = tx;
 
270
        y[i] = ty;
 
271
        z[i] = tz;
 
272
 
 
273
        var[i] = x[i]+y[i]*z[i];
 
274
    }
 
275
 
 
276
    DBPutUcdmesh(dbfile, "hex", 3, coordnames, coords, 8, 1, "zonelist",
 
277
                 "facelist", DB_FLOAT, NULL);
 
278
 
 
279
    matnos[0] = 1;
 
280
    matlist[0] = 1;
 
281
    dims[0] = 1;
 
282
 
 
283
    DBPutMaterial(dbfile, "mat", "hex", 1, matnos, matlist, dims,
 
284
                  1, NULL, NULL, NULL, NULL, 0, DB_FLOAT, NULL);
 
285
 
 
286
    if (setinf)
 
287
    {
 
288
        double d = 2.0;
 
289
        var[7] = FLT_MAX*d; 
 
290
    }
 
291
    if (setnan)
 
292
    {
 
293
        double d = -1.0;
 
294
        var[0] = sqrt(d);
 
295
    }
 
296
    DBPutUcdvar1(dbfile, "v", "hex", var, 8, NULL, 0, DB_FLOAT, DB_NODECENT,
 
297
                 NULL);
 
298
 
 
299
    nodelist[0] = 0;
 
300
    nodelist[1] = 1;
 
301
    nodelist[2] = 2;
 
302
    nodelist[3] = 3;
 
303
    nodelist[4] = 4;
 
304
    nodelist[5] = 5;
 
305
    nodelist[6] = 6;
 
306
    nodelist[7] = 7;
 
307
 
 
308
    shapesize[0] = 8;
 
309
    shapecnt[0] = 1;
 
310
 
 
311
    DBSetDeprecateWarnings(0);
 
312
    DBPutZonelist(dbfile, "zonelist", 1, 3, nodelist, 8, 0, shapesize,
 
313
                  shapecnt, 1);
 
314
    DBSetDeprecateWarnings(3);
 
315
 
 
316
    facelist = DBCalcExternalFacelist(nodelist, 8, 0, shapesize, shapecnt, 1,
 
317
                                      NULL, 0);
 
318
 
 
319
    DBPutFacelist(dbfile, "facelist", facelist->nfaces, facelist->ndims,
 
320
               facelist->nodelist, facelist->lnodelist, facelist->origin,
 
321
               facelist->zoneno, facelist->shapesize, facelist->shapecnt,
 
322
                  facelist->nshapes, facelist->types, facelist->typelist,
 
323
                  facelist->ntypes);
 
324
 
 
325
    DBFreeFacelist(facelist);
 
326
    sprintf(mesh_command, "mesh hex; contour v");
 
327
    len = strlen(mesh_command) + 1;
 
328
    DBWrite(dbfile, "_meshtvinfo", mesh_command, &len, 1, DB_CHAR);
 
329
 
 
330
    DBClose(dbfile);
 
331
 
 
332
    CleanupDriverStuff();
 
333
 
 
334
    return 0;
 
335
}