Actual source code: ex25.c
petsc-3.13.6 2020-09-29
2: static char help[] = "Tests MatTranspose()\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C,A;
9: PetscScalar v;
10: PetscInt i,j,m = 4,n = 4,Ii,J,Istart,Iend;
11: PetscMPIInt rank,size;
13: PetscBool equal=PETSC_FALSE;
15: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
16: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
17: MPI_Comm_size(PETSC_COMM_WORLD,&size);
19: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
20: n = m;
22: MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,5,NULL,5,NULL,&C);
24: /* create the symmetric matrix for the five point stencil */
25: MatGetOwnershipRange(C,&Istart,&Iend);
26: for (Ii=Istart; Ii<Iend; Ii++) {
27: v = -1.0; i = Ii/n; j = Ii - i*n;
28: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
29: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
30: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
31: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
32: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
33: }
34: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
35: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
37: MatTranspose(C,MAT_INITIAL_MATRIX,&A);
39: MatEqual(C,A,&equal);
40: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"C != C^T");
42: MatDestroy(&C);
43: MatDestroy(&A);
44: PetscFinalize();
45: return ierr;
46: }
49: /*TEST
51: test:
53: TEST*/