xref: /petsc/src/dm/tests/ex22.c (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "Tests MatSetValuesBlockedStencil() in 3d.\n\n";
3 
4 #include <petscmat.h>
5 #include <petscdm.h>
6 #include <petscdmda.h>
7 
8 int main(int argc, char **argv)
9 {
10   PetscInt        M = 3, N = 4, P = 2, s = 1, w = 2, i, m = PETSC_DECIDE, n = PETSC_DECIDE, p = PETSC_DECIDE;
11   DM              da;
12   Mat             mat;
13   DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
14   PetscBool       flg          = PETSC_FALSE;
15   MatStencil      idx[2], idy[2];
16   PetscScalar    *values;
17 
18   PetscFunctionBeginUser;
19   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
20   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
21   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
22   PetscCall(PetscOptionsGetInt(NULL, NULL, "-P", &P, NULL));
23   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
24   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
25   PetscCall(PetscOptionsGetInt(NULL, NULL, "-p", &p, NULL));
26   PetscCall(PetscOptionsGetInt(NULL, NULL, "-s", &s, NULL));
27   PetscCall(PetscOptionsGetInt(NULL, NULL, "-w", &w, NULL));
28   PetscCall(PetscOptionsGetBool(NULL, NULL, "-star", &flg, NULL));
29   if (flg) stencil_type = DMDA_STENCIL_STAR;
30 
31   /* Create distributed array and get vectors */
32   PetscCall(DMDACreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, stencil_type, M, N, P, m, n, p, w, s, 0, 0, 0, &da));
33   PetscCall(DMSetFromOptions(da));
34   PetscCall(DMSetUp(da));
35   PetscCall(DMSetMatType(da, MATMPIBAIJ));
36   PetscCall(DMCreateMatrix(da, &mat));
37 
38   idx[0].i = 1;
39   idx[0].j = 1;
40   idx[0].k = 0;
41   idx[1].i = 2;
42   idx[1].j = 1;
43   idx[1].k = 0;
44   idy[0].i = 1;
45   idy[0].j = 2;
46   idy[0].k = 0;
47   idy[1].i = 2;
48   idy[1].j = 2;
49   idy[1].k = 0;
50   PetscCall(PetscMalloc1(2 * 2 * w * w, &values));
51   for (i = 0; i < 2 * 2 * w * w; i++) values[i] = i;
52   PetscCall(MatSetValuesBlockedStencil(mat, 2, idx, 2, idy, values, INSERT_VALUES));
53   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
54   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
55 
56   /* Free memory */
57   PetscCall(PetscFree(values));
58   PetscCall(MatDestroy(&mat));
59   PetscCall(DMDestroy(&da));
60   PetscCall(PetscFinalize());
61   return 0;
62 }
63