/* Counterpart for complex256 */ hid_t create_ieee_complex256(const char *byteorder) { herr_t err = 0; hid_t float_id, complex_id; H5T_order_t h5order = H5Tget_order(H5T_NATIVE_LDOUBLE); complex_id = H5Tcreate(H5T_COMPOUND, sizeof(npy_complex256)); float_id = H5Tcopy(H5T_NATIVE_LDOUBLE); if (float_id < 0) { H5Tclose(complex_id); return float_id; } if ((strcmp(byteorder, "little") == 0) && (h5order != H5T_ORDER_LE)) err = H5Tset_order(float_id, H5T_ORDER_LE); else if ((strcmp(byteorder, "big") == 0) && (h5order != H5T_ORDER_BE)) err = H5Tset_order(float_id, H5T_ORDER_BE); if (err < 0) { H5Tclose(complex_id); return err; } H5Tinsert(complex_id, "r", HOFFSET(npy_complex256, real), float_id); H5Tinsert(complex_id, "i", HOFFSET(npy_complex256, imag), float_id); H5Tclose(float_id); return complex_id; }
/* This only works for datatypes that are not Complex. However, these types should already been created with correct byteorder */ herr_t set_order(hid_t type_id, const char *byteorder) { herr_t status=0; if (! is_complex(type_id)) { if (strcmp(byteorder, "little") == 0) status = H5Tset_order(type_id, H5T_ORDER_LE); else if (strcmp(byteorder, "big") == 0) status = H5Tset_order(type_id, H5T_ORDER_BE); else if (strcmp(byteorder, "irrelevant") == 0) { /* Do nothing because 'irrelevant' doesn't require setting the byteorder explicitely */ /* status = H5Tset_order(type_id, H5T_ORDER_NONE ); */ } else { fprintf(stderr, "Error: unsupported byteorder <%s>\n", byteorder); status = -1; } } return status; }
static void *tts_error_thread(void UNUSED *arg) { hid_t dataspace, datatype, dataset; hsize_t dimsf[1]; /* dataset dimensions */ H5E_auto2_t old_error_cb; void *old_error_client_data; int value; int ret; /* preserve previous error stack handler */ H5Eget_auto2(H5E_DEFAULT, &old_error_cb, &old_error_client_data); /* set each thread's error stack handler */ H5Eset_auto2(H5E_DEFAULT, error_callback, NULL); /* define dataspace for dataset */ dimsf[0] = 1; dataspace = H5Screate_simple(1, dimsf, NULL); assert(dataspace >= 0); /* define datatype for the data using native little endian integers */ datatype = H5Tcopy(H5T_NATIVE_INT); assert(datatype >= 0); H5Tset_order(datatype, H5T_ORDER_LE); /* create a new dataset within the file */ dataset = H5Dcreate2(error_file, DATASETNAME, datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if(dataset >= 0) { /* not an error */ value = WRITE_NUMBER; H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &value); H5Dclose(dataset); } /* end if */ ret = H5Tclose(datatype); assert(ret >= 0); ret = H5Sclose(dataspace); assert(ret >= 0); /* turn our error stack handler off */ H5Eset_auto2(H5E_DEFAULT, old_error_cb, old_error_client_data); return NULL; }
int create_new_dataset(H5block *d){ hid_t file_id, dataset_id, dataspace_id, status, dcpl, datatype; file_id = H5Fopen(d->name, H5F_ACC_RDWR, H5P_DEFAULT); hsize_t dims[2]; dims[0] = d->x_index_dim; dims[1] = d->y_index_dim; dataspace_id = H5Screate_simple(2, dims, NULL); datatype = H5Tcopy(H5T_NATIVE_FLOAT); status = H5Tset_order(datatype, H5T_ORDER_LE); char buffer[50]; sprintf(buffer, "/dset%ld", d->ticks); dataset_id = H5Dcreate(file_id, buffer, datatype, dataspace_id, H5P_DEFAULT); status = H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, d->field); status = H5Dclose(dataset_id); status = H5Tclose(datatype); status = H5Sclose(dataspace_id); status = H5Fclose(file_id); }
int write_data_to_hdf5_file (int nx, int ny, double **data, hid_t file) { hid_t dataset; /* file and dataset handles */ hid_t datatype, dataspace; /* handles */ hsize_t dimsf[2]; /* dataset dimensions */ herr_t status; dimsf[0] = nx; dimsf[1] = ny; dataspace = H5Screate_simple (RANK, dimsf, NULL); /* * Define datatype for the data in the file. * We will store little endian DOUBLE numbers. */ datatype = H5Tcopy (H5T_NATIVE_DOUBLE); status = H5Tset_order (datatype, H5T_ORDER_LE); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate (file, "Temperature", datatype, dataspace, H5P_DEFAULT); /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite (dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); /* * Close/release resources. */ H5Sclose (dataspace); H5Tclose (datatype); H5Dclose (dataset); return 0; }
/*------------------------------------------------------------------------- * Function: create_nbit_dsets_float * * Purpose: Create a dataset of FLOAT datatype with nbit filter * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 29 March 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_nbit_dsets_float(hid_t fid, hid_t fsid, hid_t msid) { hid_t dataset = -1; /* dataset handles */ hid_t datatype = -1; hid_t dcpl = -1; size_t precision, offset; float data[NX][NY]; /* data to write */ float fillvalue = -2.2f; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = ((float)(i + j + 1))/3; } /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_nbit(dcpl) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_FLOAT, &fillvalue) < 0) TEST_ERROR /* Define user-defined single-precision floating-point type for dataset. * A 20-bit little-endian data type. */ if((datatype = H5Tcopy(H5T_IEEE_F32LE)) < 0) TEST_ERROR if(H5Tset_fields(datatype, (size_t)26, (size_t)20, (size_t)6, (size_t)7, (size_t)13) < 0) TEST_ERROR offset = 7; if(H5Tset_offset(datatype,offset) < 0) TEST_ERROR precision = 20; if(H5Tset_precision(datatype,precision) < 0) TEST_ERROR if(H5Tset_size(datatype, (size_t)4) < 0) TEST_ERROR if(H5Tset_ebias(datatype, (size_t)31) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, * user-defined datatype, and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME22, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if(H5Tset_order(datatype, H5T_ORDER_BE) < 0) TEST_ERROR if((dataset = H5Dcreate2(fid, DATASETNAME23, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR return 0; error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; }
int main(void) { SRB_Info srb_info={ NULL, /* Use the server name in ~/.srb/MdasEnv */ NULL, /* Use the server port number in * ~/.srb/MdasEnv */ NULL, /* Use the password in ~/.srb/MdasAuth */ 0, /* Using Unix storage system. */ 0600, /* Open file for read and write for owner */ -1 /* default */ }; hid_t fapl =-1, file; hid_t dataspace, datatype, dataset; hsize_t dimsf[2]; herr_t status = 0; int data[NX][NY]; /* data to write */ int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = i*i + j*j; } /* * 0 1 4 9 16 25 * 1 2 5 10 17 26 * 4 5 8 13 20 29 * 9 10 13 18 25 34 * 16 17 20 25 32 41 */ /* Create access property list and set the driver to GASS */ fapl = H5Pcreate (H5P_FILE_ACCESS); if (fapl < 0) { printf (" H5Pcreate failed. \n"); return -1; } status = H5Pset_fapl_srb (fapl, srb_info); if (status < 0) { printf ("H5Pset_fapl_gass failed. \n"); return -1; } /* * Open an existing file using H5F_ACC_RDWR access, * and srb file access properties. */ file = H5Fopen(fileName, H5F_ACC_RDWR, fapl); if (file < 0) { printf ("H5Fopen failed. \n"); return -1; } /* * Describe the size of the array and create the data space for fixed * size dataset. */ dimsf[0] = NX; dimsf[1] = NY; dataspace = H5Screate_simple(RANK, dimsf, NULL); if (dataspace < 0) { printf ("H5Screate failed. \n"); return -1; } /* * Define datatype for the data in the file. * We will store little endian INT numbers. */ datatype = H5Tcopy(H5T_NATIVE_INT); if (datatype < 0) { printf ("H5Tcopy failed. \n"); return -1; } status = H5Tset_order(datatype, H5T_ORDER_LE); if (status < 0) { printf ("H5Tset_order failed. \n"); return -1; } /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate(file, DATASETNAME, datatype, dataspace, H5P_DEFAULT); if (dataset < 0) { printf ("H5Dcreate failed. \n"); return -1; } /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); if (status < 0) { printf ("H5Dwrite failed. \n"); return -1; } /* * Close/release resources. */ H5Sclose(dataspace); H5Tclose(datatype); H5Dclose(dataset); H5Fclose(file); H5Pclose(fapl); printf("Test finished!\n"); return 0; }
int output (Array3D < zone > grid, Array3D < zone > fx, Array3D < zone > fy, int time, char *filename) { #ifdef USE_HDF5 hid_t file, dataset; /* file and dataset handles */ hid_t datatype, dataspace; /* handles */ hsize_t dimsf[2]; /* dataset dimensions */ herr_t status; double data[nx][ny]; char *names[] = { "Density", "Velx", "Vely", "Velz", "Energy", "Bx", "By", "Bz" }; int ll = 0; stringstream hdf5_stream_filename; string hdf5_filename; #endif ofstream fout; ofstream gout; double gammam1 = gammag - 1; double rl, ri; double px; double py; double pz; double pressure; double bx; double by; double bz; double bsquared; double et, ul, vl, wl, ke, al; int ii = 0; int jj = 0; int kk = 0; char outputdir[50] = "output/"; // char filename[50] = "out_2d_"; stringstream s; stringstream stream_filename; stringstream stream_temp_b; string str_file_tag; string str_output_filename; string str_input_filename; double ki = 24296.3696; double mp = 1.67262158; double mpi = 1.0 / mp; double nt = 0; double nt2 = 0; double temperature = 0; s.clear (); s.width (5); s.fill ('0'); s << time; s >> str_file_tag; stream_filename.clear (); stream_filename << outputdir << filename << str_file_tag; stream_filename >> str_input_filename; #ifdef USE_HDF5 hdf5_stream_filename << outputdir << "hdf5_" << filename << str_file_tag << ".h5"; hdf5_stream_filename >> hdf5_filename; file = H5Fcreate (hdf5_filename.c_str (), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); for (ll = 0; ll < ne; ll++) { dimsf[0] = nx; dimsf[1] = ny; dataspace = H5Screate_simple (RANK, dimsf, NULL); /* * Define datatype for the data in the file. * We will store little endian DOUBLE numbers. */ datatype = H5Tcopy (H5T_NATIVE_DOUBLE); status = H5Tset_order (datatype, H5T_ORDER_LE); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate (file, names[ll], datatype, dataspace, H5P_DEFAULT); for (jj = 0; jj < ny; jj++) { for (ii = 0; ii < nx; ii++) data[ii][jj] = grid[ii][jj][kk].array[ll]; } /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite (dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); /* * Close/release resources. */ H5Sclose (dataspace); H5Tclose (datatype); H5Dclose (dataset); } dimsf[0] = nx; dimsf[1] = ny; dataspace = H5Screate_simple (RANK, dimsf, NULL); /* * Define datatype for the data in the file. * We will store little endian DOUBLE numbers. */ datatype = H5Tcopy (H5T_NATIVE_DOUBLE); status = H5Tset_order (datatype, H5T_ORDER_LE); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate (file, "Pressure", datatype, dataspace, H5P_DEFAULT); for (jj = 0; jj < ny; jj++) { for (ii = 0; ii < nx; ii++) { rl = grid[ii][jj][kk] _MASS; px = grid[ii][jj][kk] _MOMX; py = grid[ii][jj][kk] _MOMY; pz = grid[ii][jj][kk] _MOMZ; et = grid[ii][jj][kk] _ENER; bx = grid[ii][jj][kk] _B_X; by = grid[ii][jj][kk] _B_Y; bz = grid[ii][jj][kk] _B_Z; ri = 1.0 / rl; ul = px * ri; vl = py * ri; wl = pz * ri; ke = 0.5 * rl * (ul * ul + vl * vl + wl * wl); bsquared = bx * bx + by * by + bz * bz; pressure = et - ke - 0.5 * bsquared; pressure = pressure * gammam1; al = sqrt (gammag * pressure * ri); nt = 2 * mpi * rl; nt2 = nt * nt; temperature = ki * pressure / nt; temperature = log10 (temperature); data[ii][jj] = pressure; } } /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite (dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); /* * Close/release resources. */ H5Sclose (dataspace); H5Tclose (datatype); H5Dclose (dataset); H5Fclose (file); #endif /* HDF5 or not */ fout.open (str_input_filename.c_str ()); if (!fout) { cerr << "unable to open file " << endl; } jj = 0; // Determine Div B Array2D < double >divb (nx, ny); double bx1, bx2, by1, by2, bz1, bz2; for (ii = 1; ii < nx - 2; ii++) { for (jj = 1; jj < ny - 2; jj++) { bx1 = (grid[ii][jj][kk] _B_X + grid[ii - 1][jj][kk] _B_X); bx2 = (grid[ii + 1][jj][kk] _B_X + grid[ii][jj][kk] _B_X); by1 = (grid[ii][jj][kk] _B_Y + grid[ii][jj - 1][kk] _B_Y); by2 = (grid[ii][jj + 1][kk] _B_Y + grid[ii][jj][kk] _B_Y); // bz1 = ( grid[ii ][jj ][kk ]_B_Z + grid[ii ][jj ][kk-1]_B_Z ); // bz2 = ( grid[ii ][jj ][kk+1]_B_Z + grid[ii ][jj ][kk ]_B_Z ); //divb = (1/delta_x)*(bx2- bx1 + by2 -by1 +bz2 -bz1); divb[ii][jj] = (0.5 / delta_x) * (bx2 - bx1 + by2 - by1); } } for (ii = 0; ii < nx; ii++) { for (jj = 0; jj < ny; jj++) { rl = grid[ii][jj][kk] _MASS; px = grid[ii][jj][kk] _MOMX; py = grid[ii][jj][kk] _MOMY; pz = grid[ii][jj][kk] _MOMZ; et = grid[ii][jj][kk] _ENER; bx = grid[ii][jj][kk] _B_X; by = grid[ii][jj][kk] _B_Y; bz = grid[ii][jj][kk] _B_Z; ri = 1.0 / rl; ul = px * ri; vl = py * ri; wl = pz * ri; ke = 0.5 * rl * (ul * ul + vl * vl + wl * wl); bsquared = bx * bx + by * by + bz * bz; pressure = et - ke - 0.5 * bsquared; pressure = pressure * gammam1; al = sqrt (gammag * pressure * ri); #ifdef DEBUG_BC if (ii == 2 && jj == 2 && px != 0) { cout << px << endl; cout << ul << endl; cout << "wtf?" << endl; } #endif /* DEBUG_BC */ fout << setiosflags (ios::scientific) << " " << (rl) << " " << ul << " " << vl << " " << wl << " " << et << " " << bx << " " << by << " " << bz << " " << (pressure) << " " << (al) << " " << divb[ii][jj] << endl; } #ifdef TWODIM fout << endl; #endif /* TWODIM */ } fout.close (); gout.open ("gm.general"); gout << "file = /home/gmurphy/mhdvanleer-0.0.1/" << str_input_filename << endl; gout << "grid = " << nx << " x " << ny << endl; gout << "format = ascii" << endl; gout << "interleaving = field" << endl; gout << "majority = row" << endl; gout << "field = V_sound, E_tot, Rho, Vel_X, Vel_Y, Pressure" << endl; gout << "structure = scalar, scalar, scalar, scalar, scalar, scalar" << endl; gout << "type = double, double, double, double, double, double" << endl; gout << "dependency = positions, positions, positions, positions, positions, positions" << endl; gout << "positions = regular, regular, 0, 1, 0, 1" << endl; gout << "" << endl; gout << "end" << endl; gout.close (); return 0; }