Debian Codegraph @

src/read_data_silo_utils.c

raw
1 /*
2 * This subroutine performs the calls to the SILO library for the
3 * SILO data read
4 *
5 * We have to do it this way as the SILO read interface for Fortran
6 * is incomplete
7 *
8 */
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <string.h>
12 #include <silo.h>
13 static int debug = 0;
14
15 void set_blocklabel(int *icol, char *name);
16 void read_silo_data_fromc(int *icol, int *npartoftypei, double temparr[*npartoftypei],int *itype);
17 void read_silo_header(const char *filename,
18 int *npart,
19 int *ncol,
20 int *ndim,
21 int *ndimV,
22 double *time,
23 int *ierr)
24 {
25 *npart = 0;
26 *ierr = 0;
27 *ncol = 0;
28 *time = 0.;
29 *npart = 0;
30 DBfile *silofile = DBOpen(filename, DB_UNKNOWN, DB_READ);
31 if (!silofile) {
32 *ierr = 1;
33 return;
34 }
35
36 if (!DBVersionGEFileVersion(silofile)) {
37 const char *siloversion = DBFileVersion(silofile);
38 printf(" WARNING! File was created with newer version (v%s) of silo library\n",siloversion);
39 }
40
41 if (!DBInqFileHasObjects(silofile)) {
42 printf(" ERROR: silo file %s does not appear to contain any objects\n",filename);
43 *ierr = 2;
44 return;
45 }
46
47 DBtoc *toc = DBGetToc(silofile);
48 if (debug) {
49 printf(" DEBUG: File contains:\n %i curves\n %i multimesh\n %i nmultimeshadj\n %i multivar\n", \
50 toc->ncurve,toc->nmultimesh,toc->nmultimeshadj,toc->nmultivar);
51 printf(" %i multimat\n %i multimatspecies\n %i csgmesh\n %i csgvar\n", \
52 toc->nmultimat,toc->nmultimatspecies,toc->ncsgmesh,toc->ncsgvar);
53 printf(" %i defvars\n %i qmesh\n %i qvar\n %i ucdmesh\n %i ucdvar\n", \
54 toc->ndefvars,toc->nqmesh,toc->nqvar,toc->nucdmesh,toc->nucdvar);
55 printf(" %i ptmesh\n %i ptvar\n %i mat\n %i matspecies\n %i var\n %i obj\n", \
56 toc->nptmesh,toc->nptvar,toc->nmat,toc->nmatspecies,toc->nvar,toc->nobj);
57 printf(" %i dir\n %i array\n %i mrgtree\n %i groupelmap\n %i mrgvar\n", \
58 toc->ndir,toc->narray,toc->nmrgtree,toc->ngroupelmap,toc->nmrgvar);
59 }
60 int nptmesh = toc->nptmesh;
61 if (nptmesh <= 0) {
62 printf(" ERROR: silo file %s does not appear to contain any point meshes\n",filename);
63 *ierr = 3;
64 return;
65 }
66 int i;
67 for (i=0;i<nptmesh;i++) {
68 if (i > 0) {
69 printf(" WARNING: IGNORNING ptmesh #%i (%s)\n",i+1,toc->ptmesh_names[i]);
70 }
71 }
72
73 /* open the first point mesh and get info */
74 DBpointmesh *my_ptmesh = DBGetPointmesh(silofile,toc->ptmesh_names[0]);
75 if (!my_ptmesh) {
76 printf(" ERROR reading point mesh %s\n",toc->ptmesh_names[0]);
77 *ierr = 4;
78 return;
79 } else {
80 printf(" Reading point mesh %s\n",toc->ptmesh_names[0]);
81 *time = my_ptmesh->dtime;
82 *npart = my_ptmesh->nels;
83 *ndim = my_ptmesh->ndims;
84 *ndimV = *ndim;
85 if (debug) {
86 printf(" Got labels = %s %s %s \n",\
87 my_ptmesh->labels[0],my_ptmesh->labels[1],my_ptmesh->labels[2]);
88 printf(" Got title = %s \n",my_ptmesh->title);
89 printf(" Got units = %s %s %s \n",\
90 my_ptmesh->units[0],my_ptmesh->units[1],my_ptmesh->units[2]);
91 printf(" max_extents = %f %f %f\n",\
92 my_ptmesh->max_extents[0],my_ptmesh->max_extents[1],my_ptmesh->max_extents[2]);
93 }
94 DBFreePointmesh(my_ptmesh);
95 }
96
97 /* Read the other point variables */
98 int nptvar = toc->nptvar;
99 if (nptvar <= 0) {
100 printf(" WARNING: silo file %s does not appear to contain any point variables\n",filename);
101 }
102 *ncol = *ndim + nptvar;
103 if (*ncol <= 0) {
104 *ierr = 4;
105 printf(" ERROR: ncol <= 0 from silo header\n");
106 return;
107 }
108 DBClose(silofile);
109 }
110
111 void read_silo_data(char *filename,
112 int maxtypes,
113 int npartoftype[maxtypes],
114 int ncol,
115 int isrequired[ncol],
116 int *ierr)
117 {
118
119 DBfile *silofile = DBOpen(filename, DB_UNKNOWN, DB_READ);
120 if (!silofile) {
121 *ierr = 1;
122 return;
123 }
124 DBtoc *toc = DBGetToc(silofile);
125 DBpointmesh *my_ptmesh = DBGetPointmesh(silofile,toc->ptmesh_names[0]);
126 if (!my_ptmesh) {
127 printf(" ERROR reading point mesh %s\n",toc->ptmesh_names[0]);
128 DBClose(silofile);
129 *ierr = 4;
130 return;
131 } else {
132 if (debug) printf(" DEBUG: Reading data from point mesh %s\n",toc->ptmesh_names[0]);
133 int ndim = my_ptmesh->ndims;
134 /*int nels = my_ptmesh->nels;*/
135 int i;
136 int np = npartoftype[0];
137 int idim;
138 int particle_type = 1;
139
140 /* read in float */
141 float *x = 0;
142 x = malloc(np*sizeof(float));
143
144 /* must send double to splash */
145 double *tmp_dbl = 0;
146 tmp_dbl = malloc(np*sizeof(double));
147
148 /* read each coordinate in turn */
149 int icol = 0;
150 for (idim=0;idim<ndim;idim++) {
151 icol = icol + 1;
152 x = my_ptmesh->coords[idim];
153 for (i=0;i<np;i++) {
154 tmp_dbl[i] = (double) x[i];
155 }
156 read_silo_data_fromc(&icol,&np,tmp_dbl,&particle_type);
157 switch(icol) {
158 case 1:
159 set_blocklabel(&icol,"x");
160 break;
161 case 2:
162 set_blocklabel(&icol,"y");
163 break;
164 case 3:
165 set_blocklabel(&icol,"z");
166 break;
167 }
168 }
169
170 int nptvar = toc->nptvar;
171 for (i=0;i<nptvar;i++) {
172 printf(" Reading ptvar #%i: %s\n",i+1,toc->ptvar_names[i]);
173 DBmeshvar *my_ptvar = DBGetPointvar(silofile,toc->ptvar_names[i]);
174 if (debug) {
175 printf(" Associated with mesh %s\n",my_ptvar->meshname);
176 printf(" label = %s\n",my_ptvar->label);
177 printf(" Nels,nvals,nspace,ndims = %i %i %i %i\n",\
178 my_ptvar->nels,my_ptvar->nvals,my_ptvar->nspace,my_ptvar->ndims);
179 }
180 icol = icol + 1;
181 x = my_ptvar->vals[0];
182 for (i=0;i<np;i++) {
183 tmp_dbl[i] = (double) x[i];
184 }
185 read_silo_data_fromc(&icol,&np,tmp_dbl,&particle_type);
186 set_blocklabel(&icol,my_ptvar->name);
187 /*DBFreeMeshvar(my_ptvar);*/
188 }
189
190 free(x);
191 free(tmp_dbl);
192
193 DBFreePointmesh(my_ptmesh);
194 }
195 DBClose(silofile);
196 }
197