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