xref: /petsc/src/mat/tests/ex107.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Test MatCreate() with MAT_STRUCTURE_ONLY .\n\n";
2 
3 #include <petscmat.h>
4 
main(int argc,char ** argv)5 int main(int argc, char **argv)
6 {
7   Mat         mat;
8   PetscInt    m = 7, n, i, j, rstart, rend;
9   PetscMPIInt size;
10   PetscScalar v;
11   PetscBool   struct_only = PETSC_TRUE;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &argv, NULL, 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) PetscCall(MatSetOption(mat, MAT_STRUCTURE_ONLY, PETSC_TRUE));
28   PetscCall(MatSetUp(mat));
29   PetscCall(MatGetOwnershipRange(mat, &rstart, &rend));
30   for (i = rstart; i < rend; i++) {
31     for (j = 0; j < n; j++) {
32       v = 10.0 * i + j;
33       PetscCall(MatSetValues(mat, 1, &i, 1, &j, &v, INSERT_VALUES));
34     }
35   }
36   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
37   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
38   PetscCall(MatView(mat, PETSC_VIEWER_STDOUT_WORLD));
39 
40   /* Free data structures */
41   PetscCall(MatDestroy(&mat));
42   PetscCall(PetscFinalize());
43   return 0;
44 }
45 
46 /*TEST
47 
48    test:
49       output_file: output/ex107.out
50 
51    test:
52       suffix: 2
53       args: -mat_type baij -mat_block_size 2 -m 10
54 
55 TEST*/
56