Actual source code: ex81.c

  1: /*$Id: ex81.c,v 1.10 2001/08/07 03:03:07 balay Exp $*/

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

  8: /*
  9:   Include the private file (not included by most applications) so we have direct
 10:   access to the matrix data structure.
 11: */
 12:  #include src/mat/impls/aij/seq/aij.h

 14: int main(int argc,char **args)
 15: {
 16:   int         ierr,n,m,i,*ai,*aj,size,nz;
 17:   PetscTruth  flg;
 18:   Mat         A;
 19:   Vec         x;
 20:   char        bfile[512],hbfile[512];
 21:   PetscViewer fd;
 22:   Mat_SeqAIJ  *a;
 23:   PetscScalar *aa,*xx;
 24:   FILE        *file;
 25:   char        head[81];

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

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

 35:   PetscOptionsGetString(PETSC_NULL,"-fin",bfile,127,PETSC_NULL);
 36:   PetscOptionsGetString(PETSC_NULL,"-fout",hbfile,127,PETSC_NULL);

 38:   /* Read matrix and RHS */
 39:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,bfile,PETSC_BINARY_RDONLY,&fd);
 40:   MatLoad(fd,MATSEQAIJ,&A);
 41:   VecLoad(fd,&x);
 42:   PetscViewerDestroy(fd);

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

 47:   m = A->m;
 48:   n = A->n;
 49:   if (n != m) SETERRQ(1,"Only for square matrices");

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

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


 64:   sprintf(head,"%14d%14d%14d%14d%14d%10sn",3*m+1,m+1,nz,nz," ");
 65:   fprintf(file,head);
 66:   sprintf(head,"RUA%14d%14d%14d%14d%13sn",m,m,nz," ");
 67:   fprintf(file,head);

 69:   fprintf(file,"Formats I don't known");

 71:   for (i=0; i<m+1; i++) {
 72:     fprintf(file,"%10d%70sn",ai[i]," ");
 73:   }
 74:   for (i=0; i<nz; i++) {
 75:     fprintf(file,"%10d%70sn",aj[i]," ");
 76:   }

 78:   for (i=0; i<nz; i++) {
 79:     fprintf(file,"%16.14e,%64sn",aa[i]," ");
 80:   }

 82:   /* print the vector to the file */
 83:   VecGetArray(x,&xx);
 84:   for (i=0; i<m; i++) {
 85:     fprintf(file,"%16.14e%64sn",xx[i]," ");
 86:   }
 87:   VecRestoreArray(x,&xx);

 89:   fclose(file);
 90:   MatDestroy(A);
 91:   VecDestroy(x);

 93:   PetscFinalize();
 94:   return 0;
 95: }