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 file\n\
6: -fout <mat> : output marrix file\n\
7: -start <row> : the row from where the submat should be extracted\n\
8: -size <sx> : the size of the submatrix\n";
10: #include petscmat.h
11: #include petscvec.h
15: int main(int argc,char **args)
16: {
17: char fin[128],fout[128] ="default.mat";
18: PetscViewer fdin,fdout;
19: Vec b;
20: MatType mtype = MATSEQBAIJ;
21: Mat A,*B;
22: int ierr,start=0,size;
23: IS isrow,iscol;
24: PetscTruth flg;
26: PetscInitialize(&argc,&args,(char *)0,help);
29: PetscOptionsGetString(PETSC_NULL,"-fin",fin,127,&flg);
30: if (!flg) SETERRQ(1,"Must indicate binary file with the -fin option");
31: PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,PETSC_BINARY_RDONLY,&fdin);
33: PetscOptionsGetString(PETSC_NULL,"-fout",fout,127,&flg);
34: if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout);}
35: PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,PETSC_BINARY_CREATE,&fdout);
37: MatLoad(fdin,mtype,&A);
38: PetscViewerDestroy(fdin);
39:
40: MatGetSize(A,&size,&size);
41: size /= 2;
42: PetscOptionsGetInt(PETSC_NULL,"-start",&start,PETSC_NULL);
43: PetscOptionsGetInt(PETSC_NULL,"-size",&size,PETSC_NULL);
44:
45: ISCreateStride(PETSC_COMM_SELF,size,start,1,&isrow);
46: ISCreateStride(PETSC_COMM_SELF,size,start,1,&iscol);
47: MatGetSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
48: MatView(B[0],fdout);
50: VecCreate(PETSC_COMM_SELF,&b);
51: VecSetSizes(b,PETSC_DECIDE,size);
52: VecSetFromOptions(b);
53: MatView(B[0],fdout);
54: PetscViewerDestroy(fdout);
56: MatDestroy(A);
57: MatDestroy(B[0]);
58: VecDestroy(b);
59: PetscFree(B);
60: ISDestroy(iscol);
61: ISDestroy(isrow);
62: PetscFinalize();
63: return 0;
64: }