示例#1
0
文件: moden.c 项目: hellabyte/semtex
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;
}
示例#2
0
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;
}