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