Actual source code: ex4.c

  1: static char help[] = "Demonstrates various vector routines for DMDA.\n\n";

  3: /*
  4:   Include "petscpf.h" so that we can use pf functions and "petscdmda.h" so
  5:  we can use the PETSc distributed arrays
  6: */

  8: #include <petscpf.h>
  9: #include <petscdm.h>
 10: #include <petscdmda.h>

 12: PetscErrorCode myfunction(PetscCtx ctx, PetscInt n, const PetscScalar *xy, PetscScalar *u)
 13: {
 14:   PetscFunctionBeginUser;
 15:   for (PetscInt i = 0; i < n; i++) {
 16:     u[2 * i]     = xy[2 * i];
 17:     u[2 * i + 1] = xy[2 * i + 1];
 18:   }
 19:   PetscFunctionReturn(PETSC_SUCCESS);
 20: }

 22: int main(int argc, char **argv)
 23: {
 24:   Vec      u, xy;
 25:   DM       da;
 26:   PetscInt m = 10, n = 10, dof = 2;
 27:   PF       pf;

 29:   PetscFunctionBeginUser;
 30:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 31:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, m, n, PETSC_DECIDE, PETSC_DECIDE, dof, 1, 0, 0, &da));
 32:   PetscCall(DMSetFromOptions(da));
 33:   PetscCall(DMSetUp(da));
 34:   PetscCall(DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0));
 35:   PetscCall(DMCreateGlobalVector(da, &u));
 36:   PetscCall(DMGetCoordinates(da, &xy));

 38:   PetscCall(DMDACreatePF(da, &pf));
 39:   PetscCall(PFSet(pf, myfunction, 0, 0, 0, 0));
 40:   PetscCall(PFSetFromOptions(pf));

 42:   PetscCall(PFApplyVec(pf, xy, u));

 44:   PetscCall(VecView(u, PETSC_VIEWER_DRAW_WORLD));

 46:   /*
 47:      Free work space.  All PETSc objects should be destroyed when they
 48:      are no longer needed.
 49:   */
 50:   PetscCall(VecDestroy(&u));
 51:   PetscCall(PFDestroy(&pf));
 52:   PetscCall(DMDestroy(&da));
 53:   PetscCall(PetscFinalize());
 54:   return 0;
 55: }

 57: /*TEST

 59:    test:
 60:      output_file: output/empty.out

 62: TEST*/