xref: /petsc/src/mat/tests/ex35.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests MatCreateSubMatrices().\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            A,B,*Bsub;
9   PetscInt       i,j,m = 6,n = 6,N = 36,Ii,J;
10   PetscScalar    v;
11   IS             isrow;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
15   PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD,N,N,5,NULL,&A));
16   for (i=0; i<m; i++) {
17     for (j=0; j<n; j++) {
18       v = -1.0;  Ii = j + n*i;
19       if (i>0)   {J = Ii - n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));}
20       if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));}
21       if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));}
22       if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));}
23       v = 4.0; PetscCall(MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES));
24     }
25   }
26   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
27   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
28   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_SELF));
29 
30   /* take the first diagonal block */
31   PetscCall(ISCreateStride(PETSC_COMM_WORLD,m,0,1,&isrow));
32   PetscCall(MatCreateSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&Bsub));
33   B    = *Bsub;
34   PetscCall(ISDestroy(&isrow));
35   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_SELF));
36   PetscCall(MatDestroySubMatrices(1,&Bsub));
37 
38   /* take a strided block */
39   PetscCall(ISCreateStride(PETSC_COMM_WORLD,m,0,2,&isrow));
40   PetscCall(MatCreateSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&Bsub));
41   B    = *Bsub;
42   PetscCall(ISDestroy(&isrow));
43   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_SELF));
44   PetscCall(MatDestroySubMatrices(1,&Bsub));
45 
46   /* take the last block */
47   PetscCall(ISCreateStride(PETSC_COMM_WORLD,m,N-m-1,1,&isrow));
48   PetscCall(MatCreateSubMatrices(A,1,&isrow,&isrow,MAT_INITIAL_MATRIX,&Bsub));
49   B    = *Bsub;
50   PetscCall(ISDestroy(&isrow));
51   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_SELF));
52 
53   PetscCall(MatDestroySubMatrices(1,&Bsub));
54   PetscCall(MatDestroy(&A));
55 
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59 
60 /*TEST
61 
62    test:
63 
64 TEST*/
65