Actual source code: ex37.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests MatCopy() and MatStore/RetrieveValues().\n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C,A;
11: PetscInt i, n = 10,midx[3],bs=1;
13: PetscScalar v[3];
14: PetscBool flg,isAIJ;
15: MatType type;
16: PetscMPIInt size;
18: PetscInitialize(&argc,&args,(char*)0,help);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
21: PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);
23: MatCreate(PETSC_COMM_WORLD,&C);
24: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n);
25: MatSetType(C,MATAIJ);
26: MatSetFromOptions(C);
28: MatGetType(C,&type);
29: if (size == 1) {
30: PetscObjectTypeCompare((PetscObject)C,MATSEQAIJ,&isAIJ);
31: } else {
32: PetscObjectTypeCompare((PetscObject)C,MATMPIAIJ,&isAIJ);
33: }
34: MatSeqAIJSetPreallocation(C,3,NULL);
35: MatMPIAIJSetPreallocation(C,3,NULL,3,NULL);
36: MatSeqBAIJSetPreallocation(C,bs,3,NULL);
37: MatMPIBAIJSetPreallocation(C,bs,3,NULL,3,NULL);
39: v[0] = -1.; v[1] = 2.; v[2] = -1.;
40: for (i=1; i<n-1; i++) {
41: midx[2] = i-1; midx[1] = i; midx[0] = i+1;
42: MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
43: }
44: i = 0; midx[0] = 0; midx[1] = 1;
45: v[0] = 2.0; v[1] = -1.;
46: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
47: i = n-1; midx[0] = n-2; midx[1] = n-1;
48: v[0] = -1.0; v[1] = 2.;
49: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
51: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
52: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
54: MatCreate(PETSC_COMM_WORLD,&A);
55: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
56: MatSetFromOptions(A);
57: MatSetUp(A);
58: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
59: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
61: /* test matrices with different nonzero patterns - Note: A is created with different nonzero pattern of C! */
62: MatCopy(C,A,DIFFERENT_NONZERO_PATTERN);
63: MatEqual(A,C,&flg);
64: if (!flg) SETERRQ(PETSC_COMM_SELF,1,"MatCopy(C,A,DIFFERENT_NONZERO_PATTERN): Matrices are NOT equal");
66: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
67: PetscPrintf(PETSC_COMM_WORLD,"A is obtained with MatCopy(,,DIFFERENT_NONZERO_PATTERN):\n");
68: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
69: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
70: MatDestroy(&A);
72: /* test matrices with same nonzero pattern */
73: MatDuplicate(C,MAT_DO_NOT_COPY_VALUES,&A);
74: MatCopy(C,A,SAME_NONZERO_PATTERN);
75: MatEqual(A,C,&flg);
76: if (!flg) SETERRQ(PETSC_COMM_SELF,1,"MatCopy(C,A,SAME_NONZERO_PATTERN): Matrices are NOT equal");
78: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
79: PetscPrintf(PETSC_COMM_WORLD,"\nA is obtained with MatCopy(,,SAME_NONZERO_PATTERN):\n");
80: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
81: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
82:
83: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
84: PetscPrintf(PETSC_COMM_WORLD,"A:\n");
85: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
86: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
87:
88: /* test MatStore/RetrieveValues() */
89: if (isAIJ) {
90: MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
91: MatStoreValues(A);
92: MatZeroEntries(A);
93: MatRetrieveValues(A);
94: }
96: MatDestroy(&C);
97: MatDestroy(&A);
98: PetscFinalize();
99: return 0;
100: }