xref: /petsc/src/dm/tests/ex37.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 
2 static char help[] = "VecView() with a DMDA1d vector and draw viewer.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 #include <petscao.h>
7 
8 PetscErrorCode apply(void *ctx, PetscInt n, const PetscScalar *x, PetscScalar *y) {
9   PetscInt i;
10 
11   for (i = 0; i < n; i++) {
12     y[3 * i]     = x[i];
13     y[3 * i + 1] = x[i] * x[i];
14     y[3 * i + 2] = x[i] * x[i] * x[i];
15   }
16   return 0;
17 }
18 
19 int main(int argc, char **argv) {
20   DM  da;
21   Vec global;
22   PF  pf;
23 
24   PetscFunctionBeginUser;
25   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
26   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 10, 3, 1, NULL, &da));
27   PetscCall(DMSetFromOptions(da));
28   PetscCall(DMSetUp(da));
29   PetscCall(DMCreateGlobalVector(da, &global));
30   PetscCall(PFCreate(PETSC_COMM_WORLD, 1, 3, &pf));
31   PetscCall(PFSet(pf, apply, NULL, NULL, NULL, NULL));
32   PetscCall(PFApplyVec(pf, NULL, global));
33   PetscCall(PFDestroy(&pf));
34   PetscCall(VecView(global, PETSC_VIEWER_DRAW_WORLD));
35   PetscCall(VecDestroy(&global));
36   PetscCall(DMDestroy(&da));
37   PetscCall(PetscFinalize());
38   return 0;
39 }
40 
41 /*TEST
42 
43    test:
44       nsize: 2
45       requires: x
46 
47 TEST*/
48