Actual source code: ex10.c
petsc-3.8.4 2018-03-24
2: static char help[] = "Tests repeated use of assembly for matrices.\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C;
9: PetscInt i,j,m = 5,n = 2,Ii,J;
11: PetscMPIInt rank,size;
12: PetscScalar v;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
16: MPI_Comm_size(PETSC_COMM_WORLD,&size);
17: n = 2*size;
19: /* create the matrix for the five point stencil, YET AGAIN*/
20: MatCreate(PETSC_COMM_WORLD,&C);
21: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
22: MatSetFromOptions(C);
23: MatSetUp(C);
24: for (i=0; i<m; i++) {
25: for (j=2*rank; j<2*rank+2; j++) {
26: v = -1.0; Ii = j + n*i;
27: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
28: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
29: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
30: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
31: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
32: }
33: }
34: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
35: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
36: for (i=0; i<m; i++) {
37: for (j=2*rank; j<2*rank+2; j++) {
38: v = 1.0; Ii = j + n*i;
39: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
40: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
41: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
42: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
43: v = -4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
44: }
45: }
46: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
47: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
49: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
51: MatDestroy(&C);
52: PetscFinalize();
53: return ierr;
54: }