Actual source code: ex57.c
1: /*$Id: ex57.c,v 1.23 2001/04/10 19:35:44 bsmith Exp $*/
3: static char help[] = "Reads in a binary file, extracts a submatrix from it, and writes to another binary file.n
4: Options:n
5: -fin <mat> : input matrix filen
6: -fout <mat> : output marrix filen
7: -start <row> : the row from where the submat should be extractedn
8: -size <sx> : the size of the submatrixn";
10: #include petscmat.h
11: #include petscvec.h
13: int main(int argc,char **args)
14: {
15: char fin[128],fout[128] ="default.mat";
16: PetscViewer fdin,fdout;
17: Vec b;
18: MatType mtype = MATSEQBAIJ;
19: Mat A,*B;
20: int ierr,start=0,size;
21: IS isrow,iscol;
22: PetscTruth flg;
24: PetscInitialize(&argc,&args,(char *)0,help);
27: PetscOptionsGetString(PETSC_NULL,"-fin",fin,127,&flg);
28: if (!flg) SETERRQ(1,"Must indicate binary file with the -fin option");
29: PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,PETSC_BINARY_RDONLY,&fdin);
31: PetscOptionsGetString(PETSC_NULL,"-fout",fout,127,&flg);
32: if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %sn",fout);}
33: PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,PETSC_BINARY_CREATE,&fdout);
35: MatLoad(fdin,mtype,&A);
36: PetscViewerDestroy(fdin);
37:
38: MatGetSize(A,&size,&size);
39: size /= 2;
40: PetscOptionsGetInt(PETSC_NULL,"-start",&start,PETSC_NULL);
41: PetscOptionsGetInt(PETSC_NULL,"-size",&size,PETSC_NULL);
42:
43: ISCreateStride(PETSC_COMM_SELF,size,start,1,&isrow);
44: ISCreateStride(PETSC_COMM_SELF,size,start,1,&iscol);
45: MatGetSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
46: MatView(B[0],fdout);
48: VecCreate(PETSC_COMM_SELF,&b);
49: VecSetSizes(b,PETSC_DECIDE,size);
50: VecSetFromOptions(b);
51: MatView(B[0],fdout);
52: PetscViewerDestroy(fdout);
54: MatDestroy(A);
55: MatDestroy(B[0]);
56: VecDestroy(b);
57: PetscFree(B);
58: ISDestroy(iscol);
59: ISDestroy(isrow);
60: PetscFinalize();
61: return 0;
62: }