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