⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gmvread.c

📁 一个用来实现偏微分方程中网格的计算库
💻 C
📖 第 1 页 / 共 5 页
字号:
{  long pos_after_keyword;  int base_ftype;  void checkfromfile();  FILE *basefile;     basefile = gmvin;   base_ftype = ftype;   pos_after_keyword = ftell(gmvin);   checkfromfile();   if (gmvin == basefile)     {      if (fromfileflag == 0)         fseek(gmvin, pos_after_keyword, SEEK_SET);      return;     }   else     {      /*  Save current data.  */      gmvin_sav = basefile;      ftype_sav = base_ftype;      /*  Skip to fkeyword.  */      ff_keyword = fkeyword;      reading_fromfile = 1;      while (9)        {         gmvread_data();         if (gmv_data.keyword == fkeyword) fromfileflag = 1;         if (gmv_data.keyword == fkeyword) break;        }     }    return;}void endfromfile(){   /*  Reset current data.  */   ftype = ftype_sav;   fromfileskip = 0;   fclose(gmvin);   gmvin = gmvin_sav;   fromfileflag = 0;   reading_fromfile = 0;   ff_keyword = -1;   skipflag = 0;}void checkfromfile(){  char c, charptr[300], *charptr2, tmpbuf[200], stringbuf[100];  int i, ierr, fkeyword;   /*  Check for "from".  */   if (ftype != ASCII)      binread(stringbuf, charsize, CHAR, (long)4, gmvin);   else      fscanf(gmvin,"%s",stringbuf);   if (strncmp("from",stringbuf, 4) != 0) return;   if (ftype != ASCII)     {      /* Gobble the "file" part */      binread(&wordbuf, intsize, WORD, (long)1, gmvin);      /* Get the doublequote */      binread(&c, charsize, CHAR, (long)1, gmvin);      for (i = 0; 1; i++)        {         binread(&c, charsize, CHAR, (long)1, gmvin);         if (c == '"') break;         tmpbuf[i] = c;         tmpbuf[i + 1] = '\0';        }      charptr2 = tmpbuf;     }   if (ftype == ASCII)      {      fscanf(gmvin,"%s",tmpbuf);      charptr2 = strpbrk(&tmpbuf[1],"\"");      *charptr2 = '\0';      charptr2 = &tmpbuf[1];     }   /*  If the first character of the fromfile filename is    */   /*  not /, then add the input directory to the filename.  */   if (strncmp(charptr2,"/",1) != 0)     {      strcpy(charptr,input_dir);      strcat(charptr,charptr2);     }   else      strcpy(charptr,charptr2);   /*  Only returning fromfile filename.  */   if (fromfileskip == 1 && fromfileflag == 0)     {      /*  Check nodes, cells, faces and surface    */      /*  and read numbers for the other keywords.  */      if ((curr_keyword == NODES && nodes_read == 0) ||           (curr_keyword == CELLS && cells_read == 0) ||          (curr_keyword == FACES && faces_read == 0) ||           (curr_keyword == VFACES && faces_read == 0) ||           (curr_keyword == XFACES && faces_read == 0) ||           (curr_keyword == SURFACE && surface_read == 0))        {         gmvin_sav = gmvin;         ftype_sav = ftype;         ierr = gmvread_open_fromfileskip(charptr);         if (ierr > 0)            {             fprintf(stderr,"GMV cannot read fromfile %s\n",charptr);             exit(0);            }         /*  Skip to fkeyword.  */         ff_keyword = curr_keyword;         fkeyword = curr_keyword;         reading_fromfile = 1;         while (9)           {            gmvread_data();            if (gmv_data.keyword == fkeyword) break;           }         if (ff_keyword > -1)           {            fclose(gmvin);            gmvin = gmvin_sav;            ftype = ftype_sav;           }         skipflag = 0;         reading_fromfile = 0;         ff_keyword = -1;         readkeyword = 1;         fromfileskip = 1;        }      fromfileflag = 1;      gmv_data.keyword = curr_keyword;      gmv_data.datatype = FROMFILE;      i = strlen(charptr);      gmv_data.nchardata1 = i;      gmv_data.chardata1 = (char *)malloc(i*sizeof(char));      strcpy(gmv_data.chardata1,charptr);      return;     }   if (fromfileskip == 1 && fromfileflag == 1) return;   /*  Open fromfile, but do not allow more fromfiles.  */   ierr = gmvread_open_fromfileskip(charptr);   if (ierr > 0)      {       fprintf(stderr,"GMV cannot read fromfile %s\n",charptr);       exit(0);      }   printf("GMV reading %s from fromfile %s\n",sav_keyword,charptr);   return;}void readnodes(FILE* gmvin, int ftype){  int i, k, iswap, lnxv, lnyv, lnzv, lstructuredflag;  long lnodes, tmplnodes;  double *lxic, *lyic, *lzic, *tmpdouble;  long pos_after_lnodes, exp_cell_pos;  float *tmpfloat;  char ckkeyword[9];   if (ftype == ASCII)     {      fscanf(gmvin,"%ld",&lnodes);      ioerrtst(gmvin);     }   else     {      if (ftype == IEEEI8R4 || ftype == IEEEI8R8)        {                  binread(&lnodes,longlongsize,LONGLONG,(long)1,gmvin);        }      else        {                  binread(&i,intsize,INT,(long)1,gmvin);         iswap = i;         swapbytes(&iswap, intsize, 1);         lnodes = i;        }     }   /*  Check for byte swapping.  */   if (lnodes < -10 && ftype != ASCII)     {      swapbytes_on = 1;      if (ftype == IEEEI8R4 || ftype == IEEEI8R8)         swapbytes(&lnodes, longlongsize, 1);      else         lnodes = iswap;     }   /* Is more than the bottom half of the word full? */   else if (ftype != ASCII && lnodes > 0xffff)      {      tmplnodes = lnodes;      if (ftype == IEEEI8R4 || ftype == IEEEI8R8)         swapbytes(&tmplnodes, longlongsize, 1);      else          tmplnodes = iswap;      if (tmplnodes == -2)        {         /* Byte swapped, structured grid */         lnodes = tmplnodes;         swapbytes_on = 1;        }      else         {            /*  Unstructured grid, seek to "cells" keyword and see     */         /*  if it is where we expect it. If so, not byte swapped.  */                  pos_after_lnodes = ftell(gmvin);         if (ftype == IEEEI4R8 || ftype == IEEEI8R8)            exp_cell_pos = 3 * lnodes * doublesize;         else            exp_cell_pos = 3 * lnodes * floatsize;         fseek(gmvin, exp_cell_pos, SEEK_CUR);         if (ftype != ASCII)           {            binread(ckkeyword,charsize,CHAR,(long)8,gmvin);            *(ckkeyword+8)=(char)0;           }         if (ftype == ASCII) fscanf(gmvin,"%s",ckkeyword);         if (strncmp(ckkeyword,"cells",5) != 0 &&             strncmp(ckkeyword,"faces",5) != 0 &&             strncmp(ckkeyword,"xfaces",6) != 0 &&             strncmp(ckkeyword,"endgmv",6) != 0)           {            /* We are not where expected, must be byte swapped */            swapbytes_on = 1;            if (ftype == IEEEI8R4 || ftype == IEEEI8R8)               swapbytes(&lnodes, longlongsize, 1);            else                lnodes = iswap;           }         /* Go back to the previous position */         fseek(gmvin, pos_after_lnodes, SEEK_SET);        }     }   else if (ftype != ASCII && lnodes == -1)     {      /* Special case, -1 is still -1 if byte swapped */               pos_after_lnodes = ftell(gmvin);      binread(&lnxv,intsize,INT,(long)1,gmvin);      binread(&lnyv,intsize,INT,(long)1,gmvin);      binread(&lnzv,intsize,INT,(long)1,gmvin);      if (ftype == IEEEI4R8 || ftype == IEEEI8R8)         exp_cell_pos = (lnxv + lnyv + lnzv) * doublesize;      else         exp_cell_pos = (lnxv + lnyv + lnzv) * floatsize;      fseek(gmvin, exp_cell_pos, SEEK_CUR);      if (ftype != ASCII)        {         binread(ckkeyword,charsize,CHAR,(long)8,gmvin);         *(ckkeyword+8)=(char)0;        }      if (ftype == ASCII) fscanf(gmvin,"%s",ckkeyword);      if (strncmp(ckkeyword,"cells",5) != 0 &&          strncmp(ckkeyword,"faces",5) != 0 &&          strncmp(ckkeyword,"vfaces",6) != 0)        {         /* We are not where expected, must be byte swapped */         swapbytes_on = 1;        }       /* Go back to the previous position */       fseek(gmvin, pos_after_lnodes, SEEK_SET);     }   /*  If lnodes is -1, then block structured grid.      */   /*  If lnodes is -2, then logically structured grid.  */   /*  Read lnxv, lnyv, lnzv. for structured grids.      */   lstructuredflag = 0;   structflag_in = 0;   if (lnodes == -1 || lnodes == -2)     {      if (lnodes == -1) lstructuredflag = 1;      if (lnodes == -2) lstructuredflag = 2;      if (ftype != ASCII)        {         binread(&lnxv,intsize,INT,(long)1,gmvin);         binread(&lnyv,intsize,INT,(long)1,gmvin);         binread(&lnzv,intsize,INT,(long)1,gmvin);         ioerrtst(gmvin);        }      if (ftype == ASCII)         {         fscanf(gmvin,"%d%d%d",&lnxv,&lnyv,&lnzv);         ioerrtst(gmvin);        }      lnodes = lnxv * lnyv * lnzv;      structflag_in = 1;     }   /*  If lnodes is -3, then amr grid.  Read      */   /*  lnxv, lnyv, lnzv, start xyz and dx,dy,dz.  */   amrflag_in = 0;   if (lnodes == -3)     {      amrflag_in = 1;      if (ftype != ASCII)        {         binread(&lnxv,intsize,INT,(long)1,gmvin);         binread(&lnyv,intsize,INT,(long)1,gmvin);         binread(&lnzv,intsize,INT,(long)1,gmvin);         ioerrtst(gmvin);        }      if (ftype == ASCII)         {         fscanf(gmvin,"%d%d%d",&lnxv,&lnyv,&lnzv);         ioerrtst(gmvin);        }         lxic = (double *)malloc((3)*sizeof(double));      lyic = (double *)malloc((3)*sizeof(double));      if (ftype != ASCII)        {         if (ftype == IEEEI4R8 || ftype == IEEEI8R8)           {            binread(lxic,doublesize,DOUBLE,(long)3,gmvin);            ioerrtst(gmvin);            binread(lyic,doublesize,DOUBLE,(long)3,gmvin);            ioerrtst(gmvin);           }         else           {            tmpfloat = (float *)malloc((3)*sizeof(float));            binread(tmpfloat,floatsize,FLOAT,(long)3,gmvin);            ioerrtst(gmvin);            for (i = 0; i < 3; i++) lxic[i] = tmpfloat[i];            binread(tmpfloat,floatsize,FLOAT,(long)3,gmvin);            ioerrtst(gmvin);            for (i = 0; i < 3; i++) lyic[i] = tmpfloat[i];            free(tmpfloat);           }        }      if (ftype == ASCII)        {         rdfloats(lxic,(long)3,gmvin);         rdfloats(lyic,(long)3,gmvin);        }      gmv_data.keyword = NODES;      gmv_data.datatype = AMR;      gmv_data.num2 = lnxv;      gmv_data.nlongdata1 = lnyv;      gmv_data.nlongdata2 = lnzv;      gmv_data.ndoubledata1 = 3;      gmv_data.doubledata1 = lxic;      gmv_data.ndoubledata2 = 3;      gmv_data.doubledata2 = lyic;      numnodes = lnxv * lnyv * lnzv;      nodes_read = 1;      return;     }   if (printon)      printf("Reading %ld nodes.\n",lnodes);   /*  Allocate and read node x,y,z arrays.  */   if (lstructuredflag == 0 || lstructuredflag == 2)     {      lxic = (double *)malloc((lnodes)*sizeof(double));      lyic = (double *)malloc((lnodes)*sizeof(double));      lzic = (double *)malloc((lnodes)*sizeof(double));      if (lxic == NULL || lyic == NULL || lzic == NULL)        {         gmvrdmemerr();         return;        }      if (ftype != ASCII)        {         if (ftype == IEEEI4R8 || ftype == IEEEI8R8)           {            tmpdouble = (double *)malloc((3*lnodes)*sizeof(double));            if (tmpdouble == NULL)              {               gmvrdmemerr();               return;              }            binread(tmpdouble,doublesize,DOUBLE,3*lnodes,gmvin);            ioerrtst(gmvin);            if (node_inp_type == 0)  /*  nodes type  */              {               for (i = 0; i < lnodes; i++)                 {                  lxic[i] = tmpdouble[i];                  lyic[i] = tmpdouble[lnodes+i];                  lzic[i] = tmpdouble[2*lnodes+i];                 }              }            if (node_inp_type == 1)  /*  nodev type  */              {               for (i = 0; i < lnodes; i++)                 {                  lxic[i] = tmpdouble[3*i];                  lyic[i] = tmpdouble[3*i+1];                  lzic[i] = tmpdouble[3*i+2];                 }              }            FREE(tmpdouble);           }         else           {            tmpfloat = (float *)malloc((3*lnodes)*sizeof(float));            if (tmpfloat == NULL)              {               gmvrdmemerr();               return;              }            binread(tmpfloat,floatsize,FLOAT,3*lnodes,gmvin);            ioerrtst(gmvin);            if (node_inp_type == 0)  /*  nodes type  */              {               for (i = 0; i < lnodes; i++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -