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.
37
38 Neither the United States Government nor Lawrence Livermore National
39 Security, LLC nor any of their employees, makes any warranty, express
40 or implied, or assumes any liability or responsibility for the
41 accuracy, completeness, or usefulness of any information, apparatus,
42 product, or process disclosed, or represents that its use would not
43 infringe privately-owned rights.
44
45 Any reference herein to any specific commercial products, process, or
46 services by trade name, trademark, manufacturer or otherwise does not
47 necessarily constitute or imply its endorsement, recommendation, or
48 favoring by the United States Government or Lawrence Livermore
49 National Security, LLC. The views and opinions of authors expressed
50 herein do not necessarily state or reflect those of the United States
51 Government or Lawrence Livermore National Security, LLC, and shall not
52 be used for advertising or product endorsement purposes.
53 */
54
55 #include <stdlib.h>
56 #include <string.h>
57 #include "silo.h"
58 #include <std.c>
59
60 static int build_ucd3(DBfile *dbfile, char *name);
61
62 /***********************************************************************
63 * Program
64 *
65 * ucdsamp3
66 *
67 * Purpose
68 *
69 * Sample program illustrating use of SILO for writing 3D
70 * unstructured cell data.
71 *
72 * Modifications
73 *
74 * Robb Matzke, 1999-04-09
75 * Added argument parsing to control the driver which is used.
76 ***********************************************************************/
77 int
main(int argc,char * argv[])78 main(int argc, char *argv[])
79 {
80 DBfile *dbfile;
81 int i, driver=DB_PDB;
82 char *filename="ucdsamp3.pdb";
83 int show_all_errors = FALSE;
84
85 /* Parse command-line */
86 for (i=1; i<argc; i++) {
87 if (!strncmp(argv[i], "DB_PDB", 6)) {
88 driver = StringToDriver(argv[i]);
89 filename = "ucdsamp3.pdb";
90 } else if (!strncmp(argv[i], "DB_HDF5", 7)) {
91 driver = StringToDriver(argv[i]);
92 filename = "ucdsamp3.h5";
93 } else if (!strcmp(argv[i], "show-all-errors")) {
94 show_all_errors = 1;
95 } else if (argv[i][0] != '\0') {
96 fprintf(stderr, "%s: ignored argument `%s'\n", argv[0], argv[i]);
97 }
98 }
99
100 DBShowErrors(show_all_errors?DB_ALL_AND_DRVR:DB_ALL, NULL);
101
102 dbfile = DBCreate(filename, 0, DB_LOCAL, "ucd test file", driver);
103 printf("Creating file: '%s'...\n", filename);
104 build_ucd3(dbfile, "mesh1");
105 DBClose(dbfile);
106
107 CleanupDriverStuff();
108 exit(0);
109 }
110 /*----------------------------------------------------------------------
111 * Routine build_ucd3
112 *
113 * Purpose
114 *
115 * Build a 3D ucd-mesh, ucd-var, facelist and zonelist, and return
116 * the mesh ID.
117 *
118 * Arguments
119 * name Name to assign mesh.
120 *
121 * Modifications
122 *
123 * Lisa J. Roberts, Fri Apr 7 10:48:31 PDT 2000
124 * Changed the prototype to ANSI standard and explicitly indicated
125 * the function returns an int. Got rid of flid, zlid, varid, i,
126 * ln, dbid, and matid, which were unused.
127 *
128 *--------------------------------------------------------------------*/
129 static int
build_ucd3(DBfile * dbfile,char * name)130 build_ucd3(DBfile *dbfile, char *name)
131 {
132
133 #define NZONES 3 /* Number of zones (elements) */
134 #define NFACES 14 /* Number of external faces */
135 #define NNODES 16 /* Number of mesh nodes */
136 #define NZSHAPES 1 /* Number of zone shapes */
137 #define NFSHAPES 1 /* Number of face shapes */
138 #define NFTYPES 0 /* Number of face types */
139 #define LZNODELIST 24 /* Length of zonelist nodelist */
140 #define LFNODELIST 56 /* Length of facelist nodelist */
141
142 /* Misc variables */
143 int meshid;
144 int fshapesize, fshapecnt, zshapesize, zshapecnt;
145 int idatatype;
146 int dims;
147 int inode, iface, izone, ifl, izl, nface = 0;
148 #if 0
149 int mixlen = 0;
150 int tcycle = 200;
151 double ttime = 2.345;
152 float *vars[3];
153 char *varnames[3];
154 DBfacelist *fl;
155 #endif
156 float *coords[3];
157 char *coordnames[3];
158
159 /* Coordinates */
160 float x[NNODES];
161 float y[NNODES];
162 float z[NNODES];
163
164 /* Nodal quantities */
165 static float u[NNODES] =
166 {0., 0., 0., 0., .1, .1, .1, .1, .2, .2, .2, .2, .3, .3, .3, .3};
167 #if 0
168 static float v[NNODES] =
169 {0., 0., 0., 0., .1, .1, .1, .1, .2, .2, .2, .2, .3, .3, .3, .3};
170 static float w[NNODES] =
171 {0., 0., 0., 0., .1, .1, .1, .1, .2, .2, .2, .2, .3, .3, .3, .3};
172 #endif
173
174 /* Zonal quantities */
175 static float d[NZONES] =
176 {2., 4., 6.};
177
178 /* Material data */
179 static int matlist[NZONES] =
180 {1, 1, 2};
181 static int matnos[2] =
182 {1, 2};
183
184 /* Facelist data */
185 int fzoneno[NFACES];
186 int fnodelist[LFNODELIST];
187
188 /* Zonelist data */
189 int znodelist[LZNODELIST];
190
191 fshapesize = 4;
192 fshapecnt = NFACES;
193 zshapesize = 8;
194 zshapecnt = NZONES;
195
196 idatatype = DB_FLOAT;
197 dims = NZONES;
198
199 /*--------------------------------------------------
200 * Compute coordinate values, zonelist values, and
201 * facelist values for a sample 3D mesh.
202 *-------------------------------------------------*/
203 inode = 0;
204 for (izone = 0; izone <= NZONES; izone++) {
205 x[inode++] = 0.;
206 x[inode++] = 1.;
207 x[inode++] = 1.;
208 x[inode++] = 0.;
209 inode -= 4;
210 y[inode++] = (float)izone;
211 y[inode++] = (float)izone;
212 y[inode++] = (float)izone;
213 y[inode++] = (float)izone;
214 inode -= 4;
215 z[inode++] = 0.;
216 z[inode++] = 0.;
217 z[inode++] = 1.;
218 z[inode++] = 1.;
219 }
220 izl = 0;
221 for (izone = 0; izone < NZONES; izone++) {
222 for (inode = 0; inode < 4; inode++) {
223 znodelist[izl++] = izone * 4 + inode;
224 }
225 for (inode = 0; inode < 4; inode++) {
226 znodelist[izl++] = (izone + 1) * 4 + inode;
227 }
228 }
229 ifl = 0;
230 for (izone = 0; izone < NZONES; izone++) {
231 for (iface = 0; iface < 3; iface++) {
232 fnodelist[ifl++] = izone * 4 + iface;
233 fnodelist[ifl++] = izone * 4 + iface + 1;
234 fnodelist[ifl++] = izone * 4 + iface + 5;
235 fnodelist[ifl++] = izone * 4 + iface + 4;
236 }
237 fnodelist[ifl++] = izone * 4 + 3;
238 fnodelist[ifl++] = izone * 4;
239 fnodelist[ifl++] = izone * 4 + 4;
240 fnodelist[ifl++] = izone * 4 + 7;
241
242 for (iface = 0; iface < 4; iface++)
243 fzoneno[nface++] = izone;
244 }
245 fnodelist[ifl++] = 0;
246 fnodelist[ifl++] = 1;
247 fnodelist[ifl++] = 2;
248 fnodelist[ifl++] = 3;
249 fzoneno[nface++] = 0;
250 fnodelist[ifl++] = 12;
251 fnodelist[ifl++] = 13;
252 fnodelist[ifl++] = 14;
253 fnodelist[ifl++] = 15;
254 fzoneno[nface++] = NZONES - 1;
255
256 #if 1
257 /*--------------------------------------------------
258 * Write out the external facelist we defined
259 * above.
260 *-------------------------------------------------*/
261 DBPutFacelist(dbfile, "fl", NFACES, 3, fnodelist, LFNODELIST, 0,
262 fzoneno, &fshapesize, &fshapecnt, NFSHAPES,
263 NULL, NULL, 0);
264 #else
265 /*--------------------------------------------------
266 * Calculate an external facelist from the zonelist
267 * information. Write it out.
268 *-------------------------------------------------*/
269 fl = (DBfacelist *) DBCalcExternalFacelist(znodelist, NNODES, 0,
270 &zshapesize, &zshapecnt, NZSHAPES,
271 matlist, 2);
272 DBPutFacelist(dbfile, "fl", fl->nfaces, 3,
273 fl->nodelist, fl->lnodelist, 0,
274 fl->zoneno, fl->shapesize, fl->shapecnt,
275 fl->nshapes, NULL, NULL, 0);
276 #endif
277
278 /*--------------------------------------------------
279 * Write out the zonelist.
280 *-------------------------------------------------*/
281 DBSetDeprecateWarnings(0);
282 DBPutZonelist(dbfile, "zl", NZONES, 3, znodelist, LZNODELIST, 0,
283 &zshapesize, &zshapecnt, NZSHAPES);
284 DBSetDeprecateWarnings(3);
285
286 /*--------------------------------------------------
287 * Write out the mesh.
288 *-------------------------------------------------*/
289 coords[0] = x;
290 coords[1] = y;
291 coords[2] = z;
292 coordnames[0] = "X";
293 coordnames[1] = "Y";
294 coordnames[2] = "Z";
295
296 meshid = DBPutUcdmesh(dbfile, name, 3, (DBCAS_t) coordnames,
297 coords, NNODES, NZONES, "zl", "fl", DB_FLOAT, NULL);
298
299 /*--------------------------------------------------
300 * Write out the material data.
301 *-------------------------------------------------*/
302 DBPutMaterial(dbfile, "material", name, 2, matnos, matlist,
303 &dims, 1, NULL, NULL, NULL, NULL, 0, DB_FLOAT,
304 NULL);
305
306 /*--------------------------------------------------
307 * Write out a zonal variable.
308 *-------------------------------------------------*/
309 DBPutUcdvar1(dbfile, "d", name, d, NZONES,
310 NULL, 0, idatatype, DB_ZONECENT, NULL);
311
312 /*--------------------------------------------------
313 * Write out a nodal variable.
314 *-------------------------------------------------*/
315 DBPutUcdvar1(dbfile, "u", name, u, NNODES,
316 NULL, 0, idatatype, DB_NODECENT, NULL);
317
318 return (meshid);
319 }
320