| 1 |
/* |
| 2 |
* This subroutine performs the calls to the HDF5 library for the |
| 3 |
* CACTUS data read |
| 4 |
* |
| 5 |
* Easier to do it this way and link with c than to try to link against |
| 6 |
* the Fortran interface (in the latter case the modules must |
| 7 |
* have been compiled with the *exact* compiler used to compile splash |
| 8 |
* which is a real pain). |
| 9 |
* |
| 10 |
*/ |
| 11 |
#include <stdlib.h> |
| 12 |
#include <assert.h> |
| 13 |
#include <string.h> |
| 14 |
#include <hdf5.h> |
| 15 |
#include <math.h> |
| 16 |
|
| 17 |
#define MAX_DATASETS 100000 |
| 18 |
static int debug = 0; |
| 19 |
static hid_t file_id; |
| 20 |
static const herr_t HDF5_error = -1; |
| 21 |
static int have_indexed = 0; |
| 22 |
#define LEN_NAME 64 |
| 23 |
|
| 24 |
typedef struct |
| 25 |
{ |
| 26 |
char name[LEN_NAME]; |
| 27 |
} dataset_t; |
| 28 |
|
| 29 |
static dataset_t Dataset[MAX_DATASETS]; |
| 30 |
static int iter[MAX_DATASETS]; |
| 31 |
static int iorder[MAX_DATASETS]; |
| 32 |
|
| 33 |
int read_cactus_dataset(hid_t file_id,char *name,int *ncol,int *ncells,int *ndim,int *n,double *time,double *deltax,int inheader); |
| 34 |
int read_cactus_iteration(hid_t file_id,int iter,int *next,int *nsteps,int *ncells,int *ndim,int *ncol,double *time,double *deltax,int inheader,int ignoretl); |
| 35 |
int read_cactus_grid(hid_t dataset_id,hid_t dataspace_id,int ndim,int mycol,int *n,int nx,int ny,int nz,int nghost[3],double orig[3],double delta[3]); |
| 36 |
void get_ndim_ncells(hid_t dataspace_id, int *ndim, int *nx,int *ny,int *nz); |
| 37 |
void set_blocklabel_cactus(int *icol, char *name, int *lenname); |
| 38 |
void sort_cactus_data(int *n, int iarr[*n], int iorder[*n]); |
| 39 |
void read_cactus_hdf5_data_fromc(int *icol,int *ntot,int *np,double temparr[*np]); |
| 40 |
void read_cactus_itype_fromc(int *ntot,int *np,int itype[*np]); |
| 41 |
|
| 42 |
void open_cactus_hdf5_file(char *filename, |
| 43 |
int istep, |
| 44 |
int *ncells, |
| 45 |
int *ncol, |
| 46 |
int *nsteps, |
| 47 |
int *ndim, |
| 48 |
int *ndimV, |
| 49 |
double *time, |
| 50 |
int ignoretl, |
| 51 |
int *ierr) |
| 52 |
{ |
| 53 |
*ierr = 0; |
| 54 |
*ndim = 0; |
| 55 |
*ndimV = 0; |
| 56 |
*ncol = 5; /* x,y,z,h,m */ |
| 57 |
have_indexed = 0; |
| 58 |
|
| 59 |
if (debug) printf("DEBUG: opening %s \n",filename); |
| 60 |
file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); |
| 61 |
if (file_id == HDF5_error) |
| 62 |
{ printf("ERROR opening %s \n",filename); *ierr = 1; return; } |
| 63 |
|
| 64 |
/* |
| 65 |
* Open the first iteration and read the number of dimensions, |
| 66 |
* number of cells and number of variables in file |
| 67 |
*/ |
| 68 |
int next; |
| 69 |
double dx; |
| 70 |
*ierr = read_cactus_iteration(file_id,istep,&next,nsteps,ncells,ndim,ncol,time,&dx,1,ignoretl); |
| 71 |
*ierr = 0; |
| 72 |
|
| 73 |
*ndimV = *ndim; |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
void close_cactus_hdf5_file(int *ierr) |
| 78 |
{ |
| 79 |
if (debug) printf("DEBUG: closing file \n"); |
| 80 |
herr_t status = H5Fclose( file_id ); |
| 81 |
if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 1; } |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void read_cactus_hdf5_data(char *filename,int istep,int *npart,double *time,double *dx,int ignoretl,int *ierr) |
| 87 |
{ |
| 88 |
int next,ncol,ndim,nstepsread,nsteps; |
| 89 |
|
| 90 |
if (file_id == HDF5_error) |
| 91 |
{ printf("ERROR with file_id %s \n",filename); *ierr = 1; return; } |
| 92 |
|
| 93 |
ncol = 5; |
| 94 |
nstepsread = read_cactus_iteration(file_id,istep,&next,&nsteps,npart,&ndim,&ncol,time,dx,0,ignoretl); |
| 95 |
if (nstepsread <= 0) { |
| 96 |
*ierr = -1; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* read one iteration from the file, corresponding to all datasets with same iteration numbers |
| 102 |
*/ |
| 103 |
int read_cactus_iteration(hid_t file_id,int istep,int *next,int *nsteps,int *ncells,int *ndim,int *ncol,double *time,double *deltax,int inheader,int ignore_time_levels) |
| 104 |
{ |
| 105 |
hsize_t ndatasets[1]; |
| 106 |
/* get number of datasets in file */ |
| 107 |
H5Gget_num_objs(file_id, ndatasets); |
| 108 |
int it,tl,level,cnum; |
| 109 |
|
| 110 |
/* loop over all datasets looking for dataset matching the desired iteration number |
| 111 |
set function value to true (1) if it is present */ |
| 112 |
int i,nsub,ierr; |
| 113 |
int mylen = LEN_NAME; |
| 114 |
char name[LEN_NAME]; |
| 115 |
char thorn[LEN_NAME],thornprev[LEN_NAME]; |
| 116 |
int nstepsgot = 0; |
| 117 |
*ndim = 0; |
| 118 |
*ncells = 0; |
| 119 |
int n_datasets = (int)ndatasets[0]; |
| 120 |
if (n_datasets > MAX_DATASETS) { |
| 121 |
if (inheader) printf("ERROR: EXCEEDED MAX NUMBER OF DATASETS: INCREASE MAX_DATASETS TO READ ALL\n"); |
| 122 |
n_datasets = MAX_DATASETS; |
| 123 |
} |
| 124 |
if (inheader) { |
| 125 |
have_indexed = 0; |
| 126 |
} |
| 127 |
if (!have_indexed) { |
| 128 |
printf("Indexing %i datasets in file\n",n_datasets); |
| 129 |
} |
| 130 |
|
| 131 |
i = 0; |
| 132 |
cnum = 0; |
| 133 |
*next = 0; |
| 134 |
*nsteps = 0; |
| 135 |
strcpy(thornprev,""); |
| 136 |
int n = 0; |
| 137 |
int mystep = 0; |
| 138 |
int iterprev = -1; |
| 139 |
int tlprev = 0; |
| 140 |
while (i < n_datasets) { |
| 141 |
/* get dataset name, either by reading from file or from memory buffer */ |
| 142 |
if (!have_indexed) { |
| 143 |
H5Gget_objname_by_idx(file_id, i, name, LEN_NAME); |
| 144 |
strcpy(Dataset[i].name,name); |
| 145 |
if (debug) printf("%s\n",name); |
| 146 |
} else { /* have indexed file */ |
| 147 |
strcpy(name,Dataset[iorder[i]-1].name); |
| 148 |
} |
| 149 |
|
| 150 |
/* extract iteration number and thorn name */ |
| 151 |
it = -1; |
| 152 |
if (sscanf(name, "%s it=%i tl=%i rl=%i c=%i", |
| 153 |
thorn, &it, &tl, &level, &cnum)>=4) { /* 4 because c= is optional */ |
| 154 |
if (debug) printf("%s it=%i tl=%i rl=%i cnum=%i \n",thorn,it,tl,level,cnum); |
| 155 |
if (!ignore_time_levels || tl ==0) { |
| 156 |
/* count how many unique datasets (columns) there are */ |
| 157 |
if (strcmp(thorn,thornprev) != 0 || tl != tlprev) { |
| 158 |
*ncol = *ncol + 1; |
| 159 |
if (inheader) printf("-> %s tl=%i\n",thorn,tl); |
| 160 |
/* send dataset name back to phantom */ |
| 161 |
if (inheader) set_blocklabel_cactus(ncol,thorn,&mylen); |
| 162 |
nsub = 0; |
| 163 |
n = 0; |
| 164 |
if (debug) printf(" GOT ncells = %i\n",*ncells); |
| 165 |
*ncells = 0; |
| 166 |
} |
| 167 |
strncpy(thornprev,thorn,LEN_NAME); |
| 168 |
tlprev = tl; |
| 169 |
} else { |
| 170 |
it = -1; |
| 171 |
} |
| 172 |
} else { |
| 173 |
it = -1; |
| 174 |
n = 0; |
| 175 |
if (debug) printf("UNKNOWN dataset %s \n",name); |
| 176 |
} |
| 177 |
/* store iteration numbers so we can sort datasets by iteration number */ |
| 178 |
if (inheader && !have_indexed) iter[i] = it; |
| 179 |
|
| 180 |
if (it >= 0) { |
| 181 |
if (it != iterprev) { |
| 182 |
mystep++; |
| 183 |
} |
| 184 |
if (mystep == istep) { |
| 185 |
nsub++; |
| 186 |
ierr = read_cactus_dataset(file_id,name,ncol,ncells,ndim,&n,time,deltax,inheader); |
| 187 |
} else if (mystep > istep && have_indexed) { |
| 188 |
break;; |
| 189 |
} |
| 190 |
iterprev = it; |
| 191 |
} else { |
| 192 |
n = 0; |
| 193 |
} |
| 194 |
i++; |
| 195 |
} |
| 196 |
have_indexed = 1; |
| 197 |
|
| 198 |
if (nsub > 0) { |
| 199 |
nstepsgot = 1; |
| 200 |
*nsteps = mystep; /*n_datasets/nsub;*/ |
| 201 |
} |
| 202 |
if (inheader) { |
| 203 |
if (mystep > 1) { |
| 204 |
sort_cactus_data(&n_datasets,iter,iorder); |
| 205 |
} else { |
| 206 |
/* do not sort if only one iteration in file */ |
| 207 |
for (i=0;i<n_datasets;i++) { |
| 208 |
iorder[i] = i+1; |
| 209 |
} |
| 210 |
} |
| 211 |
printf("Read data from %i/%i timesteps, it=%i ncells=%i\n",istep,*nsteps,iterprev,*ncells); |
| 212 |
} |
| 213 |
|
| 214 |
return nstepsgot; |
| 215 |
} |
| 216 |
|
| 217 |
int read_cactus_dataset(hid_t file_id,char *name,int *ncol,int *ncells,int *ndim,int *n,double *time,double *deltax,int inheader) |
| 218 |
{ |
| 219 |
hid_t dataspace_id,dataset_id,attrib_id; |
| 220 |
herr_t status; |
| 221 |
herr_t HDF5_error = -1; |
| 222 |
|
| 223 |
#if H5_VERSION_GE(1,8,0) |
| 224 |
dataset_id = H5Dopen2(file_id,name,H5P_DEFAULT); |
| 225 |
#else |
| 226 |
dataset_id = H5Dopen(file_id,name); |
| 227 |
#endif |
| 228 |
if (dataset_id == HDF5_error) return 1; |
| 229 |
dataspace_id = H5Dget_space(dataset_id); |
| 230 |
if (dataspace_id == HDF5_error) return 2; |
| 231 |
|
| 232 |
/* get dimensionality */ |
| 233 |
int nx,ny,nz; |
| 234 |
get_ndim_ncells(dataspace_id,ndim,&nx,&ny,&nz); |
| 235 |
if (nx*ny*nz <= 1 || *ndim != 3) { |
| 236 |
if (debug) printf("SKIPPING %s ndim = %i size = %i\n",name,*ndim,nx); |
| 237 |
return 3; |
| 238 |
} |
| 239 |
*ncells = *ncells + nx*ny*nz; |
| 240 |
|
| 241 |
/* get number of attributes */ |
| 242 |
int nattrib = H5Aget_num_attrs(dataset_id); |
| 243 |
if (nattrib < 1) { |
| 244 |
printf("ERROR: found %i attributes in dataset\n",nattrib); |
| 245 |
} |
| 246 |
|
| 247 |
if (debug) printf("ndim=%i size=%i %i %i nattrib=%i\n",*ndim,nx,ny,nz,nattrib); |
| 248 |
|
| 249 |
int i,isize; |
| 250 |
double origin[3],delta[3]; |
| 251 |
int bbox[6],iorigin[3]; |
| 252 |
int nghost[3]; |
| 253 |
char name_attr[256]; |
| 254 |
|
| 255 |
/* |
| 256 |
* Read through all of the attributes in the header, so we |
| 257 |
* can still spit out the values even if they are not used by SPLASH |
| 258 |
*/ |
| 259 |
for(i=0; i < nattrib; i++) { |
| 260 |
attrib_id = H5Aopen_idx(dataset_id,i); |
| 261 |
ssize_t attr_status; |
| 262 |
attr_status = H5Aget_name(attrib_id, 256, name_attr); |
| 263 |
|
| 264 |
hid_t type_id; |
| 265 |
type_id = H5Aget_type(attrib_id); |
| 266 |
|
| 267 |
isize = (int) H5Aget_storage_size(attrib_id); |
| 268 |
/*type_class = H5Tget_native_type(type_id,H5T_DIR_ASCEND);*/ |
| 269 |
if (strcmp(name_attr,"time")==0) { |
| 270 |
status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,time); |
| 271 |
} else if (strcmp(name_attr,"origin")==0) { |
| 272 |
status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,&origin); |
| 273 |
} else if (strcmp(name_attr,"delta")==0) { |
| 274 |
status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,&delta); |
| 275 |
} else if (strcmp(name_attr,"cctk_bbox")==0) { |
| 276 |
status = H5Aread(attrib_id,H5T_NATIVE_INT,&bbox); |
| 277 |
} else if (strcmp(name_attr,"iorigin")==0) { |
| 278 |
status = H5Aread(attrib_id,H5T_NATIVE_INT,&iorigin); |
| 279 |
} else if (strcmp(name_attr,"cctk_nghostzones")==0) { |
| 280 |
status = H5Aread(attrib_id,H5T_NATIVE_INT,&nghost); |
| 281 |
} else { |
| 282 |
if (debug && inheader) printf("DEBUG: unknown attribute %s (%i bytes)\n",name_attr,isize); |
| 283 |
} |
| 284 |
|
| 285 |
if (status==HDF5_error) { |
| 286 |
printf(" ERROR reading attribute %s \n",name_attr); |
| 287 |
} |
| 288 |
status = H5Aclose(attrib_id); |
| 289 |
} |
| 290 |
*deltax = delta[0]; |
| 291 |
|
| 292 |
if (debug==2 && nattrib > 0) { |
| 293 |
printf("bbox: x %i %i y %i %i z %i %i\n",bbox[0],bbox[1],bbox[2],bbox[3],bbox[4],bbox[5]); |
| 294 |
printf("iorigin: %i %i %i\n",iorigin[0],iorigin[1],iorigin[2]); |
| 295 |
printf("origin: %e %e %e\n",origin[0],origin[1],origin[2]); |
| 296 |
printf("delta: %e %e %e\n",delta[0],delta[1],delta[2]); |
| 297 |
printf("Time=%e Delta=%e %e %e nghost=%i %i %i \n",*time,delta[0],delta[1],delta[2], |
| 298 |
nghost[0],nghost[1],nghost[2]); |
| 299 |
} |
| 300 |
if (inheader==0) { |
| 301 |
/* read dataset */ |
| 302 |
read_cactus_grid(dataset_id,dataspace_id,*ndim,*ncol,n,nx,ny,nz,nghost,origin,delta); |
| 303 |
} |
| 304 |
|
| 305 |
/* close dataspace and dataset */ |
| 306 |
status = H5Sclose(dataspace_id); |
| 307 |
if (status == HDF5_error) { |
| 308 |
printf("ERROR closing dataspace \n"); |
| 309 |
} |
| 310 |
|
| 311 |
status = H5Dclose(dataset_id); |
| 312 |
if (status == HDF5_error) { |
| 313 |
printf("ERROR closing data set \n"); |
| 314 |
} |
| 315 |
return (int) status; |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
* utility function to get dimensionality of a dataset |
| 320 |
*/ |
| 321 |
void get_ndim_ncells(hid_t dataspace_id, int *ndim, int *nx,int *ny,int *nz) |
| 322 |
{ |
| 323 |
*nx = 1; |
| 324 |
*ny = 1; |
| 325 |
*nz = 1; |
| 326 |
*ndim = H5Sget_simple_extent_ndims(dataspace_id); |
| 327 |
hsize_t dims[*ndim], maxdims[*ndim]; |
| 328 |
H5Sget_simple_extent_dims(dataspace_id,dims,maxdims); |
| 329 |
|
| 330 |
/* get number of cells */ |
| 331 |
if (*ndim > 0) *nx = dims[0]; |
| 332 |
if (*ndim > 1) *ny = dims[1]; |
| 333 |
if (*ndim > 2) *nz = dims[2]; |
| 334 |
} |
| 335 |
|
| 336 |
int read_cactus_grid(hid_t dataset_id,hid_t dataspace_id,int ndim,int mycol,int *n,int nx,int ny,int nz, |
| 337 |
int nghost[3],double orig[3],double delta[3]) |
| 338 |
{ |
| 339 |
hid_t memspace_id; |
| 340 |
herr_t status; |
| 341 |
int ierr = 0; |
| 342 |
|
| 343 |
int i,j,k; |
| 344 |
int ncells = nx*ny*nz; |
| 345 |
|
| 346 |
/* make a temporary array to put each column as we read it */ |
| 347 |
hsize_t nsize[3]; |
| 348 |
nsize[0] = nx; |
| 349 |
nsize[1] = ny; |
| 350 |
nsize[2] = nz; |
| 351 |
memspace_id = H5Screate_simple(ndim,nsize,NULL); |
| 352 |
double *dat = malloc(ncells * sizeof(double)); |
| 353 |
|
| 354 |
status = H5Dread(dataset_id,H5T_NATIVE_DOUBLE,memspace_id,dataspace_id,H5P_DEFAULT,dat); |
| 355 |
if (status == HDF5_error) { printf("ERROR reading dataset \n"); ierr = 4; } |
| 356 |
|
| 357 |
/* map to one dimensional arrays and determine x,y,z for each cell */ |
| 358 |
double xi,yi,zi; |
| 359 |
double dx = delta[0]; |
| 360 |
double dy = delta[1]; |
| 361 |
double dz = delta[2]; |
| 362 |
if (debug) { |
| 363 |
printf("PATCH origin = %f %f %f\n",orig[0],orig[1],orig[2]); |
| 364 |
printf(" nx=%i x=%f->%f\n",nx,orig[0],(orig[0]+(nx-1)*dx)); |
| 365 |
printf(" ny=%i y=%f->%f\n",ny,orig[1],(orig[1]+(ny-1)*dy)); |
| 366 |
printf(" nz=%i z=%f->%f\n",nz,orig[2],(orig[2]+(nz-1)*dz)); |
| 367 |
} |
| 368 |
|
| 369 |
int icol; |
| 370 |
*n = *n + ncells; |
| 371 |
if (mycol==6) { |
| 372 |
double *xx = malloc(ncells * sizeof(double)); |
| 373 |
double *yy = malloc(ncells * sizeof(double)); |
| 374 |
double *zz = malloc(ncells * sizeof(double)); |
| 375 |
int *itype = malloc(ncells * sizeof(int)); |
| 376 |
if (debug) printf("constructing grid dx=%f, dy=%f, dz=%f\n",dx,dy,dz); |
| 377 |
|
| 378 |
/* here we have to reconstruct the x, y and z positions of |
| 379 |
each cell. The following looks hacked but works. We tested it. */ |
| 380 |
int ip = 0; |
| 381 |
for (k=0;k<nx;k++) { |
| 382 |
zi = orig[2] + k*dz; |
| 383 |
for (j=0;j<ny;j++) { |
| 384 |
yi = orig[1] + j*dy; |
| 385 |
for (i=0;i<nz;i++) { |
| 386 |
xi = orig[0] + i*dx; |
| 387 |
xx[ip]=xi; |
| 388 |
yy[ip]=yi; |
| 389 |
zz[ip]=zi; |
| 390 |
/* printf("ip=%i rho %i %i %i = %f x=%f y=%f z=%f \n",ip,i,j,k,dat[ip],xi,yi,zi);*/ |
| 391 |
ip++; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/* tag ghost particles */ |
| 397 |
ip = 0; |
| 398 |
int isghostx,isghosty,isghostz; |
| 399 |
for (k=0;k<nx;k++) { |
| 400 |
if ((k < nghost[2]) || (k > nx-nghost[2]-1)) { |
| 401 |
isghostz = 1; |
| 402 |
} else { |
| 403 |
isghostz = 0; |
| 404 |
} |
| 405 |
for (j=0;j<ny;j++) { |
| 406 |
if ((j < nghost[1]) || (j > ny-nghost[1]-1)) { |
| 407 |
isghosty = 1; |
| 408 |
} else { |
| 409 |
isghosty = 0; |
| 410 |
} |
| 411 |
for (i=0;i<nz;i++) { |
| 412 |
if ((i < nghost[0]) || (i > nz-nghost[0]-1)) { |
| 413 |
isghostx = 1; |
| 414 |
} else { |
| 415 |
isghostx = 0; |
| 416 |
} |
| 417 |
if (isghostx || isghosty || isghostz) { |
| 418 |
itype[ip] = 2; |
| 419 |
} else { |
| 420 |
itype[ip] = 1; |
| 421 |
} |
| 422 |
ip++; |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
/* send additional data through to Fortran */ |
| 427 |
icol = 1; |
| 428 |
read_cactus_hdf5_data_fromc(&icol,n,&ncells,xx); |
| 429 |
icol = 2; |
| 430 |
read_cactus_hdf5_data_fromc(&icol,n,&ncells,yy); |
| 431 |
icol = 3; |
| 432 |
read_cactus_hdf5_data_fromc(&icol,n,&ncells,zz); |
| 433 |
/* zone type (regular or ghost) */ |
| 434 |
read_cactus_itype_fromc(n,&ncells,itype); |
| 435 |
free(xx); |
| 436 |
free(yy); |
| 437 |
free(zz); |
| 438 |
free(itype); |
| 439 |
} |
| 440 |
icol = mycol; |
| 441 |
read_cactus_hdf5_data_fromc(&icol,n,&ncells,dat); |
| 442 |
|
| 443 |
/* deallocate memory */ |
| 444 |
free(dat); |
| 445 |
|
| 446 |
return ierr; |
| 447 |
} |