Actual source code: ex5.c

      1: static char help[] = "Tests DMDAGetElements() and VecView() contour plotting for 2d DMDAs.\n\n";

      3: #include <petscdm.h>
      4: #include <petscdmda.h>
      5: #include <petscdraw.h>

      7: int main(int argc, char **argv)
      8: {
      9:   PetscInt        M = 10, N = 8, ne, nc, i;
     10:   const PetscInt *e;
     11:   PetscBool       flg = PETSC_FALSE;
     12:   DM              da;
     13:   PetscViewer     viewer;
     14:   Vec             local, global;
     15:   PetscScalar     value;
     16:   DMBoundaryType  bx = DM_BOUNDARY_NONE, by = DM_BOUNDARY_NONE;
     17:   DMDAStencilType stype = DMDA_STENCIL_BOX;
     18:   PetscScalar    *lv;

     20:   PetscFunctionBeginUser;
     21:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
     22:   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 300, 0, 300, 300, &viewer));

     24:   /* Read options */
     25:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
     26:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
     27:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-star_stencil", &flg, NULL));
     28:   if (flg) stype = DMDA_STENCIL_STAR;

     30:   /* Create distributed array and get vectors */
     31:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, bx, by, stype, M, N, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL, NULL, &da));
     32:   PetscCall(DMSetFromOptions(da));
     33:   PetscCall(DMSetUp(da));
     34:   PetscCall(DMCreateGlobalVector(da, &global));
     35:   PetscCall(DMCreateLocalVector(da, &local));

     37:   value = -3.0;
     38:   PetscCall(VecSet(global, value));
     39:   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
     40:   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));

     42:   PetscCall(DMDASetElementType(da, DMDA_ELEMENT_P1));
     43:   PetscCall(DMDAGetElements(da, &ne, &nc, &e));
     44:   PetscCall(VecGetArray(local, &lv));
     45:   for (i = 0; i < ne; i++) {
     46:     PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "i %" PetscInt_FMT " e[3*i] %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", i, e[3 * i], e[3 * i + 1], e[3 * i + 2]));
     47:     lv[e[3 * i]] = i;
     48:   }
     49:   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout));
     50:   PetscCall(VecRestoreArray(local, &lv));
     51:   PetscCall(DMDARestoreElements(da, &ne, &nc, &e));

     53:   PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
     54:   PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));

     56:   PetscCall(DMView(da, viewer));
     57:   PetscCall(VecView(global, viewer));

     59:   /* Free memory */
     60:   PetscCall(PetscViewerDestroy(&viewer));
     61:   PetscCall(VecDestroy(&local));
     62:   PetscCall(VecDestroy(&global));
     63:   PetscCall(DMDestroy(&da));
     64:   PetscCall(PetscFinalize());
     65:   return 0;
     66: }

     68: /*TEST

     70:    test:
     71:      requires: x

     73:    test:
     74:      suffix: 2
     75:      nsize: 2
     76:      requires: x

     78: TEST*/