xref: /petsc/src/dm/tutorials/ex51.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1 static char help[] = "Tests DMDAVecGetArrayDOF()\n";
2 #include <petscdm.h>
3 #include <petscdmda.h>
4 
5 int main(int argc, char *argv[]) {
6   DM              da, daX, daY;
7   DMDALocalInfo   info;
8   MPI_Comm        commX, commY;
9   Vec             basisX, basisY;
10   PetscScalar   **arrayX, **arrayY;
11   const PetscInt *lx, *ly;
12   PetscInt        M = 3, N = 3;
13   PetscInt        p     = 1;
14   PetscInt        numGP = 3;
15   PetscInt        dof   = 2 * (p + 1) * numGP;
16   PetscMPIInt     rank, subsize, subrank;
17 
18   PetscFunctionBeginUser;
19   PetscCall(PetscInitialize(&argc, &argv, 0, help));
20   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
21   /* Create 2D DMDA */
22   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, M, N, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL, NULL, &da));
23   PetscCall(DMSetFromOptions(da));
24   PetscCall(DMSetUp(da));
25   /* Create 1D DMDAs along two directions. */
26   PetscCall(DMDAGetOwnershipRanges(da, &lx, &ly, NULL));
27   PetscCall(DMDAGetLocalInfo(da, &info));
28   /* Partitioning in the X direction makes a subcomm extending in the Y direction and vice-versa. */
29   PetscCall(DMDAGetProcessorSubsets(da, DM_X, &commY));
30   PetscCall(DMDAGetProcessorSubsets(da, DM_Y, &commX));
31   PetscCallMPI(MPI_Comm_size(commX, &subsize));
32   PetscCallMPI(MPI_Comm_rank(commX, &subrank));
33   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]X subrank: %d subsize: %d\n", rank, subrank, subsize));
34   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
35   PetscCallMPI(MPI_Comm_size(commY, &subsize));
36   PetscCallMPI(MPI_Comm_rank(commY, &subrank));
37   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d]Y subrank: %d subsize: %d\n", rank, subrank, subsize));
38   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
39   PetscCall(DMDACreate1d(commX, DM_BOUNDARY_NONE, info.mx, dof, 1, lx, &daX));
40   PetscCall(DMSetUp(daX));
41   PetscCall(DMDACreate1d(commY, DM_BOUNDARY_NONE, info.my, dof, 1, ly, &daY));
42   PetscCall(DMSetUp(daY));
43   /* Create 1D vectors for basis functions */
44   PetscCall(DMGetGlobalVector(daX, &basisX));
45   PetscCall(DMGetGlobalVector(daY, &basisY));
46   /* Extract basis functions */
47   PetscCall(DMDAVecGetArrayDOF(daX, basisX, &arrayX));
48   PetscCall(DMDAVecGetArrayDOF(daY, basisY, &arrayY));
49   /*arrayX[i][ndof]; */
50   /*arrayY[j][ndof]; */
51   PetscCall(DMDAVecRestoreArrayDOF(daX, basisX, &arrayX));
52   PetscCall(DMDAVecRestoreArrayDOF(daY, basisY, &arrayY));
53   /* Return basis vectors */
54   PetscCall(DMRestoreGlobalVector(daX, &basisX));
55   PetscCall(DMRestoreGlobalVector(daY, &basisY));
56   /* Cleanup */
57   PetscCallMPI(MPI_Comm_free(&commX));
58   PetscCallMPI(MPI_Comm_free(&commY));
59   PetscCall(DMDestroy(&daX));
60   PetscCall(DMDestroy(&daY));
61   PetscCall(DMDestroy(&da));
62   PetscCall(PetscFinalize());
63   return 0;
64 }
65 
66 /*TEST
67 
68    test:
69       nsize: 2
70 
71 TEST*/
72