Actual source code: ex4.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: static char help[] = "Demonstrates various vector routines for DMDA.\n\n";

  4: /*T
  5:    Concepts: mathematical functions
  6:    Processors: n
  7: T*/

  9: /*
 10:   Include "petscpf.h" so that we can use pf functions and "petscdmda.h" so
 11:  we can use the PETSc distributed arrays
 12: */

 14: #include <petscpf.h>
 15: #include <petscdm.h>
 16: #include <petscdmda.h>

 20: PetscErrorCode myfunction(void *ctx,PetscInt n,const PetscScalar *xy,PetscScalar *u)
 21: {
 22:   PetscInt i;

 25:   for (i=0; i<n; i++) {
 26:     u[2*i]   = xy[2*i];
 27:     u[2*i+1] = xy[2*i+1];
 28:   }
 29:   return(0);
 30: }

 34: int main(int argc,char **argv)
 35: {
 36:   Vec            u,xy;
 37:   DM             da;
 39:   PetscInt       m = 10, n = 10, dof = 2;
 40:   PF             pf;

 42:   PetscInitialize(&argc,&argv,(char*)0,help);

 44:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,m,n,PETSC_DECIDE,PETSC_DECIDE,dof,1,0,0,&da);
 45:   DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0);
 46:   DMCreateGlobalVector(da,&u);
 47:   DMGetCoordinates(da,&xy);

 49:   DMDACreatePF(da,&pf);
 50:   PFSet(pf,myfunction,0,0,0,0);
 51:   PFSetFromOptions(pf);

 53:   PFApplyVec(pf,xy,u);

 55:   VecView(u,PETSC_VIEWER_DRAW_WORLD);

 57:   /*
 58:      Free work space.  All PETSc objects should be destroyed when they
 59:      are no longer needed.
 60:   */
 61:   VecDestroy(&u);
 62:   PFDestroy(&pf);
 63:   DMDestroy(&da);
 64:   PetscFinalize();
 65:   return 0;
 66: }