Writing simple scalar fields on a uniform grid.
#include <hdf5.h> #include <F5/F5uniform.h> #include <F5/F5X.h> #include <math.h> //Defines for the size of the datasets #define NumOfDataValuesX 53 #define NumOfDataValuesY 19 #define NumOfDataValuesZ 31 //Define for the number of time steps used by some of the examples. #define NumOfTimeSteps 17 void StaticScalar(const char* FileName) { /* Needed Variables */ int i; int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ; hid_t FileID, DataID; /* HDF5-Identifiers for opened File and opened DataSet */ hsize_t dims[3]; /* Number of Values in each dimension */ F5_vec3_point_t Origin; /* Starting Point of the Volume */ F5_vec3_float_t Delta; /* Stepsize between 2 Points on the Grid */ F5Path *fpath; float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ /* Init Data */ dims[0] = NumOfDataValuesX; /* Dimensions */ dims[1] = NumOfDataValuesY; dims[2] = NumOfDataValuesZ; Origin.x = -1; /* Volume starts here */ Origin.y = -1; Origin.z = -1; Delta.x = 2.0/(int)dims[0]; /* Stepsize on Grid */ Delta.y = 2.0/(int)dims[1]; Delta.z = 2.0/(int)dims[2]; for(i=0;i<NumOfDataValues;i++) /* Init the Field */ { ScalarData[i] = (float)i; /* This is really simple and stupid... */ } /* Save Data to F5 File - this is the real magic */ /*-----------------------------------------------*/ /* Create the File */ FileID = H5Fcreate(FileName, /* The FileName */ H5F_ACC_TRUNC, /* Creation Flags */ H5P_DEFAULT, /* File creation property list identifier */ H5P_DEFAULT); /* File access property list identifier */ /* Write the Data */ fpath = F5Fwrite_uniform_cartesian3D( FileID, /* File identifier given by HDF5 */ 3.141592, /* Time Value of the field - not used here */ "SimpleScalar", /* Name of the Grid */ &Origin, /* Starting Point of the Volume */ &Delta, /* Spacing between Grid points */ dims, /* Number of Values in each direction */ "TestField", /* Name of the Field */ H5T_NATIVE_FLOAT, /* Type of the Values in the Field */ ScalarData, /* Pointer to the Data */ NULL, /* Coordinate System - We use a standard chart here. */ F5P_DEFAULT); /* Default dataset properties */ F5close(fpath); /* Close the DataSet */ /* Close the File */ H5Fclose(FileID); } void TimeScalar(const char* FileName) { /* Needed Variables */ int i, j; /* int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ;*/ hid_t FileID; /* HDF5-Identifiers for opened File and opened DataSet */ F5Path*fpath; hsize_t dims[3]; /* Number of Values in each dimension */ F5_vec3_point_t Origin; /* Starting Point of the Volume */ F5_vec3_float_t Delta; /* Stepsize between 2 Points on the Grid */ float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ /* Init Data */ dims[0] = NumOfDataValuesX; /* Dimensions */ dims[1] = NumOfDataValuesY; dims[2] = NumOfDataValuesZ; Origin.x = -1; /* Volume starts here */ Origin.y = -1; Origin.z = -1; Delta.x = 2.0/(dims[0]-1); /* Stepsize on Grid */ Delta.y = 2.0/(dims[1]-1); Delta.z = 2.0/(dims[2]-1); /* Create the File */ FileID = H5Fcreate(FileName, /* The FileName */ H5F_ACC_TRUNC, /* Creation Flags */ H5P_DEFAULT, /* File creation property list identifier */ H5P_DEFAULT); /* File access property list identifier */ /* Step through the Time */ for(j=0;j<NumOfTimeSteps;j++) { int x,y,z; double Time = j/(NumOfTimeSteps-1.0), CosTime = .5+.5*cos(Time*2*M_PI); fprintf(stderr, "TimeScalar: Creating %s, time T=%lg\n", FileName, Time); /* Init the Field for each time step */ for(z=0; z<NumOfDataValuesZ; z++) for(y=0; y<NumOfDataValuesY; y++) for(x=0; x<NumOfDataValuesX; x++) { int i = x + NumOfDataValuesX*(y + z*NumOfDataValuesY); double X = Origin.x + x*Delta.x, Y = Origin.y + y*Delta.y, Z = Origin.z + z*Delta.z, R = (X*X + Y*Y + Z*Z)/3.0; ScalarData[i] = (1.0-R)*CosTime; } /* Save Data to F5 File - this is the real magic */ /*-----------------------------------------------*/ /* Write the Data */ fpath = F5Fwrite_uniform_cartesian3D( FileID, /* File identifier given by HDF5 */ Time, /* Time Value of the field */ "TimeScalar", /* Name of the Grid */ &Origin, /* Starting Point of the Volume */ &Delta, /* Spacing between Grid points */ dims, /* Number of Values in each direction */ "TestField", /* Name of the Field */ H5T_NATIVE_FLOAT, /* Type of the Values in the Field */ ScalarData, /* Pointer to the Data */ NULL, /* Coordinate System - We use a standard chart here. */ F5P_DEFAULT); /* Default dataset properties */ F5close(fpath); /* Close the DataSet */ } /* Close the File */ F5Xclose(FileID); } void MultiScalar(const char* FileName) { /* Needed Variables */ int i, j, retval; int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ; hid_t FileID; /* HDF5-Identifiers for opened File and opened DataSet */ hsize_t dims[3]; /* Number of Values in each dimension */ F5_vec3_point_t Origin; /* Starting Point of the Volume */ F5_vec3_float_t Delta; /* Stepsize between 2 Points on the Grid */ float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ double ScalarData2[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ double Time = 0; /* Used to store the time value */ /* Init Data */ dims[0] = NumOfDataValuesX; /* Dimensions */ dims[1] = NumOfDataValuesY; dims[2] = NumOfDataValuesZ; Origin.x = -1; /* Volume starts here */ Origin.y = -1; Origin.z = -1; Delta.x = 2.0/(int)dims[0]; /* Stepsize on Grid */ Delta.y = 2.0/(int)dims[1]; Delta.z = 2.0/(int)dims[2]; /* Create the File */ FileID = H5Fcreate(FileName, /* The FileName */ H5F_ACC_TRUNC, /* Creation Flags */ H5P_DEFAULT, /* File creation property list identifier */ H5P_DEFAULT); /* File access property list identifier */ /* Step through the Time */ for(j=0;j<NumOfTimeSteps;j++) { int x,y,z; Time = (double)(j) / (NumOfTimeSteps-1); /* Init the Field for each time step */ for(z=0; z<NumOfDataValuesZ; z++) for(y=0; y<NumOfDataValuesY; y++) for(x=0; x<NumOfDataValuesX; x++) { int i = x + NumOfDataValuesX*(y + NumOfDataValuesY*z); float X = (double)(x)/(NumOfDataValuesX-1) - 0.5, Y = (double)(y)/(NumOfDataValuesY-1) - 0.5, Z = (double)(z)/(NumOfDataValuesZ-1) - 0.5, R2 = X*X + Y*Y + Z*Z; ScalarData [i] = R2 + Time; ScalarData2[i] = R2 - Time; if (i&1) { ScalarData [i] = sqrt(R2) - Time; ScalarData2[i] = sqrt(R2) + Time*Time; } } /* Save Data to F5 File - this is the real magic */ /*-----------------------------------------------*/ /* Write the Data */ retval = F5write_uniform_cartesian3Dv( FileID, /* File identifier given by HDF5 */ Time, /* Time Value of the field */ "TimeScalar", /* Name of the Grid */ &Origin, /* Starting Point of the Volume */ &Delta, /* Spacing between Grid points */ dims, /* Number of Values in each direction */ NULL, /* Coordinate System - We use a standard chart here. */ F5P_DEFAULT, /* Default dataset properties */ "FloatData", /* Name of the first Field */ H5T_NATIVE_FLOAT, /* Type of the Values in the Field */ ScalarData, /* Pointer to the Data */ "DblData", /* Name of the second Field */ H5T_NATIVE_DOUBLE, /* Type of the Values in the Field */ ScalarData2, /* Pointer to the Data */ NULL, /* To end the List of Fields */ H5T_NATIVE_DOUBLE, /* Whatever - does not matter */ NULL /* To end the List of Fields */ ); /* Error while writing? */ if (retval != 2) { printf("\nNot all Fields written.\n"); break; } } /* Close the File */ F5Xclose(FileID); } void ManyGridMultiScalar(const char* FileName) { /* Needed Variables */ int i, j, retval, g; int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ; hid_t FileID; /* HDF5-Identifiers for opened File and opened DataSet */ hsize_t dims[3]; /* Number of Values in each dimension */ F5_vec3_point_t Origin; /* Starting Point of the Volume */ F5_vec3_float_t Delta; /* Stepsize between 2 Points on the Grid */ float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ double ScalarData2[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ]; /* The Scalar Data to be saved */ double Time = 0; /* Used to store the time value */ const char gridnames[3][20] = { "FirstGrid", "SecondGrid", "ThirdGrid" }; /* Create the File */ FileID = H5Fcreate(FileName, /* The FileName */ H5F_ACC_TRUNC, /* Creation Flags */ H5P_DEFAULT, /* File creation property list identifier */ H5P_DEFAULT); /* File access property list identifier */ for(g = 0; g<3; g++) { double Scale = 1.0/(g+1); /* Init Data */ dims[0] = NumOfDataValuesX; /* Dimensions */ dims[1] = NumOfDataValuesY; dims[2] = NumOfDataValuesZ; Origin.x = -Scale; /* Volume starts here */ Origin.y = -Scale; Origin.z = -Scale; Delta.x = 2*Scale/(int)(dims[0]-1); /* Stepsize on Grid */ Delta.y = 2*Scale/(int)(dims[1]-1); Delta.z = 2*Scale/(int)(dims[2]-1); /* Step through the Time */ for(j=0;j<NumOfTimeSteps;j++) { int x,y,z; Time = (double)(j) / (NumOfTimeSteps-1); /* Init the Field for each time step */ for(z=0; z<NumOfDataValuesZ; z++) for(y=0; y<NumOfDataValuesY; y++) for(x=0; x<NumOfDataValuesX; x++) { int i = x + NumOfDataValuesX*(y + NumOfDataValuesY*z); float X = (double)(x)/(NumOfDataValuesX-1) - 0.5, Y = (double)(y)/(NumOfDataValuesY-1) - 0.5, Z = (double)(z)/(NumOfDataValuesZ-1) - 0.5, R2 = X*X + Y*Y + Z*Z; ScalarData [i] = R2 + Time; ScalarData2[i] = R2 - Time; if (i&1) { ScalarData [i] = sqrt(R2) - Time; ScalarData2[i] = sqrt(R2) + Time*Time; } } /* Save Data to F5 File - this is the real magic */ /*-----------------------------------------------*/ /* Write the Data */ retval = F5write_uniform_cartesian3Dv( FileID, /* File identifier given by HDF5 */ Time, /* Time Value of the field */ gridnames[g], /* Name of the Grid */ &Origin, /* Starting Point of the Volume */ &Delta, /* Spacing between Grid points */ dims, /* Number of Values in each direction */ NULL, /* Coordinate System - We use a standard chart here. */ F5P_DEFAULT, /* Default dataset properties */ "FloatData", /* Name of the first Field */ H5T_NATIVE_FLOAT, /* Type of the Values in the Field */ ScalarData, /* Pointer to the Data */ "DblData", /* Name of the second Field */ H5T_NATIVE_DOUBLE, /* Type of the Values in the Field */ ScalarData2, /* Pointer to the Data */ NULL, /* To end the List of Fields */ H5T_NATIVE_DOUBLE, /* Whatever - does not matter */ NULL /* To end the List of Fields */ ); /* Error while writing? */ if (retval != 2) { printf("\nNot all Fields written.\n"); break; } } } /* Close the File */ F5Xclose(FileID); } void StaticScalarCellCentered(const char* FileName) { /* Needed Variables */ int ix,iy,iz; hid_t FileID, DataID; /* HDF5-Identifiers for opened File and opened DataSet */ hsize_t dims[3]; /* Number of Values in each dimension */ F5_vec3_point_t start, /* Starting Point of the Volume */ end; F5Path *fpath; #define NumOfDataValues ((NumOfDataValuesX-1)*(NumOfDataValuesY-1)*(NumOfDataValuesZ-1)) float ScalarData[NumOfDataValues]; /* The Scalar Data to be saved */ /* Init Data */ dims[2] = NumOfDataValuesX; /* Dimensions */ dims[1] = NumOfDataValuesY; dims[0] = NumOfDataValuesZ; start.x = -1; /* Volume starts here */ start.y = -1; start.z = -1; end.x = +1; /* Volume ends here */ end.y = +1; end.z = +1; /* Init the Field */ for(iz=0; iz<NumOfDataValuesZ-1; iz++) for(iy=0; iy<NumOfDataValuesY-1; iy++) for(ix=0; ix<NumOfDataValuesX-1; ix++) { ScalarData[ix + (NumOfDataValuesX-1) * (iy + iz * (NumOfDataValuesY-1) )] = (float)(ix+iy+iz); } /* Save Data to F5 File - this is the real magic */ /*-----------------------------------------------*/ /* Create the File */ FileID = H5Fcreate(FileName, /* The FileName */ H5F_ACC_TRUNC, /* Creation Flags */ H5P_DEFAULT, /* File creation property list identifier */ H5P_DEFAULT); /* File access property list identifier */ fpath = F5Rcreate_uniform_cartesian3Dbbox(FileID, /* File identifier given by HDF5 */ 3.141592, /* Time Value of the field - not used here */ "ExampleDataBox", /* Name of the Grid */ &start, /* Starting Point of the Volume */ &end, /* Spacing between Grid points */ dims, /* Number of Vertices in each direction */ NULL); /* Coordinate System - We use a standard chart here. */ #if 0 /* Write the Data */ fpath = F5Fwrite_uniform_cartesian3D( FileID, /* File identifier given by HDF5 */ 3.141592, /* Time Value of the field - not used here */ "SimpleScalar", /* Name of the Grid */ &Origin, /* Starting Point of the Volume */ &Delta, /* Spacing between Grid points */ dims, /* Number of Values in each direction */ "TestField", /* Name of the Field */ H5T_NATIVE_FLOAT, /* Type of the Values in the Field */ ScalarData, /* Pointer to the Data */ NULL, /* Coordinate System - We use a standard chart here. */ F5P_DEFAULT); /* Default dataset properties */ #endif F5close(fpath); /* Close the DataSet */ /* Close the File */ H5Fclose(FileID); } int main(int argc, char*argv[]) { /* Most simple example - a static scalar field */ // StaticScalar("StaticScalar.f5"); /* We are getting a bit more complicated: A time-dependent scalar field, where the Grid remains constant. */ TimeScalar("TimeDependentScalar.f5"); /* More than one field: Two time-dependent scalar fields on the same Grid. */ MultiScalar("MultiScalar.f5"); /* Three grids, two fields, time dependent. */ ManyGridMultiScalar("ManyGridMultiScalar.f5"); StaticScalarCellCentered("CellScalar.f5"); /*printf("Size float: %d\nSize H5T_NATIVE_FLOAT: %d\n", sizeof(float), sizeof(H5T_NATIVE_FLOAT));*/ return 0; }