xref: /petsc/src/dm/tests/ex19.c (revision daa037dfd3c3bec8dc8659548d2b20b07c1dc6de)
1 
2 static char help[] = "Tests DMDA with variable multiple degrees of freedom per node.\n\n";
3 
4 /*
5    This code only compiles with gcc, since it is not ANSI C
6 */
7 
8 #include <petscdm.h>
9 #include <petscdmda.h>
10 
11 PetscErrorCode doit(DM da,Vec global)
12 {
13   PetscInt       i,j,k,M,N,dof;
14 
15   PetscCall(DMDAGetInfo(da,0,&M,&N,0,0,0,0,&dof,0,0,0,0,0));
16   {
17     struct {PetscScalar inside[dof];} **mystruct;
18     PetscCall(DMDAVecGetArrayRead(da,global,(void*) &mystruct));
19     for (i=0; i<N; i++) {
20       for (j=0; j<M; j++) {
21         for (k=0; k<dof; k++) {
22           PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%D %D %g\n",i,j,(double)mystruct[i][j].inside[0]));
23 
24           mystruct[i][j].inside[1] = 2.1;
25         }
26       }
27     }
28     PetscCall(DMDAVecRestoreArrayRead(da,global,(void*) &mystruct));
29   }
30   PetscFunctionReturn(0);
31 }
32 
33 int main(int argc,char **argv)
34 {
35   PetscInt       dof = 2,M = 3,N = 3,m = PETSC_DECIDE,n = PETSC_DECIDE;
36   DM             da;
37   Vec            global,local;
38 
39   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
40   PetscCall(PetscOptionsGetInt(NULL,0,"-dof",&dof,0));
41   /* Create distributed array and get vectors */
42   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,dof,1,NULL,NULL,&da));
43   PetscCall(DMSetFromOptions(da));
44   PetscCall(DMSetUp(da));
45   PetscCall(DMCreateGlobalVector(da,&global));
46   PetscCall(DMCreateLocalVector(da,&local));
47 
48   PetscCall(doit(da,global));
49 
50   PetscCall(VecView(global,0));
51 
52   /* Free memory */
53   PetscCall(VecDestroy(&local));
54   PetscCall(VecDestroy(&global));
55   PetscCall(DMDestroy(&da));
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59