xref: /petsc/src/mat/tests/ex107.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Test MatCreate() with MAT_STRUCTURE_ONLY .\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **argv)
7 {
8   Mat            mat;
9   PetscInt       m = 7,n,i,j,rstart,rend;
10   PetscMPIInt    size;
11   PetscScalar    v;
12   PetscBool      struct_only=PETSC_TRUE;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
16   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
17   PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!");
18 
19   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON));
20   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
21   PetscCall(PetscOptionsGetBool(NULL,NULL,"-struct_only",&struct_only,NULL));
22   n    = m;
23 
24   /* ------- Assemble matrix, test MatValid() --------- */
25   PetscCall(MatCreate(PETSC_COMM_WORLD,&mat));
26   PetscCall(MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,m,n));
27   PetscCall(MatSetFromOptions(mat));
28   if (struct_only) PetscCall(MatSetOption(mat,MAT_STRUCTURE_ONLY,PETSC_TRUE));
29   PetscCall(MatSetUp(mat));
30   PetscCall(MatGetOwnershipRange(mat,&rstart,&rend));
31   for (i=rstart; i<rend; i++) {
32     for (j=0; j<n; j++) {
33       v    = 10.0*i+j;
34       PetscCall(MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES));
35     }
36   }
37   PetscCall(MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY));
38   PetscCall(MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY));
39   PetscCall(MatView(mat,PETSC_VIEWER_STDOUT_WORLD));
40 
41   /* Free data structures */
42   PetscCall(MatDestroy(&mat));
43   PetscCall(PetscFinalize());
44   return 0;
45 }
46 
47 /*TEST
48 
49    test:
50       output_file: output/ex107.out
51 
52    test:
53       suffix: 2
54       args: -mat_type baij -mat_block_size 2 -m 10
55 
56 TEST*/
57