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