xref: /petsc/src/dm/tests/ex1.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
1 static char help[] = "Tests various DM routines.\n\n";
2 
3 #include <petscdm.h>
4 #include <petscdmda.h>
5 
6 int main(int argc, char **argv)
7 {
8   PetscMPIInt rank;
9   PetscInt    M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE;
10   DM          da;
11   PetscViewer viewer;
12   Vec         local, global;
13   PetscScalar value;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
17   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 300, 0, 300, 300, &viewer));
18 
19   /* Read options */
20   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
21   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
22   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
23   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
24 
25   /* Create distributed array and get vectors */
26   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, 1, 1, NULL, NULL, &da));
27   PetscCall(DMSetFromOptions(da));
28   PetscCall(DMSetUp(da));
29   PetscCall(DMCreateGlobalVector(da, &global));
30   PetscCall(DMCreateLocalVector(da, &local));
31 
32   value = -3.0;
33   PetscCall(VecSet(global, value));
34   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
35   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
36 
37   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
38   value = rank + 1;
39   PetscCall(VecScale(local, value));
40   PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
41   PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));
42 
43   PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
44   PetscCall(DMView(da, viewer));
45 
46   /* Free memory */
47   PetscCall(PetscViewerDestroy(&viewer));
48   PetscCall(VecDestroy(&local));
49   PetscCall(VecDestroy(&global));
50   PetscCall(DMDestroy(&da));
51   PetscCall(PetscFinalize());
52   return 0;
53 }
54 
55 /*TEST
56 
57    test:
58       nsize: 2
59       args: -nox
60       filter: grep -v -i Object
61 
62    test:
63       suffix: cuda1
64       requires: cuda
65       args: -dm_vec_type cuda -nox
66       filter: grep -v -i Object
67 
68    test:
69       suffix: cuda2
70       nsize: 2
71       requires: cuda
72       args: -dm_vec_type cuda -nox
73       filter: grep -v -i Object
74 
75 TEST*/
76