Actual source code: ex19.c

  1: static char help[] = "Tests DMDA with variable multiple degrees of freedom per node.\n\n";

  3: /*
  4:    This code only compiles with gcc, since it is not ANSI C
  5: */

  7: #include <petscdm.h>
  8: #include <petscdmda.h>

 10: PetscErrorCode doit(DM da, Vec global)
 11: {
 12:   PetscInt i, j, k, M, N, dof;

 14:   PetscCall(DMDAGetInfo(da, 0, &M, &N, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0));
 15:   {
 16:     struct {
 17:       PetscScalar inside[dof];
 18:     } **mystruct;
 19:     PetscCall(DMDAVecGetArrayRead(da, global, (void *)&mystruct));
 20:     for (i = 0; i < N; i++) {
 21:       for (j = 0; j < M; j++) {
 22:         for (k = 0; k < dof; k++) {
 23:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT " %" PetscInt_FMT " %g\n", i, j, (double)mystruct[i][j].inside[0]));

 25:           mystruct[i][j].inside[1] = 2.1;
 26:         }
 27:       }
 28:     }
 29:     PetscCall(DMDAVecRestoreArrayRead(da, global, (void *)&mystruct));
 30:   }
 31:   PetscFunctionReturn(PETSC_SUCCESS);
 32: }

 34: int main(int argc, char **argv)
 35: {
 36:   PetscInt dof = 2, M = 3, N = 3, m = PETSC_DECIDE, n = PETSC_DECIDE;
 37:   DM       da;
 38:   Vec      global, local;

 40:   PetscFunctionBeginUser;
 41:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 42:   PetscCall(PetscOptionsGetInt(NULL, 0, "-dof", &dof, 0));
 43:   /* Create distributed array and get vectors */
 44:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, dof, 1, NULL, NULL, &da));
 45:   PetscCall(DMSetFromOptions(da));
 46:   PetscCall(DMSetUp(da));
 47:   PetscCall(DMCreateGlobalVector(da, &global));
 48:   PetscCall(DMCreateLocalVector(da, &local));

 50:   PetscCall(doit(da, global));

 52:   PetscCall(VecView(global, 0));

 54:   /* Free memory */
 55:   PetscCall(VecDestroy(&local));
 56:   PetscCall(VecDestroy(&global));
 57:   PetscCall(DMDestroy(&da));
 58:   PetscCall(PetscFinalize());
 59:   return 0;
 60: }