xref: /petsc/src/dm/tutorials/ex4.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
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   PetscInt i;
15 
16   PetscFunctionBeginUser;
17   for (i = 0; i < n; i++) {
18     u[2 * i]     = xy[2 * i];
19     u[2 * i + 1] = xy[2 * i + 1];
20   }
21   PetscFunctionReturn(0);
22 }
23 
24 int main(int argc, char **argv) {
25   Vec      u, xy;
26   DM       da;
27   PetscInt m = 10, n = 10, dof = 2;
28   PF       pf;
29 
30   PetscFunctionBeginUser;
31   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
32   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));
33   PetscCall(DMSetFromOptions(da));
34   PetscCall(DMSetUp(da));
35   PetscCall(DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0));
36   PetscCall(DMCreateGlobalVector(da, &u));
37   PetscCall(DMGetCoordinates(da, &xy));
38 
39   PetscCall(DMDACreatePF(da, &pf));
40   PetscCall(PFSet(pf, myfunction, 0, 0, 0, 0));
41   PetscCall(PFSetFromOptions(pf));
42 
43   PetscCall(PFApplyVec(pf, xy, u));
44 
45   PetscCall(VecView(u, PETSC_VIEWER_DRAW_WORLD));
46 
47   /*
48      Free work space.  All PETSc objects should be destroyed when they
49      are no longer needed.
50   */
51   PetscCall(VecDestroy(&u));
52   PetscCall(PFDestroy(&pf));
53   PetscCall(DMDestroy(&da));
54   PetscCall(PetscFinalize());
55   return 0;
56 }
57 
58 /*TEST
59 
60    test:
61 
62 TEST*/
63