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.ex\n\n";
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
16: int main(int argc,char **args)
17: {
18: int ierr,n,m,i,*ai,*aj,size,nz;
19: PetscTruth flg;
20: Mat A;
21: Vec x;
22: char bfile[512],hbfile[512];
23: PetscViewer fd;
24: Mat_SeqAIJ *a;
25: PetscScalar *aa,*xx;
26: FILE *file;
27: char head[81];
29: PetscInitialize(&argc,&args,(char *)0,help);
31: #if defined(PETSC_USE_COMPLEX)
32: SETERRQ(1,"This example does not work with complex numbers");
33: #endif
34: MPI_Comm_size(PETSC_COMM_WORLD,&size);
35: if (size > 1) SETERRQ(1,"Only runs on one processor");
37: PetscOptionsGetString(PETSC_NULL,"-fin",bfile,127,PETSC_NULL);
38: PetscOptionsGetString(PETSC_NULL,"-fout",hbfile,127,PETSC_NULL);
40: /* Read matrix and RHS */
41: PetscViewerBinaryOpen(PETSC_COMM_WORLD,bfile,PETSC_FILE_RDONLY,&fd);
42: MatLoad(fd,MATSEQAIJ,&A);
43: VecLoad(fd,PETSC_NULL,&x);
44: PetscViewerDestroy(fd);
46: /* Format is in column storage so we print transpose matrix */
47: MatTranspose(A,0);
49: m = A->m;
50: n = A->n;
51: if (n != m) SETERRQ(1,"Only for square matrices");
53: /* charrage returns \n may not belong below
54: depends on what 80 character fixed format means to Fortran */
56: file = fopen(hbfile,"w"); if (!file) SETERRQ(1,"Cannot open HB file");
57: sprintf(head,"%-72s%-8s\n","Title","Key");
58: fprintf(file,head);
59: a = (Mat_SeqAIJ*)A->data;
60: aa = a->a;
61: ai = a->i;
62: aj = a->j;
63: nz = a->nz;
66: sprintf(head,"%14d%14d%14d%14d%14d%10s\n",3*m+1,m+1,nz,nz," ");
67: fprintf(file,head);
68: sprintf(head,"RUA%14d%14d%14d%14d%13s\n",m,m,nz," ");
69: fprintf(file,head);
71: fprintf(file,"Formats I don't know\n");
73: for (i=0; i<m+1; i++) {
74: fprintf(file,"%10d%70s\n",ai[i]," ");
75: }
76: for (i=0; i<nz; i++) {
77: fprintf(file,"%10d%70s\n",aj[i]," ");
78: }
80: for (i=0; i<nz; i++) {
81: fprintf(file,"%16.14e,%64s\n",aa[i]," ");
82: }
84: /* print the vector to the file */
85: VecGetArray(x,&xx);
86: for (i=0; i<m; i++) {
87: fprintf(file,"%16.14e%64s\n",xx[i]," ");
88: }
89: VecRestoreArray(x,&xx);
91: fclose(file);
92: MatDestroy(A);
93: VecDestroy(x);
95: PetscFinalize();
96: return 0;
97: }