xref: /petsc/src/dm/tutorials/ex4.c (revision b1a884e745839216bfc44936cd132bbea6039dbc)
1 
2 static char help[] = "Demonstrates various vector routines for DMDA.\n\n";
3 
4 /*
5   Include "petscpf.h" so that we can use pf functions and "petscdmda.h" so
6  we can use the PETSc distributed arrays
7 */
8 
9 #include <petscpf.h>
10 #include <petscdm.h>
11 #include <petscdmda.h>
12 
13 PetscErrorCode myfunction(void *ctx,PetscInt n,const PetscScalar *xy,PetscScalar *u)
14 {
15   PetscInt i;
16 
17   PetscFunctionBeginUser;
18   for (i=0; i<n; i++) {
19     u[2*i]   = xy[2*i];
20     u[2*i+1] = xy[2*i+1];
21   }
22   PetscFunctionReturn(0);
23 }
24 
25 int main(int argc,char **argv)
26 {
27   Vec            u,xy;
28   DM             da;
29   PetscInt       m = 10, n = 10, dof = 2;
30   PF             pf;
31 
32   PetscFunctionBeginUser;
33   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
34   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));
35   PetscCall(DMSetFromOptions(da));
36   PetscCall(DMSetUp(da));
37   PetscCall(DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0));
38   PetscCall(DMCreateGlobalVector(da,&u));
39   PetscCall(DMGetCoordinates(da,&xy));
40 
41   PetscCall(DMDACreatePF(da,&pf));
42   PetscCall(PFSet(pf,myfunction,0,0,0,0));
43   PetscCall(PFSetFromOptions(pf));
44 
45   PetscCall(PFApplyVec(pf,xy,u));
46 
47   PetscCall(VecView(u,PETSC_VIEWER_DRAW_WORLD));
48 
49   /*
50      Free work space.  All PETSc objects should be destroyed when they
51      are no longer needed.
52   */
53   PetscCall(VecDestroy(&u));
54   PetscCall(PFDestroy(&pf));
55   PetscCall(DMDestroy(&da));
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59 
60 /*TEST
61 
62    test:
63 
64 TEST*/
65