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

📄 iohb.c

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 C
📖 第 1 页 / 共 5 页
字号:
           linel= strchr(line,'\n')-line;
           col = 0;
       }
       col += Rhswidth;
    }
    if (Rhsflag == 'D')  {
       while( strchr(line,'D') ) *strchr(line,'D') = 'E';
    }

/*  Read a vector of desired type, then skip to next */
/*  repeating to fill Nrhs vectors                   */

  ThisElement = (char *) malloc(Rhswidth+1);
  if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  *(ThisElement+Rhswidth) = (char) NULL;
  for (rhsi=0;rhsi<Nrhs;rhsi++) {

    for (i=0;i<Nentries;i++) {
       if ( col >= ( maxcol<linel?maxcol:linel ) ) {
           fgets(line, BUFSIZ, in_file);
           linel= strchr(line,'\n')-line;
           if (Rhsflag == 'D')  {
              while( strchr(line,'D') ) *strchr(line,'D') = 'E';
           }
           col = 0;
       }
       strncpy(ThisElement,line+col,Rhswidth);
       /*ThisElement = substr(line, col, Rhswidth);*/
          if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) { 
             /* insert a char prefix for exp */
             last = strlen(ThisElement);
             for (j=last+1;j>=0;j--) {
                ThisElement[j] = ThisElement[j-1];
                if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
                   ThisElement[j-1] = Rhsflag;                    
                   break;
                }
             }
          }
       b[i] = atof(ThisElement);
       col += Rhswidth;
    }
 
/*  Skip any interleaved Guess/eXact vectors */

    for (i=0;i<stride;i++) {
       if ( col >= ( maxcol<linel?maxcol:linel ) ) {
           fgets(line, BUFSIZ, in_file);
           linel= strchr(line,'\n')-line;
           col = 0;
       }
       col += Rhswidth;
    }

  }
  free(ThisElement);
    

    fclose(in_file);
    return Nrhs;
}

int readHB_newaux_double(const char* filename, const char AuxType, double** b)
{
        int Nrhs,M,N,nonzeros;
        char *Type;

        readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs);
        if ( Nrhs <= 0 ) {
          fprintf(stderr,"Warn: Requested read of aux vector(s) when none are present.\n");
          return 0;
        } else { 
          if ( Type[0] == 'C' ) {
            fprintf(stderr, "Warning: Reading complex aux vector(s) from HB file %s.",filename);
            fprintf(stderr, "         Real and imaginary parts will be interlaced in b[].");
            *b = (double *)malloc(M*Nrhs*sizeof(double)*2);
            if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
            return readHB_aux_double(filename, AuxType, *b);
          } else {
            *b = (double *)malloc(M*Nrhs*sizeof(double));
            if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
            return readHB_aux_double(filename, AuxType, *b);
          }
        }
}

int writeHB_mat_double(const char* filename, int M, int N, 
                        int nz, const int colptr[], const int rowind[], 
                        const double val[], int Nrhs, const double rhs[], 
                        const double guess[], const double exact[],
                        const char* Title, const char* Key, const char* Type, 
                        char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
                        const char* Rhstype)
{
/****************************************************************************/
/*  The writeHB function opens the named file and writes the specified      */
/*  matrix and optional right-hand-side(s) to that file in Harwell-Boeing   */
/*  format.                                                                 */
/*                                                                          */
/*  For a description of the Harwell Boeing standard, see:                  */
/*            Duff, et al.,  ACM TOMS Vol.15, No.1, March 1989              */
/*                                                                          */
/****************************************************************************/
    FILE *out_file;
    int i,j,entry,offset,acount,linemod;
    int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
    int nvalentries, nrhsentries;
    int Ptrperline, Ptrwidth, Indperline, Indwidth;
    int Rhsperline, Rhswidth, Rhsprec;
    int Rhsflag;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    char pformat[16],iformat[16],vformat[19],rformat[19];

    if ( Type[0] == 'C' ) {
         nvalentries = 2*nz;
         nrhsentries = 2*M;
    } else {
         nvalentries = nz;
         nrhsentries = M;
    }

    if ( filename != NULL ) {
       if ( (out_file = fopen( filename, "w")) == NULL ) {
         fprintf(stderr,"Error: Cannot open file: %s\n",filename);
         return 0;
       }
    } else out_file = stdout;

    if ( Ptrfmt == NULL ) Ptrfmt = "(8I10)";
    ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
    sprintf(pformat,"%%%dd",Ptrwidth);
    ptrcrd = (N+1)/Ptrperline;
    if ( (N+1)%Ptrperline != 0) ptrcrd++;
   
    if ( Indfmt == NULL ) Indfmt =  Ptrfmt;
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    sprintf(iformat,"%%%dd",Indwidth);
    indcrd = nz/Indperline;
    if ( nz%Indperline != 0) indcrd++;

    if ( Type[0] != 'P' ) {          /* Skip if pattern only  */
      if ( Valfmt == NULL ) Valfmt = "(4E20.13)";
      ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
      if (Valflag == 'D') *strchr(Valfmt,'D') = 'E';
      if (Valflag == 'F')
         sprintf(vformat,"%% %d.%df",Valwidth,Valprec);
      else
         sprintf(vformat,"%% %d.%dE",Valwidth,Valprec);
      valcrd = nvalentries/Valperline;
      if ( nvalentries%Valperline != 0) valcrd++;
    } else valcrd = 0;

    if ( Nrhs > 0 ) {
       if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
       ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
       if (Rhsflag == 'F')
          sprintf(rformat,"%% %d.%df",Rhswidth,Rhsprec);
       else
          sprintf(rformat,"%% %d.%dE",Rhswidth,Rhsprec);
       if (Rhsflag == 'D') *strchr(Rhsfmt,'D') = 'E';
       rhscrd = nrhsentries/Rhsperline; 
       if ( nrhsentries%Rhsperline != 0) rhscrd++;
       if ( Rhstype[1] == 'G' ) rhscrd+=rhscrd;
       if ( Rhstype[2] == 'X' ) rhscrd+=rhscrd;
       rhscrd*=Nrhs;
    } else rhscrd = 0;

    totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;


/*  Print header information:  */

    fprintf(out_file,"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
            ptrcrd, indcrd, valcrd, rhscrd);
    fprintf(out_file,"%3s%11s%14d%14d%14d\n",Type,"          ", M, N, nz);
    fprintf(out_file,"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
    if ( Nrhs != 0 ) {
/*     Print Rhsfmt on fourth line and                                    */
/*           optional fifth header line for auxillary vector information: */
       fprintf(out_file,"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
    } else fprintf(out_file,"\n");

    offset = 1-_SP_base;  /* if base 0 storage is declared (via macro definition), */
                          /* then storage entries are offset by 1                  */

/*  Print column pointers:   */
    for (i=0;i<N+1;i++)
    {
       entry = colptr[i]+offset;
       fprintf(out_file,pformat,entry);
       if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,"\n");
    }

   if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,"\n");

/*  Print row indices:       */
    for (i=0;i<nz;i++)
    {
       entry = rowind[i]+offset;
       fprintf(out_file,iformat,entry);
       if ( (i+1)%Indperline == 0 ) fprintf(out_file,"\n");
    }

   if ( nz % Indperline != 0 ) fprintf(out_file,"\n");

/*  Print values:            */

    if ( Type[0] != 'P' ) {          /* Skip if pattern only  */

    for (i=0;i<nvalentries;i++)
    {
       fprintf(out_file,vformat,val[i]);
       if ( (i+1)%Valperline == 0 ) fprintf(out_file,"\n");
    }

    if ( nvalentries % Valperline != 0 ) fprintf(out_file,"\n");

/*  If available,  print right hand sides, 
           guess vectors and exact solution vectors:  */
    acount = 1;
    linemod = 0;
    if ( Nrhs > 0 ) {
       for (i=0;i<Nrhs;i++)
       {
          for ( j=0;j<nrhsentries;j++ ) {
            fprintf(out_file,rformat,rhs[j]);
            if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
          }
          if ( acount%Rhsperline != linemod ) {
            fprintf(out_file,"\n");
            linemod = (acount-1)%Rhsperline;
          }
          rhs += nrhsentries;
          if ( Rhstype[1] == 'G' ) {
            for ( j=0;j<nrhsentries;j++ ) {
              fprintf(out_file,rformat,guess[j]);
              if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
            }
            if ( acount%Rhsperline != linemod ) {
              fprintf(out_file,"\n");
              linemod = (acount-1)%Rhsperline;
            }
            guess += nrhsentries;
          }
          if ( Rhstype[2] == 'X' ) {
            for ( j=0;j<nrhsentries;j++ ) {
              fprintf(out_file,rformat,exact[j]);
              if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
            }
            if ( acount%Rhsperline != linemod ) {
              fprintf(out_file,"\n");
              linemod = (acount-1)%Rhsperline;
            }
            exact += nrhsentries;
          }
       }
    }

    }

    if ( fclose(out_file) != 0){
      fprintf(stderr,"Error closing file in writeHB_mat_double().\n");
      return 0;
    } else return 1;
    
}

int readHB_mat_char(const char* filename, int colptr[], int rowind[], 
                                           char val[], char* Valfmt)
{
/****************************************************************************/
/*  This function opens and reads the specified file, interpreting its      */
/*  contents as a sparse matrix stored in the Harwell/Boeing standard       */
/*  format and creating compressed column storage scheme vectors to hold    */
/*  the index and nonzero value information.                                */
/*                                                                          */
/*    ----------                                                            */
/*    **CAVEAT**                                                            */
/*    ----------                                                            */
/*  Parsing real formats from Fortran is tricky, and this file reader       */
/*  does not claim to be foolproof.   It has been tested for cases when     */
/*  the real values are printed consistently and evenly spaced on each      */
/*  line, with Fixed (F), and Exponential (E or D) formats.                 */
/*                                                                          */
/*  **  If the input file does not adhere to the H/B format, the  **        */
/*  **             results will be unpredictable.                 **        */
/*                                                                          */
/****************************************************************************/
    FILE *in_file;
    int i,j,ind,col,offset,count,last;
    int Nrow,Ncol,Nnzero,Nentries,Nrhs;
    int Ptrcrd, Indcrd, Valcrd, Rhscrd;
    int Ptrperline, Ptrwidth, Indperline, Indwidth;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    char* ThisElement;
    char line[BUFSIZ];
    char Title[73], Key[8], Type[4], Rhstype[4];
    char Ptrfmt[17], Indfmt[17], Rhsfmt[21];

    if ( (in_file = fopen( filename, "r")) == NULL ) {
       fprintf(stderr,"Error: Cannot open file: %s\n",filename);
       return 0;
    }

    readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                  Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                  &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);

/*  Parse the array input formats from Line 3 of HB file  */
    ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    if ( Type[0] != 'P' ) {          /* Skip if pattern only  */
       ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
       if (Valflag == 'D') {
          *strchr(Valfmt,'D') = 'E';
       }
    }

/*  Read column pointer array:   */

⌨️ 快捷键说明

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