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