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 #ifdef PDB_LITE
53 #include <lite_score.h>
54 #include <lite_pdb.h>
55 #else
56 #include <score.h>
57 #include <pdb.h>
58 #endif
59 
60 #include <stdlib.h>
61 #include <string.h>
62 
63 
64 typedef struct {
65     char          *name;
66     char          *type;        /* Type of group/object */
67     char         **comp_names;  /* Array of component names */
68     char         **pdb_names;   /* Array of internal (PDB) variable names */
69     int            ncomponents; /* Number of components */
70 } Group;
71 
72 void CreateFile (char *filename, char *name, char *type, int num,
73      char **comp_names, char **pdb_names);
74 void ReadFile (char *filename, char *name);
75 
76 char *comp_names[] = {"coord0",
77                       "coord1",
78                       "coord2",
79                       "min_extents",
80                       "max_extents",
81                       "facelist",
82                       "zonelist",
83                       "ndims",
84                       "nnodes",
85                       "nzones",
86                       "facetype",
87                       "cycle",
88                       "coord_sys",
89                       "planar",
90                       "origin",
91                       "datatype",
92                       "time"};
93 char *pdb_names[]  = {"/mesh_coord0",
94                       "/mesh_coord1",
95                       "/mesh_coord2",
96                       "/mesh_min_extents",
97                       "/mesh_max_extents",
98                       "'<s>fl'",
99                       "'<s>zl'",
100                       "'<i>3'",
101                       "'<i>1093'",
102                       "'<i>1200'",
103                       "'<i>100'",
104                       "'<i>0'",
105                       "'<i>124'",
106                       "'<i>124'",
107                       "'<i>0'",
108                       "'<i>20'",
109                       "/time"};
110 
111 float coord0_data[20] = { 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,
112                          10., 11., 12., 13., 14., 15., 16., 17., 18., 19.};
113 
114 
main()115 int main ()
116 {
117 
118     CreateFile("abc.pdb", "mesh", "ucdmesh", 17, comp_names, pdb_names);
119 
120     ReadFile("abc.pdb", "mesh");
121 
122     return 0;
123 }
124 
125 void
ReadFile(char * filename,char * name)126 ReadFile (char *filename, char *name)
127 {
128     int       i;
129     PDBfile   *file=NULL;
130     Group     *group=NULL;
131     char      str[256];
132     char      *cptr;
133     char      **ccptr;
134     char      carray[256];
135     float     farray[20];
136     float     fval;
137 
138     /*
139      * Open the file. Test additional open mode chars
140      */
141     if ((file = PD_open(filename, "rli")) == NULL)
142     {
143         printf("Error opening file.\n");
144         exit(-1);
145     }
146 
147     /*
148      * Read some variables and print their results.
149      */
150     printf("reading group\n");
151     PD_read(file, name, &group);
152     printf("group->name = %s\n", group->name);
153     printf("group->type = %s\n", group->type);
154     printf("group->ncomponents = %d\n", group->ncomponents);
155     printf("group->comp_names[0] = %s\n", group->comp_names[0]);
156     printf("group->pdb_names[0] = %s\n", group->pdb_names[0]);
157     for (i = 0; i < 17; i++)
158     {
159         SFREE(group->comp_names[i]);
160         SFREE(group->pdb_names[i]);
161     }
162     SFREE(group->comp_names);
163     SFREE(group->pdb_names);
164     SFREE(group->name);
165     SFREE(group->type);
166     SFREE(group);
167 
168     printf("reading group->name\n");
169     sprintf(str, "%s->name", name);
170     PD_read(file, str, &cptr);
171     printf("%s->name = %s\n", name, cptr);
172     SFREE(cptr);
173 
174     printf("reading group->comp_names\n");
175     sprintf(str, "%s->comp_names", name);
176     PD_read(file, str, &ccptr);
177     printf("%s->comp_names[0] = %s\n", name, ccptr[0]);
178     for (i = 0; i < 17; i++)
179        SFREE(ccptr[i]);
180     SFREE(ccptr);
181 
182     printf("reading group->comp_names[1]\n");
183     sprintf(str, "%s->comp_names[1]", name);
184     PD_read(file, str, &cptr);
185     printf("%s->comp_names[1] = %s\n", name, cptr);
186     SFREE(cptr);
187 
188     printf("reading group->comp_names[1][2:4]\n");
189     sprintf(str, "%s->comp_names[1][2:4]", name);
190     PD_read(file, str, &carray);
191     carray[3] = '\0';
192     printf("%s->comp_names[1][2:4] = %s\n", name, carray);
193 
194     printf("reading coord0\n");
195     PD_read(file, "coord0", farray);
196     printf("coord0[4] = %g\n", farray[4]);
197 
198     printf("reading coord0(0, 3)\n");
199     PD_read(file, "coord0(0, 3)", &fval);
200     printf("coord0(0, 3) = %g\n", fval);
201 
202     /*
203      * Close the file.
204      */
205     PD_close(file);
206 
207     return;
208 }
209 
210 
211 /*
212  *  Mark C. Miller, Thu Jan  7 10:36:24 PST 2010
213  *  Replaced safe_strdup with MAKE_N as PDB's mem allocation
214  *  routines are required for this code.
215  */
216 void
CreateFile(char * filename,char * name,char * type,int num,char ** comp_names,char ** pdb_names)217 CreateFile (char *filename, char *name, char *type, int num,
218             char **comp_names, char **pdb_names)
219 {
220     int       i;
221     int       *null_ptr;
222     PDBfile   *file=NULL;
223     Group     *group=NULL;
224     float     *coord0;
225     long      ind[6];
226 
227     /*
228      * Create a file.
229      */
230     if ((file = PD_create(filename)) == NULL)
231     {
232         printf("Error creating file.\n");
233         exit(-1);
234     }
235 
236     /*
237      * Define the group to PDB.
238      */
239     null_ptr  = FMAKE(int, "NULL");
240     *null_ptr = 0;
241 
242     if (PD_defstr(file, "Group",
243                        "char    *name",
244                        "char    *type",
245                        "char    **comp_names",
246                        "char    **pdb_names",
247                        "integer ncomponents",
248                        null_ptr) == NULL)
249     {
250         printf("Error defining Group structure.\n");
251         exit(-1);
252     }
253 
254     SFREE(null_ptr);
255 
256     /*
257      * Allocate the group structure and populate it.
258      */
259     group = MAKE_N(Group, 1);
260     group->comp_names = MAKE_N(char *, num);
261     group->pdb_names = MAKE_N(char *, num);
262     for (i = 0; i < num; i++) {
263         group->comp_names[i] = MAKE_N(char, strlen(comp_names[i])+1);
264         strcpy(group->comp_names[i], comp_names[i]);
265         group->pdb_names[i] = MAKE_N(char, strlen(pdb_names[i])+1);
266         strcpy(group->pdb_names[i], pdb_names[i]);
267     }
268     group->type = MAKE_N(char, strlen(type)+1);
269     strcpy(group->type, type);
270     group->name = MAKE_N(char, strlen(name)+1);
271     strcpy(group->name, name);
272     group->ncomponents = num;
273 
274     /*
275      * Write the group.
276      */
277     if (PD_write(file, name, "Group *", &group) == 0)
278     {
279         printf("Error writing group.\n");
280         exit(-1);
281     }
282 
283     /*
284      * Free the group structure.
285      */
286     for (i = 0; i < group->ncomponents; i++)
287     {
288         SFREE(group->comp_names[i]);
289         SFREE(group->pdb_names[i]);
290     }
291     SFREE(group->comp_names);
292     SFREE(group->pdb_names);
293     SFREE(group->name);
294     SFREE(group->type);
295     SFREE(group);
296 
297     /*
298      * Write an array.
299      */
300     coord0 = MAKE_N(float, 20);
301     for (i = 0; i < 20; i++) coord0[i] = coord0_data[i];
302     ind[0] = 0;
303     ind[1] = 1;
304     ind[2] = 1;
305     ind[3] = 0;
306     ind[4] = 9;
307     ind[5] = 1;
308     if (PD_write_alt(file, "coord0", "float", coord0, 2, ind) == 0)
309     {
310         printf("Error writing array.\n");
311         exit(-1);
312     }
313 
314     SFREE(coord0);
315 
316     /*
317      * Close the file.
318      */
319     PD_close(file);
320 
321     return;
322 }
323