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];

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

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

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

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

 48:   /* Set values into global vectors */
 49:   DMDAVecGetArrayDOFRead(dac, coors, &xy);
 50:   DMDAVecGetArrayDOF(da, global, &aglobal);
 51:   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:   DMDAVecRestoreArrayDOF(da, global, &aglobal);
 58:   DMDAVecRestoreArrayDOFRead(dac, coors, &xy);
 59:   DMGlobalToLocalBegin(da, global, INSERT_VALUES, local);
 60:   DMGlobalToLocalEnd(da, global, INSERT_VALUES, local);

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

 68:   /* Free memory */
 69:   PetscViewerDestroy(&viewer);
 70:   VecDestroy(&global);
 71:   VecDestroy(&local);
 72:   DMDestroy(&da);
 73:   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*/