static void getdata (FILE* f, Dump* h) /* ------------------------------------------------------------------------- * * Find the number of fields, allocate storage & do binary read. * ------------------------------------------------------------------------- */ { char localfmt[STR_MAX]; int i, swab; const int npts = h -> np * h -> np * h -> nz * h -> nel; const int nfields = strlen (h -> field); const int ntot = npts * nfields; h -> data = dmatrix (0, nfields - 1, 0, npts - 1); if (fread (h -> data[0], sizeof (double), ntot, f) != ntot) { sprintf (localfmt, "could not read fields: %s", h -> field); message (prog, localfmt, ERROR); } format (localfmt); swab = (strstr (h -> format, "big") && strstr (localfmt, "little")) || (strstr (localfmt, "big") && strstr (h -> format, "little")); if (swab) dbrev (ntot, h -> data[0], 1, h -> data[0], 1); }
int main (int argc, char** argv) /* ------------------------------------------------------------------------- * * Wrapper. * ------------------------------------------------------------------------- */ { char buf[STR_MAX], fields[STR_MAX], fmt[STR_MAX]; int i, j, n, np, nz, nel, swab = 0; int nfields, nplane; FILE *fp_in = stdin, *fp_out = stdout; double **idata, **odata, *vcmpt1, *vcmpt2; getargs (argc, argv, &fp_in); format (fmt); while (fgets (buf, STR_MAX, fp_in)) { /* -- Process header. */ fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); if (sscanf (buf, "%d%*s%d%d", &np, &nz, &nel) != 3) message (prog, "unable to read the file size", ERROR); if (nz != 2) { sprintf (fields, "input must have nz = 2 (here %1d)", nz); message (prog, fields, ERROR); } fprintf (fp_out, hdr_fmt[2], np, np, 1, nel); n = 6; while (--n) { fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); } fgets(fields, STR_MAX, fp_in); memset(fields+25, '\0', STR_MAX-25); for (nfields = 0, i = 0; i < 25; i++) if (isalpha(fields[i])) nfields++; if (!((nfields == 4) && (strstr (fields, "uvwp")))) message (prog, "input must have only fields u v w p.", ERROR); fprintf (fp_out, hdr_fmt[8], "uvwpABCDEF"); fgets (buf, STR_MAX, fp_in); for (i = 0; i < strlen (buf); i++) buf[i] = tolower (buf[i]); if (!strstr(buf, "binary")) message (prog, "input file not binary format", ERROR); if (!strstr (buf, "endian")) message (prog, "input field file in unknown binary format", WARNING); else swab = ((strstr (buf, "big") && strstr (fmt, "little")) || (strstr (fmt, "big") && strstr (buf, "little")) ); sprintf (buf, "%s %s", "binary", fmt); fprintf (fp_out, hdr_fmt[9], buf); /* -- Set sizes, allocate storage, set to zero. */ nplane = np * np * nel; idata = dmatrix (0, 3, 0, nplane * 2); odata = dmatrix (0, 9, 0, nplane); /* -- uvwpABCDEF = 10 */ dzero (4*nplane, idata[0], 1); dzero (10*nplane, odata[0], 1); /* -- Read in all data fields. */ for (i = 0; i < nfields; i++) { if (fread (idata[i], sizeof (double), nplane * 2, fp_in) != nplane * 2) message (prog, "an error occured while reading", ERROR); if (swab) dbrev (nplane*2, idata[i], 1, idata[i], 1); } /* -- Compute A. */ vcmpt1 = idata[0]; /* -- Real part of u. */ vcmpt2 = idata[0] + nplane; /* -- Imag part of u. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[4], 1, odata[4], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[4], 1, odata[4], 1); dscal (nplane, 2.0, odata[4], 1); /* -- Compute B . */ vcmpt1 = idata[0]; /* -- Real part of u. */ vcmpt2 = idata[1]; /* -- Real part of v. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[5], 1, odata[5], 1); vcmpt1 = idata[0] + nplane; /* -- Imag part of u. */ vcmpt2 = idata[1] + nplane; /* -- Imag part of v. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[5], 1, odata[5], 1); dscal (nplane, 2.0, odata[5], 1); /* -- Compute C. */ vcmpt1 = idata[1]; /* -- Real part of v. */ vcmpt2 = idata[1] + nplane; /* -- Imag part of v. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[6], 1, odata[6], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[6], 1, odata[6], 1); dscal (nplane, 2.0, odata[6], 1); /* -- Compute D . */ vcmpt1 = idata[0]; /* -- Real part of u. */ vcmpt2 = idata[2]; /* -- Real part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[7], 1, odata[7], 1); vcmpt1 = idata[0] + nplane; /* -- Imag part of u. */ vcmpt2 = idata[2] + nplane; /* -- Imag part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[7], 1, odata[7], 1); dscal (nplane, 2.0, odata[7], 1); /* -- Compute E . */ vcmpt1 = idata[1]; /* -- Real part of v. */ vcmpt2 = idata[2]; /* -- Real part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[8], 1, odata[8], 1); vcmpt1 = idata[1] + nplane; /* -- Imag part of v. */ vcmpt2 = idata[2] + nplane; /* -- Imag part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt2, 1, odata[8], 1, odata[8], 1); dscal (nplane, 2.0, odata[8], 1); /* -- Compute F. */ vcmpt1 = idata[2]; /* -- Real part of w. */ vcmpt2 = idata[2] + nplane; /* -- Imag part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[9], 1, odata[9], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[9], 1, odata[9], 1); dscal (nplane, 2.0, odata[9], 1); /* -- Compute p. */ vcmpt1 = idata[3]; /* -- Real part of p. */ vcmpt2 = idata[3] + nplane; /* -- Imag part of p. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[2], 1, odata[2], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[2], 1, odata[2], 1); dvsqrt (nplane, odata[2], 1, odata[2], 1); /* -- Compute u & v. */ vcmpt1 = idata[0]; /* -- Real part of u. */ vcmpt2 = idata[0] + nplane; /* -- Imag part of u. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[0], 1, odata[0], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[0], 1, odata[0], 1); dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[1], 1, odata[1], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[1], 1, odata[1], 1); vcmpt1 = idata[1]; /* -- Real part of v. */ vcmpt2 = idata[1] + nplane; /* -- Imag part of v. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[0], 1, odata[0], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[0], 1, odata[0], 1); dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[1], 1, odata[1], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[1], 1, odata[1], 1); vcmpt1 = idata[1]; /* -- Real part of w. */ vcmpt2 = idata[1] + nplane; /* -- Imag part of w. */ dvvtvp (nplane, vcmpt1, 1, vcmpt1, 1, odata[1], 1, odata[1], 1); dvvtvp (nplane, vcmpt2, 1, vcmpt2, 1, odata[1], 1, odata[1], 1); dvsqrt (nplane, odata[0], 1, odata[0], 1); dsmul (nplane, 0.5, odata[0], 1, odata[0], 1); dvsqrt (nplane, odata[1], 1, odata[1], 1); dsmul (nplane, 0.5, odata[1], 1, odata[1], 1); /* FIXME: compute w if needed in future. */ /* -- Write out uvwpABCDEF in binary. */ for (i = 0; i < 10; i++) if (fwrite (odata[i], sizeof (double), nplane, fp_out) != nplane) message (prog, "an error occured while writing", ERROR); freeDmatrix (idata, 0, 0); freeDmatrix (odata, 0, 0); } return EXIT_SUCCESS; }
int main (int argc, char** argv) /* ------------------------------------------------------------------------- * * Wrapper. * ------------------------------------------------------------------------- */ { char buf[STR_MAX], fields[STR_MAX], fmt[STR_MAX]; int i, j, n, np, nz, nel, mode = 0, swab = 0, cmplx = 0; int nfields, nplane, nplaneEven, nptsEven, ntot; FILE *fp_in = stdin, *fp_out = stdout; double **data, *plane, *vcmpt; getargs (argc, argv, &fp_in, &mode, &cmplx); format (fmt); while (fgets (buf, STR_MAX, fp_in)) { fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); if (sscanf (buf, "%d%*s%d%d", &np, &nz, &nel) != 3) message (prog, "unable to read the file size", ERROR); if (2 * mode > nz) { sprintf (fields, "too many modes (%1d) for input (nz = %1d)", mode, nz); message (prog, fields, ERROR); } if (cmplx && nz != 2) { sprintf (fields, "need nz = 2 with full-complex single mode (%1d)", nz); message (prog, fields, ERROR); } fprintf (fp_out, hdr_fmt[2], np, np, 1, nel); n = 6; while (--n) { fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); } fgets (fields, STR_MAX, fp_in); memset (fields+25, '\0', STR_MAX-25); for (nfields = 0, i = 0; i < 25; i++) if (isalpha(fields[i])) nfields++; if (nfields < 4) { if (!(strchr(fields, 'u') && strchr(fields, 'v'))) message (prog, "need fields u, v to compute K.E.", ERROR); } else { if (!(strchr(fields, 'u') && strchr(fields, 'v') && strchr(fields, 'w'))) message (prog, "need fields u, v, w to compute K.E.", ERROR); } fprintf (fp_out, hdr_fmt[8], "q"); fgets (buf, STR_MAX, fp_in); for (i = 0; i < strlen (buf); i++) buf[i] = tolower (buf[i]); if (!strstr(buf, "binary")) message (prog, "input file not binary format", ERROR); if (!strstr (buf, "endian")) message (prog, "input field file in unknown binary format", WARNING); else swab = ((strstr (buf, "big") && strstr (fmt, "little")) || (strstr (fmt, "big") && strstr (buf, "little")) ); sprintf (buf, "%s %s", "binary", fmt); fprintf (fp_out, hdr_fmt[9], buf); /* -- Set sizes, allocate storage. */ nplane = np * np * nel; nplaneEven = (nplane & 1) ? nplane + 1 : nplane; nptsEven = nz * nplaneEven; ntot = nfields * nptsEven; data = dmatrix (0, nfields - 1, 0, nptsEven - 1); plane = dvector (0, nplane - 1); /* -- Read in all data fields. */ dzero (ntot, data[0], 1); dzero (nplane, plane, 1); for (i = 0; i < nfields; i++) { for (j = 0; j < nz; j++) { if (fread (data[i] + j*nplaneEven, sizeof (double), nplane, fp_in) != nplane) message (prog, "an error occured while reading", ERROR); } if (swab) dbrev (nptsEven, data[i], 1, data[i], 1); if (!cmplx) dDFTr (data[i], nz, nplaneEven, +1); } /* -- Compute K.E.: start by adding in real part. */ for (i = 0; i < nfields - 1; i++) { vcmpt = data[_index (fields, 'u' + i)] + 2 * mode * nplaneEven; dvvtvp (nplane, vcmpt, 1, vcmpt, 1, plane, 1, plane, 1); } /* -- Add in imaginary part if not mode zero. */ if (mode || cmplx) { for (i = 0; i < nfields - 1; i++) { vcmpt = data[_index (fields, 'u' + i)] + (2 * mode + 1) * nplaneEven; dvvtvp (nplane, vcmpt, 1, vcmpt, 1, plane, 1, plane, 1); } } /* -- Normalize to make q = 0.5*UiUi. */ dsmul (nplane, 0.5, plane, 1, plane, 1); if (fwrite (plane, sizeof (double), nplane, fp_out) != nplane) message (prog, "an error occured while writing", ERROR); freeDmatrix (data, 0, 0); freeDvector (plane, 0); } return EXIT_SUCCESS; }
int main (int argc, char** argv) /* ------------------------------------------------------------------------- * * Wrapper. * ------------------------------------------------------------------------- */ { char buf[STR_MAX], fmt[STR_MAX]; int i, j, k, n, np, nzin, nzout, nel, nrep = 1, force = 0; int nfields, nplane, nptin, nptout, ntot, swab; FILE *fp_in = stdin, *fp_out = stdout; double *datain, *dataout, beta; getargs (argc, argv, &fp_in, &nrep, &force); format (fmt); while (fgets (buf, STR_MAX, fp_in)) { fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); if (sscanf (buf, "%d%*s%d%d", &np, &nzin, &nel) != 3) message (prog, "unable to read the file size", ERROR); if (!force) { if ((nzout = roundup (nzin)) != nzin) message (prog, "input nz does not have 2, 3, 5 factors", ERROR); nzout = roundup (nzin * nrep); } else nzout = nzin * nrep; fprintf (fp_out, hdr_fmt[2], np, np, nzout, nel); n = 4; while (n--) { fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); } fgets (buf, STR_MAX, fp_in); sscanf (buf, "%lf", &beta); beta /= nrep; fprintf (fp_out, hdr_fmt[7], beta); fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); for (nfields = 0, i = 0; i < 25; i++) if (isalnum(buf[i])) nfields++; fgets (buf, STR_MAX, fp_in); if (!strstr(buf, "binary")) message (prog, "input file not binary format", ERROR); swab = (strstr (buf, "big") && strstr (fmt, "little")) || (strstr (fmt, "big") && strstr (buf, "little")); strcat (strcpy (buf, "binary "), fmt); fprintf (fp_out, hdr_fmt[9], buf); /* -- Set sizes, allocate storage. */ nplane = np * np * nel; ntot = nplane + (nplane & 1); nptin = nzin * ntot; nptout = nzout * ntot; datain = dvector (0, nptin - 1); dataout = dvector (0, nptout - 1); /* -- Read and write all data fields. */ for (i = 0; i < nfields; i++) { dzero (nptin, datain, 1); dzero (nptout, dataout, 1); for (j = 0; j < nzin; j++) { if (fread (datain+j*ntot, sizeof (double), nplane, fp_in) != nplane) message (prog, "an error occured while reading", ERROR); if (swab) dbrev (ntot, datain+j*ntot, 1, datain+j*ntot, 1); } if (force) { /* -- We can just copy in physical space. */ for (k = 0; k < nrep; k++) { for (j = 0; j < nzin; j++) { if (fwrite (datain+j*ntot, sizeof (double), nplane, fp_out) != nplane) message (prog, "an error occured while writing", ERROR); } } } else { /* -- Have to go to Fourier space for padding. */ dDFTr (datain, nzin, ntot, FORWARD); pack (datain, nzin, dataout, nzout, nrep, ntot); dDFTr (dataout, nzout, ntot, INVERSE); for (j = 0; j < nzout; j++) if (fwrite (dataout+j*ntot, sizeof (double), nplane, fp_out) != nplane) message (prog, "an error occured while writing", ERROR); } } } freeDvector (datain, 0); freeDvector (dataout, 0); return EXIT_SUCCESS; }
static void read_data (FILE *fp) /* ------------------------------------------------------------------------- * * Read in data files. If NZ > 1, it is assumed that data are in the file * in plane-by-plane order. For each plane, the ordering of data varies * depending on whether the file is in ASCII or binary format: for ASCII, the * fields are in column order, element-by-element (row-major), whereas for * binary formats the fields are written in the file sequentially. * * Automatic conversion between little- and big-endian binary formats. * ------------------------------------------------------------------------- */ { int i, m, n, nplane; int nr_chk, ns_chk, nz_chk, nel_chk; char buf[STR_MAX], *c; /* -- Read the header down to the field list, check size of input. */ for (n = 0; n < 3; n++) { fgets (buf, STR_MAX, fp); } if (sscanf (buf, "%d%d%d%d", &nr_chk, &ns_chk, &nz_chk, &nel_chk) != 4) { fputs ("error reading size of field\n", stderr); exit (EXIT_FAILURE); } if (nr != nr_chk || ns != ns_chk || nel != nel_chk) { fputs ("2D structure of mesh and field file do not agree\n", stderr); exit (EXIT_FAILURE); } for (n = 3; n < 9; n++) fgets(buf, STR_MAX, fp); /* -- Read the list of fields. */ n = 0; c = buf; nfields = 0; while (isalpha(*c) && nfields < MAXFIELDS) type[nfields++] = (*c++); if (nfields > MAXFIELDS) { fprintf(stderr, "sem2tec: a maximum of %d fields may be converted.\n", MAXFIELDS); exit(EXIT_FAILURE); } /* -- Allocate memory. */ nplane = nr * ns * nel; for (n = 0; n < nfields; n++) data[n] = (double*) malloc (nzp * nplane * sizeof (double)); /* -- Check the format. */ c = fgets(buf, STR_MAX, fp); while (isspace(*c)) c++; switch (tolower(*c)) { /* ASCII or binary read? */ case 'a': for (m = 0; m < nz; m++) for (i = 0; i < nplane; i++) for (n = 0; n < nfields; n++) if (fscanf(fp, "%lf", data[n] + m * nplane + i) < 0) { fputs("sem2tec: field file (ASCII) read error\n", stderr); exit (EXIT_FAILURE); } break; case 'b': { int swab, machine = iformat(); swab = (strstr (buf, "little") && machine == 0 || strstr (buf, "big" ) && machine == 1 ) ? 1 : 0; for (n = 0; n < nfields; n++) { if (fread (data[n], sizeof(double), nz * nplane, fp) != nz * nplane) { fputs("sem2tec: field file (binary) read error\n", stderr); exit (EXIT_FAILURE); } if (swab) dbrev (nz * nplane, data[n], 1, data[n], 1); } break; } default: fprintf (stderr, "sem2tec: unknown format flag: '%c'\n", *c); exit (EXIT_FAILURE); break; } }
int main (int argc, char** argv) /* ------------------------------------------------------------------------- * * Wrapper. * ------------------------------------------------------------------------- */ { char buf[STR_MAX], fmt[STR_MAX]; int i, j, k, np, nz, nel, nrep = 1; int nfields, nplane, nptin, nptout, ntot, swab; FILE *fp_in = stdin, *fp_out = stdout; double *data; getargs (argc, argv, &fp_in, &nrep); format (fmt); while (fgets (buf, STR_MAX, fp_in)) { fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); fgets (buf, STR_MAX, fp_in); if (sscanf (buf, "%d%*s%d%d", &np, &nz, &nel) != 3) message (prog, "unable to read the file size", ERROR); fprintf (fp_out, hdr_fmt[2], np, np, nz, nel*nrep); i = 5; while (i--) { fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); } fgets (buf, STR_MAX, fp_in); fputs (buf, fp_out); for (nfields = 0, i = 0; i < 25; i++) if (isalnum(buf[i])) nfields++; fgets (buf, STR_MAX, fp_in); if (!strstr(buf, "binary")) message (prog, "input file not binary format", ERROR); swab = (strstr (buf, "big") && strstr (fmt, "little")) || (strstr (fmt, "big") && strstr (buf, "little")); strcat (strcpy (buf, "binary "), fmt); fprintf (fp_out, hdr_fmt[9], buf); /* -- Set sizes, allocate storage. */ nplane = np * np * nel; ntot = nz * nplane; data = dvector (0, ntot - 1); /* -- Read and write all data fields. */ for (i = 0; i < nfields; i++) { if (fread (data, sizeof (double), ntot, fp_in) != ntot) message (prog, "an error occured while reading", ERROR); if (swab) dbrev (ntot, data, 1, data, 1); for (j = 0; j < nz; j++) for (k = 0; k < nrep; k++) if (fwrite (data+j*nplane, sizeof(double), nplane, fp_out) != nplane) message (prog, "an error occured while writing", ERROR); } } freeDvector (data, 0); return EXIT_SUCCESS; }