Actual source code: ex12.c
petsc-3.10.5 2019-03-28
2: static char help[] = "Reads a PETSc matrix and vector from a file; expands the matrix with the vector\n\n";
4: /*T
5: Concepts: Mat^ordering a matrix - loading a binary matrix and vector;
6: Concepts: Mat^loading a binary matrix and vector;
7: Concepts: Vectors^loading a binary vector;
8: Concepts: PetscLog^preloading executable
9: Processors: 1
10: requires: define(PETSC_USE_REAL_DOUBLE) !define(USE_64BIT_INDICES)
11: T*/
15: /*
16: Include "petscmat.h" so that we can use matrices.
17: automatically includes:
18: petscsys.h - base PETSc routines petscvec.h - vectors
19: petscmat.h - matrices
20: petscis.h - index sets petscviewer.h - viewers
21: */
22: #include <petscmat.h>
24: /*
26: Adds a new column and row to the vector (the last) containing the vector
27: */
28: PetscErrorCode PadMatrix(Mat A,Vec v,PetscScalar c,Mat *B)
29: {
30: PetscErrorCode ierr;
31: PetscInt n,i,*cnt,*indices,nc;
32: const PetscInt *aj;
33: const PetscScalar *vv,*aa;
36: MatGetSize(A,&n,NULL);
37: VecGetArrayRead(v,&vv);
38: PetscMalloc1(n,&indices);
39: for (i=0; i<n; i++) indices[i] = i;
41: /* determine number of nonzeros per row in the new matrix */
42: PetscMalloc1(n+1,&cnt);
43: for (i=0; i<n; i++) {
44: MatGetRow(A,i,&nc,NULL,NULL);
45: cnt[i] = nc + (vv[i] != 0.0);
46: MatRestoreRow(A,i,&nc,NULL,NULL);
47: }
48: cnt[n] = 1;
49: for (i=0; i<n; i++) {
50: cnt[n] += (vv[i] != 0.0);
51: }
52: MatCreateSeqAIJ(PETSC_COMM_SELF,n+1,n+1,0,cnt,B);
53: MatSetOption(*B,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);
55: /* copy over the matrix entries from the matrix and then the vector */
56: for (i=0; i<n; i++) {
57: MatGetRow(A,i,&nc,&aj,&aa);
58: MatSetValues(*B,1,&i,nc,aj,aa,INSERT_VALUES);
59: MatRestoreRow(A,i,&nc,&aj,&aa);
60: }
61: MatSetValues(*B,1,&n,n,indices,vv,INSERT_VALUES);
62: MatSetValues(*B,n,indices,1,&n,vv,INSERT_VALUES);
63: MatSetValues(*B,1,&n,1,&n,&c,INSERT_VALUES);
65: MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
66: MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
67: VecRestoreArrayRead(v,&vv);
68: PetscFree(cnt);
69: PetscFree(indices);
70: return(0);
71: }
74: int main(int argc,char **args)
75: {
76: Mat A,B;
77: PetscViewer fd; /* viewer */
78: char file[PETSC_MAX_PATH_LEN]; /* input file name */
80: PetscBool flg;
81: Vec v;
83: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
84: /*
85: Determine files from which we read the two linear systems
86: (matrix and right-hand-side vector).
87: */
88: PetscOptionsGetString(NULL,NULL,"-f0",file,PETSC_MAX_PATH_LEN,&flg);
89: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");
91: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
93: MatCreate(PETSC_COMM_WORLD,&A);
94: MatSetType(A,MATSEQAIJ);
95: MatLoad(A,fd);
96: VecCreate(PETSC_COMM_WORLD,&v);
97: VecLoad(v,fd);
98: MatView(A,PETSC_VIEWER_STDOUT_SELF);
99: PadMatrix(A,v,3.0,&B);
100: MatView(B,PETSC_VIEWER_STDOUT_SELF);
101: MatDestroy(&B);
102: MatDestroy(&A);
103: VecDestroy(&v);
104: PetscViewerDestroy(&fd);
106: PetscFinalize();
107: return ierr;
108: }
112: /*TEST
114: test:
115: args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64
116: requires: double !complex !define(PETSC_USE_64BIT_INDICES)
118: TEST*/