Actual source code: ex4.c

petsc-3.4.5 2014-06-29
  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 <petscdmda.h>

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

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

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

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

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

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

 52:   PFApplyVec(pf,xy,u);

 54:   VecView(u,PETSC_VIEWER_DRAW_WORLD);

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