xref: /petsc/src/dm/tests/ex14.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
1 
2 static char help[] = "Tests saving DMDA vectors to files.\n\n";
3 
4 /*
5     ex13.c reads in the DMDA and vector written by this program.
6 */
7 
8 #include <petscdm.h>
9 #include <petscdmda.h>
10 
11 int main(int argc,char **argv)
12 {
13   PetscMPIInt    rank;
14   PetscInt       M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE, dof = 1;
15   DM             da;
16   Vec            local,global,natural;
17   PetscScalar    value;
18   PetscViewer    bviewer;
19 
20   PetscCall(PetscInitialize(&argc,&argv,(char*)0,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