xref: /petsc/src/mat/tutorials/ex12.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 
2 static char help[] = "Reads a PETSc matrix and vector from a file; expands the matrix with the vector\n\n";
3 
4 /*
5   Include "petscmat.h" so that we can use matrices.
6   automatically includes:
7      petscsys.h       - base PETSc routines   petscvec.h    - vectors
8      petscmat.h    - matrices
9      petscis.h     - index sets            petscviewer.h - viewers
10 */
11 #include <petscmat.h>
12 
13 /*
14 
15    Adds a new column and row to the vector (the last) containing the vector
16 */
17 PetscErrorCode PadMatrix(Mat A, Vec v, PetscScalar c, Mat *B) {
18   PetscInt           n, i, *cnt, *indices, nc;
19   const PetscInt    *aj;
20   const PetscScalar *vv, *aa;
21 
22   PetscFunctionBegin;
23   PetscCall(MatGetSize(A, &n, NULL));
24   PetscCall(VecGetArrayRead(v, &vv));
25   PetscCall(PetscMalloc1(n, &indices));
26   for (i = 0; i < n; i++) indices[i] = i;
27 
28   /* determine number of nonzeros per row in the new matrix */
29   PetscCall(PetscMalloc1(n + 1, &cnt));
30   for (i = 0; i < n; i++) {
31     PetscCall(MatGetRow(A, i, &nc, NULL, NULL));
32     cnt[i] = nc + (vv[i] != 0.0);
33     PetscCall(MatRestoreRow(A, i, &nc, NULL, NULL));
34   }
35   cnt[n] = 1;
36   for (i = 0; i < n; i++) { cnt[n] += (vv[i] != 0.0); }
37   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, n + 1, n + 1, 0, cnt, B));
38   PetscCall(MatSetOption(*B, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE));
39 
40   /* copy over the matrix entries from the matrix and then the vector */
41   for (i = 0; i < n; i++) {
42     PetscCall(MatGetRow(A, i, &nc, &aj, &aa));
43     PetscCall(MatSetValues(*B, 1, &i, nc, aj, aa, INSERT_VALUES));
44     PetscCall(MatRestoreRow(A, i, &nc, &aj, &aa));
45   }
46   PetscCall(MatSetValues(*B, 1, &n, n, indices, vv, INSERT_VALUES));
47   PetscCall(MatSetValues(*B, n, indices, 1, &n, vv, INSERT_VALUES));
48   PetscCall(MatSetValues(*B, 1, &n, 1, &n, &c, INSERT_VALUES));
49 
50   PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
51   PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
52   PetscCall(VecRestoreArrayRead(v, &vv));
53   PetscCall(PetscFree(cnt));
54   PetscCall(PetscFree(indices));
55   PetscFunctionReturn(0);
56 }
57 
58 int main(int argc, char **args) {
59   Mat         A, B;
60   PetscViewer fd;                       /* viewer */
61   char        file[PETSC_MAX_PATH_LEN]; /* input file name */
62   PetscBool   flg;
63   Vec         v;
64 
65   PetscFunctionBeginUser;
66   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
67   /*
68      Determine files from which we read the two linear systems
69      (matrix and right-hand-side vector).
70   */
71   PetscCall(PetscOptionsGetString(NULL, NULL, "-f0", file, sizeof(file), &flg));
72   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f0 option");
73 
74   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
75 
76   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
77   PetscCall(MatSetType(A, MATSEQAIJ));
78   PetscCall(MatLoad(A, fd));
79   PetscCall(VecCreate(PETSC_COMM_WORLD, &v));
80   PetscCall(VecLoad(v, fd));
81   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_SELF));
82   PetscCall(PadMatrix(A, v, 3.0, &B));
83   PetscCall(MatView(B, PETSC_VIEWER_STDOUT_SELF));
84   PetscCall(MatDestroy(&B));
85   PetscCall(MatDestroy(&A));
86   PetscCall(VecDestroy(&v));
87   PetscCall(PetscViewerDestroy(&fd));
88 
89   PetscCall(PetscFinalize());
90   return 0;
91 }
92 
93 /*TEST
94 
95    test:
96       args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64
97       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
98 
99 TEST*/
100