Databox *CompFlux( Databox *k, Databox *h) { Databox *flux; int nx, ny, nz; double x, y, z; double dx, dy, dz; double *fluxp, *kp, *hp; double qxp, qxm, qyp, qym, qzp, qzm; int cell, cell_xm1, cell_xp1, cell_ym1, cell_yp1, cell_zm1, cell_zp1; int ii, jj, kk; nx = DataboxNx(k); ny = DataboxNy(k); nz = DataboxNz(k); x = DataboxX(k); y = DataboxY(k); z = DataboxZ(k); dx = DataboxDx(k); dy = DataboxDy(k); dz = DataboxDz(k); #if 0 /* ADD LATER */ if ((dx != DataboxDx(h)) || (dy != DataboxDy(h)) || (dz != DataboxDz(h))) { Error("Spacings are not compatible\n"); return NULL; } #endif if ((flux = NewDatabox(nx, ny, nz, x, y, z, dx, dy, dz)) == NULL) return((Databox*)NULL); kp = DataboxCoeffs(k); hp = DataboxCoeffs(h); fluxp = DataboxCoeffs(flux); cell = 0; cell_xm1 = cell - 1; cell_xp1 = cell + 1; cell_ym1 = cell - nx; cell_yp1 = cell + nx; cell_zm1 = cell - nx * ny; cell_zp1 = cell + nx * ny; kp += nx * ny; hp += nx * ny; fluxp += nx * ny; for (kk = 1; kk < (nz - 1); kk++) { kp += nx; hp += nx; fluxp += nx; for (jj = 1; jj < (ny - 1); jj++) { kp++; hp++; fluxp++; for (ii = 1; ii < (nx - 1); ii++) { qxp = -Mean(kp[cell_xp1], kp[cell]) * (hp[cell_xp1] - hp[cell]) / dx; qxm = -Mean(kp[cell], kp[cell_xm1]) * (hp[cell] - hp[cell_xm1]) / dx; qyp = -Mean(kp[cell_yp1], kp[cell]) * (hp[cell_yp1] - hp[cell]) / dy; qym = -Mean(kp[cell], kp[cell_ym1]) * (hp[cell] - hp[cell_ym1]) / dy; qzp = -Mean(kp[cell_zp1], kp[cell]) * (hp[cell_zp1] - hp[cell]) / dz; qzm = -Mean(kp[cell], kp[cell_zm1]) * (hp[cell] - hp[cell_zm1]) / dz; fluxp[cell] = (qxp - qxm) * dy * dz + (qyp - qym) * dx * dz + (qzp - qzm) * dx * dy; kp++; hp++; fluxp++; } kp++; hp++; fluxp++; } kp += nx; hp += nx; fluxp += nx; } return flux; }
Databox *ReadAVSField( char *file_name, double default_value) { Databox *v; FILE *fp; char *header = NULL; int header_size = 0; int count; int tokens; int c; char **token, **value; char begin[64]; int i, j; int NX = -1, NY = -1, NZ = -1; int datatype = -1; /* 0 = byte, 1 = int, 2 = float, 3 = double */ int is_rect = 0; int ndim = -1; int veclen = 1; double min[3], max[3]; double X, Y, Z; double DX, DY, DZ; double *ptr; /* open the input file */ if ((fp = fopen(file_name, "rb")) == NULL) return NULL; /* read in header info */ fgets(begin,64,fp); if (strncmp(begin,"# AVS",5) != 0) return NULL; while (begin[strlen(begin)-1] != '\n') fgets(begin,64,fp); header_size = HEADER_SIZE_INIT; header = (char *)malloc(header_size); count = 0; while ((c = fgetc(fp)) != EOF) { if (count == header_size) { header_size += HEADER_SIZE_INC; header = (char *)realloc(header,header_size); } if (c == 0xC) { /* end of header */ header[count] = '\0'; break; } else header[count++] = c; } fgetc(fp); /* == 0xC */ if (parse_fld_header(header,&tokens,&token,&value) != 1) return NULL; /* extract relevant info from token/value pairs */ for (i = 0; i < tokens; i++) { if (strcmp(token[i],"dim1") == 0) NX = atoi(value[i]); else if (strcmp(token[i],"dim2") == 0) NY = atoi(value[i]); else if (strcmp(token[i],"dim3") == 0) NZ = atoi(value[i]); else if (strcmp(token[i],"ndim") == 0) { switch (ndim = atoi(value[i])) { case 1: NY = 1; case 2: NZ = 1; case 3: break; default: free_pairs(tokens,token,value); return NULL; } } else if (strcmp(token[i],"veclen") == 0) { if ((veclen = atoi(value[i])) < 1) { free_pairs(tokens,token,value); return NULL; } } else if (strcmp(token[i],"field") == 0 && strcmp(value[i],"uniform") != 0) { if (strcmp(value[i],"rectilinear") == 0) /* treat rectilinear grid as uniform to convert */ is_rect = 1; else { free_pairs(tokens,token,value); return NULL; } } else if (strcmp(token[i],"data") == 0) { if (strcmp(value[i],"byte") == 0) datatype = 0; else if (strcmp(value[i],"int") == 0 || strcmp(value[i],"integer") == 0) datatype = 1; else if (strcmp(value[i],"float") == 0 || strcmp(value[i],"single") == 0) datatype = 2; else if (strcmp(value[i],"double") == 0) datatype = 3; else { free_pairs(tokens,token,value); return NULL; } } } free_pairs(tokens,token,value); free(header); if (NX == -1 || NY == -1 || NZ == -1) { return NULL; } if (datatype == -1) { /* datatype was not set */ return NULL; } /* create the new databox structure */ /* set X, Y, Z, DX, DY, DZ to 0 initially; will calculate and set later */ if ((v = NewDataboxDefault(NX, NY, NZ, 0, 0, 0, 0, 0, 0, default_value)) == NULL) { fclose(fp); return((Databox *)NULL); } { /* get data values */ char *barray; int *iarray; float *farray; double *darray; barray = (char *)malloc(veclen); iarray = (int *)malloc(veclen*sizeof(int)); farray = (float *)malloc(veclen*sizeof(float)); darray = (double *)malloc(veclen*sizeof(double)); ptr = DataboxCoeffs(v); for (i = 0; i < NX*NY*NZ; i++) { switch (datatype) { case 0: { fread(barray,sizeof(char),veclen,fp); for (j = 0; j < veclen; j++) darray[j] = barray[j]; break; } case 1: { fread(iarray,sizeof(int),veclen,fp); for (j = 0; j < veclen; j++) darray[j] = iarray[j]; break; } case 3: { fread(darray,sizeof(double),veclen,fp); break; } case 2: default: { fread(farray,sizeof(float),veclen,fp); for (j = 0; j < veclen; j++) darray[j] = farray[j]; break; } } /* use first component by default */ ptr[i] = darray[0]; } } if (is_rect) { /* hack in rectilinear data by keeping the min and max coordinates */ float *f; int xcoords, ycoords, zcoords; int coords; xcoords = NX; ycoords = NY; zcoords = NZ; switch (ndim) { case 1: ycoords = 0; case 2: zcoords = 0; break; case 3: break; default: return NULL; } coords = xcoords + ycoords + zcoords; f = (float *)malloc((coords)*sizeof(float)); fread(f,sizeof(float),coords,fp); min[0] = f[0]; max[0] = f[xcoords-1]; if (ndim == 2) min[2] = max[2] = 0; else min[2] = f[xcoords+ycoords]; max[2] = f[xcoords+ycoords+zcoords-1]; if (ndim == 1) min[1] = max[1] = 0; else min[1] = f[xcoords]; max[1] = f[xcoords+ycoords-1]; free(f); } else { /* true uniform data */ float f[6]; fread(f,sizeof(float),6,fp); min[0] = f[0]; min[1] = f[2]; min[2] = f[4]; max[0] = f[1]; max[1] = f[3]; max[2] = f[5]; } X = min[0]; Y = min[1]; Z = min[2]; /* set DX/DY/DZ to 1 if grid is only 1 wide in that dimension */ if (NX != 1) DX = (max[0]-min[0])/(NX-1); else DX = 1; if (NY != 1) DY = (max[1]-min[1])/(NY-1); else DY = 1; if (NZ != 1) DZ = (max[2]-min[2])/(NZ-1); else DZ = 1; DataboxX(v) = X; DataboxY(v) = Y; DataboxZ(v) = Z; DataboxDx(v) = DX; DataboxDy(v) = DY; DataboxDz(v) = DZ; fclose(fp); return v; }