Actual source code: ex24.c
petsc-3.4.5 2014-06-29
2: static char help[] = "Tests copying an AIJ matrix.\n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C,A;
11: PetscInt i,j,m = 5,n = 5,Ii,J;
13: PetscScalar v;
15: PetscInitialize(&argc,&args,(char*)0,help);
17: /* create the matrix for the five point stencil, YET AGAIN*/
18: MatCreateSeqAIJ(PETSC_COMM_SELF,m*n,m*n,5,NULL,&C);
19: MatSetUp(C);
20: for (i=0; i<m; i++) {
21: for (j=0; j<n; j++) {
22: v = -1.0; Ii = j + n*i;
23: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
24: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
25: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
26: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
27: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
28: }
29: }
30: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
31: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
33: MatConvert(C,MATSAME,MAT_INITIAL_MATRIX,&A);
35: for (i=0; i<m; i++) {
36: for (j=0; j<n; j++) {
37: v = -1.0; Ii = j + n*i;
38: if (i>0) {J = Ii - n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
39: if (i<m-1) {J = Ii + n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
40: if (j>0) {J = Ii - 1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
41: if (j<n-1) {J = Ii + 1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
42: v = 4.0; MatSetValues(A,1,&Ii,1,&Ii,&v,ADD_VALUES);
43: }
44: }
45: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
46: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
48: MatView(A,PETSC_VIEWER_STDOUT_SELF);
50: MatDestroy(&C);
51: MatDestroy(&A);
52: PetscFinalize();
53: return 0;
54: }