FiberBundleHDF5  $Id: FiberHDF5.dfg,v 1.8 2006/12/12 12:32:50 werner Exp $
ScalarSimple.c

Writing simple scalar fields on a uniform grid.

#include <hdf5.h>
#include <F5/F5uniform.h>
#include <F5/F5X.h>
#include <math.h>
#if 1
//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
#else
#define NumOfDataValuesX 512
#define NumOfDataValuesY 512
#define NumOfDataValuesZ 512
//Define for the number of time steps used by some of the examples.
#define NumOfTimeSteps 4
#endif
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 */
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 = (float*)malloc(sizeof(float)*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 */
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 */
}
free( ScalarData );
/* 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 */
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 */
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 */
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");
#if 0
/* 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");
#endif
/*printf("Size float: %d\nSize H5T_NATIVE_FLOAT: %d\n", sizeof(float), sizeof(H5T_NATIVE_FLOAT));*/
return 0;
}
void F5close(F5Path *f)
Definition: F5B.c:186
herr_t F5Xclose(hid_t obj_id)
Definition: F5X.c:347
int F5write_uniform_cartesian3Dv(hid_t file_id, double time, const char *gridname, const F5_vec3_point_t *origin, const F5_vec3_float_t *spacing, hsize_t dims[3], const char *coordinate_system, hid_t property_id,...)
Definition: F5uniform.c:332
F5Path * F5Fwrite_uniform_cartesian3D(hid_t file_id, double time, const char *gridname, const F5_vec3_point_t *origin, const F5_vec3_float_t *spacing, hsize_t dims[3], const char *fieldname, hid_t fieldtype, const void *dataPtr, const char *coordinate_system, hid_t prop_id)
Definition: F5uniform.c:271
Definition: F5Path.h:31
Definition: F5coordinates.h:58
Definition: F5coordinates.h:57