Writing simple scalar fields on a uniform grid.
#include <hdf5.h>
#include <F5/F5uniform.h>
#include <F5/F5X.h>
#include <math.h>
#if 1
#define NumOfDataValuesX 53
#define NumOfDataValuesY 19
#define NumOfDataValuesZ 31
#define NumOfTimeSteps 17
#else
#define NumOfDataValuesX 512
#define NumOfDataValuesY 512
#define NumOfDataValuesZ 512
#define NumOfTimeSteps 4
#endif
void StaticScalar(const char* FileName)
{
int i;
int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ;
hid_t FileID, DataID;
hsize_t dims[3];
float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ];
dims[0] = NumOfDataValuesX;
dims[1] = NumOfDataValuesY;
dims[2] = NumOfDataValuesZ;
Delta.
x = 2.0/(int)dims[0];
Delta.
y = 2.0/(int)dims[1];
Delta.
z = 2.0/(int)dims[2];
for(i=0;i<NumOfDataValues;i++)
{
ScalarData[i] = (float)i;
}
FileID = H5Fcreate(FileName,
H5F_ACC_TRUNC,
H5P_DEFAULT,
H5P_DEFAULT);
FileID,
3.141592,
"SimpleScalar",
&Origin,
&Delta,
dims,
"TestField",
H5T_NATIVE_FLOAT,
ScalarData,
NULL,
F5P_DEFAULT);
H5Fclose(FileID);
}
void TimeScalar(const char* FileName)
{
int i, j;
hid_t FileID;
hsize_t dims[3];
float *ScalarData = (float*)malloc(sizeof(float)*NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ);
dims[0] = NumOfDataValuesX;
dims[1] = NumOfDataValuesY;
dims[2] = NumOfDataValuesZ;
Origin.x = -1;
Origin.y = -1;
Origin.z = -1;
Delta.x = 2.0/(dims[0]-1);
Delta.y = 2.0/(dims[1]-1);
Delta.z = 2.0/(dims[2]-1);
FileID = H5Fcreate(FileName,
H5F_ACC_TRUNC,
H5P_DEFAULT,
H5P_DEFAULT);
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);
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;
}
FileID,
Time,
"TimeScalar",
&Origin,
&Delta,
dims,
"TestField",
H5T_NATIVE_FLOAT,
ScalarData,
NULL,
F5P_DEFAULT);
}
free( ScalarData );
}
void MultiScalar(const char* FileName)
{
int i, j, retval;
int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ;
hid_t FileID;
hsize_t dims[3];
float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ];
double ScalarData2[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ];
double Time = 0;
dims[0] = NumOfDataValuesX;
dims[1] = NumOfDataValuesY;
dims[2] = NumOfDataValuesZ;
Origin.x = -1;
Origin.y = -1;
Origin.z = -1;
Delta.x = 2.0/(int)dims[0];
Delta.y = 2.0/(int)dims[1];
Delta.z = 2.0/(int)dims[2];
FileID = H5Fcreate(FileName,
H5F_ACC_TRUNC,
H5P_DEFAULT,
H5P_DEFAULT);
for(j=0;j<NumOfTimeSteps;j++)
{
int x,y,z;
Time = (double)(j) / (NumOfTimeSteps-1);
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;
}
}
FileID,
Time,
"TimeScalar",
&Origin,
&Delta,
dims,
NULL,
F5P_DEFAULT,
"FloatData",
H5T_NATIVE_FLOAT,
ScalarData,
"DblData",
H5T_NATIVE_DOUBLE,
ScalarData2,
NULL,
H5T_NATIVE_DOUBLE,
NULL
);
if (retval != 2)
{
printf("\nNot all Fields written.\n");
break;
}
}
}
void ManyGridMultiScalar(const char* FileName)
{
int i, j, retval, g;
int NumOfDataValues = NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ;
hid_t FileID;
hsize_t dims[3];
float ScalarData[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ];
double ScalarData2[NumOfDataValuesX*NumOfDataValuesY*NumOfDataValuesZ];
double Time = 0;
const char gridnames[3][20] = { "FirstGrid", "SecondGrid", "ThirdGrid" };
FileID = H5Fcreate(FileName,
H5F_ACC_TRUNC,
H5P_DEFAULT,
H5P_DEFAULT);
for(g = 0; g<3; g++)
{
double Scale = 1.0/(g+1);
dims[0] = NumOfDataValuesX;
dims[1] = NumOfDataValuesY;
dims[2] = NumOfDataValuesZ;
Origin.x = -Scale;
Origin.y = -Scale;
Origin.z = -Scale;
Delta.x = 2*Scale/(int)(dims[0]-1);
Delta.y = 2*Scale/(int)(dims[1]-1);
Delta.z = 2*Scale/(int)(dims[2]-1);
for(j=0;j<NumOfTimeSteps;j++)
{
int x,y,z;
Time = (double)(j) / (NumOfTimeSteps-1);
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;
}
}
FileID,
Time,
gridnames[g],
&Origin,
&Delta,
dims,
NULL,
F5P_DEFAULT,
"FloatData",
H5T_NATIVE_FLOAT,
ScalarData,
"DblData",
H5T_NATIVE_DOUBLE,
ScalarData2,
NULL,
H5T_NATIVE_DOUBLE,
NULL
);
if (retval != 2)
{
printf("\nNot all Fields written.\n");
break;
}
}
}
}
void StaticScalarCellCentered(const char* FileName)
{
int ix,iy,iz;
hid_t FileID, DataID;
hsize_t dims[3];
end;
#define NumOfDataValues ((NumOfDataValuesX-1)*(NumOfDataValuesY-1)*(NumOfDataValuesZ-1))
float ScalarData[NumOfDataValues];
dims[2] = NumOfDataValuesX;
dims[1] = NumOfDataValuesY;
dims[0] = NumOfDataValuesZ;
start.x = -1;
start.y = -1;
start.z = -1;
end.x = +1;
end.y = +1;
end.z = +1;
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);
}
FileID = H5Fcreate(FileName,
H5F_ACC_TRUNC,
H5P_DEFAULT,
H5P_DEFAULT);
fpath = F5Rcreate_uniform_cartesian3Dbbox(FileID,
3.141592,
"ExampleDataBox",
&start,
&end,
dims,
NULL);
#if 0
FileID,
3.141592,
"SimpleScalar",
&Origin,
&Delta,
dims,
"TestField",
H5T_NATIVE_FLOAT,
ScalarData,
NULL,
F5P_DEFAULT);
#endif
H5Fclose(FileID);
}
int main(int argc, char*argv[])
{
TimeScalar("TimeDependentScalar.f5");
#if 0
MultiScalar("MultiScalar.f5");
ManyGridMultiScalar("ManyGridMultiScalar.f5");
StaticScalarCellCentered("CellScalar.f5");
#endif
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: F5coordinates.h:58
Definition: F5coordinates.h:57