Actual source code: ex11.c


  2: static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";

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

  7: int main(int argc, char **argv)
  8: {
  9:   PetscInt       M = 10, N = 8, dof = 1, s = 1, bx = 0, by = 0, i, n, j, k, m, wrap, xs, ys;
 10:   DM             da, dac;
 11:   PetscViewer    viewer;
 12:   Vec            local, global, coors;
 13:   PetscScalar ***xy, ***aglobal;
 14:   PetscDraw      draw;
 15:   char           fname[32];

 17:   PetscFunctionBeginUser;
 18:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 19:   /* Create viewers */
 20:   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", PETSC_DECIDE, PETSC_DECIDE, 600, 200, &viewer));
 21:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
 22:   PetscCall(PetscDrawSetDoubleBuffer(draw));

 24:   /* Read options */
 25:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
 26:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
 27:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL));
 28:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-s", &s, NULL));
 29:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-periodic_x", &wrap, NULL));
 30:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-periodic_y", &wrap, NULL));

 32:   /* Create distributed array and get vectors */
 33:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, (DMBoundaryType)bx, (DMBoundaryType)by, DMDA_STENCIL_BOX, M, N, PETSC_DECIDE, PETSC_DECIDE, dof, s, NULL, NULL, &da));
 34:   PetscCall(DMSetFromOptions(da));
 35:   PetscCall(DMSetUp(da));
 36:   PetscCall(DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0));
 37:   for (i = 0; i < dof; i++) {
 38:     PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "Field %" PetscInt_FMT, i));
 39:     PetscCall(DMDASetFieldName(da, i, fname));
 40:   }

 42:   PetscCall(DMView(da, viewer));
 43:   PetscCall(DMCreateGlobalVector(da, &global));
 44:   PetscCall(DMCreateLocalVector(da, &local));
 45:   PetscCall(DMGetCoordinates(da, &coors));
 46:   PetscCall(DMGetCoordinateDM(da, &dac));

 48:   /* Set values into global vectors */
 49:   PetscCall(DMDAVecGetArrayDOFRead(dac, coors, &xy));
 50:   PetscCall(DMDAVecGetArrayDOF(da, global, &aglobal));
 51:   PetscCall(DMDAGetCorners(da, &xs, &ys, 0, &m, &n, 0));
 52:   for (k = 0; k < dof; k++) {
 53:     for (j = ys; j < ys + n; j++) {
 54:       for (i = xs; i < xs + m; i++) aglobal[j][i][k] = PetscSinScalar(2.0 * PETSC_PI * (k + 1) * xy[j][i][0]);
 55:     }
 56:   }
 57:   PetscCall(DMDAVecRestoreArrayDOF(da, global, &aglobal));
 58:   PetscCall(DMDAVecRestoreArrayDOFRead(dac, coors, &xy));
 59:   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
 60:   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));

 62:   PetscCall(VecSet(global, 0.0));
 63:   PetscCall(DMLocalToGlobalBegin(da, local, INSERT_VALUES, global));
 64:   PetscCall(DMLocalToGlobalEnd(da, local, INSERT_VALUES, global));
 65:   PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
 66:   PetscCall(VecView(global, viewer));

 68:   /* Free memory */
 69:   PetscCall(PetscViewerDestroy(&viewer));
 70:   PetscCall(VecDestroy(&global));
 71:   PetscCall(VecDestroy(&local));
 72:   PetscCall(DMDestroy(&da));
 73:   PetscCall(PetscFinalize());
 74:   return 0;
 75: }

 77: /*TEST

 79:    test:
 80:       args: -dof 2
 81:       filter: grep -v -i Object
 82:       requires: x

 84:    test:
 85:       suffix: 2
 86:       nsize: 2
 87:       args: -dof 2
 88:       filter: grep -v -i Object
 89:       requires: x

 91:    test:
 92:       suffix: 3
 93:       nsize: 3
 94:       args: -dof 2
 95:       filter: grep -v -i Object
 96:       requires: x

 98: TEST*/