xref: /petsc/src/mat/tests/ex20.c (revision fbf9dbe564678ed6eff1806adbc4c4f01b9743f4)
1 
2 static char help[] = "Tests converting a matrix to another format with MatConvert().\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc, char **args)
7 {
8   Mat         C, A;
9   PetscInt    i, j, m = 5, n = 4, Ii, J;
10   PetscMPIInt rank, size;
11   PetscScalar v;
12   char        mtype[256];
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
16   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
17 
18   /* This example does not work correctly for np > 2 */
19   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
20   PetscCheck(size <= 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Use np <= 2");
21 
22   /* Create the matrix for the five point stencil, YET AGAIN */
23   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
24   PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n));
25   PetscCall(MatSetFromOptions(C));
26   PetscCall(MatSetUp(C));
27   for (i = 0; i < m; i++) {
28     for (j = 2 * rank; j < 2 * rank + 2; j++) {
29       v  = -1.0;
30       Ii = j + n * i;
31       if (i > 0) {
32         J = Ii - n;
33         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
34       }
35       if (i < m - 1) {
36         J = Ii + n;
37         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
38       }
39       if (j > 0) {
40         J = Ii - 1;
41         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
42       }
43       if (j < n - 1) {
44         J = Ii + 1;
45         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
46       }
47       v = 4.0;
48       PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
49     }
50   }
51 
52   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
53   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
54   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO));
55   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
56   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
57   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
58 
59   PetscCall(PetscStrncpy(mtype, MATSAME, sizeof(mtype)));
60   PetscCall(PetscOptionsGetString(NULL, NULL, "-conv_mat_type", mtype, sizeof(mtype), NULL));
61   PetscCall(MatConvert(C, mtype, MAT_INITIAL_MATRIX, &A));
62   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO));
63   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
64   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
65   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_IMPL));
66   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
67   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
68 
69   /* Free data structures */
70   PetscCall(MatDestroy(&A));
71   PetscCall(MatDestroy(&C));
72 
73   PetscCall(PetscFinalize());
74   return 0;
75 }
76 
77 /*TEST
78 
79    test:
80       args: -conv_mat_type seqaij
81 
82 TEST*/
83