Actual source code: ex81.c

  2: static char help[] = "Reads in a PETSc binary matrix and saves in Harwell-Boeing format.\n\
  3:   -fout <output_file> : file to load.\n\
  4:   -fin <input_file> : For a 5X5 example of the 5-pt. stencil,\n\
  5:                        use the file petsc/src/mat/examples/matbinary.ex\n\n";

  7: /*
  8:   Include the private file (not included by most applications) so we have direct
  9:   access to the matrix data structure.

 11:   This code is buggy! What is it doing here?
 12: */
 13:  #include src/mat/impls/aij/seq/aij.h

 17: int main(int argc,char **args)
 18: {
 20:   PetscInt       n,m,i,*ai,*aj,nz;
 21:   PetscMPIInt    size;
 22:   Mat            A;
 23:   Vec            x;
 24:   char           bfile[PETSC_MAX_PATH_LEN],hbfile[PETSC_MAX_PATH_LEN];
 25:   PetscViewer    fd;
 26:   Mat_SeqAIJ     *a;
 27:   PetscScalar    *aa,*xx;
 28:   FILE           *file;
 29:   char           head[81];

 31:   PetscInitialize(&argc,&args,(char *)0,help);

 33: #if defined(PETSC_USE_COMPLEX)
 34:   SETERRQ(1,"This example does not work with complex numbers");
 35: #endif
 36:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 37:   if (size > 1) SETERRQ(1,"Only runs on one processor");

 39:   PetscOptionsGetString(PETSC_NULL,"-fin",bfile,PETSC_MAX_PATH_LEN-1,PETSC_NULL);
 40:   PetscOptionsGetString(PETSC_NULL,"-fout",hbfile,PETSC_MAX_PATH_LEN-1,PETSC_NULL);

 42:   /* Read matrix and RHS */
 43:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,bfile,FILE_MODE_READ,&fd);
 44:   MatLoad(fd,MATSEQAIJ,&A);
 45:   VecLoad(fd,PETSC_NULL,&x);
 46:   PetscViewerDestroy(fd);

 48:   /* Format is in column storage so we print transpose matrix */
 49:   MatTranspose(A,0);

 51:   m = A->m;
 52:   n = A->n;
 53:   if (n != m) SETERRQ(1,"Only for square matrices");

 55:   /* charrage returns \n may not belong below
 56:     depends on what 80 character fixed format means to Fortran */

 58:   file = fopen(hbfile,"w"); if (!file) SETERRQ(1,"Cannot open HB file");
 59:   sprintf(head,"%-72s%-8s\n","Title","Key");
 60:   fprintf(file,head);
 61:   a  = (Mat_SeqAIJ*)A->data;
 62:   aa = a->a;
 63:   ai = a->i;
 64:   aj = a->j;
 65:   nz = a->nz;


 68:   sprintf(head,"%14d%14d%14d%14d%14d%10s\n",3*m+1,m+1,nz,nz," ");
 69:   fprintf(file,head);
 70:   sprintf(head,"RUA%14d%14d%14d%14d%13s\n",m,m,nz," ");
 71:   fprintf(file,head);

 73:   fprintf(file,"Formats I don't know\n");

 75:   for (i=0; i<m+1; i++) {
 76:     fprintf(file,"%10d%70s\n",ai[i]," ");
 77:   }
 78:   for (i=0; i<nz; i++) {
 79:     fprintf(file,"%10d%70s\n",aj[i]," ");
 80:   }

 82:   for (i=0; i<nz; i++) {
 83:     fprintf(file,"%16.14e,%64s\n",aa[i]," ");
 84:   }

 86:   /* print the vector to the file */
 87:   VecGetArray(x,&xx);
 88:   for (i=0; i<m; i++) {
 89:     fprintf(file,"%16.14e%64s\n",xx[i]," ");
 90:   }
 91:   VecRestoreArray(x,&xx);

 93:   fclose(file);
 94:   MatDestroy(A);
 95:   VecDestroy(x);

 97:   PetscFinalize();
 98:   return 0;
 99: }