1 static char help[] = "Tests saving DMDA vectors to files.\n\n";
2
3 /*
4 ex13.c reads in the DMDA and vector written by this program.
5 */
6
7 #include <petscdm.h>
8 #include <petscdmda.h>
9
main(int argc,char ** argv)10 int main(int argc, char **argv)
11 {
12 PetscMPIInt rank;
13 PetscInt M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE, dof = 1;
14 DM da;
15 Vec local, global, natural;
16 PetscScalar value;
17 PetscViewer bviewer;
18
19 PetscFunctionBeginUser;
20 PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
22 PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
23 PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
24 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
25 PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL));
26
27 /* Create distributed array and get vectors */
28 PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, dof, 1, NULL, NULL, &da));
29 PetscCall(DMSetFromOptions(da));
30 PetscCall(DMSetUp(da));
31 PetscCall(DMCreateGlobalVector(da, &global));
32 PetscCall(DMCreateLocalVector(da, &local));
33
34 value = -3.0;
35 PetscCall(VecSet(global, value));
36 PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
37 PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
38
39 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
40 value = rank + 1;
41 PetscCall(VecScale(local, value));
42 PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
43 PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));
44
45 PetscCall(DMDACreateNaturalVector(da, &natural));
46 PetscCall(DMDAGlobalToNaturalBegin(da, global, INSERT_VALUES, natural));
47 PetscCall(DMDAGlobalToNaturalEnd(da, global, INSERT_VALUES, natural));
48
49 PetscCall(DMDASetFieldName(da, 0, "First field"));
50 /* PetscCall(VecView(global,PETSC_VIEWER_DRAW_WORLD)); */
51
52 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "daoutput", FILE_MODE_WRITE, &bviewer));
53 PetscCall(DMView(da, bviewer));
54 PetscCall(VecView(global, bviewer));
55 PetscCall(PetscViewerDestroy(&bviewer));
56
57 /* Free memory */
58 PetscCall(VecDestroy(&local));
59 PetscCall(VecDestroy(&global));
60 PetscCall(VecDestroy(&natural));
61 PetscCall(DMDestroy(&da));
62 PetscCall(PetscFinalize());
63 return 0;
64 }
65