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