xref: /petsc/src/dm/tests/ex19.c (revision 21e3ffae2f3b73c0bd738cf6d0a809700fc04bb0)
1 
2 static char help[] = "Tests DMDA with variable multiple degrees of freedom per node.\n\n";
3 
4 /*
5    This code only compiles with gcc, since it is not ANSI C
6 */
7 
8 #include <petscdm.h>
9 #include <petscdmda.h>
10 
11 PetscErrorCode doit(DM da, Vec global)
12 {
13   PetscInt i, j, k, M, N, dof;
14 
15   PetscCall(DMDAGetInfo(da, 0, &M, &N, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0));
16   {
17     struct {
18       PetscScalar inside[dof];
19     } **mystruct;
20     PetscCall(DMDAVecGetArrayRead(da, global, (void *)&mystruct));
21     for (i = 0; i < N; i++) {
22       for (j = 0; j < M; j++) {
23         for (k = 0; k < dof; k++) {
24           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT " %" PetscInt_FMT " %g\n", i, j, (double)mystruct[i][j].inside[0]));
25 
26           mystruct[i][j].inside[1] = 2.1;
27         }
28       }
29     }
30     PetscCall(DMDAVecRestoreArrayRead(da, global, (void *)&mystruct));
31   }
32   PetscFunctionReturn(PETSC_SUCCESS);
33 }
34 
35 int main(int argc, char **argv)
36 {
37   PetscInt dof = 2, M = 3, N = 3, m = PETSC_DECIDE, n = PETSC_DECIDE;
38   DM       da;
39   Vec      global, local;
40 
41   PetscFunctionBeginUser;
42   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
43   PetscCall(PetscOptionsGetInt(NULL, 0, "-dof", &dof, 0));
44   /* Create distributed array and get vectors */
45   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, dof, 1, NULL, NULL, &da));
46   PetscCall(DMSetFromOptions(da));
47   PetscCall(DMSetUp(da));
48   PetscCall(DMCreateGlobalVector(da, &global));
49   PetscCall(DMCreateLocalVector(da, &local));
50 
51   PetscCall(doit(da, global));
52 
53   PetscCall(VecView(global, 0));
54 
55   /* Free memory */
56   PetscCall(VecDestroy(&local));
57   PetscCall(VecDestroy(&global));
58   PetscCall(DMDestroy(&da));
59   PetscCall(PetscFinalize());
60   return 0;
61 }
62