Actual source code: ex172.c
petsc-3.6.1 2015-08-06
2: static char help[] = "Test MatAXPY and SUBSET_NONZERO_PATTERN [-different] [-skip]\n by default subset pattern is used \n\n";
4: /* A test contributed by Jose E. Roman, Oct. 2014 */
6: #include <petscmat.h>
10: int main(int argc,char **args)
11: {
13: Mat A,B,C;
14: PetscBool different=PETSC_FALSE,skip=PETSC_FALSE;
15: PetscInt m0,m1,n=128,i;
17: PetscInitialize(&argc,&args,(char*)0,help);
18: PetscOptionsGetBool(NULL,"-different",&different,NULL);
19: PetscOptionsGetBool(NULL,"-skip",&skip,NULL);
20: /*
21: Create matrices
22: A = tridiag(1,-2,1) and B = diag(7);
23: */
24: MatCreate(PETSC_COMM_WORLD,&A);
25: MatCreate(PETSC_COMM_WORLD,&B);
26: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
27: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
28: MatSetFromOptions(A);
29: MatSetFromOptions(B);
30: MatSetUp(A);
31: MatSetUp(B);
32: MatGetOwnershipRange(A,&m0,&m1);
33: for (i=m0;i<m1;i++) {
34: if (i>0) { MatSetValue(A,i,i-1,-1.0,INSERT_VALUES); }
35: if (i<n-1) { MatSetValue(A,i,i+1,-1.0,INSERT_VALUES); }
36: MatSetValue(A,i,i,2.0,INSERT_VALUES);
37: MatSetValue(B,i,i,7.0,INSERT_VALUES);
38: }
39: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
41: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
42: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
44: MatDuplicate(A,MAT_COPY_VALUES,&C);
45: /* Add B */
46: MatAXPY(C,1.0,B,(different)?DIFFERENT_NONZERO_PATTERN:SUBSET_NONZERO_PATTERN);
47: /* Add A */
48: if (!skip) { MatAXPY(C,1.0,A,SUBSET_NONZERO_PATTERN); }
50: /*
51: Free memory
52: */
53: MatDestroy(&A);
54: MatDestroy(&B);
55: MatDestroy(&C);
56: PetscFinalize();
57: return 0;
58: }