xref: /petsc/src/mat/tests/ex172.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Test MatAXPY and SUBSET_NONZERO_PATTERN [-different] [-skip]\n by default subset pattern is used \n\n";
3 
4 /* A test contributed by Jose E. Roman, Oct. 2014 */
5 
6 #include <petscmat.h>
7 
8 int main(int argc,char **args)
9 {
10   Mat            A,B,C;
11   PetscBool      different=PETSC_FALSE,skip=PETSC_FALSE;
12   PetscInt       m0,m1,n=128,i;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
16   PetscCall(PetscOptionsGetBool(NULL,NULL,"-different",&different,NULL));
17   PetscCall(PetscOptionsGetBool(NULL,NULL,"-skip",&skip,NULL));
18   /*
19      Create matrices
20      A = tridiag(1,-2,1) and B = diag(7);
21   */
22   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
23   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
24   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
25   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
26   PetscCall(MatSetFromOptions(A));
27   PetscCall(MatSetFromOptions(B));
28   PetscCall(MatSetUp(A));
29   PetscCall(MatSetUp(B));
30   PetscCall(MatGetOwnershipRange(A,&m0,&m1));
31   for (i=m0;i<m1;i++) {
32     if (i>0) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
33     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
34     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
35     PetscCall(MatSetValue(B,i,i,7.0,INSERT_VALUES));
36   }
37   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
38   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
39   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
40   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
41 
42   PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&C));
43   /* Add B */
44   PetscCall(MatAXPY(C,1.0,B,(different)?DIFFERENT_NONZERO_PATTERN:SUBSET_NONZERO_PATTERN));
45   /* Add A */
46   if (!skip) PetscCall(MatAXPY(C,1.0,A,SUBSET_NONZERO_PATTERN));
47 
48   /*
49      Free memory
50   */
51   PetscCall(MatDestroy(&A));
52   PetscCall(MatDestroy(&B));
53   PetscCall(MatDestroy(&C));
54   PetscCall(PetscFinalize());
55   return 0;
56 }
57 
58 /*TEST
59 
60    test:
61       nsize: 4
62       output_file: output/ex172.out
63 
64    test:
65       suffix: 2
66       nsize: 4
67       args: -different
68       output_file: output/ex172.out
69 
70    test:
71       suffix: 3
72       nsize: 4
73       args: -skip
74       output_file: output/ex172.out
75 
76    test:
77       suffix: 4
78       nsize: 4
79       args: -different -skip
80       output_file: output/ex172.out
81 
82    test:
83       suffix: baij
84       args: -mat_type baij> ex172.tmp 2>&1
85       output_file: output/ex172.out
86 
87    test:
88       suffix: mpibaij
89       nsize: 4
90       args: -mat_type baij> ex172.tmp 2>&1
91       output_file: output/ex172.out
92 
93    test:
94       suffix: mpisbaij
95       nsize: 4
96       args: -mat_type sbaij> ex172.tmp 2>&1
97       output_file: output/ex172.out
98 
99    test:
100       suffix: sbaij
101       args: -mat_type sbaij> ex172.tmp 2>&1
102       output_file: output/ex172.out
103 
104 TEST*/
105