Actual source code: ex81.c
petsc-3.7.3 2016-08-01
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 example see $PETSC_DIR/share/petsc/datafiles/matrices\n\n";
6: /*
7: Include the private file (not included by most applications) so we have direct
8: access to the matrix data structure.
10: This code is buggy! What is it doing here?
11: */
12: #include <../src/mat/impls/aij/seq/aij.h>
16: int main(int argc,char **args)
17: {
19: PetscInt n,m,i,*ai,*aj,nz;
20: PetscMPIInt size;
21: Mat A;
22: Vec x;
23: char bfile[PETSC_MAX_PATH_LEN],hbfile[PETSC_MAX_PATH_LEN];
24: PetscViewer fd;
25: Mat_SeqAIJ *a;
26: PetscScalar *aa,*xx;
27: FILE *file;
28: char head[81];
30: PetscInitialize(&argc,&args,(char*)0,help);
32: #if defined(PETSC_USE_COMPLEX)
33: SETERRQ(PETSC_COMM_WORLD,1,"This example does not work with complex numbers");
34: #endif
35: MPI_Comm_size(PETSC_COMM_WORLD,&size);
36: if (size > 1) SETERRQ(PETSC_COMM_WORLD,1,"Only runs on one processor");
38: PetscOptionsGetString(NULL,NULL,"-fin",bfile,PETSC_MAX_PATH_LEN,NULL);
39: PetscOptionsGetString(NULL,NULL,"-fout",hbfile,PETSC_MAX_PATH_LEN,NULL);
41: /* Read matrix and RHS */
42: PetscViewerBinaryOpen(PETSC_COMM_WORLD,bfile,FILE_MODE_READ,&fd);
43: MatCreate(PETSC_COMM_WORLD,&A);
44: MatSetType(A,MATSEQAIJ);
45: MatLoad(A,fd);
46: VecCreate(PETSC_COMM_WORLD,&x);
47: VecLoad(x,fd);
48: PetscViewerDestroy(&fd);
50: /* Format is in column storage so we print transpose matrix */
51: MatTranspose(A,MAT_REUSE_MATRIX,&A);
53: m = A->rmap->n;
54: n = A->cmap->n;
55: if (n != m) SETERRQ(PETSC_COMM_SELF,1,"Only for square matrices");
57: /* charrage returns \n may not belong below
58: depends on what 80 character fixed format means to Fortran */
60: file = fopen(hbfile,"w"); if (!file) SETERRQ(PETSC_COMM_SELF,1,"Cannot open HB file");
61: sprintf(head,"%-72s%-8s\n","Title","Key");
62: fprintf(file,head);
63: a = (Mat_SeqAIJ*)A->data;
64: aa = a->a;
65: ai = a->i;
66: aj = a->j;
67: nz = a->nz;
70: sprintf(head,"%14d%14d%14d%14d%14d%10s\n",3*m+1,m+1,nz,nz," ");
71: fprintf(file,head);
72: sprintf(head,"RUA%14d%14d%14d%14d%13s\n",m,m,nz," ");
73: fprintf(file,head);
75: fprintf(file,"Formats I don't know\n");
77: for (i=0; i<m+1; i++) {
78: fprintf(file,"%10d%70s\n",ai[i]," ");
79: }
80: for (i=0; i<nz; i++) {
81: fprintf(file,"%10d%70s\n",aj[i]," ");
82: }
84: for (i=0; i<nz; i++) {
85: fprintf(file,"%16.14e,%64s\n",aa[i]," ");
86: }
88: /* print the vector to the file */
89: VecGetArray(x,&xx);
90: for (i=0; i<m; i++) {
91: fprintf(file,"%16.14e%64s\n",xx[i]," ");
92: }
93: VecRestoreArray(x,&xx);
95: fclose(file);
96: MatDestroy(&A);
97: VecDestroy(&x);
99: PetscFinalize();
100: return 0;
101: }