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