xref: /petsc/src/dm/tests/ex1.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
1 
2 static char help[] = "Tests various DM routines.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 
7 int main(int argc,char **argv)
8 {
9   PetscMPIInt    rank;
10   PetscInt       M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE;
11   DM             da;
12   PetscViewer    viewer;
13   Vec            local,global;
14   PetscScalar    value;
15 
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